2 * Gas Gauge driver for SBS Compliant Batteries
4 * Copyright (c) 2010, NVIDIA Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/err.h>
25 #include <linux/power_supply.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/gpio.h>
31 #include <linux/power/sbs-battery.h>
34 REG_MANUFACTURER_DATA,
44 REG_REMAINING_CAPACITY,
45 REG_REMAINING_CAPACITY_CHARGE,
46 REG_FULL_CHARGE_CAPACITY,
47 REG_FULL_CHARGE_CAPACITY_CHARGE,
49 REG_DESIGN_CAPACITY_CHARGE,
55 /* Battery Mode defines */
56 #define BATTERY_MODE_OFFSET 0x03
57 #define BATTERY_MODE_MASK 0x8000
58 enum sbs_battery_mode {
63 /* manufacturer access defines */
64 #define MANUFACTURER_ACCESS_STATUS 0x0006
65 #define MANUFACTURER_ACCESS_SLEEP 0x0011
67 /* battery status value bits */
68 #define BATTERY_DISCHARGING 0x40
69 #define BATTERY_FULL_CHARGED 0x20
70 #define BATTERY_FULL_DISCHARGED 0x10
72 /* min_value and max_value are only valid for numerical data */
73 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
76 .min_value = _min_value, \
77 .max_value = _max_value, \
80 static const struct chip_data {
81 enum power_supply_property psp;
86 [REG_MANUFACTURER_DATA] =
87 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
89 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
91 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
93 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
95 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0E, 0, 100),
96 [REG_REMAINING_CAPACITY] =
97 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
98 [REG_REMAINING_CAPACITY_CHARGE] =
99 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
100 [REG_FULL_CHARGE_CAPACITY] =
101 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
102 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
103 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
104 [REG_TIME_TO_EMPTY] =
105 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
107 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
109 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
111 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
112 [REG_DESIGN_CAPACITY] =
113 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
114 [REG_DESIGN_CAPACITY_CHARGE] =
115 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
116 [REG_DESIGN_VOLTAGE] =
117 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
118 [REG_SERIAL_NUMBER] =
119 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
120 /* Properties of type `const char *' */
122 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
124 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
127 static enum power_supply_property sbs_properties[] = {
128 POWER_SUPPLY_PROP_STATUS,
129 POWER_SUPPLY_PROP_HEALTH,
130 POWER_SUPPLY_PROP_PRESENT,
131 POWER_SUPPLY_PROP_TECHNOLOGY,
132 POWER_SUPPLY_PROP_CYCLE_COUNT,
133 POWER_SUPPLY_PROP_VOLTAGE_NOW,
134 POWER_SUPPLY_PROP_CURRENT_NOW,
135 POWER_SUPPLY_PROP_CAPACITY,
136 POWER_SUPPLY_PROP_TEMP,
137 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
138 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
139 POWER_SUPPLY_PROP_SERIAL_NUMBER,
140 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
141 POWER_SUPPLY_PROP_ENERGY_NOW,
142 POWER_SUPPLY_PROP_ENERGY_FULL,
143 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
144 POWER_SUPPLY_PROP_CHARGE_NOW,
145 POWER_SUPPLY_PROP_CHARGE_FULL,
146 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
147 /* Properties of type `const char *' */
148 POWER_SUPPLY_PROP_MANUFACTURER,
149 POWER_SUPPLY_PROP_MODEL_NAME
153 struct i2c_client *client;
154 struct power_supply power_supply;
155 struct sbs_platform_data *pdata;
158 bool enable_detection;
162 struct delayed_work work;
166 static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
167 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
169 static int sbs_read_word_data(struct i2c_client *client, u8 address)
171 struct sbs_info *chip = i2c_get_clientdata(client);
176 retries = max(chip->pdata->i2c_retry_count + 1, 1);
178 while (retries > 0) {
179 ret = i2c_smbus_read_word_data(client, address);
186 dev_dbg(&client->dev,
187 "%s: i2c read at address 0x%x failed\n",
192 return le16_to_cpu(ret);
195 static int sbs_read_string_data(struct i2c_client *client, u8 address,
198 struct sbs_info *chip = i2c_get_clientdata(client);
199 s32 ret = 0, block_length = 0;
200 int retries_length = 1, retries_block = 1;
201 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
204 retries_length = max(chip->pdata->i2c_retry_count + 1, 1);
205 retries_block = max(chip->pdata->i2c_retry_count + 1, 1);
208 /* Adapter needs to support these two functions */
209 if (!i2c_check_functionality(client->adapter,
210 I2C_FUNC_SMBUS_BYTE_DATA |
211 I2C_FUNC_SMBUS_I2C_BLOCK)){
215 /* Get the length of block data */
216 while (retries_length > 0) {
217 ret = i2c_smbus_read_byte_data(client, address);
224 dev_dbg(&client->dev,
225 "%s: i2c read at address 0x%x failed\n",
230 /* block_length does not include NULL terminator */
232 if (block_length > I2C_SMBUS_BLOCK_MAX) {
233 dev_err("%s: Returned block_length is longer than 0x%x\n",
234 __func__, I2C_SMBUS_BLOCK_MAX);
238 /* Get the block data */
239 while (retries_block > 0) {
240 ret = i2c_smbus_read_i2c_block_data(
242 block_length + 1, block_buffer);
249 dev_dbg(&client->dev,
250 "%s: i2c read at address 0x%x failed\n",
255 /* block_buffer[0] == block_length */
256 memcpy(values, block_buffer + 1, block_length);
257 values[block_length] = '\0';
259 return le16_to_cpu(ret);
262 static int sbs_write_word_data(struct i2c_client *client, u8 address,
265 struct sbs_info *chip = i2c_get_clientdata(client);
270 retries = max(chip->pdata->i2c_retry_count + 1, 1);
272 while (retries > 0) {
273 ret = i2c_smbus_write_word_data(client, address,
281 dev_dbg(&client->dev,
282 "%s: i2c write to address 0x%x failed\n",
290 static int sbs_get_battery_presence_and_health(
291 struct i2c_client *client, enum power_supply_property psp,
292 union power_supply_propval *val)
295 struct sbs_info *chip = i2c_get_clientdata(client);
297 if (psp == POWER_SUPPLY_PROP_PRESENT &&
299 ret = gpio_get_value(chip->pdata->battery_detect);
300 if (ret == chip->pdata->battery_detect_present)
304 chip->is_present = val->intval;
308 /* Write to ManufacturerAccess with
309 * ManufacturerAccess command and then
311 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
312 MANUFACTURER_ACCESS_STATUS);
314 if (psp == POWER_SUPPLY_PROP_PRESENT)
315 val->intval = 0; /* battery removed */
319 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
323 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
324 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
329 /* Mask the upper nibble of 2nd byte and
330 * lower byte of response then
331 * shift the result by 8 to get status*/
334 if (psp == POWER_SUPPLY_PROP_PRESENT) {
336 /* battery removed */
340 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
342 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
343 else if (ret == 0x0B)
344 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
345 else if (ret == 0x0C)
346 val->intval = POWER_SUPPLY_HEALTH_DEAD;
348 val->intval = POWER_SUPPLY_HEALTH_GOOD;
354 static int sbs_get_battery_property(struct i2c_client *client,
355 int reg_offset, enum power_supply_property psp,
356 union power_supply_propval *val)
358 struct sbs_info *chip = i2c_get_clientdata(client);
361 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
365 /* returned values are 16 bit */
366 if (sbs_data[reg_offset].min_value < 0)
369 if (ret >= sbs_data[reg_offset].min_value &&
370 ret <= sbs_data[reg_offset].max_value) {
372 if (psp != POWER_SUPPLY_PROP_STATUS)
375 if (ret & BATTERY_FULL_CHARGED)
376 val->intval = POWER_SUPPLY_STATUS_FULL;
377 else if (ret & BATTERY_FULL_DISCHARGED)
378 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
379 else if (ret & BATTERY_DISCHARGING)
380 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
382 val->intval = POWER_SUPPLY_STATUS_CHARGING;
384 if (chip->poll_time == 0)
385 chip->last_state = val->intval;
386 else if (chip->last_state != val->intval) {
387 cancel_delayed_work_sync(&chip->work);
388 power_supply_changed(&chip->power_supply);
392 if (psp == POWER_SUPPLY_PROP_STATUS)
393 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
401 static int sbs_get_battery_string_property(struct i2c_client *client,
402 int reg_offset, enum power_supply_property psp, char *val)
406 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
414 static void sbs_unit_adjustment(struct i2c_client *client,
415 enum power_supply_property psp, union power_supply_propval *val)
417 #define BASE_UNIT_CONVERSION 1000
418 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
419 #define TIME_UNIT_CONVERSION 60
420 #define TEMP_KELVIN_TO_CELSIUS 2731
422 case POWER_SUPPLY_PROP_ENERGY_NOW:
423 case POWER_SUPPLY_PROP_ENERGY_FULL:
424 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
425 /* sbs provides energy in units of 10mWh.
428 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
431 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
432 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
433 case POWER_SUPPLY_PROP_CURRENT_NOW:
434 case POWER_SUPPLY_PROP_CHARGE_NOW:
435 case POWER_SUPPLY_PROP_CHARGE_FULL:
436 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
437 val->intval *= BASE_UNIT_CONVERSION;
440 case POWER_SUPPLY_PROP_TEMP:
441 /* sbs provides battery temperature in 0.1K
442 * so convert it to 0.1°C
444 val->intval -= TEMP_KELVIN_TO_CELSIUS;
447 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
448 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
449 /* sbs provides time to empty and time to full in minutes.
452 val->intval *= TIME_UNIT_CONVERSION;
456 dev_dbg(&client->dev,
457 "%s: no need for unit conversion %d\n", __func__, psp);
461 static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
462 enum sbs_battery_mode mode)
464 int ret, original_val;
466 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
467 if (original_val < 0)
470 if ((original_val & BATTERY_MODE_MASK) == mode)
473 if (mode == BATTERY_MODE_AMPS)
474 ret = original_val & ~BATTERY_MODE_MASK;
476 ret = original_val | BATTERY_MODE_MASK;
478 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
482 return original_val & BATTERY_MODE_MASK;
485 static int sbs_get_battery_capacity(struct i2c_client *client,
486 int reg_offset, enum power_supply_property psp,
487 union power_supply_propval *val)
490 enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
492 if (power_supply_is_amp_property(psp))
493 mode = BATTERY_MODE_AMPS;
495 mode = sbs_set_battery_mode(client, mode);
499 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
503 if (psp == POWER_SUPPLY_PROP_CAPACITY) {
504 /* sbs spec says that this can be >100 %
505 * even if max value is 100 % */
506 val->intval = min(ret, 100);
510 ret = sbs_set_battery_mode(client, mode);
517 static char sbs_serial[5];
518 static int sbs_get_battery_serial_number(struct i2c_client *client,
519 union power_supply_propval *val)
523 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
527 ret = sprintf(sbs_serial, "%04x", ret);
528 val->strval = sbs_serial;
533 static int sbs_get_property_index(struct i2c_client *client,
534 enum power_supply_property psp)
537 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
538 if (psp == sbs_data[count].psp)
541 dev_warn(&client->dev,
542 "%s: Invalid Property - %d\n", __func__, psp);
547 static int sbs_get_property(struct power_supply *psy,
548 enum power_supply_property psp,
549 union power_supply_propval *val)
552 struct sbs_info *chip = container_of(psy,
553 struct sbs_info, power_supply);
554 struct i2c_client *client = chip->client;
557 case POWER_SUPPLY_PROP_PRESENT:
558 case POWER_SUPPLY_PROP_HEALTH:
559 ret = sbs_get_battery_presence_and_health(client, psp, val);
560 if (psp == POWER_SUPPLY_PROP_PRESENT)
564 case POWER_SUPPLY_PROP_TECHNOLOGY:
565 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
568 case POWER_SUPPLY_PROP_ENERGY_NOW:
569 case POWER_SUPPLY_PROP_ENERGY_FULL:
570 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
571 case POWER_SUPPLY_PROP_CHARGE_NOW:
572 case POWER_SUPPLY_PROP_CHARGE_FULL:
573 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
574 case POWER_SUPPLY_PROP_CAPACITY:
575 ret = sbs_get_property_index(client, psp);
579 ret = sbs_get_battery_capacity(client, ret, psp, val);
582 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
583 ret = sbs_get_battery_serial_number(client, val);
586 case POWER_SUPPLY_PROP_STATUS:
587 case POWER_SUPPLY_PROP_CYCLE_COUNT:
588 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
589 case POWER_SUPPLY_PROP_CURRENT_NOW:
590 case POWER_SUPPLY_PROP_TEMP:
591 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
592 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
593 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
594 ret = sbs_get_property_index(client, psp);
598 ret = sbs_get_battery_property(client, ret, psp, val);
601 case POWER_SUPPLY_PROP_MODEL_NAME:
602 ret = sbs_get_property_index(client, psp);
606 ret = sbs_get_battery_string_property(client, ret, psp,
608 val->strval = model_name;
611 case POWER_SUPPLY_PROP_MANUFACTURER:
612 ret = sbs_get_property_index(client, psp);
616 ret = sbs_get_battery_string_property(client, ret, psp,
618 val->strval = manufacturer;
622 dev_err(&client->dev,
623 "%s: INVALID property\n", __func__);
627 if (!chip->enable_detection)
630 if (!chip->gpio_detect &&
631 chip->is_present != (ret >= 0)) {
632 chip->is_present = (ret >= 0);
633 power_supply_changed(&chip->power_supply);
638 /* Convert units to match requirements for power supply class */
639 sbs_unit_adjustment(client, psp, val);
642 dev_dbg(&client->dev,
643 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
645 if (ret && chip->is_present)
648 /* battery not present, so return NODATA for properties */
655 static irqreturn_t sbs_irq(int irq, void *devid)
657 struct power_supply *battery = devid;
659 power_supply_changed(battery);
664 static void sbs_external_power_changed(struct power_supply *psy)
666 struct sbs_info *chip;
668 chip = container_of(psy, struct sbs_info, power_supply);
670 if (chip->ignore_changes > 0) {
671 chip->ignore_changes--;
675 /* cancel outstanding work */
676 cancel_delayed_work_sync(&chip->work);
678 schedule_delayed_work(&chip->work, HZ);
679 chip->poll_time = chip->pdata->poll_retry_count;
682 static void sbs_delayed_work(struct work_struct *work)
684 struct sbs_info *chip;
687 chip = container_of(work, struct sbs_info, work.work);
689 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
690 /* if the read failed, give up on this work */
696 if (ret & BATTERY_FULL_CHARGED)
697 ret = POWER_SUPPLY_STATUS_FULL;
698 else if (ret & BATTERY_FULL_DISCHARGED)
699 ret = POWER_SUPPLY_STATUS_NOT_CHARGING;
700 else if (ret & BATTERY_DISCHARGING)
701 ret = POWER_SUPPLY_STATUS_DISCHARGING;
703 ret = POWER_SUPPLY_STATUS_CHARGING;
705 if (chip->last_state != ret) {
707 power_supply_changed(&chip->power_supply);
710 if (chip->poll_time > 0) {
711 schedule_delayed_work(&chip->work, HZ);
717 #if defined(CONFIG_OF)
719 #include <linux/of_device.h>
720 #include <linux/of_gpio.h>
722 static const struct of_device_id sbs_dt_ids[] = {
723 { .compatible = "sbs,sbs-battery" },
724 { .compatible = "ti,bq20z75" },
727 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
729 static struct sbs_platform_data *sbs_of_populate_pdata(
730 struct i2c_client *client)
732 struct device_node *of_node = client->dev.of_node;
733 struct sbs_platform_data *pdata = client->dev.platform_data;
734 enum of_gpio_flags gpio_flags;
738 /* verify this driver matches this device */
742 /* if platform data is set, honor it */
746 /* first make sure at least one property is set, otherwise
747 * it won't change behavior from running without pdata.
749 if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) &&
750 !of_get_property(of_node, "sbs,poll-retry-count", NULL) &&
751 !of_get_property(of_node, "sbs,battery-detect-gpios", NULL))
754 pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data),
759 rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop);
761 pdata->i2c_retry_count = prop;
763 rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop);
765 pdata->poll_retry_count = prop;
767 if (!of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) {
768 pdata->battery_detect = -1;
772 pdata->battery_detect = of_get_named_gpio_flags(of_node,
773 "sbs,battery-detect-gpios", 0, &gpio_flags);
775 if (gpio_flags & OF_GPIO_ACTIVE_LOW)
776 pdata->battery_detect_present = 0;
778 pdata->battery_detect_present = 1;
784 #define sbs_dt_ids NULL
785 static struct sbs_platform_data *sbs_of_populate_pdata(
786 struct i2c_client *client)
788 return client->dev.platform_data;
792 static int __devinit sbs_probe(struct i2c_client *client,
793 const struct i2c_device_id *id)
795 struct sbs_info *chip;
796 struct sbs_platform_data *pdata = client->dev.platform_data;
801 name = kasprintf(GFP_KERNEL, "sbs-%s", dev_name(&client->dev));
803 dev_err(&client->dev, "Failed to allocate device name\n");
807 chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL);
813 chip->client = client;
814 chip->enable_detection = false;
815 chip->gpio_detect = false;
816 chip->power_supply.name = name;
817 chip->power_supply.type = POWER_SUPPLY_TYPE_BATTERY;
818 chip->power_supply.properties = sbs_properties;
819 chip->power_supply.num_properties = ARRAY_SIZE(sbs_properties);
820 chip->power_supply.get_property = sbs_get_property;
821 /* ignore first notification of external change, it is generated
822 * from the power_supply_register call back
824 chip->ignore_changes = 1;
825 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
826 chip->power_supply.external_power_changed = sbs_external_power_changed;
828 pdata = sbs_of_populate_pdata(client);
831 chip->gpio_detect = gpio_is_valid(pdata->battery_detect);
835 i2c_set_clientdata(client, chip);
837 if (!chip->gpio_detect)
840 rc = gpio_request(pdata->battery_detect, dev_name(&client->dev));
842 dev_warn(&client->dev, "Failed to request gpio: %d\n", rc);
843 chip->gpio_detect = false;
847 rc = gpio_direction_input(pdata->battery_detect);
849 dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc);
850 gpio_free(pdata->battery_detect);
851 chip->gpio_detect = false;
855 irq = gpio_to_irq(pdata->battery_detect);
857 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
858 gpio_free(pdata->battery_detect);
859 chip->gpio_detect = false;
863 rc = request_irq(irq, sbs_irq,
864 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
865 dev_name(&client->dev), &chip->power_supply);
867 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
868 gpio_free(pdata->battery_detect);
869 chip->gpio_detect = false;
877 /* Before we register, we need to make sure we can actually talk
880 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
882 dev_err(&client->dev, "%s: Failed to get device status\n",
887 rc = power_supply_register(&client->dev, &chip->power_supply);
889 dev_err(&client->dev,
890 "%s: Failed to register power supply\n", __func__);
894 dev_info(&client->dev,
895 "%s: battery gas gauge device registered\n", client->name);
897 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
899 chip->enable_detection = true;
905 free_irq(chip->irq, &chip->power_supply);
906 if (chip->gpio_detect)
907 gpio_free(pdata->battery_detect);
917 static int __devexit sbs_remove(struct i2c_client *client)
919 struct sbs_info *chip = i2c_get_clientdata(client);
922 free_irq(chip->irq, &chip->power_supply);
923 if (chip->gpio_detect)
924 gpio_free(chip->pdata->battery_detect);
926 power_supply_unregister(&chip->power_supply);
928 cancel_delayed_work_sync(&chip->work);
930 kfree(chip->power_supply.name);
937 #if defined CONFIG_PM
938 static int sbs_suspend(struct i2c_client *client,
941 struct sbs_info *chip = i2c_get_clientdata(client);
944 if (chip->poll_time > 0)
945 cancel_delayed_work_sync(&chip->work);
947 /* write to manufacturer access with sleep command */
948 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
949 MANUFACTURER_ACCESS_SLEEP);
950 if (chip->is_present && ret < 0)
956 #define sbs_suspend NULL
958 /* any smbus transaction will wake up sbs */
959 #define sbs_resume NULL
961 static const struct i2c_device_id sbs_id[] = {
963 { "sbs-battery", 1 },
966 MODULE_DEVICE_TABLE(i2c, sbs_id);
968 static struct i2c_driver sbs_battery_driver = {
970 .remove = __devexit_p(sbs_remove),
971 .suspend = sbs_suspend,
972 .resume = sbs_resume,
975 .name = "sbs-battery",
976 .of_match_table = sbs_dt_ids,
979 module_i2c_driver(sbs_battery_driver);
981 MODULE_DESCRIPTION("SBS battery monitor driver");
982 MODULE_LICENSE("GPL");