linux/drivers/power/supply/sbs-battery.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Gas Gauge driver for SBS Compliant Batteries
   4 *
   5 * Copyright (c) 2010, NVIDIA Corporation.
   6 */
   7
   8#include <linux/delay.h>
   9#include <linux/err.h>
  10#include <linux/gpio/consumer.h>
  11#include <linux/i2c.h>
  12#include <linux/init.h>
  13#include <linux/interrupt.h>
  14#include <linux/kernel.h>
  15#include <linux/module.h>
  16#include <linux/of.h>
  17#include <linux/of_device.h>
  18#include <linux/power/sbs-battery.h>
  19#include <linux/power_supply.h>
  20#include <linux/slab.h>
  21#include <linux/stat.h>
  22
  23enum {
  24        REG_MANUFACTURER_DATA,
  25        REG_TEMPERATURE,
  26        REG_VOLTAGE,
  27        REG_CURRENT,
  28        REG_CAPACITY,
  29        REG_TIME_TO_EMPTY,
  30        REG_TIME_TO_FULL,
  31        REG_STATUS,
  32        REG_CAPACITY_LEVEL,
  33        REG_CYCLE_COUNT,
  34        REG_SERIAL_NUMBER,
  35        REG_REMAINING_CAPACITY,
  36        REG_REMAINING_CAPACITY_CHARGE,
  37        REG_FULL_CHARGE_CAPACITY,
  38        REG_FULL_CHARGE_CAPACITY_CHARGE,
  39        REG_DESIGN_CAPACITY,
  40        REG_DESIGN_CAPACITY_CHARGE,
  41        REG_DESIGN_VOLTAGE_MIN,
  42        REG_DESIGN_VOLTAGE_MAX,
  43        REG_MANUFACTURER,
  44        REG_MODEL_NAME,
  45};
  46
  47/* Battery Mode defines */
  48#define BATTERY_MODE_OFFSET             0x03
  49#define BATTERY_MODE_MASK               0x8000
  50enum sbs_battery_mode {
  51        BATTERY_MODE_AMPS = 0,
  52        BATTERY_MODE_WATTS = 0x8000
  53};
  54
  55/* manufacturer access defines */
  56#define MANUFACTURER_ACCESS_STATUS      0x0006
  57#define MANUFACTURER_ACCESS_SLEEP       0x0011
  58
  59/* battery status value bits */
  60#define BATTERY_INITIALIZED             0x80
  61#define BATTERY_DISCHARGING             0x40
  62#define BATTERY_FULL_CHARGED            0x20
  63#define BATTERY_FULL_DISCHARGED         0x10
  64
  65/* min_value and max_value are only valid for numerical data */
  66#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
  67        .psp = _psp, \
  68        .addr = _addr, \
  69        .min_value = _min_value, \
  70        .max_value = _max_value, \
  71}
  72
  73static const struct chip_data {
  74        enum power_supply_property psp;
  75        u8 addr;
  76        int min_value;
  77        int max_value;
  78} sbs_data[] = {
  79        [REG_MANUFACTURER_DATA] =
  80                SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
  81        [REG_TEMPERATURE] =
  82                SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
  83        [REG_VOLTAGE] =
  84                SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
  85        [REG_CURRENT] =
  86                SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
  87        [REG_CAPACITY] =
  88                SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
  89        [REG_REMAINING_CAPACITY] =
  90                SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
  91        [REG_REMAINING_CAPACITY_CHARGE] =
  92                SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
  93        [REG_FULL_CHARGE_CAPACITY] =
  94                SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
  95        [REG_FULL_CHARGE_CAPACITY_CHARGE] =
  96                SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
  97        [REG_TIME_TO_EMPTY] =
  98                SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
  99        [REG_TIME_TO_FULL] =
 100                SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
 101        [REG_STATUS] =
 102                SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
 103        [REG_CAPACITY_LEVEL] =
 104                SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
 105        [REG_CYCLE_COUNT] =
 106                SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
 107        [REG_DESIGN_CAPACITY] =
 108                SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
 109        [REG_DESIGN_CAPACITY_CHARGE] =
 110                SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
 111        [REG_DESIGN_VOLTAGE_MIN] =
 112                SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
 113        [REG_DESIGN_VOLTAGE_MAX] =
 114                SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
 115        [REG_SERIAL_NUMBER] =
 116                SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
 117        /* Properties of type `const char *' */
 118        [REG_MANUFACTURER] =
 119                SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
 120        [REG_MODEL_NAME] =
 121                SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
 122};
 123
 124static enum power_supply_property sbs_properties[] = {
 125        POWER_SUPPLY_PROP_STATUS,
 126        POWER_SUPPLY_PROP_CAPACITY_LEVEL,
 127        POWER_SUPPLY_PROP_HEALTH,
 128        POWER_SUPPLY_PROP_PRESENT,
 129        POWER_SUPPLY_PROP_TECHNOLOGY,
 130        POWER_SUPPLY_PROP_CYCLE_COUNT,
 131        POWER_SUPPLY_PROP_VOLTAGE_NOW,
 132        POWER_SUPPLY_PROP_CURRENT_NOW,
 133        POWER_SUPPLY_PROP_CAPACITY,
 134        POWER_SUPPLY_PROP_TEMP,
 135        POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
 136        POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
 137        POWER_SUPPLY_PROP_SERIAL_NUMBER,
 138        POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
 139        POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
 140        POWER_SUPPLY_PROP_ENERGY_NOW,
 141        POWER_SUPPLY_PROP_ENERGY_FULL,
 142        POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
 143        POWER_SUPPLY_PROP_CHARGE_NOW,
 144        POWER_SUPPLY_PROP_CHARGE_FULL,
 145        POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
 146        /* Properties of type `const char *' */
 147        POWER_SUPPLY_PROP_MANUFACTURER,
 148        POWER_SUPPLY_PROP_MODEL_NAME
 149};
 150
 151/* Supports special manufacturer commands from TI BQ20Z75 IC. */
 152#define SBS_FLAGS_TI_BQ20Z75            BIT(0)
 153
 154struct sbs_info {
 155        struct i2c_client               *client;
 156        struct power_supply             *power_supply;
 157        bool                            is_present;
 158        struct gpio_desc                *gpio_detect;
 159        bool                            enable_detection;
 160        int                             last_state;
 161        int                             poll_time;
 162        u32                             i2c_retry_count;
 163        u32                             poll_retry_count;
 164        struct delayed_work             work;
 165        struct mutex                    mode_lock;
 166        u32                             flags;
 167};
 168
 169static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
 170static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
 171static bool force_load;
 172
 173static int sbs_read_word_data(struct i2c_client *client, u8 address)
 174{
 175        struct sbs_info *chip = i2c_get_clientdata(client);
 176        int retries = chip->i2c_retry_count;
 177        s32 ret = 0;
 178
 179        while (retries > 0) {
 180                ret = i2c_smbus_read_word_data(client, address);
 181                if (ret >= 0)
 182                        break;
 183                retries--;
 184        }
 185
 186        if (ret < 0) {
 187                dev_dbg(&client->dev,
 188                        "%s: i2c read at address 0x%x failed\n",
 189                        __func__, address);
 190                return ret;
 191        }
 192
 193        return ret;
 194}
 195
 196static int sbs_read_string_data(struct i2c_client *client, u8 address,
 197                                char *values)
 198{
 199        struct sbs_info *chip = i2c_get_clientdata(client);
 200        s32 ret = 0, block_length = 0;
 201        int retries_length, retries_block;
 202        u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
 203
 204        retries_length = chip->i2c_retry_count;
 205        retries_block = chip->i2c_retry_count;
 206
 207        /* Adapter needs to support these two functions */
 208        if (!i2c_check_functionality(client->adapter,
 209                                     I2C_FUNC_SMBUS_BYTE_DATA |
 210                                     I2C_FUNC_SMBUS_I2C_BLOCK)){
 211                return -ENODEV;
 212        }
 213
 214        /* Get the length of block data */
 215        while (retries_length > 0) {
 216                ret = i2c_smbus_read_byte_data(client, address);
 217                if (ret >= 0)
 218                        break;
 219                retries_length--;
 220        }
 221
 222        if (ret < 0) {
 223                dev_dbg(&client->dev,
 224                        "%s: i2c read at address 0x%x failed\n",
 225                        __func__, address);
 226                return ret;
 227        }
 228
 229        /* block_length does not include NULL terminator */
 230        block_length = ret;
 231        if (block_length > I2C_SMBUS_BLOCK_MAX) {
 232                dev_err(&client->dev,
 233                        "%s: Returned block_length is longer than 0x%x\n",
 234                        __func__, I2C_SMBUS_BLOCK_MAX);
 235                return -EINVAL;
 236        }
 237
 238        /* Get the block data */
 239        while (retries_block > 0) {
 240                ret = i2c_smbus_read_i2c_block_data(
 241                                client, address,
 242                                block_length + 1, block_buffer);
 243                if (ret >= 0)
 244                        break;
 245                retries_block--;
 246        }
 247
 248        if (ret < 0) {
 249                dev_dbg(&client->dev,
 250                        "%s: i2c read at address 0x%x failed\n",
 251                        __func__, address);
 252                return ret;
 253        }
 254
 255        /* block_buffer[0] == block_length */
 256        memcpy(values, block_buffer + 1, block_length);
 257        values[block_length] = '\0';
 258
 259        return ret;
 260}
 261
 262static int sbs_write_word_data(struct i2c_client *client, u8 address,
 263        u16 value)
 264{
 265        struct sbs_info *chip = i2c_get_clientdata(client);
 266        int retries = chip->i2c_retry_count;
 267        s32 ret = 0;
 268
 269        while (retries > 0) {
 270                ret = i2c_smbus_write_word_data(client, address, value);
 271                if (ret >= 0)
 272                        break;
 273                retries--;
 274        }
 275
 276        if (ret < 0) {
 277                dev_dbg(&client->dev,
 278                        "%s: i2c write to address 0x%x failed\n",
 279                        __func__, address);
 280                return ret;
 281        }
 282
 283        return 0;
 284}
 285
 286static int sbs_status_correct(struct i2c_client *client, int *intval)
 287{
 288        int ret;
 289
 290        ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
 291        if (ret < 0)
 292                return ret;
 293
 294        ret = (s16)ret;
 295
 296        /* Not drawing current means full (cannot be not charging) */
 297        if (ret == 0)
 298                *intval = POWER_SUPPLY_STATUS_FULL;
 299
 300        if (*intval == POWER_SUPPLY_STATUS_FULL) {
 301                /* Drawing or providing current when full */
 302                if (ret > 0)
 303                        *intval = POWER_SUPPLY_STATUS_CHARGING;
 304                else if (ret < 0)
 305                        *intval = POWER_SUPPLY_STATUS_DISCHARGING;
 306        }
 307
 308        return 0;
 309}
 310
 311static int sbs_get_battery_presence_and_health(
 312        struct i2c_client *client, enum power_supply_property psp,
 313        union power_supply_propval *val)
 314{
 315        int ret;
 316
 317        if (psp == POWER_SUPPLY_PROP_PRESENT) {
 318                /* Dummy command; if it succeeds, battery is present. */
 319                ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
 320                if (ret < 0)
 321                        val->intval = 0; /* battery disconnected */
 322                else
 323                        val->intval = 1; /* battery present */
 324        } else { /* POWER_SUPPLY_PROP_HEALTH */
 325                /* SBS spec doesn't have a general health command. */
 326                val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
 327        }
 328
 329        return 0;
 330}
 331
 332static int sbs_get_ti_battery_presence_and_health(
 333        struct i2c_client *client, enum power_supply_property psp,
 334        union power_supply_propval *val)
 335{
 336        s32 ret;
 337
 338        /*
 339         * Write to ManufacturerAccess with ManufacturerAccess command
 340         * and then read the status.
 341         */
 342        ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
 343                                  MANUFACTURER_ACCESS_STATUS);
 344        if (ret < 0) {
 345                if (psp == POWER_SUPPLY_PROP_PRESENT)
 346                        val->intval = 0; /* battery removed */
 347                return ret;
 348        }
 349
 350        ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
 351        if (ret < 0) {
 352                if (psp == POWER_SUPPLY_PROP_PRESENT)
 353                        val->intval = 0; /* battery removed */
 354                return ret;
 355        }
 356
 357        if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
 358            ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
 359                val->intval = 0;
 360                return 0;
 361        }
 362
 363        /* Mask the upper nibble of 2nd byte and
 364         * lower byte of response then
 365         * shift the result by 8 to get status*/
 366        ret &= 0x0F00;
 367        ret >>= 8;
 368        if (psp == POWER_SUPPLY_PROP_PRESENT) {
 369                if (ret == 0x0F)
 370                        /* battery removed */
 371                        val->intval = 0;
 372                else
 373                        val->intval = 1;
 374        } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
 375                if (ret == 0x09)
 376                        val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
 377                else if (ret == 0x0B)
 378                        val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
 379                else if (ret == 0x0C)
 380                        val->intval = POWER_SUPPLY_HEALTH_DEAD;
 381                else
 382                        val->intval = POWER_SUPPLY_HEALTH_GOOD;
 383        }
 384
 385        return 0;
 386}
 387
 388static int sbs_get_battery_property(struct i2c_client *client,
 389        int reg_offset, enum power_supply_property psp,
 390        union power_supply_propval *val)
 391{
 392        struct sbs_info *chip = i2c_get_clientdata(client);
 393        s32 ret;
 394
 395        ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
 396        if (ret < 0)
 397                return ret;
 398
 399        /* returned values are 16 bit */
 400        if (sbs_data[reg_offset].min_value < 0)
 401                ret = (s16)ret;
 402
 403        if (ret >= sbs_data[reg_offset].min_value &&
 404            ret <= sbs_data[reg_offset].max_value) {
 405                val->intval = ret;
 406                if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
 407                        if (!(ret & BATTERY_INITIALIZED))
 408                                val->intval =
 409                                        POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
 410                        else if (ret & BATTERY_FULL_CHARGED)
 411                                val->intval =
 412                                        POWER_SUPPLY_CAPACITY_LEVEL_FULL;
 413                        else if (ret & BATTERY_FULL_DISCHARGED)
 414                                val->intval =
 415                                        POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
 416                        else
 417                                val->intval =
 418                                        POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
 419                        return 0;
 420                } else if (psp != POWER_SUPPLY_PROP_STATUS) {
 421                        return 0;
 422                }
 423
 424                if (ret & BATTERY_FULL_CHARGED)
 425                        val->intval = POWER_SUPPLY_STATUS_FULL;
 426                else if (ret & BATTERY_DISCHARGING)
 427                        val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
 428                else
 429                        val->intval = POWER_SUPPLY_STATUS_CHARGING;
 430
 431                sbs_status_correct(client, &val->intval);
 432
 433                if (chip->poll_time == 0)
 434                        chip->last_state = val->intval;
 435                else if (chip->last_state != val->intval) {
 436                        cancel_delayed_work_sync(&chip->work);
 437                        power_supply_changed(chip->power_supply);
 438                        chip->poll_time = 0;
 439                }
 440        } else {
 441                if (psp == POWER_SUPPLY_PROP_STATUS)
 442                        val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
 443                else if (psp == POWER_SUPPLY_PROP_CAPACITY)
 444                        /* sbs spec says that this can be >100 %
 445                         * even if max value is 100 %
 446                         */
 447                        val->intval = min(ret, 100);
 448                else
 449                        val->intval = 0;
 450        }
 451
 452        return 0;
 453}
 454
 455static int sbs_get_battery_string_property(struct i2c_client *client,
 456        int reg_offset, enum power_supply_property psp, char *val)
 457{
 458        s32 ret;
 459
 460        ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
 461
 462        if (ret < 0)
 463                return ret;
 464
 465        return 0;
 466}
 467
 468static void  sbs_unit_adjustment(struct i2c_client *client,
 469        enum power_supply_property psp, union power_supply_propval *val)
 470{
 471#define BASE_UNIT_CONVERSION            1000
 472#define BATTERY_MODE_CAP_MULT_WATT      (10 * BASE_UNIT_CONVERSION)
 473#define TIME_UNIT_CONVERSION            60
 474#define TEMP_KELVIN_TO_CELSIUS          2731
 475        switch (psp) {
 476        case POWER_SUPPLY_PROP_ENERGY_NOW:
 477        case POWER_SUPPLY_PROP_ENERGY_FULL:
 478        case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
 479                /* sbs provides energy in units of 10mWh.
 480                 * Convert to µWh
 481                 */
 482                val->intval *= BATTERY_MODE_CAP_MULT_WATT;
 483                break;
 484
 485        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 486        case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
 487        case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
 488        case POWER_SUPPLY_PROP_CURRENT_NOW:
 489        case POWER_SUPPLY_PROP_CHARGE_NOW:
 490        case POWER_SUPPLY_PROP_CHARGE_FULL:
 491        case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
 492                val->intval *= BASE_UNIT_CONVERSION;
 493                break;
 494
 495        case POWER_SUPPLY_PROP_TEMP:
 496                /* sbs provides battery temperature in 0.1K
 497                 * so convert it to 0.1°C
 498                 */
 499                val->intval -= TEMP_KELVIN_TO_CELSIUS;
 500                break;
 501
 502        case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
 503        case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
 504                /* sbs provides time to empty and time to full in minutes.
 505                 * Convert to seconds
 506                 */
 507                val->intval *= TIME_UNIT_CONVERSION;
 508                break;
 509
 510        default:
 511                dev_dbg(&client->dev,
 512                        "%s: no need for unit conversion %d\n", __func__, psp);
 513        }
 514}
 515
 516static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
 517        enum sbs_battery_mode mode)
 518{
 519        int ret, original_val;
 520
 521        original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
 522        if (original_val < 0)
 523                return original_val;
 524
 525        if ((original_val & BATTERY_MODE_MASK) == mode)
 526                return mode;
 527
 528        if (mode == BATTERY_MODE_AMPS)
 529                ret = original_val & ~BATTERY_MODE_MASK;
 530        else
 531                ret = original_val | BATTERY_MODE_MASK;
 532
 533        ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
 534        if (ret < 0)
 535                return ret;
 536
 537        usleep_range(1000, 2000);
 538
 539        return original_val & BATTERY_MODE_MASK;
 540}
 541
 542static int sbs_get_battery_capacity(struct i2c_client *client,
 543        int reg_offset, enum power_supply_property psp,
 544        union power_supply_propval *val)
 545{
 546        s32 ret;
 547        enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
 548
 549        if (power_supply_is_amp_property(psp))
 550                mode = BATTERY_MODE_AMPS;
 551
 552        mode = sbs_set_battery_mode(client, mode);
 553        if (mode < 0)
 554                return mode;
 555
 556        ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
 557        if (ret < 0)
 558                return ret;
 559
 560        val->intval = ret;
 561
 562        ret = sbs_set_battery_mode(client, mode);
 563        if (ret < 0)
 564                return ret;
 565
 566        return 0;
 567}
 568
 569static char sbs_serial[5];
 570static int sbs_get_battery_serial_number(struct i2c_client *client,
 571        union power_supply_propval *val)
 572{
 573        int ret;
 574
 575        ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
 576        if (ret < 0)
 577                return ret;
 578
 579        sprintf(sbs_serial, "%04x", ret);
 580        val->strval = sbs_serial;
 581
 582        return 0;
 583}
 584
 585static int sbs_get_property_index(struct i2c_client *client,
 586        enum power_supply_property psp)
 587{
 588        int count;
 589        for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
 590                if (psp == sbs_data[count].psp)
 591                        return count;
 592
 593        dev_warn(&client->dev,
 594                "%s: Invalid Property - %d\n", __func__, psp);
 595
 596        return -EINVAL;
 597}
 598
 599static int sbs_get_property(struct power_supply *psy,
 600        enum power_supply_property psp,
 601        union power_supply_propval *val)
 602{
 603        int ret = 0;
 604        struct sbs_info *chip = power_supply_get_drvdata(psy);
 605        struct i2c_client *client = chip->client;
 606
 607        if (chip->gpio_detect) {
 608                ret = gpiod_get_value_cansleep(chip->gpio_detect);
 609                if (ret < 0)
 610                        return ret;
 611                if (psp == POWER_SUPPLY_PROP_PRESENT) {
 612                        val->intval = ret;
 613                        chip->is_present = val->intval;
 614                        return 0;
 615                }
 616                if (ret == 0)
 617                        return -ENODATA;
 618        }
 619
 620        switch (psp) {
 621        case POWER_SUPPLY_PROP_PRESENT:
 622        case POWER_SUPPLY_PROP_HEALTH:
 623                if (client->flags & SBS_FLAGS_TI_BQ20Z75)
 624                        ret = sbs_get_ti_battery_presence_and_health(client,
 625                                                                     psp, val);
 626                else
 627                        ret = sbs_get_battery_presence_and_health(client, psp,
 628                                                                  val);
 629                if (psp == POWER_SUPPLY_PROP_PRESENT)
 630                        return 0;
 631                break;
 632
 633        case POWER_SUPPLY_PROP_TECHNOLOGY:
 634                val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
 635                goto done; /* don't trigger power_supply_changed()! */
 636
 637        case POWER_SUPPLY_PROP_ENERGY_NOW:
 638        case POWER_SUPPLY_PROP_ENERGY_FULL:
 639        case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
 640        case POWER_SUPPLY_PROP_CHARGE_NOW:
 641        case POWER_SUPPLY_PROP_CHARGE_FULL:
 642        case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
 643                ret = sbs_get_property_index(client, psp);
 644                if (ret < 0)
 645                        break;
 646
 647                /* sbs_get_battery_capacity() will change the battery mode
 648                 * temporarily to read the requested attribute. Ensure we stay
 649                 * in the desired mode for the duration of the attribute read.
 650                 */
 651                mutex_lock(&chip->mode_lock);
 652                ret = sbs_get_battery_capacity(client, ret, psp, val);
 653                mutex_unlock(&chip->mode_lock);
 654                break;
 655
 656        case POWER_SUPPLY_PROP_SERIAL_NUMBER:
 657                ret = sbs_get_battery_serial_number(client, val);
 658                break;
 659
 660        case POWER_SUPPLY_PROP_STATUS:
 661        case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
 662        case POWER_SUPPLY_PROP_CYCLE_COUNT:
 663        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 664        case POWER_SUPPLY_PROP_CURRENT_NOW:
 665        case POWER_SUPPLY_PROP_TEMP:
 666        case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
 667        case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
 668        case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
 669        case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
 670        case POWER_SUPPLY_PROP_CAPACITY:
 671                ret = sbs_get_property_index(client, psp);
 672                if (ret < 0)
 673                        break;
 674
 675                ret = sbs_get_battery_property(client, ret, psp, val);
 676                break;
 677
 678        case POWER_SUPPLY_PROP_MODEL_NAME:
 679                ret = sbs_get_property_index(client, psp);
 680                if (ret < 0)
 681                        break;
 682
 683                ret = sbs_get_battery_string_property(client, ret, psp,
 684                                                      model_name);
 685                val->strval = model_name;
 686                break;
 687
 688        case POWER_SUPPLY_PROP_MANUFACTURER:
 689                ret = sbs_get_property_index(client, psp);
 690                if (ret < 0)
 691                        break;
 692
 693                ret = sbs_get_battery_string_property(client, ret, psp,
 694                                                      manufacturer);
 695                val->strval = manufacturer;
 696                break;
 697
 698        default:
 699                dev_err(&client->dev,
 700                        "%s: INVALID property\n", __func__);
 701                return -EINVAL;
 702        }
 703
 704        if (!chip->enable_detection)
 705                goto done;
 706
 707        if (!chip->gpio_detect &&
 708                chip->is_present != (ret >= 0)) {
 709                chip->is_present = (ret >= 0);
 710                power_supply_changed(chip->power_supply);
 711        }
 712
 713done:
 714        if (!ret) {
 715                /* Convert units to match requirements for power supply class */
 716                sbs_unit_adjustment(client, psp, val);
 717        }
 718
 719        dev_dbg(&client->dev,
 720                "%s: property = %d, value = %x\n", __func__, psp, val->intval);
 721
 722        if (ret && chip->is_present)
 723                return ret;
 724
 725        /* battery not present, so return NODATA for properties */
 726        if (ret)
 727                return -ENODATA;
 728
 729        return 0;
 730}
 731
 732static void sbs_supply_changed(struct sbs_info *chip)
 733{
 734        struct power_supply *battery = chip->power_supply;
 735        int ret;
 736
 737        ret = gpiod_get_value_cansleep(chip->gpio_detect);
 738        if (ret < 0)
 739                return;
 740        chip->is_present = ret;
 741        power_supply_changed(battery);
 742}
 743
 744static irqreturn_t sbs_irq(int irq, void *devid)
 745{
 746        sbs_supply_changed(devid);
 747        return IRQ_HANDLED;
 748}
 749
 750static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
 751        unsigned int data)
 752{
 753        sbs_supply_changed(i2c_get_clientdata(client));
 754}
 755
 756static void sbs_external_power_changed(struct power_supply *psy)
 757{
 758        struct sbs_info *chip = power_supply_get_drvdata(psy);
 759
 760        /* cancel outstanding work */
 761        cancel_delayed_work_sync(&chip->work);
 762
 763        schedule_delayed_work(&chip->work, HZ);
 764        chip->poll_time = chip->poll_retry_count;
 765}
 766
 767static void sbs_delayed_work(struct work_struct *work)
 768{
 769        struct sbs_info *chip;
 770        s32 ret;
 771
 772        chip = container_of(work, struct sbs_info, work.work);
 773
 774        ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
 775        /* if the read failed, give up on this work */
 776        if (ret < 0) {
 777                chip->poll_time = 0;
 778                return;
 779        }
 780
 781        if (ret & BATTERY_FULL_CHARGED)
 782                ret = POWER_SUPPLY_STATUS_FULL;
 783        else if (ret & BATTERY_DISCHARGING)
 784                ret = POWER_SUPPLY_STATUS_DISCHARGING;
 785        else
 786                ret = POWER_SUPPLY_STATUS_CHARGING;
 787
 788        sbs_status_correct(chip->client, &ret);
 789
 790        if (chip->last_state != ret) {
 791                chip->poll_time = 0;
 792                power_supply_changed(chip->power_supply);
 793                return;
 794        }
 795        if (chip->poll_time > 0) {
 796                schedule_delayed_work(&chip->work, HZ);
 797                chip->poll_time--;
 798                return;
 799        }
 800}
 801
 802static const struct power_supply_desc sbs_default_desc = {
 803        .type = POWER_SUPPLY_TYPE_BATTERY,
 804        .properties = sbs_properties,
 805        .num_properties = ARRAY_SIZE(sbs_properties),
 806        .get_property = sbs_get_property,
 807        .external_power_changed = sbs_external_power_changed,
 808};
 809
 810static int sbs_probe(struct i2c_client *client,
 811        const struct i2c_device_id *id)
 812{
 813        struct sbs_info *chip;
 814        struct power_supply_desc *sbs_desc;
 815        struct sbs_platform_data *pdata = client->dev.platform_data;
 816        struct power_supply_config psy_cfg = {};
 817        int rc;
 818        int irq;
 819
 820        sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
 821                        sizeof(*sbs_desc), GFP_KERNEL);
 822        if (!sbs_desc)
 823                return -ENOMEM;
 824
 825        sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
 826                        dev_name(&client->dev));
 827        if (!sbs_desc->name)
 828                return -ENOMEM;
 829
 830        chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
 831        if (!chip)
 832                return -ENOMEM;
 833
 834        chip->flags = (u32)(uintptr_t)of_device_get_match_data(&client->dev);
 835        chip->client = client;
 836        chip->enable_detection = false;
 837        psy_cfg.of_node = client->dev.of_node;
 838        psy_cfg.drv_data = chip;
 839        chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
 840        mutex_init(&chip->mode_lock);
 841
 842        /* use pdata if available, fall back to DT properties,
 843         * or hardcoded defaults if not
 844         */
 845        rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
 846                                  &chip->i2c_retry_count);
 847        if (rc)
 848                chip->i2c_retry_count = 0;
 849
 850        rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
 851                                  &chip->poll_retry_count);
 852        if (rc)
 853                chip->poll_retry_count = 0;
 854
 855        if (pdata) {
 856                chip->poll_retry_count = pdata->poll_retry_count;
 857                chip->i2c_retry_count  = pdata->i2c_retry_count;
 858        }
 859        chip->i2c_retry_count = chip->i2c_retry_count + 1;
 860
 861        chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
 862                        "sbs,battery-detect", GPIOD_IN);
 863        if (IS_ERR(chip->gpio_detect)) {
 864                dev_err(&client->dev, "Failed to get gpio: %ld\n",
 865                        PTR_ERR(chip->gpio_detect));
 866                return PTR_ERR(chip->gpio_detect);
 867        }
 868
 869        i2c_set_clientdata(client, chip);
 870
 871        if (!chip->gpio_detect)
 872                goto skip_gpio;
 873
 874        irq = gpiod_to_irq(chip->gpio_detect);
 875        if (irq <= 0) {
 876                dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
 877                goto skip_gpio;
 878        }
 879
 880        rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
 881                IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
 882                dev_name(&client->dev), chip);
 883        if (rc) {
 884                dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
 885                goto skip_gpio;
 886        }
 887
 888skip_gpio:
 889        /*
 890         * Before we register, we might need to make sure we can actually talk
 891         * to the battery.
 892         */
 893        if (!(force_load || chip->gpio_detect)) {
 894                rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
 895
 896                if (rc < 0) {
 897                        dev_err(&client->dev, "%s: Failed to get device status\n",
 898                                __func__);
 899                        goto exit_psupply;
 900                }
 901        }
 902
 903        chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
 904                                                   &psy_cfg);
 905        if (IS_ERR(chip->power_supply)) {
 906                dev_err(&client->dev,
 907                        "%s: Failed to register power supply\n", __func__);
 908                rc = PTR_ERR(chip->power_supply);
 909                goto exit_psupply;
 910        }
 911
 912        dev_info(&client->dev,
 913                "%s: battery gas gauge device registered\n", client->name);
 914
 915        INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
 916
 917        chip->enable_detection = true;
 918
 919        return 0;
 920
 921exit_psupply:
 922        return rc;
 923}
 924
 925static int sbs_remove(struct i2c_client *client)
 926{
 927        struct sbs_info *chip = i2c_get_clientdata(client);
 928
 929        cancel_delayed_work_sync(&chip->work);
 930
 931        return 0;
 932}
 933
 934#if defined CONFIG_PM_SLEEP
 935
 936static int sbs_suspend(struct device *dev)
 937{
 938        struct i2c_client *client = to_i2c_client(dev);
 939        struct sbs_info *chip = i2c_get_clientdata(client);
 940        int ret;
 941
 942        if (chip->poll_time > 0)
 943                cancel_delayed_work_sync(&chip->work);
 944
 945        if (chip->flags & SBS_FLAGS_TI_BQ20Z75) {
 946                /* Write to manufacturer access with sleep command. */
 947                ret = sbs_write_word_data(client,
 948                                          sbs_data[REG_MANUFACTURER_DATA].addr,
 949                                          MANUFACTURER_ACCESS_SLEEP);
 950                if (chip->is_present && ret < 0)
 951                        return ret;
 952        }
 953
 954        return 0;
 955}
 956
 957static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
 958#define SBS_PM_OPS (&sbs_pm_ops)
 959
 960#else
 961#define SBS_PM_OPS NULL
 962#endif
 963
 964static const struct i2c_device_id sbs_id[] = {
 965        { "bq20z75", 0 },
 966        { "sbs-battery", 1 },
 967        {}
 968};
 969MODULE_DEVICE_TABLE(i2c, sbs_id);
 970
 971static const struct of_device_id sbs_dt_ids[] = {
 972        { .compatible = "sbs,sbs-battery" },
 973        {
 974                .compatible = "ti,bq20z75",
 975                .data = (void *)SBS_FLAGS_TI_BQ20Z75,
 976        },
 977        { }
 978};
 979MODULE_DEVICE_TABLE(of, sbs_dt_ids);
 980
 981static struct i2c_driver sbs_battery_driver = {
 982        .probe          = sbs_probe,
 983        .remove         = sbs_remove,
 984        .alert          = sbs_alert,
 985        .id_table       = sbs_id,
 986        .driver = {
 987                .name   = "sbs-battery",
 988                .of_match_table = sbs_dt_ids,
 989                .pm     = SBS_PM_OPS,
 990        },
 991};
 992module_i2c_driver(sbs_battery_driver);
 993
 994MODULE_DESCRIPTION("SBS battery monitor driver");
 995MODULE_LICENSE("GPL");
 996
 997module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
 998MODULE_PARM_DESC(force_load,
 999                 "Attempt to load the driver even if no battery is connected");
1000