2 * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
4 * Copyright 2016 Free Electrons NextThing Co.
5 * Quentin Schulz <quentin.schulz@free-electrons.com>
7 * This driver is based on a previous upstreaming attempt by:
8 * Bruno Prémont <bonbons@linux-vserver.org>
10 * This file is subject to the terms and conditions of the GNU General
11 * Public License. See the file "COPYING" in the main directory of this
12 * archive for more details.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/err.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/power_supply.h>
27 #include <linux/regmap.h>
28 #include <linux/slab.h>
29 #include <linux/time.h>
30 #include <linux/iio/iio.h>
31 #include <linux/iio/consumer.h>
32 #include <linux/mfd/axp20x.h>
34 #define AXP20X_PWR_STATUS_BAT_CHARGING BIT(2)
36 #define AXP20X_PWR_OP_BATT_PRESENT BIT(5)
37 #define AXP20X_PWR_OP_BATT_ACTIVATED BIT(3)
39 #define AXP209_FG_PERCENT GENMASK(6, 0)
40 #define AXP22X_FG_VALID BIT(7)
42 #define AXP20X_CHRG_CTRL1_ENABLE BIT(7)
43 #define AXP20X_CHRG_CTRL1_TGT_VOLT GENMASK(6, 5)
44 #define AXP20X_CHRG_CTRL1_TGT_4_1V (0 << 5)
45 #define AXP20X_CHRG_CTRL1_TGT_4_15V (1 << 5)
46 #define AXP20X_CHRG_CTRL1_TGT_4_2V (2 << 5)
47 #define AXP20X_CHRG_CTRL1_TGT_4_36V (3 << 5)
49 #define AXP22X_CHRG_CTRL1_TGT_4_22V (1 << 5)
50 #define AXP22X_CHRG_CTRL1_TGT_4_24V (3 << 5)
52 #define AXP813_CHRG_CTRL1_TGT_4_35V (3 << 5)
54 #define AXP20X_CHRG_CTRL1_TGT_CURR GENMASK(3, 0)
56 #define AXP20X_V_OFF_MASK GENMASK(2, 0)
58 struct axp20x_batt_ps;
64 int (*get_max_voltage)(struct axp20x_batt_ps *batt, int *val);
65 int (*set_max_voltage)(struct axp20x_batt_ps *batt, int val);
68 struct axp20x_batt_ps {
69 struct regmap *regmap;
70 struct power_supply *batt;
72 struct iio_channel *batt_chrg_i;
73 struct iio_channel *batt_dischrg_i;
74 struct iio_channel *batt_v;
75 /* Maximum constant charge current */
77 const struct axp_data *data;
80 static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
85 ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
89 switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
90 case AXP20X_CHRG_CTRL1_TGT_4_1V:
93 case AXP20X_CHRG_CTRL1_TGT_4_15V:
96 case AXP20X_CHRG_CTRL1_TGT_4_2V:
99 case AXP20X_CHRG_CTRL1_TGT_4_36V:
109 static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
114 ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
118 switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
119 case AXP20X_CHRG_CTRL1_TGT_4_1V:
122 case AXP20X_CHRG_CTRL1_TGT_4_2V:
125 case AXP22X_CHRG_CTRL1_TGT_4_22V:
128 case AXP22X_CHRG_CTRL1_TGT_4_24V:
138 static int axp813_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
143 ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
147 switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
148 case AXP20X_CHRG_CTRL1_TGT_4_1V:
151 case AXP20X_CHRG_CTRL1_TGT_4_15V:
154 case AXP20X_CHRG_CTRL1_TGT_4_2V:
157 case AXP813_CHRG_CTRL1_TGT_4_35V:
167 static int axp20x_get_constant_charge_current(struct axp20x_batt_ps *axp,
172 ret = regmap_read(axp->regmap, AXP20X_CHRG_CTRL1, val);
176 *val &= AXP20X_CHRG_CTRL1_TGT_CURR;
178 *val = *val * axp->data->ccc_scale + axp->data->ccc_offset;
183 static int axp20x_battery_get_prop(struct power_supply *psy,
184 enum power_supply_property psp,
185 union power_supply_propval *val)
187 struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
188 int ret = 0, reg, val1;
191 case POWER_SUPPLY_PROP_PRESENT:
192 case POWER_SUPPLY_PROP_ONLINE:
193 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
198 val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
201 case POWER_SUPPLY_PROP_STATUS:
202 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
207 if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
208 val->intval = POWER_SUPPLY_STATUS_CHARGING;
212 ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
218 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
222 ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
227 * Fuel Gauge data takes 7 bits but the stored value seems to be
228 * directly the raw percentage without any scaling to 7 bits.
230 if ((val1 & AXP209_FG_PERCENT) == 100)
231 val->intval = POWER_SUPPLY_STATUS_FULL;
233 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
236 case POWER_SUPPLY_PROP_HEALTH:
237 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
242 if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
243 val->intval = POWER_SUPPLY_HEALTH_DEAD;
247 val->intval = POWER_SUPPLY_HEALTH_GOOD;
250 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
251 ret = axp20x_get_constant_charge_current(axp20x_batt,
257 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
258 val->intval = axp20x_batt->max_ccc;
261 case POWER_SUPPLY_PROP_CURRENT_NOW:
262 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
267 if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
268 ret = iio_read_channel_processed(axp20x_batt->batt_chrg_i, &val->intval);
270 ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i, &val1);
276 /* IIO framework gives mA but Power Supply framework gives uA */
280 case POWER_SUPPLY_PROP_CAPACITY:
281 /* When no battery is present, return capacity is 100% */
282 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
287 if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
292 ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, ®);
296 if (axp20x_batt->data->has_fg_valid && !(reg & AXP22X_FG_VALID))
300 * Fuel Gauge data takes 7 bits but the stored value seems to be
301 * directly the raw percentage without any scaling to 7 bits.
303 val->intval = reg & AXP209_FG_PERCENT;
306 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
307 return axp20x_batt->data->get_max_voltage(axp20x_batt,
310 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
311 ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, ®);
315 val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
318 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
319 ret = iio_read_channel_processed(axp20x_batt->batt_v,
324 /* IIO framework gives mV but Power Supply framework gives uV */
335 static int axp22x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
340 val = AXP20X_CHRG_CTRL1_TGT_4_1V;
344 val = AXP20X_CHRG_CTRL1_TGT_4_2V;
349 * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
350 * can be set to 4.22V and 4.24V, but these voltages are too
351 * high for Lithium based batteries (AXP PMICs are supposed to
352 * be used with these kinds of battery).
357 return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
358 AXP20X_CHRG_CTRL1_TGT_VOLT, val);
361 static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
366 val = AXP20X_CHRG_CTRL1_TGT_4_1V;
370 val = AXP20X_CHRG_CTRL1_TGT_4_15V;
374 val = AXP20X_CHRG_CTRL1_TGT_4_2V;
379 * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
380 * can be set to 4.22V and 4.24V, but these voltages are too
381 * high for Lithium based batteries (AXP PMICs are supposed to
382 * be used with these kinds of battery).
387 return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
388 AXP20X_CHRG_CTRL1_TGT_VOLT, val);
391 static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
394 if (charge_current > axp_batt->max_ccc)
397 charge_current = (charge_current - axp_batt->data->ccc_offset) /
398 axp_batt->data->ccc_scale;
400 if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
403 return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
404 AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
407 static int axp20x_set_max_constant_charge_current(struct axp20x_batt_ps *axp,
410 bool lower_max = false;
412 charge_current = (charge_current - axp->data->ccc_offset) /
413 axp->data->ccc_scale;
415 if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
418 charge_current = charge_current * axp->data->ccc_scale +
419 axp->data->ccc_offset;
421 if (charge_current > axp->max_ccc)
423 "Setting max constant charge current higher than previously defined. Note that increasing the constant charge current may damage your battery.\n");
427 axp->max_ccc = charge_current;
432 axp20x_get_constant_charge_current(axp, ¤t_cc);
433 if (current_cc > charge_current)
434 axp20x_set_constant_charge_current(axp, charge_current);
439 static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
442 int val1 = (min_voltage - 2600000) / 100000;
444 if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
447 return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
448 AXP20X_V_OFF_MASK, val1);
451 static int axp20x_battery_set_prop(struct power_supply *psy,
452 enum power_supply_property psp,
453 const union power_supply_propval *val)
455 struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
458 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
459 return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
461 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
462 return axp20x_batt->data->set_max_voltage(axp20x_batt, val->intval);
464 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
465 return axp20x_set_constant_charge_current(axp20x_batt,
467 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
468 return axp20x_set_max_constant_charge_current(axp20x_batt,
470 case POWER_SUPPLY_PROP_STATUS:
471 switch (val->intval) {
472 case POWER_SUPPLY_STATUS_CHARGING:
473 return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
474 AXP20X_CHRG_CTRL1_ENABLE, AXP20X_CHRG_CTRL1_ENABLE);
476 case POWER_SUPPLY_STATUS_DISCHARGING:
477 case POWER_SUPPLY_STATUS_NOT_CHARGING:
478 return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
479 AXP20X_CHRG_CTRL1_ENABLE, 0);
487 static enum power_supply_property axp20x_battery_props[] = {
488 POWER_SUPPLY_PROP_PRESENT,
489 POWER_SUPPLY_PROP_ONLINE,
490 POWER_SUPPLY_PROP_STATUS,
491 POWER_SUPPLY_PROP_VOLTAGE_NOW,
492 POWER_SUPPLY_PROP_CURRENT_NOW,
493 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
494 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
495 POWER_SUPPLY_PROP_HEALTH,
496 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
497 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
498 POWER_SUPPLY_PROP_CAPACITY,
501 static int axp20x_battery_prop_writeable(struct power_supply *psy,
502 enum power_supply_property psp)
504 return psp == POWER_SUPPLY_PROP_STATUS ||
505 psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
506 psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
507 psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT ||
508 psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
511 static const struct power_supply_desc axp20x_batt_ps_desc = {
512 .name = "axp20x-battery",
513 .type = POWER_SUPPLY_TYPE_BATTERY,
514 .properties = axp20x_battery_props,
515 .num_properties = ARRAY_SIZE(axp20x_battery_props),
516 .property_is_writeable = axp20x_battery_prop_writeable,
517 .get_property = axp20x_battery_get_prop,
518 .set_property = axp20x_battery_set_prop,
521 static const struct axp_data axp209_data = {
523 .ccc_offset = 300000,
524 .get_max_voltage = axp20x_battery_get_max_voltage,
525 .set_max_voltage = axp20x_battery_set_max_voltage,
528 static const struct axp_data axp221_data = {
530 .ccc_offset = 300000,
531 .has_fg_valid = true,
532 .get_max_voltage = axp22x_battery_get_max_voltage,
533 .set_max_voltage = axp22x_battery_set_max_voltage,
536 static const struct axp_data axp813_data = {
538 .ccc_offset = 200000,
539 .has_fg_valid = true,
540 .get_max_voltage = axp813_battery_get_max_voltage,
541 .set_max_voltage = axp20x_battery_set_max_voltage,
544 static const struct of_device_id axp20x_battery_ps_id[] = {
546 .compatible = "x-powers,axp209-battery-power-supply",
547 .data = (void *)&axp209_data,
549 .compatible = "x-powers,axp221-battery-power-supply",
550 .data = (void *)&axp221_data,
552 .compatible = "x-powers,axp813-battery-power-supply",
553 .data = (void *)&axp813_data,
554 }, { /* sentinel */ },
556 MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
558 static int axp20x_power_probe(struct platform_device *pdev)
560 struct axp20x_batt_ps *axp20x_batt;
561 struct power_supply_config psy_cfg = {};
562 struct power_supply_battery_info *info;
563 struct device *dev = &pdev->dev;
565 if (!of_device_is_available(pdev->dev.of_node))
568 axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
573 axp20x_batt->dev = &pdev->dev;
575 axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
576 if (IS_ERR(axp20x_batt->batt_v)) {
577 if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
578 return -EPROBE_DEFER;
579 return PTR_ERR(axp20x_batt->batt_v);
582 axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
584 if (IS_ERR(axp20x_batt->batt_chrg_i)) {
585 if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
586 return -EPROBE_DEFER;
587 return PTR_ERR(axp20x_batt->batt_chrg_i);
590 axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
592 if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
593 if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
594 return -EPROBE_DEFER;
595 return PTR_ERR(axp20x_batt->batt_dischrg_i);
598 axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
599 platform_set_drvdata(pdev, axp20x_batt);
601 psy_cfg.drv_data = axp20x_batt;
602 psy_cfg.of_node = pdev->dev.of_node;
604 axp20x_batt->data = (struct axp_data *)of_device_get_match_data(dev);
606 axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
607 &axp20x_batt_ps_desc,
609 if (IS_ERR(axp20x_batt->batt)) {
610 dev_err(&pdev->dev, "failed to register power supply: %ld\n",
611 PTR_ERR(axp20x_batt->batt));
612 return PTR_ERR(axp20x_batt->batt);
615 if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
616 int vmin = info->voltage_min_design_uv;
617 int ccc = info->constant_charge_current_max_ua;
619 if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
622 "couldn't set voltage_min_design\n");
624 /* Set max to unverified value to be able to set CCC */
625 axp20x_batt->max_ccc = ccc;
627 if (ccc <= 0 || axp20x_set_constant_charge_current(axp20x_batt,
630 "couldn't set constant charge current from DT: fallback to minimum value\n");
632 axp20x_batt->max_ccc = ccc;
633 axp20x_set_constant_charge_current(axp20x_batt, ccc);
638 * Update max CCC to a valid value if battery info is present or set it
639 * to current register value by default.
641 axp20x_get_constant_charge_current(axp20x_batt,
642 &axp20x_batt->max_ccc);
647 static struct platform_driver axp20x_batt_driver = {
648 .probe = axp20x_power_probe,
650 .name = "axp20x-battery-power-supply",
651 .of_match_table = axp20x_battery_ps_id,
655 module_platform_driver(axp20x_batt_driver);
657 MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
658 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
659 MODULE_LICENSE("GPL");