1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2024 Andreas Kemnade <andreas@kemnade.info>
7 * based on older 6030 driver found in a v3.0 vendor kernel
9 * based on twl4030_bci_battery.c by TI
10 * Copyright (C) 2008 Texas Instruments, Inc.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
18 #include <linux/bits.h>
19 #include <linux/platform_device.h>
20 #include <linux/interrupt.h>
21 #include <linux/mfd/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>
26 #include <linux/devm-helpers.h>
28 #define CONTROLLER_INT_MASK 0x00
29 #define CONTROLLER_CTRL1 0x01
30 #define CONTROLLER_WDG 0x02
31 #define CONTROLLER_STAT1 0x03
32 #define CHARGERUSB_INT_STATUS 0x04
33 #define CHARGERUSB_INT_MASK 0x05
34 #define CHARGERUSB_STATUS_INT1 0x06
35 #define CHARGERUSB_STATUS_INT2 0x07
36 #define CHARGERUSB_CTRL1 0x08
37 #define CHARGERUSB_CTRL2 0x09
38 #define CHARGERUSB_CTRL3 0x0A
39 #define CHARGERUSB_STAT1 0x0B
40 #define CHARGERUSB_VOREG 0x0C
41 #define CHARGERUSB_VICHRG 0x0D
42 #define CHARGERUSB_CINLIMIT 0x0E
43 #define CHARGERUSB_CTRLLIMIT1 0x0F
44 #define CHARGERUSB_CTRLLIMIT2 0x10
45 #define ANTICOLLAPSE_CTRL1 0x11
46 #define ANTICOLLAPSE_CTRL2 0x12
48 /* TWL6032 registers 0xDA to 0xDE - TWL6032_MODULE_CHARGER */
49 #define CONTROLLER_CTRL2 0x00
50 #define CONTROLLER_VSEL_COMP 0x01
51 #define CHARGERUSB_VSYSREG 0x02
52 #define CHARGERUSB_VICHRG_PC 0x03
53 #define LINEAR_CHRG_STS 0x04
55 #define LINEAR_CHRG_STS_CRYSTL_OSC_OK 0x40
56 #define LINEAR_CHRG_STS_END_OF_CHARGE 0x20
57 #define LINEAR_CHRG_STS_VBATOV 0x10
58 #define LINEAR_CHRG_STS_VSYSOV 0x08
59 #define LINEAR_CHRG_STS_DPPM_STS 0x04
60 #define LINEAR_CHRG_STS_CV_STS 0x02
61 #define LINEAR_CHRG_STS_CC_STS 0x01
63 #define FG_REG_00 0x00
64 #define FG_REG_01 0x01
65 #define FG_REG_02 0x02
66 #define FG_REG_03 0x03
67 #define FG_REG_04 0x04
68 #define FG_REG_05 0x05
69 #define FG_REG_06 0x06
70 #define FG_REG_07 0x07
71 #define FG_REG_08 0x08
72 #define FG_REG_09 0x09
73 #define FG_REG_10 0x0A
74 #define FG_REG_11 0x0B
76 /* CONTROLLER_INT_MASK */
77 #define MVAC_FAULT BIT(7)
78 #define MAC_EOC BIT(6)
79 #define LINCH_GATED BIT(5)
80 #define MBAT_REMOVED BIT(4)
81 #define MFAULT_WDG BIT(3)
82 #define MBAT_TEMP BIT(2)
83 #define MVBUS_DET BIT(1)
84 #define MVAC_DET BIT(0)
86 /* CONTROLLER_CTRL1 */
87 #define CONTROLLER_CTRL1_EN_LINCH BIT(5)
88 #define CONTROLLER_CTRL1_EN_CHARGER BIT(4)
89 #define CONTROLLER_CTRL1_SEL_CHARGER BIT(3)
91 /* CONTROLLER_STAT1 */
92 #define CONTROLLER_STAT1_EXTCHRG_STATZ BIT(7)
93 #define CONTROLLER_STAT1_LINCH_GATED BIT(6)
94 #define CONTROLLER_STAT1_CHRG_DET_N BIT(5)
95 #define CONTROLLER_STAT1_FAULT_WDG BIT(4)
96 #define CONTROLLER_STAT1_VAC_DET BIT(3)
97 #define VAC_DET BIT(3)
98 #define CONTROLLER_STAT1_VBUS_DET BIT(2)
99 #define VBUS_DET BIT(2)
100 #define CONTROLLER_STAT1_BAT_REMOVED BIT(1)
101 #define CONTROLLER_STAT1_BAT_TEMP_OVRANGE BIT(0)
103 /* CHARGERUSB_INT_STATUS */
104 #define EN_LINCH BIT(4)
105 #define CURRENT_TERM_INT BIT(3)
106 #define CHARGERUSB_STAT BIT(2)
107 #define CHARGERUSB_THMREG BIT(1)
108 #define CHARGERUSB_FAULT BIT(0)
110 /* CHARGERUSB_INT_MASK */
111 #define MASK_MCURRENT_TERM BIT(3)
112 #define MASK_MCHARGERUSB_STAT BIT(2)
113 #define MASK_MCHARGERUSB_THMREG BIT(1)
114 #define MASK_MCHARGERUSB_FAULT BIT(0)
116 /* CHARGERUSB_STATUS_INT1 */
117 #define CHARGERUSB_STATUS_INT1_TMREG BIT(7)
118 #define CHARGERUSB_STATUS_INT1_NO_BAT BIT(6)
119 #define CHARGERUSB_STATUS_INT1_BST_OCP BIT(5)
120 #define CHARGERUSB_STATUS_INT1_TH_SHUTD BIT(4)
121 #define CHARGERUSB_STATUS_INT1_BAT_OVP BIT(3)
122 #define CHARGERUSB_STATUS_INT1_POOR_SRC BIT(2)
123 #define CHARGERUSB_STATUS_INT1_SLP_MODE BIT(1)
124 #define CHARGERUSB_STATUS_INT1_VBUS_OVP BIT(0)
126 /* CHARGERUSB_STATUS_INT2 */
127 #define ICCLOOP BIT(3)
128 #define CURRENT_TERM BIT(2)
129 #define CHARGE_DONE BIT(1)
130 #define ANTICOLLAPSE BIT(0)
132 /* CHARGERUSB_CTRL1 */
133 #define SUSPEND_BOOT BIT(7)
134 #define OPA_MODE BIT(6)
135 #define HZ_MODE BIT(5)
138 /* CHARGERUSB_CTRL2 */
139 #define UA_TO_VITERM(x) (((x) / 50000 - 1) << 5)
141 /* CHARGERUSB_CTRL3 */
142 #define VBUSCHRG_LDO_OVRD BIT(7)
143 #define CHARGE_ONCE BIT(6)
144 #define BST_HW_PR_DIS BIT(5)
145 #define AUTOSUPPLY BIT(3)
146 #define BUCK_HSILIM BIT(0)
148 /* CHARGERUSB_VOREG */
149 #define UV_TO_VOREG(x) (((x) - 3500000) / 20000)
150 #define VOREG_TO_UV(x) (((x) & 0x3F) * 20000 + 3500000)
151 #define CHARGERUSB_VOREG_3P52 0x01
152 #define CHARGERUSB_VOREG_4P0 0x19
153 #define CHARGERUSB_VOREG_4P2 0x23
154 #define CHARGERUSB_VOREG_4P76 0x3F
156 /* CHARGERUSB_VICHRG */
158 * might be inaccurate for < 500 mA, diffent scale might apply,
159 * either starting from 100 mA or 300 mA
161 #define UA_TO_VICHRG(x) (((x) / 100000) - 1)
162 #define VICHRG_TO_UA(x) (((x) & 0xf) * 100000 + 100000)
164 /* CHARGERUSB_CINLIMIT */
165 #define CHARGERUSB_CIN_LIMIT_100 0x1
166 #define CHARGERUSB_CIN_LIMIT_300 0x5
167 #define CHARGERUSB_CIN_LIMIT_500 0x9
168 #define CHARGERUSB_CIN_LIMIT_NONE 0xF
170 /* CHARGERUSB_CTRLLIMIT2 */
171 #define CHARGERUSB_CTRLLIMIT2_1500 0x0E
172 #define LOCK_LIMIT BIT(4)
174 /* ANTICOLLAPSE_CTRL2 */
175 #define BUCK_VTH_SHIFT 5
178 #define CC_ACTIVE_MODE_SHIFT 6
179 #define CC_AUTOCLEAR BIT(2)
180 #define CC_CAL_EN BIT(1)
181 #define CC_PAUSE BIT(0)
183 #define REG_TOGGLE1 0x90
184 #define REG_PWDNSTATUS1 0x93
185 #define FGDITHS BIT(7)
186 #define FGDITHR BIT(6)
189 #define BBSPOR_CFG 0xE6
190 #define BB_CHG_EN BIT(3)
192 struct twl6030_charger_info
{
194 struct power_supply
*usb
;
195 struct power_supply_battery_info
*binfo
;
196 struct work_struct work
;
198 int input_current_limit
;
199 struct iio_channel
*channel_vusb
;
200 struct delayed_work charger_monitor
;
201 bool extended_current_range
;
204 struct twl6030_charger_chip_data
{
205 bool extended_current_range
;
208 static int twl6030_charger_read(u8 reg
, u8
*val
)
210 return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE
, val
, reg
);
213 static int twl6030_charger_write(u8 reg
, u8 val
)
215 return twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE
, val
, reg
);
218 static int twl6030_config_cinlimit_reg(struct twl6030_charger_info
*charger
,
221 if (ua
>= 50000 && ua
<= 750000) {
222 ua
= (ua
- 50000) / 50000;
223 } else if ((ua
> 750000) && (ua
<= 1500000) && charger
->extended_current_range
) {
224 ua
= ((ua
% 100000) ? 0x30 : 0x20) + ((ua
- 100000) / 100000);
227 dev_err(charger
->dev
, "invalid input current limit\n");
230 /* This is no current limit */
234 return twl6030_charger_write(CHARGERUSB_CINLIMIT
, ua
);
238 * rewriting all stuff here, resets to extremely conservative defaults were
239 * seen under some circumstances, like charge voltage to 3.5V
241 static int twl6030_enable_usb(struct twl6030_charger_info
*charger
)
245 ret
= twl6030_charger_write(CHARGERUSB_VICHRG
,
246 UA_TO_VICHRG(charger
->binfo
->constant_charge_current_max_ua
));
250 ret
= twl6030_charger_write(CONTROLLER_WDG
, 0xff);
254 charger
->input_current_limit
= 500000;
255 ret
= twl6030_config_cinlimit_reg(charger
, charger
->input_current_limit
);
259 ret
= twl6030_charger_write(CHARGERUSB_CINLIMIT
, CHARGERUSB_CIN_LIMIT_500
);
263 ret
= twl6030_charger_write(CHARGERUSB_VOREG
,
264 UV_TO_VOREG(charger
->binfo
->constant_charge_voltage_max_uv
));
268 ret
= twl6030_charger_write(CHARGERUSB_CTRL1
, TERM
);
272 if (charger
->binfo
->charge_term_current_ua
!= -EINVAL
) {
273 ret
= twl6030_charger_write(CHARGERUSB_CTRL2
,
274 UA_TO_VITERM(charger
->binfo
->charge_term_current_ua
));
279 return twl6030_charger_write(CONTROLLER_CTRL1
, CONTROLLER_CTRL1_EN_CHARGER
);
282 static void twl6030_charger_wdg(struct work_struct
*data
)
284 struct twl6030_charger_info
*charger
=
285 container_of(data
, struct twl6030_charger_info
,
286 charger_monitor
.work
);
293 twl6030_charger_read(CONTROLLER_STAT1
, &val
);
294 twl6030_charger_read(CHARGERUSB_INT_STATUS
, &int_stat
);
295 twl6030_charger_read(CHARGERUSB_STATUS_INT1
, &stat_int1
);
296 twl6030_charger_read(CHARGERUSB_STATUS_INT2
, &stat_int2
);
297 dev_dbg(charger
->dev
,
298 "wdg: stat1: %02x %s INT_STATUS %02x STATUS_INT1 %02x STATUS_INT2 %02x\n",
299 val
, (val
& VBUS_DET
) ? "usb online" : "usb offline",
300 int_stat
, stat_int1
, stat_int2
);
302 twl6030_charger_write(CONTROLLER_WDG
, 0xff);
303 schedule_delayed_work(&charger
->charger_monitor
,
304 msecs_to_jiffies(10000));
307 static irqreturn_t
twl6030_charger_interrupt(int irq
, void *arg
)
309 struct twl6030_charger_info
*charger
= arg
;
315 if (twl6030_charger_read(CONTROLLER_STAT1
, &val
) < 0)
318 if (twl6030_charger_read(CHARGERUSB_INT_STATUS
, &int_stat
) < 0)
321 if (twl6030_charger_read(CHARGERUSB_STATUS_INT1
, &stat_int1
) < 0)
324 if (twl6030_charger_read(CHARGERUSB_STATUS_INT2
, &stat_int2
) < 0)
327 dev_dbg(charger
->dev
,
328 "charger irq: stat1: %02x %s INT_STATUS %02x STATUS_INT1 %02x STATUS_INT2 %02x\n",
329 val
, (val
& VBUS_DET
) ? "usb online" : "usb offline",
330 int_stat
, stat_int1
, stat_int2
);
331 power_supply_changed(charger
->usb
);
333 if (val
& VBUS_DET
) {
334 if (twl6030_charger_read(CONTROLLER_CTRL1
, &val
) < 0)
337 if (!(val
& CONTROLLER_CTRL1_EN_CHARGER
)) {
338 if (twl6030_enable_usb(charger
) < 0)
341 schedule_delayed_work(&charger
->charger_monitor
,
342 msecs_to_jiffies(10000));
345 cancel_delayed_work(&charger
->charger_monitor
);
350 static int twl6030_charger_usb_get_property(struct power_supply
*psy
,
351 enum power_supply_property psp
,
352 union power_supply_propval
*val
)
354 struct twl6030_charger_info
*charger
= power_supply_get_drvdata(psy
);
359 ret
= twl6030_charger_read(CONTROLLER_STAT1
, &stat1
);
364 case POWER_SUPPLY_PROP_STATUS
:
365 if (!(stat1
& VBUS_DET
)) {
366 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
369 ret
= twl6030_charger_read(CHARGERUSB_STATUS_INT2
, &intstat
);
373 if (intstat
& CHARGE_DONE
)
374 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
375 else if (intstat
& CURRENT_TERM
)
376 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
378 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
380 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
381 if (!charger
->channel_vusb
)
384 ret
= iio_read_channel_processed_scale(charger
->channel_vusb
, &val
->intval
, 1000);
389 case POWER_SUPPLY_PROP_ONLINE
:
390 val
->intval
= !!(stat1
& VBUS_DET
);
392 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
393 val
->intval
= charger
->input_current_limit
;
402 static int twl6030_charger_usb_set_property(struct power_supply
*psy
,
403 enum power_supply_property psp
,
404 const union power_supply_propval
*val
)
406 struct twl6030_charger_info
*charger
= power_supply_get_drvdata(psy
);
409 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
410 charger
->input_current_limit
= val
->intval
;
411 return twl6030_config_cinlimit_reg(charger
, charger
->input_current_limit
);
419 static int twl6030_charger_usb_property_is_writeable(struct power_supply
*psy
,
420 enum power_supply_property psp
)
422 dev_info(&psy
->dev
, "is %d writeable?\n", (int)psp
);
424 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
431 static enum power_supply_property twl6030_charger_props
[] = {
432 POWER_SUPPLY_PROP_STATUS
,
433 POWER_SUPPLY_PROP_ONLINE
,
434 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
435 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
438 static const struct power_supply_desc twl6030_charger_usb_desc
= {
439 .name
= "twl6030_usb",
440 .type
= POWER_SUPPLY_TYPE_USB
,
441 .properties
= twl6030_charger_props
,
442 .num_properties
= ARRAY_SIZE(twl6030_charger_props
),
443 .get_property
= twl6030_charger_usb_get_property
,
444 .set_property
= twl6030_charger_usb_set_property
,
445 .property_is_writeable
= twl6030_charger_usb_property_is_writeable
,
448 static int twl6030_charger_probe(struct platform_device
*pdev
)
450 struct twl6030_charger_info
*charger
;
451 const struct twl6030_charger_chip_data
*chip_data
;
452 struct power_supply_config psy_cfg
= {};
456 charger
= devm_kzalloc(&pdev
->dev
, sizeof(*charger
), GFP_KERNEL
);
460 charger
->dev
= &pdev
->dev
;
461 charger
->irq_chg
= platform_get_irq(pdev
, 0);
463 chip_data
= device_get_match_data(&pdev
->dev
);
465 return dev_err_probe(&pdev
->dev
, -EINVAL
, "missing chip data\n");
467 charger
->extended_current_range
= chip_data
->extended_current_range
;
468 platform_set_drvdata(pdev
, charger
);
469 psy_cfg
.drv_data
= charger
;
470 psy_cfg
.fwnode
= dev_fwnode(&pdev
->dev
);
472 charger
->channel_vusb
= devm_iio_channel_get(&pdev
->dev
, "vusb");
473 if (IS_ERR(charger
->channel_vusb
)) {
474 ret
= PTR_ERR(charger
->channel_vusb
);
475 if (ret
== -EPROBE_DEFER
)
476 return ret
; /* iio not ready */
477 dev_warn(&pdev
->dev
, "could not request vusb iio channel (%d)",
479 charger
->channel_vusb
= NULL
;
482 charger
->usb
= devm_power_supply_register(&pdev
->dev
,
483 &twl6030_charger_usb_desc
,
485 if (IS_ERR(charger
->usb
))
486 return dev_err_probe(&pdev
->dev
, PTR_ERR(charger
->usb
),
487 "Failed to register usb\n");
489 ret
= power_supply_get_battery_info(charger
->usb
, &charger
->binfo
);
491 return dev_err_probe(&pdev
->dev
, ret
,
492 "Failed to get battery info\n");
494 dev_info(&pdev
->dev
, "battery with vmax %d imax: %d\n",
495 charger
->binfo
->constant_charge_voltage_max_uv
,
496 charger
->binfo
->constant_charge_current_max_ua
);
498 if (charger
->binfo
->constant_charge_voltage_max_uv
== -EINVAL
) {
499 ret
= twl6030_charger_read(CHARGERUSB_CTRLLIMIT1
, &val
);
503 charger
->binfo
->constant_charge_voltage_max_uv
=
507 if (charger
->binfo
->constant_charge_voltage_max_uv
> 4760000 ||
508 charger
->binfo
->constant_charge_voltage_max_uv
< 350000)
509 return dev_err_probe(&pdev
->dev
, -EINVAL
,
510 "Invalid charge voltage\n");
512 if (charger
->binfo
->constant_charge_current_max_ua
== -EINVAL
) {
513 ret
= twl6030_charger_read(CHARGERUSB_CTRLLIMIT2
, &val
);
517 charger
->binfo
->constant_charge_current_max_ua
= VICHRG_TO_UA(val
);
520 if (charger
->binfo
->constant_charge_current_max_ua
< 100000 ||
521 charger
->binfo
->constant_charge_current_max_ua
> 1500000) {
522 return dev_err_probe(&pdev
->dev
, -EINVAL
,
523 "Invalid charge current\n");
526 if ((charger
->binfo
->charge_term_current_ua
!= -EINVAL
) &&
527 (charger
->binfo
->charge_term_current_ua
> 400000 ||
528 charger
->binfo
->charge_term_current_ua
< 50000)) {
529 return dev_err_probe(&pdev
->dev
, -EINVAL
,
530 "Invalid charge termination current\n");
533 ret
= devm_delayed_work_autocancel(&pdev
->dev
,
534 &charger
->charger_monitor
,
535 twl6030_charger_wdg
);
537 return dev_err_probe(&pdev
->dev
, ret
,
538 "Failed to register delayed work\n");
540 ret
= devm_request_threaded_irq(&pdev
->dev
, charger
->irq_chg
, NULL
,
541 twl6030_charger_interrupt
,
542 IRQF_ONESHOT
, pdev
->name
,
545 return dev_err_probe(&pdev
->dev
, ret
,
546 "could not request irq %d\n",
549 /* turing to charging to configure things */
550 twl6030_charger_write(CONTROLLER_CTRL1
, 0);
551 twl6030_charger_interrupt(0, charger
);
556 static const struct twl6030_charger_chip_data twl6030_data
= {
557 .extended_current_range
= false,
560 static const struct twl6030_charger_chip_data twl6032_data
= {
561 .extended_current_range
= true,
564 static const struct of_device_id twl_charger_of_match
[] = {
565 {.compatible
= "ti,twl6030-charger", .data
= &twl6030_data
},
566 {.compatible
= "ti,twl6032-charger", .data
= &twl6032_data
},
569 MODULE_DEVICE_TABLE(of
, twl_charger_of_match
);
571 static struct platform_driver twl6030_charger_driver
= {
572 .probe
= twl6030_charger_probe
,
574 .name
= "twl6030_charger",
575 .of_match_table
= twl_charger_of_match
,
578 module_platform_driver(twl6030_charger_driver
);
580 MODULE_DESCRIPTION("TWL6030 Battery Charger Interface driver");
581 MODULE_LICENSE("GPL");