xref: /linux/drivers/power/supply/axp288_fuel_gauge.c (revision c771600c6af14749609b49565ffb4cac2959710d)
18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25a5bf490STodd Brandt /*
35a5bf490STodd Brandt  * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver
45a5bf490STodd Brandt  *
5394088f0SAndrejus Basovas  * Copyright (C) 2020-2021 Andrejus Basovas <xxx@yyy.tld>
6394088f0SAndrejus Basovas  * Copyright (C) 2016-2021 Hans de Goede <hdegoede@redhat.com>
75a5bf490STodd Brandt  * Copyright (C) 2014 Intel Corporation
85a5bf490STodd Brandt  *
95a5bf490STodd Brandt  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105a5bf490STodd Brandt  */
115a5bf490STodd Brandt 
12da365db7SHans de Goede #include <linux/acpi.h>
13b60c75b6SHans de Goede #include <linux/dmi.h>
145a5bf490STodd Brandt #include <linux/module.h>
155a5bf490STodd Brandt #include <linux/kernel.h>
165a5bf490STodd Brandt #include <linux/device.h>
175a5bf490STodd Brandt #include <linux/regmap.h>
185a5bf490STodd Brandt #include <linux/jiffies.h>
195a5bf490STodd Brandt #include <linux/interrupt.h>
205a5bf490STodd Brandt #include <linux/mfd/axp20x.h>
215a5bf490STodd Brandt #include <linux/platform_device.h>
225a5bf490STodd Brandt #include <linux/power_supply.h>
235a5bf490STodd Brandt #include <linux/iio/consumer.h>
245f60d5f6SAl Viro #include <linux/unaligned.h>
25394088f0SAndrejus Basovas #include <asm/iosf_mbi.h>
265a5bf490STodd Brandt 
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 
335a5bf490STodd Brandt #define CHRG_STAT_BAT_SAFE_MODE			(1 << 3)
345a5bf490STodd Brandt #define CHRG_STAT_BAT_VALID			(1 << 4)
355a5bf490STodd Brandt #define CHRG_STAT_BAT_PRESENT			(1 << 5)
365a5bf490STodd Brandt #define CHRG_STAT_CHARGING			(1 << 6)
375a5bf490STodd Brandt #define CHRG_STAT_PMIC_OTP			(1 << 7)
385a5bf490STodd Brandt 
395a5bf490STodd Brandt #define CHRG_CCCV_CC_MASK			0xf     /* 4 bits */
405a5bf490STodd Brandt #define CHRG_CCCV_CC_BIT_POS			0
415a5bf490STodd Brandt #define CHRG_CCCV_CC_OFFSET			200     /* 200mA */
425a5bf490STodd Brandt #define CHRG_CCCV_CC_LSB_RES			200     /* 200mA */
435a5bf490STodd Brandt #define CHRG_CCCV_ITERM_20P			(1 << 4)    /* 20% of CC */
445a5bf490STodd Brandt #define CHRG_CCCV_CV_MASK			0x60        /* 2 bits */
455a5bf490STodd Brandt #define CHRG_CCCV_CV_BIT_POS			5
465a5bf490STodd Brandt #define CHRG_CCCV_CV_4100MV			0x0     /* 4.10V */
475a5bf490STodd Brandt #define CHRG_CCCV_CV_4150MV			0x1     /* 4.15V */
485a5bf490STodd Brandt #define CHRG_CCCV_CV_4200MV			0x2     /* 4.20V */
495a5bf490STodd Brandt #define CHRG_CCCV_CV_4350MV			0x3     /* 4.35V */
505a5bf490STodd Brandt #define CHRG_CCCV_CHG_EN			(1 << 7)
515a5bf490STodd Brandt 
525a5bf490STodd Brandt #define FG_CNTL_OCV_ADJ_STAT			(1 << 2)
535a5bf490STodd Brandt #define FG_CNTL_OCV_ADJ_EN			(1 << 3)
545a5bf490STodd Brandt #define FG_CNTL_CAP_ADJ_STAT			(1 << 4)
555a5bf490STodd Brandt #define FG_CNTL_CAP_ADJ_EN			(1 << 5)
565a5bf490STodd Brandt #define FG_CNTL_CC_EN				(1 << 6)
575a5bf490STodd Brandt #define FG_CNTL_GAUGE_EN			(1 << 7)
585a5bf490STodd Brandt 
594949fc5eSHans de Goede #define FG_15BIT_WORD_VALID			(1 << 15)
604949fc5eSHans de Goede #define FG_15BIT_VAL_MASK			0x7fff
614949fc5eSHans de Goede 
625a5bf490STodd Brandt #define FG_REP_CAP_VALID			(1 << 7)
635a5bf490STodd Brandt #define FG_REP_CAP_VAL_MASK			0x7F
645a5bf490STodd Brandt 
655a5bf490STodd Brandt #define FG_DES_CAP1_VALID			(1 << 7)
665a5bf490STodd Brandt #define FG_DES_CAP_RES_LSB			1456    /* 1.456mAhr */
675a5bf490STodd Brandt 
685a5bf490STodd Brandt #define FG_DES_CC_RES_LSB			1456    /* 1.456mAhr */
695a5bf490STodd Brandt 
705a5bf490STodd Brandt #define FG_OCV_CAP_VALID			(1 << 7)
715a5bf490STodd Brandt #define FG_OCV_CAP_VAL_MASK			0x7F
725a5bf490STodd Brandt #define FG_CC_CAP_VALID				(1 << 7)
735a5bf490STodd Brandt #define FG_CC_CAP_VAL_MASK			0x7F
745a5bf490STodd Brandt 
755a5bf490STodd Brandt #define FG_LOW_CAP_THR1_MASK			0xf0    /* 5% tp 20% */
765a5bf490STodd Brandt #define FG_LOW_CAP_THR1_VAL			0xa0    /* 15 perc */
775a5bf490STodd Brandt #define FG_LOW_CAP_THR2_MASK			0x0f    /* 0% to 15% */
785a5bf490STodd Brandt #define FG_LOW_CAP_WARN_THR			14  /* 14 perc */
795a5bf490STodd Brandt #define FG_LOW_CAP_CRIT_THR			4   /* 4 perc */
805a5bf490STodd Brandt #define FG_LOW_CAP_SHDN_THR			0   /* 0 perc */
815a5bf490STodd Brandt 
825a5bf490STodd Brandt #define DEV_NAME				"axp288_fuel_gauge"
835a5bf490STodd Brandt 
845a5bf490STodd Brandt /* 1.1mV per LSB expressed in uV */
855a5bf490STodd Brandt #define VOLTAGE_FROM_ADC(a)			((a * 11) / 10)
86888f9743SHans de Goede /* properties converted to uV, uA */
875a5bf490STodd Brandt #define PROP_VOLT(a)				((a) * 1000)
885a5bf490STodd Brandt #define PROP_CURR(a)				((a) * 1000)
895a5bf490STodd Brandt 
90394088f0SAndrejus Basovas #define AXP288_REG_UPDATE_INTERVAL		(60 * HZ)
915a5bf490STodd Brandt #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 
995a5bf490STodd Brandt enum {
1005a5bf490STodd Brandt 	QWBTU_IRQ = 0,
1015a5bf490STodd Brandt 	WBTU_IRQ,
1025a5bf490STodd Brandt 	QWBTO_IRQ,
1035a5bf490STodd Brandt 	WBTO_IRQ,
1045a5bf490STodd Brandt 	WL2_IRQ,
1055a5bf490STodd Brandt 	WL1_IRQ,
1065a5bf490STodd Brandt };
1075a5bf490STodd Brandt 
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 
1155a5bf490STodd Brandt struct axp288_fg_info {
1167eef3e66SHans de Goede 	struct device *dev;
1175a5bf490STodd Brandt 	struct regmap *regmap;
1185a5bf490STodd Brandt 	int irq[AXP288_FG_INTR_NUM];
119331645e1SHans de Goede 	struct iio_channel *iio_channel[IIO_CHANNEL_NUM];
120297d716fSKrzysztof Kozlowski 	struct power_supply *bat;
1215a5bf490STodd Brandt 	struct mutex lock;
1225a5bf490STodd Brandt 	int status;
123888f9743SHans de Goede 	int max_volt;
124c371d449SHans de Goede 	int pwr_op;
125c371d449SHans de Goede 	int low_cap;
1265a5bf490STodd Brandt 	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;
1395a5bf490STodd Brandt };
1405a5bf490STodd Brandt 
1415a5bf490STodd Brandt static enum power_supply_property fuel_gauge_props[] = {
1425a5bf490STodd Brandt 	POWER_SUPPLY_PROP_STATUS,
1435a5bf490STodd Brandt 	POWER_SUPPLY_PROP_PRESENT,
1445a5bf490STodd Brandt 	POWER_SUPPLY_PROP_HEALTH,
1455a5bf490STodd Brandt 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1465a5bf490STodd Brandt 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
1475a5bf490STodd Brandt 	POWER_SUPPLY_PROP_VOLTAGE_OCV,
1485a5bf490STodd Brandt 	POWER_SUPPLY_PROP_CAPACITY,
1495a5bf490STodd Brandt 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
1505a5bf490STodd Brandt 	POWER_SUPPLY_PROP_TECHNOLOGY,
151210bc22cSHans de Goede 	/* The 3 props below are not used when no_current_sense_res is set */
1525a5bf490STodd Brandt 	POWER_SUPPLY_PROP_CHARGE_FULL,
1535a5bf490STodd Brandt 	POWER_SUPPLY_PROP_CHARGE_NOW,
154210bc22cSHans de Goede 	POWER_SUPPLY_PROP_CURRENT_NOW,
1555a5bf490STodd Brandt };
1565a5bf490STodd Brandt 
fuel_gauge_reg_readb(struct axp288_fg_info * info,int reg)1575a5bf490STodd Brandt static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg)
1585a5bf490STodd Brandt {
1595a5bf490STodd Brandt 	unsigned int val;
160f17bda7fSHans de Goede 	int ret;
1615a5bf490STodd Brandt 
1625a5bf490STodd Brandt 	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 	}
1675a5bf490STodd Brandt 
1685a5bf490STodd Brandt 	return val;
1695a5bf490STodd Brandt }
1705a5bf490STodd Brandt 
fuel_gauge_reg_writeb(struct axp288_fg_info * info,int reg,u8 val)1715a5bf490STodd Brandt static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val)
1725a5bf490STodd Brandt {
1735a5bf490STodd Brandt 	int ret;
1745a5bf490STodd Brandt 
1755a5bf490STodd Brandt 	ret = regmap_write(info->regmap, reg, (unsigned int)val);
1765a5bf490STodd Brandt 
1775a5bf490STodd Brandt 	if (ret < 0)
1787eef3e66SHans de Goede 		dev_err(info->dev, "Error writing reg 0x%02x err: %d\n", reg, ret);
1795a5bf490STodd Brandt 
1805a5bf490STodd Brandt 	return ret;
1815a5bf490STodd Brandt }
1825a5bf490STodd Brandt 
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)2875a5bf490STodd Brandt static void fuel_gauge_get_status(struct axp288_fg_info *info)
2885a5bf490STodd Brandt {
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;
3035a5bf490STodd Brandt 		return;
3045a5bf490STodd Brandt 	}
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 	}
3185a5bf490STodd Brandt 
319f451655cSHans de Goede not_full:
3202b5a4b4bSHans de Goede 	if (pwr_stat & PS_STAT_BAT_CHRG_DIR)
3215a5bf490STodd Brandt 		info->status = POWER_SUPPLY_STATUS_CHARGING;
3225a5bf490STodd Brandt 	else
3232b5a4b4bSHans de Goede 		info->status = POWER_SUPPLY_STATUS_DISCHARGING;
3245a5bf490STodd Brandt }
3255a5bf490STodd Brandt 
fuel_gauge_battery_health(struct axp288_fg_info * info)3265a5bf490STodd Brandt static int fuel_gauge_battery_health(struct axp288_fg_info *info)
3275a5bf490STodd Brandt {
328394088f0SAndrejus Basovas 	int vocv = VOLTAGE_FROM_ADC(info->ocv);
329394088f0SAndrejus Basovas 	int health = POWER_SUPPLY_HEALTH_UNKNOWN;
3305a5bf490STodd Brandt 
331888f9743SHans de Goede 	if (vocv > info->max_volt)
3325a5bf490STodd Brandt 		health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
3335a5bf490STodd Brandt 	else
3345a5bf490STodd Brandt 		health = POWER_SUPPLY_HEALTH_GOOD;
3355a5bf490STodd Brandt 
3365a5bf490STodd Brandt 	return health;
3375a5bf490STodd Brandt }
3385a5bf490STodd Brandt 
fuel_gauge_get_property(struct power_supply * ps,enum power_supply_property prop,union power_supply_propval * val)3395a5bf490STodd Brandt static int fuel_gauge_get_property(struct power_supply *ps,
3405a5bf490STodd Brandt 		enum power_supply_property prop,
3415a5bf490STodd Brandt 		union power_supply_propval *val)
3425a5bf490STodd Brandt {
343297d716fSKrzysztof Kozlowski 	struct axp288_fg_info *info = power_supply_get_drvdata(ps);
344394088f0SAndrejus Basovas 	int ret, value;
3455a5bf490STodd Brandt 
3465a5bf490STodd Brandt 	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 
3525a5bf490STodd Brandt 	switch (prop) {
3535a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_STATUS:
3545a5bf490STodd Brandt 		fuel_gauge_get_status(info);
3555a5bf490STodd Brandt 		val->intval = info->status;
3565a5bf490STodd Brandt 		break;
3575a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_HEALTH:
3585a5bf490STodd Brandt 		val->intval = fuel_gauge_battery_health(info);
3595a5bf490STodd Brandt 		break;
3605a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
361394088f0SAndrejus Basovas 		value = VOLTAGE_FROM_ADC(info->bat_volt);
3625a5bf490STodd Brandt 		val->intval = PROP_VOLT(value);
3635a5bf490STodd Brandt 		break;
3645a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
365394088f0SAndrejus Basovas 		value = VOLTAGE_FROM_ADC(info->ocv);
3665a5bf490STodd Brandt 		val->intval = PROP_VOLT(value);
3675a5bf490STodd Brandt 		break;
3685a5bf490STodd Brandt 	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 
3745a5bf490STodd Brandt 		val->intval = PROP_CURR(value);
3755a5bf490STodd Brandt 		break;
3765a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_PRESENT:
377c371d449SHans de Goede 		if (info->pwr_op & CHRG_STAT_BAT_PRESENT)
3785a5bf490STodd Brandt 			val->intval = 1;
3795a5bf490STodd Brandt 		else
3805a5bf490STodd Brandt 			val->intval = 0;
3815a5bf490STodd Brandt 		break;
3825a5bf490STodd Brandt 	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);
3865a5bf490STodd Brandt 		break;
3875a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
388c371d449SHans de Goede 		val->intval = (info->low_cap & 0x0f);
3895a5bf490STodd Brandt 		break;
3905a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_TECHNOLOGY:
3915a5bf490STodd Brandt 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
3925a5bf490STodd Brandt 		break;
3935a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_CHARGE_NOW:
394394088f0SAndrejus Basovas 		val->intval = info->fg_cc_mtr1 * FG_DES_CAP_RES_LSB;
3955a5bf490STodd Brandt 		break;
3965a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_CHARGE_FULL:
397394088f0SAndrejus Basovas 		val->intval = info->fg_des_cap1 * FG_DES_CAP_RES_LSB;
3985a5bf490STodd Brandt 		break;
3995a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
400888f9743SHans de Goede 		val->intval = PROP_VOLT(info->max_volt);
4015a5bf490STodd Brandt 		break;
4025a5bf490STodd Brandt 	default:
403394088f0SAndrejus Basovas 		ret = -EINVAL;
4045a5bf490STodd Brandt 	}
4055a5bf490STodd Brandt 
406394088f0SAndrejus Basovas out:
4075a5bf490STodd Brandt 	mutex_unlock(&info->lock);
4085a5bf490STodd Brandt 	return ret;
4095a5bf490STodd Brandt }
4105a5bf490STodd Brandt 
fuel_gauge_set_property(struct power_supply * ps,enum power_supply_property prop,const union power_supply_propval * val)4115a5bf490STodd Brandt static int fuel_gauge_set_property(struct power_supply *ps,
4125a5bf490STodd Brandt 		enum power_supply_property prop,
4135a5bf490STodd Brandt 		const union power_supply_propval *val)
4145a5bf490STodd Brandt {
415297d716fSKrzysztof Kozlowski 	struct axp288_fg_info *info = power_supply_get_drvdata(ps);
416c371d449SHans de Goede 	int new_low_cap, ret = 0;
4175a5bf490STodd Brandt 
4185a5bf490STodd Brandt 	mutex_lock(&info->lock);
4195a5bf490STodd Brandt 	switch (prop) {
4205a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
4215a5bf490STodd Brandt 		if ((val->intval < 0) || (val->intval > 15)) {
4225a5bf490STodd Brandt 			ret = -EINVAL;
4235a5bf490STodd Brandt 			break;
4245a5bf490STodd Brandt 		}
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;
4315a5bf490STodd Brandt 		break;
4325a5bf490STodd Brandt 	default:
4335a5bf490STodd Brandt 		ret = -EINVAL;
4345a5bf490STodd Brandt 		break;
4355a5bf490STodd Brandt 	}
4365a5bf490STodd Brandt 
4375a5bf490STodd Brandt 	mutex_unlock(&info->lock);
4385a5bf490STodd Brandt 	return ret;
4395a5bf490STodd Brandt }
4405a5bf490STodd Brandt 
fuel_gauge_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)4415a5bf490STodd Brandt static int fuel_gauge_property_is_writeable(struct power_supply *psy,
4425a5bf490STodd Brandt 	enum power_supply_property psp)
4435a5bf490STodd Brandt {
4445a5bf490STodd Brandt 	int ret;
4455a5bf490STodd Brandt 
4465a5bf490STodd Brandt 	switch (psp) {
4475a5bf490STodd Brandt 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
4485a5bf490STodd Brandt 		ret = 1;
4495a5bf490STodd Brandt 		break;
4505a5bf490STodd Brandt 	default:
4515a5bf490STodd Brandt 		ret = 0;
4525a5bf490STodd Brandt 	}
4535a5bf490STodd Brandt 
4545a5bf490STodd Brandt 	return ret;
4555a5bf490STodd Brandt }
4565a5bf490STodd Brandt 
fuel_gauge_thread_handler(int irq,void * dev)4575a5bf490STodd Brandt static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev)
4585a5bf490STodd Brandt {
4595a5bf490STodd Brandt 	struct axp288_fg_info *info = dev;
4605a5bf490STodd Brandt 	int i;
4615a5bf490STodd Brandt 
4625a5bf490STodd Brandt 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
4635a5bf490STodd Brandt 		if (info->irq[i] == irq)
4645a5bf490STodd Brandt 			break;
4655a5bf490STodd Brandt 	}
4665a5bf490STodd Brandt 
4675a5bf490STodd Brandt 	if (i >= AXP288_FG_INTR_NUM) {
4687eef3e66SHans de Goede 		dev_warn(info->dev, "spurious interrupt!!\n");
4695a5bf490STodd Brandt 		return IRQ_NONE;
4705a5bf490STodd Brandt 	}
4715a5bf490STodd Brandt 
4725a5bf490STodd Brandt 	switch (i) {
4735a5bf490STodd Brandt 	case QWBTU_IRQ:
4747eef3e66SHans de Goede 		dev_info(info->dev, "Quit Battery under temperature in work mode IRQ (QWBTU)\n");
4755a5bf490STodd Brandt 		break;
4765a5bf490STodd Brandt 	case WBTU_IRQ:
4777eef3e66SHans de Goede 		dev_info(info->dev, "Battery under temperature in work mode IRQ (WBTU)\n");
4785a5bf490STodd Brandt 		break;
4795a5bf490STodd Brandt 	case QWBTO_IRQ:
4807eef3e66SHans de Goede 		dev_info(info->dev, "Quit Battery over temperature in work mode IRQ (QWBTO)\n");
4815a5bf490STodd Brandt 		break;
4825a5bf490STodd Brandt 	case WBTO_IRQ:
4837eef3e66SHans de Goede 		dev_info(info->dev, "Battery over temperature in work mode IRQ (WBTO)\n");
4845a5bf490STodd Brandt 		break;
4855a5bf490STodd Brandt 	case WL2_IRQ:
4867eef3e66SHans de Goede 		dev_info(info->dev, "Low Batt Warning(2) INTR\n");
4875a5bf490STodd Brandt 		break;
4885a5bf490STodd Brandt 	case WL1_IRQ:
4897eef3e66SHans de Goede 		dev_info(info->dev, "Low Batt Warning(1) INTR\n");
4905a5bf490STodd Brandt 		break;
4915a5bf490STodd Brandt 	default:
4927eef3e66SHans de Goede 		dev_warn(info->dev, "Spurious Interrupt!!!\n");
4935a5bf490STodd Brandt 	}
4945a5bf490STodd Brandt 
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 
499297d716fSKrzysztof Kozlowski 	power_supply_changed(info->bat);
5005a5bf490STodd Brandt 	return IRQ_HANDLED;
5015a5bf490STodd Brandt }
5025a5bf490STodd Brandt 
fuel_gauge_external_power_changed(struct power_supply * psy)5035a5bf490STodd Brandt static void fuel_gauge_external_power_changed(struct power_supply *psy)
5045a5bf490STodd Brandt {
505297d716fSKrzysztof Kozlowski 	struct axp288_fg_info *info = power_supply_get_drvdata(psy);
5065a5bf490STodd Brandt 
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);
510f8319774SHans de Goede 	power_supply_changed(psy);
5115a5bf490STodd Brandt }
5125a5bf490STodd Brandt 
513210bc22cSHans de Goede static struct power_supply_desc fuel_gauge_desc = {
514297d716fSKrzysztof Kozlowski 	.name			= DEV_NAME,
515297d716fSKrzysztof Kozlowski 	.type			= POWER_SUPPLY_TYPE_BATTERY,
516297d716fSKrzysztof Kozlowski 	.properties		= fuel_gauge_props,
517297d716fSKrzysztof Kozlowski 	.num_properties		= ARRAY_SIZE(fuel_gauge_props),
518297d716fSKrzysztof Kozlowski 	.get_property		= fuel_gauge_get_property,
519297d716fSKrzysztof Kozlowski 	.set_property		= fuel_gauge_set_property,
520297d716fSKrzysztof Kozlowski 	.property_is_writeable	= fuel_gauge_property_is_writeable,
521297d716fSKrzysztof Kozlowski 	.external_power_changed	= fuel_gauge_external_power_changed,
522297d716fSKrzysztof Kozlowski };
523297d716fSKrzysztof Kozlowski 
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 	{
553a9b25489SHans de Goede 		/* Intel Bay Trail Compute Stick */
554b60c75b6SHans de Goede 		.matches = {
555e42fe5b2SJeffery Miller 			DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
556a9b25489SHans de Goede 			/* Partial match for STCK1A32WFC STCK1A32FC, STCK1A8LFC variants */
557a9b25489SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "STCK1A"),
558b60c75b6SHans de Goede 		},
55934f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
560b60c75b6SHans de Goede 	},
561b60c75b6SHans de Goede 	{
562a9b25489SHans de Goede 		/* Intel Cherry Trail Compute Stick */
563b60c75b6SHans de Goede 		.matches = {
564e42fe5b2SJeffery Miller 			DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
565a9b25489SHans de Goede 			/* Partial match for STK1AW32SC and STK1A32SC variants */
566a9b25489SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "STK1A"),
567b60c75b6SHans de Goede 		},
56834f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
569b60c75b6SHans de Goede 	},
570b60c75b6SHans de Goede 	{
5714ac54b88SRafael Gandolfi 		/* Meegopad T02 */
5724ac54b88SRafael Gandolfi 		.matches = {
5734ac54b88SRafael Gandolfi 			DMI_MATCH(DMI_PRODUCT_NAME, "MEEGOPAD T02"),
5744ac54b88SRafael Gandolfi 		},
57534f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
5764ac54b88SRafael Gandolfi 	},
5776b714ea4SHans de Goede 	{	/* Mele PCG03 Mini PC */
5786b714ea4SHans de Goede 		.matches = {
5796b714ea4SHans de Goede 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
5806b714ea4SHans de Goede 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
5816b714ea4SHans de Goede 		},
58234f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
5836b714ea4SHans de Goede 	},
584fa7da744SHans de Goede 	{
585fa7da744SHans de Goede 		/* Minix Neo Z83-4 mini PC */
586fa7da744SHans de Goede 		.matches = {
587fa7da744SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "MINIX"),
588fa7da744SHans de Goede 			DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
58934f243e9SHans de Goede 		},
59034f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
591fa7da744SHans de Goede 	},
5923a06b912SHans de Goede 	{
59334f243e9SHans de Goede 		/*
59434f243e9SHans de Goede 		 * One Mix 1, this uses the "T3 MRD" boardname used by
59534f243e9SHans de Goede 		 * generic mini PCs, but it is a mini laptop so it does
59634f243e9SHans de Goede 		 * actually have a battery!
59734f243e9SHans de Goede 		 */
59834f243e9SHans de Goede 		.matches = {
59934f243e9SHans de Goede 			DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"),
60034f243e9SHans de Goede 			DMI_MATCH(DMI_BIOS_DATE, "06/14/2018"),
60134f243e9SHans de Goede 		},
60234f243e9SHans de Goede 		.driver_data = NULL,
60334f243e9SHans de Goede 	},
60434f243e9SHans de Goede 	{
6059e6047c0SGuoyi Zhang 		/* Radxa ROCK Pi X Single Board Computer */
6069e6047c0SGuoyi Zhang 		.matches = {
6079e6047c0SGuoyi Zhang 			DMI_MATCH(DMI_BOARD_NAME, "ROCK Pi X"),
6089e6047c0SGuoyi Zhang 			DMI_MATCH(DMI_BOARD_VENDOR, "Radxa"),
6099e6047c0SGuoyi Zhang 		},
6109e6047c0SGuoyi Zhang 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
6119e6047c0SGuoyi Zhang 	},
6129e6047c0SGuoyi Zhang 	{
61334f243e9SHans de Goede 		/*
61434f243e9SHans de Goede 		 * Various Ace PC/Meegopad/MinisForum/Wintel Mini-PCs/HDMI-sticks
61534f243e9SHans de Goede 		 * This entry must be last because it is generic, this allows
61634f243e9SHans de Goede 		 * adding more specifuc quirks overriding this generic entry.
61734f243e9SHans de Goede 		 */
6183a06b912SHans de Goede 		.matches = {
6193a06b912SHans de Goede 			DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"),
6203a06b912SHans de Goede 			DMI_MATCH(DMI_CHASSIS_TYPE, "3"),
6213a06b912SHans de Goede 			DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
6223a06b912SHans de Goede 		},
62334f243e9SHans de Goede 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
6243a06b912SHans de Goede 	},
625b60c75b6SHans de Goede 	{}
626b60c75b6SHans de Goede };
627b60c75b6SHans de Goede 
axp288_fuel_gauge_read_initial_regs(struct axp288_fg_info * info)6280b80eb6cSHans de Goede static int axp288_fuel_gauge_read_initial_regs(struct axp288_fg_info *info)
6290b80eb6cSHans de Goede {
6300b80eb6cSHans de Goede 	unsigned int val;
6310b80eb6cSHans de Goede 	int ret;
6320b80eb6cSHans de Goede 
6330b80eb6cSHans de Goede 	/*
6340b80eb6cSHans de Goede 	 * On some devices the fuelgauge and charger parts of the axp288 are
6350b80eb6cSHans de Goede 	 * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
6360b80eb6cSHans de Goede 	 */
6370b80eb6cSHans de Goede 	ret = regmap_read(info->regmap, AXP20X_CC_CTRL, &val);
6380b80eb6cSHans de Goede 	if (ret < 0)
6390b80eb6cSHans de Goede 		return ret;
6400b80eb6cSHans de Goede 	if (val == 0)
6410b80eb6cSHans de Goede 		return -ENODEV;
6420b80eb6cSHans de Goede 
6430b80eb6cSHans de Goede 	ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG);
6440b80eb6cSHans de Goede 	if (ret < 0)
6450b80eb6cSHans de Goede 		return ret;
6460b80eb6cSHans de Goede 
6470b80eb6cSHans de Goede 	if (!(ret & FG_DES_CAP1_VALID)) {
6480b80eb6cSHans de Goede 		dev_err(info->dev, "axp288 not configured by firmware\n");
6490b80eb6cSHans de Goede 		return -ENODEV;
6500b80eb6cSHans de Goede 	}
6510b80eb6cSHans de Goede 
6520b80eb6cSHans de Goede 	ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1);
6530b80eb6cSHans de Goede 	if (ret < 0)
6540b80eb6cSHans de Goede 		return ret;
6550b80eb6cSHans de Goede 	switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) {
6560b80eb6cSHans de Goede 	case CHRG_CCCV_CV_4100MV:
6570b80eb6cSHans de Goede 		info->max_volt = 4100;
6580b80eb6cSHans de Goede 		break;
6590b80eb6cSHans de Goede 	case CHRG_CCCV_CV_4150MV:
6600b80eb6cSHans de Goede 		info->max_volt = 4150;
6610b80eb6cSHans de Goede 		break;
6620b80eb6cSHans de Goede 	case CHRG_CCCV_CV_4200MV:
6630b80eb6cSHans de Goede 		info->max_volt = 4200;
6640b80eb6cSHans de Goede 		break;
6650b80eb6cSHans de Goede 	case CHRG_CCCV_CV_4350MV:
6660b80eb6cSHans de Goede 		info->max_volt = 4350;
6670b80eb6cSHans de Goede 		break;
6680b80eb6cSHans de Goede 	}
6690b80eb6cSHans de Goede 
6700b80eb6cSHans de Goede 	ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE);
6710b80eb6cSHans de Goede 	if (ret < 0)
6720b80eb6cSHans de Goede 		return ret;
6730b80eb6cSHans de Goede 	info->pwr_op = ret;
6740b80eb6cSHans de Goede 
6750b80eb6cSHans de Goede 	ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
6760b80eb6cSHans de Goede 	if (ret < 0)
6770b80eb6cSHans de Goede 		return ret;
6780b80eb6cSHans de Goede 	info->low_cap = ret;
6790b80eb6cSHans de Goede 
6800b80eb6cSHans de Goede 	return 0;
6810b80eb6cSHans de Goede }
6820b80eb6cSHans de Goede 
axp288_fuel_gauge_release_iio_chans(void * data)68336010866SHans de Goede static void axp288_fuel_gauge_release_iio_chans(void *data)
68436010866SHans de Goede {
68536010866SHans de Goede 	struct axp288_fg_info *info = data;
68636010866SHans de Goede 	int i;
68736010866SHans de Goede 
68836010866SHans de Goede 	for (i = 0; i < IIO_CHANNEL_NUM; i++)
68936010866SHans de Goede 		if (!IS_ERR_OR_NULL(info->iio_channel[i]))
69036010866SHans de Goede 			iio_channel_release(info->iio_channel[i]);
69136010866SHans de Goede }
69236010866SHans de Goede 
axp288_fuel_gauge_probe(struct platform_device * pdev)6935a5bf490STodd Brandt static int axp288_fuel_gauge_probe(struct platform_device *pdev)
6945a5bf490STodd Brandt {
6955a5bf490STodd Brandt 	struct axp288_fg_info *info;
6965a5bf490STodd Brandt 	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
697297d716fSKrzysztof Kozlowski 	struct power_supply_config psy_cfg = {};
698331645e1SHans de Goede 	static const char * const iio_chan_name[] = {
699331645e1SHans de Goede 		[BAT_CHRG_CURR] = "axp288-chrg-curr",
700331645e1SHans de Goede 		[BAT_D_CURR] = "axp288-chrg-d-curr",
701331645e1SHans de Goede 		[BAT_VOLT] = "axp288-batt-volt",
702331645e1SHans de Goede 	};
70334f243e9SHans de Goede 	const struct dmi_system_id *dmi_id;
704b2657167SHans de Goede 	struct device *dev = &pdev->dev;
70534f243e9SHans de Goede 	unsigned long quirks = 0;
70605972f53SHans de Goede 	int i, pirq, ret;
7075a5bf490STodd Brandt 
708da365db7SHans de Goede 	/*
709da365db7SHans de Goede 	 * Normally the native AXP288 fg/charger drivers are preferred but
710da365db7SHans de Goede 	 * on some devices the ACPI drivers should be used instead.
711da365db7SHans de Goede 	 */
712da365db7SHans de Goede 	if (!acpi_quirk_skip_acpi_ac_and_battery())
713da365db7SHans de Goede 		return -ENODEV;
714da365db7SHans de Goede 
71534f243e9SHans de Goede 	dmi_id = dmi_first_match(axp288_quirks);
71634f243e9SHans de Goede 	if (dmi_id)
71734f243e9SHans de Goede 		quirks = (unsigned long)dmi_id->driver_data;
71834f243e9SHans de Goede 
71934f243e9SHans de Goede 	if (quirks & AXP288_QUIRK_NO_BATTERY)
720b60c75b6SHans de Goede 		return -ENODEV;
721b60c75b6SHans de Goede 
722b2657167SHans de Goede 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
7235a5bf490STodd Brandt 	if (!info)
7245a5bf490STodd Brandt 		return -ENOMEM;
7255a5bf490STodd Brandt 
726b2657167SHans de Goede 	info->dev = dev;
7275a5bf490STodd Brandt 	info->regmap = axp20x->regmap;
7285a5bf490STodd Brandt 	info->status = POWER_SUPPLY_STATUS_UNKNOWN;
729394088f0SAndrejus Basovas 	info->valid = 0;
7305a5bf490STodd Brandt 
7315a5bf490STodd Brandt 	platform_set_drvdata(pdev, info);
7325a5bf490STodd Brandt 
7335a5bf490STodd Brandt 	mutex_init(&info->lock);
7345a5bf490STodd Brandt 
73505972f53SHans de Goede 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
73605972f53SHans de Goede 		pirq = platform_get_irq(pdev, i);
737bf6c880dSDenis Arefev 		if (pirq < 0)
738bf6c880dSDenis Arefev 			continue;
73905972f53SHans de Goede 		ret = regmap_irq_get_virq(axp20x->regmap_irqc, pirq);
74005972f53SHans de Goede 		if (ret < 0)
74105972f53SHans de Goede 			return dev_err_probe(dev, ret, "getting vIRQ %d\n", pirq);
74205972f53SHans de Goede 
74305972f53SHans de Goede 		info->irq[i] = ret;
74405972f53SHans de Goede 	}
74505972f53SHans de Goede 
746331645e1SHans de Goede 	for (i = 0; i < IIO_CHANNEL_NUM; i++) {
747331645e1SHans de Goede 		/*
748331645e1SHans de Goede 		 * Note cannot use devm_iio_channel_get because x86 systems
749331645e1SHans de Goede 		 * lack the device<->channel maps which iio_channel_get will
750331645e1SHans de Goede 		 * try to use when passed a non NULL device pointer.
751331645e1SHans de Goede 		 */
752331645e1SHans de Goede 		info->iio_channel[i] =
753331645e1SHans de Goede 			iio_channel_get(NULL, iio_chan_name[i]);
754331645e1SHans de Goede 		if (IS_ERR(info->iio_channel[i])) {
755331645e1SHans de Goede 			ret = PTR_ERR(info->iio_channel[i]);
756b2657167SHans de Goede 			dev_dbg(dev, "error getting iiochan %s: %d\n", iio_chan_name[i], ret);
757331645e1SHans de Goede 			/* Wait for axp288_adc to load */
758331645e1SHans de Goede 			if (ret == -ENODEV)
759331645e1SHans de Goede 				ret = -EPROBE_DEFER;
760331645e1SHans de Goede 
76136010866SHans de Goede 			axp288_fuel_gauge_release_iio_chans(info);
76236010866SHans de Goede 			return ret;
763331645e1SHans de Goede 		}
764331645e1SHans de Goede 	}
765331645e1SHans de Goede 
76636010866SHans de Goede 	ret = devm_add_action_or_reset(dev, axp288_fuel_gauge_release_iio_chans, info);
76736010866SHans de Goede 	if (ret)
76836010866SHans de Goede 		return ret;
76936010866SHans de Goede 
770213e19d6SHans de Goede 	ret = iosf_mbi_block_punit_i2c_access();
771213e19d6SHans de Goede 	if (ret < 0)
77236010866SHans de Goede 		return ret;
773213e19d6SHans de Goede 
7740b80eb6cSHans de Goede 	ret = axp288_fuel_gauge_read_initial_regs(info);
775213e19d6SHans de Goede 	iosf_mbi_unblock_punit_i2c_access();
776213e19d6SHans de Goede 	if (ret < 0)
77736010866SHans de Goede 		return ret;
778213e19d6SHans de Goede 
779297d716fSKrzysztof Kozlowski 	psy_cfg.drv_data = info;
780210bc22cSHans de Goede 	if (no_current_sense_res)
781210bc22cSHans de Goede 		fuel_gauge_desc.num_properties = ARRAY_SIZE(fuel_gauge_props) - 3;
782f1b7e088SHans de Goede 	info->bat = devm_power_supply_register(dev, &fuel_gauge_desc, &psy_cfg);
783297d716fSKrzysztof Kozlowski 	if (IS_ERR(info->bat)) {
784297d716fSKrzysztof Kozlowski 		ret = PTR_ERR(info->bat);
785b2657167SHans de Goede 		dev_err(dev, "failed to register battery: %d\n", ret);
78636010866SHans de Goede 		return ret;
7875a5bf490STodd Brandt 	}
7885a5bf490STodd Brandt 
78905972f53SHans de Goede 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
79005972f53SHans de Goede 		ret = devm_request_threaded_irq(dev, info->irq[i], NULL,
79105972f53SHans de Goede 						fuel_gauge_thread_handler,
79205972f53SHans de Goede 						IRQF_ONESHOT, DEV_NAME, info);
79305972f53SHans de Goede 		if (ret)
79405972f53SHans de Goede 			return dev_err_probe(dev, ret, "requesting IRQ %d\n", info->irq[i]);
79505972f53SHans de Goede 	}
7965a5bf490STodd Brandt 
797888f9743SHans de Goede 	return 0;
7985a5bf490STodd Brandt }
7995a5bf490STodd Brandt 
800f1f27a4aSKrzysztof Kozlowski static const struct platform_device_id axp288_fg_id_table[] = {
8015a5bf490STodd Brandt 	{ .name = DEV_NAME },
8025a5bf490STodd Brandt 	{},
8035a5bf490STodd Brandt };
80499e33fbdSJavier Martinez Canillas MODULE_DEVICE_TABLE(platform, axp288_fg_id_table);
8055a5bf490STodd Brandt 
8065a5bf490STodd Brandt static struct platform_driver axp288_fuel_gauge_driver = {
8075a5bf490STodd Brandt 	.probe = axp288_fuel_gauge_probe,
8085a5bf490STodd Brandt 	.id_table = axp288_fg_id_table,
8095a5bf490STodd Brandt 	.driver = {
8105a5bf490STodd Brandt 		.name = DEV_NAME,
8115a5bf490STodd Brandt 	},
8125a5bf490STodd Brandt };
8135a5bf490STodd Brandt 
8145a5bf490STodd Brandt module_platform_driver(axp288_fuel_gauge_driver);
8155a5bf490STodd Brandt 
816409e718eSRamakrishna Pallala MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
8175a5bf490STodd Brandt MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>");
8185a5bf490STodd Brandt MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver");
8195a5bf490STodd Brandt MODULE_LICENSE("GPL");
820