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 | * |
| 4 | * Copyright (C) 2014 Intel Corporation |
| 5 | * |
| 6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/device.h> |
| 22 | #include <linux/regmap.h> |
| 23 | #include <linux/jiffies.h> |
| 24 | #include <linux/interrupt.h> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 25 | #include <linux/workqueue.h> |
| 26 | #include <linux/mfd/axp20x.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/power_supply.h> |
| 29 | #include <linux/iio/consumer.h> |
| 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/seq_file.h> |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 32 | #include <asm/unaligned.h> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 33 | |
| 34 | #define CHRG_STAT_BAT_SAFE_MODE (1 << 3) |
| 35 | #define CHRG_STAT_BAT_VALID (1 << 4) |
| 36 | #define CHRG_STAT_BAT_PRESENT (1 << 5) |
| 37 | #define CHRG_STAT_CHARGING (1 << 6) |
| 38 | #define CHRG_STAT_PMIC_OTP (1 << 7) |
| 39 | |
| 40 | #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */ |
| 41 | #define CHRG_CCCV_CC_BIT_POS 0 |
| 42 | #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */ |
| 43 | #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */ |
| 44 | #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */ |
| 45 | #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */ |
| 46 | #define CHRG_CCCV_CV_BIT_POS 5 |
| 47 | #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */ |
| 48 | #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */ |
| 49 | #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */ |
| 50 | #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */ |
| 51 | #define CHRG_CCCV_CHG_EN (1 << 7) |
| 52 | |
| 53 | #define CV_4100 4100 /* 4100mV */ |
| 54 | #define CV_4150 4150 /* 4150mV */ |
| 55 | #define CV_4200 4200 /* 4200mV */ |
| 56 | #define CV_4350 4350 /* 4350mV */ |
| 57 | |
| 58 | #define TEMP_IRQ_CFG_QWBTU (1 << 0) |
| 59 | #define TEMP_IRQ_CFG_WBTU (1 << 1) |
| 60 | #define TEMP_IRQ_CFG_QWBTO (1 << 2) |
| 61 | #define TEMP_IRQ_CFG_WBTO (1 << 3) |
| 62 | #define TEMP_IRQ_CFG_MASK 0xf |
| 63 | |
| 64 | #define FG_IRQ_CFG_LOWBATT_WL2 (1 << 0) |
| 65 | #define FG_IRQ_CFG_LOWBATT_WL1 (1 << 1) |
| 66 | #define FG_IRQ_CFG_LOWBATT_MASK 0x3 |
| 67 | #define LOWBAT_IRQ_STAT_LOWBATT_WL2 (1 << 0) |
| 68 | #define LOWBAT_IRQ_STAT_LOWBATT_WL1 (1 << 1) |
| 69 | |
| 70 | #define FG_CNTL_OCV_ADJ_STAT (1 << 2) |
| 71 | #define FG_CNTL_OCV_ADJ_EN (1 << 3) |
| 72 | #define FG_CNTL_CAP_ADJ_STAT (1 << 4) |
| 73 | #define FG_CNTL_CAP_ADJ_EN (1 << 5) |
| 74 | #define FG_CNTL_CC_EN (1 << 6) |
| 75 | #define FG_CNTL_GAUGE_EN (1 << 7) |
| 76 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 77 | #define FG_15BIT_WORD_VALID (1 << 15) |
| 78 | #define FG_15BIT_VAL_MASK 0x7fff |
| 79 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 80 | #define FG_REP_CAP_VALID (1 << 7) |
| 81 | #define FG_REP_CAP_VAL_MASK 0x7F |
| 82 | |
| 83 | #define FG_DES_CAP1_VALID (1 << 7) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 84 | #define FG_DES_CAP_RES_LSB 1456 /* 1.456mAhr */ |
| 85 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 86 | #define FG_DES_CC_RES_LSB 1456 /* 1.456mAhr */ |
| 87 | |
| 88 | #define FG_OCV_CAP_VALID (1 << 7) |
| 89 | #define FG_OCV_CAP_VAL_MASK 0x7F |
| 90 | #define FG_CC_CAP_VALID (1 << 7) |
| 91 | #define FG_CC_CAP_VAL_MASK 0x7F |
| 92 | |
| 93 | #define FG_LOW_CAP_THR1_MASK 0xf0 /* 5% tp 20% */ |
| 94 | #define FG_LOW_CAP_THR1_VAL 0xa0 /* 15 perc */ |
| 95 | #define FG_LOW_CAP_THR2_MASK 0x0f /* 0% to 15% */ |
| 96 | #define FG_LOW_CAP_WARN_THR 14 /* 14 perc */ |
| 97 | #define FG_LOW_CAP_CRIT_THR 4 /* 4 perc */ |
| 98 | #define FG_LOW_CAP_SHDN_THR 0 /* 0 perc */ |
| 99 | |
| 100 | #define STATUS_MON_DELAY_JIFFIES (HZ * 60) /*60 sec */ |
| 101 | #define NR_RETRY_CNT 3 |
| 102 | #define DEV_NAME "axp288_fuel_gauge" |
| 103 | |
| 104 | /* 1.1mV per LSB expressed in uV */ |
| 105 | #define VOLTAGE_FROM_ADC(a) ((a * 11) / 10) |
| 106 | /* properties converted to tenths of degrees, uV, uA, uW */ |
| 107 | #define PROP_TEMP(a) ((a) * 10) |
| 108 | #define UNPROP_TEMP(a) ((a) / 10) |
| 109 | #define PROP_VOLT(a) ((a) * 1000) |
| 110 | #define PROP_CURR(a) ((a) * 1000) |
| 111 | |
| 112 | #define AXP288_FG_INTR_NUM 6 |
| 113 | enum { |
| 114 | QWBTU_IRQ = 0, |
| 115 | WBTU_IRQ, |
| 116 | QWBTO_IRQ, |
| 117 | WBTO_IRQ, |
| 118 | WL2_IRQ, |
| 119 | WL1_IRQ, |
| 120 | }; |
| 121 | |
| 122 | struct axp288_fg_info { |
| 123 | struct platform_device *pdev; |
| 124 | struct axp20x_fg_pdata *pdata; |
| 125 | struct regmap *regmap; |
| 126 | struct regmap_irq_chip_data *regmap_irqc; |
| 127 | int irq[AXP288_FG_INTR_NUM]; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 128 | struct power_supply *bat; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 129 | struct mutex lock; |
| 130 | int status; |
| 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, |
| 140 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 141 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 142 | POWER_SUPPLY_PROP_VOLTAGE_OCV, |
| 143 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 144 | POWER_SUPPLY_PROP_CAPACITY, |
| 145 | POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, |
| 146 | POWER_SUPPLY_PROP_TEMP, |
| 147 | POWER_SUPPLY_PROP_TEMP_MAX, |
| 148 | POWER_SUPPLY_PROP_TEMP_MIN, |
| 149 | POWER_SUPPLY_PROP_TEMP_ALERT_MIN, |
| 150 | POWER_SUPPLY_PROP_TEMP_ALERT_MAX, |
| 151 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 152 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 153 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 154 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 155 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 156 | }; |
| 157 | |
| 158 | static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg) |
| 159 | { |
| 160 | int ret, i; |
| 161 | unsigned int val; |
| 162 | |
| 163 | for (i = 0; i < NR_RETRY_CNT; i++) { |
| 164 | ret = regmap_read(info->regmap, reg, &val); |
| 165 | if (ret == -EBUSY) |
| 166 | continue; |
| 167 | else |
| 168 | break; |
| 169 | } |
| 170 | |
Hans de Goede | 6f074bc | 2016-12-14 17:38:50 +0100 | [diff] [blame] | 171 | if (ret < 0) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 172 | 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] | 173 | return ret; |
| 174 | } |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 175 | |
| 176 | return val; |
| 177 | } |
| 178 | |
| 179 | static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val) |
| 180 | { |
| 181 | int ret; |
| 182 | |
| 183 | ret = regmap_write(info->regmap, reg, (unsigned int)val); |
| 184 | |
| 185 | if (ret < 0) |
| 186 | dev_err(&info->pdev->dev, "axp288 reg write err:%d\n", ret); |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 191 | static int fuel_gauge_read_15bit_word(struct axp288_fg_info *info, int reg) |
| 192 | { |
| 193 | unsigned char buf[2]; |
| 194 | int ret; |
| 195 | |
| 196 | ret = regmap_bulk_read(info->regmap, reg, buf, 2); |
| 197 | if (ret < 0) { |
| 198 | dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n", |
| 199 | reg, ret); |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | ret = get_unaligned_be16(buf); |
| 204 | if (!(ret & FG_15BIT_WORD_VALID)) { |
| 205 | dev_err(&info->pdev->dev, "Error reg 0x%02x contents not valid\n", |
| 206 | reg); |
| 207 | return -ENXIO; |
| 208 | } |
| 209 | |
| 210 | return ret & FG_15BIT_VAL_MASK; |
| 211 | } |
| 212 | |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame^] | 213 | static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg) |
| 214 | { |
| 215 | unsigned char buf[2]; |
| 216 | int ret; |
| 217 | |
| 218 | ret = regmap_bulk_read(info->regmap, reg, buf, 2); |
| 219 | if (ret < 0) { |
| 220 | dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n", |
| 221 | reg, ret); |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | /* 12-bit data values have upper 8 bits in buf[0], lower 4 in buf[1] */ |
| 226 | return (buf[0] << 4) | ((buf[1] >> 4) & 0x0f); |
| 227 | } |
| 228 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 229 | static int pmic_read_adc_val(const char *name, int *raw_val, |
| 230 | struct axp288_fg_info *info) |
| 231 | { |
| 232 | int ret, val = 0; |
| 233 | struct iio_channel *indio_chan; |
| 234 | |
| 235 | indio_chan = iio_channel_get(NULL, name); |
| 236 | if (IS_ERR_OR_NULL(indio_chan)) { |
| 237 | ret = PTR_ERR(indio_chan); |
| 238 | goto exit; |
| 239 | } |
| 240 | ret = iio_read_channel_raw(indio_chan, &val); |
| 241 | if (ret < 0) { |
| 242 | dev_err(&info->pdev->dev, |
| 243 | "IIO channel read error: %x, %x\n", ret, val); |
| 244 | goto err_exit; |
| 245 | } |
| 246 | |
| 247 | dev_dbg(&info->pdev->dev, "adc raw val=%x\n", val); |
| 248 | *raw_val = val; |
| 249 | |
| 250 | err_exit: |
| 251 | iio_channel_release(indio_chan); |
| 252 | exit: |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | #ifdef CONFIG_DEBUG_FS |
| 257 | static int fuel_gauge_debug_show(struct seq_file *s, void *data) |
| 258 | { |
| 259 | struct axp288_fg_info *info = s->private; |
| 260 | int raw_val, ret; |
| 261 | |
| 262 | seq_printf(s, " PWR_STATUS[%02x] : %02x\n", |
| 263 | AXP20X_PWR_INPUT_STATUS, |
| 264 | fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS)); |
| 265 | seq_printf(s, "PWR_OP_MODE[%02x] : %02x\n", |
| 266 | AXP20X_PWR_OP_MODE, |
| 267 | fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE)); |
| 268 | seq_printf(s, " CHRG_CTRL1[%02x] : %02x\n", |
| 269 | AXP20X_CHRG_CTRL1, |
| 270 | fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1)); |
| 271 | seq_printf(s, " VLTF[%02x] : %02x\n", |
| 272 | AXP20X_V_LTF_DISCHRG, |
| 273 | fuel_gauge_reg_readb(info, AXP20X_V_LTF_DISCHRG)); |
| 274 | seq_printf(s, " VHTF[%02x] : %02x\n", |
| 275 | AXP20X_V_HTF_DISCHRG, |
| 276 | fuel_gauge_reg_readb(info, AXP20X_V_HTF_DISCHRG)); |
| 277 | seq_printf(s, " CC_CTRL[%02x] : %02x\n", |
| 278 | AXP20X_CC_CTRL, |
| 279 | fuel_gauge_reg_readb(info, AXP20X_CC_CTRL)); |
| 280 | seq_printf(s, "BATTERY CAP[%02x] : %02x\n", |
| 281 | AXP20X_FG_RES, |
| 282 | fuel_gauge_reg_readb(info, AXP20X_FG_RES)); |
| 283 | seq_printf(s, " FG_RDC1[%02x] : %02x\n", |
| 284 | AXP288_FG_RDC1_REG, |
| 285 | fuel_gauge_reg_readb(info, AXP288_FG_RDC1_REG)); |
| 286 | seq_printf(s, " FG_RDC0[%02x] : %02x\n", |
| 287 | AXP288_FG_RDC0_REG, |
| 288 | fuel_gauge_reg_readb(info, AXP288_FG_RDC0_REG)); |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame^] | 289 | seq_printf(s, " FG_OCV[%02x] : %04x\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 290 | AXP288_FG_OCVH_REG, |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame^] | 291 | fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG)); |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 292 | seq_printf(s, " FG_DES_CAP[%02x] : %04x\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 293 | AXP288_FG_DES_CAP1_REG, |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 294 | fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG)); |
| 295 | seq_printf(s, " FG_CC_MTR[%02x] : %04x\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 296 | AXP288_FG_CC_MTR1_REG, |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 297 | fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG)); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 298 | seq_printf(s, " FG_OCV_CAP[%02x] : %02x\n", |
| 299 | AXP288_FG_OCV_CAP_REG, |
| 300 | fuel_gauge_reg_readb(info, AXP288_FG_OCV_CAP_REG)); |
| 301 | seq_printf(s, " FG_CC_CAP[%02x] : %02x\n", |
| 302 | AXP288_FG_CC_CAP_REG, |
| 303 | fuel_gauge_reg_readb(info, AXP288_FG_CC_CAP_REG)); |
| 304 | seq_printf(s, " FG_LOW_CAP[%02x] : %02x\n", |
| 305 | AXP288_FG_LOW_CAP_REG, |
| 306 | fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG)); |
| 307 | seq_printf(s, "TUNING_CTL0[%02x] : %02x\n", |
| 308 | AXP288_FG_TUNE0, |
| 309 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE0)); |
| 310 | seq_printf(s, "TUNING_CTL1[%02x] : %02x\n", |
| 311 | AXP288_FG_TUNE1, |
| 312 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE1)); |
| 313 | seq_printf(s, "TUNING_CTL2[%02x] : %02x\n", |
| 314 | AXP288_FG_TUNE2, |
| 315 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE2)); |
| 316 | seq_printf(s, "TUNING_CTL3[%02x] : %02x\n", |
| 317 | AXP288_FG_TUNE3, |
| 318 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE3)); |
| 319 | seq_printf(s, "TUNING_CTL4[%02x] : %02x\n", |
| 320 | AXP288_FG_TUNE4, |
| 321 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE4)); |
| 322 | seq_printf(s, "TUNING_CTL5[%02x] : %02x\n", |
| 323 | AXP288_FG_TUNE5, |
| 324 | fuel_gauge_reg_readb(info, AXP288_FG_TUNE5)); |
| 325 | |
| 326 | ret = pmic_read_adc_val("axp288-batt-temp", &raw_val, info); |
| 327 | if (ret >= 0) |
| 328 | seq_printf(s, "axp288-batttemp : %d\n", raw_val); |
| 329 | ret = pmic_read_adc_val("axp288-pmic-temp", &raw_val, info); |
| 330 | if (ret >= 0) |
| 331 | seq_printf(s, "axp288-pmictemp : %d\n", raw_val); |
| 332 | ret = pmic_read_adc_val("axp288-system-temp", &raw_val, info); |
| 333 | if (ret >= 0) |
| 334 | seq_printf(s, "axp288-systtemp : %d\n", raw_val); |
| 335 | ret = pmic_read_adc_val("axp288-chrg-curr", &raw_val, info); |
| 336 | if (ret >= 0) |
| 337 | seq_printf(s, "axp288-chrgcurr : %d\n", raw_val); |
| 338 | ret = pmic_read_adc_val("axp288-chrg-d-curr", &raw_val, info); |
| 339 | if (ret >= 0) |
| 340 | seq_printf(s, "axp288-dchrgcur : %d\n", raw_val); |
| 341 | ret = pmic_read_adc_val("axp288-batt-volt", &raw_val, info); |
| 342 | if (ret >= 0) |
| 343 | seq_printf(s, "axp288-battvolt : %d\n", raw_val); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int debug_open(struct inode *inode, struct file *file) |
| 349 | { |
| 350 | return single_open(file, fuel_gauge_debug_show, inode->i_private); |
| 351 | } |
| 352 | |
| 353 | static const struct file_operations fg_debug_fops = { |
| 354 | .open = debug_open, |
| 355 | .read = seq_read, |
| 356 | .llseek = seq_lseek, |
| 357 | .release = single_release, |
| 358 | }; |
| 359 | |
| 360 | static void fuel_gauge_create_debugfs(struct axp288_fg_info *info) |
| 361 | { |
| 362 | info->debug_file = debugfs_create_file("fuelgauge", 0666, NULL, |
| 363 | info, &fg_debug_fops); |
| 364 | } |
| 365 | |
| 366 | static void fuel_gauge_remove_debugfs(struct axp288_fg_info *info) |
| 367 | { |
| 368 | debugfs_remove(info->debug_file); |
| 369 | } |
| 370 | #else |
| 371 | static inline void fuel_gauge_create_debugfs(struct axp288_fg_info *info) |
| 372 | { |
| 373 | } |
| 374 | static inline void fuel_gauge_remove_debugfs(struct axp288_fg_info *info) |
| 375 | { |
| 376 | } |
| 377 | #endif |
| 378 | |
| 379 | static void fuel_gauge_get_status(struct axp288_fg_info *info) |
| 380 | { |
| 381 | int pwr_stat, ret; |
| 382 | int charge, discharge; |
| 383 | |
| 384 | pwr_stat = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS); |
| 385 | if (pwr_stat < 0) { |
| 386 | dev_err(&info->pdev->dev, |
| 387 | "PWR STAT read failed:%d\n", pwr_stat); |
| 388 | return; |
| 389 | } |
| 390 | ret = pmic_read_adc_val("axp288-chrg-curr", &charge, info); |
| 391 | if (ret < 0) { |
| 392 | dev_err(&info->pdev->dev, |
| 393 | "ADC charge current read failed:%d\n", ret); |
| 394 | return; |
| 395 | } |
| 396 | ret = pmic_read_adc_val("axp288-chrg-d-curr", &discharge, info); |
| 397 | if (ret < 0) { |
| 398 | dev_err(&info->pdev->dev, |
| 399 | "ADC discharge current read failed:%d\n", ret); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | if (charge > 0) |
| 404 | info->status = POWER_SUPPLY_STATUS_CHARGING; |
| 405 | else if (discharge > 0) |
| 406 | info->status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 407 | else { |
| 408 | if (pwr_stat & CHRG_STAT_BAT_PRESENT) |
| 409 | info->status = POWER_SUPPLY_STATUS_FULL; |
| 410 | else |
| 411 | info->status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | static int fuel_gauge_get_vbatt(struct axp288_fg_info *info, int *vbatt) |
| 416 | { |
| 417 | int ret = 0, raw_val; |
| 418 | |
| 419 | ret = pmic_read_adc_val("axp288-batt-volt", &raw_val, info); |
| 420 | if (ret < 0) |
| 421 | goto vbatt_read_fail; |
| 422 | |
| 423 | *vbatt = VOLTAGE_FROM_ADC(raw_val); |
| 424 | vbatt_read_fail: |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | static int fuel_gauge_get_current(struct axp288_fg_info *info, int *cur) |
| 429 | { |
| 430 | int ret, value = 0; |
| 431 | int charge, discharge; |
| 432 | |
| 433 | ret = pmic_read_adc_val("axp288-chrg-curr", &charge, info); |
| 434 | if (ret < 0) |
| 435 | goto current_read_fail; |
| 436 | ret = pmic_read_adc_val("axp288-chrg-d-curr", &discharge, info); |
| 437 | if (ret < 0) |
| 438 | goto current_read_fail; |
| 439 | |
| 440 | if (charge > 0) |
| 441 | value = charge; |
| 442 | else if (discharge > 0) |
| 443 | value = -1 * discharge; |
| 444 | |
| 445 | *cur = value; |
| 446 | current_read_fail: |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | static int temp_to_adc(struct axp288_fg_info *info, int tval) |
| 451 | { |
| 452 | int rntc = 0, i, ret, adc_val; |
| 453 | int rmin, rmax, tmin, tmax; |
| 454 | int tcsz = info->pdata->tcsz; |
| 455 | |
| 456 | /* get the Rntc resitance value for this temp */ |
| 457 | if (tval > info->pdata->thermistor_curve[0][1]) { |
| 458 | rntc = info->pdata->thermistor_curve[0][0]; |
| 459 | } else if (tval <= info->pdata->thermistor_curve[tcsz-1][1]) { |
| 460 | rntc = info->pdata->thermistor_curve[tcsz-1][0]; |
| 461 | } else { |
| 462 | for (i = 1; i < tcsz; i++) { |
| 463 | if (tval > info->pdata->thermistor_curve[i][1]) { |
| 464 | rmin = info->pdata->thermistor_curve[i-1][0]; |
| 465 | rmax = info->pdata->thermistor_curve[i][0]; |
| 466 | tmin = info->pdata->thermistor_curve[i-1][1]; |
| 467 | tmax = info->pdata->thermistor_curve[i][1]; |
| 468 | rntc = rmin + ((rmax - rmin) * |
| 469 | (tval - tmin) / (tmax - tmin)); |
| 470 | break; |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /* we need the current to calculate the proper adc voltage */ |
| 476 | ret = fuel_gauge_reg_readb(info, AXP20X_ADC_RATE); |
| 477 | if (ret < 0) { |
| 478 | dev_err(&info->pdev->dev, "%s:read err:%d\n", __func__, ret); |
| 479 | ret = 0x30; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * temperature is proportional to NTS thermistor resistance |
| 484 | * ADC_RATE[5-4] determines current, 00=20uA,01=40uA,10=60uA,11=80uA |
| 485 | * [12-bit ADC VAL] = R_NTC(Ω) * current / 800 |
| 486 | */ |
| 487 | adc_val = rntc * (20 + (20 * ((ret >> 4) & 0x3))) / 800; |
| 488 | |
| 489 | return adc_val; |
| 490 | } |
| 491 | |
| 492 | static int adc_to_temp(struct axp288_fg_info *info, int adc_val) |
| 493 | { |
| 494 | int ret, r, i, tval = 0; |
| 495 | int rmin, rmax, tmin, tmax; |
| 496 | int tcsz = info->pdata->tcsz; |
| 497 | |
| 498 | ret = fuel_gauge_reg_readb(info, AXP20X_ADC_RATE); |
| 499 | if (ret < 0) { |
| 500 | dev_err(&info->pdev->dev, "%s:read err:%d\n", __func__, ret); |
| 501 | ret = 0x30; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * temperature is proportional to NTS thermistor resistance |
| 506 | * ADC_RATE[5-4] determines current, 00=20uA,01=40uA,10=60uA,11=80uA |
| 507 | * R_NTC(Ω) = [12-bit ADC VAL] * 800 / current |
| 508 | */ |
| 509 | r = adc_val * 800 / (20 + (20 * ((ret >> 4) & 0x3))); |
| 510 | |
| 511 | if (r < info->pdata->thermistor_curve[0][0]) { |
| 512 | tval = info->pdata->thermistor_curve[0][1]; |
| 513 | } else if (r >= info->pdata->thermistor_curve[tcsz-1][0]) { |
| 514 | tval = info->pdata->thermistor_curve[tcsz-1][1]; |
| 515 | } else { |
| 516 | for (i = 1; i < tcsz; i++) { |
| 517 | if (r < info->pdata->thermistor_curve[i][0]) { |
| 518 | rmin = info->pdata->thermistor_curve[i-1][0]; |
| 519 | rmax = info->pdata->thermistor_curve[i][0]; |
| 520 | tmin = info->pdata->thermistor_curve[i-1][1]; |
| 521 | tmax = info->pdata->thermistor_curve[i][1]; |
| 522 | tval = tmin + ((tmax - tmin) * |
| 523 | (r - rmin) / (rmax - rmin)); |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | return tval; |
| 530 | } |
| 531 | |
| 532 | static int fuel_gauge_get_btemp(struct axp288_fg_info *info, int *btemp) |
| 533 | { |
| 534 | int ret, raw_val = 0; |
| 535 | |
| 536 | ret = pmic_read_adc_val("axp288-batt-temp", &raw_val, info); |
| 537 | if (ret < 0) |
| 538 | goto temp_read_fail; |
| 539 | |
| 540 | *btemp = adc_to_temp(info, raw_val); |
| 541 | |
| 542 | temp_read_fail: |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | static int fuel_gauge_get_vocv(struct axp288_fg_info *info, int *vocv) |
| 547 | { |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame^] | 548 | int ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 549 | |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame^] | 550 | ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG); |
| 551 | if (ret >= 0) |
| 552 | *vocv = VOLTAGE_FROM_ADC(ret); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 553 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | static int fuel_gauge_battery_health(struct axp288_fg_info *info) |
| 558 | { |
| 559 | int temp, vocv; |
| 560 | int ret, health = POWER_SUPPLY_HEALTH_UNKNOWN; |
| 561 | |
| 562 | ret = fuel_gauge_get_btemp(info, &temp); |
| 563 | if (ret < 0) |
| 564 | goto health_read_fail; |
| 565 | |
| 566 | ret = fuel_gauge_get_vocv(info, &vocv); |
| 567 | if (ret < 0) |
| 568 | goto health_read_fail; |
| 569 | |
| 570 | if (vocv > info->pdata->max_volt) |
| 571 | health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 572 | else if (temp > info->pdata->max_temp) |
| 573 | health = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 574 | else if (temp < info->pdata->min_temp) |
| 575 | health = POWER_SUPPLY_HEALTH_COLD; |
| 576 | else if (vocv < info->pdata->min_volt) |
| 577 | health = POWER_SUPPLY_HEALTH_DEAD; |
| 578 | else |
| 579 | health = POWER_SUPPLY_HEALTH_GOOD; |
| 580 | |
| 581 | health_read_fail: |
| 582 | return health; |
| 583 | } |
| 584 | |
| 585 | static int fuel_gauge_set_high_btemp_alert(struct axp288_fg_info *info) |
| 586 | { |
| 587 | int ret, adc_val; |
| 588 | |
| 589 | /* program temperature threshold as 1/16 ADC value */ |
| 590 | adc_val = temp_to_adc(info, info->pdata->max_temp); |
| 591 | ret = fuel_gauge_reg_writeb(info, AXP20X_V_HTF_DISCHRG, adc_val >> 4); |
| 592 | |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | static int fuel_gauge_set_low_btemp_alert(struct axp288_fg_info *info) |
| 597 | { |
| 598 | int ret, adc_val; |
| 599 | |
| 600 | /* program temperature threshold as 1/16 ADC value */ |
| 601 | adc_val = temp_to_adc(info, info->pdata->min_temp); |
| 602 | ret = fuel_gauge_reg_writeb(info, AXP20X_V_LTF_DISCHRG, adc_val >> 4); |
| 603 | |
| 604 | return ret; |
| 605 | } |
| 606 | |
| 607 | static int fuel_gauge_get_property(struct power_supply *ps, |
| 608 | enum power_supply_property prop, |
| 609 | union power_supply_propval *val) |
| 610 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 611 | struct axp288_fg_info *info = power_supply_get_drvdata(ps); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 612 | int ret = 0, value; |
| 613 | |
| 614 | mutex_lock(&info->lock); |
| 615 | switch (prop) { |
| 616 | case POWER_SUPPLY_PROP_STATUS: |
| 617 | fuel_gauge_get_status(info); |
| 618 | val->intval = info->status; |
| 619 | break; |
| 620 | case POWER_SUPPLY_PROP_HEALTH: |
| 621 | val->intval = fuel_gauge_battery_health(info); |
| 622 | break; |
| 623 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 624 | ret = fuel_gauge_get_vbatt(info, &value); |
| 625 | if (ret < 0) |
| 626 | goto fuel_gauge_read_err; |
| 627 | val->intval = PROP_VOLT(value); |
| 628 | break; |
| 629 | case POWER_SUPPLY_PROP_VOLTAGE_OCV: |
| 630 | ret = fuel_gauge_get_vocv(info, &value); |
| 631 | if (ret < 0) |
| 632 | goto fuel_gauge_read_err; |
| 633 | val->intval = PROP_VOLT(value); |
| 634 | break; |
| 635 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 636 | ret = fuel_gauge_get_current(info, &value); |
| 637 | if (ret < 0) |
| 638 | goto fuel_gauge_read_err; |
| 639 | val->intval = PROP_CURR(value); |
| 640 | break; |
| 641 | case POWER_SUPPLY_PROP_PRESENT: |
| 642 | ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE); |
| 643 | if (ret < 0) |
| 644 | goto fuel_gauge_read_err; |
| 645 | |
| 646 | if (ret & CHRG_STAT_BAT_PRESENT) |
| 647 | val->intval = 1; |
| 648 | else |
| 649 | val->intval = 0; |
| 650 | break; |
| 651 | case POWER_SUPPLY_PROP_CAPACITY: |
| 652 | ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES); |
| 653 | if (ret < 0) |
| 654 | goto fuel_gauge_read_err; |
| 655 | |
| 656 | if (!(ret & FG_REP_CAP_VALID)) |
| 657 | dev_err(&info->pdev->dev, |
| 658 | "capacity measurement not valid\n"); |
| 659 | val->intval = (ret & FG_REP_CAP_VAL_MASK); |
| 660 | break; |
| 661 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 662 | ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); |
| 663 | if (ret < 0) |
| 664 | goto fuel_gauge_read_err; |
| 665 | val->intval = (ret & 0x0f); |
| 666 | break; |
| 667 | case POWER_SUPPLY_PROP_TEMP: |
| 668 | ret = fuel_gauge_get_btemp(info, &value); |
| 669 | if (ret < 0) |
| 670 | goto fuel_gauge_read_err; |
| 671 | val->intval = PROP_TEMP(value); |
| 672 | break; |
| 673 | case POWER_SUPPLY_PROP_TEMP_MAX: |
| 674 | case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
| 675 | val->intval = PROP_TEMP(info->pdata->max_temp); |
| 676 | break; |
| 677 | case POWER_SUPPLY_PROP_TEMP_MIN: |
| 678 | case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
| 679 | val->intval = PROP_TEMP(info->pdata->min_temp); |
| 680 | break; |
| 681 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 682 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 683 | break; |
| 684 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 685 | ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 686 | if (ret < 0) |
| 687 | goto fuel_gauge_read_err; |
| 688 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 689 | val->intval = ret * FG_DES_CAP_RES_LSB; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 690 | break; |
| 691 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 692 | ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 693 | if (ret < 0) |
| 694 | goto fuel_gauge_read_err; |
| 695 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 696 | val->intval = ret * FG_DES_CAP_RES_LSB; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 697 | break; |
| 698 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 699 | val->intval = PROP_CURR(info->pdata->design_cap); |
| 700 | break; |
| 701 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 702 | val->intval = PROP_VOLT(info->pdata->max_volt); |
| 703 | break; |
| 704 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
| 705 | val->intval = PROP_VOLT(info->pdata->min_volt); |
| 706 | break; |
| 707 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 708 | val->strval = info->pdata->battid; |
| 709 | break; |
| 710 | default: |
| 711 | mutex_unlock(&info->lock); |
| 712 | return -EINVAL; |
| 713 | } |
| 714 | |
| 715 | mutex_unlock(&info->lock); |
| 716 | return 0; |
| 717 | |
| 718 | fuel_gauge_read_err: |
| 719 | mutex_unlock(&info->lock); |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | static int fuel_gauge_set_property(struct power_supply *ps, |
| 724 | enum power_supply_property prop, |
| 725 | const union power_supply_propval *val) |
| 726 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 727 | struct axp288_fg_info *info = power_supply_get_drvdata(ps); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 728 | int ret = 0; |
| 729 | |
| 730 | mutex_lock(&info->lock); |
| 731 | switch (prop) { |
| 732 | case POWER_SUPPLY_PROP_STATUS: |
| 733 | info->status = val->intval; |
| 734 | break; |
| 735 | case POWER_SUPPLY_PROP_TEMP_MIN: |
| 736 | case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
| 737 | if ((val->intval < PD_DEF_MIN_TEMP) || |
| 738 | (val->intval > PD_DEF_MAX_TEMP)) { |
| 739 | ret = -EINVAL; |
| 740 | break; |
| 741 | } |
| 742 | info->pdata->min_temp = UNPROP_TEMP(val->intval); |
| 743 | ret = fuel_gauge_set_low_btemp_alert(info); |
| 744 | if (ret < 0) |
| 745 | dev_err(&info->pdev->dev, |
| 746 | "temp alert min set fail:%d\n", ret); |
| 747 | break; |
| 748 | case POWER_SUPPLY_PROP_TEMP_MAX: |
| 749 | case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
| 750 | if ((val->intval < PD_DEF_MIN_TEMP) || |
| 751 | (val->intval > PD_DEF_MAX_TEMP)) { |
| 752 | ret = -EINVAL; |
| 753 | break; |
| 754 | } |
| 755 | info->pdata->max_temp = UNPROP_TEMP(val->intval); |
| 756 | ret = fuel_gauge_set_high_btemp_alert(info); |
| 757 | if (ret < 0) |
| 758 | dev_err(&info->pdev->dev, |
| 759 | "temp alert max set fail:%d\n", ret); |
| 760 | break; |
| 761 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 762 | if ((val->intval < 0) || (val->intval > 15)) { |
| 763 | ret = -EINVAL; |
| 764 | break; |
| 765 | } |
| 766 | ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); |
| 767 | if (ret < 0) |
| 768 | break; |
| 769 | ret &= 0xf0; |
| 770 | ret |= (val->intval & 0xf); |
| 771 | ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, ret); |
| 772 | break; |
| 773 | default: |
| 774 | ret = -EINVAL; |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | mutex_unlock(&info->lock); |
| 779 | return ret; |
| 780 | } |
| 781 | |
| 782 | static int fuel_gauge_property_is_writeable(struct power_supply *psy, |
| 783 | enum power_supply_property psp) |
| 784 | { |
| 785 | int ret; |
| 786 | |
| 787 | switch (psp) { |
| 788 | case POWER_SUPPLY_PROP_STATUS: |
| 789 | case POWER_SUPPLY_PROP_TEMP_MIN: |
| 790 | case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
| 791 | case POWER_SUPPLY_PROP_TEMP_MAX: |
| 792 | case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
| 793 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 794 | ret = 1; |
| 795 | break; |
| 796 | default: |
| 797 | ret = 0; |
| 798 | } |
| 799 | |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | static void fuel_gauge_status_monitor(struct work_struct *work) |
| 804 | { |
| 805 | struct axp288_fg_info *info = container_of(work, |
| 806 | struct axp288_fg_info, status_monitor.work); |
| 807 | |
| 808 | fuel_gauge_get_status(info); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 809 | power_supply_changed(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 810 | schedule_delayed_work(&info->status_monitor, STATUS_MON_DELAY_JIFFIES); |
| 811 | } |
| 812 | |
| 813 | static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev) |
| 814 | { |
| 815 | struct axp288_fg_info *info = dev; |
| 816 | int i; |
| 817 | |
| 818 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) { |
| 819 | if (info->irq[i] == irq) |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | if (i >= AXP288_FG_INTR_NUM) { |
| 824 | dev_warn(&info->pdev->dev, "spurious interrupt!!\n"); |
| 825 | return IRQ_NONE; |
| 826 | } |
| 827 | |
| 828 | switch (i) { |
| 829 | case QWBTU_IRQ: |
| 830 | dev_info(&info->pdev->dev, |
| 831 | "Quit Battery under temperature in work mode IRQ (QWBTU)\n"); |
| 832 | break; |
| 833 | case WBTU_IRQ: |
| 834 | dev_info(&info->pdev->dev, |
| 835 | "Battery under temperature in work mode IRQ (WBTU)\n"); |
| 836 | break; |
| 837 | case QWBTO_IRQ: |
| 838 | dev_info(&info->pdev->dev, |
| 839 | "Quit Battery over temperature in work mode IRQ (QWBTO)\n"); |
| 840 | break; |
| 841 | case WBTO_IRQ: |
| 842 | dev_info(&info->pdev->dev, |
| 843 | "Battery over temperature in work mode IRQ (WBTO)\n"); |
| 844 | break; |
| 845 | case WL2_IRQ: |
| 846 | dev_info(&info->pdev->dev, "Low Batt Warning(2) INTR\n"); |
| 847 | break; |
| 848 | case WL1_IRQ: |
| 849 | dev_info(&info->pdev->dev, "Low Batt Warning(1) INTR\n"); |
| 850 | break; |
| 851 | default: |
| 852 | dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n"); |
| 853 | } |
| 854 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 855 | power_supply_changed(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 856 | return IRQ_HANDLED; |
| 857 | } |
| 858 | |
| 859 | static void fuel_gauge_external_power_changed(struct power_supply *psy) |
| 860 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 861 | struct axp288_fg_info *info = power_supply_get_drvdata(psy); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 862 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 863 | power_supply_changed(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 864 | } |
| 865 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 866 | static const struct power_supply_desc fuel_gauge_desc = { |
| 867 | .name = DEV_NAME, |
| 868 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 869 | .properties = fuel_gauge_props, |
| 870 | .num_properties = ARRAY_SIZE(fuel_gauge_props), |
| 871 | .get_property = fuel_gauge_get_property, |
| 872 | .set_property = fuel_gauge_set_property, |
| 873 | .property_is_writeable = fuel_gauge_property_is_writeable, |
| 874 | .external_power_changed = fuel_gauge_external_power_changed, |
| 875 | }; |
| 876 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 877 | static int fuel_gauge_set_lowbatt_thresholds(struct axp288_fg_info *info) |
| 878 | { |
| 879 | int ret; |
| 880 | u8 reg_val; |
| 881 | |
| 882 | ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES); |
| 883 | if (ret < 0) { |
| 884 | dev_err(&info->pdev->dev, "%s:read err:%d\n", __func__, ret); |
| 885 | return ret; |
| 886 | } |
| 887 | ret = (ret & FG_REP_CAP_VAL_MASK); |
| 888 | |
| 889 | if (ret > FG_LOW_CAP_WARN_THR) |
| 890 | reg_val = FG_LOW_CAP_WARN_THR; |
| 891 | else if (ret > FG_LOW_CAP_CRIT_THR) |
| 892 | reg_val = FG_LOW_CAP_CRIT_THR; |
| 893 | else |
| 894 | reg_val = FG_LOW_CAP_SHDN_THR; |
| 895 | |
| 896 | reg_val |= FG_LOW_CAP_THR1_VAL; |
| 897 | ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, reg_val); |
| 898 | if (ret < 0) |
| 899 | dev_err(&info->pdev->dev, "%s:write err:%d\n", __func__, ret); |
| 900 | |
| 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | static int fuel_gauge_program_vbatt_full(struct axp288_fg_info *info) |
| 905 | { |
| 906 | int ret; |
| 907 | u8 val; |
| 908 | |
| 909 | ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1); |
| 910 | if (ret < 0) |
| 911 | goto fg_prog_ocv_fail; |
| 912 | else |
| 913 | val = (ret & ~CHRG_CCCV_CV_MASK); |
| 914 | |
| 915 | switch (info->pdata->max_volt) { |
| 916 | case CV_4100: |
| 917 | val |= (CHRG_CCCV_CV_4100MV << CHRG_CCCV_CV_BIT_POS); |
| 918 | break; |
| 919 | case CV_4150: |
| 920 | val |= (CHRG_CCCV_CV_4150MV << CHRG_CCCV_CV_BIT_POS); |
| 921 | break; |
| 922 | case CV_4200: |
| 923 | val |= (CHRG_CCCV_CV_4200MV << CHRG_CCCV_CV_BIT_POS); |
| 924 | break; |
| 925 | case CV_4350: |
| 926 | val |= (CHRG_CCCV_CV_4350MV << CHRG_CCCV_CV_BIT_POS); |
| 927 | break; |
| 928 | default: |
| 929 | val |= (CHRG_CCCV_CV_4200MV << CHRG_CCCV_CV_BIT_POS); |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | ret = fuel_gauge_reg_writeb(info, AXP20X_CHRG_CTRL1, val); |
| 934 | fg_prog_ocv_fail: |
| 935 | return ret; |
| 936 | } |
| 937 | |
| 938 | static int fuel_gauge_program_design_cap(struct axp288_fg_info *info) |
| 939 | { |
| 940 | int ret; |
| 941 | |
| 942 | ret = fuel_gauge_reg_writeb(info, |
| 943 | AXP288_FG_DES_CAP1_REG, info->pdata->cap1); |
| 944 | if (ret < 0) |
| 945 | goto fg_prog_descap_fail; |
| 946 | |
| 947 | ret = fuel_gauge_reg_writeb(info, |
| 948 | AXP288_FG_DES_CAP0_REG, info->pdata->cap0); |
| 949 | |
| 950 | fg_prog_descap_fail: |
| 951 | return ret; |
| 952 | } |
| 953 | |
| 954 | static int fuel_gauge_program_ocv_curve(struct axp288_fg_info *info) |
| 955 | { |
| 956 | int ret = 0, i; |
| 957 | |
| 958 | for (i = 0; i < OCV_CURVE_SIZE; i++) { |
| 959 | ret = fuel_gauge_reg_writeb(info, |
| 960 | AXP288_FG_OCV_CURVE_REG + i, info->pdata->ocv_curve[i]); |
| 961 | if (ret < 0) |
| 962 | goto fg_prog_ocv_fail; |
| 963 | } |
| 964 | |
| 965 | fg_prog_ocv_fail: |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | static int fuel_gauge_program_rdc_vals(struct axp288_fg_info *info) |
| 970 | { |
| 971 | int ret; |
| 972 | |
| 973 | ret = fuel_gauge_reg_writeb(info, |
| 974 | AXP288_FG_RDC1_REG, info->pdata->rdc1); |
| 975 | if (ret < 0) |
| 976 | goto fg_prog_ocv_fail; |
| 977 | |
| 978 | ret = fuel_gauge_reg_writeb(info, |
| 979 | AXP288_FG_RDC0_REG, info->pdata->rdc0); |
| 980 | |
| 981 | fg_prog_ocv_fail: |
| 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | static void fuel_gauge_init_config_regs(struct axp288_fg_info *info) |
| 986 | { |
| 987 | int ret; |
| 988 | |
| 989 | /* |
| 990 | * check if the config data is already |
| 991 | * programmed and if so just return. |
| 992 | */ |
| 993 | |
| 994 | ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG); |
| 995 | if (ret < 0) { |
| 996 | dev_warn(&info->pdev->dev, "CAP1 reg read err!!\n"); |
| 997 | } else if (!(ret & FG_DES_CAP1_VALID)) { |
| 998 | dev_info(&info->pdev->dev, "FG data needs to be initialized\n"); |
| 999 | } else { |
| 1000 | dev_info(&info->pdev->dev, "FG data is already initialized\n"); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | ret = fuel_gauge_program_vbatt_full(info); |
| 1005 | if (ret < 0) |
| 1006 | dev_err(&info->pdev->dev, "set vbatt full fail:%d\n", ret); |
| 1007 | |
| 1008 | ret = fuel_gauge_program_design_cap(info); |
| 1009 | if (ret < 0) |
| 1010 | dev_err(&info->pdev->dev, "set design cap fail:%d\n", ret); |
| 1011 | |
| 1012 | ret = fuel_gauge_program_rdc_vals(info); |
| 1013 | if (ret < 0) |
| 1014 | dev_err(&info->pdev->dev, "set rdc fail:%d\n", ret); |
| 1015 | |
| 1016 | ret = fuel_gauge_program_ocv_curve(info); |
| 1017 | if (ret < 0) |
| 1018 | dev_err(&info->pdev->dev, "set ocv curve fail:%d\n", ret); |
| 1019 | |
| 1020 | ret = fuel_gauge_set_lowbatt_thresholds(info); |
| 1021 | if (ret < 0) |
| 1022 | dev_err(&info->pdev->dev, "lowbatt thr set fail:%d\n", ret); |
| 1023 | |
| 1024 | ret = fuel_gauge_reg_writeb(info, AXP20X_CC_CTRL, 0xef); |
| 1025 | if (ret < 0) |
| 1026 | dev_err(&info->pdev->dev, "gauge cntl set fail:%d\n", ret); |
| 1027 | } |
| 1028 | |
| 1029 | static void fuel_gauge_init_irq(struct axp288_fg_info *info) |
| 1030 | { |
| 1031 | int ret, i, pirq; |
| 1032 | |
| 1033 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) { |
| 1034 | pirq = platform_get_irq(info->pdev, i); |
| 1035 | info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq); |
| 1036 | if (info->irq[i] < 0) { |
| 1037 | dev_warn(&info->pdev->dev, |
| 1038 | "regmap_irq get virq failed for IRQ %d: %d\n", |
| 1039 | pirq, info->irq[i]); |
| 1040 | info->irq[i] = -1; |
| 1041 | goto intr_failed; |
| 1042 | } |
| 1043 | ret = request_threaded_irq(info->irq[i], |
| 1044 | NULL, fuel_gauge_thread_handler, |
| 1045 | IRQF_ONESHOT, DEV_NAME, info); |
| 1046 | if (ret) { |
| 1047 | dev_warn(&info->pdev->dev, |
| 1048 | "request irq failed for IRQ %d: %d\n", |
| 1049 | pirq, info->irq[i]); |
| 1050 | info->irq[i] = -1; |
| 1051 | goto intr_failed; |
| 1052 | } else { |
| 1053 | dev_info(&info->pdev->dev, "HW IRQ %d -> VIRQ %d\n", |
| 1054 | pirq, info->irq[i]); |
| 1055 | } |
| 1056 | } |
| 1057 | return; |
| 1058 | |
| 1059 | intr_failed: |
| 1060 | for (; i > 0; i--) { |
| 1061 | free_irq(info->irq[i - 1], info); |
| 1062 | info->irq[i - 1] = -1; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | static void fuel_gauge_init_hw_regs(struct axp288_fg_info *info) |
| 1067 | { |
| 1068 | int ret; |
| 1069 | unsigned int val; |
| 1070 | |
| 1071 | ret = fuel_gauge_set_high_btemp_alert(info); |
| 1072 | if (ret < 0) |
| 1073 | dev_err(&info->pdev->dev, "high batt temp set fail:%d\n", ret); |
| 1074 | |
| 1075 | ret = fuel_gauge_set_low_btemp_alert(info); |
| 1076 | if (ret < 0) |
| 1077 | dev_err(&info->pdev->dev, "low batt temp set fail:%d\n", ret); |
| 1078 | |
| 1079 | /* enable interrupts */ |
| 1080 | val = fuel_gauge_reg_readb(info, AXP20X_IRQ3_EN); |
| 1081 | val |= TEMP_IRQ_CFG_MASK; |
| 1082 | fuel_gauge_reg_writeb(info, AXP20X_IRQ3_EN, val); |
| 1083 | |
| 1084 | val = fuel_gauge_reg_readb(info, AXP20X_IRQ4_EN); |
| 1085 | val |= FG_IRQ_CFG_LOWBATT_MASK; |
| 1086 | val = fuel_gauge_reg_writeb(info, AXP20X_IRQ4_EN, val); |
| 1087 | } |
| 1088 | |
| 1089 | static int axp288_fuel_gauge_probe(struct platform_device *pdev) |
| 1090 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1091 | int ret = 0; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 1092 | struct axp288_fg_info *info; |
| 1093 | struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1094 | struct power_supply_config psy_cfg = {}; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 1095 | |
| 1096 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 1097 | if (!info) |
| 1098 | return -ENOMEM; |
| 1099 | |
| 1100 | info->pdev = pdev; |
| 1101 | info->regmap = axp20x->regmap; |
| 1102 | info->regmap_irqc = axp20x->regmap_irqc; |
| 1103 | info->status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 1104 | info->pdata = pdev->dev.platform_data; |
| 1105 | if (!info->pdata) |
| 1106 | return -ENODEV; |
| 1107 | |
| 1108 | platform_set_drvdata(pdev, info); |
| 1109 | |
| 1110 | mutex_init(&info->lock); |
| 1111 | INIT_DELAYED_WORK(&info->status_monitor, fuel_gauge_status_monitor); |
| 1112 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1113 | psy_cfg.drv_data = info; |
| 1114 | info->bat = power_supply_register(&pdev->dev, &fuel_gauge_desc, &psy_cfg); |
| 1115 | if (IS_ERR(info->bat)) { |
| 1116 | ret = PTR_ERR(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 1117 | dev_err(&pdev->dev, "failed to register battery: %d\n", ret); |
| 1118 | return ret; |
| 1119 | } |
| 1120 | |
| 1121 | fuel_gauge_create_debugfs(info); |
| 1122 | fuel_gauge_init_config_regs(info); |
| 1123 | fuel_gauge_init_irq(info); |
| 1124 | fuel_gauge_init_hw_regs(info); |
| 1125 | schedule_delayed_work(&info->status_monitor, STATUS_MON_DELAY_JIFFIES); |
| 1126 | |
| 1127 | return ret; |
| 1128 | } |
| 1129 | |
Krzysztof Kozlowski | f1f27a4 | 2015-05-02 00:46:43 +0900 | [diff] [blame] | 1130 | static const struct platform_device_id axp288_fg_id_table[] = { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 1131 | { .name = DEV_NAME }, |
| 1132 | {}, |
| 1133 | }; |
Javier Martinez Canillas | 99e33fb | 2016-10-17 16:13:36 -0300 | [diff] [blame] | 1134 | MODULE_DEVICE_TABLE(platform, axp288_fg_id_table); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 1135 | |
| 1136 | static int axp288_fuel_gauge_remove(struct platform_device *pdev) |
| 1137 | { |
| 1138 | struct axp288_fg_info *info = platform_get_drvdata(pdev); |
| 1139 | int i; |
| 1140 | |
| 1141 | cancel_delayed_work_sync(&info->status_monitor); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1142 | power_supply_unregister(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 1143 | fuel_gauge_remove_debugfs(info); |
| 1144 | |
| 1145 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) |
| 1146 | if (info->irq[i] >= 0) |
| 1147 | free_irq(info->irq[i], info); |
| 1148 | |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | static struct platform_driver axp288_fuel_gauge_driver = { |
| 1153 | .probe = axp288_fuel_gauge_probe, |
| 1154 | .remove = axp288_fuel_gauge_remove, |
| 1155 | .id_table = axp288_fg_id_table, |
| 1156 | .driver = { |
| 1157 | .name = DEV_NAME, |
| 1158 | }, |
| 1159 | }; |
| 1160 | |
| 1161 | module_platform_driver(axp288_fuel_gauge_driver); |
| 1162 | |
Ramakrishna Pallala | 409e718 | 2015-03-13 21:49:09 +0530 | [diff] [blame] | 1163 | MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 1164 | MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>"); |
| 1165 | MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver"); |
| 1166 | MODULE_LICENSE("GPL"); |