Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver |
| 3 | * |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 4 | * Copyright (C) 2016-2017 Hans de Goede <hdegoede@redhat.com> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 5 | * Copyright (C) 2014 Intel Corporation |
| 6 | * |
| 7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | */ |
| 19 | |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 20 | #include <linux/dmi.h> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/regmap.h> |
| 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/interrupt.h> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 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> |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 33 | #include <asm/unaligned.h> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 34 | |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 35 | #define PS_STAT_VBUS_TRIGGER (1 << 0) |
| 36 | #define PS_STAT_BAT_CHRG_DIR (1 << 2) |
| 37 | #define PS_STAT_VBAT_ABOVE_VHOLD (1 << 3) |
| 38 | #define PS_STAT_VBUS_VALID (1 << 4) |
| 39 | #define PS_STAT_VBUS_PRESENT (1 << 5) |
| 40 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 41 | #define CHRG_STAT_BAT_SAFE_MODE (1 << 3) |
| 42 | #define CHRG_STAT_BAT_VALID (1 << 4) |
| 43 | #define CHRG_STAT_BAT_PRESENT (1 << 5) |
| 44 | #define CHRG_STAT_CHARGING (1 << 6) |
| 45 | #define CHRG_STAT_PMIC_OTP (1 << 7) |
| 46 | |
| 47 | #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */ |
| 48 | #define CHRG_CCCV_CC_BIT_POS 0 |
| 49 | #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */ |
| 50 | #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */ |
| 51 | #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */ |
| 52 | #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */ |
| 53 | #define CHRG_CCCV_CV_BIT_POS 5 |
| 54 | #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */ |
| 55 | #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */ |
| 56 | #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */ |
| 57 | #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */ |
| 58 | #define CHRG_CCCV_CHG_EN (1 << 7) |
| 59 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 60 | #define FG_CNTL_OCV_ADJ_STAT (1 << 2) |
| 61 | #define FG_CNTL_OCV_ADJ_EN (1 << 3) |
| 62 | #define FG_CNTL_CAP_ADJ_STAT (1 << 4) |
| 63 | #define FG_CNTL_CAP_ADJ_EN (1 << 5) |
| 64 | #define FG_CNTL_CC_EN (1 << 6) |
| 65 | #define FG_CNTL_GAUGE_EN (1 << 7) |
| 66 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 67 | #define FG_15BIT_WORD_VALID (1 << 15) |
| 68 | #define FG_15BIT_VAL_MASK 0x7fff |
| 69 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 70 | #define FG_REP_CAP_VALID (1 << 7) |
| 71 | #define FG_REP_CAP_VAL_MASK 0x7F |
| 72 | |
| 73 | #define FG_DES_CAP1_VALID (1 << 7) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 74 | #define FG_DES_CAP_RES_LSB 1456 /* 1.456mAhr */ |
| 75 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 76 | #define FG_DES_CC_RES_LSB 1456 /* 1.456mAhr */ |
| 77 | |
| 78 | #define FG_OCV_CAP_VALID (1 << 7) |
| 79 | #define FG_OCV_CAP_VAL_MASK 0x7F |
| 80 | #define FG_CC_CAP_VALID (1 << 7) |
| 81 | #define FG_CC_CAP_VAL_MASK 0x7F |
| 82 | |
| 83 | #define FG_LOW_CAP_THR1_MASK 0xf0 /* 5% tp 20% */ |
| 84 | #define FG_LOW_CAP_THR1_VAL 0xa0 /* 15 perc */ |
| 85 | #define FG_LOW_CAP_THR2_MASK 0x0f /* 0% to 15% */ |
| 86 | #define FG_LOW_CAP_WARN_THR 14 /* 14 perc */ |
| 87 | #define FG_LOW_CAP_CRIT_THR 4 /* 4 perc */ |
| 88 | #define FG_LOW_CAP_SHDN_THR 0 /* 0 perc */ |
| 89 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 90 | #define NR_RETRY_CNT 3 |
| 91 | #define DEV_NAME "axp288_fuel_gauge" |
| 92 | |
| 93 | /* 1.1mV per LSB expressed in uV */ |
| 94 | #define VOLTAGE_FROM_ADC(a) ((a * 11) / 10) |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 95 | /* properties converted to uV, uA */ |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 96 | #define PROP_VOLT(a) ((a) * 1000) |
| 97 | #define PROP_CURR(a) ((a) * 1000) |
| 98 | |
| 99 | #define AXP288_FG_INTR_NUM 6 |
| 100 | enum { |
| 101 | QWBTU_IRQ = 0, |
| 102 | WBTU_IRQ, |
| 103 | QWBTO_IRQ, |
| 104 | WBTO_IRQ, |
| 105 | WL2_IRQ, |
| 106 | WL1_IRQ, |
| 107 | }; |
| 108 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 109 | enum { |
| 110 | BAT_TEMP = 0, |
| 111 | PMIC_TEMP, |
| 112 | SYSTEM_TEMP, |
| 113 | BAT_CHRG_CURR, |
| 114 | BAT_D_CURR, |
| 115 | BAT_VOLT, |
| 116 | IIO_CHANNEL_NUM |
| 117 | }; |
| 118 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 119 | struct axp288_fg_info { |
| 120 | struct platform_device *pdev; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 121 | struct regmap *regmap; |
| 122 | struct regmap_irq_chip_data *regmap_irqc; |
| 123 | int irq[AXP288_FG_INTR_NUM]; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 124 | struct iio_channel *iio_channel[IIO_CHANNEL_NUM]; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 125 | struct power_supply *bat; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 126 | struct mutex lock; |
| 127 | int status; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 128 | int max_volt; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 129 | struct dentry *debug_file; |
| 130 | }; |
| 131 | |
| 132 | static enum power_supply_property fuel_gauge_props[] = { |
| 133 | POWER_SUPPLY_PROP_STATUS, |
| 134 | POWER_SUPPLY_PROP_PRESENT, |
| 135 | POWER_SUPPLY_PROP_HEALTH, |
| 136 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 137 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 138 | POWER_SUPPLY_PROP_VOLTAGE_OCV, |
| 139 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 140 | POWER_SUPPLY_PROP_CAPACITY, |
| 141 | POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 142 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 143 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 144 | POWER_SUPPLY_PROP_CHARGE_NOW, |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg) |
| 148 | { |
| 149 | int ret, i; |
| 150 | unsigned int val; |
| 151 | |
| 152 | for (i = 0; i < NR_RETRY_CNT; i++) { |
| 153 | ret = regmap_read(info->regmap, reg, &val); |
| 154 | if (ret == -EBUSY) |
| 155 | continue; |
| 156 | else |
| 157 | break; |
| 158 | } |
| 159 | |
Hans de Goede | 6f074bc | 2016-12-14 17:38:50 +0100 | [diff] [blame] | 160 | if (ret < 0) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 161 | dev_err(&info->pdev->dev, "axp288 reg read err:%d\n", ret); |
Hans de Goede | 6f074bc | 2016-12-14 17:38:50 +0100 | [diff] [blame] | 162 | return ret; |
| 163 | } |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 164 | |
| 165 | return val; |
| 166 | } |
| 167 | |
| 168 | static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val) |
| 169 | { |
| 170 | int ret; |
| 171 | |
| 172 | ret = regmap_write(info->regmap, reg, (unsigned int)val); |
| 173 | |
| 174 | if (ret < 0) |
| 175 | dev_err(&info->pdev->dev, "axp288 reg write err:%d\n", ret); |
| 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 180 | static int fuel_gauge_read_15bit_word(struct axp288_fg_info *info, int reg) |
| 181 | { |
| 182 | unsigned char buf[2]; |
| 183 | int ret; |
| 184 | |
| 185 | ret = regmap_bulk_read(info->regmap, reg, buf, 2); |
| 186 | if (ret < 0) { |
| 187 | dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n", |
| 188 | reg, ret); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | ret = get_unaligned_be16(buf); |
| 193 | if (!(ret & FG_15BIT_WORD_VALID)) { |
| 194 | dev_err(&info->pdev->dev, "Error reg 0x%02x contents not valid\n", |
| 195 | reg); |
| 196 | return -ENXIO; |
| 197 | } |
| 198 | |
| 199 | return ret & FG_15BIT_VAL_MASK; |
| 200 | } |
| 201 | |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 202 | static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg) |
| 203 | { |
| 204 | unsigned char buf[2]; |
| 205 | int ret; |
| 206 | |
| 207 | ret = regmap_bulk_read(info->regmap, reg, buf, 2); |
| 208 | if (ret < 0) { |
| 209 | dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n", |
| 210 | reg, ret); |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | /* 12-bit data values have upper 8 bits in buf[0], lower 4 in buf[1] */ |
| 215 | return (buf[0] << 4) | ((buf[1] >> 4) & 0x0f); |
| 216 | } |
| 217 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 218 | #ifdef CONFIG_DEBUG_FS |
| 219 | static int fuel_gauge_debug_show(struct seq_file *s, void *data) |
| 220 | { |
| 221 | struct axp288_fg_info *info = s->private; |
| 222 | int raw_val, ret; |
| 223 | |
| 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", |
| 228 | AXP20X_PWR_OP_MODE, |
| 229 | fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE)); |
| 230 | seq_printf(s, " CHRG_CTRL1[%02x] : %02x\n", |
| 231 | AXP20X_CHRG_CTRL1, |
| 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", |
| 240 | AXP20X_CC_CTRL, |
| 241 | fuel_gauge_reg_readb(info, AXP20X_CC_CTRL)); |
| 242 | seq_printf(s, "BATTERY CAP[%02x] : %02x\n", |
| 243 | AXP20X_FG_RES, |
| 244 | fuel_gauge_reg_readb(info, AXP20X_FG_RES)); |
| 245 | seq_printf(s, " FG_RDC1[%02x] : %02x\n", |
| 246 | AXP288_FG_RDC1_REG, |
| 247 | fuel_gauge_reg_readb(info, AXP288_FG_RDC1_REG)); |
| 248 | seq_printf(s, " FG_RDC0[%02x] : %02x\n", |
| 249 | AXP288_FG_RDC0_REG, |
| 250 | fuel_gauge_reg_readb(info, AXP288_FG_RDC0_REG)); |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 251 | seq_printf(s, " FG_OCV[%02x] : %04x\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 252 | AXP288_FG_OCVH_REG, |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 253 | fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG)); |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 254 | seq_printf(s, " FG_DES_CAP[%02x] : %04x\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 255 | AXP288_FG_DES_CAP1_REG, |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 256 | fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG)); |
| 257 | seq_printf(s, " FG_CC_MTR[%02x] : %04x\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 258 | AXP288_FG_CC_MTR1_REG, |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 259 | fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG)); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 260 | seq_printf(s, " FG_OCV_CAP[%02x] : %02x\n", |
| 261 | AXP288_FG_OCV_CAP_REG, |
| 262 | fuel_gauge_reg_readb(info, AXP288_FG_OCV_CAP_REG)); |
| 263 | seq_printf(s, " FG_CC_CAP[%02x] : %02x\n", |
| 264 | AXP288_FG_CC_CAP_REG, |
| 265 | fuel_gauge_reg_readb(info, AXP288_FG_CC_CAP_REG)); |
| 266 | seq_printf(s, " FG_LOW_CAP[%02x] : %02x\n", |
| 267 | AXP288_FG_LOW_CAP_REG, |
| 268 | fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG)); |
| 269 | seq_printf(s, "TUNING_CTL0[%02x] : %02x\n", |
| 270 | AXP288_FG_TUNE0, |
| 271 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE0)); |
| 272 | seq_printf(s, "TUNING_CTL1[%02x] : %02x\n", |
| 273 | AXP288_FG_TUNE1, |
| 274 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE1)); |
| 275 | seq_printf(s, "TUNING_CTL2[%02x] : %02x\n", |
| 276 | AXP288_FG_TUNE2, |
| 277 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE2)); |
| 278 | seq_printf(s, "TUNING_CTL3[%02x] : %02x\n", |
| 279 | AXP288_FG_TUNE3, |
| 280 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE3)); |
| 281 | seq_printf(s, "TUNING_CTL4[%02x] : %02x\n", |
| 282 | AXP288_FG_TUNE4, |
| 283 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE4)); |
| 284 | seq_printf(s, "TUNING_CTL5[%02x] : %02x\n", |
| 285 | AXP288_FG_TUNE5, |
| 286 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE5)); |
| 287 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 288 | ret = iio_read_channel_raw(info->iio_channel[BAT_TEMP], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 289 | if (ret >= 0) |
| 290 | seq_printf(s, "axp288-batttemp : %d\n", raw_val); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 291 | ret = iio_read_channel_raw(info->iio_channel[PMIC_TEMP], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 292 | if (ret >= 0) |
| 293 | seq_printf(s, "axp288-pmictemp : %d\n", raw_val); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 294 | ret = iio_read_channel_raw(info->iio_channel[SYSTEM_TEMP], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 295 | if (ret >= 0) |
| 296 | seq_printf(s, "axp288-systtemp : %d\n", raw_val); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 297 | ret = iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 298 | if (ret >= 0) |
| 299 | seq_printf(s, "axp288-chrgcurr : %d\n", raw_val); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 300 | ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 301 | if (ret >= 0) |
| 302 | seq_printf(s, "axp288-dchrgcur : %d\n", raw_val); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 303 | ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 304 | if (ret >= 0) |
| 305 | seq_printf(s, "axp288-battvolt : %d\n", raw_val); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int debug_open(struct inode *inode, struct file *file) |
| 311 | { |
| 312 | return single_open(file, fuel_gauge_debug_show, inode->i_private); |
| 313 | } |
| 314 | |
| 315 | static const struct file_operations fg_debug_fops = { |
| 316 | .open = debug_open, |
| 317 | .read = seq_read, |
| 318 | .llseek = seq_lseek, |
| 319 | .release = single_release, |
| 320 | }; |
| 321 | |
| 322 | static void fuel_gauge_create_debugfs(struct axp288_fg_info *info) |
| 323 | { |
| 324 | info->debug_file = debugfs_create_file("fuelgauge", 0666, NULL, |
| 325 | info, &fg_debug_fops); |
| 326 | } |
| 327 | |
| 328 | static void fuel_gauge_remove_debugfs(struct axp288_fg_info *info) |
| 329 | { |
| 330 | debugfs_remove(info->debug_file); |
| 331 | } |
| 332 | #else |
| 333 | static inline void fuel_gauge_create_debugfs(struct axp288_fg_info *info) |
| 334 | { |
| 335 | } |
| 336 | static inline void fuel_gauge_remove_debugfs(struct axp288_fg_info *info) |
| 337 | { |
| 338 | } |
| 339 | #endif |
| 340 | |
| 341 | static void fuel_gauge_get_status(struct axp288_fg_info *info) |
| 342 | { |
Hans de Goede | f451655 | 2018-02-15 15:00:36 +0100 | [diff] [blame] | 343 | int pwr_stat, fg_res, curr, ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 344 | |
| 345 | pwr_stat = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS); |
| 346 | if (pwr_stat < 0) { |
| 347 | dev_err(&info->pdev->dev, |
| 348 | "PWR STAT read failed:%d\n", pwr_stat); |
| 349 | return; |
| 350 | } |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 351 | |
| 352 | /* Report full if Vbus is valid and the reported capacity is 100% */ |
Hans de Goede | f451655 | 2018-02-15 15:00:36 +0100 | [diff] [blame] | 353 | if (!(pwr_stat & PS_STAT_VBUS_VALID)) |
| 354 | goto not_full; |
| 355 | |
| 356 | fg_res = fuel_gauge_reg_readb(info, AXP20X_FG_RES); |
| 357 | if (fg_res < 0) { |
| 358 | dev_err(&info->pdev->dev, "FG RES read failed: %d\n", fg_res); |
| 359 | return; |
| 360 | } |
| 361 | if (!(fg_res & FG_REP_CAP_VALID)) |
| 362 | goto not_full; |
| 363 | |
| 364 | fg_res &= ~FG_REP_CAP_VALID; |
| 365 | if (fg_res == 100) { |
| 366 | info->status = POWER_SUPPLY_STATUS_FULL; |
| 367 | return; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Hans de Goede | f451655 | 2018-02-15 15:00:36 +0100 | [diff] [blame] | 370 | /* |
| 371 | * Sometimes the charger turns itself off before fg-res reaches 100%. |
| 372 | * When this happens the AXP288 reports a not-charging status and |
| 373 | * 0 mA discharge current. |
| 374 | */ |
| 375 | if (fg_res < 90 || (pwr_stat & PS_STAT_BAT_CHRG_DIR)) |
| 376 | goto not_full; |
| 377 | |
| 378 | ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &curr); |
| 379 | if (ret < 0) { |
| 380 | dev_err(&info->pdev->dev, "FG get current failed: %d\n", ret); |
| 381 | return; |
| 382 | } |
| 383 | if (curr == 0) { |
| 384 | info->status = POWER_SUPPLY_STATUS_FULL; |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | not_full: |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 389 | if (pwr_stat & PS_STAT_BAT_CHRG_DIR) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 390 | info->status = POWER_SUPPLY_STATUS_CHARGING; |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 391 | else |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 392 | info->status = POWER_SUPPLY_STATUS_DISCHARGING; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static int fuel_gauge_get_vbatt(struct axp288_fg_info *info, int *vbatt) |
| 396 | { |
| 397 | int ret = 0, raw_val; |
| 398 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 399 | ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 400 | if (ret < 0) |
| 401 | goto vbatt_read_fail; |
| 402 | |
| 403 | *vbatt = VOLTAGE_FROM_ADC(raw_val); |
| 404 | vbatt_read_fail: |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | static int fuel_gauge_get_current(struct axp288_fg_info *info, int *cur) |
| 409 | { |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 410 | int ret, discharge; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 411 | |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 412 | /* First check discharge current, so that we do only 1 read on bat. */ |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 413 | ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &discharge); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 414 | if (ret < 0) |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 415 | return ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 416 | |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 417 | if (discharge > 0) { |
| 418 | *cur = -1 * discharge; |
| 419 | return 0; |
| 420 | } |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 421 | |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 422 | return iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], cur); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 425 | static int fuel_gauge_get_vocv(struct axp288_fg_info *info, int *vocv) |
| 426 | { |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 427 | int ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 428 | |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 429 | ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG); |
| 430 | if (ret >= 0) |
| 431 | *vocv = VOLTAGE_FROM_ADC(ret); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 432 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | static int fuel_gauge_battery_health(struct axp288_fg_info *info) |
| 437 | { |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 438 | int ret, vocv, health = POWER_SUPPLY_HEALTH_UNKNOWN; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 439 | |
| 440 | ret = fuel_gauge_get_vocv(info, &vocv); |
| 441 | if (ret < 0) |
| 442 | goto health_read_fail; |
| 443 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 444 | if (vocv > info->max_volt) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 445 | health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 446 | else |
| 447 | health = POWER_SUPPLY_HEALTH_GOOD; |
| 448 | |
| 449 | health_read_fail: |
| 450 | return health; |
| 451 | } |
| 452 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 453 | static int fuel_gauge_get_property(struct power_supply *ps, |
| 454 | enum power_supply_property prop, |
| 455 | union power_supply_propval *val) |
| 456 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 457 | struct axp288_fg_info *info = power_supply_get_drvdata(ps); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 458 | int ret = 0, value; |
| 459 | |
| 460 | mutex_lock(&info->lock); |
| 461 | switch (prop) { |
| 462 | case POWER_SUPPLY_PROP_STATUS: |
| 463 | fuel_gauge_get_status(info); |
| 464 | val->intval = info->status; |
| 465 | break; |
| 466 | case POWER_SUPPLY_PROP_HEALTH: |
| 467 | val->intval = fuel_gauge_battery_health(info); |
| 468 | break; |
| 469 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 470 | ret = fuel_gauge_get_vbatt(info, &value); |
| 471 | if (ret < 0) |
| 472 | goto fuel_gauge_read_err; |
| 473 | val->intval = PROP_VOLT(value); |
| 474 | break; |
| 475 | case POWER_SUPPLY_PROP_VOLTAGE_OCV: |
| 476 | ret = fuel_gauge_get_vocv(info, &value); |
| 477 | if (ret < 0) |
| 478 | goto fuel_gauge_read_err; |
| 479 | val->intval = PROP_VOLT(value); |
| 480 | break; |
| 481 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 482 | ret = fuel_gauge_get_current(info, &value); |
| 483 | if (ret < 0) |
| 484 | goto fuel_gauge_read_err; |
| 485 | val->intval = PROP_CURR(value); |
| 486 | break; |
| 487 | case POWER_SUPPLY_PROP_PRESENT: |
| 488 | ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE); |
| 489 | if (ret < 0) |
| 490 | goto fuel_gauge_read_err; |
| 491 | |
| 492 | if (ret & CHRG_STAT_BAT_PRESENT) |
| 493 | val->intval = 1; |
| 494 | else |
| 495 | val->intval = 0; |
| 496 | break; |
| 497 | case POWER_SUPPLY_PROP_CAPACITY: |
| 498 | ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES); |
| 499 | if (ret < 0) |
| 500 | goto fuel_gauge_read_err; |
| 501 | |
| 502 | if (!(ret & FG_REP_CAP_VALID)) |
| 503 | dev_err(&info->pdev->dev, |
| 504 | "capacity measurement not valid\n"); |
| 505 | val->intval = (ret & FG_REP_CAP_VAL_MASK); |
| 506 | break; |
| 507 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 508 | ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); |
| 509 | if (ret < 0) |
| 510 | goto fuel_gauge_read_err; |
| 511 | val->intval = (ret & 0x0f); |
| 512 | break; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 513 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 514 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 515 | break; |
| 516 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 517 | ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 518 | if (ret < 0) |
| 519 | goto fuel_gauge_read_err; |
| 520 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 521 | val->intval = ret * FG_DES_CAP_RES_LSB; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 522 | break; |
| 523 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 524 | ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 525 | if (ret < 0) |
| 526 | goto fuel_gauge_read_err; |
| 527 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 528 | val->intval = ret * FG_DES_CAP_RES_LSB; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 529 | break; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 530 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 531 | val->intval = PROP_VOLT(info->max_volt); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 532 | break; |
| 533 | default: |
| 534 | mutex_unlock(&info->lock); |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | |
| 538 | mutex_unlock(&info->lock); |
| 539 | return 0; |
| 540 | |
| 541 | fuel_gauge_read_err: |
| 542 | mutex_unlock(&info->lock); |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | static int fuel_gauge_set_property(struct power_supply *ps, |
| 547 | enum power_supply_property prop, |
| 548 | const union power_supply_propval *val) |
| 549 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 550 | struct axp288_fg_info *info = power_supply_get_drvdata(ps); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 551 | int ret = 0; |
| 552 | |
| 553 | mutex_lock(&info->lock); |
| 554 | switch (prop) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 555 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 556 | if ((val->intval < 0) || (val->intval > 15)) { |
| 557 | ret = -EINVAL; |
| 558 | break; |
| 559 | } |
| 560 | ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); |
| 561 | if (ret < 0) |
| 562 | break; |
| 563 | ret &= 0xf0; |
| 564 | ret |= (val->intval & 0xf); |
| 565 | ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, ret); |
| 566 | break; |
| 567 | default: |
| 568 | ret = -EINVAL; |
| 569 | break; |
| 570 | } |
| 571 | |
| 572 | mutex_unlock(&info->lock); |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | static int fuel_gauge_property_is_writeable(struct power_supply *psy, |
| 577 | enum power_supply_property psp) |
| 578 | { |
| 579 | int ret; |
| 580 | |
| 581 | switch (psp) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 582 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 583 | ret = 1; |
| 584 | break; |
| 585 | default: |
| 586 | ret = 0; |
| 587 | } |
| 588 | |
| 589 | return ret; |
| 590 | } |
| 591 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 592 | static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev) |
| 593 | { |
| 594 | struct axp288_fg_info *info = dev; |
| 595 | int i; |
| 596 | |
| 597 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) { |
| 598 | if (info->irq[i] == irq) |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | if (i >= AXP288_FG_INTR_NUM) { |
| 603 | dev_warn(&info->pdev->dev, "spurious interrupt!!\n"); |
| 604 | return IRQ_NONE; |
| 605 | } |
| 606 | |
| 607 | switch (i) { |
| 608 | case QWBTU_IRQ: |
| 609 | dev_info(&info->pdev->dev, |
| 610 | "Quit Battery under temperature in work mode IRQ (QWBTU)\n"); |
| 611 | break; |
| 612 | case WBTU_IRQ: |
| 613 | dev_info(&info->pdev->dev, |
| 614 | "Battery under temperature in work mode IRQ (WBTU)\n"); |
| 615 | break; |
| 616 | case QWBTO_IRQ: |
| 617 | dev_info(&info->pdev->dev, |
| 618 | "Quit Battery over temperature in work mode IRQ (QWBTO)\n"); |
| 619 | break; |
| 620 | case WBTO_IRQ: |
| 621 | dev_info(&info->pdev->dev, |
| 622 | "Battery over temperature in work mode IRQ (WBTO)\n"); |
| 623 | break; |
| 624 | case WL2_IRQ: |
| 625 | dev_info(&info->pdev->dev, "Low Batt Warning(2) INTR\n"); |
| 626 | break; |
| 627 | case WL1_IRQ: |
| 628 | dev_info(&info->pdev->dev, "Low Batt Warning(1) INTR\n"); |
| 629 | break; |
| 630 | default: |
| 631 | dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n"); |
| 632 | } |
| 633 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 634 | power_supply_changed(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 635 | return IRQ_HANDLED; |
| 636 | } |
| 637 | |
| 638 | static void fuel_gauge_external_power_changed(struct power_supply *psy) |
| 639 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 640 | struct axp288_fg_info *info = power_supply_get_drvdata(psy); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 641 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 642 | power_supply_changed(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 645 | static const struct power_supply_desc fuel_gauge_desc = { |
| 646 | .name = DEV_NAME, |
| 647 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 648 | .properties = fuel_gauge_props, |
| 649 | .num_properties = ARRAY_SIZE(fuel_gauge_props), |
| 650 | .get_property = fuel_gauge_get_property, |
| 651 | .set_property = fuel_gauge_set_property, |
| 652 | .property_is_writeable = fuel_gauge_property_is_writeable, |
| 653 | .external_power_changed = fuel_gauge_external_power_changed, |
| 654 | }; |
| 655 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 656 | static void fuel_gauge_init_irq(struct axp288_fg_info *info) |
| 657 | { |
| 658 | int ret, i, pirq; |
| 659 | |
| 660 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) { |
| 661 | pirq = platform_get_irq(info->pdev, i); |
| 662 | info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq); |
| 663 | if (info->irq[i] < 0) { |
| 664 | dev_warn(&info->pdev->dev, |
| 665 | "regmap_irq get virq failed for IRQ %d: %d\n", |
| 666 | pirq, info->irq[i]); |
| 667 | info->irq[i] = -1; |
| 668 | goto intr_failed; |
| 669 | } |
| 670 | ret = request_threaded_irq(info->irq[i], |
| 671 | NULL, fuel_gauge_thread_handler, |
| 672 | IRQF_ONESHOT, DEV_NAME, info); |
| 673 | if (ret) { |
| 674 | dev_warn(&info->pdev->dev, |
| 675 | "request irq failed for IRQ %d: %d\n", |
| 676 | pirq, info->irq[i]); |
| 677 | info->irq[i] = -1; |
| 678 | goto intr_failed; |
| 679 | } else { |
| 680 | dev_info(&info->pdev->dev, "HW IRQ %d -> VIRQ %d\n", |
| 681 | pirq, info->irq[i]); |
| 682 | } |
| 683 | } |
| 684 | return; |
| 685 | |
| 686 | intr_failed: |
| 687 | for (; i > 0; i--) { |
| 688 | free_irq(info->irq[i - 1], info); |
| 689 | info->irq[i - 1] = -1; |
| 690 | } |
| 691 | } |
| 692 | |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 693 | /* |
| 694 | * Some devices have no battery (HDMI sticks) and the axp288 battery's |
| 695 | * detection reports one despite it not being there. |
| 696 | */ |
| 697 | static const struct dmi_system_id axp288_fuel_gauge_blacklist[] = { |
| 698 | { |
| 699 | /* Intel Cherry Trail Compute Stick, Windows version */ |
| 700 | .matches = { |
| 701 | DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), |
| 702 | DMI_MATCH(DMI_PRODUCT_NAME, "STK1AW32SC"), |
| 703 | }, |
| 704 | }, |
| 705 | { |
| 706 | /* Intel Cherry Trail Compute Stick, version without an OS */ |
| 707 | .matches = { |
| 708 | DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), |
| 709 | DMI_MATCH(DMI_PRODUCT_NAME, "STK1A32SC"), |
| 710 | }, |
| 711 | }, |
| 712 | { |
| 713 | /* Meegopad T08 */ |
| 714 | .matches = { |
| 715 | DMI_MATCH(DMI_SYS_VENDOR, "Default string"), |
| 716 | DMI_MATCH(DMI_BOARD_VENDOR, "To be filled by OEM."), |
| 717 | DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"), |
| 718 | DMI_MATCH(DMI_BOARD_VERSION, "V1.1"), |
| 719 | }, |
| 720 | }, |
Carlo Caione | 7638eb5 | 2018-02-16 08:26:16 +0000 | [diff] [blame] | 721 | { |
| 722 | /* ECS EF20EA */ |
| 723 | .matches = { |
| 724 | DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"), |
| 725 | }, |
| 726 | }, |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 727 | {} |
| 728 | }; |
| 729 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 730 | static int axp288_fuel_gauge_probe(struct platform_device *pdev) |
| 731 | { |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 732 | int i, ret = 0; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 733 | struct axp288_fg_info *info; |
| 734 | struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 735 | struct power_supply_config psy_cfg = {}; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 736 | static const char * const iio_chan_name[] = { |
| 737 | [BAT_TEMP] = "axp288-batt-temp", |
| 738 | [PMIC_TEMP] = "axp288-pmic-temp", |
| 739 | [SYSTEM_TEMP] = "axp288-system-temp", |
| 740 | [BAT_CHRG_CURR] = "axp288-chrg-curr", |
| 741 | [BAT_D_CURR] = "axp288-chrg-d-curr", |
| 742 | [BAT_VOLT] = "axp288-batt-volt", |
| 743 | }; |
Hans de Goede | 04d6f72 | 2018-04-18 14:08:21 +0200 | [diff] [blame] | 744 | unsigned int val; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 745 | |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 746 | if (dmi_check_system(axp288_fuel_gauge_blacklist)) |
| 747 | return -ENODEV; |
| 748 | |
Hans de Goede | 04d6f72 | 2018-04-18 14:08:21 +0200 | [diff] [blame] | 749 | /* |
| 750 | * On some devices the fuelgauge and charger parts of the axp288 are |
| 751 | * not used, check that the fuelgauge is enabled (CC_CTRL != 0). |
| 752 | */ |
| 753 | ret = regmap_read(axp20x->regmap, AXP20X_CC_CTRL, &val); |
| 754 | if (ret < 0) |
| 755 | return ret; |
| 756 | if (val == 0) |
| 757 | return -ENODEV; |
| 758 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 759 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 760 | if (!info) |
| 761 | return -ENOMEM; |
| 762 | |
| 763 | info->pdev = pdev; |
| 764 | info->regmap = axp20x->regmap; |
| 765 | info->regmap_irqc = axp20x->regmap_irqc; |
| 766 | info->status = POWER_SUPPLY_STATUS_UNKNOWN; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 767 | |
| 768 | platform_set_drvdata(pdev, info); |
| 769 | |
| 770 | mutex_init(&info->lock); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 771 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 772 | for (i = 0; i < IIO_CHANNEL_NUM; i++) { |
| 773 | /* |
| 774 | * Note cannot use devm_iio_channel_get because x86 systems |
| 775 | * lack the device<->channel maps which iio_channel_get will |
| 776 | * try to use when passed a non NULL device pointer. |
| 777 | */ |
| 778 | info->iio_channel[i] = |
| 779 | iio_channel_get(NULL, iio_chan_name[i]); |
| 780 | if (IS_ERR(info->iio_channel[i])) { |
| 781 | ret = PTR_ERR(info->iio_channel[i]); |
| 782 | dev_dbg(&pdev->dev, "error getting iiochan %s: %d\n", |
| 783 | iio_chan_name[i], ret); |
| 784 | /* Wait for axp288_adc to load */ |
| 785 | if (ret == -ENODEV) |
| 786 | ret = -EPROBE_DEFER; |
| 787 | |
| 788 | goto out_free_iio_chan; |
| 789 | } |
| 790 | } |
| 791 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 792 | ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG); |
| 793 | if (ret < 0) |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 794 | goto out_free_iio_chan; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 795 | |
| 796 | if (!(ret & FG_DES_CAP1_VALID)) { |
| 797 | dev_err(&pdev->dev, "axp288 not configured by firmware\n"); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 798 | ret = -ENODEV; |
| 799 | goto out_free_iio_chan; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1); |
| 803 | if (ret < 0) |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 804 | goto out_free_iio_chan; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 805 | switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) { |
| 806 | case CHRG_CCCV_CV_4100MV: |
| 807 | info->max_volt = 4100; |
| 808 | break; |
| 809 | case CHRG_CCCV_CV_4150MV: |
| 810 | info->max_volt = 4150; |
| 811 | break; |
| 812 | case CHRG_CCCV_CV_4200MV: |
| 813 | info->max_volt = 4200; |
| 814 | break; |
| 815 | case CHRG_CCCV_CV_4350MV: |
| 816 | info->max_volt = 4350; |
| 817 | break; |
| 818 | } |
| 819 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 820 | psy_cfg.drv_data = info; |
| 821 | info->bat = power_supply_register(&pdev->dev, &fuel_gauge_desc, &psy_cfg); |
| 822 | if (IS_ERR(info->bat)) { |
| 823 | ret = PTR_ERR(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 824 | dev_err(&pdev->dev, "failed to register battery: %d\n", ret); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 825 | goto out_free_iio_chan; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | fuel_gauge_create_debugfs(info); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 829 | fuel_gauge_init_irq(info); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 830 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 831 | return 0; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 832 | |
| 833 | out_free_iio_chan: |
| 834 | for (i = 0; i < IIO_CHANNEL_NUM; i++) |
| 835 | if (!IS_ERR_OR_NULL(info->iio_channel[i])) |
| 836 | iio_channel_release(info->iio_channel[i]); |
| 837 | |
| 838 | return ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 839 | } |
| 840 | |
Krzysztof Kozlowski | f1f27a4 | 2015-05-02 00:46:43 +0900 | [diff] [blame] | 841 | static const struct platform_device_id axp288_fg_id_table[] = { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 842 | { .name = DEV_NAME }, |
| 843 | {}, |
| 844 | }; |
Javier Martinez Canillas | 99e33fb | 2016-10-17 16:13:36 -0300 | [diff] [blame] | 845 | MODULE_DEVICE_TABLE(platform, axp288_fg_id_table); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 846 | |
| 847 | static int axp288_fuel_gauge_remove(struct platform_device *pdev) |
| 848 | { |
| 849 | struct axp288_fg_info *info = platform_get_drvdata(pdev); |
| 850 | int i; |
| 851 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 852 | power_supply_unregister(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 853 | fuel_gauge_remove_debugfs(info); |
| 854 | |
| 855 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) |
| 856 | if (info->irq[i] >= 0) |
| 857 | free_irq(info->irq[i], info); |
| 858 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 859 | for (i = 0; i < IIO_CHANNEL_NUM; i++) |
| 860 | iio_channel_release(info->iio_channel[i]); |
| 861 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | static struct platform_driver axp288_fuel_gauge_driver = { |
| 866 | .probe = axp288_fuel_gauge_probe, |
| 867 | .remove = axp288_fuel_gauge_remove, |
| 868 | .id_table = axp288_fg_id_table, |
| 869 | .driver = { |
| 870 | .name = DEV_NAME, |
| 871 | }, |
| 872 | }; |
| 873 | |
| 874 | module_platform_driver(axp288_fuel_gauge_driver); |
| 875 | |
Ramakrishna Pallala | 409e718 | 2015-03-13 21:49:09 +0530 | [diff] [blame] | 876 | MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 877 | MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>"); |
| 878 | MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver"); |
| 879 | MODULE_LICENSE("GPL"); |