18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver
48c0984e5SSebastian Reichel  *
5394088f0SAndrejus Basovas  * Copyright (C) 2020-2021 Andrejus Basovas <xxx@yyy.tld>
6394088f0SAndrejus Basovas  * Copyright (C) 2016-2021 Hans de Goede <hdegoede@redhat.com>
78c0984e5SSebastian Reichel  * Copyright (C) 2014 Intel Corporation
88c0984e5SSebastian Reichel  *
98c0984e5SSebastian Reichel  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108c0984e5SSebastian Reichel  */
118c0984e5SSebastian Reichel 
12da365db7SHans de Goede #include <linux/acpi.h>
13b60c75b6SHans de Goede #include <linux/dmi.h>
148c0984e5SSebastian Reichel #include <linux/module.h>
158c0984e5SSebastian Reichel #include <linux/kernel.h>
168c0984e5SSebastian Reichel #include <linux/device.h>
178c0984e5SSebastian Reichel #include <linux/regmap.h>
188c0984e5SSebastian Reichel #include <linux/jiffies.h>
198c0984e5SSebastian Reichel #include <linux/interrupt.h>
208c0984e5SSebastian Reichel #include <linux/mfd/axp20x.h>
218c0984e5SSebastian Reichel #include <linux/platform_device.h>
228c0984e5SSebastian Reichel #include <linux/power_supply.h>
238c0984e5SSebastian Reichel #include <linux/iio/consumer.h>
244949fc5eSHans de Goede #include <asm/unaligned.h>
25394088f0SAndrejus Basovas #include <asm/iosf_mbi.h>
268c0984e5SSebastian Reichel 
272b5a4b4bSHans de Goede #define PS_STAT_VBUS_TRIGGER			(1 << 0)
282b5a4b4bSHans de Goede #define PS_STAT_BAT_CHRG_DIR			(1 << 2)
292b5a4b4bSHans de Goede #define PS_STAT_VBAT_ABOVE_VHOLD		(1 << 3)
302b5a4b4bSHans de Goede #define PS_STAT_VBUS_VALID			(1 << 4)
312b5a4b4bSHans de Goede #define PS_STAT_VBUS_PRESENT			(1 << 5)
322b5a4b4bSHans de Goede 
338c0984e5SSebastian Reichel #define CHRG_STAT_BAT_SAFE_MODE			(1 << 3)
348c0984e5SSebastian Reichel #define CHRG_STAT_BAT_VALID			(1 << 4)
358c0984e5SSebastian Reichel #define CHRG_STAT_BAT_PRESENT			(1 << 5)
368c0984e5SSebastian Reichel #define CHRG_STAT_CHARGING			(1 << 6)
378c0984e5SSebastian Reichel #define CHRG_STAT_PMIC_OTP			(1 << 7)
388c0984e5SSebastian Reichel 
398c0984e5SSebastian Reichel #define CHRG_CCCV_CC_MASK			0xf     /* 4 bits */
408c0984e5SSebastian Reichel #define CHRG_CCCV_CC_BIT_POS			0
418c0984e5SSebastian Reichel #define CHRG_CCCV_CC_OFFSET			200     /* 200mA */
428c0984e5SSebastian Reichel #define CHRG_CCCV_CC_LSB_RES			200     /* 200mA */
438c0984e5SSebastian Reichel #define CHRG_CCCV_ITERM_20P			(1 << 4)    /* 20% of CC */
448c0984e5SSebastian Reichel #define CHRG_CCCV_CV_MASK			0x60        /* 2 bits */
458c0984e5SSebastian Reichel #define CHRG_CCCV_CV_BIT_POS			5
468c0984e5SSebastian Reichel #define CHRG_CCCV_CV_4100MV			0x0     /* 4.10V */
478c0984e5SSebastian Reichel #define CHRG_CCCV_CV_4150MV			0x1     /* 4.15V */
488c0984e5SSebastian Reichel #define CHRG_CCCV_CV_4200MV			0x2     /* 4.20V */
498c0984e5SSebastian Reichel #define CHRG_CCCV_CV_4350MV			0x3     /* 4.35V */
508c0984e5SSebastian Reichel #define CHRG_CCCV_CHG_EN			(1 << 7)
518c0984e5SSebastian Reichel 
528c0984e5SSebastian Reichel #define FG_CNTL_OCV_ADJ_STAT			(1 << 2)
538c0984e5SSebastian Reichel #define FG_CNTL_OCV_ADJ_EN			(1 << 3)
548c0984e5SSebastian Reichel #define FG_CNTL_CAP_ADJ_STAT			(1 << 4)
558c0984e5SSebastian Reichel #define FG_CNTL_CAP_ADJ_EN			(1 << 5)
568c0984e5SSebastian Reichel #define FG_CNTL_CC_EN				(1 << 6)
578c0984e5SSebastian Reichel #define FG_CNTL_GAUGE_EN			(1 << 7)
588c0984e5SSebastian Reichel 
594949fc5eSHans de Goede #define FG_15BIT_WORD_VALID			(1 << 15)
604949fc5eSHans de Goede #define FG_15BIT_VAL_MASK			0x7fff
614949fc5eSHans de Goede 
628c0984e5SSebastian Reichel #define FG_REP_CAP_VALID			(1 << 7)
638c0984e5SSebastian Reichel #define FG_REP_CAP_VAL_MASK			0x7F
648c0984e5SSebastian Reichel 
658c0984e5SSebastian Reichel #define FG_DES_CAP1_VALID			(1 << 7)
668c0984e5SSebastian Reichel #define FG_DES_CAP_RES_LSB			1456    /* 1.456mAhr */
678c0984e5SSebastian Reichel 
688c0984e5SSebastian Reichel #define FG_DES_CC_RES_LSB			1456    /* 1.456mAhr */
698c0984e5SSebastian Reichel 
708c0984e5SSebastian Reichel #define FG_OCV_CAP_VALID			(1 << 7)
718c0984e5SSebastian Reichel #define FG_OCV_CAP_VAL_MASK			0x7F
728c0984e5SSebastian Reichel #define FG_CC_CAP_VALID				(1 << 7)
738c0984e5SSebastian Reichel #define FG_CC_CAP_VAL_MASK			0x7F
748c0984e5SSebastian Reichel 
758c0984e5SSebastian Reichel #define FG_LOW_CAP_THR1_MASK			0xf0    /* 5% tp 20% */
768c0984e5SSebastian Reichel #define FG_LOW_CAP_THR1_VAL			0xa0    /* 15 perc */
778c0984e5SSebastian Reichel #define FG_LOW_CAP_THR2_MASK			0x0f    /* 0% to 15% */
788c0984e5SSebastian Reichel #define FG_LOW_CAP_WARN_THR			14  /* 14 perc */
798c0984e5SSebastian Reichel #define FG_LOW_CAP_CRIT_THR			4   /* 4 perc */
808c0984e5SSebastian Reichel #define FG_LOW_CAP_SHDN_THR			0   /* 0 perc */
818c0984e5SSebastian Reichel 
828c0984e5SSebastian Reichel #define DEV_NAME				"axp288_fuel_gauge"
838c0984e5SSebastian Reichel 
848c0984e5SSebastian Reichel /* 1.1mV per LSB expressed in uV */
858c0984e5SSebastian Reichel #define VOLTAGE_FROM_ADC(a)			((a * 11) / 10)
86888f9743SHans de Goede /* properties converted to uV, uA */
878c0984e5SSebastian Reichel #define PROP_VOLT(a)				((a) * 1000)
888c0984e5SSebastian Reichel #define PROP_CURR(a)				((a) * 1000)
898c0984e5SSebastian Reichel 
90394088f0SAndrejus Basovas #define AXP288_REG_UPDATE_INTERVAL		(60 * HZ)
918c0984e5SSebastian Reichel #define AXP288_FG_INTR_NUM			6
92210bc22cSHans de Goede 
9334f243e9SHans de Goede #define AXP288_QUIRK_NO_BATTERY			BIT(0)
9434f243e9SHans de Goede 
95210bc22cSHans de Goede static bool no_current_sense_res;
96210bc22cSHans de Goede module_param(no_current_sense_res, bool, 0444);
97784056d3SColin Ian King MODULE_PARM_DESC(no_current_sense_res, "No (or broken) current sense resistor");
98210bc22cSHans de Goede 
998c0984e5SSebastian Reichel enum {
1008c0984e5SSebastian Reichel 	QWBTU_IRQ = 0,
1018c0984e5SSebastian Reichel 	WBTU_IRQ,
1028c0984e5SSebastian Reichel 	QWBTO_IRQ,
1038c0984e5SSebastian Reichel 	WBTO_IRQ,
1048c0984e5SSebastian Reichel 	WL2_IRQ,
1058c0984e5SSebastian Reichel 	WL1_IRQ,
1068c0984e5SSebastian Reichel };
1078c0984e5SSebastian Reichel 
108331645e1SHans de Goede enum {
109331645e1SHans de Goede 	BAT_CHRG_CURR,
110331645e1SHans de Goede 	BAT_D_CURR,
111331645e1SHans de Goede 	BAT_VOLT,
112331645e1SHans de Goede 	IIO_CHANNEL_NUM
113331645e1SHans de Goede };
114331645e1SHans de Goede 
1158c0984e5SSebastian Reichel struct axp288_fg_info {
1167eef3e66SHans de Goede 	struct device *dev;
1178c0984e5SSebastian Reichel 	struct regmap *regmap;
1188c0984e5SSebastian Reichel 	int irq[AXP288_FG_INTR_NUM];
119331645e1SHans de Goede 	struct iio_channel *iio_channel[IIO_CHANNEL_NUM];
1208c0984e5SSebastian Reichel 	struct power_supply *bat;
1218c0984e5SSebastian Reichel 	struct mutex lock;
1228c0984e5SSebastian Reichel 	int status;
123888f9743SHans de Goede 	int max_volt;
124c371d449SHans de Goede 	int pwr_op;
125c371d449SHans de Goede 	int low_cap;
1268c0984e5SSebastian Reichel 	struct dentry *debug_file;
127394088f0SAndrejus Basovas 
128394088f0SAndrejus Basovas 	char valid;                 /* zero until following fields are valid */
129394088f0SAndrejus Basovas 	unsigned long last_updated; /* in jiffies */
130394088f0SAndrejus Basovas 
131394088f0SAndrejus Basovas 	int pwr_stat;
132394088f0SAndrejus Basovas 	int fg_res;
133394088f0SAndrejus Basovas 	int bat_volt;
134394088f0SAndrejus Basovas 	int d_curr;
135394088f0SAndrejus Basovas 	int c_curr;
136394088f0SAndrejus Basovas 	int ocv;
137394088f0SAndrejus Basovas 	int fg_cc_mtr1;
138394088f0SAndrejus Basovas 	int fg_des_cap1;
1398c0984e5SSebastian Reichel };
1408c0984e5SSebastian Reichel 
1418c0984e5SSebastian Reichel static enum power_supply_property fuel_gauge_props[] = {
1428c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
1438c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
1448c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
1458c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1468c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
1478c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_OCV,
1488c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
1498c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
1508c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
151210bc22cSHans de Goede 	/* The 3 props below are not used when no_current_sense_res is set */
1528c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL,
1538c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
154210bc22cSHans de Goede 	POWER_SUPPLY_PROP_CURRENT_NOW,
1558c0984e5SSebastian Reichel };
1568c0984e5SSebastian Reichel 
fuel_gauge_reg_readb(struct axp288_fg_info * info,int reg)1578c0984e5SSebastian Reichel static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg)
1588c0984e5SSebastian Reichel {
1598c0984e5SSebastian Reichel 	unsigned int val;
160f17bda7fSHans de Goede 	int ret;
1618c0984e5SSebastian Reichel 
1628c0984e5SSebastian Reichel 	ret = regmap_read(info->regmap, reg, &val);
1636f074bc8SHans de Goede 	if (ret < 0) {
1647eef3e66SHans de Goede 		dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret);
1656f074bc8SHans de Goede 		return ret;
1666f074bc8SHans de Goede 	}
1678c0984e5SSebastian Reichel 
1688c0984e5SSebastian Reichel 	return val;
1698c0984e5SSebastian Reichel }
1708c0984e5SSebastian Reichel 
fuel_gauge_reg_writeb(struct axp288_fg_info * info,int reg,u8 val)1718c0984e5SSebastian Reichel static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val)
1728c0984e5SSebastian Reichel {
1738c0984e5SSebastian Reichel 	int ret;
1748c0984e5SSebastian Reichel 
1758c0984e5SSebastian Reichel 	ret = regmap_write(info->regmap, reg, (unsigned int)val);
1768c0984e5SSebastian Reichel 
1778c0984e5SSebastian Reichel 	if (ret < 0)
1787eef3e66SHans de Goede 		dev_err(info->dev, "Error writing reg 0x%02x err: %d\n", reg, ret);
1798c0984e5SSebastian Reichel 
1808c0984e5SSebastian Reichel 	return ret;
1818c0984e5SSebastian Reichel }
1828c0984e5SSebastian Reichel 
fuel_gauge_read_15bit_word(struct axp288_fg_info * info,int reg)1834949fc5eSHans de Goede static int fuel_gauge_read_15bit_word(struct axp288_fg_info *info, int reg)
1844949fc5eSHans de Goede {
1854949fc5eSHans de Goede 	unsigned char buf[2];
1864949fc5eSHans de Goede 	int ret;
1874949fc5eSHans de Goede 
1884949fc5eSHans de Goede 	ret = regmap_bulk_read(info->regmap, reg, buf, 2);
1894949fc5eSHans de Goede 	if (ret < 0) {
1907eef3e66SHans de Goede 		dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret);
1914949fc5eSHans de Goede 		return ret;
1924949fc5eSHans de Goede 	}
1934949fc5eSHans de Goede 
1944949fc5eSHans de Goede 	ret = get_unaligned_be16(buf);
1954949fc5eSHans de Goede 	if (!(ret & FG_15BIT_WORD_VALID)) {
1967eef3e66SHans de Goede 		dev_err(info->dev, "Error reg 0x%02x contents not valid\n", reg);
1974949fc5eSHans de Goede 		return -ENXIO;
1984949fc5eSHans de Goede 	}
1994949fc5eSHans de Goede 
2004949fc5eSHans de Goede 	return ret & FG_15BIT_VAL_MASK;
2014949fc5eSHans de Goede }
2024949fc5eSHans de Goede 
fuel_gauge_read_12bit_word(struct axp288_fg_info * info,int reg)203248efcf0SHans de Goede static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg)
204248efcf0SHans de Goede {
205248efcf0SHans de Goede 	unsigned char buf[2];
206248efcf0SHans de Goede 	int ret;
207248efcf0SHans de Goede 
208248efcf0SHans de Goede 	ret = regmap_bulk_read(info->regmap, reg, buf, 2);
209248efcf0SHans de Goede 	if (ret < 0) {
2107eef3e66SHans de Goede 		dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret);
211248efcf0SHans de Goede 		return ret;
212248efcf0SHans de Goede 	}
213248efcf0SHans de Goede 
214248efcf0SHans de Goede 	/* 12-bit data values have upper 8 bits in buf[0], lower 4 in buf[1] */
215248efcf0SHans de Goede 	return (buf[0] << 4) | ((buf[1] >> 4) & 0x0f);
216248efcf0SHans de Goede }
217248efcf0SHans de Goede 
fuel_gauge_update_registers(struct axp288_fg_info * info)218394088f0SAndrejus Basovas static int fuel_gauge_update_registers(struct axp288_fg_info *info)
219394088f0SAndrejus Basovas {
220394088f0SAndrejus Basovas 	int ret;
221394088f0SAndrejus Basovas 
222394088f0SAndrejus Basovas 	if (info->valid && time_before(jiffies, info->last_updated + AXP288_REG_UPDATE_INTERVAL))
223394088f0SAndrejus Basovas 		return 0;
224394088f0SAndrejus Basovas 
225394088f0SAndrejus Basovas 	dev_dbg(info->dev, "Fuel Gauge updating register values...\n");
226394088f0SAndrejus Basovas 
227394088f0SAndrejus Basovas 	ret = iosf_mbi_block_punit_i2c_access();
228394088f0SAndrejus Basovas 	if (ret < 0)
229394088f0SAndrejus Basovas 		return ret;
230394088f0SAndrejus Basovas 
231394088f0SAndrejus Basovas 	ret = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS);
232394088f0SAndrejus Basovas 	if (ret < 0)
233394088f0SAndrejus Basovas 		goto out;
234394088f0SAndrejus Basovas 	info->pwr_stat = ret;
235394088f0SAndrejus Basovas 
236210bc22cSHans de Goede 	if (no_current_sense_res)
237210bc22cSHans de Goede 		ret = fuel_gauge_reg_readb(info, AXP288_FG_OCV_CAP_REG);
238210bc22cSHans de Goede 	else
239394088f0SAndrejus Basovas 		ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES);
240394088f0SAndrejus Basovas 	if (ret < 0)
241394088f0SAndrejus Basovas 		goto out;
242394088f0SAndrejus Basovas 	info->fg_res = ret;
243394088f0SAndrejus Basovas 
244394088f0SAndrejus Basovas 	ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &info->bat_volt);
245394088f0SAndrejus Basovas 	if (ret < 0)
246394088f0SAndrejus Basovas 		goto out;
247394088f0SAndrejus Basovas 
248210bc22cSHans de Goede 	ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG);
249210bc22cSHans de Goede 	if (ret < 0)
250210bc22cSHans de Goede 		goto out;
251210bc22cSHans de Goede 	info->ocv = ret;
252210bc22cSHans de Goede 
253210bc22cSHans de Goede 	if (no_current_sense_res)
254210bc22cSHans de Goede 		goto out_no_current_sense_res;
255210bc22cSHans de Goede 
256394088f0SAndrejus Basovas 	if (info->pwr_stat & PS_STAT_BAT_CHRG_DIR) {
257394088f0SAndrejus Basovas 		info->d_curr = 0;
258394088f0SAndrejus Basovas 		ret = iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], &info->c_curr);
259394088f0SAndrejus Basovas 		if (ret < 0)
260394088f0SAndrejus Basovas 			goto out;
261394088f0SAndrejus Basovas 	} else {
262394088f0SAndrejus Basovas 		info->c_curr = 0;
263394088f0SAndrejus Basovas 		ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &info->d_curr);
264394088f0SAndrejus Basovas 		if (ret < 0)
265394088f0SAndrejus Basovas 			goto out;
266394088f0SAndrejus Basovas 	}
267394088f0SAndrejus Basovas 
268394088f0SAndrejus Basovas 	ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG);
269394088f0SAndrejus Basovas 	if (ret < 0)
270394088f0SAndrejus Basovas 		goto out;
271394088f0SAndrejus Basovas 	info->fg_cc_mtr1 = ret;
272394088f0SAndrejus Basovas 
273394088f0SAndrejus Basovas 	ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG);
274394088f0SAndrejus Basovas 	if (ret < 0)
275394088f0SAndrejus Basovas 		goto out;
276394088f0SAndrejus Basovas 	info->fg_des_cap1 = ret;
277394088f0SAndrejus Basovas 
278210bc22cSHans de Goede out_no_current_sense_res:
279394088f0SAndrejus Basovas 	info->last_updated = jiffies;
280394088f0SAndrejus Basovas 	info->valid = 1;
281394088f0SAndrejus Basovas 	ret = 0;
282394088f0SAndrejus Basovas out:
283394088f0SAndrejus Basovas 	iosf_mbi_unblock_punit_i2c_access();
284394088f0SAndrejus Basovas 	return ret;
285394088f0SAndrejus Basovas }
286394088f0SAndrejus Basovas 
fuel_gauge_get_status(struct axp288_fg_info * info)2878c0984e5SSebastian Reichel static void fuel_gauge_get_status(struct axp288_fg_info *info)
2888c0984e5SSebastian Reichel {
289394088f0SAndrejus Basovas 	int pwr_stat = info->pwr_stat;
290394088f0SAndrejus Basovas 	int fg_res = info->fg_res;
291394088f0SAndrejus Basovas 	int curr = info->d_curr;
2922b5a4b4bSHans de Goede 
2932b5a4b4bSHans de Goede 	/* Report full if Vbus is valid and the reported capacity is 100% */
294f451655cSHans de Goede 	if (!(pwr_stat & PS_STAT_VBUS_VALID))
295f451655cSHans de Goede 		goto not_full;
296f451655cSHans de Goede 
297f451655cSHans de Goede 	if (!(fg_res & FG_REP_CAP_VALID))
298f451655cSHans de Goede 		goto not_full;
299f451655cSHans de Goede 
300f451655cSHans de Goede 	fg_res &= ~FG_REP_CAP_VALID;
301f451655cSHans de Goede 	if (fg_res == 100) {
3022b5a4b4bSHans de Goede 		info->status = POWER_SUPPLY_STATUS_FULL;
3038c0984e5SSebastian Reichel 		return;
3048c0984e5SSebastian Reichel 	}
305f451655cSHans de Goede 
306f451655cSHans de Goede 	/*
307f451655cSHans de Goede 	 * Sometimes the charger turns itself off before fg-res reaches 100%.
308f451655cSHans de Goede 	 * When this happens the AXP288 reports a not-charging status and
309f451655cSHans de Goede 	 * 0 mA discharge current.
310f451655cSHans de Goede 	 */
311210bc22cSHans de Goede 	if (fg_res < 90 || (pwr_stat & PS_STAT_BAT_CHRG_DIR) || no_current_sense_res)
312f451655cSHans de Goede 		goto not_full;
313f451655cSHans de Goede 
314f451655cSHans de Goede 	if (curr == 0) {
315f451655cSHans de Goede 		info->status = POWER_SUPPLY_STATUS_FULL;
316f451655cSHans de Goede 		return;
3172b5a4b4bSHans de Goede 	}
3188c0984e5SSebastian Reichel 
319f451655cSHans de Goede not_full:
3202b5a4b4bSHans de Goede 	if (pwr_stat & PS_STAT_BAT_CHRG_DIR)
3218c0984e5SSebastian Reichel 		info->status = POWER_SUPPLY_STATUS_CHARGING;
3228c0984e5SSebastian Reichel 	else
3232b5a4b4bSHans de Goede 		info->status = POWER_SUPPLY_STATUS_DISCHARGING;
3248c0984e5SSebastian Reichel }
3258c0984e5SSebastian Reichel 
fuel_gauge_battery_health(struct axp288_fg_info * info)3268c0984e5SSebastian Reichel static int fuel_gauge_battery_health(struct axp288_fg_info *info)
3278c0984e5SSebastian Reichel {
328394088f0SAndrejus Basovas 	int vocv = VOLTAGE_FROM_ADC(info->ocv);
329394088f0SAndrejus Basovas 	int health = POWER_SUPPLY_HEALTH_UNKNOWN;
3308c0984e5SSebastian Reichel 
331888f9743SHans de Goede 	if (vocv > info->max_volt)
3328c0984e5SSebastian Reichel 		health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
3338c0984e5SSebastian Reichel 	else
3348c0984e5SSebastian Reichel 		health = POWER_SUPPLY_HEALTH_GOOD;
3358c0984e5SSebastian Reichel 
3368c0984e5SSebastian Reichel 	return health;
3378c0984e5SSebastian Reichel }
3388c0984e5SSebastian Reichel 
fuel_gauge_get_property(struct power_supply * ps,enum power_supply_property prop,union power_supply_propval * val)3398c0984e5SSebastian Reichel static int fuel_gauge_get_property(struct power_supply *ps,
3408c0984e5SSebastian Reichel 		enum power_supply_property prop,
3418c0984e5SSebastian Reichel 		union power_supply_propval *val)
3428c0984e5SSebastian Reichel {
3438c0984e5SSebastian Reichel 	struct axp288_fg_info *info = power_supply_get_drvdata(ps);
344394088f0SAndrejus Basovas 	int ret, value;
3458c0984e5SSebastian Reichel 
3468c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
347394088f0SAndrejus Basovas 
348394088f0SAndrejus Basovas 	ret = fuel_gauge_update_registers(info);
349394088f0SAndrejus Basovas 	if (ret < 0)
350394088f0SAndrejus Basovas 		goto out;
351394088f0SAndrejus Basovas 
3528c0984e5SSebastian Reichel 	switch (prop) {
3538c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
3548c0984e5SSebastian Reichel 		fuel_gauge_get_status(info);
3558c0984e5SSebastian Reichel 		val->intval = info->status;
3568c0984e5SSebastian Reichel 		break;
3578c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_HEALTH:
3588c0984e5SSebastian Reichel 		val->intval = fuel_gauge_battery_health(info);
3598c0984e5SSebastian Reichel 		break;
3608c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
361394088f0SAndrejus Basovas 		value = VOLTAGE_FROM_ADC(info->bat_volt);
3628c0984e5SSebastian Reichel 		val->intval = PROP_VOLT(value);
3638c0984e5SSebastian Reichel 		break;
3648c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
365394088f0SAndrejus Basovas 		value = VOLTAGE_FROM_ADC(info->ocv);
3668c0984e5SSebastian Reichel 		val->intval = PROP_VOLT(value);
3678c0984e5SSebastian Reichel 		break;
3688c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_NOW:
369394088f0SAndrejus Basovas 		if (info->d_curr > 0)
370394088f0SAndrejus Basovas 			value = -1 * info->d_curr;
371394088f0SAndrejus Basovas 		else
372394088f0SAndrejus Basovas 			value = info->c_curr;
373394088f0SAndrejus Basovas 
3748c0984e5SSebastian Reichel 		val->intval = PROP_CURR(value);
3758c0984e5SSebastian Reichel 		break;
3768c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
377c371d449SHans de Goede 		if (info->pwr_op & CHRG_STAT_BAT_PRESENT)
3788c0984e5SSebastian Reichel 			val->intval = 1;
3798c0984e5SSebastian Reichel 		else
3808c0984e5SSebastian Reichel 			val->intval = 0;
3818c0984e5SSebastian Reichel 		break;
3828c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
383394088f0SAndrejus Basovas 		if (!(info->fg_res & FG_REP_CAP_VALID))
3847eef3e66SHans de Goede 			dev_err(info->dev, "capacity measurement not valid\n");
385394088f0SAndrejus Basovas 		val->intval = (info->fg_res & FG_REP_CAP_VAL_MASK);
3868c0984e5SSebastian Reichel 		break;
3878c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
388c371d449SHans de Goede 		val->intval = (info->low_cap & 0x0f);
3898c0984e5SSebastian Reichel 		break;
3908c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TECHNOLOGY:
3918c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
3928c0984e5SSebastian Reichel 		break;
3938c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_NOW:
394394088f0SAndrejus Basovas 		val->intval = info->fg_cc_mtr1 * FG_DES_CAP_RES_LSB;
3958c0984e5SSebastian Reichel 		break;
3968c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL:
397394088f0SAndrejus Basovas 		val->intval = info->fg_des_cap1 * FG_DES_CAP_RES_LSB;
3988c0984e5SSebastian Reichel 		break;
3998c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
400888f9743SHans de Goede 		val->intval = PROP_VOLT(info->max_volt);
4018c0984e5SSebastian Reichel 		break;
4028c0984e5SSebastian Reichel 	default:
403394088f0SAndrejus Basovas 		ret = -EINVAL;
4048c0984e5SSebastian Reichel 	}
4058c0984e5SSebastian Reichel 
406394088f0SAndrejus Basovas out:
4078c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
4088c0984e5SSebastian Reichel 	return ret;
4098c0984e5SSebastian Reichel }
4108c0984e5SSebastian Reichel 
fuel_gauge_set_property(struct power_supply * ps,enum power_supply_property prop,const union power_supply_propval * val)4118c0984e5SSebastian Reichel static int fuel_gauge_set_property(struct power_supply *ps,
4128c0984e5SSebastian Reichel 		enum power_supply_property prop,
4138c0984e5SSebastian Reichel 		const union power_supply_propval *val)
4148c0984e5SSebastian Reichel {
4158c0984e5SSebastian Reichel 	struct axp288_fg_info *info = power_supply_get_drvdata(ps);
416c371d449SHans de Goede 	int new_low_cap, ret = 0;
4178c0984e5SSebastian Reichel 
4188c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
4198c0984e5SSebastian Reichel 	switch (prop) {
4208c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
4218c0984e5SSebastian Reichel 		if ((val->intval < 0) || (val->intval > 15)) {
4228c0984e5SSebastian Reichel 			ret = -EINVAL;
4238c0984e5SSebastian Reichel 			break;
4248c0984e5SSebastian Reichel 		}
425c371d449SHans de Goede 		new_low_cap = info->low_cap;
426c371d449SHans de Goede 		new_low_cap &= 0xf0;
427c371d449SHans de Goede 		new_low_cap |= (val->intval & 0xf);
428c371d449SHans de Goede 		ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, new_low_cap);
429c371d449SHans de Goede 		if (ret == 0)
430c371d449SHans de Goede 			info->low_cap = new_low_cap;
4318c0984e5SSebastian Reichel 		break;
4328c0984e5SSebastian Reichel 	default:
4338c0984e5SSebastian Reichel 		ret = -EINVAL;
4348c0984e5SSebastian Reichel 		break;
4358c0984e5SSebastian Reichel 	}
4368c0984e5SSebastian Reichel 
4378c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
4388c0984e5SSebastian Reichel 	return ret;
4398c0984e5SSebastian Reichel }
4408c0984e5SSebastian Reichel 
fuel_gauge_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)4418c0984e5SSebastian Reichel static int fuel_gauge_property_is_writeable(struct power_supply *psy,
4428c0984e5SSebastian Reichel 	enum power_supply_property psp)
4438c0984e5SSebastian Reichel {
4448c0984e5SSebastian Reichel 	int ret;
4458c0984e5SSebastian Reichel 
4468c0984e5SSebastian Reichel 	switch (psp) {
4478c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
4488c0984e5SSebastian Reichel 		ret = 1;
4498c0984e5SSebastian Reichel 		break;
4508c0984e5SSebastian Reichel 	default:
4518c0984e5SSebastian Reichel 		ret = 0;
4528c0984e5SSebastian Reichel 	}
4538c0984e5SSebastian Reichel 
4548c0984e5SSebastian Reichel 	return ret;
4558c0984e5SSebastian Reichel }
4568c0984e5SSebastian Reichel 
fuel_gauge_thread_handler(int irq,void * dev)4578c0984e5SSebastian Reichel static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev)
4588c0984e5SSebastian Reichel {
4598c0984e5SSebastian Reichel 	struct axp288_fg_info *info = dev;
4608c0984e5SSebastian Reichel 	int i;
4618c0984e5SSebastian Reichel 
4628c0984e5SSebastian Reichel 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
4638c0984e5SSebastian Reichel 		if (info->irq[i] == irq)
4648c0984e5SSebastian Reichel 			break;
4658c0984e5SSebastian Reichel 	}
4668c0984e5SSebastian Reichel 
4678c0984e5SSebastian Reichel 	if (i >= AXP288_FG_INTR_NUM) {
4687eef3e66SHans de Goede 		dev_warn(info->dev, "spurious interrupt!!\n");
4698c0984e5SSebastian Reichel 		return IRQ_NONE;
4708c0984e5SSebastian Reichel 	}
4718c0984e5SSebastian Reichel 
4728c0984e5SSebastian Reichel 	switch (i) {
4738c0984e5SSebastian Reichel 	case QWBTU_IRQ:
4747eef3e66SHans de Goede 		dev_info(info->dev, "Quit Battery under temperature in work mode IRQ (QWBTU)\n");
4758c0984e5SSebastian Reichel 		break;
4768c0984e5SSebastian Reichel 	case WBTU_IRQ:
4777eef3e66SHans de Goede 		dev_info(info->dev, "Battery under temperature in work mode IRQ (WBTU)\n");
4788c0984e5SSebastian Reichel 		break;
4798c0984e5SSebastian Reichel 	case QWBTO_IRQ:
4807eef3e66SHans de Goede 		dev_info(info->dev, "Quit Battery over temperature in work mode IRQ (QWBTO)\n");
4818c0984e5SSebastian Reichel 		break;
4828c0984e5SSebastian Reichel 	case WBTO_IRQ:
4837eef3e66SHans de Goede 		dev_info(info->dev, "Battery over temperature in work mode IRQ (WBTO)\n");
4848c0984e5SSebastian Reichel 		break;
4858c0984e5SSebastian Reichel 	case WL2_IRQ:
4867eef3e66SHans de Goede 		dev_info(info->dev, "Low Batt Warning(2) INTR\n");
4878c0984e5SSebastian Reichel 		break;
4888c0984e5SSebastian Reichel 	case WL1_IRQ:
4897eef3e66SHans de Goede 		dev_info(info->dev, "Low Batt Warning(1) INTR\n");
4908c0984e5SSebastian Reichel 		break;
4918c0984e5SSebastian Reichel 	default:
4927eef3e66SHans de Goede 		dev_warn(info->dev, "Spurious Interrupt!!!\n");
4938c0984e5SSebastian Reichel 	}
4948c0984e5SSebastian Reichel 
49530abb3d0SHans de Goede 	mutex_lock(&info->lock);
496394088f0SAndrejus Basovas 	info->valid = 0; /* Force updating of the cached registers */
49730abb3d0SHans de Goede 	mutex_unlock(&info->lock);
498394088f0SAndrejus Basovas 
4998c0984e5SSebastian Reichel 	power_supply_changed(info->bat);
5008c0984e5SSebastian Reichel 	return IRQ_HANDLED;
5018c0984e5SSebastian Reichel }
5028c0984e5SSebastian Reichel 
fuel_gauge_external_power_changed(struct power_supply * psy)5038c0984e5SSebastian Reichel static void fuel_gauge_external_power_changed(struct power_supply *psy)
5048c0984e5SSebastian Reichel {
5058c0984e5SSebastian Reichel 	struct axp288_fg_info *info = power_supply_get_drvdata(psy);
5068c0984e5SSebastian Reichel 
50730abb3d0SHans de Goede 	mutex_lock(&info->lock);
508394088f0SAndrejus Basovas 	info->valid = 0; /* Force updating of the cached registers */
50930abb3d0SHans de Goede 	mutex_unlock(&info->lock);
510*f8319774SHans de Goede 	power_supply_changed(psy);
5118c0984e5SSebastian Reichel }
5128c0984e5SSebastian Reichel 
513210bc22cSHans de Goede static struct power_supply_desc fuel_gauge_desc = {
5148c0984e5SSebastian Reichel 	.name			= DEV_NAME,
5158c0984e5SSebastian Reichel 	.type			= POWER_SUPPLY_TYPE_BATTERY,
5168c0984e5SSebastian Reichel 	.properties		= fuel_gauge_props,
5178c0984e5SSebastian Reichel 	.num_properties		= ARRAY_SIZE(fuel_gauge_props),
5188c0984e5SSebastian Reichel 	.get_property		= fuel_gauge_get_property,
5198c0984e5SSebastian Reichel 	.set_property		= fuel_gauge_set_property,
5208c0984e5SSebastian Reichel 	.property_is_writeable	= fuel_gauge_property_is_writeable,
5218c0984e5SSebastian Reichel 	.external_power_changed	= fuel_gauge_external_power_changed,
5228c0984e5SSebastian Reichel };
5238c0984e5SSebastian Reichel 
524b60c75b6SHans de Goede /*
525b60c75b6SHans de Goede  * Some devices have no battery (HDMI sticks) and the axp288 battery's
526b60c75b6SHans de Goede  * detection reports one despite it not being there.
5276f3ed834SHans de Goede  * Please keep this listed sorted alphabetically.
528b60c75b6SHans de Goede  */
52934f243e9SHans de Goede static const struct dmi_system_id axp288_quirks[] = {
530b60c75b6SHans de Goede 	{
5319274c783SHans de Goede 		/* ACEPC T8 Cherry Trail Z8350 mini PC */
5329274c783SHans de Goede 		.matches = {
5339274c783SHans de Goede 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
5349274c783SHans de Goede 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"),
5359274c783SHans de Goede 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T8"),
5369274c783SHans de Goede 			/* also match on somewhat unique bios-version */
5379274c783SHans de Goede 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"),
5389274c783SHans de Goede 		},
53934f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
5409274c783SHans de Goede 	},
5419274c783SHans de Goede 	{
5429274c783SHans de Goede 		/* ACEPC T11 Cherry Trail Z8350 mini PC */
5439274c783SHans de Goede 		.matches = {
5449274c783SHans de Goede 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
5459274c783SHans de Goede 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"),
5469274c783SHans de Goede 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T11"),
5479274c783SHans de Goede 			/* also match on somewhat unique bios-version */
5489274c783SHans de Goede 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"),
5499274c783SHans de Goede 		},
55034f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
5519274c783SHans de Goede 	},
5529274c783SHans de Goede 	{
553b60c75b6SHans de Goede 		/* Intel Cherry Trail Compute Stick, Windows version */
554b60c75b6SHans de Goede 		.matches = {
555e42fe5b2SJeffery Miller 			DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
556b60c75b6SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "STK1AW32SC"),
557b60c75b6SHans de Goede 		},
55834f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
559b60c75b6SHans de Goede 	},
560b60c75b6SHans de Goede 	{
561b60c75b6SHans de Goede 		/* Intel Cherry Trail Compute Stick, version without an OS */
562b60c75b6SHans de Goede 		.matches = {
563e42fe5b2SJeffery Miller 			DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
564b60c75b6SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "STK1A32SC"),
565b60c75b6SHans de Goede 		},
56634f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
567b60c75b6SHans de Goede 	},
568b60c75b6SHans de Goede 	{
5694ac54b88SRafael Gandolfi 		/* Meegopad T02 */
5704ac54b88SRafael Gandolfi 		.matches = {
5714ac54b88SRafael Gandolfi 			DMI_MATCH(DMI_PRODUCT_NAME, "MEEGOPAD T02"),
5724ac54b88SRafael Gandolfi 		},
57334f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
5744ac54b88SRafael Gandolfi 	},
5756b714ea4SHans de Goede 	{	/* Mele PCG03 Mini PC */
5766b714ea4SHans de Goede 		.matches = {
5776b714ea4SHans de Goede 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
5786b714ea4SHans de Goede 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
5796b714ea4SHans de Goede 		},
58034f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
5816b714ea4SHans de Goede 	},
582fa7da744SHans de Goede 	{
583fa7da744SHans de Goede 		/* Minix Neo Z83-4 mini PC */
584fa7da744SHans de Goede 		.matches = {
585fa7da744SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "MINIX"),
586fa7da744SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
58734f243e9SHans de Goede 		},
58834f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
589fa7da744SHans de Goede 	},
5903a06b912SHans de Goede 	{
59134f243e9SHans de Goede 		/*
59234f243e9SHans de Goede 		 * One Mix 1, this uses the "T3 MRD" boardname used by
59334f243e9SHans de Goede 		 * generic mini PCs, but it is a mini laptop so it does
59434f243e9SHans de Goede 		 * actually have a battery!
59534f243e9SHans de Goede 		 */
59634f243e9SHans de Goede 		.matches = {
59734f243e9SHans de Goede 			DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"),
59834f243e9SHans de Goede 			DMI_MATCH(DMI_BIOS_DATE, "06/14/2018"),
59934f243e9SHans de Goede 		},
60034f243e9SHans de Goede 		.driver_data = NULL,
60134f243e9SHans de Goede 	},
60234f243e9SHans de Goede 	{
60334f243e9SHans de Goede 		/*
60434f243e9SHans de Goede 		 * Various Ace PC/Meegopad/MinisForum/Wintel Mini-PCs/HDMI-sticks
60534f243e9SHans de Goede 		 * This entry must be last because it is generic, this allows
60634f243e9SHans de Goede 		 * adding more specifuc quirks overriding this generic entry.
60734f243e9SHans de Goede 		 */
6083a06b912SHans de Goede 		.matches = {
6093a06b912SHans de Goede 			DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"),
6103a06b912SHans de Goede 			DMI_MATCH(DMI_CHASSIS_TYPE, "3"),
6113a06b912SHans de Goede 			DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
6123a06b912SHans de Goede 		},
61334f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
6143a06b912SHans de Goede 	},
615b60c75b6SHans de Goede 	{}
616b60c75b6SHans de Goede };
617b60c75b6SHans de Goede 
axp288_fuel_gauge_read_initial_regs(struct axp288_fg_info * info)6180b80eb6cSHans de Goede static int axp288_fuel_gauge_read_initial_regs(struct axp288_fg_info *info)
6190b80eb6cSHans de Goede {
6200b80eb6cSHans de Goede 	unsigned int val;
6210b80eb6cSHans de Goede 	int ret;
6220b80eb6cSHans de Goede 
6230b80eb6cSHans de Goede 	/*
6240b80eb6cSHans de Goede 	 * On some devices the fuelgauge and charger parts of the axp288 are
6250b80eb6cSHans de Goede 	 * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
6260b80eb6cSHans de Goede 	 */
6270b80eb6cSHans de Goede 	ret = regmap_read(info->regmap, AXP20X_CC_CTRL, &val);
6280b80eb6cSHans de Goede 	if (ret < 0)
6290b80eb6cSHans de Goede 		return ret;
6300b80eb6cSHans de Goede 	if (val == 0)
6310b80eb6cSHans de Goede 		return -ENODEV;
6320b80eb6cSHans de Goede 
6330b80eb6cSHans de Goede 	ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG);
6340b80eb6cSHans de Goede 	if (ret < 0)
6350b80eb6cSHans de Goede 		return ret;
6360b80eb6cSHans de Goede 
6370b80eb6cSHans de Goede 	if (!(ret & FG_DES_CAP1_VALID)) {
6380b80eb6cSHans de Goede 		dev_err(info->dev, "axp288 not configured by firmware\n");
6390b80eb6cSHans de Goede 		return -ENODEV;
6400b80eb6cSHans de Goede 	}
6410b80eb6cSHans de Goede 
6420b80eb6cSHans de Goede 	ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1);
6430b80eb6cSHans de Goede 	if (ret < 0)
6440b80eb6cSHans de Goede 		return ret;
6450b80eb6cSHans de Goede 	switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) {
6460b80eb6cSHans de Goede 	case CHRG_CCCV_CV_4100MV:
6470b80eb6cSHans de Goede 		info->max_volt = 4100;
6480b80eb6cSHans de Goede 		break;
6490b80eb6cSHans de Goede 	case CHRG_CCCV_CV_4150MV:
6500b80eb6cSHans de Goede 		info->max_volt = 4150;
6510b80eb6cSHans de Goede 		break;
6520b80eb6cSHans de Goede 	case CHRG_CCCV_CV_4200MV:
6530b80eb6cSHans de Goede 		info->max_volt = 4200;
6540b80eb6cSHans de Goede 		break;
6550b80eb6cSHans de Goede 	case CHRG_CCCV_CV_4350MV:
6560b80eb6cSHans de Goede 		info->max_volt = 4350;
6570b80eb6cSHans de Goede 		break;
6580b80eb6cSHans de Goede 	}
6590b80eb6cSHans de Goede 
6600b80eb6cSHans de Goede 	ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE);
6610b80eb6cSHans de Goede 	if (ret < 0)
6620b80eb6cSHans de Goede 		return ret;
6630b80eb6cSHans de Goede 	info->pwr_op = ret;
6640b80eb6cSHans de Goede 
6650b80eb6cSHans de Goede 	ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
6660b80eb6cSHans de Goede 	if (ret < 0)
6670b80eb6cSHans de Goede 		return ret;
6680b80eb6cSHans de Goede 	info->low_cap = ret;
6690b80eb6cSHans de Goede 
6700b80eb6cSHans de Goede 	return 0;
6710b80eb6cSHans de Goede }
6720b80eb6cSHans de Goede 
axp288_fuel_gauge_release_iio_chans(void * data)67336010866SHans de Goede static void axp288_fuel_gauge_release_iio_chans(void *data)
67436010866SHans de Goede {
67536010866SHans de Goede 	struct axp288_fg_info *info = data;
67636010866SHans de Goede 	int i;
67736010866SHans de Goede 
67836010866SHans de Goede 	for (i = 0; i < IIO_CHANNEL_NUM; i++)
67936010866SHans de Goede 		if (!IS_ERR_OR_NULL(info->iio_channel[i]))
68036010866SHans de Goede 			iio_channel_release(info->iio_channel[i]);
68136010866SHans de Goede }
68236010866SHans de Goede 
axp288_fuel_gauge_probe(struct platform_device * pdev)6838c0984e5SSebastian Reichel static int axp288_fuel_gauge_probe(struct platform_device *pdev)
6848c0984e5SSebastian Reichel {
6858c0984e5SSebastian Reichel 	struct axp288_fg_info *info;
6868c0984e5SSebastian Reichel 	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
6878c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
688331645e1SHans de Goede 	static const char * const iio_chan_name[] = {
689331645e1SHans de Goede 		[BAT_CHRG_CURR] = "axp288-chrg-curr",
690331645e1SHans de Goede 		[BAT_D_CURR] = "axp288-chrg-d-curr",
691331645e1SHans de Goede 		[BAT_VOLT] = "axp288-batt-volt",
692331645e1SHans de Goede 	};
69334f243e9SHans de Goede 	const struct dmi_system_id *dmi_id;
694b2657167SHans de Goede 	struct device *dev = &pdev->dev;
69534f243e9SHans de Goede 	unsigned long quirks = 0;
69605972f53SHans de Goede 	int i, pirq, ret;
6978c0984e5SSebastian Reichel 
698da365db7SHans de Goede 	/*
699da365db7SHans de Goede 	 * Normally the native AXP288 fg/charger drivers are preferred but
700da365db7SHans de Goede 	 * on some devices the ACPI drivers should be used instead.
701da365db7SHans de Goede 	 */
702da365db7SHans de Goede 	if (!acpi_quirk_skip_acpi_ac_and_battery())
703da365db7SHans de Goede 		return -ENODEV;
704da365db7SHans de Goede 
70534f243e9SHans de Goede 	dmi_id = dmi_first_match(axp288_quirks);
70634f243e9SHans de Goede 	if (dmi_id)
70734f243e9SHans de Goede 		quirks = (unsigned long)dmi_id->driver_data;
70834f243e9SHans de Goede 
70934f243e9SHans de Goede 	if (quirks & AXP288_QUIRK_NO_BATTERY)
710b60c75b6SHans de Goede 		return -ENODEV;
711b60c75b6SHans de Goede 
712b2657167SHans de Goede 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
7138c0984e5SSebastian Reichel 	if (!info)
7148c0984e5SSebastian Reichel 		return -ENOMEM;
7158c0984e5SSebastian Reichel 
716b2657167SHans de Goede 	info->dev = dev;
7178c0984e5SSebastian Reichel 	info->regmap = axp20x->regmap;
7188c0984e5SSebastian Reichel 	info->status = POWER_SUPPLY_STATUS_UNKNOWN;
719394088f0SAndrejus Basovas 	info->valid = 0;
7208c0984e5SSebastian Reichel 
7218c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, info);
7228c0984e5SSebastian Reichel 
7238c0984e5SSebastian Reichel 	mutex_init(&info->lock);
7248c0984e5SSebastian Reichel 
72505972f53SHans de Goede 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
72605972f53SHans de Goede 		pirq = platform_get_irq(pdev, i);
727bf6c880dSDenis Arefev 		if (pirq < 0)
728bf6c880dSDenis Arefev 			continue;
72905972f53SHans de Goede 		ret = regmap_irq_get_virq(axp20x->regmap_irqc, pirq);
73005972f53SHans de Goede 		if (ret < 0)
73105972f53SHans de Goede 			return dev_err_probe(dev, ret, "getting vIRQ %d\n", pirq);
73205972f53SHans de Goede 
73305972f53SHans de Goede 		info->irq[i] = ret;
73405972f53SHans de Goede 	}
73505972f53SHans de Goede 
736331645e1SHans de Goede 	for (i = 0; i < IIO_CHANNEL_NUM; i++) {
737331645e1SHans de Goede 		/*
738331645e1SHans de Goede 		 * Note cannot use devm_iio_channel_get because x86 systems
739331645e1SHans de Goede 		 * lack the device<->channel maps which iio_channel_get will
740331645e1SHans de Goede 		 * try to use when passed a non NULL device pointer.
741331645e1SHans de Goede 		 */
742331645e1SHans de Goede 		info->iio_channel[i] =
743331645e1SHans de Goede 			iio_channel_get(NULL, iio_chan_name[i]);
744331645e1SHans de Goede 		if (IS_ERR(info->iio_channel[i])) {
745331645e1SHans de Goede 			ret = PTR_ERR(info->iio_channel[i]);
746b2657167SHans de Goede 			dev_dbg(dev, "error getting iiochan %s: %d\n", iio_chan_name[i], ret);
747331645e1SHans de Goede 			/* Wait for axp288_adc to load */
748331645e1SHans de Goede 			if (ret == -ENODEV)
749331645e1SHans de Goede 				ret = -EPROBE_DEFER;
750331645e1SHans de Goede 
75136010866SHans de Goede 			axp288_fuel_gauge_release_iio_chans(info);
75236010866SHans de Goede 			return ret;
753331645e1SHans de Goede 		}
754331645e1SHans de Goede 	}
755331645e1SHans de Goede 
75636010866SHans de Goede 	ret = devm_add_action_or_reset(dev, axp288_fuel_gauge_release_iio_chans, info);
75736010866SHans de Goede 	if (ret)
75836010866SHans de Goede 		return ret;
75936010866SHans de Goede 
760213e19d6SHans de Goede 	ret = iosf_mbi_block_punit_i2c_access();
761213e19d6SHans de Goede 	if (ret < 0)
76236010866SHans de Goede 		return ret;
763213e19d6SHans de Goede 
7640b80eb6cSHans de Goede 	ret = axp288_fuel_gauge_read_initial_regs(info);
765213e19d6SHans de Goede 	iosf_mbi_unblock_punit_i2c_access();
766213e19d6SHans de Goede 	if (ret < 0)
76736010866SHans de Goede 		return ret;
768213e19d6SHans de Goede 
7698c0984e5SSebastian Reichel 	psy_cfg.drv_data = info;
770210bc22cSHans de Goede 	if (no_current_sense_res)
771210bc22cSHans de Goede 		fuel_gauge_desc.num_properties = ARRAY_SIZE(fuel_gauge_props) - 3;
772f1b7e088SHans de Goede 	info->bat = devm_power_supply_register(dev, &fuel_gauge_desc, &psy_cfg);
7738c0984e5SSebastian Reichel 	if (IS_ERR(info->bat)) {
7748c0984e5SSebastian Reichel 		ret = PTR_ERR(info->bat);
775b2657167SHans de Goede 		dev_err(dev, "failed to register battery: %d\n", ret);
77636010866SHans de Goede 		return ret;
7778c0984e5SSebastian Reichel 	}
7788c0984e5SSebastian Reichel 
77905972f53SHans de Goede 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
78005972f53SHans de Goede 		ret = devm_request_threaded_irq(dev, info->irq[i], NULL,
78105972f53SHans de Goede 						fuel_gauge_thread_handler,
78205972f53SHans de Goede 						IRQF_ONESHOT, DEV_NAME, info);
78305972f53SHans de Goede 		if (ret)
78405972f53SHans de Goede 			return dev_err_probe(dev, ret, "requesting IRQ %d\n", info->irq[i]);
78505972f53SHans de Goede 	}
7868c0984e5SSebastian Reichel 
787888f9743SHans de Goede 	return 0;
7888c0984e5SSebastian Reichel }
7898c0984e5SSebastian Reichel 
7908c0984e5SSebastian Reichel static const struct platform_device_id axp288_fg_id_table[] = {
7918c0984e5SSebastian Reichel 	{ .name = DEV_NAME },
7928c0984e5SSebastian Reichel 	{},
7938c0984e5SSebastian Reichel };
79499e33fbdSJavier Martinez Canillas MODULE_DEVICE_TABLE(platform, axp288_fg_id_table);
7958c0984e5SSebastian Reichel 
7968c0984e5SSebastian Reichel static struct platform_driver axp288_fuel_gauge_driver = {
7978c0984e5SSebastian Reichel 	.probe = axp288_fuel_gauge_probe,
7988c0984e5SSebastian Reichel 	.id_table = axp288_fg_id_table,
7998c0984e5SSebastian Reichel 	.driver = {
8008c0984e5SSebastian Reichel 		.name = DEV_NAME,
8018c0984e5SSebastian Reichel 	},
8028c0984e5SSebastian Reichel };
8038c0984e5SSebastian Reichel 
8048c0984e5SSebastian Reichel module_platform_driver(axp288_fuel_gauge_driver);
8058c0984e5SSebastian Reichel 
8068c0984e5SSebastian Reichel MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
8078c0984e5SSebastian Reichel MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>");
8088c0984e5SSebastian Reichel MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver");
8098c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
810