Thomas Gleixner | 8e8e69d | 2019-05-29 07:17:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 2 | /* |
| 3 | * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver |
| 4 | * |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 5 | * Copyright (C) 2020-2021 Andrejus Basovas <xxx@yyy.tld> |
| 6 | * Copyright (C) 2016-2021 Hans de Goede <hdegoede@redhat.com> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 7 | * Copyright (C) 2014 Intel Corporation |
| 8 | * |
| 9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 12 | #include <linux/dmi.h> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/regmap.h> |
| 17 | #include <linux/jiffies.h> |
| 18 | #include <linux/interrupt.h> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 19 | #include <linux/mfd/axp20x.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/power_supply.h> |
| 22 | #include <linux/iio/consumer.h> |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 23 | #include <asm/unaligned.h> |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 24 | #include <asm/iosf_mbi.h> |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 25 | |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 26 | #define PS_STAT_VBUS_TRIGGER (1 << 0) |
| 27 | #define PS_STAT_BAT_CHRG_DIR (1 << 2) |
| 28 | #define PS_STAT_VBAT_ABOVE_VHOLD (1 << 3) |
| 29 | #define PS_STAT_VBUS_VALID (1 << 4) |
| 30 | #define PS_STAT_VBUS_PRESENT (1 << 5) |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 31 | |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 32 | #define CHRG_STAT_BAT_SAFE_MODE (1 << 3) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 33 | #define CHRG_STAT_BAT_VALID (1 << 4) |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 34 | #define CHRG_STAT_BAT_PRESENT (1 << 5) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 35 | #define CHRG_STAT_CHARGING (1 << 6) |
| 36 | #define CHRG_STAT_PMIC_OTP (1 << 7) |
| 37 | |
| 38 | #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */ |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 39 | #define CHRG_CCCV_CC_BIT_POS 0 |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 40 | #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */ |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 41 | #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */ |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 42 | #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */ |
| 43 | #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */ |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 44 | #define CHRG_CCCV_CV_BIT_POS 5 |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 45 | #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */ |
| 46 | #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */ |
| 47 | #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */ |
| 48 | #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */ |
| 49 | #define CHRG_CCCV_CHG_EN (1 << 7) |
| 50 | |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 51 | #define FG_CNTL_OCV_ADJ_STAT (1 << 2) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 52 | #define FG_CNTL_OCV_ADJ_EN (1 << 3) |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 53 | #define FG_CNTL_CAP_ADJ_STAT (1 << 4) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 54 | #define FG_CNTL_CAP_ADJ_EN (1 << 5) |
| 55 | #define FG_CNTL_CC_EN (1 << 6) |
| 56 | #define FG_CNTL_GAUGE_EN (1 << 7) |
| 57 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 58 | #define FG_15BIT_WORD_VALID (1 << 15) |
| 59 | #define FG_15BIT_VAL_MASK 0x7fff |
| 60 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 61 | #define FG_REP_CAP_VALID (1 << 7) |
| 62 | #define FG_REP_CAP_VAL_MASK 0x7F |
| 63 | |
| 64 | #define FG_DES_CAP1_VALID (1 << 7) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 65 | #define FG_DES_CAP_RES_LSB 1456 /* 1.456mAhr */ |
| 66 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 67 | #define FG_DES_CC_RES_LSB 1456 /* 1.456mAhr */ |
| 68 | |
| 69 | #define FG_OCV_CAP_VALID (1 << 7) |
| 70 | #define FG_OCV_CAP_VAL_MASK 0x7F |
| 71 | #define FG_CC_CAP_VALID (1 << 7) |
| 72 | #define FG_CC_CAP_VAL_MASK 0x7F |
| 73 | |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 74 | #define FG_LOW_CAP_THR1_MASK 0xf0 /* 5% tp 20% */ |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 75 | #define FG_LOW_CAP_THR1_VAL 0xa0 /* 15 perc */ |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 76 | #define FG_LOW_CAP_THR2_MASK 0x0f /* 0% to 15% */ |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 77 | #define FG_LOW_CAP_WARN_THR 14 /* 14 perc */ |
| 78 | #define FG_LOW_CAP_CRIT_THR 4 /* 4 perc */ |
| 79 | #define FG_LOW_CAP_SHDN_THR 0 /* 0 perc */ |
| 80 | |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 81 | #define DEV_NAME "axp288_fuel_gauge" |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 82 | |
| 83 | /* 1.1mV per LSB expressed in uV */ |
| 84 | #define VOLTAGE_FROM_ADC(a) ((a * 11) / 10) |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 85 | /* properties converted to uV, uA */ |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 86 | #define PROP_VOLT(a) ((a) * 1000) |
| 87 | #define PROP_CURR(a) ((a) * 1000) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 88 | |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 89 | #define AXP288_REG_UPDATE_INTERVAL (60 * HZ) |
Hans de Goede | f9ac973 | 2021-08-01 15:30:56 +0200 | [diff] [blame] | 90 | #define AXP288_FG_INTR_NUM 6 |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 91 | enum { |
| 92 | QWBTU_IRQ = 0, |
| 93 | WBTU_IRQ, |
| 94 | QWBTO_IRQ, |
| 95 | WBTO_IRQ, |
| 96 | WL2_IRQ, |
| 97 | WL1_IRQ, |
| 98 | }; |
| 99 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 100 | enum { |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 101 | BAT_CHRG_CURR, |
| 102 | BAT_D_CURR, |
| 103 | BAT_VOLT, |
| 104 | IIO_CHANNEL_NUM |
| 105 | }; |
| 106 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 107 | struct axp288_fg_info { |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 108 | struct device *dev; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 109 | struct regmap *regmap; |
| 110 | struct regmap_irq_chip_data *regmap_irqc; |
| 111 | int irq[AXP288_FG_INTR_NUM]; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 112 | struct iio_channel *iio_channel[IIO_CHANNEL_NUM]; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 113 | struct power_supply *bat; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 114 | struct mutex lock; |
| 115 | int status; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 116 | int max_volt; |
Hans de Goede | c371d44 | 2021-08-01 15:31:02 +0200 | [diff] [blame] | 117 | int pwr_op; |
| 118 | int low_cap; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 119 | struct dentry *debug_file; |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 120 | |
| 121 | char valid; /* zero until following fields are valid */ |
| 122 | unsigned long last_updated; /* in jiffies */ |
| 123 | |
| 124 | int pwr_stat; |
| 125 | int fg_res; |
| 126 | int bat_volt; |
| 127 | int d_curr; |
| 128 | int c_curr; |
| 129 | int ocv; |
| 130 | int fg_cc_mtr1; |
| 131 | int fg_des_cap1; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | static enum power_supply_property fuel_gauge_props[] = { |
| 135 | POWER_SUPPLY_PROP_STATUS, |
| 136 | POWER_SUPPLY_PROP_PRESENT, |
| 137 | POWER_SUPPLY_PROP_HEALTH, |
| 138 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 139 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 140 | POWER_SUPPLY_PROP_VOLTAGE_OCV, |
| 141 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 142 | POWER_SUPPLY_PROP_CAPACITY, |
| 143 | POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 144 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 145 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 146 | POWER_SUPPLY_PROP_CHARGE_NOW, |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg) |
| 150 | { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 151 | unsigned int val; |
Hans de Goede | f17bda7 | 2021-08-01 15:31:00 +0200 | [diff] [blame] | 152 | int ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 153 | |
Hans de Goede | f17bda7 | 2021-08-01 15:31:00 +0200 | [diff] [blame] | 154 | ret = regmap_read(info->regmap, reg, &val); |
Hans de Goede | 6f074bc | 2016-12-14 17:38:50 +0100 | [diff] [blame] | 155 | if (ret < 0) { |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 156 | dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret); |
Hans de Goede | 6f074bc | 2016-12-14 17:38:50 +0100 | [diff] [blame] | 157 | return ret; |
| 158 | } |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 159 | |
| 160 | return val; |
| 161 | } |
| 162 | |
| 163 | static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val) |
| 164 | { |
| 165 | int ret; |
| 166 | |
| 167 | ret = regmap_write(info->regmap, reg, (unsigned int)val); |
| 168 | |
| 169 | if (ret < 0) |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 170 | dev_err(info->dev, "Error writing reg 0x%02x err: %d\n", reg, ret); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 175 | static int fuel_gauge_read_15bit_word(struct axp288_fg_info *info, int reg) |
| 176 | { |
| 177 | unsigned char buf[2]; |
| 178 | int ret; |
| 179 | |
| 180 | ret = regmap_bulk_read(info->regmap, reg, buf, 2); |
| 181 | if (ret < 0) { |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 182 | dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret); |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | ret = get_unaligned_be16(buf); |
| 187 | if (!(ret & FG_15BIT_WORD_VALID)) { |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 188 | dev_err(info->dev, "Error reg 0x%02x contents not valid\n", reg); |
Hans de Goede | 4949fc5 | 2016-12-14 17:38:51 +0100 | [diff] [blame] | 189 | return -ENXIO; |
| 190 | } |
| 191 | |
| 192 | return ret & FG_15BIT_VAL_MASK; |
| 193 | } |
| 194 | |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 195 | static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg) |
| 196 | { |
| 197 | unsigned char buf[2]; |
| 198 | int ret; |
| 199 | |
| 200 | ret = regmap_bulk_read(info->regmap, reg, buf, 2); |
| 201 | if (ret < 0) { |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 202 | dev_err(info->dev, "Error reading reg 0x%02x err: %d\n", reg, ret); |
Hans de Goede | 248efcf | 2016-12-14 17:38:52 +0100 | [diff] [blame] | 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | /* 12-bit data values have upper 8 bits in buf[0], lower 4 in buf[1] */ |
| 207 | return (buf[0] << 4) | ((buf[1] >> 4) & 0x0f); |
| 208 | } |
| 209 | |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 210 | static int fuel_gauge_update_registers(struct axp288_fg_info *info) |
| 211 | { |
| 212 | int ret; |
| 213 | |
| 214 | if (info->valid && time_before(jiffies, info->last_updated + AXP288_REG_UPDATE_INTERVAL)) |
| 215 | return 0; |
| 216 | |
| 217 | dev_dbg(info->dev, "Fuel Gauge updating register values...\n"); |
| 218 | |
| 219 | ret = iosf_mbi_block_punit_i2c_access(); |
| 220 | if (ret < 0) |
| 221 | return ret; |
| 222 | |
| 223 | ret = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS); |
| 224 | if (ret < 0) |
| 225 | goto out; |
| 226 | info->pwr_stat = ret; |
| 227 | |
| 228 | ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES); |
| 229 | if (ret < 0) |
| 230 | goto out; |
| 231 | info->fg_res = ret; |
| 232 | |
| 233 | ret = iio_read_channel_raw(info->iio_channel[BAT_VOLT], &info->bat_volt); |
| 234 | if (ret < 0) |
| 235 | goto out; |
| 236 | |
| 237 | if (info->pwr_stat & PS_STAT_BAT_CHRG_DIR) { |
| 238 | info->d_curr = 0; |
| 239 | ret = iio_read_channel_raw(info->iio_channel[BAT_CHRG_CURR], &info->c_curr); |
| 240 | if (ret < 0) |
| 241 | goto out; |
| 242 | } else { |
| 243 | info->c_curr = 0; |
| 244 | ret = iio_read_channel_raw(info->iio_channel[BAT_D_CURR], &info->d_curr); |
| 245 | if (ret < 0) |
| 246 | goto out; |
| 247 | } |
| 248 | |
| 249 | ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG); |
| 250 | if (ret < 0) |
| 251 | goto out; |
| 252 | info->ocv = ret; |
| 253 | |
| 254 | ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG); |
| 255 | if (ret < 0) |
| 256 | goto out; |
| 257 | info->fg_cc_mtr1 = ret; |
| 258 | |
| 259 | ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG); |
| 260 | if (ret < 0) |
| 261 | goto out; |
| 262 | info->fg_des_cap1 = ret; |
| 263 | |
| 264 | info->last_updated = jiffies; |
| 265 | info->valid = 1; |
| 266 | ret = 0; |
| 267 | out: |
| 268 | iosf_mbi_unblock_punit_i2c_access(); |
| 269 | return ret; |
| 270 | } |
| 271 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 272 | static void fuel_gauge_get_status(struct axp288_fg_info *info) |
| 273 | { |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 274 | int pwr_stat = info->pwr_stat; |
| 275 | int fg_res = info->fg_res; |
| 276 | int curr = info->d_curr; |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 277 | |
| 278 | /* 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] | 279 | if (!(pwr_stat & PS_STAT_VBUS_VALID)) |
| 280 | goto not_full; |
| 281 | |
Hans de Goede | f451655 | 2018-02-15 15:00:36 +0100 | [diff] [blame] | 282 | if (!(fg_res & FG_REP_CAP_VALID)) |
| 283 | goto not_full; |
| 284 | |
| 285 | fg_res &= ~FG_REP_CAP_VALID; |
| 286 | if (fg_res == 100) { |
| 287 | info->status = POWER_SUPPLY_STATUS_FULL; |
| 288 | return; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Hans de Goede | f451655 | 2018-02-15 15:00:36 +0100 | [diff] [blame] | 291 | /* |
| 292 | * Sometimes the charger turns itself off before fg-res reaches 100%. |
| 293 | * When this happens the AXP288 reports a not-charging status and |
| 294 | * 0 mA discharge current. |
| 295 | */ |
| 296 | if (fg_res < 90 || (pwr_stat & PS_STAT_BAT_CHRG_DIR)) |
| 297 | goto not_full; |
| 298 | |
Hans de Goede | f451655 | 2018-02-15 15:00:36 +0100 | [diff] [blame] | 299 | if (curr == 0) { |
| 300 | info->status = POWER_SUPPLY_STATUS_FULL; |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | not_full: |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 305 | if (pwr_stat & PS_STAT_BAT_CHRG_DIR) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 306 | info->status = POWER_SUPPLY_STATUS_CHARGING; |
Hans de Goede | 2b5a4b4 | 2017-12-26 13:59:11 +0100 | [diff] [blame] | 307 | else |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 308 | info->status = POWER_SUPPLY_STATUS_DISCHARGING; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 311 | static int fuel_gauge_battery_health(struct axp288_fg_info *info) |
| 312 | { |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 313 | int vocv = VOLTAGE_FROM_ADC(info->ocv); |
| 314 | int health = POWER_SUPPLY_HEALTH_UNKNOWN; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 315 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 316 | if (vocv > info->max_volt) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 317 | health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 318 | else |
| 319 | health = POWER_SUPPLY_HEALTH_GOOD; |
| 320 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 321 | return health; |
| 322 | } |
| 323 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 324 | static int fuel_gauge_get_property(struct power_supply *ps, |
| 325 | enum power_supply_property prop, |
| 326 | union power_supply_propval *val) |
| 327 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 328 | struct axp288_fg_info *info = power_supply_get_drvdata(ps); |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 329 | int ret, value; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 330 | |
| 331 | mutex_lock(&info->lock); |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 332 | |
| 333 | ret = fuel_gauge_update_registers(info); |
| 334 | if (ret < 0) |
| 335 | goto out; |
| 336 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 337 | switch (prop) { |
| 338 | case POWER_SUPPLY_PROP_STATUS: |
| 339 | fuel_gauge_get_status(info); |
| 340 | val->intval = info->status; |
| 341 | break; |
| 342 | case POWER_SUPPLY_PROP_HEALTH: |
| 343 | val->intval = fuel_gauge_battery_health(info); |
| 344 | break; |
| 345 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 346 | value = VOLTAGE_FROM_ADC(info->bat_volt); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 347 | val->intval = PROP_VOLT(value); |
| 348 | break; |
| 349 | case POWER_SUPPLY_PROP_VOLTAGE_OCV: |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 350 | value = VOLTAGE_FROM_ADC(info->ocv); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 351 | val->intval = PROP_VOLT(value); |
| 352 | break; |
| 353 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 354 | if (info->d_curr > 0) |
| 355 | value = -1 * info->d_curr; |
| 356 | else |
| 357 | value = info->c_curr; |
| 358 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 359 | val->intval = PROP_CURR(value); |
| 360 | break; |
| 361 | case POWER_SUPPLY_PROP_PRESENT: |
Hans de Goede | c371d44 | 2021-08-01 15:31:02 +0200 | [diff] [blame] | 362 | if (info->pwr_op & CHRG_STAT_BAT_PRESENT) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 363 | val->intval = 1; |
| 364 | else |
| 365 | val->intval = 0; |
| 366 | break; |
| 367 | case POWER_SUPPLY_PROP_CAPACITY: |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 368 | if (!(info->fg_res & FG_REP_CAP_VALID)) |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 369 | dev_err(info->dev, "capacity measurement not valid\n"); |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 370 | val->intval = (info->fg_res & FG_REP_CAP_VAL_MASK); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 371 | break; |
| 372 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
Hans de Goede | c371d44 | 2021-08-01 15:31:02 +0200 | [diff] [blame] | 373 | val->intval = (info->low_cap & 0x0f); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 374 | break; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 375 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 376 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 377 | break; |
| 378 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 379 | val->intval = info->fg_cc_mtr1 * FG_DES_CAP_RES_LSB; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 380 | break; |
| 381 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 382 | val->intval = info->fg_des_cap1 * FG_DES_CAP_RES_LSB; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 383 | break; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 384 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 385 | val->intval = PROP_VOLT(info->max_volt); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 386 | break; |
| 387 | default: |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 388 | ret = -EINVAL; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 391 | out: |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 392 | mutex_unlock(&info->lock); |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | static int fuel_gauge_set_property(struct power_supply *ps, |
| 397 | enum power_supply_property prop, |
| 398 | const union power_supply_propval *val) |
| 399 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 400 | struct axp288_fg_info *info = power_supply_get_drvdata(ps); |
Hans de Goede | c371d44 | 2021-08-01 15:31:02 +0200 | [diff] [blame] | 401 | int new_low_cap, ret = 0; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 402 | |
| 403 | mutex_lock(&info->lock); |
| 404 | switch (prop) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 405 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 406 | if ((val->intval < 0) || (val->intval > 15)) { |
| 407 | ret = -EINVAL; |
| 408 | break; |
| 409 | } |
Hans de Goede | c371d44 | 2021-08-01 15:31:02 +0200 | [diff] [blame] | 410 | new_low_cap = info->low_cap; |
| 411 | new_low_cap &= 0xf0; |
| 412 | new_low_cap |= (val->intval & 0xf); |
| 413 | ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, new_low_cap); |
| 414 | if (ret == 0) |
| 415 | info->low_cap = new_low_cap; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 416 | break; |
| 417 | default: |
| 418 | ret = -EINVAL; |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | mutex_unlock(&info->lock); |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | static int fuel_gauge_property_is_writeable(struct power_supply *psy, |
| 427 | enum power_supply_property psp) |
| 428 | { |
| 429 | int ret; |
| 430 | |
| 431 | switch (psp) { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 432 | case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN: |
| 433 | ret = 1; |
| 434 | break; |
| 435 | default: |
| 436 | ret = 0; |
| 437 | } |
| 438 | |
| 439 | return ret; |
| 440 | } |
| 441 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 442 | static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev) |
| 443 | { |
| 444 | struct axp288_fg_info *info = dev; |
| 445 | int i; |
| 446 | |
| 447 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) { |
| 448 | if (info->irq[i] == irq) |
| 449 | break; |
| 450 | } |
| 451 | |
| 452 | if (i >= AXP288_FG_INTR_NUM) { |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 453 | dev_warn(info->dev, "spurious interrupt!!\n"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 454 | return IRQ_NONE; |
| 455 | } |
| 456 | |
| 457 | switch (i) { |
| 458 | case QWBTU_IRQ: |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 459 | dev_info(info->dev, "Quit Battery under temperature in work mode IRQ (QWBTU)\n"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 460 | break; |
| 461 | case WBTU_IRQ: |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 462 | dev_info(info->dev, "Battery under temperature in work mode IRQ (WBTU)\n"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 463 | break; |
| 464 | case QWBTO_IRQ: |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 465 | dev_info(info->dev, "Quit Battery over temperature in work mode IRQ (QWBTO)\n"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 466 | break; |
| 467 | case WBTO_IRQ: |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 468 | dev_info(info->dev, "Battery over temperature in work mode IRQ (WBTO)\n"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 469 | break; |
| 470 | case WL2_IRQ: |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 471 | dev_info(info->dev, "Low Batt Warning(2) INTR\n"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 472 | break; |
| 473 | case WL1_IRQ: |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 474 | dev_info(info->dev, "Low Batt Warning(1) INTR\n"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 475 | break; |
| 476 | default: |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 477 | dev_warn(info->dev, "Spurious Interrupt!!!\n"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 480 | info->valid = 0; /* Force updating of the cached registers */ |
| 481 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 482 | power_supply_changed(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 483 | return IRQ_HANDLED; |
| 484 | } |
| 485 | |
| 486 | static void fuel_gauge_external_power_changed(struct power_supply *psy) |
| 487 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 488 | struct axp288_fg_info *info = power_supply_get_drvdata(psy); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 489 | |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 490 | info->valid = 0; /* Force updating of the cached registers */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 491 | power_supply_changed(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 492 | } |
| 493 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 494 | static const struct power_supply_desc fuel_gauge_desc = { |
| 495 | .name = DEV_NAME, |
| 496 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 497 | .properties = fuel_gauge_props, |
| 498 | .num_properties = ARRAY_SIZE(fuel_gauge_props), |
| 499 | .get_property = fuel_gauge_get_property, |
| 500 | .set_property = fuel_gauge_set_property, |
| 501 | .property_is_writeable = fuel_gauge_property_is_writeable, |
| 502 | .external_power_changed = fuel_gauge_external_power_changed, |
| 503 | }; |
| 504 | |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 505 | static void fuel_gauge_init_irq(struct axp288_fg_info *info, struct platform_device *pdev) |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 506 | { |
| 507 | int ret, i, pirq; |
| 508 | |
| 509 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) { |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 510 | pirq = platform_get_irq(pdev, i); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 511 | info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq); |
| 512 | if (info->irq[i] < 0) { |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 513 | dev_warn(info->dev, "regmap_irq get virq failed for IRQ %d: %d\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 514 | pirq, info->irq[i]); |
| 515 | info->irq[i] = -1; |
| 516 | goto intr_failed; |
| 517 | } |
| 518 | ret = request_threaded_irq(info->irq[i], |
| 519 | NULL, fuel_gauge_thread_handler, |
| 520 | IRQF_ONESHOT, DEV_NAME, info); |
| 521 | if (ret) { |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 522 | dev_warn(info->dev, "request irq failed for IRQ %d: %d\n", |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 523 | pirq, info->irq[i]); |
| 524 | info->irq[i] = -1; |
| 525 | goto intr_failed; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | return; |
| 529 | |
| 530 | intr_failed: |
| 531 | for (; i > 0; i--) { |
| 532 | free_irq(info->irq[i - 1], info); |
| 533 | info->irq[i - 1] = -1; |
| 534 | } |
| 535 | } |
| 536 | |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 537 | /* |
| 538 | * Some devices have no battery (HDMI sticks) and the axp288 battery's |
| 539 | * detection reports one despite it not being there. |
Hans de Goede | 6f3ed83 | 2019-08-25 17:41:51 +0200 | [diff] [blame] | 540 | * Please keep this listed sorted alphabetically. |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 541 | */ |
Hans de Goede | 0973e96 | 2021-06-17 21:27:01 +0200 | [diff] [blame] | 542 | static const struct dmi_system_id axp288_no_battery_list[] = { |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 543 | { |
Hans de Goede | 9274c78 | 2019-04-22 22:43:01 +0200 | [diff] [blame] | 544 | /* ACEPC T8 Cherry Trail Z8350 mini PC */ |
| 545 | .matches = { |
| 546 | DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."), |
| 547 | DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"), |
| 548 | DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T8"), |
| 549 | /* also match on somewhat unique bios-version */ |
| 550 | DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"), |
| 551 | }, |
| 552 | }, |
| 553 | { |
| 554 | /* ACEPC T11 Cherry Trail Z8350 mini PC */ |
| 555 | .matches = { |
| 556 | DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."), |
| 557 | DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"), |
| 558 | DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "T11"), |
| 559 | /* also match on somewhat unique bios-version */ |
| 560 | DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1.000"), |
| 561 | }, |
| 562 | }, |
| 563 | { |
Hans de Goede | 6f3ed83 | 2019-08-25 17:41:51 +0200 | [diff] [blame] | 564 | /* ECS EF20EA */ |
| 565 | .matches = { |
| 566 | DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"), |
| 567 | }, |
| 568 | }, |
| 569 | { |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 570 | /* Intel Cherry Trail Compute Stick, Windows version */ |
| 571 | .matches = { |
Jeffery Miller | e42fe5b | 2020-02-25 16:59:41 -0600 | [diff] [blame] | 572 | DMI_MATCH(DMI_SYS_VENDOR, "Intel"), |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 573 | DMI_MATCH(DMI_PRODUCT_NAME, "STK1AW32SC"), |
| 574 | }, |
| 575 | }, |
| 576 | { |
| 577 | /* Intel Cherry Trail Compute Stick, version without an OS */ |
| 578 | .matches = { |
Jeffery Miller | e42fe5b | 2020-02-25 16:59:41 -0600 | [diff] [blame] | 579 | DMI_MATCH(DMI_SYS_VENDOR, "Intel"), |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 580 | DMI_MATCH(DMI_PRODUCT_NAME, "STK1A32SC"), |
| 581 | }, |
| 582 | }, |
| 583 | { |
Rafael Gandolfi | 4ac54b8 | 2020-04-06 16:05:38 +0200 | [diff] [blame] | 584 | /* Meegopad T02 */ |
| 585 | .matches = { |
| 586 | DMI_MATCH(DMI_PRODUCT_NAME, "MEEGOPAD T02"), |
| 587 | }, |
| 588 | }, |
Hans de Goede | 6b714ea | 2021-01-12 11:36:05 +0100 | [diff] [blame] | 589 | { /* Mele PCG03 Mini PC */ |
| 590 | .matches = { |
| 591 | DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"), |
| 592 | DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"), |
| 593 | }, |
| 594 | }, |
Hans de Goede | fa7da74 | 2019-08-25 17:41:52 +0200 | [diff] [blame] | 595 | { |
| 596 | /* Minix Neo Z83-4 mini PC */ |
| 597 | .matches = { |
| 598 | DMI_MATCH(DMI_SYS_VENDOR, "MINIX"), |
| 599 | DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"), |
| 600 | } |
| 601 | }, |
Hans de Goede | 3a06b91 | 2021-06-17 21:27:02 +0200 | [diff] [blame] | 602 | { |
| 603 | /* Various Ace PC/Meegopad/MinisForum/Wintel Mini-PCs/HDMI-sticks */ |
| 604 | .matches = { |
| 605 | DMI_MATCH(DMI_BOARD_NAME, "T3 MRD"), |
| 606 | DMI_MATCH(DMI_CHASSIS_TYPE, "3"), |
| 607 | DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."), |
| 608 | DMI_MATCH(DMI_BIOS_VERSION, "5.11"), |
| 609 | }, |
| 610 | }, |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 611 | {} |
| 612 | }; |
| 613 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 614 | static int axp288_fuel_gauge_probe(struct platform_device *pdev) |
| 615 | { |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 616 | int i, ret = 0; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 617 | struct axp288_fg_info *info; |
| 618 | struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 619 | struct power_supply_config psy_cfg = {}; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 620 | static const char * const iio_chan_name[] = { |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 621 | [BAT_CHRG_CURR] = "axp288-chrg-curr", |
| 622 | [BAT_D_CURR] = "axp288-chrg-d-curr", |
| 623 | [BAT_VOLT] = "axp288-batt-volt", |
| 624 | }; |
Hans de Goede | 04d6f72 | 2018-04-18 14:08:21 +0200 | [diff] [blame] | 625 | unsigned int val; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 626 | |
Hans de Goede | 0973e96 | 2021-06-17 21:27:01 +0200 | [diff] [blame] | 627 | if (dmi_check_system(axp288_no_battery_list)) |
Hans de Goede | b60c75b | 2017-12-26 13:59:13 +0100 | [diff] [blame] | 628 | return -ENODEV; |
| 629 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 630 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 631 | if (!info) |
| 632 | return -ENOMEM; |
| 633 | |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 634 | info->dev = &pdev->dev; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 635 | info->regmap = axp20x->regmap; |
| 636 | info->regmap_irqc = axp20x->regmap_irqc; |
| 637 | info->status = POWER_SUPPLY_STATUS_UNKNOWN; |
Andrejus Basovas | 394088f | 2021-08-01 15:31:03 +0200 | [diff] [blame] | 638 | info->valid = 0; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 639 | |
| 640 | platform_set_drvdata(pdev, info); |
| 641 | |
| 642 | mutex_init(&info->lock); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 643 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 644 | for (i = 0; i < IIO_CHANNEL_NUM; i++) { |
| 645 | /* |
| 646 | * Note cannot use devm_iio_channel_get because x86 systems |
| 647 | * lack the device<->channel maps which iio_channel_get will |
| 648 | * try to use when passed a non NULL device pointer. |
| 649 | */ |
| 650 | info->iio_channel[i] = |
| 651 | iio_channel_get(NULL, iio_chan_name[i]); |
| 652 | if (IS_ERR(info->iio_channel[i])) { |
| 653 | ret = PTR_ERR(info->iio_channel[i]); |
| 654 | dev_dbg(&pdev->dev, "error getting iiochan %s: %d\n", |
| 655 | iio_chan_name[i], ret); |
| 656 | /* Wait for axp288_adc to load */ |
| 657 | if (ret == -ENODEV) |
| 658 | ret = -EPROBE_DEFER; |
| 659 | |
| 660 | goto out_free_iio_chan; |
| 661 | } |
| 662 | } |
| 663 | |
Hans de Goede | 213e19d6 | 2021-08-01 15:31:05 +0200 | [diff] [blame] | 664 | ret = iosf_mbi_block_punit_i2c_access(); |
| 665 | if (ret < 0) |
| 666 | goto out_free_iio_chan; |
| 667 | |
Hans de Goede | 964b3e9 | 2021-08-01 15:31:04 +0200 | [diff] [blame] | 668 | /* |
| 669 | * On some devices the fuelgauge and charger parts of the axp288 are |
| 670 | * not used, check that the fuelgauge is enabled (CC_CTRL != 0). |
| 671 | */ |
| 672 | ret = regmap_read(axp20x->regmap, AXP20X_CC_CTRL, &val); |
| 673 | if (ret < 0) |
Hans de Goede | 213e19d6 | 2021-08-01 15:31:05 +0200 | [diff] [blame] | 674 | goto unblock_punit_i2c_access; |
Hans de Goede | 964b3e9 | 2021-08-01 15:31:04 +0200 | [diff] [blame] | 675 | if (val == 0) { |
| 676 | ret = -ENODEV; |
Hans de Goede | 213e19d6 | 2021-08-01 15:31:05 +0200 | [diff] [blame] | 677 | goto unblock_punit_i2c_access; |
Hans de Goede | 964b3e9 | 2021-08-01 15:31:04 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 680 | ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG); |
| 681 | if (ret < 0) |
Hans de Goede | 213e19d6 | 2021-08-01 15:31:05 +0200 | [diff] [blame] | 682 | goto unblock_punit_i2c_access; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 683 | |
| 684 | if (!(ret & FG_DES_CAP1_VALID)) { |
| 685 | dev_err(&pdev->dev, "axp288 not configured by firmware\n"); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 686 | ret = -ENODEV; |
Hans de Goede | 213e19d6 | 2021-08-01 15:31:05 +0200 | [diff] [blame] | 687 | goto unblock_punit_i2c_access; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1); |
| 691 | if (ret < 0) |
Hans de Goede | 213e19d6 | 2021-08-01 15:31:05 +0200 | [diff] [blame] | 692 | goto unblock_punit_i2c_access; |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 693 | switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) { |
| 694 | case CHRG_CCCV_CV_4100MV: |
| 695 | info->max_volt = 4100; |
| 696 | break; |
| 697 | case CHRG_CCCV_CV_4150MV: |
| 698 | info->max_volt = 4150; |
| 699 | break; |
| 700 | case CHRG_CCCV_CV_4200MV: |
| 701 | info->max_volt = 4200; |
| 702 | break; |
| 703 | case CHRG_CCCV_CV_4350MV: |
| 704 | info->max_volt = 4350; |
| 705 | break; |
| 706 | } |
| 707 | |
Hans de Goede | c371d44 | 2021-08-01 15:31:02 +0200 | [diff] [blame] | 708 | ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE); |
| 709 | if (ret < 0) |
Hans de Goede | 213e19d6 | 2021-08-01 15:31:05 +0200 | [diff] [blame] | 710 | goto unblock_punit_i2c_access; |
Hans de Goede | c371d44 | 2021-08-01 15:31:02 +0200 | [diff] [blame] | 711 | info->pwr_op = ret; |
| 712 | |
| 713 | ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG); |
| 714 | if (ret < 0) |
Hans de Goede | 213e19d6 | 2021-08-01 15:31:05 +0200 | [diff] [blame] | 715 | goto unblock_punit_i2c_access; |
Hans de Goede | c371d44 | 2021-08-01 15:31:02 +0200 | [diff] [blame] | 716 | info->low_cap = ret; |
| 717 | |
Hans de Goede | 213e19d6 | 2021-08-01 15:31:05 +0200 | [diff] [blame] | 718 | unblock_punit_i2c_access: |
| 719 | iosf_mbi_unblock_punit_i2c_access(); |
| 720 | /* In case we arrive here by goto because of a register access error */ |
| 721 | if (ret < 0) |
| 722 | goto out_free_iio_chan; |
| 723 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 724 | psy_cfg.drv_data = info; |
| 725 | info->bat = power_supply_register(&pdev->dev, &fuel_gauge_desc, &psy_cfg); |
| 726 | if (IS_ERR(info->bat)) { |
| 727 | ret = PTR_ERR(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 728 | dev_err(&pdev->dev, "failed to register battery: %d\n", ret); |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 729 | goto out_free_iio_chan; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Hans de Goede | 7eef3e6 | 2021-08-01 15:31:01 +0200 | [diff] [blame] | 732 | fuel_gauge_init_irq(info, pdev); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 733 | |
Hans de Goede | 888f974 | 2016-12-14 17:38:53 +0100 | [diff] [blame] | 734 | return 0; |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 735 | |
| 736 | out_free_iio_chan: |
| 737 | for (i = 0; i < IIO_CHANNEL_NUM; i++) |
| 738 | if (!IS_ERR_OR_NULL(info->iio_channel[i])) |
| 739 | iio_channel_release(info->iio_channel[i]); |
| 740 | |
| 741 | return ret; |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Krzysztof Kozlowski | f1f27a4 | 2015-05-02 00:46:43 +0900 | [diff] [blame] | 744 | static const struct platform_device_id axp288_fg_id_table[] = { |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 745 | { .name = DEV_NAME }, |
| 746 | {}, |
| 747 | }; |
Javier Martinez Canillas | 99e33fb | 2016-10-17 16:13:36 -0300 | [diff] [blame] | 748 | MODULE_DEVICE_TABLE(platform, axp288_fg_id_table); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 749 | |
| 750 | static int axp288_fuel_gauge_remove(struct platform_device *pdev) |
| 751 | { |
| 752 | struct axp288_fg_info *info = platform_get_drvdata(pdev); |
| 753 | int i; |
| 754 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 755 | power_supply_unregister(info->bat); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 756 | |
| 757 | for (i = 0; i < AXP288_FG_INTR_NUM; i++) |
| 758 | if (info->irq[i] >= 0) |
| 759 | free_irq(info->irq[i], info); |
| 760 | |
Hans de Goede | 331645e | 2017-12-26 13:59:10 +0100 | [diff] [blame] | 761 | for (i = 0; i < IIO_CHANNEL_NUM; i++) |
| 762 | iio_channel_release(info->iio_channel[i]); |
| 763 | |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | static struct platform_driver axp288_fuel_gauge_driver = { |
| 768 | .probe = axp288_fuel_gauge_probe, |
| 769 | .remove = axp288_fuel_gauge_remove, |
| 770 | .id_table = axp288_fg_id_table, |
| 771 | .driver = { |
| 772 | .name = DEV_NAME, |
| 773 | }, |
| 774 | }; |
| 775 | |
| 776 | module_platform_driver(axp288_fuel_gauge_driver); |
| 777 | |
Ramakrishna Pallala | 409e718 | 2015-03-13 21:49:09 +0530 | [diff] [blame] | 778 | MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>"); |
Todd Brandt | 5a5bf49 | 2015-02-04 16:24:38 -0800 | [diff] [blame] | 779 | MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>"); |
| 780 | MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver"); |
| 781 | MODULE_LICENSE("GPL"); |