16 #include <linux/module.h>
17 #include <linux/slab.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)
57 #define TWL4030_TMOVF BIT(1)
58 #define TWL4030_ICHGHIGH BIT(2)
59 #define TWL4030_ICHGLOW BIT(3)
60 #define TWL4030_ICHGEOC BIT(4)
61 #define TWL4030_TBATOR2 BIT(5)
62 #define TWL4030_TBATOR1 BIT(6)
63 #define TWL4030_BATSTS BIT(7)
65 #define TWL4030_VBATLVL BIT(0)
66 #define TWL4030_VBATOV BIT(1)
67 #define TWL4030_VBUSOV BIT(2)
68 #define TWL4030_ACCHGOV BIT(3)
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;
120 static int twl4030_clear_set_boot_bci(
u8 clear,
u8 set)
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);
150 static int twl4030_bci_have_vbus(
struct twl4030_bci *bci)
160 dev_dbg(bci->
dev,
"check_vbus: HW_CONDITIONS %02x\n", hwsts);
178 if (!twl4030_bci_have_vbus(bci))
218 static int twl4030_charger_enable_ac(
bool enable)
233 static int twl4030_charger_enable_backup(
int uvolt,
int uamp)
238 if (uvolt < 2500000 ||
247 if (uvolt >= 3200000)
249 else if (uvolt >= 3100000)
251 else if (uvolt >= 3000000)
258 else if (uamp >= 500)
260 else if (uamp >= 150)
306 dev_dbg(bci->
dev,
"BCI irq %02x %02x\n", irqs2, irqs1);
316 dev_warn(bci->
dev,
"battery temperature out of range\n");
319 dev_crit(bci->
dev,
"battery disconnected\n");
322 dev_crit(bci->
dev,
"VBAT overvoltage\n");
325 dev_crit(bci->
dev,
"VBUS overvoltage\n");
328 dev_crit(bci->
dev,
"Ac charger overvoltage\n");
337 switch (bci->
event) {
340 twl4030_charger_enable_usb(bci,
true);
343 twl4030_charger_enable_usb(bci,
false);
348 static int twl4030_bci_usb_ncb(
struct notifier_block *nb,
unsigned long val,
369 static int twl4030_charger_get_current(
void)
383 ret = (curr * 16618 - 850 * 10000) / 10;
394 static int twl4030bci_state(
struct twl4030_bci *bci)
401 pr_err(
"twl4030_bci: error reading BCIMSTATEC\n");
410 static int twl4030_bci_state_to_status(
int state)
422 static int twl4030_bci_get_property(
struct power_supply *psy,
431 state = twl4030bci_state(bci);
443 val->
intval = twl4030_bci_state_to_status(state);
469 ret = twl4030_charger_get_current();
475 val->
intval = is_charging &&
476 twl4030_bci_state_to_status(state) !=
508 platform_set_drvdata(pdev, bci);
510 bci->
ac.name =
"twl4030_ac";
512 bci->
ac.properties = twl4030_charger_props;
513 bci->
ac.num_properties =
ARRAY_SIZE(twl4030_charger_props);
514 bci->
ac.get_property = twl4030_bci_get_property;
518 dev_err(&pdev->
dev,
"failed to register ac: %d\n", ret);
519 goto fail_register_ac;
522 bci->
usb.name =
"twl4030_usb";
524 bci->
usb.properties = twl4030_charger_props;
526 bci->
usb.get_property = twl4030_bci_get_property;
532 dev_err(&pdev->
dev,
"failed to register usb: %d\n", ret);
533 goto fail_register_usb;
540 dev_err(&pdev->
dev,
"could not request irq %d, status %d\n",
548 dev_err(&pdev->
dev,
"could not request irq %d, status %d\n",
557 bci->
usb_nb.notifier_call = twl4030_bci_usb_ncb;
567 dev_err(&pdev->
dev,
"failed to unmask interrupts: %d\n", ret);
568 goto fail_unmask_interrupts;
575 dev_warn(&pdev->
dev,
"failed to unmask interrupts: %d\n", ret);
577 twl4030_charger_enable_ac(
true);
578 twl4030_charger_enable_usb(bci,
true);
579 twl4030_charger_enable_backup(pdata->
bb_uvolt,
584 fail_unmask_interrupts:
597 platform_set_drvdata(pdev,
NULL);
605 struct twl4030_bci *bci = platform_get_drvdata(pdev);
607 twl4030_charger_enable_ac(
false);
608 twl4030_charger_enable_usb(bci,
false);
609 twl4030_charger_enable_backup(0, 0);
625 platform_set_drvdata(pdev,
NULL);
633 .name =
"twl4030_bci",
636 .remove =
__exit_p(twl4030_bci_remove),
639 static int __init twl4030_bci_init(
void)
645 static void __exit twl4030_bci_exit(
void)