linux/drivers/power/supply/bq2515x_charger.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2// BQ2515X Battery Charger Driver
   3// Copyright (C) 2020 Texas Instruments Incorporated - https://www.ti.com/
   4
   5#include <linux/err.h>
   6#include <linux/i2c.h>
   7#include <linux/init.h>
   8#include <linux/kernel.h>
   9#include <linux/module.h>
  10#include <linux/gpio/consumer.h>
  11#include <linux/power_supply.h>
  12#include <linux/regmap.h>
  13#include <linux/types.h>
  14
  15#define BQ2515X_MANUFACTURER "Texas Instruments"
  16
  17#define BQ2515X_STAT0           0x00
  18#define BQ2515X_STAT1           0x01
  19#define BQ2515X_STAT2           0x02
  20#define BQ2515X_FLAG0           0x03
  21#define BQ2515X_FLAG1           0x04
  22#define BQ2515X_FLAG2           0x05
  23#define BQ2515X_FLAG3           0x06
  24#define BQ2515X_MASK0           0x07
  25#define BQ2515X_MASK1           0x08
  26#define BQ2515X_MASK2           0x09
  27#define BQ2515X_MASK3           0x0a
  28#define BQ2515X_VBAT_CTRL       0x12
  29#define BQ2515X_ICHG_CTRL       0x13
  30#define BQ2515X_PCHRGCTRL       0x14
  31#define BQ2515X_TERMCTRL        0x15
  32#define BQ2515X_BUVLO           0x16
  33#define BQ2515X_CHARGERCTRL0    0x17
  34#define BQ2515X_CHARGERCTRL1    0x18
  35#define BQ2515X_ILIMCTRL        0x19
  36#define BQ2515X_LDOCTRL         0x1d
  37#define BQ2515X_MRCTRL          0x30
  38#define BQ2515X_ICCTRL0         0x35
  39#define BQ2515X_ICCTRL1         0x36
  40#define BQ2515X_ICCTRL2         0x37
  41#define BQ2515X_ADCCTRL0        0x40
  42#define BQ2515X_ADCCTRL1        0x41
  43#define BQ2515X_ADC_VBAT_M      0x42
  44#define BQ2515X_ADC_VBAT_L      0x43
  45#define BQ2515X_ADC_TS_M        0x44
  46#define BQ2515X_ADC_TS_L        0x45
  47#define BQ2515X_ADC_ICHG_M      0x46
  48#define BQ2515X_ADC_ICHG_L      0x47
  49#define BQ2515X_ADC_ADCIN_M     0x48
  50#define BQ2515X_ADC_ADCIN_L     0x49
  51#define BQ2515X_ADC_VIN_M       0x4a
  52#define BQ2515X_ADC_VIN_L       0x4b
  53#define BQ2515X_ADC_PMID_M      0x4c
  54#define BQ2515X_ADC_PMID_L      0x4d
  55#define BQ2515X_ADC_IIN_M       0x4e
  56#define BQ2515X_ADC_IIN_L       0x4f
  57#define BQ2515X_ADC_COMP1_M     0x52
  58#define BQ2515X_ADC_COMP1_L     0X53
  59#define BQ2515X_ADC_COMP2_M     0X54
  60#define BQ2515X_ADC_COMP2_L     0x55
  61#define BQ2515X_ADC_COMP3_M     0x56
  62#define BQ2515X_ADC_COMP3_L     0x57
  63#define BQ2515X_ADC_READ_EN     0x58
  64#define BQ2515X_TS_FASTCHGCTRL  0x61
  65#define BQ2515X_TS_COLD         0x62
  66#define BQ2515X_TS_COOL         0x63
  67#define BQ2515X_TS_WARM         0x64
  68#define BQ2515X_TS_HOT          0x65
  69#define BQ2515X_DEVICE_ID       0x6f
  70
  71#define BQ2515X_DEFAULT_ICHG_UA         10000
  72#define BQ25150_DEFAULT_ILIM_UA         100000
  73#define BQ25155_DEFAULT_ILIM_UA         500000
  74#define BQ2515X_DEFAULT_VBAT_REG_UV     4200000
  75#define BQ2515X_DEFAULT_IPRECHARGE_UA   2500
  76
  77#define BQ2515X_DIVISOR                         65536
  78#define BQ2515X_VBAT_BASE_VOLT                  3600000
  79#define BQ2515X_VBAT_REG_MAX                    4600000
  80#define BQ2515X_VBAT_REG_MIN                    3600000
  81#define BQ2515X_VBAT_STEP_UV                    10000
  82#define BQ2515X_UV_FACTOR                       1000000
  83#define BQ2515X_VBAT_MULTIPLIER                 6
  84#define BQ2515X_ICHG_DIVISOR                    52429
  85#define BQ2515X_ICHG_CURR_STEP_THRESH_UA        318750
  86#define BQ2515X_ICHG_MIN_UA                     0
  87#define BQ2515X_ICHG_MAX_UA                     500000
  88#define BQ2515X_ICHG_RNG_1B0_UA                 1250
  89#define BQ2515X_ICHG_RNG_1B1_UA                 2500
  90#define BQ2515X_VLOWV_SEL_1B0_UV                3000000
  91#define BQ2515X_VLOWV_SEL_1B1_UV                2800000
  92#define BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA      18750
  93#define BQ2515X_PRECHRG_ICHRG_RNGE_3750_UA      37500
  94#define BQ2515X_TWAKE2_MIN_US                   1700000
  95#define BQ2515X_TWAKE2_MAX_US                   2300000
  96
  97#define BQ2515X_ILIM_150MA      0x2
  98#define BQ2515X_ILIM_MASK       0x7
  99#define BQ2515X_ILIM_MIN        50000
 100#define BQ2515X_ILIM_MAX        600000
 101#define BQ2515X_HEALTH_MASK     0xf
 102#define BQ2515X_ICHGRNG_MASK    0x80
 103#define BQ2515X_STAT0_MASK      0x0f
 104#define BQ2515X_STAT1_MASK      0x1f
 105#define BQ2515X_PRECHARGE_MASK  0x1f
 106
 107#define BQ2515X_TS_HOT_STAT             BIT(0)
 108#define BQ2515X_TS_WARM_STAT            BIT(1)
 109#define BQ2515X_TS_COOL_STAT            BIT(2)
 110#define BQ2515X_TS_COLD_STAT            BIT(3)
 111#define BQ2515X_SAFETY_TIMER_EXP        BIT(5)
 112
 113#define BQ2515X_EN_VBAT_READ            BIT(3)
 114#define BQ2515X_EN_ICHG_READ            BIT(5)
 115
 116#define BQ2515X_VIN_GOOD                BIT(0)
 117#define BQ2515X_CHRG_DONE               BIT(5)
 118#define BQ2515X_CV_CHRG_MODE            BIT(6)
 119
 120#define BQ2515X_VIN_OVP_FAULT_STAT      BIT(7)
 121
 122#define BQ2515X_WATCHDOG_DISABLE        BIT(4)
 123
 124#define BQ2515X_ICHARGE_RANGE           BIT(7)
 125
 126#define BQ2515X_VLOWV_SEL               BIT(5)
 127
 128#define BQ2515X_CHARGER_DISABLE         BIT(0)
 129
 130#define BQ2515X_HWRESET_14S_WD          BIT(1)
 131
 132static const int bq2515x_ilim_lvl_values[] = {
 133        50000, 100000, 150000, 200000, 300000, 400000, 500000, 600000
 134};
 135
 136/**
 137 * struct bq2515x_init_data -
 138 * @ilim: input current limit
 139 * @ichg: fast charge current
 140 * @vbatreg: battery regulation voltage
 141 * @iprechg: precharge current
 142 */
 143struct bq2515x_init_data {
 144        int ilim;
 145        int ichg;
 146        int vbatreg;
 147        int iprechg;
 148};
 149
 150enum bq2515x_id {
 151        BQ25150,
 152        BQ25155,
 153};
 154
 155/**
 156 * struct bq2515x_device -
 157 * @mains: mains properties
 158 * @battery: battery properties
 159 * @regmap: register map structure
 160 * @dev: device structure
 161 *
 162 * @reset_gpio: manual reset (MR) pin
 163 * @powerdown_gpio: low power mode pin
 164 * @ac_detect_gpio: power good (PG) pin
 165 * @ce_gpio: charge enable (CE) pin
 166 *
 167 * @model_name: string value describing device model
 168 * @device_id: value of device_id
 169 * @mains_online: boolean value indicating power supply online
 170 *
 171 * @init_data: charger initialization data structure
 172 */
 173struct bq2515x_device {
 174        struct power_supply *mains;
 175        struct power_supply *battery;
 176        struct regmap *regmap;
 177        struct device *dev;
 178
 179        struct gpio_desc *reset_gpio;
 180        struct gpio_desc *powerdown_gpio;
 181        struct gpio_desc *ac_detect_gpio;
 182        struct gpio_desc *ce_gpio;
 183
 184        char model_name[I2C_NAME_SIZE];
 185        int device_id;
 186        bool mains_online;
 187
 188        struct bq2515x_init_data init_data;
 189};
 190
 191static const struct reg_default bq25150_reg_defaults[] = {
 192        {BQ2515X_FLAG0, 0x0},
 193        {BQ2515X_FLAG1, 0x0},
 194        {BQ2515X_FLAG2, 0x0},
 195        {BQ2515X_FLAG3, 0x0},
 196        {BQ2515X_MASK0, 0x0},
 197        {BQ2515X_MASK1, 0x0},
 198        {BQ2515X_MASK2, 0x71},
 199        {BQ2515X_MASK3, 0x0},
 200        {BQ2515X_VBAT_CTRL, 0x3C},
 201        {BQ2515X_ICHG_CTRL, 0x8},
 202        {BQ2515X_PCHRGCTRL, 0x2},
 203        {BQ2515X_TERMCTRL, 0x14},
 204        {BQ2515X_BUVLO, 0x0},
 205        {BQ2515X_CHARGERCTRL0, 0x82},
 206        {BQ2515X_CHARGERCTRL1, 0x42},
 207        {BQ2515X_ILIMCTRL, 0x1},
 208        {BQ2515X_LDOCTRL, 0xB0},
 209        {BQ2515X_MRCTRL, 0x2A},
 210        {BQ2515X_ICCTRL0, 0x10},
 211        {BQ2515X_ICCTRL1, 0x0},
 212        {BQ2515X_ICCTRL2, 0x0},
 213        {BQ2515X_ADCCTRL0, 0x2},
 214        {BQ2515X_ADCCTRL1, 0x40},
 215        {BQ2515X_ADC_COMP1_M, 0x23},
 216        {BQ2515X_ADC_COMP1_L, 0x20},
 217        {BQ2515X_ADC_COMP2_M, 0x38},
 218        {BQ2515X_ADC_COMP2_L, 0x90},
 219        {BQ2515X_ADC_COMP3_M, 0x0},
 220        {BQ2515X_ADC_COMP3_L, 0x0},
 221        {BQ2515X_ADC_READ_EN, 0x0},
 222        {BQ2515X_TS_FASTCHGCTRL, 0x34},
 223        {BQ2515X_TS_COLD, 0x7C},
 224        {BQ2515X_TS_COOL, 0x6D},
 225        {BQ2515X_TS_WARM, 0x38},
 226        {BQ2515X_TS_HOT, 0x27},
 227        {BQ2515X_DEVICE_ID, 0x20},
 228};
 229
 230static const struct reg_default bq25155_reg_defaults[] = {
 231        {BQ2515X_FLAG0, 0x0},
 232        {BQ2515X_FLAG1, 0x0},
 233        {BQ2515X_FLAG2, 0x0},
 234        {BQ2515X_FLAG3, 0x0},
 235        {BQ2515X_MASK0, 0x0},
 236        {BQ2515X_MASK1, 0x0},
 237        {BQ2515X_MASK2, 0x71},
 238        {BQ2515X_MASK3, 0x0},
 239        {BQ2515X_VBAT_CTRL, 0x3C},
 240        {BQ2515X_ICHG_CTRL, 0x8},
 241        {BQ2515X_PCHRGCTRL, 0x2},
 242        {BQ2515X_TERMCTRL, 0x14},
 243        {BQ2515X_BUVLO, 0x0},
 244        {BQ2515X_CHARGERCTRL0, 0x82},
 245        {BQ2515X_CHARGERCTRL1, 0xC2},
 246        {BQ2515X_ILIMCTRL, 0x6},
 247        {BQ2515X_LDOCTRL, 0xB0},
 248        {BQ2515X_MRCTRL, 0x2A},
 249        {BQ2515X_ICCTRL0, 0x10},
 250        {BQ2515X_ICCTRL1, 0x0},
 251        {BQ2515X_ICCTRL2, 0x40},
 252        {BQ2515X_ADCCTRL0, 0x2},
 253        {BQ2515X_ADCCTRL1, 0x40},
 254        {BQ2515X_ADC_COMP1_M, 0x23},
 255        {BQ2515X_ADC_COMP1_L, 0x20},
 256        {BQ2515X_ADC_COMP2_M, 0x38},
 257        {BQ2515X_ADC_COMP2_L, 0x90},
 258        {BQ2515X_ADC_COMP3_M, 0x0},
 259        {BQ2515X_ADC_COMP3_L, 0x0},
 260        {BQ2515X_ADC_READ_EN, 0x0},
 261        {BQ2515X_TS_FASTCHGCTRL, 0x34},
 262        {BQ2515X_TS_COLD, 0x7C},
 263        {BQ2515X_TS_COOL, 0x6D},
 264        {BQ2515X_TS_WARM, 0x38},
 265        {BQ2515X_TS_HOT, 0x27},
 266        {BQ2515X_DEVICE_ID, 0x35},
 267};
 268
 269static int bq2515x_wake_up(struct bq2515x_device *bq2515x)
 270{
 271        int ret;
 272        int val;
 273
 274        /* Read the STAT register if we can read it then the device is out
 275         * of ship mode.  If the register cannot be read then attempt to wake
 276         * it up and enable the ADC.
 277         */
 278        ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &val);
 279        if (ret)
 280                return ret;
 281
 282        /* Need to toggle LP and bring device out of ship mode. The device
 283         * will exit the ship mode when the MR pin is held low for at least
 284         * t_WAKE2 as shown in section 8.3.7.1 of the datasheet.
 285         */
 286        gpiod_set_value_cansleep(bq2515x->powerdown_gpio, 0);
 287
 288        gpiod_set_value_cansleep(bq2515x->reset_gpio, 0);
 289        usleep_range(BQ2515X_TWAKE2_MIN_US, BQ2515X_TWAKE2_MAX_US);
 290        gpiod_set_value_cansleep(bq2515x->reset_gpio, 1);
 291
 292        return regmap_write(bq2515x->regmap, BQ2515X_ADC_READ_EN,
 293                                (BQ2515X_EN_VBAT_READ | BQ2515X_EN_ICHG_READ));
 294}
 295
 296static int bq2515x_update_ps_status(struct bq2515x_device *bq2515x)
 297{
 298        bool dc = false;
 299        unsigned int val;
 300        int ret;
 301
 302        if (bq2515x->ac_detect_gpio)
 303                val = gpiod_get_value_cansleep(bq2515x->ac_detect_gpio);
 304        else {
 305                ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &val);
 306                if (ret)
 307                        return ret;
 308        }
 309
 310        dc = val & BQ2515X_VIN_GOOD;
 311
 312        ret = bq2515x->mains_online != dc;
 313
 314        bq2515x->mains_online = dc;
 315
 316        return ret;
 317}
 318
 319static int bq2515x_disable_watchdog_timers(struct bq2515x_device *bq2515x)
 320{
 321        int ret;
 322
 323        ret = regmap_update_bits(bq2515x->regmap, BQ2515X_CHARGERCTRL0,
 324                        BQ2515X_WATCHDOG_DISABLE, BQ2515X_WATCHDOG_DISABLE);
 325        if (ret)
 326                return ret;
 327
 328        return regmap_update_bits(bq2515x->regmap, BQ2515X_ICCTRL2,
 329                                                BQ2515X_HWRESET_14S_WD, 0);
 330}
 331
 332static int bq2515x_get_battery_voltage_now(struct bq2515x_device *bq2515x)
 333{
 334        int ret;
 335        int vbat_msb;
 336        int vbat_lsb;
 337        uint32_t vbat_measurement;
 338
 339        if (!bq2515x->mains_online)
 340                bq2515x_wake_up(bq2515x);
 341
 342        ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_VBAT_M, &vbat_msb);
 343        if (ret)
 344                return ret;
 345
 346        ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_VBAT_L, &vbat_lsb);
 347        if (ret)
 348                return ret;
 349
 350        vbat_measurement = (vbat_msb << 8) | vbat_lsb;
 351
 352        return vbat_measurement * (BQ2515X_UV_FACTOR / BQ2515X_DIVISOR) *
 353                                                BQ2515X_VBAT_MULTIPLIER;
 354}
 355
 356static int bq2515x_get_battery_current_now(struct bq2515x_device *bq2515x)
 357{
 358        int ret;
 359        int ichg_msb;
 360        int ichg_lsb;
 361        uint32_t ichg_measurement;
 362        u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
 363        unsigned int ichg_reg_code, reg_code;
 364        unsigned int icharge_range = 0, pchrgctrl;
 365        unsigned int buvlo, vlowv_sel, vlowv = BQ2515X_VLOWV_SEL_1B0_UV;
 366
 367        if (!bq2515x->mains_online)
 368                return -ENODATA;
 369
 370        ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_ICHG_M, &ichg_msb);
 371        if (ret)
 372                return ret;
 373
 374        ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_ICHG_L, &ichg_lsb);
 375        if (ret)
 376                return ret;
 377
 378        ichg_measurement = (ichg_msb << 8) | ichg_lsb;
 379
 380        ret = regmap_read(bq2515x->regmap, BQ2515X_BUVLO, &buvlo);
 381        if (ret)
 382                return ret;
 383
 384        vlowv_sel = buvlo & BQ2515X_VLOWV_SEL;
 385
 386        if (vlowv_sel)
 387                vlowv = BQ2515X_VLOWV_SEL_1B1_UV;
 388
 389        if (bq2515x_get_battery_voltage_now(bq2515x) < vlowv) {
 390                ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL,
 391                                                                &pchrgctrl);
 392                if (ret)
 393                        return ret;
 394
 395                reg_code = pchrgctrl & BQ2515X_PRECHARGE_MASK;
 396        } else {
 397                ret = regmap_read(bq2515x->regmap, BQ2515X_ICHG_CTRL,
 398                                                        &ichg_reg_code);
 399                if (ret)
 400                        return ret;
 401
 402                reg_code = ichg_reg_code;
 403        }
 404
 405        ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
 406        if (ret)
 407                return ret;
 408
 409        icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
 410
 411        if (icharge_range)
 412                ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
 413
 414        return reg_code * (ichg_multiplier * ichg_measurement /
 415                                                        BQ2515X_ICHG_DIVISOR);
 416}
 417
 418static bool bq2515x_get_charge_disable(struct bq2515x_device *bq2515x)
 419{
 420        int ret;
 421        int ce_pin;
 422        int icctrl2;
 423        int charger_disable;
 424
 425        ce_pin = gpiod_get_value_cansleep(bq2515x->ce_gpio);
 426
 427        ret = regmap_read(bq2515x->regmap, BQ2515X_ICCTRL2, &icctrl2);
 428        if (ret)
 429                return ret;
 430
 431        charger_disable = icctrl2 & BQ2515X_CHARGER_DISABLE;
 432
 433        if (charger_disable || ce_pin)
 434                return true;
 435
 436        return false;
 437}
 438
 439static int bq2515x_set_charge_disable(struct bq2515x_device *bq2515x, int val)
 440{
 441        gpiod_set_value_cansleep(bq2515x->ce_gpio, val);
 442
 443        return regmap_update_bits(bq2515x->regmap, BQ2515X_ICCTRL2,
 444                                        BQ2515X_CHARGER_DISABLE, val);
 445}
 446
 447static int bq2515x_get_const_charge_current(struct bq2515x_device *bq2515x)
 448{
 449        int ret;
 450        u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
 451        unsigned int ichg_reg_code;
 452        unsigned int pchrgctrl;
 453        unsigned int icharge_range;
 454
 455        ret = regmap_read(bq2515x->regmap, BQ2515X_ICHG_CTRL, &ichg_reg_code);
 456        if (ret)
 457                return ret;
 458
 459        ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
 460        if (ret)
 461                return ret;
 462
 463        icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
 464
 465        if (icharge_range)
 466                ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
 467
 468        return ichg_reg_code * ichg_multiplier;
 469}
 470
 471static int bq2515x_set_const_charge_current(struct bq2515x_device *bq2515x,
 472                                                                int val)
 473{
 474        int ret;
 475        unsigned int ichg_reg_code;
 476        u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
 477        unsigned int icharge_range = 0;
 478
 479        if (val > BQ2515X_ICHG_MAX_UA || val < BQ2515X_ICHG_MIN_UA)
 480                return -EINVAL;
 481
 482        if (val > BQ2515X_ICHG_CURR_STEP_THRESH_UA) {
 483                ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
 484                icharge_range = BQ2515X_ICHARGE_RANGE;
 485        }
 486
 487        bq2515x_set_charge_disable(bq2515x, 1);
 488
 489        ret = regmap_update_bits(bq2515x->regmap, BQ2515X_PCHRGCTRL,
 490                                        BQ2515X_ICHARGE_RANGE, icharge_range);
 491        if (ret)
 492                return ret;
 493
 494        ichg_reg_code = val / ichg_multiplier;
 495
 496        ret = regmap_write(bq2515x->regmap, BQ2515X_ICHG_CTRL, ichg_reg_code);
 497        if (ret)
 498                return ret;
 499
 500        return bq2515x_set_charge_disable(bq2515x, 0);
 501}
 502
 503static int bq2515x_get_precharge_current(struct bq2515x_device *bq2515x)
 504{
 505        int ret;
 506        unsigned int pchrgctrl;
 507        unsigned int icharge_range;
 508        u16 precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
 509        unsigned int precharge_reg_code;
 510
 511        ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
 512        if (ret)
 513                return ret;
 514
 515        icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
 516
 517        if (icharge_range)
 518                precharge_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
 519
 520        precharge_reg_code = pchrgctrl & BQ2515X_PRECHARGE_MASK;
 521
 522        return precharge_reg_code * precharge_multiplier;
 523}
 524
 525static int bq2515x_set_precharge_current(struct bq2515x_device *bq2515x,
 526                                        int val)
 527{
 528        int ret;
 529        unsigned int pchrgctrl;
 530        unsigned int icharge_range;
 531        unsigned int precharge_reg_code;
 532        unsigned int precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
 533        unsigned int precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA;
 534
 535        ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
 536        if (ret)
 537                return ret;
 538
 539        icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
 540
 541        if (icharge_range) {
 542                precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_3750_UA;
 543                precharge_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
 544        } else {
 545                precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA;
 546                precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
 547        }
 548        if (val > precharge_max_ua || val < BQ2515X_ICHG_MIN_UA)
 549                return -EINVAL;
 550
 551        precharge_reg_code = val / precharge_multiplier;
 552
 553        ret = bq2515x_set_charge_disable(bq2515x, 1);
 554        if (ret)
 555                return ret;
 556
 557        ret = regmap_update_bits(bq2515x->regmap, BQ2515X_PCHRGCTRL,
 558                                BQ2515X_PRECHARGE_MASK, precharge_reg_code);
 559        if (ret)
 560                return ret;
 561
 562        return bq2515x_set_charge_disable(bq2515x, 0);
 563}
 564
 565static int bq2515x_charging_status(struct bq2515x_device *bq2515x,
 566                                   union power_supply_propval *val)
 567{
 568        bool status0_no_fault;
 569        bool status1_no_fault;
 570        bool ce_status;
 571        bool charge_done;
 572        unsigned int status;
 573        int ret;
 574
 575        if (!bq2515x->mains_online) {
 576                val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
 577                return 0;
 578        }
 579
 580        ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &status);
 581        if (ret)
 582                return ret;
 583
 584        /*
 585         * The code block below is used to determine if any faults from the
 586         * STAT0 register are disbaling charging or if the charge has completed
 587         * according to the CHARGE_DONE_STAT bit.
 588         */
 589        if (((status & BQ2515X_STAT0_MASK) == true) &
 590                        ((status & BQ2515X_CHRG_DONE) == false)) {
 591                status0_no_fault = true;
 592                charge_done = false;
 593        } else if (status & BQ2515X_CHRG_DONE) {
 594                charge_done = true;
 595                status0_no_fault = false;
 596        } else {
 597                status0_no_fault = false;
 598                charge_done = false;
 599        }
 600
 601        ret = regmap_read(bq2515x->regmap, BQ2515X_STAT1, &status);
 602        if (ret)
 603                return ret;
 604        /*
 605         * The code block below is used to determine if any faults from the
 606         * STAT1 register are disbaling charging
 607         */
 608        if ((status & BQ2515X_STAT1_MASK) == false)
 609                status1_no_fault = true;
 610        else
 611                status1_no_fault = false;
 612
 613        ce_status = (!bq2515x_get_charge_disable(bq2515x));
 614
 615        /*
 616         * If there are no faults and charging is enabled, then status is
 617         * charging. Otherwise, if charging is complete, then status is full.
 618         * Otherwise, if a fault exists or charging is disabled, then status is
 619         * not charging
 620         */
 621        if (status0_no_fault & status1_no_fault & ce_status)
 622                val->intval = POWER_SUPPLY_STATUS_CHARGING;
 623        else if (charge_done)
 624                val->intval = POWER_SUPPLY_STATUS_FULL;
 625        else if (!(status0_no_fault & status1_no_fault & ce_status))
 626                val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
 627
 628        return 0;
 629}
 630
 631static int bq2515x_get_batt_reg(struct bq2515x_device *bq2515x)
 632{
 633        int vbat_reg_code;
 634        int ret;
 635
 636        ret = regmap_read(bq2515x->regmap, BQ2515X_VBAT_CTRL, &vbat_reg_code);
 637        if (ret)
 638                return ret;
 639
 640        return BQ2515X_VBAT_BASE_VOLT + vbat_reg_code * BQ2515X_VBAT_STEP_UV;
 641}
 642
 643static int bq2515x_set_batt_reg(struct bq2515x_device *bq2515x, int val)
 644{
 645        int vbat_reg_code;
 646
 647        if (val > BQ2515X_VBAT_REG_MAX || val < BQ2515X_VBAT_REG_MIN)
 648                return -EINVAL;
 649
 650        vbat_reg_code = (val - BQ2515X_VBAT_BASE_VOLT) / BQ2515X_VBAT_STEP_UV;
 651
 652        return regmap_write(bq2515x->regmap, BQ2515X_VBAT_CTRL, vbat_reg_code);
 653}
 654
 655static int bq2515x_get_ilim_lvl(struct bq2515x_device *bq2515x)
 656{
 657        int ret;
 658        int ilimctrl;
 659
 660        ret = regmap_read(bq2515x->regmap, BQ2515X_ILIMCTRL, &ilimctrl);
 661        if (ret)
 662                return ret;
 663
 664        return bq2515x_ilim_lvl_values[ilimctrl & BQ2515X_ILIM_MASK];
 665}
 666
 667static int bq2515x_set_ilim_lvl(struct bq2515x_device *bq2515x, int val)
 668{
 669        int i = 0;
 670        unsigned int array_size = ARRAY_SIZE(bq2515x_ilim_lvl_values);
 671
 672        for (i = array_size - 1; i > 0; i--) {
 673                if (val >= bq2515x_ilim_lvl_values[i])
 674                        break;
 675        }
 676        return regmap_write(bq2515x->regmap, BQ2515X_ILIMCTRL, i);
 677}
 678
 679static int bq2515x_power_supply_property_is_writeable(struct power_supply *psy,
 680                                        enum power_supply_property prop)
 681{
 682        switch (prop) {
 683        case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 684        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
 685        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
 686        case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
 687                return true;
 688        default:
 689                return false;
 690        }
 691}
 692
 693static int bq2515x_charger_get_health(struct bq2515x_device *bq2515x,
 694                                      union power_supply_propval *val)
 695{
 696        int health = POWER_SUPPLY_HEALTH_GOOD;
 697        int ret;
 698        unsigned int stat1;
 699        unsigned int flag3;
 700
 701        if (!bq2515x->mains_online)
 702                bq2515x_wake_up(bq2515x);
 703
 704        ret = regmap_read(bq2515x->regmap, BQ2515X_FLAG3, &flag3);
 705        if (ret)
 706                return ret;
 707
 708        ret = regmap_read(bq2515x->regmap, BQ2515X_STAT1, &stat1);
 709        if (ret)
 710                return ret;
 711
 712        if (stat1 & BQ2515X_HEALTH_MASK) {
 713                switch (stat1 & BQ2515X_HEALTH_MASK) {
 714                case BQ2515X_TS_HOT_STAT:
 715                        health = POWER_SUPPLY_HEALTH_HOT;
 716                        break;
 717                case BQ2515X_TS_WARM_STAT:
 718                        health = POWER_SUPPLY_HEALTH_WARM;
 719                        break;
 720                case BQ2515X_TS_COOL_STAT:
 721                        health = POWER_SUPPLY_HEALTH_COOL;
 722                        break;
 723                case BQ2515X_TS_COLD_STAT:
 724                        health = POWER_SUPPLY_HEALTH_COLD;
 725                        break;
 726                default:
 727                        health = POWER_SUPPLY_HEALTH_UNKNOWN;
 728                        break;
 729                }
 730        }
 731
 732        if (stat1 & BQ2515X_VIN_OVP_FAULT_STAT)
 733                health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
 734
 735        if (flag3 & BQ2515X_SAFETY_TIMER_EXP)
 736                health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
 737
 738        val->intval = health;
 739        return 0;
 740}
 741
 742static int bq2515x_mains_set_property(struct power_supply *psy,
 743                enum power_supply_property prop,
 744                const union power_supply_propval *val)
 745{
 746        struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy);
 747        int ret;
 748
 749        switch (prop) {
 750        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
 751                ret = bq2515x_set_batt_reg(bq2515x, val->intval);
 752                break;
 753
 754        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
 755                ret = bq2515x_set_const_charge_current(bq2515x, val->intval);
 756                break;
 757
 758        case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 759                ret = bq2515x_set_ilim_lvl(bq2515x, val->intval);
 760                break;
 761
 762        case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
 763                ret = bq2515x_set_precharge_current(bq2515x, val->intval);
 764                break;
 765
 766        default:
 767                return -EINVAL;
 768        }
 769
 770        return ret;
 771}
 772
 773static int bq2515x_mains_get_property(struct power_supply *psy,
 774                                     enum power_supply_property prop,
 775                                     union power_supply_propval *val)
 776{
 777        struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy);
 778        int ret = 0;
 779
 780        switch (prop) {
 781
 782        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
 783                ret = bq2515x_get_const_charge_current(bq2515x);
 784                if (ret < 0)
 785                        return ret;
 786
 787                val->intval = ret;
 788                break;
 789
 790        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
 791                ret = bq2515x_get_batt_reg(bq2515x);
 792                if (ret < 0)
 793                        return ret;
 794                val->intval = ret;
 795                break;
 796
 797        case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
 798                ret = bq2515x_get_precharge_current(bq2515x);
 799                if (ret < 0)
 800                        return ret;
 801                val->intval = ret;
 802                break;
 803
 804        case POWER_SUPPLY_PROP_ONLINE:
 805                val->intval = bq2515x->mains_online;
 806                break;
 807
 808        case POWER_SUPPLY_PROP_HEALTH:
 809                ret = bq2515x_charger_get_health(bq2515x, val);
 810                if (ret)
 811                        val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
 812                break;
 813
 814        case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 815                ret = bq2515x_get_ilim_lvl(bq2515x);
 816                if (ret < 0)
 817                        return ret;
 818                val->intval = ret;
 819                break;
 820
 821        case POWER_SUPPLY_PROP_MODEL_NAME:
 822                val->strval = bq2515x->model_name;
 823                break;
 824
 825        case POWER_SUPPLY_PROP_MANUFACTURER:
 826                val->strval = BQ2515X_MANUFACTURER;
 827                break;
 828
 829        case POWER_SUPPLY_PROP_STATUS:
 830                ret = bq2515x_charging_status(bq2515x, val);
 831                if (ret)
 832                        return ret;
 833                break;
 834
 835        default:
 836                return -EINVAL;
 837        }
 838
 839        return ret;
 840}
 841
 842static int bq2515x_battery_get_property(struct power_supply *psy,
 843                                       enum power_supply_property prop,
 844                                       union power_supply_propval *val)
 845{
 846        struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy);
 847        int ret;
 848
 849        ret = bq2515x_update_ps_status(bq2515x);
 850        if (ret)
 851                return ret;
 852
 853        switch (prop) {
 854
 855        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
 856                ret = bq2515x->init_data.vbatreg;
 857                if (ret < 0)
 858                        return ret;
 859                val->intval = ret;
 860                break;
 861
 862        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
 863                ret = bq2515x->init_data.ichg;
 864                if (ret < 0)
 865                        return ret;
 866                val->intval = ret;
 867                break;
 868
 869        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 870                ret = bq2515x_get_battery_voltage_now(bq2515x);
 871                if (ret < 0)
 872                        return ret;
 873                val->intval = ret;
 874                break;
 875
 876        case POWER_SUPPLY_PROP_CURRENT_NOW:
 877                ret = bq2515x_get_battery_current_now(bq2515x);
 878                if (ret < 0)
 879                        return ret;
 880                val->intval = ret;
 881                break;
 882
 883        default:
 884                return -EINVAL;
 885        }
 886        return 0;
 887}
 888
 889static const enum power_supply_property bq2515x_battery_properties[] = {
 890        POWER_SUPPLY_PROP_VOLTAGE_NOW,
 891        POWER_SUPPLY_PROP_CURRENT_NOW,
 892        POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
 893        POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
 894};
 895
 896static const enum power_supply_property bq2515x_mains_properties[] = {
 897        POWER_SUPPLY_PROP_ONLINE,
 898        POWER_SUPPLY_PROP_STATUS,
 899        POWER_SUPPLY_PROP_HEALTH,
 900        POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
 901        POWER_SUPPLY_PROP_MODEL_NAME,
 902        POWER_SUPPLY_PROP_MANUFACTURER,
 903        POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
 904        POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
 905        POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
 906};
 907
 908static const struct power_supply_desc bq2515x_mains_desc = {
 909        .name                   = "bq2515x-mains",
 910        .type                   = POWER_SUPPLY_TYPE_MAINS,
 911        .get_property           = bq2515x_mains_get_property,
 912        .set_property           = bq2515x_mains_set_property,
 913        .properties             = bq2515x_mains_properties,
 914        .num_properties         = ARRAY_SIZE(bq2515x_mains_properties),
 915        .property_is_writeable  = bq2515x_power_supply_property_is_writeable,
 916};
 917
 918static const struct power_supply_desc bq2515x_battery_desc = {
 919        .name                   = "bq2515x-battery",
 920        .type                   = POWER_SUPPLY_TYPE_BATTERY,
 921        .get_property           = bq2515x_battery_get_property,
 922        .properties             = bq2515x_battery_properties,
 923        .num_properties         = ARRAY_SIZE(bq2515x_battery_properties),
 924        .property_is_writeable  = bq2515x_power_supply_property_is_writeable,
 925};
 926
 927static int bq2515x_power_supply_register(struct bq2515x_device *bq2515x,
 928                struct device *dev, struct power_supply_config psy_cfg)
 929{
 930        bq2515x->mains = devm_power_supply_register(bq2515x->dev,
 931                                                    &bq2515x_mains_desc,
 932                                                    &psy_cfg);
 933        if (IS_ERR(bq2515x->mains))
 934                return -EINVAL;
 935
 936        bq2515x->battery = devm_power_supply_register(bq2515x->dev,
 937                                                      &bq2515x_battery_desc,
 938                                                      &psy_cfg);
 939        if (IS_ERR(bq2515x->battery))
 940                return -EINVAL;
 941
 942        return 0;
 943}
 944
 945static int bq2515x_hw_init(struct bq2515x_device *bq2515x)
 946{
 947        int ret;
 948        struct power_supply_battery_info bat_info = { };
 949
 950        ret = bq2515x_disable_watchdog_timers(bq2515x);
 951        if (ret)
 952                return ret;
 953
 954        if (bq2515x->init_data.ilim) {
 955                ret = bq2515x_set_ilim_lvl(bq2515x, bq2515x->init_data.ilim);
 956                if (ret)
 957                        return ret;
 958        }
 959
 960        ret = power_supply_get_battery_info(bq2515x->mains, &bat_info);
 961        if (ret) {
 962                dev_warn(bq2515x->dev, "battery info missing, default values will be applied\n");
 963
 964                bq2515x->init_data.ichg = BQ2515X_DEFAULT_ICHG_UA;
 965
 966                bq2515x->init_data.vbatreg = BQ2515X_DEFAULT_VBAT_REG_UV;
 967
 968                bq2515x->init_data.iprechg = BQ2515X_DEFAULT_IPRECHARGE_UA;
 969
 970        } else {
 971                bq2515x->init_data.ichg =
 972                                bat_info.constant_charge_current_max_ua;
 973
 974                bq2515x->init_data.vbatreg =
 975                                bat_info.constant_charge_voltage_max_uv;
 976
 977                bq2515x->init_data.iprechg =
 978                                bat_info.precharge_current_ua;
 979        }
 980
 981        ret = bq2515x_set_const_charge_current(bq2515x,
 982                                                bq2515x->init_data.ichg);
 983        if (ret)
 984                return ret;
 985
 986        ret = bq2515x_set_batt_reg(bq2515x, bq2515x->init_data.vbatreg);
 987        if (ret)
 988                return ret;
 989
 990        return bq2515x_set_precharge_current(bq2515x,
 991                                                bq2515x->init_data.iprechg);
 992}
 993
 994static int bq2515x_read_properties(struct bq2515x_device *bq2515x)
 995{
 996        int ret;
 997
 998        ret = device_property_read_u32(bq2515x->dev,
 999                                      "input-current-limit-microamp",
1000                                      &bq2515x->init_data.ilim);
1001        if (ret) {
1002                switch (bq2515x->device_id) {
1003                case BQ25150:
1004                        bq2515x->init_data.ilim = BQ25150_DEFAULT_ILIM_UA;
1005                        break;
1006                case BQ25155:
1007                        bq2515x->init_data.ilim = BQ25155_DEFAULT_ILIM_UA;
1008                        break;
1009                }
1010        }
1011
1012        bq2515x->ac_detect_gpio = devm_gpiod_get_optional(bq2515x->dev,
1013                                                   "ac-detect", GPIOD_IN);
1014        if (IS_ERR(bq2515x->ac_detect_gpio)) {
1015                ret = PTR_ERR(bq2515x->ac_detect_gpio);
1016                dev_err(bq2515x->dev, "Failed to get ac detect");
1017                return ret;
1018        }
1019
1020        bq2515x->reset_gpio = devm_gpiod_get_optional(bq2515x->dev,
1021                                                   "reset", GPIOD_OUT_LOW);
1022        if (IS_ERR(bq2515x->reset_gpio)) {
1023                ret = PTR_ERR(bq2515x->reset_gpio);
1024                dev_err(bq2515x->dev, "Failed to get reset");
1025                return ret;
1026        }
1027
1028        bq2515x->powerdown_gpio = devm_gpiod_get_optional(bq2515x->dev,
1029                                                "powerdown", GPIOD_OUT_LOW);
1030        if (IS_ERR(bq2515x->powerdown_gpio)) {
1031                ret = PTR_ERR(bq2515x->powerdown_gpio);
1032                dev_err(bq2515x->dev, "Failed to get powerdown");
1033                return ret;
1034        }
1035
1036        bq2515x->ce_gpio = devm_gpiod_get_optional(bq2515x->dev,
1037                                                   "charge-enable",
1038                                                   GPIOD_OUT_LOW);
1039        if (IS_ERR(bq2515x->ce_gpio)) {
1040                ret = PTR_ERR(bq2515x->ce_gpio);
1041                dev_err(bq2515x->dev, "Failed to get ce");
1042                return ret;
1043        }
1044
1045        return 0;
1046}
1047
1048static bool bq2515x_volatile_register(struct device *dev, unsigned int reg)
1049{
1050        switch (reg) {
1051        case BQ2515X_STAT0 ... BQ2515X_FLAG3:
1052        case BQ2515X_ADC_VBAT_M ... BQ2515X_ADC_IIN_L:
1053                return true;
1054        default:
1055                return false;
1056        }
1057}
1058
1059static const struct regmap_config bq25150_regmap_config = {
1060        .reg_bits = 8,
1061        .val_bits = 8,
1062
1063        .max_register           = BQ2515X_DEVICE_ID,
1064        .reg_defaults           = bq25150_reg_defaults,
1065        .num_reg_defaults       = ARRAY_SIZE(bq25150_reg_defaults),
1066        .cache_type             = REGCACHE_RBTREE,
1067        .volatile_reg           = bq2515x_volatile_register,
1068};
1069
1070static const struct regmap_config bq25155_regmap_config = {
1071        .reg_bits = 8,
1072        .val_bits = 8,
1073
1074        .max_register           = BQ2515X_DEVICE_ID,
1075        .reg_defaults           = bq25155_reg_defaults,
1076        .num_reg_defaults       = ARRAY_SIZE(bq25155_reg_defaults),
1077        .cache_type             = REGCACHE_RBTREE,
1078        .volatile_reg           = bq2515x_volatile_register,
1079};
1080
1081static int bq2515x_probe(struct i2c_client *client,
1082                         const struct i2c_device_id *id)
1083{
1084        struct device *dev = &client->dev;
1085        struct bq2515x_device *bq2515x;
1086        struct power_supply_config charger_cfg = {};
1087        int ret;
1088
1089        bq2515x = devm_kzalloc(dev, sizeof(*bq2515x), GFP_KERNEL);
1090        if (!bq2515x)
1091                return -ENOMEM;
1092
1093        bq2515x->dev = dev;
1094
1095        strncpy(bq2515x->model_name, id->name, I2C_NAME_SIZE);
1096
1097        bq2515x->device_id = id->driver_data;
1098
1099        switch (bq2515x->device_id) {
1100        case BQ25150:
1101                bq2515x->regmap = devm_regmap_init_i2c(client,
1102                                                &bq25150_regmap_config);
1103                break;
1104        case BQ25155:
1105                bq2515x->regmap = devm_regmap_init_i2c(client,
1106                                                &bq25155_regmap_config);
1107                break;
1108        }
1109
1110        if (IS_ERR(bq2515x->regmap)) {
1111                dev_err(dev, "failed to allocate register map\n");
1112                return PTR_ERR(bq2515x->regmap);
1113        }
1114
1115        i2c_set_clientdata(client, bq2515x);
1116
1117        charger_cfg.drv_data = bq2515x;
1118        charger_cfg.of_node = dev->of_node;
1119
1120        ret = bq2515x_read_properties(bq2515x);
1121        if (ret) {
1122                dev_err(dev, "Failed to read device tree properties %d\n",
1123                                                                        ret);
1124                return ret;
1125        }
1126
1127        ret = bq2515x_power_supply_register(bq2515x, dev, charger_cfg);
1128        if (ret) {
1129                dev_err(dev, "failed to register power supply\n");
1130                return ret;
1131        }
1132
1133        ret = bq2515x_hw_init(bq2515x);
1134        if (ret) {
1135                dev_err(dev, "Cannot initialize the chip\n");
1136                return ret;
1137        }
1138
1139        return 0;
1140}
1141
1142static const struct i2c_device_id bq2515x_i2c_ids[] = {
1143        { "bq25150", BQ25150, },
1144        { "bq25155", BQ25155, },
1145        {},
1146};
1147MODULE_DEVICE_TABLE(i2c, bq2515x_i2c_ids);
1148
1149static const struct of_device_id bq2515x_of_match[] = {
1150        { .compatible = "ti,bq25150", },
1151        { .compatible = "ti,bq25155", },
1152        { },
1153};
1154MODULE_DEVICE_TABLE(of, bq2515x_of_match);
1155
1156static struct i2c_driver bq2515x_driver = {
1157        .driver = {
1158                .name = "bq2515x-charger",
1159                .of_match_table = bq2515x_of_match,
1160        },
1161        .probe = bq2515x_probe,
1162        .id_table = bq2515x_i2c_ids,
1163};
1164module_i2c_driver(bq2515x_driver);
1165
1166MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
1167MODULE_AUTHOR("Ricardo Rivera-Matos <r-rivera-matos@ti.com>");
1168MODULE_DESCRIPTION("BQ2515X charger driver");
1169MODULE_LICENSE("GPL v2");
1170
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.