blob: daeb0860736c1d5a954eda63f1a87d716c1b6832 [file] [log] [blame]
Mike Looijmans085bc242014-10-28 08:05:12 +01001/*
2 * I2C client/driver for the Linear Technology LTC2941 and LTC2943
3 * Battery Gas Gauge IC
4 *
5 * Copyright (C) 2014 Topic Embedded Systems
6 *
7 * Author: Auryn Verwegen
8 * Author: Mike Looijmans
9 */
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/swab.h>
15#include <linux/i2c.h>
16#include <linux/delay.h>
17#include <linux/idr.h>
18#include <linux/power_supply.h>
19#include <linux/slab.h>
20
21#define I16_MSB(x) ((x >> 8) & 0xFF)
22#define I16_LSB(x) (x & 0xFF)
23
24#define LTC294X_WORK_DELAY 10 /* Update delay in seconds */
25
26#define LTC294X_MAX_VALUE 0xFFFF
27#define LTC294X_MID_SUPPLY 0x7FFF
28
29#define LTC2941_MAX_PRESCALER_EXP 7
30#define LTC2943_MAX_PRESCALER_EXP 6
31
32enum ltc294x_reg {
33 LTC294X_REG_STATUS = 0x00,
34 LTC294X_REG_CONTROL = 0x01,
35 LTC294X_REG_ACC_CHARGE_MSB = 0x02,
36 LTC294X_REG_ACC_CHARGE_LSB = 0x03,
37 LTC294X_REG_THRESH_HIGH_MSB = 0x04,
38 LTC294X_REG_THRESH_HIGH_LSB = 0x05,
39 LTC294X_REG_THRESH_LOW_MSB = 0x06,
40 LTC294X_REG_THRESH_LOW_LSB = 0x07,
41 LTC294X_REG_VOLTAGE_MSB = 0x08,
42 LTC294X_REG_VOLTAGE_LSB = 0x09,
43 LTC294X_REG_CURRENT_MSB = 0x0E,
44 LTC294X_REG_CURRENT_LSB = 0x0F,
45 LTC294X_REG_TEMPERATURE_MSB = 0x14,
46 LTC294X_REG_TEMPERATURE_LSB = 0x15,
47};
48
49#define LTC2943_REG_CONTROL_MODE_MASK (BIT(7) | BIT(6))
50#define LTC2943_REG_CONTROL_MODE_SCAN BIT(7)
51#define LTC294X_REG_CONTROL_PRESCALER_MASK (BIT(5) | BIT(4) | BIT(3))
52#define LTC294X_REG_CONTROL_SHUTDOWN_MASK (BIT(0))
53#define LTC294X_REG_CONTROL_PRESCALER_SET(x) \
54 ((x << 3) & LTC294X_REG_CONTROL_PRESCALER_MASK)
55#define LTC294X_REG_CONTROL_ALCC_CONFIG_DISABLED 0
56
57#define LTC2941_NUM_REGS 0x08
58#define LTC2943_NUM_REGS 0x18
59
60struct ltc294x_info {
61 struct i2c_client *client; /* I2C Client pointer */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010062 struct power_supply *supply; /* Supply pointer */
63 struct power_supply_desc supply_desc; /* Supply description */
Mike Looijmans085bc242014-10-28 08:05:12 +010064 struct delayed_work work; /* Work scheduler */
65 int num_regs; /* Number of registers (chip type) */
66 int id; /* Identifier of ltc294x chip */
67 int charge; /* Last charge register content */
68 int r_sense; /* mOhm */
69 int Qlsb; /* nAh */
70};
71
72static DEFINE_IDR(ltc294x_id);
73static DEFINE_MUTEX(ltc294x_lock);
74
75static inline int convert_bin_to_uAh(
76 const struct ltc294x_info *info, int Q)
77{
78 return ((Q * (info->Qlsb / 10))) / 100;
79}
80
81static inline int convert_uAh_to_bin(
82 const struct ltc294x_info *info, int uAh)
83{
84 int Q;
85
86 Q = (uAh * 100) / (info->Qlsb/10);
87 return (Q < LTC294X_MAX_VALUE) ? Q : LTC294X_MAX_VALUE;
88}
89
90static int ltc294x_read_regs(struct i2c_client *client,
91 enum ltc294x_reg reg, u8 *buf, int num_regs)
92{
93 int ret;
94 struct i2c_msg msgs[2] = { };
95 u8 reg_start = reg;
96
97 msgs[0].addr = client->addr;
98 msgs[0].len = 1;
99 msgs[0].buf = &reg_start;
100
101 msgs[1].addr = client->addr;
102 msgs[1].len = num_regs;
103 msgs[1].buf = buf;
104 msgs[1].flags = I2C_M_RD;
105
106 ret = i2c_transfer(client->adapter, &msgs[0], 2);
107 if (ret < 0) {
108 dev_err(&client->dev, "ltc2941 read_reg failed!\n");
109 return ret;
110 }
111
112 dev_dbg(&client->dev, "%s (%#x, %d) -> %#x\n",
113 __func__, reg, num_regs, *buf);
114
115 return 0;
116}
117
118static int ltc294x_write_regs(struct i2c_client *client,
119 enum ltc294x_reg reg, const u8 *buf, int num_regs)
120{
121 int ret;
122 u8 reg_start = reg;
123
124 ret = i2c_smbus_write_i2c_block_data(client, reg_start, num_regs, buf);
125 if (ret < 0) {
126 dev_err(&client->dev, "ltc2941 write_reg failed!\n");
127 return ret;
128 }
129
130 dev_dbg(&client->dev, "%s (%#x, %d) -> %#x\n",
131 __func__, reg, num_regs, *buf);
132
133 return 0;
134}
135
136static int ltc294x_reset(const struct ltc294x_info *info, int prescaler_exp)
137{
138 int ret;
139 u8 value;
140 u8 control;
141
142 /* Read status and control registers */
143 ret = ltc294x_read_regs(info->client, LTC294X_REG_CONTROL, &value, 1);
144 if (ret < 0) {
145 dev_err(&info->client->dev,
146 "Could not read registers from device\n");
147 goto error_exit;
148 }
149
150 control = LTC294X_REG_CONTROL_PRESCALER_SET(prescaler_exp) |
151 LTC294X_REG_CONTROL_ALCC_CONFIG_DISABLED;
152 /* Put the 2943 into "monitor" mode, so it measures every 10 sec */
153 if (info->num_regs == LTC2943_NUM_REGS)
154 control |= LTC2943_REG_CONTROL_MODE_SCAN;
155
156 if (value != control) {
157 ret = ltc294x_write_regs(info->client,
158 LTC294X_REG_CONTROL, &control, 1);
159 if (ret < 0) {
160 dev_err(&info->client->dev,
161 "Could not write register\n");
162 goto error_exit;
163 }
164 }
165
166 return 0;
167
168error_exit:
169 return ret;
170}
171
172static int ltc294x_read_charge_register(const struct ltc294x_info *info)
173{
174 int ret;
175 u8 datar[2];
176
177 ret = ltc294x_read_regs(info->client,
178 LTC294X_REG_ACC_CHARGE_MSB, &datar[0], 2);
179 if (ret < 0)
180 return ret;
181 return (datar[0] << 8) + datar[1];
182}
183
184static int ltc294x_get_charge_now(const struct ltc294x_info *info, int *val)
185{
186 int value = ltc294x_read_charge_register(info);
187
188 if (value < 0)
189 return value;
190 /* When r_sense < 0, this counts up when the battery discharges */
191 if (info->Qlsb < 0)
192 value -= 0xFFFF;
193 *val = convert_bin_to_uAh(info, value);
194 return 0;
195}
196
197static int ltc294x_set_charge_now(const struct ltc294x_info *info, int val)
198{
199 int ret;
200 u8 dataw[2];
201 u8 ctrl_reg;
202 s32 value;
203
204 value = convert_uAh_to_bin(info, val);
205 /* Direction depends on how sense+/- were connected */
206 if (info->Qlsb < 0)
207 value += 0xFFFF;
208 if ((value < 0) || (value > 0xFFFF)) /* input validation */
209 return -EINVAL;
210
211 /* Read control register */
212 ret = ltc294x_read_regs(info->client,
213 LTC294X_REG_CONTROL, &ctrl_reg, 1);
214 if (ret < 0)
215 return ret;
216 /* Disable analog section */
217 ctrl_reg |= LTC294X_REG_CONTROL_SHUTDOWN_MASK;
218 ret = ltc294x_write_regs(info->client,
219 LTC294X_REG_CONTROL, &ctrl_reg, 1);
220 if (ret < 0)
221 return ret;
222 /* Set new charge value */
223 dataw[0] = I16_MSB(value);
224 dataw[1] = I16_LSB(value);
225 ret = ltc294x_write_regs(info->client,
226 LTC294X_REG_ACC_CHARGE_MSB, &dataw[0], 2);
227 if (ret < 0)
228 goto error_exit;
229 /* Enable analog section */
230error_exit:
231 ctrl_reg &= ~LTC294X_REG_CONTROL_SHUTDOWN_MASK;
232 ret = ltc294x_write_regs(info->client,
233 LTC294X_REG_CONTROL, &ctrl_reg, 1);
234
235 return ret < 0 ? ret : 0;
236}
237
238static int ltc294x_get_charge_counter(
239 const struct ltc294x_info *info, int *val)
240{
241 int value = ltc294x_read_charge_register(info);
242
243 if (value < 0)
244 return value;
245 value -= LTC294X_MID_SUPPLY;
246 *val = convert_bin_to_uAh(info, value);
247 return 0;
248}
249
250static int ltc294x_get_voltage(const struct ltc294x_info *info, int *val)
251{
252 int ret;
253 u8 datar[2];
254 u32 value;
255
256 ret = ltc294x_read_regs(info->client,
257 LTC294X_REG_VOLTAGE_MSB, &datar[0], 2);
258 value = (datar[0] << 8) | datar[1];
259 *val = ((value * 23600) / 0xFFFF) * 1000; /* in uV */
260 return ret;
261}
262
263static int ltc294x_get_current(const struct ltc294x_info *info, int *val)
264{
265 int ret;
266 u8 datar[2];
267 s32 value;
268
269 ret = ltc294x_read_regs(info->client,
270 LTC294X_REG_CURRENT_MSB, &datar[0], 2);
271 value = (datar[0] << 8) | datar[1];
272 value -= 0x7FFF;
273 /* Value is in range -32k..+32k, r_sense is usually 10..50 mOhm,
274 * the formula below keeps everything in s32 range while preserving
275 * enough digits */
276 *val = 1000 * ((60000 * value) / (info->r_sense * 0x7FFF)); /* in uA */
277 return ret;
278}
279
280static int ltc294x_get_temperature(const struct ltc294x_info *info, int *val)
281{
282 int ret;
283 u8 datar[2];
284 u32 value;
285
286 ret = ltc294x_read_regs(info->client,
287 LTC294X_REG_TEMPERATURE_MSB, &datar[0], 2);
288 value = (datar[0] << 8) | datar[1];
289 /* Full-scale is 510 Kelvin, convert to centidegrees */
290 *val = (((51000 * value) / 0xFFFF) - 27215);
291 return ret;
292}
293
294static int ltc294x_get_property(struct power_supply *psy,
295 enum power_supply_property prop,
296 union power_supply_propval *val)
297{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100298 struct ltc294x_info *info = power_supply_get_drvdata(psy);
Mike Looijmans085bc242014-10-28 08:05:12 +0100299
300 switch (prop) {
301 case POWER_SUPPLY_PROP_CHARGE_NOW:
302 return ltc294x_get_charge_now(info, &val->intval);
303 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
304 return ltc294x_get_charge_counter(info, &val->intval);
305 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
306 return ltc294x_get_voltage(info, &val->intval);
307 case POWER_SUPPLY_PROP_CURRENT_NOW:
308 return ltc294x_get_current(info, &val->intval);
309 case POWER_SUPPLY_PROP_TEMP:
310 return ltc294x_get_temperature(info, &val->intval);
311 default:
312 return -EINVAL;
313 }
314}
315
316static int ltc294x_set_property(struct power_supply *psy,
317 enum power_supply_property psp,
318 const union power_supply_propval *val)
319{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100320 struct ltc294x_info *info = power_supply_get_drvdata(psy);
Mike Looijmans085bc242014-10-28 08:05:12 +0100321
322 switch (psp) {
323 case POWER_SUPPLY_PROP_CHARGE_NOW:
324 return ltc294x_set_charge_now(info, val->intval);
325 default:
326 return -EPERM;
327 }
328}
329
330static int ltc294x_property_is_writeable(
331 struct power_supply *psy, enum power_supply_property psp)
332{
333 switch (psp) {
334 case POWER_SUPPLY_PROP_CHARGE_NOW:
335 return 1;
336 default:
337 return 0;
338 }
339}
340
341static void ltc294x_update(struct ltc294x_info *info)
342{
343 int charge = ltc294x_read_charge_register(info);
344
345 if (charge != info->charge) {
346 info->charge = charge;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100347 power_supply_changed(info->supply);
Mike Looijmans085bc242014-10-28 08:05:12 +0100348 }
349}
350
351static void ltc294x_work(struct work_struct *work)
352{
353 struct ltc294x_info *info;
354
355 info = container_of(work, struct ltc294x_info, work.work);
356 ltc294x_update(info);
357 schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
358}
359
360static enum power_supply_property ltc294x_properties[] = {
361 POWER_SUPPLY_PROP_CHARGE_COUNTER,
362 POWER_SUPPLY_PROP_CHARGE_NOW,
363 POWER_SUPPLY_PROP_VOLTAGE_NOW,
364 POWER_SUPPLY_PROP_CURRENT_NOW,
365 POWER_SUPPLY_PROP_TEMP,
366};
367
368static int ltc294x_i2c_remove(struct i2c_client *client)
369{
370 struct ltc294x_info *info = i2c_get_clientdata(client);
371
372 cancel_delayed_work(&info->work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100373 power_supply_unregister(info->supply);
374 kfree(info->supply_desc.name);
Mike Looijmans085bc242014-10-28 08:05:12 +0100375 mutex_lock(&ltc294x_lock);
376 idr_remove(&ltc294x_id, info->id);
377 mutex_unlock(&ltc294x_lock);
378 return 0;
379}
380
381static int ltc294x_i2c_probe(struct i2c_client *client,
382 const struct i2c_device_id *id)
383{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100384 struct power_supply_config psy_cfg = {};
Mike Looijmans085bc242014-10-28 08:05:12 +0100385 struct ltc294x_info *info;
386 int ret;
387 int num;
388 u32 prescaler_exp;
389 s32 r_sense;
390 struct device_node *np;
391
392 mutex_lock(&ltc294x_lock);
393 ret = idr_alloc(&ltc294x_id, client, 0, 0, GFP_KERNEL);
394 mutex_unlock(&ltc294x_lock);
395 if (ret < 0)
396 goto fail_id;
397
398 num = ret;
399
400 info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
401 if (info == NULL) {
402 ret = -ENOMEM;
403 goto fail_info;
404 }
405
406 i2c_set_clientdata(client, info);
407
408 info->num_regs = id->driver_data;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100409 info->supply_desc.name = kasprintf(GFP_KERNEL, "%s-%d", client->name,
410 num);
411 if (!info->supply_desc.name) {
Mike Looijmans085bc242014-10-28 08:05:12 +0100412 ret = -ENOMEM;
413 goto fail_name;
414 }
415
416 np = of_node_get(client->dev.of_node);
417
418 /* r_sense can be negative, when sense+ is connected to the battery
419 * instead of the sense-. This results in reversed measurements. */
420 ret = of_property_read_u32(np, "lltc,resistor-sense", &r_sense);
421 if (ret < 0) {
422 dev_err(&client->dev,
423 "Could not find lltc,resistor-sense in devicetree\n");
424 goto fail_name;
425 }
426 info->r_sense = r_sense;
427
428 ret = of_property_read_u32(np, "lltc,prescaler-exponent",
429 &prescaler_exp);
430 if (ret < 0) {
431 dev_warn(&client->dev,
432 "lltc,prescaler-exponent not in devicetree\n");
433 prescaler_exp = LTC2941_MAX_PRESCALER_EXP;
434 }
435
436 if (info->num_regs == LTC2943_NUM_REGS) {
437 if (prescaler_exp > LTC2943_MAX_PRESCALER_EXP)
438 prescaler_exp = LTC2943_MAX_PRESCALER_EXP;
439 info->Qlsb = ((340 * 50000) / r_sense) /
440 (4096 / (1 << (2*prescaler_exp)));
441 } else {
442 if (prescaler_exp > LTC2941_MAX_PRESCALER_EXP)
443 prescaler_exp = LTC2941_MAX_PRESCALER_EXP;
Mike Looijmans4bf828c2015-02-11 12:36:51 +0100444 info->Qlsb = ((85 * 50000) / r_sense) /
Mike Looijmans085bc242014-10-28 08:05:12 +0100445 (128 / (1 << prescaler_exp));
446 }
447
448 info->client = client;
449 info->id = num;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100450 info->supply_desc.type = POWER_SUPPLY_TYPE_BATTERY;
451 info->supply_desc.properties = ltc294x_properties;
Mike Looijmans085bc242014-10-28 08:05:12 +0100452 if (info->num_regs >= LTC294X_REG_TEMPERATURE_LSB)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100453 info->supply_desc.num_properties =
Mike Looijmans085bc242014-10-28 08:05:12 +0100454 ARRAY_SIZE(ltc294x_properties);
455 else if (info->num_regs >= LTC294X_REG_CURRENT_LSB)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100456 info->supply_desc.num_properties =
Mike Looijmans085bc242014-10-28 08:05:12 +0100457 ARRAY_SIZE(ltc294x_properties) - 1;
458 else if (info->num_regs >= LTC294X_REG_VOLTAGE_LSB)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100459 info->supply_desc.num_properties =
Mike Looijmans085bc242014-10-28 08:05:12 +0100460 ARRAY_SIZE(ltc294x_properties) - 2;
461 else
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100462 info->supply_desc.num_properties =
Mike Looijmans085bc242014-10-28 08:05:12 +0100463 ARRAY_SIZE(ltc294x_properties) - 3;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100464 info->supply_desc.get_property = ltc294x_get_property;
465 info->supply_desc.set_property = ltc294x_set_property;
466 info->supply_desc.property_is_writeable = ltc294x_property_is_writeable;
467 info->supply_desc.external_power_changed = NULL;
468
469 psy_cfg.drv_data = info;
Mike Looijmans085bc242014-10-28 08:05:12 +0100470
471 INIT_DELAYED_WORK(&info->work, ltc294x_work);
472
473 ret = ltc294x_reset(info, prescaler_exp);
474 if (ret < 0) {
475 dev_err(&client->dev, "Communication with chip failed\n");
476 goto fail_comm;
477 }
478
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100479 info->supply = power_supply_register(&client->dev, &info->supply_desc,
480 &psy_cfg);
481 if (IS_ERR(info->supply)) {
Mike Looijmans085bc242014-10-28 08:05:12 +0100482 dev_err(&client->dev, "failed to register ltc2941\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100483 ret = PTR_ERR(info->supply);
Mike Looijmans085bc242014-10-28 08:05:12 +0100484 goto fail_register;
485 } else {
486 schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
487 }
488
489 return 0;
490
491fail_register:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100492 kfree(info->supply_desc.name);
Mike Looijmans085bc242014-10-28 08:05:12 +0100493fail_comm:
494fail_name:
495fail_info:
496 mutex_lock(&ltc294x_lock);
497 idr_remove(&ltc294x_id, num);
498 mutex_unlock(&ltc294x_lock);
499fail_id:
500 return ret;
501}
502
503#ifdef CONFIG_PM_SLEEP
504
505static int ltc294x_suspend(struct device *dev)
506{
507 struct i2c_client *client = to_i2c_client(dev);
508 struct ltc294x_info *info = i2c_get_clientdata(client);
509
510 cancel_delayed_work(&info->work);
511 return 0;
512}
513
514static int ltc294x_resume(struct device *dev)
515{
516 struct i2c_client *client = to_i2c_client(dev);
517 struct ltc294x_info *info = i2c_get_clientdata(client);
518
519 schedule_delayed_work(&info->work, LTC294X_WORK_DELAY * HZ);
520 return 0;
521}
522
523static SIMPLE_DEV_PM_OPS(ltc294x_pm_ops, ltc294x_suspend, ltc294x_resume);
524#define LTC294X_PM_OPS (&ltc294x_pm_ops)
525
526#else
527#define LTC294X_PM_OPS NULL
528#endif /* CONFIG_PM_SLEEP */
529
530
531static const struct i2c_device_id ltc294x_i2c_id[] = {
532 {"ltc2941", LTC2941_NUM_REGS},
533 {"ltc2943", LTC2943_NUM_REGS},
534 { },
535};
536MODULE_DEVICE_TABLE(i2c, ltc294x_i2c_id);
537
538static struct i2c_driver ltc294x_driver = {
539 .driver = {
540 .name = "LTC2941",
541 .pm = LTC294X_PM_OPS,
542 },
543 .probe = ltc294x_i2c_probe,
544 .remove = ltc294x_i2c_remove,
545 .id_table = ltc294x_i2c_id,
546};
547module_i2c_driver(ltc294x_driver);
548
549MODULE_AUTHOR("Auryn Verwegen, Topic Embedded Systems");
550MODULE_AUTHOR("Mike Looijmans, Topic Embedded Products");
551MODULE_DESCRIPTION("LTC2941/LTC2943 Battery Gas Gauge IC driver");
552MODULE_LICENSE("GPL");