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 capacity */
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 energy */
54 #define BQ27x00_POWER_AVG 0x24
56 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
57 #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
58 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
59 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
60 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
61 #define BQ27000_FLAG_FC BIT(5)
62 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
64 #define BQ27500_REG_SOC 0x2C
65 #define BQ27500_REG_DCAP 0x3C /* Design capacity */
66 #define BQ27500_FLAG_DSC BIT(0)
67 #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
68 #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
69 #define BQ27500_FLAG_FC BIT(9)
70 #define BQ27500_FLAG_OTC BIT(15)
72 #define BQ27000_RS 20 /* Resistor sense */
73 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
75 struct bq27x00_device_info;
76 struct bq27x00_access_methods {
77 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
80 enum bq27x00_chip { BQ27000, BQ27500 };
82 struct bq27x00_reg_cache {
85 int time_to_empty_avg;
96 struct bq27x00_device_info {
99 enum bq27x00_chip chip;
101 struct bq27x00_reg_cache cache;
102 int charge_design_full;
104 unsigned long last_update;
105 struct delayed_work work;
107 struct power_supply bat;
109 struct bq27x00_access_methods bus;
114 static enum power_supply_property bq27x00_battery_props[] = {
115 POWER_SUPPLY_PROP_STATUS,
116 POWER_SUPPLY_PROP_PRESENT,
117 POWER_SUPPLY_PROP_VOLTAGE_NOW,
118 POWER_SUPPLY_PROP_CURRENT_NOW,
119 POWER_SUPPLY_PROP_CAPACITY,
120 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
121 POWER_SUPPLY_PROP_TEMP,
122 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
123 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
124 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
125 POWER_SUPPLY_PROP_TECHNOLOGY,
126 POWER_SUPPLY_PROP_CHARGE_FULL,
127 POWER_SUPPLY_PROP_CHARGE_NOW,
128 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
129 POWER_SUPPLY_PROP_CYCLE_COUNT,
130 POWER_SUPPLY_PROP_ENERGY_NOW,
131 POWER_SUPPLY_PROP_POWER_AVG,
132 POWER_SUPPLY_PROP_HEALTH,
135 static unsigned int poll_interval = 360;
136 module_param(poll_interval, uint, 0644);
137 MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
138 "0 disables polling");
141 * Common code for BQ27x00 devices
144 static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
147 return di->bus.read(di, reg, single);
151 * Return the battery Relative State-of-Charge
152 * Or < 0 if something fails.
154 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
158 if (di->chip == BQ27500)
159 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
161 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
164 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
170 * Return a battery charge value in µAh
171 * Or < 0 if something fails.
173 static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
177 charge = bq27x00_read(di, reg, false);
179 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
184 if (di->chip == BQ27500)
187 charge = charge * 3570 / BQ27000_RS;
193 * Return the battery Nominal available capaciy in µAh
194 * Or < 0 if something fails.
196 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
198 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
202 * Return the battery Last measured discharge in µAh
203 * Or < 0 if something fails.
205 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
207 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
211 * Return the battery Initial last measured discharge in µAh
212 * Or < 0 if something fails.
214 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
218 if (di->chip == BQ27500)
219 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
221 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
224 dev_dbg(di->dev, "error reading initial last measured discharge\n");
228 if (di->chip == BQ27500)
231 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
237 * Return the battery Available energy in µWh
238 * Or < 0 if something fails.
240 static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
244 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
246 dev_dbg(di->dev, "error reading available energy\n");
250 if (di->chip == BQ27500)
253 ae = ae * 29200 / BQ27000_RS;
259 * Return the battery temperature in tenths of degree Celsius
260 * Or < 0 if something fails.
262 static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
266 temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
268 dev_err(di->dev, "error reading temperature\n");
272 if (di->chip == BQ27500)
275 temp = ((temp * 5) - 5463) / 2;
281 * Return the battery Cycle count total
282 * Or < 0 if something fails.
284 static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
288 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
290 dev_err(di->dev, "error reading cycle count total\n");
296 * Read a time register.
297 * Return < 0 if something fails.
299 static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
303 tval = bq27x00_read(di, reg, false);
305 dev_dbg(di->dev, "error reading time register %02x: %d\n",
317 * Read a power avg register.
318 * Return < 0 if something fails.
320 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
324 tval = bq27x00_read(di, reg, false);
326 dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
331 if (di->chip == BQ27500)
334 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
338 * Read flag register.
339 * Return < 0 if something fails.
341 static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
345 tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
347 dev_err(di->dev, "error reading flag register:%d\n", tval);
351 if ((di->chip == BQ27500)) {
352 if (tval & BQ27500_FLAG_SOCF)
353 tval = POWER_SUPPLY_HEALTH_DEAD;
354 else if (tval & BQ27500_FLAG_OTC)
355 tval = POWER_SUPPLY_HEALTH_OVERHEAT;
357 tval = POWER_SUPPLY_HEALTH_GOOD;
360 if (tval & BQ27000_FLAG_EDV1)
361 tval = POWER_SUPPLY_HEALTH_DEAD;
363 tval = POWER_SUPPLY_HEALTH_GOOD;
370 static void bq27x00_update(struct bq27x00_device_info *di)
372 struct bq27x00_reg_cache cache = {0, };
373 bool is_bq27500 = di->chip == BQ27500;
375 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
376 if (cache.flags >= 0) {
377 if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) {
378 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
379 cache.capacity = -ENODATA;
380 cache.energy = -ENODATA;
381 cache.time_to_empty = -ENODATA;
382 cache.time_to_empty_avg = -ENODATA;
383 cache.time_to_full = -ENODATA;
384 cache.charge_full = -ENODATA;
385 cache.health = -ENODATA;
387 cache.capacity = bq27x00_battery_read_rsoc(di);
388 cache.energy = bq27x00_battery_read_energy(di);
389 cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
390 cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
391 cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
392 cache.charge_full = bq27x00_battery_read_lmd(di);
393 cache.health = bq27x00_battery_read_health(di);
395 cache.temperature = bq27x00_battery_read_temperature(di);
396 cache.cycle_count = bq27x00_battery_read_cyct(di);
398 bq27x00_battery_read_pwr_avg(di, BQ27x00_POWER_AVG);
400 /* We only have to read charge design full once */
401 if (di->charge_design_full <= 0)
402 di->charge_design_full = bq27x00_battery_read_ilmd(di);
405 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
407 power_supply_changed(&di->bat);
410 di->last_update = jiffies;
413 static void bq27x00_battery_poll(struct work_struct *work)
415 struct bq27x00_device_info *di =
416 container_of(work, struct bq27x00_device_info, work.work);
420 if (poll_interval > 0) {
421 /* The timer does not have to be accurate. */
422 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
423 schedule_delayed_work(&di->work, poll_interval * HZ);
428 * Return the battery average current in µA
429 * Note that current can be negative signed as well
430 * Or 0 if something fails.
432 static int bq27x00_battery_current(struct bq27x00_device_info *di,
433 union power_supply_propval *val)
438 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
440 dev_err(di->dev, "error reading current\n");
444 if (di->chip == BQ27500) {
445 /* bq27500 returns signed value */
446 val->intval = (int)((s16)curr) * 1000;
448 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
449 if (flags & BQ27000_FLAG_CHGS) {
450 dev_dbg(di->dev, "negative current!\n");
454 val->intval = curr * 3570 / BQ27000_RS;
460 static int bq27x00_battery_status(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 status = POWER_SUPPLY_STATUS_FULL;
468 else if (di->cache.flags & BQ27500_FLAG_DSC)
469 status = POWER_SUPPLY_STATUS_DISCHARGING;
471 status = POWER_SUPPLY_STATUS_CHARGING;
473 if (di->cache.flags & BQ27000_FLAG_FC)
474 status = POWER_SUPPLY_STATUS_FULL;
475 else if (di->cache.flags & BQ27000_FLAG_CHGS)
476 status = POWER_SUPPLY_STATUS_CHARGING;
477 else if (power_supply_am_i_supplied(&di->bat))
478 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
480 status = POWER_SUPPLY_STATUS_DISCHARGING;
483 val->intval = status;
488 static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
489 union power_supply_propval *val)
493 if (di->chip == BQ27500) {
494 if (di->cache.flags & BQ27500_FLAG_FC)
495 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
496 else if (di->cache.flags & BQ27500_FLAG_SOC1)
497 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
498 else if (di->cache.flags & BQ27500_FLAG_SOCF)
499 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
501 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
503 if (di->cache.flags & BQ27000_FLAG_FC)
504 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
505 else if (di->cache.flags & BQ27000_FLAG_EDV1)
506 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
507 else if (di->cache.flags & BQ27000_FLAG_EDVF)
508 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
510 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
519 * Return the battery Voltage in millivolts
520 * Or < 0 if something fails.
522 static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
523 union power_supply_propval *val)
527 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
529 dev_err(di->dev, "error reading voltage\n");
533 val->intval = volt * 1000;
538 static int bq27x00_simple_value(int value,
539 union power_supply_propval *val)
549 #define to_bq27x00_device_info(x) container_of((x), \
550 struct bq27x00_device_info, bat);
552 static int bq27x00_battery_get_property(struct power_supply *psy,
553 enum power_supply_property psp,
554 union power_supply_propval *val)
557 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
559 mutex_lock(&di->lock);
560 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
561 cancel_delayed_work_sync(&di->work);
562 bq27x00_battery_poll(&di->work.work);
564 mutex_unlock(&di->lock);
566 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
570 case POWER_SUPPLY_PROP_STATUS:
571 ret = bq27x00_battery_status(di, val);
573 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
574 ret = bq27x00_battery_voltage(di, val);
576 case POWER_SUPPLY_PROP_PRESENT:
577 val->intval = di->cache.flags < 0 ? 0 : 1;
579 case POWER_SUPPLY_PROP_CURRENT_NOW:
580 ret = bq27x00_battery_current(di, val);
582 case POWER_SUPPLY_PROP_CAPACITY:
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 ret = bq27x00_simple_value(di->cache.temperature, val);
591 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
592 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
594 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
595 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
597 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
598 ret = bq27x00_simple_value(di->cache.time_to_full, val);
600 case POWER_SUPPLY_PROP_TECHNOLOGY:
601 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
603 case POWER_SUPPLY_PROP_CHARGE_NOW:
604 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
606 case POWER_SUPPLY_PROP_CHARGE_FULL:
607 ret = bq27x00_simple_value(di->cache.charge_full, val);
609 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
610 ret = bq27x00_simple_value(di->charge_design_full, val);
612 case POWER_SUPPLY_PROP_CYCLE_COUNT:
613 ret = bq27x00_simple_value(di->cache.cycle_count, val);
615 case POWER_SUPPLY_PROP_ENERGY_NOW:
616 ret = bq27x00_simple_value(di->cache.energy, val);
618 case POWER_SUPPLY_PROP_POWER_AVG:
619 ret = bq27x00_simple_value(di->cache.power_avg, val);
621 case POWER_SUPPLY_PROP_HEALTH:
622 ret = bq27x00_simple_value(di->cache.health, val);
631 static void bq27x00_external_power_changed(struct power_supply *psy)
633 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
635 cancel_delayed_work_sync(&di->work);
636 schedule_delayed_work(&di->work, 0);
639 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
643 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
644 di->bat.properties = bq27x00_battery_props;
645 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
646 di->bat.get_property = bq27x00_battery_get_property;
647 di->bat.external_power_changed = bq27x00_external_power_changed;
649 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
650 mutex_init(&di->lock);
652 ret = power_supply_register(di->dev, &di->bat);
654 dev_err(di->dev, "failed to register battery: %d\n", ret);
658 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
665 static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
668 * power_supply_unregister call bq27x00_battery_get_property which
669 * call bq27x00_battery_poll.
670 * Make sure that bq27x00_battery_poll will not call
671 * schedule_delayed_work again after unregister (which cause OOPS).
675 cancel_delayed_work_sync(&di->work);
677 power_supply_unregister(&di->bat);
679 mutex_destroy(&di->lock);
683 /* i2c specific code */
684 #ifdef CONFIG_BATTERY_BQ27X00_I2C
686 /* If the system has several batteries we need a different name for each
689 static DEFINE_IDR(battery_id);
690 static DEFINE_MUTEX(battery_mutex);
692 static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
694 struct i2c_client *client = to_i2c_client(di->dev);
695 struct i2c_msg msg[2];
696 unsigned char data[2];
699 if (!client->adapter)
702 msg[0].addr = client->addr;
705 msg[0].len = sizeof(reg);
706 msg[1].addr = client->addr;
707 msg[1].flags = I2C_M_RD;
714 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
719 ret = get_unaligned_le16(data);
726 static int bq27x00_battery_probe(struct i2c_client *client,
727 const struct i2c_device_id *id)
730 struct bq27x00_device_info *di;
734 /* Get new ID for the new battery device */
735 retval = idr_pre_get(&battery_id, GFP_KERNEL);
738 mutex_lock(&battery_mutex);
739 retval = idr_get_new(&battery_id, client, &num);
740 mutex_unlock(&battery_mutex);
744 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
746 dev_err(&client->dev, "failed to allocate device name\n");
751 di = kzalloc(sizeof(*di), GFP_KERNEL);
753 dev_err(&client->dev, "failed to allocate device info data\n");
759 di->dev = &client->dev;
760 di->chip = id->driver_data;
762 di->bus.read = &bq27x00_read_i2c;
764 if (bq27x00_powersupply_init(di))
767 i2c_set_clientdata(client, di);
776 mutex_lock(&battery_mutex);
777 idr_remove(&battery_id, num);
778 mutex_unlock(&battery_mutex);
783 static int bq27x00_battery_remove(struct i2c_client *client)
785 struct bq27x00_device_info *di = i2c_get_clientdata(client);
787 bq27x00_powersupply_unregister(di);
791 mutex_lock(&battery_mutex);
792 idr_remove(&battery_id, di->id);
793 mutex_unlock(&battery_mutex);
800 static const struct i2c_device_id bq27x00_id[] = {
801 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
802 { "bq27500", BQ27500 },
805 MODULE_DEVICE_TABLE(i2c, bq27x00_id);
807 static struct i2c_driver bq27x00_battery_driver = {
809 .name = "bq27x00-battery",
811 .probe = bq27x00_battery_probe,
812 .remove = bq27x00_battery_remove,
813 .id_table = bq27x00_id,
816 static inline int bq27x00_battery_i2c_init(void)
818 int ret = i2c_add_driver(&bq27x00_battery_driver);
820 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
825 static inline void bq27x00_battery_i2c_exit(void)
827 i2c_del_driver(&bq27x00_battery_driver);
832 static inline int bq27x00_battery_i2c_init(void) { return 0; }
833 static inline void bq27x00_battery_i2c_exit(void) {};
837 /* platform specific code */
838 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
840 static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
843 struct device *dev = di->dev;
844 struct bq27000_platform_data *pdata = dev->platform_data;
845 unsigned int timeout = 3;
850 /* Make sure the value has not changed in between reading the
851 * lower and the upper part */
852 upper = pdata->read(dev, reg + 1);
858 lower = pdata->read(dev, reg);
862 upper = pdata->read(dev, reg + 1);
863 } while (temp != upper && --timeout);
868 return (upper << 8) | lower;
871 return pdata->read(dev, reg);
874 static int __devinit bq27000_battery_probe(struct platform_device *pdev)
876 struct bq27x00_device_info *di;
877 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
881 dev_err(&pdev->dev, "no platform_data supplied\n");
886 dev_err(&pdev->dev, "no hdq read callback supplied\n");
890 di = kzalloc(sizeof(*di), GFP_KERNEL);
892 dev_err(&pdev->dev, "failed to allocate device info data\n");
896 platform_set_drvdata(pdev, di);
898 di->dev = &pdev->dev;
901 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
902 di->bus.read = &bq27000_read_platform;
904 ret = bq27x00_powersupply_init(di);
911 platform_set_drvdata(pdev, NULL);
917 static int __devexit bq27000_battery_remove(struct platform_device *pdev)
919 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
921 bq27x00_powersupply_unregister(di);
923 platform_set_drvdata(pdev, NULL);
929 static struct platform_driver bq27000_battery_driver = {
930 .probe = bq27000_battery_probe,
931 .remove = __devexit_p(bq27000_battery_remove),
933 .name = "bq27000-battery",
934 .owner = THIS_MODULE,
938 static inline int bq27x00_battery_platform_init(void)
940 int ret = platform_driver_register(&bq27000_battery_driver);
942 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
947 static inline void bq27x00_battery_platform_exit(void)
949 platform_driver_unregister(&bq27000_battery_driver);
954 static inline int bq27x00_battery_platform_init(void) { return 0; }
955 static inline void bq27x00_battery_platform_exit(void) {};
963 static int __init bq27x00_battery_init(void)
967 ret = bq27x00_battery_i2c_init();
971 ret = bq27x00_battery_platform_init();
973 bq27x00_battery_i2c_exit();
977 module_init(bq27x00_battery_init);
979 static void __exit bq27x00_battery_exit(void)
981 bq27x00_battery_platform_exit();
982 bq27x00_battery_i2c_exit();
984 module_exit(bq27x00_battery_exit);
986 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
987 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
988 MODULE_LICENSE("GPL");