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