1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // Copyright (C) 2018 ROHM Semiconductors
5 // power-supply driver for ROHM BD70528 PMIC
8 * BD70528 charger HW state machine.
10 * The thermal shutdown state is not drawn. From any other state but
11 * battery error and suspend it is possible to go to TSD/TMP states
12 * if temperature is out of bounds.
16 * or (DCIN2_UVLO=L && DCIN1_UVLO=L)
17 * or (DCIN2_OVLO=H & DCIN1_UVKLO=L)
19 * +--------------+ +--------------+
21 * | Any state +-------> | Suspend |
23 * +--------------+ +------+-------+
25 * CHG_EN = H && BAT_DET = H && |
26 * No errors (temp, bat_ov, UVLO, |
29 * BAT_OV or +---------v----------+
31 * +-----------------+ Trickle Charge | <---------------+
33 * | +-------+------------+ |
36 * | V_BAT > VTRI_TH | | VBAT < VTRI_TH - 50mV |
40 * | BAT_OV or +----------+----+ |
41 * | (DBAT && TFST) | | |
42 * | +----------------+ Fast Charge | |
44 * v v +----+----------+ |
46 *+----------------+ ILIM_DET=L | ^ ILIM_DET |
47 *| | & CV_DET=H | | or CV_DET=L |
48 *| Battery Error | & VBAT > | | or VBAT < VRECHG_TH |
49 *| | VRECHG_TH | | or IBAT > IFST/x |
50 *+----------------+ & IBAT < | | |
55 * +-------------------+ Top OFF | |
57 * (DBAT && TFST) +-----+-----+ |
59 * Stay top-off for 15s | |
64 * | Done +-------------------------+
66 * +--------+ VBAT < VRECHG_TH
69 #include <linux/kernel.h>
70 #include <linux/interrupt.h>
71 #include <linux/mfd/rohm-bd70528.h>
72 #include <linux/module.h>
73 #include <linux/platform_device.h>
74 #include <linux/power_supply.h>
75 #include <linux/linear_range.h>
77 #define CHG_STAT_SUSPEND 0x0
78 #define CHG_STAT_TRICKLE 0x1
79 #define CHG_STAT_FAST 0x3
80 #define CHG_STAT_TOPOFF 0xe
81 #define CHG_STAT_DONE 0xf
82 #define CHG_STAT_OTP_TRICKLE 0x10
83 #define CHG_STAT_OTP_FAST 0x11
84 #define CHG_STAT_OTP_DONE 0x12
85 #define CHG_STAT_TSD_TRICKLE 0x20
86 #define CHG_STAT_TSD_FAST 0x21
87 #define CHG_STAT_TSD_TOPOFF 0x22
88 #define CHG_STAT_BAT_ERR 0x7f
90 static const char *bd70528_charger_model
= "BD70528";
91 static const char *bd70528_charger_manufacturer
= "ROHM Semiconductors";
93 #define BD_ERR_IRQ_HND(_name_, _wrn_) \
94 static irqreturn_t bd0528_##_name_##_interrupt(int irq, void *arg) \
96 struct power_supply *psy = (struct power_supply *)arg; \
98 power_supply_changed(psy); \
99 dev_err(&psy->dev, (_wrn_)); \
101 return IRQ_HANDLED; \
104 #define BD_INFO_IRQ_HND(_name_, _wrn_) \
105 static irqreturn_t bd0528_##_name_##_interrupt(int irq, void *arg) \
107 struct power_supply *psy = (struct power_supply *)arg; \
109 power_supply_changed(psy); \
110 dev_dbg(&psy->dev, (_wrn_)); \
112 return IRQ_HANDLED; \
115 #define BD_IRQ_HND(_name_) bd0528_##_name_##_interrupt
118 struct regmap
*regmap
;
120 struct power_supply
*psy
;
123 BD_ERR_IRQ_HND(BAT_OV_DET
, "Battery overvoltage detected\n");
124 BD_ERR_IRQ_HND(DBAT_DET
, "Dead battery detected\n");
125 BD_ERR_IRQ_HND(COLD_DET
, "Battery cold\n");
126 BD_ERR_IRQ_HND(HOT_DET
, "Battery hot\n");
127 BD_ERR_IRQ_HND(CHG_TSD
, "Charger thermal shutdown\n");
128 BD_ERR_IRQ_HND(DCIN2_OV_DET
, "DCIN2 overvoltage detected\n");
130 BD_INFO_IRQ_HND(BAT_OV_RES
, "Battery voltage back to normal\n");
131 BD_INFO_IRQ_HND(COLD_RES
, "Battery temperature back to normal\n");
132 BD_INFO_IRQ_HND(HOT_RES
, "Battery temperature back to normal\n");
133 BD_INFO_IRQ_HND(BAT_RMV
, "Battery removed\n");
134 BD_INFO_IRQ_HND(BAT_DET
, "Battery detected\n");
135 BD_INFO_IRQ_HND(DCIN2_OV_RES
, "DCIN2 voltage back to normal\n");
136 BD_INFO_IRQ_HND(DCIN2_RMV
, "DCIN2 removed\n");
137 BD_INFO_IRQ_HND(DCIN2_DET
, "DCIN2 detected\n");
138 BD_INFO_IRQ_HND(DCIN1_RMV
, "DCIN1 removed\n");
139 BD_INFO_IRQ_HND(DCIN1_DET
, "DCIN1 detected\n");
141 struct irq_name_pair
{
143 irqreturn_t (*h
)(int irq
, void *arg
);
146 static int bd70528_get_irqs(struct platform_device
*pdev
,
147 struct bd70528_psy
*bdpsy
)
151 static const struct irq_name_pair bd70528_chg_irqs
[] = {
152 { .n
= "bd70528-bat-ov-res", .h
= BD_IRQ_HND(BAT_OV_RES
) },
153 { .n
= "bd70528-bat-ov-det", .h
= BD_IRQ_HND(BAT_OV_DET
) },
154 { .n
= "bd70528-bat-dead", .h
= BD_IRQ_HND(DBAT_DET
) },
155 { .n
= "bd70528-bat-warmed", .h
= BD_IRQ_HND(COLD_RES
) },
156 { .n
= "bd70528-bat-cold", .h
= BD_IRQ_HND(COLD_DET
) },
157 { .n
= "bd70528-bat-cooled", .h
= BD_IRQ_HND(HOT_RES
) },
158 { .n
= "bd70528-bat-hot", .h
= BD_IRQ_HND(HOT_DET
) },
159 { .n
= "bd70528-chg-tshd", .h
= BD_IRQ_HND(CHG_TSD
) },
160 { .n
= "bd70528-bat-removed", .h
= BD_IRQ_HND(BAT_RMV
) },
161 { .n
= "bd70528-bat-detected", .h
= BD_IRQ_HND(BAT_DET
) },
162 { .n
= "bd70528-dcin2-ov-res", .h
= BD_IRQ_HND(DCIN2_OV_RES
) },
163 { .n
= "bd70528-dcin2-ov-det", .h
= BD_IRQ_HND(DCIN2_OV_DET
) },
164 { .n
= "bd70528-dcin2-removed", .h
= BD_IRQ_HND(DCIN2_RMV
) },
165 { .n
= "bd70528-dcin2-detected", .h
= BD_IRQ_HND(DCIN2_DET
) },
166 { .n
= "bd70528-dcin1-removed", .h
= BD_IRQ_HND(DCIN1_RMV
) },
167 { .n
= "bd70528-dcin1-detected", .h
= BD_IRQ_HND(DCIN1_DET
) },
170 for (i
= 0; i
< ARRAY_SIZE(bd70528_chg_irqs
); i
++) {
171 irq
= platform_get_irq_byname(pdev
, bd70528_chg_irqs
[i
].n
);
173 dev_err(&pdev
->dev
, "Bad IRQ information for %s (%d)\n",
174 bd70528_chg_irqs
[i
].n
, irq
);
177 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
178 bd70528_chg_irqs
[i
].h
,
180 bd70528_chg_irqs
[i
].n
,
187 * BD70528 irq controller is not touching the main mask register.
188 * So enable the charger block interrupts at main level. We can just
189 * leave them enabled as irq-controller should disable irqs
190 * from sub-registers when IRQ is disabled or freed.
192 mask
= BD70528_REG_INT_BAT1_MASK
| BD70528_REG_INT_BAT2_MASK
;
193 ret
= regmap_update_bits(bdpsy
->regmap
,
194 BD70528_REG_INT_MAIN_MASK
, mask
, 0);
196 dev_err(&pdev
->dev
, "Failed to enable charger IRQs\n");
201 static int bd70528_get_charger_status(struct bd70528_psy
*bdpsy
, int *val
)
206 ret
= regmap_read(bdpsy
->regmap
, BD70528_REG_CHG_CURR_STAT
, &v
);
208 dev_err(bdpsy
->dev
, "Charger state read failure %d\n",
213 switch (v
& BD70528_MASK_CHG_STAT
) {
214 case CHG_STAT_SUSPEND
:
215 /* Maybe we should check the CHG_TTRI_EN? */
216 case CHG_STAT_OTP_TRICKLE
:
217 case CHG_STAT_OTP_FAST
:
218 case CHG_STAT_OTP_DONE
:
219 case CHG_STAT_TSD_TRICKLE
:
220 case CHG_STAT_TSD_FAST
:
221 case CHG_STAT_TSD_TOPOFF
:
222 case CHG_STAT_BAT_ERR
:
223 *val
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
226 *val
= POWER_SUPPLY_STATUS_FULL
;
228 case CHG_STAT_TRICKLE
:
230 case CHG_STAT_TOPOFF
:
231 *val
= POWER_SUPPLY_STATUS_CHARGING
;
234 *val
= POWER_SUPPLY_STATUS_UNKNOWN
;
241 static int bd70528_get_charge_type(struct bd70528_psy
*bdpsy
, int *val
)
246 ret
= regmap_read(bdpsy
->regmap
, BD70528_REG_CHG_CURR_STAT
, &v
);
248 dev_err(bdpsy
->dev
, "Charger state read failure %d\n",
253 switch (v
& BD70528_MASK_CHG_STAT
) {
254 case CHG_STAT_TRICKLE
:
255 *val
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
258 case CHG_STAT_TOPOFF
:
259 *val
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
262 case CHG_STAT_SUSPEND
:
263 /* Maybe we should check the CHG_TTRI_EN? */
264 case CHG_STAT_OTP_TRICKLE
:
265 case CHG_STAT_OTP_FAST
:
266 case CHG_STAT_OTP_DONE
:
267 case CHG_STAT_TSD_TRICKLE
:
268 case CHG_STAT_TSD_FAST
:
269 case CHG_STAT_TSD_TOPOFF
:
270 case CHG_STAT_BAT_ERR
:
271 *val
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
274 *val
= POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
281 static int bd70528_get_battery_health(struct bd70528_psy
*bdpsy
, int *val
)
286 ret
= regmap_read(bdpsy
->regmap
, BD70528_REG_CHG_BAT_STAT
, &v
);
288 dev_err(bdpsy
->dev
, "Battery state read failure %d\n",
293 if (!(v
& BD70528_MASK_CHG_BAT_DETECT
))
294 *val
= POWER_SUPPLY_HEALTH_DEAD
;
295 else if (v
& BD70528_MASK_CHG_BAT_OVERVOLT
)
296 *val
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
297 else if (v
& BD70528_MASK_CHG_BAT_TIMER
)
298 *val
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
300 *val
= POWER_SUPPLY_HEALTH_GOOD
;
305 static int bd70528_get_online(struct bd70528_psy
*bdpsy
, int *val
)
310 ret
= regmap_read(bdpsy
->regmap
, BD70528_REG_CHG_IN_STAT
, &v
);
312 dev_err(bdpsy
->dev
, "DC1 IN state read failure %d\n",
317 *val
= (v
& BD70528_MASK_CHG_DCIN1_UVLO
) ? 1 : 0;
322 static int bd70528_get_present(struct bd70528_psy
*bdpsy
, int *val
)
327 ret
= regmap_read(bdpsy
->regmap
, BD70528_REG_CHG_BAT_STAT
, &v
);
329 dev_err(bdpsy
->dev
, "Battery state read failure %d\n",
334 *val
= (v
& BD70528_MASK_CHG_BAT_DETECT
) ? 1 : 0;
339 static const struct linear_range current_limit_ranges
[] = {
373 * BD70528 would support setting and getting own charge current/
374 * voltage for low temperatures. The driver currently only reads
375 * the charge current at room temperature. We do set both though.
377 static const struct linear_range warm_charge_curr
[] = {
393 * Cold charge current selectors are identical to warm charge current
394 * selectors. The difference is that only smaller currents are available
395 * at cold charge range.
397 #define MAX_COLD_CHG_CURR_SEL 0x15
398 #define MAX_WARM_CHG_CURR_SEL 0x1f
399 #define MIN_CHG_CURR_SEL 0x0
401 static int get_charge_current(struct bd70528_psy
*bdpsy
, int *ma
)
406 ret
= regmap_read(bdpsy
->regmap
, BD70528_REG_CHG_CHG_CURR_WARM
,
410 "Charge current reading failed (%d)\n", ret
);
414 sel
&= BD70528_MASK_CHG_CHG_CURR
;
416 ret
= linear_range_get_value_array(&warm_charge_curr
[0],
417 ARRAY_SIZE(warm_charge_curr
),
421 "Unknown charge current value 0x%x\n",
428 static int get_current_limit(struct bd70528_psy
*bdpsy
, int *ma
)
433 ret
= regmap_read(bdpsy
->regmap
, BD70528_REG_CHG_DCIN_ILIM
,
438 "Input current limit reading failed (%d)\n", ret
);
442 sel
&= BD70528_MASK_CHG_DCIN_ILIM
;
444 ret
= linear_range_get_value_array(¤t_limit_ranges
[0],
445 ARRAY_SIZE(current_limit_ranges
),
448 /* Unspecified values mean 500 mA */
454 static enum power_supply_property bd70528_charger_props
[] = {
455 POWER_SUPPLY_PROP_STATUS
,
456 POWER_SUPPLY_PROP_CHARGE_TYPE
,
457 POWER_SUPPLY_PROP_HEALTH
,
458 POWER_SUPPLY_PROP_PRESENT
,
459 POWER_SUPPLY_PROP_ONLINE
,
460 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
461 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
462 POWER_SUPPLY_PROP_MODEL_NAME
,
463 POWER_SUPPLY_PROP_MANUFACTURER
,
466 static int bd70528_charger_get_property(struct power_supply
*psy
,
467 enum power_supply_property psp
,
468 union power_supply_propval
*val
)
470 struct bd70528_psy
*bdpsy
= power_supply_get_drvdata(psy
);
474 case POWER_SUPPLY_PROP_STATUS
:
475 return bd70528_get_charger_status(bdpsy
, &val
->intval
);
476 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
477 return bd70528_get_charge_type(bdpsy
, &val
->intval
);
478 case POWER_SUPPLY_PROP_HEALTH
:
479 return bd70528_get_battery_health(bdpsy
, &val
->intval
);
480 case POWER_SUPPLY_PROP_PRESENT
:
481 return bd70528_get_present(bdpsy
, &val
->intval
);
482 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
483 ret
= get_current_limit(bdpsy
, &val
->intval
);
486 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
487 ret
= get_charge_current(bdpsy
, &val
->intval
);
490 case POWER_SUPPLY_PROP_ONLINE
:
491 return bd70528_get_online(bdpsy
, &val
->intval
);
492 case POWER_SUPPLY_PROP_MODEL_NAME
:
493 val
->strval
= bd70528_charger_model
;
495 case POWER_SUPPLY_PROP_MANUFACTURER
:
496 val
->strval
= bd70528_charger_manufacturer
;
505 static int bd70528_prop_is_writable(struct power_supply
*psy
,
506 enum power_supply_property psp
)
509 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
510 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
518 static int set_charge_current(struct bd70528_psy
*bdpsy
, int ma
)
526 "Requested charge current %u exceed maximum (500mA)\n",
528 reg
= MAX_WARM_CHG_CURR_SEL
;
533 "Requested charge current %u smaller than min (10mA)\n",
535 reg
= MIN_CHG_CURR_SEL
;
541 * For BD70528 voltage/current limits we happily accept any value which
542 * belongs the range. We could check if value matching the selector is
543 * desired by computing the range min + (sel - sel_low) * range step - but
544 * I guess it is enough if we use voltage/current which is closest (below)
548 ret
= linear_range_get_selector_low_array(warm_charge_curr
,
549 ARRAY_SIZE(warm_charge_curr
),
553 "Unsupported charge current %u mA\n", ma
);
554 reg
= MIN_CHG_CURR_SEL
;
559 * There was a gap in supported values and we hit it.
560 * Yet a smaller value was found so we use it.
563 "Unsupported charge current %u mA\n", ma
);
567 tmpret
= regmap_update_bits(bdpsy
->regmap
,
568 BD70528_REG_CHG_CHG_CURR_WARM
,
569 BD70528_MASK_CHG_CHG_CURR
, reg
);
572 "Charge current write failure (%d)\n", tmpret
);
574 if (reg
> MAX_COLD_CHG_CURR_SEL
)
575 reg
= MAX_COLD_CHG_CURR_SEL
;
578 tmpret
= regmap_update_bits(bdpsy
->regmap
,
579 BD70528_REG_CHG_CHG_CURR_COLD
,
580 BD70528_MASK_CHG_CHG_CURR
, reg
);
588 #define MAX_CURR_LIMIT_SEL 0x34
589 #define MIN_CURR_LIMIT_SEL 0x0
591 static int set_current_limit(struct bd70528_psy
*bdpsy
, int ma
)
599 "Requested current limit %u exceed maximum (500mA)\n",
601 reg
= MAX_CURR_LIMIT_SEL
;
606 "Requested current limit %u smaller than min (5mA)\n",
608 reg
= MIN_CURR_LIMIT_SEL
;
613 ret
= linear_range_get_selector_low_array(current_limit_ranges
,
614 ARRAY_SIZE(current_limit_ranges
),
617 dev_err(bdpsy
->dev
, "Unsupported current limit %umA\n", ma
);
618 reg
= MIN_CURR_LIMIT_SEL
;
623 * There was a gap in supported values and we hit it.
624 * We found a smaller value from ranges and use it.
627 dev_warn(bdpsy
->dev
, "Unsupported current limit %umA\n", ma
);
631 tmpret
= regmap_update_bits(bdpsy
->regmap
,
632 BD70528_REG_CHG_DCIN_ILIM
,
633 BD70528_MASK_CHG_DCIN_ILIM
, reg
);
641 static int bd70528_charger_set_property(struct power_supply
*psy
,
642 enum power_supply_property psp
,
643 const union power_supply_propval
*val
)
645 struct bd70528_psy
*bdpsy
= power_supply_get_drvdata(psy
);
648 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
649 return set_current_limit(bdpsy
, val
->intval
/ 1000);
650 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
651 return set_charge_current(bdpsy
, val
->intval
/ 1000);
658 static const struct power_supply_desc bd70528_charger_desc
= {
659 .name
= "bd70528-charger",
660 .type
= POWER_SUPPLY_TYPE_MAINS
,
661 .properties
= bd70528_charger_props
,
662 .num_properties
= ARRAY_SIZE(bd70528_charger_props
),
663 .get_property
= bd70528_charger_get_property
,
664 .set_property
= bd70528_charger_set_property
,
665 .property_is_writeable
= bd70528_prop_is_writable
,
668 static int bd70528_power_probe(struct platform_device
*pdev
)
670 struct bd70528_psy
*bdpsy
;
671 struct power_supply_config cfg
= {};
673 bdpsy
= devm_kzalloc(&pdev
->dev
, sizeof(*bdpsy
), GFP_KERNEL
);
677 bdpsy
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
678 if (!bdpsy
->regmap
) {
679 dev_err(&pdev
->dev
, "No regmap found for chip\n");
682 bdpsy
->dev
= &pdev
->dev
;
684 platform_set_drvdata(pdev
, bdpsy
);
685 cfg
.drv_data
= bdpsy
;
686 cfg
.of_node
= pdev
->dev
.parent
->of_node
;
688 bdpsy
->psy
= devm_power_supply_register(&pdev
->dev
,
689 &bd70528_charger_desc
, &cfg
);
690 if (IS_ERR(bdpsy
->psy
)) {
691 dev_err(&pdev
->dev
, "failed: power supply register\n");
692 return PTR_ERR(bdpsy
->psy
);
695 return bd70528_get_irqs(pdev
, bdpsy
);
698 static struct platform_driver bd70528_power
= {
700 .name
= "bd70528-power"
702 .probe
= bd70528_power_probe
,
705 module_platform_driver(bd70528_power
);
707 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
708 MODULE_DESCRIPTION("BD70528 power-supply driver");
709 MODULE_LICENSE("GPL");
710 MODULE_ALIAS("platform:bd70528-power");