linux/drivers/power/supply/bq25890_charger.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * TI BQ25890 charger driver
   4 *
   5 * Copyright (C) 2015 Intel Corporation
   6 */
   7
   8#include <linux/module.h>
   9#include <linux/i2c.h>
  10#include <linux/power_supply.h>
  11#include <linux/regmap.h>
  12#include <linux/types.h>
  13#include <linux/gpio/consumer.h>
  14#include <linux/interrupt.h>
  15#include <linux/delay.h>
  16#include <linux/usb/phy.h>
  17
  18#include <linux/acpi.h>
  19#include <linux/of.h>
  20
  21#define BQ25890_MANUFACTURER            "Texas Instruments"
  22#define BQ25890_IRQ_PIN                 "bq25890_irq"
  23
  24#define BQ25890_ID                      3
  25#define BQ25895_ID                      7
  26#define BQ25896_ID                      0
  27
  28enum bq25890_chip_version {
  29        BQ25890,
  30        BQ25892,
  31        BQ25895,
  32        BQ25896,
  33};
  34
  35static const char *const bq25890_chip_name[] = {
  36        "BQ25890",
  37        "BQ25892",
  38        "BQ25895",
  39        "BQ25896",
  40};
  41
  42enum bq25890_fields {
  43        F_EN_HIZ, F_EN_ILIM, F_IILIM,                                /* Reg00 */
  44        F_BHOT, F_BCOLD, F_VINDPM_OFS,                               /* Reg01 */
  45        F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
  46        F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN,          /* Reg02 */
  47        F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,
  48        F_MIN_VBAT_SEL,                                              /* Reg03 */
  49        F_PUMPX_EN, F_ICHG,                                          /* Reg04 */
  50        F_IPRECHG, F_ITERM,                                          /* Reg05 */
  51        F_VREG, F_BATLOWV, F_VRECHG,                                 /* Reg06 */
  52        F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
  53        F_JEITA_ISET,                                                /* Reg07 */
  54        F_BATCMP, F_VCLAMP, F_TREG,                                  /* Reg08 */
  55        F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
  56        F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN,       /* Reg09 */
  57        F_BOOSTV, F_PFM_OTG_DIS, F_BOOSTI,                           /* Reg0A */
  58        F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_0B_RSVD,
  59        F_VSYS_STAT,                                                 /* Reg0B */
  60        F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
  61        F_NTC_FAULT,                                                 /* Reg0C */
  62        F_FORCE_VINDPM, F_VINDPM,                                    /* Reg0D */
  63        F_THERM_STAT, F_BATV,                                        /* Reg0E */
  64        F_SYSV,                                                      /* Reg0F */
  65        F_TSPCT,                                                     /* Reg10 */
  66        F_VBUS_GD, F_VBUSV,                                          /* Reg11 */
  67        F_ICHGR,                                                     /* Reg12 */
  68        F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM,                        /* Reg13 */
  69        F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV,   /* Reg14 */
  70
  71        F_MAX_FIELDS
  72};
  73
  74/* initial field values, converted to register values */
  75struct bq25890_init_data {
  76        u8 ichg;        /* charge current               */
  77        u8 vreg;        /* regulation voltage           */
  78        u8 iterm;       /* termination current          */
  79        u8 iprechg;     /* precharge current            */
  80        u8 sysvmin;     /* minimum system voltage limit */
  81        u8 boostv;      /* boost regulation voltage     */
  82        u8 boosti;      /* boost current limit          */
  83        u8 boostf;      /* boost frequency              */
  84        u8 ilim_en;     /* enable ILIM pin              */
  85        u8 treg;        /* thermal regulation threshold */
  86        u8 rbatcomp;    /* IBAT sense resistor value    */
  87        u8 vclamp;      /* IBAT compensation voltage limit */
  88};
  89
  90struct bq25890_state {
  91        u8 online;
  92        u8 chrg_status;
  93        u8 chrg_fault;
  94        u8 vsys_status;
  95        u8 boost_fault;
  96        u8 bat_fault;
  97};
  98
  99struct bq25890_device {
 100        struct i2c_client *client;
 101        struct device *dev;
 102        struct power_supply *charger;
 103
 104        struct usb_phy *usb_phy;
 105        struct notifier_block usb_nb;
 106        struct work_struct usb_work;
 107        unsigned long usb_event;
 108
 109        struct regmap *rmap;
 110        struct regmap_field *rmap_fields[F_MAX_FIELDS];
 111
 112        enum bq25890_chip_version chip_version;
 113        struct bq25890_init_data init_data;
 114        struct bq25890_state state;
 115
 116        struct mutex lock; /* protect state data */
 117};
 118
 119static const struct regmap_range bq25890_readonly_reg_ranges[] = {
 120        regmap_reg_range(0x0b, 0x0c),
 121        regmap_reg_range(0x0e, 0x13),
 122};
 123
 124static const struct regmap_access_table bq25890_writeable_regs = {
 125        .no_ranges = bq25890_readonly_reg_ranges,
 126        .n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
 127};
 128
 129static const struct regmap_range bq25890_volatile_reg_ranges[] = {
 130        regmap_reg_range(0x00, 0x00),
 131        regmap_reg_range(0x02, 0x02),
 132        regmap_reg_range(0x09, 0x09),
 133        regmap_reg_range(0x0b, 0x14),
 134};
 135
 136static const struct regmap_access_table bq25890_volatile_regs = {
 137        .yes_ranges = bq25890_volatile_reg_ranges,
 138        .n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
 139};
 140
 141static const struct regmap_config bq25890_regmap_config = {
 142        .reg_bits = 8,
 143        .val_bits = 8,
 144
 145        .max_register = 0x14,
 146        .cache_type = REGCACHE_RBTREE,
 147
 148        .wr_table = &bq25890_writeable_regs,
 149        .volatile_table = &bq25890_volatile_regs,
 150};
 151
 152static const struct reg_field bq25890_reg_fields[] = {
 153        /* REG00 */
 154        [F_EN_HIZ]              = REG_FIELD(0x00, 7, 7),
 155        [F_EN_ILIM]             = REG_FIELD(0x00, 6, 6),
 156        [F_IILIM]               = REG_FIELD(0x00, 0, 5),
 157        /* REG01 */
 158        [F_BHOT]                = REG_FIELD(0x01, 6, 7),
 159        [F_BCOLD]               = REG_FIELD(0x01, 5, 5),
 160        [F_VINDPM_OFS]          = REG_FIELD(0x01, 0, 4),
 161        /* REG02 */
 162        [F_CONV_START]          = REG_FIELD(0x02, 7, 7),
 163        [F_CONV_RATE]           = REG_FIELD(0x02, 6, 6),
 164        [F_BOOSTF]              = REG_FIELD(0x02, 5, 5),
 165        [F_ICO_EN]              = REG_FIELD(0x02, 4, 4),
 166        [F_HVDCP_EN]            = REG_FIELD(0x02, 3, 3),  // reserved on BQ25896
 167        [F_MAXC_EN]             = REG_FIELD(0x02, 2, 2),  // reserved on BQ25896
 168        [F_FORCE_DPM]           = REG_FIELD(0x02, 1, 1),
 169        [F_AUTO_DPDM_EN]        = REG_FIELD(0x02, 0, 0),
 170        /* REG03 */
 171        [F_BAT_LOAD_EN]         = REG_FIELD(0x03, 7, 7),
 172        [F_WD_RST]              = REG_FIELD(0x03, 6, 6),
 173        [F_OTG_CFG]             = REG_FIELD(0x03, 5, 5),
 174        [F_CHG_CFG]             = REG_FIELD(0x03, 4, 4),
 175        [F_SYSVMIN]             = REG_FIELD(0x03, 1, 3),
 176        [F_MIN_VBAT_SEL]        = REG_FIELD(0x03, 0, 0), // BQ25896 only
 177        /* REG04 */
 178        [F_PUMPX_EN]            = REG_FIELD(0x04, 7, 7),
 179        [F_ICHG]                = REG_FIELD(0x04, 0, 6),
 180        /* REG05 */
 181        [F_IPRECHG]             = REG_FIELD(0x05, 4, 7),
 182        [F_ITERM]               = REG_FIELD(0x05, 0, 3),
 183        /* REG06 */
 184        [F_VREG]                = REG_FIELD(0x06, 2, 7),
 185        [F_BATLOWV]             = REG_FIELD(0x06, 1, 1),
 186        [F_VRECHG]              = REG_FIELD(0x06, 0, 0),
 187        /* REG07 */
 188        [F_TERM_EN]             = REG_FIELD(0x07, 7, 7),
 189        [F_STAT_DIS]            = REG_FIELD(0x07, 6, 6),
 190        [F_WD]                  = REG_FIELD(0x07, 4, 5),
 191        [F_TMR_EN]              = REG_FIELD(0x07, 3, 3),
 192        [F_CHG_TMR]             = REG_FIELD(0x07, 1, 2),
 193        [F_JEITA_ISET]          = REG_FIELD(0x07, 0, 0), // reserved on BQ25895
 194        /* REG08 */
 195        [F_BATCMP]              = REG_FIELD(0x08, 5, 7),
 196        [F_VCLAMP]              = REG_FIELD(0x08, 2, 4),
 197        [F_TREG]                = REG_FIELD(0x08, 0, 1),
 198        /* REG09 */
 199        [F_FORCE_ICO]           = REG_FIELD(0x09, 7, 7),
 200        [F_TMR2X_EN]            = REG_FIELD(0x09, 6, 6),
 201        [F_BATFET_DIS]          = REG_FIELD(0x09, 5, 5),
 202        [F_JEITA_VSET]          = REG_FIELD(0x09, 4, 4), // reserved on BQ25895
 203        [F_BATFET_DLY]          = REG_FIELD(0x09, 3, 3),
 204        [F_BATFET_RST_EN]       = REG_FIELD(0x09, 2, 2),
 205        [F_PUMPX_UP]            = REG_FIELD(0x09, 1, 1),
 206        [F_PUMPX_DN]            = REG_FIELD(0x09, 0, 0),
 207        /* REG0A */
 208        [F_BOOSTV]              = REG_FIELD(0x0A, 4, 7),
 209        [F_BOOSTI]              = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
 210        [F_PFM_OTG_DIS]         = REG_FIELD(0x0A, 3, 3), // BQ25896 only
 211        /* REG0B */
 212        [F_VBUS_STAT]           = REG_FIELD(0x0B, 5, 7),
 213        [F_CHG_STAT]            = REG_FIELD(0x0B, 3, 4),
 214        [F_PG_STAT]             = REG_FIELD(0x0B, 2, 2),
 215        [F_SDP_STAT]            = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
 216        [F_VSYS_STAT]           = REG_FIELD(0x0B, 0, 0),
 217        /* REG0C */
 218        [F_WD_FAULT]            = REG_FIELD(0x0C, 7, 7),
 219        [F_BOOST_FAULT]         = REG_FIELD(0x0C, 6, 6),
 220        [F_CHG_FAULT]           = REG_FIELD(0x0C, 4, 5),
 221        [F_BAT_FAULT]           = REG_FIELD(0x0C, 3, 3),
 222        [F_NTC_FAULT]           = REG_FIELD(0x0C, 0, 2),
 223        /* REG0D */
 224        [F_FORCE_VINDPM]        = REG_FIELD(0x0D, 7, 7),
 225        [F_VINDPM]              = REG_FIELD(0x0D, 0, 6),
 226        /* REG0E */
 227        [F_THERM_STAT]          = REG_FIELD(0x0E, 7, 7),
 228        [F_BATV]                = REG_FIELD(0x0E, 0, 6),
 229        /* REG0F */
 230        [F_SYSV]                = REG_FIELD(0x0F, 0, 6),
 231        /* REG10 */
 232        [F_TSPCT]               = REG_FIELD(0x10, 0, 6),
 233        /* REG11 */
 234        [F_VBUS_GD]             = REG_FIELD(0x11, 7, 7),
 235        [F_VBUSV]               = REG_FIELD(0x11, 0, 6),
 236        /* REG12 */
 237        [F_ICHGR]               = REG_FIELD(0x12, 0, 6),
 238        /* REG13 */
 239        [F_VDPM_STAT]           = REG_FIELD(0x13, 7, 7),
 240        [F_IDPM_STAT]           = REG_FIELD(0x13, 6, 6),
 241        [F_IDPM_LIM]            = REG_FIELD(0x13, 0, 5),
 242        /* REG14 */
 243        [F_REG_RST]             = REG_FIELD(0x14, 7, 7),
 244        [F_ICO_OPTIMIZED]       = REG_FIELD(0x14, 6, 6),
 245        [F_PN]                  = REG_FIELD(0x14, 3, 5),
 246        [F_TS_PROFILE]          = REG_FIELD(0x14, 2, 2),
 247        [F_DEV_REV]             = REG_FIELD(0x14, 0, 1)
 248};
 249
 250/*
 251 * Most of the val -> idx conversions can be computed, given the minimum,
 252 * maximum and the step between values. For the rest of conversions, we use
 253 * lookup tables.
 254 */
 255enum bq25890_table_ids {
 256        /* range tables */
 257        TBL_ICHG,
 258        TBL_ITERM,
 259        TBL_IILIM,
 260        TBL_VREG,
 261        TBL_BOOSTV,
 262        TBL_SYSVMIN,
 263        TBL_VBATCOMP,
 264        TBL_RBATCOMP,
 265
 266        /* lookup tables */
 267        TBL_TREG,
 268        TBL_BOOSTI,
 269};
 270
 271/* Thermal Regulation Threshold lookup table, in degrees Celsius */
 272static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
 273
 274#define BQ25890_TREG_TBL_SIZE           ARRAY_SIZE(bq25890_treg_tbl)
 275
 276/* Boost mode current limit lookup table, in uA */
 277static const u32 bq25890_boosti_tbl[] = {
 278        500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
 279};
 280
 281#define BQ25890_BOOSTI_TBL_SIZE         ARRAY_SIZE(bq25890_boosti_tbl)
 282
 283struct bq25890_range {
 284        u32 min;
 285        u32 max;
 286        u32 step;
 287};
 288
 289struct bq25890_lookup {
 290        const u32 *tbl;
 291        u32 size;
 292};
 293
 294static const union {
 295        struct bq25890_range  rt;
 296        struct bq25890_lookup lt;
 297} bq25890_tables[] = {
 298        /* range tables */
 299        /* TODO: BQ25896 has max ICHG 3008 mA */
 300        [TBL_ICHG] =    { .rt = {0,       5056000, 64000} },     /* uA */
 301        [TBL_ITERM] =   { .rt = {64000,   1024000, 64000} },     /* uA */
 302        [TBL_IILIM] =   { .rt = {100000,  3250000, 50000} },     /* uA */
 303        [TBL_VREG] =    { .rt = {3840000, 4608000, 16000} },     /* uV */
 304        [TBL_BOOSTV] =  { .rt = {4550000, 5510000, 64000} },     /* uV */
 305        [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} },    /* uV */
 306        [TBL_VBATCOMP] ={ .rt = {0,        224000, 32000} },     /* uV */
 307        [TBL_RBATCOMP] ={ .rt = {0,        140000, 20000} },     /* uOhm */
 308
 309        /* lookup tables */
 310        [TBL_TREG] =    { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
 311        [TBL_BOOSTI] =  { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} }
 312};
 313
 314static int bq25890_field_read(struct bq25890_device *bq,
 315                              enum bq25890_fields field_id)
 316{
 317        int ret;
 318        int val;
 319
 320        ret = regmap_field_read(bq->rmap_fields[field_id], &val);
 321        if (ret < 0)
 322                return ret;
 323
 324        return val;
 325}
 326
 327static int bq25890_field_write(struct bq25890_device *bq,
 328                               enum bq25890_fields field_id, u8 val)
 329{
 330        return regmap_field_write(bq->rmap_fields[field_id], val);
 331}
 332
 333static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
 334{
 335        u8 idx;
 336
 337        if (id >= TBL_TREG) {
 338                const u32 *tbl = bq25890_tables[id].lt.tbl;
 339                u32 tbl_size = bq25890_tables[id].lt.size;
 340
 341                for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
 342                        ;
 343        } else {
 344                const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
 345                u8 rtbl_size;
 346
 347                rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
 348
 349                for (idx = 1;
 350                     idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
 351                     idx++)
 352                        ;
 353        }
 354
 355        return idx - 1;
 356}
 357
 358static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
 359{
 360        const struct bq25890_range *rtbl;
 361
 362        /* lookup table? */
 363        if (id >= TBL_TREG)
 364                return bq25890_tables[id].lt.tbl[idx];
 365
 366        /* range table */
 367        rtbl = &bq25890_tables[id].rt;
 368
 369        return (rtbl->min + idx * rtbl->step);
 370}
 371
 372enum bq25890_status {
 373        STATUS_NOT_CHARGING,
 374        STATUS_PRE_CHARGING,
 375        STATUS_FAST_CHARGING,
 376        STATUS_TERMINATION_DONE,
 377};
 378
 379enum bq25890_chrg_fault {
 380        CHRG_FAULT_NORMAL,
 381        CHRG_FAULT_INPUT,
 382        CHRG_FAULT_THERMAL_SHUTDOWN,
 383        CHRG_FAULT_TIMER_EXPIRED,
 384};
 385
 386static bool bq25890_is_adc_property(enum power_supply_property psp)
 387{
 388        switch (psp) {
 389        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 390        case POWER_SUPPLY_PROP_CURRENT_NOW:
 391                return true;
 392
 393        default:
 394                return false;
 395        }
 396}
 397
 398static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq);
 399
 400static int bq25890_power_supply_get_property(struct power_supply *psy,
 401                                             enum power_supply_property psp,
 402                                             union power_supply_propval *val)
 403{
 404        struct bq25890_device *bq = power_supply_get_drvdata(psy);
 405        struct bq25890_state state;
 406        bool do_adc_conv;
 407        int ret;
 408
 409        mutex_lock(&bq->lock);
 410        /* update state in case we lost an interrupt */
 411        __bq25890_handle_irq(bq);
 412        state = bq->state;
 413        do_adc_conv = !state.online && bq25890_is_adc_property(psp);
 414        if (do_adc_conv)
 415                bq25890_field_write(bq, F_CONV_START, 1);
 416        mutex_unlock(&bq->lock);
 417
 418        if (do_adc_conv)
 419                regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START],
 420                        ret, !ret, 25000, 1000000);
 421
 422        switch (psp) {
 423        case POWER_SUPPLY_PROP_STATUS:
 424                if (!state.online)
 425                        val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
 426                else if (state.chrg_status == STATUS_NOT_CHARGING)
 427                        val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
 428                else if (state.chrg_status == STATUS_PRE_CHARGING ||
 429                         state.chrg_status == STATUS_FAST_CHARGING)
 430                        val->intval = POWER_SUPPLY_STATUS_CHARGING;
 431                else if (state.chrg_status == STATUS_TERMINATION_DONE)
 432                        val->intval = POWER_SUPPLY_STATUS_FULL;
 433                else
 434                        val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
 435
 436                break;
 437
 438        case POWER_SUPPLY_PROP_CHARGE_TYPE:
 439                if (!state.online || state.chrg_status == STATUS_NOT_CHARGING ||
 440                    state.chrg_status == STATUS_TERMINATION_DONE)
 441                        val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
 442                else if (state.chrg_status == STATUS_PRE_CHARGING)
 443                        val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
 444                else if (state.chrg_status == STATUS_FAST_CHARGING)
 445                        val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
 446                else /* unreachable */
 447                        val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
 448                break;
 449
 450        case POWER_SUPPLY_PROP_MANUFACTURER:
 451                val->strval = BQ25890_MANUFACTURER;
 452                break;
 453
 454        case POWER_SUPPLY_PROP_MODEL_NAME:
 455                val->strval = bq25890_chip_name[bq->chip_version];
 456                break;
 457
 458        case POWER_SUPPLY_PROP_ONLINE:
 459                val->intval = state.online;
 460                break;
 461
 462        case POWER_SUPPLY_PROP_HEALTH:
 463                if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
 464                        val->intval = POWER_SUPPLY_HEALTH_GOOD;
 465                else if (state.bat_fault)
 466                        val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
 467                else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
 468                        val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
 469                else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
 470                        val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
 471                else
 472                        val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
 473                break;
 474
 475        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
 476                val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
 477                break;
 478
 479        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
 480                if (!state.online) {
 481                        val->intval = 0;
 482                        break;
 483                }
 484
 485                ret = bq25890_field_read(bq, F_BATV); /* read measured value */
 486                if (ret < 0)
 487                        return ret;
 488
 489                /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
 490                val->intval = 2304000 + ret * 20000;
 491                break;
 492
 493        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
 494                val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
 495                break;
 496
 497        case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
 498                val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM);
 499                break;
 500
 501        case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
 502                val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
 503                break;
 504
 505        case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 506                ret = bq25890_field_read(bq, F_IILIM);
 507                if (ret < 0)
 508                        return ret;
 509
 510                val->intval = bq25890_find_val(ret, TBL_IILIM);
 511                break;
 512
 513        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 514                ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
 515                if (ret < 0)
 516                        return ret;
 517
 518                /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
 519                val->intval = 2304000 + ret * 20000;
 520                break;
 521
 522        case POWER_SUPPLY_PROP_CURRENT_NOW:
 523                ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
 524                if (ret < 0)
 525                        return ret;
 526
 527                /* converted_val = ADC_val * 50mA (table 10.3.19) */
 528                val->intval = ret * -50000;
 529                break;
 530
 531        default:
 532                return -EINVAL;
 533        }
 534
 535        return 0;
 536}
 537
 538static int bq25890_get_chip_state(struct bq25890_device *bq,
 539                                  struct bq25890_state *state)
 540{
 541        int i, ret;
 542
 543        struct {
 544                enum bq25890_fields id;
 545                u8 *data;
 546        } state_fields[] = {
 547                {F_CHG_STAT,    &state->chrg_status},
 548                {F_PG_STAT,     &state->online},
 549                {F_VSYS_STAT,   &state->vsys_status},
 550                {F_BOOST_FAULT, &state->boost_fault},
 551                {F_BAT_FAULT,   &state->bat_fault},
 552                {F_CHG_FAULT,   &state->chrg_fault}
 553        };
 554
 555        for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
 556                ret = bq25890_field_read(bq, state_fields[i].id);
 557                if (ret < 0)
 558                        return ret;
 559
 560                *state_fields[i].data = ret;
 561        }
 562
 563        dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
 564                state->chrg_status, state->online, state->vsys_status,
 565                state->chrg_fault, state->boost_fault, state->bat_fault);
 566
 567        return 0;
 568}
 569
 570static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)
 571{
 572        struct bq25890_state new_state;
 573        int ret;
 574
 575        ret = bq25890_get_chip_state(bq, &new_state);
 576        if (ret < 0)
 577                return IRQ_NONE;
 578
 579        if (!memcmp(&bq->state, &new_state, sizeof(new_state)))
 580                return IRQ_NONE;
 581
 582        if (!new_state.online && bq->state.online) {        /* power removed */
 583                /* disable ADC */
 584                ret = bq25890_field_write(bq, F_CONV_START, 0);
 585                if (ret < 0)
 586                        goto error;
 587        } else if (new_state.online && !bq->state.online) { /* power inserted */
 588                /* enable ADC, to have control of charge current/voltage */
 589                ret = bq25890_field_write(bq, F_CONV_START, 1);
 590                if (ret < 0)
 591                        goto error;
 592        }
 593
 594        bq->state = new_state;
 595        power_supply_changed(bq->charger);
 596
 597        return IRQ_HANDLED;
 598error:
 599        dev_err(bq->dev, "Error communicating with the chip: %pe\n",
 600                ERR_PTR(ret));
 601        return IRQ_HANDLED;
 602}
 603
 604static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
 605{
 606        struct bq25890_device *bq = private;
 607        irqreturn_t ret;
 608
 609        mutex_lock(&bq->lock);
 610        ret = __bq25890_handle_irq(bq);
 611        mutex_unlock(&bq->lock);
 612
 613        return ret;
 614}
 615
 616static int bq25890_chip_reset(struct bq25890_device *bq)
 617{
 618        int ret;
 619        int rst_check_counter = 10;
 620
 621        ret = bq25890_field_write(bq, F_REG_RST, 1);
 622        if (ret < 0)
 623                return ret;
 624
 625        do {
 626                ret = bq25890_field_read(bq, F_REG_RST);
 627                if (ret < 0)
 628                        return ret;
 629
 630                usleep_range(5, 10);
 631        } while (ret == 1 && --rst_check_counter);
 632
 633        if (!rst_check_counter)
 634                return -ETIMEDOUT;
 635
 636        return 0;
 637}
 638
 639static int bq25890_hw_init(struct bq25890_device *bq)
 640{
 641        int ret;
 642        int i;
 643
 644        const struct {
 645                enum bq25890_fields id;
 646                u32 value;
 647        } init_data[] = {
 648                {F_ICHG,         bq->init_data.ichg},
 649                {F_VREG,         bq->init_data.vreg},
 650                {F_ITERM,        bq->init_data.iterm},
 651                {F_IPRECHG,      bq->init_data.iprechg},
 652                {F_SYSVMIN,      bq->init_data.sysvmin},
 653                {F_BOOSTV,       bq->init_data.boostv},
 654                {F_BOOSTI,       bq->init_data.boosti},
 655                {F_BOOSTF,       bq->init_data.boostf},
 656                {F_EN_ILIM,      bq->init_data.ilim_en},
 657                {F_TREG,         bq->init_data.treg},
 658                {F_BATCMP,       bq->init_data.rbatcomp},
 659                {F_VCLAMP,       bq->init_data.vclamp},
 660        };
 661
 662        ret = bq25890_chip_reset(bq);
 663        if (ret < 0) {
 664                dev_dbg(bq->dev, "Reset failed %d\n", ret);
 665                return ret;
 666        }
 667
 668        /* disable watchdog */
 669        ret = bq25890_field_write(bq, F_WD, 0);
 670        if (ret < 0) {
 671                dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
 672                return ret;
 673        }
 674
 675        /* initialize currents/voltages and other parameters */
 676        for (i = 0; i < ARRAY_SIZE(init_data); i++) {
 677                ret = bq25890_field_write(bq, init_data[i].id,
 678                                          init_data[i].value);
 679                if (ret < 0) {
 680                        dev_dbg(bq->dev, "Writing init data failed %d\n", ret);
 681                        return ret;
 682                }
 683        }
 684
 685        /* Configure ADC for continuous conversions when charging */
 686        ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online);
 687        if (ret < 0) {
 688                dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
 689                return ret;
 690        }
 691
 692        ret = bq25890_get_chip_state(bq, &bq->state);
 693        if (ret < 0) {
 694                dev_dbg(bq->dev, "Get state failed %d\n", ret);
 695                return ret;
 696        }
 697
 698        return 0;
 699}
 700
 701static const enum power_supply_property bq25890_power_supply_props[] = {
 702        POWER_SUPPLY_PROP_MANUFACTURER,
 703        POWER_SUPPLY_PROP_MODEL_NAME,
 704        POWER_SUPPLY_PROP_STATUS,
 705        POWER_SUPPLY_PROP_CHARGE_TYPE,
 706        POWER_SUPPLY_PROP_ONLINE,
 707        POWER_SUPPLY_PROP_HEALTH,
 708        POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
 709        POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
 710        POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
 711        POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
 712        POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
 713        POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
 714        POWER_SUPPLY_PROP_VOLTAGE_NOW,
 715        POWER_SUPPLY_PROP_CURRENT_NOW,
 716};
 717
 718static char *bq25890_charger_supplied_to[] = {
 719        "main-battery",
 720};
 721
 722static const struct power_supply_desc bq25890_power_supply_desc = {
 723        .name = "bq25890-charger",
 724        .type = POWER_SUPPLY_TYPE_USB,
 725        .properties = bq25890_power_supply_props,
 726        .num_properties = ARRAY_SIZE(bq25890_power_supply_props),
 727        .get_property = bq25890_power_supply_get_property,
 728};
 729
 730static int bq25890_power_supply_init(struct bq25890_device *bq)
 731{
 732        struct power_supply_config psy_cfg = { .drv_data = bq, };
 733
 734        psy_cfg.supplied_to = bq25890_charger_supplied_to;
 735        psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
 736
 737        bq->charger = power_supply_register(bq->dev, &bq25890_power_supply_desc,
 738                                            &psy_cfg);
 739
 740        return PTR_ERR_OR_ZERO(bq->charger);
 741}
 742
 743static void bq25890_usb_work(struct work_struct *data)
 744{
 745        int ret;
 746        struct bq25890_device *bq =
 747                        container_of(data, struct bq25890_device, usb_work);
 748
 749        switch (bq->usb_event) {
 750        case USB_EVENT_ID:
 751                /* Enable boost mode */
 752                ret = bq25890_field_write(bq, F_OTG_CFG, 1);
 753                if (ret < 0)
 754                        goto error;
 755                break;
 756
 757        case USB_EVENT_NONE:
 758                /* Disable boost mode */
 759                ret = bq25890_field_write(bq, F_OTG_CFG, 0);
 760                if (ret < 0)
 761                        goto error;
 762
 763                power_supply_changed(bq->charger);
 764                break;
 765        }
 766
 767        return;
 768
 769error:
 770        dev_err(bq->dev, "Error switching to boost/charger mode.\n");
 771}
 772
 773static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
 774                                void *priv)
 775{
 776        struct bq25890_device *bq =
 777                        container_of(nb, struct bq25890_device, usb_nb);
 778
 779        bq->usb_event = val;
 780        queue_work(system_power_efficient_wq, &bq->usb_work);
 781
 782        return NOTIFY_OK;
 783}
 784
 785static int bq25890_get_chip_version(struct bq25890_device *bq)
 786{
 787        int id, rev;
 788
 789        id = bq25890_field_read(bq, F_PN);
 790        if (id < 0) {
 791                dev_err(bq->dev, "Cannot read chip ID.\n");
 792                return id;
 793        }
 794
 795        rev = bq25890_field_read(bq, F_DEV_REV);
 796        if (rev < 0) {
 797                dev_err(bq->dev, "Cannot read chip revision.\n");
 798                return rev;
 799        }
 800
 801        switch (id) {
 802        case BQ25890_ID:
 803                bq->chip_version = BQ25890;
 804                break;
 805
 806        /* BQ25892 and BQ25896 share same ID 0 */
 807        case BQ25896_ID:
 808                switch (rev) {
 809                case 2:
 810                        bq->chip_version = BQ25896;
 811                        break;
 812                case 1:
 813                        bq->chip_version = BQ25892;
 814                        break;
 815                default:
 816                        dev_err(bq->dev,
 817                                "Unknown device revision %d, assume BQ25892\n",
 818                                rev);
 819                        bq->chip_version = BQ25892;
 820                }
 821                break;
 822
 823        case BQ25895_ID:
 824                bq->chip_version = BQ25895;
 825                break;
 826
 827        default:
 828                dev_err(bq->dev, "Unknown chip ID %d\n", id);
 829                return -ENODEV;
 830        }
 831
 832        return 0;
 833}
 834
 835static int bq25890_irq_probe(struct bq25890_device *bq)
 836{
 837        struct gpio_desc *irq;
 838
 839        irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
 840        if (IS_ERR(irq)) {
 841                dev_err(bq->dev, "Could not probe irq pin.\n");
 842                return PTR_ERR(irq);
 843        }
 844
 845        return gpiod_to_irq(irq);
 846}
 847
 848static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
 849{
 850        int ret;
 851        u32 property;
 852        int i;
 853        struct bq25890_init_data *init = &bq->init_data;
 854        struct {
 855                char *name;
 856                bool optional;
 857                enum bq25890_table_ids tbl_id;
 858                u8 *conv_data; /* holds converted value from given property */
 859        } props[] = {
 860                /* required properties */
 861                {"ti,charge-current", false, TBL_ICHG, &init->ichg},
 862                {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
 863                {"ti,termination-current", false, TBL_ITERM, &init->iterm},
 864                {"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
 865                {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
 866                {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
 867                {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
 868
 869                /* optional properties */
 870                {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
 871                {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
 872                {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
 873        };
 874
 875        /* initialize data for optional properties */
 876        init->treg = 3; /* 120 degrees Celsius */
 877        init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */
 878
 879        for (i = 0; i < ARRAY_SIZE(props); i++) {
 880                ret = device_property_read_u32(bq->dev, props[i].name,
 881                                               &property);
 882                if (ret < 0) {
 883                        if (props[i].optional)
 884                                continue;
 885
 886                        dev_err(bq->dev, "Unable to read property %d %s\n", ret,
 887                                props[i].name);
 888
 889                        return ret;
 890                }
 891
 892                *props[i].conv_data = bq25890_find_idx(property,
 893                                                       props[i].tbl_id);
 894        }
 895
 896        return 0;
 897}
 898
 899static int bq25890_fw_probe(struct bq25890_device *bq)
 900{
 901        int ret;
 902        struct bq25890_init_data *init = &bq->init_data;
 903
 904        ret = bq25890_fw_read_u32_props(bq);
 905        if (ret < 0)
 906                return ret;
 907
 908        init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
 909        init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
 910
 911        return 0;
 912}
 913
 914static int bq25890_probe(struct i2c_client *client,
 915                         const struct i2c_device_id *id)
 916{
 917        struct device *dev = &client->dev;
 918        struct bq25890_device *bq;
 919        int ret;
 920        int i;
 921
 922        bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
 923        if (!bq)
 924                return -ENOMEM;
 925
 926        bq->client = client;
 927        bq->dev = dev;
 928
 929        mutex_init(&bq->lock);
 930
 931        bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
 932        if (IS_ERR(bq->rmap)) {
 933                dev_err(dev, "failed to allocate register map\n");
 934                return PTR_ERR(bq->rmap);
 935        }
 936
 937        for (i = 0; i < ARRAY_SIZE(bq25890_reg_fields); i++) {
 938                const struct reg_field *reg_fields = bq25890_reg_fields;
 939
 940                bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
 941                                                             reg_fields[i]);
 942                if (IS_ERR(bq->rmap_fields[i])) {
 943                        dev_err(dev, "cannot allocate regmap field\n");
 944                        return PTR_ERR(bq->rmap_fields[i]);
 945                }
 946        }
 947
 948        i2c_set_clientdata(client, bq);
 949
 950        ret = bq25890_get_chip_version(bq);
 951        if (ret) {
 952                dev_err(dev, "Cannot read chip ID or unknown chip.\n");
 953                return ret;
 954        }
 955
 956        if (!dev->platform_data) {
 957                ret = bq25890_fw_probe(bq);
 958                if (ret < 0) {
 959                        dev_err(dev, "Cannot read device properties.\n");
 960                        return ret;
 961                }
 962        } else {
 963                return -ENODEV;
 964        }
 965
 966        ret = bq25890_hw_init(bq);
 967        if (ret < 0) {
 968                dev_err(dev, "Cannot initialize the chip.\n");
 969                return ret;
 970        }
 971
 972        if (client->irq <= 0)
 973                client->irq = bq25890_irq_probe(bq);
 974
 975        if (client->irq < 0) {
 976                dev_err(dev, "No irq resource found.\n");
 977                return client->irq;
 978        }
 979
 980        /* OTG reporting */
 981        bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
 982        if (!IS_ERR_OR_NULL(bq->usb_phy)) {
 983                INIT_WORK(&bq->usb_work, bq25890_usb_work);
 984                bq->usb_nb.notifier_call = bq25890_usb_notifier;
 985                usb_register_notifier(bq->usb_phy, &bq->usb_nb);
 986        }
 987
 988        ret = devm_request_threaded_irq(dev, client->irq, NULL,
 989                                        bq25890_irq_handler_thread,
 990                                        IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
 991                                        BQ25890_IRQ_PIN, bq);
 992        if (ret)
 993                goto irq_fail;
 994
 995        ret = bq25890_power_supply_init(bq);
 996        if (ret < 0) {
 997                dev_err(dev, "Failed to register power supply\n");
 998                goto irq_fail;
 999        }
1000
1001        return 0;
1002
1003irq_fail:
1004        if (!IS_ERR_OR_NULL(bq->usb_phy))
1005                usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1006
1007        return ret;
1008}
1009
1010static int bq25890_remove(struct i2c_client *client)
1011{
1012        struct bq25890_device *bq = i2c_get_clientdata(client);
1013
1014        power_supply_unregister(bq->charger);
1015
1016        if (!IS_ERR_OR_NULL(bq->usb_phy))
1017                usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1018
1019        /* reset all registers to default values */
1020        bq25890_chip_reset(bq);
1021
1022        return 0;
1023}
1024
1025#ifdef CONFIG_PM_SLEEP
1026static int bq25890_suspend(struct device *dev)
1027{
1028        struct bq25890_device *bq = dev_get_drvdata(dev);
1029
1030        /*
1031         * If charger is removed, while in suspend, make sure ADC is diabled
1032         * since it consumes slightly more power.
1033         */
1034        return bq25890_field_write(bq, F_CONV_RATE, 0);
1035}
1036
1037static int bq25890_resume(struct device *dev)
1038{
1039        int ret;
1040        struct bq25890_device *bq = dev_get_drvdata(dev);
1041
1042        mutex_lock(&bq->lock);
1043
1044        ret = bq25890_get_chip_state(bq, &bq->state);
1045        if (ret < 0)
1046                goto unlock;
1047
1048        /* Re-enable ADC only if charger is plugged in. */
1049        if (bq->state.online) {
1050                ret = bq25890_field_write(bq, F_CONV_RATE, 1);
1051                if (ret < 0)
1052                        goto unlock;
1053        }
1054
1055        /* signal userspace, maybe state changed while suspended */
1056        power_supply_changed(bq->charger);
1057
1058unlock:
1059        mutex_unlock(&bq->lock);
1060
1061        return ret;
1062}
1063#endif
1064
1065static const struct dev_pm_ops bq25890_pm = {
1066        SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
1067};
1068
1069static const struct i2c_device_id bq25890_i2c_ids[] = {
1070        { "bq25890", 0 },
1071        { "bq25892", 0 },
1072        { "bq25895", 0 },
1073        { "bq25896", 0 },
1074        {},
1075};
1076MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
1077
1078static const struct of_device_id bq25890_of_match[] = {
1079        { .compatible = "ti,bq25890", },
1080        { .compatible = "ti,bq25892", },
1081        { .compatible = "ti,bq25895", },
1082        { .compatible = "ti,bq25896", },
1083        { },
1084};
1085MODULE_DEVICE_TABLE(of, bq25890_of_match);
1086
1087#ifdef CONFIG_ACPI
1088static const struct acpi_device_id bq25890_acpi_match[] = {
1089        {"BQ258900", 0},
1090        {},
1091};
1092MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
1093#endif
1094
1095static struct i2c_driver bq25890_driver = {
1096        .driver = {
1097                .name = "bq25890-charger",
1098                .of_match_table = of_match_ptr(bq25890_of_match),
1099                .acpi_match_table = ACPI_PTR(bq25890_acpi_match),
1100                .pm = &bq25890_pm,
1101        },
1102        .probe = bq25890_probe,
1103        .remove = bq25890_remove,
1104        .id_table = bq25890_i2c_ids,
1105};
1106module_i2c_driver(bq25890_driver);
1107
1108MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1109MODULE_DESCRIPTION("bq25890 charger driver");
1110MODULE_LICENSE("GPL");
1111