1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * TWL4030/TPS65950 BCI (Battery Charger Interface) driver
5 * Copyright (C) 2010 Gražvydas Ignotas <notasas@gmail.com>
7 * based on twl4030_bci_battery.c by TI
8 * Copyright (C) 2008 Texas Instruments, Inc.
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/err.h>
15 #include <linux/platform_device.h>
16 #include <linux/interrupt.h>
17 #include <linux/mfd/twl.h>
18 #include <linux/power_supply.h>
19 #include <linux/notifier.h>
20 #include <linux/usb/otg.h>
21 #include <linux/iio/consumer.h>
23 #define TWL4030_BCIMDEN 0x00
24 #define TWL4030_BCIMDKEY 0x01
25 #define TWL4030_BCIMSTATEC 0x02
26 #define TWL4030_BCIICHG 0x08
27 #define TWL4030_BCIVAC 0x0a
28 #define TWL4030_BCIVBUS 0x0c
29 #define TWL4030_BCIMFSTS3 0x0F
30 #define TWL4030_BCIMFSTS4 0x10
31 #define TWL4030_BCICTL1 0x23
32 #define TWL4030_BB_CFG 0x12
33 #define TWL4030_BCIIREF1 0x27
34 #define TWL4030_BCIIREF2 0x28
35 #define TWL4030_BCIMFKEY 0x11
36 #define TWL4030_BCIMFEN3 0x14
37 #define TWL4030_BCIMFTH8 0x1d
38 #define TWL4030_BCIMFTH9 0x1e
39 #define TWL4030_BCIWDKEY 0x21
41 #define TWL4030_BCIMFSTS1 0x01
43 #define TWL4030_BCIAUTOWEN BIT(5)
44 #define TWL4030_CONFIG_DONE BIT(4)
45 #define TWL4030_CVENAC BIT(2)
46 #define TWL4030_BCIAUTOUSB BIT(1)
47 #define TWL4030_BCIAUTOAC BIT(0)
48 #define TWL4030_CGAIN BIT(5)
49 #define TWL4030_USBFASTMCHG BIT(2)
50 #define TWL4030_STS_VBUS BIT(7)
51 #define TWL4030_STS_USB_ID BIT(2)
52 #define TWL4030_BBCHEN BIT(4)
53 #define TWL4030_BBSEL_MASK 0x0c
54 #define TWL4030_BBSEL_2V5 0x00
55 #define TWL4030_BBSEL_3V0 0x04
56 #define TWL4030_BBSEL_3V1 0x08
57 #define TWL4030_BBSEL_3V2 0x0c
58 #define TWL4030_BBISEL_MASK 0x03
59 #define TWL4030_BBISEL_25uA 0x00
60 #define TWL4030_BBISEL_150uA 0x01
61 #define TWL4030_BBISEL_500uA 0x02
62 #define TWL4030_BBISEL_1000uA 0x03
64 #define TWL4030_BATSTSPCHG BIT(2)
65 #define TWL4030_BATSTSMCHG BIT(6)
68 #define TWL4030_WOVF BIT(0) /* Watchdog overflow */
69 #define TWL4030_TMOVF BIT(1) /* Timer overflow */
70 #define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
71 #define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
72 #define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
73 #define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
74 #define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
75 #define TWL4030_BATSTS BIT(7) /* Battery status */
77 #define TWL4030_VBATLVL BIT(0) /* VBAT level */
78 #define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */
79 #define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */
80 #define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */
82 #define TWL4030_MSTATEC_USB BIT(4)
83 #define TWL4030_MSTATEC_AC BIT(5)
84 #define TWL4030_MSTATEC_MASK 0x0f
85 #define TWL4030_MSTATEC_QUICK1 0x02
86 #define TWL4030_MSTATEC_QUICK7 0x07
87 #define TWL4030_MSTATEC_COMPLETE1 0x0b
88 #define TWL4030_MSTATEC_COMPLETE4 0x0e
91 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
92 * then AC is available.
94 static inline int ac_available(struct iio_channel
*channel_vac
)
101 err
= iio_read_channel_processed(channel_vac
, &val
);
107 static bool allow_usb
;
108 module_param(allow_usb
, bool, 0644);
109 MODULE_PARM_DESC(allow_usb
, "Allow USB charge drawing default current");
113 struct power_supply
*ac
;
114 struct power_supply
*usb
;
115 struct usb_phy
*transceiver
;
116 struct notifier_block usb_nb
;
117 struct work_struct work
;
123 * ichg_* and *_cur values in uA. If any are 'large', we set
124 * CGAIN to '1' which doubles the range for half the
127 unsigned int ichg_eoc
, ichg_lo
, ichg_hi
;
128 unsigned int usb_cur
, ac_cur
;
129 struct iio_channel
*channel_vac
;
131 int usb_mode
, ac_mode
; /* charging mode requested */
133 #define CHARGE_AUTO 1
134 #define CHARGE_LINEAR 2
136 /* When setting the USB current we slowly increase the
137 * requested current until target is reached or the voltage
138 * drops below 4.75V. In the latter case we step back one
141 unsigned int usb_cur_target
;
142 struct delayed_work current_worker
;
143 #define USB_CUR_STEP 20000 /* 20mA at a time */
144 #define USB_MIN_VOLT 4750000 /* 4.75V */
145 #define USB_CUR_DELAY msecs_to_jiffies(100)
146 #define USB_MAX_CURRENT 1700000 /* TWL4030 caps at 1.7A */
151 /* strings for 'usb_mode' values */
152 static const char *modes
[] = { "off", "auto", "continuous" };
155 * clear and set bits on an given register on a given module
157 static int twl4030_clear_set(u8 mod_no
, u8 clear
, u8 set
, u8 reg
)
162 ret
= twl_i2c_read_u8(mod_no
, &val
, reg
);
169 return twl_i2c_write_u8(mod_no
, val
, reg
);
172 static int twl4030_bci_read(u8 reg
, u8
*val
)
174 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE
, val
, reg
);
177 static int twl4030_clear_set_boot_bci(u8 clear
, u8 set
)
179 return twl4030_clear_set(TWL_MODULE_PM_MASTER
, clear
,
180 TWL4030_CONFIG_DONE
| TWL4030_BCIAUTOWEN
| set
,
181 TWL4030_PM_MASTER_BOOT_BCI
);
184 static int twl4030bci_read_adc_val(u8 reg
)
190 ret
= twl4030_bci_read(reg
+ 1, &val
);
194 temp
= (int)(val
& 0x03) << 8;
197 ret
= twl4030_bci_read(reg
, &val
);
205 * TI provided formulas:
206 * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
207 * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
208 * Here we use integer approximation of:
209 * CGAIN == 0: val * 1.6618 - 0.85 * 1000
210 * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2
213 * convert twl register value for currents into uA
215 static int regval2ua(int regval
, bool cgain
)
218 return (regval
* 16618 - 8500 * 1000) / 5;
220 return (regval
* 16618 - 8500 * 1000) / 10;
224 * convert uA currents into twl register value
226 static int ua2regval(int ua
, bool cgain
)
231 ret
= (ua
* 10 + 8500 * 1000) / 16618;
232 /* rounding problems */
238 static int twl4030_charger_update_current(struct twl4030_bci
*bci
)
242 unsigned reg
, cur_reg
;
243 u8 bcictl1
, oldreg
, fullreg
;
248 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
249 * and AC is enabled, set current for 'ac'
251 if (ac_available(bci
->channel_vac
)) {
253 bci
->ac_is_active
= true;
256 bci
->ac_is_active
= false;
257 if (cur
> bci
->usb_cur_target
) {
258 cur
= bci
->usb_cur_target
;
261 if (cur
< bci
->usb_cur_target
)
262 schedule_delayed_work(&bci
->current_worker
, USB_CUR_DELAY
);
265 /* First, check thresholds and see if cgain is needed */
266 if (bci
->ichg_eoc
>= 200000)
268 if (bci
->ichg_lo
>= 400000)
270 if (bci
->ichg_hi
>= 820000)
275 status
= twl4030_bci_read(TWL4030_BCICTL1
, &bcictl1
);
278 if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER
, &boot_bci
,
279 TWL4030_PM_MASTER_BOOT_BCI
) < 0)
283 if ((!!cgain
) != !!(bcictl1
& TWL4030_CGAIN
))
284 /* Need to turn for charging while we change the
285 * CGAIN bit. Leave it off while everything is
288 twl4030_clear_set_boot_bci(boot_bci
, 0);
291 * For ichg_eoc, the hardware only supports reg values matching
292 * 100XXXX000, and requires the XXXX be stored in the high nibble
293 * of TWL4030_BCIMFTH8.
295 reg
= ua2regval(bci
->ichg_eoc
, cgain
);
300 reg
= (reg
>> 3) & 0xf;
304 * For ichg_lo, reg value must match 10XXXX0000.
305 * XXXX is stored in low nibble of TWL4030_BCIMFTH8.
307 reg
= ua2regval(bci
->ichg_lo
, cgain
);
312 reg
= (reg
>> 4) & 0xf;
315 /* ichg_eoc and ichg_lo live in same register */
316 status
= twl4030_bci_read(TWL4030_BCIMFTH8
, &oldreg
);
319 if (oldreg
!= fullreg
) {
320 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xF4,
324 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
325 fullreg
, TWL4030_BCIMFTH8
);
328 /* ichg_hi threshold must be 1XXXX01100 (I think) */
329 reg
= ua2regval(bci
->ichg_hi
, cgain
);
334 fullreg
= (reg
>> 5) & 0xF;
336 status
= twl4030_bci_read(TWL4030_BCIMFTH9
, &oldreg
);
339 if ((oldreg
& 0xF0) != fullreg
) {
340 fullreg
|= (oldreg
& 0x0F);
341 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xE7,
345 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
346 fullreg
, TWL4030_BCIMFTH9
);
350 * And finally, set the current. This is stored in
353 reg
= ua2regval(cur
, cgain
);
354 /* we have only 10 bits */
357 status
= twl4030_bci_read(TWL4030_BCIIREF1
, &oldreg
);
361 status
= twl4030_bci_read(TWL4030_BCIIREF2
, &oldreg
);
364 cur_reg
|= oldreg
<< 8;
366 /* disable write protection for one write access for
368 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xE7,
372 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
373 (reg
& 0x100) ? 3 : 2,
377 /* disable write protection for one write access for
379 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xE7,
383 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
387 if ((!!cgain
) != !!(bcictl1
& TWL4030_CGAIN
)) {
388 /* Flip CGAIN and re-enable charging */
389 bcictl1
^= TWL4030_CGAIN
;
390 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
391 bcictl1
, TWL4030_BCICTL1
);
392 twl4030_clear_set_boot_bci(0, boot_bci
);
397 static int twl4030_charger_get_current(void);
399 static void twl4030_current_worker(struct work_struct
*data
)
403 struct twl4030_bci
*bci
= container_of(data
, struct twl4030_bci
,
404 current_worker
.work
);
406 res
= twl4030bci_read_adc_val(TWL4030_BCIVBUS
);
410 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
412 curr
= twl4030_charger_get_current();
414 dev_dbg(bci
->dev
, "v=%d cur=%d limit=%d target=%d\n", v
, curr
,
415 bci
->usb_cur
, bci
->usb_cur_target
);
417 if (v
< USB_MIN_VOLT
) {
418 /* Back up and stop adjusting. */
419 if (bci
->usb_cur
>= USB_CUR_STEP
)
420 bci
->usb_cur
-= USB_CUR_STEP
;
421 bci
->usb_cur_target
= bci
->usb_cur
;
422 } else if (bci
->usb_cur
>= bci
->usb_cur_target
||
423 bci
->usb_cur
+ USB_CUR_STEP
> USB_MAX_CURRENT
) {
424 /* Reached target and voltage is OK - stop */
427 bci
->usb_cur
+= USB_CUR_STEP
;
428 schedule_delayed_work(&bci
->current_worker
, USB_CUR_DELAY
);
430 twl4030_charger_update_current(bci
);
434 * Enable/Disable USB Charge functionality.
436 static int twl4030_charger_enable_usb(struct twl4030_bci
*bci
, bool enable
)
441 if (bci
->usb_mode
== CHARGE_OFF
)
443 if (enable
&& !IS_ERR_OR_NULL(bci
->transceiver
)) {
445 twl4030_charger_update_current(bci
);
447 /* Need to keep phy powered */
448 if (!bci
->usb_enabled
) {
449 pm_runtime_get_sync(bci
->transceiver
->dev
);
450 bci
->usb_enabled
= 1;
453 if (bci
->usb_mode
== CHARGE_AUTO
) {
454 /* Enable interrupts now. */
455 reg
= ~(u32
)(TWL4030_ICHGLOW
| TWL4030_ICHGEOC
|
456 TWL4030_TBATOR2
| TWL4030_TBATOR1
|
458 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
459 TWL4030_INTERRUPTS_BCIIMR1A
);
462 "failed to unmask interrupts: %d\n",
466 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
467 ret
= twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB
);
470 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
471 ret
= twl4030_clear_set(TWL_MODULE_MAIN_CHARGE
, 0,
472 TWL4030_USBFASTMCHG
, TWL4030_BCIMFSTS4
);
473 if (bci
->usb_mode
== CHARGE_LINEAR
) {
474 /* Enable interrupts now. */
475 reg
= ~(u32
)(TWL4030_ICHGLOW
| TWL4030_TBATOR2
|
476 TWL4030_TBATOR1
| TWL4030_BATSTS
);
477 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
478 TWL4030_INTERRUPTS_BCIIMR1A
);
481 "failed to unmask interrupts: %d\n",
485 twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC
|TWL4030_CVENAC
, 0);
486 /* Watch dog key: WOVF acknowledge */
487 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x33,
489 /* 0x24 + EKEY6: off mode */
490 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x2a,
492 /* EKEY2: Linear charge: USB path */
493 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x26,
495 /* WDKEY5: stop watchdog count */
496 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xf3,
498 /* enable MFEN3 access */
499 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x9c,
501 /* ICHGEOCEN - end-of-charge monitor (current < 80mA)
502 * (charging continues)
503 * ICHGLOWEN - current level monitor (charge continues)
504 * don't monitor over-current or heat save
506 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xf0,
510 ret
= twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB
, 0);
511 ret
|= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x2a,
513 if (bci
->usb_enabled
) {
514 pm_runtime_mark_last_busy(bci
->transceiver
->dev
);
515 pm_runtime_put_autosuspend(bci
->transceiver
->dev
);
516 bci
->usb_enabled
= 0;
525 * Enable/Disable AC Charge funtionality.
527 static int twl4030_charger_enable_ac(struct twl4030_bci
*bci
, bool enable
)
531 if (bci
->ac_mode
== CHARGE_OFF
)
535 ret
= twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC
);
537 ret
= twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC
, 0);
543 * Enable/Disable charging of Backup Battery.
545 static int twl4030_charger_enable_backup(int uvolt
, int uamp
)
550 if (uvolt
< 2500000 ||
552 /* disable charging of backup battery */
553 ret
= twl4030_clear_set(TWL_MODULE_PM_RECEIVER
,
554 TWL4030_BBCHEN
, 0, TWL4030_BB_CFG
);
558 flags
= TWL4030_BBCHEN
;
559 if (uvolt
>= 3200000)
560 flags
|= TWL4030_BBSEL_3V2
;
561 else if (uvolt
>= 3100000)
562 flags
|= TWL4030_BBSEL_3V1
;
563 else if (uvolt
>= 3000000)
564 flags
|= TWL4030_BBSEL_3V0
;
566 flags
|= TWL4030_BBSEL_2V5
;
569 flags
|= TWL4030_BBISEL_1000uA
;
570 else if (uamp
>= 500)
571 flags
|= TWL4030_BBISEL_500uA
;
572 else if (uamp
>= 150)
573 flags
|= TWL4030_BBISEL_150uA
;
575 flags
|= TWL4030_BBISEL_25uA
;
577 ret
= twl4030_clear_set(TWL_MODULE_PM_RECEIVER
,
578 TWL4030_BBSEL_MASK
| TWL4030_BBISEL_MASK
,
586 * TWL4030 CHG_PRES (AC charger presence) events
588 static irqreturn_t
twl4030_charger_interrupt(int irq
, void *arg
)
590 struct twl4030_bci
*bci
= arg
;
592 dev_dbg(bci
->dev
, "CHG_PRES irq\n");
593 /* reset current on each 'plug' event */
594 bci
->ac_cur
= 500000;
595 twl4030_charger_update_current(bci
);
596 power_supply_changed(bci
->ac
);
597 power_supply_changed(bci
->usb
);
603 * TWL4030 BCI monitoring events
605 static irqreturn_t
twl4030_bci_interrupt(int irq
, void *arg
)
607 struct twl4030_bci
*bci
= arg
;
611 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS
, &irqs1
,
612 TWL4030_INTERRUPTS_BCIISR1A
);
616 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS
, &irqs2
,
617 TWL4030_INTERRUPTS_BCIISR2A
);
621 dev_dbg(bci
->dev
, "BCI irq %02x %02x\n", irqs2
, irqs1
);
623 if (irqs1
& (TWL4030_ICHGLOW
| TWL4030_ICHGEOC
)) {
624 /* charger state change, inform the core */
625 power_supply_changed(bci
->ac
);
626 power_supply_changed(bci
->usb
);
628 twl4030_charger_update_current(bci
);
630 /* various monitoring events, for now we just log them here */
631 if (irqs1
& (TWL4030_TBATOR2
| TWL4030_TBATOR1
))
632 dev_warn(bci
->dev
, "battery temperature out of range\n");
634 if (irqs1
& TWL4030_BATSTS
)
635 dev_crit(bci
->dev
, "battery disconnected\n");
637 if (irqs2
& TWL4030_VBATOV
)
638 dev_crit(bci
->dev
, "VBAT overvoltage\n");
640 if (irqs2
& TWL4030_VBUSOV
)
641 dev_crit(bci
->dev
, "VBUS overvoltage\n");
643 if (irqs2
& TWL4030_ACCHGOV
)
644 dev_crit(bci
->dev
, "Ac charger overvoltage\n");
649 static void twl4030_bci_usb_work(struct work_struct
*data
)
651 struct twl4030_bci
*bci
= container_of(data
, struct twl4030_bci
, work
);
653 switch (bci
->event
) {
655 case USB_EVENT_CHARGER
:
656 twl4030_charger_enable_usb(bci
, true);
659 twl4030_charger_enable_usb(bci
, false);
664 static int twl4030_bci_usb_ncb(struct notifier_block
*nb
, unsigned long val
,
667 struct twl4030_bci
*bci
= container_of(nb
, struct twl4030_bci
, usb_nb
);
669 dev_dbg(bci
->dev
, "OTG notify %lu\n", val
);
671 /* reset current on each 'plug' event */
673 bci
->usb_cur_target
= 500000;
675 bci
->usb_cur_target
= 100000;
678 schedule_work(&bci
->work
);
684 * sysfs charger enabled store
687 twl4030_bci_mode_store(struct device
*dev
, struct device_attribute
*attr
,
688 const char *buf
, size_t n
)
690 struct twl4030_bci
*bci
= dev_get_drvdata(dev
->parent
);
694 mode
= sysfs_match_string(modes
, buf
);
698 if (dev
== &bci
->ac
->dev
) {
701 twl4030_charger_enable_ac(bci
, false);
703 status
= twl4030_charger_enable_ac(bci
, true);
705 twl4030_charger_enable_usb(bci
, false);
706 bci
->usb_mode
= mode
;
707 status
= twl4030_charger_enable_usb(bci
, true);
709 return (status
== 0) ? n
: status
;
713 * sysfs charger enabled show
716 twl4030_bci_mode_show(struct device
*dev
,
717 struct device_attribute
*attr
, char *buf
)
719 struct twl4030_bci
*bci
= dev_get_drvdata(dev
->parent
);
722 int mode
= bci
->usb_mode
;
724 if (dev
== &bci
->ac
->dev
)
727 for (i
= 0; i
< ARRAY_SIZE(modes
); i
++)
729 len
+= snprintf(buf
+len
, PAGE_SIZE
-len
,
732 len
+= snprintf(buf
+len
, PAGE_SIZE
-len
,
737 static DEVICE_ATTR(mode
, 0644, twl4030_bci_mode_show
,
738 twl4030_bci_mode_store
);
740 static int twl4030_charger_get_current(void)
746 curr
= twl4030bci_read_adc_val(TWL4030_BCIICHG
);
750 ret
= twl4030_bci_read(TWL4030_BCICTL1
, &bcictl1
);
754 return regval2ua(curr
, bcictl1
& TWL4030_CGAIN
);
758 * Returns the main charge FSM state
761 static int twl4030bci_state(struct twl4030_bci
*bci
)
766 ret
= twl4030_bci_read(TWL4030_BCIMSTATEC
, &state
);
768 dev_err(bci
->dev
, "error reading BCIMSTATEC\n");
772 dev_dbg(bci
->dev
, "state: %02x\n", state
);
777 static int twl4030_bci_state_to_status(int state
)
779 state
&= TWL4030_MSTATEC_MASK
;
780 if (TWL4030_MSTATEC_QUICK1
<= state
&& state
<= TWL4030_MSTATEC_QUICK7
)
781 return POWER_SUPPLY_STATUS_CHARGING
;
782 else if (TWL4030_MSTATEC_COMPLETE1
<= state
&&
783 state
<= TWL4030_MSTATEC_COMPLETE4
)
784 return POWER_SUPPLY_STATUS_FULL
;
786 return POWER_SUPPLY_STATUS_NOT_CHARGING
;
789 static int twl4030_bci_get_property(struct power_supply
*psy
,
790 enum power_supply_property psp
,
791 union power_supply_propval
*val
)
793 struct twl4030_bci
*bci
= dev_get_drvdata(psy
->dev
.parent
);
798 state
= twl4030bci_state(bci
);
802 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
803 is_charging
= state
& TWL4030_MSTATEC_USB
;
805 is_charging
= state
& TWL4030_MSTATEC_AC
;
808 ret
= twl4030_bci_read(TWL4030_BCIMDEN
, &s
);
811 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
816 /* A little white lie */
817 state
= TWL4030_MSTATEC_QUICK1
;
821 case POWER_SUPPLY_PROP_STATUS
:
823 val
->intval
= twl4030_bci_state_to_status(state
);
825 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
827 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
828 /* charging must be active for meaningful result */
831 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
) {
832 ret
= twl4030bci_read_adc_val(TWL4030_BCIVBUS
);
835 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
836 val
->intval
= ret
* 6843;
838 ret
= twl4030bci_read_adc_val(TWL4030_BCIVAC
);
841 /* BCIVAC uses ADCIN11, 10/1023 V/step */
842 val
->intval
= ret
* 9775;
845 case POWER_SUPPLY_PROP_CURRENT_NOW
:
848 /* current measurement is shared between AC and USB */
849 ret
= twl4030_charger_get_current();
854 case POWER_SUPPLY_PROP_ONLINE
:
855 val
->intval
= is_charging
&&
856 twl4030_bci_state_to_status(state
) !=
857 POWER_SUPPLY_STATUS_NOT_CHARGING
;
859 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
861 if (psy
->desc
->type
!= POWER_SUPPLY_TYPE_USB
) {
862 if (!bci
->ac_is_active
)
863 val
->intval
= bci
->ac_cur
;
865 if (bci
->ac_is_active
)
866 val
->intval
= bci
->usb_cur_target
;
868 if (val
->intval
< 0) {
871 val
->intval
= twl4030bci_read_adc_val(TWL4030_BCIIREF1
);
874 ret
= twl4030_bci_read(TWL4030_BCICTL1
, &bcictl1
);
877 val
->intval
= regval2ua(val
->intval
, bcictl1
&
888 static int twl4030_bci_set_property(struct power_supply
*psy
,
889 enum power_supply_property psp
,
890 const union power_supply_propval
*val
)
892 struct twl4030_bci
*bci
= dev_get_drvdata(psy
->dev
.parent
);
895 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
896 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
897 bci
->usb_cur_target
= val
->intval
;
899 bci
->ac_cur
= val
->intval
;
900 twl4030_charger_update_current(bci
);
909 static int twl4030_bci_property_is_writeable(struct power_supply
*psy
,
910 enum power_supply_property psp
)
913 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
920 static enum power_supply_property twl4030_charger_props
[] = {
921 POWER_SUPPLY_PROP_STATUS
,
922 POWER_SUPPLY_PROP_ONLINE
,
923 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
924 POWER_SUPPLY_PROP_CURRENT_NOW
,
925 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
929 static const struct twl4030_bci_platform_data
*
930 twl4030_bci_parse_dt(struct device
*dev
)
932 struct device_node
*np
= dev
->of_node
;
933 struct twl4030_bci_platform_data
*pdata
;
938 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
942 if (of_property_read_u32(np
, "ti,bb-uvolt", &num
) == 0)
943 pdata
->bb_uvolt
= num
;
944 if (of_property_read_u32(np
, "ti,bb-uamp", &num
) == 0)
945 pdata
->bb_uamp
= num
;
949 static inline const struct twl4030_bci_platform_data
*
950 twl4030_bci_parse_dt(struct device
*dev
)
956 static const struct power_supply_desc twl4030_bci_ac_desc
= {
957 .name
= "twl4030_ac",
958 .type
= POWER_SUPPLY_TYPE_MAINS
,
959 .properties
= twl4030_charger_props
,
960 .num_properties
= ARRAY_SIZE(twl4030_charger_props
),
961 .get_property
= twl4030_bci_get_property
,
962 .set_property
= twl4030_bci_set_property
,
963 .property_is_writeable
= twl4030_bci_property_is_writeable
,
966 static const struct power_supply_desc twl4030_bci_usb_desc
= {
967 .name
= "twl4030_usb",
968 .type
= POWER_SUPPLY_TYPE_USB
,
969 .properties
= twl4030_charger_props
,
970 .num_properties
= ARRAY_SIZE(twl4030_charger_props
),
971 .get_property
= twl4030_bci_get_property
,
972 .set_property
= twl4030_bci_set_property
,
973 .property_is_writeable
= twl4030_bci_property_is_writeable
,
976 static int twl4030_bci_probe(struct platform_device
*pdev
)
978 struct twl4030_bci
*bci
;
979 const struct twl4030_bci_platform_data
*pdata
= pdev
->dev
.platform_data
;
983 bci
= devm_kzalloc(&pdev
->dev
, sizeof(*bci
), GFP_KERNEL
);
988 pdata
= twl4030_bci_parse_dt(&pdev
->dev
);
990 bci
->ichg_eoc
= 80100; /* Stop charging when current drops to here */
991 bci
->ichg_lo
= 241000; /* Low threshold */
992 bci
->ichg_hi
= 500000; /* High threshold */
993 bci
->ac_cur
= 500000; /* 500mA */
995 bci
->usb_cur_target
= 500000; /* 500mA */
997 bci
->usb_cur_target
= 100000; /* 100mA */
998 bci
->usb_mode
= CHARGE_AUTO
;
999 bci
->ac_mode
= CHARGE_AUTO
;
1001 bci
->dev
= &pdev
->dev
;
1002 bci
->irq_chg
= platform_get_irq(pdev
, 0);
1003 bci
->irq_bci
= platform_get_irq(pdev
, 1);
1005 platform_set_drvdata(pdev
, bci
);
1007 INIT_WORK(&bci
->work
, twl4030_bci_usb_work
);
1008 INIT_DELAYED_WORK(&bci
->current_worker
, twl4030_current_worker
);
1010 bci
->channel_vac
= devm_iio_channel_get(&pdev
->dev
, "vac");
1011 if (IS_ERR(bci
->channel_vac
)) {
1012 ret
= PTR_ERR(bci
->channel_vac
);
1013 if (ret
== -EPROBE_DEFER
)
1014 return ret
; /* iio not ready */
1015 dev_warn(&pdev
->dev
, "could not request vac iio channel (%d)",
1017 bci
->channel_vac
= NULL
;
1020 if (bci
->dev
->of_node
) {
1021 struct device_node
*phynode
;
1023 phynode
= of_get_compatible_child(bci
->dev
->of_node
->parent
,
1026 bci
->usb_nb
.notifier_call
= twl4030_bci_usb_ncb
;
1027 bci
->transceiver
= devm_usb_get_phy_by_node(
1028 bci
->dev
, phynode
, &bci
->usb_nb
);
1029 of_node_put(phynode
);
1030 if (IS_ERR(bci
->transceiver
)) {
1031 ret
= PTR_ERR(bci
->transceiver
);
1032 if (ret
== -EPROBE_DEFER
)
1033 return ret
; /* phy not ready */
1034 dev_warn(&pdev
->dev
, "could not request transceiver (%d)",
1036 bci
->transceiver
= NULL
;
1041 bci
->ac
= devm_power_supply_register(&pdev
->dev
, &twl4030_bci_ac_desc
,
1043 if (IS_ERR(bci
->ac
)) {
1044 ret
= PTR_ERR(bci
->ac
);
1045 dev_err(&pdev
->dev
, "failed to register ac: %d\n", ret
);
1049 bci
->usb
= devm_power_supply_register(&pdev
->dev
, &twl4030_bci_usb_desc
,
1051 if (IS_ERR(bci
->usb
)) {
1052 ret
= PTR_ERR(bci
->usb
);
1053 dev_err(&pdev
->dev
, "failed to register usb: %d\n", ret
);
1057 ret
= devm_request_threaded_irq(&pdev
->dev
, bci
->irq_chg
, NULL
,
1058 twl4030_charger_interrupt
, IRQF_ONESHOT
, pdev
->name
,
1061 dev_err(&pdev
->dev
, "could not request irq %d, status %d\n",
1066 ret
= devm_request_threaded_irq(&pdev
->dev
, bci
->irq_bci
, NULL
,
1067 twl4030_bci_interrupt
, IRQF_ONESHOT
, pdev
->name
, bci
);
1069 dev_err(&pdev
->dev
, "could not request irq %d, status %d\n",
1074 /* Enable interrupts now. */
1075 reg
= ~(u32
)(TWL4030_ICHGLOW
| TWL4030_ICHGEOC
| TWL4030_TBATOR2
|
1076 TWL4030_TBATOR1
| TWL4030_BATSTS
);
1077 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
1078 TWL4030_INTERRUPTS_BCIIMR1A
);
1080 dev_err(&pdev
->dev
, "failed to unmask interrupts: %d\n", ret
);
1084 reg
= ~(u32
)(TWL4030_VBATOV
| TWL4030_VBUSOV
| TWL4030_ACCHGOV
);
1085 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
1086 TWL4030_INTERRUPTS_BCIIMR2A
);
1088 dev_warn(&pdev
->dev
, "failed to unmask interrupts: %d\n", ret
);
1090 twl4030_charger_update_current(bci
);
1091 if (device_create_file(&bci
->usb
->dev
, &dev_attr_mode
))
1092 dev_warn(&pdev
->dev
, "could not create sysfs file\n");
1093 if (device_create_file(&bci
->ac
->dev
, &dev_attr_mode
))
1094 dev_warn(&pdev
->dev
, "could not create sysfs file\n");
1096 twl4030_charger_enable_ac(bci
, true);
1097 if (!IS_ERR_OR_NULL(bci
->transceiver
))
1098 twl4030_bci_usb_ncb(&bci
->usb_nb
,
1099 bci
->transceiver
->last_event
,
1102 twl4030_charger_enable_usb(bci
, false);
1104 twl4030_charger_enable_backup(pdata
->bb_uvolt
,
1107 twl4030_charger_enable_backup(0, 0);
1112 static int twl4030_bci_remove(struct platform_device
*pdev
)
1114 struct twl4030_bci
*bci
= platform_get_drvdata(pdev
);
1116 twl4030_charger_enable_ac(bci
, false);
1117 twl4030_charger_enable_usb(bci
, false);
1118 twl4030_charger_enable_backup(0, 0);
1120 device_remove_file(&bci
->usb
->dev
, &dev_attr_mode
);
1121 device_remove_file(&bci
->ac
->dev
, &dev_attr_mode
);
1122 /* mask interrupts */
1123 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, 0xff,
1124 TWL4030_INTERRUPTS_BCIIMR1A
);
1125 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, 0xff,
1126 TWL4030_INTERRUPTS_BCIIMR2A
);
1131 static const struct of_device_id twl_bci_of_match
[] = {
1132 {.compatible
= "ti,twl4030-bci", },
1135 MODULE_DEVICE_TABLE(of
, twl_bci_of_match
);
1137 static struct platform_driver twl4030_bci_driver
= {
1138 .probe
= twl4030_bci_probe
,
1139 .remove
= twl4030_bci_remove
,
1141 .name
= "twl4030_bci",
1142 .of_match_table
= of_match_ptr(twl_bci_of_match
),
1145 module_platform_driver(twl4030_bci_driver
);
1147 MODULE_AUTHOR("Gražvydas Ignotas");
1148 MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
1149 MODULE_LICENSE("GPL");
1150 MODULE_ALIAS("platform:twl4030_bci");