Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Steve Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010 |
| 4 | * Texas Instruments, <www.ti.com> |
Steve Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 5 | */ |
| 6 | #include <config.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 7 | #include <linux/delay.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 8 | #include <linux/printk.h> |
Steve Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 9 | |
| 10 | #include <twl6030.h> |
| 11 | |
Oleg Kosheliev | fc88950 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 12 | static struct twl6030_data *twl; |
| 13 | |
| 14 | static 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 Kosheliev | 340e6c8 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 23 | static 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 Kocialkowski | d6a2042 | 2016-02-27 19:19:12 +0100 | [diff] [blame] | 32 | |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 33 | static int twl6030_gpadc_read_channel(u8 channel_no) |
| 34 | { |
| 35 | u8 lsb = 0; |
| 36 | u8 msb = 0; |
| 37 | int ret = 0; |
| 38 | |
Nishanth Menon | 345ef20 | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 39 | ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC, |
Oleg Kosheliev | fc88950 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 40 | twl->adc_rbase + channel_no * 2, &lsb); |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 41 | if (ret) |
| 42 | return ret; |
| 43 | |
Nishanth Menon | 345ef20 | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 44 | ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC, |
Oleg Kosheliev | fc88950 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 45 | twl->adc_rbase + 1 + channel_no * 2, &msb); |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 46 | if (ret) |
| 47 | return ret; |
| 48 | |
| 49 | return (msb << 8) | lsb; |
| 50 | } |
| 51 | |
| 52 | static int twl6030_gpadc_sw2_trigger(void) |
| 53 | { |
| 54 | u8 val; |
| 55 | int ret = 0; |
| 56 | |
Oleg Kosheliev | fc88950 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 57 | ret = twl6030_i2c_write_u8(TWL6030_CHIP_ADC, |
| 58 | twl->adc_ctrl, twl->adc_enable); |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 59 | 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 Kosheliev | fc88950 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 66 | ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC, |
| 67 | twl->adc_ctrl, &val); |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 68 | if (ret) |
| 69 | return ret; |
| 70 | udelay(1000); |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Paul Kocialkowski | d6a2042 | 2016-02-27 19:19:12 +0100 | [diff] [blame] | 76 | void 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 K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 83 | void twl6030_stop_usb_charging(void) |
| 84 | { |
Nishanth Menon | 345ef20 | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 85 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CONTROLLER_CTRL1, 0); |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 86 | |
| 87 | return; |
| 88 | } |
| 89 | |
Steve Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 90 | void twl6030_start_usb_charging(void) |
| 91 | { |
Nishanth Menon | 345ef20 | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 92 | 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 Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 105 | /* Enable USB charging */ |
Nishanth Menon | 345ef20 | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 106 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, |
| 107 | CONTROLLER_CTRL1, CONTROLLER_CTRL1_EN_CHARGER); |
Steve Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 111 | int twl6030_get_battery_current(void) |
| 112 | { |
| 113 | int battery_current = 0; |
| 114 | u8 msb = 0; |
| 115 | u8 lsb = 0; |
| 116 | |
Nishanth Menon | 345ef20 | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 117 | 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 K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 119 | 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 | |
| 131 | int twl6030_get_battery_voltage(void) |
| 132 | { |
| 133 | int battery_volt = 0; |
| 134 | int ret = 0; |
Oleg Kosheliev | 340e6c8 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 135 | 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 K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 147 | |
| 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 Kosheliev | 340e6c8 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 156 | battery_volt = twl6030_gpadc_read_channel(vbatch); |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 157 | if (battery_volt < 0) { |
| 158 | printf("Failed to read battery voltage\n"); |
| 159 | return ret; |
| 160 | } |
Oleg Kosheliev | fc88950 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 161 | battery_volt = (battery_volt * twl->vbat_mult) >> twl->vbat_shift; |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 162 | printf("Battery Voltage: %d mV\n", battery_volt); |
| 163 | |
| 164 | return battery_volt; |
| 165 | } |
| 166 | |
Steve Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 167 | void twl6030_init_battery_charging(void) |
| 168 | { |
Oleg Kosheliev | 340e6c8 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 169 | u8 val = 0; |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 170 | int battery_volt = 0; |
| 171 | int ret = 0; |
| 172 | |
Oleg Kosheliev | 340e6c8 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 173 | 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 Kosheliev | fc88950 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 186 | |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 187 | /* Enable VBAT measurement */ |
Oleg Kosheliev | 340e6c8 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 188 | 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 K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 198 | |
| 199 | /* Enable GPADC module */ |
Nishanth Menon | 345ef20 | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 200 | ret = twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, TOGGLE1, FGS | GPADCS); |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 201 | 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 Kosheliev | 340e6c8 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 214 | twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, CONTROLLER_STAT1, &val); |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 215 | |
| 216 | /* check for battery presence indirectly via Fuel gauge */ |
Oleg Kosheliev | 340e6c8 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 217 | if ((val & VBUS_DET) && (battery_volt < 3300)) |
Balaji T K | 3e664f6 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 218 | twl6030_start_usb_charging(); |
| 219 | |
Steve Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
Paul Kocialkowski | a85362f | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 223 | void twl6030_power_mmc_init(int dev_index) |
Balaji T K | 14fa2dd | 2011-09-08 06:34:57 +0000 | [diff] [blame] | 224 | { |
Paul Kocialkowski | d7b6a75 | 2016-02-27 19:18:50 +0100 | [diff] [blame] | 225 | u8 value = 0; |
| 226 | |
Paul Kocialkowski | a85362f | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 227 | 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 Kocialkowski | c5dbae7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 231 | |
Paul Kocialkowski | a85362f | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 232 | /* 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 Kocialkowski | a85362f | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 235 | } else if (dev_index == 1) { |
Nicolae Rosia | 28a3a43 | 2016-10-13 13:47:53 +0300 | [diff] [blame] | 236 | twl6030_i2c_read_u8(TWL6030_CHIP_PM, TWL6030_PH_STS_BOOT, |
| 237 | &value); |
Paul Kocialkowski | a85362f | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 238 | /* 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 Kocialkowski | d7b6a75 | 2016-02-27 19:18:50 +0100 | [diff] [blame] | 248 | |
Paul Kocialkowski | a85362f | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 249 | /* 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 Kocialkowski | d7b6a75 | 2016-02-27 19:18:50 +0100 | [diff] [blame] | 252 | } |
Balaji T K | 14fa2dd | 2011-09-08 06:34:57 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Steve Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 255 | void twl6030_usb_device_settings() |
| 256 | { |
Paul Kocialkowski | c5dbae7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 257 | u8 value = 0; |
Steve Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 258 | |
Paul Kocialkowski | 0343f71 | 2016-02-27 19:19:03 +0100 | [diff] [blame] | 259 | /* 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 Kocialkowski | c5dbae7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 263 | /* 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 Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 266 | |
Paul Kocialkowski | c5dbae7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 267 | /* 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 Kocialkowski | 6313c65 | 2016-02-27 19:19:04 +0100 | [diff] [blame] | 270 | value &= ~TWL6030_MISC2_VUSB_IN_PMID; |
Paul Kocialkowski | c5dbae7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 271 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_MISC2, value); |
Steve Sakoman | 516799f | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 272 | } |
Jean-Jacques Hiblot | fb1b771 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 273 | |
Igor Opaniuk | 2147a16 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 274 | #if CONFIG_IS_ENABLED(DM_I2C) |
Jean-Jacques Hiblot | fb1b771 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 275 | int 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 | |
| 293 | int 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 |