root/drivers/power/supply/bq24257_charger.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. bq24257_is_volatile_reg
  2. bq24257_field_read
  3. bq24257_field_write
  4. bq24257_find_idx
  5. bq24257_get_input_current_limit
  6. bq24257_set_input_current_limit
  7. bq24257_power_supply_get_property
  8. bq24257_power_supply_set_property
  9. bq24257_power_supply_property_is_writeable
  10. bq24257_get_chip_state
  11. bq24257_state_changed
  12. bq24257_iilimit_autoset
  13. bq24257_iilimit_setup_work
  14. bq24257_handle_state_change
  15. bq24257_irq_handler_thread
  16. bq24257_hw_init
  17. bq24257_show_ovp_voltage
  18. bq24257_show_in_dpm_voltage
  19. bq24257_sysfs_show_enable
  20. bq24257_sysfs_set_enable
  21. bq24257_power_supply_init
  22. bq24257_pg_gpio_probe
  23. bq24257_fw_probe
  24. bq24257_probe
  25. bq24257_remove
  26. bq24257_suspend
  27. bq24257_resume

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  * TI BQ24257 charger driver
   4  *
   5  * Copyright (C) 2015 Intel Corporation
   6  *
   7  * Datasheets:
   8  * http://www.ti.com/product/bq24250
   9  * http://www.ti.com/product/bq24251
  10  * http://www.ti.com/product/bq24257
  11  */
  12 
  13 #include <linux/module.h>
  14 #include <linux/i2c.h>
  15 #include <linux/power_supply.h>
  16 #include <linux/regmap.h>
  17 #include <linux/types.h>
  18 #include <linux/gpio/consumer.h>
  19 #include <linux/interrupt.h>
  20 #include <linux/delay.h>
  21 
  22 #include <linux/acpi.h>
  23 #include <linux/of.h>
  24 
  25 #define BQ24257_REG_1                   0x00
  26 #define BQ24257_REG_2                   0x01
  27 #define BQ24257_REG_3                   0x02
  28 #define BQ24257_REG_4                   0x03
  29 #define BQ24257_REG_5                   0x04
  30 #define BQ24257_REG_6                   0x05
  31 #define BQ24257_REG_7                   0x06
  32 
  33 #define BQ24257_MANUFACTURER            "Texas Instruments"
  34 #define BQ24257_PG_GPIO                 "pg"
  35 
  36 #define BQ24257_ILIM_SET_DELAY          1000    /* msec */
  37 
  38 /*
  39  * When adding support for new devices make sure that enum bq2425x_chip and
  40  * bq2425x_chip_name[] always stay in sync!
  41  */
  42 enum bq2425x_chip {
  43         BQ24250,
  44         BQ24251,
  45         BQ24257,
  46 };
  47 
  48 static const char *const bq2425x_chip_name[] = {
  49         "bq24250",
  50         "bq24251",
  51         "bq24257",
  52 };
  53 
  54 enum bq24257_fields {
  55         F_WD_FAULT, F_WD_EN, F_STAT, F_FAULT,                       /* REG 1 */
  56         F_RESET, F_IILIMIT, F_EN_STAT, F_EN_TERM, F_CE, F_HZ_MODE,  /* REG 2 */
  57         F_VBAT, F_USB_DET,                                          /* REG 3 */
  58         F_ICHG, F_ITERM,                                            /* REG 4 */
  59         F_LOOP_STATUS, F_LOW_CHG, F_DPDM_EN, F_CE_STATUS, F_VINDPM, /* REG 5 */
  60         F_X2_TMR_EN, F_TMR, F_SYSOFF, F_TS_EN, F_TS_STAT,           /* REG 6 */
  61         F_VOVP, F_CLR_VDP, F_FORCE_BATDET, F_FORCE_PTM,             /* REG 7 */
  62 
  63         F_MAX_FIELDS
  64 };
  65 
  66 /* initial field values, converted from uV/uA */
  67 struct bq24257_init_data {
  68         u8 ichg;        /* charge current      */
  69         u8 vbat;        /* regulation voltage  */
  70         u8 iterm;       /* termination current */
  71         u8 iilimit;     /* input current limit */
  72         u8 vovp;        /* over voltage protection voltage */
  73         u8 vindpm;      /* VDMP input threshold voltage */
  74 };
  75 
  76 struct bq24257_state {
  77         u8 status;
  78         u8 fault;
  79         bool power_good;
  80 };
  81 
  82 struct bq24257_device {
  83         struct i2c_client *client;
  84         struct device *dev;
  85         struct power_supply *charger;
  86 
  87         enum bq2425x_chip chip;
  88 
  89         struct regmap *rmap;
  90         struct regmap_field *rmap_fields[F_MAX_FIELDS];
  91 
  92         struct gpio_desc *pg;
  93 
  94         struct delayed_work iilimit_setup_work;
  95 
  96         struct bq24257_init_data init_data;
  97         struct bq24257_state state;
  98 
  99         struct mutex lock; /* protect state data */
 100 
 101         bool iilimit_autoset_enable;
 102 };
 103 
 104 static bool bq24257_is_volatile_reg(struct device *dev, unsigned int reg)
 105 {
 106         switch (reg) {
 107         case BQ24257_REG_2:
 108         case BQ24257_REG_4:
 109                 return false;
 110 
 111         default:
 112                 return true;
 113         }
 114 }
 115 
 116 static const struct regmap_config bq24257_regmap_config = {
 117         .reg_bits = 8,
 118         .val_bits = 8,
 119 
 120         .max_register = BQ24257_REG_7,
 121         .cache_type = REGCACHE_RBTREE,
 122 
 123         .volatile_reg = bq24257_is_volatile_reg,
 124 };
 125 
 126 static const struct reg_field bq24257_reg_fields[] = {
 127         /* REG 1 */
 128         [F_WD_FAULT]            = REG_FIELD(BQ24257_REG_1, 7, 7),
 129         [F_WD_EN]               = REG_FIELD(BQ24257_REG_1, 6, 6),
 130         [F_STAT]                = REG_FIELD(BQ24257_REG_1, 4, 5),
 131         [F_FAULT]               = REG_FIELD(BQ24257_REG_1, 0, 3),
 132         /* REG 2 */
 133         [F_RESET]               = REG_FIELD(BQ24257_REG_2, 7, 7),
 134         [F_IILIMIT]             = REG_FIELD(BQ24257_REG_2, 4, 6),
 135         [F_EN_STAT]             = REG_FIELD(BQ24257_REG_2, 3, 3),
 136         [F_EN_TERM]             = REG_FIELD(BQ24257_REG_2, 2, 2),
 137         [F_CE]                  = REG_FIELD(BQ24257_REG_2, 1, 1),
 138         [F_HZ_MODE]             = REG_FIELD(BQ24257_REG_2, 0, 0),
 139         /* REG 3 */
 140         [F_VBAT]                = REG_FIELD(BQ24257_REG_3, 2, 7),
 141         [F_USB_DET]             = REG_FIELD(BQ24257_REG_3, 0, 1),
 142         /* REG 4 */
 143         [F_ICHG]                = REG_FIELD(BQ24257_REG_4, 3, 7),
 144         [F_ITERM]               = REG_FIELD(BQ24257_REG_4, 0, 2),
 145         /* REG 5 */
 146         [F_LOOP_STATUS]         = REG_FIELD(BQ24257_REG_5, 6, 7),
 147         [F_LOW_CHG]             = REG_FIELD(BQ24257_REG_5, 5, 5),
 148         [F_DPDM_EN]             = REG_FIELD(BQ24257_REG_5, 4, 4),
 149         [F_CE_STATUS]           = REG_FIELD(BQ24257_REG_5, 3, 3),
 150         [F_VINDPM]              = REG_FIELD(BQ24257_REG_5, 0, 2),
 151         /* REG 6 */
 152         [F_X2_TMR_EN]           = REG_FIELD(BQ24257_REG_6, 7, 7),
 153         [F_TMR]                 = REG_FIELD(BQ24257_REG_6, 5, 6),
 154         [F_SYSOFF]              = REG_FIELD(BQ24257_REG_6, 4, 4),
 155         [F_TS_EN]               = REG_FIELD(BQ24257_REG_6, 3, 3),
 156         [F_TS_STAT]             = REG_FIELD(BQ24257_REG_6, 0, 2),
 157         /* REG 7 */
 158         [F_VOVP]                = REG_FIELD(BQ24257_REG_7, 5, 7),
 159         [F_CLR_VDP]             = REG_FIELD(BQ24257_REG_7, 4, 4),
 160         [F_FORCE_BATDET]        = REG_FIELD(BQ24257_REG_7, 3, 3),
 161         [F_FORCE_PTM]           = REG_FIELD(BQ24257_REG_7, 2, 2)
 162 };
 163 
 164 static const u32 bq24257_vbat_map[] = {
 165         3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
 166         3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
 167         3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
 168         3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
 169         4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
 170         4300000, 4320000, 4340000, 4360000, 4380000, 4400000, 4420000, 4440000
 171 };
 172 
 173 #define BQ24257_VBAT_MAP_SIZE           ARRAY_SIZE(bq24257_vbat_map)
 174 
 175 static const u32 bq24257_ichg_map[] = {
 176         500000, 550000, 600000, 650000, 700000, 750000, 800000, 850000, 900000,
 177         950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000,
 178         1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
 179         1750000, 1800000, 1850000, 1900000, 1950000, 2000000
 180 };
 181 
 182 #define BQ24257_ICHG_MAP_SIZE           ARRAY_SIZE(bq24257_ichg_map)
 183 
 184 static const u32 bq24257_iterm_map[] = {
 185         50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000
 186 };
 187 
 188 #define BQ24257_ITERM_MAP_SIZE          ARRAY_SIZE(bq24257_iterm_map)
 189 
 190 static const u32 bq24257_iilimit_map[] = {
 191         100000, 150000, 500000, 900000, 1500000, 2000000
 192 };
 193 
 194 #define BQ24257_IILIMIT_MAP_SIZE        ARRAY_SIZE(bq24257_iilimit_map)
 195 
 196 static const u32 bq24257_vovp_map[] = {
 197         6000000, 6500000, 7000000, 8000000, 9000000, 9500000, 10000000,
 198         10500000
 199 };
 200 
 201 #define BQ24257_VOVP_MAP_SIZE           ARRAY_SIZE(bq24257_vovp_map)
 202 
 203 static const u32 bq24257_vindpm_map[] = {
 204         4200000, 4280000, 4360000, 4440000, 4520000, 4600000, 4680000,
 205         4760000
 206 };
 207 
 208 #define BQ24257_VINDPM_MAP_SIZE         ARRAY_SIZE(bq24257_vindpm_map)
 209 
 210 static int bq24257_field_read(struct bq24257_device *bq,
 211                               enum bq24257_fields field_id)
 212 {
 213         int ret;
 214         int val;
 215 
 216         ret = regmap_field_read(bq->rmap_fields[field_id], &val);
 217         if (ret < 0)
 218                 return ret;
 219 
 220         return val;
 221 }
 222 
 223 static int bq24257_field_write(struct bq24257_device *bq,
 224                                enum bq24257_fields field_id, u8 val)
 225 {
 226         return regmap_field_write(bq->rmap_fields[field_id], val);
 227 }
 228 
 229 static u8 bq24257_find_idx(u32 value, const u32 *map, u8 map_size)
 230 {
 231         u8 idx;
 232 
 233         for (idx = 1; idx < map_size; idx++)
 234                 if (value < map[idx])
 235                         break;
 236 
 237         return idx - 1;
 238 }
 239 
 240 enum bq24257_status {
 241         STATUS_READY,
 242         STATUS_CHARGE_IN_PROGRESS,
 243         STATUS_CHARGE_DONE,
 244         STATUS_FAULT,
 245 };
 246 
 247 enum bq24257_fault {
 248         FAULT_NORMAL,
 249         FAULT_INPUT_OVP,
 250         FAULT_INPUT_UVLO,
 251         FAULT_SLEEP,
 252         FAULT_BAT_TS,
 253         FAULT_BAT_OVP,
 254         FAULT_TS,
 255         FAULT_TIMER,
 256         FAULT_NO_BAT,
 257         FAULT_ISET,
 258         FAULT_INPUT_LDO_LOW,
 259 };
 260 
 261 static int bq24257_get_input_current_limit(struct bq24257_device *bq,
 262                                            union power_supply_propval *val)
 263 {
 264         int ret;
 265 
 266         ret = bq24257_field_read(bq, F_IILIMIT);
 267         if (ret < 0)
 268                 return ret;
 269 
 270         /*
 271          * The "External ILIM" and "Production & Test" modes are not exposed
 272          * through this driver and not being covered by the lookup table.
 273          * Should such a mode have become active let's return an error rather
 274          * than exceeding the bounds of the lookup table and returning
 275          * garbage.
 276          */
 277         if (ret >= BQ24257_IILIMIT_MAP_SIZE)
 278                 return -ENODATA;
 279 
 280         val->intval = bq24257_iilimit_map[ret];
 281 
 282         return 0;
 283 }
 284 
 285 static int bq24257_set_input_current_limit(struct bq24257_device *bq,
 286                                         const union power_supply_propval *val)
 287 {
 288         /*
 289          * Address the case where the user manually sets an input current limit
 290          * while the charger auto-detection mechanism is is active. In this
 291          * case we want to abort and go straight to the user-specified value.
 292          */
 293         if (bq->iilimit_autoset_enable)
 294                 cancel_delayed_work_sync(&bq->iilimit_setup_work);
 295 
 296         return bq24257_field_write(bq, F_IILIMIT,
 297                                    bq24257_find_idx(val->intval,
 298                                                     bq24257_iilimit_map,
 299                                                     BQ24257_IILIMIT_MAP_SIZE));
 300 }
 301 
 302 static int bq24257_power_supply_get_property(struct power_supply *psy,
 303                                              enum power_supply_property psp,
 304                                              union power_supply_propval *val)
 305 {
 306         struct bq24257_device *bq = power_supply_get_drvdata(psy);
 307         struct bq24257_state state;
 308 
 309         mutex_lock(&bq->lock);
 310         state = bq->state;
 311         mutex_unlock(&bq->lock);
 312 
 313         switch (psp) {
 314         case POWER_SUPPLY_PROP_STATUS:
 315                 if (!state.power_good)
 316                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
 317                 else if (state.status == STATUS_READY)
 318                         val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
 319                 else if (state.status == STATUS_CHARGE_IN_PROGRESS)
 320                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
 321                 else if (state.status == STATUS_CHARGE_DONE)
 322                         val->intval = POWER_SUPPLY_STATUS_FULL;
 323                 else
 324                         val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
 325                 break;
 326 
 327         case POWER_SUPPLY_PROP_MANUFACTURER:
 328                 val->strval = BQ24257_MANUFACTURER;
 329                 break;
 330 
 331         case POWER_SUPPLY_PROP_MODEL_NAME:
 332                 val->strval = bq2425x_chip_name[bq->chip];
 333                 break;
 334 
 335         case POWER_SUPPLY_PROP_ONLINE:
 336                 val->intval = state.power_good;
 337                 break;
 338 
 339         case POWER_SUPPLY_PROP_HEALTH:
 340                 switch (state.fault) {
 341                 case FAULT_NORMAL:
 342                         val->intval = POWER_SUPPLY_HEALTH_GOOD;
 343                         break;
 344 
 345                 case FAULT_INPUT_OVP:
 346                 case FAULT_BAT_OVP:
 347                         val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
 348                         break;
 349 
 350                 case FAULT_TS:
 351                 case FAULT_BAT_TS:
 352                         val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
 353                         break;
 354 
 355                 case FAULT_TIMER:
 356                         val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
 357                         break;
 358 
 359                 default:
 360                         val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
 361                         break;
 362                 }
 363 
 364                 break;
 365 
 366         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
 367                 val->intval = bq24257_ichg_map[bq->init_data.ichg];
 368                 break;
 369 
 370         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
 371                 val->intval = bq24257_ichg_map[BQ24257_ICHG_MAP_SIZE - 1];
 372                 break;
 373 
 374         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
 375                 val->intval = bq24257_vbat_map[bq->init_data.vbat];
 376                 break;
 377 
 378         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
 379                 val->intval = bq24257_vbat_map[BQ24257_VBAT_MAP_SIZE - 1];
 380                 break;
 381 
 382         case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
 383                 val->intval = bq24257_iterm_map[bq->init_data.iterm];
 384                 break;
 385 
 386         case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 387                 return bq24257_get_input_current_limit(bq, val);
 388 
 389         default:
 390                 return -EINVAL;
 391         }
 392 
 393         return 0;
 394 }
 395 
 396 static int bq24257_power_supply_set_property(struct power_supply *psy,
 397                                         enum power_supply_property prop,
 398                                         const union power_supply_propval *val)
 399 {
 400         struct bq24257_device *bq = power_supply_get_drvdata(psy);
 401 
 402         switch (prop) {
 403         case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 404                 return bq24257_set_input_current_limit(bq, val);
 405         default:
 406                 return -EINVAL;
 407         }
 408 }
 409 
 410 static int bq24257_power_supply_property_is_writeable(struct power_supply *psy,
 411                                         enum power_supply_property psp)
 412 {
 413         switch (psp) {
 414         case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
 415                 return true;
 416         default:
 417                 return false;
 418         }
 419 }
 420 
 421 static int bq24257_get_chip_state(struct bq24257_device *bq,
 422                                   struct bq24257_state *state)
 423 {
 424         int ret;
 425 
 426         ret = bq24257_field_read(bq, F_STAT);
 427         if (ret < 0)
 428                 return ret;
 429 
 430         state->status = ret;
 431 
 432         ret = bq24257_field_read(bq, F_FAULT);
 433         if (ret < 0)
 434                 return ret;
 435 
 436         state->fault = ret;
 437 
 438         if (bq->pg)
 439                 state->power_good = !gpiod_get_value_cansleep(bq->pg);
 440         else
 441                 /*
 442                  * If we have a chip without a dedicated power-good GPIO or
 443                  * some other explicit bit that would provide this information
 444                  * assume the power is good if there is no supply related
 445                  * fault - and not good otherwise. There is a possibility for
 446                  * other errors to mask that power in fact is not good but this
 447                  * is probably the best we can do here.
 448                  */
 449                 switch (state->fault) {
 450                 case FAULT_INPUT_OVP:
 451                 case FAULT_INPUT_UVLO:
 452                 case FAULT_INPUT_LDO_LOW:
 453                         state->power_good = false;
 454                         break;
 455                 default:
 456                         state->power_good = true;
 457                 }
 458 
 459         return 0;
 460 }
 461 
 462 static bool bq24257_state_changed(struct bq24257_device *bq,
 463                                   struct bq24257_state *new_state)
 464 {
 465         int ret;
 466 
 467         mutex_lock(&bq->lock);
 468         ret = (bq->state.status != new_state->status ||
 469                bq->state.fault != new_state->fault ||
 470                bq->state.power_good != new_state->power_good);
 471         mutex_unlock(&bq->lock);
 472 
 473         return ret;
 474 }
 475 
 476 enum bq24257_loop_status {
 477         LOOP_STATUS_NONE,
 478         LOOP_STATUS_IN_DPM,
 479         LOOP_STATUS_IN_CURRENT_LIMIT,
 480         LOOP_STATUS_THERMAL,
 481 };
 482 
 483 enum bq24257_in_ilimit {
 484         IILIMIT_100,
 485         IILIMIT_150,
 486         IILIMIT_500,
 487         IILIMIT_900,
 488         IILIMIT_1500,
 489         IILIMIT_2000,
 490         IILIMIT_EXT,
 491         IILIMIT_NONE,
 492 };
 493 
 494 enum bq24257_vovp {
 495         VOVP_6000,
 496         VOVP_6500,
 497         VOVP_7000,
 498         VOVP_8000,
 499         VOVP_9000,
 500         VOVP_9500,
 501         VOVP_10000,
 502         VOVP_10500
 503 };
 504 
 505 enum bq24257_vindpm {
 506         VINDPM_4200,
 507         VINDPM_4280,
 508         VINDPM_4360,
 509         VINDPM_4440,
 510         VINDPM_4520,
 511         VINDPM_4600,
 512         VINDPM_4680,
 513         VINDPM_4760
 514 };
 515 
 516 enum bq24257_port_type {
 517         PORT_TYPE_DCP,          /* Dedicated Charging Port */
 518         PORT_TYPE_CDP,          /* Charging Downstream Port */
 519         PORT_TYPE_SDP,          /* Standard Downstream Port */
 520         PORT_TYPE_NON_STANDARD,
 521 };
 522 
 523 enum bq24257_safety_timer {
 524         SAFETY_TIMER_45,
 525         SAFETY_TIMER_360,
 526         SAFETY_TIMER_540,
 527         SAFETY_TIMER_NONE,
 528 };
 529 
 530 static int bq24257_iilimit_autoset(struct bq24257_device *bq)
 531 {
 532         int loop_status;
 533         int iilimit;
 534         int port_type;
 535         int ret;
 536         const u8 new_iilimit[] = {
 537                 [PORT_TYPE_DCP] = IILIMIT_2000,
 538                 [PORT_TYPE_CDP] = IILIMIT_2000,
 539                 [PORT_TYPE_SDP] = IILIMIT_500,
 540                 [PORT_TYPE_NON_STANDARD] = IILIMIT_500
 541         };
 542 
 543         ret = bq24257_field_read(bq, F_LOOP_STATUS);
 544         if (ret < 0)
 545                 goto error;
 546 
 547         loop_status = ret;
 548 
 549         ret = bq24257_field_read(bq, F_IILIMIT);
 550         if (ret < 0)
 551                 goto error;
 552 
 553         iilimit = ret;
 554 
 555         /*
 556          * All USB ports should be able to handle 500mA. If not, DPM will lower
 557          * the charging current to accommodate the power source. No need to set
 558          * a lower IILIMIT value.
 559          */
 560         if (loop_status == LOOP_STATUS_IN_DPM && iilimit == IILIMIT_500)
 561                 return 0;
 562 
 563         ret = bq24257_field_read(bq, F_USB_DET);
 564         if (ret < 0)
 565                 goto error;
 566 
 567         port_type = ret;
 568 
 569         ret = bq24257_field_write(bq, F_IILIMIT, new_iilimit[port_type]);
 570         if (ret < 0)
 571                 goto error;
 572 
 573         ret = bq24257_field_write(bq, F_TMR, SAFETY_TIMER_360);
 574         if (ret < 0)
 575                 goto error;
 576 
 577         ret = bq24257_field_write(bq, F_CLR_VDP, 1);
 578         if (ret < 0)
 579                 goto error;
 580 
 581         dev_dbg(bq->dev, "port/loop = %d/%d -> iilimit = %d\n",
 582                 port_type, loop_status, new_iilimit[port_type]);
 583 
 584         return 0;
 585 
 586 error:
 587         dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
 588         return ret;
 589 }
 590 
 591 static void bq24257_iilimit_setup_work(struct work_struct *work)
 592 {
 593         struct bq24257_device *bq = container_of(work, struct bq24257_device,
 594                                                  iilimit_setup_work.work);
 595 
 596         bq24257_iilimit_autoset(bq);
 597 }
 598 
 599 static void bq24257_handle_state_change(struct bq24257_device *bq,
 600                                         struct bq24257_state *new_state)
 601 {
 602         int ret;
 603         struct bq24257_state old_state;
 604 
 605         mutex_lock(&bq->lock);
 606         old_state = bq->state;
 607         mutex_unlock(&bq->lock);
 608 
 609         /*
 610          * Handle BQ2425x state changes observing whether the D+/D- based input
 611          * current limit autoset functionality is enabled.
 612          */
 613         if (!new_state->power_good) {
 614                 dev_dbg(bq->dev, "Power removed\n");
 615                 if (bq->iilimit_autoset_enable) {
 616                         cancel_delayed_work_sync(&bq->iilimit_setup_work);
 617 
 618                         /* activate D+/D- port detection algorithm */
 619                         ret = bq24257_field_write(bq, F_DPDM_EN, 1);
 620                         if (ret < 0)
 621                                 goto error;
 622                 }
 623                 /*
 624                  * When power is removed always return to the default input
 625                  * current limit as configured during probe.
 626                  */
 627                 ret = bq24257_field_write(bq, F_IILIMIT, bq->init_data.iilimit);
 628                 if (ret < 0)
 629                         goto error;
 630         } else if (!old_state.power_good) {
 631                 dev_dbg(bq->dev, "Power inserted\n");
 632 
 633                 if (bq->iilimit_autoset_enable)
 634                         /* configure input current limit */
 635                         schedule_delayed_work(&bq->iilimit_setup_work,
 636                                       msecs_to_jiffies(BQ24257_ILIM_SET_DELAY));
 637         } else if (new_state->fault == FAULT_NO_BAT) {
 638                 dev_warn(bq->dev, "Battery removed\n");
 639         } else if (new_state->fault == FAULT_TIMER) {
 640                 dev_err(bq->dev, "Safety timer expired! Battery dead?\n");
 641         }
 642 
 643         return;
 644 
 645 error:
 646         dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
 647 }
 648 
 649 static irqreturn_t bq24257_irq_handler_thread(int irq, void *private)
 650 {
 651         int ret;
 652         struct bq24257_device *bq = private;
 653         struct bq24257_state state;
 654 
 655         ret = bq24257_get_chip_state(bq, &state);
 656         if (ret < 0)
 657                 return IRQ_HANDLED;
 658 
 659         if (!bq24257_state_changed(bq, &state))
 660                 return IRQ_HANDLED;
 661 
 662         dev_dbg(bq->dev, "irq(state changed): status/fault/pg = %d/%d/%d\n",
 663                 state.status, state.fault, state.power_good);
 664 
 665         bq24257_handle_state_change(bq, &state);
 666 
 667         mutex_lock(&bq->lock);
 668         bq->state = state;
 669         mutex_unlock(&bq->lock);
 670 
 671         power_supply_changed(bq->charger);
 672 
 673         return IRQ_HANDLED;
 674 }
 675 
 676 static int bq24257_hw_init(struct bq24257_device *bq)
 677 {
 678         int ret;
 679         int i;
 680         struct bq24257_state state;
 681 
 682         const struct {
 683                 int field;
 684                 u32 value;
 685         } init_data[] = {
 686                 {F_ICHG, bq->init_data.ichg},
 687                 {F_VBAT, bq->init_data.vbat},
 688                 {F_ITERM, bq->init_data.iterm},
 689                 {F_VOVP, bq->init_data.vovp},
 690                 {F_VINDPM, bq->init_data.vindpm},
 691         };
 692 
 693         /*
 694          * Disable the watchdog timer to prevent the IC from going back to
 695          * default settings after 50 seconds of I2C inactivity.
 696          */
 697         ret = bq24257_field_write(bq, F_WD_EN, 0);
 698         if (ret < 0)
 699                 return ret;
 700 
 701         /* configure the charge currents and voltages */
 702         for (i = 0; i < ARRAY_SIZE(init_data); i++) {
 703                 ret = bq24257_field_write(bq, init_data[i].field,
 704                                           init_data[i].value);
 705                 if (ret < 0)
 706                         return ret;
 707         }
 708 
 709         ret = bq24257_get_chip_state(bq, &state);
 710         if (ret < 0)
 711                 return ret;
 712 
 713         mutex_lock(&bq->lock);
 714         bq->state = state;
 715         mutex_unlock(&bq->lock);
 716 
 717         if (!bq->iilimit_autoset_enable) {
 718                 dev_dbg(bq->dev, "manually setting iilimit = %u\n",
 719                         bq->init_data.iilimit);
 720 
 721                 /* program fixed input current limit */
 722                 ret = bq24257_field_write(bq, F_IILIMIT,
 723                                           bq->init_data.iilimit);
 724                 if (ret < 0)
 725                         return ret;
 726         } else if (!state.power_good)
 727                 /* activate D+/D- detection algorithm */
 728                 ret = bq24257_field_write(bq, F_DPDM_EN, 1);
 729         else if (state.fault != FAULT_NO_BAT)
 730                 ret = bq24257_iilimit_autoset(bq);
 731 
 732         return ret;
 733 }
 734 
 735 static enum power_supply_property bq24257_power_supply_props[] = {
 736         POWER_SUPPLY_PROP_MANUFACTURER,
 737         POWER_SUPPLY_PROP_MODEL_NAME,
 738         POWER_SUPPLY_PROP_STATUS,
 739         POWER_SUPPLY_PROP_ONLINE,
 740         POWER_SUPPLY_PROP_HEALTH,
 741         POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
 742         POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
 743         POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
 744         POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
 745         POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
 746         POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
 747 };
 748 
 749 static char *bq24257_charger_supplied_to[] = {
 750         "main-battery",
 751 };
 752 
 753 static const struct power_supply_desc bq24257_power_supply_desc = {
 754         .name = "bq24257-charger",
 755         .type = POWER_SUPPLY_TYPE_USB,
 756         .properties = bq24257_power_supply_props,
 757         .num_properties = ARRAY_SIZE(bq24257_power_supply_props),
 758         .get_property = bq24257_power_supply_get_property,
 759         .set_property = bq24257_power_supply_set_property,
 760         .property_is_writeable = bq24257_power_supply_property_is_writeable,
 761 };
 762 
 763 static ssize_t bq24257_show_ovp_voltage(struct device *dev,
 764                                         struct device_attribute *attr,
 765                                         char *buf)
 766 {
 767         struct power_supply *psy = dev_get_drvdata(dev);
 768         struct bq24257_device *bq = power_supply_get_drvdata(psy);
 769 
 770         return scnprintf(buf, PAGE_SIZE, "%u\n",
 771                          bq24257_vovp_map[bq->init_data.vovp]);
 772 }
 773 
 774 static ssize_t bq24257_show_in_dpm_voltage(struct device *dev,
 775                                            struct device_attribute *attr,
 776                                            char *buf)
 777 {
 778         struct power_supply *psy = dev_get_drvdata(dev);
 779         struct bq24257_device *bq = power_supply_get_drvdata(psy);
 780 
 781         return scnprintf(buf, PAGE_SIZE, "%u\n",
 782                          bq24257_vindpm_map[bq->init_data.vindpm]);
 783 }
 784 
 785 static ssize_t bq24257_sysfs_show_enable(struct device *dev,
 786                                          struct device_attribute *attr,
 787                                          char *buf)
 788 {
 789         struct power_supply *psy = dev_get_drvdata(dev);
 790         struct bq24257_device *bq = power_supply_get_drvdata(psy);
 791         int ret;
 792 
 793         if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
 794                 ret = bq24257_field_read(bq, F_HZ_MODE);
 795         else if (strcmp(attr->attr.name, "sysoff_enable") == 0)
 796                 ret = bq24257_field_read(bq, F_SYSOFF);
 797         else
 798                 return -EINVAL;
 799 
 800         if (ret < 0)
 801                 return ret;
 802 
 803         return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
 804 }
 805 
 806 static ssize_t bq24257_sysfs_set_enable(struct device *dev,
 807                                         struct device_attribute *attr,
 808                                         const char *buf,
 809                                         size_t count)
 810 {
 811         struct power_supply *psy = dev_get_drvdata(dev);
 812         struct bq24257_device *bq = power_supply_get_drvdata(psy);
 813         long val;
 814         int ret;
 815 
 816         if (kstrtol(buf, 10, &val) < 0)
 817                 return -EINVAL;
 818 
 819         if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
 820                 ret = bq24257_field_write(bq, F_HZ_MODE, (bool)val);
 821         else if (strcmp(attr->attr.name, "sysoff_enable") == 0)
 822                 ret = bq24257_field_write(bq, F_SYSOFF, (bool)val);
 823         else
 824                 return -EINVAL;
 825 
 826         if (ret < 0)
 827                 return ret;
 828 
 829         return count;
 830 }
 831 
 832 static DEVICE_ATTR(ovp_voltage, S_IRUGO, bq24257_show_ovp_voltage, NULL);
 833 static DEVICE_ATTR(in_dpm_voltage, S_IRUGO, bq24257_show_in_dpm_voltage, NULL);
 834 static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
 835                    bq24257_sysfs_show_enable, bq24257_sysfs_set_enable);
 836 static DEVICE_ATTR(sysoff_enable, S_IWUSR | S_IRUGO,
 837                    bq24257_sysfs_show_enable, bq24257_sysfs_set_enable);
 838 
 839 static struct attribute *bq24257_charger_sysfs_attrs[] = {
 840         &dev_attr_ovp_voltage.attr,
 841         &dev_attr_in_dpm_voltage.attr,
 842         &dev_attr_high_impedance_enable.attr,
 843         &dev_attr_sysoff_enable.attr,
 844         NULL,
 845 };
 846 
 847 ATTRIBUTE_GROUPS(bq24257_charger_sysfs);
 848 
 849 static int bq24257_power_supply_init(struct bq24257_device *bq)
 850 {
 851         struct power_supply_config psy_cfg = { .drv_data = bq, };
 852 
 853         psy_cfg.attr_grp = bq24257_charger_sysfs_groups;
 854         psy_cfg.supplied_to = bq24257_charger_supplied_to;
 855         psy_cfg.num_supplicants = ARRAY_SIZE(bq24257_charger_supplied_to);
 856 
 857         bq->charger = devm_power_supply_register(bq->dev,
 858                                                  &bq24257_power_supply_desc,
 859                                                  &psy_cfg);
 860 
 861         return PTR_ERR_OR_ZERO(bq->charger);
 862 }
 863 
 864 static void bq24257_pg_gpio_probe(struct bq24257_device *bq)
 865 {
 866         bq->pg = devm_gpiod_get_optional(bq->dev, BQ24257_PG_GPIO, GPIOD_IN);
 867 
 868         if (PTR_ERR(bq->pg) == -EPROBE_DEFER) {
 869                 dev_info(bq->dev, "probe retry requested for PG pin\n");
 870                 return;
 871         } else if (IS_ERR(bq->pg)) {
 872                 dev_err(bq->dev, "error probing PG pin\n");
 873                 bq->pg = NULL;
 874                 return;
 875         }
 876 
 877         if (bq->pg)
 878                 dev_dbg(bq->dev, "probed PG pin = %d\n", desc_to_gpio(bq->pg));
 879 }
 880 
 881 static int bq24257_fw_probe(struct bq24257_device *bq)
 882 {
 883         int ret;
 884         u32 property;
 885 
 886         /* Required properties */
 887         ret = device_property_read_u32(bq->dev, "ti,charge-current", &property);
 888         if (ret < 0)
 889                 return ret;
 890 
 891         bq->init_data.ichg = bq24257_find_idx(property, bq24257_ichg_map,
 892                                               BQ24257_ICHG_MAP_SIZE);
 893 
 894         ret = device_property_read_u32(bq->dev, "ti,battery-regulation-voltage",
 895                                        &property);
 896         if (ret < 0)
 897                 return ret;
 898 
 899         bq->init_data.vbat = bq24257_find_idx(property, bq24257_vbat_map,
 900                                               BQ24257_VBAT_MAP_SIZE);
 901 
 902         ret = device_property_read_u32(bq->dev, "ti,termination-current",
 903                                        &property);
 904         if (ret < 0)
 905                 return ret;
 906 
 907         bq->init_data.iterm = bq24257_find_idx(property, bq24257_iterm_map,
 908                                                BQ24257_ITERM_MAP_SIZE);
 909 
 910         /* Optional properties. If not provided use reasonable default. */
 911         ret = device_property_read_u32(bq->dev, "ti,current-limit",
 912                                        &property);
 913         if (ret < 0) {
 914                 bq->iilimit_autoset_enable = true;
 915 
 916                 /*
 917                  * Explicitly set a default value which will be needed for
 918                  * devices that don't support the automatic setting of the input
 919                  * current limit through the charger type detection mechanism.
 920                  */
 921                 bq->init_data.iilimit = IILIMIT_500;
 922         } else
 923                 bq->init_data.iilimit =
 924                                 bq24257_find_idx(property,
 925                                                  bq24257_iilimit_map,
 926                                                  BQ24257_IILIMIT_MAP_SIZE);
 927 
 928         ret = device_property_read_u32(bq->dev, "ti,ovp-voltage",
 929                                        &property);
 930         if (ret < 0)
 931                 bq->init_data.vovp = VOVP_6500;
 932         else
 933                 bq->init_data.vovp = bq24257_find_idx(property,
 934                                                       bq24257_vovp_map,
 935                                                       BQ24257_VOVP_MAP_SIZE);
 936 
 937         ret = device_property_read_u32(bq->dev, "ti,in-dpm-voltage",
 938                                        &property);
 939         if (ret < 0)
 940                 bq->init_data.vindpm = VINDPM_4360;
 941         else
 942                 bq->init_data.vindpm =
 943                                 bq24257_find_idx(property,
 944                                                  bq24257_vindpm_map,
 945                                                  BQ24257_VINDPM_MAP_SIZE);
 946 
 947         return 0;
 948 }
 949 
 950 static int bq24257_probe(struct i2c_client *client,
 951                          const struct i2c_device_id *id)
 952 {
 953         struct i2c_adapter *adapter = client->adapter;
 954         struct device *dev = &client->dev;
 955         const struct acpi_device_id *acpi_id;
 956         struct bq24257_device *bq;
 957         int ret;
 958         int i;
 959 
 960         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
 961                 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
 962                 return -ENODEV;
 963         }
 964 
 965         bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
 966         if (!bq)
 967                 return -ENOMEM;
 968 
 969         bq->client = client;
 970         bq->dev = dev;
 971 
 972         if (ACPI_HANDLE(dev)) {
 973                 acpi_id = acpi_match_device(dev->driver->acpi_match_table,
 974                                             &client->dev);
 975                 if (!acpi_id) {
 976                         dev_err(dev, "Failed to match ACPI device\n");
 977                         return -ENODEV;
 978                 }
 979                 bq->chip = (enum bq2425x_chip)acpi_id->driver_data;
 980         } else {
 981                 bq->chip = (enum bq2425x_chip)id->driver_data;
 982         }
 983 
 984         mutex_init(&bq->lock);
 985 
 986         bq->rmap = devm_regmap_init_i2c(client, &bq24257_regmap_config);
 987         if (IS_ERR(bq->rmap)) {
 988                 dev_err(dev, "failed to allocate register map\n");
 989                 return PTR_ERR(bq->rmap);
 990         }
 991 
 992         for (i = 0; i < ARRAY_SIZE(bq24257_reg_fields); i++) {
 993                 const struct reg_field *reg_fields = bq24257_reg_fields;
 994 
 995                 bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
 996                                                              reg_fields[i]);
 997                 if (IS_ERR(bq->rmap_fields[i])) {
 998                         dev_err(dev, "cannot allocate regmap field\n");
 999                         return PTR_ERR(bq->rmap_fields[i]);
1000                 }
1001         }
1002 
1003         i2c_set_clientdata(client, bq);
1004 
1005         if (!dev->platform_data) {
1006                 ret = bq24257_fw_probe(bq);
1007                 if (ret < 0) {
1008                         dev_err(dev, "Cannot read device properties.\n");
1009                         return ret;
1010                 }
1011         } else {
1012                 return -ENODEV;
1013         }
1014 
1015         /*
1016          * The BQ24250 doesn't support the D+/D- based charger type detection
1017          * used for the automatic setting of the input current limit setting so
1018          * explicitly disable that feature.
1019          */
1020         if (bq->chip == BQ24250)
1021                 bq->iilimit_autoset_enable = false;
1022 
1023         if (bq->iilimit_autoset_enable)
1024                 INIT_DELAYED_WORK(&bq->iilimit_setup_work,
1025                                   bq24257_iilimit_setup_work);
1026 
1027         /*
1028          * The BQ24250 doesn't have a dedicated Power Good (PG) pin so let's
1029          * not probe for it and instead use a SW-based approach to determine
1030          * the PG state. We also use a SW-based approach for all other devices
1031          * if the PG pin is either not defined or can't be probed.
1032          */
1033         if (bq->chip != BQ24250)
1034                 bq24257_pg_gpio_probe(bq);
1035 
1036         if (PTR_ERR(bq->pg) == -EPROBE_DEFER)
1037                 return PTR_ERR(bq->pg);
1038         else if (!bq->pg)
1039                 dev_info(bq->dev, "using SW-based power-good detection\n");
1040 
1041         /* reset all registers to defaults */
1042         ret = bq24257_field_write(bq, F_RESET, 1);
1043         if (ret < 0)
1044                 return ret;
1045 
1046         /*
1047          * Put the RESET bit back to 0, in cache. For some reason the HW always
1048          * returns 1 on this bit, so this is the only way to avoid resetting the
1049          * chip every time we update another field in this register.
1050          */
1051         ret = bq24257_field_write(bq, F_RESET, 0);
1052         if (ret < 0)
1053                 return ret;
1054 
1055         ret = bq24257_hw_init(bq);
1056         if (ret < 0) {
1057                 dev_err(dev, "Cannot initialize the chip.\n");
1058                 return ret;
1059         }
1060 
1061         ret = bq24257_power_supply_init(bq);
1062         if (ret < 0) {
1063                 dev_err(dev, "Failed to register power supply\n");
1064                 return ret;
1065         }
1066 
1067         ret = devm_request_threaded_irq(dev, client->irq, NULL,
1068                                         bq24257_irq_handler_thread,
1069                                         IRQF_TRIGGER_FALLING |
1070                                         IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1071                                         bq2425x_chip_name[bq->chip], bq);
1072         if (ret) {
1073                 dev_err(dev, "Failed to request IRQ #%d\n", client->irq);
1074                 return ret;
1075         }
1076 
1077         return 0;
1078 }
1079 
1080 static int bq24257_remove(struct i2c_client *client)
1081 {
1082         struct bq24257_device *bq = i2c_get_clientdata(client);
1083 
1084         if (bq->iilimit_autoset_enable)
1085                 cancel_delayed_work_sync(&bq->iilimit_setup_work);
1086 
1087         bq24257_field_write(bq, F_RESET, 1); /* reset to defaults */
1088 
1089         return 0;
1090 }
1091 
1092 #ifdef CONFIG_PM_SLEEP
1093 static int bq24257_suspend(struct device *dev)
1094 {
1095         struct bq24257_device *bq = dev_get_drvdata(dev);
1096         int ret = 0;
1097 
1098         if (bq->iilimit_autoset_enable)
1099                 cancel_delayed_work_sync(&bq->iilimit_setup_work);
1100 
1101         /* reset all registers to default (and activate standalone mode) */
1102         ret = bq24257_field_write(bq, F_RESET, 1);
1103         if (ret < 0)
1104                 dev_err(bq->dev, "Cannot reset chip to standalone mode.\n");
1105 
1106         return ret;
1107 }
1108 
1109 static int bq24257_resume(struct device *dev)
1110 {
1111         int ret;
1112         struct bq24257_device *bq = dev_get_drvdata(dev);
1113 
1114         ret = regcache_drop_region(bq->rmap, BQ24257_REG_1, BQ24257_REG_7);
1115         if (ret < 0)
1116                 return ret;
1117 
1118         ret = bq24257_field_write(bq, F_RESET, 0);
1119         if (ret < 0)
1120                 return ret;
1121 
1122         ret = bq24257_hw_init(bq);
1123         if (ret < 0) {
1124                 dev_err(bq->dev, "Cannot init chip after resume.\n");
1125                 return ret;
1126         }
1127 
1128         /* signal userspace, maybe state changed while suspended */
1129         power_supply_changed(bq->charger);
1130 
1131         return 0;
1132 }
1133 #endif
1134 
1135 static const struct dev_pm_ops bq24257_pm = {
1136         SET_SYSTEM_SLEEP_PM_OPS(bq24257_suspend, bq24257_resume)
1137 };
1138 
1139 static const struct i2c_device_id bq24257_i2c_ids[] = {
1140         { "bq24250", BQ24250 },
1141         { "bq24251", BQ24251 },
1142         { "bq24257", BQ24257 },
1143         {},
1144 };
1145 MODULE_DEVICE_TABLE(i2c, bq24257_i2c_ids);
1146 
1147 static const struct of_device_id bq24257_of_match[] = {
1148         { .compatible = "ti,bq24250", },
1149         { .compatible = "ti,bq24251", },
1150         { .compatible = "ti,bq24257", },
1151         { },
1152 };
1153 MODULE_DEVICE_TABLE(of, bq24257_of_match);
1154 
1155 static const struct acpi_device_id bq24257_acpi_match[] = {
1156         { "BQ242500", BQ24250 },
1157         { "BQ242510", BQ24251 },
1158         { "BQ242570", BQ24257 },
1159         {},
1160 };
1161 MODULE_DEVICE_TABLE(acpi, bq24257_acpi_match);
1162 
1163 static struct i2c_driver bq24257_driver = {
1164         .driver = {
1165                 .name = "bq24257-charger",
1166                 .of_match_table = of_match_ptr(bq24257_of_match),
1167                 .acpi_match_table = ACPI_PTR(bq24257_acpi_match),
1168                 .pm = &bq24257_pm,
1169         },
1170         .probe = bq24257_probe,
1171         .remove = bq24257_remove,
1172         .id_table = bq24257_i2c_ids,
1173 };
1174 module_i2c_driver(bq24257_driver);
1175 
1176 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1177 MODULE_DESCRIPTION("bq24257 charger driver");
1178 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */