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/iio/consumer.h>
27 #define TWL4030_BCIMDEN 0x00
28 #define TWL4030_BCIMDKEY 0x01
29 #define TWL4030_BCIMSTATEC 0x02
30 #define TWL4030_BCIICHG 0x08
31 #define TWL4030_BCIVAC 0x0a
32 #define TWL4030_BCIVBUS 0x0c
33 #define TWL4030_BCIMFSTS3 0x0F
34 #define TWL4030_BCIMFSTS4 0x10
35 #define TWL4030_BCICTL1 0x23
36 #define TWL4030_BB_CFG 0x12
37 #define TWL4030_BCIIREF1 0x27
38 #define TWL4030_BCIIREF2 0x28
39 #define TWL4030_BCIMFKEY 0x11
40 #define TWL4030_BCIMFEN3 0x14
41 #define TWL4030_BCIMFTH8 0x1d
42 #define TWL4030_BCIMFTH9 0x1e
43 #define TWL4030_BCIWDKEY 0x21
45 #define TWL4030_BCIMFSTS1 0x01
47 #define TWL4030_BCIAUTOWEN BIT(5)
48 #define TWL4030_CONFIG_DONE BIT(4)
49 #define TWL4030_CVENAC BIT(2)
50 #define TWL4030_BCIAUTOUSB BIT(1)
51 #define TWL4030_BCIAUTOAC BIT(0)
52 #define TWL4030_CGAIN BIT(5)
53 #define TWL4030_USBFASTMCHG BIT(2)
54 #define TWL4030_STS_VBUS BIT(7)
55 #define TWL4030_STS_USB_ID BIT(2)
56 #define TWL4030_BBCHEN BIT(4)
57 #define TWL4030_BBSEL_MASK 0x0c
58 #define TWL4030_BBSEL_2V5 0x00
59 #define TWL4030_BBSEL_3V0 0x04
60 #define TWL4030_BBSEL_3V1 0x08
61 #define TWL4030_BBSEL_3V2 0x0c
62 #define TWL4030_BBISEL_MASK 0x03
63 #define TWL4030_BBISEL_25uA 0x00
64 #define TWL4030_BBISEL_150uA 0x01
65 #define TWL4030_BBISEL_500uA 0x02
66 #define TWL4030_BBISEL_1000uA 0x03
68 #define TWL4030_BATSTSPCHG BIT(2)
69 #define TWL4030_BATSTSMCHG BIT(6)
72 #define TWL4030_WOVF BIT(0) /* Watchdog overflow */
73 #define TWL4030_TMOVF BIT(1) /* Timer overflow */
74 #define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
75 #define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
76 #define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
77 #define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
78 #define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
79 #define TWL4030_BATSTS BIT(7) /* Battery status */
81 #define TWL4030_VBATLVL BIT(0) /* VBAT level */
82 #define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */
83 #define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */
84 #define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */
86 #define TWL4030_MSTATEC_USB BIT(4)
87 #define TWL4030_MSTATEC_AC BIT(5)
88 #define TWL4030_MSTATEC_MASK 0x0f
89 #define TWL4030_MSTATEC_QUICK1 0x02
90 #define TWL4030_MSTATEC_QUICK7 0x07
91 #define TWL4030_MSTATEC_COMPLETE1 0x0b
92 #define TWL4030_MSTATEC_COMPLETE4 0x0e
95 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
96 * then AC is available.
98 static inline int ac_available(struct iio_channel
*channel_vac
)
105 err
= iio_read_channel_processed(channel_vac
, &val
);
111 static bool allow_usb
;
112 module_param(allow_usb
, bool, 0644);
113 MODULE_PARM_DESC(allow_usb
, "Allow USB charge drawing default current");
117 struct power_supply
*ac
;
118 struct power_supply
*usb
;
119 struct usb_phy
*transceiver
;
120 struct notifier_block usb_nb
;
121 struct work_struct work
;
127 * ichg_* and *_cur values in uA. If any are 'large', we set
128 * CGAIN to '1' which doubles the range for half the
131 unsigned int ichg_eoc
, ichg_lo
, ichg_hi
;
132 unsigned int usb_cur
, ac_cur
;
133 struct iio_channel
*channel_vac
;
135 int usb_mode
, ac_mode
; /* charging mode requested */
137 #define CHARGE_AUTO 1
138 #define CHARGE_LINEAR 2
140 /* When setting the USB current we slowly increase the
141 * requested current until target is reached or the voltage
142 * drops below 4.75V. In the latter case we step back one
145 unsigned int usb_cur_target
;
146 struct delayed_work current_worker
;
147 #define USB_CUR_STEP 20000 /* 20mA at a time */
148 #define USB_MIN_VOLT 4750000 /* 4.75V */
149 #define USB_CUR_DELAY msecs_to_jiffies(100)
150 #define USB_MAX_CURRENT 1700000 /* TWL4030 caps at 1.7A */
155 /* strings for 'usb_mode' values */
156 static const char *modes
[] = { "off", "auto", "continuous" };
159 * clear and set bits on an given register on a given module
161 static int twl4030_clear_set(u8 mod_no
, u8 clear
, u8 set
, u8 reg
)
166 ret
= twl_i2c_read_u8(mod_no
, &val
, reg
);
173 return twl_i2c_write_u8(mod_no
, val
, reg
);
176 static int twl4030_bci_read(u8 reg
, u8
*val
)
178 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE
, val
, reg
);
181 static int twl4030_clear_set_boot_bci(u8 clear
, u8 set
)
183 return twl4030_clear_set(TWL_MODULE_PM_MASTER
, clear
,
184 TWL4030_CONFIG_DONE
| TWL4030_BCIAUTOWEN
| set
,
185 TWL4030_PM_MASTER_BOOT_BCI
);
188 static int twl4030bci_read_adc_val(u8 reg
)
194 ret
= twl4030_bci_read(reg
+ 1, &val
);
198 temp
= (int)(val
& 0x03) << 8;
201 ret
= twl4030_bci_read(reg
, &val
);
209 * TI provided formulas:
210 * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
211 * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
212 * Here we use integer approximation of:
213 * CGAIN == 0: val * 1.6618 - 0.85 * 1000
214 * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2
217 * convert twl register value for currents into uA
219 static int regval2ua(int regval
, bool cgain
)
222 return (regval
* 16618 - 8500 * 1000) / 5;
224 return (regval
* 16618 - 8500 * 1000) / 10;
228 * convert uA currents into twl register value
230 static int ua2regval(int ua
, bool cgain
)
235 ret
= (ua
* 10 + 8500 * 1000) / 16618;
236 /* rounding problems */
242 static int twl4030_charger_update_current(struct twl4030_bci
*bci
)
246 unsigned reg
, cur_reg
;
247 u8 bcictl1
, oldreg
, fullreg
;
252 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
253 * and AC is enabled, set current for 'ac'
255 if (ac_available(bci
->channel_vac
)) {
257 bci
->ac_is_active
= true;
260 bci
->ac_is_active
= false;
261 if (cur
> bci
->usb_cur_target
) {
262 cur
= bci
->usb_cur_target
;
265 if (cur
< bci
->usb_cur_target
)
266 schedule_delayed_work(&bci
->current_worker
, USB_CUR_DELAY
);
269 /* First, check thresholds and see if cgain is needed */
270 if (bci
->ichg_eoc
>= 200000)
272 if (bci
->ichg_lo
>= 400000)
274 if (bci
->ichg_hi
>= 820000)
279 status
= twl4030_bci_read(TWL4030_BCICTL1
, &bcictl1
);
282 if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER
, &boot_bci
,
283 TWL4030_PM_MASTER_BOOT_BCI
) < 0)
287 if ((!!cgain
) != !!(bcictl1
& TWL4030_CGAIN
))
288 /* Need to turn for charging while we change the
289 * CGAIN bit. Leave it off while everything is
292 twl4030_clear_set_boot_bci(boot_bci
, 0);
295 * For ichg_eoc, the hardware only supports reg values matching
296 * 100XXXX000, and requires the XXXX be stored in the high nibble
297 * of TWL4030_BCIMFTH8.
299 reg
= ua2regval(bci
->ichg_eoc
, cgain
);
304 reg
= (reg
>> 3) & 0xf;
308 * For ichg_lo, reg value must match 10XXXX0000.
309 * XXXX is stored in low nibble of TWL4030_BCIMFTH8.
311 reg
= ua2regval(bci
->ichg_lo
, cgain
);
316 reg
= (reg
>> 4) & 0xf;
319 /* ichg_eoc and ichg_lo live in same register */
320 status
= twl4030_bci_read(TWL4030_BCIMFTH8
, &oldreg
);
323 if (oldreg
!= fullreg
) {
324 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xF4,
328 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
329 fullreg
, TWL4030_BCIMFTH8
);
332 /* ichg_hi threshold must be 1XXXX01100 (I think) */
333 reg
= ua2regval(bci
->ichg_hi
, cgain
);
338 fullreg
= (reg
>> 5) & 0xF;
340 status
= twl4030_bci_read(TWL4030_BCIMFTH9
, &oldreg
);
343 if ((oldreg
& 0xF0) != fullreg
) {
344 fullreg
|= (oldreg
& 0x0F);
345 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xE7,
349 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
350 fullreg
, TWL4030_BCIMFTH9
);
354 * And finally, set the current. This is stored in
357 reg
= ua2regval(cur
, cgain
);
358 /* we have only 10 bits */
361 status
= twl4030_bci_read(TWL4030_BCIIREF1
, &oldreg
);
365 status
= twl4030_bci_read(TWL4030_BCIIREF2
, &oldreg
);
368 cur_reg
|= oldreg
<< 8;
370 /* disable write protection for one write access for
372 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xE7,
376 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
377 (reg
& 0x100) ? 3 : 2,
381 /* disable write protection for one write access for
383 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xE7,
387 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
391 if ((!!cgain
) != !!(bcictl1
& TWL4030_CGAIN
)) {
392 /* Flip CGAIN and re-enable charging */
393 bcictl1
^= TWL4030_CGAIN
;
394 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
395 bcictl1
, TWL4030_BCICTL1
);
396 twl4030_clear_set_boot_bci(0, boot_bci
);
401 static int twl4030_charger_get_current(void);
403 static void twl4030_current_worker(struct work_struct
*data
)
407 struct twl4030_bci
*bci
= container_of(data
, struct twl4030_bci
,
408 current_worker
.work
);
410 res
= twl4030bci_read_adc_val(TWL4030_BCIVBUS
);
414 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
416 curr
= twl4030_charger_get_current();
418 dev_dbg(bci
->dev
, "v=%d cur=%d limit=%d target=%d\n", v
, curr
,
419 bci
->usb_cur
, bci
->usb_cur_target
);
421 if (v
< USB_MIN_VOLT
) {
422 /* Back up and stop adjusting. */
423 bci
->usb_cur
-= USB_CUR_STEP
;
424 bci
->usb_cur_target
= bci
->usb_cur
;
425 } else if (bci
->usb_cur
>= bci
->usb_cur_target
||
426 bci
->usb_cur
+ USB_CUR_STEP
> USB_MAX_CURRENT
) {
427 /* Reached target and voltage is OK - stop */
430 bci
->usb_cur
+= USB_CUR_STEP
;
431 schedule_delayed_work(&bci
->current_worker
, USB_CUR_DELAY
);
433 twl4030_charger_update_current(bci
);
437 * Enable/Disable USB Charge functionality.
439 static int twl4030_charger_enable_usb(struct twl4030_bci
*bci
, bool enable
)
443 if (bci
->usb_mode
== CHARGE_OFF
)
445 if (enable
&& !IS_ERR_OR_NULL(bci
->transceiver
)) {
447 twl4030_charger_update_current(bci
);
449 /* Need to keep phy powered */
450 if (!bci
->usb_enabled
) {
451 pm_runtime_get_sync(bci
->transceiver
->dev
);
452 bci
->usb_enabled
= 1;
455 if (bci
->usb_mode
== CHARGE_AUTO
)
456 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
457 ret
= twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB
);
459 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
460 ret
= twl4030_clear_set(TWL_MODULE_MAIN_CHARGE
, 0,
461 TWL4030_USBFASTMCHG
, TWL4030_BCIMFSTS4
);
462 if (bci
->usb_mode
== CHARGE_LINEAR
) {
463 twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC
|TWL4030_CVENAC
, 0);
464 /* Watch dog key: WOVF acknowledge */
465 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x33,
467 /* 0x24 + EKEY6: off mode */
468 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x2a,
470 /* EKEY2: Linear charge: USB path */
471 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x26,
473 /* WDKEY5: stop watchdog count */
474 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xf3,
476 /* enable MFEN3 access */
477 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x9c,
479 /* ICHGEOCEN - end-of-charge monitor (current < 80mA)
480 * (charging continues)
481 * ICHGLOWEN - current level monitor (charge continues)
482 * don't monitor over-current or heat save
484 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xf0,
488 ret
= twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB
, 0);
489 ret
|= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x2a,
491 if (bci
->usb_enabled
) {
492 pm_runtime_mark_last_busy(bci
->transceiver
->dev
);
493 pm_runtime_put_autosuspend(bci
->transceiver
->dev
);
494 bci
->usb_enabled
= 0;
503 * Enable/Disable AC Charge funtionality.
505 static int twl4030_charger_enable_ac(struct twl4030_bci
*bci
, bool enable
)
509 if (bci
->ac_mode
== CHARGE_OFF
)
513 ret
= twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC
);
515 ret
= twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC
, 0);
521 * Enable/Disable charging of Backup Battery.
523 static int twl4030_charger_enable_backup(int uvolt
, int uamp
)
528 if (uvolt
< 2500000 ||
530 /* disable charging of backup battery */
531 ret
= twl4030_clear_set(TWL_MODULE_PM_RECEIVER
,
532 TWL4030_BBCHEN
, 0, TWL4030_BB_CFG
);
536 flags
= TWL4030_BBCHEN
;
537 if (uvolt
>= 3200000)
538 flags
|= TWL4030_BBSEL_3V2
;
539 else if (uvolt
>= 3100000)
540 flags
|= TWL4030_BBSEL_3V1
;
541 else if (uvolt
>= 3000000)
542 flags
|= TWL4030_BBSEL_3V0
;
544 flags
|= TWL4030_BBSEL_2V5
;
547 flags
|= TWL4030_BBISEL_1000uA
;
548 else if (uamp
>= 500)
549 flags
|= TWL4030_BBISEL_500uA
;
550 else if (uamp
>= 150)
551 flags
|= TWL4030_BBISEL_150uA
;
553 flags
|= TWL4030_BBISEL_25uA
;
555 ret
= twl4030_clear_set(TWL_MODULE_PM_RECEIVER
,
556 TWL4030_BBSEL_MASK
| TWL4030_BBISEL_MASK
,
564 * TWL4030 CHG_PRES (AC charger presence) events
566 static irqreturn_t
twl4030_charger_interrupt(int irq
, void *arg
)
568 struct twl4030_bci
*bci
= arg
;
570 dev_dbg(bci
->dev
, "CHG_PRES irq\n");
571 /* reset current on each 'plug' event */
572 bci
->ac_cur
= 500000;
573 twl4030_charger_update_current(bci
);
574 power_supply_changed(bci
->ac
);
575 power_supply_changed(bci
->usb
);
581 * TWL4030 BCI monitoring events
583 static irqreturn_t
twl4030_bci_interrupt(int irq
, void *arg
)
585 struct twl4030_bci
*bci
= arg
;
589 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS
, &irqs1
,
590 TWL4030_INTERRUPTS_BCIISR1A
);
594 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS
, &irqs2
,
595 TWL4030_INTERRUPTS_BCIISR2A
);
599 dev_dbg(bci
->dev
, "BCI irq %02x %02x\n", irqs2
, irqs1
);
601 if (irqs1
& (TWL4030_ICHGLOW
| TWL4030_ICHGEOC
)) {
602 /* charger state change, inform the core */
603 power_supply_changed(bci
->ac
);
604 power_supply_changed(bci
->usb
);
606 twl4030_charger_update_current(bci
);
608 /* various monitoring events, for now we just log them here */
609 if (irqs1
& (TWL4030_TBATOR2
| TWL4030_TBATOR1
))
610 dev_warn(bci
->dev
, "battery temperature out of range\n");
612 if (irqs1
& TWL4030_BATSTS
)
613 dev_crit(bci
->dev
, "battery disconnected\n");
615 if (irqs2
& TWL4030_VBATOV
)
616 dev_crit(bci
->dev
, "VBAT overvoltage\n");
618 if (irqs2
& TWL4030_VBUSOV
)
619 dev_crit(bci
->dev
, "VBUS overvoltage\n");
621 if (irqs2
& TWL4030_ACCHGOV
)
622 dev_crit(bci
->dev
, "Ac charger overvoltage\n");
627 static void twl4030_bci_usb_work(struct work_struct
*data
)
629 struct twl4030_bci
*bci
= container_of(data
, struct twl4030_bci
, work
);
631 switch (bci
->event
) {
633 case USB_EVENT_CHARGER
:
634 twl4030_charger_enable_usb(bci
, true);
637 twl4030_charger_enable_usb(bci
, false);
642 static int twl4030_bci_usb_ncb(struct notifier_block
*nb
, unsigned long val
,
645 struct twl4030_bci
*bci
= container_of(nb
, struct twl4030_bci
, usb_nb
);
647 dev_dbg(bci
->dev
, "OTG notify %lu\n", val
);
649 /* reset current on each 'plug' event */
651 bci
->usb_cur_target
= 500000;
653 bci
->usb_cur_target
= 100000;
656 schedule_work(&bci
->work
);
662 * sysfs charger enabled store
665 twl4030_bci_mode_store(struct device
*dev
, struct device_attribute
*attr
,
666 const char *buf
, size_t n
)
668 struct twl4030_bci
*bci
= dev_get_drvdata(dev
->parent
);
672 mode
= sysfs_match_string(modes
, buf
);
676 if (dev
== &bci
->ac
->dev
) {
679 twl4030_charger_enable_ac(bci
, false);
681 status
= twl4030_charger_enable_ac(bci
, true);
683 twl4030_charger_enable_usb(bci
, false);
684 bci
->usb_mode
= mode
;
685 status
= twl4030_charger_enable_usb(bci
, true);
687 return (status
== 0) ? n
: status
;
691 * sysfs charger enabled show
694 twl4030_bci_mode_show(struct device
*dev
,
695 struct device_attribute
*attr
, char *buf
)
697 struct twl4030_bci
*bci
= dev_get_drvdata(dev
->parent
);
700 int mode
= bci
->usb_mode
;
702 if (dev
== &bci
->ac
->dev
)
705 for (i
= 0; i
< ARRAY_SIZE(modes
); i
++)
707 len
+= snprintf(buf
+len
, PAGE_SIZE
-len
,
710 len
+= snprintf(buf
+len
, PAGE_SIZE
-len
,
715 static DEVICE_ATTR(mode
, 0644, twl4030_bci_mode_show
,
716 twl4030_bci_mode_store
);
718 static int twl4030_charger_get_current(void)
724 curr
= twl4030bci_read_adc_val(TWL4030_BCIICHG
);
728 ret
= twl4030_bci_read(TWL4030_BCICTL1
, &bcictl1
);
732 return regval2ua(curr
, bcictl1
& TWL4030_CGAIN
);
736 * Returns the main charge FSM state
739 static int twl4030bci_state(struct twl4030_bci
*bci
)
744 ret
= twl4030_bci_read(TWL4030_BCIMSTATEC
, &state
);
746 pr_err("twl4030_bci: error reading BCIMSTATEC\n");
750 dev_dbg(bci
->dev
, "state: %02x\n", state
);
755 static int twl4030_bci_state_to_status(int state
)
757 state
&= TWL4030_MSTATEC_MASK
;
758 if (TWL4030_MSTATEC_QUICK1
<= state
&& state
<= TWL4030_MSTATEC_QUICK7
)
759 return POWER_SUPPLY_STATUS_CHARGING
;
760 else if (TWL4030_MSTATEC_COMPLETE1
<= state
&&
761 state
<= TWL4030_MSTATEC_COMPLETE4
)
762 return POWER_SUPPLY_STATUS_FULL
;
764 return POWER_SUPPLY_STATUS_NOT_CHARGING
;
767 static int twl4030_bci_get_property(struct power_supply
*psy
,
768 enum power_supply_property psp
,
769 union power_supply_propval
*val
)
771 struct twl4030_bci
*bci
= dev_get_drvdata(psy
->dev
.parent
);
776 state
= twl4030bci_state(bci
);
780 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
781 is_charging
= state
& TWL4030_MSTATEC_USB
;
783 is_charging
= state
& TWL4030_MSTATEC_AC
;
786 twl4030_bci_read(TWL4030_BCIMDEN
, &s
);
787 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
792 /* A little white lie */
793 state
= TWL4030_MSTATEC_QUICK1
;
797 case POWER_SUPPLY_PROP_STATUS
:
799 val
->intval
= twl4030_bci_state_to_status(state
);
801 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
803 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
804 /* charging must be active for meaningful result */
807 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
) {
808 ret
= twl4030bci_read_adc_val(TWL4030_BCIVBUS
);
811 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
812 val
->intval
= ret
* 6843;
814 ret
= twl4030bci_read_adc_val(TWL4030_BCIVAC
);
817 /* BCIVAC uses ADCIN11, 10/1023 V/step */
818 val
->intval
= ret
* 9775;
821 case POWER_SUPPLY_PROP_CURRENT_NOW
:
824 /* current measurement is shared between AC and USB */
825 ret
= twl4030_charger_get_current();
830 case POWER_SUPPLY_PROP_ONLINE
:
831 val
->intval
= is_charging
&&
832 twl4030_bci_state_to_status(state
) !=
833 POWER_SUPPLY_STATUS_NOT_CHARGING
;
835 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
837 if (psy
->desc
->type
!= POWER_SUPPLY_TYPE_USB
) {
838 if (!bci
->ac_is_active
)
839 val
->intval
= bci
->ac_cur
;
841 if (bci
->ac_is_active
)
842 val
->intval
= bci
->usb_cur_target
;
844 if (val
->intval
< 0) {
847 val
->intval
= twl4030bci_read_adc_val(TWL4030_BCIIREF1
);
850 ret
= twl4030_bci_read(TWL4030_BCICTL1
, &bcictl1
);
853 val
->intval
= regval2ua(val
->intval
, bcictl1
&
864 static int twl4030_bci_set_property(struct power_supply
*psy
,
865 enum power_supply_property psp
,
866 const union power_supply_propval
*val
)
868 struct twl4030_bci
*bci
= dev_get_drvdata(psy
->dev
.parent
);
871 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
872 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
873 bci
->usb_cur_target
= val
->intval
;
875 bci
->ac_cur
= val
->intval
;
876 twl4030_charger_update_current(bci
);
885 static int twl4030_bci_property_is_writeable(struct power_supply
*psy
,
886 enum power_supply_property psp
)
889 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
896 static enum power_supply_property twl4030_charger_props
[] = {
897 POWER_SUPPLY_PROP_STATUS
,
898 POWER_SUPPLY_PROP_ONLINE
,
899 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
900 POWER_SUPPLY_PROP_CURRENT_NOW
,
901 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
905 static const struct twl4030_bci_platform_data
*
906 twl4030_bci_parse_dt(struct device
*dev
)
908 struct device_node
*np
= dev
->of_node
;
909 struct twl4030_bci_platform_data
*pdata
;
914 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
918 if (of_property_read_u32(np
, "ti,bb-uvolt", &num
) == 0)
919 pdata
->bb_uvolt
= num
;
920 if (of_property_read_u32(np
, "ti,bb-uamp", &num
) == 0)
921 pdata
->bb_uamp
= num
;
925 static inline const struct twl4030_bci_platform_data
*
926 twl4030_bci_parse_dt(struct device
*dev
)
932 static const struct power_supply_desc twl4030_bci_ac_desc
= {
933 .name
= "twl4030_ac",
934 .type
= POWER_SUPPLY_TYPE_MAINS
,
935 .properties
= twl4030_charger_props
,
936 .num_properties
= ARRAY_SIZE(twl4030_charger_props
),
937 .get_property
= twl4030_bci_get_property
,
938 .set_property
= twl4030_bci_set_property
,
939 .property_is_writeable
= twl4030_bci_property_is_writeable
,
942 static const struct power_supply_desc twl4030_bci_usb_desc
= {
943 .name
= "twl4030_usb",
944 .type
= POWER_SUPPLY_TYPE_USB
,
945 .properties
= twl4030_charger_props
,
946 .num_properties
= ARRAY_SIZE(twl4030_charger_props
),
947 .get_property
= twl4030_bci_get_property
,
948 .set_property
= twl4030_bci_set_property
,
949 .property_is_writeable
= twl4030_bci_property_is_writeable
,
952 static int twl4030_bci_probe(struct platform_device
*pdev
)
954 struct twl4030_bci
*bci
;
955 const struct twl4030_bci_platform_data
*pdata
= pdev
->dev
.platform_data
;
959 bci
= devm_kzalloc(&pdev
->dev
, sizeof(*bci
), GFP_KERNEL
);
964 pdata
= twl4030_bci_parse_dt(&pdev
->dev
);
966 bci
->ichg_eoc
= 80100; /* Stop charging when current drops to here */
967 bci
->ichg_lo
= 241000; /* Low threshold */
968 bci
->ichg_hi
= 500000; /* High threshold */
969 bci
->ac_cur
= 500000; /* 500mA */
971 bci
->usb_cur_target
= 500000; /* 500mA */
973 bci
->usb_cur_target
= 100000; /* 100mA */
974 bci
->usb_mode
= CHARGE_AUTO
;
975 bci
->ac_mode
= CHARGE_AUTO
;
977 bci
->dev
= &pdev
->dev
;
978 bci
->irq_chg
= platform_get_irq(pdev
, 0);
979 bci
->irq_bci
= platform_get_irq(pdev
, 1);
981 platform_set_drvdata(pdev
, bci
);
983 INIT_WORK(&bci
->work
, twl4030_bci_usb_work
);
984 INIT_DELAYED_WORK(&bci
->current_worker
, twl4030_current_worker
);
986 bci
->channel_vac
= devm_iio_channel_get(&pdev
->dev
, "vac");
987 if (IS_ERR(bci
->channel_vac
)) {
988 ret
= PTR_ERR(bci
->channel_vac
);
989 if (ret
== -EPROBE_DEFER
)
990 return ret
; /* iio not ready */
991 dev_warn(&pdev
->dev
, "could not request vac iio channel (%d)",
993 bci
->channel_vac
= NULL
;
996 if (bci
->dev
->of_node
) {
997 struct device_node
*phynode
;
999 phynode
= of_find_compatible_node(bci
->dev
->of_node
->parent
,
1000 NULL
, "ti,twl4030-usb");
1002 bci
->usb_nb
.notifier_call
= twl4030_bci_usb_ncb
;
1003 bci
->transceiver
= devm_usb_get_phy_by_node(
1004 bci
->dev
, phynode
, &bci
->usb_nb
);
1005 if (IS_ERR(bci
->transceiver
)) {
1006 ret
= PTR_ERR(bci
->transceiver
);
1007 if (ret
== -EPROBE_DEFER
)
1008 return ret
; /* phy not ready */
1009 dev_warn(&pdev
->dev
, "could not request transceiver (%d)",
1011 bci
->transceiver
= NULL
;
1016 bci
->ac
= devm_power_supply_register(&pdev
->dev
, &twl4030_bci_ac_desc
,
1018 if (IS_ERR(bci
->ac
)) {
1019 ret
= PTR_ERR(bci
->ac
);
1020 dev_err(&pdev
->dev
, "failed to register ac: %d\n", ret
);
1024 bci
->usb
= devm_power_supply_register(&pdev
->dev
, &twl4030_bci_usb_desc
,
1026 if (IS_ERR(bci
->usb
)) {
1027 ret
= PTR_ERR(bci
->usb
);
1028 dev_err(&pdev
->dev
, "failed to register usb: %d\n", ret
);
1032 ret
= devm_request_threaded_irq(&pdev
->dev
, bci
->irq_chg
, NULL
,
1033 twl4030_charger_interrupt
, IRQF_ONESHOT
, pdev
->name
,
1036 dev_err(&pdev
->dev
, "could not request irq %d, status %d\n",
1041 ret
= devm_request_threaded_irq(&pdev
->dev
, bci
->irq_bci
, NULL
,
1042 twl4030_bci_interrupt
, IRQF_ONESHOT
, pdev
->name
, bci
);
1044 dev_err(&pdev
->dev
, "could not request irq %d, status %d\n",
1049 /* Enable interrupts now. */
1050 reg
= ~(u32
)(TWL4030_ICHGLOW
| TWL4030_ICHGEOC
| TWL4030_TBATOR2
|
1051 TWL4030_TBATOR1
| TWL4030_BATSTS
);
1052 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
1053 TWL4030_INTERRUPTS_BCIIMR1A
);
1055 dev_err(&pdev
->dev
, "failed to unmask interrupts: %d\n", ret
);
1059 reg
= ~(u32
)(TWL4030_VBATOV
| TWL4030_VBUSOV
| TWL4030_ACCHGOV
);
1060 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
1061 TWL4030_INTERRUPTS_BCIIMR2A
);
1063 dev_warn(&pdev
->dev
, "failed to unmask interrupts: %d\n", ret
);
1065 twl4030_charger_update_current(bci
);
1066 if (device_create_file(&bci
->usb
->dev
, &dev_attr_mode
))
1067 dev_warn(&pdev
->dev
, "could not create sysfs file\n");
1068 if (device_create_file(&bci
->ac
->dev
, &dev_attr_mode
))
1069 dev_warn(&pdev
->dev
, "could not create sysfs file\n");
1071 twl4030_charger_enable_ac(bci
, true);
1072 if (!IS_ERR_OR_NULL(bci
->transceiver
))
1073 twl4030_bci_usb_ncb(&bci
->usb_nb
,
1074 bci
->transceiver
->last_event
,
1077 twl4030_charger_enable_usb(bci
, false);
1079 twl4030_charger_enable_backup(pdata
->bb_uvolt
,
1082 twl4030_charger_enable_backup(0, 0);
1087 static int twl4030_bci_remove(struct platform_device
*pdev
)
1089 struct twl4030_bci
*bci
= platform_get_drvdata(pdev
);
1091 twl4030_charger_enable_ac(bci
, false);
1092 twl4030_charger_enable_usb(bci
, false);
1093 twl4030_charger_enable_backup(0, 0);
1095 device_remove_file(&bci
->usb
->dev
, &dev_attr_mode
);
1096 device_remove_file(&bci
->ac
->dev
, &dev_attr_mode
);
1097 /* mask interrupts */
1098 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, 0xff,
1099 TWL4030_INTERRUPTS_BCIIMR1A
);
1100 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, 0xff,
1101 TWL4030_INTERRUPTS_BCIIMR2A
);
1106 static const struct of_device_id twl_bci_of_match
[] = {
1107 {.compatible
= "ti,twl4030-bci", },
1110 MODULE_DEVICE_TABLE(of
, twl_bci_of_match
);
1112 static struct platform_driver twl4030_bci_driver
= {
1113 .probe
= twl4030_bci_probe
,
1114 .remove
= twl4030_bci_remove
,
1116 .name
= "twl4030_bci",
1117 .of_match_table
= of_match_ptr(twl_bci_of_match
),
1120 module_platform_driver(twl4030_bci_driver
);
1122 MODULE_AUTHOR("Gražvydas Ignotas");
1123 MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
1124 MODULE_LICENSE("GPL");
1125 MODULE_ALIAS("platform:twl4030_bci");