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