root/drivers/power/supply/bq25890_charger.c

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

DEFINITIONS

This source file includes following definitions.
  1. bq25890_field_read
  2. bq25890_field_write
  3. bq25890_find_idx
  4. bq25890_find_val
  5. bq25890_power_supply_get_property
  6. bq25890_get_chip_state
  7. bq25890_state_changed
  8. bq25890_handle_state_change
  9. bq25890_irq_handler_thread
  10. bq25890_chip_reset
  11. bq25890_hw_init
  12. bq25890_power_supply_init
  13. bq25890_usb_work
  14. bq25890_usb_notifier
  15. bq25890_irq_probe
  16. bq25890_fw_read_u32_props
  17. bq25890_fw_probe
  18. bq25890_probe
  19. bq25890_remove
  20. bq25890_suspend
  21. bq25890_resume

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

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