^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright 2016 Free Electrons NextThing Co.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Quentin Schulz <quentin.schulz@free-electrons.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This driver is based on a previous upstreaming attempt by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Bruno Prémont <bonbons@linux-vserver.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * This file is subject to the terms and conditions of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Public License. See the file "COPYING" in the main directory of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * archive for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/mfd/axp20x.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define AXP20X_PWR_STATUS_BAT_CHARGING BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define AXP20X_PWR_OP_BATT_PRESENT BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define AXP20X_PWR_OP_BATT_ACTIVATED BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define AXP209_FG_PERCENT GENMASK(6, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define AXP22X_FG_VALID BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define AXP20X_CHRG_CTRL1_TGT_VOLT GENMASK(6, 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define AXP20X_CHRG_CTRL1_TGT_4_1V (0 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define AXP20X_CHRG_CTRL1_TGT_4_15V (1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define AXP20X_CHRG_CTRL1_TGT_4_2V (2 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define AXP20X_CHRG_CTRL1_TGT_4_36V (3 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define AXP22X_CHRG_CTRL1_TGT_4_22V (1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define AXP22X_CHRG_CTRL1_TGT_4_24V (3 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define AXP813_CHRG_CTRL1_TGT_4_35V (3 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define AXP20X_CHRG_CTRL1_TGT_CURR GENMASK(3, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define AXP20X_V_OFF_MASK GENMASK(2, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct axp20x_batt_ps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct axp_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int ccc_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int ccc_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) bool has_fg_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int (*get_max_voltage)(struct axp20x_batt_ps *batt, int *val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int (*set_max_voltage)(struct axp20x_batt_ps *batt, int val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct axp20x_batt_ps {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct power_supply *batt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct iio_channel *batt_chrg_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct iio_channel *batt_dischrg_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct iio_channel *batt_v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* Maximum constant charge current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) unsigned int max_ccc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) const struct axp_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int ret, reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) case AXP20X_CHRG_CTRL1_TGT_4_1V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) *val = 4100000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case AXP20X_CHRG_CTRL1_TGT_4_15V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) *val = 4150000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) case AXP20X_CHRG_CTRL1_TGT_4_2V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *val = 4200000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) case AXP20X_CHRG_CTRL1_TGT_4_36V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) *val = 4360000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int ret, reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) case AXP20X_CHRG_CTRL1_TGT_4_1V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) *val = 4100000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) case AXP20X_CHRG_CTRL1_TGT_4_2V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) *val = 4200000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) case AXP22X_CHRG_CTRL1_TGT_4_22V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) *val = 4220000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) case AXP22X_CHRG_CTRL1_TGT_4_24V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) *val = 4240000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int axp813_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int ret, reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) case AXP20X_CHRG_CTRL1_TGT_4_1V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) *val = 4100000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) case AXP20X_CHRG_CTRL1_TGT_4_15V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *val = 4150000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) case AXP20X_CHRG_CTRL1_TGT_4_2V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) *val = 4200000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) case AXP813_CHRG_CTRL1_TGT_4_35V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) *val = 4350000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int axp20x_get_constant_charge_current(struct axp20x_batt_ps *axp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret = regmap_read(axp->regmap, AXP20X_CHRG_CTRL1, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) *val &= AXP20X_CHRG_CTRL1_TGT_CURR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) *val = *val * axp->data->ccc_scale + axp->data->ccc_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int axp20x_battery_get_prop(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct iio_channel *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int ret = 0, reg, val1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) &val1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (val1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * Fuel Gauge data takes 7 bits but the stored value seems to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * directly the raw percentage without any scaling to 7 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if ((val1 & AXP209_FG_PERCENT) == 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) val->intval = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) &val1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) val->intval = POWER_SUPPLY_HEALTH_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) val->intval = POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ret = axp20x_get_constant_charge_current(axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) val->intval = axp20x_batt->max_ccc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) chan = axp20x_batt->batt_chrg_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) chan = axp20x_batt->batt_dischrg_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ret = iio_read_channel_processed(chan, &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /* IIO framework gives mA but Power Supply framework gives uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) val->intval *= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) case POWER_SUPPLY_PROP_CAPACITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /* When no battery is present, return capacity is 100% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) val->intval = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (axp20x_batt->data->has_fg_valid && !(reg & AXP22X_FG_VALID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * Fuel Gauge data takes 7 bits but the stored value seems to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * directly the raw percentage without any scaling to 7 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) val->intval = reg & AXP209_FG_PERCENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return axp20x_batt->data->get_max_voltage(axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) ret = iio_read_channel_processed(axp20x_batt->batt_v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) &val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /* IIO framework gives mV but Power Supply framework gives uV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) val->intval *= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static int axp22x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) case 4100000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) val = AXP20X_CHRG_CTRL1_TGT_4_1V;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) case 4200000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) val = AXP20X_CHRG_CTRL1_TGT_4_2V;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * can be set to 4.22V and 4.24V, but these voltages are too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * high for Lithium based batteries (AXP PMICs are supposed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * be used with these kinds of battery).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) AXP20X_CHRG_CTRL1_TGT_VOLT, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) case 4100000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) val = AXP20X_CHRG_CTRL1_TGT_4_1V;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) case 4150000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) val = AXP20X_CHRG_CTRL1_TGT_4_15V;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) case 4200000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) val = AXP20X_CHRG_CTRL1_TGT_4_2V;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * can be set to 4.22V and 4.24V, but these voltages are too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * high for Lithium based batteries (AXP PMICs are supposed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * be used with these kinds of battery).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) AXP20X_CHRG_CTRL1_TGT_VOLT, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) int charge_current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (charge_current > axp_batt->max_ccc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) charge_current = (charge_current - axp_batt->data->ccc_offset) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) axp_batt->data->ccc_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static int axp20x_set_max_constant_charge_current(struct axp20x_batt_ps *axp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) int charge_current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) bool lower_max = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) charge_current = (charge_current - axp->data->ccc_offset) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) axp->data->ccc_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) charge_current = charge_current * axp->data->ccc_scale +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) axp->data->ccc_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (charge_current > axp->max_ccc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) dev_warn(axp->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) "Setting max constant charge current higher than previously defined. Note that increasing the constant charge current may damage your battery.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) lower_max = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) axp->max_ccc = charge_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (lower_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) int current_cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) axp20x_get_constant_charge_current(axp, ¤t_cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (current_cc > charge_current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) axp20x_set_constant_charge_current(axp, charge_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) int min_voltage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) int val1 = (min_voltage - 2600000) / 100000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) AXP20X_V_OFF_MASK, val1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static int axp20x_battery_set_prop(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) const union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) return axp20x_batt->data->set_max_voltage(axp20x_batt, val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) return axp20x_set_constant_charge_current(axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return axp20x_set_max_constant_charge_current(axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static enum power_supply_property axp20x_battery_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) POWER_SUPPLY_PROP_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static int axp20x_battery_prop_writeable(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) enum power_supply_property psp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static const struct power_supply_desc axp20x_batt_ps_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) .name = "axp20x-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) .type = POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) .properties = axp20x_battery_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) .num_properties = ARRAY_SIZE(axp20x_battery_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) .property_is_writeable = axp20x_battery_prop_writeable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) .get_property = axp20x_battery_get_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) .set_property = axp20x_battery_set_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static const struct axp_data axp209_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) .ccc_scale = 100000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) .ccc_offset = 300000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) .get_max_voltage = axp20x_battery_get_max_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) .set_max_voltage = axp20x_battery_set_max_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) static const struct axp_data axp221_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) .ccc_scale = 150000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) .ccc_offset = 300000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) .has_fg_valid = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) .get_max_voltage = axp22x_battery_get_max_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) .set_max_voltage = axp22x_battery_set_max_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) static const struct axp_data axp813_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) .ccc_scale = 200000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) .ccc_offset = 200000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) .has_fg_valid = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) .get_max_voltage = axp813_battery_get_max_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) .set_max_voltage = axp20x_battery_set_max_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) static const struct of_device_id axp20x_battery_ps_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) .compatible = "x-powers,axp209-battery-power-supply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) .data = (void *)&axp209_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) .compatible = "x-powers,axp221-battery-power-supply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) .data = (void *)&axp221_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) .compatible = "x-powers,axp813-battery-power-supply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) .data = (void *)&axp813_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }, { /* sentinel */ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static int axp20x_power_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) struct axp20x_batt_ps *axp20x_batt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) struct power_supply_battery_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (!of_device_is_available(pdev->dev.of_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (!axp20x_batt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) axp20x_batt->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (IS_ERR(axp20x_batt->batt_v)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return PTR_ERR(axp20x_batt->batt_v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) "batt_chrg_i");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (IS_ERR(axp20x_batt->batt_chrg_i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return PTR_ERR(axp20x_batt->batt_chrg_i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) "batt_dischrg_i");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) return PTR_ERR(axp20x_batt->batt_dischrg_i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) platform_set_drvdata(pdev, axp20x_batt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) psy_cfg.drv_data = axp20x_batt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) psy_cfg.of_node = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) axp20x_batt->data = (struct axp_data *)of_device_get_match_data(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) &axp20x_batt_ps_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (IS_ERR(axp20x_batt->batt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) dev_err(&pdev->dev, "failed to register power supply: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) PTR_ERR(axp20x_batt->batt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) return PTR_ERR(axp20x_batt->batt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) int vmin = info.voltage_min_design_uv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) int ccc = info.constant_charge_current_max_ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) vmin))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) "couldn't set voltage_min_design\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) /* Set max to unverified value to be able to set CCC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) axp20x_batt->max_ccc = ccc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (ccc <= 0 || axp20x_set_constant_charge_current(axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) ccc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) "couldn't set constant charge current from DT: fallback to minimum value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ccc = 300000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) axp20x_batt->max_ccc = ccc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) axp20x_set_constant_charge_current(axp20x_batt, ccc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * Update max CCC to a valid value if battery info is present or set it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) * to current register value by default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) axp20x_get_constant_charge_current(axp20x_batt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) &axp20x_batt->max_ccc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) static struct platform_driver axp20x_batt_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) .probe = axp20x_power_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) .name = "axp20x-battery-power-supply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) .of_match_table = axp20x_battery_ps_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) module_platform_driver(axp20x_batt_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) MODULE_LICENSE("GPL");