Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Gas Gauge driver for SBS Compliant Batteries
0004  *
0005  * Copyright (c) 2010, NVIDIA Corporation.
0006  */
0007 
0008 #include <linux/bits.h>
0009 #include <linux/delay.h>
0010 #include <linux/devm-helpers.h>
0011 #include <linux/err.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/i2c.h>
0014 #include <linux/init.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/property.h>
0019 #include <linux/of_device.h>
0020 #include <linux/power/sbs-battery.h>
0021 #include <linux/power_supply.h>
0022 #include <linux/slab.h>
0023 #include <linux/stat.h>
0024 
0025 enum {
0026     REG_MANUFACTURER_DATA,
0027     REG_BATTERY_MODE,
0028     REG_TEMPERATURE,
0029     REG_VOLTAGE,
0030     REG_CURRENT_NOW,
0031     REG_CURRENT_AVG,
0032     REG_MAX_ERR,
0033     REG_CAPACITY,
0034     REG_TIME_TO_EMPTY_NOW,
0035     REG_TIME_TO_EMPTY_AVG,
0036     REG_TIME_TO_FULL_AVG,
0037     REG_STATUS,
0038     REG_CAPACITY_LEVEL,
0039     REG_CYCLE_COUNT,
0040     REG_SERIAL_NUMBER,
0041     REG_REMAINING_CAPACITY,
0042     REG_REMAINING_CAPACITY_CHARGE,
0043     REG_FULL_CHARGE_CAPACITY,
0044     REG_FULL_CHARGE_CAPACITY_CHARGE,
0045     REG_DESIGN_CAPACITY,
0046     REG_DESIGN_CAPACITY_CHARGE,
0047     REG_DESIGN_VOLTAGE_MIN,
0048     REG_DESIGN_VOLTAGE_MAX,
0049     REG_CHEMISTRY,
0050     REG_MANUFACTURER,
0051     REG_MODEL_NAME,
0052     REG_CHARGE_CURRENT,
0053     REG_CHARGE_VOLTAGE,
0054 };
0055 
0056 #define REG_ADDR_SPEC_INFO      0x1A
0057 #define SPEC_INFO_VERSION_MASK      GENMASK(7, 4)
0058 #define SPEC_INFO_VERSION_SHIFT     4
0059 
0060 #define SBS_VERSION_1_0         1
0061 #define SBS_VERSION_1_1         2
0062 #define SBS_VERSION_1_1_WITH_PEC    3
0063 
0064 #define REG_ADDR_MANUFACTURE_DATE   0x1B
0065 
0066 /* Battery Mode defines */
0067 #define BATTERY_MODE_OFFSET     0x03
0068 #define BATTERY_MODE_CAPACITY_MASK  BIT(15)
0069 enum sbs_capacity_mode {
0070     CAPACITY_MODE_AMPS = 0,
0071     CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK
0072 };
0073 #define BATTERY_MODE_CHARGER_MASK   (1<<14)
0074 
0075 /* manufacturer access defines */
0076 #define MANUFACTURER_ACCESS_STATUS  0x0006
0077 #define MANUFACTURER_ACCESS_SLEEP   0x0011
0078 
0079 /* battery status value bits */
0080 #define BATTERY_INITIALIZED     0x80
0081 #define BATTERY_DISCHARGING     0x40
0082 #define BATTERY_FULL_CHARGED        0x20
0083 #define BATTERY_FULL_DISCHARGED     0x10
0084 
0085 /* min_value and max_value are only valid for numerical data */
0086 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
0087     .psp = _psp, \
0088     .addr = _addr, \
0089     .min_value = _min_value, \
0090     .max_value = _max_value, \
0091 }
0092 
0093 static const struct chip_data {
0094     enum power_supply_property psp;
0095     u8 addr;
0096     int min_value;
0097     int max_value;
0098 } sbs_data[] = {
0099     [REG_MANUFACTURER_DATA] =
0100         SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
0101     [REG_BATTERY_MODE] =
0102         SBS_DATA(-1, 0x03, 0, 65535),
0103     [REG_TEMPERATURE] =
0104         SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
0105     [REG_VOLTAGE] =
0106         SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 65535),
0107     [REG_CURRENT_NOW] =
0108         SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
0109     [REG_CURRENT_AVG] =
0110         SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767),
0111     [REG_MAX_ERR] =
0112         SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100),
0113     [REG_CAPACITY] =
0114         SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
0115     [REG_REMAINING_CAPACITY] =
0116         SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
0117     [REG_REMAINING_CAPACITY_CHARGE] =
0118         SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
0119     [REG_FULL_CHARGE_CAPACITY] =
0120         SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
0121     [REG_FULL_CHARGE_CAPACITY_CHARGE] =
0122         SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
0123     [REG_TIME_TO_EMPTY_NOW] =
0124         SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 0x11, 0, 65535),
0125     [REG_TIME_TO_EMPTY_AVG] =
0126         SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
0127     [REG_TIME_TO_FULL_AVG] =
0128         SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
0129     [REG_CHARGE_CURRENT] =
0130         SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535),
0131     [REG_CHARGE_VOLTAGE] =
0132         SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535),
0133     [REG_STATUS] =
0134         SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
0135     [REG_CAPACITY_LEVEL] =
0136         SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
0137     [REG_CYCLE_COUNT] =
0138         SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
0139     [REG_DESIGN_CAPACITY] =
0140         SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
0141     [REG_DESIGN_CAPACITY_CHARGE] =
0142         SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
0143     [REG_DESIGN_VOLTAGE_MIN] =
0144         SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
0145     [REG_DESIGN_VOLTAGE_MAX] =
0146         SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
0147     [REG_SERIAL_NUMBER] =
0148         SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
0149     /* Properties of type `const char *' */
0150     [REG_MANUFACTURER] =
0151         SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
0152     [REG_MODEL_NAME] =
0153         SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535),
0154     [REG_CHEMISTRY] =
0155         SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535)
0156 };
0157 
0158 static const enum power_supply_property sbs_properties[] = {
0159     POWER_SUPPLY_PROP_STATUS,
0160     POWER_SUPPLY_PROP_CAPACITY_LEVEL,
0161     POWER_SUPPLY_PROP_HEALTH,
0162     POWER_SUPPLY_PROP_PRESENT,
0163     POWER_SUPPLY_PROP_TECHNOLOGY,
0164     POWER_SUPPLY_PROP_CYCLE_COUNT,
0165     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0166     POWER_SUPPLY_PROP_CURRENT_NOW,
0167     POWER_SUPPLY_PROP_CURRENT_AVG,
0168     POWER_SUPPLY_PROP_CAPACITY,
0169     POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN,
0170     POWER_SUPPLY_PROP_TEMP,
0171     POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
0172     POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
0173     POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
0174     POWER_SUPPLY_PROP_SERIAL_NUMBER,
0175     POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
0176     POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
0177     POWER_SUPPLY_PROP_ENERGY_NOW,
0178     POWER_SUPPLY_PROP_ENERGY_FULL,
0179     POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
0180     POWER_SUPPLY_PROP_CHARGE_NOW,
0181     POWER_SUPPLY_PROP_CHARGE_FULL,
0182     POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
0183     POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
0184     POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
0185     POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
0186     POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
0187     POWER_SUPPLY_PROP_MANUFACTURE_DAY,
0188     /* Properties of type `const char *' */
0189     POWER_SUPPLY_PROP_MANUFACTURER,
0190     POWER_SUPPLY_PROP_MODEL_NAME
0191 };
0192 
0193 /* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */
0194 #define SBS_FLAGS_TI_BQ20ZX5        BIT(0)
0195 
0196 static const enum power_supply_property string_properties[] = {
0197     POWER_SUPPLY_PROP_TECHNOLOGY,
0198     POWER_SUPPLY_PROP_MANUFACTURER,
0199     POWER_SUPPLY_PROP_MODEL_NAME,
0200 };
0201 
0202 #define NR_STRING_BUFFERS   ARRAY_SIZE(string_properties)
0203 
0204 struct sbs_info {
0205     struct i2c_client       *client;
0206     struct power_supply     *power_supply;
0207     bool                is_present;
0208     struct gpio_desc        *gpio_detect;
0209     bool                charger_broadcasts;
0210     int             last_state;
0211     int             poll_time;
0212     u32             i2c_retry_count;
0213     u32             poll_retry_count;
0214     struct delayed_work     work;
0215     struct mutex            mode_lock;
0216     u32             flags;
0217     int             technology;
0218     char                strings[NR_STRING_BUFFERS][I2C_SMBUS_BLOCK_MAX + 1];
0219 };
0220 
0221 static char *sbs_get_string_buf(struct sbs_info *chip,
0222                 enum power_supply_property psp)
0223 {
0224     int i = 0;
0225 
0226     for (i = 0; i < NR_STRING_BUFFERS; i++)
0227         if (string_properties[i] == psp)
0228             return chip->strings[i];
0229 
0230     return ERR_PTR(-EINVAL);
0231 }
0232 
0233 static void sbs_invalidate_cached_props(struct sbs_info *chip)
0234 {
0235     int i = 0;
0236 
0237     chip->technology = -1;
0238 
0239     for (i = 0; i < NR_STRING_BUFFERS; i++)
0240         chip->strings[i][0] = 0;
0241 }
0242 
0243 static bool force_load;
0244 
0245 static int sbs_read_word_data(struct i2c_client *client, u8 address);
0246 static int sbs_write_word_data(struct i2c_client *client, u8 address, u16 value);
0247 
0248 static void sbs_disable_charger_broadcasts(struct sbs_info *chip)
0249 {
0250     int val = sbs_read_word_data(chip->client, BATTERY_MODE_OFFSET);
0251     if (val < 0)
0252         goto exit;
0253 
0254     val |= BATTERY_MODE_CHARGER_MASK;
0255 
0256     val = sbs_write_word_data(chip->client, BATTERY_MODE_OFFSET, val);
0257 
0258 exit:
0259     if (val < 0)
0260         dev_err(&chip->client->dev,
0261             "Failed to disable charger broadcasting: %d\n", val);
0262     else
0263         dev_dbg(&chip->client->dev, "%s\n", __func__);
0264 }
0265 
0266 static int sbs_update_presence(struct sbs_info *chip, bool is_present)
0267 {
0268     struct i2c_client *client = chip->client;
0269     int retries = chip->i2c_retry_count;
0270     s32 ret = 0;
0271     u8 version;
0272 
0273     if (chip->is_present == is_present)
0274         return 0;
0275 
0276     if (!is_present) {
0277         chip->is_present = false;
0278         /* Disable PEC when no device is present */
0279         client->flags &= ~I2C_CLIENT_PEC;
0280         sbs_invalidate_cached_props(chip);
0281         return 0;
0282     }
0283 
0284     /* Check if device supports packet error checking and use it */
0285     while (retries > 0) {
0286         ret = i2c_smbus_read_word_data(client, REG_ADDR_SPEC_INFO);
0287         if (ret >= 0)
0288             break;
0289 
0290         /*
0291          * Some batteries trigger the detection pin before the
0292          * I2C bus is properly connected. This works around the
0293          * issue.
0294          */
0295         msleep(100);
0296 
0297         retries--;
0298     }
0299 
0300     if (ret < 0) {
0301         dev_dbg(&client->dev, "failed to read spec info: %d\n", ret);
0302 
0303         /* fallback to old behaviour */
0304         client->flags &= ~I2C_CLIENT_PEC;
0305         chip->is_present = true;
0306 
0307         return ret;
0308     }
0309 
0310     version = (ret & SPEC_INFO_VERSION_MASK) >> SPEC_INFO_VERSION_SHIFT;
0311 
0312     if (version == SBS_VERSION_1_1_WITH_PEC)
0313         client->flags |= I2C_CLIENT_PEC;
0314     else
0315         client->flags &= ~I2C_CLIENT_PEC;
0316 
0317     if (of_device_is_compatible(client->dev.parent->of_node, "google,cros-ec-i2c-tunnel")
0318         && client->flags & I2C_CLIENT_PEC) {
0319         dev_info(&client->dev, "Disabling PEC because of broken Cros-EC implementation\n");
0320         client->flags &= ~I2C_CLIENT_PEC;
0321     }
0322 
0323     dev_dbg(&client->dev, "PEC: %s\n", (client->flags & I2C_CLIENT_PEC) ?
0324         "enabled" : "disabled");
0325 
0326     if (!chip->is_present && is_present && !chip->charger_broadcasts)
0327         sbs_disable_charger_broadcasts(chip);
0328 
0329     chip->is_present = true;
0330 
0331     return 0;
0332 }
0333 
0334 static int sbs_read_word_data(struct i2c_client *client, u8 address)
0335 {
0336     struct sbs_info *chip = i2c_get_clientdata(client);
0337     int retries = chip->i2c_retry_count;
0338     s32 ret = 0;
0339 
0340     while (retries > 0) {
0341         ret = i2c_smbus_read_word_data(client, address);
0342         if (ret >= 0)
0343             break;
0344         retries--;
0345     }
0346 
0347     if (ret < 0) {
0348         dev_dbg(&client->dev,
0349             "%s: i2c read at address 0x%x failed\n",
0350             __func__, address);
0351         return ret;
0352     }
0353 
0354     return ret;
0355 }
0356 
0357 static int sbs_read_string_data_fallback(struct i2c_client *client, u8 address, char *values)
0358 {
0359     struct sbs_info *chip = i2c_get_clientdata(client);
0360     s32 ret = 0, block_length = 0;
0361     int retries_length, retries_block;
0362     u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
0363 
0364     retries_length = chip->i2c_retry_count;
0365     retries_block = chip->i2c_retry_count;
0366 
0367     dev_warn_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n"
0368                     "Fallback method does not support PEC.\n");
0369 
0370     /* Adapter needs to support these two functions */
0371     if (!i2c_check_functionality(client->adapter,
0372                      I2C_FUNC_SMBUS_BYTE_DATA |
0373                      I2C_FUNC_SMBUS_I2C_BLOCK)){
0374         return -ENODEV;
0375     }
0376 
0377     /* Get the length of block data */
0378     while (retries_length > 0) {
0379         ret = i2c_smbus_read_byte_data(client, address);
0380         if (ret >= 0)
0381             break;
0382         retries_length--;
0383     }
0384 
0385     if (ret < 0) {
0386         dev_dbg(&client->dev,
0387             "%s: i2c read at address 0x%x failed\n",
0388             __func__, address);
0389         return ret;
0390     }
0391 
0392     /* block_length does not include NULL terminator */
0393     block_length = ret;
0394     if (block_length > I2C_SMBUS_BLOCK_MAX) {
0395         dev_err(&client->dev,
0396             "%s: Returned block_length is longer than 0x%x\n",
0397             __func__, I2C_SMBUS_BLOCK_MAX);
0398         return -EINVAL;
0399     }
0400 
0401     /* Get the block data */
0402     while (retries_block > 0) {
0403         ret = i2c_smbus_read_i2c_block_data(
0404                 client, address,
0405                 block_length + 1, block_buffer);
0406         if (ret >= 0)
0407             break;
0408         retries_block--;
0409     }
0410 
0411     if (ret < 0) {
0412         dev_dbg(&client->dev,
0413             "%s: i2c read at address 0x%x failed\n",
0414             __func__, address);
0415         return ret;
0416     }
0417 
0418     /* block_buffer[0] == block_length */
0419     memcpy(values, block_buffer + 1, block_length);
0420     values[block_length] = '\0';
0421 
0422     return ret;
0423 }
0424 
0425 static int sbs_read_string_data(struct i2c_client *client, u8 address, char *values)
0426 {
0427     struct sbs_info *chip = i2c_get_clientdata(client);
0428     int retries = chip->i2c_retry_count;
0429     int ret = 0;
0430 
0431     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
0432         bool pec = client->flags & I2C_CLIENT_PEC;
0433         client->flags &= ~I2C_CLIENT_PEC;
0434         ret = sbs_read_string_data_fallback(client, address, values);
0435         if (pec)
0436             client->flags |= I2C_CLIENT_PEC;
0437         return ret;
0438     }
0439 
0440     while (retries > 0) {
0441         ret = i2c_smbus_read_block_data(client, address, values);
0442         if (ret >= 0)
0443             break;
0444         retries--;
0445     }
0446 
0447     if (ret < 0) {
0448         dev_dbg(&client->dev, "failed to read block 0x%x: %d\n", address, ret);
0449         return ret;
0450     }
0451 
0452     /* add string termination */
0453     values[ret] = '\0';
0454     return ret;
0455 }
0456 
0457 static int sbs_write_word_data(struct i2c_client *client, u8 address,
0458     u16 value)
0459 {
0460     struct sbs_info *chip = i2c_get_clientdata(client);
0461     int retries = chip->i2c_retry_count;
0462     s32 ret = 0;
0463 
0464     while (retries > 0) {
0465         ret = i2c_smbus_write_word_data(client, address, value);
0466         if (ret >= 0)
0467             break;
0468         retries--;
0469     }
0470 
0471     if (ret < 0) {
0472         dev_dbg(&client->dev,
0473             "%s: i2c write to address 0x%x failed\n",
0474             __func__, address);
0475         return ret;
0476     }
0477 
0478     return 0;
0479 }
0480 
0481 static int sbs_status_correct(struct i2c_client *client, int *intval)
0482 {
0483     int ret;
0484 
0485     ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr);
0486     if (ret < 0)
0487         return ret;
0488 
0489     ret = (s16)ret;
0490 
0491     /* Not drawing current -> not charging (i.e. idle) */
0492     if (*intval != POWER_SUPPLY_STATUS_FULL && ret == 0)
0493         *intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
0494 
0495     if (*intval == POWER_SUPPLY_STATUS_FULL) {
0496         /* Drawing or providing current when full */
0497         if (ret > 0)
0498             *intval = POWER_SUPPLY_STATUS_CHARGING;
0499         else if (ret < 0)
0500             *intval = POWER_SUPPLY_STATUS_DISCHARGING;
0501     }
0502 
0503     return 0;
0504 }
0505 
0506 static bool sbs_bat_needs_calibration(struct i2c_client *client)
0507 {
0508     int ret;
0509 
0510     ret = sbs_read_word_data(client, sbs_data[REG_BATTERY_MODE].addr);
0511     if (ret < 0)
0512         return false;
0513 
0514     return !!(ret & BIT(7));
0515 }
0516 
0517 static int sbs_get_ti_battery_presence_and_health(
0518     struct i2c_client *client, enum power_supply_property psp,
0519     union power_supply_propval *val)
0520 {
0521     s32 ret;
0522 
0523     /*
0524      * Write to ManufacturerAccess with ManufacturerAccess command
0525      * and then read the status.
0526      */
0527     ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
0528                   MANUFACTURER_ACCESS_STATUS);
0529     if (ret < 0) {
0530         if (psp == POWER_SUPPLY_PROP_PRESENT)
0531             val->intval = 0; /* battery removed */
0532         return ret;
0533     }
0534 
0535     ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
0536     if (ret < 0) {
0537         if (psp == POWER_SUPPLY_PROP_PRESENT)
0538             val->intval = 0; /* battery removed */
0539         return ret;
0540     }
0541 
0542     if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
0543         ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
0544         val->intval = 0;
0545         return 0;
0546     }
0547 
0548     /* Mask the upper nibble of 2nd byte and
0549      * lower byte of response then
0550      * shift the result by 8 to get status*/
0551     ret &= 0x0F00;
0552     ret >>= 8;
0553     if (psp == POWER_SUPPLY_PROP_PRESENT) {
0554         if (ret == 0x0F)
0555             /* battery removed */
0556             val->intval = 0;
0557         else
0558             val->intval = 1;
0559     } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
0560         if (ret == 0x09)
0561             val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
0562         else if (ret == 0x0B)
0563             val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
0564         else if (ret == 0x0C)
0565             val->intval = POWER_SUPPLY_HEALTH_DEAD;
0566         else if (sbs_bat_needs_calibration(client))
0567             val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
0568         else
0569             val->intval = POWER_SUPPLY_HEALTH_GOOD;
0570     }
0571 
0572     return 0;
0573 }
0574 
0575 static int sbs_get_battery_presence_and_health(
0576     struct i2c_client *client, enum power_supply_property psp,
0577     union power_supply_propval *val)
0578 {
0579     struct sbs_info *chip = i2c_get_clientdata(client);
0580     int ret;
0581 
0582     if (chip->flags & SBS_FLAGS_TI_BQ20ZX5)
0583         return sbs_get_ti_battery_presence_and_health(client, psp, val);
0584 
0585     /* Dummy command; if it succeeds, battery is present. */
0586     ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
0587 
0588     if (ret < 0) { /* battery not present*/
0589         if (psp == POWER_SUPPLY_PROP_PRESENT) {
0590             val->intval = 0;
0591             return 0;
0592         }
0593         return ret;
0594     }
0595 
0596     if (psp == POWER_SUPPLY_PROP_PRESENT)
0597         val->intval = 1; /* battery present */
0598     else { /* POWER_SUPPLY_PROP_HEALTH */
0599         if (sbs_bat_needs_calibration(client)) {
0600             val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
0601         } else {
0602             /* SBS spec doesn't have a general health command. */
0603             val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
0604         }
0605     }
0606 
0607     return 0;
0608 }
0609 
0610 static int sbs_get_battery_property(struct i2c_client *client,
0611     int reg_offset, enum power_supply_property psp,
0612     union power_supply_propval *val)
0613 {
0614     struct sbs_info *chip = i2c_get_clientdata(client);
0615     s32 ret;
0616 
0617     ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
0618     if (ret < 0)
0619         return ret;
0620 
0621     /* returned values are 16 bit */
0622     if (sbs_data[reg_offset].min_value < 0)
0623         ret = (s16)ret;
0624 
0625     if (ret >= sbs_data[reg_offset].min_value &&
0626         ret <= sbs_data[reg_offset].max_value) {
0627         val->intval = ret;
0628         if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
0629             if (!(ret & BATTERY_INITIALIZED))
0630                 val->intval =
0631                     POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
0632             else if (ret & BATTERY_FULL_CHARGED)
0633                 val->intval =
0634                     POWER_SUPPLY_CAPACITY_LEVEL_FULL;
0635             else if (ret & BATTERY_FULL_DISCHARGED)
0636                 val->intval =
0637                     POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
0638             else
0639                 val->intval =
0640                     POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
0641             return 0;
0642         } else if (psp != POWER_SUPPLY_PROP_STATUS) {
0643             return 0;
0644         }
0645 
0646         if (ret & BATTERY_FULL_CHARGED)
0647             val->intval = POWER_SUPPLY_STATUS_FULL;
0648         else if (ret & BATTERY_DISCHARGING)
0649             val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
0650         else
0651             val->intval = POWER_SUPPLY_STATUS_CHARGING;
0652 
0653         sbs_status_correct(client, &val->intval);
0654 
0655         if (chip->poll_time == 0)
0656             chip->last_state = val->intval;
0657         else if (chip->last_state != val->intval) {
0658             cancel_delayed_work_sync(&chip->work);
0659             power_supply_changed(chip->power_supply);
0660             chip->poll_time = 0;
0661         }
0662     } else {
0663         if (psp == POWER_SUPPLY_PROP_STATUS)
0664             val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
0665         else if (psp == POWER_SUPPLY_PROP_CAPACITY)
0666             /* sbs spec says that this can be >100 %
0667              * even if max value is 100 %
0668              */
0669             val->intval = min(ret, 100);
0670         else
0671             val->intval = 0;
0672     }
0673 
0674     return 0;
0675 }
0676 
0677 static int sbs_get_property_index(struct i2c_client *client,
0678     enum power_supply_property psp)
0679 {
0680     int count;
0681 
0682     for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
0683         if (psp == sbs_data[count].psp)
0684             return count;
0685 
0686     dev_warn(&client->dev,
0687         "%s: Invalid Property - %d\n", __func__, psp);
0688 
0689     return -EINVAL;
0690 }
0691 
0692 static const char *sbs_get_constant_string(struct sbs_info *chip,
0693             enum power_supply_property psp)
0694 {
0695     int ret;
0696     char *buf;
0697     u8 addr;
0698 
0699     buf = sbs_get_string_buf(chip, psp);
0700     if (IS_ERR(buf))
0701         return buf;
0702 
0703     if (!buf[0]) {
0704         ret = sbs_get_property_index(chip->client, psp);
0705         if (ret < 0)
0706             return ERR_PTR(ret);
0707 
0708         addr = sbs_data[ret].addr;
0709 
0710         ret = sbs_read_string_data(chip->client, addr, buf);
0711         if (ret < 0)
0712             return ERR_PTR(ret);
0713     }
0714 
0715     return buf;
0716 }
0717 
0718 static void  sbs_unit_adjustment(struct i2c_client *client,
0719     enum power_supply_property psp, union power_supply_propval *val)
0720 {
0721 #define BASE_UNIT_CONVERSION        1000
0722 #define BATTERY_MODE_CAP_MULT_WATT  (10 * BASE_UNIT_CONVERSION)
0723 #define TIME_UNIT_CONVERSION        60
0724 #define TEMP_KELVIN_TO_CELSIUS      2731
0725     switch (psp) {
0726     case POWER_SUPPLY_PROP_ENERGY_NOW:
0727     case POWER_SUPPLY_PROP_ENERGY_FULL:
0728     case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
0729         /* sbs provides energy in units of 10mWh.
0730          * Convert to µWh
0731          */
0732         val->intval *= BATTERY_MODE_CAP_MULT_WATT;
0733         break;
0734 
0735     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0736     case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
0737     case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
0738     case POWER_SUPPLY_PROP_CURRENT_NOW:
0739     case POWER_SUPPLY_PROP_CURRENT_AVG:
0740     case POWER_SUPPLY_PROP_CHARGE_NOW:
0741     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
0742     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
0743     case POWER_SUPPLY_PROP_CHARGE_FULL:
0744     case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0745         val->intval *= BASE_UNIT_CONVERSION;
0746         break;
0747 
0748     case POWER_SUPPLY_PROP_TEMP:
0749         /* sbs provides battery temperature in 0.1K
0750          * so convert it to 0.1°C
0751          */
0752         val->intval -= TEMP_KELVIN_TO_CELSIUS;
0753         break;
0754 
0755     case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
0756     case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
0757     case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
0758         /* sbs provides time to empty and time to full in minutes.
0759          * Convert to seconds
0760          */
0761         val->intval *= TIME_UNIT_CONVERSION;
0762         break;
0763 
0764     default:
0765         dev_dbg(&client->dev,
0766             "%s: no need for unit conversion %d\n", __func__, psp);
0767     }
0768 }
0769 
0770 static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client,
0771     enum sbs_capacity_mode mode)
0772 {
0773     int ret, original_val;
0774 
0775     original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
0776     if (original_val < 0)
0777         return original_val;
0778 
0779     if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode)
0780         return mode;
0781 
0782     if (mode == CAPACITY_MODE_AMPS)
0783         ret = original_val & ~BATTERY_MODE_CAPACITY_MASK;
0784     else
0785         ret = original_val | BATTERY_MODE_CAPACITY_MASK;
0786 
0787     ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
0788     if (ret < 0)
0789         return ret;
0790 
0791     usleep_range(1000, 2000);
0792 
0793     return original_val & BATTERY_MODE_CAPACITY_MASK;
0794 }
0795 
0796 static int sbs_get_battery_capacity(struct i2c_client *client,
0797     int reg_offset, enum power_supply_property psp,
0798     union power_supply_propval *val)
0799 {
0800     s32 ret;
0801     enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS;
0802 
0803     if (power_supply_is_amp_property(psp))
0804         mode = CAPACITY_MODE_AMPS;
0805 
0806     mode = sbs_set_capacity_mode(client, mode);
0807     if ((int)mode < 0)
0808         return mode;
0809 
0810     ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
0811     if (ret < 0)
0812         return ret;
0813 
0814     val->intval = ret;
0815 
0816     ret = sbs_set_capacity_mode(client, mode);
0817     if (ret < 0)
0818         return ret;
0819 
0820     return 0;
0821 }
0822 
0823 static char sbs_serial[5];
0824 static int sbs_get_battery_serial_number(struct i2c_client *client,
0825     union power_supply_propval *val)
0826 {
0827     int ret;
0828 
0829     ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
0830     if (ret < 0)
0831         return ret;
0832 
0833     sprintf(sbs_serial, "%04x", ret);
0834     val->strval = sbs_serial;
0835 
0836     return 0;
0837 }
0838 
0839 static int sbs_get_chemistry(struct sbs_info *chip,
0840         union power_supply_propval *val)
0841 {
0842     const char *chemistry;
0843 
0844     if (chip->technology != -1) {
0845         val->intval = chip->technology;
0846         return 0;
0847     }
0848 
0849     chemistry = sbs_get_constant_string(chip, POWER_SUPPLY_PROP_TECHNOLOGY);
0850 
0851     if (IS_ERR(chemistry))
0852         return PTR_ERR(chemistry);
0853 
0854     if (!strncasecmp(chemistry, "LION", 4))
0855         chip->technology = POWER_SUPPLY_TECHNOLOGY_LION;
0856     else if (!strncasecmp(chemistry, "LiP", 3))
0857         chip->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
0858     else if (!strncasecmp(chemistry, "NiCd", 4))
0859         chip->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
0860     else if (!strncasecmp(chemistry, "NiMH", 4))
0861         chip->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
0862     else
0863         chip->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
0864 
0865     if (chip->technology == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
0866         dev_warn(&chip->client->dev, "Unknown chemistry: %s\n", chemistry);
0867 
0868     val->intval = chip->technology;
0869 
0870     return 0;
0871 }
0872 
0873 static int sbs_get_battery_manufacture_date(struct i2c_client *client,
0874     enum power_supply_property psp,
0875     union power_supply_propval *val)
0876 {
0877     int ret;
0878     u16 day, month, year;
0879 
0880     ret = sbs_read_word_data(client, REG_ADDR_MANUFACTURE_DATE);
0881     if (ret < 0)
0882         return ret;
0883 
0884     day   = ret   & GENMASK(4,  0);
0885     month = (ret  & GENMASK(8,  5)) >> 5;
0886     year  = ((ret & GENMASK(15, 9)) >> 9) + 1980;
0887 
0888     switch (psp) {
0889     case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
0890         val->intval = year;
0891         break;
0892     case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
0893         val->intval = month;
0894         break;
0895     case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
0896         val->intval = day;
0897         break;
0898     default:
0899         return -EINVAL;
0900     }
0901 
0902     return 0;
0903 }
0904 
0905 static int sbs_get_property(struct power_supply *psy,
0906     enum power_supply_property psp,
0907     union power_supply_propval *val)
0908 {
0909     int ret = 0;
0910     struct sbs_info *chip = power_supply_get_drvdata(psy);
0911     struct i2c_client *client = chip->client;
0912     const char *str;
0913 
0914     if (chip->gpio_detect) {
0915         ret = gpiod_get_value_cansleep(chip->gpio_detect);
0916         if (ret < 0)
0917             return ret;
0918         if (psp == POWER_SUPPLY_PROP_PRESENT) {
0919             val->intval = ret;
0920             sbs_update_presence(chip, ret);
0921             return 0;
0922         }
0923         if (ret == 0)
0924             return -ENODATA;
0925     }
0926 
0927     switch (psp) {
0928     case POWER_SUPPLY_PROP_PRESENT:
0929     case POWER_SUPPLY_PROP_HEALTH:
0930         ret = sbs_get_battery_presence_and_health(client, psp, val);
0931 
0932         /* this can only be true if no gpio is used */
0933         if (psp == POWER_SUPPLY_PROP_PRESENT)
0934             return 0;
0935         break;
0936 
0937     case POWER_SUPPLY_PROP_TECHNOLOGY:
0938         ret = sbs_get_chemistry(chip, val);
0939         if (ret < 0)
0940             break;
0941 
0942         goto done; /* don't trigger power_supply_changed()! */
0943 
0944     case POWER_SUPPLY_PROP_ENERGY_NOW:
0945     case POWER_SUPPLY_PROP_ENERGY_FULL:
0946     case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
0947     case POWER_SUPPLY_PROP_CHARGE_NOW:
0948     case POWER_SUPPLY_PROP_CHARGE_FULL:
0949     case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0950         ret = sbs_get_property_index(client, psp);
0951         if (ret < 0)
0952             break;
0953 
0954         /* sbs_get_battery_capacity() will change the battery mode
0955          * temporarily to read the requested attribute. Ensure we stay
0956          * in the desired mode for the duration of the attribute read.
0957          */
0958         mutex_lock(&chip->mode_lock);
0959         ret = sbs_get_battery_capacity(client, ret, psp, val);
0960         mutex_unlock(&chip->mode_lock);
0961         break;
0962 
0963     case POWER_SUPPLY_PROP_SERIAL_NUMBER:
0964         ret = sbs_get_battery_serial_number(client, val);
0965         break;
0966 
0967     case POWER_SUPPLY_PROP_STATUS:
0968     case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
0969     case POWER_SUPPLY_PROP_CYCLE_COUNT:
0970     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0971     case POWER_SUPPLY_PROP_CURRENT_NOW:
0972     case POWER_SUPPLY_PROP_CURRENT_AVG:
0973     case POWER_SUPPLY_PROP_TEMP:
0974     case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
0975     case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
0976     case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
0977     case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
0978     case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
0979     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
0980     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
0981     case POWER_SUPPLY_PROP_CAPACITY:
0982     case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN:
0983         ret = sbs_get_property_index(client, psp);
0984         if (ret < 0)
0985             break;
0986 
0987         ret = sbs_get_battery_property(client, ret, psp, val);
0988         break;
0989 
0990     case POWER_SUPPLY_PROP_MODEL_NAME:
0991     case POWER_SUPPLY_PROP_MANUFACTURER:
0992         str = sbs_get_constant_string(chip, psp);
0993         if (IS_ERR(str))
0994             ret = PTR_ERR(str);
0995         else
0996             val->strval = str;
0997         break;
0998 
0999     case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
1000     case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
1001     case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
1002         ret = sbs_get_battery_manufacture_date(client, psp, val);
1003         break;
1004 
1005     default:
1006         dev_err(&client->dev,
1007             "%s: INVALID property\n", __func__);
1008         return -EINVAL;
1009     }
1010 
1011     if (!chip->gpio_detect && chip->is_present != (ret >= 0)) {
1012         bool old_present = chip->is_present;
1013         union power_supply_propval val;
1014         int err = sbs_get_battery_presence_and_health(
1015                 client, POWER_SUPPLY_PROP_PRESENT, &val);
1016 
1017         sbs_update_presence(chip, !err && val.intval);
1018 
1019         if (old_present != chip->is_present)
1020             power_supply_changed(chip->power_supply);
1021     }
1022 
1023 done:
1024     if (!ret) {
1025         /* Convert units to match requirements for power supply class */
1026         sbs_unit_adjustment(client, psp, val);
1027         dev_dbg(&client->dev,
1028             "%s: property = %d, value = %x\n", __func__,
1029             psp, val->intval);
1030     } else if (!chip->is_present)  {
1031         /* battery not present, so return NODATA for properties */
1032         ret = -ENODATA;
1033     }
1034     return ret;
1035 }
1036 
1037 static void sbs_supply_changed(struct sbs_info *chip)
1038 {
1039     struct power_supply *battery = chip->power_supply;
1040     int ret;
1041 
1042     ret = gpiod_get_value_cansleep(chip->gpio_detect);
1043     if (ret < 0)
1044         return;
1045     sbs_update_presence(chip, ret);
1046     power_supply_changed(battery);
1047 }
1048 
1049 static irqreturn_t sbs_irq(int irq, void *devid)
1050 {
1051     sbs_supply_changed(devid);
1052     return IRQ_HANDLED;
1053 }
1054 
1055 static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
1056     unsigned int data)
1057 {
1058     sbs_supply_changed(i2c_get_clientdata(client));
1059 }
1060 
1061 static void sbs_external_power_changed(struct power_supply *psy)
1062 {
1063     struct sbs_info *chip = power_supply_get_drvdata(psy);
1064 
1065     /* cancel outstanding work */
1066     cancel_delayed_work_sync(&chip->work);
1067 
1068     schedule_delayed_work(&chip->work, HZ);
1069     chip->poll_time = chip->poll_retry_count;
1070 }
1071 
1072 static void sbs_delayed_work(struct work_struct *work)
1073 {
1074     struct sbs_info *chip;
1075     s32 ret;
1076 
1077     chip = container_of(work, struct sbs_info, work.work);
1078 
1079     ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
1080     /* if the read failed, give up on this work */
1081     if (ret < 0) {
1082         chip->poll_time = 0;
1083         return;
1084     }
1085 
1086     if (ret & BATTERY_FULL_CHARGED)
1087         ret = POWER_SUPPLY_STATUS_FULL;
1088     else if (ret & BATTERY_DISCHARGING)
1089         ret = POWER_SUPPLY_STATUS_DISCHARGING;
1090     else
1091         ret = POWER_SUPPLY_STATUS_CHARGING;
1092 
1093     sbs_status_correct(chip->client, &ret);
1094 
1095     if (chip->last_state != ret) {
1096         chip->poll_time = 0;
1097         power_supply_changed(chip->power_supply);
1098         return;
1099     }
1100     if (chip->poll_time > 0) {
1101         schedule_delayed_work(&chip->work, HZ);
1102         chip->poll_time--;
1103         return;
1104     }
1105 }
1106 
1107 static const struct power_supply_desc sbs_default_desc = {
1108     .type = POWER_SUPPLY_TYPE_BATTERY,
1109     .properties = sbs_properties,
1110     .num_properties = ARRAY_SIZE(sbs_properties),
1111     .get_property = sbs_get_property,
1112     .external_power_changed = sbs_external_power_changed,
1113 };
1114 
1115 static int sbs_probe(struct i2c_client *client)
1116 {
1117     struct sbs_info *chip;
1118     struct power_supply_desc *sbs_desc;
1119     struct sbs_platform_data *pdata = client->dev.platform_data;
1120     struct power_supply_config psy_cfg = {};
1121     int rc;
1122     int irq;
1123 
1124     sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
1125             sizeof(*sbs_desc), GFP_KERNEL);
1126     if (!sbs_desc)
1127         return -ENOMEM;
1128 
1129     sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
1130             dev_name(&client->dev));
1131     if (!sbs_desc->name)
1132         return -ENOMEM;
1133 
1134     chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
1135     if (!chip)
1136         return -ENOMEM;
1137 
1138     chip->flags = (u32)(uintptr_t)device_get_match_data(&client->dev);
1139     chip->client = client;
1140     psy_cfg.of_node = client->dev.of_node;
1141     psy_cfg.drv_data = chip;
1142     chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
1143     sbs_invalidate_cached_props(chip);
1144     mutex_init(&chip->mode_lock);
1145 
1146     /* use pdata if available, fall back to DT properties,
1147      * or hardcoded defaults if not
1148      */
1149     rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count",
1150                       &chip->i2c_retry_count);
1151     if (rc)
1152         chip->i2c_retry_count = 0;
1153 
1154     rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count",
1155                       &chip->poll_retry_count);
1156     if (rc)
1157         chip->poll_retry_count = 0;
1158 
1159     if (pdata) {
1160         chip->poll_retry_count = pdata->poll_retry_count;
1161         chip->i2c_retry_count  = pdata->i2c_retry_count;
1162     }
1163     chip->i2c_retry_count = chip->i2c_retry_count + 1;
1164 
1165     chip->charger_broadcasts = !device_property_read_bool(&client->dev,
1166                     "sbs,disable-charger-broadcasts");
1167 
1168     chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
1169             "sbs,battery-detect", GPIOD_IN);
1170     if (IS_ERR(chip->gpio_detect))
1171         return dev_err_probe(&client->dev, PTR_ERR(chip->gpio_detect),
1172                      "Failed to get gpio\n");
1173 
1174     i2c_set_clientdata(client, chip);
1175 
1176     if (!chip->gpio_detect)
1177         goto skip_gpio;
1178 
1179     irq = gpiod_to_irq(chip->gpio_detect);
1180     if (irq <= 0) {
1181         dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
1182         goto skip_gpio;
1183     }
1184 
1185     rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
1186         IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1187         dev_name(&client->dev), chip);
1188     if (rc) {
1189         dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
1190         goto skip_gpio;
1191     }
1192 
1193 skip_gpio:
1194     /*
1195      * Before we register, we might need to make sure we can actually talk
1196      * to the battery.
1197      */
1198     if (!(force_load || chip->gpio_detect)) {
1199         union power_supply_propval val;
1200 
1201         rc = sbs_get_battery_presence_and_health(
1202                 client, POWER_SUPPLY_PROP_PRESENT, &val);
1203         if (rc < 0 || !val.intval)
1204             return dev_err_probe(&client->dev, -ENODEV,
1205                          "Failed to get present status\n");
1206     }
1207 
1208     rc = devm_delayed_work_autocancel(&client->dev, &chip->work,
1209                       sbs_delayed_work);
1210     if (rc)
1211         return rc;
1212 
1213     chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
1214                            &psy_cfg);
1215     if (IS_ERR(chip->power_supply))
1216         return dev_err_probe(&client->dev, PTR_ERR(chip->power_supply),
1217                      "Failed to register power supply\n");
1218 
1219     dev_info(&client->dev,
1220         "%s: battery gas gauge device registered\n", client->name);
1221 
1222     return 0;
1223 }
1224 
1225 #if defined CONFIG_PM_SLEEP
1226 
1227 static int sbs_suspend(struct device *dev)
1228 {
1229     struct i2c_client *client = to_i2c_client(dev);
1230     struct sbs_info *chip = i2c_get_clientdata(client);
1231     int ret;
1232 
1233     if (chip->poll_time > 0)
1234         cancel_delayed_work_sync(&chip->work);
1235 
1236     if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) {
1237         /* Write to manufacturer access with sleep command. */
1238         ret = sbs_write_word_data(client,
1239                       sbs_data[REG_MANUFACTURER_DATA].addr,
1240                       MANUFACTURER_ACCESS_SLEEP);
1241         if (chip->is_present && ret < 0)
1242             return ret;
1243     }
1244 
1245     return 0;
1246 }
1247 
1248 static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
1249 #define SBS_PM_OPS (&sbs_pm_ops)
1250 
1251 #else
1252 #define SBS_PM_OPS NULL
1253 #endif
1254 
1255 static const struct i2c_device_id sbs_id[] = {
1256     { "bq20z65", 0 },
1257     { "bq20z75", 0 },
1258     { "sbs-battery", 1 },
1259     {}
1260 };
1261 MODULE_DEVICE_TABLE(i2c, sbs_id);
1262 
1263 static const struct of_device_id sbs_dt_ids[] = {
1264     { .compatible = "sbs,sbs-battery" },
1265     {
1266         .compatible = "ti,bq20z65",
1267         .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1268     },
1269     {
1270         .compatible = "ti,bq20z75",
1271         .data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1272     },
1273     { }
1274 };
1275 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
1276 
1277 static struct i2c_driver sbs_battery_driver = {
1278     .probe_new  = sbs_probe,
1279     .alert      = sbs_alert,
1280     .id_table   = sbs_id,
1281     .driver = {
1282         .name   = "sbs-battery",
1283         .of_match_table = sbs_dt_ids,
1284         .pm = SBS_PM_OPS,
1285     },
1286 };
1287 module_i2c_driver(sbs_battery_driver);
1288 
1289 MODULE_DESCRIPTION("SBS battery monitor driver");
1290 MODULE_LICENSE("GPL");
1291 
1292 module_param(force_load, bool, 0444);
1293 MODULE_PARM_DESC(force_load,
1294          "Attempt to load the driver even if no battery is connected");