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
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
26 #include <linux/power/sbs-battery.h>
27 #include <linux/power_supply.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
32 REG_MANUFACTURER_DATA,
43 REG_REMAINING_CAPACITY,
44 REG_REMAINING_CAPACITY_CHARGE,
45 REG_FULL_CHARGE_CAPACITY,
46 REG_FULL_CHARGE_CAPACITY_CHARGE,
48 REG_DESIGN_CAPACITY_CHARGE,
49 REG_DESIGN_VOLTAGE_MIN,
50 REG_DESIGN_VOLTAGE_MAX,
55 /* Battery Mode defines */
56 #define BATTERY_MODE_OFFSET 0x03
57 #define BATTERY_MODE_MASK 0x8000
58 enum sbs_battery_mode {
59 BATTERY_MODE_AMPS = 0,
60 BATTERY_MODE_WATTS = 0x8000
63 /* manufacturer access defines */
64 #define MANUFACTURER_ACCESS_STATUS 0x0006
65 #define MANUFACTURER_ACCESS_SLEEP 0x0011
67 /* battery status value bits */
68 #define BATTERY_INITIALIZED 0x80
69 #define BATTERY_DISCHARGING 0x40
70 #define BATTERY_FULL_CHARGED 0x20
71 #define BATTERY_FULL_DISCHARGED 0x10
73 /* min_value and max_value are only valid for numerical data */
74 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
77 .min_value = _min_value, \
78 .max_value = _max_value, \
81 static const struct chip_data {
82 enum power_supply_property psp;
87 [REG_MANUFACTURER_DATA] =
88 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
90 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
92 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
94 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
96 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
97 [REG_REMAINING_CAPACITY] =
98 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
99 [REG_REMAINING_CAPACITY_CHARGE] =
100 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
101 [REG_FULL_CHARGE_CAPACITY] =
102 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
103 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
104 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
105 [REG_TIME_TO_EMPTY] =
106 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
108 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
110 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
111 [REG_CAPACITY_LEVEL] =
112 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
114 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
115 [REG_DESIGN_CAPACITY] =
116 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
117 [REG_DESIGN_CAPACITY_CHARGE] =
118 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
119 [REG_DESIGN_VOLTAGE_MIN] =
120 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
121 [REG_DESIGN_VOLTAGE_MAX] =
122 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
123 [REG_SERIAL_NUMBER] =
124 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
125 /* Properties of type `const char *' */
127 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
129 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
132 static enum power_supply_property sbs_properties[] = {
133 POWER_SUPPLY_PROP_STATUS,
134 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
135 POWER_SUPPLY_PROP_HEALTH,
136 POWER_SUPPLY_PROP_PRESENT,
137 POWER_SUPPLY_PROP_TECHNOLOGY,
138 POWER_SUPPLY_PROP_CYCLE_COUNT,
139 POWER_SUPPLY_PROP_VOLTAGE_NOW,
140 POWER_SUPPLY_PROP_CURRENT_NOW,
141 POWER_SUPPLY_PROP_CAPACITY,
142 POWER_SUPPLY_PROP_TEMP,
143 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
144 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
145 POWER_SUPPLY_PROP_SERIAL_NUMBER,
146 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
147 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
148 POWER_SUPPLY_PROP_ENERGY_NOW,
149 POWER_SUPPLY_PROP_ENERGY_FULL,
150 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
151 POWER_SUPPLY_PROP_CHARGE_NOW,
152 POWER_SUPPLY_PROP_CHARGE_FULL,
153 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
154 /* Properties of type `const char *' */
155 POWER_SUPPLY_PROP_MANUFACTURER,
156 POWER_SUPPLY_PROP_MODEL_NAME
160 struct i2c_client *client;
161 struct power_supply *power_supply;
163 struct gpio_desc *gpio_detect;
164 bool enable_detection;
168 u32 poll_retry_count;
169 struct delayed_work work;
170 struct mutex mode_lock;
173 static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
174 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
175 static bool force_load;
177 static int sbs_read_word_data(struct i2c_client *client, u8 address)
179 struct sbs_info *chip = i2c_get_clientdata(client);
183 retries = chip->i2c_retry_count;
185 while (retries > 0) {
186 ret = i2c_smbus_read_word_data(client, address);
193 dev_dbg(&client->dev,
194 "%s: i2c read at address 0x%x failed\n",
202 static int sbs_read_string_data(struct i2c_client *client, u8 address,
205 struct sbs_info *chip = i2c_get_clientdata(client);
206 s32 ret = 0, block_length = 0;
207 int retries_length = 1, retries_block = 1;
208 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
210 retries_length = chip->i2c_retry_count;
211 retries_block = chip->i2c_retry_count;
213 /* Adapter needs to support these two functions */
214 if (!i2c_check_functionality(client->adapter,
215 I2C_FUNC_SMBUS_BYTE_DATA |
216 I2C_FUNC_SMBUS_I2C_BLOCK)){
220 /* Get the length of block data */
221 while (retries_length > 0) {
222 ret = i2c_smbus_read_byte_data(client, address);
229 dev_dbg(&client->dev,
230 "%s: i2c read at address 0x%x failed\n",
235 /* block_length does not include NULL terminator */
237 if (block_length > I2C_SMBUS_BLOCK_MAX) {
238 dev_err(&client->dev,
239 "%s: Returned block_length is longer than 0x%x\n",
240 __func__, I2C_SMBUS_BLOCK_MAX);
244 /* Get the block data */
245 while (retries_block > 0) {
246 ret = i2c_smbus_read_i2c_block_data(
248 block_length + 1, block_buffer);
255 dev_dbg(&client->dev,
256 "%s: i2c read at address 0x%x failed\n",
261 /* block_buffer[0] == block_length */
262 memcpy(values, block_buffer + 1, block_length);
263 values[block_length] = '\0';
268 static int sbs_write_word_data(struct i2c_client *client, u8 address,
271 struct sbs_info *chip = i2c_get_clientdata(client);
275 retries = chip->i2c_retry_count;
277 while (retries > 0) {
278 ret = i2c_smbus_write_word_data(client, address, value);
285 dev_dbg(&client->dev,
286 "%s: i2c write to address 0x%x failed\n",
294 static int sbs_status_correct(struct i2c_client *client, int *intval)
298 ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
304 /* Not drawing current means full (cannot be not charging) */
306 *intval = POWER_SUPPLY_STATUS_FULL;
308 if (*intval == POWER_SUPPLY_STATUS_FULL) {
309 /* Drawing or providing current when full */
311 *intval = POWER_SUPPLY_STATUS_CHARGING;
313 *intval = POWER_SUPPLY_STATUS_DISCHARGING;
319 static int sbs_get_battery_presence_and_health(
320 struct i2c_client *client, enum power_supply_property psp,
321 union power_supply_propval *val)
324 struct sbs_info *chip = i2c_get_clientdata(client);
326 if (psp == POWER_SUPPLY_PROP_PRESENT && chip->gpio_detect) {
327 ret = gpiod_get_value_cansleep(chip->gpio_detect);
331 chip->is_present = val->intval;
336 * Write to ManufacturerAccess with ManufacturerAccess command
337 * and then read the status. Do not check for error on the write
338 * since not all batteries implement write access to this command,
339 * while others mandate it.
341 sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
342 MANUFACTURER_ACCESS_STATUS);
344 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
346 if (psp == POWER_SUPPLY_PROP_PRESENT)
347 val->intval = 0; /* battery removed */
351 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
352 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
357 /* Mask the upper nibble of 2nd byte and
358 * lower byte of response then
359 * shift the result by 8 to get status*/
362 if (psp == POWER_SUPPLY_PROP_PRESENT) {
364 /* battery removed */
368 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
370 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
371 else if (ret == 0x0B)
372 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
373 else if (ret == 0x0C)
374 val->intval = POWER_SUPPLY_HEALTH_DEAD;
376 val->intval = POWER_SUPPLY_HEALTH_GOOD;
382 static int sbs_get_battery_property(struct i2c_client *client,
383 int reg_offset, enum power_supply_property psp,
384 union power_supply_propval *val)
386 struct sbs_info *chip = i2c_get_clientdata(client);
389 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
393 /* returned values are 16 bit */
394 if (sbs_data[reg_offset].min_value < 0)
397 if (ret >= sbs_data[reg_offset].min_value &&
398 ret <= sbs_data[reg_offset].max_value) {
400 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
401 if (!(ret & BATTERY_INITIALIZED))
403 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
404 else if (ret & BATTERY_FULL_CHARGED)
406 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
407 else if (ret & BATTERY_FULL_DISCHARGED)
409 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
412 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
414 } else if (psp != POWER_SUPPLY_PROP_STATUS) {
418 if (ret & BATTERY_FULL_CHARGED)
419 val->intval = POWER_SUPPLY_STATUS_FULL;
420 else if (ret & BATTERY_DISCHARGING)
421 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
423 val->intval = POWER_SUPPLY_STATUS_CHARGING;
425 sbs_status_correct(client, &val->intval);
427 if (chip->poll_time == 0)
428 chip->last_state = val->intval;
429 else if (chip->last_state != val->intval) {
430 cancel_delayed_work_sync(&chip->work);
431 power_supply_changed(chip->power_supply);
435 if (psp == POWER_SUPPLY_PROP_STATUS)
436 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
437 else if (psp == POWER_SUPPLY_PROP_CAPACITY)
438 /* sbs spec says that this can be >100 %
439 * even if max value is 100 %
441 val->intval = min(ret, 100);
449 static int sbs_get_battery_string_property(struct i2c_client *client,
450 int reg_offset, enum power_supply_property psp, char *val)
454 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
462 static void sbs_unit_adjustment(struct i2c_client *client,
463 enum power_supply_property psp, union power_supply_propval *val)
465 #define BASE_UNIT_CONVERSION 1000
466 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
467 #define TIME_UNIT_CONVERSION 60
468 #define TEMP_KELVIN_TO_CELSIUS 2731
470 case POWER_SUPPLY_PROP_ENERGY_NOW:
471 case POWER_SUPPLY_PROP_ENERGY_FULL:
472 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
473 /* sbs provides energy in units of 10mWh.
476 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
479 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
480 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
481 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
482 case POWER_SUPPLY_PROP_CURRENT_NOW:
483 case POWER_SUPPLY_PROP_CHARGE_NOW:
484 case POWER_SUPPLY_PROP_CHARGE_FULL:
485 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
486 val->intval *= BASE_UNIT_CONVERSION;
489 case POWER_SUPPLY_PROP_TEMP:
490 /* sbs provides battery temperature in 0.1K
491 * so convert it to 0.1°C
493 val->intval -= TEMP_KELVIN_TO_CELSIUS;
496 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
497 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
498 /* sbs provides time to empty and time to full in minutes.
501 val->intval *= TIME_UNIT_CONVERSION;
505 dev_dbg(&client->dev,
506 "%s: no need for unit conversion %d\n", __func__, psp);
510 static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
511 enum sbs_battery_mode mode)
513 int ret, original_val;
515 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
516 if (original_val < 0)
519 if ((original_val & BATTERY_MODE_MASK) == mode)
522 if (mode == BATTERY_MODE_AMPS)
523 ret = original_val & ~BATTERY_MODE_MASK;
525 ret = original_val | BATTERY_MODE_MASK;
527 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
531 usleep_range(1000, 2000);
533 return original_val & BATTERY_MODE_MASK;
536 static int sbs_get_battery_capacity(struct i2c_client *client,
537 int reg_offset, enum power_supply_property psp,
538 union power_supply_propval *val)
541 enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
543 if (power_supply_is_amp_property(psp))
544 mode = BATTERY_MODE_AMPS;
546 mode = sbs_set_battery_mode(client, mode);
550 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
556 ret = sbs_set_battery_mode(client, mode);
563 static char sbs_serial[5];
564 static int sbs_get_battery_serial_number(struct i2c_client *client,
565 union power_supply_propval *val)
569 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
573 ret = sprintf(sbs_serial, "%04x", ret);
574 val->strval = sbs_serial;
579 static int sbs_get_property_index(struct i2c_client *client,
580 enum power_supply_property psp)
583 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
584 if (psp == sbs_data[count].psp)
587 dev_warn(&client->dev,
588 "%s: Invalid Property - %d\n", __func__, psp);
593 static int sbs_get_property(struct power_supply *psy,
594 enum power_supply_property psp,
595 union power_supply_propval *val)
598 struct sbs_info *chip = power_supply_get_drvdata(psy);
599 struct i2c_client *client = chip->client;
602 case POWER_SUPPLY_PROP_PRESENT:
603 case POWER_SUPPLY_PROP_HEALTH:
604 ret = sbs_get_battery_presence_and_health(client, psp, val);
605 if (psp == POWER_SUPPLY_PROP_PRESENT)
609 case POWER_SUPPLY_PROP_TECHNOLOGY:
610 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
611 goto done; /* don't trigger power_supply_changed()! */
613 case POWER_SUPPLY_PROP_ENERGY_NOW:
614 case POWER_SUPPLY_PROP_ENERGY_FULL:
615 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
616 case POWER_SUPPLY_PROP_CHARGE_NOW:
617 case POWER_SUPPLY_PROP_CHARGE_FULL:
618 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
619 ret = sbs_get_property_index(client, psp);
623 /* sbs_get_battery_capacity() will change the battery mode
624 * temporarily to read the requested attribute. Ensure we stay
625 * in the desired mode for the duration of the attribute read.
627 mutex_lock(&chip->mode_lock);
628 ret = sbs_get_battery_capacity(client, ret, psp, val);
629 mutex_unlock(&chip->mode_lock);
632 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
633 ret = sbs_get_battery_serial_number(client, val);
636 case POWER_SUPPLY_PROP_STATUS:
637 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
638 case POWER_SUPPLY_PROP_CYCLE_COUNT:
639 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
640 case POWER_SUPPLY_PROP_CURRENT_NOW:
641 case POWER_SUPPLY_PROP_TEMP:
642 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
643 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
644 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
645 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
646 case POWER_SUPPLY_PROP_CAPACITY:
647 ret = sbs_get_property_index(client, psp);
651 ret = sbs_get_battery_property(client, ret, psp, val);
654 case POWER_SUPPLY_PROP_MODEL_NAME:
655 ret = sbs_get_property_index(client, psp);
659 ret = sbs_get_battery_string_property(client, ret, psp,
661 val->strval = model_name;
664 case POWER_SUPPLY_PROP_MANUFACTURER:
665 ret = sbs_get_property_index(client, psp);
669 ret = sbs_get_battery_string_property(client, ret, psp,
671 val->strval = manufacturer;
675 dev_err(&client->dev,
676 "%s: INVALID property\n", __func__);
680 if (!chip->enable_detection)
683 if (!chip->gpio_detect &&
684 chip->is_present != (ret >= 0)) {
685 chip->is_present = (ret >= 0);
686 power_supply_changed(chip->power_supply);
691 /* Convert units to match requirements for power supply class */
692 sbs_unit_adjustment(client, psp, val);
695 dev_dbg(&client->dev,
696 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
698 if (ret && chip->is_present)
701 /* battery not present, so return NODATA for properties */
708 static void sbs_supply_changed(struct sbs_info *chip)
710 struct power_supply *battery = chip->power_supply;
713 ret = gpiod_get_value_cansleep(chip->gpio_detect);
716 chip->is_present = ret;
717 power_supply_changed(battery);
720 static irqreturn_t sbs_irq(int irq, void *devid)
722 sbs_supply_changed(devid);
726 static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
729 sbs_supply_changed(i2c_get_clientdata(client));
732 static void sbs_external_power_changed(struct power_supply *psy)
734 struct sbs_info *chip = power_supply_get_drvdata(psy);
736 /* cancel outstanding work */
737 cancel_delayed_work_sync(&chip->work);
739 schedule_delayed_work(&chip->work, HZ);
740 chip->poll_time = chip->poll_retry_count;
743 static void sbs_delayed_work(struct work_struct *work)
745 struct sbs_info *chip;
748 chip = container_of(work, struct sbs_info, work.work);
750 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
751 /* if the read failed, give up on this work */
757 if (ret & BATTERY_FULL_CHARGED)
758 ret = POWER_SUPPLY_STATUS_FULL;
759 else if (ret & BATTERY_DISCHARGING)
760 ret = POWER_SUPPLY_STATUS_DISCHARGING;
762 ret = POWER_SUPPLY_STATUS_CHARGING;
764 sbs_status_correct(chip->client, &ret);
766 if (chip->last_state != ret) {
768 power_supply_changed(chip->power_supply);
771 if (chip->poll_time > 0) {
772 schedule_delayed_work(&chip->work, HZ);
778 static const struct power_supply_desc sbs_default_desc = {
779 .type = POWER_SUPPLY_TYPE_BATTERY,
780 .properties = sbs_properties,
781 .num_properties = ARRAY_SIZE(sbs_properties),
782 .get_property = sbs_get_property,
783 .external_power_changed = sbs_external_power_changed,
786 static int sbs_probe(struct i2c_client *client,
787 const struct i2c_device_id *id)
789 struct sbs_info *chip;
790 struct power_supply_desc *sbs_desc;
791 struct sbs_platform_data *pdata = client->dev.platform_data;
792 struct power_supply_config psy_cfg = {};
796 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
797 sizeof(*sbs_desc), GFP_KERNEL);
801 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
802 dev_name(&client->dev));
806 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
810 chip->client = client;
811 chip->enable_detection = false;
812 psy_cfg.of_node = client->dev.of_node;
813 psy_cfg.drv_data = chip;
814 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
815 mutex_init(&chip->mode_lock);
817 /* use pdata if available, fall back to DT properties,
818 * or hardcoded defaults if not
820 rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
821 &chip->i2c_retry_count);
823 chip->i2c_retry_count = 0;
825 rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
826 &chip->poll_retry_count);
828 chip->poll_retry_count = 0;
831 chip->poll_retry_count = pdata->poll_retry_count;
832 chip->i2c_retry_count = pdata->i2c_retry_count;
834 chip->i2c_retry_count = chip->i2c_retry_count + 1;
836 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
837 "sbs,battery-detect", GPIOD_IN);
838 if (IS_ERR(chip->gpio_detect)) {
839 dev_err(&client->dev, "Failed to get gpio: %ld\n",
840 PTR_ERR(chip->gpio_detect));
841 return PTR_ERR(chip->gpio_detect);
844 i2c_set_clientdata(client, chip);
846 if (!chip->gpio_detect)
849 irq = gpiod_to_irq(chip->gpio_detect);
851 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
855 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
856 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
857 dev_name(&client->dev), chip);
859 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
865 * Before we register, we might need to make sure we can actually talk
868 if (!(force_load || chip->gpio_detect)) {
869 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
872 dev_err(&client->dev, "%s: Failed to get device status\n",
878 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
880 if (IS_ERR(chip->power_supply)) {
881 dev_err(&client->dev,
882 "%s: Failed to register power supply\n", __func__);
883 rc = PTR_ERR(chip->power_supply);
887 dev_info(&client->dev,
888 "%s: battery gas gauge device registered\n", client->name);
890 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
892 chip->enable_detection = true;
900 static int sbs_remove(struct i2c_client *client)
902 struct sbs_info *chip = i2c_get_clientdata(client);
904 cancel_delayed_work_sync(&chip->work);
909 #if defined CONFIG_PM_SLEEP
911 static int sbs_suspend(struct device *dev)
913 struct i2c_client *client = to_i2c_client(dev);
914 struct sbs_info *chip = i2c_get_clientdata(client);
916 if (chip->poll_time > 0)
917 cancel_delayed_work_sync(&chip->work);
920 * Write to manufacturer access with sleep command.
921 * Support is manufacturer dependend, so ignore errors.
923 sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
924 MANUFACTURER_ACCESS_SLEEP);
929 static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
930 #define SBS_PM_OPS (&sbs_pm_ops)
933 #define SBS_PM_OPS NULL
936 static const struct i2c_device_id sbs_id[] = {
938 { "sbs-battery", 1 },
941 MODULE_DEVICE_TABLE(i2c, sbs_id);
943 static const struct of_device_id sbs_dt_ids[] = {
944 { .compatible = "sbs,sbs-battery" },
945 { .compatible = "ti,bq20z75" },
948 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
950 static struct i2c_driver sbs_battery_driver = {
952 .remove = sbs_remove,
956 .name = "sbs-battery",
957 .of_match_table = sbs_dt_ids,
961 module_i2c_driver(sbs_battery_driver);
963 MODULE_DESCRIPTION("SBS battery monitor driver");
964 MODULE_LICENSE("GPL");
966 module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
967 MODULE_PARM_DESC(force_load,
968 "Attempt to load the driver even if no battery is connected");