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>
76 #define CHG_STAT_SUSPEND 0x0
77 #define CHG_STAT_TRICKLE 0x1
78 #define CHG_STAT_FAST 0x3
79 #define CHG_STAT_TOPOFF 0xe
80 #define CHG_STAT_DONE 0xf
81 #define CHG_STAT_OTP_TRICKLE 0x10
82 #define CHG_STAT_OTP_FAST 0x11
83 #define CHG_STAT_OTP_DONE 0x12
84 #define CHG_STAT_TSD_TRICKLE 0x20
85 #define CHG_STAT_TSD_FAST 0x21
86 #define CHG_STAT_TSD_TOPOFF 0x22
87 #define CHG_STAT_BAT_ERR 0x7f
89 static const char *bd70528_charger_model = "BD70528";
90 static const char *bd70528_charger_manufacturer = "ROHM Semiconductors";
92 #define BD_ERR_IRQ_HND(_name_, _wrn_) \
93 static irqreturn_t bd0528_##_name_##_interrupt(int irq, void *arg) \
95 struct power_supply *psy = (struct power_supply *)arg; \
97 power_supply_changed(psy); \
98 dev_err(&psy->dev, (_wrn_)); \
100 return IRQ_HANDLED; \
103 #define BD_INFO_IRQ_HND(_name_, _wrn_) \
104 static irqreturn_t bd0528_##_name_##_interrupt(int irq, void *arg) \
106 struct power_supply *psy = (struct power_supply *)arg; \
108 power_supply_changed(psy); \
109 dev_dbg(&psy->dev, (_wrn_)); \
111 return IRQ_HANDLED; \
114 #define BD_IRQ_HND(_name_) bd0528_##_name_##_interrupt
117 struct regmap *regmap;
119 struct power_supply *psy;
122 BD_ERR_IRQ_HND(BAT_OV_DET, "Battery overvoltage detected\n");
123 BD_ERR_IRQ_HND(DBAT_DET, "Dead battery detected\n");
124 BD_ERR_IRQ_HND(COLD_DET, "Battery cold\n");
125 BD_ERR_IRQ_HND(HOT_DET, "Battery hot\n");
126 BD_ERR_IRQ_HND(CHG_TSD, "Charger thermal shutdown\n");
127 BD_ERR_IRQ_HND(DCIN2_OV_DET, "DCIN2 overvoltage detected\n");
129 BD_INFO_IRQ_HND(BAT_OV_RES, "Battery voltage back to normal\n");
130 BD_INFO_IRQ_HND(COLD_RES, "Battery temperature back to normal\n");
131 BD_INFO_IRQ_HND(HOT_RES, "Battery temperature back to normal\n");
132 BD_INFO_IRQ_HND(BAT_RMV, "Battery removed\n");
133 BD_INFO_IRQ_HND(BAT_DET, "Battery detected\n");
134 BD_INFO_IRQ_HND(DCIN2_OV_RES, "DCIN2 voltage back to normal\n");
135 BD_INFO_IRQ_HND(DCIN2_RMV, "DCIN2 removed\n");
136 BD_INFO_IRQ_HND(DCIN2_DET, "DCIN2 detected\n");
137 BD_INFO_IRQ_HND(DCIN1_RMV, "DCIN1 removed\n");
138 BD_INFO_IRQ_HND(DCIN1_DET, "DCIN1 detected\n");
140 struct irq_name_pair {
142 irqreturn_t (*h)(int irq, void *arg);
145 static int bd70528_get_irqs(struct platform_device *pdev,
146 struct bd70528_psy *bdpsy)
150 static const struct irq_name_pair bd70528_chg_irqs[] = {
151 { .n = "bd70528-bat-ov-res", .h = BD_IRQ_HND(BAT_OV_RES) },
152 { .n = "bd70528-bat-ov-det", .h = BD_IRQ_HND(BAT_OV_DET) },
153 { .n = "bd70528-bat-dead", .h = BD_IRQ_HND(DBAT_DET) },
154 { .n = "bd70528-bat-warmed", .h = BD_IRQ_HND(COLD_RES) },
155 { .n = "bd70528-bat-cold", .h = BD_IRQ_HND(COLD_DET) },
156 { .n = "bd70528-bat-cooled", .h = BD_IRQ_HND(HOT_RES) },
157 { .n = "bd70528-bat-hot", .h = BD_IRQ_HND(HOT_DET) },
158 { .n = "bd70528-chg-tshd", .h = BD_IRQ_HND(CHG_TSD) },
159 { .n = "bd70528-bat-removed", .h = BD_IRQ_HND(BAT_RMV) },
160 { .n = "bd70528-bat-detected", .h = BD_IRQ_HND(BAT_DET) },
161 { .n = "bd70528-dcin2-ov-res", .h = BD_IRQ_HND(DCIN2_OV_RES) },
162 { .n = "bd70528-dcin2-ov-det", .h = BD_IRQ_HND(DCIN2_OV_DET) },
163 { .n = "bd70528-dcin2-removed", .h = BD_IRQ_HND(DCIN2_RMV) },
164 { .n = "bd70528-dcin2-detected", .h = BD_IRQ_HND(DCIN2_DET) },
165 { .n = "bd70528-dcin1-removed", .h = BD_IRQ_HND(DCIN1_RMV) },
166 { .n = "bd70528-dcin1-detected", .h = BD_IRQ_HND(DCIN1_DET) },
169 for (i = 0; i < ARRAY_SIZE(bd70528_chg_irqs); i++) {
170 irq = platform_get_irq_byname(pdev, bd70528_chg_irqs[i].n);
172 dev_err(&pdev->dev, "Bad IRQ information for %s (%d)\n",
173 bd70528_chg_irqs[i].n, irq);
176 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
177 bd70528_chg_irqs[i].h,
179 bd70528_chg_irqs[i].n,
186 * BD70528 irq controller is not touching the main mask register.
187 * So enable the charger block interrupts at main level. We can just
188 * leave them enabled as irq-controller should disable irqs
189 * from sub-registers when IRQ is disabled or freed.
191 mask = BD70528_REG_INT_BAT1_MASK | BD70528_REG_INT_BAT2_MASK;
192 ret = regmap_update_bits(bdpsy->regmap,
193 BD70528_REG_INT_MAIN_MASK, mask, 0);
195 dev_err(&pdev->dev, "Failed to enable charger IRQs\n");
200 static int bd70528_get_charger_status(struct bd70528_psy *bdpsy, int *val)
205 ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_CURR_STAT, &v);
207 dev_err(bdpsy->dev, "Charger state read failure %d\n",
212 switch (v & BD70528_MASK_CHG_STAT) {
213 case CHG_STAT_SUSPEND:
214 /* Maybe we should check the CHG_TTRI_EN? */
215 case CHG_STAT_OTP_TRICKLE:
216 case CHG_STAT_OTP_FAST:
217 case CHG_STAT_OTP_DONE:
218 case CHG_STAT_TSD_TRICKLE:
219 case CHG_STAT_TSD_FAST:
220 case CHG_STAT_TSD_TOPOFF:
221 case CHG_STAT_BAT_ERR:
222 *val = POWER_SUPPLY_STATUS_NOT_CHARGING;
225 *val = POWER_SUPPLY_STATUS_FULL;
227 case CHG_STAT_TRICKLE:
229 case CHG_STAT_TOPOFF:
230 *val = POWER_SUPPLY_STATUS_CHARGING;
233 *val = POWER_SUPPLY_STATUS_UNKNOWN;
240 static int bd70528_get_charge_type(struct bd70528_psy *bdpsy, int *val)
245 ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_CURR_STAT, &v);
247 dev_err(bdpsy->dev, "Charger state read failure %d\n",
252 switch (v & BD70528_MASK_CHG_STAT) {
253 case CHG_STAT_TRICKLE:
254 *val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
257 case CHG_STAT_TOPOFF:
258 *val = POWER_SUPPLY_CHARGE_TYPE_FAST;
261 case CHG_STAT_SUSPEND:
262 /* Maybe we should check the CHG_TTRI_EN? */
263 case CHG_STAT_OTP_TRICKLE:
264 case CHG_STAT_OTP_FAST:
265 case CHG_STAT_OTP_DONE:
266 case CHG_STAT_TSD_TRICKLE:
267 case CHG_STAT_TSD_FAST:
268 case CHG_STAT_TSD_TOPOFF:
269 case CHG_STAT_BAT_ERR:
270 *val = POWER_SUPPLY_CHARGE_TYPE_NONE;
273 *val = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
280 static int bd70528_get_battery_health(struct bd70528_psy *bdpsy, int *val)
285 ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_BAT_STAT, &v);
287 dev_err(bdpsy->dev, "Battery state read failure %d\n",
292 if (!(v & BD70528_MASK_CHG_BAT_DETECT))
293 *val = POWER_SUPPLY_HEALTH_DEAD;
294 else if (v & BD70528_MASK_CHG_BAT_OVERVOLT)
295 *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
296 else if (v & BD70528_MASK_CHG_BAT_TIMER)
297 *val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
299 *val = POWER_SUPPLY_HEALTH_GOOD;
304 static int bd70528_get_online(struct bd70528_psy *bdpsy, int *val)
309 ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_IN_STAT, &v);
311 dev_err(bdpsy->dev, "DC1 IN state read failure %d\n",
316 *val = (v & BD70528_MASK_CHG_DCIN1_UVLO) ? 1 : 0;
321 static int bd70528_get_present(struct bd70528_psy *bdpsy, int *val)
326 ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_BAT_STAT, &v);
328 dev_err(bdpsy->dev, "Battery state read failure %d\n",
333 *val = (v & BD70528_MASK_CHG_BAT_DETECT) ? 1 : 0;
338 struct linear_range {
345 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 find_value_for_selector_low(const struct linear_range *r,
402 int selectors, unsigned int sel,
407 for (i = 0; i < selectors; i++) {
408 if (r[i].low_sel <= sel && r[i].low_sel + r[i].vals >= sel) {
409 *val = r[i].min + (sel - r[i].low_sel) * r[i].step;
417 * For BD70528 voltage/current limits we happily accept any value which
418 * belongs the range. We could check if value matching the selector is
419 * desired by computing the range min + (sel - sel_low) * range step - but
420 * I guess it is enough if we use voltage/current which is closest (below)
423 static int find_selector_for_value_low(const struct linear_range *r,
424 int selectors, unsigned int val,
425 unsigned int *sel, bool *found)
431 for (i = 0; i < selectors; i++) {
432 if (r[i].min <= val) {
433 if (r[i].min + r[i].step * r[i].vals >= val) {
435 *sel = r[i].low_sel + (val - r[i].min) /
441 * If the range max is smaller than requested
442 * we can set the max supported value from range
444 *sel = r[i].low_sel + r[i].vals;
451 static int get_charge_current(struct bd70528_psy *bdpsy, int *ma)
456 ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_CHG_CURR_WARM,
460 "Charge current reading failed (%d)\n", ret);
464 sel &= BD70528_MASK_CHG_CHG_CURR;
466 ret = find_value_for_selector_low(&warm_charge_curr[0],
467 ARRAY_SIZE(warm_charge_curr), sel,
471 "Unknown charge current value 0x%x\n",
478 static int get_current_limit(struct bd70528_psy *bdpsy, int *ma)
483 ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_DCIN_ILIM,
488 "Input current limit reading failed (%d)\n", ret);
492 sel &= BD70528_MASK_CHG_DCIN_ILIM;
494 ret = find_value_for_selector_low(¤t_limit_ranges[0],
495 ARRAY_SIZE(current_limit_ranges), sel,
499 /* Unspecified values mean 500 mA */
505 static enum power_supply_property bd70528_charger_props[] = {
506 POWER_SUPPLY_PROP_STATUS,
507 POWER_SUPPLY_PROP_CHARGE_TYPE,
508 POWER_SUPPLY_PROP_HEALTH,
509 POWER_SUPPLY_PROP_PRESENT,
510 POWER_SUPPLY_PROP_ONLINE,
511 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
512 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
513 POWER_SUPPLY_PROP_MODEL_NAME,
514 POWER_SUPPLY_PROP_MANUFACTURER,
517 static int bd70528_charger_get_property(struct power_supply *psy,
518 enum power_supply_property psp,
519 union power_supply_propval *val)
521 struct bd70528_psy *bdpsy = power_supply_get_drvdata(psy);
525 case POWER_SUPPLY_PROP_STATUS:
526 return bd70528_get_charger_status(bdpsy, &val->intval);
527 case POWER_SUPPLY_PROP_CHARGE_TYPE:
528 return bd70528_get_charge_type(bdpsy, &val->intval);
529 case POWER_SUPPLY_PROP_HEALTH:
530 return bd70528_get_battery_health(bdpsy, &val->intval);
531 case POWER_SUPPLY_PROP_PRESENT:
532 return bd70528_get_present(bdpsy, &val->intval);
533 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
534 ret = get_current_limit(bdpsy, &val->intval);
537 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
538 ret = get_charge_current(bdpsy, &val->intval);
541 case POWER_SUPPLY_PROP_ONLINE:
542 return bd70528_get_online(bdpsy, &val->intval);
543 case POWER_SUPPLY_PROP_MODEL_NAME:
544 val->strval = bd70528_charger_model;
546 case POWER_SUPPLY_PROP_MANUFACTURER:
547 val->strval = bd70528_charger_manufacturer;
556 static int bd70528_prop_is_writable(struct power_supply *psy,
557 enum power_supply_property psp)
560 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
561 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
569 static int set_charge_current(struct bd70528_psy *bdpsy, int ma)
577 "Requested charge current %u exceed maximum (500mA)\n",
579 reg = MAX_WARM_CHG_CURR_SEL;
584 "Requested charge current %u smaller than min (10mA)\n",
586 reg = MIN_CHG_CURR_SEL;
591 ret = find_selector_for_value_low(&warm_charge_curr[0],
592 ARRAY_SIZE(warm_charge_curr), ma,
595 reg = MIN_CHG_CURR_SEL;
599 /* There was a gap in supported values and we hit it */
601 "Unsupported charge current %u mA\n", ma);
605 tmpret = regmap_update_bits(bdpsy->regmap,
606 BD70528_REG_CHG_CHG_CURR_WARM,
607 BD70528_MASK_CHG_CHG_CURR, reg);
610 "Charge current write failure (%d)\n", tmpret);
612 if (reg > MAX_COLD_CHG_CURR_SEL)
613 reg = MAX_COLD_CHG_CURR_SEL;
616 tmpret = regmap_update_bits(bdpsy->regmap,
617 BD70528_REG_CHG_CHG_CURR_COLD,
618 BD70528_MASK_CHG_CHG_CURR, reg);
626 #define MAX_CURR_LIMIT_SEL 0x34
627 #define MIN_CURR_LIMIT_SEL 0x0
629 static int set_current_limit(struct bd70528_psy *bdpsy, int ma)
637 "Requested current limit %u exceed maximum (500mA)\n",
639 reg = MAX_CURR_LIMIT_SEL;
644 "Requested current limit %u smaller than min (5mA)\n",
646 reg = MIN_CURR_LIMIT_SEL;
651 ret = find_selector_for_value_low(¤t_limit_ranges[0],
652 ARRAY_SIZE(current_limit_ranges), ma,
655 reg = MIN_CURR_LIMIT_SEL;
659 /* There was a gap in supported values and we hit it ?*/
660 dev_warn(bdpsy->dev, "Unsupported current limit %umA\n",
665 tmpret = regmap_update_bits(bdpsy->regmap,
666 BD70528_REG_CHG_DCIN_ILIM,
667 BD70528_MASK_CHG_DCIN_ILIM, reg);
675 static int bd70528_charger_set_property(struct power_supply *psy,
676 enum power_supply_property psp,
677 const union power_supply_propval *val)
679 struct bd70528_psy *bdpsy = power_supply_get_drvdata(psy);
682 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
683 return set_current_limit(bdpsy, val->intval / 1000);
684 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
685 return set_charge_current(bdpsy, val->intval / 1000);
692 static const struct power_supply_desc bd70528_charger_desc = {
693 .name = "bd70528-charger",
694 .type = POWER_SUPPLY_TYPE_MAINS,
695 .properties = bd70528_charger_props,
696 .num_properties = ARRAY_SIZE(bd70528_charger_props),
697 .get_property = bd70528_charger_get_property,
698 .set_property = bd70528_charger_set_property,
699 .property_is_writeable = bd70528_prop_is_writable,
702 static int bd70528_power_probe(struct platform_device *pdev)
704 struct bd70528_psy *bdpsy;
705 struct power_supply_config cfg = {};
707 bdpsy = devm_kzalloc(&pdev->dev, sizeof(*bdpsy), GFP_KERNEL);
711 bdpsy->regmap = dev_get_regmap(pdev->dev.parent, NULL);
712 if (!bdpsy->regmap) {
713 dev_err(&pdev->dev, "No regmap found for chip\n");
716 bdpsy->dev = &pdev->dev;
718 platform_set_drvdata(pdev, bdpsy);
719 cfg.drv_data = bdpsy;
720 cfg.of_node = pdev->dev.parent->of_node;
722 bdpsy->psy = devm_power_supply_register(&pdev->dev,
723 &bd70528_charger_desc, &cfg);
724 if (IS_ERR(bdpsy->psy)) {
725 dev_err(&pdev->dev, "failed: power supply register\n");
726 return PTR_ERR(bdpsy->psy);
729 return bd70528_get_irqs(pdev, bdpsy);
732 static struct platform_driver bd70528_power = {
734 .name = "bd70528-power"
736 .probe = bd70528_power_probe,
739 module_platform_driver(bd70528_power);
741 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
742 MODULE_DESCRIPTION("BD70528 power-supply driver");
743 MODULE_LICENSE("GPL");
744 MODULE_ALIAS("platform:bd70528-power");