2 * TWL4030/TPS65950 BCI (Battery Charger Interface) driver
4 * Copyright (C) 2010 Gražvydas Ignotas <notasas@gmail.com>
6 * based on twl4030_bci_battery.c by TI
7 * Copyright (C) 2008 Texas Instruments, Inc.
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.
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
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 #include <linux/regulator/machine.h>
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
33 #define TWL4030_BB_CFG 0x12
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)
43 #define TWL4030_BBCHEN BIT(4)
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
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 */
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 */
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
78 static bool allow_usb
;
79 module_param(allow_usb
, bool, 0644);
80 MODULE_PARM_DESC(allow_usb
, "Allow USB charge drawing default current");
84 struct power_supply ac
;
85 struct power_supply usb
;
86 struct usb_phy
*transceiver
;
87 struct notifier_block usb_nb
;
88 struct work_struct work
;
91 struct regulator
*usb_reg
;
98 * clear and set bits on an given register on a given module
100 static int twl4030_clear_set(u8 mod_no
, u8 clear
, u8 set
, u8 reg
)
105 ret
= twl_i2c_read_u8(mod_no
, &val
, reg
);
112 return twl_i2c_write_u8(mod_no
, val
, reg
);
115 static int twl4030_bci_read(u8 reg
, u8
*val
)
117 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE
, val
, reg
);
120 static int twl4030_clear_set_boot_bci(u8 clear
, u8 set
)
122 return twl4030_clear_set(TWL_MODULE_PM_MASTER
, clear
,
123 TWL4030_CONFIG_DONE
| TWL4030_BCIAUTOWEN
| set
,
124 TWL4030_PM_MASTER_BOOT_BCI
);
127 static int twl4030bci_read_adc_val(u8 reg
)
133 ret
= twl4030_bci_read(reg
+ 1, &val
);
137 temp
= (int)(val
& 0x03) << 8;
140 ret
= twl4030_bci_read(reg
, &val
);
148 * Check if VBUS power is present
150 static int twl4030_bci_have_vbus(struct twl4030_bci
*bci
)
155 ret
= twl_i2c_read_u8(TWL_MODULE_PM_MASTER
, &hwsts
,
156 TWL4030_PM_MASTER_STS_HW_CONDITIONS
);
160 dev_dbg(bci
->dev
, "check_vbus: HW_CONDITIONS %02x\n", hwsts
);
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
))
170 * Enable/Disable USB Charge functionality.
172 static int twl4030_charger_enable_usb(struct twl4030_bci
*bci
, bool enable
)
177 /* Check for USB charger connected */
178 if (!twl4030_bci_have_vbus(bci
))
182 * Until we can find out what current the device can provide,
183 * require a module param to enable USB charging.
186 dev_warn(bci
->dev
, "USB charging is disabled.\n");
190 /* Need to keep regulator on */
191 if (!bci
->usb_enabled
) {
192 ret
= regulator_enable(bci
->usb_reg
);
195 "Failed to enable regulator\n");
198 bci
->usb_enabled
= 1;
201 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
202 ret
= twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB
);
206 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
207 ret
= twl4030_clear_set(TWL_MODULE_MAIN_CHARGE
, 0,
208 TWL4030_USBFASTMCHG
, TWL4030_BCIMFSTS4
);
210 ret
= twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB
, 0);
211 if (bci
->usb_enabled
) {
212 regulator_disable(bci
->usb_reg
);
213 bci
->usb_enabled
= 0;
221 * Enable/Disable AC Charge funtionality.
223 static int twl4030_charger_enable_ac(bool enable
)
228 ret
= twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC
);
230 ret
= twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC
, 0);
236 * Enable/Disable charging of Backup Battery.
238 static int twl4030_charger_enable_backup(int uvolt
, int uamp
)
243 if (uvolt
< 2500000 ||
245 /* disable charging of backup battery */
246 ret
= twl4030_clear_set(TWL_MODULE_PM_RECEIVER
,
247 TWL4030_BBCHEN
, 0, TWL4030_BB_CFG
);
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
;
259 flags
|= TWL4030_BBSEL_2V5
;
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
;
268 flags
|= TWL4030_BBISEL_25uA
;
270 ret
= twl4030_clear_set(TWL_MODULE_PM_RECEIVER
,
271 TWL4030_BBSEL_MASK
| TWL4030_BBISEL_MASK
,
279 * TWL4030 CHG_PRES (AC charger presence) events
281 static irqreturn_t
twl4030_charger_interrupt(int irq
, void *arg
)
283 struct twl4030_bci
*bci
= arg
;
285 dev_dbg(bci
->dev
, "CHG_PRES irq\n");
286 power_supply_changed(&bci
->ac
);
287 power_supply_changed(&bci
->usb
);
293 * TWL4030 BCI monitoring events
295 static irqreturn_t
twl4030_bci_interrupt(int irq
, void *arg
)
297 struct twl4030_bci
*bci
= arg
;
301 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS
, &irqs1
,
302 TWL4030_INTERRUPTS_BCIISR1A
);
306 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS
, &irqs2
,
307 TWL4030_INTERRUPTS_BCIISR2A
);
311 dev_dbg(bci
->dev
, "BCI irq %02x %02x\n", irqs2
, irqs1
);
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
);
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");
323 if (irqs1
& TWL4030_BATSTS
)
324 dev_crit(bci
->dev
, "battery disconnected\n");
326 if (irqs2
& TWL4030_VBATOV
)
327 dev_crit(bci
->dev
, "VBAT overvoltage\n");
329 if (irqs2
& TWL4030_VBUSOV
)
330 dev_crit(bci
->dev
, "VBUS overvoltage\n");
332 if (irqs2
& TWL4030_ACCHGOV
)
333 dev_crit(bci
->dev
, "Ac charger overvoltage\n");
338 static void twl4030_bci_usb_work(struct work_struct
*data
)
340 struct twl4030_bci
*bci
= container_of(data
, struct twl4030_bci
, work
);
342 switch (bci
->event
) {
344 case USB_EVENT_CHARGER
:
345 twl4030_charger_enable_usb(bci
, true);
348 twl4030_charger_enable_usb(bci
, false);
353 static int twl4030_bci_usb_ncb(struct notifier_block
*nb
, unsigned long val
,
356 struct twl4030_bci
*bci
= container_of(nb
, struct twl4030_bci
, usb_nb
);
358 dev_dbg(bci
->dev
, "OTG notify %lu\n", val
);
361 schedule_work(&bci
->work
);
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
374 static int twl4030_charger_get_current(void)
380 curr
= twl4030bci_read_adc_val(TWL4030_BCIICHG
);
384 ret
= twl4030_bci_read(TWL4030_BCICTL1
, &bcictl1
);
388 ret
= (curr
* 16618 - 850 * 10000) / 10;
389 if (bcictl1
& TWL4030_CGAIN
)
396 * Returns the main charge FSM state
399 static int twl4030bci_state(struct twl4030_bci
*bci
)
404 ret
= twl4030_bci_read(TWL4030_BCIMSTATEC
, &state
);
406 pr_err("twl4030_bci: error reading BCIMSTATEC\n");
410 dev_dbg(bci
->dev
, "state: %02x\n", state
);
415 static int twl4030_bci_state_to_status(int state
)
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
;
424 return POWER_SUPPLY_STATUS_NOT_CHARGING
;
427 static int twl4030_bci_get_property(struct power_supply
*psy
,
428 enum power_supply_property psp
,
429 union power_supply_propval
*val
)
431 struct twl4030_bci
*bci
= dev_get_drvdata(psy
->dev
->parent
);
436 state
= twl4030bci_state(bci
);
440 if (psy
->type
== POWER_SUPPLY_TYPE_USB
)
441 is_charging
= state
& TWL4030_MSTATEC_USB
;
443 is_charging
= state
& TWL4030_MSTATEC_AC
;
446 case POWER_SUPPLY_PROP_STATUS
:
448 val
->intval
= twl4030_bci_state_to_status(state
);
450 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
452 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
453 /* charging must be active for meaningful result */
456 if (psy
->type
== POWER_SUPPLY_TYPE_USB
) {
457 ret
= twl4030bci_read_adc_val(TWL4030_BCIVBUS
);
460 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
461 val
->intval
= ret
* 6843;
463 ret
= twl4030bci_read_adc_val(TWL4030_BCIVAC
);
466 /* BCIVAC uses ADCIN11, 10/1023 V/step */
467 val
->intval
= ret
* 9775;
470 case POWER_SUPPLY_PROP_CURRENT_NOW
:
473 /* current measurement is shared between AC and USB */
474 ret
= twl4030_charger_get_current();
479 case POWER_SUPPLY_PROP_ONLINE
:
480 val
->intval
= is_charging
&&
481 twl4030_bci_state_to_status(state
) !=
482 POWER_SUPPLY_STATUS_NOT_CHARGING
;
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
,
499 static const struct twl4030_bci_platform_data
*
500 twl4030_bci_parse_dt(struct device
*dev
)
502 struct device_node
*np
= dev
->of_node
;
503 struct twl4030_bci_platform_data
*pdata
;
508 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
512 if (of_property_read_u32(np
, "ti,bb-uvolt", &num
) == 0)
513 pdata
->bb_uvolt
= num
;
514 if (of_property_read_u32(np
, "ti,bb-uamp", &num
) == 0)
515 pdata
->bb_uamp
= num
;
519 static inline const struct twl4030_bci_platform_data
*
520 twl4030_bci_parse_dt(struct device
*dev
)
526 static int __init
twl4030_bci_probe(struct platform_device
*pdev
)
528 struct twl4030_bci
*bci
;
529 const struct twl4030_bci_platform_data
*pdata
= pdev
->dev
.platform_data
;
533 bci
= kzalloc(sizeof(*bci
), GFP_KERNEL
);
538 pdata
= twl4030_bci_parse_dt(&pdev
->dev
);
540 bci
->dev
= &pdev
->dev
;
541 bci
->irq_chg
= platform_get_irq(pdev
, 0);
542 bci
->irq_bci
= platform_get_irq(pdev
, 1);
544 platform_set_drvdata(pdev
, bci
);
546 bci
->ac
.name
= "twl4030_ac";
547 bci
->ac
.type
= POWER_SUPPLY_TYPE_MAINS
;
548 bci
->ac
.properties
= twl4030_charger_props
;
549 bci
->ac
.num_properties
= ARRAY_SIZE(twl4030_charger_props
);
550 bci
->ac
.get_property
= twl4030_bci_get_property
;
552 ret
= power_supply_register(&pdev
->dev
, &bci
->ac
);
554 dev_err(&pdev
->dev
, "failed to register ac: %d\n", ret
);
555 goto fail_register_ac
;
558 bci
->usb
.name
= "twl4030_usb";
559 bci
->usb
.type
= POWER_SUPPLY_TYPE_USB
;
560 bci
->usb
.properties
= twl4030_charger_props
;
561 bci
->usb
.num_properties
= ARRAY_SIZE(twl4030_charger_props
);
562 bci
->usb
.get_property
= twl4030_bci_get_property
;
564 bci
->usb_reg
= regulator_get(bci
->dev
, "bci3v1");
566 ret
= power_supply_register(&pdev
->dev
, &bci
->usb
);
568 dev_err(&pdev
->dev
, "failed to register usb: %d\n", ret
);
569 goto fail_register_usb
;
572 ret
= request_threaded_irq(bci
->irq_chg
, NULL
,
573 twl4030_charger_interrupt
, IRQF_ONESHOT
, pdev
->name
,
576 dev_err(&pdev
->dev
, "could not request irq %d, status %d\n",
581 ret
= request_threaded_irq(bci
->irq_bci
, NULL
,
582 twl4030_bci_interrupt
, IRQF_ONESHOT
, pdev
->name
, bci
);
584 dev_err(&pdev
->dev
, "could not request irq %d, status %d\n",
589 INIT_WORK(&bci
->work
, twl4030_bci_usb_work
);
591 bci
->transceiver
= usb_get_phy(USB_PHY_TYPE_USB2
);
592 if (!IS_ERR_OR_NULL(bci
->transceiver
)) {
593 bci
->usb_nb
.notifier_call
= twl4030_bci_usb_ncb
;
594 usb_register_notifier(bci
->transceiver
, &bci
->usb_nb
);
597 /* Enable interrupts now. */
598 reg
= ~(u32
)(TWL4030_ICHGLOW
| TWL4030_ICHGEOC
| TWL4030_TBATOR2
|
599 TWL4030_TBATOR1
| TWL4030_BATSTS
);
600 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
601 TWL4030_INTERRUPTS_BCIIMR1A
);
603 dev_err(&pdev
->dev
, "failed to unmask interrupts: %d\n", ret
);
604 goto fail_unmask_interrupts
;
607 reg
= ~(u32
)(TWL4030_VBATOV
| TWL4030_VBUSOV
| TWL4030_ACCHGOV
);
608 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
609 TWL4030_INTERRUPTS_BCIIMR2A
);
611 dev_warn(&pdev
->dev
, "failed to unmask interrupts: %d\n", ret
);
613 twl4030_charger_enable_ac(true);
614 twl4030_charger_enable_usb(bci
, true);
616 twl4030_charger_enable_backup(pdata
->bb_uvolt
,
619 twl4030_charger_enable_backup(0, 0);
623 fail_unmask_interrupts
:
624 if (!IS_ERR_OR_NULL(bci
->transceiver
)) {
625 usb_unregister_notifier(bci
->transceiver
, &bci
->usb_nb
);
626 usb_put_phy(bci
->transceiver
);
628 free_irq(bci
->irq_bci
, bci
);
630 free_irq(bci
->irq_chg
, bci
);
632 power_supply_unregister(&bci
->usb
);
634 power_supply_unregister(&bci
->ac
);
641 static int __exit
twl4030_bci_remove(struct platform_device
*pdev
)
643 struct twl4030_bci
*bci
= platform_get_drvdata(pdev
);
645 twl4030_charger_enable_ac(false);
646 twl4030_charger_enable_usb(bci
, false);
647 twl4030_charger_enable_backup(0, 0);
649 /* mask interrupts */
650 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, 0xff,
651 TWL4030_INTERRUPTS_BCIIMR1A
);
652 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, 0xff,
653 TWL4030_INTERRUPTS_BCIIMR2A
);
655 if (!IS_ERR_OR_NULL(bci
->transceiver
)) {
656 usb_unregister_notifier(bci
->transceiver
, &bci
->usb_nb
);
657 usb_put_phy(bci
->transceiver
);
659 free_irq(bci
->irq_bci
, bci
);
660 free_irq(bci
->irq_chg
, bci
);
661 power_supply_unregister(&bci
->usb
);
662 power_supply_unregister(&bci
->ac
);
668 static const struct of_device_id twl_bci_of_match
[] = {
669 {.compatible
= "ti,twl4030-bci", },
672 MODULE_DEVICE_TABLE(of
, twl_bci_of_match
);
674 static struct platform_driver twl4030_bci_driver
= {
676 .name
= "twl4030_bci",
677 .owner
= THIS_MODULE
,
678 .of_match_table
= of_match_ptr(twl_bci_of_match
),
680 .remove
= __exit_p(twl4030_bci_remove
),
683 module_platform_driver_probe(twl4030_bci_driver
, twl4030_bci_probe
);
685 MODULE_AUTHOR("Gražvydas Ignotas");
686 MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
687 MODULE_LICENSE("GPL");
688 MODULE_ALIAS("platform:twl4030_bci");