1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADP5061 I2C Programmable Linear Battery Charger
5 * Copyright 2018 Analog Devices Inc.
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/i2c.h>
12 #include <linux/delay.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/power_supply.h>
16 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
20 /* ADP5061 registers definition */
21 #define ADP5061_ID 0x00
22 #define ADP5061_REV 0x01
23 #define ADP5061_VINX_SET 0x02
24 #define ADP5061_TERM_SET 0x03
25 #define ADP5061_CHG_CURR 0x04
26 #define ADP5061_VOLTAGE_TH 0x05
27 #define ADP5061_TIMER_SET 0x06
28 #define ADP5061_FUNC_SET_1 0x07
29 #define ADP5061_FUNC_SET_2 0x08
30 #define ADP5061_INT_EN 0x09
31 #define ADP5061_INT_ACT 0x0A
32 #define ADP5061_CHG_STATUS_1 0x0B
33 #define ADP5061_CHG_STATUS_2 0x0C
34 #define ADP5061_FAULT 0x0D
35 #define ADP5061_BATTERY_SHORT 0x10
36 #define ADP5061_IEND 0x11
38 /* ADP5061_VINX_SET */
39 #define ADP5061_VINX_SET_ILIM_MSK GENMASK(3, 0)
40 #define ADP5061_VINX_SET_ILIM_MODE(x) (((x) & 0x0F) << 0)
42 /* ADP5061_TERM_SET */
43 #define ADP5061_TERM_SET_VTRM_MSK GENMASK(7, 2)
44 #define ADP5061_TERM_SET_VTRM_MODE(x) (((x) & 0x3F) << 2)
45 #define ADP5061_TERM_SET_CHG_VLIM_MSK GENMASK(1, 0)
46 #define ADP5061_TERM_SET_CHG_VLIM_MODE(x) (((x) & 0x03) << 0)
48 /* ADP5061_CHG_CURR */
49 #define ADP5061_CHG_CURR_ICHG_MSK GENMASK(6, 2)
50 #define ADP5061_CHG_CURR_ICHG_MODE(x) (((x) & 0x1F) << 2)
51 #define ADP5061_CHG_CURR_ITRK_DEAD_MSK GENMASK(1, 0)
52 #define ADP5061_CHG_CURR_ITRK_DEAD_MODE(x) (((x) & 0x03) << 0)
54 /* ADP5061_VOLTAGE_TH */
55 #define ADP5061_VOLTAGE_TH_DIS_RCH_MSK BIT(7)
56 #define ADP5061_VOLTAGE_TH_DIS_RCH_MODE(x) (((x) & 0x01) << 7)
57 #define ADP5061_VOLTAGE_TH_VRCH_MSK GENMASK(6, 5)
58 #define ADP5061_VOLTAGE_TH_VRCH_MODE(x) (((x) & 0x03) << 5)
59 #define ADP5061_VOLTAGE_TH_VTRK_DEAD_MSK GENMASK(4, 3)
60 #define ADP5061_VOLTAGE_TH_VTRK_DEAD_MODE(x) (((x) & 0x03) << 3)
61 #define ADP5061_VOLTAGE_TH_VWEAK_MSK GENMASK(2, 0)
62 #define ADP5061_VOLTAGE_TH_VWEAK_MODE(x) (((x) & 0x07) << 0)
64 /* ADP5061_CHG_STATUS_1 */
65 #define ADP5061_CHG_STATUS_1_VIN_OV(x) (((x) >> 7) & 0x1)
66 #define ADP5061_CHG_STATUS_1_VIN_OK(x) (((x) >> 6) & 0x1)
67 #define ADP5061_CHG_STATUS_1_VIN_ILIM(x) (((x) >> 5) & 0x1)
68 #define ADP5061_CHG_STATUS_1_THERM_LIM(x) (((x) >> 4) & 0x1)
69 #define ADP5061_CHG_STATUS_1_CHDONE(x) (((x) >> 3) & 0x1)
70 #define ADP5061_CHG_STATUS_1_CHG_STATUS(x) (((x) >> 0) & 0x7)
72 /* ADP5061_CHG_STATUS_2 */
73 #define ADP5061_CHG_STATUS_2_THR_STATUS(x) (((x) >> 5) & 0x7)
74 #define ADP5061_CHG_STATUS_2_RCH_LIM_INFO(x) (((x) >> 3) & 0x1)
75 #define ADP5061_CHG_STATUS_2_BAT_STATUS(x) (((x) >> 0) & 0x7)
78 #define ADP5061_IEND_IEND_MSK GENMASK(7, 5)
79 #define ADP5061_IEND_IEND_MODE(x) (((x) & 0x07) << 5)
81 #define ADP5061_NO_BATTERY 0x01
82 #define ADP5061_ICHG_MAX 1300 // mA
84 enum adp5061_chg_status {
91 ADP5061_CHG_TIMER_EXP,
95 static const int adp5061_chg_type[4] = {
96 [ADP5061_CHG_OFF] = POWER_SUPPLY_CHARGE_TYPE_NONE,
97 [ADP5061_CHG_TRICKLE] = POWER_SUPPLY_CHARGE_TYPE_TRICKLE,
98 [ADP5061_CHG_FAST_CC] = POWER_SUPPLY_CHARGE_TYPE_FAST,
99 [ADP5061_CHG_FAST_CV] = POWER_SUPPLY_CHARGE_TYPE_FAST,
102 static const int adp5061_vweak_th[8] = {
103 2700, 2800, 2900, 3000, 3100, 3200, 3300, 3400,
106 static const int adp5061_prechg_current[4] = {
110 static const int adp5061_vmin[4] = {
111 2000, 2500, 2600, 2900,
114 static const int adp5061_const_chg_vmax[4] = {
115 3200, 3400, 3700, 3800,
118 static const int adp5061_const_ichg[24] = {
119 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650,
120 700, 750, 800, 850, 900, 950, 1000, 1050, 1100, 1200, 1300,
123 static const int adp5061_vmax[36] = {
124 3800, 3820, 3840, 3860, 3880, 3900, 3920, 3940, 3960, 3980,
125 4000, 4020, 4040, 4060, 4080, 4100, 4120, 4140, 4160, 4180,
126 4200, 4220, 4240, 4260, 4280, 4300, 4320, 4340, 4360, 4380,
127 4400, 4420, 4440, 4460, 4480, 4500,
130 static const int adp5061_in_current_lim[16] = {
131 100, 150, 200, 250, 300, 400, 500, 600, 700,
132 800, 900, 1000, 1200, 1500, 1800, 2100,
135 static const int adp5061_iend[8] = {
136 12500, 32500, 52500, 72500, 92500, 117500, 142500, 170000,
139 struct adp5061_state {
140 struct i2c_client *client;
141 struct regmap *regmap;
142 struct power_supply *psy;
145 static int adp5061_get_array_index(const int *array, u8 size, int val)
149 for (i = 1; i < size; i++) {
157 static int adp5061_get_status(struct adp5061_state *st,
158 u8 *status1, u8 *status2)
163 /* CHG_STATUS1 and CHG_STATUS2 are adjacent regs */
164 ret = regmap_bulk_read(st->regmap, ADP5061_CHG_STATUS_1,
175 static int adp5061_get_input_current_limit(struct adp5061_state *st,
176 union power_supply_propval *val)
181 ret = regmap_read(st->regmap, ADP5061_VINX_SET, ®val);
185 mode = ADP5061_VINX_SET_ILIM_MODE(regval);
186 val->intval = adp5061_in_current_lim[mode] * 1000;
191 static int adp5061_set_input_current_limit(struct adp5061_state *st, int val)
195 /* Convert from uA to mA */
197 index = adp5061_get_array_index(adp5061_in_current_lim,
198 ARRAY_SIZE(adp5061_in_current_lim),
203 return regmap_update_bits(st->regmap, ADP5061_VINX_SET,
204 ADP5061_VINX_SET_ILIM_MSK,
205 ADP5061_VINX_SET_ILIM_MODE(index));
208 static int adp5061_set_min_voltage(struct adp5061_state *st, int val)
212 /* Convert from uV to mV */
214 index = adp5061_get_array_index(adp5061_vmin,
215 ARRAY_SIZE(adp5061_vmin),
220 return regmap_update_bits(st->regmap, ADP5061_VOLTAGE_TH,
221 ADP5061_VOLTAGE_TH_VTRK_DEAD_MSK,
222 ADP5061_VOLTAGE_TH_VTRK_DEAD_MODE(index));
225 static int adp5061_get_min_voltage(struct adp5061_state *st,
226 union power_supply_propval *val)
231 ret = regmap_read(st->regmap, ADP5061_VOLTAGE_TH, ®val);
235 regval = ((regval & ADP5061_VOLTAGE_TH_VTRK_DEAD_MSK) >> 3);
236 val->intval = adp5061_vmin[regval] * 1000;
241 static int adp5061_get_chg_volt_lim(struct adp5061_state *st,
242 union power_supply_propval *val)
247 ret = regmap_read(st->regmap, ADP5061_TERM_SET, ®val);
251 mode = ADP5061_TERM_SET_CHG_VLIM_MODE(regval);
252 val->intval = adp5061_const_chg_vmax[mode] * 1000;
257 static int adp5061_get_max_voltage(struct adp5061_state *st,
258 union power_supply_propval *val)
263 ret = regmap_read(st->regmap, ADP5061_TERM_SET, ®val);
267 regval = ((regval & ADP5061_TERM_SET_VTRM_MSK) >> 2) - 0x0F;
268 if (regval >= ARRAY_SIZE(adp5061_vmax))
269 regval = ARRAY_SIZE(adp5061_vmax) - 1;
271 val->intval = adp5061_vmax[regval] * 1000;
276 static int adp5061_set_max_voltage(struct adp5061_state *st, int val)
280 /* Convert from uV to mV */
285 vmax_index = adp5061_get_array_index(adp5061_vmax,
286 ARRAY_SIZE(adp5061_vmax), val);
292 return regmap_update_bits(st->regmap, ADP5061_TERM_SET,
293 ADP5061_TERM_SET_VTRM_MSK,
294 ADP5061_TERM_SET_VTRM_MODE(vmax_index));
297 static int adp5061_set_const_chg_vmax(struct adp5061_state *st, int val)
301 /* Convert from uV to mV */
303 index = adp5061_get_array_index(adp5061_const_chg_vmax,
304 ARRAY_SIZE(adp5061_const_chg_vmax),
309 return regmap_update_bits(st->regmap, ADP5061_TERM_SET,
310 ADP5061_TERM_SET_CHG_VLIM_MSK,
311 ADP5061_TERM_SET_CHG_VLIM_MODE(index));
314 static int adp5061_set_const_chg_current(struct adp5061_state *st, int val)
319 /* Convert from uA to mA */
321 if (val > ADP5061_ICHG_MAX)
322 val = ADP5061_ICHG_MAX;
324 index = adp5061_get_array_index(adp5061_const_ichg,
325 ARRAY_SIZE(adp5061_const_ichg),
330 return regmap_update_bits(st->regmap, ADP5061_CHG_CURR,
331 ADP5061_CHG_CURR_ICHG_MSK,
332 ADP5061_CHG_CURR_ICHG_MODE(index));
335 static int adp5061_get_const_chg_current(struct adp5061_state *st,
336 union power_supply_propval *val)
341 ret = regmap_read(st->regmap, ADP5061_CHG_CURR, ®val);
345 regval = ((regval & ADP5061_CHG_CURR_ICHG_MSK) >> 2);
346 if (regval >= ARRAY_SIZE(adp5061_const_ichg))
347 regval = ARRAY_SIZE(adp5061_const_ichg) - 1;
349 val->intval = adp5061_const_ichg[regval] * 1000;
354 static int adp5061_get_prechg_current(struct adp5061_state *st,
355 union power_supply_propval *val)
360 ret = regmap_read(st->regmap, ADP5061_CHG_CURR, ®val);
364 regval &= ADP5061_CHG_CURR_ITRK_DEAD_MSK;
365 val->intval = adp5061_prechg_current[regval] * 1000;
370 static int adp5061_set_prechg_current(struct adp5061_state *st, int val)
374 /* Convert from uA to mA */
376 index = adp5061_get_array_index(adp5061_prechg_current,
377 ARRAY_SIZE(adp5061_prechg_current),
382 return regmap_update_bits(st->regmap, ADP5061_CHG_CURR,
383 ADP5061_CHG_CURR_ITRK_DEAD_MSK,
384 ADP5061_CHG_CURR_ITRK_DEAD_MODE(index));
387 static int adp5061_get_vweak_th(struct adp5061_state *st,
388 union power_supply_propval *val)
393 ret = regmap_read(st->regmap, ADP5061_VOLTAGE_TH, ®val);
397 regval &= ADP5061_VOLTAGE_TH_VWEAK_MSK;
398 val->intval = adp5061_vweak_th[regval] * 1000;
403 static int adp5061_set_vweak_th(struct adp5061_state *st, int val)
407 /* Convert from uV to mV */
409 index = adp5061_get_array_index(adp5061_vweak_th,
410 ARRAY_SIZE(adp5061_vweak_th),
415 return regmap_update_bits(st->regmap, ADP5061_VOLTAGE_TH,
416 ADP5061_VOLTAGE_TH_VWEAK_MSK,
417 ADP5061_VOLTAGE_TH_VWEAK_MODE(index));
420 static int adp5061_get_chg_type(struct adp5061_state *st,
421 union power_supply_propval *val)
426 ret = adp5061_get_status(st, &status1, &status2);
430 chg_type = ADP5061_CHG_STATUS_1_CHG_STATUS(status1);
431 if (chg_type >= ARRAY_SIZE(adp5061_chg_type))
432 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
434 val->intval = adp5061_chg_type[chg_type];
439 static int adp5061_get_charger_status(struct adp5061_state *st,
440 union power_supply_propval *val)
445 ret = adp5061_get_status(st, &status1, &status2);
449 switch (ADP5061_CHG_STATUS_1_CHG_STATUS(status1)) {
450 case ADP5061_CHG_OFF:
451 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
453 case ADP5061_CHG_TRICKLE:
454 case ADP5061_CHG_FAST_CC:
455 case ADP5061_CHG_FAST_CV:
456 val->intval = POWER_SUPPLY_STATUS_CHARGING;
458 case ADP5061_CHG_COMPLETE:
459 val->intval = POWER_SUPPLY_STATUS_FULL;
461 case ADP5061_CHG_TIMER_EXP:
462 /* The battery must be discharging if there is a charge fault */
463 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
466 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
472 static int adp5061_get_battery_status(struct adp5061_state *st,
473 union power_supply_propval *val)
478 ret = adp5061_get_status(st, &status1, &status2);
482 switch (ADP5061_CHG_STATUS_2_BAT_STATUS(status2)) {
483 case 0x0: /* Battery monitor off */
484 case 0x1: /* No battery */
485 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
487 case 0x2: /* VBAT < VTRK */
488 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
490 case 0x3: /* VTRK < VBAT_SNS < VWEAK */
491 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
493 case 0x4: /* VBAT_SNS > VWEAK */
494 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
497 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
504 static int adp5061_get_termination_current(struct adp5061_state *st,
505 union power_supply_propval *val)
510 ret = regmap_read(st->regmap, ADP5061_IEND, ®val);
514 regval = (regval & ADP5061_IEND_IEND_MSK) >> 5;
515 val->intval = adp5061_iend[regval];
520 static int adp5061_set_termination_current(struct adp5061_state *st, int val)
524 index = adp5061_get_array_index(adp5061_iend,
525 ARRAY_SIZE(adp5061_iend),
530 return regmap_update_bits(st->regmap, ADP5061_IEND,
531 ADP5061_IEND_IEND_MSK,
532 ADP5061_IEND_IEND_MODE(index));
535 static int adp5061_get_property(struct power_supply *psy,
536 enum power_supply_property psp,
537 union power_supply_propval *val)
539 struct adp5061_state *st = power_supply_get_drvdata(psy);
544 case POWER_SUPPLY_PROP_PRESENT:
545 ret = adp5061_get_status(st, &status1, &status2);
549 mode = ADP5061_CHG_STATUS_2_BAT_STATUS(status2);
550 if (mode == ADP5061_NO_BATTERY)
555 case POWER_SUPPLY_PROP_CHARGE_TYPE:
556 return adp5061_get_chg_type(st, val);
557 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
558 /* This property is used to indicate the input current
559 * limit into VINx (ILIM)
561 return adp5061_get_input_current_limit(st, val);
562 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
563 /* This property is used to indicate the termination
566 return adp5061_get_max_voltage(st, val);
567 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
569 * This property is used to indicate the trickle to fast
570 * charge threshold (VTRK_DEAD)
572 return adp5061_get_min_voltage(st, val);
573 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
574 /* This property is used to indicate the charging
575 * voltage limit (CHG_VLIM)
577 return adp5061_get_chg_volt_lim(st, val);
578 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
580 * This property is used to indicate the value of the constant
581 * current charge (ICHG)
583 return adp5061_get_const_chg_current(st, val);
584 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
586 * This property is used to indicate the value of the trickle
587 * and weak charge currents (ITRK_DEAD)
589 return adp5061_get_prechg_current(st, val);
590 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
592 * This property is used to set the VWEAK threshold
593 * bellow this value, weak charge mode is entered
594 * above this value, fast chargerge mode is entered
596 return adp5061_get_vweak_th(st, val);
597 case POWER_SUPPLY_PROP_STATUS:
599 * Indicate the charger status in relation to power
600 * supply status property
602 return adp5061_get_charger_status(st, val);
603 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
605 * Indicate the battery status in relation to power
606 * supply capacity level property
608 return adp5061_get_battery_status(st, val);
609 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
610 /* Indicate the values of the termination current */
611 return adp5061_get_termination_current(st, val);
619 static int adp5061_set_property(struct power_supply *psy,
620 enum power_supply_property psp,
621 const union power_supply_propval *val)
623 struct adp5061_state *st = power_supply_get_drvdata(psy);
626 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
627 return adp5061_set_input_current_limit(st, val->intval);
628 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
629 return adp5061_set_max_voltage(st, val->intval);
630 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
631 return adp5061_set_min_voltage(st, val->intval);
632 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
633 return adp5061_set_const_chg_vmax(st, val->intval);
634 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
635 return adp5061_set_const_chg_current(st, val->intval);
636 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
637 return adp5061_set_prechg_current(st, val->intval);
638 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
639 return adp5061_set_vweak_th(st, val->intval);
640 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
641 return adp5061_set_termination_current(st, val->intval);
649 static int adp5061_prop_writeable(struct power_supply *psy,
650 enum power_supply_property psp)
653 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
654 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
655 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
656 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
657 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
658 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
659 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
660 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
667 static enum power_supply_property adp5061_props[] = {
668 POWER_SUPPLY_PROP_PRESENT,
669 POWER_SUPPLY_PROP_CHARGE_TYPE,
670 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
671 POWER_SUPPLY_PROP_VOLTAGE_MAX,
672 POWER_SUPPLY_PROP_VOLTAGE_MIN,
673 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
674 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
675 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
676 POWER_SUPPLY_PROP_VOLTAGE_AVG,
677 POWER_SUPPLY_PROP_STATUS,
678 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
679 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
682 static const struct regmap_config adp5061_regmap_config = {
687 static const struct power_supply_desc adp5061_desc = {
689 .type = POWER_SUPPLY_TYPE_USB,
690 .get_property = adp5061_get_property,
691 .set_property = adp5061_set_property,
692 .property_is_writeable = adp5061_prop_writeable,
693 .properties = adp5061_props,
694 .num_properties = ARRAY_SIZE(adp5061_props),
697 static int adp5061_probe(struct i2c_client *client)
699 struct power_supply_config psy_cfg = {};
700 struct adp5061_state *st;
702 st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
707 st->regmap = devm_regmap_init_i2c(client,
708 &adp5061_regmap_config);
709 if (IS_ERR(st->regmap)) {
710 dev_err(&client->dev, "Failed to initialize register map\n");
714 i2c_set_clientdata(client, st);
715 psy_cfg.drv_data = st;
717 st->psy = devm_power_supply_register(&client->dev,
721 if (IS_ERR(st->psy)) {
722 dev_err(&client->dev, "Failed to register power supply\n");
723 return PTR_ERR(st->psy);
729 static const struct i2c_device_id adp5061_id[] = {
733 MODULE_DEVICE_TABLE(i2c, adp5061_id);
735 static struct i2c_driver adp5061_driver = {
737 .name = KBUILD_MODNAME,
739 .probe = adp5061_probe,
740 .id_table = adp5061_id,
742 module_i2c_driver(adp5061_driver);
744 MODULE_DESCRIPTION("Analog Devices adp5061 battery charger driver");
745 MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
746 MODULE_LICENSE("GPL v2");