146c202b5SQuentin Schulz /*
246c202b5SQuentin Schulz * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
346c202b5SQuentin Schulz *
446c202b5SQuentin Schulz * Copyright 2016 Free Electrons NextThing Co.
546c202b5SQuentin Schulz * Quentin Schulz <quentin.schulz@free-electrons.com>
646c202b5SQuentin Schulz *
746c202b5SQuentin Schulz * This driver is based on a previous upstreaming attempt by:
846c202b5SQuentin Schulz * Bruno Prémont <bonbons@linux-vserver.org>
946c202b5SQuentin Schulz *
1046c202b5SQuentin Schulz * This file is subject to the terms and conditions of the GNU General
1146c202b5SQuentin Schulz * Public License. See the file "COPYING" in the main directory of this
1246c202b5SQuentin Schulz * archive for more details.
1346c202b5SQuentin Schulz *
1446c202b5SQuentin Schulz * This program is distributed in the hope that it will be useful,
1546c202b5SQuentin Schulz * but WITHOUT ANY WARRANTY; without even the implied warranty of
1646c202b5SQuentin Schulz * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1746c202b5SQuentin Schulz * GNU General Public License for more details.
1846c202b5SQuentin Schulz */
1946c202b5SQuentin Schulz
2046c202b5SQuentin Schulz #include <linux/err.h>
2146c202b5SQuentin Schulz #include <linux/interrupt.h>
2246c202b5SQuentin Schulz #include <linux/irq.h>
2346c202b5SQuentin Schulz #include <linux/module.h>
2446c202b5SQuentin Schulz #include <linux/of.h>
2546c202b5SQuentin Schulz #include <linux/platform_device.h>
2646c202b5SQuentin Schulz #include <linux/power_supply.h>
2746c202b5SQuentin Schulz #include <linux/regmap.h>
2846c202b5SQuentin Schulz #include <linux/slab.h>
2946c202b5SQuentin Schulz #include <linux/time.h>
3046c202b5SQuentin Schulz #include <linux/iio/iio.h>
3146c202b5SQuentin Schulz #include <linux/iio/consumer.h>
3246c202b5SQuentin Schulz #include <linux/mfd/axp20x.h>
3346c202b5SQuentin Schulz
3446c202b5SQuentin Schulz #define AXP20X_PWR_STATUS_BAT_CHARGING BIT(2)
3546c202b5SQuentin Schulz
3646c202b5SQuentin Schulz #define AXP20X_PWR_OP_BATT_PRESENT BIT(5)
3746c202b5SQuentin Schulz #define AXP20X_PWR_OP_BATT_ACTIVATED BIT(3)
3846c202b5SQuentin Schulz
3946c202b5SQuentin Schulz #define AXP209_FG_PERCENT GENMASK(6, 0)
4046c202b5SQuentin Schulz #define AXP22X_FG_VALID BIT(7)
4146c202b5SQuentin Schulz
426a0fcc87SHermann Lauer #define AXP20X_CHRG_CTRL1_ENABLE BIT(7)
4346c202b5SQuentin Schulz #define AXP20X_CHRG_CTRL1_TGT_VOLT GENMASK(6, 5)
4446c202b5SQuentin Schulz #define AXP20X_CHRG_CTRL1_TGT_4_1V (0 << 5)
4546c202b5SQuentin Schulz #define AXP20X_CHRG_CTRL1_TGT_4_15V (1 << 5)
4646c202b5SQuentin Schulz #define AXP20X_CHRG_CTRL1_TGT_4_2V (2 << 5)
4746c202b5SQuentin Schulz #define AXP20X_CHRG_CTRL1_TGT_4_36V (3 << 5)
4846c202b5SQuentin Schulz
4946c202b5SQuentin Schulz #define AXP22X_CHRG_CTRL1_TGT_4_22V (1 << 5)
5046c202b5SQuentin Schulz #define AXP22X_CHRG_CTRL1_TGT_4_24V (3 << 5)
5146c202b5SQuentin Schulz
526ff653e3SQuentin Schulz #define AXP813_CHRG_CTRL1_TGT_4_35V (3 << 5)
536ff653e3SQuentin Schulz
5446c202b5SQuentin Schulz #define AXP20X_CHRG_CTRL1_TGT_CURR GENMASK(3, 0)
5546c202b5SQuentin Schulz
5646c202b5SQuentin Schulz #define AXP20X_V_OFF_MASK GENMASK(2, 0)
5746c202b5SQuentin Schulz
58648badd7SQuentin Schulz struct axp20x_batt_ps;
59648badd7SQuentin Schulz
60648badd7SQuentin Schulz struct axp_data {
61648badd7SQuentin Schulz int ccc_scale;
62648badd7SQuentin Schulz int ccc_offset;
63648badd7SQuentin Schulz bool has_fg_valid;
64648badd7SQuentin Schulz int (*get_max_voltage)(struct axp20x_batt_ps *batt, int *val);
65648badd7SQuentin Schulz int (*set_max_voltage)(struct axp20x_batt_ps *batt, int val);
66648badd7SQuentin Schulz };
67648badd7SQuentin Schulz
6846c202b5SQuentin Schulz struct axp20x_batt_ps {
6946c202b5SQuentin Schulz struct regmap *regmap;
7046c202b5SQuentin Schulz struct power_supply *batt;
7146c202b5SQuentin Schulz struct device *dev;
7246c202b5SQuentin Schulz struct iio_channel *batt_chrg_i;
7346c202b5SQuentin Schulz struct iio_channel *batt_dischrg_i;
7446c202b5SQuentin Schulz struct iio_channel *batt_v;
75c8003844SQuentin Schulz /* Maximum constant charge current */
76c8003844SQuentin Schulz unsigned int max_ccc;
77648badd7SQuentin Schulz const struct axp_data *data;
7846c202b5SQuentin Schulz };
7946c202b5SQuentin Schulz
axp20x_battery_get_max_voltage(struct axp20x_batt_ps * axp20x_batt,int * val)8046c202b5SQuentin Schulz static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
8146c202b5SQuentin Schulz int *val)
8246c202b5SQuentin Schulz {
8346c202b5SQuentin Schulz int ret, reg;
8446c202b5SQuentin Schulz
8546c202b5SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
8646c202b5SQuentin Schulz if (ret)
8746c202b5SQuentin Schulz return ret;
8846c202b5SQuentin Schulz
8946c202b5SQuentin Schulz switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
9046c202b5SQuentin Schulz case AXP20X_CHRG_CTRL1_TGT_4_1V:
9146c202b5SQuentin Schulz *val = 4100000;
9246c202b5SQuentin Schulz break;
9346c202b5SQuentin Schulz case AXP20X_CHRG_CTRL1_TGT_4_15V:
9446c202b5SQuentin Schulz *val = 4150000;
9546c202b5SQuentin Schulz break;
9646c202b5SQuentin Schulz case AXP20X_CHRG_CTRL1_TGT_4_2V:
9746c202b5SQuentin Schulz *val = 4200000;
9846c202b5SQuentin Schulz break;
9946c202b5SQuentin Schulz case AXP20X_CHRG_CTRL1_TGT_4_36V:
10046c202b5SQuentin Schulz *val = 4360000;
10146c202b5SQuentin Schulz break;
10246c202b5SQuentin Schulz default:
10346c202b5SQuentin Schulz return -EINVAL;
10446c202b5SQuentin Schulz }
10546c202b5SQuentin Schulz
10646c202b5SQuentin Schulz return 0;
10746c202b5SQuentin Schulz }
10846c202b5SQuentin Schulz
axp22x_battery_get_max_voltage(struct axp20x_batt_ps * axp20x_batt,int * val)10946c202b5SQuentin Schulz static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
11046c202b5SQuentin Schulz int *val)
11146c202b5SQuentin Schulz {
11246c202b5SQuentin Schulz int ret, reg;
11346c202b5SQuentin Schulz
11446c202b5SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
11546c202b5SQuentin Schulz if (ret)
11646c202b5SQuentin Schulz return ret;
11746c202b5SQuentin Schulz
11846c202b5SQuentin Schulz switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
11946c202b5SQuentin Schulz case AXP20X_CHRG_CTRL1_TGT_4_1V:
12046c202b5SQuentin Schulz *val = 4100000;
12146c202b5SQuentin Schulz break;
12246c202b5SQuentin Schulz case AXP20X_CHRG_CTRL1_TGT_4_2V:
12346c202b5SQuentin Schulz *val = 4200000;
12446c202b5SQuentin Schulz break;
12546c202b5SQuentin Schulz case AXP22X_CHRG_CTRL1_TGT_4_22V:
12646c202b5SQuentin Schulz *val = 4220000;
12746c202b5SQuentin Schulz break;
12846c202b5SQuentin Schulz case AXP22X_CHRG_CTRL1_TGT_4_24V:
12946c202b5SQuentin Schulz *val = 4240000;
13046c202b5SQuentin Schulz break;
13146c202b5SQuentin Schulz default:
13246c202b5SQuentin Schulz return -EINVAL;
13346c202b5SQuentin Schulz }
13446c202b5SQuentin Schulz
13546c202b5SQuentin Schulz return 0;
13646c202b5SQuentin Schulz }
13746c202b5SQuentin Schulz
axp813_battery_get_max_voltage(struct axp20x_batt_ps * axp20x_batt,int * val)1386ff653e3SQuentin Schulz static int axp813_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
1396ff653e3SQuentin Schulz int *val)
1406ff653e3SQuentin Schulz {
1416ff653e3SQuentin Schulz int ret, reg;
1426ff653e3SQuentin Schulz
1436ff653e3SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
1446ff653e3SQuentin Schulz if (ret)
1456ff653e3SQuentin Schulz return ret;
1466ff653e3SQuentin Schulz
1476ff653e3SQuentin Schulz switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
1486ff653e3SQuentin Schulz case AXP20X_CHRG_CTRL1_TGT_4_1V:
1496ff653e3SQuentin Schulz *val = 4100000;
1506ff653e3SQuentin Schulz break;
1516ff653e3SQuentin Schulz case AXP20X_CHRG_CTRL1_TGT_4_15V:
1526ff653e3SQuentin Schulz *val = 4150000;
1536ff653e3SQuentin Schulz break;
1546ff653e3SQuentin Schulz case AXP20X_CHRG_CTRL1_TGT_4_2V:
1556ff653e3SQuentin Schulz *val = 4200000;
1566ff653e3SQuentin Schulz break;
1576ff653e3SQuentin Schulz case AXP813_CHRG_CTRL1_TGT_4_35V:
1586ff653e3SQuentin Schulz *val = 4350000;
1596ff653e3SQuentin Schulz break;
1606ff653e3SQuentin Schulz default:
1616ff653e3SQuentin Schulz return -EINVAL;
1626ff653e3SQuentin Schulz }
1636ff653e3SQuentin Schulz
1646ff653e3SQuentin Schulz return 0;
1656ff653e3SQuentin Schulz }
1666ff653e3SQuentin Schulz
axp20x_get_constant_charge_current(struct axp20x_batt_ps * axp,int * val)16746c202b5SQuentin Schulz static int axp20x_get_constant_charge_current(struct axp20x_batt_ps *axp,
16846c202b5SQuentin Schulz int *val)
16946c202b5SQuentin Schulz {
17046c202b5SQuentin Schulz int ret;
17146c202b5SQuentin Schulz
17246c202b5SQuentin Schulz ret = regmap_read(axp->regmap, AXP20X_CHRG_CTRL1, val);
17346c202b5SQuentin Schulz if (ret)
17446c202b5SQuentin Schulz return ret;
17546c202b5SQuentin Schulz
17646c202b5SQuentin Schulz *val &= AXP20X_CHRG_CTRL1_TGT_CURR;
17746c202b5SQuentin Schulz
178648badd7SQuentin Schulz *val = *val * axp->data->ccc_scale + axp->data->ccc_offset;
17946c202b5SQuentin Schulz
18046c202b5SQuentin Schulz return 0;
18146c202b5SQuentin Schulz }
18246c202b5SQuentin Schulz
axp20x_battery_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)18346c202b5SQuentin Schulz static int axp20x_battery_get_prop(struct power_supply *psy,
18446c202b5SQuentin Schulz enum power_supply_property psp,
18546c202b5SQuentin Schulz union power_supply_propval *val)
18646c202b5SQuentin Schulz {
18746c202b5SQuentin Schulz struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
18846c202b5SQuentin Schulz int ret = 0, reg, val1;
18946c202b5SQuentin Schulz
19046c202b5SQuentin Schulz switch (psp) {
19146c202b5SQuentin Schulz case POWER_SUPPLY_PROP_PRESENT:
19246c202b5SQuentin Schulz case POWER_SUPPLY_PROP_ONLINE:
19346c202b5SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
19446c202b5SQuentin Schulz ®);
19546c202b5SQuentin Schulz if (ret)
19646c202b5SQuentin Schulz return ret;
19746c202b5SQuentin Schulz
19846c202b5SQuentin Schulz val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
19946c202b5SQuentin Schulz break;
20046c202b5SQuentin Schulz
20146c202b5SQuentin Schulz case POWER_SUPPLY_PROP_STATUS:
20246c202b5SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
20346c202b5SQuentin Schulz ®);
20446c202b5SQuentin Schulz if (ret)
20546c202b5SQuentin Schulz return ret;
20646c202b5SQuentin Schulz
20746c202b5SQuentin Schulz if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
20846c202b5SQuentin Schulz val->intval = POWER_SUPPLY_STATUS_CHARGING;
20946c202b5SQuentin Schulz return 0;
21046c202b5SQuentin Schulz }
21146c202b5SQuentin Schulz
21246c202b5SQuentin Schulz ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
21346c202b5SQuentin Schulz &val1);
21446c202b5SQuentin Schulz if (ret)
21546c202b5SQuentin Schulz return ret;
21646c202b5SQuentin Schulz
21746c202b5SQuentin Schulz if (val1) {
21846c202b5SQuentin Schulz val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
21946c202b5SQuentin Schulz return 0;
22046c202b5SQuentin Schulz }
22146c202b5SQuentin Schulz
22246c202b5SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
22346c202b5SQuentin Schulz if (ret)
22446c202b5SQuentin Schulz return ret;
22546c202b5SQuentin Schulz
22646c202b5SQuentin Schulz /*
22746c202b5SQuentin Schulz * Fuel Gauge data takes 7 bits but the stored value seems to be
22846c202b5SQuentin Schulz * directly the raw percentage without any scaling to 7 bits.
22946c202b5SQuentin Schulz */
23046c202b5SQuentin Schulz if ((val1 & AXP209_FG_PERCENT) == 100)
23146c202b5SQuentin Schulz val->intval = POWER_SUPPLY_STATUS_FULL;
23246c202b5SQuentin Schulz else
23346c202b5SQuentin Schulz val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
23446c202b5SQuentin Schulz break;
23546c202b5SQuentin Schulz
23646c202b5SQuentin Schulz case POWER_SUPPLY_PROP_HEALTH:
23746c202b5SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
23846c202b5SQuentin Schulz &val1);
23946c202b5SQuentin Schulz if (ret)
24046c202b5SQuentin Schulz return ret;
24146c202b5SQuentin Schulz
24246c202b5SQuentin Schulz if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
24346c202b5SQuentin Schulz val->intval = POWER_SUPPLY_HEALTH_DEAD;
24446c202b5SQuentin Schulz return 0;
24546c202b5SQuentin Schulz }
24646c202b5SQuentin Schulz
24746c202b5SQuentin Schulz val->intval = POWER_SUPPLY_HEALTH_GOOD;
24846c202b5SQuentin Schulz break;
24946c202b5SQuentin Schulz
25046c202b5SQuentin Schulz case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
25146c202b5SQuentin Schulz ret = axp20x_get_constant_charge_current(axp20x_batt,
25246c202b5SQuentin Schulz &val->intval);
25346c202b5SQuentin Schulz if (ret)
25446c202b5SQuentin Schulz return ret;
25546c202b5SQuentin Schulz break;
25646c202b5SQuentin Schulz
25746c202b5SQuentin Schulz case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
258c8003844SQuentin Schulz val->intval = axp20x_batt->max_ccc;
25946c202b5SQuentin Schulz break;
26046c202b5SQuentin Schulz
26146c202b5SQuentin Schulz case POWER_SUPPLY_PROP_CURRENT_NOW:
26246c202b5SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
26346c202b5SQuentin Schulz ®);
26446c202b5SQuentin Schulz if (ret)
26546c202b5SQuentin Schulz return ret;
26646c202b5SQuentin Schulz
267d4f408cdSEvgeny Boger if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
268d4f408cdSEvgeny Boger ret = iio_read_channel_processed(axp20x_batt->batt_chrg_i, &val->intval);
269d4f408cdSEvgeny Boger } else {
270d4f408cdSEvgeny Boger ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i, &val1);
271d4f408cdSEvgeny Boger val->intval = -val1;
272d4f408cdSEvgeny Boger }
27346c202b5SQuentin Schulz if (ret)
27446c202b5SQuentin Schulz return ret;
27546c202b5SQuentin Schulz
27646c202b5SQuentin Schulz /* IIO framework gives mA but Power Supply framework gives uA */
27746c202b5SQuentin Schulz val->intval *= 1000;
27846c202b5SQuentin Schulz break;
27946c202b5SQuentin Schulz
28046c202b5SQuentin Schulz case POWER_SUPPLY_PROP_CAPACITY:
28146c202b5SQuentin Schulz /* When no battery is present, return capacity is 100% */
28246c202b5SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
28346c202b5SQuentin Schulz ®);
28446c202b5SQuentin Schulz if (ret)
28546c202b5SQuentin Schulz return ret;
28646c202b5SQuentin Schulz
28746c202b5SQuentin Schulz if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
28846c202b5SQuentin Schulz val->intval = 100;
28946c202b5SQuentin Schulz return 0;
29046c202b5SQuentin Schulz }
29146c202b5SQuentin Schulz
29246c202b5SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, ®);
29346c202b5SQuentin Schulz if (ret)
29446c202b5SQuentin Schulz return ret;
29546c202b5SQuentin Schulz
296648badd7SQuentin Schulz if (axp20x_batt->data->has_fg_valid && !(reg & AXP22X_FG_VALID))
29746c202b5SQuentin Schulz return -EINVAL;
29846c202b5SQuentin Schulz
29946c202b5SQuentin Schulz /*
30046c202b5SQuentin Schulz * Fuel Gauge data takes 7 bits but the stored value seems to be
30146c202b5SQuentin Schulz * directly the raw percentage without any scaling to 7 bits.
30246c202b5SQuentin Schulz */
30346c202b5SQuentin Schulz val->intval = reg & AXP209_FG_PERCENT;
30446c202b5SQuentin Schulz break;
30546c202b5SQuentin Schulz
306*05dba127SChris Morgan case POWER_SUPPLY_PROP_VOLTAGE_MAX:
307648badd7SQuentin Schulz return axp20x_batt->data->get_max_voltage(axp20x_batt,
30846c202b5SQuentin Schulz &val->intval);
30946c202b5SQuentin Schulz
310*05dba127SChris Morgan case POWER_SUPPLY_PROP_VOLTAGE_MIN:
31146c202b5SQuentin Schulz ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, ®);
31246c202b5SQuentin Schulz if (ret)
31346c202b5SQuentin Schulz return ret;
31446c202b5SQuentin Schulz
31546c202b5SQuentin Schulz val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
31646c202b5SQuentin Schulz break;
31746c202b5SQuentin Schulz
31846c202b5SQuentin Schulz case POWER_SUPPLY_PROP_VOLTAGE_NOW:
31946c202b5SQuentin Schulz ret = iio_read_channel_processed(axp20x_batt->batt_v,
32046c202b5SQuentin Schulz &val->intval);
32146c202b5SQuentin Schulz if (ret)
32246c202b5SQuentin Schulz return ret;
32346c202b5SQuentin Schulz
32446c202b5SQuentin Schulz /* IIO framework gives mV but Power Supply framework gives uV */
32546c202b5SQuentin Schulz val->intval *= 1000;
32646c202b5SQuentin Schulz break;
32746c202b5SQuentin Schulz
32846c202b5SQuentin Schulz default:
32946c202b5SQuentin Schulz return -EINVAL;
33046c202b5SQuentin Schulz }
33146c202b5SQuentin Schulz
33246c202b5SQuentin Schulz return 0;
33346c202b5SQuentin Schulz }
33446c202b5SQuentin Schulz
axp22x_battery_set_max_voltage(struct axp20x_batt_ps * axp20x_batt,int val)335648badd7SQuentin Schulz static int axp22x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
336648badd7SQuentin Schulz int val)
337648badd7SQuentin Schulz {
338648badd7SQuentin Schulz switch (val) {
339648badd7SQuentin Schulz case 4100000:
340648badd7SQuentin Schulz val = AXP20X_CHRG_CTRL1_TGT_4_1V;
341648badd7SQuentin Schulz break;
342648badd7SQuentin Schulz
343648badd7SQuentin Schulz case 4200000:
344648badd7SQuentin Schulz val = AXP20X_CHRG_CTRL1_TGT_4_2V;
345648badd7SQuentin Schulz break;
346648badd7SQuentin Schulz
347648badd7SQuentin Schulz default:
348648badd7SQuentin Schulz /*
349648badd7SQuentin Schulz * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
350648badd7SQuentin Schulz * can be set to 4.22V and 4.24V, but these voltages are too
351648badd7SQuentin Schulz * high for Lithium based batteries (AXP PMICs are supposed to
352648badd7SQuentin Schulz * be used with these kinds of battery).
353648badd7SQuentin Schulz */
354648badd7SQuentin Schulz return -EINVAL;
355648badd7SQuentin Schulz }
356648badd7SQuentin Schulz
357648badd7SQuentin Schulz return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
358648badd7SQuentin Schulz AXP20X_CHRG_CTRL1_TGT_VOLT, val);
359648badd7SQuentin Schulz }
360648badd7SQuentin Schulz
axp20x_battery_set_max_voltage(struct axp20x_batt_ps * axp20x_batt,int val)36146c202b5SQuentin Schulz static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
36246c202b5SQuentin Schulz int val)
36346c202b5SQuentin Schulz {
36446c202b5SQuentin Schulz switch (val) {
36546c202b5SQuentin Schulz case 4100000:
36646c202b5SQuentin Schulz val = AXP20X_CHRG_CTRL1_TGT_4_1V;
36746c202b5SQuentin Schulz break;
36846c202b5SQuentin Schulz
36946c202b5SQuentin Schulz case 4150000:
37046c202b5SQuentin Schulz val = AXP20X_CHRG_CTRL1_TGT_4_15V;
37146c202b5SQuentin Schulz break;
37246c202b5SQuentin Schulz
37346c202b5SQuentin Schulz case 4200000:
37446c202b5SQuentin Schulz val = AXP20X_CHRG_CTRL1_TGT_4_2V;
37546c202b5SQuentin Schulz break;
37646c202b5SQuentin Schulz
37746c202b5SQuentin Schulz default:
37846c202b5SQuentin Schulz /*
37946c202b5SQuentin Schulz * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
38046c202b5SQuentin Schulz * can be set to 4.22V and 4.24V, but these voltages are too
38146c202b5SQuentin Schulz * high for Lithium based batteries (AXP PMICs are supposed to
38246c202b5SQuentin Schulz * be used with these kinds of battery).
38346c202b5SQuentin Schulz */
38446c202b5SQuentin Schulz return -EINVAL;
38546c202b5SQuentin Schulz }
38646c202b5SQuentin Schulz
38746c202b5SQuentin Schulz return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
38846c202b5SQuentin Schulz AXP20X_CHRG_CTRL1_TGT_VOLT, val);
38946c202b5SQuentin Schulz }
39046c202b5SQuentin Schulz
axp20x_set_constant_charge_current(struct axp20x_batt_ps * axp_batt,int charge_current)39146c202b5SQuentin Schulz static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
39246c202b5SQuentin Schulz int charge_current)
39346c202b5SQuentin Schulz {
394c8003844SQuentin Schulz if (charge_current > axp_batt->max_ccc)
395c8003844SQuentin Schulz return -EINVAL;
396c8003844SQuentin Schulz
397648badd7SQuentin Schulz charge_current = (charge_current - axp_batt->data->ccc_offset) /
398648badd7SQuentin Schulz axp_batt->data->ccc_scale;
39946c202b5SQuentin Schulz
40046c202b5SQuentin Schulz if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
40146c202b5SQuentin Schulz return -EINVAL;
40246c202b5SQuentin Schulz
40346c202b5SQuentin Schulz return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
40446c202b5SQuentin Schulz AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
40546c202b5SQuentin Schulz }
40646c202b5SQuentin Schulz
axp20x_set_max_constant_charge_current(struct axp20x_batt_ps * axp,int charge_current)407c8003844SQuentin Schulz static int axp20x_set_max_constant_charge_current(struct axp20x_batt_ps *axp,
408c8003844SQuentin Schulz int charge_current)
409c8003844SQuentin Schulz {
410c8003844SQuentin Schulz bool lower_max = false;
411c8003844SQuentin Schulz
412648badd7SQuentin Schulz charge_current = (charge_current - axp->data->ccc_offset) /
413648badd7SQuentin Schulz axp->data->ccc_scale;
414c8003844SQuentin Schulz
415c8003844SQuentin Schulz if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
416c8003844SQuentin Schulz return -EINVAL;
417c8003844SQuentin Schulz
418648badd7SQuentin Schulz charge_current = charge_current * axp->data->ccc_scale +
419648badd7SQuentin Schulz axp->data->ccc_offset;
420c8003844SQuentin Schulz
421c8003844SQuentin Schulz if (charge_current > axp->max_ccc)
422c8003844SQuentin Schulz dev_warn(axp->dev,
423c8003844SQuentin Schulz "Setting max constant charge current higher than previously defined. Note that increasing the constant charge current may damage your battery.\n");
424c8003844SQuentin Schulz else
425c8003844SQuentin Schulz lower_max = true;
426c8003844SQuentin Schulz
427c8003844SQuentin Schulz axp->max_ccc = charge_current;
428c8003844SQuentin Schulz
429c8003844SQuentin Schulz if (lower_max) {
430c8003844SQuentin Schulz int current_cc;
431c8003844SQuentin Schulz
432c8003844SQuentin Schulz axp20x_get_constant_charge_current(axp, ¤t_cc);
433c8003844SQuentin Schulz if (current_cc > charge_current)
434c8003844SQuentin Schulz axp20x_set_constant_charge_current(axp, charge_current);
435c8003844SQuentin Schulz }
436c8003844SQuentin Schulz
437c8003844SQuentin Schulz return 0;
438c8003844SQuentin Schulz }
axp20x_set_voltage_min_design(struct axp20x_batt_ps * axp_batt,int min_voltage)43946c202b5SQuentin Schulz static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
44046c202b5SQuentin Schulz int min_voltage)
44146c202b5SQuentin Schulz {
44246c202b5SQuentin Schulz int val1 = (min_voltage - 2600000) / 100000;
44346c202b5SQuentin Schulz
44446c202b5SQuentin Schulz if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
44546c202b5SQuentin Schulz return -EINVAL;
44646c202b5SQuentin Schulz
44746c202b5SQuentin Schulz return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
44846c202b5SQuentin Schulz AXP20X_V_OFF_MASK, val1);
44946c202b5SQuentin Schulz }
45046c202b5SQuentin Schulz
axp20x_battery_set_prop(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)45146c202b5SQuentin Schulz static int axp20x_battery_set_prop(struct power_supply *psy,
45246c202b5SQuentin Schulz enum power_supply_property psp,
45346c202b5SQuentin Schulz const union power_supply_propval *val)
45446c202b5SQuentin Schulz {
45546c202b5SQuentin Schulz struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
45646c202b5SQuentin Schulz
45746c202b5SQuentin Schulz switch (psp) {
458*05dba127SChris Morgan case POWER_SUPPLY_PROP_VOLTAGE_MIN:
45946c202b5SQuentin Schulz return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
46046c202b5SQuentin Schulz
461*05dba127SChris Morgan case POWER_SUPPLY_PROP_VOLTAGE_MAX:
462648badd7SQuentin Schulz return axp20x_batt->data->set_max_voltage(axp20x_batt, val->intval);
46346c202b5SQuentin Schulz
46446c202b5SQuentin Schulz case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
46546c202b5SQuentin Schulz return axp20x_set_constant_charge_current(axp20x_batt,
46646c202b5SQuentin Schulz val->intval);
467c8003844SQuentin Schulz case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
468c8003844SQuentin Schulz return axp20x_set_max_constant_charge_current(axp20x_batt,
469c8003844SQuentin Schulz val->intval);
4706a0fcc87SHermann Lauer case POWER_SUPPLY_PROP_STATUS:
4716a0fcc87SHermann Lauer switch (val->intval) {
4726a0fcc87SHermann Lauer case POWER_SUPPLY_STATUS_CHARGING:
4736a0fcc87SHermann Lauer return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
4746a0fcc87SHermann Lauer AXP20X_CHRG_CTRL1_ENABLE, AXP20X_CHRG_CTRL1_ENABLE);
47546c202b5SQuentin Schulz
4766a0fcc87SHermann Lauer case POWER_SUPPLY_STATUS_DISCHARGING:
4776a0fcc87SHermann Lauer case POWER_SUPPLY_STATUS_NOT_CHARGING:
4786a0fcc87SHermann Lauer return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
4796a0fcc87SHermann Lauer AXP20X_CHRG_CTRL1_ENABLE, 0);
4806a0fcc87SHermann Lauer }
4816a0fcc87SHermann Lauer fallthrough;
48246c202b5SQuentin Schulz default:
48346c202b5SQuentin Schulz return -EINVAL;
48446c202b5SQuentin Schulz }
48546c202b5SQuentin Schulz }
48646c202b5SQuentin Schulz
48746c202b5SQuentin Schulz static enum power_supply_property axp20x_battery_props[] = {
48846c202b5SQuentin Schulz POWER_SUPPLY_PROP_PRESENT,
48946c202b5SQuentin Schulz POWER_SUPPLY_PROP_ONLINE,
49046c202b5SQuentin Schulz POWER_SUPPLY_PROP_STATUS,
49146c202b5SQuentin Schulz POWER_SUPPLY_PROP_VOLTAGE_NOW,
49246c202b5SQuentin Schulz POWER_SUPPLY_PROP_CURRENT_NOW,
49346c202b5SQuentin Schulz POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
49446c202b5SQuentin Schulz POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
49546c202b5SQuentin Schulz POWER_SUPPLY_PROP_HEALTH,
496*05dba127SChris Morgan POWER_SUPPLY_PROP_VOLTAGE_MAX,
497*05dba127SChris Morgan POWER_SUPPLY_PROP_VOLTAGE_MIN,
49846c202b5SQuentin Schulz POWER_SUPPLY_PROP_CAPACITY,
49946c202b5SQuentin Schulz };
50046c202b5SQuentin Schulz
axp20x_battery_prop_writeable(struct power_supply * psy,enum power_supply_property psp)50146c202b5SQuentin Schulz static int axp20x_battery_prop_writeable(struct power_supply *psy,
50246c202b5SQuentin Schulz enum power_supply_property psp)
50346c202b5SQuentin Schulz {
5046a0fcc87SHermann Lauer return psp == POWER_SUPPLY_PROP_STATUS ||
505*05dba127SChris Morgan psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
506*05dba127SChris Morgan psp == POWER_SUPPLY_PROP_VOLTAGE_MAX ||
507c8003844SQuentin Schulz psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT ||
508c8003844SQuentin Schulz psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
50946c202b5SQuentin Schulz }
51046c202b5SQuentin Schulz
51146c202b5SQuentin Schulz static const struct power_supply_desc axp20x_batt_ps_desc = {
51246c202b5SQuentin Schulz .name = "axp20x-battery",
51346c202b5SQuentin Schulz .type = POWER_SUPPLY_TYPE_BATTERY,
51446c202b5SQuentin Schulz .properties = axp20x_battery_props,
51546c202b5SQuentin Schulz .num_properties = ARRAY_SIZE(axp20x_battery_props),
51646c202b5SQuentin Schulz .property_is_writeable = axp20x_battery_prop_writeable,
51746c202b5SQuentin Schulz .get_property = axp20x_battery_get_prop,
51846c202b5SQuentin Schulz .set_property = axp20x_battery_set_prop,
51946c202b5SQuentin Schulz };
52046c202b5SQuentin Schulz
521648badd7SQuentin Schulz static const struct axp_data axp209_data = {
522648badd7SQuentin Schulz .ccc_scale = 100000,
523648badd7SQuentin Schulz .ccc_offset = 300000,
524648badd7SQuentin Schulz .get_max_voltage = axp20x_battery_get_max_voltage,
525648badd7SQuentin Schulz .set_max_voltage = axp20x_battery_set_max_voltage,
526648badd7SQuentin Schulz };
527648badd7SQuentin Schulz
528648badd7SQuentin Schulz static const struct axp_data axp221_data = {
529648badd7SQuentin Schulz .ccc_scale = 150000,
530648badd7SQuentin Schulz .ccc_offset = 300000,
531648badd7SQuentin Schulz .has_fg_valid = true,
532648badd7SQuentin Schulz .get_max_voltage = axp22x_battery_get_max_voltage,
533648badd7SQuentin Schulz .set_max_voltage = axp22x_battery_set_max_voltage,
534648badd7SQuentin Schulz };
535648badd7SQuentin Schulz
5366ff653e3SQuentin Schulz static const struct axp_data axp813_data = {
5376ff653e3SQuentin Schulz .ccc_scale = 200000,
5386ff653e3SQuentin Schulz .ccc_offset = 200000,
5396ff653e3SQuentin Schulz .has_fg_valid = true,
5406ff653e3SQuentin Schulz .get_max_voltage = axp813_battery_get_max_voltage,
5416ff653e3SQuentin Schulz .set_max_voltage = axp20x_battery_set_max_voltage,
5426ff653e3SQuentin Schulz };
5436ff653e3SQuentin Schulz
54446c202b5SQuentin Schulz static const struct of_device_id axp20x_battery_ps_id[] = {
54546c202b5SQuentin Schulz {
54646c202b5SQuentin Schulz .compatible = "x-powers,axp209-battery-power-supply",
547648badd7SQuentin Schulz .data = (void *)&axp209_data,
54846c202b5SQuentin Schulz }, {
54946c202b5SQuentin Schulz .compatible = "x-powers,axp221-battery-power-supply",
550648badd7SQuentin Schulz .data = (void *)&axp221_data,
5516ff653e3SQuentin Schulz }, {
5526ff653e3SQuentin Schulz .compatible = "x-powers,axp813-battery-power-supply",
5536ff653e3SQuentin Schulz .data = (void *)&axp813_data,
55446c202b5SQuentin Schulz }, { /* sentinel */ },
55546c202b5SQuentin Schulz };
55646c202b5SQuentin Schulz MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
55746c202b5SQuentin Schulz
axp20x_power_probe(struct platform_device * pdev)55846c202b5SQuentin Schulz static int axp20x_power_probe(struct platform_device *pdev)
55946c202b5SQuentin Schulz {
56046c202b5SQuentin Schulz struct axp20x_batt_ps *axp20x_batt;
56146c202b5SQuentin Schulz struct power_supply_config psy_cfg = {};
56225fd3303SLinus Walleij struct power_supply_battery_info *info;
563648badd7SQuentin Schulz struct device *dev = &pdev->dev;
56446c202b5SQuentin Schulz
56546c202b5SQuentin Schulz if (!of_device_is_available(pdev->dev.of_node))
56646c202b5SQuentin Schulz return -ENODEV;
56746c202b5SQuentin Schulz
56846c202b5SQuentin Schulz axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
56946c202b5SQuentin Schulz GFP_KERNEL);
57046c202b5SQuentin Schulz if (!axp20x_batt)
57146c202b5SQuentin Schulz return -ENOMEM;
57246c202b5SQuentin Schulz
57346c202b5SQuentin Schulz axp20x_batt->dev = &pdev->dev;
57446c202b5SQuentin Schulz
57546c202b5SQuentin Schulz axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
57646c202b5SQuentin Schulz if (IS_ERR(axp20x_batt->batt_v)) {
57746c202b5SQuentin Schulz if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
57846c202b5SQuentin Schulz return -EPROBE_DEFER;
57946c202b5SQuentin Schulz return PTR_ERR(axp20x_batt->batt_v);
58046c202b5SQuentin Schulz }
58146c202b5SQuentin Schulz
58246c202b5SQuentin Schulz axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
58346c202b5SQuentin Schulz "batt_chrg_i");
58446c202b5SQuentin Schulz if (IS_ERR(axp20x_batt->batt_chrg_i)) {
58546c202b5SQuentin Schulz if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
58646c202b5SQuentin Schulz return -EPROBE_DEFER;
58746c202b5SQuentin Schulz return PTR_ERR(axp20x_batt->batt_chrg_i);
58846c202b5SQuentin Schulz }
58946c202b5SQuentin Schulz
59046c202b5SQuentin Schulz axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
59146c202b5SQuentin Schulz "batt_dischrg_i");
59246c202b5SQuentin Schulz if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
59346c202b5SQuentin Schulz if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
59446c202b5SQuentin Schulz return -EPROBE_DEFER;
59546c202b5SQuentin Schulz return PTR_ERR(axp20x_batt->batt_dischrg_i);
59646c202b5SQuentin Schulz }
59746c202b5SQuentin Schulz
59846c202b5SQuentin Schulz axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
59946c202b5SQuentin Schulz platform_set_drvdata(pdev, axp20x_batt);
60046c202b5SQuentin Schulz
60146c202b5SQuentin Schulz psy_cfg.drv_data = axp20x_batt;
60246c202b5SQuentin Schulz psy_cfg.of_node = pdev->dev.of_node;
60346c202b5SQuentin Schulz
604648badd7SQuentin Schulz axp20x_batt->data = (struct axp_data *)of_device_get_match_data(dev);
60546c202b5SQuentin Schulz
60646c202b5SQuentin Schulz axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
60746c202b5SQuentin Schulz &axp20x_batt_ps_desc,
60846c202b5SQuentin Schulz &psy_cfg);
60946c202b5SQuentin Schulz if (IS_ERR(axp20x_batt->batt)) {
61046c202b5SQuentin Schulz dev_err(&pdev->dev, "failed to register power supply: %ld\n",
61146c202b5SQuentin Schulz PTR_ERR(axp20x_batt->batt));
61246c202b5SQuentin Schulz return PTR_ERR(axp20x_batt->batt);
61346c202b5SQuentin Schulz }
61446c202b5SQuentin Schulz
615f8c91baeSQuentin Schulz if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
61625fd3303SLinus Walleij int vmin = info->voltage_min_design_uv;
61725fd3303SLinus Walleij int ccc = info->constant_charge_current_max_ua;
618f8c91baeSQuentin Schulz
619f8c91baeSQuentin Schulz if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
620f8c91baeSQuentin Schulz vmin))
621f8c91baeSQuentin Schulz dev_err(&pdev->dev,
622f8c91baeSQuentin Schulz "couldn't set voltage_min_design\n");
623c8003844SQuentin Schulz
624c8003844SQuentin Schulz /* Set max to unverified value to be able to set CCC */
625c8003844SQuentin Schulz axp20x_batt->max_ccc = ccc;
626c8003844SQuentin Schulz
627c8003844SQuentin Schulz if (ccc <= 0 || axp20x_set_constant_charge_current(axp20x_batt,
628c8003844SQuentin Schulz ccc)) {
629c8003844SQuentin Schulz dev_err(&pdev->dev,
630c8003844SQuentin Schulz "couldn't set constant charge current from DT: fallback to minimum value\n");
631c8003844SQuentin Schulz ccc = 300000;
632c8003844SQuentin Schulz axp20x_batt->max_ccc = ccc;
633c8003844SQuentin Schulz axp20x_set_constant_charge_current(axp20x_batt, ccc);
634f8c91baeSQuentin Schulz }
635c8003844SQuentin Schulz }
636c8003844SQuentin Schulz
637c8003844SQuentin Schulz /*
638c8003844SQuentin Schulz * Update max CCC to a valid value if battery info is present or set it
639c8003844SQuentin Schulz * to current register value by default.
640c8003844SQuentin Schulz */
641c8003844SQuentin Schulz axp20x_get_constant_charge_current(axp20x_batt,
642c8003844SQuentin Schulz &axp20x_batt->max_ccc);
643f8c91baeSQuentin Schulz
64446c202b5SQuentin Schulz return 0;
64546c202b5SQuentin Schulz }
64646c202b5SQuentin Schulz
64746c202b5SQuentin Schulz static struct platform_driver axp20x_batt_driver = {
64846c202b5SQuentin Schulz .probe = axp20x_power_probe,
64946c202b5SQuentin Schulz .driver = {
65046c202b5SQuentin Schulz .name = "axp20x-battery-power-supply",
65146c202b5SQuentin Schulz .of_match_table = axp20x_battery_ps_id,
65246c202b5SQuentin Schulz },
65346c202b5SQuentin Schulz };
65446c202b5SQuentin Schulz
65546c202b5SQuentin Schulz module_platform_driver(axp20x_batt_driver);
65646c202b5SQuentin Schulz
65746c202b5SQuentin Schulz MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
65846c202b5SQuentin Schulz MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
65946c202b5SQuentin Schulz MODULE_LICENSE("GPL");
660