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/of_device.h>
27 #include <linux/power/sbs-battery.h>
28 #include <linux/power_supply.h>
29 #include <linux/slab.h>
30 #include <linux/stat.h>
33 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,
50 REG_DESIGN_VOLTAGE_MIN,
51 REG_DESIGN_VOLTAGE_MAX,
56 /* Battery Mode defines */
57 #define BATTERY_MODE_OFFSET 0x03
58 #define BATTERY_MODE_MASK 0x8000
59 enum sbs_battery_mode {
60 BATTERY_MODE_AMPS = 0,
61 BATTERY_MODE_WATTS = 0x8000
64 /* manufacturer access defines */
65 #define MANUFACTURER_ACCESS_STATUS 0x0006
66 #define MANUFACTURER_ACCESS_SLEEP 0x0011
68 /* battery status value bits */
69 #define BATTERY_INITIALIZED 0x80
70 #define BATTERY_DISCHARGING 0x40
71 #define BATTERY_FULL_CHARGED 0x20
72 #define BATTERY_FULL_DISCHARGED 0x10
74 /* min_value and max_value are only valid for numerical data */
75 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
78 .min_value = _min_value, \
79 .max_value = _max_value, \
82 static const struct chip_data {
83 enum power_supply_property psp;
88 [REG_MANUFACTURER_DATA] =
89 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
91 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
93 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
95 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
97 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
98 [REG_REMAINING_CAPACITY] =
99 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
100 [REG_REMAINING_CAPACITY_CHARGE] =
101 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
102 [REG_FULL_CHARGE_CAPACITY] =
103 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
104 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
105 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
106 [REG_TIME_TO_EMPTY] =
107 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
109 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
111 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
112 [REG_CAPACITY_LEVEL] =
113 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
115 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
116 [REG_DESIGN_CAPACITY] =
117 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
118 [REG_DESIGN_CAPACITY_CHARGE] =
119 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
120 [REG_DESIGN_VOLTAGE_MIN] =
121 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
122 [REG_DESIGN_VOLTAGE_MAX] =
123 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
124 [REG_SERIAL_NUMBER] =
125 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
126 /* Properties of type `const char *' */
128 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
130 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
133 static enum power_supply_property sbs_properties[] = {
134 POWER_SUPPLY_PROP_STATUS,
135 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
136 POWER_SUPPLY_PROP_HEALTH,
137 POWER_SUPPLY_PROP_PRESENT,
138 POWER_SUPPLY_PROP_TECHNOLOGY,
139 POWER_SUPPLY_PROP_CYCLE_COUNT,
140 POWER_SUPPLY_PROP_VOLTAGE_NOW,
141 POWER_SUPPLY_PROP_CURRENT_NOW,
142 POWER_SUPPLY_PROP_CAPACITY,
143 POWER_SUPPLY_PROP_TEMP,
144 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
145 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
146 POWER_SUPPLY_PROP_SERIAL_NUMBER,
147 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
148 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
149 POWER_SUPPLY_PROP_ENERGY_NOW,
150 POWER_SUPPLY_PROP_ENERGY_FULL,
151 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
152 POWER_SUPPLY_PROP_CHARGE_NOW,
153 POWER_SUPPLY_PROP_CHARGE_FULL,
154 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
155 /* Properties of type `const char *' */
156 POWER_SUPPLY_PROP_MANUFACTURER,
157 POWER_SUPPLY_PROP_MODEL_NAME
160 /* Supports special manufacturer commands from TI BQ20Z75 IC. */
161 #define SBS_FLAGS_TI_BQ20Z75 BIT(0)
164 struct i2c_client *client;
165 struct power_supply *power_supply;
167 struct gpio_desc *gpio_detect;
168 bool enable_detection;
172 u32 poll_retry_count;
173 struct delayed_work work;
174 struct mutex mode_lock;
178 static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
179 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
180 static bool force_load;
182 static int sbs_read_word_data(struct i2c_client *client, u8 address)
184 struct sbs_info *chip = i2c_get_clientdata(client);
185 int retries = chip->i2c_retry_count;
188 while (retries > 0) {
189 ret = i2c_smbus_read_word_data(client, address);
196 dev_dbg(&client->dev,
197 "%s: i2c read at address 0x%x failed\n",
205 static int sbs_read_string_data(struct i2c_client *client, u8 address,
208 struct sbs_info *chip = i2c_get_clientdata(client);
209 s32 ret = 0, block_length = 0;
210 int retries_length, retries_block;
211 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
213 retries_length = chip->i2c_retry_count;
214 retries_block = chip->i2c_retry_count;
216 /* Adapter needs to support these two functions */
217 if (!i2c_check_functionality(client->adapter,
218 I2C_FUNC_SMBUS_BYTE_DATA |
219 I2C_FUNC_SMBUS_I2C_BLOCK)){
223 /* Get the length of block data */
224 while (retries_length > 0) {
225 ret = i2c_smbus_read_byte_data(client, address);
232 dev_dbg(&client->dev,
233 "%s: i2c read at address 0x%x failed\n",
238 /* block_length does not include NULL terminator */
240 if (block_length > I2C_SMBUS_BLOCK_MAX) {
241 dev_err(&client->dev,
242 "%s: Returned block_length is longer than 0x%x\n",
243 __func__, I2C_SMBUS_BLOCK_MAX);
247 /* Get the block data */
248 while (retries_block > 0) {
249 ret = i2c_smbus_read_i2c_block_data(
251 block_length + 1, block_buffer);
258 dev_dbg(&client->dev,
259 "%s: i2c read at address 0x%x failed\n",
264 /* block_buffer[0] == block_length */
265 memcpy(values, block_buffer + 1, block_length);
266 values[block_length] = '\0';
271 static int sbs_write_word_data(struct i2c_client *client, u8 address,
274 struct sbs_info *chip = i2c_get_clientdata(client);
275 int retries = chip->i2c_retry_count;
278 while (retries > 0) {
279 ret = i2c_smbus_write_word_data(client, address, value);
286 dev_dbg(&client->dev,
287 "%s: i2c write to address 0x%x failed\n",
295 static int sbs_status_correct(struct i2c_client *client, int *intval)
299 ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
305 /* Not drawing current means full (cannot be not charging) */
307 *intval = POWER_SUPPLY_STATUS_FULL;
309 if (*intval == POWER_SUPPLY_STATUS_FULL) {
310 /* Drawing or providing current when full */
312 *intval = POWER_SUPPLY_STATUS_CHARGING;
314 *intval = POWER_SUPPLY_STATUS_DISCHARGING;
320 static int sbs_get_battery_presence_and_health(
321 struct i2c_client *client, enum power_supply_property psp,
322 union power_supply_propval *val)
326 if (psp == POWER_SUPPLY_PROP_PRESENT) {
327 /* Dummy command; if it succeeds, battery is present. */
328 ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
330 val->intval = 0; /* battery disconnected */
332 val->intval = 1; /* battery present */
333 } else { /* POWER_SUPPLY_PROP_HEALTH */
334 /* SBS spec doesn't have a general health command. */
335 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
341 static int sbs_get_ti_battery_presence_and_health(
342 struct i2c_client *client, enum power_supply_property psp,
343 union power_supply_propval *val)
348 * Write to ManufacturerAccess with ManufacturerAccess command
349 * and then read the status.
351 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
352 MANUFACTURER_ACCESS_STATUS);
354 if (psp == POWER_SUPPLY_PROP_PRESENT)
355 val->intval = 0; /* battery removed */
359 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
361 if (psp == POWER_SUPPLY_PROP_PRESENT)
362 val->intval = 0; /* battery removed */
366 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
367 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
372 /* Mask the upper nibble of 2nd byte and
373 * lower byte of response then
374 * shift the result by 8 to get status*/
377 if (psp == POWER_SUPPLY_PROP_PRESENT) {
379 /* battery removed */
383 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
385 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
386 else if (ret == 0x0B)
387 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
388 else if (ret == 0x0C)
389 val->intval = POWER_SUPPLY_HEALTH_DEAD;
391 val->intval = POWER_SUPPLY_HEALTH_GOOD;
397 static int sbs_get_battery_property(struct i2c_client *client,
398 int reg_offset, enum power_supply_property psp,
399 union power_supply_propval *val)
401 struct sbs_info *chip = i2c_get_clientdata(client);
404 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
408 /* returned values are 16 bit */
409 if (sbs_data[reg_offset].min_value < 0)
412 if (ret >= sbs_data[reg_offset].min_value &&
413 ret <= sbs_data[reg_offset].max_value) {
415 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
416 if (!(ret & BATTERY_INITIALIZED))
418 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
419 else if (ret & BATTERY_FULL_CHARGED)
421 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
422 else if (ret & BATTERY_FULL_DISCHARGED)
424 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
427 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
429 } else if (psp != POWER_SUPPLY_PROP_STATUS) {
433 if (ret & BATTERY_FULL_CHARGED)
434 val->intval = POWER_SUPPLY_STATUS_FULL;
435 else if (ret & BATTERY_DISCHARGING)
436 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
438 val->intval = POWER_SUPPLY_STATUS_CHARGING;
440 sbs_status_correct(client, &val->intval);
442 if (chip->poll_time == 0)
443 chip->last_state = val->intval;
444 else if (chip->last_state != val->intval) {
445 cancel_delayed_work_sync(&chip->work);
446 power_supply_changed(chip->power_supply);
450 if (psp == POWER_SUPPLY_PROP_STATUS)
451 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
452 else if (psp == POWER_SUPPLY_PROP_CAPACITY)
453 /* sbs spec says that this can be >100 %
454 * even if max value is 100 %
456 val->intval = min(ret, 100);
464 static int sbs_get_battery_string_property(struct i2c_client *client,
465 int reg_offset, enum power_supply_property psp, char *val)
469 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
477 static void sbs_unit_adjustment(struct i2c_client *client,
478 enum power_supply_property psp, union power_supply_propval *val)
480 #define BASE_UNIT_CONVERSION 1000
481 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
482 #define TIME_UNIT_CONVERSION 60
483 #define TEMP_KELVIN_TO_CELSIUS 2731
485 case POWER_SUPPLY_PROP_ENERGY_NOW:
486 case POWER_SUPPLY_PROP_ENERGY_FULL:
487 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
488 /* sbs provides energy in units of 10mWh.
491 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
494 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
495 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
496 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
497 case POWER_SUPPLY_PROP_CURRENT_NOW:
498 case POWER_SUPPLY_PROP_CHARGE_NOW:
499 case POWER_SUPPLY_PROP_CHARGE_FULL:
500 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
501 val->intval *= BASE_UNIT_CONVERSION;
504 case POWER_SUPPLY_PROP_TEMP:
505 /* sbs provides battery temperature in 0.1K
506 * so convert it to 0.1°C
508 val->intval -= TEMP_KELVIN_TO_CELSIUS;
511 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
512 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
513 /* sbs provides time to empty and time to full in minutes.
516 val->intval *= TIME_UNIT_CONVERSION;
520 dev_dbg(&client->dev,
521 "%s: no need for unit conversion %d\n", __func__, psp);
525 static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
526 enum sbs_battery_mode mode)
528 int ret, original_val;
530 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
531 if (original_val < 0)
534 if ((original_val & BATTERY_MODE_MASK) == mode)
537 if (mode == BATTERY_MODE_AMPS)
538 ret = original_val & ~BATTERY_MODE_MASK;
540 ret = original_val | BATTERY_MODE_MASK;
542 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
546 usleep_range(1000, 2000);
548 return original_val & BATTERY_MODE_MASK;
551 static int sbs_get_battery_capacity(struct i2c_client *client,
552 int reg_offset, enum power_supply_property psp,
553 union power_supply_propval *val)
556 enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
558 if (power_supply_is_amp_property(psp))
559 mode = BATTERY_MODE_AMPS;
561 mode = sbs_set_battery_mode(client, mode);
565 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
571 ret = sbs_set_battery_mode(client, mode);
578 static char sbs_serial[5];
579 static int sbs_get_battery_serial_number(struct i2c_client *client,
580 union power_supply_propval *val)
584 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
588 sprintf(sbs_serial, "%04x", ret);
589 val->strval = sbs_serial;
594 static int sbs_get_property_index(struct i2c_client *client,
595 enum power_supply_property psp)
598 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
599 if (psp == sbs_data[count].psp)
602 dev_warn(&client->dev,
603 "%s: Invalid Property - %d\n", __func__, psp);
608 static int sbs_get_property(struct power_supply *psy,
609 enum power_supply_property psp,
610 union power_supply_propval *val)
613 struct sbs_info *chip = power_supply_get_drvdata(psy);
614 struct i2c_client *client = chip->client;
616 if (chip->gpio_detect) {
617 ret = gpiod_get_value_cansleep(chip->gpio_detect);
620 if (psp == POWER_SUPPLY_PROP_PRESENT) {
622 chip->is_present = val->intval;
630 case POWER_SUPPLY_PROP_PRESENT:
631 case POWER_SUPPLY_PROP_HEALTH:
632 if (client->flags & SBS_FLAGS_TI_BQ20Z75)
633 ret = sbs_get_ti_battery_presence_and_health(client,
636 ret = sbs_get_battery_presence_and_health(client, psp,
638 if (psp == POWER_SUPPLY_PROP_PRESENT)
642 case POWER_SUPPLY_PROP_TECHNOLOGY:
643 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
644 goto done; /* don't trigger power_supply_changed()! */
646 case POWER_SUPPLY_PROP_ENERGY_NOW:
647 case POWER_SUPPLY_PROP_ENERGY_FULL:
648 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
649 case POWER_SUPPLY_PROP_CHARGE_NOW:
650 case POWER_SUPPLY_PROP_CHARGE_FULL:
651 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
652 ret = sbs_get_property_index(client, psp);
656 /* sbs_get_battery_capacity() will change the battery mode
657 * temporarily to read the requested attribute. Ensure we stay
658 * in the desired mode for the duration of the attribute read.
660 mutex_lock(&chip->mode_lock);
661 ret = sbs_get_battery_capacity(client, ret, psp, val);
662 mutex_unlock(&chip->mode_lock);
665 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
666 ret = sbs_get_battery_serial_number(client, val);
669 case POWER_SUPPLY_PROP_STATUS:
670 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
671 case POWER_SUPPLY_PROP_CYCLE_COUNT:
672 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
673 case POWER_SUPPLY_PROP_CURRENT_NOW:
674 case POWER_SUPPLY_PROP_TEMP:
675 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
676 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
677 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
678 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
679 case POWER_SUPPLY_PROP_CAPACITY:
680 ret = sbs_get_property_index(client, psp);
684 ret = sbs_get_battery_property(client, ret, psp, val);
687 case POWER_SUPPLY_PROP_MODEL_NAME:
688 ret = sbs_get_property_index(client, psp);
692 ret = sbs_get_battery_string_property(client, ret, psp,
694 val->strval = model_name;
697 case POWER_SUPPLY_PROP_MANUFACTURER:
698 ret = sbs_get_property_index(client, psp);
702 ret = sbs_get_battery_string_property(client, ret, psp,
704 val->strval = manufacturer;
708 dev_err(&client->dev,
709 "%s: INVALID property\n", __func__);
713 if (!chip->enable_detection)
716 if (!chip->gpio_detect &&
717 chip->is_present != (ret >= 0)) {
718 chip->is_present = (ret >= 0);
719 power_supply_changed(chip->power_supply);
724 /* Convert units to match requirements for power supply class */
725 sbs_unit_adjustment(client, psp, val);
728 dev_dbg(&client->dev,
729 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
731 if (ret && chip->is_present)
734 /* battery not present, so return NODATA for properties */
741 static void sbs_supply_changed(struct sbs_info *chip)
743 struct power_supply *battery = chip->power_supply;
746 ret = gpiod_get_value_cansleep(chip->gpio_detect);
749 chip->is_present = ret;
750 power_supply_changed(battery);
753 static irqreturn_t sbs_irq(int irq, void *devid)
755 sbs_supply_changed(devid);
759 static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
762 sbs_supply_changed(i2c_get_clientdata(client));
765 static void sbs_external_power_changed(struct power_supply *psy)
767 struct sbs_info *chip = power_supply_get_drvdata(psy);
769 /* cancel outstanding work */
770 cancel_delayed_work_sync(&chip->work);
772 schedule_delayed_work(&chip->work, HZ);
773 chip->poll_time = chip->poll_retry_count;
776 static void sbs_delayed_work(struct work_struct *work)
778 struct sbs_info *chip;
781 chip = container_of(work, struct sbs_info, work.work);
783 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
784 /* if the read failed, give up on this work */
790 if (ret & BATTERY_FULL_CHARGED)
791 ret = POWER_SUPPLY_STATUS_FULL;
792 else if (ret & BATTERY_DISCHARGING)
793 ret = POWER_SUPPLY_STATUS_DISCHARGING;
795 ret = POWER_SUPPLY_STATUS_CHARGING;
797 sbs_status_correct(chip->client, &ret);
799 if (chip->last_state != ret) {
801 power_supply_changed(chip->power_supply);
804 if (chip->poll_time > 0) {
805 schedule_delayed_work(&chip->work, HZ);
811 static const struct power_supply_desc sbs_default_desc = {
812 .type = POWER_SUPPLY_TYPE_BATTERY,
813 .properties = sbs_properties,
814 .num_properties = ARRAY_SIZE(sbs_properties),
815 .get_property = sbs_get_property,
816 .external_power_changed = sbs_external_power_changed,
819 static int sbs_probe(struct i2c_client *client,
820 const struct i2c_device_id *id)
822 struct sbs_info *chip;
823 struct power_supply_desc *sbs_desc;
824 struct sbs_platform_data *pdata = client->dev.platform_data;
825 struct power_supply_config psy_cfg = {};
829 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
830 sizeof(*sbs_desc), GFP_KERNEL);
834 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
835 dev_name(&client->dev));
839 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
843 chip->flags = (u32)(uintptr_t)of_device_get_match_data(&client->dev);
844 chip->client = client;
845 chip->enable_detection = false;
846 psy_cfg.of_node = client->dev.of_node;
847 psy_cfg.drv_data = chip;
848 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
849 mutex_init(&chip->mode_lock);
851 /* use pdata if available, fall back to DT properties,
852 * or hardcoded defaults if not
854 rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
855 &chip->i2c_retry_count);
857 chip->i2c_retry_count = 0;
859 rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
860 &chip->poll_retry_count);
862 chip->poll_retry_count = 0;
865 chip->poll_retry_count = pdata->poll_retry_count;
866 chip->i2c_retry_count = pdata->i2c_retry_count;
868 chip->i2c_retry_count = chip->i2c_retry_count + 1;
870 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
871 "sbs,battery-detect", GPIOD_IN);
872 if (IS_ERR(chip->gpio_detect)) {
873 dev_err(&client->dev, "Failed to get gpio: %ld\n",
874 PTR_ERR(chip->gpio_detect));
875 return PTR_ERR(chip->gpio_detect);
878 i2c_set_clientdata(client, chip);
880 if (!chip->gpio_detect)
883 irq = gpiod_to_irq(chip->gpio_detect);
885 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
889 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
890 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
891 dev_name(&client->dev), chip);
893 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
899 * Before we register, we might need to make sure we can actually talk
902 if (!(force_load || chip->gpio_detect)) {
903 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
906 dev_err(&client->dev, "%s: Failed to get device status\n",
912 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
914 if (IS_ERR(chip->power_supply)) {
915 dev_err(&client->dev,
916 "%s: Failed to register power supply\n", __func__);
917 rc = PTR_ERR(chip->power_supply);
921 dev_info(&client->dev,
922 "%s: battery gas gauge device registered\n", client->name);
924 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
926 chip->enable_detection = true;
934 static int sbs_remove(struct i2c_client *client)
936 struct sbs_info *chip = i2c_get_clientdata(client);
938 cancel_delayed_work_sync(&chip->work);
943 #if defined CONFIG_PM_SLEEP
945 static int sbs_suspend(struct device *dev)
947 struct i2c_client *client = to_i2c_client(dev);
948 struct sbs_info *chip = i2c_get_clientdata(client);
951 if (chip->poll_time > 0)
952 cancel_delayed_work_sync(&chip->work);
954 if (chip->flags & SBS_FLAGS_TI_BQ20Z75) {
955 /* Write to manufacturer access with sleep command. */
956 ret = sbs_write_word_data(client,
957 sbs_data[REG_MANUFACTURER_DATA].addr,
958 MANUFACTURER_ACCESS_SLEEP);
959 if (chip->is_present && ret < 0)
966 static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
967 #define SBS_PM_OPS (&sbs_pm_ops)
970 #define SBS_PM_OPS NULL
973 static const struct i2c_device_id sbs_id[] = {
975 { "sbs-battery", 1 },
978 MODULE_DEVICE_TABLE(i2c, sbs_id);
980 static const struct of_device_id sbs_dt_ids[] = {
981 { .compatible = "sbs,sbs-battery" },
983 .compatible = "ti,bq20z75",
984 .data = (void *)SBS_FLAGS_TI_BQ20Z75,
988 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
990 static struct i2c_driver sbs_battery_driver = {
992 .remove = sbs_remove,
996 .name = "sbs-battery",
997 .of_match_table = sbs_dt_ids,
1001 module_i2c_driver(sbs_battery_driver);
1003 MODULE_DESCRIPTION("SBS battery monitor driver");
1004 MODULE_LICENSE("GPL");
1006 module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
1007 MODULE_PARM_DESC(force_load,
1008 "Attempt to load the driver even if no battery is connected");