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 struct iio_channel
*chan
;
164 int ret
= 0, reg
, val1
;
167 case POWER_SUPPLY_PROP_PRESENT
:
168 case POWER_SUPPLY_PROP_ONLINE
:
169 ret
= regmap_read(axp20x_batt
->regmap
, AXP20X_PWR_OP_MODE
,
174 val
->intval
= !!(reg
& AXP20X_PWR_OP_BATT_PRESENT
);
177 case POWER_SUPPLY_PROP_STATUS
:
178 ret
= regmap_read(axp20x_batt
->regmap
, AXP20X_PWR_INPUT_STATUS
,
183 if (reg
& AXP20X_PWR_STATUS_BAT_CHARGING
) {
184 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
188 ret
= iio_read_channel_processed(axp20x_batt
->batt_dischrg_i
,
194 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
198 ret
= regmap_read(axp20x_batt
->regmap
, AXP20X_FG_RES
, &val1
);
203 * Fuel Gauge data takes 7 bits but the stored value seems to be
204 * directly the raw percentage without any scaling to 7 bits.
206 if ((val1
& AXP209_FG_PERCENT
) == 100)
207 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
209 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
212 case POWER_SUPPLY_PROP_HEALTH
:
213 ret
= regmap_read(axp20x_batt
->regmap
, AXP20X_PWR_OP_MODE
,
218 if (val1
& AXP20X_PWR_OP_BATT_ACTIVATED
) {
219 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
223 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
226 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
227 ret
= axp20x_get_constant_charge_current(axp20x_batt
,
233 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
234 val
->intval
= axp20x_batt
->max_ccc
;
237 case POWER_SUPPLY_PROP_CURRENT_NOW
:
238 ret
= regmap_read(axp20x_batt
->regmap
, AXP20X_PWR_INPUT_STATUS
,
243 if (reg
& AXP20X_PWR_STATUS_BAT_CHARGING
)
244 chan
= axp20x_batt
->batt_chrg_i
;
246 chan
= axp20x_batt
->batt_dischrg_i
;
248 ret
= iio_read_channel_processed(chan
, &val
->intval
);
252 /* IIO framework gives mA but Power Supply framework gives uA */
256 case POWER_SUPPLY_PROP_CAPACITY
:
257 /* When no battery is present, return capacity is 100% */
258 ret
= regmap_read(axp20x_batt
->regmap
, AXP20X_PWR_OP_MODE
,
263 if (!(reg
& AXP20X_PWR_OP_BATT_PRESENT
)) {
268 ret
= regmap_read(axp20x_batt
->regmap
, AXP20X_FG_RES
, ®
);
272 if (axp20x_batt
->axp_id
== AXP221_ID
&&
273 !(reg
& AXP22X_FG_VALID
))
277 * Fuel Gauge data takes 7 bits but the stored value seems to be
278 * directly the raw percentage without any scaling to 7 bits.
280 val
->intval
= reg
& AXP209_FG_PERCENT
;
283 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
284 if (axp20x_batt
->axp_id
== AXP209_ID
)
285 return axp20x_battery_get_max_voltage(axp20x_batt
,
287 return axp22x_battery_get_max_voltage(axp20x_batt
,
290 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
291 ret
= regmap_read(axp20x_batt
->regmap
, AXP20X_V_OFF
, ®
);
295 val
->intval
= 2600000 + 100000 * (reg
& AXP20X_V_OFF_MASK
);
298 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
299 ret
= iio_read_channel_processed(axp20x_batt
->batt_v
,
304 /* IIO framework gives mV but Power Supply framework gives uV */
315 static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps
*axp20x_batt
,
320 val
= AXP20X_CHRG_CTRL1_TGT_4_1V
;
324 if (axp20x_batt
->axp_id
== AXP221_ID
)
327 val
= AXP20X_CHRG_CTRL1_TGT_4_15V
;
331 val
= AXP20X_CHRG_CTRL1_TGT_4_2V
;
336 * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
337 * can be set to 4.22V and 4.24V, but these voltages are too
338 * high for Lithium based batteries (AXP PMICs are supposed to
339 * be used with these kinds of battery).
344 return regmap_update_bits(axp20x_batt
->regmap
, AXP20X_CHRG_CTRL1
,
345 AXP20X_CHRG_CTRL1_TGT_VOLT
, val
);
348 static int axp20x_set_constant_charge_current(struct axp20x_batt_ps
*axp_batt
,
351 if (charge_current
> axp_batt
->max_ccc
)
354 constant_charge_current_to_raw(axp_batt
, &charge_current
);
356 if (charge_current
> AXP20X_CHRG_CTRL1_TGT_CURR
|| charge_current
< 0)
359 return regmap_update_bits(axp_batt
->regmap
, AXP20X_CHRG_CTRL1
,
360 AXP20X_CHRG_CTRL1_TGT_CURR
, charge_current
);
363 static int axp20x_set_max_constant_charge_current(struct axp20x_batt_ps
*axp
,
366 bool lower_max
= false;
368 constant_charge_current_to_raw(axp
, &charge_current
);
370 if (charge_current
> AXP20X_CHRG_CTRL1_TGT_CURR
|| charge_current
< 0)
373 raw_to_constant_charge_current(axp
, &charge_current
);
375 if (charge_current
> axp
->max_ccc
)
377 "Setting max constant charge current higher than previously defined. Note that increasing the constant charge current may damage your battery.\n");
381 axp
->max_ccc
= charge_current
;
386 axp20x_get_constant_charge_current(axp
, ¤t_cc
);
387 if (current_cc
> charge_current
)
388 axp20x_set_constant_charge_current(axp
, charge_current
);
393 static int axp20x_set_voltage_min_design(struct axp20x_batt_ps
*axp_batt
,
396 int val1
= (min_voltage
- 2600000) / 100000;
398 if (val1
< 0 || val1
> AXP20X_V_OFF_MASK
)
401 return regmap_update_bits(axp_batt
->regmap
, AXP20X_V_OFF
,
402 AXP20X_V_OFF_MASK
, val1
);
405 static int axp20x_battery_set_prop(struct power_supply
*psy
,
406 enum power_supply_property psp
,
407 const union power_supply_propval
*val
)
409 struct axp20x_batt_ps
*axp20x_batt
= power_supply_get_drvdata(psy
);
412 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
413 return axp20x_set_voltage_min_design(axp20x_batt
, val
->intval
);
415 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
416 return axp20x_battery_set_max_voltage(axp20x_batt
, val
->intval
);
418 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
419 return axp20x_set_constant_charge_current(axp20x_batt
,
421 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
422 return axp20x_set_max_constant_charge_current(axp20x_batt
,
430 static enum power_supply_property axp20x_battery_props
[] = {
431 POWER_SUPPLY_PROP_PRESENT
,
432 POWER_SUPPLY_PROP_ONLINE
,
433 POWER_SUPPLY_PROP_STATUS
,
434 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
435 POWER_SUPPLY_PROP_CURRENT_NOW
,
436 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
437 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
438 POWER_SUPPLY_PROP_HEALTH
,
439 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
440 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
441 POWER_SUPPLY_PROP_CAPACITY
,
444 static int axp20x_battery_prop_writeable(struct power_supply
*psy
,
445 enum power_supply_property psp
)
447 return psp
== POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
||
448 psp
== POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
||
449 psp
== POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
||
450 psp
== POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
;
453 static const struct power_supply_desc axp20x_batt_ps_desc
= {
454 .name
= "axp20x-battery",
455 .type
= POWER_SUPPLY_TYPE_BATTERY
,
456 .properties
= axp20x_battery_props
,
457 .num_properties
= ARRAY_SIZE(axp20x_battery_props
),
458 .property_is_writeable
= axp20x_battery_prop_writeable
,
459 .get_property
= axp20x_battery_get_prop
,
460 .set_property
= axp20x_battery_set_prop
,
463 static const struct of_device_id axp20x_battery_ps_id
[] = {
465 .compatible
= "x-powers,axp209-battery-power-supply",
466 .data
= (void *)AXP209_ID
,
468 .compatible
= "x-powers,axp221-battery-power-supply",
469 .data
= (void *)AXP221_ID
,
470 }, { /* sentinel */ },
472 MODULE_DEVICE_TABLE(of
, axp20x_battery_ps_id
);
474 static int axp20x_power_probe(struct platform_device
*pdev
)
476 struct axp20x_batt_ps
*axp20x_batt
;
477 struct power_supply_config psy_cfg
= {};
478 struct power_supply_battery_info info
;
480 if (!of_device_is_available(pdev
->dev
.of_node
))
483 axp20x_batt
= devm_kzalloc(&pdev
->dev
, sizeof(*axp20x_batt
),
488 axp20x_batt
->dev
= &pdev
->dev
;
490 axp20x_batt
->batt_v
= devm_iio_channel_get(&pdev
->dev
, "batt_v");
491 if (IS_ERR(axp20x_batt
->batt_v
)) {
492 if (PTR_ERR(axp20x_batt
->batt_v
) == -ENODEV
)
493 return -EPROBE_DEFER
;
494 return PTR_ERR(axp20x_batt
->batt_v
);
497 axp20x_batt
->batt_chrg_i
= devm_iio_channel_get(&pdev
->dev
,
499 if (IS_ERR(axp20x_batt
->batt_chrg_i
)) {
500 if (PTR_ERR(axp20x_batt
->batt_chrg_i
) == -ENODEV
)
501 return -EPROBE_DEFER
;
502 return PTR_ERR(axp20x_batt
->batt_chrg_i
);
505 axp20x_batt
->batt_dischrg_i
= devm_iio_channel_get(&pdev
->dev
,
507 if (IS_ERR(axp20x_batt
->batt_dischrg_i
)) {
508 if (PTR_ERR(axp20x_batt
->batt_dischrg_i
) == -ENODEV
)
509 return -EPROBE_DEFER
;
510 return PTR_ERR(axp20x_batt
->batt_dischrg_i
);
513 axp20x_batt
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
514 platform_set_drvdata(pdev
, axp20x_batt
);
516 psy_cfg
.drv_data
= axp20x_batt
;
517 psy_cfg
.of_node
= pdev
->dev
.of_node
;
519 axp20x_batt
->axp_id
= (uintptr_t)of_device_get_match_data(&pdev
->dev
);
521 axp20x_batt
->batt
= devm_power_supply_register(&pdev
->dev
,
522 &axp20x_batt_ps_desc
,
524 if (IS_ERR(axp20x_batt
->batt
)) {
525 dev_err(&pdev
->dev
, "failed to register power supply: %ld\n",
526 PTR_ERR(axp20x_batt
->batt
));
527 return PTR_ERR(axp20x_batt
->batt
);
530 if (!power_supply_get_battery_info(axp20x_batt
->batt
, &info
)) {
531 int vmin
= info
.voltage_min_design_uv
;
532 int ccc
= info
.constant_charge_current_max_ua
;
534 if (vmin
> 0 && axp20x_set_voltage_min_design(axp20x_batt
,
537 "couldn't set voltage_min_design\n");
539 /* Set max to unverified value to be able to set CCC */
540 axp20x_batt
->max_ccc
= ccc
;
542 if (ccc
<= 0 || axp20x_set_constant_charge_current(axp20x_batt
,
545 "couldn't set constant charge current from DT: fallback to minimum value\n");
547 axp20x_batt
->max_ccc
= ccc
;
548 axp20x_set_constant_charge_current(axp20x_batt
, ccc
);
553 * Update max CCC to a valid value if battery info is present or set it
554 * to current register value by default.
556 axp20x_get_constant_charge_current(axp20x_batt
,
557 &axp20x_batt
->max_ccc
);
562 static struct platform_driver axp20x_batt_driver
= {
563 .probe
= axp20x_power_probe
,
565 .name
= "axp20x-battery-power-supply",
566 .of_match_table
= axp20x_battery_ps_id
,
570 module_platform_driver(axp20x_batt_driver
);
572 MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
573 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
574 MODULE_LICENSE("GPL");