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>
16 #include <linux/platform_device.h>
17 #include <linux/interrupt.h>
18 #include <linux/mfd/twl.h>
19 #include <linux/power_supply.h>
20 #include <linux/notifier.h>
21 #include <linux/usb/otg.h>
22 #include <linux/iio/consumer.h>
24 #define TWL4030_BCIMDEN 0x00
25 #define TWL4030_BCIMDKEY 0x01
26 #define TWL4030_BCIMSTATEC 0x02
27 #define TWL4030_BCIICHG 0x08
28 #define TWL4030_BCIVAC 0x0a
29 #define TWL4030_BCIVBUS 0x0c
30 #define TWL4030_BCIMFSTS3 0x0F
31 #define TWL4030_BCIMFSTS4 0x10
32 #define TWL4030_BCICTL1 0x23
33 #define TWL4030_BB_CFG 0x12
34 #define TWL4030_BCIIREF1 0x27
35 #define TWL4030_BCIIREF2 0x28
36 #define TWL4030_BCIMFKEY 0x11
37 #define TWL4030_BCIMFEN3 0x14
38 #define TWL4030_BCIMFTH8 0x1d
39 #define TWL4030_BCIMFTH9 0x1e
40 #define TWL4030_BCIWDKEY 0x21
42 #define TWL4030_BCIMFSTS1 0x01
44 #define TWL4030_BCIAUTOWEN BIT(5)
45 #define TWL4030_CONFIG_DONE BIT(4)
46 #define TWL4030_CVENAC BIT(2)
47 #define TWL4030_BCIAUTOUSB BIT(1)
48 #define TWL4030_BCIAUTOAC BIT(0)
49 #define TWL4030_CGAIN BIT(5)
50 #define TWL4030_USBFASTMCHG BIT(2)
51 #define TWL4030_STS_VBUS BIT(7)
52 #define TWL4030_STS_USB_ID BIT(2)
53 #define TWL4030_BBCHEN BIT(4)
54 #define TWL4030_BBSEL_MASK 0x0c
55 #define TWL4030_BBSEL_2V5 0x00
56 #define TWL4030_BBSEL_3V0 0x04
57 #define TWL4030_BBSEL_3V1 0x08
58 #define TWL4030_BBSEL_3V2 0x0c
59 #define TWL4030_BBISEL_MASK 0x03
60 #define TWL4030_BBISEL_25uA 0x00
61 #define TWL4030_BBISEL_150uA 0x01
62 #define TWL4030_BBISEL_500uA 0x02
63 #define TWL4030_BBISEL_1000uA 0x03
65 #define TWL4030_BATSTSPCHG BIT(2)
66 #define TWL4030_BATSTSMCHG BIT(6)
69 #define TWL4030_WOVF BIT(0) /* Watchdog overflow */
70 #define TWL4030_TMOVF BIT(1) /* Timer overflow */
71 #define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
72 #define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
73 #define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
74 #define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
75 #define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
76 #define TWL4030_BATSTS BIT(7) /* Battery status */
78 #define TWL4030_VBATLVL BIT(0) /* VBAT level */
79 #define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */
80 #define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */
81 #define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */
83 #define TWL4030_MSTATEC_USB BIT(4)
84 #define TWL4030_MSTATEC_AC BIT(5)
85 #define TWL4030_MSTATEC_MASK 0x0f
86 #define TWL4030_MSTATEC_QUICK1 0x02
87 #define TWL4030_MSTATEC_QUICK7 0x07
88 #define TWL4030_MSTATEC_COMPLETE1 0x0b
89 #define TWL4030_MSTATEC_COMPLETE4 0x0e
92 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
93 * then AC is available.
95 static inline int ac_available(struct iio_channel
*channel_vac
)
102 err
= iio_read_channel_processed(channel_vac
, &val
);
108 static bool allow_usb
;
109 module_param(allow_usb
, bool, 0644);
110 MODULE_PARM_DESC(allow_usb
, "Allow USB charge drawing default current");
114 struct power_supply
*ac
;
115 struct power_supply
*usb
;
116 struct usb_phy
*transceiver
;
117 struct notifier_block usb_nb
;
118 struct work_struct work
;
124 * ichg_* and *_cur values in uA. If any are 'large', we set
125 * CGAIN to '1' which doubles the range for half the
128 unsigned int ichg_eoc
, ichg_lo
, ichg_hi
;
129 unsigned int usb_cur
, ac_cur
;
130 struct iio_channel
*channel_vac
;
132 int usb_mode
, ac_mode
; /* charging mode requested */
134 #define CHARGE_AUTO 1
135 #define CHARGE_LINEAR 2
137 /* When setting the USB current we slowly increase the
138 * requested current until target is reached or the voltage
139 * drops below 4.75V. In the latter case we step back one
142 unsigned int usb_cur_target
;
143 struct delayed_work current_worker
;
144 #define USB_CUR_STEP 20000 /* 20mA at a time */
145 #define USB_MIN_VOLT 4750000 /* 4.75V */
146 #define USB_CUR_DELAY msecs_to_jiffies(100)
147 #define USB_MAX_CURRENT 1700000 /* TWL4030 caps at 1.7A */
152 /* strings for 'usb_mode' values */
153 static const char *modes
[] = { "off", "auto", "continuous" };
156 * clear and set bits on an given register on a given module
158 static int twl4030_clear_set(u8 mod_no
, u8 clear
, u8 set
, u8 reg
)
163 ret
= twl_i2c_read_u8(mod_no
, &val
, reg
);
170 return twl_i2c_write_u8(mod_no
, val
, reg
);
173 static int twl4030_bci_read(u8 reg
, u8
*val
)
175 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE
, val
, reg
);
178 static int twl4030_clear_set_boot_bci(u8 clear
, u8 set
)
180 return twl4030_clear_set(TWL_MODULE_PM_MASTER
, clear
,
181 TWL4030_CONFIG_DONE
| TWL4030_BCIAUTOWEN
| set
,
182 TWL4030_PM_MASTER_BOOT_BCI
);
185 static int twl4030bci_read_adc_val(u8 reg
)
191 ret
= twl4030_bci_read(reg
+ 1, &val
);
195 temp
= (int)(val
& 0x03) << 8;
198 ret
= twl4030_bci_read(reg
, &val
);
206 * TI provided formulas:
207 * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
208 * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
209 * Here we use integer approximation of:
210 * CGAIN == 0: val * 1.6618 - 0.85 * 1000
211 * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2
214 * convert twl register value for currents into uA
216 static int regval2ua(int regval
, bool cgain
)
219 return (regval
* 16618 - 8500 * 1000) / 5;
221 return (regval
* 16618 - 8500 * 1000) / 10;
225 * convert uA currents into twl register value
227 static int ua2regval(int ua
, bool cgain
)
232 ret
= (ua
* 10 + 8500 * 1000) / 16618;
233 /* rounding problems */
239 static int twl4030_charger_update_current(struct twl4030_bci
*bci
)
243 unsigned reg
, cur_reg
;
244 u8 bcictl1
, oldreg
, fullreg
;
249 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
250 * and AC is enabled, set current for 'ac'
252 if (ac_available(bci
->channel_vac
)) {
254 bci
->ac_is_active
= true;
257 bci
->ac_is_active
= false;
258 if (cur
> bci
->usb_cur_target
) {
259 cur
= bci
->usb_cur_target
;
262 if (cur
< bci
->usb_cur_target
)
263 schedule_delayed_work(&bci
->current_worker
, USB_CUR_DELAY
);
266 /* First, check thresholds and see if cgain is needed */
267 if (bci
->ichg_eoc
>= 200000)
269 if (bci
->ichg_lo
>= 400000)
271 if (bci
->ichg_hi
>= 820000)
276 status
= twl4030_bci_read(TWL4030_BCICTL1
, &bcictl1
);
279 if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER
, &boot_bci
,
280 TWL4030_PM_MASTER_BOOT_BCI
) < 0)
284 if ((!!cgain
) != !!(bcictl1
& TWL4030_CGAIN
))
285 /* Need to turn for charging while we change the
286 * CGAIN bit. Leave it off while everything is
289 twl4030_clear_set_boot_bci(boot_bci
, 0);
292 * For ichg_eoc, the hardware only supports reg values matching
293 * 100XXXX000, and requires the XXXX be stored in the high nibble
294 * of TWL4030_BCIMFTH8.
296 reg
= ua2regval(bci
->ichg_eoc
, cgain
);
301 reg
= (reg
>> 3) & 0xf;
305 * For ichg_lo, reg value must match 10XXXX0000.
306 * XXXX is stored in low nibble of TWL4030_BCIMFTH8.
308 reg
= ua2regval(bci
->ichg_lo
, cgain
);
313 reg
= (reg
>> 4) & 0xf;
316 /* ichg_eoc and ichg_lo live in same register */
317 status
= twl4030_bci_read(TWL4030_BCIMFTH8
, &oldreg
);
320 if (oldreg
!= fullreg
) {
321 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xF4,
325 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
326 fullreg
, TWL4030_BCIMFTH8
);
329 /* ichg_hi threshold must be 1XXXX01100 (I think) */
330 reg
= ua2regval(bci
->ichg_hi
, cgain
);
335 fullreg
= (reg
>> 5) & 0xF;
337 status
= twl4030_bci_read(TWL4030_BCIMFTH9
, &oldreg
);
340 if ((oldreg
& 0xF0) != fullreg
) {
341 fullreg
|= (oldreg
& 0x0F);
342 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xE7,
346 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
347 fullreg
, TWL4030_BCIMFTH9
);
351 * And finally, set the current. This is stored in
354 reg
= ua2regval(cur
, cgain
);
355 /* we have only 10 bits */
358 status
= twl4030_bci_read(TWL4030_BCIIREF1
, &oldreg
);
362 status
= twl4030_bci_read(TWL4030_BCIIREF2
, &oldreg
);
365 cur_reg
|= oldreg
<< 8;
366 if (reg
!= cur_reg
) {
367 /* disable write protection for one write access for
369 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xE7,
373 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
374 (reg
& 0x100) ? 3 : 2,
378 /* disable write protection for one write access for
380 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xE7,
384 status
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
388 if ((!!cgain
) != !!(bcictl1
& TWL4030_CGAIN
)) {
389 /* Flip CGAIN and re-enable charging */
390 bcictl1
^= TWL4030_CGAIN
;
391 twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
,
392 bcictl1
, TWL4030_BCICTL1
);
393 twl4030_clear_set_boot_bci(0, boot_bci
);
398 static int twl4030_charger_get_current(void);
400 static void twl4030_current_worker(struct work_struct
*data
)
404 struct twl4030_bci
*bci
= container_of(data
, struct twl4030_bci
,
405 current_worker
.work
);
407 res
= twl4030bci_read_adc_val(TWL4030_BCIVBUS
);
411 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
413 curr
= twl4030_charger_get_current();
415 dev_dbg(bci
->dev
, "v=%d cur=%d limit=%d target=%d\n", v
, curr
,
416 bci
->usb_cur
, bci
->usb_cur_target
);
418 if (v
< USB_MIN_VOLT
) {
419 /* Back up and stop adjusting. */
420 if (bci
->usb_cur
>= USB_CUR_STEP
)
421 bci
->usb_cur
-= USB_CUR_STEP
;
422 bci
->usb_cur_target
= bci
->usb_cur
;
423 } else if (bci
->usb_cur
>= bci
->usb_cur_target
||
424 bci
->usb_cur
+ USB_CUR_STEP
> USB_MAX_CURRENT
) {
425 /* Reached target and voltage is OK - stop */
428 bci
->usb_cur
+= USB_CUR_STEP
;
429 schedule_delayed_work(&bci
->current_worker
, USB_CUR_DELAY
);
431 twl4030_charger_update_current(bci
);
435 * Enable/Disable USB Charge functionality.
437 static int twl4030_charger_enable_usb(struct twl4030_bci
*bci
, bool enable
)
442 if (bci
->usb_mode
== CHARGE_OFF
)
444 if (enable
&& !IS_ERR_OR_NULL(bci
->transceiver
)) {
446 twl4030_charger_update_current(bci
);
448 /* Need to keep phy powered */
449 if (!bci
->usb_enabled
) {
450 pm_runtime_get_sync(bci
->transceiver
->dev
);
451 bci
->usb_enabled
= 1;
454 if (bci
->usb_mode
== CHARGE_AUTO
) {
455 /* Enable interrupts now. */
456 reg
= ~(u32
)(TWL4030_ICHGLOW
| TWL4030_ICHGEOC
|
457 TWL4030_TBATOR2
| TWL4030_TBATOR1
|
459 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
460 TWL4030_INTERRUPTS_BCIIMR1A
);
463 "failed to unmask interrupts: %d\n",
467 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
468 ret
= twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB
);
471 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
472 ret
= twl4030_clear_set(TWL_MODULE_MAIN_CHARGE
, 0,
473 TWL4030_USBFASTMCHG
, TWL4030_BCIMFSTS4
);
474 if (bci
->usb_mode
== CHARGE_LINEAR
) {
475 /* Enable interrupts now. */
476 reg
= ~(u32
)(TWL4030_ICHGLOW
| TWL4030_TBATOR2
|
477 TWL4030_TBATOR1
| TWL4030_BATSTS
);
478 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
479 TWL4030_INTERRUPTS_BCIIMR1A
);
482 "failed to unmask interrupts: %d\n",
486 twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC
|TWL4030_CVENAC
, 0);
487 /* Watch dog key: WOVF acknowledge */
488 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x33,
490 /* 0x24 + EKEY6: off mode */
491 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x2a,
493 /* EKEY2: Linear charge: USB path */
494 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x26,
496 /* WDKEY5: stop watchdog count */
497 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xf3,
499 /* enable MFEN3 access */
500 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x9c,
502 /* ICHGEOCEN - end-of-charge monitor (current < 80mA)
503 * (charging continues)
504 * ICHGLOWEN - current level monitor (charge continues)
505 * don't monitor over-current or heat save
507 ret
= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0xf0,
511 ret
= twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB
, 0);
512 ret
|= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, 0x2a,
514 if (bci
->usb_enabled
) {
515 pm_runtime_mark_last_busy(bci
->transceiver
->dev
);
516 pm_runtime_put_autosuspend(bci
->transceiver
->dev
);
517 bci
->usb_enabled
= 0;
526 * Enable/Disable AC Charge funtionality.
528 static int twl4030_charger_enable_ac(struct twl4030_bci
*bci
, bool enable
)
532 if (bci
->ac_mode
== CHARGE_OFF
)
536 ret
= twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC
);
538 ret
= twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC
, 0);
544 * Enable/Disable charging of Backup Battery.
546 static int twl4030_charger_enable_backup(int uvolt
, int uamp
)
551 if (uvolt
< 2500000 ||
553 /* disable charging of backup battery */
554 ret
= twl4030_clear_set(TWL_MODULE_PM_RECEIVER
,
555 TWL4030_BBCHEN
, 0, TWL4030_BB_CFG
);
559 flags
= TWL4030_BBCHEN
;
560 if (uvolt
>= 3200000)
561 flags
|= TWL4030_BBSEL_3V2
;
562 else if (uvolt
>= 3100000)
563 flags
|= TWL4030_BBSEL_3V1
;
564 else if (uvolt
>= 3000000)
565 flags
|= TWL4030_BBSEL_3V0
;
567 flags
|= TWL4030_BBSEL_2V5
;
570 flags
|= TWL4030_BBISEL_1000uA
;
571 else if (uamp
>= 500)
572 flags
|= TWL4030_BBISEL_500uA
;
573 else if (uamp
>= 150)
574 flags
|= TWL4030_BBISEL_150uA
;
576 flags
|= TWL4030_BBISEL_25uA
;
578 ret
= twl4030_clear_set(TWL_MODULE_PM_RECEIVER
,
579 TWL4030_BBSEL_MASK
| TWL4030_BBISEL_MASK
,
587 * TWL4030 CHG_PRES (AC charger presence) events
589 static irqreturn_t
twl4030_charger_interrupt(int irq
, void *arg
)
591 struct twl4030_bci
*bci
= arg
;
593 dev_dbg(bci
->dev
, "CHG_PRES irq\n");
594 /* reset current on each 'plug' event */
595 bci
->ac_cur
= 500000;
596 twl4030_charger_update_current(bci
);
597 power_supply_changed(bci
->ac
);
598 power_supply_changed(bci
->usb
);
604 * TWL4030 BCI monitoring events
606 static irqreturn_t
twl4030_bci_interrupt(int irq
, void *arg
)
608 struct twl4030_bci
*bci
= arg
;
612 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS
, &irqs1
,
613 TWL4030_INTERRUPTS_BCIISR1A
);
617 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS
, &irqs2
,
618 TWL4030_INTERRUPTS_BCIISR2A
);
622 dev_dbg(bci
->dev
, "BCI irq %02x %02x\n", irqs2
, irqs1
);
624 if (irqs1
& (TWL4030_ICHGLOW
| TWL4030_ICHGEOC
)) {
625 /* charger state change, inform the core */
626 power_supply_changed(bci
->ac
);
627 power_supply_changed(bci
->usb
);
629 twl4030_charger_update_current(bci
);
631 /* various monitoring events, for now we just log them here */
632 if (irqs1
& (TWL4030_TBATOR2
| TWL4030_TBATOR1
))
633 dev_warn(bci
->dev
, "battery temperature out of range\n");
635 if (irqs1
& TWL4030_BATSTS
)
636 dev_crit(bci
->dev
, "battery disconnected\n");
638 if (irqs2
& TWL4030_VBATOV
)
639 dev_crit(bci
->dev
, "VBAT overvoltage\n");
641 if (irqs2
& TWL4030_VBUSOV
)
642 dev_crit(bci
->dev
, "VBUS overvoltage\n");
644 if (irqs2
& TWL4030_ACCHGOV
)
645 dev_crit(bci
->dev
, "Ac charger overvoltage\n");
650 static void twl4030_bci_usb_work(struct work_struct
*data
)
652 struct twl4030_bci
*bci
= container_of(data
, struct twl4030_bci
, work
);
654 switch (bci
->event
) {
656 case USB_EVENT_CHARGER
:
657 twl4030_charger_enable_usb(bci
, true);
660 twl4030_charger_enable_usb(bci
, false);
665 static int twl4030_bci_usb_ncb(struct notifier_block
*nb
, unsigned long val
,
668 struct twl4030_bci
*bci
= container_of(nb
, struct twl4030_bci
, usb_nb
);
670 dev_dbg(bci
->dev
, "OTG notify %lu\n", val
);
672 /* reset current on each 'plug' event */
674 bci
->usb_cur_target
= 500000;
676 bci
->usb_cur_target
= 100000;
679 schedule_work(&bci
->work
);
685 * sysfs charger enabled store
688 twl4030_bci_mode_store(struct device
*dev
, struct device_attribute
*attr
,
689 const char *buf
, size_t n
)
691 struct twl4030_bci
*bci
= dev_get_drvdata(dev
->parent
);
695 mode
= sysfs_match_string(modes
, buf
);
699 if (dev
== &bci
->ac
->dev
) {
702 twl4030_charger_enable_ac(bci
, false);
704 status
= twl4030_charger_enable_ac(bci
, true);
706 twl4030_charger_enable_usb(bci
, false);
707 bci
->usb_mode
= mode
;
708 status
= twl4030_charger_enable_usb(bci
, true);
710 return (status
== 0) ? n
: status
;
714 * sysfs charger enabled show
717 twl4030_bci_mode_show(struct device
*dev
,
718 struct device_attribute
*attr
, char *buf
)
720 struct twl4030_bci
*bci
= dev_get_drvdata(dev
->parent
);
723 int mode
= bci
->usb_mode
;
725 if (dev
== &bci
->ac
->dev
)
728 for (i
= 0; i
< ARRAY_SIZE(modes
); i
++)
730 len
+= sysfs_emit_at(buf
, len
, "[%s] ", modes
[i
]);
732 len
+= sysfs_emit_at(buf
, len
, "%s ", modes
[i
]);
736 static DEVICE_ATTR(mode
, 0644, twl4030_bci_mode_show
,
737 twl4030_bci_mode_store
);
739 static int twl4030_charger_get_current(void)
745 curr
= twl4030bci_read_adc_val(TWL4030_BCIICHG
);
749 ret
= twl4030_bci_read(TWL4030_BCICTL1
, &bcictl1
);
753 return regval2ua(curr
, bcictl1
& TWL4030_CGAIN
);
757 * Returns the main charge FSM state
760 static int twl4030bci_state(struct twl4030_bci
*bci
)
765 ret
= twl4030_bci_read(TWL4030_BCIMSTATEC
, &state
);
767 dev_err(bci
->dev
, "error reading BCIMSTATEC\n");
771 dev_dbg(bci
->dev
, "state: %02x\n", state
);
776 static int twl4030_bci_state_to_status(int state
)
778 state
&= TWL4030_MSTATEC_MASK
;
779 if (TWL4030_MSTATEC_QUICK1
<= state
&& state
<= TWL4030_MSTATEC_QUICK7
)
780 return POWER_SUPPLY_STATUS_CHARGING
;
781 else if (TWL4030_MSTATEC_COMPLETE1
<= state
&&
782 state
<= TWL4030_MSTATEC_COMPLETE4
)
783 return POWER_SUPPLY_STATUS_FULL
;
785 return POWER_SUPPLY_STATUS_NOT_CHARGING
;
788 static int twl4030_bci_get_property(struct power_supply
*psy
,
789 enum power_supply_property psp
,
790 union power_supply_propval
*val
)
792 struct twl4030_bci
*bci
= dev_get_drvdata(psy
->dev
.parent
);
797 state
= twl4030bci_state(bci
);
801 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
802 is_charging
= state
& TWL4030_MSTATEC_USB
;
804 is_charging
= state
& TWL4030_MSTATEC_AC
;
807 ret
= twl4030_bci_read(TWL4030_BCIMDEN
, &s
);
810 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
815 /* A little white lie */
816 state
= TWL4030_MSTATEC_QUICK1
;
820 case POWER_SUPPLY_PROP_STATUS
:
822 val
->intval
= twl4030_bci_state_to_status(state
);
824 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
826 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
827 /* charging must be active for meaningful result */
830 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
) {
831 ret
= twl4030bci_read_adc_val(TWL4030_BCIVBUS
);
834 /* BCIVBUS uses ADCIN8, 7/1023 V/step */
835 val
->intval
= ret
* 6843;
837 ret
= twl4030bci_read_adc_val(TWL4030_BCIVAC
);
840 /* BCIVAC uses ADCIN11, 10/1023 V/step */
841 val
->intval
= ret
* 9775;
844 case POWER_SUPPLY_PROP_CURRENT_NOW
:
847 /* current measurement is shared between AC and USB */
848 ret
= twl4030_charger_get_current();
853 case POWER_SUPPLY_PROP_ONLINE
:
854 val
->intval
= is_charging
&&
855 twl4030_bci_state_to_status(state
) !=
856 POWER_SUPPLY_STATUS_NOT_CHARGING
;
858 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
860 if (psy
->desc
->type
!= POWER_SUPPLY_TYPE_USB
) {
861 if (!bci
->ac_is_active
)
862 val
->intval
= bci
->ac_cur
;
864 if (bci
->ac_is_active
)
865 val
->intval
= bci
->usb_cur_target
;
867 if (val
->intval
< 0) {
870 val
->intval
= twl4030bci_read_adc_val(TWL4030_BCIIREF1
);
873 ret
= twl4030_bci_read(TWL4030_BCICTL1
, &bcictl1
);
876 val
->intval
= regval2ua(val
->intval
, bcictl1
&
887 static int twl4030_bci_set_property(struct power_supply
*psy
,
888 enum power_supply_property psp
,
889 const union power_supply_propval
*val
)
891 struct twl4030_bci
*bci
= dev_get_drvdata(psy
->dev
.parent
);
894 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
895 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_USB
)
896 bci
->usb_cur_target
= val
->intval
;
898 bci
->ac_cur
= val
->intval
;
899 twl4030_charger_update_current(bci
);
908 static int twl4030_bci_property_is_writeable(struct power_supply
*psy
,
909 enum power_supply_property psp
)
912 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
919 static enum power_supply_property twl4030_charger_props
[] = {
920 POWER_SUPPLY_PROP_STATUS
,
921 POWER_SUPPLY_PROP_ONLINE
,
922 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
923 POWER_SUPPLY_PROP_CURRENT_NOW
,
924 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
928 static const struct twl4030_bci_platform_data
*
929 twl4030_bci_parse_dt(struct device
*dev
)
931 struct device_node
*np
= dev
->of_node
;
932 struct twl4030_bci_platform_data
*pdata
;
937 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
941 if (of_property_read_u32(np
, "ti,bb-uvolt", &num
) == 0)
942 pdata
->bb_uvolt
= num
;
943 if (of_property_read_u32(np
, "ti,bb-uamp", &num
) == 0)
944 pdata
->bb_uamp
= num
;
948 static inline const struct twl4030_bci_platform_data
*
949 twl4030_bci_parse_dt(struct device
*dev
)
955 static const struct power_supply_desc twl4030_bci_ac_desc
= {
956 .name
= "twl4030_ac",
957 .type
= POWER_SUPPLY_TYPE_MAINS
,
958 .properties
= twl4030_charger_props
,
959 .num_properties
= ARRAY_SIZE(twl4030_charger_props
),
960 .get_property
= twl4030_bci_get_property
,
961 .set_property
= twl4030_bci_set_property
,
962 .property_is_writeable
= twl4030_bci_property_is_writeable
,
965 static const struct power_supply_desc twl4030_bci_usb_desc
= {
966 .name
= "twl4030_usb",
967 .type
= POWER_SUPPLY_TYPE_USB
,
968 .properties
= twl4030_charger_props
,
969 .num_properties
= ARRAY_SIZE(twl4030_charger_props
),
970 .get_property
= twl4030_bci_get_property
,
971 .set_property
= twl4030_bci_set_property
,
972 .property_is_writeable
= twl4030_bci_property_is_writeable
,
975 static int twl4030_bci_probe(struct platform_device
*pdev
)
977 struct twl4030_bci
*bci
;
978 const struct twl4030_bci_platform_data
*pdata
= pdev
->dev
.platform_data
;
982 bci
= devm_kzalloc(&pdev
->dev
, sizeof(*bci
), GFP_KERNEL
);
987 pdata
= twl4030_bci_parse_dt(&pdev
->dev
);
989 bci
->ichg_eoc
= 80100; /* Stop charging when current drops to here */
990 bci
->ichg_lo
= 241000; /* Low threshold */
991 bci
->ichg_hi
= 500000; /* High threshold */
992 bci
->ac_cur
= 500000; /* 500mA */
994 bci
->usb_cur_target
= 500000; /* 500mA */
996 bci
->usb_cur_target
= 100000; /* 100mA */
997 bci
->usb_mode
= CHARGE_AUTO
;
998 bci
->ac_mode
= CHARGE_AUTO
;
1000 bci
->dev
= &pdev
->dev
;
1001 bci
->irq_chg
= platform_get_irq(pdev
, 0);
1002 bci
->irq_bci
= platform_get_irq(pdev
, 1);
1004 platform_set_drvdata(pdev
, bci
);
1006 INIT_WORK(&bci
->work
, twl4030_bci_usb_work
);
1007 INIT_DELAYED_WORK(&bci
->current_worker
, twl4030_current_worker
);
1009 bci
->channel_vac
= devm_iio_channel_get(&pdev
->dev
, "vac");
1010 if (IS_ERR(bci
->channel_vac
)) {
1011 ret
= PTR_ERR(bci
->channel_vac
);
1012 if (ret
== -EPROBE_DEFER
)
1013 return ret
; /* iio not ready */
1014 dev_warn(&pdev
->dev
, "could not request vac iio channel (%d)",
1016 bci
->channel_vac
= NULL
;
1019 if (bci
->dev
->of_node
) {
1020 struct device_node
*phynode
;
1022 phynode
= of_get_compatible_child(bci
->dev
->of_node
->parent
,
1025 bci
->usb_nb
.notifier_call
= twl4030_bci_usb_ncb
;
1026 bci
->transceiver
= devm_usb_get_phy_by_node(
1027 bci
->dev
, phynode
, &bci
->usb_nb
);
1028 of_node_put(phynode
);
1029 if (IS_ERR(bci
->transceiver
)) {
1030 ret
= PTR_ERR(bci
->transceiver
);
1031 if (ret
== -EPROBE_DEFER
)
1032 return ret
; /* phy not ready */
1033 dev_warn(&pdev
->dev
, "could not request transceiver (%d)",
1035 bci
->transceiver
= NULL
;
1040 bci
->ac
= devm_power_supply_register(&pdev
->dev
, &twl4030_bci_ac_desc
,
1042 if (IS_ERR(bci
->ac
)) {
1043 ret
= PTR_ERR(bci
->ac
);
1044 dev_err(&pdev
->dev
, "failed to register ac: %d\n", ret
);
1048 bci
->usb
= devm_power_supply_register(&pdev
->dev
, &twl4030_bci_usb_desc
,
1050 if (IS_ERR(bci
->usb
)) {
1051 ret
= PTR_ERR(bci
->usb
);
1052 dev_err(&pdev
->dev
, "failed to register usb: %d\n", ret
);
1056 ret
= devm_request_threaded_irq(&pdev
->dev
, bci
->irq_chg
, NULL
,
1057 twl4030_charger_interrupt
, IRQF_ONESHOT
, pdev
->name
,
1060 dev_err(&pdev
->dev
, "could not request irq %d, status %d\n",
1065 ret
= devm_request_threaded_irq(&pdev
->dev
, bci
->irq_bci
, NULL
,
1066 twl4030_bci_interrupt
, IRQF_ONESHOT
, pdev
->name
, bci
);
1068 dev_err(&pdev
->dev
, "could not request irq %d, status %d\n",
1073 /* Enable interrupts now. */
1074 reg
= ~(u32
)(TWL4030_ICHGLOW
| TWL4030_ICHGEOC
| TWL4030_TBATOR2
|
1075 TWL4030_TBATOR1
| TWL4030_BATSTS
);
1076 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
1077 TWL4030_INTERRUPTS_BCIIMR1A
);
1079 dev_err(&pdev
->dev
, "failed to unmask interrupts: %d\n", ret
);
1083 reg
= ~(u32
)(TWL4030_VBATOV
| TWL4030_VBUSOV
| TWL4030_ACCHGOV
);
1084 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, reg
,
1085 TWL4030_INTERRUPTS_BCIIMR2A
);
1087 dev_warn(&pdev
->dev
, "failed to unmask interrupts: %d\n", ret
);
1089 twl4030_charger_update_current(bci
);
1090 if (device_create_file(&bci
->usb
->dev
, &dev_attr_mode
))
1091 dev_warn(&pdev
->dev
, "could not create sysfs file\n");
1092 if (device_create_file(&bci
->ac
->dev
, &dev_attr_mode
))
1093 dev_warn(&pdev
->dev
, "could not create sysfs file\n");
1095 twl4030_charger_enable_ac(bci
, true);
1096 if (!IS_ERR_OR_NULL(bci
->transceiver
))
1097 twl4030_bci_usb_ncb(&bci
->usb_nb
,
1098 bci
->transceiver
->last_event
,
1101 twl4030_charger_enable_usb(bci
, false);
1103 twl4030_charger_enable_backup(pdata
->bb_uvolt
,
1106 twl4030_charger_enable_backup(0, 0);
1111 static void twl4030_bci_remove(struct platform_device
*pdev
)
1113 struct twl4030_bci
*bci
= platform_get_drvdata(pdev
);
1115 twl4030_charger_enable_ac(bci
, false);
1116 twl4030_charger_enable_usb(bci
, false);
1117 twl4030_charger_enable_backup(0, 0);
1119 device_remove_file(&bci
->usb
->dev
, &dev_attr_mode
);
1120 device_remove_file(&bci
->ac
->dev
, &dev_attr_mode
);
1121 /* mask interrupts */
1122 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, 0xff,
1123 TWL4030_INTERRUPTS_BCIIMR1A
);
1124 twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS
, 0xff,
1125 TWL4030_INTERRUPTS_BCIIMR2A
);
1128 static const struct of_device_id twl_bci_of_match
[] __maybe_unused
= {
1129 {.compatible
= "ti,twl4030-bci", },
1132 MODULE_DEVICE_TABLE(of
, twl_bci_of_match
);
1134 static struct platform_driver twl4030_bci_driver
= {
1135 .probe
= twl4030_bci_probe
,
1136 .remove
= twl4030_bci_remove
,
1138 .name
= "twl4030_bci",
1139 .of_match_table
= of_match_ptr(twl_bci_of_match
),
1142 module_platform_driver(twl4030_bci_driver
);
1144 MODULE_AUTHOR("Gražvydas Ignotas");
1145 MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
1146 MODULE_LICENSE("GPL");
1147 MODULE_ALIAS("platform:twl4030_bci");