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