blob: 39c05f9b7d37732274f722c60c5a69e2144140de [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Steve Sakoman516799f2010-07-15 12:53:42 -07002/*
3 * (C) Copyright 2010
4 * Texas Instruments, <www.ti.com>
Steve Sakoman516799f2010-07-15 12:53:42 -07005 */
6#include <config.h>
Simon Glassc05ed002020-05-10 11:40:11 -06007#include <linux/delay.h>
Simon Glass1e94b462023-09-14 18:21:46 -06008#include <linux/printk.h>
Steve Sakoman516799f2010-07-15 12:53:42 -07009
10#include <twl6030.h>
11
Oleg Koshelievfc889502013-10-08 15:49:55 +030012static struct twl6030_data *twl;
13
14static struct twl6030_data twl6030_info = {
15 .chip_type = chip_TWL6030,
16 .adc_rbase = GPCH0_LSB,
17 .adc_ctrl = CTRL_P2,
18 .adc_enable = CTRL_P2_SP2,
19 .vbat_mult = TWL6030_VBAT_MULT,
20 .vbat_shift = TWL6030_VBAT_SHIFT,
21};
22
Oleg Kosheliev340e6c82013-10-08 15:49:56 +030023static struct twl6030_data twl6032_info = {
24 .chip_type = chip_TWL6032,
25 .adc_rbase = TWL6032_GPCH0_LSB,
26 .adc_ctrl = TWL6032_CTRL_P1,
27 .adc_enable = CTRL_P1_SP1,
28 .vbat_mult = TWL6032_VBAT_MULT,
29 .vbat_shift = TWL6032_VBAT_SHIFT,
30};
31
Paul Kocialkowskid6a20422016-02-27 19:19:12 +010032
Balaji T K3e664f62010-11-25 16:22:04 +053033static int twl6030_gpadc_read_channel(u8 channel_no)
34{
35 u8 lsb = 0;
36 u8 msb = 0;
37 int ret = 0;
38
Nishanth Menon345ef202013-03-26 05:20:51 +000039 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC,
Oleg Koshelievfc889502013-10-08 15:49:55 +030040 twl->adc_rbase + channel_no * 2, &lsb);
Balaji T K3e664f62010-11-25 16:22:04 +053041 if (ret)
42 return ret;
43
Nishanth Menon345ef202013-03-26 05:20:51 +000044 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC,
Oleg Koshelievfc889502013-10-08 15:49:55 +030045 twl->adc_rbase + 1 + channel_no * 2, &msb);
Balaji T K3e664f62010-11-25 16:22:04 +053046 if (ret)
47 return ret;
48
49 return (msb << 8) | lsb;
50}
51
52static int twl6030_gpadc_sw2_trigger(void)
53{
54 u8 val;
55 int ret = 0;
56
Oleg Koshelievfc889502013-10-08 15:49:55 +030057 ret = twl6030_i2c_write_u8(TWL6030_CHIP_ADC,
58 twl->adc_ctrl, twl->adc_enable);
Balaji T K3e664f62010-11-25 16:22:04 +053059 if (ret)
60 return ret;
61
62 /* Waiting until the SW1 conversion ends*/
63 val = CTRL_P2_BUSY;
64
65 while (!((val & CTRL_P2_EOCP2) && (!(val & CTRL_P2_BUSY)))) {
Oleg Koshelievfc889502013-10-08 15:49:55 +030066 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC,
67 twl->adc_ctrl, &val);
Balaji T K3e664f62010-11-25 16:22:04 +053068 if (ret)
69 return ret;
70 udelay(1000);
71 }
72
73 return 0;
74}
75
Paul Kocialkowskid6a20422016-02-27 19:19:12 +010076void twl6030_power_off(void)
77{
78 twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_PHOENIX_DEV_ON,
79 TWL6030_PHOENIX_APP_DEVOFF | TWL6030_PHOENIX_CON_DEVOFF |
80 TWL6030_PHOENIX_MOD_DEVOFF);
81}
82
Balaji T K3e664f62010-11-25 16:22:04 +053083void twl6030_stop_usb_charging(void)
84{
Nishanth Menon345ef202013-03-26 05:20:51 +000085 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CONTROLLER_CTRL1, 0);
Balaji T K3e664f62010-11-25 16:22:04 +053086
87 return;
88}
89
Steve Sakoman516799f2010-07-15 12:53:42 -070090void twl6030_start_usb_charging(void)
91{
Nishanth Menon345ef202013-03-26 05:20:51 +000092 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
93 CHARGERUSB_VICHRG, CHARGERUSB_VICHRG_1500);
94 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
95 CHARGERUSB_CINLIMIT, CHARGERUSB_CIN_LIMIT_NONE);
96 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
97 CONTROLLER_INT_MASK, MBAT_TEMP);
98 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
99 CHARGERUSB_INT_MASK, MASK_MCHARGERUSB_THMREG);
100 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
101 CHARGERUSB_VOREG, CHARGERUSB_VOREG_4P0);
102 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
103 CHARGERUSB_CTRL2, CHARGERUSB_CTRL2_VITERM_400);
104 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CHARGERUSB_CTRL1, TERM);
Steve Sakoman516799f2010-07-15 12:53:42 -0700105 /* Enable USB charging */
Nishanth Menon345ef202013-03-26 05:20:51 +0000106 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
107 CONTROLLER_CTRL1, CONTROLLER_CTRL1_EN_CHARGER);
Steve Sakoman516799f2010-07-15 12:53:42 -0700108 return;
109}
110
Balaji T K3e664f62010-11-25 16:22:04 +0530111int twl6030_get_battery_current(void)
112{
113 int battery_current = 0;
114 u8 msb = 0;
115 u8 lsb = 0;
116
Nishanth Menon345ef202013-03-26 05:20:51 +0000117 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, FG_REG_11, &msb);
118 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, FG_REG_10, &lsb);
Balaji T K3e664f62010-11-25 16:22:04 +0530119 battery_current = ((msb << 8) | lsb);
120
121 /* convert 10 bit signed number to 16 bit signed number */
122 if (battery_current >= 0x2000)
123 battery_current = (battery_current - 0x4000);
124
125 battery_current = battery_current * 3000 / 4096;
126 printf("Battery Current: %d mA\n", battery_current);
127
128 return battery_current;
129}
130
131int twl6030_get_battery_voltage(void)
132{
133 int battery_volt = 0;
134 int ret = 0;
Oleg Kosheliev340e6c82013-10-08 15:49:56 +0300135 u8 vbatch;
136
137 if (twl->chip_type == chip_TWL6030) {
138 vbatch = TWL6030_GPADC_VBAT_CHNL;
139 } else {
140 ret = twl6030_i2c_write_u8(TWL6030_CHIP_ADC,
141 TWL6032_GPSELECT_ISB,
142 TWL6032_GPADC_VBAT_CHNL);
143 if (ret)
144 return ret;
145 vbatch = 0;
146 }
Balaji T K3e664f62010-11-25 16:22:04 +0530147
148 /* Start GPADC SW conversion */
149 ret = twl6030_gpadc_sw2_trigger();
150 if (ret) {
151 printf("Failed to convert battery voltage\n");
152 return ret;
153 }
154
155 /* measure Vbat voltage */
Oleg Kosheliev340e6c82013-10-08 15:49:56 +0300156 battery_volt = twl6030_gpadc_read_channel(vbatch);
Balaji T K3e664f62010-11-25 16:22:04 +0530157 if (battery_volt < 0) {
158 printf("Failed to read battery voltage\n");
159 return ret;
160 }
Oleg Koshelievfc889502013-10-08 15:49:55 +0300161 battery_volt = (battery_volt * twl->vbat_mult) >> twl->vbat_shift;
Balaji T K3e664f62010-11-25 16:22:04 +0530162 printf("Battery Voltage: %d mV\n", battery_volt);
163
164 return battery_volt;
165}
166
Steve Sakoman516799f2010-07-15 12:53:42 -0700167void twl6030_init_battery_charging(void)
168{
Oleg Kosheliev340e6c82013-10-08 15:49:56 +0300169 u8 val = 0;
Balaji T K3e664f62010-11-25 16:22:04 +0530170 int battery_volt = 0;
171 int ret = 0;
172
Oleg Kosheliev340e6c82013-10-08 15:49:56 +0300173 ret = twl6030_i2c_read_u8(TWL6030_CHIP_USB, USB_PRODUCT_ID_LSB, &val);
174 if (ret) {
175 puts("twl6030_init_battery_charging(): could not determine chip!\n");
176 return;
177 }
178 if (val == 0x30) {
179 twl = &twl6030_info;
180 } else if (val == 0x32) {
181 twl = &twl6032_info;
182 } else {
183 puts("twl6030_init_battery_charging(): unsupported chip type\n");
184 return;
185 }
Oleg Koshelievfc889502013-10-08 15:49:55 +0300186
Balaji T K3e664f62010-11-25 16:22:04 +0530187 /* Enable VBAT measurement */
Oleg Kosheliev340e6c82013-10-08 15:49:56 +0300188 if (twl->chip_type == chip_TWL6030) {
189 twl6030_i2c_write_u8(TWL6030_CHIP_PM, MISC1, VBAT_MEAS);
190 twl6030_i2c_write_u8(TWL6030_CHIP_ADC,
191 TWL6030_GPADC_CTRL,
192 GPADC_CTRL_SCALER_DIV4);
193 } else {
194 twl6030_i2c_write_u8(TWL6030_CHIP_ADC,
195 TWL6032_GPADC_CTRL2,
196 GPADC_CTRL2_CH18_SCALER_EN);
197 }
Balaji T K3e664f62010-11-25 16:22:04 +0530198
199 /* Enable GPADC module */
Nishanth Menon345ef202013-03-26 05:20:51 +0000200 ret = twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, TOGGLE1, FGS | GPADCS);
Balaji T K3e664f62010-11-25 16:22:04 +0530201 if (ret) {
202 printf("Failed to enable GPADC\n");
203 return;
204 }
205
206 battery_volt = twl6030_get_battery_voltage();
207 if (battery_volt < 0)
208 return;
209
210 if (battery_volt < 3000)
211 printf("Main battery voltage too low!\n");
212
213 /* Check for the presence of USB charger */
Oleg Kosheliev340e6c82013-10-08 15:49:56 +0300214 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, CONTROLLER_STAT1, &val);
Balaji T K3e664f62010-11-25 16:22:04 +0530215
216 /* check for battery presence indirectly via Fuel gauge */
Oleg Kosheliev340e6c82013-10-08 15:49:56 +0300217 if ((val & VBUS_DET) && (battery_volt < 3300))
Balaji T K3e664f62010-11-25 16:22:04 +0530218 twl6030_start_usb_charging();
219
Steve Sakoman516799f2010-07-15 12:53:42 -0700220 return;
221}
222
Paul Kocialkowskia85362f2016-02-27 19:18:51 +0100223void twl6030_power_mmc_init(int dev_index)
Balaji T K14fa2dd2011-09-08 06:34:57 +0000224{
Paul Kocialkowskid7b6a752016-02-27 19:18:50 +0100225 u8 value = 0;
226
Paul Kocialkowskia85362f2016-02-27 19:18:51 +0100227 if (dev_index == 0) {
228 /* 3.0V voltage output for VMMC */
229 twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VMMC_CFG_VOLTAGE,
230 TWL6030_CFG_VOLTAGE_30);
Paul Kocialkowskic5dbae72016-02-27 19:18:49 +0100231
Paul Kocialkowskia85362f2016-02-27 19:18:51 +0100232 /* Enable P1 output for VMMC */
233 twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VMMC_CFG_STATE,
234 TWL6030_CFG_STATE_P1 | TWL6030_CFG_STATE_ON);
Paul Kocialkowskia85362f2016-02-27 19:18:51 +0100235 } else if (dev_index == 1) {
Nicolae Rosia28a3a432016-10-13 13:47:53 +0300236 twl6030_i2c_read_u8(TWL6030_CHIP_PM, TWL6030_PH_STS_BOOT,
237 &value);
Paul Kocialkowskia85362f2016-02-27 19:18:51 +0100238 /* BOOT2 indicates 1.8V/2.8V VAUX1 for eMMC */
239 if (value & TWL6030_PH_STS_BOOT2) {
240 /* 1.8V voltage output for VAUX1 */
241 twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VAUX1_CFG_VOLTAGE,
242 TWL6030_CFG_VOLTAGE_18);
243 } else {
244 /* 2.8V voltage output for VAUX1 */
245 twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VAUX1_CFG_VOLTAGE,
246 TWL6030_CFG_VOLTAGE_28);
247 }
Paul Kocialkowskid7b6a752016-02-27 19:18:50 +0100248
Paul Kocialkowskia85362f2016-02-27 19:18:51 +0100249 /* Enable P1 output for VAUX */
250 twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VAUX1_CFG_STATE,
251 TWL6030_CFG_STATE_P1 | TWL6030_CFG_STATE_ON);
Paul Kocialkowskid7b6a752016-02-27 19:18:50 +0100252 }
Balaji T K14fa2dd2011-09-08 06:34:57 +0000253}
254
Steve Sakoman516799f2010-07-15 12:53:42 -0700255void twl6030_usb_device_settings()
256{
Paul Kocialkowskic5dbae72016-02-27 19:18:49 +0100257 u8 value = 0;
Steve Sakoman516799f2010-07-15 12:53:42 -0700258
Paul Kocialkowski0343f712016-02-27 19:19:03 +0100259 /* 3.3V voltage output for VUSB */
260 twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VUSB_CFG_VOLTAGE,
261 TWL6030_CFG_VOLTAGE_33);
262
Paul Kocialkowskic5dbae72016-02-27 19:18:49 +0100263 /* Enable P1 output for VUSB */
264 twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VUSB_CFG_STATE,
265 TWL6030_CFG_STATE_P1 | TWL6030_CFG_STATE_ON);
Steve Sakoman516799f2010-07-15 12:53:42 -0700266
Paul Kocialkowskic5dbae72016-02-27 19:18:49 +0100267 /* Select the input supply for VUSB regulator */
268 twl6030_i2c_read_u8(TWL6030_CHIP_PM, TWL6030_MISC2, &value);
269 value |= TWL6030_MISC2_VUSB_IN_VSYS;
Paul Kocialkowski6313c652016-02-27 19:19:04 +0100270 value &= ~TWL6030_MISC2_VUSB_IN_PMID;
Paul Kocialkowskic5dbae72016-02-27 19:18:49 +0100271 twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_MISC2, value);
Steve Sakoman516799f2010-07-15 12:53:42 -0700272}
Jean-Jacques Hiblotfb1b7712018-12-07 14:50:46 +0100273
Igor Opaniuk2147a162021-02-09 13:52:45 +0200274#if CONFIG_IS_ENABLED(DM_I2C)
Jean-Jacques Hiblotfb1b7712018-12-07 14:50:46 +0100275int twl6030_i2c_write_u8(u8 chip_no, u8 reg, u8 val)
276{
277 struct udevice *dev;
278 int ret;
279
280 ret = i2c_get_chip_for_busnum(0, chip_no, 1, &dev);
281 if (ret) {
282 pr_err("unable to get I2C bus. ret %d\n", ret);
283 return ret;
284 }
285 ret = dm_i2c_reg_write(dev, reg, val);
286 if (ret) {
287 pr_err("writing to twl6030 failed. ret %d\n", ret);
288 return ret;
289 }
290 return 0;
291}
292
293int twl6030_i2c_read_u8(u8 chip_no, u8 reg, u8 *valp)
294{
295 struct udevice *dev;
296 int ret;
297
298 ret = i2c_get_chip_for_busnum(0, chip_no, 1, &dev);
299 if (ret) {
300 pr_err("unable to get I2C bus. ret %d\n", ret);
301 return ret;
302 }
303 ret = dm_i2c_reg_read(dev, reg);
304 if (ret < 0) {
305 pr_err("reading from twl6030 failed. ret %d\n", ret);
306 return ret;
307 }
308 *valp = (u8)ret;
309 return 0;
310}
311#endif