blob: c1da217fdb0e2bb4cf7bc4b7c461958a9f3724dd [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 *
Andrejus Basovas394088f2021-08-01 15:31:03 +02005 * Copyright (C) 2020-2021 Andrejus Basovas <xxx@yyy.tld>
6 * Copyright (C) 2016-2021 Hans de Goede <hdegoede@redhat.com>
Todd Brandt5a5bf492015-02-04 16:24:38 -08007 * Copyright (C) 2014 Intel Corporation
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Todd Brandt5a5bf492015-02-04 16:24:38 -080010 */
11
Hans de Goedeb60c75b2017-12-26 13:59:13 +010012#include <linux/dmi.h>
Todd Brandt5a5bf492015-02-04 16:24:38 -080013#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/device.h>
16#include <linux/regmap.h>
17#include <linux/jiffies.h>
18#include <linux/interrupt.h>
Todd Brandt5a5bf492015-02-04 16:24:38 -080019#include <linux/mfd/axp20x.h>
20#include <linux/platform_device.h>
21#include <linux/power_supply.h>
22#include <linux/iio/consumer.h>
Hans de Goede4949fc52016-12-14 17:38:51 +010023#include <asm/unaligned.h>
Andrejus Basovas394088f2021-08-01 15:31:03 +020024#include <asm/iosf_mbi.h>
Todd Brandt5a5bf492015-02-04 16:24:38 -080025
Hans de Goedef9ac9732021-08-01 15:30:56 +020026#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)
Hans de Goede2b5a4b42017-12-26 13:59:11 +010031
Hans de Goedef9ac9732021-08-01 15:30:56 +020032#define CHRG_STAT_BAT_SAFE_MODE (1 << 3)
Todd Brandt5a5bf492015-02-04 16:24:38 -080033#define CHRG_STAT_BAT_VALID (1 << 4)
Hans de Goedef9ac9732021-08-01 15:30:56 +020034#define CHRG_STAT_BAT_PRESENT (1 << 5)
Todd Brandt5a5bf492015-02-04 16:24:38 -080035#define CHRG_STAT_CHARGING (1 << 6)
36#define CHRG_STAT_PMIC_OTP (1 << 7)
37
38#define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
Hans de Goedef9ac9732021-08-01 15:30:56 +020039#define CHRG_CCCV_CC_BIT_POS 0
Todd Brandt5a5bf492015-02-04 16:24:38 -080040#define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
Hans de Goedef9ac9732021-08-01 15:30:56 +020041#define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
Todd Brandt5a5bf492015-02-04 16:24:38 -080042#define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */
43#define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
Hans de Goedef9ac9732021-08-01 15:30:56 +020044#define CHRG_CCCV_CV_BIT_POS 5
Todd Brandt5a5bf492015-02-04 16:24:38 -080045#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
Hans de Goedef9ac9732021-08-01 15:30:56 +020051#define FG_CNTL_OCV_ADJ_STAT (1 << 2)
Todd Brandt5a5bf492015-02-04 16:24:38 -080052#define FG_CNTL_OCV_ADJ_EN (1 << 3)
Hans de Goedef9ac9732021-08-01 15:30:56 +020053#define FG_CNTL_CAP_ADJ_STAT (1 << 4)
Todd Brandt5a5bf492015-02-04 16:24:38 -080054#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
Hans de Goedef9ac9732021-08-01 15:30:56 +020074#define FG_LOW_CAP_THR1_MASK 0xf0 /* 5% tp 20% */
Todd Brandt5a5bf492015-02-04 16:24:38 -080075#define FG_LOW_CAP_THR1_VAL 0xa0 /* 15 perc */
Hans de Goedef9ac9732021-08-01 15:30:56 +020076#define FG_LOW_CAP_THR2_MASK 0x0f /* 0% to 15% */
Todd Brandt5a5bf492015-02-04 16:24:38 -080077#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
Hans de Goedef9ac9732021-08-01 15:30:56 +020081#define DEV_NAME "axp288_fuel_gauge"
Todd Brandt5a5bf492015-02-04 16:24:38 -080082
83/* 1.1mV per LSB expressed in uV */
84#define VOLTAGE_FROM_ADC(a) ((a * 11) / 10)
Hans de Goede888f9742016-12-14 17:38:53 +010085/* properties converted to uV, uA */
Hans de Goedef9ac9732021-08-01 15:30:56 +020086#define PROP_VOLT(a) ((a) * 1000)
87#define PROP_CURR(a) ((a) * 1000)
Todd Brandt5a5bf492015-02-04 16:24:38 -080088
Andrejus Basovas394088f2021-08-01 15:31:03 +020089#define AXP288_REG_UPDATE_INTERVAL (60 * HZ)
Hans de Goedef9ac9732021-08-01 15:30:56 +020090#define AXP288_FG_INTR_NUM 6
Todd Brandt5a5bf492015-02-04 16:24:38 -080091enum {
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 {
Hans de Goede331645e2017-12-26 13:59:10 +0100101 BAT_CHRG_CURR,
102 BAT_D_CURR,
103 BAT_VOLT,
104 IIO_CHANNEL_NUM
105};
106
Todd Brandt5a5bf492015-02-04 16:24:38 -0800107struct axp288_fg_info {
Hans de Goede7eef3e62021-08-01 15:31:01 +0200108 struct device *dev;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800109 struct regmap *regmap;
110 struct regmap_irq_chip_data *regmap_irqc;
111 int irq[AXP288_FG_INTR_NUM];
Hans de Goede331645e2017-12-26 13:59:10 +0100112 struct iio_channel *iio_channel[IIO_CHANNEL_NUM];
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100113 struct power_supply *bat;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800114 struct mutex lock;
115 int status;
Hans de Goede888f9742016-12-14 17:38:53 +0100116 int max_volt;
Hans de Goedec371d442021-08-01 15:31:02 +0200117 int pwr_op;
118 int low_cap;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800119 struct dentry *debug_file;
Andrejus Basovas394088f2021-08-01 15:31:03 +0200120
121 char valid; /* zero until following fields are valid */
122 unsigned long last_updated; /* in jiffies */
123
124 int pwr_stat;
125 int fg_res;
126 int bat_volt;
127 int d_curr;
128 int c_curr;
129 int ocv;
130 int fg_cc_mtr1;
131 int fg_des_cap1;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800132};
133
134static enum power_supply_property fuel_gauge_props[] = {
135 POWER_SUPPLY_PROP_STATUS,
136 POWER_SUPPLY_PROP_PRESENT,
137 POWER_SUPPLY_PROP_HEALTH,
138 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
Todd Brandt5a5bf492015-02-04 16:24:38 -0800139 POWER_SUPPLY_PROP_VOLTAGE_NOW,
140 POWER_SUPPLY_PROP_VOLTAGE_OCV,
141 POWER_SUPPLY_PROP_CURRENT_NOW,
142 POWER_SUPPLY_PROP_CAPACITY,
143 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
Todd Brandt5a5bf492015-02-04 16:24:38 -0800144 POWER_SUPPLY_PROP_TECHNOLOGY,
145 POWER_SUPPLY_PROP_CHARGE_FULL,
146 POWER_SUPPLY_PROP_CHARGE_NOW,
Todd Brandt5a5bf492015-02-04 16:24:38 -0800147};
148
149static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg)
150{
Todd Brandt5a5bf492015-02-04 16:24:38 -0800151 unsigned int val;
Hans de Goedef17bda72021-08-01 15:31:00 +0200152 int ret;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800153
Hans de Goedef17bda72021-08-01 15:31:00 +0200154 ret = regmap_read(info->regmap, reg, &val);
Hans de Goede6f074bc2016-12-14 17:38:50 +0100155 if (ret < 0) {
Hans de Goede7eef3e62021-08-01 15:31:01 +0200156 dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret);
Hans de Goede6f074bc2016-12-14 17:38:50 +0100157 return ret;
158 }
Todd Brandt5a5bf492015-02-04 16:24:38 -0800159
160 return val;
161}
162
163static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val)
164{
165 int ret;
166
167 ret = regmap_write(info->regmap, reg, (unsigned int)val);
168
169 if (ret < 0)
Hans de Goede7eef3e62021-08-01 15:31:01 +0200170 dev_err(info->dev, "Error writing reg 0x%02x err: %d\n", reg, ret);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800171
172 return ret;
173}
174
Hans de Goede4949fc52016-12-14 17:38:51 +0100175static int fuel_gauge_read_15bit_word(struct axp288_fg_info *info, int reg)
176{
177 unsigned char buf[2];
178 int ret;
179
180 ret = regmap_bulk_read(info->regmap, reg, buf, 2);
181 if (ret < 0) {
Hans de Goede7eef3e62021-08-01 15:31:01 +0200182 dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret);
Hans de Goede4949fc52016-12-14 17:38:51 +0100183 return ret;
184 }
185
186 ret = get_unaligned_be16(buf);
187 if (!(ret & FG_15BIT_WORD_VALID)) {
Hans de Goede7eef3e62021-08-01 15:31:01 +0200188 dev_err(info->dev, "Error reg 0x%02x contents not valid\n", reg);
Hans de Goede4949fc52016-12-14 17:38:51 +0100189 return -ENXIO;
190 }
191
192 return ret & FG_15BIT_VAL_MASK;
193}
194
Hans de Goede248efcf2016-12-14 17:38:52 +0100195static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg)
196{
197 unsigned char buf[2];
198 int ret;
199
200 ret = regmap_bulk_read(info->regmap, reg, buf, 2);
201 if (ret < 0) {
Hans de Goede7eef3e62021-08-01 15:31:01 +0200202 dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret);
Hans de Goede248efcf2016-12-14 17:38:52 +0100203 return ret;
204 }
205
206 /* 12-bit data values have upper 8 bits in buf[0], lower 4 in buf[1] */
207 return (buf[0] << 4) | ((buf[1] >> 4) & 0x0f);
208}
209
Andrejus Basovas394088f2021-08-01 15:31:03 +0200210static int fuel_gauge_update_registers(struct axp288_fg_info *info)
211{
212 int ret;
213
214 if (info->valid && time_before(jiffies, info->last_updated + AXP288_REG_UPDATE_INTERVAL))
215 return 0;
216
217 dev_dbg(info->dev, "Fuel Gauge updating register values...\n");
218
219 ret = iosf_mbi_block_punit_i2c_access();
220 if (ret < 0)
221 return ret;
222
223 ret = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS);
224 if (ret < 0)
225 goto out;
226 info->pwr_stat = ret;
227
228 ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES);
229 if (ret < 0)
230 goto out;
231 info->fg_res = ret;
232
233 ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &info->bat_volt);
234 if (ret < 0)
235 goto out;
236
237 if (info->pwr_stat & PS_STAT_BAT_CHRG_DIR) {
238 info->d_curr = 0;
239 ret = iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], &info->c_curr);
240 if (ret < 0)
241 goto out;
242 } else {
243 info->c_curr = 0;
244 ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &info->d_curr);
245 if (ret < 0)
246 goto out;
247 }
248
249 ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG);
250 if (ret < 0)
251 goto out;
252 info->ocv = ret;
253
254 ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG);
255 if (ret < 0)
256 goto out;
257 info->fg_cc_mtr1 = ret;
258
259 ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG);
260 if (ret < 0)
261 goto out;
262 info->fg_des_cap1 = ret;
263
264 info->last_updated = jiffies;
265 info->valid = 1;
266 ret = 0;
267out:
268 iosf_mbi_unblock_punit_i2c_access();
269 return ret;
270}
271
Todd Brandt5a5bf492015-02-04 16:24:38 -0800272static void fuel_gauge_get_status(struct axp288_fg_info *info)
273{
Andrejus Basovas394088f2021-08-01 15:31:03 +0200274 int pwr_stat = info->pwr_stat;
275 int fg_res = info->fg_res;
276 int curr = info->d_curr;
Hans de Goede2b5a4b42017-12-26 13:59:11 +0100277
278 /* Report full if Vbus is valid and the reported capacity is 100% */
Hans de Goedef4516552018-02-15 15:00:36 +0100279 if (!(pwr_stat & PS_STAT_VBUS_VALID))
280 goto not_full;
281
Hans de Goedef4516552018-02-15 15:00:36 +0100282 if (!(fg_res & FG_REP_CAP_VALID))
283 goto not_full;
284
285 fg_res &= ~FG_REP_CAP_VALID;
286 if (fg_res == 100) {
287 info->status = POWER_SUPPLY_STATUS_FULL;
288 return;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800289 }
290
Hans de Goedef4516552018-02-15 15:00:36 +0100291 /*
292 * Sometimes the charger turns itself off before fg-res reaches 100%.
293 * When this happens the AXP288 reports a not-charging status and
294 * 0 mA discharge current.
295 */
296 if (fg_res < 90 || (pwr_stat & PS_STAT_BAT_CHRG_DIR))
297 goto not_full;
298
Hans de Goedef4516552018-02-15 15:00:36 +0100299 if (curr == 0) {
300 info->status = POWER_SUPPLY_STATUS_FULL;
301 return;
302 }
303
304not_full:
Hans de Goede2b5a4b42017-12-26 13:59:11 +0100305 if (pwr_stat & PS_STAT_BAT_CHRG_DIR)
Todd Brandt5a5bf492015-02-04 16:24:38 -0800306 info->status = POWER_SUPPLY_STATUS_CHARGING;
Hans de Goede2b5a4b42017-12-26 13:59:11 +0100307 else
Todd Brandt5a5bf492015-02-04 16:24:38 -0800308 info->status = POWER_SUPPLY_STATUS_DISCHARGING;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800309}
310
Todd Brandt5a5bf492015-02-04 16:24:38 -0800311static int fuel_gauge_battery_health(struct axp288_fg_info *info)
312{
Andrejus Basovas394088f2021-08-01 15:31:03 +0200313 int vocv = VOLTAGE_FROM_ADC(info->ocv);
314 int health = POWER_SUPPLY_HEALTH_UNKNOWN;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800315
Hans de Goede888f9742016-12-14 17:38:53 +0100316 if (vocv > info->max_volt)
Todd Brandt5a5bf492015-02-04 16:24:38 -0800317 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800318 else
319 health = POWER_SUPPLY_HEALTH_GOOD;
320
Todd Brandt5a5bf492015-02-04 16:24:38 -0800321 return health;
322}
323
Todd Brandt5a5bf492015-02-04 16:24:38 -0800324static int fuel_gauge_get_property(struct power_supply *ps,
325 enum power_supply_property prop,
326 union power_supply_propval *val)
327{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100328 struct axp288_fg_info *info = power_supply_get_drvdata(ps);
Andrejus Basovas394088f2021-08-01 15:31:03 +0200329 int ret, value;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800330
331 mutex_lock(&info->lock);
Andrejus Basovas394088f2021-08-01 15:31:03 +0200332
333 ret = fuel_gauge_update_registers(info);
334 if (ret < 0)
335 goto out;
336
Todd Brandt5a5bf492015-02-04 16:24:38 -0800337 switch (prop) {
338 case POWER_SUPPLY_PROP_STATUS:
339 fuel_gauge_get_status(info);
340 val->intval = info->status;
341 break;
342 case POWER_SUPPLY_PROP_HEALTH:
343 val->intval = fuel_gauge_battery_health(info);
344 break;
345 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Andrejus Basovas394088f2021-08-01 15:31:03 +0200346 value = VOLTAGE_FROM_ADC(info->bat_volt);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800347 val->intval = PROP_VOLT(value);
348 break;
349 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
Andrejus Basovas394088f2021-08-01 15:31:03 +0200350 value = VOLTAGE_FROM_ADC(info->ocv);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800351 val->intval = PROP_VOLT(value);
352 break;
353 case POWER_SUPPLY_PROP_CURRENT_NOW:
Andrejus Basovas394088f2021-08-01 15:31:03 +0200354 if (info->d_curr > 0)
355 value = -1 * info->d_curr;
356 else
357 value = info->c_curr;
358
Todd Brandt5a5bf492015-02-04 16:24:38 -0800359 val->intval = PROP_CURR(value);
360 break;
361 case POWER_SUPPLY_PROP_PRESENT:
Hans de Goedec371d442021-08-01 15:31:02 +0200362 if (info->pwr_op & CHRG_STAT_BAT_PRESENT)
Todd Brandt5a5bf492015-02-04 16:24:38 -0800363 val->intval = 1;
364 else
365 val->intval = 0;
366 break;
367 case POWER_SUPPLY_PROP_CAPACITY:
Andrejus Basovas394088f2021-08-01 15:31:03 +0200368 if (!(info->fg_res & FG_REP_CAP_VALID))
Hans de Goede7eef3e62021-08-01 15:31:01 +0200369 dev_err(info->dev, "capacity measurement not valid\n");
Andrejus Basovas394088f2021-08-01 15:31:03 +0200370 val->intval = (info->fg_res & FG_REP_CAP_VAL_MASK);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800371 break;
372 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
Hans de Goedec371d442021-08-01 15:31:02 +0200373 val->intval = (info->low_cap & 0x0f);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800374 break;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800375 case POWER_SUPPLY_PROP_TECHNOLOGY:
376 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
377 break;
378 case POWER_SUPPLY_PROP_CHARGE_NOW:
Andrejus Basovas394088f2021-08-01 15:31:03 +0200379 val->intval = info->fg_cc_mtr1 * FG_DES_CAP_RES_LSB;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800380 break;
381 case POWER_SUPPLY_PROP_CHARGE_FULL:
Andrejus Basovas394088f2021-08-01 15:31:03 +0200382 val->intval = info->fg_des_cap1 * FG_DES_CAP_RES_LSB;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800383 break;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800384 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Hans de Goede888f9742016-12-14 17:38:53 +0100385 val->intval = PROP_VOLT(info->max_volt);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800386 break;
387 default:
Andrejus Basovas394088f2021-08-01 15:31:03 +0200388 ret = -EINVAL;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800389 }
390
Andrejus Basovas394088f2021-08-01 15:31:03 +0200391out:
Todd Brandt5a5bf492015-02-04 16:24:38 -0800392 mutex_unlock(&info->lock);
393 return ret;
394}
395
396static int fuel_gauge_set_property(struct power_supply *ps,
397 enum power_supply_property prop,
398 const union power_supply_propval *val)
399{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100400 struct axp288_fg_info *info = power_supply_get_drvdata(ps);
Hans de Goedec371d442021-08-01 15:31:02 +0200401 int new_low_cap, ret = 0;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800402
403 mutex_lock(&info->lock);
404 switch (prop) {
Todd Brandt5a5bf492015-02-04 16:24:38 -0800405 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
406 if ((val->intval < 0) || (val->intval > 15)) {
407 ret = -EINVAL;
408 break;
409 }
Hans de Goedec371d442021-08-01 15:31:02 +0200410 new_low_cap = info->low_cap;
411 new_low_cap &= 0xf0;
412 new_low_cap |= (val->intval & 0xf);
413 ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, new_low_cap);
414 if (ret == 0)
415 info->low_cap = new_low_cap;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800416 break;
417 default:
418 ret = -EINVAL;
419 break;
420 }
421
422 mutex_unlock(&info->lock);
423 return ret;
424}
425
426static int fuel_gauge_property_is_writeable(struct power_supply *psy,
427 enum power_supply_property psp)
428{
429 int ret;
430
431 switch (psp) {
Todd Brandt5a5bf492015-02-04 16:24:38 -0800432 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
433 ret = 1;
434 break;
435 default:
436 ret = 0;
437 }
438
439 return ret;
440}
441
Todd Brandt5a5bf492015-02-04 16:24:38 -0800442static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev)
443{
444 struct axp288_fg_info *info = dev;
445 int i;
446
447 for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
448 if (info->irq[i] == irq)
449 break;
450 }
451
452 if (i >= AXP288_FG_INTR_NUM) {
Hans de Goede7eef3e62021-08-01 15:31:01 +0200453 dev_warn(info->dev, "spurious interrupt!!\n");
Todd Brandt5a5bf492015-02-04 16:24:38 -0800454 return IRQ_NONE;
455 }
456
457 switch (i) {
458 case QWBTU_IRQ:
Hans de Goede7eef3e62021-08-01 15:31:01 +0200459 dev_info(info->dev, "Quit Battery under temperature in work mode IRQ (QWBTU)\n");
Todd Brandt5a5bf492015-02-04 16:24:38 -0800460 break;
461 case WBTU_IRQ:
Hans de Goede7eef3e62021-08-01 15:31:01 +0200462 dev_info(info->dev, "Battery under temperature in work mode IRQ (WBTU)\n");
Todd Brandt5a5bf492015-02-04 16:24:38 -0800463 break;
464 case QWBTO_IRQ:
Hans de Goede7eef3e62021-08-01 15:31:01 +0200465 dev_info(info->dev, "Quit Battery over temperature in work mode IRQ (QWBTO)\n");
Todd Brandt5a5bf492015-02-04 16:24:38 -0800466 break;
467 case WBTO_IRQ:
Hans de Goede7eef3e62021-08-01 15:31:01 +0200468 dev_info(info->dev, "Battery over temperature in work mode IRQ (WBTO)\n");
Todd Brandt5a5bf492015-02-04 16:24:38 -0800469 break;
470 case WL2_IRQ:
Hans de Goede7eef3e62021-08-01 15:31:01 +0200471 dev_info(info->dev, "Low Batt Warning(2) INTR\n");
Todd Brandt5a5bf492015-02-04 16:24:38 -0800472 break;
473 case WL1_IRQ:
Hans de Goede7eef3e62021-08-01 15:31:01 +0200474 dev_info(info->dev, "Low Batt Warning(1) INTR\n");
Todd Brandt5a5bf492015-02-04 16:24:38 -0800475 break;
476 default:
Hans de Goede7eef3e62021-08-01 15:31:01 +0200477 dev_warn(info->dev, "Spurious Interrupt!!!\n");
Todd Brandt5a5bf492015-02-04 16:24:38 -0800478 }
479
Andrejus Basovas394088f2021-08-01 15:31:03 +0200480 info->valid = 0; /* Force updating of the cached registers */
481
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100482 power_supply_changed(info->bat);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800483 return IRQ_HANDLED;
484}
485
486static void fuel_gauge_external_power_changed(struct power_supply *psy)
487{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100488 struct axp288_fg_info *info = power_supply_get_drvdata(psy);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800489
Andrejus Basovas394088f2021-08-01 15:31:03 +0200490 info->valid = 0; /* Force updating of the cached registers */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100491 power_supply_changed(info->bat);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800492}
493
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100494static const struct power_supply_desc fuel_gauge_desc = {
495 .name = DEV_NAME,
496 .type = POWER_SUPPLY_TYPE_BATTERY,
497 .properties = fuel_gauge_props,
498 .num_properties = ARRAY_SIZE(fuel_gauge_props),
499 .get_property = fuel_gauge_get_property,
500 .set_property = fuel_gauge_set_property,
501 .property_is_writeable = fuel_gauge_property_is_writeable,
502 .external_power_changed = fuel_gauge_external_power_changed,
503};
504
Hans de Goede7eef3e62021-08-01 15:31:01 +0200505static void fuel_gauge_init_irq(struct axp288_fg_info *info, struct platform_device *pdev)
Todd Brandt5a5bf492015-02-04 16:24:38 -0800506{
507 int ret, i, pirq;
508
509 for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
Hans de Goede7eef3e62021-08-01 15:31:01 +0200510 pirq = platform_get_irq(pdev, i);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800511 info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
512 if (info->irq[i] < 0) {
Hans de Goede7eef3e62021-08-01 15:31:01 +0200513 dev_warn(info->dev, "regmap_irq get virq failed for IRQ %d: %d\n",
Todd Brandt5a5bf492015-02-04 16:24:38 -0800514 pirq, info->irq[i]);
515 info->irq[i] = -1;
516 goto intr_failed;
517 }
518 ret = request_threaded_irq(info->irq[i],
519 NULL, fuel_gauge_thread_handler,
520 IRQF_ONESHOT, DEV_NAME, info);
521 if (ret) {
Hans de Goede7eef3e62021-08-01 15:31:01 +0200522 dev_warn(info->dev, "request irq failed for IRQ %d: %d\n",
Todd Brandt5a5bf492015-02-04 16:24:38 -0800523 pirq, info->irq[i]);
524 info->irq[i] = -1;
525 goto intr_failed;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800526 }
527 }
528 return;
529
530intr_failed:
531 for (; i > 0; i--) {
532 free_irq(info->irq[i - 1], info);
533 info->irq[i - 1] = -1;
534 }
535}
536
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100537/*
538 * Some devices have no battery (HDMI sticks) and the axp288 battery's
539 * detection reports one despite it not being there.
Hans de Goede6f3ed832019-08-25 17:41:51 +0200540 * Please keep this listed sorted alphabetically.
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100541 */
Hans de Goede0973e962021-06-17 21:27:01 +0200542static const struct dmi_system_id axp288_no_battery_list[] = {
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100543 {
Hans de Goede9274c782019-04-22 22:43:01 +0200544 /* ACEPC T8 Cherry Trail Z8350 mini PC */
545 .matches = {
546 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
547 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"),
548 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T8"),
549 /* also match on somewhat unique bios-version */
550 DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"),
551 },
552 },
553 {
554 /* ACEPC T11 Cherry Trail Z8350 mini PC */
555 .matches = {
556 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
557 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"),
558 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T11"),
559 /* also match on somewhat unique bios-version */
560 DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"),
561 },
562 },
563 {
Hans de Goede6f3ed832019-08-25 17:41:51 +0200564 /* ECS EF20EA */
565 .matches = {
566 DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
567 },
568 },
569 {
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100570 /* Intel Cherry Trail Compute Stick, Windows version */
571 .matches = {
Jeffery Millere42fe5b2020-02-25 16:59:41 -0600572 DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100573 DMI_MATCH(DMI_PRODUCT_NAME, "STK1AW32SC"),
574 },
575 },
576 {
577 /* Intel Cherry Trail Compute Stick, version without an OS */
578 .matches = {
Jeffery Millere42fe5b2020-02-25 16:59:41 -0600579 DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100580 DMI_MATCH(DMI_PRODUCT_NAME, "STK1A32SC"),
581 },
582 },
583 {
Rafael Gandolfi4ac54b82020-04-06 16:05:38 +0200584 /* Meegopad T02 */
585 .matches = {
586 DMI_MATCH(DMI_PRODUCT_NAME, "MEEGOPAD T02"),
587 },
588 },
Hans de Goede6b714ea2021-01-12 11:36:05 +0100589 { /* Mele PCG03 Mini PC */
590 .matches = {
591 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
592 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
593 },
594 },
Hans de Goedefa7da742019-08-25 17:41:52 +0200595 {
596 /* Minix Neo Z83-4 mini PC */
597 .matches = {
598 DMI_MATCH(DMI_SYS_VENDOR, "MINIX"),
599 DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
600 }
601 },
Hans de Goede3a06b912021-06-17 21:27:02 +0200602 {
603 /* Various Ace PC/Meegopad/MinisForum/Wintel Mini-PCs/HDMI-sticks */
604 .matches = {
605 DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"),
606 DMI_MATCH(DMI_CHASSIS_TYPE, "3"),
607 DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
608 DMI_MATCH(DMI_BIOS_VERSION, "5.11"),
609 },
610 },
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100611 {}
612};
613
Todd Brandt5a5bf492015-02-04 16:24:38 -0800614static int axp288_fuel_gauge_probe(struct platform_device *pdev)
615{
Hans de Goede331645e2017-12-26 13:59:10 +0100616 int i, ret = 0;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800617 struct axp288_fg_info *info;
618 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100619 struct power_supply_config psy_cfg = {};
Hans de Goede331645e2017-12-26 13:59:10 +0100620 static const char * const iio_chan_name[] = {
Hans de Goede331645e2017-12-26 13:59:10 +0100621 [BAT_CHRG_CURR] = "axp288-chrg-curr",
622 [BAT_D_CURR] = "axp288-chrg-d-curr",
623 [BAT_VOLT] = "axp288-batt-volt",
624 };
Hans de Goede04d6f722018-04-18 14:08:21 +0200625 unsigned int val;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800626
Hans de Goede0973e962021-06-17 21:27:01 +0200627 if (dmi_check_system(axp288_no_battery_list))
Hans de Goedeb60c75b2017-12-26 13:59:13 +0100628 return -ENODEV;
629
Todd Brandt5a5bf492015-02-04 16:24:38 -0800630 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
631 if (!info)
632 return -ENOMEM;
633
Hans de Goede7eef3e62021-08-01 15:31:01 +0200634 info->dev = &pdev->dev;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800635 info->regmap = axp20x->regmap;
636 info->regmap_irqc = axp20x->regmap_irqc;
637 info->status = POWER_SUPPLY_STATUS_UNKNOWN;
Andrejus Basovas394088f2021-08-01 15:31:03 +0200638 info->valid = 0;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800639
640 platform_set_drvdata(pdev, info);
641
642 mutex_init(&info->lock);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800643
Hans de Goede331645e2017-12-26 13:59:10 +0100644 for (i = 0; i < IIO_CHANNEL_NUM; i++) {
645 /*
646 * Note cannot use devm_iio_channel_get because x86 systems
647 * lack the device<->channel maps which iio_channel_get will
648 * try to use when passed a non NULL device pointer.
649 */
650 info->iio_channel[i] =
651 iio_channel_get(NULL, iio_chan_name[i]);
652 if (IS_ERR(info->iio_channel[i])) {
653 ret = PTR_ERR(info->iio_channel[i]);
654 dev_dbg(&pdev->dev, "error getting iiochan %s: %d\n",
655 iio_chan_name[i], ret);
656 /* Wait for axp288_adc to load */
657 if (ret == -ENODEV)
658 ret = -EPROBE_DEFER;
659
660 goto out_free_iio_chan;
661 }
662 }
663
Hans de Goede213e19d62021-08-01 15:31:05 +0200664 ret = iosf_mbi_block_punit_i2c_access();
665 if (ret < 0)
666 goto out_free_iio_chan;
667
Hans de Goede964b3e92021-08-01 15:31:04 +0200668 /*
669 * On some devices the fuelgauge and charger parts of the axp288 are
670 * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
671 */
672 ret = regmap_read(axp20x->regmap, AXP20X_CC_CTRL, &val);
673 if (ret < 0)
Hans de Goede213e19d62021-08-01 15:31:05 +0200674 goto unblock_punit_i2c_access;
Hans de Goede964b3e92021-08-01 15:31:04 +0200675 if (val == 0) {
676 ret = -ENODEV;
Hans de Goede213e19d62021-08-01 15:31:05 +0200677 goto unblock_punit_i2c_access;
Hans de Goede964b3e92021-08-01 15:31:04 +0200678 }
679
Hans de Goede888f9742016-12-14 17:38:53 +0100680 ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG);
681 if (ret < 0)
Hans de Goede213e19d62021-08-01 15:31:05 +0200682 goto unblock_punit_i2c_access;
Hans de Goede888f9742016-12-14 17:38:53 +0100683
684 if (!(ret & FG_DES_CAP1_VALID)) {
685 dev_err(&pdev->dev, "axp288 not configured by firmware\n");
Hans de Goede331645e2017-12-26 13:59:10 +0100686 ret = -ENODEV;
Hans de Goede213e19d62021-08-01 15:31:05 +0200687 goto unblock_punit_i2c_access;
Hans de Goede888f9742016-12-14 17:38:53 +0100688 }
689
690 ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1);
691 if (ret < 0)
Hans de Goede213e19d62021-08-01 15:31:05 +0200692 goto unblock_punit_i2c_access;
Hans de Goede888f9742016-12-14 17:38:53 +0100693 switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) {
694 case CHRG_CCCV_CV_4100MV:
695 info->max_volt = 4100;
696 break;
697 case CHRG_CCCV_CV_4150MV:
698 info->max_volt = 4150;
699 break;
700 case CHRG_CCCV_CV_4200MV:
701 info->max_volt = 4200;
702 break;
703 case CHRG_CCCV_CV_4350MV:
704 info->max_volt = 4350;
705 break;
706 }
707
Hans de Goedec371d442021-08-01 15:31:02 +0200708 ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE);
709 if (ret < 0)
Hans de Goede213e19d62021-08-01 15:31:05 +0200710 goto unblock_punit_i2c_access;
Hans de Goedec371d442021-08-01 15:31:02 +0200711 info->pwr_op = ret;
712
713 ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
714 if (ret < 0)
Hans de Goede213e19d62021-08-01 15:31:05 +0200715 goto unblock_punit_i2c_access;
Hans de Goedec371d442021-08-01 15:31:02 +0200716 info->low_cap = ret;
717
Hans de Goede213e19d62021-08-01 15:31:05 +0200718unblock_punit_i2c_access:
719 iosf_mbi_unblock_punit_i2c_access();
720 /* In case we arrive here by goto because of a register access error */
721 if (ret < 0)
722 goto out_free_iio_chan;
723
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100724 psy_cfg.drv_data = info;
725 info->bat = power_supply_register(&pdev->dev, &fuel_gauge_desc, &psy_cfg);
726 if (IS_ERR(info->bat)) {
727 ret = PTR_ERR(info->bat);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800728 dev_err(&pdev->dev, "failed to register battery: %d\n", ret);
Hans de Goede331645e2017-12-26 13:59:10 +0100729 goto out_free_iio_chan;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800730 }
731
Hans de Goede7eef3e62021-08-01 15:31:01 +0200732 fuel_gauge_init_irq(info, pdev);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800733
Hans de Goede888f9742016-12-14 17:38:53 +0100734 return 0;
Hans de Goede331645e2017-12-26 13:59:10 +0100735
736out_free_iio_chan:
737 for (i = 0; i < IIO_CHANNEL_NUM; i++)
738 if (!IS_ERR_OR_NULL(info->iio_channel[i]))
739 iio_channel_release(info->iio_channel[i]);
740
741 return ret;
Todd Brandt5a5bf492015-02-04 16:24:38 -0800742}
743
Krzysztof Kozlowskif1f27a42015-05-02 00:46:43 +0900744static const struct platform_device_id axp288_fg_id_table[] = {
Todd Brandt5a5bf492015-02-04 16:24:38 -0800745 { .name = DEV_NAME },
746 {},
747};
Javier Martinez Canillas99e33fb2016-10-17 16:13:36 -0300748MODULE_DEVICE_TABLE(platform, axp288_fg_id_table);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800749
750static int axp288_fuel_gauge_remove(struct platform_device *pdev)
751{
752 struct axp288_fg_info *info = platform_get_drvdata(pdev);
753 int i;
754
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100755 power_supply_unregister(info->bat);
Todd Brandt5a5bf492015-02-04 16:24:38 -0800756
757 for (i = 0; i < AXP288_FG_INTR_NUM; i++)
758 if (info->irq[i] >= 0)
759 free_irq(info->irq[i], info);
760
Hans de Goede331645e2017-12-26 13:59:10 +0100761 for (i = 0; i < IIO_CHANNEL_NUM; i++)
762 iio_channel_release(info->iio_channel[i]);
763
Todd Brandt5a5bf492015-02-04 16:24:38 -0800764 return 0;
765}
766
767static struct platform_driver axp288_fuel_gauge_driver = {
768 .probe = axp288_fuel_gauge_probe,
769 .remove = axp288_fuel_gauge_remove,
770 .id_table = axp288_fg_id_table,
771 .driver = {
772 .name = DEV_NAME,
773 },
774};
775
776module_platform_driver(axp288_fuel_gauge_driver);
777
Ramakrishna Pallala409e7182015-03-13 21:49:09 +0530778MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
Todd Brandt5a5bf492015-02-04 16:24:38 -0800779MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>");
780MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver");
781MODULE_LICENSE("GPL");