1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Gas Gauge driver for SBS Compliant Batteries
5 * Copyright (c) 2010, NVIDIA Corporation.
8 #include <linux/bits.h>
9 #include <linux/delay.h>
10 #include <linux/devm-helpers.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/property.h>
19 #include <linux/of_device.h>
20 #include <linux/power/sbs-battery.h>
21 #include <linux/power_supply.h>
22 #include <linux/slab.h>
23 #include <linux/stat.h>
26 REG_MANUFACTURER_DATA,
40 REG_REMAINING_CAPACITY,
41 REG_REMAINING_CAPACITY_CHARGE,
42 REG_FULL_CHARGE_CAPACITY,
43 REG_FULL_CHARGE_CAPACITY_CHARGE,
45 REG_DESIGN_CAPACITY_CHARGE,
46 REG_DESIGN_VOLTAGE_MIN,
47 REG_DESIGN_VOLTAGE_MAX,
55 #define REG_ADDR_SPEC_INFO 0x1A
56 #define SPEC_INFO_VERSION_MASK GENMASK(7, 4)
57 #define SPEC_INFO_VERSION_SHIFT 4
59 #define SBS_VERSION_1_0 1
60 #define SBS_VERSION_1_1 2
61 #define SBS_VERSION_1_1_WITH_PEC 3
63 #define REG_ADDR_MANUFACTURE_DATE 0x1B
65 /* Battery Mode defines */
66 #define BATTERY_MODE_OFFSET 0x03
67 #define BATTERY_MODE_CAPACITY_MASK BIT(15)
68 enum sbs_capacity_mode {
69 CAPACITY_MODE_AMPS = 0,
70 CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK
72 #define BATTERY_MODE_CHARGER_MASK (1<<14)
74 /* manufacturer access defines */
75 #define MANUFACTURER_ACCESS_STATUS 0x0006
76 #define MANUFACTURER_ACCESS_SLEEP 0x0011
78 /* battery status value bits */
79 #define BATTERY_INITIALIZED 0x80
80 #define BATTERY_DISCHARGING 0x40
81 #define BATTERY_FULL_CHARGED 0x20
82 #define BATTERY_FULL_DISCHARGED 0x10
84 /* min_value and max_value are only valid for numerical data */
85 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
88 .min_value = _min_value, \
89 .max_value = _max_value, \
92 static const struct chip_data {
93 enum power_supply_property psp;
98 [REG_MANUFACTURER_DATA] =
99 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
101 SBS_DATA(-1, 0x03, 0, 65535),
103 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
105 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
107 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
109 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767),
111 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100),
113 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
114 [REG_REMAINING_CAPACITY] =
115 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
116 [REG_REMAINING_CAPACITY_CHARGE] =
117 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
118 [REG_FULL_CHARGE_CAPACITY] =
119 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
120 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
121 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
122 [REG_TIME_TO_EMPTY] =
123 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
125 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
126 [REG_CHARGE_CURRENT] =
127 SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535),
128 [REG_CHARGE_VOLTAGE] =
129 SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535),
131 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
132 [REG_CAPACITY_LEVEL] =
133 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
135 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
136 [REG_DESIGN_CAPACITY] =
137 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
138 [REG_DESIGN_CAPACITY_CHARGE] =
139 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
140 [REG_DESIGN_VOLTAGE_MIN] =
141 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
142 [REG_DESIGN_VOLTAGE_MAX] =
143 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
144 [REG_SERIAL_NUMBER] =
145 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
146 /* Properties of type `const char *' */
148 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
150 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535),
152 SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535)
155 static const enum power_supply_property sbs_properties[] = {
156 POWER_SUPPLY_PROP_STATUS,
157 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
158 POWER_SUPPLY_PROP_HEALTH,
159 POWER_SUPPLY_PROP_PRESENT,
160 POWER_SUPPLY_PROP_TECHNOLOGY,
161 POWER_SUPPLY_PROP_CYCLE_COUNT,
162 POWER_SUPPLY_PROP_VOLTAGE_NOW,
163 POWER_SUPPLY_PROP_CURRENT_NOW,
164 POWER_SUPPLY_PROP_CURRENT_AVG,
165 POWER_SUPPLY_PROP_CAPACITY,
166 POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN,
167 POWER_SUPPLY_PROP_TEMP,
168 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
169 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
170 POWER_SUPPLY_PROP_SERIAL_NUMBER,
171 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
172 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
173 POWER_SUPPLY_PROP_ENERGY_NOW,
174 POWER_SUPPLY_PROP_ENERGY_FULL,
175 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
176 POWER_SUPPLY_PROP_CHARGE_NOW,
177 POWER_SUPPLY_PROP_CHARGE_FULL,
178 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
179 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
180 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
181 POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
182 POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
183 POWER_SUPPLY_PROP_MANUFACTURE_DAY,
184 /* Properties of type `const char *' */
185 POWER_SUPPLY_PROP_MANUFACTURER,
186 POWER_SUPPLY_PROP_MODEL_NAME
189 /* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */
190 #define SBS_FLAGS_TI_BQ20ZX5 BIT(0)
192 static const enum power_supply_property string_properties[] = {
193 POWER_SUPPLY_PROP_TECHNOLOGY,
194 POWER_SUPPLY_PROP_MANUFACTURER,
195 POWER_SUPPLY_PROP_MODEL_NAME,
198 #define NR_STRING_BUFFERS ARRAY_SIZE(string_properties)
201 struct i2c_client *client;
202 struct power_supply *power_supply;
204 struct gpio_desc *gpio_detect;
205 bool charger_broadcasts;
209 u32 poll_retry_count;
210 struct delayed_work work;
211 struct mutex mode_lock;
214 char strings[NR_STRING_BUFFERS][I2C_SMBUS_BLOCK_MAX + 1];
217 static char *sbs_get_string_buf(struct sbs_info *chip,
218 enum power_supply_property psp)
222 for (i = 0; i < NR_STRING_BUFFERS; i++)
223 if (string_properties[i] == psp)
224 return chip->strings[i];
226 return ERR_PTR(-EINVAL);
229 static void sbs_invalidate_cached_props(struct sbs_info *chip)
233 chip->technology = -1;
235 for (i = 0; i < NR_STRING_BUFFERS; i++)
236 chip->strings[i][0] = 0;
239 static bool force_load;
241 static int sbs_read_word_data(struct i2c_client *client, u8 address);
242 static int sbs_write_word_data(struct i2c_client *client, u8 address, u16 value);
244 static void sbs_disable_charger_broadcasts(struct sbs_info *chip)
246 int val = sbs_read_word_data(chip->client, BATTERY_MODE_OFFSET);
250 val |= BATTERY_MODE_CHARGER_MASK;
252 val = sbs_write_word_data(chip->client, BATTERY_MODE_OFFSET, val);
256 dev_err(&chip->client->dev,
257 "Failed to disable charger broadcasting: %d\n", val);
259 dev_dbg(&chip->client->dev, "%s\n", __func__);
262 static int sbs_update_presence(struct sbs_info *chip, bool is_present)
264 struct i2c_client *client = chip->client;
265 int retries = chip->i2c_retry_count;
269 if (chip->is_present == is_present)
273 chip->is_present = false;
274 /* Disable PEC when no device is present */
275 client->flags &= ~I2C_CLIENT_PEC;
276 sbs_invalidate_cached_props(chip);
280 /* Check if device supports packet error checking and use it */
281 while (retries > 0) {
282 ret = i2c_smbus_read_word_data(client, REG_ADDR_SPEC_INFO);
287 * Some batteries trigger the detection pin before the
288 * I2C bus is properly connected. This works around the
297 dev_dbg(&client->dev, "failed to read spec info: %d\n", ret);
299 /* fallback to old behaviour */
300 client->flags &= ~I2C_CLIENT_PEC;
301 chip->is_present = true;
306 version = (ret & SPEC_INFO_VERSION_MASK) >> SPEC_INFO_VERSION_SHIFT;
308 if (version == SBS_VERSION_1_1_WITH_PEC)
309 client->flags |= I2C_CLIENT_PEC;
311 client->flags &= ~I2C_CLIENT_PEC;
313 if (of_device_is_compatible(client->dev.parent->of_node, "google,cros-ec-i2c-tunnel")
314 && client->flags & I2C_CLIENT_PEC) {
315 dev_info(&client->dev, "Disabling PEC because of broken Cros-EC implementation\n");
316 client->flags &= ~I2C_CLIENT_PEC;
319 dev_dbg(&client->dev, "PEC: %s\n", (client->flags & I2C_CLIENT_PEC) ?
320 "enabled" : "disabled");
322 if (!chip->is_present && is_present && !chip->charger_broadcasts)
323 sbs_disable_charger_broadcasts(chip);
325 chip->is_present = true;
330 static int sbs_read_word_data(struct i2c_client *client, u8 address)
332 struct sbs_info *chip = i2c_get_clientdata(client);
333 int retries = chip->i2c_retry_count;
336 while (retries > 0) {
337 ret = i2c_smbus_read_word_data(client, address);
344 dev_dbg(&client->dev,
345 "%s: i2c read at address 0x%x failed\n",
353 static int sbs_read_string_data_fallback(struct i2c_client *client, u8 address, char *values)
355 struct sbs_info *chip = i2c_get_clientdata(client);
356 s32 ret = 0, block_length = 0;
357 int retries_length, retries_block;
358 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
360 retries_length = chip->i2c_retry_count;
361 retries_block = chip->i2c_retry_count;
363 dev_warn_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n"
364 "Fallback method does not support PEC.\n");
366 /* Adapter needs to support these two functions */
367 if (!i2c_check_functionality(client->adapter,
368 I2C_FUNC_SMBUS_BYTE_DATA |
369 I2C_FUNC_SMBUS_I2C_BLOCK)){
373 /* Get the length of block data */
374 while (retries_length > 0) {
375 ret = i2c_smbus_read_byte_data(client, address);
382 dev_dbg(&client->dev,
383 "%s: i2c read at address 0x%x failed\n",
388 /* block_length does not include NULL terminator */
390 if (block_length > I2C_SMBUS_BLOCK_MAX) {
391 dev_err(&client->dev,
392 "%s: Returned block_length is longer than 0x%x\n",
393 __func__, I2C_SMBUS_BLOCK_MAX);
397 /* Get the block data */
398 while (retries_block > 0) {
399 ret = i2c_smbus_read_i2c_block_data(
401 block_length + 1, block_buffer);
408 dev_dbg(&client->dev,
409 "%s: i2c read at address 0x%x failed\n",
414 /* block_buffer[0] == block_length */
415 memcpy(values, block_buffer + 1, block_length);
416 values[block_length] = '\0';
421 static int sbs_read_string_data(struct i2c_client *client, u8 address, char *values)
423 struct sbs_info *chip = i2c_get_clientdata(client);
424 int retries = chip->i2c_retry_count;
427 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
428 bool pec = client->flags & I2C_CLIENT_PEC;
429 client->flags &= ~I2C_CLIENT_PEC;
430 ret = sbs_read_string_data_fallback(client, address, values);
432 client->flags |= I2C_CLIENT_PEC;
436 while (retries > 0) {
437 ret = i2c_smbus_read_block_data(client, address, values);
444 dev_dbg(&client->dev, "failed to read block 0x%x: %d\n", address, ret);
448 /* add string termination */
453 static int sbs_write_word_data(struct i2c_client *client, u8 address,
456 struct sbs_info *chip = i2c_get_clientdata(client);
457 int retries = chip->i2c_retry_count;
460 while (retries > 0) {
461 ret = i2c_smbus_write_word_data(client, address, value);
468 dev_dbg(&client->dev,
469 "%s: i2c write to address 0x%x failed\n",
477 static int sbs_status_correct(struct i2c_client *client, int *intval)
481 ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr);
487 /* Not drawing current -> not charging (i.e. idle) */
488 if (*intval != POWER_SUPPLY_STATUS_FULL && ret == 0)
489 *intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
491 if (*intval == POWER_SUPPLY_STATUS_FULL) {
492 /* Drawing or providing current when full */
494 *intval = POWER_SUPPLY_STATUS_CHARGING;
496 *intval = POWER_SUPPLY_STATUS_DISCHARGING;
502 static bool sbs_bat_needs_calibration(struct i2c_client *client)
506 ret = sbs_read_word_data(client, sbs_data[REG_BATTERY_MODE].addr);
510 return !!(ret & BIT(7));
513 static int sbs_get_ti_battery_presence_and_health(
514 struct i2c_client *client, enum power_supply_property psp,
515 union power_supply_propval *val)
520 * Write to ManufacturerAccess with ManufacturerAccess command
521 * and then read the status.
523 ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
524 MANUFACTURER_ACCESS_STATUS);
526 if (psp == POWER_SUPPLY_PROP_PRESENT)
527 val->intval = 0; /* battery removed */
531 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
533 if (psp == POWER_SUPPLY_PROP_PRESENT)
534 val->intval = 0; /* battery removed */
538 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
539 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
544 /* Mask the upper nibble of 2nd byte and
545 * lower byte of response then
546 * shift the result by 8 to get status*/
549 if (psp == POWER_SUPPLY_PROP_PRESENT) {
551 /* battery removed */
555 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
557 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
558 else if (ret == 0x0B)
559 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
560 else if (ret == 0x0C)
561 val->intval = POWER_SUPPLY_HEALTH_DEAD;
562 else if (sbs_bat_needs_calibration(client))
563 val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
565 val->intval = POWER_SUPPLY_HEALTH_GOOD;
571 static int sbs_get_battery_presence_and_health(
572 struct i2c_client *client, enum power_supply_property psp,
573 union power_supply_propval *val)
575 struct sbs_info *chip = i2c_get_clientdata(client);
578 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5)
579 return sbs_get_ti_battery_presence_and_health(client, psp, val);
581 /* Dummy command; if it succeeds, battery is present. */
582 ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
584 if (ret < 0) { /* battery not present*/
585 if (psp == POWER_SUPPLY_PROP_PRESENT) {
592 if (psp == POWER_SUPPLY_PROP_PRESENT)
593 val->intval = 1; /* battery present */
594 else { /* POWER_SUPPLY_PROP_HEALTH */
595 if (sbs_bat_needs_calibration(client)) {
596 val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
598 /* SBS spec doesn't have a general health command. */
599 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
606 static int sbs_get_battery_property(struct i2c_client *client,
607 int reg_offset, enum power_supply_property psp,
608 union power_supply_propval *val)
610 struct sbs_info *chip = i2c_get_clientdata(client);
613 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
617 /* returned values are 16 bit */
618 if (sbs_data[reg_offset].min_value < 0)
621 if (ret >= sbs_data[reg_offset].min_value &&
622 ret <= sbs_data[reg_offset].max_value) {
624 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
625 if (!(ret & BATTERY_INITIALIZED))
627 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
628 else if (ret & BATTERY_FULL_CHARGED)
630 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
631 else if (ret & BATTERY_FULL_DISCHARGED)
633 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
636 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
638 } else if (psp != POWER_SUPPLY_PROP_STATUS) {
642 if (ret & BATTERY_FULL_CHARGED)
643 val->intval = POWER_SUPPLY_STATUS_FULL;
644 else if (ret & BATTERY_DISCHARGING)
645 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
647 val->intval = POWER_SUPPLY_STATUS_CHARGING;
649 sbs_status_correct(client, &val->intval);
651 if (chip->poll_time == 0)
652 chip->last_state = val->intval;
653 else if (chip->last_state != val->intval) {
654 cancel_delayed_work_sync(&chip->work);
655 power_supply_changed(chip->power_supply);
659 if (psp == POWER_SUPPLY_PROP_STATUS)
660 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
661 else if (psp == POWER_SUPPLY_PROP_CAPACITY)
662 /* sbs spec says that this can be >100 %
663 * even if max value is 100 %
665 val->intval = min(ret, 100);
673 static int sbs_get_property_index(struct i2c_client *client,
674 enum power_supply_property psp)
678 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
679 if (psp == sbs_data[count].psp)
682 dev_warn(&client->dev,
683 "%s: Invalid Property - %d\n", __func__, psp);
688 static const char *sbs_get_constant_string(struct sbs_info *chip,
689 enum power_supply_property psp)
695 buf = sbs_get_string_buf(chip, psp);
700 ret = sbs_get_property_index(chip->client, psp);
704 addr = sbs_data[ret].addr;
706 ret = sbs_read_string_data(chip->client, addr, buf);
714 static void sbs_unit_adjustment(struct i2c_client *client,
715 enum power_supply_property psp, union power_supply_propval *val)
717 #define BASE_UNIT_CONVERSION 1000
718 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
719 #define TIME_UNIT_CONVERSION 60
720 #define TEMP_KELVIN_TO_CELSIUS 2731
722 case POWER_SUPPLY_PROP_ENERGY_NOW:
723 case POWER_SUPPLY_PROP_ENERGY_FULL:
724 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
725 /* sbs provides energy in units of 10mWh.
728 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
731 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
732 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
733 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
734 case POWER_SUPPLY_PROP_CURRENT_NOW:
735 case POWER_SUPPLY_PROP_CURRENT_AVG:
736 case POWER_SUPPLY_PROP_CHARGE_NOW:
737 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
738 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
739 case POWER_SUPPLY_PROP_CHARGE_FULL:
740 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
741 val->intval *= BASE_UNIT_CONVERSION;
744 case POWER_SUPPLY_PROP_TEMP:
745 /* sbs provides battery temperature in 0.1K
746 * so convert it to 0.1°C
748 val->intval -= TEMP_KELVIN_TO_CELSIUS;
751 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
752 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
753 /* sbs provides time to empty and time to full in minutes.
756 val->intval *= TIME_UNIT_CONVERSION;
760 dev_dbg(&client->dev,
761 "%s: no need for unit conversion %d\n", __func__, psp);
765 static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client,
766 enum sbs_capacity_mode mode)
768 int ret, original_val;
770 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
771 if (original_val < 0)
774 if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode)
777 if (mode == CAPACITY_MODE_AMPS)
778 ret = original_val & ~BATTERY_MODE_CAPACITY_MASK;
780 ret = original_val | BATTERY_MODE_CAPACITY_MASK;
782 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
786 usleep_range(1000, 2000);
788 return original_val & BATTERY_MODE_CAPACITY_MASK;
791 static int sbs_get_battery_capacity(struct i2c_client *client,
792 int reg_offset, enum power_supply_property psp,
793 union power_supply_propval *val)
796 enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS;
798 if (power_supply_is_amp_property(psp))
799 mode = CAPACITY_MODE_AMPS;
801 mode = sbs_set_capacity_mode(client, mode);
805 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
811 ret = sbs_set_capacity_mode(client, mode);
818 static char sbs_serial[5];
819 static int sbs_get_battery_serial_number(struct i2c_client *client,
820 union power_supply_propval *val)
824 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
828 sprintf(sbs_serial, "%04x", ret);
829 val->strval = sbs_serial;
834 static int sbs_get_chemistry(struct sbs_info *chip,
835 union power_supply_propval *val)
837 const char *chemistry;
839 if (chip->technology != -1) {
840 val->intval = chip->technology;
844 chemistry = sbs_get_constant_string(chip, POWER_SUPPLY_PROP_TECHNOLOGY);
846 if (IS_ERR(chemistry))
847 return PTR_ERR(chemistry);
849 if (!strncasecmp(chemistry, "LION", 4))
850 chip->technology = POWER_SUPPLY_TECHNOLOGY_LION;
851 else if (!strncasecmp(chemistry, "LiP", 3))
852 chip->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
853 else if (!strncasecmp(chemistry, "NiCd", 4))
854 chip->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
855 else if (!strncasecmp(chemistry, "NiMH", 4))
856 chip->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
858 chip->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
860 if (chip->technology == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
861 dev_warn(&chip->client->dev, "Unknown chemistry: %s\n", chemistry);
863 val->intval = chip->technology;
868 static int sbs_get_battery_manufacture_date(struct i2c_client *client,
869 enum power_supply_property psp,
870 union power_supply_propval *val)
873 u16 day, month, year;
875 ret = sbs_read_word_data(client, REG_ADDR_MANUFACTURE_DATE);
879 day = ret & GENMASK(4, 0);
880 month = (ret & GENMASK(8, 5)) >> 5;
881 year = ((ret & GENMASK(15, 9)) >> 9) + 1980;
884 case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
887 case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
890 case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
900 static int sbs_get_property(struct power_supply *psy,
901 enum power_supply_property psp,
902 union power_supply_propval *val)
905 struct sbs_info *chip = power_supply_get_drvdata(psy);
906 struct i2c_client *client = chip->client;
909 if (chip->gpio_detect) {
910 ret = gpiod_get_value_cansleep(chip->gpio_detect);
913 if (psp == POWER_SUPPLY_PROP_PRESENT) {
915 sbs_update_presence(chip, ret);
923 case POWER_SUPPLY_PROP_PRESENT:
924 case POWER_SUPPLY_PROP_HEALTH:
925 ret = sbs_get_battery_presence_and_health(client, psp, val);
927 /* this can only be true if no gpio is used */
928 if (psp == POWER_SUPPLY_PROP_PRESENT)
932 case POWER_SUPPLY_PROP_TECHNOLOGY:
933 ret = sbs_get_chemistry(chip, val);
937 goto done; /* don't trigger power_supply_changed()! */
939 case POWER_SUPPLY_PROP_ENERGY_NOW:
940 case POWER_SUPPLY_PROP_ENERGY_FULL:
941 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
942 case POWER_SUPPLY_PROP_CHARGE_NOW:
943 case POWER_SUPPLY_PROP_CHARGE_FULL:
944 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
945 ret = sbs_get_property_index(client, psp);
949 /* sbs_get_battery_capacity() will change the battery mode
950 * temporarily to read the requested attribute. Ensure we stay
951 * in the desired mode for the duration of the attribute read.
953 mutex_lock(&chip->mode_lock);
954 ret = sbs_get_battery_capacity(client, ret, psp, val);
955 mutex_unlock(&chip->mode_lock);
958 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
959 ret = sbs_get_battery_serial_number(client, val);
962 case POWER_SUPPLY_PROP_STATUS:
963 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
964 case POWER_SUPPLY_PROP_CYCLE_COUNT:
965 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
966 case POWER_SUPPLY_PROP_CURRENT_NOW:
967 case POWER_SUPPLY_PROP_CURRENT_AVG:
968 case POWER_SUPPLY_PROP_TEMP:
969 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
970 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
971 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
972 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
973 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
974 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
975 case POWER_SUPPLY_PROP_CAPACITY:
976 case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN:
977 ret = sbs_get_property_index(client, psp);
981 ret = sbs_get_battery_property(client, ret, psp, val);
984 case POWER_SUPPLY_PROP_MODEL_NAME:
985 case POWER_SUPPLY_PROP_MANUFACTURER:
986 str = sbs_get_constant_string(chip, psp);
993 case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
994 case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
995 case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
996 ret = sbs_get_battery_manufacture_date(client, psp, val);
1000 dev_err(&client->dev,
1001 "%s: INVALID property\n", __func__);
1005 if (!chip->gpio_detect && chip->is_present != (ret >= 0)) {
1006 bool old_present = chip->is_present;
1007 union power_supply_propval val;
1008 int err = sbs_get_battery_presence_and_health(
1009 client, POWER_SUPPLY_PROP_PRESENT, &val);
1011 sbs_update_presence(chip, !err && val.intval);
1013 if (old_present != chip->is_present)
1014 power_supply_changed(chip->power_supply);
1019 /* Convert units to match requirements for power supply class */
1020 sbs_unit_adjustment(client, psp, val);
1021 dev_dbg(&client->dev,
1022 "%s: property = %d, value = %x\n", __func__,
1024 } else if (!chip->is_present) {
1025 /* battery not present, so return NODATA for properties */
1031 static void sbs_supply_changed(struct sbs_info *chip)
1033 struct power_supply *battery = chip->power_supply;
1036 ret = gpiod_get_value_cansleep(chip->gpio_detect);
1039 sbs_update_presence(chip, ret);
1040 power_supply_changed(battery);
1043 static irqreturn_t sbs_irq(int irq, void *devid)
1045 sbs_supply_changed(devid);
1049 static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
1052 sbs_supply_changed(i2c_get_clientdata(client));
1055 static void sbs_external_power_changed(struct power_supply *psy)
1057 struct sbs_info *chip = power_supply_get_drvdata(psy);
1059 /* cancel outstanding work */
1060 cancel_delayed_work_sync(&chip->work);
1062 schedule_delayed_work(&chip->work, HZ);
1063 chip->poll_time = chip->poll_retry_count;
1066 static void sbs_delayed_work(struct work_struct *work)
1068 struct sbs_info *chip;
1071 chip = container_of(work, struct sbs_info, work.work);
1073 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
1074 /* if the read failed, give up on this work */
1076 chip->poll_time = 0;
1080 if (ret & BATTERY_FULL_CHARGED)
1081 ret = POWER_SUPPLY_STATUS_FULL;
1082 else if (ret & BATTERY_DISCHARGING)
1083 ret = POWER_SUPPLY_STATUS_DISCHARGING;
1085 ret = POWER_SUPPLY_STATUS_CHARGING;
1087 sbs_status_correct(chip->client, &ret);
1089 if (chip->last_state != ret) {
1090 chip->poll_time = 0;
1091 power_supply_changed(chip->power_supply);
1094 if (chip->poll_time > 0) {
1095 schedule_delayed_work(&chip->work, HZ);
1101 static const struct power_supply_desc sbs_default_desc = {
1102 .type = POWER_SUPPLY_TYPE_BATTERY,
1103 .properties = sbs_properties,
1104 .num_properties = ARRAY_SIZE(sbs_properties),
1105 .get_property = sbs_get_property,
1106 .external_power_changed = sbs_external_power_changed,
1109 static int sbs_probe(struct i2c_client *client)
1111 struct sbs_info *chip;
1112 struct power_supply_desc *sbs_desc;
1113 struct sbs_platform_data *pdata = client->dev.platform_data;
1114 struct power_supply_config psy_cfg = {};
1118 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
1119 sizeof(*sbs_desc), GFP_KERNEL);
1123 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
1124 dev_name(&client->dev));
1125 if (!sbs_desc->name)
1128 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
1132 chip->flags = (u32)(uintptr_t)device_get_match_data(&client->dev);
1133 chip->client = client;
1134 psy_cfg.of_node = client->dev.of_node;
1135 psy_cfg.drv_data = chip;
1136 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
1137 sbs_invalidate_cached_props(chip);
1138 mutex_init(&chip->mode_lock);
1140 /* use pdata if available, fall back to DT properties,
1141 * or hardcoded defaults if not
1143 rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count",
1144 &chip->i2c_retry_count);
1146 chip->i2c_retry_count = 0;
1148 rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count",
1149 &chip->poll_retry_count);
1151 chip->poll_retry_count = 0;
1154 chip->poll_retry_count = pdata->poll_retry_count;
1155 chip->i2c_retry_count = pdata->i2c_retry_count;
1157 chip->i2c_retry_count = chip->i2c_retry_count + 1;
1159 chip->charger_broadcasts = !device_property_read_bool(&client->dev,
1160 "sbs,disable-charger-broadcasts");
1162 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
1163 "sbs,battery-detect", GPIOD_IN);
1164 if (IS_ERR(chip->gpio_detect))
1165 return dev_err_probe(&client->dev, PTR_ERR(chip->gpio_detect),
1166 "Failed to get gpio\n");
1168 i2c_set_clientdata(client, chip);
1170 if (!chip->gpio_detect)
1173 irq = gpiod_to_irq(chip->gpio_detect);
1175 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
1179 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
1180 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1181 dev_name(&client->dev), chip);
1183 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
1189 * Before we register, we might need to make sure we can actually talk
1192 if (!(force_load || chip->gpio_detect)) {
1193 union power_supply_propval val;
1195 rc = sbs_get_battery_presence_and_health(
1196 client, POWER_SUPPLY_PROP_PRESENT, &val);
1197 if (rc < 0 || !val.intval)
1198 return dev_err_probe(&client->dev, -ENODEV,
1199 "Failed to get present status\n");
1202 rc = devm_delayed_work_autocancel(&client->dev, &chip->work,
1207 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
1209 if (IS_ERR(chip->power_supply))
1210 return dev_err_probe(&client->dev, PTR_ERR(chip->power_supply),
1211 "Failed to register power supply\n");
1213 dev_info(&client->dev,
1214 "%s: battery gas gauge device registered\n", client->name);
1219 #if defined CONFIG_PM_SLEEP
1221 static int sbs_suspend(struct device *dev)
1223 struct i2c_client *client = to_i2c_client(dev);
1224 struct sbs_info *chip = i2c_get_clientdata(client);
1227 if (chip->poll_time > 0)
1228 cancel_delayed_work_sync(&chip->work);
1230 if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) {
1231 /* Write to manufacturer access with sleep command. */
1232 ret = sbs_write_word_data(client,
1233 sbs_data[REG_MANUFACTURER_DATA].addr,
1234 MANUFACTURER_ACCESS_SLEEP);
1235 if (chip->is_present && ret < 0)
1242 static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
1243 #define SBS_PM_OPS (&sbs_pm_ops)
1246 #define SBS_PM_OPS NULL
1249 static const struct i2c_device_id sbs_id[] = {
1252 { "sbs-battery", 1 },
1255 MODULE_DEVICE_TABLE(i2c, sbs_id);
1257 static const struct of_device_id sbs_dt_ids[] = {
1258 { .compatible = "sbs,sbs-battery" },
1260 .compatible = "ti,bq20z65",
1261 .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1264 .compatible = "ti,bq20z75",
1265 .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1269 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
1271 static struct i2c_driver sbs_battery_driver = {
1272 .probe_new = sbs_probe,
1276 .name = "sbs-battery",
1277 .of_match_table = sbs_dt_ids,
1281 module_i2c_driver(sbs_battery_driver);
1283 MODULE_DESCRIPTION("SBS battery monitor driver");
1284 MODULE_LICENSE("GPL");
1286 module_param(force_load, bool, 0444);
1287 MODULE_PARM_DESC(force_load,
1288 "Attempt to load the driver even if no battery is connected");