Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010 |
| 4 | * Texas Instruments, <www.ti.com> |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 5 | */ |
| 6 | #include <config.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame^] | 7 | #include <linux/delay.h> |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 8 | |
| 9 | #include <twl6030.h> |
| 10 | |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 11 | static struct twl6030_data *twl; |
| 12 | |
| 13 | static struct twl6030_data twl6030_info = { |
| 14 | .chip_type = chip_TWL6030, |
| 15 | .adc_rbase = GPCH0_LSB, |
| 16 | .adc_ctrl = CTRL_P2, |
| 17 | .adc_enable = CTRL_P2_SP2, |
| 18 | .vbat_mult = TWL6030_VBAT_MULT, |
| 19 | .vbat_shift = TWL6030_VBAT_SHIFT, |
| 20 | }; |
| 21 | |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 22 | static struct twl6030_data twl6032_info = { |
| 23 | .chip_type = chip_TWL6032, |
| 24 | .adc_rbase = TWL6032_GPCH0_LSB, |
| 25 | .adc_ctrl = TWL6032_CTRL_P1, |
| 26 | .adc_enable = CTRL_P1_SP1, |
| 27 | .vbat_mult = TWL6032_VBAT_MULT, |
| 28 | .vbat_shift = TWL6032_VBAT_SHIFT, |
| 29 | }; |
| 30 | |
Paul Kocialkowski | c76b97a | 2016-02-27 19:19:12 +0100 | [diff] [blame] | 31 | |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 32 | static int twl6030_gpadc_read_channel(u8 channel_no) |
| 33 | { |
| 34 | u8 lsb = 0; |
| 35 | u8 msb = 0; |
| 36 | int ret = 0; |
| 37 | |
Nishanth Menon | 9deff3c | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 38 | ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC, |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 39 | twl->adc_rbase + channel_no * 2, &lsb); |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 40 | if (ret) |
| 41 | return ret; |
| 42 | |
Nishanth Menon | 9deff3c | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 43 | ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC, |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 44 | twl->adc_rbase + 1 + channel_no * 2, &msb); |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 45 | if (ret) |
| 46 | return ret; |
| 47 | |
| 48 | return (msb << 8) | lsb; |
| 49 | } |
| 50 | |
| 51 | static int twl6030_gpadc_sw2_trigger(void) |
| 52 | { |
| 53 | u8 val; |
| 54 | int ret = 0; |
| 55 | |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 56 | ret = twl6030_i2c_write_u8(TWL6030_CHIP_ADC, |
| 57 | twl->adc_ctrl, twl->adc_enable); |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 58 | if (ret) |
| 59 | return ret; |
| 60 | |
| 61 | /* Waiting until the SW1 conversion ends*/ |
| 62 | val = CTRL_P2_BUSY; |
| 63 | |
| 64 | while (!((val & CTRL_P2_EOCP2) && (!(val & CTRL_P2_BUSY)))) { |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 65 | ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC, |
| 66 | twl->adc_ctrl, &val); |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 67 | if (ret) |
| 68 | return ret; |
| 69 | udelay(1000); |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Paul Kocialkowski | c76b97a | 2016-02-27 19:19:12 +0100 | [diff] [blame] | 75 | void twl6030_power_off(void) |
| 76 | { |
| 77 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_PHOENIX_DEV_ON, |
| 78 | TWL6030_PHOENIX_APP_DEVOFF | TWL6030_PHOENIX_CON_DEVOFF | |
| 79 | TWL6030_PHOENIX_MOD_DEVOFF); |
| 80 | } |
| 81 | |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 82 | void twl6030_stop_usb_charging(void) |
| 83 | { |
Nishanth Menon | 9deff3c | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 84 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CONTROLLER_CTRL1, 0); |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 85 | |
| 86 | return; |
| 87 | } |
| 88 | |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 89 | void twl6030_start_usb_charging(void) |
| 90 | { |
Nishanth Menon | 9deff3c | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 91 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, |
| 92 | CHARGERUSB_VICHRG, CHARGERUSB_VICHRG_1500); |
| 93 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, |
| 94 | CHARGERUSB_CINLIMIT, CHARGERUSB_CIN_LIMIT_NONE); |
| 95 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, |
| 96 | CONTROLLER_INT_MASK, MBAT_TEMP); |
| 97 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, |
| 98 | CHARGERUSB_INT_MASK, MASK_MCHARGERUSB_THMREG); |
| 99 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, |
| 100 | CHARGERUSB_VOREG, CHARGERUSB_VOREG_4P0); |
| 101 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, |
| 102 | CHARGERUSB_CTRL2, CHARGERUSB_CTRL2_VITERM_400); |
| 103 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CHARGERUSB_CTRL1, TERM); |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 104 | /* Enable USB charging */ |
Nishanth Menon | 9deff3c | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 105 | twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, |
| 106 | CONTROLLER_CTRL1, CONTROLLER_CTRL1_EN_CHARGER); |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 110 | int twl6030_get_battery_current(void) |
| 111 | { |
| 112 | int battery_current = 0; |
| 113 | u8 msb = 0; |
| 114 | u8 lsb = 0; |
| 115 | |
Nishanth Menon | 9deff3c | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 116 | twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, FG_REG_11, &msb); |
| 117 | twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, FG_REG_10, &lsb); |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 118 | battery_current = ((msb << 8) | lsb); |
| 119 | |
| 120 | /* convert 10 bit signed number to 16 bit signed number */ |
| 121 | if (battery_current >= 0x2000) |
| 122 | battery_current = (battery_current - 0x4000); |
| 123 | |
| 124 | battery_current = battery_current * 3000 / 4096; |
| 125 | printf("Battery Current: %d mA\n", battery_current); |
| 126 | |
| 127 | return battery_current; |
| 128 | } |
| 129 | |
| 130 | int twl6030_get_battery_voltage(void) |
| 131 | { |
| 132 | int battery_volt = 0; |
| 133 | int ret = 0; |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 134 | u8 vbatch; |
| 135 | |
| 136 | if (twl->chip_type == chip_TWL6030) { |
| 137 | vbatch = TWL6030_GPADC_VBAT_CHNL; |
| 138 | } else { |
| 139 | ret = twl6030_i2c_write_u8(TWL6030_CHIP_ADC, |
| 140 | TWL6032_GPSELECT_ISB, |
| 141 | TWL6032_GPADC_VBAT_CHNL); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | vbatch = 0; |
| 145 | } |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 146 | |
| 147 | /* Start GPADC SW conversion */ |
| 148 | ret = twl6030_gpadc_sw2_trigger(); |
| 149 | if (ret) { |
| 150 | printf("Failed to convert battery voltage\n"); |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | /* measure Vbat voltage */ |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 155 | battery_volt = twl6030_gpadc_read_channel(vbatch); |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 156 | if (battery_volt < 0) { |
| 157 | printf("Failed to read battery voltage\n"); |
| 158 | return ret; |
| 159 | } |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 160 | battery_volt = (battery_volt * twl->vbat_mult) >> twl->vbat_shift; |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 161 | printf("Battery Voltage: %d mV\n", battery_volt); |
| 162 | |
| 163 | return battery_volt; |
| 164 | } |
| 165 | |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 166 | void twl6030_init_battery_charging(void) |
| 167 | { |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 168 | u8 val = 0; |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 169 | int battery_volt = 0; |
| 170 | int ret = 0; |
| 171 | |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 172 | ret = twl6030_i2c_read_u8(TWL6030_CHIP_USB, USB_PRODUCT_ID_LSB, &val); |
| 173 | if (ret) { |
| 174 | puts("twl6030_init_battery_charging(): could not determine chip!\n"); |
| 175 | return; |
| 176 | } |
| 177 | if (val == 0x30) { |
| 178 | twl = &twl6030_info; |
| 179 | } else if (val == 0x32) { |
| 180 | twl = &twl6032_info; |
| 181 | } else { |
| 182 | puts("twl6030_init_battery_charging(): unsupported chip type\n"); |
| 183 | return; |
| 184 | } |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 185 | |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 186 | /* Enable VBAT measurement */ |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 187 | if (twl->chip_type == chip_TWL6030) { |
| 188 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, MISC1, VBAT_MEAS); |
| 189 | twl6030_i2c_write_u8(TWL6030_CHIP_ADC, |
| 190 | TWL6030_GPADC_CTRL, |
| 191 | GPADC_CTRL_SCALER_DIV4); |
| 192 | } else { |
| 193 | twl6030_i2c_write_u8(TWL6030_CHIP_ADC, |
| 194 | TWL6032_GPADC_CTRL2, |
| 195 | GPADC_CTRL2_CH18_SCALER_EN); |
| 196 | } |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 197 | |
| 198 | /* Enable GPADC module */ |
Nishanth Menon | 9deff3c | 2013-03-26 05:20:51 +0000 | [diff] [blame] | 199 | ret = twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, TOGGLE1, FGS | GPADCS); |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 200 | if (ret) { |
| 201 | printf("Failed to enable GPADC\n"); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | battery_volt = twl6030_get_battery_voltage(); |
| 206 | if (battery_volt < 0) |
| 207 | return; |
| 208 | |
| 209 | if (battery_volt < 3000) |
| 210 | printf("Main battery voltage too low!\n"); |
| 211 | |
| 212 | /* Check for the presence of USB charger */ |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 213 | twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, CONTROLLER_STAT1, &val); |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 214 | |
| 215 | /* check for battery presence indirectly via Fuel gauge */ |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 216 | if ((val & VBUS_DET) && (battery_volt < 3300)) |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 217 | twl6030_start_usb_charging(); |
| 218 | |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
Paul Kocialkowski | b649748 | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 222 | void twl6030_power_mmc_init(int dev_index) |
Balaji T K | f843d33 | 2011-09-08 06:34:57 +0000 | [diff] [blame] | 223 | { |
Paul Kocialkowski | 1db349f | 2016-02-27 19:18:50 +0100 | [diff] [blame] | 224 | u8 value = 0; |
| 225 | |
Paul Kocialkowski | b649748 | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 226 | if (dev_index == 0) { |
| 227 | /* 3.0V voltage output for VMMC */ |
| 228 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VMMC_CFG_VOLTAGE, |
| 229 | TWL6030_CFG_VOLTAGE_30); |
Paul Kocialkowski | b8421a7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 230 | |
Paul Kocialkowski | b649748 | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 231 | /* Enable P1 output for VMMC */ |
| 232 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VMMC_CFG_STATE, |
| 233 | TWL6030_CFG_STATE_P1 | TWL6030_CFG_STATE_ON); |
Paul Kocialkowski | b649748 | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 234 | } else if (dev_index == 1) { |
Nicolae Rosia | 8bc958a | 2016-10-13 13:47:53 +0300 | [diff] [blame] | 235 | twl6030_i2c_read_u8(TWL6030_CHIP_PM, TWL6030_PH_STS_BOOT, |
| 236 | &value); |
Paul Kocialkowski | b649748 | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 237 | /* BOOT2 indicates 1.8V/2.8V VAUX1 for eMMC */ |
| 238 | if (value & TWL6030_PH_STS_BOOT2) { |
| 239 | /* 1.8V voltage output for VAUX1 */ |
| 240 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VAUX1_CFG_VOLTAGE, |
| 241 | TWL6030_CFG_VOLTAGE_18); |
| 242 | } else { |
| 243 | /* 2.8V voltage output for VAUX1 */ |
| 244 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VAUX1_CFG_VOLTAGE, |
| 245 | TWL6030_CFG_VOLTAGE_28); |
| 246 | } |
Paul Kocialkowski | 1db349f | 2016-02-27 19:18:50 +0100 | [diff] [blame] | 247 | |
Paul Kocialkowski | b649748 | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 248 | /* Enable P1 output for VAUX */ |
| 249 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VAUX1_CFG_STATE, |
| 250 | TWL6030_CFG_STATE_P1 | TWL6030_CFG_STATE_ON); |
Paul Kocialkowski | 1db349f | 2016-02-27 19:18:50 +0100 | [diff] [blame] | 251 | } |
Balaji T K | f843d33 | 2011-09-08 06:34:57 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 254 | void twl6030_usb_device_settings() |
| 255 | { |
Paul Kocialkowski | b8421a7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 256 | u8 value = 0; |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 257 | |
Paul Kocialkowski | 3f3b12b | 2016-02-27 19:19:03 +0100 | [diff] [blame] | 258 | /* 3.3V voltage output for VUSB */ |
| 259 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VUSB_CFG_VOLTAGE, |
| 260 | TWL6030_CFG_VOLTAGE_33); |
| 261 | |
Paul Kocialkowski | b8421a7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 262 | /* Enable P1 output for VUSB */ |
| 263 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_VUSB_CFG_STATE, |
| 264 | TWL6030_CFG_STATE_P1 | TWL6030_CFG_STATE_ON); |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 265 | |
Paul Kocialkowski | b8421a7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 266 | /* Select the input supply for VUSB regulator */ |
| 267 | twl6030_i2c_read_u8(TWL6030_CHIP_PM, TWL6030_MISC2, &value); |
| 268 | value |= TWL6030_MISC2_VUSB_IN_VSYS; |
Paul Kocialkowski | e1545d9 | 2016-02-27 19:19:04 +0100 | [diff] [blame] | 269 | value &= ~TWL6030_MISC2_VUSB_IN_PMID; |
Paul Kocialkowski | b8421a7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 270 | twl6030_i2c_write_u8(TWL6030_CHIP_PM, TWL6030_MISC2, value); |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 271 | } |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 272 | |
| 273 | #ifdef CONFIG_DM_I2C |
| 274 | int twl6030_i2c_write_u8(u8 chip_no, u8 reg, u8 val) |
| 275 | { |
| 276 | struct udevice *dev; |
| 277 | int ret; |
| 278 | |
| 279 | ret = i2c_get_chip_for_busnum(0, chip_no, 1, &dev); |
| 280 | if (ret) { |
| 281 | pr_err("unable to get I2C bus. ret %d\n", ret); |
| 282 | return ret; |
| 283 | } |
| 284 | ret = dm_i2c_reg_write(dev, reg, val); |
| 285 | if (ret) { |
| 286 | pr_err("writing to twl6030 failed. ret %d\n", ret); |
| 287 | return ret; |
| 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | int twl6030_i2c_read_u8(u8 chip_no, u8 reg, u8 *valp) |
| 293 | { |
| 294 | struct udevice *dev; |
| 295 | int ret; |
| 296 | |
| 297 | ret = i2c_get_chip_for_busnum(0, chip_no, 1, &dev); |
| 298 | if (ret) { |
| 299 | pr_err("unable to get I2C bus. ret %d\n", ret); |
| 300 | return ret; |
| 301 | } |
| 302 | ret = dm_i2c_reg_read(dev, reg); |
| 303 | if (ret < 0) { |
| 304 | pr_err("reading from twl6030 failed. ret %d\n", ret); |
| 305 | return ret; |
| 306 | } |
| 307 | *valp = (u8)ret; |
| 308 | return 0; |
| 309 | } |
| 310 | #endif |