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/of_device.h>
26 #include <linux/platform_device.h>
27 #include <linux/power_supply.h>
28 #include <linux/regmap.h>
29 #include <linux/slab.h>
30 #include <linux/time.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/consumer.h>
33 #include <linux/mfd/axp20x.h>
35 #define AXP20X_PWR_STATUS_BAT_CHARGING BIT(2)
37 #define AXP20X_PWR_OP_BATT_PRESENT BIT(5)
38 #define AXP20X_PWR_OP_BATT_ACTIVATED BIT(3)
40 #define AXP209_FG_PERCENT GENMASK(6, 0)
41 #define AXP22X_FG_VALID 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 AXP20X_CHRG_CTRL1_TGT_CURR GENMASK(3, 0)
54 #define AXP20X_V_OFF_MASK GENMASK(2, 0)
56 struct axp20x_batt_ps {
57 struct regmap *regmap;
58 struct power_supply *batt;
60 struct iio_channel *batt_chrg_i;
61 struct iio_channel *batt_dischrg_i;
62 struct iio_channel *batt_v;
63 /* Maximum constant charge current */
68 static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
73 ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
77 switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
78 case AXP20X_CHRG_CTRL1_TGT_4_1V:
81 case AXP20X_CHRG_CTRL1_TGT_4_15V:
84 case AXP20X_CHRG_CTRL1_TGT_4_2V:
87 case AXP20X_CHRG_CTRL1_TGT_4_36V:
97 static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
102 ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
106 switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
107 case AXP20X_CHRG_CTRL1_TGT_4_1V:
110 case AXP20X_CHRG_CTRL1_TGT_4_2V:
113 case AXP22X_CHRG_CTRL1_TGT_4_22V:
116 case AXP22X_CHRG_CTRL1_TGT_4_24V:
126 static void raw_to_constant_charge_current(struct axp20x_batt_ps *axp, int *val)
128 if (axp->axp_id == AXP209_ID)
129 *val = *val * 100000 + 300000;
131 *val = *val * 150000 + 300000;
134 static void constant_charge_current_to_raw(struct axp20x_batt_ps *axp, int *val)
136 if (axp->axp_id == AXP209_ID)
137 *val = (*val - 300000) / 100000;
139 *val = (*val - 300000) / 150000;
142 static int axp20x_get_constant_charge_current(struct axp20x_batt_ps *axp,
147 ret = regmap_read(axp->regmap, AXP20X_CHRG_CTRL1, val);
151 *val &= AXP20X_CHRG_CTRL1_TGT_CURR;
153 raw_to_constant_charge_current(axp, val);
158 static int axp20x_battery_get_prop(struct power_supply *psy,
159 enum power_supply_property psp,
160 union power_supply_propval *val)
162 struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
163 int ret = 0, reg, val1;
166 case POWER_SUPPLY_PROP_PRESENT:
167 case POWER_SUPPLY_PROP_ONLINE:
168 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
173 val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
176 case POWER_SUPPLY_PROP_STATUS:
177 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
182 if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
183 val->intval = POWER_SUPPLY_STATUS_CHARGING;
187 ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
193 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
197 ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
202 * Fuel Gauge data takes 7 bits but the stored value seems to be
203 * directly the raw percentage without any scaling to 7 bits.
205 if ((val1 & AXP209_FG_PERCENT) == 100)
206 val->intval = POWER_SUPPLY_STATUS_FULL;
208 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
211 case POWER_SUPPLY_PROP_HEALTH:
212 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
217 if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
218 val->intval = POWER_SUPPLY_HEALTH_DEAD;
222 val->intval = POWER_SUPPLY_HEALTH_GOOD;
225 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
226 ret = axp20x_get_constant_charge_current(axp20x_batt,
232 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
233 val->intval = axp20x_batt->max_ccc;
236 case POWER_SUPPLY_PROP_CURRENT_NOW:
237 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
242 if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
243 ret = iio_read_channel_processed(axp20x_batt->batt_chrg_i, &val->intval);
245 ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i, &val1);
251 /* IIO framework gives mA but Power Supply framework gives uA */
255 case POWER_SUPPLY_PROP_CAPACITY:
256 /* When no battery is present, return capacity is 100% */
257 ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
262 if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
267 ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, ®);
271 if (axp20x_batt->axp_id == AXP221_ID &&
272 !(reg & AXP22X_FG_VALID))
276 * Fuel Gauge data takes 7 bits but the stored value seems to be
277 * directly the raw percentage without any scaling to 7 bits.
279 val->intval = reg & AXP209_FG_PERCENT;
282 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
283 if (axp20x_batt->axp_id == AXP209_ID)
284 return axp20x_battery_get_max_voltage(axp20x_batt,
286 return axp22x_battery_get_max_voltage(axp20x_batt,
289 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
290 ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, ®);
294 val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
297 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
298 ret = iio_read_channel_processed(axp20x_batt->batt_v,
303 /* IIO framework gives mV but Power Supply framework gives uV */
314 static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
319 val = AXP20X_CHRG_CTRL1_TGT_4_1V;
323 if (axp20x_batt->axp_id == AXP221_ID)
326 val = AXP20X_CHRG_CTRL1_TGT_4_15V;
330 val = AXP20X_CHRG_CTRL1_TGT_4_2V;
335 * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
336 * can be set to 4.22V and 4.24V, but these voltages are too
337 * high for Lithium based batteries (AXP PMICs are supposed to
338 * be used with these kinds of battery).
343 return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
344 AXP20X_CHRG_CTRL1_TGT_VOLT, val);
347 static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
350 if (charge_current > axp_batt->max_ccc)
353 constant_charge_current_to_raw(axp_batt, &charge_current);
355 if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
358 return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
359 AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
362 static int axp20x_set_max_constant_charge_current(struct axp20x_batt_ps *axp,
365 bool lower_max = false;
367 constant_charge_current_to_raw(axp, &charge_current);
369 if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
372 raw_to_constant_charge_current(axp, &charge_current);
374 if (charge_current > axp->max_ccc)
376 "Setting max constant charge current higher than previously defined. Note that increasing the constant charge current may damage your battery.\n");
380 axp->max_ccc = charge_current;
385 axp20x_get_constant_charge_current(axp, ¤t_cc);
386 if (current_cc > charge_current)
387 axp20x_set_constant_charge_current(axp, charge_current);
392 static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
395 int val1 = (min_voltage - 2600000) / 100000;
397 if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
400 return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
401 AXP20X_V_OFF_MASK, val1);
404 static int axp20x_battery_set_prop(struct power_supply *psy,
405 enum power_supply_property psp,
406 const union power_supply_propval *val)
408 struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
411 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
412 return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
414 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
415 return axp20x_battery_set_max_voltage(axp20x_batt, val->intval);
417 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
418 return axp20x_set_constant_charge_current(axp20x_batt,
420 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
421 return axp20x_set_max_constant_charge_current(axp20x_batt,
429 static enum power_supply_property axp20x_battery_props[] = {
430 POWER_SUPPLY_PROP_PRESENT,
431 POWER_SUPPLY_PROP_ONLINE,
432 POWER_SUPPLY_PROP_STATUS,
433 POWER_SUPPLY_PROP_VOLTAGE_NOW,
434 POWER_SUPPLY_PROP_CURRENT_NOW,
435 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
436 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
437 POWER_SUPPLY_PROP_HEALTH,
438 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
439 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
440 POWER_SUPPLY_PROP_CAPACITY,
443 static int axp20x_battery_prop_writeable(struct power_supply *psy,
444 enum power_supply_property psp)
446 return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
447 psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
448 psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT ||
449 psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
452 static const struct power_supply_desc axp20x_batt_ps_desc = {
453 .name = "axp20x-battery",
454 .type = POWER_SUPPLY_TYPE_BATTERY,
455 .properties = axp20x_battery_props,
456 .num_properties = ARRAY_SIZE(axp20x_battery_props),
457 .property_is_writeable = axp20x_battery_prop_writeable,
458 .get_property = axp20x_battery_get_prop,
459 .set_property = axp20x_battery_set_prop,
462 static const struct of_device_id axp20x_battery_ps_id[] = {
464 .compatible = "x-powers,axp209-battery-power-supply",
465 .data = (void *)AXP209_ID,
467 .compatible = "x-powers,axp221-battery-power-supply",
468 .data = (void *)AXP221_ID,
469 }, { /* sentinel */ },
471 MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
473 static int axp20x_power_probe(struct platform_device *pdev)
475 struct axp20x_batt_ps *axp20x_batt;
476 struct power_supply_config psy_cfg = {};
477 struct power_supply_battery_info info;
479 if (!of_device_is_available(pdev->dev.of_node))
482 axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
487 axp20x_batt->dev = &pdev->dev;
489 axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
490 if (IS_ERR(axp20x_batt->batt_v)) {
491 if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
492 return -EPROBE_DEFER;
493 return PTR_ERR(axp20x_batt->batt_v);
496 axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
498 if (IS_ERR(axp20x_batt->batt_chrg_i)) {
499 if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
500 return -EPROBE_DEFER;
501 return PTR_ERR(axp20x_batt->batt_chrg_i);
504 axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
506 if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
507 if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
508 return -EPROBE_DEFER;
509 return PTR_ERR(axp20x_batt->batt_dischrg_i);
512 axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
513 platform_set_drvdata(pdev, axp20x_batt);
515 psy_cfg.drv_data = axp20x_batt;
516 psy_cfg.of_node = pdev->dev.of_node;
518 axp20x_batt->axp_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
520 axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
521 &axp20x_batt_ps_desc,
523 if (IS_ERR(axp20x_batt->batt)) {
524 dev_err(&pdev->dev, "failed to register power supply: %ld\n",
525 PTR_ERR(axp20x_batt->batt));
526 return PTR_ERR(axp20x_batt->batt);
529 if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
530 int vmin = info.voltage_min_design_uv;
531 int ccc = info.constant_charge_current_max_ua;
533 if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
536 "couldn't set voltage_min_design\n");
538 /* Set max to unverified value to be able to set CCC */
539 axp20x_batt->max_ccc = ccc;
541 if (ccc <= 0 || axp20x_set_constant_charge_current(axp20x_batt,
544 "couldn't set constant charge current from DT: fallback to minimum value\n");
546 axp20x_batt->max_ccc = ccc;
547 axp20x_set_constant_charge_current(axp20x_batt, ccc);
552 * Update max CCC to a valid value if battery info is present or set it
553 * to current register value by default.
555 axp20x_get_constant_charge_current(axp20x_batt,
556 &axp20x_batt->max_ccc);
561 static struct platform_driver axp20x_batt_driver = {
562 .probe = axp20x_power_probe,
564 .name = "axp20x-battery-power-supply",
565 .of_match_table = axp20x_battery_ps_id,
569 module_platform_driver(axp20x_batt_driver);
571 MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
572 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
573 MODULE_LICENSE("GPL");