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/workqueue.h> |
| 28 | #include <linux/mfd/axp20x.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/power_supply.h> |
| 31 | #include <linux/iio/consumer.h> |
| 32 | #include <linux/debugfs.h> |
| 33 | #include <linux/seq_file.h> |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 34 | #include <asm/unaligned.h> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 35 | |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 36 | #define PS_STAT_VBUS_TRIGGER (1 << 0) |
| 37 | #define PS_STAT_BAT_CHRG_DIR (1 << 2) |
| 38 | #define PS_STAT_VBAT_ABOVE_VHOLD (1 << 3) |
| 39 | #define PS_STAT_VBUS_VALID (1 << 4) |
| 40 | #define PS_STAT_VBUS_PRESENT (1 << 5) |
| 41 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 42 | #define CHRG_STAT_BAT_SAFE_MODE (1 << 3) |
| 43 | #define CHRG_STAT_BAT_VALID (1 << 4) |
| 44 | #define CHRG_STAT_BAT_PRESENT (1 << 5) |
| 45 | #define CHRG_STAT_CHARGING (1 << 6) |
| 46 | #define CHRG_STAT_PMIC_OTP (1 << 7) |
| 47 | |
| 48 | #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */ |
| 49 | #define CHRG_CCCV_CC_BIT_POS 0 |
| 50 | #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */ |
| 51 | #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */ |
| 52 | #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */ |
| 53 | #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */ |
| 54 | #define CHRG_CCCV_CV_BIT_POS 5 |
| 55 | #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */ |
| 56 | #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */ |
| 57 | #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */ |
| 58 | #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */ |
| 59 | #define CHRG_CCCV_CHG_EN (1 << 7) |
| 60 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 61 | #define FG_CNTL_OCV_ADJ_STAT (1 << 2) |
| 62 | #define FG_CNTL_OCV_ADJ_EN (1 << 3) |
| 63 | #define FG_CNTL_CAP_ADJ_STAT (1 << 4) |
| 64 | #define FG_CNTL_CAP_ADJ_EN (1 << 5) |
| 65 | #define FG_CNTL_CC_EN (1 << 6) |
| 66 | #define FG_CNTL_GAUGE_EN (1 << 7) |
| 67 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 68 | #define FG_15BIT_WORD_VALID (1 << 15) |
| 69 | #define FG_15BIT_VAL_MASK 0x7fff |
| 70 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 71 | #define FG_REP_CAP_VALID (1 << 7) |
| 72 | #define FG_REP_CAP_VAL_MASK 0x7F |
| 73 | |
| 74 | #define FG_DES_CAP1_VALID (1 << 7) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 75 | #define FG_DES_CAP_RES_LSB 1456 /* 1.456mAhr */ |
| 76 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 77 | #define FG_DES_CC_RES_LSB 1456 /* 1.456mAhr */ |
| 78 | |
| 79 | #define FG_OCV_CAP_VALID (1 << 7) |
| 80 | #define FG_OCV_CAP_VAL_MASK 0x7F |
| 81 | #define FG_CC_CAP_VALID (1 << 7) |
| 82 | #define FG_CC_CAP_VAL_MASK 0x7F |
| 83 | |
| 84 | #define FG_LOW_CAP_THR1_MASK 0xf0 /* 5% tp 20% */ |
| 85 | #define FG_LOW_CAP_THR1_VAL 0xa0 /* 15 perc */ |
| 86 | #define FG_LOW_CAP_THR2_MASK 0x0f /* 0% to 15% */ |
| 87 | #define FG_LOW_CAP_WARN_THR 14 /* 14 perc */ |
| 88 | #define FG_LOW_CAP_CRIT_THR 4 /* 4 perc */ |
| 89 | #define FG_LOW_CAP_SHDN_THR 0 /* 0 perc */ |
| 90 | |
| 91 | #define STATUS_MON_DELAY_JIFFIES (HZ * 60) /*60 sec */ |
| 92 | #define NR_RETRY_CNT 3 |
| 93 | #define DEV_NAME "axp288_fuel_gauge" |
| 94 | |
| 95 | /* 1.1mV per LSB expressed in uV */ |
| 96 | #define VOLTAGE_FROM_ADC(a) ((a * 11) / 10) |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 97 | /* properties converted to uV, uA */ |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 98 | #define PROP_VOLT(a) ((a) * 1000) |
| 99 | #define PROP_CURR(a) ((a) * 1000) |
| 100 | |
| 101 | #define AXP288_FG_INTR_NUM 6 |
| 102 | enum { |
| 103 | QWBTU_IRQ = 0, |
| 104 | WBTU_IRQ, |
| 105 | QWBTO_IRQ, |
| 106 | WBTO_IRQ, |
| 107 | WL2_IRQ, |
| 108 | WL1_IRQ, |
| 109 | }; |
| 110 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 111 | enum { |
| 112 | BAT_TEMP = 0, |
| 113 | PMIC_TEMP, |
| 114 | SYSTEM_TEMP, |
| 115 | BAT_CHRG_CURR, |
| 116 | BAT_D_CURR, |
| 117 | BAT_VOLT, |
| 118 | IIO_CHANNEL_NUM |
| 119 | }; |
| 120 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 121 | struct axp288_fg_info { |
| 122 | struct platform_device *pdev; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 123 | struct regmap *regmap; |
| 124 | struct regmap_irq_chip_data *regmap_irqc; |
| 125 | int irq[AXP288_FG_INTR_NUM]; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 126 | struct iio_channel *iio_channel[IIO_CHANNEL_NUM]; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 127 | struct power_supply *bat; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 128 | struct mutex lock; |
| 129 | int status; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 130 | int max_volt; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 131 | struct delayed_work status_monitor; |
| 132 | struct dentry *debug_file; |
| 133 | }; |
| 134 | |
| 135 | static enum power_supply_property fuel_gauge_props[] = { |
| 136 | POWER_SUPPLY_PROP_STATUS, |
| 137 | POWER_SUPPLY_PROP_PRESENT, |
| 138 | POWER_SUPPLY_PROP_HEALTH, |
| 139 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 140 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 141 | POWER_SUPPLY_PROP_VOLTAGE_OCV, |
| 142 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 143 | POWER_SUPPLY_PROP_CAPACITY, |
| 144 | POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 145 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 146 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 147 | POWER_SUPPLY_PROP_CHARGE_NOW, |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg) |
| 151 | { |
| 152 | int ret, i; |
| 153 | unsigned int val; |
| 154 | |
| 155 | for (i = 0; i < NR_RETRY_CNT; i++) { |
| 156 | ret = regmap_read(info->regmap, reg, &val); |
| 157 | if (ret == -EBUSY) |
| 158 | continue; |
| 159 | else |
| 160 | break; |
| 161 | } |
| 162 | |
Hans de Goede | 6f074bc | 2016-12-14 17:38:50 +0100 | [diff] [blame] | 163 | if (ret < 0) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 164 | 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] | 165 | return ret; |
| 166 | } |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 167 | |
| 168 | return val; |
| 169 | } |
| 170 | |
| 171 | static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val) |
| 172 | { |
| 173 | int ret; |
| 174 | |
| 175 | ret = regmap_write(info->regmap, reg, (unsigned int)val); |
| 176 | |
| 177 | if (ret < 0) |
| 178 | dev_err(&info->pdev->dev, "axp288 reg write err:%d\n", ret); |
| 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 183 | static int fuel_gauge_read_15bit_word(struct axp288_fg_info *info, int reg) |
| 184 | { |
| 185 | unsigned char buf[2]; |
| 186 | int ret; |
| 187 | |
| 188 | ret = regmap_bulk_read(info->regmap, reg, buf, 2); |
| 189 | if (ret < 0) { |
| 190 | dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n", |
| 191 | reg, ret); |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | ret = get_unaligned_be16(buf); |
| 196 | if (!(ret & FG_15BIT_WORD_VALID)) { |
| 197 | dev_err(&info->pdev->dev, "Error reg 0x%02x contents not valid\n", |
| 198 | reg); |
| 199 | return -ENXIO; |
| 200 | } |
| 201 | |
| 202 | return ret & FG_15BIT_VAL_MASK; |
| 203 | } |
| 204 | |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 205 | static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg) |
| 206 | { |
| 207 | unsigned char buf[2]; |
| 208 | int ret; |
| 209 | |
| 210 | ret = regmap_bulk_read(info->regmap, reg, buf, 2); |
| 211 | if (ret < 0) { |
| 212 | dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n", |
| 213 | reg, ret); |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | /* 12-bit data values have upper 8 bits in buf[0], lower 4 in buf[1] */ |
| 218 | return (buf[0] << 4) | ((buf[1] >> 4) & 0x0f); |
| 219 | } |
| 220 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 221 | #ifdef CONFIG_DEBUG_FS |
| 222 | static int fuel_gauge_debug_show(struct seq_file *s, void *data) |
| 223 | { |
| 224 | struct axp288_fg_info *info = s->private; |
| 225 | int raw_val, ret; |
| 226 | |
| 227 | seq_printf(s, " PWR_STATUS[%02x] : %02x\n", |
| 228 | AXP20X_PWR_INPUT_STATUS, |
| 229 | fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS)); |
| 230 | seq_printf(s, "PWR_OP_MODE[%02x] : %02x\n", |
| 231 | AXP20X_PWR_OP_MODE, |
| 232 | fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE)); |
| 233 | seq_printf(s, " CHRG_CTRL1[%02x] : %02x\n", |
| 234 | AXP20X_CHRG_CTRL1, |
| 235 | fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1)); |
| 236 | seq_printf(s, " VLTF[%02x] : %02x\n", |
| 237 | AXP20X_V_LTF_DISCHRG, |
| 238 | fuel_gauge_reg_readb(info, AXP20X_V_LTF_DISCHRG)); |
| 239 | seq_printf(s, " VHTF[%02x] : %02x\n", |
| 240 | AXP20X_V_HTF_DISCHRG, |
| 241 | fuel_gauge_reg_readb(info, AXP20X_V_HTF_DISCHRG)); |
| 242 | seq_printf(s, " CC_CTRL[%02x] : %02x\n", |
| 243 | AXP20X_CC_CTRL, |
| 244 | fuel_gauge_reg_readb(info, AXP20X_CC_CTRL)); |
| 245 | seq_printf(s, "BATTERY CAP[%02x] : %02x\n", |
| 246 | AXP20X_FG_RES, |
| 247 | fuel_gauge_reg_readb(info, AXP20X_FG_RES)); |
| 248 | seq_printf(s, " FG_RDC1[%02x] : %02x\n", |
| 249 | AXP288_FG_RDC1_REG, |
| 250 | fuel_gauge_reg_readb(info, AXP288_FG_RDC1_REG)); |
| 251 | seq_printf(s, " FG_RDC0[%02x] : %02x\n", |
| 252 | AXP288_FG_RDC0_REG, |
| 253 | fuel_gauge_reg_readb(info, AXP288_FG_RDC0_REG)); |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 254 | seq_printf(s, " FG_OCV[%02x] : %04x\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 255 | AXP288_FG_OCVH_REG, |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 256 | fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG)); |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 257 | seq_printf(s, " FG_DES_CAP[%02x] : %04x\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 258 | AXP288_FG_DES_CAP1_REG, |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 259 | fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG)); |
| 260 | seq_printf(s, " FG_CC_MTR[%02x] : %04x\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 261 | AXP288_FG_CC_MTR1_REG, |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 262 | fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG)); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 263 | seq_printf(s, " FG_OCV_CAP[%02x] : %02x\n", |
| 264 | AXP288_FG_OCV_CAP_REG, |
| 265 | fuel_gauge_reg_readb(info, AXP288_FG_OCV_CAP_REG)); |
| 266 | seq_printf(s, " FG_CC_CAP[%02x] : %02x\n", |
| 267 | AXP288_FG_CC_CAP_REG, |
| 268 | fuel_gauge_reg_readb(info, AXP288_FG_CC_CAP_REG)); |
| 269 | seq_printf(s, " FG_LOW_CAP[%02x] : %02x\n", |
| 270 | AXP288_FG_LOW_CAP_REG, |
| 271 | fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG)); |
| 272 | seq_printf(s, "TUNING_CTL0[%02x] : %02x\n", |
| 273 | AXP288_FG_TUNE0, |
| 274 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE0)); |
| 275 | seq_printf(s, "TUNING_CTL1[%02x] : %02x\n", |
| 276 | AXP288_FG_TUNE1, |
| 277 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE1)); |
| 278 | seq_printf(s, "TUNING_CTL2[%02x] : %02x\n", |
| 279 | AXP288_FG_TUNE2, |
| 280 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE2)); |
| 281 | seq_printf(s, "TUNING_CTL3[%02x] : %02x\n", |
| 282 | AXP288_FG_TUNE3, |
| 283 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE3)); |
| 284 | seq_printf(s, "TUNING_CTL4[%02x] : %02x\n", |
| 285 | AXP288_FG_TUNE4, |
| 286 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE4)); |
| 287 | seq_printf(s, "TUNING_CTL5[%02x] : %02x\n", |
| 288 | AXP288_FG_TUNE5, |
| 289 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE5)); |
| 290 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 291 | ret = iio_read_channel_raw(info->iio_channel[BAT_TEMP], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 292 | if (ret >= 0) |
| 293 | seq_printf(s, "axp288-batttemp : %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[PMIC_TEMP], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 295 | if (ret >= 0) |
| 296 | seq_printf(s, "axp288-pmictemp : %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[SYSTEM_TEMP], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 298 | if (ret >= 0) |
| 299 | seq_printf(s, "axp288-systtemp : %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_CHRG_CURR], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 301 | if (ret >= 0) |
| 302 | seq_printf(s, "axp288-chrgcurr : %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_D_CURR], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 304 | if (ret >= 0) |
| 305 | seq_printf(s, "axp288-dchrgcur : %d\n", raw_val); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 306 | ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 307 | if (ret >= 0) |
| 308 | seq_printf(s, "axp288-battvolt : %d\n", raw_val); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int debug_open(struct inode *inode, struct file *file) |
| 314 | { |
| 315 | return single_open(file, fuel_gauge_debug_show, inode->i_private); |
| 316 | } |
| 317 | |
| 318 | static const struct file_operations fg_debug_fops = { |
| 319 | .open = debug_open, |
| 320 | .read = seq_read, |
| 321 | .llseek = seq_lseek, |
| 322 | .release = single_release, |
| 323 | }; |
| 324 | |
| 325 | static void fuel_gauge_create_debugfs(struct axp288_fg_info *info) |
| 326 | { |
| 327 | info->debug_file = debugfs_create_file("fuelgauge", 0666, NULL, |
| 328 | info, &fg_debug_fops); |
| 329 | } |
| 330 | |
| 331 | static void fuel_gauge_remove_debugfs(struct axp288_fg_info *info) |
| 332 | { |
| 333 | debugfs_remove(info->debug_file); |
| 334 | } |
| 335 | #else |
| 336 | static inline void fuel_gauge_create_debugfs(struct axp288_fg_info *info) |
| 337 | { |
| 338 | } |
| 339 | static inline void fuel_gauge_remove_debugfs(struct axp288_fg_info *info) |
| 340 | { |
| 341 | } |
| 342 | #endif |
| 343 | |
| 344 | static void fuel_gauge_get_status(struct axp288_fg_info *info) |
| 345 | { |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 346 | int pwr_stat, fg_res; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 347 | |
| 348 | pwr_stat = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS); |
| 349 | if (pwr_stat < 0) { |
| 350 | dev_err(&info->pdev->dev, |
| 351 | "PWR STAT read failed:%d\n", pwr_stat); |
| 352 | return; |
| 353 | } |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 354 | |
| 355 | /* Report full if Vbus is valid and the reported capacity is 100% */ |
| 356 | if (pwr_stat & PS_STAT_VBUS_VALID) { |
| 357 | fg_res = fuel_gauge_reg_readb(info, AXP20X_FG_RES); |
| 358 | if (fg_res < 0) { |
| 359 | dev_err(&info->pdev->dev, |
| 360 | "FG RES read failed: %d\n", fg_res); |
| 361 | return; |
| 362 | } |
| 363 | if (fg_res == (FG_REP_CAP_VALID | 100)) { |
| 364 | info->status = POWER_SUPPLY_STATUS_FULL; |
| 365 | return; |
| 366 | } |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 369 | if (pwr_stat & PS_STAT_BAT_CHRG_DIR) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 370 | info->status = POWER_SUPPLY_STATUS_CHARGING; |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 371 | else |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 372 | info->status = POWER_SUPPLY_STATUS_DISCHARGING; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static int fuel_gauge_get_vbatt(struct axp288_fg_info *info, int *vbatt) |
| 376 | { |
| 377 | int ret = 0, raw_val; |
| 378 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 379 | ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &raw_val); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 380 | if (ret < 0) |
| 381 | goto vbatt_read_fail; |
| 382 | |
| 383 | *vbatt = VOLTAGE_FROM_ADC(raw_val); |
| 384 | vbatt_read_fail: |
| 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | static int fuel_gauge_get_current(struct axp288_fg_info *info, int *cur) |
| 389 | { |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 390 | int ret, discharge; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 391 | |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 392 | /* 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] | 393 | ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &discharge); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 394 | if (ret < 0) |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 395 | return ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 396 | |
Hans de Goede | ceb4083 | 2017-12-26 13:59:12 +0100 | [diff] [blame] | 397 | if (discharge > 0) { |
| 398 | *cur = -1 * discharge; |
| 399 | return 0; |
| 400 | } |
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 | return iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], cur); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 405 | static int fuel_gauge_get_vocv(struct axp288_fg_info *info, int *vocv) |
| 406 | { |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 407 | int ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 408 | |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 409 | ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG); |
| 410 | if (ret >= 0) |
| 411 | *vocv = VOLTAGE_FROM_ADC(ret); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 412 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 413 | return ret; |
| 414 | } |
| 415 | |
| 416 | static int fuel_gauge_battery_health(struct axp288_fg_info *info) |
| 417 | { |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 418 | int ret, vocv, health = POWER_SUPPLY_HEALTH_UNKNOWN; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 419 | |
| 420 | ret = fuel_gauge_get_vocv(info, &vocv); |
| 421 | if (ret < 0) |
| 422 | goto health_read_fail; |
| 423 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 424 | if (vocv > info->max_volt) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 425 | health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 426 | else |
| 427 | health = POWER_SUPPLY_HEALTH_GOOD; |
| 428 | |
| 429 | health_read_fail: |
| 430 | return health; |
| 431 | } |
| 432 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 433 | static int fuel_gauge_get_property(struct power_supply *ps, |
| 434 | enum power_supply_property prop, |
| 435 | union power_supply_propval *val) |
| 436 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 437 | struct axp288_fg_info *info = power_supply_get_drvdata(ps); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 438 | int ret = 0, value; |
| 439 | |
| 440 | mutex_lock(&info->lock); |
| 441 | switch (prop) { |
| 442 | case POWER_SUPPLY_PROP_STATUS: |
| 443 | fuel_gauge_get_status(info); |
| 444 | val->intval = info->status; |
| 445 | break; |
| 446 | case POWER_SUPPLY_PROP_HEALTH: |
| 447 | val->intval = fuel_gauge_battery_health(info); |
| 448 | break; |
| 449 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 450 | ret = fuel_gauge_get_vbatt(info, &value); |
| 451 | if (ret < 0) |
| 452 | goto fuel_gauge_read_err; |
| 453 | val->intval = PROP_VOLT(value); |
| 454 | break; |
| 455 | case POWER_SUPPLY_PROP_VOLTAGE_OCV: |
| 456 | ret = fuel_gauge_get_vocv(info, &value); |
| 457 | if (ret < 0) |
| 458 | goto fuel_gauge_read_err; |
| 459 | val->intval = PROP_VOLT(value); |
| 460 | break; |
| 461 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 462 | ret = fuel_gauge_get_current(info, &value); |
| 463 | if (ret < 0) |
| 464 | goto fuel_gauge_read_err; |
| 465 | val->intval = PROP_CURR(value); |
| 466 | break; |
| 467 | case POWER_SUPPLY_PROP_PRESENT: |
| 468 | ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE); |
| 469 | if (ret < 0) |
| 470 | goto fuel_gauge_read_err; |
| 471 | |
| 472 | if (ret & CHRG_STAT_BAT_PRESENT) |
| 473 | val->intval = 1; |
| 474 | else |
| 475 | val->intval = 0; |
| 476 | break; |
| 477 | case POWER_SUPPLY_PROP_CAPACITY: |
| 478 | ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES); |
| 479 | if (ret < 0) |
| 480 | goto fuel_gauge_read_err; |
| 481 | |
| 482 | if (!(ret & FG_REP_CAP_VALID)) |
| 483 | dev_err(&info->pdev->dev, |
| 484 | "capacity measurement not valid\n"); |
| 485 | val->intval = (ret & FG_REP_CAP_VAL_MASK); |
| 486 | break; |
| 487 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 488 | ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); |
| 489 | if (ret < 0) |
| 490 | goto fuel_gauge_read_err; |
| 491 | val->intval = (ret & 0x0f); |
| 492 | break; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 493 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 494 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 495 | break; |
| 496 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 497 | ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 498 | if (ret < 0) |
| 499 | goto fuel_gauge_read_err; |
| 500 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 501 | val->intval = ret * FG_DES_CAP_RES_LSB; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 502 | break; |
| 503 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 504 | ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 505 | if (ret < 0) |
| 506 | goto fuel_gauge_read_err; |
| 507 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 508 | val->intval = ret * FG_DES_CAP_RES_LSB; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 509 | break; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 510 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 511 | val->intval = PROP_VOLT(info->max_volt); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 512 | break; |
| 513 | default: |
| 514 | mutex_unlock(&info->lock); |
| 515 | return -EINVAL; |
| 516 | } |
| 517 | |
| 518 | mutex_unlock(&info->lock); |
| 519 | return 0; |
| 520 | |
| 521 | fuel_gauge_read_err: |
| 522 | mutex_unlock(&info->lock); |
| 523 | return ret; |
| 524 | } |
| 525 | |
| 526 | static int fuel_gauge_set_property(struct power_supply *ps, |
| 527 | enum power_supply_property prop, |
| 528 | const union power_supply_propval *val) |
| 529 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 530 | struct axp288_fg_info *info = power_supply_get_drvdata(ps); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 531 | int ret = 0; |
| 532 | |
| 533 | mutex_lock(&info->lock); |
| 534 | switch (prop) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 535 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 536 | if ((val->intval < 0) || (val->intval > 15)) { |
| 537 | ret = -EINVAL; |
| 538 | break; |
| 539 | } |
| 540 | ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); |
| 541 | if (ret < 0) |
| 542 | break; |
| 543 | ret &= 0xf0; |
| 544 | ret |= (val->intval & 0xf); |
| 545 | ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, ret); |
| 546 | break; |
| 547 | default: |
| 548 | ret = -EINVAL; |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | mutex_unlock(&info->lock); |
| 553 | return ret; |
| 554 | } |
| 555 | |
| 556 | static int fuel_gauge_property_is_writeable(struct power_supply *psy, |
| 557 | enum power_supply_property psp) |
| 558 | { |
| 559 | int ret; |
| 560 | |
| 561 | switch (psp) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 562 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 563 | ret = 1; |
| 564 | break; |
| 565 | default: |
| 566 | ret = 0; |
| 567 | } |
| 568 | |
| 569 | return ret; |
| 570 | } |
| 571 | |
| 572 | static void fuel_gauge_status_monitor(struct work_struct *work) |
| 573 | { |
| 574 | struct axp288_fg_info *info = container_of(work, |
| 575 | struct axp288_fg_info, status_monitor.work); |
| 576 | |
| 577 | fuel_gauge_get_status(info); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 578 | power_supply_changed(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 579 | schedule_delayed_work(&info->status_monitor, STATUS_MON_DELAY_JIFFIES); |
| 580 | } |
| 581 | |
| 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 | { |
| 689 | /* Intel Cherry Trail Compute Stick, Windows version */ |
| 690 | .matches = { |
| 691 | DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), |
| 692 | DMI_MATCH(DMI_PRODUCT_NAME, "STK1AW32SC"), |
| 693 | }, |
| 694 | }, |
| 695 | { |
| 696 | /* Intel Cherry Trail Compute Stick, version without an OS */ |
| 697 | .matches = { |
| 698 | DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), |
| 699 | DMI_MATCH(DMI_PRODUCT_NAME, "STK1A32SC"), |
| 700 | }, |
| 701 | }, |
| 702 | { |
| 703 | /* Meegopad T08 */ |
| 704 | .matches = { |
| 705 | DMI_MATCH(DMI_SYS_VENDOR, "Default string"), |
| 706 | DMI_MATCH(DMI_BOARD_VENDOR, "To be filled by OEM."), |
| 707 | DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"), |
| 708 | DMI_MATCH(DMI_BOARD_VERSION, "V1.1"), |
| 709 | }, |
| 710 | }, |
| 711 | {} |
| 712 | }; |
| 713 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 714 | static int axp288_fuel_gauge_probe(struct platform_device *pdev) |
| 715 | { |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 716 | int i, ret = 0; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 717 | struct axp288_fg_info *info; |
| 718 | struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 719 | struct power_supply_config psy_cfg = {}; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 720 | static const char * const iio_chan_name[] = { |
| 721 | [BAT_TEMP] = "axp288-batt-temp", |
| 722 | [PMIC_TEMP] = "axp288-pmic-temp", |
| 723 | [SYSTEM_TEMP] = "axp288-system-temp", |
| 724 | [BAT_CHRG_CURR] = "axp288-chrg-curr", |
| 725 | [BAT_D_CURR] = "axp288-chrg-d-curr", |
| 726 | [BAT_VOLT] = "axp288-batt-volt", |
| 727 | }; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 728 | |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 729 | if (dmi_check_system(axp288_fuel_gauge_blacklist)) |
| 730 | return -ENODEV; |
| 731 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 732 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 733 | if (!info) |
| 734 | return -ENOMEM; |
| 735 | |
| 736 | info->pdev = pdev; |
| 737 | info->regmap = axp20x->regmap; |
| 738 | info->regmap_irqc = axp20x->regmap_irqc; |
| 739 | info->status = POWER_SUPPLY_STATUS_UNKNOWN; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 740 | |
| 741 | platform_set_drvdata(pdev, info); |
| 742 | |
| 743 | mutex_init(&info->lock); |
| 744 | INIT_DELAYED_WORK(&info->status_monitor, fuel_gauge_status_monitor); |
| 745 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 746 | for (i = 0; i < IIO_CHANNEL_NUM; i++) { |
| 747 | /* |
| 748 | * Note cannot use devm_iio_channel_get because x86 systems |
| 749 | * lack the device<->channel maps which iio_channel_get will |
| 750 | * try to use when passed a non NULL device pointer. |
| 751 | */ |
| 752 | info->iio_channel[i] = |
| 753 | iio_channel_get(NULL, iio_chan_name[i]); |
| 754 | if (IS_ERR(info->iio_channel[i])) { |
| 755 | ret = PTR_ERR(info->iio_channel[i]); |
| 756 | dev_dbg(&pdev->dev, "error getting iiochan %s: %d\n", |
| 757 | iio_chan_name[i], ret); |
| 758 | /* Wait for axp288_adc to load */ |
| 759 | if (ret == -ENODEV) |
| 760 | ret = -EPROBE_DEFER; |
| 761 | |
| 762 | goto out_free_iio_chan; |
| 763 | } |
| 764 | } |
| 765 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 766 | ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG); |
| 767 | if (ret < 0) |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 768 | goto out_free_iio_chan; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 769 | |
| 770 | if (!(ret & FG_DES_CAP1_VALID)) { |
| 771 | dev_err(&pdev->dev, "axp288 not configured by firmware\n"); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 772 | ret = -ENODEV; |
| 773 | goto out_free_iio_chan; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1); |
| 777 | if (ret < 0) |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 778 | goto out_free_iio_chan; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 779 | switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) { |
| 780 | case CHRG_CCCV_CV_4100MV: |
| 781 | info->max_volt = 4100; |
| 782 | break; |
| 783 | case CHRG_CCCV_CV_4150MV: |
| 784 | info->max_volt = 4150; |
| 785 | break; |
| 786 | case CHRG_CCCV_CV_4200MV: |
| 787 | info->max_volt = 4200; |
| 788 | break; |
| 789 | case CHRG_CCCV_CV_4350MV: |
| 790 | info->max_volt = 4350; |
| 791 | break; |
| 792 | } |
| 793 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 794 | psy_cfg.drv_data = info; |
| 795 | info->bat = power_supply_register(&pdev->dev, &fuel_gauge_desc, &psy_cfg); |
| 796 | if (IS_ERR(info->bat)) { |
| 797 | ret = PTR_ERR(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 798 | dev_err(&pdev->dev, "failed to register battery: %d\n", ret); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 799 | goto out_free_iio_chan; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | fuel_gauge_create_debugfs(info); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 803 | fuel_gauge_init_irq(info); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 804 | schedule_delayed_work(&info->status_monitor, STATUS_MON_DELAY_JIFFIES); |
| 805 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 806 | return 0; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 807 | |
| 808 | out_free_iio_chan: |
| 809 | for (i = 0; i < IIO_CHANNEL_NUM; i++) |
| 810 | if (!IS_ERR_OR_NULL(info->iio_channel[i])) |
| 811 | iio_channel_release(info->iio_channel[i]); |
| 812 | |
| 813 | return ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Krzysztof Kozlowski | f1f27a4 | 2015-05-02 00:46:43 +0900 | [diff] [blame] | 816 | static const struct platform_device_id axp288_fg_id_table[] = { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 817 | { .name = DEV_NAME }, |
| 818 | {}, |
| 819 | }; |
Javier Martinez Canillas | 99e33fb | 2016-10-17 16:13:36 -0300 | [diff] [blame] | 820 | MODULE_DEVICE_TABLE(platform, axp288_fg_id_table); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 821 | |
| 822 | static int axp288_fuel_gauge_remove(struct platform_device *pdev) |
| 823 | { |
| 824 | struct axp288_fg_info *info = platform_get_drvdata(pdev); |
| 825 | int i; |
| 826 | |
| 827 | cancel_delayed_work_sync(&info->status_monitor); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 828 | power_supply_unregister(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 829 | fuel_gauge_remove_debugfs(info); |
| 830 | |
| 831 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) |
| 832 | if (info->irq[i] >= 0) |
| 833 | free_irq(info->irq[i], info); |
| 834 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 835 | for (i = 0; i < IIO_CHANNEL_NUM; i++) |
| 836 | iio_channel_release(info->iio_channel[i]); |
| 837 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | static struct platform_driver axp288_fuel_gauge_driver = { |
| 842 | .probe = axp288_fuel_gauge_probe, |
| 843 | .remove = axp288_fuel_gauge_remove, |
| 844 | .id_table = axp288_fg_id_table, |
| 845 | .driver = { |
| 846 | .name = DEV_NAME, |
| 847 | }, |
| 848 | }; |
| 849 | |
| 850 | module_platform_driver(axp288_fuel_gauge_driver); |
| 851 | |
Ramakrishna Pallala | 409e718 | 2015-03-13 21:49:09 +0530 | [diff] [blame] | 852 | MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 853 | MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>"); |
| 854 | MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver"); |
| 855 | MODULE_LICENSE("GPL"); |