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