Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 2 | /* |
Dragos Bogdan | 17825ff | 2017-07-16 11:35:32 +0200 | [diff] [blame] | 3 | * I2C client/driver for the Linear Technology LTC2941, LTC2942, LTC2943 |
| 4 | * and LTC2944 Battery Gas Gauge IC |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2014 Topic Embedded Systems |
| 7 | * |
| 8 | * Author: Auryn Verwegen |
| 9 | * Author: Mike Looijmans |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
Javier Martinez Canillas | 9697277 | 2017-03-29 12:00:57 -0400 | [diff] [blame] | 13 | #include <linux/of_device.h> |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/swab.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/delay.h> |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 19 | #include <linux/power_supply.h> |
| 20 | #include <linux/slab.h> |
| 21 | |
| 22 | #define I16_MSB(x) ((x >> 8) & 0xFF) |
| 23 | #define I16_LSB(x) (x & 0xFF) |
| 24 | |
| 25 | #define LTC294X_WORK_DELAY 10 /* Update delay in seconds */ |
| 26 | |
| 27 | #define LTC294X_MAX_VALUE 0xFFFF |
| 28 | #define LTC294X_MID_SUPPLY 0x7FFF |
| 29 | |
| 30 | #define LTC2941_MAX_PRESCALER_EXP 7 |
| 31 | #define LTC2943_MAX_PRESCALER_EXP 6 |
| 32 | |
| 33 | enum ltc294x_reg { |
| 34 | LTC294X_REG_STATUS = 0x00, |
| 35 | LTC294X_REG_CONTROL = 0x01, |
| 36 | LTC294X_REG_ACC_CHARGE_MSB = 0x02, |
| 37 | LTC294X_REG_ACC_CHARGE_LSB = 0x03, |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 38 | LTC294X_REG_CHARGE_THR_HIGH_MSB = 0x04, |
| 39 | LTC294X_REG_CHARGE_THR_HIGH_LSB = 0x05, |
| 40 | LTC294X_REG_CHARGE_THR_LOW_MSB = 0x06, |
| 41 | LTC294X_REG_CHARGE_THR_LOW_LSB = 0x07, |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 42 | LTC294X_REG_VOLTAGE_MSB = 0x08, |
| 43 | LTC294X_REG_VOLTAGE_LSB = 0x09, |
| 44 | LTC2942_REG_TEMPERATURE_MSB = 0x0C, |
| 45 | LTC2942_REG_TEMPERATURE_LSB = 0x0D, |
| 46 | LTC2943_REG_CURRENT_MSB = 0x0E, |
| 47 | LTC2943_REG_CURRENT_LSB = 0x0F, |
| 48 | LTC2943_REG_TEMPERATURE_MSB = 0x14, |
| 49 | LTC2943_REG_TEMPERATURE_LSB = 0x15, |
| 50 | }; |
| 51 | |
| 52 | enum ltc294x_id { |
| 53 | LTC2941_ID, |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 54 | LTC2942_ID, |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 55 | LTC2943_ID, |
Dragos Bogdan | 17825ff | 2017-07-16 11:35:32 +0200 | [diff] [blame] | 56 | LTC2944_ID, |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 59 | #define LTC2941_REG_STATUS_CHIP_ID BIT(7) |
| 60 | |
| 61 | #define LTC2942_REG_CONTROL_MODE_SCAN (BIT(7) | BIT(6)) |
| 62 | #define LTC2943_REG_CONTROL_MODE_SCAN BIT(7) |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 63 | #define LTC294X_REG_CONTROL_PRESCALER_MASK (BIT(5) | BIT(4) | BIT(3)) |
| 64 | #define LTC294X_REG_CONTROL_SHUTDOWN_MASK (BIT(0)) |
| 65 | #define LTC294X_REG_CONTROL_PRESCALER_SET(x) \ |
| 66 | ((x << 3) & LTC294X_REG_CONTROL_PRESCALER_MASK) |
| 67 | #define LTC294X_REG_CONTROL_ALCC_CONFIG_DISABLED 0 |
Mike Looijmans | 5f2f0d6 | 2017-11-23 15:41:05 +0100 | [diff] [blame] | 68 | #define LTC294X_REG_CONTROL_ADC_DISABLE(x) ((x) & ~(BIT(7) | BIT(6))) |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 69 | |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 70 | struct ltc294x_info { |
| 71 | struct i2c_client *client; /* I2C Client pointer */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 72 | struct power_supply *supply; /* Supply pointer */ |
| 73 | struct power_supply_desc supply_desc; /* Supply description */ |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 74 | struct delayed_work work; /* Work scheduler */ |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 75 | enum ltc294x_id id; /* Chip type */ |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 76 | int charge; /* Last charge register content */ |
| 77 | int r_sense; /* mOhm */ |
| 78 | int Qlsb; /* nAh */ |
| 79 | }; |
| 80 | |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 81 | static inline int convert_bin_to_uAh( |
| 82 | const struct ltc294x_info *info, int Q) |
| 83 | { |
| 84 | return ((Q * (info->Qlsb / 10))) / 100; |
| 85 | } |
| 86 | |
| 87 | static inline int convert_uAh_to_bin( |
| 88 | const struct ltc294x_info *info, int uAh) |
| 89 | { |
| 90 | int Q; |
| 91 | |
| 92 | Q = (uAh * 100) / (info->Qlsb/10); |
| 93 | return (Q < LTC294X_MAX_VALUE) ? Q : LTC294X_MAX_VALUE; |
| 94 | } |
| 95 | |
| 96 | static int ltc294x_read_regs(struct i2c_client *client, |
| 97 | enum ltc294x_reg reg, u8 *buf, int num_regs) |
| 98 | { |
| 99 | int ret; |
| 100 | struct i2c_msg msgs[2] = { }; |
| 101 | u8 reg_start = reg; |
| 102 | |
| 103 | msgs[0].addr = client->addr; |
| 104 | msgs[0].len = 1; |
| 105 | msgs[0].buf = ®_start; |
| 106 | |
| 107 | msgs[1].addr = client->addr; |
| 108 | msgs[1].len = num_regs; |
| 109 | msgs[1].buf = buf; |
| 110 | msgs[1].flags = I2C_M_RD; |
| 111 | |
| 112 | ret = i2c_transfer(client->adapter, &msgs[0], 2); |
| 113 | if (ret < 0) { |
| 114 | dev_err(&client->dev, "ltc2941 read_reg failed!\n"); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | dev_dbg(&client->dev, "%s (%#x, %d) -> %#x\n", |
| 119 | __func__, reg, num_regs, *buf); |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int ltc294x_write_regs(struct i2c_client *client, |
| 125 | enum ltc294x_reg reg, const u8 *buf, int num_regs) |
| 126 | { |
| 127 | int ret; |
| 128 | u8 reg_start = reg; |
| 129 | |
| 130 | ret = i2c_smbus_write_i2c_block_data(client, reg_start, num_regs, buf); |
| 131 | if (ret < 0) { |
| 132 | dev_err(&client->dev, "ltc2941 write_reg failed!\n"); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | dev_dbg(&client->dev, "%s (%#x, %d) -> %#x\n", |
| 137 | __func__, reg, num_regs, *buf); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int ltc294x_reset(const struct ltc294x_info *info, int prescaler_exp) |
| 143 | { |
| 144 | int ret; |
| 145 | u8 value; |
| 146 | u8 control; |
| 147 | |
| 148 | /* Read status and control registers */ |
| 149 | ret = ltc294x_read_regs(info->client, LTC294X_REG_CONTROL, &value, 1); |
| 150 | if (ret < 0) { |
| 151 | dev_err(&info->client->dev, |
| 152 | "Could not read registers from device\n"); |
| 153 | goto error_exit; |
| 154 | } |
| 155 | |
| 156 | control = LTC294X_REG_CONTROL_PRESCALER_SET(prescaler_exp) | |
| 157 | LTC294X_REG_CONTROL_ALCC_CONFIG_DISABLED; |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 158 | /* Put device into "monitor" mode */ |
| 159 | switch (info->id) { |
| 160 | case LTC2942_ID: /* 2942 measures every 2 sec */ |
| 161 | control |= LTC2942_REG_CONTROL_MODE_SCAN; |
| 162 | break; |
Dragos Bogdan | 17825ff | 2017-07-16 11:35:32 +0200 | [diff] [blame] | 163 | case LTC2943_ID: |
| 164 | case LTC2944_ID: /* 2943 and 2944 measure every 10 sec */ |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 165 | control |= LTC2943_REG_CONTROL_MODE_SCAN; |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 166 | break; |
| 167 | default: |
| 168 | break; |
| 169 | } |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 170 | |
| 171 | if (value != control) { |
| 172 | ret = ltc294x_write_regs(info->client, |
| 173 | LTC294X_REG_CONTROL, &control, 1); |
| 174 | if (ret < 0) { |
| 175 | dev_err(&info->client->dev, |
| 176 | "Could not write register\n"); |
| 177 | goto error_exit; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | |
| 183 | error_exit: |
| 184 | return ret; |
| 185 | } |
| 186 | |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 187 | static int ltc294x_read_charge_register(const struct ltc294x_info *info, |
| 188 | enum ltc294x_reg reg) |
| 189 | { |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 190 | int ret; |
| 191 | u8 datar[2]; |
| 192 | |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 193 | ret = ltc294x_read_regs(info->client, reg, &datar[0], 2); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 194 | if (ret < 0) |
| 195 | return ret; |
| 196 | return (datar[0] << 8) + datar[1]; |
| 197 | } |
| 198 | |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 199 | static int ltc294x_get_charge(const struct ltc294x_info *info, |
| 200 | enum ltc294x_reg reg, int *val) |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 201 | { |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 202 | int value = ltc294x_read_charge_register(info, reg); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 203 | |
| 204 | if (value < 0) |
| 205 | return value; |
| 206 | /* When r_sense < 0, this counts up when the battery discharges */ |
| 207 | if (info->Qlsb < 0) |
| 208 | value -= 0xFFFF; |
| 209 | *val = convert_bin_to_uAh(info, value); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static int ltc294x_set_charge_now(const struct ltc294x_info *info, int val) |
| 214 | { |
| 215 | int ret; |
| 216 | u8 dataw[2]; |
| 217 | u8 ctrl_reg; |
| 218 | s32 value; |
| 219 | |
| 220 | value = convert_uAh_to_bin(info, val); |
| 221 | /* Direction depends on how sense+/- were connected */ |
| 222 | if (info->Qlsb < 0) |
| 223 | value += 0xFFFF; |
| 224 | if ((value < 0) || (value > 0xFFFF)) /* input validation */ |
| 225 | return -EINVAL; |
| 226 | |
| 227 | /* Read control register */ |
| 228 | ret = ltc294x_read_regs(info->client, |
| 229 | LTC294X_REG_CONTROL, &ctrl_reg, 1); |
| 230 | if (ret < 0) |
| 231 | return ret; |
| 232 | /* Disable analog section */ |
| 233 | ctrl_reg |= LTC294X_REG_CONTROL_SHUTDOWN_MASK; |
| 234 | ret = ltc294x_write_regs(info->client, |
| 235 | LTC294X_REG_CONTROL, &ctrl_reg, 1); |
| 236 | if (ret < 0) |
| 237 | return ret; |
| 238 | /* Set new charge value */ |
| 239 | dataw[0] = I16_MSB(value); |
| 240 | dataw[1] = I16_LSB(value); |
| 241 | ret = ltc294x_write_regs(info->client, |
| 242 | LTC294X_REG_ACC_CHARGE_MSB, &dataw[0], 2); |
| 243 | if (ret < 0) |
| 244 | goto error_exit; |
| 245 | /* Enable analog section */ |
| 246 | error_exit: |
| 247 | ctrl_reg &= ~LTC294X_REG_CONTROL_SHUTDOWN_MASK; |
| 248 | ret = ltc294x_write_regs(info->client, |
| 249 | LTC294X_REG_CONTROL, &ctrl_reg, 1); |
| 250 | |
| 251 | return ret < 0 ? ret : 0; |
| 252 | } |
| 253 | |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 254 | static int ltc294x_set_charge_thr(const struct ltc294x_info *info, |
| 255 | enum ltc294x_reg reg, int val) |
| 256 | { |
| 257 | u8 dataw[2]; |
| 258 | s32 value; |
| 259 | |
| 260 | value = convert_uAh_to_bin(info, val); |
| 261 | /* Direction depends on how sense+/- were connected */ |
| 262 | if (info->Qlsb < 0) |
| 263 | value += 0xFFFF; |
| 264 | if ((value < 0) || (value > 0xFFFF)) /* input validation */ |
| 265 | return -EINVAL; |
| 266 | |
| 267 | /* Set new charge value */ |
| 268 | dataw[0] = I16_MSB(value); |
| 269 | dataw[1] = I16_LSB(value); |
| 270 | return ltc294x_write_regs(info->client, reg, &dataw[0], 2); |
| 271 | } |
| 272 | |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 273 | static int ltc294x_get_charge_counter( |
| 274 | const struct ltc294x_info *info, int *val) |
| 275 | { |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 276 | int value = ltc294x_read_charge_register(info, LTC294X_REG_ACC_CHARGE_MSB); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 277 | |
| 278 | if (value < 0) |
| 279 | return value; |
| 280 | value -= LTC294X_MID_SUPPLY; |
| 281 | *val = convert_bin_to_uAh(info, value); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int ltc294x_get_voltage(const struct ltc294x_info *info, int *val) |
| 286 | { |
| 287 | int ret; |
| 288 | u8 datar[2]; |
| 289 | u32 value; |
| 290 | |
| 291 | ret = ltc294x_read_regs(info->client, |
| 292 | LTC294X_REG_VOLTAGE_MSB, &datar[0], 2); |
| 293 | value = (datar[0] << 8) | datar[1]; |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 294 | switch (info->id) { |
| 295 | case LTC2943_ID: |
| 296 | value *= 23600 * 2; |
| 297 | value /= 0xFFFF; |
| 298 | value *= 1000 / 2; |
| 299 | break; |
Dragos Bogdan | 17825ff | 2017-07-16 11:35:32 +0200 | [diff] [blame] | 300 | case LTC2944_ID: |
| 301 | value *= 70800 / 5*4; |
| 302 | value /= 0xFFFF; |
| 303 | value *= 1000 * 5/4; |
| 304 | break; |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 305 | default: |
| 306 | value *= 6000 * 10; |
| 307 | value /= 0xFFFF; |
| 308 | value *= 1000 / 10; |
| 309 | break; |
| 310 | } |
| 311 | *val = value; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | static int ltc294x_get_current(const struct ltc294x_info *info, int *val) |
| 316 | { |
| 317 | int ret; |
| 318 | u8 datar[2]; |
| 319 | s32 value; |
| 320 | |
| 321 | ret = ltc294x_read_regs(info->client, |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 322 | LTC2943_REG_CURRENT_MSB, &datar[0], 2); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 323 | value = (datar[0] << 8) | datar[1]; |
| 324 | value -= 0x7FFF; |
Dragos Bogdan | 17825ff | 2017-07-16 11:35:32 +0200 | [diff] [blame] | 325 | if (info->id == LTC2944_ID) |
| 326 | value *= 64000; |
| 327 | else |
| 328 | value *= 60000; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 329 | /* Value is in range -32k..+32k, r_sense is usually 10..50 mOhm, |
| 330 | * the formula below keeps everything in s32 range while preserving |
| 331 | * enough digits */ |
Dragos Bogdan | 17825ff | 2017-07-16 11:35:32 +0200 | [diff] [blame] | 332 | *val = 1000 * (value / (info->r_sense * 0x7FFF)); /* in uA */ |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static int ltc294x_get_temperature(const struct ltc294x_info *info, int *val) |
| 337 | { |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 338 | enum ltc294x_reg reg; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 339 | int ret; |
| 340 | u8 datar[2]; |
| 341 | u32 value; |
| 342 | |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 343 | if (info->id == LTC2942_ID) { |
| 344 | reg = LTC2942_REG_TEMPERATURE_MSB; |
Ladislav Michl | dde5953 | 2018-02-22 18:21:36 +0100 | [diff] [blame] | 345 | value = 6000; /* Full-scale is 600 Kelvin */ |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 346 | } else { |
| 347 | reg = LTC2943_REG_TEMPERATURE_MSB; |
Ladislav Michl | dde5953 | 2018-02-22 18:21:36 +0100 | [diff] [blame] | 348 | value = 5100; /* Full-scale is 510 Kelvin */ |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 349 | } |
| 350 | ret = ltc294x_read_regs(info->client, reg, &datar[0], 2); |
| 351 | value *= (datar[0] << 8) | datar[1]; |
Ladislav Michl | dde5953 | 2018-02-22 18:21:36 +0100 | [diff] [blame] | 352 | /* Convert to tenths of degree Celsius */ |
| 353 | *val = value / 0xFFFF - 2722; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | static int ltc294x_get_property(struct power_supply *psy, |
| 358 | enum power_supply_property prop, |
| 359 | union power_supply_propval *val) |
| 360 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 361 | struct ltc294x_info *info = power_supply_get_drvdata(psy); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 362 | |
| 363 | switch (prop) { |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 364 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 365 | return ltc294x_get_charge(info, LTC294X_REG_CHARGE_THR_HIGH_MSB, |
| 366 | &val->intval); |
| 367 | case POWER_SUPPLY_PROP_CHARGE_EMPTY: |
| 368 | return ltc294x_get_charge(info, LTC294X_REG_CHARGE_THR_LOW_MSB, |
| 369 | &val->intval); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 370 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 371 | return ltc294x_get_charge(info, LTC294X_REG_ACC_CHARGE_MSB, |
| 372 | &val->intval); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 373 | case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
| 374 | return ltc294x_get_charge_counter(info, &val->intval); |
| 375 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 376 | return ltc294x_get_voltage(info, &val->intval); |
| 377 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 378 | return ltc294x_get_current(info, &val->intval); |
| 379 | case POWER_SUPPLY_PROP_TEMP: |
| 380 | return ltc294x_get_temperature(info, &val->intval); |
| 381 | default: |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static int ltc294x_set_property(struct power_supply *psy, |
| 387 | enum power_supply_property psp, |
| 388 | const union power_supply_propval *val) |
| 389 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 390 | struct ltc294x_info *info = power_supply_get_drvdata(psy); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 391 | |
| 392 | switch (psp) { |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 393 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 394 | return ltc294x_set_charge_thr(info, |
| 395 | LTC294X_REG_CHARGE_THR_HIGH_MSB, val->intval); |
| 396 | case POWER_SUPPLY_PROP_CHARGE_EMPTY: |
| 397 | return ltc294x_set_charge_thr(info, |
| 398 | LTC294X_REG_CHARGE_THR_LOW_MSB, val->intval); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 399 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 400 | return ltc294x_set_charge_now(info, val->intval); |
| 401 | default: |
| 402 | return -EPERM; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static int ltc294x_property_is_writeable( |
| 407 | struct power_supply *psy, enum power_supply_property psp) |
| 408 | { |
| 409 | switch (psp) { |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 410 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 411 | case POWER_SUPPLY_PROP_CHARGE_EMPTY: |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 412 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 413 | return 1; |
| 414 | default: |
| 415 | return 0; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static void ltc294x_update(struct ltc294x_info *info) |
| 420 | { |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 421 | int charge = ltc294x_read_charge_register(info, LTC294X_REG_ACC_CHARGE_MSB); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 422 | |
| 423 | if (charge != info->charge) { |
| 424 | info->charge = charge; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 425 | power_supply_changed(info->supply); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | static void ltc294x_work(struct work_struct *work) |
| 430 | { |
| 431 | struct ltc294x_info *info; |
| 432 | |
| 433 | info = container_of(work, struct ltc294x_info, work.work); |
| 434 | ltc294x_update(info); |
| 435 | schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ); |
| 436 | } |
| 437 | |
| 438 | static enum power_supply_property ltc294x_properties[] = { |
| 439 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
Ladislav Michl | 3bf4e03 | 2018-01-17 20:48:54 +0100 | [diff] [blame] | 440 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 441 | POWER_SUPPLY_PROP_CHARGE_EMPTY, |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 442 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 443 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 444 | POWER_SUPPLY_PROP_TEMP, |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 445 | POWER_SUPPLY_PROP_CURRENT_NOW, |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 446 | }; |
| 447 | |
| 448 | static int ltc294x_i2c_remove(struct i2c_client *client) |
| 449 | { |
| 450 | struct ltc294x_info *info = i2c_get_clientdata(client); |
| 451 | |
Sven Van Asbroeck | a60ec78 | 2019-09-19 11:11:37 -0400 | [diff] [blame] | 452 | cancel_delayed_work_sync(&info->work); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 453 | power_supply_unregister(info->supply); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static int ltc294x_i2c_probe(struct i2c_client *client, |
| 458 | const struct i2c_device_id *id) |
| 459 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 460 | struct power_supply_config psy_cfg = {}; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 461 | struct ltc294x_info *info; |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 462 | struct device_node *np; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 463 | int ret; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 464 | u32 prescaler_exp; |
| 465 | s32 r_sense; |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 466 | u8 status; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 467 | |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 468 | info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL); |
Mike Looijmans | 377b641 | 2015-07-01 11:40:53 +0200 | [diff] [blame] | 469 | if (info == NULL) |
| 470 | return -ENOMEM; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 471 | |
| 472 | i2c_set_clientdata(client, info); |
| 473 | |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 474 | np = of_node_get(client->dev.of_node); |
| 475 | |
Iskren Chernev | 58d1620 | 2020-10-10 09:55:26 +0300 | [diff] [blame] | 476 | info->id = (enum ltc294x_id) (uintptr_t) of_device_get_match_data( |
| 477 | &client->dev); |
Mike Looijmans | 377b641 | 2015-07-01 11:40:53 +0200 | [diff] [blame] | 478 | info->supply_desc.name = np->name; |
| 479 | |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 480 | /* r_sense can be negative, when sense+ is connected to the battery |
| 481 | * instead of the sense-. This results in reversed measurements. */ |
| 482 | ret = of_property_read_u32(np, "lltc,resistor-sense", &r_sense); |
| 483 | if (ret < 0) { |
| 484 | dev_err(&client->dev, |
| 485 | "Could not find lltc,resistor-sense in devicetree\n"); |
Mike Looijmans | 377b641 | 2015-07-01 11:40:53 +0200 | [diff] [blame] | 486 | return ret; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 487 | } |
| 488 | info->r_sense = r_sense; |
| 489 | |
| 490 | ret = of_property_read_u32(np, "lltc,prescaler-exponent", |
| 491 | &prescaler_exp); |
| 492 | if (ret < 0) { |
| 493 | dev_warn(&client->dev, |
| 494 | "lltc,prescaler-exponent not in devicetree\n"); |
| 495 | prescaler_exp = LTC2941_MAX_PRESCALER_EXP; |
| 496 | } |
| 497 | |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 498 | if (info->id == LTC2943_ID) { |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 499 | if (prescaler_exp > LTC2943_MAX_PRESCALER_EXP) |
| 500 | prescaler_exp = LTC2943_MAX_PRESCALER_EXP; |
| 501 | info->Qlsb = ((340 * 50000) / r_sense) / |
| 502 | (4096 / (1 << (2*prescaler_exp))); |
| 503 | } else { |
| 504 | if (prescaler_exp > LTC2941_MAX_PRESCALER_EXP) |
| 505 | prescaler_exp = LTC2941_MAX_PRESCALER_EXP; |
Mike Looijmans | 4bf828c | 2015-02-11 12:36:51 +0100 | [diff] [blame] | 506 | info->Qlsb = ((85 * 50000) / r_sense) / |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 507 | (128 / (1 << prescaler_exp)); |
| 508 | } |
| 509 | |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 510 | /* Read status register to check for LTC2942 */ |
| 511 | if (info->id == LTC2941_ID || info->id == LTC2942_ID) { |
| 512 | ret = ltc294x_read_regs(client, LTC294X_REG_STATUS, &status, 1); |
| 513 | if (ret < 0) { |
| 514 | dev_err(&client->dev, |
| 515 | "Could not read status register\n"); |
| 516 | return ret; |
| 517 | } |
| 518 | if (status & LTC2941_REG_STATUS_CHIP_ID) |
| 519 | info->id = LTC2941_ID; |
| 520 | else |
| 521 | info->id = LTC2942_ID; |
| 522 | } |
| 523 | |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 524 | info->client = client; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 525 | info->supply_desc.type = POWER_SUPPLY_TYPE_BATTERY; |
| 526 | info->supply_desc.properties = ltc294x_properties; |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 527 | switch (info->id) { |
Dragos Bogdan | 17825ff | 2017-07-16 11:35:32 +0200 | [diff] [blame] | 528 | case LTC2944_ID: |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 529 | case LTC2943_ID: |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 530 | info->supply_desc.num_properties = |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 531 | ARRAY_SIZE(ltc294x_properties); |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 532 | break; |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 533 | case LTC2942_ID: |
| 534 | info->supply_desc.num_properties = |
| 535 | ARRAY_SIZE(ltc294x_properties) - 1; |
| 536 | break; |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 537 | case LTC2941_ID: |
| 538 | default: |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 539 | info->supply_desc.num_properties = |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 540 | ARRAY_SIZE(ltc294x_properties) - 3; |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 541 | break; |
| 542 | } |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 543 | info->supply_desc.get_property = ltc294x_get_property; |
| 544 | info->supply_desc.set_property = ltc294x_set_property; |
| 545 | info->supply_desc.property_is_writeable = ltc294x_property_is_writeable; |
| 546 | info->supply_desc.external_power_changed = NULL; |
| 547 | |
| 548 | psy_cfg.drv_data = info; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 549 | |
| 550 | INIT_DELAYED_WORK(&info->work, ltc294x_work); |
| 551 | |
| 552 | ret = ltc294x_reset(info, prescaler_exp); |
| 553 | if (ret < 0) { |
| 554 | dev_err(&client->dev, "Communication with chip failed\n"); |
Mike Looijmans | 377b641 | 2015-07-01 11:40:53 +0200 | [diff] [blame] | 555 | return ret; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 558 | info->supply = power_supply_register(&client->dev, &info->supply_desc, |
| 559 | &psy_cfg); |
| 560 | if (IS_ERR(info->supply)) { |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 561 | dev_err(&client->dev, "failed to register ltc2941\n"); |
Mike Looijmans | 377b641 | 2015-07-01 11:40:53 +0200 | [diff] [blame] | 562 | return PTR_ERR(info->supply); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 563 | } else { |
| 564 | schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ); |
| 565 | } |
| 566 | |
| 567 | return 0; |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 568 | } |
| 569 | |
Mike Looijmans | 5f2f0d6 | 2017-11-23 15:41:05 +0100 | [diff] [blame] | 570 | static void ltc294x_i2c_shutdown(struct i2c_client *client) |
| 571 | { |
| 572 | struct ltc294x_info *info = i2c_get_clientdata(client); |
| 573 | int ret; |
| 574 | u8 value; |
| 575 | u8 control; |
| 576 | |
| 577 | /* The LTC2941 does not need any special handling */ |
| 578 | if (info->id == LTC2941_ID) |
| 579 | return; |
| 580 | |
| 581 | /* Read control register */ |
| 582 | ret = ltc294x_read_regs(info->client, LTC294X_REG_CONTROL, &value, 1); |
| 583 | if (ret < 0) |
| 584 | return; |
| 585 | |
| 586 | /* Disable continuous ADC conversion as this drains the battery */ |
| 587 | control = LTC294X_REG_CONTROL_ADC_DISABLE(value); |
| 588 | if (control != value) |
| 589 | ltc294x_write_regs(info->client, LTC294X_REG_CONTROL, |
| 590 | &control, 1); |
| 591 | } |
| 592 | |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 593 | #ifdef CONFIG_PM_SLEEP |
| 594 | |
| 595 | static int ltc294x_suspend(struct device *dev) |
| 596 | { |
| 597 | struct i2c_client *client = to_i2c_client(dev); |
| 598 | struct ltc294x_info *info = i2c_get_clientdata(client); |
| 599 | |
| 600 | cancel_delayed_work(&info->work); |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | static int ltc294x_resume(struct device *dev) |
| 605 | { |
| 606 | struct i2c_client *client = to_i2c_client(dev); |
| 607 | struct ltc294x_info *info = i2c_get_clientdata(client); |
| 608 | |
| 609 | schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ); |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static SIMPLE_DEV_PM_OPS(ltc294x_pm_ops, ltc294x_suspend, ltc294x_resume); |
| 614 | #define LTC294X_PM_OPS (<c294x_pm_ops) |
| 615 | |
| 616 | #else |
| 617 | #define LTC294X_PM_OPS NULL |
| 618 | #endif /* CONFIG_PM_SLEEP */ |
| 619 | |
| 620 | |
| 621 | static const struct i2c_device_id ltc294x_i2c_id[] = { |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 622 | { "ltc2941", LTC2941_ID, }, |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 623 | { "ltc2942", LTC2942_ID, }, |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 624 | { "ltc2943", LTC2943_ID, }, |
Dragos Bogdan | 17825ff | 2017-07-16 11:35:32 +0200 | [diff] [blame] | 625 | { "ltc2944", LTC2944_ID, }, |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 626 | { }, |
| 627 | }; |
| 628 | MODULE_DEVICE_TABLE(i2c, ltc294x_i2c_id); |
| 629 | |
Javier Martinez Canillas | 9697277 | 2017-03-29 12:00:57 -0400 | [diff] [blame] | 630 | static const struct of_device_id ltc294x_i2c_of_match[] = { |
| 631 | { |
| 632 | .compatible = "lltc,ltc2941", |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 633 | .data = (void *)LTC2941_ID, |
Javier Martinez Canillas | 9697277 | 2017-03-29 12:00:57 -0400 | [diff] [blame] | 634 | }, |
| 635 | { |
Ladislav Michl | a65df83 | 2017-07-16 11:34:00 +0200 | [diff] [blame] | 636 | .compatible = "lltc,ltc2942", |
| 637 | .data = (void *)LTC2942_ID, |
| 638 | }, |
| 639 | { |
Javier Martinez Canillas | 9697277 | 2017-03-29 12:00:57 -0400 | [diff] [blame] | 640 | .compatible = "lltc,ltc2943", |
Ladislav Michl | 63e67c5 | 2017-07-16 11:33:06 +0200 | [diff] [blame] | 641 | .data = (void *)LTC2943_ID, |
Javier Martinez Canillas | 9697277 | 2017-03-29 12:00:57 -0400 | [diff] [blame] | 642 | }, |
Dragos Bogdan | 17825ff | 2017-07-16 11:35:32 +0200 | [diff] [blame] | 643 | { |
| 644 | .compatible = "lltc,ltc2944", |
| 645 | .data = (void *)LTC2944_ID, |
| 646 | }, |
Javier Martinez Canillas | 9697277 | 2017-03-29 12:00:57 -0400 | [diff] [blame] | 647 | { }, |
| 648 | }; |
| 649 | MODULE_DEVICE_TABLE(of, ltc294x_i2c_of_match); |
| 650 | |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 651 | static struct i2c_driver ltc294x_driver = { |
| 652 | .driver = { |
| 653 | .name = "LTC2941", |
Javier Martinez Canillas | 9697277 | 2017-03-29 12:00:57 -0400 | [diff] [blame] | 654 | .of_match_table = ltc294x_i2c_of_match, |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 655 | .pm = LTC294X_PM_OPS, |
| 656 | }, |
| 657 | .probe = ltc294x_i2c_probe, |
| 658 | .remove = ltc294x_i2c_remove, |
Mike Looijmans | 5f2f0d6 | 2017-11-23 15:41:05 +0100 | [diff] [blame] | 659 | .shutdown = ltc294x_i2c_shutdown, |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 660 | .id_table = ltc294x_i2c_id, |
| 661 | }; |
| 662 | module_i2c_driver(ltc294x_driver); |
| 663 | |
| 664 | MODULE_AUTHOR("Auryn Verwegen, Topic Embedded Systems"); |
| 665 | MODULE_AUTHOR("Mike Looijmans, Topic Embedded Products"); |
Dragos Bogdan | 17825ff | 2017-07-16 11:35:32 +0200 | [diff] [blame] | 666 | MODULE_DESCRIPTION("LTC2941/LTC2942/LTC2943/LTC2944 Battery Gas Gauge IC driver"); |
Mike Looijmans | 085bc24 | 2014-10-28 08:05:12 +0100 | [diff] [blame] | 667 | MODULE_LICENSE("GPL"); |