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