1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver
4  *
5  * Copyright (C) 2020-2021 Andrejus Basovas <xxx@yyy.tld>
6  * Copyright (C) 2016-2021 Hans de Goede <hdegoede@redhat.com>
7  * Copyright (C) 2014 Intel Corporation
8  *
9  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  */
11 
12 #include <linux/acpi.h>
13 #include <linux/dmi.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/device.h>
17 #include <linux/regmap.h>
18 #include <linux/jiffies.h>
19 #include <linux/interrupt.h>
20 #include <linux/mfd/axp20x.h>
21 #include <linux/platform_device.h>
22 #include <linux/power_supply.h>
23 #include <linux/iio/consumer.h>
24 #include <asm/unaligned.h>
25 #include <asm/iosf_mbi.h>
26 
27 #define PS_STAT_VBUS_TRIGGER			(1 << 0)
28 #define PS_STAT_BAT_CHRG_DIR			(1 << 2)
29 #define PS_STAT_VBAT_ABOVE_VHOLD		(1 << 3)
30 #define PS_STAT_VBUS_VALID			(1 << 4)
31 #define PS_STAT_VBUS_PRESENT			(1 << 5)
32 
33 #define CHRG_STAT_BAT_SAFE_MODE			(1 << 3)
34 #define CHRG_STAT_BAT_VALID			(1 << 4)
35 #define CHRG_STAT_BAT_PRESENT			(1 << 5)
36 #define CHRG_STAT_CHARGING			(1 << 6)
37 #define CHRG_STAT_PMIC_OTP			(1 << 7)
38 
39 #define CHRG_CCCV_CC_MASK			0xf     /* 4 bits */
40 #define CHRG_CCCV_CC_BIT_POS			0
41 #define CHRG_CCCV_CC_OFFSET			200     /* 200mA */
42 #define CHRG_CCCV_CC_LSB_RES			200     /* 200mA */
43 #define CHRG_CCCV_ITERM_20P			(1 << 4)    /* 20% of CC */
44 #define CHRG_CCCV_CV_MASK			0x60        /* 2 bits */
45 #define CHRG_CCCV_CV_BIT_POS			5
46 #define CHRG_CCCV_CV_4100MV			0x0     /* 4.10V */
47 #define CHRG_CCCV_CV_4150MV			0x1     /* 4.15V */
48 #define CHRG_CCCV_CV_4200MV			0x2     /* 4.20V */
49 #define CHRG_CCCV_CV_4350MV			0x3     /* 4.35V */
50 #define CHRG_CCCV_CHG_EN			(1 << 7)
51 
52 #define FG_CNTL_OCV_ADJ_STAT			(1 << 2)
53 #define FG_CNTL_OCV_ADJ_EN			(1 << 3)
54 #define FG_CNTL_CAP_ADJ_STAT			(1 << 4)
55 #define FG_CNTL_CAP_ADJ_EN			(1 << 5)
56 #define FG_CNTL_CC_EN				(1 << 6)
57 #define FG_CNTL_GAUGE_EN			(1 << 7)
58 
59 #define FG_15BIT_WORD_VALID			(1 << 15)
60 #define FG_15BIT_VAL_MASK			0x7fff
61 
62 #define FG_REP_CAP_VALID			(1 << 7)
63 #define FG_REP_CAP_VAL_MASK			0x7F
64 
65 #define FG_DES_CAP1_VALID			(1 << 7)
66 #define FG_DES_CAP_RES_LSB			1456    /* 1.456mAhr */
67 
68 #define FG_DES_CC_RES_LSB			1456    /* 1.456mAhr */
69 
70 #define FG_OCV_CAP_VALID			(1 << 7)
71 #define FG_OCV_CAP_VAL_MASK			0x7F
72 #define FG_CC_CAP_VALID				(1 << 7)
73 #define FG_CC_CAP_VAL_MASK			0x7F
74 
75 #define FG_LOW_CAP_THR1_MASK			0xf0    /* 5% tp 20% */
76 #define FG_LOW_CAP_THR1_VAL			0xa0    /* 15 perc */
77 #define FG_LOW_CAP_THR2_MASK			0x0f    /* 0% to 15% */
78 #define FG_LOW_CAP_WARN_THR			14  /* 14 perc */
79 #define FG_LOW_CAP_CRIT_THR			4   /* 4 perc */
80 #define FG_LOW_CAP_SHDN_THR			0   /* 0 perc */
81 
82 #define DEV_NAME				"axp288_fuel_gauge"
83 
84 /* 1.1mV per LSB expressed in uV */
85 #define VOLTAGE_FROM_ADC(a)			((a * 11) / 10)
86 /* properties converted to uV, uA */
87 #define PROP_VOLT(a)				((a) * 1000)
88 #define PROP_CURR(a)				((a) * 1000)
89 
90 #define AXP288_REG_UPDATE_INTERVAL		(60 * HZ)
91 #define AXP288_FG_INTR_NUM			6
92 
93 #define AXP288_QUIRK_NO_BATTERY			BIT(0)
94 
95 static bool no_current_sense_res;
96 module_param(no_current_sense_res, bool, 0444);
97 MODULE_PARM_DESC(no_current_sense_res, "No (or broken) current sense resistor");
98 
99 enum {
100 	QWBTU_IRQ = 0,
101 	WBTU_IRQ,
102 	QWBTO_IRQ,
103 	WBTO_IRQ,
104 	WL2_IRQ,
105 	WL1_IRQ,
106 };
107 
108 enum {
109 	BAT_CHRG_CURR,
110 	BAT_D_CURR,
111 	BAT_VOLT,
112 	IIO_CHANNEL_NUM
113 };
114 
115 struct axp288_fg_info {
116 	struct device *dev;
117 	struct regmap *regmap;
118 	int irq[AXP288_FG_INTR_NUM];
119 	struct iio_channel *iio_channel[IIO_CHANNEL_NUM];
120 	struct power_supply *bat;
121 	struct mutex lock;
122 	int status;
123 	int max_volt;
124 	int pwr_op;
125 	int low_cap;
126 	struct dentry *debug_file;
127 
128 	char valid;                 /* zero until following fields are valid */
129 	unsigned long last_updated; /* in jiffies */
130 
131 	int pwr_stat;
132 	int fg_res;
133 	int bat_volt;
134 	int d_curr;
135 	int c_curr;
136 	int ocv;
137 	int fg_cc_mtr1;
138 	int fg_des_cap1;
139 };
140 
141 static enum power_supply_property fuel_gauge_props[] = {
142 	POWER_SUPPLY_PROP_STATUS,
143 	POWER_SUPPLY_PROP_PRESENT,
144 	POWER_SUPPLY_PROP_HEALTH,
145 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
146 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
147 	POWER_SUPPLY_PROP_VOLTAGE_OCV,
148 	POWER_SUPPLY_PROP_CAPACITY,
149 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
150 	POWER_SUPPLY_PROP_TECHNOLOGY,
151 	/* The 3 props below are not used when no_current_sense_res is set */
152 	POWER_SUPPLY_PROP_CHARGE_FULL,
153 	POWER_SUPPLY_PROP_CHARGE_NOW,
154 	POWER_SUPPLY_PROP_CURRENT_NOW,
155 };
156 
157 static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg)
158 {
159 	unsigned int val;
160 	int ret;
161 
162 	ret = regmap_read(info->regmap, reg, &val);
163 	if (ret < 0) {
164 		dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, 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->dev, "Error writing reg 0x%02x err: %d\n", reg, 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->dev, "Error reading reg 0x%02x err: %d\n", reg, ret);
191 		return ret;
192 	}
193 
194 	ret = get_unaligned_be16(buf);
195 	if (!(ret & FG_15BIT_WORD_VALID)) {
196 		dev_err(info->dev, "Error reg 0x%02x contents not valid\n", reg);
197 		return -ENXIO;
198 	}
199 
200 	return ret & FG_15BIT_VAL_MASK;
201 }
202 
203 static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg)
204 {
205 	unsigned char buf[2];
206 	int ret;
207 
208 	ret = regmap_bulk_read(info->regmap, reg, buf, 2);
209 	if (ret < 0) {
210 		dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", 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 static int fuel_gauge_update_registers(struct axp288_fg_info *info)
219 {
220 	int ret;
221 
222 	if (info->valid && time_before(jiffies, info->last_updated + AXP288_REG_UPDATE_INTERVAL))
223 		return 0;
224 
225 	dev_dbg(info->dev, "Fuel Gauge updating register values...\n");
226 
227 	ret = iosf_mbi_block_punit_i2c_access();
228 	if (ret < 0)
229 		return ret;
230 
231 	ret = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS);
232 	if (ret < 0)
233 		goto out;
234 	info->pwr_stat = ret;
235 
236 	if (no_current_sense_res)
237 		ret = fuel_gauge_reg_readb(info, AXP288_FG_OCV_CAP_REG);
238 	else
239 		ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES);
240 	if (ret < 0)
241 		goto out;
242 	info->fg_res = ret;
243 
244 	ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &info->bat_volt);
245 	if (ret < 0)
246 		goto out;
247 
248 	ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG);
249 	if (ret < 0)
250 		goto out;
251 	info->ocv = ret;
252 
253 	if (no_current_sense_res)
254 		goto out_no_current_sense_res;
255 
256 	if (info->pwr_stat & PS_STAT_BAT_CHRG_DIR) {
257 		info->d_curr = 0;
258 		ret = iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], &info->c_curr);
259 		if (ret < 0)
260 			goto out;
261 	} else {
262 		info->c_curr = 0;
263 		ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &info->d_curr);
264 		if (ret < 0)
265 			goto out;
266 	}
267 
268 	ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG);
269 	if (ret < 0)
270 		goto out;
271 	info->fg_cc_mtr1 = ret;
272 
273 	ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG);
274 	if (ret < 0)
275 		goto out;
276 	info->fg_des_cap1 = ret;
277 
278 out_no_current_sense_res:
279 	info->last_updated = jiffies;
280 	info->valid = 1;
281 	ret = 0;
282 out:
283 	iosf_mbi_unblock_punit_i2c_access();
284 	return ret;
285 }
286 
287 static void fuel_gauge_get_status(struct axp288_fg_info *info)
288 {
289 	int pwr_stat = info->pwr_stat;
290 	int fg_res = info->fg_res;
291 	int curr = info->d_curr;
292 
293 	/* Report full if Vbus is valid and the reported capacity is 100% */
294 	if (!(pwr_stat & PS_STAT_VBUS_VALID))
295 		goto not_full;
296 
297 	if (!(fg_res & FG_REP_CAP_VALID))
298 		goto not_full;
299 
300 	fg_res &= ~FG_REP_CAP_VALID;
301 	if (fg_res == 100) {
302 		info->status = POWER_SUPPLY_STATUS_FULL;
303 		return;
304 	}
305 
306 	/*
307 	 * Sometimes the charger turns itself off before fg-res reaches 100%.
308 	 * When this happens the AXP288 reports a not-charging status and
309 	 * 0 mA discharge current.
310 	 */
311 	if (fg_res < 90 || (pwr_stat & PS_STAT_BAT_CHRG_DIR) || no_current_sense_res)
312 		goto not_full;
313 
314 	if (curr == 0) {
315 		info->status = POWER_SUPPLY_STATUS_FULL;
316 		return;
317 	}
318 
319 not_full:
320 	if (pwr_stat & PS_STAT_BAT_CHRG_DIR)
321 		info->status = POWER_SUPPLY_STATUS_CHARGING;
322 	else
323 		info->status = POWER_SUPPLY_STATUS_DISCHARGING;
324 }
325 
326 static int fuel_gauge_battery_health(struct axp288_fg_info *info)
327 {
328 	int vocv = VOLTAGE_FROM_ADC(info->ocv);
329 	int health = POWER_SUPPLY_HEALTH_UNKNOWN;
330 
331 	if (vocv > info->max_volt)
332 		health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
333 	else
334 		health = POWER_SUPPLY_HEALTH_GOOD;
335 
336 	return health;
337 }
338 
339 static int fuel_gauge_get_property(struct power_supply *ps,
340 		enum power_supply_property prop,
341 		union power_supply_propval *val)
342 {
343 	struct axp288_fg_info *info = power_supply_get_drvdata(ps);
344 	int ret, value;
345 
346 	mutex_lock(&info->lock);
347 
348 	ret = fuel_gauge_update_registers(info);
349 	if (ret < 0)
350 		goto out;
351 
352 	switch (prop) {
353 	case POWER_SUPPLY_PROP_STATUS:
354 		fuel_gauge_get_status(info);
355 		val->intval = info->status;
356 		break;
357 	case POWER_SUPPLY_PROP_HEALTH:
358 		val->intval = fuel_gauge_battery_health(info);
359 		break;
360 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
361 		value = VOLTAGE_FROM_ADC(info->bat_volt);
362 		val->intval = PROP_VOLT(value);
363 		break;
364 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
365 		value = VOLTAGE_FROM_ADC(info->ocv);
366 		val->intval = PROP_VOLT(value);
367 		break;
368 	case POWER_SUPPLY_PROP_CURRENT_NOW:
369 		if (info->d_curr > 0)
370 			value = -1 * info->d_curr;
371 		else
372 			value = info->c_curr;
373 
374 		val->intval = PROP_CURR(value);
375 		break;
376 	case POWER_SUPPLY_PROP_PRESENT:
377 		if (info->pwr_op & CHRG_STAT_BAT_PRESENT)
378 			val->intval = 1;
379 		else
380 			val->intval = 0;
381 		break;
382 	case POWER_SUPPLY_PROP_CAPACITY:
383 		if (!(info->fg_res & FG_REP_CAP_VALID))
384 			dev_err(info->dev, "capacity measurement not valid\n");
385 		val->intval = (info->fg_res & FG_REP_CAP_VAL_MASK);
386 		break;
387 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
388 		val->intval = (info->low_cap & 0x0f);
389 		break;
390 	case POWER_SUPPLY_PROP_TECHNOLOGY:
391 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
392 		break;
393 	case POWER_SUPPLY_PROP_CHARGE_NOW:
394 		val->intval = info->fg_cc_mtr1 * FG_DES_CAP_RES_LSB;
395 		break;
396 	case POWER_SUPPLY_PROP_CHARGE_FULL:
397 		val->intval = info->fg_des_cap1 * FG_DES_CAP_RES_LSB;
398 		break;
399 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
400 		val->intval = PROP_VOLT(info->max_volt);
401 		break;
402 	default:
403 		ret = -EINVAL;
404 	}
405 
406 out:
407 	mutex_unlock(&info->lock);
408 	return ret;
409 }
410 
411 static int fuel_gauge_set_property(struct power_supply *ps,
412 		enum power_supply_property prop,
413 		const union power_supply_propval *val)
414 {
415 	struct axp288_fg_info *info = power_supply_get_drvdata(ps);
416 	int new_low_cap, ret = 0;
417 
418 	mutex_lock(&info->lock);
419 	switch (prop) {
420 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
421 		if ((val->intval < 0) || (val->intval > 15)) {
422 			ret = -EINVAL;
423 			break;
424 		}
425 		new_low_cap = info->low_cap;
426 		new_low_cap &= 0xf0;
427 		new_low_cap |= (val->intval & 0xf);
428 		ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, new_low_cap);
429 		if (ret == 0)
430 			info->low_cap = new_low_cap;
431 		break;
432 	default:
433 		ret = -EINVAL;
434 		break;
435 	}
436 
437 	mutex_unlock(&info->lock);
438 	return ret;
439 }
440 
441 static int fuel_gauge_property_is_writeable(struct power_supply *psy,
442 	enum power_supply_property psp)
443 {
444 	int ret;
445 
446 	switch (psp) {
447 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
448 		ret = 1;
449 		break;
450 	default:
451 		ret = 0;
452 	}
453 
454 	return ret;
455 }
456 
457 static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev)
458 {
459 	struct axp288_fg_info *info = dev;
460 	int i;
461 
462 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
463 		if (info->irq[i] == irq)
464 			break;
465 	}
466 
467 	if (i >= AXP288_FG_INTR_NUM) {
468 		dev_warn(info->dev, "spurious interrupt!!\n");
469 		return IRQ_NONE;
470 	}
471 
472 	switch (i) {
473 	case QWBTU_IRQ:
474 		dev_info(info->dev, "Quit Battery under temperature in work mode IRQ (QWBTU)\n");
475 		break;
476 	case WBTU_IRQ:
477 		dev_info(info->dev, "Battery under temperature in work mode IRQ (WBTU)\n");
478 		break;
479 	case QWBTO_IRQ:
480 		dev_info(info->dev, "Quit Battery over temperature in work mode IRQ (QWBTO)\n");
481 		break;
482 	case WBTO_IRQ:
483 		dev_info(info->dev, "Battery over temperature in work mode IRQ (WBTO)\n");
484 		break;
485 	case WL2_IRQ:
486 		dev_info(info->dev, "Low Batt Warning(2) INTR\n");
487 		break;
488 	case WL1_IRQ:
489 		dev_info(info->dev, "Low Batt Warning(1) INTR\n");
490 		break;
491 	default:
492 		dev_warn(info->dev, "Spurious Interrupt!!!\n");
493 	}
494 
495 	mutex_lock(&info->lock);
496 	info->valid = 0; /* Force updating of the cached registers */
497 	mutex_unlock(&info->lock);
498 
499 	power_supply_changed(info->bat);
500 	return IRQ_HANDLED;
501 }
502 
503 static void fuel_gauge_external_power_changed(struct power_supply *psy)
504 {
505 	struct axp288_fg_info *info = power_supply_get_drvdata(psy);
506 
507 	mutex_lock(&info->lock);
508 	info->valid = 0; /* Force updating of the cached registers */
509 	mutex_unlock(&info->lock);
510 	power_supply_changed(info->bat);
511 }
512 
513 static struct power_supply_desc fuel_gauge_desc = {
514 	.name			= DEV_NAME,
515 	.type			= POWER_SUPPLY_TYPE_BATTERY,
516 	.properties		= fuel_gauge_props,
517 	.num_properties		= ARRAY_SIZE(fuel_gauge_props),
518 	.get_property		= fuel_gauge_get_property,
519 	.set_property		= fuel_gauge_set_property,
520 	.property_is_writeable	= fuel_gauge_property_is_writeable,
521 	.external_power_changed	= fuel_gauge_external_power_changed,
522 };
523 
524 /*
525  * Some devices have no battery (HDMI sticks) and the axp288 battery's
526  * detection reports one despite it not being there.
527  * Please keep this listed sorted alphabetically.
528  */
529 static const struct dmi_system_id axp288_quirks[] = {
530 	{
531 		/* ACEPC T8 Cherry Trail Z8350 mini PC */
532 		.matches = {
533 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
534 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"),
535 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T8"),
536 			/* also match on somewhat unique bios-version */
537 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"),
538 		},
539 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
540 	},
541 	{
542 		/* ACEPC T11 Cherry Trail Z8350 mini PC */
543 		.matches = {
544 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
545 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"),
546 			DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T11"),
547 			/* also match on somewhat unique bios-version */
548 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"),
549 		},
550 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
551 	},
552 	{
553 		/* Intel Cherry Trail Compute Stick, Windows version */
554 		.matches = {
555 			DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
556 			DMI_MATCH(DMI_PRODUCT_NAME, "STK1AW32SC"),
557 		},
558 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
559 	},
560 	{
561 		/* Intel Cherry Trail Compute Stick, version without an OS */
562 		.matches = {
563 			DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
564 			DMI_MATCH(DMI_PRODUCT_NAME, "STK1A32SC"),
565 		},
566 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
567 	},
568 	{
569 		/* Meegopad T02 */
570 		.matches = {
571 			DMI_MATCH(DMI_PRODUCT_NAME, "MEEGOPAD T02"),
572 		},
573 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
574 	},
575 	{	/* Mele PCG03 Mini PC */
576 		.matches = {
577 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
578 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
579 		},
580 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
581 	},
582 	{
583 		/* Minix Neo Z83-4 mini PC */
584 		.matches = {
585 			DMI_MATCH(DMI_SYS_VENDOR, "MINIX"),
586 			DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
587 		},
588 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
589 	},
590 	{
591 		/*
592 		 * One Mix 1, this uses the "T3 MRD" boardname used by
593 		 * generic mini PCs, but it is a mini laptop so it does
594 		 * actually have a battery!
595 		 */
596 		.matches = {
597 			DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"),
598 			DMI_MATCH(DMI_BIOS_DATE, "06/14/2018"),
599 		},
600 		.driver_data = NULL,
601 	},
602 	{
603 		/*
604 		 * Various Ace PC/Meegopad/MinisForum/Wintel Mini-PCs/HDMI-sticks
605 		 * This entry must be last because it is generic, this allows
606 		 * adding more specifuc quirks overriding this generic entry.
607 		 */
608 		.matches = {
609 			DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"),
610 			DMI_MATCH(DMI_CHASSIS_TYPE, "3"),
611 			DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
612 		},
613 		.driver_data = (void *)AXP288_QUIRK_NO_BATTERY,
614 	},
615 	{}
616 };
617 
618 static int axp288_fuel_gauge_read_initial_regs(struct axp288_fg_info *info)
619 {
620 	unsigned int val;
621 	int ret;
622 
623 	/*
624 	 * On some devices the fuelgauge and charger parts of the axp288 are
625 	 * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
626 	 */
627 	ret = regmap_read(info->regmap, AXP20X_CC_CTRL, &val);
628 	if (ret < 0)
629 		return ret;
630 	if (val == 0)
631 		return -ENODEV;
632 
633 	ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG);
634 	if (ret < 0)
635 		return ret;
636 
637 	if (!(ret & FG_DES_CAP1_VALID)) {
638 		dev_err(info->dev, "axp288 not configured by firmware\n");
639 		return -ENODEV;
640 	}
641 
642 	ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1);
643 	if (ret < 0)
644 		return ret;
645 	switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) {
646 	case CHRG_CCCV_CV_4100MV:
647 		info->max_volt = 4100;
648 		break;
649 	case CHRG_CCCV_CV_4150MV:
650 		info->max_volt = 4150;
651 		break;
652 	case CHRG_CCCV_CV_4200MV:
653 		info->max_volt = 4200;
654 		break;
655 	case CHRG_CCCV_CV_4350MV:
656 		info->max_volt = 4350;
657 		break;
658 	}
659 
660 	ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE);
661 	if (ret < 0)
662 		return ret;
663 	info->pwr_op = ret;
664 
665 	ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
666 	if (ret < 0)
667 		return ret;
668 	info->low_cap = ret;
669 
670 	return 0;
671 }
672 
673 static void axp288_fuel_gauge_release_iio_chans(void *data)
674 {
675 	struct axp288_fg_info *info = data;
676 	int i;
677 
678 	for (i = 0; i < IIO_CHANNEL_NUM; i++)
679 		if (!IS_ERR_OR_NULL(info->iio_channel[i]))
680 			iio_channel_release(info->iio_channel[i]);
681 }
682 
683 static int axp288_fuel_gauge_probe(struct platform_device *pdev)
684 {
685 	struct axp288_fg_info *info;
686 	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
687 	struct power_supply_config psy_cfg = {};
688 	static const char * const iio_chan_name[] = {
689 		[BAT_CHRG_CURR] = "axp288-chrg-curr",
690 		[BAT_D_CURR] = "axp288-chrg-d-curr",
691 		[BAT_VOLT] = "axp288-batt-volt",
692 	};
693 	const struct dmi_system_id *dmi_id;
694 	struct device *dev = &pdev->dev;
695 	unsigned long quirks = 0;
696 	int i, pirq, ret;
697 
698 	/*
699 	 * Normally the native AXP288 fg/charger drivers are preferred but
700 	 * on some devices the ACPI drivers should be used instead.
701 	 */
702 	if (!acpi_quirk_skip_acpi_ac_and_battery())
703 		return -ENODEV;
704 
705 	dmi_id = dmi_first_match(axp288_quirks);
706 	if (dmi_id)
707 		quirks = (unsigned long)dmi_id->driver_data;
708 
709 	if (quirks & AXP288_QUIRK_NO_BATTERY)
710 		return -ENODEV;
711 
712 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
713 	if (!info)
714 		return -ENOMEM;
715 
716 	info->dev = dev;
717 	info->regmap = axp20x->regmap;
718 	info->status = POWER_SUPPLY_STATUS_UNKNOWN;
719 	info->valid = 0;
720 
721 	platform_set_drvdata(pdev, info);
722 
723 	mutex_init(&info->lock);
724 
725 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
726 		pirq = platform_get_irq(pdev, i);
727 		if (pirq < 0)
728 			continue;
729 		ret = regmap_irq_get_virq(axp20x->regmap_irqc, pirq);
730 		if (ret < 0)
731 			return dev_err_probe(dev, ret, "getting vIRQ %d\n", pirq);
732 
733 		info->irq[i] = ret;
734 	}
735 
736 	for (i = 0; i < IIO_CHANNEL_NUM; i++) {
737 		/*
738 		 * Note cannot use devm_iio_channel_get because x86 systems
739 		 * lack the device<->channel maps which iio_channel_get will
740 		 * try to use when passed a non NULL device pointer.
741 		 */
742 		info->iio_channel[i] =
743 			iio_channel_get(NULL, iio_chan_name[i]);
744 		if (IS_ERR(info->iio_channel[i])) {
745 			ret = PTR_ERR(info->iio_channel[i]);
746 			dev_dbg(dev, "error getting iiochan %s: %d\n", iio_chan_name[i], ret);
747 			/* Wait for axp288_adc to load */
748 			if (ret == -ENODEV)
749 				ret = -EPROBE_DEFER;
750 
751 			axp288_fuel_gauge_release_iio_chans(info);
752 			return ret;
753 		}
754 	}
755 
756 	ret = devm_add_action_or_reset(dev, axp288_fuel_gauge_release_iio_chans, info);
757 	if (ret)
758 		return ret;
759 
760 	ret = iosf_mbi_block_punit_i2c_access();
761 	if (ret < 0)
762 		return ret;
763 
764 	ret = axp288_fuel_gauge_read_initial_regs(info);
765 	iosf_mbi_unblock_punit_i2c_access();
766 	if (ret < 0)
767 		return ret;
768 
769 	psy_cfg.drv_data = info;
770 	if (no_current_sense_res)
771 		fuel_gauge_desc.num_properties = ARRAY_SIZE(fuel_gauge_props) - 3;
772 	info->bat = devm_power_supply_register(dev, &fuel_gauge_desc, &psy_cfg);
773 	if (IS_ERR(info->bat)) {
774 		ret = PTR_ERR(info->bat);
775 		dev_err(dev, "failed to register battery: %d\n", ret);
776 		return ret;
777 	}
778 
779 	for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
780 		ret = devm_request_threaded_irq(dev, info->irq[i], NULL,
781 						fuel_gauge_thread_handler,
782 						IRQF_ONESHOT, DEV_NAME, info);
783 		if (ret)
784 			return dev_err_probe(dev, ret, "requesting IRQ %d\n", info->irq[i]);
785 	}
786 
787 	return 0;
788 }
789 
790 static const struct platform_device_id axp288_fg_id_table[] = {
791 	{ .name = DEV_NAME },
792 	{},
793 };
794 MODULE_DEVICE_TABLE(platform, axp288_fg_id_table);
795 
796 static struct platform_driver axp288_fuel_gauge_driver = {
797 	.probe = axp288_fuel_gauge_probe,
798 	.id_table = axp288_fg_id_table,
799 	.driver = {
800 		.name = DEV_NAME,
801 	},
802 };
803 
804 module_platform_driver(axp288_fuel_gauge_driver);
805 
806 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
807 MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>");
808 MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver");
809 MODULE_LICENSE("GPL");
810