2 * BQ27x00 battery driver
4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
6 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
7 * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
9 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
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.
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.
23 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
24 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
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>
35 #include <linux/i2c.h>
36 #include <linux/slab.h>
37 #include <asm/unaligned.h>
39 #include <linux/power/bq27x00_battery.h>
41 #define DRIVER_VERSION "1.2.0"
43 #define BQ27x00_REG_TEMP 0x06
44 #define BQ27x00_REG_VOLT 0x08
45 #define BQ27x00_REG_AI 0x14
46 #define BQ27x00_REG_FLAGS 0x0A
47 #define BQ27x00_REG_TTE 0x16
48 #define BQ27x00_REG_TTF 0x18
49 #define BQ27x00_REG_TTECP 0x26
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 */
55 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
56 #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
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 */
59 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
60 #define BQ27000_FLAG_FC BIT(5)
61 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
63 #define BQ27000_FLAGS_IMPORTANT (BQ27000_FLAG_FC|BQ27000_FLAG_CHGS|BIT(31))
65 #define BQ27500_REG_SOC 0x2C
66 #define BQ27500_REG_DCAP 0x3C /* Design capacity */
67 #define BQ27500_FLAG_DSC BIT(0)
68 #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
69 #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
70 #define BQ27500_FLAG_FC BIT(9)
72 #define BQ27500_FLAGS_IMPORTANT (BQ27500_FLAG_FC|BQ27500_FLAG_DSC|BIT(31))
74 #define BQ27000_RS 20 /* Resistor sense */
76 struct bq27x00_device_info;
77 struct bq27x00_access_methods {
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;
97 struct bq27x00_device_info {
100 enum bq27x00_chip chip;
102 struct bq27x00_reg_cache cache;
103 int charge_design_full;
105 unsigned long last_update;
106 struct delayed_work work;
108 struct power_supply bat;
110 struct bq27x00_access_methods bus;
115 static enum power_supply_property bq27x00_battery_props[] = {
116 POWER_SUPPLY_PROP_STATUS,
117 POWER_SUPPLY_PROP_PRESENT,
118 POWER_SUPPLY_PROP_VOLTAGE_NOW,
119 POWER_SUPPLY_PROP_CURRENT_NOW,
120 POWER_SUPPLY_PROP_CAPACITY,
121 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
122 POWER_SUPPLY_PROP_TEMP,
123 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
124 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
125 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
126 POWER_SUPPLY_PROP_TECHNOLOGY,
127 POWER_SUPPLY_PROP_CHARGE_FULL,
128 POWER_SUPPLY_PROP_CHARGE_NOW,
129 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
130 POWER_SUPPLY_PROP_CYCLE_COUNT,
131 POWER_SUPPLY_PROP_ENERGY_NOW,
134 static unsigned int poll_interval = 360;
135 module_param(poll_interval, uint, 0644);
136 MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
137 "0 disables polling");
140 * Common code for BQ27x00 devices
143 static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
146 return di->bus.read(di, reg, single);
150 * Return the battery Relative State-of-Charge
151 * Or < 0 if something fails.
153 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
157 if (di->chip == BQ27500)
158 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
160 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
163 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
169 * Return a battery charge value in µAh
170 * Or < 0 if something fails.
172 static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
176 charge = bq27x00_read(di, reg, false);
178 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
183 if (di->chip == BQ27500)
186 charge = charge * 3570 / BQ27000_RS;
192 * Return the battery Nominal available capaciy in µAh
193 * Or < 0 if something fails.
195 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
197 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
201 * Return the battery Last measured discharge in µAh
202 * Or < 0 if something fails.
204 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
206 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
210 * Return the battery Initial last measured discharge in µAh
211 * Or < 0 if something fails.
213 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
217 if (di->chip == BQ27500)
218 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
220 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
223 dev_dbg(di->dev, "error reading initial last measured discharge\n");
227 if (di->chip == BQ27500)
230 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
236 * Return the battery Available energy in µWh
237 * Or < 0 if something fails.
239 static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
243 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
245 dev_dbg(di->dev, "error reading available energy\n");
249 if (di->chip == BQ27500)
252 ae = ae * 29200 / BQ27000_RS;
258 * Return the battery temperature in tenths of degree Kelvin
259 * Or < 0 if something fails.
261 static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
265 temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
267 dev_err(di->dev, "error reading temperature\n");
271 if (di->chip != BQ27500)
278 * Return the battery Cycle count total
279 * Or < 0 if something fails.
281 static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
285 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
287 dev_err(di->dev, "error reading cycle count total\n");
293 * Read a time register.
294 * Return < 0 if something fails.
296 static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
300 tval = bq27x00_read(di, reg, false);
302 dev_dbg(di->dev, "error reading time register %02x: %d\n",
313 static int bq27x00_time(int tval)
321 static int bq27x00_read_i2c_n(struct bq27x00_device_info *di, u8 *data,
322 size_t len, u8 start);
324 static void bq27x00_update(struct bq27x00_device_info *di)
326 struct bq27x00_reg_cache cache = {0, };
327 bool is_bq27500 = di->chip == BQ27500;
333 WARN_ON_ONCE(!is_bq27500);
334 (void)bq27x00_battery_read_energy;
335 (void)bq27x00_battery_read_temperature;
336 (void)bq27x00_battery_read_time;
338 /* reading reserved field breaks subsequent reads,
339 * so can't read everything in one go :( */
340 ret = bq27x00_read_i2c_n(di, state + 6, sizeof(state) - 6, 6);
342 dev_err(di->dev, "error reading state: %d\n", ret);
346 cache.flags = get_unaligned_le16(&state[BQ27x00_REG_FLAGS]);
347 cache.energy = get_unaligned_le16(&state[BQ27x00_REG_AE]) * 1000;
348 cache.time_to_empty = get_unaligned_le16(&state[BQ27x00_REG_TTE]);
349 cache.time_to_empty = bq27x00_time(cache.time_to_empty);
350 cache.time_to_empty_avg = get_unaligned_le16(&state[BQ27x00_REG_TTECP]);
351 cache.time_to_empty_avg = bq27x00_time(cache.time_to_empty_avg);
352 cache.time_to_full = get_unaligned_le16(&state[BQ27x00_REG_TTF]);
353 cache.time_to_full = bq27x00_time(cache.time_to_full);
354 cache.charge_full = get_unaligned_le16(&state[BQ27x00_REG_LMD]) * 1000;
355 cache.charge_now = get_unaligned_le16(&state[BQ27x00_REG_NAC]) * 1000;
356 cache.temperature = get_unaligned_le16(&state[BQ27x00_REG_TEMP]);
357 cache.voltage = get_unaligned_le16(&state[BQ27x00_REG_VOLT]) * 1000;
358 cache.curr = (s16)get_unaligned_le16(&state[BQ27x00_REG_AI]) * 1000;
360 /* We only have to read charge design full once */
361 if (di->charge_design_full <= 0)
362 di->charge_design_full = bq27x00_battery_read_ilmd(di);
365 * On bq27500, DSG is not set on discharge with very low currents,
366 * so check AI to not misreport that we are charging in status query
368 if (is_bq27500 && !(cache.flags & BQ27500_FLAG_DSC)) {
370 cache.flags |= BQ27500_FLAG_DSC;
373 flags_changed = di->cache.flags ^ cache.flags;
376 flags_changed &= BQ27500_FLAGS_IMPORTANT;
378 flags_changed &= BQ27000_FLAGS_IMPORTANT;
380 power_supply_changed(&di->bat);
382 di->last_update = jiffies;
385 static void bq27x00_battery_poll(struct work_struct *work)
387 struct bq27x00_device_info *di =
388 container_of(work, struct bq27x00_device_info, work.work);
392 if (poll_interval > 0) {
393 /* The timer does not have to be accurate. */
394 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
395 schedule_delayed_work(&di->work, poll_interval * HZ);
400 * Return the battery average current in µA
401 * Note that current can be negative signed as well
402 * Or 0 if something fails.
404 static int bq27x00_battery_current(struct bq27x00_device_info *di,
405 union power_supply_propval *val)
410 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
412 dev_err(di->dev, "error reading current\n");
416 if (di->chip == BQ27500) {
417 /* bq27500 returns signed value */
418 val->intval = (int)((s16)curr) * 1000;
420 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
421 if (flags & BQ27000_FLAG_CHGS) {
422 dev_dbg(di->dev, "negative current!\n");
426 val->intval = curr * 3570 / BQ27000_RS;
432 static int bq27x00_battery_status(struct bq27x00_device_info *di,
433 union power_supply_propval *val)
437 if (di->chip == BQ27500) {
438 if (di->cache.flags & BQ27500_FLAG_FC)
439 status = POWER_SUPPLY_STATUS_FULL;
440 else if (di->cache.flags & BQ27500_FLAG_DSC)
441 status = POWER_SUPPLY_STATUS_DISCHARGING;
443 status = POWER_SUPPLY_STATUS_CHARGING;
445 if (di->cache.flags & BQ27000_FLAG_FC)
446 status = POWER_SUPPLY_STATUS_FULL;
447 else if (di->cache.flags & BQ27000_FLAG_CHGS)
448 status = POWER_SUPPLY_STATUS_CHARGING;
449 else if (power_supply_am_i_supplied(&di->bat))
450 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
452 status = POWER_SUPPLY_STATUS_DISCHARGING;
455 val->intval = status;
460 static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
461 union power_supply_propval *val)
465 if (di->chip == BQ27500) {
466 if (di->cache.flags & BQ27500_FLAG_FC)
467 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
468 else if (di->cache.flags & BQ27500_FLAG_SOC1)
469 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
470 else if (di->cache.flags & BQ27500_FLAG_SOCF)
471 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
473 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
475 if (di->cache.flags & BQ27000_FLAG_FC)
476 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
477 else if (di->cache.flags & BQ27000_FLAG_EDV1)
478 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
479 else if (di->cache.flags & BQ27000_FLAG_EDVF)
480 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
482 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
491 * Return the battery Voltage in milivolts
492 * Or < 0 if something fails.
494 static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
495 union power_supply_propval *val)
499 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
501 dev_err(di->dev, "error reading voltage\n");
505 val->intval = volt * 1000;
510 static int bq27x00_simple_value(int value,
511 union power_supply_propval *val)
521 #define to_bq27x00_device_info(x) container_of((x), \
522 struct bq27x00_device_info, bat);
524 static int bq27x00_battery_get_property(struct power_supply *psy,
525 enum power_supply_property psp,
526 union power_supply_propval *val)
529 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
531 mutex_lock(&di->lock);
532 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
533 cancel_delayed_work_sync(&di->work);
534 bq27x00_battery_poll(&di->work.work);
536 mutex_unlock(&di->lock);
538 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
542 case POWER_SUPPLY_PROP_STATUS:
543 ret = bq27x00_battery_status(di, val);
545 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
546 if (time_is_before_jiffies(di->last_update + HZ))
547 ret = bq27x00_battery_voltage(di, val);
549 ret = bq27x00_simple_value(di->cache.voltage, val);
551 case POWER_SUPPLY_PROP_PRESENT:
552 val->intval = di->cache.flags < 0 ? 0 : 1;
554 case POWER_SUPPLY_PROP_CURRENT_NOW:
556 if (time_is_before_jiffies(di->last_update + HZ))
557 ret = bq27x00_battery_current(di, val);
559 val->intval = di->cache.curr;
561 case POWER_SUPPLY_PROP_CAPACITY:
562 ret = bq27x00_simple_value(bq27x00_battery_read_rsoc(di), val);
564 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
565 ret = bq27x00_battery_capacity_level(di, val);
567 case POWER_SUPPLY_PROP_TEMP:
568 ret = bq27x00_simple_value(di->cache.temperature, val);
572 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
573 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
575 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
576 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
578 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
579 ret = bq27x00_simple_value(di->cache.time_to_full, val);
581 case POWER_SUPPLY_PROP_TECHNOLOGY:
582 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
584 case POWER_SUPPLY_PROP_CHARGE_NOW:
585 ret = bq27x00_simple_value(di->cache.charge_now, val);
587 case POWER_SUPPLY_PROP_CHARGE_FULL:
588 ret = bq27x00_simple_value(di->cache.charge_full, val);
590 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
591 ret = bq27x00_simple_value(di->charge_design_full, val);
593 case POWER_SUPPLY_PROP_CYCLE_COUNT:
594 ret = bq27x00_simple_value(bq27x00_battery_read_cyct(di), val);
596 case POWER_SUPPLY_PROP_ENERGY_NOW:
597 ret = bq27x00_simple_value(di->cache.energy, val);
606 static void bq27x00_external_power_changed(struct power_supply *psy)
608 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
610 mutex_lock(&di->lock);
612 cancel_delayed_work_sync(&di->work);
613 set_timer_slack(&di->work.timer, 1 * HZ);
614 schedule_delayed_work(&di->work, 3 * HZ);
616 mutex_unlock(&di->lock);
619 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
623 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
624 di->bat.properties = bq27x00_battery_props;
625 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
626 di->bat.get_property = bq27x00_battery_get_property;
627 di->bat.external_power_changed = bq27x00_external_power_changed;
629 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
630 mutex_init(&di->lock);
632 ret = power_supply_register(di->dev, &di->bat);
634 dev_err(di->dev, "failed to register battery: %d\n", ret);
638 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
645 static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
648 * power_supply_unregister call bq27x00_battery_get_property which
649 * call bq27x00_battery_poll.
650 * Make sure that bq27x00_battery_poll will not call
651 * schedule_delayed_work again after unregister (which cause OOPS).
655 cancel_delayed_work_sync(&di->work);
657 power_supply_unregister(&di->bat);
659 mutex_destroy(&di->lock);
663 /* i2c specific code */
664 #ifdef CONFIG_BATTERY_BQ27X00_I2C
666 /* If the system has several batteries we need a different name for each
669 static DEFINE_IDR(battery_id);
670 static DEFINE_MUTEX(battery_mutex);
672 static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
674 struct i2c_client *client = to_i2c_client(di->dev);
675 struct i2c_msg msg[2];
676 unsigned char data[2];
679 if (!client->adapter)
682 msg[0].addr = client->addr;
685 msg[0].len = sizeof(reg);
686 msg[1].addr = client->addr;
687 msg[1].flags = I2C_M_RD;
694 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
699 ret = get_unaligned_le16(data);
706 static int bq27x00_read_i2c_n(struct bq27x00_device_info *di, u8 *data,
707 size_t len, u8 start)
709 struct i2c_client *client = to_i2c_client(di->dev);
710 struct i2c_msg msg[2];
712 if (!client->adapter)
715 msg[0].addr = client->addr;
718 msg[0].len = sizeof(start);
719 msg[1].addr = client->addr;
720 msg[1].flags = I2C_M_RD;
724 return i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
727 static int bq27x00_battery_probe(struct i2c_client *client,
728 const struct i2c_device_id *id)
731 struct bq27x00_device_info *di;
735 /* Get new ID for the new battery device */
736 retval = idr_pre_get(&battery_id, GFP_KERNEL);
739 mutex_lock(&battery_mutex);
740 retval = idr_get_new(&battery_id, client, &num);
741 mutex_unlock(&battery_mutex);
745 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
747 dev_err(&client->dev, "failed to allocate device name\n");
752 di = kzalloc(sizeof(*di), GFP_KERNEL);
754 dev_err(&client->dev, "failed to allocate device info data\n");
760 di->dev = &client->dev;
761 di->chip = id->driver_data;
763 di->bus.read = &bq27x00_read_i2c;
765 if (bq27x00_powersupply_init(di))
768 i2c_set_clientdata(client, di);
777 mutex_lock(&battery_mutex);
778 idr_remove(&battery_id, num);
779 mutex_unlock(&battery_mutex);
784 static int bq27x00_battery_remove(struct i2c_client *client)
786 struct bq27x00_device_info *di = i2c_get_clientdata(client);
788 bq27x00_powersupply_unregister(di);
792 mutex_lock(&battery_mutex);
793 idr_remove(&battery_id, di->id);
794 mutex_unlock(&battery_mutex);
801 static const struct i2c_device_id bq27x00_id[] = {
802 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
803 { "bq27500", BQ27500 },
806 MODULE_DEVICE_TABLE(i2c, bq27x00_id);
808 static struct i2c_driver bq27x00_battery_driver = {
810 .name = "bq27x00-battery",
812 .probe = bq27x00_battery_probe,
813 .remove = bq27x00_battery_remove,
814 .id_table = bq27x00_id,
817 static inline int bq27x00_battery_i2c_init(void)
819 int ret = i2c_add_driver(&bq27x00_battery_driver);
821 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
826 static inline void bq27x00_battery_i2c_exit(void)
828 i2c_del_driver(&bq27x00_battery_driver);
833 static inline int bq27x00_battery_i2c_init(void) { return 0; }
834 static inline void bq27x00_battery_i2c_exit(void) {};
838 /* platform specific code */
839 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
841 static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
844 struct device *dev = di->dev;
845 struct bq27000_platform_data *pdata = dev->platform_data;
846 unsigned int timeout = 3;
851 /* Make sure the value has not changed in between reading the
852 * lower and the upper part */
853 upper = pdata->read(dev, reg + 1);
859 lower = pdata->read(dev, reg);
863 upper = pdata->read(dev, reg + 1);
864 } while (temp != upper && --timeout);
869 return (upper << 8) | lower;
872 return pdata->read(dev, reg);
875 static int __devinit bq27000_battery_probe(struct platform_device *pdev)
877 struct bq27x00_device_info *di;
878 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
882 dev_err(&pdev->dev, "no platform_data supplied\n");
887 dev_err(&pdev->dev, "no hdq read callback supplied\n");
891 di = kzalloc(sizeof(*di), GFP_KERNEL);
893 dev_err(&pdev->dev, "failed to allocate device info data\n");
897 platform_set_drvdata(pdev, di);
899 di->dev = &pdev->dev;
902 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
903 di->bus.read = &bq27000_read_platform;
905 ret = bq27x00_powersupply_init(di);
912 platform_set_drvdata(pdev, NULL);
918 static int __devexit bq27000_battery_remove(struct platform_device *pdev)
920 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
922 bq27x00_powersupply_unregister(di);
924 platform_set_drvdata(pdev, NULL);
930 static struct platform_driver bq27000_battery_driver = {
931 .probe = bq27000_battery_probe,
932 .remove = __devexit_p(bq27000_battery_remove),
934 .name = "bq27000-battery",
935 .owner = THIS_MODULE,
939 static inline int bq27x00_battery_platform_init(void)
941 int ret = platform_driver_register(&bq27000_battery_driver);
943 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
948 static inline void bq27x00_battery_platform_exit(void)
950 platform_driver_unregister(&bq27000_battery_driver);
955 static inline int bq27x00_battery_platform_init(void) { return 0; }
956 static inline void bq27x00_battery_platform_exit(void) {};
964 static int __init bq27x00_battery_init(void)
968 ret = bq27x00_battery_i2c_init();
972 ret = bq27x00_battery_platform_init();
974 bq27x00_battery_i2c_exit();
978 module_init(bq27x00_battery_init);
980 static void __exit bq27x00_battery_exit(void)
982 bq27x00_battery_platform_exit();
983 bq27x00_battery_i2c_exit();
985 module_exit(bq27x00_battery_exit);
987 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
988 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
989 MODULE_LICENSE("GPL");