xref: /openbmc/linux/drivers/power/supply/axp288_fuel_gauge.c (revision 7f2e85840871f199057e65232ebde846192ed989)
1 /*
2  * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver
3  *
4  * Copyright (C) 2016-2017 Hans de Goede <hdegoede@redhat.com>
5  * Copyright (C) 2014 Intel Corporation
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; version 2 of the License.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
16  * General Public License for more details.
17  *
18  */
19 
20 #include <linux/dmi.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/device.h>
24 #include <linux/regmap.h>
25 #include <linux/jiffies.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/mfd/axp20x.h>
29 #include <linux/platform_device.h>
30 #include <linux/power_supply.h>
31 #include <linux/iio/consumer.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <asm/unaligned.h>
35 
36 #define PS_STAT_VBUS_TRIGGER		(1 << 0)
37 #define PS_STAT_BAT_CHRG_DIR		(1 << 2)
38 #define PS_STAT_VBAT_ABOVE_VHOLD	(1 << 3)
39 #define PS_STAT_VBUS_VALID		(1 << 4)
40 #define PS_STAT_VBUS_PRESENT		(1 << 5)
41 
42 #define CHRG_STAT_BAT_SAFE_MODE		(1 << 3)
43 #define CHRG_STAT_BAT_VALID			(1 << 4)
44 #define CHRG_STAT_BAT_PRESENT		(1 << 5)
45 #define CHRG_STAT_CHARGING			(1 << 6)
46 #define CHRG_STAT_PMIC_OTP			(1 << 7)
47 
48 #define CHRG_CCCV_CC_MASK			0xf     /* 4 bits */
49 #define CHRG_CCCV_CC_BIT_POS		0
50 #define CHRG_CCCV_CC_OFFSET			200     /* 200mA */
51 #define CHRG_CCCV_CC_LSB_RES		200     /* 200mA */
52 #define CHRG_CCCV_ITERM_20P			(1 << 4)    /* 20% of CC */
53 #define CHRG_CCCV_CV_MASK			0x60        /* 2 bits */
54 #define CHRG_CCCV_CV_BIT_POS		5
55 #define CHRG_CCCV_CV_4100MV			0x0     /* 4.10V */
56 #define CHRG_CCCV_CV_4150MV			0x1     /* 4.15V */
57 #define CHRG_CCCV_CV_4200MV			0x2     /* 4.20V */
58 #define CHRG_CCCV_CV_4350MV			0x3     /* 4.35V */
59 #define CHRG_CCCV_CHG_EN			(1 << 7)
60 
61 #define FG_CNTL_OCV_ADJ_STAT		(1 << 2)
62 #define FG_CNTL_OCV_ADJ_EN			(1 << 3)
63 #define FG_CNTL_CAP_ADJ_STAT		(1 << 4)
64 #define FG_CNTL_CAP_ADJ_EN			(1 << 5)
65 #define FG_CNTL_CC_EN				(1 << 6)
66 #define FG_CNTL_GAUGE_EN			(1 << 7)
67 
68 #define FG_15BIT_WORD_VALID			(1 << 15)
69 #define FG_15BIT_VAL_MASK			0x7fff
70 
71 #define FG_REP_CAP_VALID			(1 << 7)
72 #define FG_REP_CAP_VAL_MASK			0x7F
73 
74 #define FG_DES_CAP1_VALID			(1 << 7)
75 #define FG_DES_CAP_RES_LSB			1456    /* 1.456mAhr */
76 
77 #define FG_DES_CC_RES_LSB			1456    /* 1.456mAhr */
78 
79 #define FG_OCV_CAP_VALID			(1 << 7)
80 #define FG_OCV_CAP_VAL_MASK			0x7F
81 #define FG_CC_CAP_VALID				(1 << 7)
82 #define FG_CC_CAP_VAL_MASK			0x7F
83 
84 #define FG_LOW_CAP_THR1_MASK		0xf0    /* 5% tp 20% */
85 #define FG_LOW_CAP_THR1_VAL			0xa0    /* 15 perc */
86 #define FG_LOW_CAP_THR2_MASK		0x0f    /* 0% to 15% */
87 #define FG_LOW_CAP_WARN_THR			14  /* 14 perc */
88 #define FG_LOW_CAP_CRIT_THR			4   /* 4 perc */
89 #define FG_LOW_CAP_SHDN_THR			0   /* 0 perc */
90 
91 #define STATUS_MON_DELAY_JIFFIES    (HZ * 60)   /*60 sec */
92 #define NR_RETRY_CNT    3
93 #define DEV_NAME	"axp288_fuel_gauge"
94 
95 /* 1.1mV per LSB expressed in uV */
96 #define VOLTAGE_FROM_ADC(a)			((a * 11) / 10)
97 /* properties converted to uV, uA */
98 #define PROP_VOLT(a)		((a) * 1000)
99 #define PROP_CURR(a)		((a) * 1000)
100 
101 #define AXP288_FG_INTR_NUM	6
102 enum {
103 	QWBTU_IRQ = 0,
104 	WBTU_IRQ,
105 	QWBTO_IRQ,
106 	WBTO_IRQ,
107 	WL2_IRQ,
108 	WL1_IRQ,
109 };
110 
111 enum {
112 	BAT_TEMP = 0,
113 	PMIC_TEMP,
114 	SYSTEM_TEMP,
115 	BAT_CHRG_CURR,
116 	BAT_D_CURR,
117 	BAT_VOLT,
118 	IIO_CHANNEL_NUM
119 };
120 
121 struct axp288_fg_info {
122 	struct platform_device *pdev;
123 	struct regmap *regmap;
124 	struct regmap_irq_chip_data *regmap_irqc;
125 	int irq[AXP288_FG_INTR_NUM];
126 	struct iio_channel *iio_channel[IIO_CHANNEL_NUM];
127 	struct power_supply *bat;
128 	struct mutex lock;
129 	int status;
130 	int max_volt;
131 	struct delayed_work status_monitor;
132 	struct dentry *debug_file;
133 };
134 
135 static enum power_supply_property fuel_gauge_props[] = {
136 	POWER_SUPPLY_PROP_STATUS,
137 	POWER_SUPPLY_PROP_PRESENT,
138 	POWER_SUPPLY_PROP_HEALTH,
139 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
140 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
141 	POWER_SUPPLY_PROP_VOLTAGE_OCV,
142 	POWER_SUPPLY_PROP_CURRENT_NOW,
143 	POWER_SUPPLY_PROP_CAPACITY,
144 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
145 	POWER_SUPPLY_PROP_TECHNOLOGY,
146 	POWER_SUPPLY_PROP_CHARGE_FULL,
147 	POWER_SUPPLY_PROP_CHARGE_NOW,
148 };
149 
150 static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg)
151 {
152 	int ret, i;
153 	unsigned int val;
154 
155 	for (i = 0; i < NR_RETRY_CNT; i++) {
156 		ret = regmap_read(info->regmap, reg, &val);
157 		if (ret == -EBUSY)
158 			continue;
159 		else
160 			break;
161 	}
162 
163 	if (ret < 0) {
164 		dev_err(&info->pdev->dev, "axp288 reg read err:%d\n", ret);
165 		return ret;
166 	}
167 
168 	return val;
169 }
170 
171 static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val)
172 {
173 	int ret;
174 
175 	ret = regmap_write(info->regmap, reg, (unsigned int)val);
176 
177 	if (ret < 0)
178 		dev_err(&info->pdev->dev, "axp288 reg write err:%d\n", ret);
179 
180 	return ret;
181 }
182 
183 static int fuel_gauge_read_15bit_word(struct axp288_fg_info *info, int reg)
184 {
185 	unsigned char buf[2];
186 	int ret;
187 
188 	ret = regmap_bulk_read(info->regmap, reg, buf, 2);
189 	if (ret < 0) {
190 		dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n",
191 			reg, ret);
192 		return ret;
193 	}
194 
195 	ret = get_unaligned_be16(buf);
196 	if (!(ret & FG_15BIT_WORD_VALID)) {
197 		dev_err(&info->pdev->dev, "Error reg 0x%02x contents not valid\n",
198 			reg);
199 		return -ENXIO;
200 	}
201 
202 	return ret & FG_15BIT_VAL_MASK;
203 }
204 
205 static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg)
206 {
207 	unsigned char buf[2];
208 	int ret;
209 
210 	ret = regmap_bulk_read(info->regmap, reg, buf, 2);
211 	if (ret < 0) {
212 		dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n",
213 			reg, ret);
214 		return ret;
215 	}
216 
217 	/* 12-bit data values have upper 8 bits in buf[0], lower 4 in buf[1] */
218 	return (buf[0] << 4) | ((buf[1] >> 4) & 0x0f);
219 }
220 
221 #ifdef CONFIG_DEBUG_FS
222 static int fuel_gauge_debug_show(struct seq_file *s, void *data)
223 {
224 	struct axp288_fg_info *info = s->private;
225 	int raw_val, ret;
226 
227 	seq_printf(s, " PWR_STATUS[%02x] : %02x\n",
228 		AXP20X_PWR_INPUT_STATUS,
229 		fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS));
230 	seq_printf(s, "PWR_OP_MODE[%02x] : %02x\n",
231 		AXP20X_PWR_OP_MODE,
232 		fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE));
233 	seq_printf(s, " CHRG_CTRL1[%02x] : %02x\n",
234 		AXP20X_CHRG_CTRL1,
235 		fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1));
236 	seq_printf(s, "       VLTF[%02x] : %02x\n",
237 		AXP20X_V_LTF_DISCHRG,
238 		fuel_gauge_reg_readb(info, AXP20X_V_LTF_DISCHRG));
239 	seq_printf(s, "       VHTF[%02x] : %02x\n",
240 		AXP20X_V_HTF_DISCHRG,
241 		fuel_gauge_reg_readb(info, AXP20X_V_HTF_DISCHRG));
242 	seq_printf(s, "    CC_CTRL[%02x] : %02x\n",
243 		AXP20X_CC_CTRL,
244 		fuel_gauge_reg_readb(info, AXP20X_CC_CTRL));
245 	seq_printf(s, "BATTERY CAP[%02x] : %02x\n",
246 		AXP20X_FG_RES,
247 		fuel_gauge_reg_readb(info, AXP20X_FG_RES));
248 	seq_printf(s, "    FG_RDC1[%02x] : %02x\n",
249 		AXP288_FG_RDC1_REG,
250 		fuel_gauge_reg_readb(info, AXP288_FG_RDC1_REG));
251 	seq_printf(s, "    FG_RDC0[%02x] : %02x\n",
252 		AXP288_FG_RDC0_REG,
253 		fuel_gauge_reg_readb(info, AXP288_FG_RDC0_REG));
254 	seq_printf(s, "     FG_OCV[%02x] : %04x\n",
255 		AXP288_FG_OCVH_REG,
256 		fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG));
257 	seq_printf(s, " FG_DES_CAP[%02x] : %04x\n",
258 		AXP288_FG_DES_CAP1_REG,
259 		fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG));
260 	seq_printf(s, "  FG_CC_MTR[%02x] : %04x\n",
261 		AXP288_FG_CC_MTR1_REG,
262 		fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG));
263 	seq_printf(s, " FG_OCV_CAP[%02x] : %02x\n",
264 		AXP288_FG_OCV_CAP_REG,
265 		fuel_gauge_reg_readb(info, AXP288_FG_OCV_CAP_REG));
266 	seq_printf(s, "  FG_CC_CAP[%02x] : %02x\n",
267 		AXP288_FG_CC_CAP_REG,
268 		fuel_gauge_reg_readb(info, AXP288_FG_CC_CAP_REG));
269 	seq_printf(s, " FG_LOW_CAP[%02x] : %02x\n",
270 		AXP288_FG_LOW_CAP_REG,
271 		fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG));
272 	seq_printf(s, "TUNING_CTL0[%02x] : %02x\n",
273 		AXP288_FG_TUNE0,
274 		fuel_gauge_reg_readb(info, AXP288_FG_TUNE0));
275 	seq_printf(s, "TUNING_CTL1[%02x] : %02x\n",
276 		AXP288_FG_TUNE1,
277 		fuel_gauge_reg_readb(info, AXP288_FG_TUNE1));
278 	seq_printf(s, "TUNING_CTL2[%02x] : %02x\n",
279 		AXP288_FG_TUNE2,
280 		fuel_gauge_reg_readb(info, AXP288_FG_TUNE2));
281 	seq_printf(s, "TUNING_CTL3[%02x] : %02x\n",
282 		AXP288_FG_TUNE3,
283 		fuel_gauge_reg_readb(info, AXP288_FG_TUNE3));
284 	seq_printf(s, "TUNING_CTL4[%02x] : %02x\n",
285 		AXP288_FG_TUNE4,
286 		fuel_gauge_reg_readb(info, AXP288_FG_TUNE4));
287 	seq_printf(s, "TUNING_CTL5[%02x] : %02x\n",
288 		AXP288_FG_TUNE5,
289 		fuel_gauge_reg_readb(info, AXP288_FG_TUNE5));
290 
291 	ret = iio_read_channel_raw(info->iio_channel[BAT_TEMP], &raw_val);
292 	if (ret >= 0)
293 		seq_printf(s, "axp288-batttemp : %d\n", raw_val);
294 	ret = iio_read_channel_raw(info->iio_channel[PMIC_TEMP], &raw_val);
295 	if (ret >= 0)
296 		seq_printf(s, "axp288-pmictemp : %d\n", raw_val);
297 	ret = iio_read_channel_raw(info->iio_channel[SYSTEM_TEMP], &raw_val);
298 	if (ret >= 0)
299 		seq_printf(s, "axp288-systtemp : %d\n", raw_val);
300 	ret = iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], &raw_val);
301 	if (ret >= 0)
302 		seq_printf(s, "axp288-chrgcurr : %d\n", raw_val);
303 	ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &raw_val);
304 	if (ret >= 0)
305 		seq_printf(s, "axp288-dchrgcur : %d\n", raw_val);
306 	ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &raw_val);
307 	if (ret >= 0)
308 		seq_printf(s, "axp288-battvolt : %d\n", raw_val);
309 
310 	return 0;
311 }
312 
313 static int debug_open(struct inode *inode, struct file *file)
314 {
315 	return single_open(file, fuel_gauge_debug_show, inode->i_private);
316 }
317 
318 static const struct file_operations fg_debug_fops = {
319 	.open       = debug_open,
320 	.read       = seq_read,
321 	.llseek     = seq_lseek,
322 	.release    = single_release,
323 };
324 
325 static void fuel_gauge_create_debugfs(struct axp288_fg_info *info)
326 {
327 	info->debug_file = debugfs_create_file("fuelgauge", 0666, NULL,
328 		info, &fg_debug_fops);
329 }
330 
331 static void fuel_gauge_remove_debugfs(struct axp288_fg_info *info)
332 {
333 	debugfs_remove(info->debug_file);
334 }
335 #else
336 static inline void fuel_gauge_create_debugfs(struct axp288_fg_info *info)
337 {
338 }
339 static inline void fuel_gauge_remove_debugfs(struct axp288_fg_info *info)
340 {
341 }
342 #endif
343 
344 static void fuel_gauge_get_status(struct axp288_fg_info *info)
345 {
346 	int pwr_stat, fg_res;
347 
348 	pwr_stat = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS);
349 	if (pwr_stat < 0) {
350 		dev_err(&info->pdev->dev,
351 			"PWR STAT read failed:%d\n", pwr_stat);
352 		return;
353 	}
354 
355 	/* Report full if Vbus is valid and the reported capacity is 100% */
356 	if (pwr_stat & PS_STAT_VBUS_VALID) {
357 		fg_res = fuel_gauge_reg_readb(info, AXP20X_FG_RES);
358 		if (fg_res < 0) {
359 			dev_err(&info->pdev->dev,
360 				"FG RES read failed: %d\n", fg_res);
361 			return;
362 		}
363 		if (fg_res == (FG_REP_CAP_VALID | 100)) {
364 			info->status = POWER_SUPPLY_STATUS_FULL;
365 			return;
366 		}
367 	}
368 
369 	if (pwr_stat & PS_STAT_BAT_CHRG_DIR)
370 		info->status = POWER_SUPPLY_STATUS_CHARGING;
371 	else
372 		info->status = POWER_SUPPLY_STATUS_DISCHARGING;
373 }
374 
375 static int fuel_gauge_get_vbatt(struct axp288_fg_info *info, int *vbatt)
376 {
377 	int ret = 0, raw_val;
378 
379 	ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &raw_val);
380 	if (ret < 0)
381 		goto vbatt_read_fail;
382 
383 	*vbatt = VOLTAGE_FROM_ADC(raw_val);
384 vbatt_read_fail:
385 	return ret;
386 }
387 
388 static int fuel_gauge_get_current(struct axp288_fg_info *info, int *cur)
389 {
390 	int ret, discharge;
391 
392 	/* First check discharge current, so that we do only 1 read on bat. */
393 	ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &discharge);
394 	if (ret < 0)
395 		return ret;
396 
397 	if (discharge > 0) {
398 		*cur = -1 * discharge;
399 		return 0;
400 	}
401 
402 	return iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], cur);
403 }
404 
405 static int fuel_gauge_get_vocv(struct axp288_fg_info *info, int *vocv)
406 {
407 	int ret;
408 
409 	ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG);
410 	if (ret >= 0)
411 		*vocv = VOLTAGE_FROM_ADC(ret);
412 
413 	return ret;
414 }
415 
416 static int fuel_gauge_battery_health(struct axp288_fg_info *info)
417 {
418 	int ret, vocv, health = POWER_SUPPLY_HEALTH_UNKNOWN;
419 
420 	ret = fuel_gauge_get_vocv(info, &vocv);
421 	if (ret < 0)
422 		goto health_read_fail;
423 
424 	if (vocv > info->max_volt)
425 		health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
426 	else
427 		health = POWER_SUPPLY_HEALTH_GOOD;
428 
429 health_read_fail:
430 	return health;
431 }
432 
433 static int fuel_gauge_get_property(struct power_supply *ps,
434 		enum power_supply_property prop,
435 		union power_supply_propval *val)
436 {
437 	struct axp288_fg_info *info = power_supply_get_drvdata(ps);
438 	int ret = 0, value;
439 
440 	mutex_lock(&info->lock);
441 	switch (prop) {
442 	case POWER_SUPPLY_PROP_STATUS:
443 		fuel_gauge_get_status(info);
444 		val->intval = info->status;
445 		break;
446 	case POWER_SUPPLY_PROP_HEALTH:
447 		val->intval = fuel_gauge_battery_health(info);
448 		break;
449 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
450 		ret = fuel_gauge_get_vbatt(info, &value);
451 		if (ret < 0)
452 			goto fuel_gauge_read_err;
453 		val->intval = PROP_VOLT(value);
454 		break;
455 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
456 		ret = fuel_gauge_get_vocv(info, &value);
457 		if (ret < 0)
458 			goto fuel_gauge_read_err;
459 		val->intval = PROP_VOLT(value);
460 		break;
461 	case POWER_SUPPLY_PROP_CURRENT_NOW:
462 		ret = fuel_gauge_get_current(info, &value);
463 		if (ret < 0)
464 			goto fuel_gauge_read_err;
465 		val->intval = PROP_CURR(value);
466 		break;
467 	case POWER_SUPPLY_PROP_PRESENT:
468 		ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE);
469 		if (ret < 0)
470 			goto fuel_gauge_read_err;
471 
472 		if (ret & CHRG_STAT_BAT_PRESENT)
473 			val->intval = 1;
474 		else
475 			val->intval = 0;
476 		break;
477 	case POWER_SUPPLY_PROP_CAPACITY:
478 		ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES);
479 		if (ret < 0)
480 			goto fuel_gauge_read_err;
481 
482 		if (!(ret & FG_REP_CAP_VALID))
483 			dev_err(&info->pdev->dev,
484 				"capacity measurement not valid\n");
485 		val->intval = (ret & FG_REP_CAP_VAL_MASK);
486 		break;
487 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
488 		ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
489 		if (ret < 0)
490 			goto fuel_gauge_read_err;
491 		val->intval = (ret & 0x0f);
492 		break;
493 	case POWER_SUPPLY_PROP_TECHNOLOGY:
494 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
495 		break;
496 	case POWER_SUPPLY_PROP_CHARGE_NOW:
497 		ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG);
498 		if (ret < 0)
499 			goto fuel_gauge_read_err;
500 
501 		val->intval = ret * FG_DES_CAP_RES_LSB;
502 		break;
503 	case POWER_SUPPLY_PROP_CHARGE_FULL:
504 		ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG);
505 		if (ret < 0)
506 			goto fuel_gauge_read_err;
507 
508 		val->intval = ret * FG_DES_CAP_RES_LSB;
509 		break;
510 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
511 		val->intval = PROP_VOLT(info->max_volt);
512 		break;
513 	default:
514 		mutex_unlock(&info->lock);
515 		return -EINVAL;
516 	}
517 
518 	mutex_unlock(&info->lock);
519 	return 0;
520 
521 fuel_gauge_read_err:
522 	mutex_unlock(&info->lock);
523 	return ret;
524 }
525 
526 static int fuel_gauge_set_property(struct power_supply *ps,
527 		enum power_supply_property prop,
528 		const union power_supply_propval *val)
529 {
530 	struct axp288_fg_info *info = power_supply_get_drvdata(ps);
531 	int ret = 0;
532 
533 	mutex_lock(&info->lock);
534 	switch (prop) {
535 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
536 		if ((val->intval < 0) || (val->intval > 15)) {
537 			ret = -EINVAL;
538 			break;
539 		}
540 		ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
541 		if (ret < 0)
542 			break;
543 		ret &= 0xf0;
544 		ret |= (val->intval & 0xf);
545 		ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, ret);
546 		break;
547 	default:
548 		ret = -EINVAL;
549 		break;
550 	}
551 
552 	mutex_unlock(&info->lock);
553 	return ret;
554 }
555 
556 static int fuel_gauge_property_is_writeable(struct power_supply *psy,
557 	enum power_supply_property psp)
558 {
559 	int ret;
560 
561 	switch (psp) {
562 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
563 		ret = 1;
564 		break;
565 	default:
566 		ret = 0;
567 	}
568 
569 	return ret;
570 }
571 
572 static void fuel_gauge_status_monitor(struct work_struct *work)
573 {
574 	struct axp288_fg_info *info = container_of(work,
575 		struct axp288_fg_info, status_monitor.work);
576 
577 	fuel_gauge_get_status(info);
578 	power_supply_changed(info->bat);
579 	schedule_delayed_work(&info->status_monitor, STATUS_MON_DELAY_JIFFIES);
580 }
581 
582 static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev)
583 {
584 	struct axp288_fg_info *info = dev;
585 	int i;
586 
587 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
588 		if (info->irq[i] == irq)
589 			break;
590 	}
591 
592 	if (i >= AXP288_FG_INTR_NUM) {
593 		dev_warn(&info->pdev->dev, "spurious interrupt!!\n");
594 		return IRQ_NONE;
595 	}
596 
597 	switch (i) {
598 	case QWBTU_IRQ:
599 		dev_info(&info->pdev->dev,
600 			"Quit Battery under temperature in work mode IRQ (QWBTU)\n");
601 		break;
602 	case WBTU_IRQ:
603 		dev_info(&info->pdev->dev,
604 			"Battery under temperature in work mode IRQ (WBTU)\n");
605 		break;
606 	case QWBTO_IRQ:
607 		dev_info(&info->pdev->dev,
608 			"Quit Battery over temperature in work mode IRQ (QWBTO)\n");
609 		break;
610 	case WBTO_IRQ:
611 		dev_info(&info->pdev->dev,
612 			"Battery over temperature in work mode IRQ (WBTO)\n");
613 		break;
614 	case WL2_IRQ:
615 		dev_info(&info->pdev->dev, "Low Batt Warning(2) INTR\n");
616 		break;
617 	case WL1_IRQ:
618 		dev_info(&info->pdev->dev, "Low Batt Warning(1) INTR\n");
619 		break;
620 	default:
621 		dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n");
622 	}
623 
624 	power_supply_changed(info->bat);
625 	return IRQ_HANDLED;
626 }
627 
628 static void fuel_gauge_external_power_changed(struct power_supply *psy)
629 {
630 	struct axp288_fg_info *info = power_supply_get_drvdata(psy);
631 
632 	power_supply_changed(info->bat);
633 }
634 
635 static const struct power_supply_desc fuel_gauge_desc = {
636 	.name			= DEV_NAME,
637 	.type			= POWER_SUPPLY_TYPE_BATTERY,
638 	.properties		= fuel_gauge_props,
639 	.num_properties		= ARRAY_SIZE(fuel_gauge_props),
640 	.get_property		= fuel_gauge_get_property,
641 	.set_property		= fuel_gauge_set_property,
642 	.property_is_writeable	= fuel_gauge_property_is_writeable,
643 	.external_power_changed	= fuel_gauge_external_power_changed,
644 };
645 
646 static void fuel_gauge_init_irq(struct axp288_fg_info *info)
647 {
648 	int ret, i, pirq;
649 
650 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
651 		pirq = platform_get_irq(info->pdev, i);
652 		info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
653 		if (info->irq[i] < 0) {
654 			dev_warn(&info->pdev->dev,
655 				"regmap_irq get virq failed for IRQ %d: %d\n",
656 				pirq, info->irq[i]);
657 			info->irq[i] = -1;
658 			goto intr_failed;
659 		}
660 		ret = request_threaded_irq(info->irq[i],
661 				NULL, fuel_gauge_thread_handler,
662 				IRQF_ONESHOT, DEV_NAME, info);
663 		if (ret) {
664 			dev_warn(&info->pdev->dev,
665 				"request irq failed for IRQ %d: %d\n",
666 				pirq, info->irq[i]);
667 			info->irq[i] = -1;
668 			goto intr_failed;
669 		} else {
670 			dev_info(&info->pdev->dev, "HW IRQ %d -> VIRQ %d\n",
671 				pirq, info->irq[i]);
672 		}
673 	}
674 	return;
675 
676 intr_failed:
677 	for (; i > 0; i--) {
678 		free_irq(info->irq[i - 1], info);
679 		info->irq[i - 1] = -1;
680 	}
681 }
682 
683 /*
684  * Some devices have no battery (HDMI sticks) and the axp288 battery's
685  * detection reports one despite it not being there.
686  */
687 static const struct dmi_system_id axp288_fuel_gauge_blacklist[] = {
688 	{
689 		/* Intel Cherry Trail Compute Stick, Windows version */
690 		.matches = {
691 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
692 			DMI_MATCH(DMI_PRODUCT_NAME, "STK1AW32SC"),
693 		},
694 	},
695 	{
696 		/* Intel Cherry Trail Compute Stick, version without an OS */
697 		.matches = {
698 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
699 			DMI_MATCH(DMI_PRODUCT_NAME, "STK1A32SC"),
700 		},
701 	},
702 	{
703 		/* Meegopad T08 */
704 		.matches = {
705 			DMI_MATCH(DMI_SYS_VENDOR, "Default string"),
706 			DMI_MATCH(DMI_BOARD_VENDOR, "To be filled by OEM."),
707 			DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"),
708 			DMI_MATCH(DMI_BOARD_VERSION, "V1.1"),
709 		},
710 	},
711 	{}
712 };
713 
714 static int axp288_fuel_gauge_probe(struct platform_device *pdev)
715 {
716 	int i, ret = 0;
717 	struct axp288_fg_info *info;
718 	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
719 	struct power_supply_config psy_cfg = {};
720 	static const char * const iio_chan_name[] = {
721 		[BAT_TEMP] = "axp288-batt-temp",
722 		[PMIC_TEMP] = "axp288-pmic-temp",
723 		[SYSTEM_TEMP] = "axp288-system-temp",
724 		[BAT_CHRG_CURR] = "axp288-chrg-curr",
725 		[BAT_D_CURR] = "axp288-chrg-d-curr",
726 		[BAT_VOLT] = "axp288-batt-volt",
727 	};
728 
729 	if (dmi_check_system(axp288_fuel_gauge_blacklist))
730 		return -ENODEV;
731 
732 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
733 	if (!info)
734 		return -ENOMEM;
735 
736 	info->pdev = pdev;
737 	info->regmap = axp20x->regmap;
738 	info->regmap_irqc = axp20x->regmap_irqc;
739 	info->status = POWER_SUPPLY_STATUS_UNKNOWN;
740 
741 	platform_set_drvdata(pdev, info);
742 
743 	mutex_init(&info->lock);
744 	INIT_DELAYED_WORK(&info->status_monitor, fuel_gauge_status_monitor);
745 
746 	for (i = 0; i < IIO_CHANNEL_NUM; i++) {
747 		/*
748 		 * Note cannot use devm_iio_channel_get because x86 systems
749 		 * lack the device<->channel maps which iio_channel_get will
750 		 * try to use when passed a non NULL device pointer.
751 		 */
752 		info->iio_channel[i] =
753 			iio_channel_get(NULL, iio_chan_name[i]);
754 		if (IS_ERR(info->iio_channel[i])) {
755 			ret = PTR_ERR(info->iio_channel[i]);
756 			dev_dbg(&pdev->dev, "error getting iiochan %s: %d\n",
757 				iio_chan_name[i], ret);
758 			/* Wait for axp288_adc to load */
759 			if (ret == -ENODEV)
760 				ret = -EPROBE_DEFER;
761 
762 			goto out_free_iio_chan;
763 		}
764 	}
765 
766 	ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG);
767 	if (ret < 0)
768 		goto out_free_iio_chan;
769 
770 	if (!(ret & FG_DES_CAP1_VALID)) {
771 		dev_err(&pdev->dev, "axp288 not configured by firmware\n");
772 		ret = -ENODEV;
773 		goto out_free_iio_chan;
774 	}
775 
776 	ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1);
777 	if (ret < 0)
778 		goto out_free_iio_chan;
779 	switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) {
780 	case CHRG_CCCV_CV_4100MV:
781 		info->max_volt = 4100;
782 		break;
783 	case CHRG_CCCV_CV_4150MV:
784 		info->max_volt = 4150;
785 		break;
786 	case CHRG_CCCV_CV_4200MV:
787 		info->max_volt = 4200;
788 		break;
789 	case CHRG_CCCV_CV_4350MV:
790 		info->max_volt = 4350;
791 		break;
792 	}
793 
794 	psy_cfg.drv_data = info;
795 	info->bat = power_supply_register(&pdev->dev, &fuel_gauge_desc, &psy_cfg);
796 	if (IS_ERR(info->bat)) {
797 		ret = PTR_ERR(info->bat);
798 		dev_err(&pdev->dev, "failed to register battery: %d\n", ret);
799 		goto out_free_iio_chan;
800 	}
801 
802 	fuel_gauge_create_debugfs(info);
803 	fuel_gauge_init_irq(info);
804 	schedule_delayed_work(&info->status_monitor, STATUS_MON_DELAY_JIFFIES);
805 
806 	return 0;
807 
808 out_free_iio_chan:
809 	for (i = 0; i < IIO_CHANNEL_NUM; i++)
810 		if (!IS_ERR_OR_NULL(info->iio_channel[i]))
811 			iio_channel_release(info->iio_channel[i]);
812 
813 	return ret;
814 }
815 
816 static const struct platform_device_id axp288_fg_id_table[] = {
817 	{ .name = DEV_NAME },
818 	{},
819 };
820 MODULE_DEVICE_TABLE(platform, axp288_fg_id_table);
821 
822 static int axp288_fuel_gauge_remove(struct platform_device *pdev)
823 {
824 	struct axp288_fg_info *info = platform_get_drvdata(pdev);
825 	int i;
826 
827 	cancel_delayed_work_sync(&info->status_monitor);
828 	power_supply_unregister(info->bat);
829 	fuel_gauge_remove_debugfs(info);
830 
831 	for (i = 0; i < AXP288_FG_INTR_NUM; i++)
832 		if (info->irq[i] >= 0)
833 			free_irq(info->irq[i], info);
834 
835 	for (i = 0; i < IIO_CHANNEL_NUM; i++)
836 		iio_channel_release(info->iio_channel[i]);
837 
838 	return 0;
839 }
840 
841 static struct platform_driver axp288_fuel_gauge_driver = {
842 	.probe = axp288_fuel_gauge_probe,
843 	.remove = axp288_fuel_gauge_remove,
844 	.id_table = axp288_fg_id_table,
845 	.driver = {
846 		.name = DEV_NAME,
847 	},
848 };
849 
850 module_platform_driver(axp288_fuel_gauge_driver);
851 
852 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
853 MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>");
854 MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver");
855 MODULE_LICENSE("GPL");
856