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