1 // SPDX-License-Identifier: GPL-2.0-only
3 * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver
5 * Copyright (C) 2016-2017 Hans de Goede <hdegoede@redhat.com>
6 * Copyright (C) 2014 Intel Corporation
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 #include <linux/dmi.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/regmap.h>
16 #include <linux/jiffies.h>
17 #include <linux/interrupt.h>
18 #include <linux/mfd/axp20x.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/iio/consumer.h>
22 #include <linux/debugfs.h>
23 #include <linux/seq_file.h>
24 #include <asm/unaligned.h>
26 #define PS_STAT_VBUS_TRIGGER (1 << 0)
27 #define PS_STAT_BAT_CHRG_DIR (1 << 2)
28 #define PS_STAT_VBAT_ABOVE_VHOLD (1 << 3)
29 #define PS_STAT_VBUS_VALID (1 << 4)
30 #define PS_STAT_VBUS_PRESENT (1 << 5)
32 #define CHRG_STAT_BAT_SAFE_MODE (1 << 3)
33 #define CHRG_STAT_BAT_VALID (1 << 4)
34 #define CHRG_STAT_BAT_PRESENT (1 << 5)
35 #define CHRG_STAT_CHARGING (1 << 6)
36 #define CHRG_STAT_PMIC_OTP (1 << 7)
38 #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
39 #define CHRG_CCCV_CC_BIT_POS 0
40 #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
41 #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
42 #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */
43 #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
44 #define CHRG_CCCV_CV_BIT_POS 5
45 #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */
46 #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */
47 #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */
48 #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */
49 #define CHRG_CCCV_CHG_EN (1 << 7)
51 #define FG_CNTL_OCV_ADJ_STAT (1 << 2)
52 #define FG_CNTL_OCV_ADJ_EN (1 << 3)
53 #define FG_CNTL_CAP_ADJ_STAT (1 << 4)
54 #define FG_CNTL_CAP_ADJ_EN (1 << 5)
55 #define FG_CNTL_CC_EN (1 << 6)
56 #define FG_CNTL_GAUGE_EN (1 << 7)
58 #define FG_15BIT_WORD_VALID (1 << 15)
59 #define FG_15BIT_VAL_MASK 0x7fff
61 #define FG_REP_CAP_VALID (1 << 7)
62 #define FG_REP_CAP_VAL_MASK 0x7F
64 #define FG_DES_CAP1_VALID (1 << 7)
65 #define FG_DES_CAP_RES_LSB 1456 /* 1.456mAhr */
67 #define FG_DES_CC_RES_LSB 1456 /* 1.456mAhr */
69 #define FG_OCV_CAP_VALID (1 << 7)
70 #define FG_OCV_CAP_VAL_MASK 0x7F
71 #define FG_CC_CAP_VALID (1 << 7)
72 #define FG_CC_CAP_VAL_MASK 0x7F
74 #define FG_LOW_CAP_THR1_MASK 0xf0 /* 5% tp 20% */
75 #define FG_LOW_CAP_THR1_VAL 0xa0 /* 15 perc */
76 #define FG_LOW_CAP_THR2_MASK 0x0f /* 0% to 15% */
77 #define FG_LOW_CAP_WARN_THR 14 /* 14 perc */
78 #define FG_LOW_CAP_CRIT_THR 4 /* 4 perc */
79 #define FG_LOW_CAP_SHDN_THR 0 /* 0 perc */
81 #define NR_RETRY_CNT 3
82 #define DEV_NAME "axp288_fuel_gauge"
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)
90 #define AXP288_FG_INTR_NUM 6
110 struct axp288_fg_info
{
111 struct platform_device
*pdev
;
112 struct regmap
*regmap
;
113 struct regmap_irq_chip_data
*regmap_irqc
;
114 int irq
[AXP288_FG_INTR_NUM
];
115 struct iio_channel
*iio_channel
[IIO_CHANNEL_NUM
];
116 struct power_supply
*bat
;
120 struct dentry
*debug_file
;
123 static enum power_supply_property fuel_gauge_props
[] = {
124 POWER_SUPPLY_PROP_STATUS
,
125 POWER_SUPPLY_PROP_PRESENT
,
126 POWER_SUPPLY_PROP_HEALTH
,
127 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
128 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
129 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
130 POWER_SUPPLY_PROP_CURRENT_NOW
,
131 POWER_SUPPLY_PROP_CAPACITY
,
132 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
,
133 POWER_SUPPLY_PROP_TECHNOLOGY
,
134 POWER_SUPPLY_PROP_CHARGE_FULL
,
135 POWER_SUPPLY_PROP_CHARGE_NOW
,
138 static int fuel_gauge_reg_readb(struct axp288_fg_info
*info
, int reg
)
143 for (i
= 0; i
< NR_RETRY_CNT
; i
++) {
144 ret
= regmap_read(info
->regmap
, reg
, &val
);
152 dev_err(&info
->pdev
->dev
, "axp288 reg read err:%d\n", ret
);
159 static int fuel_gauge_reg_writeb(struct axp288_fg_info
*info
, int reg
, u8 val
)
163 ret
= regmap_write(info
->regmap
, reg
, (unsigned int)val
);
166 dev_err(&info
->pdev
->dev
, "axp288 reg write err:%d\n", ret
);
171 static int fuel_gauge_read_15bit_word(struct axp288_fg_info
*info
, int reg
)
173 unsigned char buf
[2];
176 ret
= regmap_bulk_read(info
->regmap
, reg
, buf
, 2);
178 dev_err(&info
->pdev
->dev
, "Error reading reg 0x%02x err: %d\n",
183 ret
= get_unaligned_be16(buf
);
184 if (!(ret
& FG_15BIT_WORD_VALID
)) {
185 dev_err(&info
->pdev
->dev
, "Error reg 0x%02x contents not valid\n",
190 return ret
& FG_15BIT_VAL_MASK
;
193 static int fuel_gauge_read_12bit_word(struct axp288_fg_info
*info
, int reg
)
195 unsigned char buf
[2];
198 ret
= regmap_bulk_read(info
->regmap
, reg
, buf
, 2);
200 dev_err(&info
->pdev
->dev
, "Error reading reg 0x%02x err: %d\n",
205 /* 12-bit data values have upper 8 bits in buf[0], lower 4 in buf[1] */
206 return (buf
[0] << 4) | ((buf
[1] >> 4) & 0x0f);
209 #ifdef CONFIG_DEBUG_FS
210 static int fuel_gauge_debug_show(struct seq_file
*s
, void *data
)
212 struct axp288_fg_info
*info
= s
->private;
215 seq_printf(s
, " PWR_STATUS[%02x] : %02x\n",
216 AXP20X_PWR_INPUT_STATUS
,
217 fuel_gauge_reg_readb(info
, AXP20X_PWR_INPUT_STATUS
));
218 seq_printf(s
, "PWR_OP_MODE[%02x] : %02x\n",
220 fuel_gauge_reg_readb(info
, AXP20X_PWR_OP_MODE
));
221 seq_printf(s
, " CHRG_CTRL1[%02x] : %02x\n",
223 fuel_gauge_reg_readb(info
, AXP20X_CHRG_CTRL1
));
224 seq_printf(s
, " VLTF[%02x] : %02x\n",
225 AXP20X_V_LTF_DISCHRG
,
226 fuel_gauge_reg_readb(info
, AXP20X_V_LTF_DISCHRG
));
227 seq_printf(s
, " VHTF[%02x] : %02x\n",
228 AXP20X_V_HTF_DISCHRG
,
229 fuel_gauge_reg_readb(info
, AXP20X_V_HTF_DISCHRG
));
230 seq_printf(s
, " CC_CTRL[%02x] : %02x\n",
232 fuel_gauge_reg_readb(info
, AXP20X_CC_CTRL
));
233 seq_printf(s
, "BATTERY CAP[%02x] : %02x\n",
235 fuel_gauge_reg_readb(info
, AXP20X_FG_RES
));
236 seq_printf(s
, " FG_RDC1[%02x] : %02x\n",
238 fuel_gauge_reg_readb(info
, AXP288_FG_RDC1_REG
));
239 seq_printf(s
, " FG_RDC0[%02x] : %02x\n",
241 fuel_gauge_reg_readb(info
, AXP288_FG_RDC0_REG
));
242 seq_printf(s
, " FG_OCV[%02x] : %04x\n",
244 fuel_gauge_read_12bit_word(info
, AXP288_FG_OCVH_REG
));
245 seq_printf(s
, " FG_DES_CAP[%02x] : %04x\n",
246 AXP288_FG_DES_CAP1_REG
,
247 fuel_gauge_read_15bit_word(info
, AXP288_FG_DES_CAP1_REG
));
248 seq_printf(s
, " FG_CC_MTR[%02x] : %04x\n",
249 AXP288_FG_CC_MTR1_REG
,
250 fuel_gauge_read_15bit_word(info
, AXP288_FG_CC_MTR1_REG
));
251 seq_printf(s
, " FG_OCV_CAP[%02x] : %02x\n",
252 AXP288_FG_OCV_CAP_REG
,
253 fuel_gauge_reg_readb(info
, AXP288_FG_OCV_CAP_REG
));
254 seq_printf(s
, " FG_CC_CAP[%02x] : %02x\n",
255 AXP288_FG_CC_CAP_REG
,
256 fuel_gauge_reg_readb(info
, AXP288_FG_CC_CAP_REG
));
257 seq_printf(s
, " FG_LOW_CAP[%02x] : %02x\n",
258 AXP288_FG_LOW_CAP_REG
,
259 fuel_gauge_reg_readb(info
, AXP288_FG_LOW_CAP_REG
));
260 seq_printf(s
, "TUNING_CTL0[%02x] : %02x\n",
262 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE0
));
263 seq_printf(s
, "TUNING_CTL1[%02x] : %02x\n",
265 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE1
));
266 seq_printf(s
, "TUNING_CTL2[%02x] : %02x\n",
268 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE2
));
269 seq_printf(s
, "TUNING_CTL3[%02x] : %02x\n",
271 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE3
));
272 seq_printf(s
, "TUNING_CTL4[%02x] : %02x\n",
274 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE4
));
275 seq_printf(s
, "TUNING_CTL5[%02x] : %02x\n",
277 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE5
));
279 ret
= iio_read_channel_raw(info
->iio_channel
[BAT_TEMP
], &raw_val
);
281 seq_printf(s
, "axp288-batttemp : %d\n", raw_val
);
282 ret
= iio_read_channel_raw(info
->iio_channel
[PMIC_TEMP
], &raw_val
);
284 seq_printf(s
, "axp288-pmictemp : %d\n", raw_val
);
285 ret
= iio_read_channel_raw(info
->iio_channel
[SYSTEM_TEMP
], &raw_val
);
287 seq_printf(s
, "axp288-systtemp : %d\n", raw_val
);
288 ret
= iio_read_channel_raw(info
->iio_channel
[BAT_CHRG_CURR
], &raw_val
);
290 seq_printf(s
, "axp288-chrgcurr : %d\n", raw_val
);
291 ret
= iio_read_channel_raw(info
->iio_channel
[BAT_D_CURR
], &raw_val
);
293 seq_printf(s
, "axp288-dchrgcur : %d\n", raw_val
);
294 ret
= iio_read_channel_raw(info
->iio_channel
[BAT_VOLT
], &raw_val
);
296 seq_printf(s
, "axp288-battvolt : %d\n", raw_val
);
301 DEFINE_SHOW_ATTRIBUTE(fuel_gauge_debug
);
303 static void fuel_gauge_create_debugfs(struct axp288_fg_info
*info
)
305 info
->debug_file
= debugfs_create_file("fuelgauge", 0666, NULL
,
306 info
, &fuel_gauge_debug_fops
);
309 static void fuel_gauge_remove_debugfs(struct axp288_fg_info
*info
)
311 debugfs_remove(info
->debug_file
);
314 static inline void fuel_gauge_create_debugfs(struct axp288_fg_info
*info
)
317 static inline void fuel_gauge_remove_debugfs(struct axp288_fg_info
*info
)
322 static void fuel_gauge_get_status(struct axp288_fg_info
*info
)
324 int pwr_stat
, fg_res
, curr
, ret
;
326 pwr_stat
= fuel_gauge_reg_readb(info
, AXP20X_PWR_INPUT_STATUS
);
328 dev_err(&info
->pdev
->dev
,
329 "PWR STAT read failed:%d\n", pwr_stat
);
333 /* Report full if Vbus is valid and the reported capacity is 100% */
334 if (!(pwr_stat
& PS_STAT_VBUS_VALID
))
337 fg_res
= fuel_gauge_reg_readb(info
, AXP20X_FG_RES
);
339 dev_err(&info
->pdev
->dev
, "FG RES read failed: %d\n", fg_res
);
342 if (!(fg_res
& FG_REP_CAP_VALID
))
345 fg_res
&= ~FG_REP_CAP_VALID
;
347 info
->status
= POWER_SUPPLY_STATUS_FULL
;
352 * Sometimes the charger turns itself off before fg-res reaches 100%.
353 * When this happens the AXP288 reports a not-charging status and
354 * 0 mA discharge current.
356 if (fg_res
< 90 || (pwr_stat
& PS_STAT_BAT_CHRG_DIR
))
359 ret
= iio_read_channel_raw(info
->iio_channel
[BAT_D_CURR
], &curr
);
361 dev_err(&info
->pdev
->dev
, "FG get current failed: %d\n", ret
);
365 info
->status
= POWER_SUPPLY_STATUS_FULL
;
370 if (pwr_stat
& PS_STAT_BAT_CHRG_DIR
)
371 info
->status
= POWER_SUPPLY_STATUS_CHARGING
;
373 info
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
376 static int fuel_gauge_get_vbatt(struct axp288_fg_info
*info
, int *vbatt
)
378 int ret
= 0, raw_val
;
380 ret
= iio_read_channel_raw(info
->iio_channel
[BAT_VOLT
], &raw_val
);
382 goto vbatt_read_fail
;
384 *vbatt
= VOLTAGE_FROM_ADC(raw_val
);
389 static int fuel_gauge_get_current(struct axp288_fg_info
*info
, int *cur
)
393 /* First check discharge current, so that we do only 1 read on bat. */
394 ret
= iio_read_channel_raw(info
->iio_channel
[BAT_D_CURR
], &discharge
);
399 *cur
= -1 * discharge
;
403 return iio_read_channel_raw(info
->iio_channel
[BAT_CHRG_CURR
], cur
);
406 static int fuel_gauge_get_vocv(struct axp288_fg_info
*info
, int *vocv
)
410 ret
= fuel_gauge_read_12bit_word(info
, AXP288_FG_OCVH_REG
);
412 *vocv
= VOLTAGE_FROM_ADC(ret
);
417 static int fuel_gauge_battery_health(struct axp288_fg_info
*info
)
419 int ret
, vocv
, health
= POWER_SUPPLY_HEALTH_UNKNOWN
;
421 ret
= fuel_gauge_get_vocv(info
, &vocv
);
423 goto health_read_fail
;
425 if (vocv
> info
->max_volt
)
426 health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
428 health
= POWER_SUPPLY_HEALTH_GOOD
;
434 static int fuel_gauge_get_property(struct power_supply
*ps
,
435 enum power_supply_property prop
,
436 union power_supply_propval
*val
)
438 struct axp288_fg_info
*info
= power_supply_get_drvdata(ps
);
441 mutex_lock(&info
->lock
);
443 case POWER_SUPPLY_PROP_STATUS
:
444 fuel_gauge_get_status(info
);
445 val
->intval
= info
->status
;
447 case POWER_SUPPLY_PROP_HEALTH
:
448 val
->intval
= fuel_gauge_battery_health(info
);
450 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
451 ret
= fuel_gauge_get_vbatt(info
, &value
);
453 goto fuel_gauge_read_err
;
454 val
->intval
= PROP_VOLT(value
);
456 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
457 ret
= fuel_gauge_get_vocv(info
, &value
);
459 goto fuel_gauge_read_err
;
460 val
->intval
= PROP_VOLT(value
);
462 case POWER_SUPPLY_PROP_CURRENT_NOW
:
463 ret
= fuel_gauge_get_current(info
, &value
);
465 goto fuel_gauge_read_err
;
466 val
->intval
= PROP_CURR(value
);
468 case POWER_SUPPLY_PROP_PRESENT
:
469 ret
= fuel_gauge_reg_readb(info
, AXP20X_PWR_OP_MODE
);
471 goto fuel_gauge_read_err
;
473 if (ret
& CHRG_STAT_BAT_PRESENT
)
478 case POWER_SUPPLY_PROP_CAPACITY
:
479 ret
= fuel_gauge_reg_readb(info
, AXP20X_FG_RES
);
481 goto fuel_gauge_read_err
;
483 if (!(ret
& FG_REP_CAP_VALID
))
484 dev_err(&info
->pdev
->dev
,
485 "capacity measurement not valid\n");
486 val
->intval
= (ret
& FG_REP_CAP_VAL_MASK
);
488 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
:
489 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_LOW_CAP_REG
);
491 goto fuel_gauge_read_err
;
492 val
->intval
= (ret
& 0x0f);
494 case POWER_SUPPLY_PROP_TECHNOLOGY
:
495 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
497 case POWER_SUPPLY_PROP_CHARGE_NOW
:
498 ret
= fuel_gauge_read_15bit_word(info
, AXP288_FG_CC_MTR1_REG
);
500 goto fuel_gauge_read_err
;
502 val
->intval
= ret
* FG_DES_CAP_RES_LSB
;
504 case POWER_SUPPLY_PROP_CHARGE_FULL
:
505 ret
= fuel_gauge_read_15bit_word(info
, AXP288_FG_DES_CAP1_REG
);
507 goto fuel_gauge_read_err
;
509 val
->intval
= ret
* FG_DES_CAP_RES_LSB
;
511 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
512 val
->intval
= PROP_VOLT(info
->max_volt
);
515 mutex_unlock(&info
->lock
);
519 mutex_unlock(&info
->lock
);
523 mutex_unlock(&info
->lock
);
527 static int fuel_gauge_set_property(struct power_supply
*ps
,
528 enum power_supply_property prop
,
529 const union power_supply_propval
*val
)
531 struct axp288_fg_info
*info
= power_supply_get_drvdata(ps
);
534 mutex_lock(&info
->lock
);
536 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
:
537 if ((val
->intval
< 0) || (val
->intval
> 15)) {
541 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_LOW_CAP_REG
);
545 ret
|= (val
->intval
& 0xf);
546 ret
= fuel_gauge_reg_writeb(info
, AXP288_FG_LOW_CAP_REG
, ret
);
553 mutex_unlock(&info
->lock
);
557 static int fuel_gauge_property_is_writeable(struct power_supply
*psy
,
558 enum power_supply_property psp
)
563 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
:
573 static irqreturn_t
fuel_gauge_thread_handler(int irq
, void *dev
)
575 struct axp288_fg_info
*info
= dev
;
578 for (i
= 0; i
< AXP288_FG_INTR_NUM
; i
++) {
579 if (info
->irq
[i
] == irq
)
583 if (i
>= AXP288_FG_INTR_NUM
) {
584 dev_warn(&info
->pdev
->dev
, "spurious interrupt!!\n");
590 dev_info(&info
->pdev
->dev
,
591 "Quit Battery under temperature in work mode IRQ (QWBTU)\n");
594 dev_info(&info
->pdev
->dev
,
595 "Battery under temperature in work mode IRQ (WBTU)\n");
598 dev_info(&info
->pdev
->dev
,
599 "Quit Battery over temperature in work mode IRQ (QWBTO)\n");
602 dev_info(&info
->pdev
->dev
,
603 "Battery over temperature in work mode IRQ (WBTO)\n");
606 dev_info(&info
->pdev
->dev
, "Low Batt Warning(2) INTR\n");
609 dev_info(&info
->pdev
->dev
, "Low Batt Warning(1) INTR\n");
612 dev_warn(&info
->pdev
->dev
, "Spurious Interrupt!!!\n");
615 power_supply_changed(info
->bat
);
619 static void fuel_gauge_external_power_changed(struct power_supply
*psy
)
621 struct axp288_fg_info
*info
= power_supply_get_drvdata(psy
);
623 power_supply_changed(info
->bat
);
626 static const struct power_supply_desc fuel_gauge_desc
= {
628 .type
= POWER_SUPPLY_TYPE_BATTERY
,
629 .properties
= fuel_gauge_props
,
630 .num_properties
= ARRAY_SIZE(fuel_gauge_props
),
631 .get_property
= fuel_gauge_get_property
,
632 .set_property
= fuel_gauge_set_property
,
633 .property_is_writeable
= fuel_gauge_property_is_writeable
,
634 .external_power_changed
= fuel_gauge_external_power_changed
,
637 static void fuel_gauge_init_irq(struct axp288_fg_info
*info
)
641 for (i
= 0; i
< AXP288_FG_INTR_NUM
; i
++) {
642 pirq
= platform_get_irq(info
->pdev
, i
);
643 info
->irq
[i
] = regmap_irq_get_virq(info
->regmap_irqc
, pirq
);
644 if (info
->irq
[i
] < 0) {
645 dev_warn(&info
->pdev
->dev
,
646 "regmap_irq get virq failed for IRQ %d: %d\n",
651 ret
= request_threaded_irq(info
->irq
[i
],
652 NULL
, fuel_gauge_thread_handler
,
653 IRQF_ONESHOT
, DEV_NAME
, info
);
655 dev_warn(&info
->pdev
->dev
,
656 "request irq failed for IRQ %d: %d\n",
661 dev_info(&info
->pdev
->dev
, "HW IRQ %d -> VIRQ %d\n",
669 free_irq(info
->irq
[i
- 1], info
);
670 info
->irq
[i
- 1] = -1;
675 * Some devices have no battery (HDMI sticks) and the axp288 battery's
676 * detection reports one despite it not being there.
677 * Please keep this listed sorted alphabetically.
679 static const struct dmi_system_id axp288_fuel_gauge_blacklist
[] = {
681 /* ACEPC T8 Cherry Trail Z8350 mini PC */
683 DMI_EXACT_MATCH(DMI_BOARD_VENDOR
, "To be filled by O.E.M."),
684 DMI_EXACT_MATCH(DMI_BOARD_NAME
, "Cherry Trail CR"),
685 DMI_EXACT_MATCH(DMI_PRODUCT_SKU
, "T8"),
686 /* also match on somewhat unique bios-version */
687 DMI_EXACT_MATCH(DMI_BIOS_VERSION
, "1.000"),
691 /* ACEPC T11 Cherry Trail Z8350 mini PC */
693 DMI_EXACT_MATCH(DMI_BOARD_VENDOR
, "To be filled by O.E.M."),
694 DMI_EXACT_MATCH(DMI_BOARD_NAME
, "Cherry Trail CR"),
695 DMI_EXACT_MATCH(DMI_PRODUCT_SKU
, "T11"),
696 /* also match on somewhat unique bios-version */
697 DMI_EXACT_MATCH(DMI_BIOS_VERSION
, "1.000"),
703 DMI_MATCH(DMI_PRODUCT_NAME
, "EF20EA"),
707 /* Intel Cherry Trail Compute Stick, Windows version */
709 DMI_MATCH(DMI_SYS_VENDOR
, "Intel"),
710 DMI_MATCH(DMI_PRODUCT_NAME
, "STK1AW32SC"),
714 /* Intel Cherry Trail Compute Stick, version without an OS */
716 DMI_MATCH(DMI_SYS_VENDOR
, "Intel"),
717 DMI_MATCH(DMI_PRODUCT_NAME
, "STK1A32SC"),
723 DMI_MATCH(DMI_PRODUCT_NAME
, "MEEGOPAD T02"),
729 DMI_MATCH(DMI_SYS_VENDOR
, "Default string"),
730 DMI_MATCH(DMI_BOARD_VENDOR
, "To be filled by OEM."),
731 DMI_MATCH(DMI_BOARD_NAME
, "T3 MRD"),
732 DMI_MATCH(DMI_BOARD_VERSION
, "V1.1"),
736 /* Minix Neo Z83-4 mini PC */
738 DMI_MATCH(DMI_SYS_VENDOR
, "MINIX"),
739 DMI_MATCH(DMI_PRODUCT_NAME
, "Z83-4"),
745 static int axp288_fuel_gauge_probe(struct platform_device
*pdev
)
748 struct axp288_fg_info
*info
;
749 struct axp20x_dev
*axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
750 struct power_supply_config psy_cfg
= {};
751 static const char * const iio_chan_name
[] = {
752 [BAT_TEMP
] = "axp288-batt-temp",
753 [PMIC_TEMP
] = "axp288-pmic-temp",
754 [SYSTEM_TEMP
] = "axp288-system-temp",
755 [BAT_CHRG_CURR
] = "axp288-chrg-curr",
756 [BAT_D_CURR
] = "axp288-chrg-d-curr",
757 [BAT_VOLT
] = "axp288-batt-volt",
761 if (dmi_check_system(axp288_fuel_gauge_blacklist
))
765 * On some devices the fuelgauge and charger parts of the axp288 are
766 * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
768 ret
= regmap_read(axp20x
->regmap
, AXP20X_CC_CTRL
, &val
);
774 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
779 info
->regmap
= axp20x
->regmap
;
780 info
->regmap_irqc
= axp20x
->regmap_irqc
;
781 info
->status
= POWER_SUPPLY_STATUS_UNKNOWN
;
783 platform_set_drvdata(pdev
, info
);
785 mutex_init(&info
->lock
);
787 for (i
= 0; i
< IIO_CHANNEL_NUM
; i
++) {
789 * Note cannot use devm_iio_channel_get because x86 systems
790 * lack the device<->channel maps which iio_channel_get will
791 * try to use when passed a non NULL device pointer.
793 info
->iio_channel
[i
] =
794 iio_channel_get(NULL
, iio_chan_name
[i
]);
795 if (IS_ERR(info
->iio_channel
[i
])) {
796 ret
= PTR_ERR(info
->iio_channel
[i
]);
797 dev_dbg(&pdev
->dev
, "error getting iiochan %s: %d\n",
798 iio_chan_name
[i
], ret
);
799 /* Wait for axp288_adc to load */
803 goto out_free_iio_chan
;
807 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_DES_CAP1_REG
);
809 goto out_free_iio_chan
;
811 if (!(ret
& FG_DES_CAP1_VALID
)) {
812 dev_err(&pdev
->dev
, "axp288 not configured by firmware\n");
814 goto out_free_iio_chan
;
817 ret
= fuel_gauge_reg_readb(info
, AXP20X_CHRG_CTRL1
);
819 goto out_free_iio_chan
;
820 switch ((ret
& CHRG_CCCV_CV_MASK
) >> CHRG_CCCV_CV_BIT_POS
) {
821 case CHRG_CCCV_CV_4100MV
:
822 info
->max_volt
= 4100;
824 case CHRG_CCCV_CV_4150MV
:
825 info
->max_volt
= 4150;
827 case CHRG_CCCV_CV_4200MV
:
828 info
->max_volt
= 4200;
830 case CHRG_CCCV_CV_4350MV
:
831 info
->max_volt
= 4350;
835 psy_cfg
.drv_data
= info
;
836 info
->bat
= power_supply_register(&pdev
->dev
, &fuel_gauge_desc
, &psy_cfg
);
837 if (IS_ERR(info
->bat
)) {
838 ret
= PTR_ERR(info
->bat
);
839 dev_err(&pdev
->dev
, "failed to register battery: %d\n", ret
);
840 goto out_free_iio_chan
;
843 fuel_gauge_create_debugfs(info
);
844 fuel_gauge_init_irq(info
);
849 for (i
= 0; i
< IIO_CHANNEL_NUM
; i
++)
850 if (!IS_ERR_OR_NULL(info
->iio_channel
[i
]))
851 iio_channel_release(info
->iio_channel
[i
]);
856 static const struct platform_device_id axp288_fg_id_table
[] = {
857 { .name
= DEV_NAME
},
860 MODULE_DEVICE_TABLE(platform
, axp288_fg_id_table
);
862 static int axp288_fuel_gauge_remove(struct platform_device
*pdev
)
864 struct axp288_fg_info
*info
= platform_get_drvdata(pdev
);
867 power_supply_unregister(info
->bat
);
868 fuel_gauge_remove_debugfs(info
);
870 for (i
= 0; i
< AXP288_FG_INTR_NUM
; i
++)
871 if (info
->irq
[i
] >= 0)
872 free_irq(info
->irq
[i
], info
);
874 for (i
= 0; i
< IIO_CHANNEL_NUM
; i
++)
875 iio_channel_release(info
->iio_channel
[i
]);
880 static struct platform_driver axp288_fuel_gauge_driver
= {
881 .probe
= axp288_fuel_gauge_probe
,
882 .remove
= axp288_fuel_gauge_remove
,
883 .id_table
= axp288_fg_id_table
,
889 module_platform_driver(axp288_fuel_gauge_driver
);
891 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
892 MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>");
893 MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver");
894 MODULE_LICENSE("GPL");