Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1 | /* |
| 2 | * TWL4030/TPS65950 BCI (Battery Charger Interface) driver |
| 3 | * |
| 4 | * Copyright (C) 2010 Gražvydas Ignotas <notasas@gmail.com> |
| 5 | * |
| 6 | * based on twl4030_bci_battery.c by TI |
| 7 | * Copyright (C) 2008 Texas Instruments, Inc. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
Kishon Vijay Abraham I | ded017e | 2012-06-26 17:40:32 +0530 | [diff] [blame] | 18 | #include <linux/err.h> |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/i2c/twl.h> |
| 22 | #include <linux/power_supply.h> |
| 23 | #include <linux/notifier.h> |
| 24 | #include <linux/usb/otg.h> |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 25 | #include <linux/regulator/machine.h> |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 26 | |
| 27 | #define TWL4030_BCIMSTATEC 0x02 |
| 28 | #define TWL4030_BCIICHG 0x08 |
| 29 | #define TWL4030_BCIVAC 0x0a |
| 30 | #define TWL4030_BCIVBUS 0x0c |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 31 | #define TWL4030_BCIMFSTS3 0x0F |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 32 | #define TWL4030_BCIMFSTS4 0x10 |
| 33 | #define TWL4030_BCICTL1 0x23 |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 34 | #define TWL4030_BB_CFG 0x12 |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 35 | |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 36 | #define TWL4030_BCIMFSTS1 0x01 |
| 37 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 38 | #define TWL4030_BCIAUTOWEN BIT(5) |
| 39 | #define TWL4030_CONFIG_DONE BIT(4) |
| 40 | #define TWL4030_BCIAUTOUSB BIT(1) |
| 41 | #define TWL4030_BCIAUTOAC BIT(0) |
| 42 | #define TWL4030_CGAIN BIT(5) |
| 43 | #define TWL4030_USBFASTMCHG BIT(2) |
| 44 | #define TWL4030_STS_VBUS BIT(7) |
| 45 | #define TWL4030_STS_USB_ID BIT(2) |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 46 | #define TWL4030_BBCHEN BIT(4) |
Il Han | 94b05ca | 2012-08-17 16:49:43 +0900 | [diff] [blame] | 47 | #define TWL4030_BBSEL_MASK 0x0c |
| 48 | #define TWL4030_BBSEL_2V5 0x00 |
| 49 | #define TWL4030_BBSEL_3V0 0x04 |
| 50 | #define TWL4030_BBSEL_3V1 0x08 |
| 51 | #define TWL4030_BBSEL_3V2 0x0c |
| 52 | #define TWL4030_BBISEL_MASK 0x03 |
| 53 | #define TWL4030_BBISEL_25uA 0x00 |
| 54 | #define TWL4030_BBISEL_150uA 0x01 |
| 55 | #define TWL4030_BBISEL_500uA 0x02 |
| 56 | #define TWL4030_BBISEL_1000uA 0x03 |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 57 | |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 58 | #define TWL4030_BATSTSPCHG BIT(2) |
| 59 | #define TWL4030_BATSTSMCHG BIT(6) |
| 60 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 61 | /* BCI interrupts */ |
| 62 | #define TWL4030_WOVF BIT(0) /* Watchdog overflow */ |
| 63 | #define TWL4030_TMOVF BIT(1) /* Timer overflow */ |
| 64 | #define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */ |
| 65 | #define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */ |
| 66 | #define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */ |
| 67 | #define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */ |
| 68 | #define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */ |
| 69 | #define TWL4030_BATSTS BIT(7) /* Battery status */ |
| 70 | |
| 71 | #define TWL4030_VBATLVL BIT(0) /* VBAT level */ |
| 72 | #define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */ |
| 73 | #define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */ |
| 74 | #define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */ |
| 75 | |
| 76 | #define TWL4030_MSTATEC_USB BIT(4) |
| 77 | #define TWL4030_MSTATEC_AC BIT(5) |
| 78 | #define TWL4030_MSTATEC_MASK 0x0f |
| 79 | #define TWL4030_MSTATEC_QUICK1 0x02 |
| 80 | #define TWL4030_MSTATEC_QUICK7 0x07 |
| 81 | #define TWL4030_MSTATEC_COMPLETE1 0x0b |
| 82 | #define TWL4030_MSTATEC_COMPLETE4 0x0e |
| 83 | |
| 84 | static bool allow_usb; |
Grazvydas Ignotas | 28d48f0 | 2011-06-03 14:43:11 +0300 | [diff] [blame] | 85 | module_param(allow_usb, bool, 0644); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 86 | MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current"); |
| 87 | |
| 88 | struct twl4030_bci { |
| 89 | struct device *dev; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 90 | struct power_supply *ac; |
| 91 | struct power_supply *usb; |
Heikki Krogerus | 8675381 | 2012-02-13 13:24:02 +0200 | [diff] [blame] | 92 | struct usb_phy *transceiver; |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 93 | struct notifier_block usb_nb; |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 94 | struct work_struct work; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 95 | int irq_chg; |
| 96 | int irq_bci; |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 97 | struct regulator *usb_reg; |
| 98 | int usb_enabled; |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 99 | |
| 100 | unsigned long event; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | /* |
| 104 | * clear and set bits on an given register on a given module |
| 105 | */ |
| 106 | static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg) |
| 107 | { |
| 108 | u8 val = 0; |
| 109 | int ret; |
| 110 | |
| 111 | ret = twl_i2c_read_u8(mod_no, &val, reg); |
| 112 | if (ret) |
| 113 | return ret; |
| 114 | |
| 115 | val &= ~clear; |
| 116 | val |= set; |
| 117 | |
| 118 | return twl_i2c_write_u8(mod_no, val, reg); |
| 119 | } |
| 120 | |
| 121 | static int twl4030_bci_read(u8 reg, u8 *val) |
| 122 | { |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 123 | return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, val, reg); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int twl4030_clear_set_boot_bci(u8 clear, u8 set) |
| 127 | { |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 128 | return twl4030_clear_set(TWL_MODULE_PM_MASTER, clear, |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 129 | TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set, |
| 130 | TWL4030_PM_MASTER_BOOT_BCI); |
| 131 | } |
| 132 | |
| 133 | static int twl4030bci_read_adc_val(u8 reg) |
| 134 | { |
| 135 | int ret, temp; |
| 136 | u8 val; |
| 137 | |
| 138 | /* read MSB */ |
| 139 | ret = twl4030_bci_read(reg + 1, &val); |
| 140 | if (ret) |
| 141 | return ret; |
| 142 | |
| 143 | temp = (int)(val & 0x03) << 8; |
| 144 | |
| 145 | /* read LSB */ |
| 146 | ret = twl4030_bci_read(reg, &val); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | |
| 150 | return temp | val; |
| 151 | } |
| 152 | |
| 153 | /* |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 154 | * Check if Battery Pack was present |
| 155 | */ |
| 156 | static int twl4030_is_battery_present(struct twl4030_bci *bci) |
| 157 | { |
| 158 | int ret; |
| 159 | u8 val = 0; |
| 160 | |
| 161 | /* Battery presence in Main charge? */ |
| 162 | ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, TWL4030_BCIMFSTS3); |
| 163 | if (ret) |
| 164 | return ret; |
| 165 | if (val & TWL4030_BATSTSMCHG) |
| 166 | return 0; |
| 167 | |
| 168 | /* |
| 169 | * OK, It could be that bootloader did not enable main charger, |
| 170 | * pre-charge is h/w auto. So, Battery presence in Pre-charge? |
| 171 | */ |
| 172 | ret = twl_i2c_read_u8(TWL4030_MODULE_PRECHARGE, &val, |
| 173 | TWL4030_BCIMFSTS1); |
| 174 | if (ret) |
| 175 | return ret; |
| 176 | if (val & TWL4030_BATSTSPCHG) |
| 177 | return 0; |
| 178 | |
| 179 | return -ENODEV; |
| 180 | } |
| 181 | |
| 182 | /* |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 183 | * Check if VBUS power is present |
| 184 | */ |
| 185 | static int twl4030_bci_have_vbus(struct twl4030_bci *bci) |
| 186 | { |
| 187 | int ret; |
| 188 | u8 hwsts; |
| 189 | |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 190 | ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &hwsts, |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 191 | TWL4030_PM_MASTER_STS_HW_CONDITIONS); |
| 192 | if (ret < 0) |
| 193 | return 0; |
| 194 | |
| 195 | dev_dbg(bci->dev, "check_vbus: HW_CONDITIONS %02x\n", hwsts); |
| 196 | |
| 197 | /* in case we also have STS_USB_ID, VBUS is driven by TWL itself */ |
| 198 | if ((hwsts & TWL4030_STS_VBUS) && !(hwsts & TWL4030_STS_USB_ID)) |
| 199 | return 1; |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /* |
NeilBrown | 9c645d2 | 2012-05-09 07:40:39 +1000 | [diff] [blame] | 205 | * Enable/Disable USB Charge functionality. |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 206 | */ |
| 207 | static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable) |
| 208 | { |
| 209 | int ret; |
| 210 | |
| 211 | if (enable) { |
NeilBrown | 9c645d2 | 2012-05-09 07:40:39 +1000 | [diff] [blame] | 212 | /* Check for USB charger connected */ |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 213 | if (!twl4030_bci_have_vbus(bci)) |
| 214 | return -ENODEV; |
| 215 | |
| 216 | /* |
| 217 | * Until we can find out what current the device can provide, |
| 218 | * require a module param to enable USB charging. |
| 219 | */ |
| 220 | if (!allow_usb) { |
| 221 | dev_warn(bci->dev, "USB charging is disabled.\n"); |
| 222 | return -EACCES; |
| 223 | } |
| 224 | |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 225 | /* Need to keep regulator on */ |
| 226 | if (!bci->usb_enabled) { |
Peter Ujfalusi | 697be36 | 2013-08-21 11:31:37 +0300 | [diff] [blame] | 227 | ret = regulator_enable(bci->usb_reg); |
| 228 | if (ret) { |
| 229 | dev_err(bci->dev, |
| 230 | "Failed to enable regulator\n"); |
| 231 | return ret; |
| 232 | } |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 233 | bci->usb_enabled = 1; |
| 234 | } |
| 235 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 236 | /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */ |
| 237 | ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB); |
| 238 | if (ret < 0) |
| 239 | return ret; |
| 240 | |
| 241 | /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */ |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 242 | ret = twl4030_clear_set(TWL_MODULE_MAIN_CHARGE, 0, |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 243 | TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4); |
| 244 | } else { |
| 245 | ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0); |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 246 | if (bci->usb_enabled) { |
| 247 | regulator_disable(bci->usb_reg); |
| 248 | bci->usb_enabled = 0; |
| 249 | } |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Enable/Disable AC Charge funtionality. |
| 257 | */ |
| 258 | static int twl4030_charger_enable_ac(bool enable) |
| 259 | { |
| 260 | int ret; |
| 261 | |
| 262 | if (enable) |
| 263 | ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC); |
| 264 | else |
| 265 | ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0); |
| 266 | |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | /* |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 271 | * Enable/Disable charging of Backup Battery. |
| 272 | */ |
| 273 | static int twl4030_charger_enable_backup(int uvolt, int uamp) |
| 274 | { |
| 275 | int ret; |
| 276 | u8 flags; |
| 277 | |
| 278 | if (uvolt < 2500000 || |
| 279 | uamp < 25) { |
| 280 | /* disable charging of backup battery */ |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 281 | ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER, |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 282 | TWL4030_BBCHEN, 0, TWL4030_BB_CFG); |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | flags = TWL4030_BBCHEN; |
| 287 | if (uvolt >= 3200000) |
| 288 | flags |= TWL4030_BBSEL_3V2; |
| 289 | else if (uvolt >= 3100000) |
| 290 | flags |= TWL4030_BBSEL_3V1; |
| 291 | else if (uvolt >= 3000000) |
| 292 | flags |= TWL4030_BBSEL_3V0; |
| 293 | else |
| 294 | flags |= TWL4030_BBSEL_2V5; |
| 295 | |
| 296 | if (uamp >= 1000) |
| 297 | flags |= TWL4030_BBISEL_1000uA; |
| 298 | else if (uamp >= 500) |
| 299 | flags |= TWL4030_BBISEL_500uA; |
| 300 | else if (uamp >= 150) |
| 301 | flags |= TWL4030_BBISEL_150uA; |
| 302 | else |
| 303 | flags |= TWL4030_BBISEL_25uA; |
| 304 | |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 305 | ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER, |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 306 | TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK, |
| 307 | flags, |
| 308 | TWL4030_BB_CFG); |
| 309 | |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | /* |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 314 | * TWL4030 CHG_PRES (AC charger presence) events |
| 315 | */ |
| 316 | static irqreturn_t twl4030_charger_interrupt(int irq, void *arg) |
| 317 | { |
| 318 | struct twl4030_bci *bci = arg; |
| 319 | |
| 320 | dev_dbg(bci->dev, "CHG_PRES irq\n"); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 321 | power_supply_changed(bci->ac); |
| 322 | power_supply_changed(bci->usb); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 323 | |
| 324 | return IRQ_HANDLED; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * TWL4030 BCI monitoring events |
| 329 | */ |
| 330 | static irqreturn_t twl4030_bci_interrupt(int irq, void *arg) |
| 331 | { |
| 332 | struct twl4030_bci *bci = arg; |
| 333 | u8 irqs1, irqs2; |
| 334 | int ret; |
| 335 | |
| 336 | ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1, |
| 337 | TWL4030_INTERRUPTS_BCIISR1A); |
| 338 | if (ret < 0) |
| 339 | return IRQ_HANDLED; |
| 340 | |
| 341 | ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2, |
| 342 | TWL4030_INTERRUPTS_BCIISR2A); |
| 343 | if (ret < 0) |
| 344 | return IRQ_HANDLED; |
| 345 | |
| 346 | dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1); |
| 347 | |
| 348 | if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) { |
| 349 | /* charger state change, inform the core */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 350 | power_supply_changed(bci->ac); |
| 351 | power_supply_changed(bci->usb); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* various monitoring events, for now we just log them here */ |
| 355 | if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1)) |
| 356 | dev_warn(bci->dev, "battery temperature out of range\n"); |
| 357 | |
| 358 | if (irqs1 & TWL4030_BATSTS) |
| 359 | dev_crit(bci->dev, "battery disconnected\n"); |
| 360 | |
| 361 | if (irqs2 & TWL4030_VBATOV) |
| 362 | dev_crit(bci->dev, "VBAT overvoltage\n"); |
| 363 | |
| 364 | if (irqs2 & TWL4030_VBUSOV) |
| 365 | dev_crit(bci->dev, "VBUS overvoltage\n"); |
| 366 | |
| 367 | if (irqs2 & TWL4030_ACCHGOV) |
| 368 | dev_crit(bci->dev, "Ac charger overvoltage\n"); |
| 369 | |
| 370 | return IRQ_HANDLED; |
| 371 | } |
| 372 | |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 373 | static void twl4030_bci_usb_work(struct work_struct *data) |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 374 | { |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 375 | struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 376 | |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 377 | switch (bci->event) { |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 378 | case USB_EVENT_VBUS: |
| 379 | case USB_EVENT_CHARGER: |
| 380 | twl4030_charger_enable_usb(bci, true); |
| 381 | break; |
| 382 | case USB_EVENT_NONE: |
| 383 | twl4030_charger_enable_usb(bci, false); |
| 384 | break; |
| 385 | } |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val, |
| 389 | void *priv) |
| 390 | { |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 391 | struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, usb_nb); |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 392 | |
| 393 | dev_dbg(bci->dev, "OTG notify %lu\n", val); |
| 394 | |
| 395 | bci->event = val; |
| 396 | schedule_work(&bci->work); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 397 | |
| 398 | return NOTIFY_OK; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * TI provided formulas: |
| 403 | * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85 |
| 404 | * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7 |
| 405 | * Here we use integer approximation of: |
| 406 | * CGAIN == 0: val * 1.6618 - 0.85 |
| 407 | * CGAIN == 1: (val * 1.6618 - 0.85) * 2 |
| 408 | */ |
| 409 | static int twl4030_charger_get_current(void) |
| 410 | { |
| 411 | int curr; |
| 412 | int ret; |
| 413 | u8 bcictl1; |
| 414 | |
| 415 | curr = twl4030bci_read_adc_val(TWL4030_BCIICHG); |
| 416 | if (curr < 0) |
| 417 | return curr; |
| 418 | |
| 419 | ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1); |
| 420 | if (ret) |
| 421 | return ret; |
| 422 | |
| 423 | ret = (curr * 16618 - 850 * 10000) / 10; |
| 424 | if (bcictl1 & TWL4030_CGAIN) |
| 425 | ret *= 2; |
| 426 | |
| 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Returns the main charge FSM state |
| 432 | * Or < 0 on failure. |
| 433 | */ |
| 434 | static int twl4030bci_state(struct twl4030_bci *bci) |
| 435 | { |
| 436 | int ret; |
| 437 | u8 state; |
| 438 | |
| 439 | ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state); |
| 440 | if (ret) { |
| 441 | pr_err("twl4030_bci: error reading BCIMSTATEC\n"); |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | dev_dbg(bci->dev, "state: %02x\n", state); |
| 446 | |
| 447 | return state; |
| 448 | } |
| 449 | |
| 450 | static int twl4030_bci_state_to_status(int state) |
| 451 | { |
| 452 | state &= TWL4030_MSTATEC_MASK; |
| 453 | if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7) |
| 454 | return POWER_SUPPLY_STATUS_CHARGING; |
| 455 | else if (TWL4030_MSTATEC_COMPLETE1 <= state && |
| 456 | state <= TWL4030_MSTATEC_COMPLETE4) |
| 457 | return POWER_SUPPLY_STATUS_FULL; |
| 458 | else |
| 459 | return POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 460 | } |
| 461 | |
| 462 | static int twl4030_bci_get_property(struct power_supply *psy, |
| 463 | enum power_supply_property psp, |
| 464 | union power_supply_propval *val) |
| 465 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 466 | struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 467 | int is_charging; |
| 468 | int state; |
| 469 | int ret; |
| 470 | |
| 471 | state = twl4030bci_state(bci); |
| 472 | if (state < 0) |
| 473 | return state; |
| 474 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 475 | if (psy->desc->type == POWER_SUPPLY_TYPE_USB) |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 476 | is_charging = state & TWL4030_MSTATEC_USB; |
| 477 | else |
| 478 | is_charging = state & TWL4030_MSTATEC_AC; |
| 479 | |
| 480 | switch (psp) { |
| 481 | case POWER_SUPPLY_PROP_STATUS: |
| 482 | if (is_charging) |
| 483 | val->intval = twl4030_bci_state_to_status(state); |
| 484 | else |
| 485 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 486 | break; |
| 487 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 488 | /* charging must be active for meaningful result */ |
| 489 | if (!is_charging) |
| 490 | return -ENODATA; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 491 | if (psy->desc->type == POWER_SUPPLY_TYPE_USB) { |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 492 | ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS); |
| 493 | if (ret < 0) |
| 494 | return ret; |
| 495 | /* BCIVBUS uses ADCIN8, 7/1023 V/step */ |
| 496 | val->intval = ret * 6843; |
| 497 | } else { |
| 498 | ret = twl4030bci_read_adc_val(TWL4030_BCIVAC); |
| 499 | if (ret < 0) |
| 500 | return ret; |
| 501 | /* BCIVAC uses ADCIN11, 10/1023 V/step */ |
| 502 | val->intval = ret * 9775; |
| 503 | } |
| 504 | break; |
| 505 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 506 | if (!is_charging) |
| 507 | return -ENODATA; |
| 508 | /* current measurement is shared between AC and USB */ |
| 509 | ret = twl4030_charger_get_current(); |
| 510 | if (ret < 0) |
| 511 | return ret; |
| 512 | val->intval = ret; |
| 513 | break; |
| 514 | case POWER_SUPPLY_PROP_ONLINE: |
| 515 | val->intval = is_charging && |
| 516 | twl4030_bci_state_to_status(state) != |
| 517 | POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 518 | break; |
| 519 | default: |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static enum power_supply_property twl4030_charger_props[] = { |
| 527 | POWER_SUPPLY_PROP_STATUS, |
| 528 | POWER_SUPPLY_PROP_ONLINE, |
| 529 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 530 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 531 | }; |
| 532 | |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 533 | #ifdef CONFIG_OF |
| 534 | static const struct twl4030_bci_platform_data * |
| 535 | twl4030_bci_parse_dt(struct device *dev) |
| 536 | { |
| 537 | struct device_node *np = dev->of_node; |
| 538 | struct twl4030_bci_platform_data *pdata; |
| 539 | u32 num; |
| 540 | |
| 541 | if (!np) |
| 542 | return NULL; |
| 543 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 544 | if (!pdata) |
| 545 | return pdata; |
| 546 | |
| 547 | if (of_property_read_u32(np, "ti,bb-uvolt", &num) == 0) |
| 548 | pdata->bb_uvolt = num; |
| 549 | if (of_property_read_u32(np, "ti,bb-uamp", &num) == 0) |
| 550 | pdata->bb_uamp = num; |
| 551 | return pdata; |
| 552 | } |
| 553 | #else |
| 554 | static inline const struct twl4030_bci_platform_data * |
| 555 | twl4030_bci_parse_dt(struct device *dev) |
| 556 | { |
| 557 | return NULL; |
| 558 | } |
| 559 | #endif |
| 560 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 561 | static const struct power_supply_desc twl4030_bci_ac_desc = { |
| 562 | .name = "twl4030_ac", |
| 563 | .type = POWER_SUPPLY_TYPE_MAINS, |
| 564 | .properties = twl4030_charger_props, |
| 565 | .num_properties = ARRAY_SIZE(twl4030_charger_props), |
| 566 | .get_property = twl4030_bci_get_property, |
| 567 | }; |
| 568 | |
| 569 | static const struct power_supply_desc twl4030_bci_usb_desc = { |
| 570 | .name = "twl4030_usb", |
| 571 | .type = POWER_SUPPLY_TYPE_USB, |
| 572 | .properties = twl4030_charger_props, |
| 573 | .num_properties = ARRAY_SIZE(twl4030_charger_props), |
| 574 | .get_property = twl4030_bci_get_property, |
| 575 | }; |
| 576 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 577 | static int __init twl4030_bci_probe(struct platform_device *pdev) |
| 578 | { |
| 579 | struct twl4030_bci *bci; |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 580 | const struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 581 | int ret; |
Grazvydas Ignotas | 28d48f0 | 2011-06-03 14:43:11 +0300 | [diff] [blame] | 582 | u32 reg; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 583 | |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 584 | bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 585 | if (bci == NULL) |
| 586 | return -ENOMEM; |
| 587 | |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 588 | if (!pdata) |
| 589 | pdata = twl4030_bci_parse_dt(&pdev->dev); |
| 590 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 591 | bci->dev = &pdev->dev; |
| 592 | bci->irq_chg = platform_get_irq(pdev, 0); |
| 593 | bci->irq_bci = platform_get_irq(pdev, 1); |
| 594 | |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 595 | /* Only proceed further *IF* battery is physically present */ |
| 596 | ret = twl4030_is_battery_present(bci); |
| 597 | if (ret) { |
| 598 | dev_crit(&pdev->dev, "Battery was not detected:%d\n", ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 599 | return ret; |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 600 | } |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 601 | |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 602 | platform_set_drvdata(pdev, bci); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 603 | |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 604 | bci->ac = devm_power_supply_register(&pdev->dev, &twl4030_bci_ac_desc, |
| 605 | NULL); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 606 | if (IS_ERR(bci->ac)) { |
| 607 | ret = PTR_ERR(bci->ac); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 608 | dev_err(&pdev->dev, "failed to register ac: %d\n", ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 609 | return ret; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 610 | } |
| 611 | |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 612 | bci->usb_reg = regulator_get(bci->dev, "bci3v1"); |
| 613 | |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 614 | bci->usb = devm_power_supply_register(&pdev->dev, &twl4030_bci_usb_desc, |
| 615 | NULL); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 616 | if (IS_ERR(bci->usb)) { |
| 617 | ret = PTR_ERR(bci->usb); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 618 | dev_err(&pdev->dev, "failed to register usb: %d\n", ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 619 | return ret; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 620 | } |
| 621 | |
NeilBrown | 1b7cac2 | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 622 | ret = devm_request_threaded_irq(&pdev->dev, bci->irq_chg, NULL, |
Fengguang Wu | a2778b0 | 2012-08-23 19:56:43 +0800 | [diff] [blame] | 623 | twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name, |
| 624 | bci); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 625 | if (ret < 0) { |
| 626 | dev_err(&pdev->dev, "could not request irq %d, status %d\n", |
| 627 | bci->irq_chg, ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 628 | return ret; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 629 | } |
| 630 | |
NeilBrown | 1b7cac2 | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 631 | ret = devm_request_threaded_irq(&pdev->dev, bci->irq_bci, NULL, |
Fengguang Wu | a2778b0 | 2012-08-23 19:56:43 +0800 | [diff] [blame] | 632 | twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 633 | if (ret < 0) { |
| 634 | dev_err(&pdev->dev, "could not request irq %d, status %d\n", |
| 635 | bci->irq_bci, ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 636 | return ret; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 637 | } |
| 638 | |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 639 | INIT_WORK(&bci->work, twl4030_bci_usb_work); |
| 640 | |
NeilBrown | f5e4edb | 2015-03-23 09:52:48 +1100 | [diff] [blame] | 641 | bci->usb_nb.notifier_call = twl4030_bci_usb_ncb; |
| 642 | if (bci->dev->of_node) { |
| 643 | struct device_node *phynode; |
| 644 | |
| 645 | phynode = of_find_compatible_node(bci->dev->of_node->parent, |
| 646 | NULL, "ti,twl4030-usb"); |
| 647 | if (phynode) |
| 648 | bci->transceiver = devm_usb_get_phy_by_node( |
| 649 | bci->dev, phynode, &bci->usb_nb); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* Enable interrupts now. */ |
Grazvydas Ignotas | 28d48f0 | 2011-06-03 14:43:11 +0300 | [diff] [blame] | 653 | reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 654 | TWL4030_TBATOR1 | TWL4030_BATSTS); |
| 655 | ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg, |
| 656 | TWL4030_INTERRUPTS_BCIIMR1A); |
| 657 | if (ret < 0) { |
| 658 | dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 659 | return ret; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 660 | } |
| 661 | |
Grazvydas Ignotas | 28d48f0 | 2011-06-03 14:43:11 +0300 | [diff] [blame] | 662 | reg = ~(u32)(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 663 | ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg, |
| 664 | TWL4030_INTERRUPTS_BCIIMR2A); |
| 665 | if (ret < 0) |
| 666 | dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret); |
| 667 | |
| 668 | twl4030_charger_enable_ac(true); |
| 669 | twl4030_charger_enable_usb(bci, true); |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 670 | if (pdata) |
| 671 | twl4030_charger_enable_backup(pdata->bb_uvolt, |
| 672 | pdata->bb_uamp); |
| 673 | else |
| 674 | twl4030_charger_enable_backup(0, 0); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 675 | |
| 676 | return 0; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static int __exit twl4030_bci_remove(struct platform_device *pdev) |
| 680 | { |
| 681 | struct twl4030_bci *bci = platform_get_drvdata(pdev); |
| 682 | |
| 683 | twl4030_charger_enable_ac(false); |
| 684 | twl4030_charger_enable_usb(bci, false); |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 685 | twl4030_charger_enable_backup(0, 0); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 686 | |
| 687 | /* mask interrupts */ |
| 688 | twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff, |
| 689 | TWL4030_INTERRUPTS_BCIIMR1A); |
| 690 | twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff, |
| 691 | TWL4030_INTERRUPTS_BCIIMR2A); |
| 692 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 693 | return 0; |
| 694 | } |
| 695 | |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 696 | static const struct of_device_id twl_bci_of_match[] = { |
| 697 | {.compatible = "ti,twl4030-bci", }, |
| 698 | { } |
| 699 | }; |
| 700 | MODULE_DEVICE_TABLE(of, twl_bci_of_match); |
| 701 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 702 | static struct platform_driver twl4030_bci_driver = { |
| 703 | .driver = { |
| 704 | .name = "twl4030_bci", |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 705 | .of_match_table = of_match_ptr(twl_bci_of_match), |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 706 | }, |
| 707 | .remove = __exit_p(twl4030_bci_remove), |
| 708 | }; |
| 709 | |
Jingoo Han | 4420dc2 | 2013-03-05 13:01:36 +0900 | [diff] [blame] | 710 | module_platform_driver_probe(twl4030_bci_driver, twl4030_bci_probe); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 711 | |
Grazvydas Ignotas | 28d48f0 | 2011-06-03 14:43:11 +0300 | [diff] [blame] | 712 | MODULE_AUTHOR("Gražvydas Ignotas"); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 713 | MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver"); |
| 714 | MODULE_LICENSE("GPL"); |
| 715 | MODULE_ALIAS("platform:twl4030_bci"); |