2 * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver
4 * Copyright (C) 2014 Intel Corporation
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <linux/regmap.h>
23 #include <linux/jiffies.h>
24 #include <linux/interrupt.h>
25 #include <linux/device.h>
26 #include <linux/workqueue.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>
34 #define CHRG_STAT_BAT_SAFE_MODE (1 << 3)
35 #define CHRG_STAT_BAT_VALID (1 << 4)
36 #define CHRG_STAT_BAT_PRESENT (1 << 5)
37 #define CHRG_STAT_CHARGING (1 << 6)
38 #define CHRG_STAT_PMIC_OTP (1 << 7)
40 #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
41 #define CHRG_CCCV_CC_BIT_POS 0
42 #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
43 #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
44 #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */
45 #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
46 #define CHRG_CCCV_CV_BIT_POS 5
47 #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */
48 #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */
49 #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */
50 #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */
51 #define CHRG_CCCV_CHG_EN (1 << 7)
53 #define CV_4100 4100 /* 4100mV */
54 #define CV_4150 4150 /* 4150mV */
55 #define CV_4200 4200 /* 4200mV */
56 #define CV_4350 4350 /* 4350mV */
58 #define TEMP_IRQ_CFG_QWBTU (1 << 0)
59 #define TEMP_IRQ_CFG_WBTU (1 << 1)
60 #define TEMP_IRQ_CFG_QWBTO (1 << 2)
61 #define TEMP_IRQ_CFG_WBTO (1 << 3)
62 #define TEMP_IRQ_CFG_MASK 0xf
64 #define FG_IRQ_CFG_LOWBATT_WL2 (1 << 0)
65 #define FG_IRQ_CFG_LOWBATT_WL1 (1 << 1)
66 #define FG_IRQ_CFG_LOWBATT_MASK 0x3
67 #define LOWBAT_IRQ_STAT_LOWBATT_WL2 (1 << 0)
68 #define LOWBAT_IRQ_STAT_LOWBATT_WL1 (1 << 1)
70 #define FG_CNTL_OCV_ADJ_STAT (1 << 2)
71 #define FG_CNTL_OCV_ADJ_EN (1 << 3)
72 #define FG_CNTL_CAP_ADJ_STAT (1 << 4)
73 #define FG_CNTL_CAP_ADJ_EN (1 << 5)
74 #define FG_CNTL_CC_EN (1 << 6)
75 #define FG_CNTL_GAUGE_EN (1 << 7)
77 #define FG_REP_CAP_VALID (1 << 7)
78 #define FG_REP_CAP_VAL_MASK 0x7F
80 #define FG_DES_CAP1_VALID (1 << 7)
81 #define FG_DES_CAP1_VAL_MASK 0x7F
82 #define FG_DES_CAP0_VAL_MASK 0xFF
83 #define FG_DES_CAP_RES_LSB 1456 /* 1.456mAhr */
85 #define FG_CC_MTR1_VALID (1 << 7)
86 #define FG_CC_MTR1_VAL_MASK 0x7F
87 #define FG_CC_MTR0_VAL_MASK 0xFF
88 #define FG_DES_CC_RES_LSB 1456 /* 1.456mAhr */
90 #define FG_OCV_CAP_VALID (1 << 7)
91 #define FG_OCV_CAP_VAL_MASK 0x7F
92 #define FG_CC_CAP_VALID (1 << 7)
93 #define FG_CC_CAP_VAL_MASK 0x7F
95 #define FG_LOW_CAP_THR1_MASK 0xf0 /* 5% tp 20% */
96 #define FG_LOW_CAP_THR1_VAL 0xa0 /* 15 perc */
97 #define FG_LOW_CAP_THR2_MASK 0x0f /* 0% to 15% */
98 #define FG_LOW_CAP_WARN_THR 14 /* 14 perc */
99 #define FG_LOW_CAP_CRIT_THR 4 /* 4 perc */
100 #define FG_LOW_CAP_SHDN_THR 0 /* 0 perc */
102 #define STATUS_MON_DELAY_JIFFIES (HZ * 60) /*60 sec */
103 #define NR_RETRY_CNT 3
104 #define DEV_NAME "axp288_fuel_gauge"
106 /* 1.1mV per LSB expressed in uV */
107 #define VOLTAGE_FROM_ADC(a) ((a * 11) / 10)
108 /* properties converted to tenths of degrees, uV, uA, uW */
109 #define PROP_TEMP(a) ((a) * 10)
110 #define UNPROP_TEMP(a) ((a) / 10)
111 #define PROP_VOLT(a) ((a) * 1000)
112 #define PROP_CURR(a) ((a) * 1000)
114 #define AXP288_FG_INTR_NUM 6
124 struct axp288_fg_info
{
125 struct platform_device
*pdev
;
126 struct axp20x_fg_pdata
*pdata
;
127 struct regmap
*regmap
;
128 struct regmap_irq_chip_data
*regmap_irqc
;
129 int irq
[AXP288_FG_INTR_NUM
];
130 struct power_supply
*bat
;
133 struct delayed_work status_monitor
;
134 struct dentry
*debug_file
;
137 static enum power_supply_property fuel_gauge_props
[] = {
138 POWER_SUPPLY_PROP_STATUS
,
139 POWER_SUPPLY_PROP_PRESENT
,
140 POWER_SUPPLY_PROP_HEALTH
,
141 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
142 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
143 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
144 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
145 POWER_SUPPLY_PROP_CURRENT_NOW
,
146 POWER_SUPPLY_PROP_CAPACITY
,
147 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
,
148 POWER_SUPPLY_PROP_TEMP
,
149 POWER_SUPPLY_PROP_TEMP_MAX
,
150 POWER_SUPPLY_PROP_TEMP_MIN
,
151 POWER_SUPPLY_PROP_TEMP_ALERT_MIN
,
152 POWER_SUPPLY_PROP_TEMP_ALERT_MAX
,
153 POWER_SUPPLY_PROP_TECHNOLOGY
,
154 POWER_SUPPLY_PROP_CHARGE_FULL
,
155 POWER_SUPPLY_PROP_CHARGE_NOW
,
156 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
157 POWER_SUPPLY_PROP_MODEL_NAME
,
160 static int fuel_gauge_reg_readb(struct axp288_fg_info
*info
, int reg
)
165 for (i
= 0; i
< NR_RETRY_CNT
; i
++) {
166 ret
= regmap_read(info
->regmap
, reg
, &val
);
174 dev_err(&info
->pdev
->dev
, "axp288 reg read err:%d\n", ret
);
179 static int fuel_gauge_reg_writeb(struct axp288_fg_info
*info
, int reg
, u8 val
)
183 ret
= regmap_write(info
->regmap
, reg
, (unsigned int)val
);
186 dev_err(&info
->pdev
->dev
, "axp288 reg write err:%d\n", ret
);
191 static int pmic_read_adc_val(const char *name
, int *raw_val
,
192 struct axp288_fg_info
*info
)
195 struct iio_channel
*indio_chan
;
197 indio_chan
= iio_channel_get(NULL
, name
);
198 if (IS_ERR_OR_NULL(indio_chan
)) {
199 ret
= PTR_ERR(indio_chan
);
202 ret
= iio_read_channel_raw(indio_chan
, &val
);
204 dev_err(&info
->pdev
->dev
,
205 "IIO channel read error: %x, %x\n", ret
, val
);
209 dev_dbg(&info
->pdev
->dev
, "adc raw val=%x\n", val
);
213 iio_channel_release(indio_chan
);
218 #ifdef CONFIG_DEBUG_FS
219 static int fuel_gauge_debug_show(struct seq_file
*s
, void *data
)
221 struct axp288_fg_info
*info
= s
->private;
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",
229 fuel_gauge_reg_readb(info
, AXP20X_PWR_OP_MODE
));
230 seq_printf(s
, " CHRG_CTRL1[%02x] : %02x\n",
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",
241 fuel_gauge_reg_readb(info
, AXP20X_CC_CTRL
));
242 seq_printf(s
, "BATTERY CAP[%02x] : %02x\n",
244 fuel_gauge_reg_readb(info
, AXP20X_FG_RES
));
245 seq_printf(s
, " FG_RDC1[%02x] : %02x\n",
247 fuel_gauge_reg_readb(info
, AXP288_FG_RDC1_REG
));
248 seq_printf(s
, " FG_RDC0[%02x] : %02x\n",
250 fuel_gauge_reg_readb(info
, AXP288_FG_RDC0_REG
));
251 seq_printf(s
, " FG_OCVH[%02x] : %02x\n",
253 fuel_gauge_reg_readb(info
, AXP288_FG_OCVH_REG
));
254 seq_printf(s
, " FG_OCVL[%02x] : %02x\n",
256 fuel_gauge_reg_readb(info
, AXP288_FG_OCVL_REG
));
257 seq_printf(s
, "FG_DES_CAP1[%02x] : %02x\n",
258 AXP288_FG_DES_CAP1_REG
,
259 fuel_gauge_reg_readb(info
, AXP288_FG_DES_CAP1_REG
));
260 seq_printf(s
, "FG_DES_CAP0[%02x] : %02x\n",
261 AXP288_FG_DES_CAP0_REG
,
262 fuel_gauge_reg_readb(info
, AXP288_FG_DES_CAP0_REG
));
263 seq_printf(s
, " FG_CC_MTR1[%02x] : %02x\n",
264 AXP288_FG_CC_MTR1_REG
,
265 fuel_gauge_reg_readb(info
, AXP288_FG_CC_MTR1_REG
));
266 seq_printf(s
, " FG_CC_MTR0[%02x] : %02x\n",
267 AXP288_FG_CC_MTR0_REG
,
268 fuel_gauge_reg_readb(info
, AXP288_FG_CC_MTR0_REG
));
269 seq_printf(s
, " FG_OCV_CAP[%02x] : %02x\n",
270 AXP288_FG_OCV_CAP_REG
,
271 fuel_gauge_reg_readb(info
, AXP288_FG_OCV_CAP_REG
));
272 seq_printf(s
, " FG_CC_CAP[%02x] : %02x\n",
273 AXP288_FG_CC_CAP_REG
,
274 fuel_gauge_reg_readb(info
, AXP288_FG_CC_CAP_REG
));
275 seq_printf(s
, " FG_LOW_CAP[%02x] : %02x\n",
276 AXP288_FG_LOW_CAP_REG
,
277 fuel_gauge_reg_readb(info
, AXP288_FG_LOW_CAP_REG
));
278 seq_printf(s
, "TUNING_CTL0[%02x] : %02x\n",
280 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE0
));
281 seq_printf(s
, "TUNING_CTL1[%02x] : %02x\n",
283 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE1
));
284 seq_printf(s
, "TUNING_CTL2[%02x] : %02x\n",
286 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE2
));
287 seq_printf(s
, "TUNING_CTL3[%02x] : %02x\n",
289 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE3
));
290 seq_printf(s
, "TUNING_CTL4[%02x] : %02x\n",
292 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE4
));
293 seq_printf(s
, "TUNING_CTL5[%02x] : %02x\n",
295 fuel_gauge_reg_readb(info
, AXP288_FG_TUNE5
));
297 ret
= pmic_read_adc_val("axp288-batt-temp", &raw_val
, info
);
299 seq_printf(s
, "axp288-batttemp : %d\n", raw_val
);
300 ret
= pmic_read_adc_val("axp288-pmic-temp", &raw_val
, info
);
302 seq_printf(s
, "axp288-pmictemp : %d\n", raw_val
);
303 ret
= pmic_read_adc_val("axp288-system-temp", &raw_val
, info
);
305 seq_printf(s
, "axp288-systtemp : %d\n", raw_val
);
306 ret
= pmic_read_adc_val("axp288-chrg-curr", &raw_val
, info
);
308 seq_printf(s
, "axp288-chrgcurr : %d\n", raw_val
);
309 ret
= pmic_read_adc_val("axp288-chrg-d-curr", &raw_val
, info
);
311 seq_printf(s
, "axp288-dchrgcur : %d\n", raw_val
);
312 ret
= pmic_read_adc_val("axp288-batt-volt", &raw_val
, info
);
314 seq_printf(s
, "axp288-battvolt : %d\n", raw_val
);
319 static int debug_open(struct inode
*inode
, struct file
*file
)
321 return single_open(file
, fuel_gauge_debug_show
, inode
->i_private
);
324 static const struct file_operations fg_debug_fops
= {
328 .release
= single_release
,
331 static void fuel_gauge_create_debugfs(struct axp288_fg_info
*info
)
333 info
->debug_file
= debugfs_create_file("fuelgauge", 0666, NULL
,
334 info
, &fg_debug_fops
);
337 static void fuel_gauge_remove_debugfs(struct axp288_fg_info
*info
)
339 debugfs_remove(info
->debug_file
);
342 static inline void fuel_gauge_create_debugfs(struct axp288_fg_info
*info
)
345 static inline void fuel_gauge_remove_debugfs(struct axp288_fg_info
*info
)
350 static void fuel_gauge_get_status(struct axp288_fg_info
*info
)
353 int charge
, discharge
;
355 pwr_stat
= fuel_gauge_reg_readb(info
, AXP20X_PWR_INPUT_STATUS
);
357 dev_err(&info
->pdev
->dev
,
358 "PWR STAT read failed:%d\n", pwr_stat
);
361 ret
= pmic_read_adc_val("axp288-chrg-curr", &charge
, info
);
363 dev_err(&info
->pdev
->dev
,
364 "ADC charge current read failed:%d\n", ret
);
367 ret
= pmic_read_adc_val("axp288-chrg-d-curr", &discharge
, info
);
369 dev_err(&info
->pdev
->dev
,
370 "ADC discharge current read failed:%d\n", ret
);
375 info
->status
= POWER_SUPPLY_STATUS_CHARGING
;
376 else if (discharge
> 0)
377 info
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
379 if (pwr_stat
& CHRG_STAT_BAT_PRESENT
)
380 info
->status
= POWER_SUPPLY_STATUS_FULL
;
382 info
->status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
386 static int fuel_gauge_get_vbatt(struct axp288_fg_info
*info
, int *vbatt
)
388 int ret
= 0, raw_val
;
390 ret
= pmic_read_adc_val("axp288-batt-volt", &raw_val
, info
);
392 goto vbatt_read_fail
;
394 *vbatt
= VOLTAGE_FROM_ADC(raw_val
);
399 static int fuel_gauge_get_current(struct axp288_fg_info
*info
, int *cur
)
402 int charge
, discharge
;
404 ret
= pmic_read_adc_val("axp288-chrg-curr", &charge
, info
);
406 goto current_read_fail
;
407 ret
= pmic_read_adc_val("axp288-chrg-d-curr", &discharge
, info
);
409 goto current_read_fail
;
413 else if (discharge
> 0)
414 value
= -1 * discharge
;
421 static int temp_to_adc(struct axp288_fg_info
*info
, int tval
)
423 int rntc
= 0, i
, ret
, adc_val
;
424 int rmin
, rmax
, tmin
, tmax
;
425 int tcsz
= info
->pdata
->tcsz
;
427 /* get the Rntc resitance value for this temp */
428 if (tval
> info
->pdata
->thermistor_curve
[0][1]) {
429 rntc
= info
->pdata
->thermistor_curve
[0][0];
430 } else if (tval
<= info
->pdata
->thermistor_curve
[tcsz
-1][1]) {
431 rntc
= info
->pdata
->thermistor_curve
[tcsz
-1][0];
433 for (i
= 1; i
< tcsz
; i
++) {
434 if (tval
> info
->pdata
->thermistor_curve
[i
][1]) {
435 rmin
= info
->pdata
->thermistor_curve
[i
-1][0];
436 rmax
= info
->pdata
->thermistor_curve
[i
][0];
437 tmin
= info
->pdata
->thermistor_curve
[i
-1][1];
438 tmax
= info
->pdata
->thermistor_curve
[i
][1];
439 rntc
= rmin
+ ((rmax
- rmin
) *
440 (tval
- tmin
) / (tmax
- tmin
));
446 /* we need the current to calculate the proper adc voltage */
447 ret
= fuel_gauge_reg_readb(info
, AXP20X_ADC_RATE
);
449 dev_err(&info
->pdev
->dev
, "%s:read err:%d\n", __func__
, ret
);
454 * temperature is proportional to NTS thermistor resistance
455 * ADC_RATE[5-4] determines current, 00=20uA,01=40uA,10=60uA,11=80uA
456 * [12-bit ADC VAL] = R_NTC(Ω) * current / 800
458 adc_val
= rntc
* (20 + (20 * ((ret
>> 4) & 0x3))) / 800;
463 static int adc_to_temp(struct axp288_fg_info
*info
, int adc_val
)
465 int ret
, r
, i
, tval
= 0;
466 int rmin
, rmax
, tmin
, tmax
;
467 int tcsz
= info
->pdata
->tcsz
;
469 ret
= fuel_gauge_reg_readb(info
, AXP20X_ADC_RATE
);
471 dev_err(&info
->pdev
->dev
, "%s:read err:%d\n", __func__
, ret
);
476 * temperature is proportional to NTS thermistor resistance
477 * ADC_RATE[5-4] determines current, 00=20uA,01=40uA,10=60uA,11=80uA
478 * R_NTC(Ω) = [12-bit ADC VAL] * 800 / current
480 r
= adc_val
* 800 / (20 + (20 * ((ret
>> 4) & 0x3)));
482 if (r
< info
->pdata
->thermistor_curve
[0][0]) {
483 tval
= info
->pdata
->thermistor_curve
[0][1];
484 } else if (r
>= info
->pdata
->thermistor_curve
[tcsz
-1][0]) {
485 tval
= info
->pdata
->thermistor_curve
[tcsz
-1][1];
487 for (i
= 1; i
< tcsz
; i
++) {
488 if (r
< info
->pdata
->thermistor_curve
[i
][0]) {
489 rmin
= info
->pdata
->thermistor_curve
[i
-1][0];
490 rmax
= info
->pdata
->thermistor_curve
[i
][0];
491 tmin
= info
->pdata
->thermistor_curve
[i
-1][1];
492 tmax
= info
->pdata
->thermistor_curve
[i
][1];
493 tval
= tmin
+ ((tmax
- tmin
) *
494 (r
- rmin
) / (rmax
- rmin
));
503 static int fuel_gauge_get_btemp(struct axp288_fg_info
*info
, int *btemp
)
505 int ret
, raw_val
= 0;
507 ret
= pmic_read_adc_val("axp288-batt-temp", &raw_val
, info
);
511 *btemp
= adc_to_temp(info
, raw_val
);
517 static int fuel_gauge_get_vocv(struct axp288_fg_info
*info
, int *vocv
)
521 /* 12-bit data value, upper 8 in OCVH, lower 4 in OCVL */
522 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_OCVH_REG
);
527 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_OCVL_REG
);
530 value
|= (ret
& 0xf);
532 *vocv
= VOLTAGE_FROM_ADC(value
);
537 static int fuel_gauge_battery_health(struct axp288_fg_info
*info
)
540 int ret
, health
= POWER_SUPPLY_HEALTH_UNKNOWN
;
542 ret
= fuel_gauge_get_btemp(info
, &temp
);
544 goto health_read_fail
;
546 ret
= fuel_gauge_get_vocv(info
, &vocv
);
548 goto health_read_fail
;
550 if (vocv
> info
->pdata
->max_volt
)
551 health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
552 else if (temp
> info
->pdata
->max_temp
)
553 health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
554 else if (temp
< info
->pdata
->min_temp
)
555 health
= POWER_SUPPLY_HEALTH_COLD
;
556 else if (vocv
< info
->pdata
->min_volt
)
557 health
= POWER_SUPPLY_HEALTH_DEAD
;
559 health
= POWER_SUPPLY_HEALTH_GOOD
;
565 static int fuel_gauge_set_high_btemp_alert(struct axp288_fg_info
*info
)
569 /* program temperature threshold as 1/16 ADC value */
570 adc_val
= temp_to_adc(info
, info
->pdata
->max_temp
);
571 ret
= fuel_gauge_reg_writeb(info
, AXP20X_V_HTF_DISCHRG
, adc_val
>> 4);
576 static int fuel_gauge_set_low_btemp_alert(struct axp288_fg_info
*info
)
580 /* program temperature threshold as 1/16 ADC value */
581 adc_val
= temp_to_adc(info
, info
->pdata
->min_temp
);
582 ret
= fuel_gauge_reg_writeb(info
, AXP20X_V_LTF_DISCHRG
, adc_val
>> 4);
587 static int fuel_gauge_get_property(struct power_supply
*ps
,
588 enum power_supply_property prop
,
589 union power_supply_propval
*val
)
591 struct axp288_fg_info
*info
= power_supply_get_drvdata(ps
);
594 mutex_lock(&info
->lock
);
596 case POWER_SUPPLY_PROP_STATUS
:
597 fuel_gauge_get_status(info
);
598 val
->intval
= info
->status
;
600 case POWER_SUPPLY_PROP_HEALTH
:
601 val
->intval
= fuel_gauge_battery_health(info
);
603 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
604 ret
= fuel_gauge_get_vbatt(info
, &value
);
606 goto fuel_gauge_read_err
;
607 val
->intval
= PROP_VOLT(value
);
609 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
610 ret
= fuel_gauge_get_vocv(info
, &value
);
612 goto fuel_gauge_read_err
;
613 val
->intval
= PROP_VOLT(value
);
615 case POWER_SUPPLY_PROP_CURRENT_NOW
:
616 ret
= fuel_gauge_get_current(info
, &value
);
618 goto fuel_gauge_read_err
;
619 val
->intval
= PROP_CURR(value
);
621 case POWER_SUPPLY_PROP_PRESENT
:
622 ret
= fuel_gauge_reg_readb(info
, AXP20X_PWR_OP_MODE
);
624 goto fuel_gauge_read_err
;
626 if (ret
& CHRG_STAT_BAT_PRESENT
)
631 case POWER_SUPPLY_PROP_CAPACITY
:
632 ret
= fuel_gauge_reg_readb(info
, AXP20X_FG_RES
);
634 goto fuel_gauge_read_err
;
636 if (!(ret
& FG_REP_CAP_VALID
))
637 dev_err(&info
->pdev
->dev
,
638 "capacity measurement not valid\n");
639 val
->intval
= (ret
& FG_REP_CAP_VAL_MASK
);
641 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
:
642 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_LOW_CAP_REG
);
644 goto fuel_gauge_read_err
;
645 val
->intval
= (ret
& 0x0f);
647 case POWER_SUPPLY_PROP_TEMP
:
648 ret
= fuel_gauge_get_btemp(info
, &value
);
650 goto fuel_gauge_read_err
;
651 val
->intval
= PROP_TEMP(value
);
653 case POWER_SUPPLY_PROP_TEMP_MAX
:
654 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
655 val
->intval
= PROP_TEMP(info
->pdata
->max_temp
);
657 case POWER_SUPPLY_PROP_TEMP_MIN
:
658 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
659 val
->intval
= PROP_TEMP(info
->pdata
->min_temp
);
661 case POWER_SUPPLY_PROP_TECHNOLOGY
:
662 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
664 case POWER_SUPPLY_PROP_CHARGE_NOW
:
665 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_CC_MTR1_REG
);
667 goto fuel_gauge_read_err
;
669 value
= (ret
& FG_CC_MTR1_VAL_MASK
) << 8;
670 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_CC_MTR0_REG
);
672 goto fuel_gauge_read_err
;
673 value
|= (ret
& FG_CC_MTR0_VAL_MASK
);
674 val
->intval
= value
* FG_DES_CAP_RES_LSB
;
676 case POWER_SUPPLY_PROP_CHARGE_FULL
:
677 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_DES_CAP1_REG
);
679 goto fuel_gauge_read_err
;
681 value
= (ret
& FG_DES_CAP1_VAL_MASK
) << 8;
682 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_DES_CAP0_REG
);
684 goto fuel_gauge_read_err
;
685 value
|= (ret
& FG_DES_CAP0_VAL_MASK
);
686 val
->intval
= value
* FG_DES_CAP_RES_LSB
;
688 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
689 val
->intval
= PROP_CURR(info
->pdata
->design_cap
);
691 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
692 val
->intval
= PROP_VOLT(info
->pdata
->max_volt
);
694 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
695 val
->intval
= PROP_VOLT(info
->pdata
->min_volt
);
697 case POWER_SUPPLY_PROP_MODEL_NAME
:
698 val
->strval
= info
->pdata
->battid
;
701 mutex_unlock(&info
->lock
);
705 mutex_unlock(&info
->lock
);
709 mutex_unlock(&info
->lock
);
713 static int fuel_gauge_set_property(struct power_supply
*ps
,
714 enum power_supply_property prop
,
715 const union power_supply_propval
*val
)
717 struct axp288_fg_info
*info
= power_supply_get_drvdata(ps
);
720 mutex_lock(&info
->lock
);
722 case POWER_SUPPLY_PROP_STATUS
:
723 info
->status
= val
->intval
;
725 case POWER_SUPPLY_PROP_TEMP_MIN
:
726 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
727 if ((val
->intval
< PD_DEF_MIN_TEMP
) ||
728 (val
->intval
> PD_DEF_MAX_TEMP
)) {
732 info
->pdata
->min_temp
= UNPROP_TEMP(val
->intval
);
733 ret
= fuel_gauge_set_low_btemp_alert(info
);
735 dev_err(&info
->pdev
->dev
,
736 "temp alert min set fail:%d\n", ret
);
738 case POWER_SUPPLY_PROP_TEMP_MAX
:
739 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
740 if ((val
->intval
< PD_DEF_MIN_TEMP
) ||
741 (val
->intval
> PD_DEF_MAX_TEMP
)) {
745 info
->pdata
->max_temp
= UNPROP_TEMP(val
->intval
);
746 ret
= fuel_gauge_set_high_btemp_alert(info
);
748 dev_err(&info
->pdev
->dev
,
749 "temp alert max set fail:%d\n", ret
);
751 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
:
752 if ((val
->intval
< 0) || (val
->intval
> 15)) {
756 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_LOW_CAP_REG
);
760 ret
|= (val
->intval
& 0xf);
761 ret
= fuel_gauge_reg_writeb(info
, AXP288_FG_LOW_CAP_REG
, ret
);
768 mutex_unlock(&info
->lock
);
772 static int fuel_gauge_property_is_writeable(struct power_supply
*psy
,
773 enum power_supply_property psp
)
778 case POWER_SUPPLY_PROP_STATUS
:
779 case POWER_SUPPLY_PROP_TEMP_MIN
:
780 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
781 case POWER_SUPPLY_PROP_TEMP_MAX
:
782 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
783 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
:
793 static void fuel_gauge_status_monitor(struct work_struct
*work
)
795 struct axp288_fg_info
*info
= container_of(work
,
796 struct axp288_fg_info
, status_monitor
.work
);
798 fuel_gauge_get_status(info
);
799 power_supply_changed(info
->bat
);
800 schedule_delayed_work(&info
->status_monitor
, STATUS_MON_DELAY_JIFFIES
);
803 static irqreturn_t
fuel_gauge_thread_handler(int irq
, void *dev
)
805 struct axp288_fg_info
*info
= dev
;
808 for (i
= 0; i
< AXP288_FG_INTR_NUM
; i
++) {
809 if (info
->irq
[i
] == irq
)
813 if (i
>= AXP288_FG_INTR_NUM
) {
814 dev_warn(&info
->pdev
->dev
, "spurious interrupt!!\n");
820 dev_info(&info
->pdev
->dev
,
821 "Quit Battery under temperature in work mode IRQ (QWBTU)\n");
824 dev_info(&info
->pdev
->dev
,
825 "Battery under temperature in work mode IRQ (WBTU)\n");
828 dev_info(&info
->pdev
->dev
,
829 "Quit Battery over temperature in work mode IRQ (QWBTO)\n");
832 dev_info(&info
->pdev
->dev
,
833 "Battery over temperature in work mode IRQ (WBTO)\n");
836 dev_info(&info
->pdev
->dev
, "Low Batt Warning(2) INTR\n");
839 dev_info(&info
->pdev
->dev
, "Low Batt Warning(1) INTR\n");
842 dev_warn(&info
->pdev
->dev
, "Spurious Interrupt!!!\n");
845 power_supply_changed(info
->bat
);
849 static void fuel_gauge_external_power_changed(struct power_supply
*psy
)
851 struct axp288_fg_info
*info
= power_supply_get_drvdata(psy
);
853 power_supply_changed(info
->bat
);
856 static const struct power_supply_desc fuel_gauge_desc
= {
858 .type
= POWER_SUPPLY_TYPE_BATTERY
,
859 .properties
= fuel_gauge_props
,
860 .num_properties
= ARRAY_SIZE(fuel_gauge_props
),
861 .get_property
= fuel_gauge_get_property
,
862 .set_property
= fuel_gauge_set_property
,
863 .property_is_writeable
= fuel_gauge_property_is_writeable
,
864 .external_power_changed
= fuel_gauge_external_power_changed
,
867 static int fuel_gauge_set_lowbatt_thresholds(struct axp288_fg_info
*info
)
872 ret
= fuel_gauge_reg_readb(info
, AXP20X_FG_RES
);
874 dev_err(&info
->pdev
->dev
, "%s:read err:%d\n", __func__
, ret
);
877 ret
= (ret
& FG_REP_CAP_VAL_MASK
);
879 if (ret
> FG_LOW_CAP_WARN_THR
)
880 reg_val
= FG_LOW_CAP_WARN_THR
;
881 else if (ret
> FG_LOW_CAP_CRIT_THR
)
882 reg_val
= FG_LOW_CAP_CRIT_THR
;
884 reg_val
= FG_LOW_CAP_SHDN_THR
;
886 reg_val
|= FG_LOW_CAP_THR1_VAL
;
887 ret
= fuel_gauge_reg_writeb(info
, AXP288_FG_LOW_CAP_REG
, reg_val
);
889 dev_err(&info
->pdev
->dev
, "%s:write err:%d\n", __func__
, ret
);
894 static int fuel_gauge_program_vbatt_full(struct axp288_fg_info
*info
)
899 ret
= fuel_gauge_reg_readb(info
, AXP20X_CHRG_CTRL1
);
901 goto fg_prog_ocv_fail
;
903 val
= (ret
& ~CHRG_CCCV_CV_MASK
);
905 switch (info
->pdata
->max_volt
) {
907 val
|= (CHRG_CCCV_CV_4100MV
<< CHRG_CCCV_CV_BIT_POS
);
910 val
|= (CHRG_CCCV_CV_4150MV
<< CHRG_CCCV_CV_BIT_POS
);
913 val
|= (CHRG_CCCV_CV_4200MV
<< CHRG_CCCV_CV_BIT_POS
);
916 val
|= (CHRG_CCCV_CV_4350MV
<< CHRG_CCCV_CV_BIT_POS
);
919 val
|= (CHRG_CCCV_CV_4200MV
<< CHRG_CCCV_CV_BIT_POS
);
923 ret
= fuel_gauge_reg_writeb(info
, AXP20X_CHRG_CTRL1
, val
);
928 static int fuel_gauge_program_design_cap(struct axp288_fg_info
*info
)
932 ret
= fuel_gauge_reg_writeb(info
,
933 AXP288_FG_DES_CAP1_REG
, info
->pdata
->cap1
);
935 goto fg_prog_descap_fail
;
937 ret
= fuel_gauge_reg_writeb(info
,
938 AXP288_FG_DES_CAP0_REG
, info
->pdata
->cap0
);
944 static int fuel_gauge_program_ocv_curve(struct axp288_fg_info
*info
)
948 for (i
= 0; i
< OCV_CURVE_SIZE
; i
++) {
949 ret
= fuel_gauge_reg_writeb(info
,
950 AXP288_FG_OCV_CURVE_REG
+ i
, info
->pdata
->ocv_curve
[i
]);
952 goto fg_prog_ocv_fail
;
959 static int fuel_gauge_program_rdc_vals(struct axp288_fg_info
*info
)
963 ret
= fuel_gauge_reg_writeb(info
,
964 AXP288_FG_RDC1_REG
, info
->pdata
->rdc1
);
966 goto fg_prog_ocv_fail
;
968 ret
= fuel_gauge_reg_writeb(info
,
969 AXP288_FG_RDC0_REG
, info
->pdata
->rdc0
);
975 static void fuel_gauge_init_config_regs(struct axp288_fg_info
*info
)
980 * check if the config data is already
981 * programmed and if so just return.
984 ret
= fuel_gauge_reg_readb(info
, AXP288_FG_DES_CAP1_REG
);
986 dev_warn(&info
->pdev
->dev
, "CAP1 reg read err!!\n");
987 } else if (!(ret
& FG_DES_CAP1_VALID
)) {
988 dev_info(&info
->pdev
->dev
, "FG data needs to be initialized\n");
990 dev_info(&info
->pdev
->dev
, "FG data is already initialized\n");
994 ret
= fuel_gauge_program_vbatt_full(info
);
996 dev_err(&info
->pdev
->dev
, "set vbatt full fail:%d\n", ret
);
998 ret
= fuel_gauge_program_design_cap(info
);
1000 dev_err(&info
->pdev
->dev
, "set design cap fail:%d\n", ret
);
1002 ret
= fuel_gauge_program_rdc_vals(info
);
1004 dev_err(&info
->pdev
->dev
, "set rdc fail:%d\n", ret
);
1006 ret
= fuel_gauge_program_ocv_curve(info
);
1008 dev_err(&info
->pdev
->dev
, "set ocv curve fail:%d\n", ret
);
1010 ret
= fuel_gauge_set_lowbatt_thresholds(info
);
1012 dev_err(&info
->pdev
->dev
, "lowbatt thr set fail:%d\n", ret
);
1014 ret
= fuel_gauge_reg_writeb(info
, AXP20X_CC_CTRL
, 0xef);
1016 dev_err(&info
->pdev
->dev
, "gauge cntl set fail:%d\n", ret
);
1019 static void fuel_gauge_init_irq(struct axp288_fg_info
*info
)
1023 for (i
= 0; i
< AXP288_FG_INTR_NUM
; i
++) {
1024 pirq
= platform_get_irq(info
->pdev
, i
);
1025 info
->irq
[i
] = regmap_irq_get_virq(info
->regmap_irqc
, pirq
);
1026 if (info
->irq
[i
] < 0) {
1027 dev_warn(&info
->pdev
->dev
,
1028 "regmap_irq get virq failed for IRQ %d: %d\n",
1029 pirq
, info
->irq
[i
]);
1033 ret
= request_threaded_irq(info
->irq
[i
],
1034 NULL
, fuel_gauge_thread_handler
,
1035 IRQF_ONESHOT
, DEV_NAME
, info
);
1037 dev_warn(&info
->pdev
->dev
,
1038 "request irq failed for IRQ %d: %d\n",
1039 pirq
, info
->irq
[i
]);
1043 dev_info(&info
->pdev
->dev
, "HW IRQ %d -> VIRQ %d\n",
1044 pirq
, info
->irq
[i
]);
1050 for (; i
> 0; i
--) {
1051 free_irq(info
->irq
[i
- 1], info
);
1052 info
->irq
[i
- 1] = -1;
1056 static void fuel_gauge_init_hw_regs(struct axp288_fg_info
*info
)
1061 ret
= fuel_gauge_set_high_btemp_alert(info
);
1063 dev_err(&info
->pdev
->dev
, "high batt temp set fail:%d\n", ret
);
1065 ret
= fuel_gauge_set_low_btemp_alert(info
);
1067 dev_err(&info
->pdev
->dev
, "low batt temp set fail:%d\n", ret
);
1069 /* enable interrupts */
1070 val
= fuel_gauge_reg_readb(info
, AXP20X_IRQ3_EN
);
1071 val
|= TEMP_IRQ_CFG_MASK
;
1072 fuel_gauge_reg_writeb(info
, AXP20X_IRQ3_EN
, val
);
1074 val
= fuel_gauge_reg_readb(info
, AXP20X_IRQ4_EN
);
1075 val
|= FG_IRQ_CFG_LOWBATT_MASK
;
1076 val
= fuel_gauge_reg_writeb(info
, AXP20X_IRQ4_EN
, val
);
1079 static int axp288_fuel_gauge_probe(struct platform_device
*pdev
)
1082 struct axp288_fg_info
*info
;
1083 struct axp20x_dev
*axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
1084 struct power_supply_config psy_cfg
= {};
1086 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
1091 info
->regmap
= axp20x
->regmap
;
1092 info
->regmap_irqc
= axp20x
->regmap_irqc
;
1093 info
->status
= POWER_SUPPLY_STATUS_UNKNOWN
;
1094 info
->pdata
= pdev
->dev
.platform_data
;
1098 platform_set_drvdata(pdev
, info
);
1100 mutex_init(&info
->lock
);
1101 INIT_DELAYED_WORK(&info
->status_monitor
, fuel_gauge_status_monitor
);
1103 psy_cfg
.drv_data
= info
;
1104 info
->bat
= power_supply_register(&pdev
->dev
, &fuel_gauge_desc
, &psy_cfg
);
1105 if (IS_ERR(info
->bat
)) {
1106 ret
= PTR_ERR(info
->bat
);
1107 dev_err(&pdev
->dev
, "failed to register battery: %d\n", ret
);
1111 fuel_gauge_create_debugfs(info
);
1112 fuel_gauge_init_config_regs(info
);
1113 fuel_gauge_init_irq(info
);
1114 fuel_gauge_init_hw_regs(info
);
1115 schedule_delayed_work(&info
->status_monitor
, STATUS_MON_DELAY_JIFFIES
);
1120 static const struct platform_device_id axp288_fg_id_table
[] = {
1121 { .name
= DEV_NAME
},
1125 static int axp288_fuel_gauge_remove(struct platform_device
*pdev
)
1127 struct axp288_fg_info
*info
= platform_get_drvdata(pdev
);
1130 cancel_delayed_work_sync(&info
->status_monitor
);
1131 power_supply_unregister(info
->bat
);
1132 fuel_gauge_remove_debugfs(info
);
1134 for (i
= 0; i
< AXP288_FG_INTR_NUM
; i
++)
1135 if (info
->irq
[i
] >= 0)
1136 free_irq(info
->irq
[i
], info
);
1141 static struct platform_driver axp288_fuel_gauge_driver
= {
1142 .probe
= axp288_fuel_gauge_probe
,
1143 .remove
= axp288_fuel_gauge_remove
,
1144 .id_table
= axp288_fg_id_table
,
1150 module_platform_driver(axp288_fuel_gauge_driver
);
1152 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
1153 MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>");
1154 MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver");
1155 MODULE_LICENSE("GPL");