power_supply core: support use of devres to register/unregister a power supply.
[deliverable/linux.git] / drivers / power / bq27x00_battery.c
CommitLineData
b996ad0e
RG
1/*
2 * BQ27x00 battery driver
3 *
4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
7fb7ba58 6 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
73c244a8 7 * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
b996ad0e
RG
8 *
9 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
10 *
11 * This package is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 *
631c17ee
PR
19 * Datasheets:
20 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
21 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
a66f59ba 22 * http://www.ti.com/product/bq27425-g1
628ef02c 23 * http://www.ti.com/product/BQ27742-G1
631c17ee
PR
24 */
25
1cb82fdb 26#include <linux/device.h>
b996ad0e
RG
27#include <linux/module.h>
28#include <linux/param.h>
29#include <linux/jiffies.h>
30#include <linux/workqueue.h>
31#include <linux/delay.h>
32#include <linux/platform_device.h>
33#include <linux/power_supply.h>
34#include <linux/idr.h>
b996ad0e 35#include <linux/i2c.h>
5a0e3ad6 36#include <linux/slab.h>
8aef7e8f 37#include <asm/unaligned.h>
b996ad0e 38
7fb7ba58
LPC
39#include <linux/power/bq27x00_battery.h>
40
631c17ee 41#define DRIVER_VERSION "1.2.0"
b996ad0e
RG
42
43#define BQ27x00_REG_TEMP 0x06
44#define BQ27x00_REG_VOLT 0x08
b996ad0e
RG
45#define BQ27x00_REG_AI 0x14
46#define BQ27x00_REG_FLAGS 0x0A
4e924a81
GI
47#define BQ27x00_REG_TTE 0x16
48#define BQ27x00_REG_TTF 0x18
49#define BQ27x00_REG_TTECP 0x26
202e0116 50#define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
631c17ee
PR
51#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
52#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
202e0116 53#define BQ27x00_REG_AE 0x22 /* Available energy */
9903e627 54#define BQ27x00_POWER_AVG 0x24
b996ad0e 55
e20908d9 56#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
631c17ee 57#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
d66bab3f
PR
58#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
59#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
4b226c2c 60#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
c1b9ab67 61#define BQ27000_FLAG_FC BIT(5)
d66bab3f 62#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
e20908d9 63
bf7d4140 64#define BQ27500_REG_SOC 0x2C
631c17ee 65#define BQ27500_REG_DCAP 0x3C /* Design capacity */
b7aaacf5 66#define BQ27500_FLAG_DSC BIT(0)
d66bab3f
PR
67#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
68#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
b7aaacf5 69#define BQ27500_FLAG_FC BIT(9)
9903e627 70#define BQ27500_FLAG_OTC BIT(15)
e20908d9 71
628ef02c
PV
72#define BQ27742_POWER_AVG 0x76
73
a66f59ba
SG
74/* bq27425 register addresses are same as bq27x00 addresses minus 4 */
75#define BQ27425_REG_OFFSET 0x04
9dbf5a28
EB
76#define BQ27425_REG_SOC (0x1C + BQ27425_REG_OFFSET)
77#define BQ27425_REG_DCAP (0x3C + BQ27425_REG_OFFSET)
a66f59ba 78
a2e5118c 79#define BQ27000_RS 20 /* Resistor sense */
9903e627 80#define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
a2e5118c 81
b996ad0e
RG
82struct bq27x00_device_info;
83struct bq27x00_access_methods {
297a533b 84 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
b996ad0e
RG
85};
86
628ef02c 87enum bq27x00_chip { BQ27000, BQ27500, BQ27425, BQ27742};
e20908d9 88
297a533b
LPC
89struct bq27x00_reg_cache {
90 int temperature;
91 int time_to_empty;
92 int time_to_empty_avg;
93 int time_to_full;
631c17ee 94 int charge_full;
73c244a8 95 int cycle_count;
297a533b 96 int capacity;
a8f6bd23 97 int energy;
297a533b 98 int flags;
9903e627
SR
99 int power_avg;
100 int health;
297a533b
LPC
101};
102
b996ad0e
RG
103struct bq27x00_device_info {
104 struct device *dev;
105 int id;
e20908d9 106 enum bq27x00_chip chip;
b996ad0e 107
297a533b 108 struct bq27x00_reg_cache cache;
631c17ee
PR
109 int charge_design_full;
110
297a533b 111 unsigned long last_update;
740b755a 112 struct delayed_work work;
297a533b 113
a40402ef
LPC
114 struct power_supply bat;
115
116 struct bq27x00_access_methods bus;
740b755a
LPC
117
118 struct mutex lock;
b996ad0e
RG
119};
120
121static enum power_supply_property bq27x00_battery_props[] = {
4e924a81 122 POWER_SUPPLY_PROP_STATUS,
b996ad0e
RG
123 POWER_SUPPLY_PROP_PRESENT,
124 POWER_SUPPLY_PROP_VOLTAGE_NOW,
125 POWER_SUPPLY_PROP_CURRENT_NOW,
126 POWER_SUPPLY_PROP_CAPACITY,
d66bab3f 127 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
b996ad0e 128 POWER_SUPPLY_PROP_TEMP,
4e924a81
GI
129 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
130 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
131 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
5661f334 132 POWER_SUPPLY_PROP_TECHNOLOGY,
631c17ee
PR
133 POWER_SUPPLY_PROP_CHARGE_FULL,
134 POWER_SUPPLY_PROP_CHARGE_NOW,
135 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
73c244a8 136 POWER_SUPPLY_PROP_CYCLE_COUNT,
631c17ee 137 POWER_SUPPLY_PROP_ENERGY_NOW,
9903e627
SR
138 POWER_SUPPLY_PROP_POWER_AVG,
139 POWER_SUPPLY_PROP_HEALTH,
b996ad0e
RG
140};
141
a66f59ba
SG
142static enum power_supply_property bq27425_battery_props[] = {
143 POWER_SUPPLY_PROP_STATUS,
144 POWER_SUPPLY_PROP_PRESENT,
145 POWER_SUPPLY_PROP_VOLTAGE_NOW,
146 POWER_SUPPLY_PROP_CURRENT_NOW,
147 POWER_SUPPLY_PROP_CAPACITY,
148 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
149 POWER_SUPPLY_PROP_TEMP,
150 POWER_SUPPLY_PROP_TECHNOLOGY,
151 POWER_SUPPLY_PROP_CHARGE_FULL,
152 POWER_SUPPLY_PROP_CHARGE_NOW,
153 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
154};
155
628ef02c
PV
156static enum power_supply_property bq27742_battery_props[] = {
157 POWER_SUPPLY_PROP_STATUS,
158 POWER_SUPPLY_PROP_PRESENT,
159 POWER_SUPPLY_PROP_VOLTAGE_NOW,
160 POWER_SUPPLY_PROP_CURRENT_NOW,
161 POWER_SUPPLY_PROP_CAPACITY,
162 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
163 POWER_SUPPLY_PROP_TEMP,
164 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
165 POWER_SUPPLY_PROP_TECHNOLOGY,
166 POWER_SUPPLY_PROP_CHARGE_FULL,
167 POWER_SUPPLY_PROP_CHARGE_NOW,
168 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
169 POWER_SUPPLY_PROP_CYCLE_COUNT,
170 POWER_SUPPLY_PROP_POWER_AVG,
171 POWER_SUPPLY_PROP_HEALTH,
172};
173
740b755a
LPC
174static unsigned int poll_interval = 360;
175module_param(poll_interval, uint, 0644);
176MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
177 "0 disables polling");
178
b996ad0e
RG
179/*
180 * Common code for BQ27x00 devices
181 */
182
a40402ef 183static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
297a533b 184 bool single)
b996ad0e 185{
a66f59ba
SG
186 if (di->chip == BQ27425)
187 return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
297a533b 188 return di->bus.read(di, reg, single);
b996ad0e
RG
189}
190
a66f59ba
SG
191/*
192 * Higher versions of the chip like BQ27425 and BQ27500
193 * differ from BQ27000 and BQ27200 in calculation of certain
194 * parameters. Hence we need to check for the chip type.
195 */
196static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di)
197{
628ef02c 198 if (di->chip == BQ27425 || di->chip == BQ27500 || di->chip == BQ27742)
a66f59ba
SG
199 return true;
200 return false;
201}
202
b996ad0e 203/*
297a533b 204 * Return the battery Relative State-of-Charge
b996ad0e
RG
205 * Or < 0 if something fails.
206 */
297a533b 207static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
b996ad0e 208{
297a533b 209 int rsoc;
b996ad0e 210
628ef02c 211 if (di->chip == BQ27500 || di->chip == BQ27742)
297a533b 212 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
a66f59ba
SG
213 else if (di->chip == BQ27425)
214 rsoc = bq27x00_read(di, BQ27425_REG_SOC, false);
e20908d9 215 else
297a533b
LPC
216 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
217
218 if (rsoc < 0)
c6cd4f26 219 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
297a533b
LPC
220
221 return rsoc;
b996ad0e
RG
222}
223
631c17ee
PR
224/*
225 * Return a battery charge value in µAh
226 * Or < 0 if something fails.
227 */
228static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
229{
230 int charge;
231
232 charge = bq27x00_read(di, reg, false);
233 if (charge < 0) {
c6cd4f26
PR
234 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
235 reg, charge);
631c17ee
PR
236 return charge;
237 }
238
a66f59ba 239 if (bq27xxx_is_chip_version_higher(di))
631c17ee
PR
240 charge *= 1000;
241 else
242 charge = charge * 3570 / BQ27000_RS;
243
244 return charge;
245}
246
247/*
248 * Return the battery Nominal available capaciy in µAh
249 * Or < 0 if something fails.
250 */
251static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
252{
e59ec4a1
PR
253 int flags;
254 bool is_bq27500 = di->chip == BQ27500;
a3c0c3e7 255 bool is_bq27742 = di->chip == BQ27742;
e59ec4a1 256 bool is_higher = bq27xxx_is_chip_version_higher(di);
a3c0c3e7 257 bool flags_1b = !(is_bq27500 || is_bq27742);
e59ec4a1 258
a3c0c3e7 259 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
e59ec4a1
PR
260 if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
261 return -ENODATA;
262
631c17ee
PR
263 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
264}
265
266/*
267 * Return the battery Last measured discharge in µAh
268 * Or < 0 if something fails.
269 */
270static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
271{
272 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
273}
274
275/*
276 * Return the battery Initial last measured discharge in µAh
277 * Or < 0 if something fails.
278 */
279static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
280{
281 int ilmd;
282
9dbf5a28
EB
283 if (bq27xxx_is_chip_version_higher(di)) {
284 if (di->chip == BQ27425)
285 ilmd = bq27x00_read(di, BQ27425_REG_DCAP, false);
286 else
287 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
288 } else
631c17ee
PR
289 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
290
291 if (ilmd < 0) {
c6cd4f26 292 dev_dbg(di->dev, "error reading initial last measured discharge\n");
631c17ee
PR
293 return ilmd;
294 }
295
a66f59ba 296 if (bq27xxx_is_chip_version_higher(di))
631c17ee
PR
297 ilmd *= 1000;
298 else
299 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
300
301 return ilmd;
302}
303
a8f6bd23
PR
304/*
305 * Return the battery Available energy in µWh
306 * Or < 0 if something fails.
307 */
308static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
309{
310 int ae;
311
312 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
313 if (ae < 0) {
c6cd4f26 314 dev_dbg(di->dev, "error reading available energy\n");
a8f6bd23
PR
315 return ae;
316 }
317
318 if (di->chip == BQ27500)
319 ae *= 1000;
320 else
321 ae = ae * 29200 / BQ27000_RS;
322
323 return ae;
324}
325
d149e98e 326/*
5dc3443e 327 * Return the battery temperature in tenths of degree Kelvin
d149e98e
PR
328 * Or < 0 if something fails.
329 */
330static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
331{
332 int temp;
333
334 temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
335 if (temp < 0) {
336 dev_err(di->dev, "error reading temperature\n");
337 return temp;
338 }
339
5dc3443e
PR
340 if (!bq27xxx_is_chip_version_higher(di))
341 temp = 5 * temp / 2;
d149e98e
PR
342
343 return temp;
344}
345
631c17ee
PR
346/*
347 * Return the battery Cycle count total
348 * Or < 0 if something fails.
349 */
350static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
351{
352 int cyct;
353
354 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
355 if (cyct < 0)
356 dev_err(di->dev, "error reading cycle count total\n");
357
358 return cyct;
359}
360
b996ad0e 361/*
297a533b
LPC
362 * Read a time register.
363 * Return < 0 if something fails.
b996ad0e 364 */
297a533b 365static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
b996ad0e 366{
297a533b 367 int tval;
b996ad0e 368
297a533b
LPC
369 tval = bq27x00_read(di, reg, false);
370 if (tval < 0) {
c6cd4f26
PR
371 dev_dbg(di->dev, "error reading time register %02x: %d\n",
372 reg, tval);
297a533b 373 return tval;
b996ad0e
RG
374 }
375
297a533b
LPC
376 if (tval == 65535)
377 return -ENODATA;
378
379 return tval * 60;
380}
381
9903e627
SR
382/*
383 * Read a power avg register.
384 * Return < 0 if something fails.
385 */
386static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
387{
388 int tval;
389
390 tval = bq27x00_read(di, reg, false);
391 if (tval < 0) {
392 dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
393 reg, tval);
394 return tval;
395 }
396
397 if (di->chip == BQ27500)
398 return tval;
399 else
400 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
401}
402
403/*
404 * Read flag register.
405 * Return < 0 if something fails.
406 */
407static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
408{
409 int tval;
410
411 tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
412 if (tval < 0) {
413 dev_err(di->dev, "error reading flag register:%d\n", tval);
414 return tval;
415 }
416
417 if ((di->chip == BQ27500)) {
418 if (tval & BQ27500_FLAG_SOCF)
419 tval = POWER_SUPPLY_HEALTH_DEAD;
420 else if (tval & BQ27500_FLAG_OTC)
421 tval = POWER_SUPPLY_HEALTH_OVERHEAT;
422 else
423 tval = POWER_SUPPLY_HEALTH_GOOD;
424 return tval;
425 } else {
426 if (tval & BQ27000_FLAG_EDV1)
427 tval = POWER_SUPPLY_HEALTH_DEAD;
428 else
429 tval = POWER_SUPPLY_HEALTH_GOOD;
430 return tval;
431 }
432
433 return -1;
434}
435
297a533b
LPC
436static void bq27x00_update(struct bq27x00_device_info *di)
437{
438 struct bq27x00_reg_cache cache = {0, };
439 bool is_bq27500 = di->chip == BQ27500;
a66f59ba 440 bool is_bq27425 = di->chip == BQ27425;
628ef02c 441 bool is_bq27742 = di->chip == BQ27742;
a3c0c3e7 442 bool flags_1b = !(is_bq27500 || is_bq27742);
297a533b 443
a3c0c3e7 444 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
3dd843e1
MB
445 if ((cache.flags & 0xff) == 0xff)
446 /* read error */
447 cache.flags = -1;
297a533b 448 if (cache.flags >= 0) {
a3c0c3e7 449 if (!is_bq27500 && !is_bq27425 && !is_bq27742
a66f59ba 450 && (cache.flags & BQ27000_FLAG_CI)) {
c6cd4f26 451 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
4b226c2c 452 cache.capacity = -ENODATA;
a8f6bd23 453 cache.energy = -ENODATA;
4b226c2c
PR
454 cache.time_to_empty = -ENODATA;
455 cache.time_to_empty_avg = -ENODATA;
456 cache.time_to_full = -ENODATA;
457 cache.charge_full = -ENODATA;
9903e627 458 cache.health = -ENODATA;
4b226c2c
PR
459 } else {
460 cache.capacity = bq27x00_battery_read_rsoc(di);
628ef02c
PV
461 if (is_bq27742)
462 cache.time_to_empty =
463 bq27x00_battery_read_time(di,
464 BQ27x00_REG_TTE);
465 else if (!is_bq27425) {
a66f59ba
SG
466 cache.energy = bq27x00_battery_read_energy(di);
467 cache.time_to_empty =
468 bq27x00_battery_read_time(di,
469 BQ27x00_REG_TTE);
470 cache.time_to_empty_avg =
471 bq27x00_battery_read_time(di,
472 BQ27x00_REG_TTECP);
473 cache.time_to_full =
474 bq27x00_battery_read_time(di,
475 BQ27x00_REG_TTF);
476 }
a3c0c3e7 477 cache.charge_full = bq27x00_battery_read_lmd(di);
9903e627 478 cache.health = bq27x00_battery_read_health(di);
4b226c2c 479 }
d149e98e 480 cache.temperature = bq27x00_battery_read_temperature(di);
a66f59ba
SG
481 if (!is_bq27425)
482 cache.cycle_count = bq27x00_battery_read_cyct(di);
628ef02c
PV
483 if (is_bq27742)
484 cache.power_avg =
485 bq27x00_battery_read_pwr_avg(di,
486 BQ27742_POWER_AVG);
487 else
488 cache.power_avg =
489 bq27x00_battery_read_pwr_avg(di,
490 BQ27x00_POWER_AVG);
297a533b 491
631c17ee
PR
492 /* We only have to read charge design full once */
493 if (di->charge_design_full <= 0)
494 di->charge_design_full = bq27x00_battery_read_ilmd(di);
297a533b
LPC
495 }
496
90f04a28 497 if (di->cache.capacity != cache.capacity)
297a533b 498 power_supply_changed(&di->bat);
90f04a28
PV
499
500 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
501 di->cache = cache;
297a533b
LPC
502
503 di->last_update = jiffies;
504}
505
740b755a
LPC
506static void bq27x00_battery_poll(struct work_struct *work)
507{
508 struct bq27x00_device_info *di =
509 container_of(work, struct bq27x00_device_info, work.work);
510
511 bq27x00_update(di);
512
513 if (poll_interval > 0) {
514 /* The timer does not have to be accurate. */
515 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
516 schedule_delayed_work(&di->work, poll_interval * HZ);
517 }
518}
519
b996ad0e 520/*
bf7d4140 521 * Return the battery average current in µA
b996ad0e
RG
522 * Note that current can be negative signed as well
523 * Or 0 if something fails.
524 */
297a533b
LPC
525static int bq27x00_battery_current(struct bq27x00_device_info *di,
526 union power_supply_propval *val)
b996ad0e 527{
297a533b 528 int curr;
b68f6216 529 int flags;
b996ad0e 530
b68f6216 531 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
c6cd4f26
PR
532 if (curr < 0) {
533 dev_err(di->dev, "error reading current\n");
297a533b 534 return curr;
c6cd4f26 535 }
e20908d9 536
a66f59ba 537 if (bq27xxx_is_chip_version_higher(di)) {
e20908d9 538 /* bq27500 returns signed value */
297a533b 539 val->intval = (int)((s16)curr) * 1000;
e20908d9 540 } else {
b68f6216
PR
541 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
542 if (flags & BQ27000_FLAG_CHGS) {
e20908d9 543 dev_dbg(di->dev, "negative current!\n");
afbc74fd 544 curr = -curr;
e20908d9 545 }
b996ad0e 546
297a533b 547 val->intval = curr * 3570 / BQ27000_RS;
b996ad0e
RG
548 }
549
297a533b 550 return 0;
b996ad0e
RG
551}
552
4e924a81 553static int bq27x00_battery_status(struct bq27x00_device_info *di,
297a533b 554 union power_supply_propval *val)
4e924a81 555{
4e924a81 556 int status;
4e924a81 557
a66f59ba 558 if (bq27xxx_is_chip_version_higher(di)) {
297a533b 559 if (di->cache.flags & BQ27500_FLAG_FC)
4e924a81 560 status = POWER_SUPPLY_STATUS_FULL;
b7aaacf5 561 else if (di->cache.flags & BQ27500_FLAG_DSC)
4e924a81 562 status = POWER_SUPPLY_STATUS_DISCHARGING;
270968c0 563 else
b7aaacf5 564 status = POWER_SUPPLY_STATUS_CHARGING;
4e924a81 565 } else {
c1b9ab67
LPC
566 if (di->cache.flags & BQ27000_FLAG_FC)
567 status = POWER_SUPPLY_STATUS_FULL;
568 else if (di->cache.flags & BQ27000_FLAG_CHGS)
4e924a81 569 status = POWER_SUPPLY_STATUS_CHARGING;
c1b9ab67
LPC
570 else if (power_supply_am_i_supplied(&di->bat))
571 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
4e924a81
GI
572 else
573 status = POWER_SUPPLY_STATUS_DISCHARGING;
574 }
575
576 val->intval = status;
297a533b 577
4e924a81
GI
578 return 0;
579}
580
d66bab3f
PR
581static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
582 union power_supply_propval *val)
583{
584 int level;
585
a66f59ba 586 if (bq27xxx_is_chip_version_higher(di)) {
d66bab3f
PR
587 if (di->cache.flags & BQ27500_FLAG_FC)
588 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
589 else if (di->cache.flags & BQ27500_FLAG_SOC1)
590 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
591 else if (di->cache.flags & BQ27500_FLAG_SOCF)
592 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
593 else
594 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
595 } else {
596 if (di->cache.flags & BQ27000_FLAG_FC)
597 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
598 else if (di->cache.flags & BQ27000_FLAG_EDV1)
599 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
600 else if (di->cache.flags & BQ27000_FLAG_EDVF)
601 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
602 else
603 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
604 }
605
606 val->intval = level;
607
608 return 0;
609}
610
4e924a81 611/*
91fe4d50 612 * Return the battery Voltage in millivolts
297a533b 613 * Or < 0 if something fails.
4e924a81 614 */
297a533b
LPC
615static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
616 union power_supply_propval *val)
4e924a81 617{
297a533b 618 int volt;
4e924a81 619
297a533b 620 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
c6cd4f26
PR
621 if (volt < 0) {
622 dev_err(di->dev, "error reading voltage\n");
297a533b 623 return volt;
c6cd4f26 624 }
4e924a81 625
297a533b
LPC
626 val->intval = volt * 1000;
627
628 return 0;
629}
630
631static int bq27x00_simple_value(int value,
632 union power_supply_propval *val)
633{
634 if (value < 0)
635 return value;
636
637 val->intval = value;
4e924a81 638
4e924a81
GI
639 return 0;
640}
641
b996ad0e
RG
642#define to_bq27x00_device_info(x) container_of((x), \
643 struct bq27x00_device_info, bat);
644
645static int bq27x00_battery_get_property(struct power_supply *psy,
646 enum power_supply_property psp,
647 union power_supply_propval *val)
648{
4e924a81 649 int ret = 0;
b996ad0e 650 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
3413b4ea 651
740b755a
LPC
652 mutex_lock(&di->lock);
653 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
654 cancel_delayed_work_sync(&di->work);
655 bq27x00_battery_poll(&di->work.work);
656 }
657 mutex_unlock(&di->lock);
297a533b
LPC
658
659 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
3413b4ea 660 return -ENODEV;
b996ad0e
RG
661
662 switch (psp) {
4e924a81
GI
663 case POWER_SUPPLY_PROP_STATUS:
664 ret = bq27x00_battery_status(di, val);
665 break;
b996ad0e 666 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
297a533b 667 ret = bq27x00_battery_voltage(di, val);
3413b4ea 668 break;
b996ad0e 669 case POWER_SUPPLY_PROP_PRESENT:
297a533b 670 val->intval = di->cache.flags < 0 ? 0 : 1;
b996ad0e
RG
671 break;
672 case POWER_SUPPLY_PROP_CURRENT_NOW:
297a533b 673 ret = bq27x00_battery_current(di, val);
b996ad0e
RG
674 break;
675 case POWER_SUPPLY_PROP_CAPACITY:
297a533b 676 ret = bq27x00_simple_value(di->cache.capacity, val);
b996ad0e 677 break;
d66bab3f
PR
678 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
679 ret = bq27x00_battery_capacity_level(di, val);
680 break;
b996ad0e 681 case POWER_SUPPLY_PROP_TEMP:
d149e98e 682 ret = bq27x00_simple_value(di->cache.temperature, val);
5dc3443e
PR
683 if (ret == 0)
684 val->intval -= 2731;
b996ad0e 685 break;
4e924a81 686 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
297a533b 687 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
4e924a81
GI
688 break;
689 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
297a533b 690 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
4e924a81
GI
691 break;
692 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
297a533b 693 ret = bq27x00_simple_value(di->cache.time_to_full, val);
4e924a81 694 break;
5661f334
LPC
695 case POWER_SUPPLY_PROP_TECHNOLOGY:
696 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
697 break;
631c17ee
PR
698 case POWER_SUPPLY_PROP_CHARGE_NOW:
699 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
700 break;
701 case POWER_SUPPLY_PROP_CHARGE_FULL:
702 ret = bq27x00_simple_value(di->cache.charge_full, val);
703 break;
704 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
705 ret = bq27x00_simple_value(di->charge_design_full, val);
706 break;
73c244a8
PR
707 case POWER_SUPPLY_PROP_CYCLE_COUNT:
708 ret = bq27x00_simple_value(di->cache.cycle_count, val);
631c17ee
PR
709 break;
710 case POWER_SUPPLY_PROP_ENERGY_NOW:
a8f6bd23 711 ret = bq27x00_simple_value(di->cache.energy, val);
631c17ee 712 break;
9903e627
SR
713 case POWER_SUPPLY_PROP_POWER_AVG:
714 ret = bq27x00_simple_value(di->cache.power_avg, val);
715 break;
716 case POWER_SUPPLY_PROP_HEALTH:
717 ret = bq27x00_simple_value(di->cache.health, val);
718 break;
b996ad0e
RG
719 default:
720 return -EINVAL;
721 }
722
4e924a81 723 return ret;
b996ad0e
RG
724}
725
740b755a
LPC
726static void bq27x00_external_power_changed(struct power_supply *psy)
727{
728 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
729
730 cancel_delayed_work_sync(&di->work);
731 schedule_delayed_work(&di->work, 0);
732}
733
a40402ef 734static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
b996ad0e 735{
a40402ef
LPC
736 int ret;
737
b996ad0e 738 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
a66f59ba
SG
739 if (di->chip == BQ27425) {
740 di->bat.properties = bq27425_battery_props;
741 di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props);
628ef02c
PV
742 } else if (di->chip == BQ27742) {
743 di->bat.properties = bq27742_battery_props;
744 di->bat.num_properties = ARRAY_SIZE(bq27742_battery_props);
a66f59ba
SG
745 } else {
746 di->bat.properties = bq27x00_battery_props;
747 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
748 }
b996ad0e 749 di->bat.get_property = bq27x00_battery_get_property;
740b755a
LPC
750 di->bat.external_power_changed = bq27x00_external_power_changed;
751
752 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
753 mutex_init(&di->lock);
a40402ef 754
5cd0c76d 755 ret = power_supply_register_no_ws(di->dev, &di->bat);
a40402ef
LPC
756 if (ret) {
757 dev_err(di->dev, "failed to register battery: %d\n", ret);
758 return ret;
759 }
760
761 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
762
297a533b
LPC
763 bq27x00_update(di);
764
a40402ef 765 return 0;
b996ad0e
RG
766}
767
740b755a
LPC
768static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
769{
8cfaaa81
PR
770 /*
771 * power_supply_unregister call bq27x00_battery_get_property which
772 * call bq27x00_battery_poll.
773 * Make sure that bq27x00_battery_poll will not call
774 * schedule_delayed_work again after unregister (which cause OOPS).
775 */
776 poll_interval = 0;
777
740b755a
LPC
778 cancel_delayed_work_sync(&di->work);
779
780 power_supply_unregister(&di->bat);
781
782 mutex_destroy(&di->lock);
783}
784
7fb7ba58
LPC
785
786/* i2c specific code */
787#ifdef CONFIG_BATTERY_BQ27X00_I2C
788
789/* If the system has several batteries we need a different name for each
790 * of them...
b996ad0e 791 */
7fb7ba58
LPC
792static DEFINE_IDR(battery_id);
793static DEFINE_MUTEX(battery_mutex);
b996ad0e 794
297a533b 795static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
b996ad0e 796{
a40402ef 797 struct i2c_client *client = to_i2c_client(di->dev);
9e912f45 798 struct i2c_msg msg[2];
b996ad0e 799 unsigned char data[2];
297a533b 800 int ret;
b996ad0e
RG
801
802 if (!client->adapter)
803 return -ENODEV;
804
9e912f45
GI
805 msg[0].addr = client->addr;
806 msg[0].flags = 0;
807 msg[0].buf = &reg;
808 msg[0].len = sizeof(reg);
809 msg[1].addr = client->addr;
810 msg[1].flags = I2C_M_RD;
811 msg[1].buf = data;
2ec523a8 812 if (single)
9e912f45 813 msg[1].len = 1;
2ec523a8 814 else
9e912f45 815 msg[1].len = 2;
2ec523a8 816
9e912f45 817 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
2ec523a8
LPC
818 if (ret < 0)
819 return ret;
820
821 if (!single)
822 ret = get_unaligned_le16(data);
823 else
824 ret = data[0];
b996ad0e 825
297a533b 826 return ret;
b996ad0e
RG
827}
828
e20908d9 829static int bq27x00_battery_probe(struct i2c_client *client,
b996ad0e
RG
830 const struct i2c_device_id *id)
831{
832 char *name;
833 struct bq27x00_device_info *di;
b996ad0e
RG
834 int num;
835 int retval = 0;
836
837 /* Get new ID for the new battery device */
b996ad0e 838 mutex_lock(&battery_mutex);
05e2cefa 839 num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
b996ad0e 840 mutex_unlock(&battery_mutex);
05e2cefa
TH
841 if (num < 0)
842 return num;
b996ad0e 843
e20908d9 844 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
b996ad0e
RG
845 if (!name) {
846 dev_err(&client->dev, "failed to allocate device name\n");
847 retval = -ENOMEM;
848 goto batt_failed_1;
849 }
850
1cb82fdb 851 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
b996ad0e
RG
852 if (!di) {
853 dev_err(&client->dev, "failed to allocate device info data\n");
854 retval = -ENOMEM;
855 goto batt_failed_2;
856 }
a40402ef 857
b996ad0e 858 di->id = num;
a40402ef 859 di->dev = &client->dev;
e20908d9 860 di->chip = id->driver_data;
a40402ef
LPC
861 di->bat.name = name;
862 di->bus.read = &bq27x00_read_i2c;
b996ad0e 863
f7760995
JL
864 retval = bq27x00_powersupply_init(di);
865 if (retval)
1cb82fdb 866 goto batt_failed_2;
b996ad0e
RG
867
868 i2c_set_clientdata(client, di);
b996ad0e
RG
869
870 return 0;
871
b996ad0e
RG
872batt_failed_2:
873 kfree(name);
874batt_failed_1:
875 mutex_lock(&battery_mutex);
876 idr_remove(&battery_id, num);
877 mutex_unlock(&battery_mutex);
878
879 return retval;
880}
881
e20908d9 882static int bq27x00_battery_remove(struct i2c_client *client)
b996ad0e
RG
883{
884 struct bq27x00_device_info *di = i2c_get_clientdata(client);
885
740b755a 886 bq27x00_powersupply_unregister(di);
b996ad0e
RG
887
888 kfree(di->bat.name);
889
890 mutex_lock(&battery_mutex);
891 idr_remove(&battery_id, di->id);
892 mutex_unlock(&battery_mutex);
893
b996ad0e
RG
894 return 0;
895}
896
e20908d9
GI
897static const struct i2c_device_id bq27x00_id[] = {
898 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
899 { "bq27500", BQ27500 },
a66f59ba 900 { "bq27425", BQ27425 },
628ef02c 901 { "bq27742", BQ27742 },
b996ad0e
RG
902 {},
903};
fd9b958c 904MODULE_DEVICE_TABLE(i2c, bq27x00_id);
b996ad0e 905
e20908d9 906static struct i2c_driver bq27x00_battery_driver = {
b996ad0e 907 .driver = {
e20908d9 908 .name = "bq27x00-battery",
b996ad0e 909 },
e20908d9
GI
910 .probe = bq27x00_battery_probe,
911 .remove = bq27x00_battery_remove,
912 .id_table = bq27x00_id,
b996ad0e
RG
913};
914
7fb7ba58
LPC
915static inline int bq27x00_battery_i2c_init(void)
916{
917 int ret = i2c_add_driver(&bq27x00_battery_driver);
918 if (ret)
919 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
920
921 return ret;
922}
923
924static inline void bq27x00_battery_i2c_exit(void)
925{
926 i2c_del_driver(&bq27x00_battery_driver);
927}
928
929#else
930
931static inline int bq27x00_battery_i2c_init(void) { return 0; }
932static inline void bq27x00_battery_i2c_exit(void) {};
933
934#endif
935
936/* platform specific code */
937#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
938
939static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
297a533b 940 bool single)
7fb7ba58
LPC
941{
942 struct device *dev = di->dev;
943 struct bq27000_platform_data *pdata = dev->platform_data;
944 unsigned int timeout = 3;
945 int upper, lower;
946 int temp;
947
948 if (!single) {
949 /* Make sure the value has not changed in between reading the
950 * lower and the upper part */
951 upper = pdata->read(dev, reg + 1);
952 do {
953 temp = upper;
954 if (upper < 0)
955 return upper;
956
957 lower = pdata->read(dev, reg);
958 if (lower < 0)
959 return lower;
960
961 upper = pdata->read(dev, reg + 1);
962 } while (temp != upper && --timeout);
963
964 if (timeout == 0)
965 return -EIO;
966
297a533b 967 return (upper << 8) | lower;
7fb7ba58 968 }
297a533b
LPC
969
970 return pdata->read(dev, reg);
7fb7ba58
LPC
971}
972
c8afa640 973static int bq27000_battery_probe(struct platform_device *pdev)
7fb7ba58
LPC
974{
975 struct bq27x00_device_info *di;
976 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
7fb7ba58
LPC
977
978 if (!pdata) {
979 dev_err(&pdev->dev, "no platform_data supplied\n");
980 return -EINVAL;
981 }
982
983 if (!pdata->read) {
984 dev_err(&pdev->dev, "no hdq read callback supplied\n");
985 return -EINVAL;
986 }
987
1cb82fdb 988 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
7fb7ba58
LPC
989 if (!di) {
990 dev_err(&pdev->dev, "failed to allocate device info data\n");
991 return -ENOMEM;
992 }
993
994 platform_set_drvdata(pdev, di);
995
996 di->dev = &pdev->dev;
997 di->chip = BQ27000;
998
999 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
1000 di->bus.read = &bq27000_read_platform;
1001
1cb82fdb 1002 return bq27x00_powersupply_init(di);
7fb7ba58
LPC
1003}
1004
415ec69f 1005static int bq27000_battery_remove(struct platform_device *pdev)
7fb7ba58
LPC
1006{
1007 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
1008
740b755a
LPC
1009 bq27x00_powersupply_unregister(di);
1010
7fb7ba58
LPC
1011 return 0;
1012}
1013
1014static struct platform_driver bq27000_battery_driver = {
1015 .probe = bq27000_battery_probe,
28ea73f4 1016 .remove = bq27000_battery_remove,
7fb7ba58
LPC
1017 .driver = {
1018 .name = "bq27000-battery",
7fb7ba58
LPC
1019 },
1020};
1021
1022static inline int bq27x00_battery_platform_init(void)
1023{
1024 int ret = platform_driver_register(&bq27000_battery_driver);
1025 if (ret)
1026 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
1027
1028 return ret;
1029}
1030
1031static inline void bq27x00_battery_platform_exit(void)
1032{
1033 platform_driver_unregister(&bq27000_battery_driver);
1034}
1035
1036#else
1037
1038static inline int bq27x00_battery_platform_init(void) { return 0; }
1039static inline void bq27x00_battery_platform_exit(void) {};
1040
1041#endif
1042
1043/*
1044 * Module stuff
1045 */
1046
b996ad0e
RG
1047static int __init bq27x00_battery_init(void)
1048{
1049 int ret;
1050
7fb7ba58
LPC
1051 ret = bq27x00_battery_i2c_init();
1052 if (ret)
1053 return ret;
1054
1055 ret = bq27x00_battery_platform_init();
b996ad0e 1056 if (ret)
7fb7ba58 1057 bq27x00_battery_i2c_exit();
b996ad0e
RG
1058
1059 return ret;
1060}
1061module_init(bq27x00_battery_init);
1062
1063static void __exit bq27x00_battery_exit(void)
1064{
7fb7ba58
LPC
1065 bq27x00_battery_platform_exit();
1066 bq27x00_battery_i2c_exit();
b996ad0e
RG
1067}
1068module_exit(bq27x00_battery_exit);
1069
1070MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1071MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1072MODULE_LICENSE("GPL");
This page took 0.421739 seconds and 5 git commands to generate.