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