blob: 0858b60e06d3f4d5ff1d31f344e09e8752cd0e5c [file] [log] [blame]
Steve Sakoman516799f2010-07-15 12:53:42 -07001/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Steve Sakoman516799f2010-07-15 12:53:42 -07006 */
7#include <config.h>
8#ifdef CONFIG_TWL6030_POWER
9
10#include <twl6030.h>
11
Balaji T K3e664f62010-11-25 16:22:04 +053012static int twl6030_gpadc_read_channel(u8 channel_no)
13{
14 u8 lsb = 0;
15 u8 msb = 0;
16 int ret = 0;
17
Nishanth Menon345ef202013-03-26 05:20:51 +000018 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC,
19 GPCH0_LSB + channel_no * 2, &lsb);
Balaji T K3e664f62010-11-25 16:22:04 +053020 if (ret)
21 return ret;
22
Nishanth Menon345ef202013-03-26 05:20:51 +000023 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC,
24 GPCH0_MSB + channel_no * 2, &msb);
Balaji T K3e664f62010-11-25 16:22:04 +053025 if (ret)
26 return ret;
27
28 return (msb << 8) | lsb;
29}
30
31static int twl6030_gpadc_sw2_trigger(void)
32{
33 u8 val;
34 int ret = 0;
35
Nishanth Menon345ef202013-03-26 05:20:51 +000036 ret = twl6030_i2c_write_u8(TWL6030_CHIP_ADC, CTRL_P2, CTRL_P2_SP2);
Balaji T K3e664f62010-11-25 16:22:04 +053037 if (ret)
38 return ret;
39
40 /* Waiting until the SW1 conversion ends*/
41 val = CTRL_P2_BUSY;
42
43 while (!((val & CTRL_P2_EOCP2) && (!(val & CTRL_P2_BUSY)))) {
Nishanth Menon345ef202013-03-26 05:20:51 +000044 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC, CTRL_P2, &val);
Balaji T K3e664f62010-11-25 16:22:04 +053045 if (ret)
46 return ret;
47 udelay(1000);
48 }
49
50 return 0;
51}
52
53void twl6030_stop_usb_charging(void)
54{
Nishanth Menon345ef202013-03-26 05:20:51 +000055 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CONTROLLER_CTRL1, 0);
Balaji T K3e664f62010-11-25 16:22:04 +053056
57 return;
58}
59
Steve Sakoman516799f2010-07-15 12:53:42 -070060void twl6030_start_usb_charging(void)
61{
Nishanth Menon345ef202013-03-26 05:20:51 +000062 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
63 CHARGERUSB_VICHRG, CHARGERUSB_VICHRG_1500);
64 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
65 CHARGERUSB_CINLIMIT, CHARGERUSB_CIN_LIMIT_NONE);
66 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
67 CONTROLLER_INT_MASK, MBAT_TEMP);
68 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
69 CHARGERUSB_INT_MASK, MASK_MCHARGERUSB_THMREG);
70 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
71 CHARGERUSB_VOREG, CHARGERUSB_VOREG_4P0);
72 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
73 CHARGERUSB_CTRL2, CHARGERUSB_CTRL2_VITERM_400);
74 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CHARGERUSB_CTRL1, TERM);
Steve Sakoman516799f2010-07-15 12:53:42 -070075 /* Enable USB charging */
Nishanth Menon345ef202013-03-26 05:20:51 +000076 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
77 CONTROLLER_CTRL1, CONTROLLER_CTRL1_EN_CHARGER);
Steve Sakoman516799f2010-07-15 12:53:42 -070078 return;
79}
80
Balaji T K3e664f62010-11-25 16:22:04 +053081int twl6030_get_battery_current(void)
82{
83 int battery_current = 0;
84 u8 msb = 0;
85 u8 lsb = 0;
86
Nishanth Menon345ef202013-03-26 05:20:51 +000087 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, FG_REG_11, &msb);
88 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, FG_REG_10, &lsb);
Balaji T K3e664f62010-11-25 16:22:04 +053089 battery_current = ((msb << 8) | lsb);
90
91 /* convert 10 bit signed number to 16 bit signed number */
92 if (battery_current >= 0x2000)
93 battery_current = (battery_current - 0x4000);
94
95 battery_current = battery_current * 3000 / 4096;
96 printf("Battery Current: %d mA\n", battery_current);
97
98 return battery_current;
99}
100
101int twl6030_get_battery_voltage(void)
102{
103 int battery_volt = 0;
104 int ret = 0;
105
106 /* Start GPADC SW conversion */
107 ret = twl6030_gpadc_sw2_trigger();
108 if (ret) {
109 printf("Failed to convert battery voltage\n");
110 return ret;
111 }
112
113 /* measure Vbat voltage */
114 battery_volt = twl6030_gpadc_read_channel(7);
115 if (battery_volt < 0) {
116 printf("Failed to read battery voltage\n");
117 return ret;
118 }
119 battery_volt = (battery_volt * 25 * 1000) >> (10 + 2);
120 printf("Battery Voltage: %d mV\n", battery_volt);
121
122 return battery_volt;
123}
124
Steve Sakoman516799f2010-07-15 12:53:42 -0700125void twl6030_init_battery_charging(void)
126{
Balaji T K3e664f62010-11-25 16:22:04 +0530127 u8 stat1 = 0;
128 int battery_volt = 0;
129 int ret = 0;
130
131 /* Enable VBAT measurement */
Nishanth Menon345ef202013-03-26 05:20:51 +0000132 twl6030_i2c_write_u8(TWL6030_CHIP_PM, MISC1, VBAT_MEAS);
Balaji T K3e664f62010-11-25 16:22:04 +0530133
134 /* Enable GPADC module */
Nishanth Menon345ef202013-03-26 05:20:51 +0000135 ret = twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, TOGGLE1, FGS | GPADCS);
Balaji T K3e664f62010-11-25 16:22:04 +0530136 if (ret) {
137 printf("Failed to enable GPADC\n");
138 return;
139 }
140
141 battery_volt = twl6030_get_battery_voltage();
142 if (battery_volt < 0)
143 return;
144
145 if (battery_volt < 3000)
146 printf("Main battery voltage too low!\n");
147
148 /* Check for the presence of USB charger */
Nishanth Menon345ef202013-03-26 05:20:51 +0000149 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, CONTROLLER_STAT1, &stat1);
Balaji T K3e664f62010-11-25 16:22:04 +0530150
151 /* check for battery presence indirectly via Fuel gauge */
152 if ((stat1 & VBUS_DET) && (battery_volt < 3300))
153 twl6030_start_usb_charging();
154
Steve Sakoman516799f2010-07-15 12:53:42 -0700155 return;
156}
157
Balaji T K14fa2dd2011-09-08 06:34:57 +0000158void twl6030_power_mmc_init()
159{
160 /* set voltage to 3.0 and turnon for APP */
Nishanth Menon345ef202013-03-26 05:20:51 +0000161 twl6030_i2c_write_u8(TWL6030_CHIP_PM, VMMC_CFG_VOLTATE, 0x15);
162 twl6030_i2c_write_u8(TWL6030_CHIP_PM, VMMC_CFG_STATE, 0x21);
Balaji T K14fa2dd2011-09-08 06:34:57 +0000163}
164
Steve Sakoman516799f2010-07-15 12:53:42 -0700165void twl6030_usb_device_settings()
166{
167 u8 data = 0;
168
169 /* Select APP Group and set state to ON */
Nishanth Menon345ef202013-03-26 05:20:51 +0000170 twl6030_i2c_write_u8(TWL6030_CHIP_PM, VUSB_CFG_STATE, 0x21);
Steve Sakoman516799f2010-07-15 12:53:42 -0700171
Nishanth Menon345ef202013-03-26 05:20:51 +0000172 twl6030_i2c_read_u8(TWL6030_CHIP_PM, MISC2, &data);
Steve Sakoman516799f2010-07-15 12:53:42 -0700173 data |= 0x10;
174
175 /* Select the input supply for VBUS regulator */
Nishanth Menon345ef202013-03-26 05:20:51 +0000176 twl6030_i2c_write_u8(TWL6030_CHIP_PM, MISC2, data);
Steve Sakoman516799f2010-07-15 12:53:42 -0700177}
178#endif