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