blob: 10cd617516ec26168270aff95af4524aec3014e7 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Mike Looijmans085bc242014-10-28 08:05:12 +01002/*
Dragos Bogdan17825ff2017-07-16 11:35:32 +02003 * I2C client/driver for the Linear Technology LTC2941, LTC2942, LTC2943
4 * and LTC2944 Battery Gas Gauge IC
Mike Looijmans085bc242014-10-28 08:05:12 +01005 *
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 Canillas96972772017-03-29 12:00:57 -040013#include <linux/of_device.h>
Mike Looijmans085bc242014-10-28 08:05:12 +010014#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 Looijmans085bc242014-10-28 08:05:12 +010019#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
33enum 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 Michl3bf4e032018-01-17 20:48:54 +010038 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 Michl63e67c52017-07-16 11:33:06 +020042 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
52enum ltc294x_id {
53 LTC2941_ID,
Ladislav Michla65df832017-07-16 11:34:00 +020054 LTC2942_ID,
Ladislav Michl63e67c52017-07-16 11:33:06 +020055 LTC2943_ID,
Dragos Bogdan17825ff2017-07-16 11:35:32 +020056 LTC2944_ID,
Mike Looijmans085bc242014-10-28 08:05:12 +010057};
58
Ladislav Michla65df832017-07-16 11:34:00 +020059#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 Looijmans085bc242014-10-28 08:05:12 +010063#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 Looijmans5f2f0d62017-11-23 15:41:05 +010068#define LTC294X_REG_CONTROL_ADC_DISABLE(x) ((x) & ~(BIT(7) | BIT(6)))
Mike Looijmans085bc242014-10-28 08:05:12 +010069
Mike Looijmans085bc242014-10-28 08:05:12 +010070struct ltc294x_info {
71 struct i2c_client *client; /* I2C Client pointer */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010072 struct power_supply *supply; /* Supply pointer */
73 struct power_supply_desc supply_desc; /* Supply description */
Mike Looijmans085bc242014-10-28 08:05:12 +010074 struct delayed_work work; /* Work scheduler */
Ladislav Michl63e67c52017-07-16 11:33:06 +020075 enum ltc294x_id id; /* Chip type */
Mike Looijmans085bc242014-10-28 08:05:12 +010076 int charge; /* Last charge register content */
77 int r_sense; /* mOhm */
78 int Qlsb; /* nAh */
79};
80
Mike Looijmans085bc242014-10-28 08:05:12 +010081static inline int convert_bin_to_uAh(
82 const struct ltc294x_info *info, int Q)
83{
84 return ((Q * (info->Qlsb / 10))) / 100;
85}
86
87static 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
96static 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 = &reg_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
124static 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
142static 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 Michla65df832017-07-16 11:34:00 +0200158 /* 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 Bogdan17825ff2017-07-16 11:35:32 +0200163 case LTC2943_ID:
164 case LTC2944_ID: /* 2943 and 2944 measure every 10 sec */
Mike Looijmans085bc242014-10-28 08:05:12 +0100165 control |= LTC2943_REG_CONTROL_MODE_SCAN;
Ladislav Michla65df832017-07-16 11:34:00 +0200166 break;
167 default:
168 break;
169 }
Mike Looijmans085bc242014-10-28 08:05:12 +0100170
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
183error_exit:
184 return ret;
185}
186
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100187static int ltc294x_read_charge_register(const struct ltc294x_info *info,
188 enum ltc294x_reg reg)
189 {
Mike Looijmans085bc242014-10-28 08:05:12 +0100190 int ret;
191 u8 datar[2];
192
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100193 ret = ltc294x_read_regs(info->client, reg, &datar[0], 2);
Mike Looijmans085bc242014-10-28 08:05:12 +0100194 if (ret < 0)
195 return ret;
196 return (datar[0] << 8) + datar[1];
197}
198
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100199static int ltc294x_get_charge(const struct ltc294x_info *info,
200 enum ltc294x_reg reg, int *val)
Mike Looijmans085bc242014-10-28 08:05:12 +0100201{
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100202 int value = ltc294x_read_charge_register(info, reg);
Mike Looijmans085bc242014-10-28 08:05:12 +0100203
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
213static 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 */
246error_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 Michl3bf4e032018-01-17 20:48:54 +0100254static 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 Looijmans085bc242014-10-28 08:05:12 +0100273static int ltc294x_get_charge_counter(
274 const struct ltc294x_info *info, int *val)
275{
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100276 int value = ltc294x_read_charge_register(info, LTC294X_REG_ACC_CHARGE_MSB);
Mike Looijmans085bc242014-10-28 08:05:12 +0100277
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
285static 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 Michla65df832017-07-16 11:34:00 +0200294 switch (info->id) {
295 case LTC2943_ID:
296 value *= 23600 * 2;
297 value /= 0xFFFF;
298 value *= 1000 / 2;
299 break;
Dragos Bogdan17825ff2017-07-16 11:35:32 +0200300 case LTC2944_ID:
301 value *= 70800 / 5*4;
302 value /= 0xFFFF;
303 value *= 1000 * 5/4;
304 break;
Ladislav Michla65df832017-07-16 11:34:00 +0200305 default:
306 value *= 6000 * 10;
307 value /= 0xFFFF;
308 value *= 1000 / 10;
309 break;
310 }
311 *val = value;
Mike Looijmans085bc242014-10-28 08:05:12 +0100312 return ret;
313}
314
315static 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 Michl63e67c52017-07-16 11:33:06 +0200322 LTC2943_REG_CURRENT_MSB, &datar[0], 2);
Mike Looijmans085bc242014-10-28 08:05:12 +0100323 value = (datar[0] << 8) | datar[1];
324 value -= 0x7FFF;
Dragos Bogdan17825ff2017-07-16 11:35:32 +0200325 if (info->id == LTC2944_ID)
326 value *= 64000;
327 else
328 value *= 60000;
Mike Looijmans085bc242014-10-28 08:05:12 +0100329 /* 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 Bogdan17825ff2017-07-16 11:35:32 +0200332 *val = 1000 * (value / (info->r_sense * 0x7FFF)); /* in uA */
Mike Looijmans085bc242014-10-28 08:05:12 +0100333 return ret;
334}
335
336static int ltc294x_get_temperature(const struct ltc294x_info *info, int *val)
337{
Ladislav Michla65df832017-07-16 11:34:00 +0200338 enum ltc294x_reg reg;
Mike Looijmans085bc242014-10-28 08:05:12 +0100339 int ret;
340 u8 datar[2];
341 u32 value;
342
Ladislav Michla65df832017-07-16 11:34:00 +0200343 if (info->id == LTC2942_ID) {
344 reg = LTC2942_REG_TEMPERATURE_MSB;
Ladislav Michldde59532018-02-22 18:21:36 +0100345 value = 6000; /* Full-scale is 600 Kelvin */
Ladislav Michla65df832017-07-16 11:34:00 +0200346 } else {
347 reg = LTC2943_REG_TEMPERATURE_MSB;
Ladislav Michldde59532018-02-22 18:21:36 +0100348 value = 5100; /* Full-scale is 510 Kelvin */
Ladislav Michla65df832017-07-16 11:34:00 +0200349 }
350 ret = ltc294x_read_regs(info->client, reg, &datar[0], 2);
351 value *= (datar[0] << 8) | datar[1];
Ladislav Michldde59532018-02-22 18:21:36 +0100352 /* Convert to tenths of degree Celsius */
353 *val = value / 0xFFFF - 2722;
Mike Looijmans085bc242014-10-28 08:05:12 +0100354 return ret;
355}
356
357static int ltc294x_get_property(struct power_supply *psy,
358 enum power_supply_property prop,
359 union power_supply_propval *val)
360{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100361 struct ltc294x_info *info = power_supply_get_drvdata(psy);
Mike Looijmans085bc242014-10-28 08:05:12 +0100362
363 switch (prop) {
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100364 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 Looijmans085bc242014-10-28 08:05:12 +0100370 case POWER_SUPPLY_PROP_CHARGE_NOW:
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100371 return ltc294x_get_charge(info, LTC294X_REG_ACC_CHARGE_MSB,
372 &val->intval);
Mike Looijmans085bc242014-10-28 08:05:12 +0100373 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
386static int ltc294x_set_property(struct power_supply *psy,
387 enum power_supply_property psp,
388 const union power_supply_propval *val)
389{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100390 struct ltc294x_info *info = power_supply_get_drvdata(psy);
Mike Looijmans085bc242014-10-28 08:05:12 +0100391
392 switch (psp) {
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100393 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 Looijmans085bc242014-10-28 08:05:12 +0100399 case POWER_SUPPLY_PROP_CHARGE_NOW:
400 return ltc294x_set_charge_now(info, val->intval);
401 default:
402 return -EPERM;
403 }
404}
405
406static int ltc294x_property_is_writeable(
407 struct power_supply *psy, enum power_supply_property psp)
408{
409 switch (psp) {
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100410 case POWER_SUPPLY_PROP_CHARGE_FULL:
411 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
Mike Looijmans085bc242014-10-28 08:05:12 +0100412 case POWER_SUPPLY_PROP_CHARGE_NOW:
413 return 1;
414 default:
415 return 0;
416 }
417}
418
419static void ltc294x_update(struct ltc294x_info *info)
420{
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100421 int charge = ltc294x_read_charge_register(info, LTC294X_REG_ACC_CHARGE_MSB);
Mike Looijmans085bc242014-10-28 08:05:12 +0100422
423 if (charge != info->charge) {
424 info->charge = charge;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100425 power_supply_changed(info->supply);
Mike Looijmans085bc242014-10-28 08:05:12 +0100426 }
427}
428
429static 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
438static enum power_supply_property ltc294x_properties[] = {
439 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Ladislav Michl3bf4e032018-01-17 20:48:54 +0100440 POWER_SUPPLY_PROP_CHARGE_FULL,
441 POWER_SUPPLY_PROP_CHARGE_EMPTY,
Mike Looijmans085bc242014-10-28 08:05:12 +0100442 POWER_SUPPLY_PROP_CHARGE_NOW,
443 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Mike Looijmans085bc242014-10-28 08:05:12 +0100444 POWER_SUPPLY_PROP_TEMP,
Ladislav Michl63e67c52017-07-16 11:33:06 +0200445 POWER_SUPPLY_PROP_CURRENT_NOW,
Mike Looijmans085bc242014-10-28 08:05:12 +0100446};
447
448static int ltc294x_i2c_remove(struct i2c_client *client)
449{
450 struct ltc294x_info *info = i2c_get_clientdata(client);
451
Sven Van Asbroecka60ec782019-09-19 11:11:37 -0400452 cancel_delayed_work_sync(&info->work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100453 power_supply_unregister(info->supply);
Mike Looijmans085bc242014-10-28 08:05:12 +0100454 return 0;
455}
456
457static int ltc294x_i2c_probe(struct i2c_client *client,
458 const struct i2c_device_id *id)
459{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100460 struct power_supply_config psy_cfg = {};
Mike Looijmans085bc242014-10-28 08:05:12 +0100461 struct ltc294x_info *info;
Ladislav Michla65df832017-07-16 11:34:00 +0200462 struct device_node *np;
Mike Looijmans085bc242014-10-28 08:05:12 +0100463 int ret;
Mike Looijmans085bc242014-10-28 08:05:12 +0100464 u32 prescaler_exp;
465 s32 r_sense;
Ladislav Michla65df832017-07-16 11:34:00 +0200466 u8 status;
Mike Looijmans085bc242014-10-28 08:05:12 +0100467
Mike Looijmans085bc242014-10-28 08:05:12 +0100468 info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
Mike Looijmans377b6412015-07-01 11:40:53 +0200469 if (info == NULL)
470 return -ENOMEM;
Mike Looijmans085bc242014-10-28 08:05:12 +0100471
472 i2c_set_clientdata(client, info);
473
Mike Looijmans085bc242014-10-28 08:05:12 +0100474 np = of_node_get(client->dev.of_node);
475
Iskren Chernev58d16202020-10-10 09:55:26 +0300476 info->id = (enum ltc294x_id) (uintptr_t) of_device_get_match_data(
477 &client->dev);
Mike Looijmans377b6412015-07-01 11:40:53 +0200478 info->supply_desc.name = np->name;
479
Mike Looijmans085bc242014-10-28 08:05:12 +0100480 /* 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 Looijmans377b6412015-07-01 11:40:53 +0200486 return ret;
Mike Looijmans085bc242014-10-28 08:05:12 +0100487 }
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 Michl63e67c52017-07-16 11:33:06 +0200498 if (info->id == LTC2943_ID) {
Mike Looijmans085bc242014-10-28 08:05:12 +0100499 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 Looijmans4bf828c2015-02-11 12:36:51 +0100506 info->Qlsb = ((85 * 50000) / r_sense) /
Mike Looijmans085bc242014-10-28 08:05:12 +0100507 (128 / (1 << prescaler_exp));
508 }
509
Ladislav Michla65df832017-07-16 11:34:00 +0200510 /* 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 Looijmans085bc242014-10-28 08:05:12 +0100524 info->client = client;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100525 info->supply_desc.type = POWER_SUPPLY_TYPE_BATTERY;
526 info->supply_desc.properties = ltc294x_properties;
Ladislav Michl63e67c52017-07-16 11:33:06 +0200527 switch (info->id) {
Dragos Bogdan17825ff2017-07-16 11:35:32 +0200528 case LTC2944_ID:
Ladislav Michl63e67c52017-07-16 11:33:06 +0200529 case LTC2943_ID:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100530 info->supply_desc.num_properties =
Mike Looijmans085bc242014-10-28 08:05:12 +0100531 ARRAY_SIZE(ltc294x_properties);
Ladislav Michl63e67c52017-07-16 11:33:06 +0200532 break;
Ladislav Michla65df832017-07-16 11:34:00 +0200533 case LTC2942_ID:
534 info->supply_desc.num_properties =
535 ARRAY_SIZE(ltc294x_properties) - 1;
536 break;
Ladislav Michl63e67c52017-07-16 11:33:06 +0200537 case LTC2941_ID:
538 default:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100539 info->supply_desc.num_properties =
Mike Looijmans085bc242014-10-28 08:05:12 +0100540 ARRAY_SIZE(ltc294x_properties) - 3;
Ladislav Michl63e67c52017-07-16 11:33:06 +0200541 break;
542 }
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100543 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 Looijmans085bc242014-10-28 08:05:12 +0100549
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 Looijmans377b6412015-07-01 11:40:53 +0200555 return ret;
Mike Looijmans085bc242014-10-28 08:05:12 +0100556 }
557
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100558 info->supply = power_supply_register(&client->dev, &info->supply_desc,
559 &psy_cfg);
560 if (IS_ERR(info->supply)) {
Mike Looijmans085bc242014-10-28 08:05:12 +0100561 dev_err(&client->dev, "failed to register ltc2941\n");
Mike Looijmans377b6412015-07-01 11:40:53 +0200562 return PTR_ERR(info->supply);
Mike Looijmans085bc242014-10-28 08:05:12 +0100563 } else {
564 schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
565 }
566
567 return 0;
Mike Looijmans085bc242014-10-28 08:05:12 +0100568}
569
Mike Looijmans5f2f0d62017-11-23 15:41:05 +0100570static 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 Looijmans085bc242014-10-28 08:05:12 +0100593#ifdef CONFIG_PM_SLEEP
594
595static 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
604static 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
613static SIMPLE_DEV_PM_OPS(ltc294x_pm_ops, ltc294x_suspend, ltc294x_resume);
614#define LTC294X_PM_OPS (&ltc294x_pm_ops)
615
616#else
617#define LTC294X_PM_OPS NULL
618#endif /* CONFIG_PM_SLEEP */
619
620
621static const struct i2c_device_id ltc294x_i2c_id[] = {
Ladislav Michl63e67c52017-07-16 11:33:06 +0200622 { "ltc2941", LTC2941_ID, },
Ladislav Michla65df832017-07-16 11:34:00 +0200623 { "ltc2942", LTC2942_ID, },
Ladislav Michl63e67c52017-07-16 11:33:06 +0200624 { "ltc2943", LTC2943_ID, },
Dragos Bogdan17825ff2017-07-16 11:35:32 +0200625 { "ltc2944", LTC2944_ID, },
Mike Looijmans085bc242014-10-28 08:05:12 +0100626 { },
627};
628MODULE_DEVICE_TABLE(i2c, ltc294x_i2c_id);
629
Javier Martinez Canillas96972772017-03-29 12:00:57 -0400630static const struct of_device_id ltc294x_i2c_of_match[] = {
631 {
632 .compatible = "lltc,ltc2941",
Ladislav Michl63e67c52017-07-16 11:33:06 +0200633 .data = (void *)LTC2941_ID,
Javier Martinez Canillas96972772017-03-29 12:00:57 -0400634 },
635 {
Ladislav Michla65df832017-07-16 11:34:00 +0200636 .compatible = "lltc,ltc2942",
637 .data = (void *)LTC2942_ID,
638 },
639 {
Javier Martinez Canillas96972772017-03-29 12:00:57 -0400640 .compatible = "lltc,ltc2943",
Ladislav Michl63e67c52017-07-16 11:33:06 +0200641 .data = (void *)LTC2943_ID,
Javier Martinez Canillas96972772017-03-29 12:00:57 -0400642 },
Dragos Bogdan17825ff2017-07-16 11:35:32 +0200643 {
644 .compatible = "lltc,ltc2944",
645 .data = (void *)LTC2944_ID,
646 },
Javier Martinez Canillas96972772017-03-29 12:00:57 -0400647 { },
648};
649MODULE_DEVICE_TABLE(of, ltc294x_i2c_of_match);
650
Mike Looijmans085bc242014-10-28 08:05:12 +0100651static struct i2c_driver ltc294x_driver = {
652 .driver = {
653 .name = "LTC2941",
Javier Martinez Canillas96972772017-03-29 12:00:57 -0400654 .of_match_table = ltc294x_i2c_of_match,
Mike Looijmans085bc242014-10-28 08:05:12 +0100655 .pm = LTC294X_PM_OPS,
656 },
657 .probe = ltc294x_i2c_probe,
658 .remove = ltc294x_i2c_remove,
Mike Looijmans5f2f0d62017-11-23 15:41:05 +0100659 .shutdown = ltc294x_i2c_shutdown,
Mike Looijmans085bc242014-10-28 08:05:12 +0100660 .id_table = ltc294x_i2c_id,
661};
662module_i2c_driver(ltc294x_driver);
663
664MODULE_AUTHOR("Auryn Verwegen, Topic Embedded Systems");
665MODULE_AUTHOR("Mike Looijmans, Topic Embedded Products");
Dragos Bogdan17825ff2017-07-16 11:35:32 +0200666MODULE_DESCRIPTION("LTC2941/LTC2942/LTC2943/LTC2944 Battery Gas Gauge IC driver");
Mike Looijmans085bc242014-10-28 08:05:12 +0100667MODULE_LICENSE("GPL");