1 --- kernel-power-2.6.28/drivers/power/bq27x00_battery.c 2011-09-15 00:34:44.600070307 +0200
2 +++ kernel-power-2.6.28/drivers/power/bq27x00_battery.c 2011-09-21 13:16:45.007978008 +0200
5 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
6 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
7 + * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
8 + * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
10 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
13 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 + * http://focus.ti.com/docs/prod/folders/print/bq27000.html
20 + * http://focus.ti.com/docs/prod/folders/print/bq27500.html
23 #include <linux/module.h>
24 #include <linux/param.h>
25 #include <linux/jiffies.h>
27 #include <linux/power_supply.h>
28 #include <linux/idr.h>
29 #include <linux/i2c.h>
30 +#include <linux/slab.h>
31 #include <asm/unaligned.h>
33 -#define DRIVER_VERSION "1.0.0"
34 +#include <linux/power/bq27x00_battery.h>
36 +#define DRIVER_VERSION "1.2.0"
38 #define BQ27x00_REG_TEMP 0x06
39 #define BQ27x00_REG_VOLT 0x08
40 -#define BQ27x00_REG_RSOC 0x0B /* Relative State-of-Charge */
41 #define BQ27x00_REG_AI 0x14
42 #define BQ27x00_REG_FLAGS 0x0A
43 +#define BQ27x00_REG_TTE 0x16
44 +#define BQ27x00_REG_TTF 0x18
45 +#define BQ27x00_REG_TTECP 0x26
46 +#define BQ27x00_REG_NAC 0x0C /* Nominal available capaciy */
47 +#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
48 +#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
49 +#define BQ27x00_REG_AE 0x22 /* Available enery */
51 +#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
52 +#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
53 +#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
54 +#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
55 +#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
56 +#define BQ27000_FLAG_FC BIT(5)
57 +#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
59 +#define BQ27500_REG_SOC 0x2C
60 +#define BQ27500_REG_DCAP 0x3C /* Design capacity */
61 +#define BQ27500_FLAG_DSG BIT(0) /* Discharging */
62 +#define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
63 +#define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
64 +#define BQ27500_FLAG_CHG BIT(8) /* Charging */
65 +#define BQ27500_FLAG_FC BIT(9) /* Fully charged */
67 -/* If the system has several batteries we need a different name for each
70 -static DEFINE_IDR(battery_id);
71 -static DEFINE_MUTEX(battery_mutex);
72 +#define BQ27000_RS 20 /* Resistor sense */
74 struct bq27x00_device_info;
75 struct bq27x00_access_methods {
76 - int (*read)(u8 reg, int *rt_value, int b_single,
77 - struct bq27x00_device_info *di);
78 + int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
81 +enum bq27x00_chip { BQ27000, BQ27500 };
83 +struct bq27x00_reg_cache {
86 + int time_to_empty_avg;
95 struct bq27x00_device_info {
102 - struct bq27x00_access_methods *bus;
103 + enum bq27x00_chip chip;
105 + struct bq27x00_reg_cache cache;
106 + int charge_design_full;
108 + unsigned long last_update;
109 + struct delayed_work work;
111 struct power_supply bat;
113 - struct i2c_client *client;
114 + struct bq27x00_access_methods bus;
119 static enum power_supply_property bq27x00_battery_props[] = {
120 + POWER_SUPPLY_PROP_STATUS,
121 POWER_SUPPLY_PROP_PRESENT,
122 POWER_SUPPLY_PROP_VOLTAGE_NOW,
123 POWER_SUPPLY_PROP_CURRENT_NOW,
124 POWER_SUPPLY_PROP_CAPACITY,
125 + POWER_SUPPLY_PROP_CAPACITY_LEVEL,
126 POWER_SUPPLY_PROP_TEMP,
127 + POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
128 + POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
129 + POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
130 + POWER_SUPPLY_PROP_TECHNOLOGY,
131 + POWER_SUPPLY_PROP_CHARGE_FULL,
132 + POWER_SUPPLY_PROP_CHARGE_NOW,
133 + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
134 + POWER_SUPPLY_PROP_CYCLE_COUNT,
135 + POWER_SUPPLY_PROP_ENERGY_NOW,
138 +static unsigned int poll_interval = 360;
139 +module_param(poll_interval, uint, 0644);
140 +MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
141 + "0 disables polling");
144 * Common code for BQ27x00 devices
147 -static int bq27x00_read(u8 reg, int *rt_value, int b_single,
148 - struct bq27x00_device_info *di)
149 +static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
153 + return di->bus.read(di, reg, single);
156 - ret = di->bus->read(reg, rt_value, b_single, di);
157 - *rt_value = be16_to_cpu(*rt_value);
159 + * Return the battery Relative State-of-Charge
160 + * Or < 0 if something fails.
162 +static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
167 + if (di->chip == BQ27500)
168 + rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
170 + rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
173 + dev_err(di->dev, "error reading relative State-of-Charge\n");
179 - * Return the battery temperature in Celcius degrees
180 + * Return a battery charge value in µAh
181 * Or < 0 if something fails.
183 -static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
184 +static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
190 - ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
192 + charge = bq27x00_read(di, reg, false);
194 + dev_err(di->dev, "error reading charge register %02x: %d\n", reg, charge);
198 + if (di->chip == BQ27500)
201 + charge = charge * 3570 / BQ27000_RS;
207 + * Return the battery Nominal available capaciy in µAh
208 + * Or < 0 if something fails.
210 +static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
212 + return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
216 + * Return the battery Last measured discharge in µAh
217 + * Or < 0 if something fails.
219 +static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
221 + return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
225 + * Return the battery Initial last measured discharge in µAh
226 + * Or < 0 if something fails.
228 +static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
232 + if (di->chip == BQ27500)
233 + ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
235 + ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
238 + dev_err(di->dev, "error reading initial last measured discharge\n");
242 + if (di->chip == BQ27500)
245 + ilmd = ilmd * 256 * 3570 / BQ27000_RS;
251 + * Return the battery Available energy in µWh
252 + * Or < 0 if something fails.
254 +static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
258 + ae = bq27x00_read(di, BQ27x00_REG_AE, false);
260 + dev_err(di->dev, "error reading available energy\n");
264 + if (di->chip == BQ27500)
267 + ae = ae * 29200 / BQ27000_RS;
273 + * Return the battery temperature in tenths of degree Celsius
274 + * Or < 0 if something fails.
276 +static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
280 + temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
282 dev_err(di->dev, "error reading temperature\n");
287 - return (temp >> 2) - 273;
288 + if (di->chip == BQ27500)
291 + temp = ((temp * 5) - 5463) / 2;
297 - * Return the battery Voltage in milivolts
298 + * Return the battery Cycle count total
299 * Or < 0 if something fails.
301 -static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
302 +static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
308 - ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
310 - dev_err(di->dev, "error reading voltage\n");
312 + cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
314 + dev_err(di->dev, "error reading cycle count total\n");
320 + * Read a time register.
321 + * Return < 0 if something fails.
323 +static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
327 + tval = bq27x00_read(di, reg, false);
329 + dev_err(di->dev, "error reading time register %02x: %d\n", reg, tval);
339 +static void bq27x00_update(struct bq27x00_device_info *di)
341 + struct bq27x00_reg_cache cache = {0, };
342 + bool is_bq27500 = di->chip == BQ27500;
344 + cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
345 + if (cache.flags >= 0) {
346 + if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) {
347 + dev_err(di->dev, "battery is not calibrated! ignoring capacity values\n");
348 + cache.capacity = -ENODATA;
349 + cache.energy = -ENODATA;
350 + cache.time_to_empty = -ENODATA;
351 + cache.time_to_empty_avg = -ENODATA;
352 + cache.time_to_full = -ENODATA;
353 + cache.charge_full = -ENODATA;
355 + cache.capacity = bq27x00_battery_read_rsoc(di);
356 + cache.energy = bq27x00_battery_read_energy(di);
357 + cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
358 + cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
359 + cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
360 + cache.charge_full = bq27x00_battery_read_lmd(di);
362 + cache.temperature = bq27x00_battery_read_temperature(di);
363 + cache.cycle_count = bq27x00_battery_read_cyct(di);
365 + /* We only have to read charge design full once */
366 + if (di->charge_design_full <= 0)
367 + di->charge_design_full = bq27x00_battery_read_ilmd(di);
370 + if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
372 + power_supply_changed(&di->bat);
376 + di->last_update = jiffies;
379 +static void bq27x00_battery_poll(struct work_struct *work)
381 + struct bq27x00_device_info *di =
382 + container_of(work, struct bq27x00_device_info, work.work);
384 + bq27x00_update(di);
386 + if (poll_interval > 0) {
387 + /* The timer does not have to be accurate. */
388 + set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
389 + schedule_delayed_work(&di->work, poll_interval * HZ);
394 - * Return the battery average current
395 + * Return the battery average current in µA
396 * Note that current can be negative signed as well
397 * Or 0 if something fails.
399 -static int bq27x00_battery_current(struct bq27x00_device_info *di)
400 +static int bq27x00_battery_current(struct bq27x00_device_info *di,
401 + union power_supply_propval *val)
409 - ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
411 - dev_err(di->dev, "error reading current\n");
413 + curr = bq27x00_read(di, BQ27x00_REG_AI, false);
415 + dev_err(di->dev, "error reading current");
419 + if (di->chip == BQ27500) {
420 + /* bq27500 returns signed value */
421 + val->intval = (int)((s16)curr) * 1000;
423 + flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
424 + if (flags & BQ27000_FLAG_CHGS) {
425 + dev_dbg(di->dev, "negative current!\n");
429 + val->intval = curr * 3570 / BQ27000_RS;
435 +static int bq27x00_battery_status(struct bq27x00_device_info *di,
436 + union power_supply_propval *val)
440 + if (di->chip == BQ27500) {
441 + if (di->cache.flags & BQ27500_FLAG_FC)
442 + status = POWER_SUPPLY_STATUS_FULL;
443 + else if (di->cache.flags & BQ27500_FLAG_DSG)
444 + status = POWER_SUPPLY_STATUS_DISCHARGING;
445 + else if (di->cache.flags & BQ27500_FLAG_CHG)
446 + status = POWER_SUPPLY_STATUS_CHARGING;
447 + else if (power_supply_am_i_supplied(&di->bat))
448 + status = POWER_SUPPLY_STATUS_NOT_CHARGING;
450 + status = POWER_SUPPLY_STATUS_UNKNOWN;
452 + if (di->cache.flags & BQ27000_FLAG_FC)
453 + status = POWER_SUPPLY_STATUS_FULL;
454 + else if (di->cache.flags & BQ27000_FLAG_CHGS)
455 + status = POWER_SUPPLY_STATUS_CHARGING;
456 + else if (power_supply_am_i_supplied(&di->bat))
457 + status = POWER_SUPPLY_STATUS_NOT_CHARGING;
459 + status = POWER_SUPPLY_STATUS_DISCHARGING;
461 - ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
463 - dev_err(di->dev, "error reading flags\n");
466 - if ((flags & (1 << 7)) != 0) {
467 - dev_dbg(di->dev, "negative current!\n");
470 + val->intval = status;
475 +static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
476 + union power_supply_propval *val)
480 + if (di->chip == BQ27500) {
481 + if (di->cache.flags & BQ27500_FLAG_FC)
482 + level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
483 + else if (di->cache.flags & BQ27500_FLAG_SOC1)
484 + level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
485 + else if (di->cache.flags & BQ27500_FLAG_SOCF)
486 + level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
488 + level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
490 + if (di->cache.flags & BQ27000_FLAG_FC)
491 + level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
492 + else if (di->cache.flags & BQ27000_FLAG_EDV1)
493 + level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
494 + else if (di->cache.flags & BQ27000_FLAG_EDVF)
495 + level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
497 + level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
501 + val->intval = level;
507 - * Return the battery Relative State-of-Charge
508 + * Return the battery Voltage in milivolts
509 * Or < 0 if something fails.
511 -static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
512 +static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
513 + union power_supply_propval *val)
519 - ret = bq27x00_read(BQ27x00_REG_RSOC, &rsoc, 1, di);
521 - dev_err(di->dev, "error reading relative State-of-Charge\n");
523 + volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
525 + dev_err(di->dev, "error reading voltage\n");
530 + val->intval = volt * 1000;
535 +static int bq27x00_simple_value(int value,
536 + union power_supply_propval *val)
541 + val->intval = value;
546 #define to_bq27x00_device_info(x) container_of((x), \
547 @@ -171,89 +491,167 @@ static int bq27x00_battery_get_property(
548 enum power_supply_property psp,
549 union power_supply_propval *val)
552 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
554 + mutex_lock(&di->lock);
555 + if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
556 + cancel_delayed_work_sync(&di->work);
557 + bq27x00_battery_poll(&di->work.work);
559 + mutex_unlock(&di->lock);
561 + if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
565 + case POWER_SUPPLY_PROP_STATUS:
566 + ret = bq27x00_battery_status(di, val);
568 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
569 + ret = bq27x00_battery_voltage(di, val);
571 case POWER_SUPPLY_PROP_PRESENT:
572 - val->intval = bq27x00_battery_voltage(di);
573 - if (psp == POWER_SUPPLY_PROP_PRESENT)
574 - val->intval = val->intval <= 0 ? 0 : 1;
575 + val->intval = di->cache.flags < 0 ? 0 : 1;
577 case POWER_SUPPLY_PROP_CURRENT_NOW:
578 - val->intval = bq27x00_battery_current(di);
579 + ret = bq27x00_battery_current(di, val);
581 case POWER_SUPPLY_PROP_CAPACITY:
582 - val->intval = bq27x00_battery_rsoc(di);
583 + ret = bq27x00_simple_value(di->cache.capacity, val);
585 + case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
586 + ret = bq27x00_battery_capacity_level(di, val);
588 case POWER_SUPPLY_PROP_TEMP:
589 - val->intval = bq27x00_battery_temperature(di);
590 + ret = bq27x00_simple_value(di->cache.temperature, val);
592 + case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
593 + ret = bq27x00_simple_value(di->cache.time_to_empty, val);
595 + case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
596 + ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
598 + case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
599 + ret = bq27x00_simple_value(di->cache.time_to_full, val);
601 + case POWER_SUPPLY_PROP_TECHNOLOGY:
602 + val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
604 + case POWER_SUPPLY_PROP_CHARGE_NOW:
605 + ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
607 + case POWER_SUPPLY_PROP_CHARGE_FULL:
608 + ret = bq27x00_simple_value(di->cache.charge_full, val);
610 + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
611 + ret = bq27x00_simple_value(di->charge_design_full, val);
613 + case POWER_SUPPLY_PROP_CYCLE_COUNT:
614 + ret = bq27x00_simple_value(di->cache.cycle_count, val);
616 + case POWER_SUPPLY_PROP_ENERGY_NOW:
617 + ret = bq27x00_simple_value(di->cache.energy, val);
627 -static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
628 +static void bq27x00_external_power_changed(struct power_supply *psy)
630 + struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
632 + cancel_delayed_work_sync(&di->work);
633 + schedule_delayed_work(&di->work, 0);
636 +static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
640 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
641 di->bat.properties = bq27x00_battery_props;
642 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
643 di->bat.get_property = bq27x00_battery_get_property;
644 - di->bat.external_power_changed = NULL;
645 + di->bat.external_power_changed = bq27x00_external_power_changed;
647 + INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
648 + mutex_init(&di->lock);
650 + ret = power_supply_register(di->dev, &di->bat);
652 + dev_err(di->dev, "failed to register battery: %d\n", ret);
656 + dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
658 + bq27x00_update(di);
664 - * BQ27200 specific code
665 +static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
667 + cancel_delayed_work_sync(&di->work);
669 + power_supply_unregister(&di->bat);
671 + mutex_destroy(&di->lock);
675 +/* i2c specific code */
676 +#ifdef CONFIG_BATTERY_BQ27X00_I2C
678 +/* If the system has several batteries we need a different name for each
681 +static DEFINE_IDR(battery_id);
682 +static DEFINE_MUTEX(battery_mutex);
684 -static int bq27200_read(u8 reg, int *rt_value, int b_single,
685 - struct bq27x00_device_info *di)
686 +static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
688 - struct i2c_client *client = di->client;
689 - struct i2c_msg msg[1];
690 + struct i2c_client *client = to_i2c_client(di->dev);
691 + struct i2c_msg msg[2];
692 unsigned char data[2];
696 if (!client->adapter)
699 - msg->addr = client->addr;
705 - err = i2c_transfer(client->adapter, msg, 1);
712 + msg[0].addr = client->addr;
715 + msg[0].len = sizeof(reg);
716 + msg[1].addr = client->addr;
717 + msg[1].flags = I2C_M_RD;
724 - msg->flags = I2C_M_RD;
725 - err = i2c_transfer(client->adapter, msg, 1);
728 - *rt_value = get_unaligned_be16(data);
730 - *rt_value = data[0];
731 + ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
740 + ret = get_unaligned_le16(data);
747 -static int bq27200_battery_probe(struct i2c_client *client,
748 +static int bq27x00_battery_probe(struct i2c_client *client,
749 const struct i2c_device_id *id)
752 struct bq27x00_device_info *di;
753 - struct bq27x00_access_methods *bus;
757 @@ -267,7 +665,7 @@ static int bq27200_battery_probe(struct
761 - name = kasprintf(GFP_KERNEL, "bq27200-%d", num);
762 + name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
764 dev_err(&client->dev, "failed to allocate device name\n");
766 @@ -280,37 +678,20 @@ static int bq27200_battery_probe(struct
772 - bus = kzalloc(sizeof(*bus), GFP_KERNEL);
774 - dev_err(&client->dev, "failed to allocate access method "
777 - goto batt_failed_3;
780 - i2c_set_clientdata(client, di);
782 di->dev = &client->dev;
783 + di->chip = id->driver_data;
785 - bus->read = &bq27200_read;
787 - di->client = client;
788 + di->bus.read = &bq27x00_read_i2c;
790 - bq27x00_powersupply_init(di);
792 - retval = power_supply_register(&client->dev, &di->bat);
794 - dev_err(&client->dev, "failed to register battery\n");
795 - goto batt_failed_4;
797 + if (bq27x00_powersupply_init(di))
798 + goto batt_failed_3;
800 - dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
801 + i2c_set_clientdata(client, di);
810 @@ -323,11 +704,11 @@ batt_failed_1:
814 -static int bq27200_battery_remove(struct i2c_client *client)
815 +static int bq27x00_battery_remove(struct i2c_client *client)
817 struct bq27x00_device_info *di = i2c_get_clientdata(client);
819 - power_supply_unregister(&di->bat);
820 + bq27x00_powersupply_unregister(di);
824 @@ -340,31 +721,180 @@ static int bq27200_battery_remove(struct
832 -static const struct i2c_device_id bq27200_id[] = {
834 +static const struct i2c_device_id bq27x00_id[] = {
835 + { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
836 + { "bq27500", BQ27500 },
839 +MODULE_DEVICE_TABLE(i2c, bq27x00_id);
841 +static struct i2c_driver bq27x00_battery_driver = {
843 + .name = "bq27x00-battery",
845 + .probe = bq27x00_battery_probe,
846 + .remove = bq27x00_battery_remove,
847 + .id_table = bq27x00_id,
850 +static inline int bq27x00_battery_i2c_init(void)
852 + int ret = i2c_add_driver(&bq27x00_battery_driver);
854 + printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
859 +static inline void bq27x00_battery_i2c_exit(void)
861 + i2c_del_driver(&bq27x00_battery_driver);
866 +static inline int bq27x00_battery_i2c_init(void) { return 0; }
867 +static inline void bq27x00_battery_i2c_exit(void) {};
871 +/* platform specific code */
872 +#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
874 +static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
877 + struct device *dev = di->dev;
878 + struct bq27000_platform_data *pdata = dev->platform_data;
879 + unsigned int timeout = 3;
884 + /* Make sure the value has not changed in between reading the
885 + * lower and the upper part */
886 + upper = pdata->read(dev, reg + 1);
892 + lower = pdata->read(dev, reg);
896 + upper = pdata->read(dev, reg + 1);
897 + } while (temp != upper && --timeout);
902 + return (upper << 8) | lower;
905 + return pdata->read(dev, reg);
908 +static int __devinit bq27000_battery_probe(struct platform_device *pdev)
910 + struct bq27x00_device_info *di;
911 + struct bq27000_platform_data *pdata = pdev->dev.platform_data;
915 + dev_err(&pdev->dev, "no platform_data supplied\n");
919 + if (!pdata->read) {
920 + dev_err(&pdev->dev, "no hdq read callback supplied\n");
924 + di = kzalloc(sizeof(*di), GFP_KERNEL);
926 + dev_err(&pdev->dev, "failed to allocate device info data\n");
930 + platform_set_drvdata(pdev, di);
932 + di->dev = &pdev->dev;
933 + di->chip = BQ27000;
935 + di->bat.name = pdata->name ?: dev_name(&pdev->dev);
936 + di->bus.read = &bq27000_read_platform;
938 -static struct i2c_driver bq27200_battery_driver = {
939 + ret = bq27x00_powersupply_init(di);
946 + platform_set_drvdata(pdev, NULL);
952 +static int __devexit bq27000_battery_remove(struct platform_device *pdev)
954 + struct bq27x00_device_info *di = platform_get_drvdata(pdev);
956 + bq27x00_powersupply_unregister(di);
958 + platform_set_drvdata(pdev, NULL);
964 +static struct platform_driver bq27000_battery_driver = {
965 + .probe = bq27000_battery_probe,
966 + .remove = __devexit_p(bq27000_battery_remove),
968 - .name = "bq27200-battery",
969 + .name = "bq27000-battery",
970 + .owner = THIS_MODULE,
972 - .probe = bq27200_battery_probe,
973 - .remove = bq27200_battery_remove,
974 - .id_table = bq27200_id,
977 +static inline int bq27x00_battery_platform_init(void)
979 + int ret = platform_driver_register(&bq27000_battery_driver);
981 + printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
986 +static inline void bq27x00_battery_platform_exit(void)
988 + platform_driver_unregister(&bq27000_battery_driver);
993 +static inline int bq27x00_battery_platform_init(void) { return 0; }
994 +static inline void bq27x00_battery_platform_exit(void) {};
1002 static int __init bq27x00_battery_init(void)
1006 - ret = i2c_add_driver(&bq27200_battery_driver);
1007 + ret = bq27x00_battery_i2c_init();
1011 + ret = bq27x00_battery_platform_init();
1013 - printk(KERN_ERR "Unable to register BQ27200 driver\n");
1014 + bq27x00_battery_i2c_exit();
1018 @@ -372,7 +902,8 @@ module_init(bq27x00_battery_init);
1020 static void __exit bq27x00_battery_exit(void)
1022 - i2c_del_driver(&bq27200_battery_driver);
1023 + bq27x00_battery_platform_exit();
1024 + bq27x00_battery_i2c_exit();
1026 module_exit(bq27x00_battery_exit);