blob: b74c4acba25d96a6418247533eb4e16ee8df7b42 [file] [log] [blame]
Thomas Gleixner8e8e69d2019-05-29 07:17:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Todd Brandt5a5bf492015-02-04 16:24:38 -08002/*
3 * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver
4 *
Hans de Goede331645e2017-12-26 13:59:10 +01005 * Copyright (C) 2016-2017 Hans de Goede <hdegoede@redhat.com>
Todd Brandt5a5bf492015-02-04 16:24:38 -08006 * Copyright (C) 2014 Intel Corporation
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Todd Brandt5a5bf492015-02-04 16:24:38 -08009 */
10
Hans de Goedeb60c75b2017-12-26 13:59:13 +010011#include <linux/dmi.h>
Todd Brandt5a5bf492015-02-04 16:24:38 -080012#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>
Todd Brandt5a5bf492015-02-04 16:24:38 -080018#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>
Hans de Goede4949fc52016-12-14 17:38:51 +010024#include <asm/unaligned.h>
Todd Brandt5a5bf492015-02-04 16:24:38 -080025
Hans de Goede2b5a4b42017-12-26 13:59:11 +010026#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)
31
Todd Brandt5a5bf492015-02-04 16:24:38 -080032#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)
37
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)
50
Todd Brandt5a5bf492015-02-04 16:24:38 -080051#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)
57
Hans de Goede4949fc52016-12-14 17:38:51 +010058#define FG_15BIT_WORD_VALID (1 << 15)
59#define FG_15BIT_VAL_MASK 0x7fff
60
Todd Brandt5a5bf492015-02-04 16:24:38 -080061#define FG_REP_CAP_VALID (1 << 7)
62#define FG_REP_CAP_VAL_MASK 0x7F
63
64#define FG_DES_CAP1_VALID (1 << 7)
Todd Brandt5a5bf492015-02-04 16:24:38 -080065#define FG_DES_CAP_RES_LSB 1456 /* 1.456mAhr */
66
Todd Brandt5a5bf492015-02-04 16:24:38 -080067#define FG_DES_CC_RES_LSB 1456 /* 1.456mAhr */
68
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
73
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 */
80
Todd Brandt5a5bf492015-02-04 16:24:38 -080081#define NR_RETRY_CNT 3
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)
Hans de Goede888f9742016-12-14 17:38:53 +010086/* properties converted to uV, uA */
Todd Brandt5a5bf492015-02-04 16:24:38 -080087#define PROP_VOLT(a) ((a) * 1000)
88#define PROP_CURR(a) ((a) * 1000)
89
90#define AXP288_FG_INTR_NUM 6
91enum {
92 QWBTU_IRQ = 0,
93 WBTU_IRQ,
94 QWBTO_IRQ,
95 WBTO_IRQ,
96 WL2_IRQ,
97 WL1_IRQ,
98};
99
Hans de Goede331645e2017-12-26 13:59:10 +0100100enum {
101 BAT_TEMP = 0,
102 PMIC_TEMP,
103 SYSTEM_TEMP,
104 BAT_CHRG_CURR,
105 BAT_D_CURR,
106 BAT_VOLT,
107 IIO_CHANNEL_NUM
108};
109
Todd Brandt5a5bf492015-02-04 16:24:38 -0800110struct axp288_fg_info {
111 struct platform_device *pdev;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800112 struct regmap *regmap;
113 struct regmap_irq_chip_data *regmap_irqc;
114 int irq[AXP288_FG_INTR_NUM];
Hans de Goede331645e2017-12-26 13:59:10 +0100115 struct iio_channel *iio_channel[IIO_CHANNEL_NUM];
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100116 struct power_supply *bat;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800117 struct mutex lock;
118 int status;
Hans de Goede888f9742016-12-14 17:38:53 +0100119 int max_volt;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800120 struct dentry *debug_file;
121};
122
123static 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,
Todd Brandt5a5bf492015-02-04 16:24:38 -0800128 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,
Todd Brandt5a5bf492015-02-04 16:24:38 -0800133 POWER_SUPPLY_PROP_TECHNOLOGY,
134 POWER_SUPPLY_PROP_CHARGE_FULL,
135 POWER_SUPPLY_PROP_CHARGE_NOW,
Todd Brandt5a5bf492015-02-04 16:24:38 -0800136};
137
138static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg)
139{
140 int ret, i;
141 unsigned int val;
142
143 for (i = 0; i < NR_RETRY_CNT; i++) {
144 ret = regmap_read(info->regmap, reg, &val);
145 if (ret == -EBUSY)
146 continue;
147 else
148 break;
149 }
150
Hans de Goede6f074bc2016-12-14 17:38:50 +0100151 if (ret < 0) {
Todd Brandt5a5bf492015-02-04 16:24:38 -0800152 dev_err(&info->pdev->dev, "axp288 reg read err:%d\n", ret);
Hans de Goede6f074bc2016-12-14 17:38:50 +0100153 return ret;
154 }
Todd Brandt5a5bf492015-02-04 16:24:38 -0800155
156 return val;
157}
158
159static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val)
160{
161 int ret;
162
163 ret = regmap_write(info->regmap, reg, (unsigned int)val);
164
165 if (ret < 0)
166 dev_err(&info->pdev->dev, "axp288 reg write err:%d\n", ret);
167
168 return ret;
169}
170
Hans de Goede4949fc52016-12-14 17:38:51 +0100171static int fuel_gauge_read_15bit_word(struct axp288_fg_info *info, int reg)
172{
173 unsigned char buf[2];
174 int ret;
175
176 ret = regmap_bulk_read(info->regmap, reg, buf, 2);
177 if (ret < 0) {
178 dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n",
179 reg, ret);
180 return ret;
181 }
182
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",
186 reg);
187 return -ENXIO;
188 }
189
190 return ret & FG_15BIT_VAL_MASK;
191}
192
Hans de Goede248efcf2016-12-14 17:38:52 +0100193static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg)
194{
195 unsigned char buf[2];
196 int ret;
197
198 ret = regmap_bulk_read(info->regmap, reg, buf, 2);
199 if (ret < 0) {
200 dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n",
201 reg, ret);
202 return ret;
203 }
204
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);
207}
208
Todd Brandt5a5bf492015-02-04 16:24:38 -0800209#ifdef CONFIG_DEBUG_FS
210static int fuel_gauge_debug_show(struct seq_file *s, void *data)
211{
212 struct axp288_fg_info *info = s->private;
213 int raw_val, ret;
214
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",
219 AXP20X_PWR_OP_MODE,
220 fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE));
221 seq_printf(s, " CHRG_CTRL1[%02x] : %02x\n",
222 AXP20X_CHRG_CTRL1,
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",
231 AXP20X_CC_CTRL,
232 fuel_gauge_reg_readb(info, AXP20X_CC_CTRL));
233 seq_printf(s, "BATTERY CAP[%02x] : %02x\n",
234 AXP20X_FG_RES,
235 fuel_gauge_reg_readb(info, AXP20X_FG_RES));
236 seq_printf(s, " FG_RDC1[%02x] : %02x\n",
237 AXP288_FG_RDC1_REG,
238 fuel_gauge_reg_readb(info, AXP288_FG_RDC1_REG));
239 seq_printf(s, " FG_RDC0[%02x] : %02x\n",
240 AXP288_FG_RDC0_REG,
241 fuel_gauge_reg_readb(info, AXP288_FG_RDC0_REG));
Hans de Goede248efcf2016-12-14 17:38:52 +0100242 seq_printf(s, " FG_OCV[%02x] : %04x\n",
Todd Brandt5a5bf492015-02-04 16:24:38 -0800243 AXP288_FG_OCVH_REG,
Hans de Goede248efcf2016-12-14 17:38:52 +0100244 fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG));
Hans de Goede4949fc52016-12-14 17:38:51 +0100245 seq_printf(s, " FG_DES_CAP[%02x] : %04x\n",
Todd Brandt5a5bf492015-02-04 16:24:38 -0800246 AXP288_FG_DES_CAP1_REG,
Hans de Goede4949fc52016-12-14 17:38:51 +0100247 fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG));
248 seq_printf(s, " FG_CC_MTR[%02x] : %04x\n",
Todd Brandt5a5bf492015-02-04 16:24:38 -0800249 AXP288_FG_CC_MTR1_REG,
Hans de Goede4949fc52016-12-14 17:38:51 +0100250 fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG));
Todd Brandt5a5bf492015-02-04 16:24:38 -0800251 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",
261 AXP288_FG_TUNE0,
262 fuel_gauge_reg_readb(info, AXP288_FG_TUNE0));
263 seq_printf(s, "TUNING_CTL1[%02x] : %02x\n",
264 AXP288_FG_TUNE1,
265 fuel_gauge_reg_readb(info, AXP288_FG_TUNE1));
266 seq_printf(s, "TUNING_CTL2[%02x] : %02x\n",
267 AXP288_FG_TUNE2,
268 fuel_gauge_reg_readb(info, AXP288_FG_TUNE2));
269 seq_printf(s, "TUNING_CTL3[%02x] : %02x\n",
270 AXP288_FG_TUNE3,
271 fuel_gauge_reg_readb(info, AXP288_FG_TUNE3));
272 seq_printf(s, "TUNING_CTL4[%02x] : %02x\n",
273 AXP288_FG_TUNE4,
274 fuel_gauge_reg_readb(info, AXP288_FG_TUNE4));
275 seq_printf(s, "TUNING_CTL5[%02x] : %02x\n",
276 AXP288_FG_TUNE5,
277 fuel_gauge_reg_readb(info, AXP288_FG_TUNE5));
278
Hans de Goede331645e2017-12-26 13:59:10 +0100279 ret = iio_read_channel_raw(info->iio_channel[BAT_TEMP], &raw_val);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800280 if (ret >= 0)
281 seq_printf(s, "axp288-batttemp : %d\n", raw_val);
Hans de Goede331645e2017-12-26 13:59:10 +0100282 ret = iio_read_channel_raw(info->iio_channel[PMIC_TEMP], &raw_val);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800283 if (ret >= 0)
284 seq_printf(s, "axp288-pmictemp : %d\n", raw_val);
Hans de Goede331645e2017-12-26 13:59:10 +0100285 ret = iio_read_channel_raw(info->iio_channel[SYSTEM_TEMP], &raw_val);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800286 if (ret >= 0)
287 seq_printf(s, "axp288-systtemp : %d\n", raw_val);
Hans de Goede331645e2017-12-26 13:59:10 +0100288 ret = iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], &raw_val);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800289 if (ret >= 0)
290 seq_printf(s, "axp288-chrgcurr : %d\n", raw_val);
Hans de Goede331645e2017-12-26 13:59:10 +0100291 ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &raw_val);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800292 if (ret >= 0)
293 seq_printf(s, "axp288-dchrgcur : %d\n", raw_val);
Hans de Goede331645e2017-12-26 13:59:10 +0100294 ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &raw_val);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800295 if (ret >= 0)
296 seq_printf(s, "axp288-battvolt : %d\n", raw_val);
297
298 return 0;
299}
300
Yangtao Li0367e232018-11-22 09:25:51 -0500301DEFINE_SHOW_ATTRIBUTE(fuel_gauge_debug);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800302
303static void fuel_gauge_create_debugfs(struct axp288_fg_info *info)
304{
305 info->debug_file = debugfs_create_file("fuelgauge", 0666, NULL,
Yangtao Li0367e232018-11-22 09:25:51 -0500306 info, &fuel_gauge_debug_fops);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800307}
308
309static void fuel_gauge_remove_debugfs(struct axp288_fg_info *info)
310{
311 debugfs_remove(info->debug_file);
312}
313#else
314static inline void fuel_gauge_create_debugfs(struct axp288_fg_info *info)
315{
316}
317static inline void fuel_gauge_remove_debugfs(struct axp288_fg_info *info)
318{
319}
320#endif
321
322static void fuel_gauge_get_status(struct axp288_fg_info *info)
323{
Hans de Goedef4516552018-02-15 15:00:36 +0100324 int pwr_stat, fg_res, curr, ret;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800325
326 pwr_stat = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS);
327 if (pwr_stat < 0) {
328 dev_err(&info->pdev->dev,
329 "PWR STAT read failed:%d\n", pwr_stat);
330 return;
331 }
Hans de Goede2b5a4b42017-12-26 13:59:11 +0100332
333 /* Report full if Vbus is valid and the reported capacity is 100% */
Hans de Goedef4516552018-02-15 15:00:36 +0100334 if (!(pwr_stat & PS_STAT_VBUS_VALID))
335 goto not_full;
336
337 fg_res = fuel_gauge_reg_readb(info, AXP20X_FG_RES);
338 if (fg_res < 0) {
339 dev_err(&info->pdev->dev, "FG RES read failed: %d\n", fg_res);
340 return;
341 }
342 if (!(fg_res & FG_REP_CAP_VALID))
343 goto not_full;
344
345 fg_res &= ~FG_REP_CAP_VALID;
346 if (fg_res == 100) {
347 info->status = POWER_SUPPLY_STATUS_FULL;
348 return;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800349 }
350
Hans de Goedef4516552018-02-15 15:00:36 +0100351 /*
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.
355 */
356 if (fg_res < 90 || (pwr_stat & PS_STAT_BAT_CHRG_DIR))
357 goto not_full;
358
359 ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &curr);
360 if (ret < 0) {
361 dev_err(&info->pdev->dev, "FG get current failed: %d\n", ret);
362 return;
363 }
364 if (curr == 0) {
365 info->status = POWER_SUPPLY_STATUS_FULL;
366 return;
367 }
368
369not_full:
Hans de Goede2b5a4b42017-12-26 13:59:11 +0100370 if (pwr_stat & PS_STAT_BAT_CHRG_DIR)
Todd Brandt5a5bf492015-02-04 16:24:38 -0800371 info->status = POWER_SUPPLY_STATUS_CHARGING;
Hans de Goede2b5a4b42017-12-26 13:59:11 +0100372 else
Todd Brandt5a5bf492015-02-04 16:24:38 -0800373 info->status = POWER_SUPPLY_STATUS_DISCHARGING;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800374}
375
376static int fuel_gauge_get_vbatt(struct axp288_fg_info *info, int *vbatt)
377{
378 int ret = 0, raw_val;
379
Hans de Goede331645e2017-12-26 13:59:10 +0100380 ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &raw_val);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800381 if (ret < 0)
382 goto vbatt_read_fail;
383
384 *vbatt = VOLTAGE_FROM_ADC(raw_val);
385vbatt_read_fail:
386 return ret;
387}
388
389static int fuel_gauge_get_current(struct axp288_fg_info *info, int *cur)
390{
Hans de Goedeceb40832017-12-26 13:59:12 +0100391 int ret, discharge;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800392
Hans de Goedeceb40832017-12-26 13:59:12 +0100393 /* First check discharge current, so that we do only 1 read on bat. */
Hans de Goede331645e2017-12-26 13:59:10 +0100394 ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &discharge);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800395 if (ret < 0)
Hans de Goedeceb40832017-12-26 13:59:12 +0100396 return ret;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800397
Hans de Goedeceb40832017-12-26 13:59:12 +0100398 if (discharge > 0) {
399 *cur = -1 * discharge;
400 return 0;
401 }
Todd Brandt5a5bf492015-02-04 16:24:38 -0800402
Hans de Goedeceb40832017-12-26 13:59:12 +0100403 return iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], cur);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800404}
405
Todd Brandt5a5bf492015-02-04 16:24:38 -0800406static int fuel_gauge_get_vocv(struct axp288_fg_info *info, int *vocv)
407{
Hans de Goede248efcf2016-12-14 17:38:52 +0100408 int ret;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800409
Hans de Goede248efcf2016-12-14 17:38:52 +0100410 ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG);
411 if (ret >= 0)
412 *vocv = VOLTAGE_FROM_ADC(ret);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800413
Todd Brandt5a5bf492015-02-04 16:24:38 -0800414 return ret;
415}
416
417static int fuel_gauge_battery_health(struct axp288_fg_info *info)
418{
Hans de Goede888f9742016-12-14 17:38:53 +0100419 int ret, vocv, health = POWER_SUPPLY_HEALTH_UNKNOWN;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800420
421 ret = fuel_gauge_get_vocv(info, &vocv);
422 if (ret < 0)
423 goto health_read_fail;
424
Hans de Goede888f9742016-12-14 17:38:53 +0100425 if (vocv > info->max_volt)
Todd Brandt5a5bf492015-02-04 16:24:38 -0800426 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800427 else
428 health = POWER_SUPPLY_HEALTH_GOOD;
429
430health_read_fail:
431 return health;
432}
433
Todd Brandt5a5bf492015-02-04 16:24:38 -0800434static int fuel_gauge_get_property(struct power_supply *ps,
435 enum power_supply_property prop,
436 union power_supply_propval *val)
437{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100438 struct axp288_fg_info *info = power_supply_get_drvdata(ps);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800439 int ret = 0, value;
440
441 mutex_lock(&info->lock);
442 switch (prop) {
443 case POWER_SUPPLY_PROP_STATUS:
444 fuel_gauge_get_status(info);
445 val->intval = info->status;
446 break;
447 case POWER_SUPPLY_PROP_HEALTH:
448 val->intval = fuel_gauge_battery_health(info);
449 break;
450 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
451 ret = fuel_gauge_get_vbatt(info, &value);
452 if (ret < 0)
453 goto fuel_gauge_read_err;
454 val->intval = PROP_VOLT(value);
455 break;
456 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
457 ret = fuel_gauge_get_vocv(info, &value);
458 if (ret < 0)
459 goto fuel_gauge_read_err;
460 val->intval = PROP_VOLT(value);
461 break;
462 case POWER_SUPPLY_PROP_CURRENT_NOW:
463 ret = fuel_gauge_get_current(info, &value);
464 if (ret < 0)
465 goto fuel_gauge_read_err;
466 val->intval = PROP_CURR(value);
467 break;
468 case POWER_SUPPLY_PROP_PRESENT:
469 ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE);
470 if (ret < 0)
471 goto fuel_gauge_read_err;
472
473 if (ret & CHRG_STAT_BAT_PRESENT)
474 val->intval = 1;
475 else
476 val->intval = 0;
477 break;
478 case POWER_SUPPLY_PROP_CAPACITY:
479 ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES);
480 if (ret < 0)
481 goto fuel_gauge_read_err;
482
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);
487 break;
488 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
489 ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
490 if (ret < 0)
491 goto fuel_gauge_read_err;
492 val->intval = (ret & 0x0f);
493 break;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800494 case POWER_SUPPLY_PROP_TECHNOLOGY:
495 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
496 break;
497 case POWER_SUPPLY_PROP_CHARGE_NOW:
Hans de Goede4949fc52016-12-14 17:38:51 +0100498 ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800499 if (ret < 0)
500 goto fuel_gauge_read_err;
501
Hans de Goede4949fc52016-12-14 17:38:51 +0100502 val->intval = ret * FG_DES_CAP_RES_LSB;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800503 break;
504 case POWER_SUPPLY_PROP_CHARGE_FULL:
Hans de Goede4949fc52016-12-14 17:38:51 +0100505 ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800506 if (ret < 0)
507 goto fuel_gauge_read_err;
508
Hans de Goede4949fc52016-12-14 17:38:51 +0100509 val->intval = ret * FG_DES_CAP_RES_LSB;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800510 break;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800511 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Hans de Goede888f9742016-12-14 17:38:53 +0100512 val->intval = PROP_VOLT(info->max_volt);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800513 break;
514 default:
515 mutex_unlock(&info->lock);
516 return -EINVAL;
517 }
518
519 mutex_unlock(&info->lock);
520 return 0;
521
522fuel_gauge_read_err:
523 mutex_unlock(&info->lock);
524 return ret;
525}
526
527static int fuel_gauge_set_property(struct power_supply *ps,
528 enum power_supply_property prop,
529 const union power_supply_propval *val)
530{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100531 struct axp288_fg_info *info = power_supply_get_drvdata(ps);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800532 int ret = 0;
533
534 mutex_lock(&info->lock);
535 switch (prop) {
Todd Brandt5a5bf492015-02-04 16:24:38 -0800536 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
537 if ((val->intval < 0) || (val->intval > 15)) {
538 ret = -EINVAL;
539 break;
540 }
541 ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
542 if (ret < 0)
543 break;
544 ret &= 0xf0;
545 ret |= (val->intval & 0xf);
546 ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, ret);
547 break;
548 default:
549 ret = -EINVAL;
550 break;
551 }
552
553 mutex_unlock(&info->lock);
554 return ret;
555}
556
557static int fuel_gauge_property_is_writeable(struct power_supply *psy,
558 enum power_supply_property psp)
559{
560 int ret;
561
562 switch (psp) {
Todd Brandt5a5bf492015-02-04 16:24:38 -0800563 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
564 ret = 1;
565 break;
566 default:
567 ret = 0;
568 }
569
570 return ret;
571}
572
Todd Brandt5a5bf492015-02-04 16:24:38 -0800573static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev)
574{
575 struct axp288_fg_info *info = dev;
576 int i;
577
578 for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
579 if (info->irq[i] == irq)
580 break;
581 }
582
583 if (i >= AXP288_FG_INTR_NUM) {
584 dev_warn(&info->pdev->dev, "spurious interrupt!!\n");
585 return IRQ_NONE;
586 }
587
588 switch (i) {
589 case QWBTU_IRQ:
590 dev_info(&info->pdev->dev,
591 "Quit Battery under temperature in work mode IRQ (QWBTU)\n");
592 break;
593 case WBTU_IRQ:
594 dev_info(&info->pdev->dev,
595 "Battery under temperature in work mode IRQ (WBTU)\n");
596 break;
597 case QWBTO_IRQ:
598 dev_info(&info->pdev->dev,
599 "Quit Battery over temperature in work mode IRQ (QWBTO)\n");
600 break;
601 case WBTO_IRQ:
602 dev_info(&info->pdev->dev,
603 "Battery over temperature in work mode IRQ (WBTO)\n");
604 break;
605 case WL2_IRQ:
606 dev_info(&info->pdev->dev, "Low Batt Warning(2) INTR\n");
607 break;
608 case WL1_IRQ:
609 dev_info(&info->pdev->dev, "Low Batt Warning(1) INTR\n");
610 break;
611 default:
612 dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n");
613 }
614
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100615 power_supply_changed(info->bat);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800616 return IRQ_HANDLED;
617}
618
619static void fuel_gauge_external_power_changed(struct power_supply *psy)
620{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100621 struct axp288_fg_info *info = power_supply_get_drvdata(psy);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800622
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100623 power_supply_changed(info->bat);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800624}
625
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100626static const struct power_supply_desc fuel_gauge_desc = {
627 .name = DEV_NAME,
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,
635};
636
Todd Brandt5a5bf492015-02-04 16:24:38 -0800637static void fuel_gauge_init_irq(struct axp288_fg_info *info)
638{
639 int ret, i, pirq;
640
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",
647 pirq, info->irq[i]);
648 info->irq[i] = -1;
649 goto intr_failed;
650 }
651 ret = request_threaded_irq(info->irq[i],
652 NULL, fuel_gauge_thread_handler,
653 IRQF_ONESHOT, DEV_NAME, info);
654 if (ret) {
655 dev_warn(&info->pdev->dev,
656 "request irq failed for IRQ %d: %d\n",
657 pirq, info->irq[i]);
658 info->irq[i] = -1;
659 goto intr_failed;
660 } else {
661 dev_info(&info->pdev->dev, "HW IRQ %d -> VIRQ %d\n",
662 pirq, info->irq[i]);
663 }
664 }
665 return;
666
667intr_failed:
668 for (; i > 0; i--) {
669 free_irq(info->irq[i - 1], info);
670 info->irq[i - 1] = -1;
671 }
672}
673
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100674/*
675 * Some devices have no battery (HDMI sticks) and the axp288 battery's
676 * detection reports one despite it not being there.
Hans de Goede6f3ed832019-08-25 17:41:51 +0200677 * Please keep this listed sorted alphabetically.
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100678 */
Hans de Goede0973e962021-06-17 21:27:01 +0200679static const struct dmi_system_id axp288_no_battery_list[] = {
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100680 {
Hans de Goede9274c782019-04-22 22:43:01 +0200681 /* ACEPC T8 Cherry Trail Z8350 mini PC */
682 .matches = {
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"),
688 },
689 },
690 {
691 /* ACEPC T11 Cherry Trail Z8350 mini PC */
692 .matches = {
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"),
698 },
699 },
700 {
Hans de Goede6f3ed832019-08-25 17:41:51 +0200701 /* ECS EF20EA */
702 .matches = {
703 DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
704 },
705 },
706 {
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100707 /* Intel Cherry Trail Compute Stick, Windows version */
708 .matches = {
Jeffery Millere42fe5b2020-02-25 16:59:41 -0600709 DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100710 DMI_MATCH(DMI_PRODUCT_NAME, "STK1AW32SC"),
711 },
712 },
713 {
714 /* Intel Cherry Trail Compute Stick, version without an OS */
715 .matches = {
Jeffery Millere42fe5b2020-02-25 16:59:41 -0600716 DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100717 DMI_MATCH(DMI_PRODUCT_NAME, "STK1A32SC"),
718 },
719 },
720 {
Rafael Gandolfi4ac54b82020-04-06 16:05:38 +0200721 /* Meegopad T02 */
722 .matches = {
723 DMI_MATCH(DMI_PRODUCT_NAME, "MEEGOPAD T02"),
724 },
725 },
726 {
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100727 /* Meegopad T08 */
728 .matches = {
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"),
733 },
734 },
Hans de Goede6b714ea2021-01-12 11:36:05 +0100735 { /* Mele PCG03 Mini PC */
736 .matches = {
737 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
738 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
739 },
740 },
Hans de Goedefa7da742019-08-25 17:41:52 +0200741 {
742 /* Minix Neo Z83-4 mini PC */
743 .matches = {
744 DMI_MATCH(DMI_SYS_VENDOR, "MINIX"),
745 DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
746 }
747 },
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100748 {}
749};
750
Todd Brandt5a5bf492015-02-04 16:24:38 -0800751static int axp288_fuel_gauge_probe(struct platform_device *pdev)
752{
Hans de Goede331645e2017-12-26 13:59:10 +0100753 int i, ret = 0;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800754 struct axp288_fg_info *info;
755 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100756 struct power_supply_config psy_cfg = {};
Hans de Goede331645e2017-12-26 13:59:10 +0100757 static const char * const iio_chan_name[] = {
758 [BAT_TEMP] = "axp288-batt-temp",
759 [PMIC_TEMP] = "axp288-pmic-temp",
760 [SYSTEM_TEMP] = "axp288-system-temp",
761 [BAT_CHRG_CURR] = "axp288-chrg-curr",
762 [BAT_D_CURR] = "axp288-chrg-d-curr",
763 [BAT_VOLT] = "axp288-batt-volt",
764 };
Hans de Goede04d6f722018-04-18 14:08:21 +0200765 unsigned int val;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800766
Hans de Goede0973e962021-06-17 21:27:01 +0200767 if (dmi_check_system(axp288_no_battery_list))
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100768 return -ENODEV;
769
Hans de Goede04d6f722018-04-18 14:08:21 +0200770 /*
771 * On some devices the fuelgauge and charger parts of the axp288 are
772 * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
773 */
774 ret = regmap_read(axp20x->regmap, AXP20X_CC_CTRL, &val);
775 if (ret < 0)
776 return ret;
777 if (val == 0)
778 return -ENODEV;
779
Todd Brandt5a5bf492015-02-04 16:24:38 -0800780 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
781 if (!info)
782 return -ENOMEM;
783
784 info->pdev = pdev;
785 info->regmap = axp20x->regmap;
786 info->regmap_irqc = axp20x->regmap_irqc;
787 info->status = POWER_SUPPLY_STATUS_UNKNOWN;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800788
789 platform_set_drvdata(pdev, info);
790
791 mutex_init(&info->lock);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800792
Hans de Goede331645e2017-12-26 13:59:10 +0100793 for (i = 0; i < IIO_CHANNEL_NUM; i++) {
794 /*
795 * Note cannot use devm_iio_channel_get because x86 systems
796 * lack the device<->channel maps which iio_channel_get will
797 * try to use when passed a non NULL device pointer.
798 */
799 info->iio_channel[i] =
800 iio_channel_get(NULL, iio_chan_name[i]);
801 if (IS_ERR(info->iio_channel[i])) {
802 ret = PTR_ERR(info->iio_channel[i]);
803 dev_dbg(&pdev->dev, "error getting iiochan %s: %d\n",
804 iio_chan_name[i], ret);
805 /* Wait for axp288_adc to load */
806 if (ret == -ENODEV)
807 ret = -EPROBE_DEFER;
808
809 goto out_free_iio_chan;
810 }
811 }
812
Hans de Goede888f9742016-12-14 17:38:53 +0100813 ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG);
814 if (ret < 0)
Hans de Goede331645e2017-12-26 13:59:10 +0100815 goto out_free_iio_chan;
Hans de Goede888f9742016-12-14 17:38:53 +0100816
817 if (!(ret & FG_DES_CAP1_VALID)) {
818 dev_err(&pdev->dev, "axp288 not configured by firmware\n");
Hans de Goede331645e2017-12-26 13:59:10 +0100819 ret = -ENODEV;
820 goto out_free_iio_chan;
Hans de Goede888f9742016-12-14 17:38:53 +0100821 }
822
823 ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1);
824 if (ret < 0)
Hans de Goede331645e2017-12-26 13:59:10 +0100825 goto out_free_iio_chan;
Hans de Goede888f9742016-12-14 17:38:53 +0100826 switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) {
827 case CHRG_CCCV_CV_4100MV:
828 info->max_volt = 4100;
829 break;
830 case CHRG_CCCV_CV_4150MV:
831 info->max_volt = 4150;
832 break;
833 case CHRG_CCCV_CV_4200MV:
834 info->max_volt = 4200;
835 break;
836 case CHRG_CCCV_CV_4350MV:
837 info->max_volt = 4350;
838 break;
839 }
840
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100841 psy_cfg.drv_data = info;
842 info->bat = power_supply_register(&pdev->dev, &fuel_gauge_desc, &psy_cfg);
843 if (IS_ERR(info->bat)) {
844 ret = PTR_ERR(info->bat);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800845 dev_err(&pdev->dev, "failed to register battery: %d\n", ret);
Hans de Goede331645e2017-12-26 13:59:10 +0100846 goto out_free_iio_chan;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800847 }
848
849 fuel_gauge_create_debugfs(info);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800850 fuel_gauge_init_irq(info);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800851
Hans de Goede888f9742016-12-14 17:38:53 +0100852 return 0;
Hans de Goede331645e2017-12-26 13:59:10 +0100853
854out_free_iio_chan:
855 for (i = 0; i < IIO_CHANNEL_NUM; i++)
856 if (!IS_ERR_OR_NULL(info->iio_channel[i]))
857 iio_channel_release(info->iio_channel[i]);
858
859 return ret;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800860}
861
Krzysztof Kozlowskif1f27a42015-05-02 00:46:43 +0900862static const struct platform_device_id axp288_fg_id_table[] = {
Todd Brandt5a5bf492015-02-04 16:24:38 -0800863 { .name = DEV_NAME },
864 {},
865};
Javier Martinez Canillas99e33fb2016-10-17 16:13:36 -0300866MODULE_DEVICE_TABLE(platform, axp288_fg_id_table);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800867
868static int axp288_fuel_gauge_remove(struct platform_device *pdev)
869{
870 struct axp288_fg_info *info = platform_get_drvdata(pdev);
871 int i;
872
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100873 power_supply_unregister(info->bat);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800874 fuel_gauge_remove_debugfs(info);
875
876 for (i = 0; i < AXP288_FG_INTR_NUM; i++)
877 if (info->irq[i] >= 0)
878 free_irq(info->irq[i], info);
879
Hans de Goede331645e2017-12-26 13:59:10 +0100880 for (i = 0; i < IIO_CHANNEL_NUM; i++)
881 iio_channel_release(info->iio_channel[i]);
882
Todd Brandt5a5bf492015-02-04 16:24:38 -0800883 return 0;
884}
885
886static struct platform_driver axp288_fuel_gauge_driver = {
887 .probe = axp288_fuel_gauge_probe,
888 .remove = axp288_fuel_gauge_remove,
889 .id_table = axp288_fg_id_table,
890 .driver = {
891 .name = DEV_NAME,
892 },
893};
894
895module_platform_driver(axp288_fuel_gauge_driver);
896
Ramakrishna Pallala409e7182015-03-13 21:49:09 +0530897MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
Todd Brandt5a5bf492015-02-04 16:24:38 -0800898MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>");
899MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver");
900MODULE_LICENSE("GPL");