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 | |
Yangtao Li | 0367e23 | 2018-11-22 09:25:51 -0500 | [diff] [blame] | 310 | DEFINE_SHOW_ATTRIBUTE(fuel_gauge_debug); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 311 | |
| 312 | static void fuel_gauge_create_debugfs(struct axp288_fg_info *info) |
| 313 | { |
| 314 | info->debug_file = debugfs_create_file("fuelgauge", 0666, NULL, |
Yangtao Li | 0367e23 | 2018-11-22 09:25:51 -0500 | [diff] [blame] | 315 | info, &fuel_gauge_debug_fops); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void fuel_gauge_remove_debugfs(struct axp288_fg_info *info) |
| 319 | { |
| 320 | debugfs_remove(info->debug_file); |
| 321 | } |
| 322 | #else |
| 323 | static inline void fuel_gauge_create_debugfs(struct axp288_fg_info *info) |
| 324 | { |
| 325 | } |
| 326 | static inline void fuel_gauge_remove_debugfs(struct axp288_fg_info *info) |
| 327 | { |
| 328 | } |
| 329 | #endif |
| 330 | |
| 331 | static void fuel_gauge_get_status(struct axp288_fg_info *info) |
| 332 | { |
Hans de Goede | f451655 | 2018-02-15 15:00:36 +0100 | [diff] [blame] | 333 | int pwr_stat, fg_res, curr, ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 334 | |
| 335 | pwr_stat = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS); |
| 336 | if (pwr_stat < 0) { |
| 337 | dev_err(&info->pdev->dev, |
| 338 | "PWR STAT read failed:%d\n", pwr_stat); |
| 339 | return; |
| 340 | } |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 341 | |
| 342 | /* 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] | 343 | if (!(pwr_stat & PS_STAT_VBUS_VALID)) |
| 344 | goto not_full; |
| 345 | |
| 346 | fg_res = fuel_gauge_reg_readb(info, AXP20X_FG_RES); |
| 347 | if (fg_res < 0) { |
| 348 | dev_err(&info->pdev->dev, "FG RES read failed: %d\n", fg_res); |
| 349 | return; |
| 350 | } |
| 351 | if (!(fg_res & FG_REP_CAP_VALID)) |
| 352 | goto not_full; |
| 353 | |
| 354 | fg_res &= ~FG_REP_CAP_VALID; |
| 355 | if (fg_res == 100) { |
| 356 | info->status = POWER_SUPPLY_STATUS_FULL; |
| 357 | return; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Hans de Goede | f451655 | 2018-02-15 15:00:36 +0100 | [diff] [blame] | 360 | /* |
| 361 | * Sometimes the charger turns itself off before fg-res reaches 100%. |
| 362 | * When this happens the AXP288 reports a not-charging status and |
| 363 | * 0 mA discharge current. |
| 364 | */ |
| 365 | if (fg_res < 90 || (pwr_stat & PS_STAT_BAT_CHRG_DIR)) |
| 366 | goto not_full; |
| 367 | |
| 368 | ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &curr); |
| 369 | if (ret < 0) { |
| 370 | dev_err(&info->pdev->dev, "FG get current failed: %d\n", ret); |
| 371 | return; |
| 372 | } |
| 373 | if (curr == 0) { |
| 374 | info->status = POWER_SUPPLY_STATUS_FULL; |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | not_full: |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 379 | if (pwr_stat & PS_STAT_BAT_CHRG_DIR) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 380 | info->status = POWER_SUPPLY_STATUS_CHARGING; |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 381 | else |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 382 | info->status = POWER_SUPPLY_STATUS_DISCHARGING; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static int fuel_gauge_get_vbatt(struct axp288_fg_info *info, int *vbatt) |
| 386 | { |
| 387 | int ret = 0, raw_val; |
| 388 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 389 | ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 390 | if (ret < 0) |
| 391 | goto vbatt_read_fail; |
| 392 | |
| 393 | *vbatt = VOLTAGE_FROM_ADC(raw_val); |
| 394 | vbatt_read_fail: |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | static int fuel_gauge_get_current(struct axp288_fg_info *info, int *cur) |
| 399 | { |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 400 | int ret, discharge; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 401 | |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 402 | /* 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] | 403 | ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &discharge); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 404 | if (ret < 0) |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 405 | return ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 406 | |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 407 | if (discharge > 0) { |
| 408 | *cur = -1 * discharge; |
| 409 | return 0; |
| 410 | } |
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 | return iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], cur); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 415 | static int fuel_gauge_get_vocv(struct axp288_fg_info *info, int *vocv) |
| 416 | { |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 417 | int ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 418 | |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 419 | ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG); |
| 420 | if (ret >= 0) |
| 421 | *vocv = VOLTAGE_FROM_ADC(ret); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 422 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | static int fuel_gauge_battery_health(struct axp288_fg_info *info) |
| 427 | { |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 428 | int ret, vocv, health = POWER_SUPPLY_HEALTH_UNKNOWN; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 429 | |
| 430 | ret = fuel_gauge_get_vocv(info, &vocv); |
| 431 | if (ret < 0) |
| 432 | goto health_read_fail; |
| 433 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 434 | if (vocv > info->max_volt) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 435 | health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 436 | else |
| 437 | health = POWER_SUPPLY_HEALTH_GOOD; |
| 438 | |
| 439 | health_read_fail: |
| 440 | return health; |
| 441 | } |
| 442 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 443 | static int fuel_gauge_get_property(struct power_supply *ps, |
| 444 | enum power_supply_property prop, |
| 445 | union power_supply_propval *val) |
| 446 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 447 | struct axp288_fg_info *info = power_supply_get_drvdata(ps); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 448 | int ret = 0, value; |
| 449 | |
| 450 | mutex_lock(&info->lock); |
| 451 | switch (prop) { |
| 452 | case POWER_SUPPLY_PROP_STATUS: |
| 453 | fuel_gauge_get_status(info); |
| 454 | val->intval = info->status; |
| 455 | break; |
| 456 | case POWER_SUPPLY_PROP_HEALTH: |
| 457 | val->intval = fuel_gauge_battery_health(info); |
| 458 | break; |
| 459 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 460 | ret = fuel_gauge_get_vbatt(info, &value); |
| 461 | if (ret < 0) |
| 462 | goto fuel_gauge_read_err; |
| 463 | val->intval = PROP_VOLT(value); |
| 464 | break; |
| 465 | case POWER_SUPPLY_PROP_VOLTAGE_OCV: |
| 466 | ret = fuel_gauge_get_vocv(info, &value); |
| 467 | if (ret < 0) |
| 468 | goto fuel_gauge_read_err; |
| 469 | val->intval = PROP_VOLT(value); |
| 470 | break; |
| 471 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 472 | ret = fuel_gauge_get_current(info, &value); |
| 473 | if (ret < 0) |
| 474 | goto fuel_gauge_read_err; |
| 475 | val->intval = PROP_CURR(value); |
| 476 | break; |
| 477 | case POWER_SUPPLY_PROP_PRESENT: |
| 478 | ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE); |
| 479 | if (ret < 0) |
| 480 | goto fuel_gauge_read_err; |
| 481 | |
| 482 | if (ret & CHRG_STAT_BAT_PRESENT) |
| 483 | val->intval = 1; |
| 484 | else |
| 485 | val->intval = 0; |
| 486 | break; |
| 487 | case POWER_SUPPLY_PROP_CAPACITY: |
| 488 | ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES); |
| 489 | if (ret < 0) |
| 490 | goto fuel_gauge_read_err; |
| 491 | |
| 492 | if (!(ret & FG_REP_CAP_VALID)) |
| 493 | dev_err(&info->pdev->dev, |
| 494 | "capacity measurement not valid\n"); |
| 495 | val->intval = (ret & FG_REP_CAP_VAL_MASK); |
| 496 | break; |
| 497 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 498 | ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); |
| 499 | if (ret < 0) |
| 500 | goto fuel_gauge_read_err; |
| 501 | val->intval = (ret & 0x0f); |
| 502 | break; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 503 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 504 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 505 | break; |
| 506 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 507 | ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 508 | if (ret < 0) |
| 509 | goto fuel_gauge_read_err; |
| 510 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 511 | val->intval = ret * FG_DES_CAP_RES_LSB; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 512 | break; |
| 513 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 514 | ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 515 | if (ret < 0) |
| 516 | goto fuel_gauge_read_err; |
| 517 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 518 | val->intval = ret * FG_DES_CAP_RES_LSB; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 519 | break; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 520 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 521 | val->intval = PROP_VOLT(info->max_volt); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 522 | break; |
| 523 | default: |
| 524 | mutex_unlock(&info->lock); |
| 525 | return -EINVAL; |
| 526 | } |
| 527 | |
| 528 | mutex_unlock(&info->lock); |
| 529 | return 0; |
| 530 | |
| 531 | fuel_gauge_read_err: |
| 532 | mutex_unlock(&info->lock); |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | static int fuel_gauge_set_property(struct power_supply *ps, |
| 537 | enum power_supply_property prop, |
| 538 | const union power_supply_propval *val) |
| 539 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 540 | struct axp288_fg_info *info = power_supply_get_drvdata(ps); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 541 | int ret = 0; |
| 542 | |
| 543 | mutex_lock(&info->lock); |
| 544 | switch (prop) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 545 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 546 | if ((val->intval < 0) || (val->intval > 15)) { |
| 547 | ret = -EINVAL; |
| 548 | break; |
| 549 | } |
| 550 | ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); |
| 551 | if (ret < 0) |
| 552 | break; |
| 553 | ret &= 0xf0; |
| 554 | ret |= (val->intval & 0xf); |
| 555 | ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, ret); |
| 556 | break; |
| 557 | default: |
| 558 | ret = -EINVAL; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | mutex_unlock(&info->lock); |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | static int fuel_gauge_property_is_writeable(struct power_supply *psy, |
| 567 | enum power_supply_property psp) |
| 568 | { |
| 569 | int ret; |
| 570 | |
| 571 | switch (psp) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 572 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 573 | ret = 1; |
| 574 | break; |
| 575 | default: |
| 576 | ret = 0; |
| 577 | } |
| 578 | |
| 579 | return ret; |
| 580 | } |
| 581 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 582 | static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev) |
| 583 | { |
| 584 | struct axp288_fg_info *info = dev; |
| 585 | int i; |
| 586 | |
| 587 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) { |
| 588 | if (info->irq[i] == irq) |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | if (i >= AXP288_FG_INTR_NUM) { |
| 593 | dev_warn(&info->pdev->dev, "spurious interrupt!!\n"); |
| 594 | return IRQ_NONE; |
| 595 | } |
| 596 | |
| 597 | switch (i) { |
| 598 | case QWBTU_IRQ: |
| 599 | dev_info(&info->pdev->dev, |
| 600 | "Quit Battery under temperature in work mode IRQ (QWBTU)\n"); |
| 601 | break; |
| 602 | case WBTU_IRQ: |
| 603 | dev_info(&info->pdev->dev, |
| 604 | "Battery under temperature in work mode IRQ (WBTU)\n"); |
| 605 | break; |
| 606 | case QWBTO_IRQ: |
| 607 | dev_info(&info->pdev->dev, |
| 608 | "Quit Battery over temperature in work mode IRQ (QWBTO)\n"); |
| 609 | break; |
| 610 | case WBTO_IRQ: |
| 611 | dev_info(&info->pdev->dev, |
| 612 | "Battery over temperature in work mode IRQ (WBTO)\n"); |
| 613 | break; |
| 614 | case WL2_IRQ: |
| 615 | dev_info(&info->pdev->dev, "Low Batt Warning(2) INTR\n"); |
| 616 | break; |
| 617 | case WL1_IRQ: |
| 618 | dev_info(&info->pdev->dev, "Low Batt Warning(1) INTR\n"); |
| 619 | break; |
| 620 | default: |
| 621 | dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n"); |
| 622 | } |
| 623 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 624 | power_supply_changed(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 625 | return IRQ_HANDLED; |
| 626 | } |
| 627 | |
| 628 | static void fuel_gauge_external_power_changed(struct power_supply *psy) |
| 629 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 630 | struct axp288_fg_info *info = power_supply_get_drvdata(psy); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 631 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 632 | power_supply_changed(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 635 | static const struct power_supply_desc fuel_gauge_desc = { |
| 636 | .name = DEV_NAME, |
| 637 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 638 | .properties = fuel_gauge_props, |
| 639 | .num_properties = ARRAY_SIZE(fuel_gauge_props), |
| 640 | .get_property = fuel_gauge_get_property, |
| 641 | .set_property = fuel_gauge_set_property, |
| 642 | .property_is_writeable = fuel_gauge_property_is_writeable, |
| 643 | .external_power_changed = fuel_gauge_external_power_changed, |
| 644 | }; |
| 645 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 646 | static void fuel_gauge_init_irq(struct axp288_fg_info *info) |
| 647 | { |
| 648 | int ret, i, pirq; |
| 649 | |
| 650 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) { |
| 651 | pirq = platform_get_irq(info->pdev, i); |
| 652 | info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq); |
| 653 | if (info->irq[i] < 0) { |
| 654 | dev_warn(&info->pdev->dev, |
| 655 | "regmap_irq get virq failed for IRQ %d: %d\n", |
| 656 | pirq, info->irq[i]); |
| 657 | info->irq[i] = -1; |
| 658 | goto intr_failed; |
| 659 | } |
| 660 | ret = request_threaded_irq(info->irq[i], |
| 661 | NULL, fuel_gauge_thread_handler, |
| 662 | IRQF_ONESHOT, DEV_NAME, info); |
| 663 | if (ret) { |
| 664 | dev_warn(&info->pdev->dev, |
| 665 | "request irq failed for IRQ %d: %d\n", |
| 666 | pirq, info->irq[i]); |
| 667 | info->irq[i] = -1; |
| 668 | goto intr_failed; |
| 669 | } else { |
| 670 | dev_info(&info->pdev->dev, "HW IRQ %d -> VIRQ %d\n", |
| 671 | pirq, info->irq[i]); |
| 672 | } |
| 673 | } |
| 674 | return; |
| 675 | |
| 676 | intr_failed: |
| 677 | for (; i > 0; i--) { |
| 678 | free_irq(info->irq[i - 1], info); |
| 679 | info->irq[i - 1] = -1; |
| 680 | } |
| 681 | } |
| 682 | |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 683 | /* |
| 684 | * Some devices have no battery (HDMI sticks) and the axp288 battery's |
| 685 | * detection reports one despite it not being there. |
| 686 | */ |
| 687 | static const struct dmi_system_id axp288_fuel_gauge_blacklist[] = { |
| 688 | { |
Hans de Goede | 9274c78 | 2019-04-22 22:43:01 +0200 | [diff] [blame^] | 689 | /* ACEPC T8 Cherry Trail Z8350 mini PC */ |
| 690 | .matches = { |
| 691 | DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."), |
| 692 | DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"), |
| 693 | DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T8"), |
| 694 | /* also match on somewhat unique bios-version */ |
| 695 | DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"), |
| 696 | }, |
| 697 | }, |
| 698 | { |
| 699 | /* ACEPC T11 Cherry Trail Z8350 mini PC */ |
| 700 | .matches = { |
| 701 | DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."), |
| 702 | DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"), |
| 703 | DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T11"), |
| 704 | /* also match on somewhat unique bios-version */ |
| 705 | DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"), |
| 706 | }, |
| 707 | }, |
| 708 | { |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 709 | /* Intel Cherry Trail Compute Stick, Windows version */ |
| 710 | .matches = { |
| 711 | DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), |
| 712 | DMI_MATCH(DMI_PRODUCT_NAME, "STK1AW32SC"), |
| 713 | }, |
| 714 | }, |
| 715 | { |
| 716 | /* Intel Cherry Trail Compute Stick, version without an OS */ |
| 717 | .matches = { |
| 718 | DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), |
| 719 | DMI_MATCH(DMI_PRODUCT_NAME, "STK1A32SC"), |
| 720 | }, |
| 721 | }, |
| 722 | { |
| 723 | /* Meegopad T08 */ |
| 724 | .matches = { |
| 725 | DMI_MATCH(DMI_SYS_VENDOR, "Default string"), |
| 726 | DMI_MATCH(DMI_BOARD_VENDOR, "To be filled by OEM."), |
| 727 | DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"), |
| 728 | DMI_MATCH(DMI_BOARD_VERSION, "V1.1"), |
| 729 | }, |
| 730 | }, |
Carlo Caione | 7638eb5 | 2018-02-16 08:26:16 +0000 | [diff] [blame] | 731 | { |
| 732 | /* ECS EF20EA */ |
| 733 | .matches = { |
| 734 | DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"), |
| 735 | }, |
| 736 | }, |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 737 | {} |
| 738 | }; |
| 739 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 740 | static int axp288_fuel_gauge_probe(struct platform_device *pdev) |
| 741 | { |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 742 | int i, ret = 0; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 743 | struct axp288_fg_info *info; |
| 744 | struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 745 | struct power_supply_config psy_cfg = {}; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 746 | static const char * const iio_chan_name[] = { |
| 747 | [BAT_TEMP] = "axp288-batt-temp", |
| 748 | [PMIC_TEMP] = "axp288-pmic-temp", |
| 749 | [SYSTEM_TEMP] = "axp288-system-temp", |
| 750 | [BAT_CHRG_CURR] = "axp288-chrg-curr", |
| 751 | [BAT_D_CURR] = "axp288-chrg-d-curr", |
| 752 | [BAT_VOLT] = "axp288-batt-volt", |
| 753 | }; |
Hans de Goede | 04d6f72 | 2018-04-18 14:08:21 +0200 | [diff] [blame] | 754 | unsigned int val; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 755 | |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 756 | if (dmi_check_system(axp288_fuel_gauge_blacklist)) |
| 757 | return -ENODEV; |
| 758 | |
Hans de Goede | 04d6f72 | 2018-04-18 14:08:21 +0200 | [diff] [blame] | 759 | /* |
| 760 | * On some devices the fuelgauge and charger parts of the axp288 are |
| 761 | * not used, check that the fuelgauge is enabled (CC_CTRL != 0). |
| 762 | */ |
| 763 | ret = regmap_read(axp20x->regmap, AXP20X_CC_CTRL, &val); |
| 764 | if (ret < 0) |
| 765 | return ret; |
| 766 | if (val == 0) |
| 767 | return -ENODEV; |
| 768 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 769 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 770 | if (!info) |
| 771 | return -ENOMEM; |
| 772 | |
| 773 | info->pdev = pdev; |
| 774 | info->regmap = axp20x->regmap; |
| 775 | info->regmap_irqc = axp20x->regmap_irqc; |
| 776 | info->status = POWER_SUPPLY_STATUS_UNKNOWN; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 777 | |
| 778 | platform_set_drvdata(pdev, info); |
| 779 | |
| 780 | mutex_init(&info->lock); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 781 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 782 | for (i = 0; i < IIO_CHANNEL_NUM; i++) { |
| 783 | /* |
| 784 | * Note cannot use devm_iio_channel_get because x86 systems |
| 785 | * lack the device<->channel maps which iio_channel_get will |
| 786 | * try to use when passed a non NULL device pointer. |
| 787 | */ |
| 788 | info->iio_channel[i] = |
| 789 | iio_channel_get(NULL, iio_chan_name[i]); |
| 790 | if (IS_ERR(info->iio_channel[i])) { |
| 791 | ret = PTR_ERR(info->iio_channel[i]); |
| 792 | dev_dbg(&pdev->dev, "error getting iiochan %s: %d\n", |
| 793 | iio_chan_name[i], ret); |
| 794 | /* Wait for axp288_adc to load */ |
| 795 | if (ret == -ENODEV) |
| 796 | ret = -EPROBE_DEFER; |
| 797 | |
| 798 | goto out_free_iio_chan; |
| 799 | } |
| 800 | } |
| 801 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 802 | ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG); |
| 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 | |
| 806 | if (!(ret & FG_DES_CAP1_VALID)) { |
| 807 | dev_err(&pdev->dev, "axp288 not configured by firmware\n"); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 808 | ret = -ENODEV; |
| 809 | goto out_free_iio_chan; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1); |
| 813 | if (ret < 0) |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 814 | goto out_free_iio_chan; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 815 | switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) { |
| 816 | case CHRG_CCCV_CV_4100MV: |
| 817 | info->max_volt = 4100; |
| 818 | break; |
| 819 | case CHRG_CCCV_CV_4150MV: |
| 820 | info->max_volt = 4150; |
| 821 | break; |
| 822 | case CHRG_CCCV_CV_4200MV: |
| 823 | info->max_volt = 4200; |
| 824 | break; |
| 825 | case CHRG_CCCV_CV_4350MV: |
| 826 | info->max_volt = 4350; |
| 827 | break; |
| 828 | } |
| 829 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 830 | psy_cfg.drv_data = info; |
| 831 | info->bat = power_supply_register(&pdev->dev, &fuel_gauge_desc, &psy_cfg); |
| 832 | if (IS_ERR(info->bat)) { |
| 833 | ret = PTR_ERR(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 834 | dev_err(&pdev->dev, "failed to register battery: %d\n", ret); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 835 | goto out_free_iio_chan; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | fuel_gauge_create_debugfs(info); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 839 | fuel_gauge_init_irq(info); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 840 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 841 | return 0; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 842 | |
| 843 | out_free_iio_chan: |
| 844 | for (i = 0; i < IIO_CHANNEL_NUM; i++) |
| 845 | if (!IS_ERR_OR_NULL(info->iio_channel[i])) |
| 846 | iio_channel_release(info->iio_channel[i]); |
| 847 | |
| 848 | return ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 849 | } |
| 850 | |
Krzysztof Kozlowski | f1f27a4 | 2015-05-02 00:46:43 +0900 | [diff] [blame] | 851 | static const struct platform_device_id axp288_fg_id_table[] = { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 852 | { .name = DEV_NAME }, |
| 853 | {}, |
| 854 | }; |
Javier Martinez Canillas | 99e33fb | 2016-10-17 16:13:36 -0300 | [diff] [blame] | 855 | MODULE_DEVICE_TABLE(platform, axp288_fg_id_table); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 856 | |
| 857 | static int axp288_fuel_gauge_remove(struct platform_device *pdev) |
| 858 | { |
| 859 | struct axp288_fg_info *info = platform_get_drvdata(pdev); |
| 860 | int i; |
| 861 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 862 | power_supply_unregister(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 863 | fuel_gauge_remove_debugfs(info); |
| 864 | |
| 865 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) |
| 866 | if (info->irq[i] >= 0) |
| 867 | free_irq(info->irq[i], info); |
| 868 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 869 | for (i = 0; i < IIO_CHANNEL_NUM; i++) |
| 870 | iio_channel_release(info->iio_channel[i]); |
| 871 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static struct platform_driver axp288_fuel_gauge_driver = { |
| 876 | .probe = axp288_fuel_gauge_probe, |
| 877 | .remove = axp288_fuel_gauge_remove, |
| 878 | .id_table = axp288_fg_id_table, |
| 879 | .driver = { |
| 880 | .name = DEV_NAME, |
| 881 | }, |
| 882 | }; |
| 883 | |
| 884 | module_platform_driver(axp288_fuel_gauge_driver); |
| 885 | |
Ramakrishna Pallala | 409e718 | 2015-03-13 21:49:09 +0530 | [diff] [blame] | 886 | MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 887 | MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>"); |
| 888 | MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver"); |
| 889 | MODULE_LICENSE("GPL"); |