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