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/power/bq25890_charger.h> 12 #include <linux/regmap.h> 13 #include <linux/regulator/driver.h> 14 #include <linux/types.h> 15 #include <linux/gpio/consumer.h> 16 #include <linux/interrupt.h> 17 #include <linux/delay.h> 18 #include <linux/usb/phy.h> 19 20 #include <linux/acpi.h> 21 #include <linux/of.h> 22 23 #define BQ25890_MANUFACTURER "Texas Instruments" 24 #define BQ25890_IRQ_PIN "bq25890_irq" 25 26 #define BQ25890_ID 3 27 #define BQ25895_ID 7 28 #define BQ25896_ID 0 29 30 #define PUMP_EXPRESS_START_DELAY (5 * HZ) 31 #define PUMP_EXPRESS_MAX_TRIES 6 32 #define PUMP_EXPRESS_VBUS_MARGIN_uV 1000000 33 34 enum bq25890_chip_version { 35 BQ25890, 36 BQ25892, 37 BQ25895, 38 BQ25896, 39 }; 40 41 static const char *const bq25890_chip_name[] = { 42 "BQ25890", 43 "BQ25892", 44 "BQ25895", 45 "BQ25896", 46 }; 47 48 enum bq25890_fields { 49 F_EN_HIZ, F_EN_ILIM, F_IINLIM, /* Reg00 */ 50 F_BHOT, F_BCOLD, F_VINDPM_OFS, /* Reg01 */ 51 F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN, 52 F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN, /* Reg02 */ 53 F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN, 54 F_MIN_VBAT_SEL, /* Reg03 */ 55 F_PUMPX_EN, F_ICHG, /* Reg04 */ 56 F_IPRECHG, F_ITERM, /* Reg05 */ 57 F_VREG, F_BATLOWV, F_VRECHG, /* Reg06 */ 58 F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR, 59 F_JEITA_ISET, /* Reg07 */ 60 F_BATCMP, F_VCLAMP, F_TREG, /* Reg08 */ 61 F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET, 62 F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN, /* Reg09 */ 63 F_BOOSTV, F_PFM_OTG_DIS, F_BOOSTI, /* Reg0A */ 64 F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_0B_RSVD, 65 F_VSYS_STAT, /* Reg0B */ 66 F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT, 67 F_NTC_FAULT, /* Reg0C */ 68 F_FORCE_VINDPM, F_VINDPM, /* Reg0D */ 69 F_THERM_STAT, F_BATV, /* Reg0E */ 70 F_SYSV, /* Reg0F */ 71 F_TSPCT, /* Reg10 */ 72 F_VBUS_GD, F_VBUSV, /* Reg11 */ 73 F_ICHGR, /* Reg12 */ 74 F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM, /* Reg13 */ 75 F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV, /* Reg14 */ 76 77 F_MAX_FIELDS 78 }; 79 80 /* initial field values, converted to register values */ 81 struct bq25890_init_data { 82 u8 ichg; /* charge current */ 83 u8 vreg; /* regulation voltage */ 84 u8 iterm; /* termination current */ 85 u8 iprechg; /* precharge current */ 86 u8 sysvmin; /* minimum system voltage limit */ 87 u8 boostv; /* boost regulation voltage */ 88 u8 boosti; /* boost current limit */ 89 u8 boostf; /* boost frequency */ 90 u8 ilim_en; /* enable ILIM pin */ 91 u8 treg; /* thermal regulation threshold */ 92 u8 rbatcomp; /* IBAT sense resistor value */ 93 u8 vclamp; /* IBAT compensation voltage limit */ 94 }; 95 96 struct bq25890_state { 97 u8 online; 98 u8 chrg_status; 99 u8 chrg_fault; 100 u8 vsys_status; 101 u8 boost_fault; 102 u8 bat_fault; 103 u8 ntc_fault; 104 }; 105 106 struct bq25890_device { 107 struct i2c_client *client; 108 struct device *dev; 109 struct power_supply *charger; 110 111 struct usb_phy *usb_phy; 112 struct notifier_block usb_nb; 113 struct work_struct usb_work; 114 struct delayed_work pump_express_work; 115 unsigned long usb_event; 116 117 struct regmap *rmap; 118 struct regmap_field *rmap_fields[F_MAX_FIELDS]; 119 120 bool skip_reset; 121 bool read_back_init_data; 122 u32 pump_express_vbus_max; 123 enum bq25890_chip_version chip_version; 124 struct bq25890_init_data init_data; 125 struct bq25890_state state; 126 127 struct mutex lock; /* protect state data */ 128 }; 129 130 static const struct regmap_range bq25890_readonly_reg_ranges[] = { 131 regmap_reg_range(0x0b, 0x0c), 132 regmap_reg_range(0x0e, 0x13), 133 }; 134 135 static const struct regmap_access_table bq25890_writeable_regs = { 136 .no_ranges = bq25890_readonly_reg_ranges, 137 .n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges), 138 }; 139 140 static const struct regmap_range bq25890_volatile_reg_ranges[] = { 141 regmap_reg_range(0x00, 0x00), 142 regmap_reg_range(0x02, 0x02), 143 regmap_reg_range(0x09, 0x09), 144 regmap_reg_range(0x0b, 0x14), 145 }; 146 147 static const struct regmap_access_table bq25890_volatile_regs = { 148 .yes_ranges = bq25890_volatile_reg_ranges, 149 .n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges), 150 }; 151 152 static const struct regmap_config bq25890_regmap_config = { 153 .reg_bits = 8, 154 .val_bits = 8, 155 156 .max_register = 0x14, 157 .cache_type = REGCACHE_RBTREE, 158 159 .wr_table = &bq25890_writeable_regs, 160 .volatile_table = &bq25890_volatile_regs, 161 }; 162 163 static const struct reg_field bq25890_reg_fields[] = { 164 /* REG00 */ 165 [F_EN_HIZ] = REG_FIELD(0x00, 7, 7), 166 [F_EN_ILIM] = REG_FIELD(0x00, 6, 6), 167 [F_IINLIM] = REG_FIELD(0x00, 0, 5), 168 /* REG01 */ 169 [F_BHOT] = REG_FIELD(0x01, 6, 7), 170 [F_BCOLD] = REG_FIELD(0x01, 5, 5), 171 [F_VINDPM_OFS] = REG_FIELD(0x01, 0, 4), 172 /* REG02 */ 173 [F_CONV_START] = REG_FIELD(0x02, 7, 7), 174 [F_CONV_RATE] = REG_FIELD(0x02, 6, 6), 175 [F_BOOSTF] = REG_FIELD(0x02, 5, 5), 176 [F_ICO_EN] = REG_FIELD(0x02, 4, 4), 177 [F_HVDCP_EN] = REG_FIELD(0x02, 3, 3), // reserved on BQ25896 178 [F_MAXC_EN] = REG_FIELD(0x02, 2, 2), // reserved on BQ25896 179 [F_FORCE_DPM] = REG_FIELD(0x02, 1, 1), 180 [F_AUTO_DPDM_EN] = REG_FIELD(0x02, 0, 0), 181 /* REG03 */ 182 [F_BAT_LOAD_EN] = REG_FIELD(0x03, 7, 7), 183 [F_WD_RST] = REG_FIELD(0x03, 6, 6), 184 [F_OTG_CFG] = REG_FIELD(0x03, 5, 5), 185 [F_CHG_CFG] = REG_FIELD(0x03, 4, 4), 186 [F_SYSVMIN] = REG_FIELD(0x03, 1, 3), 187 [F_MIN_VBAT_SEL] = REG_FIELD(0x03, 0, 0), // BQ25896 only 188 /* REG04 */ 189 [F_PUMPX_EN] = REG_FIELD(0x04, 7, 7), 190 [F_ICHG] = REG_FIELD(0x04, 0, 6), 191 /* REG05 */ 192 [F_IPRECHG] = REG_FIELD(0x05, 4, 7), 193 [F_ITERM] = REG_FIELD(0x05, 0, 3), 194 /* REG06 */ 195 [F_VREG] = REG_FIELD(0x06, 2, 7), 196 [F_BATLOWV] = REG_FIELD(0x06, 1, 1), 197 [F_VRECHG] = REG_FIELD(0x06, 0, 0), 198 /* REG07 */ 199 [F_TERM_EN] = REG_FIELD(0x07, 7, 7), 200 [F_STAT_DIS] = REG_FIELD(0x07, 6, 6), 201 [F_WD] = REG_FIELD(0x07, 4, 5), 202 [F_TMR_EN] = REG_FIELD(0x07, 3, 3), 203 [F_CHG_TMR] = REG_FIELD(0x07, 1, 2), 204 [F_JEITA_ISET] = REG_FIELD(0x07, 0, 0), // reserved on BQ25895 205 /* REG08 */ 206 [F_BATCMP] = REG_FIELD(0x08, 5, 7), 207 [F_VCLAMP] = REG_FIELD(0x08, 2, 4), 208 [F_TREG] = REG_FIELD(0x08, 0, 1), 209 /* REG09 */ 210 [F_FORCE_ICO] = REG_FIELD(0x09, 7, 7), 211 [F_TMR2X_EN] = REG_FIELD(0x09, 6, 6), 212 [F_BATFET_DIS] = REG_FIELD(0x09, 5, 5), 213 [F_JEITA_VSET] = REG_FIELD(0x09, 4, 4), // reserved on BQ25895 214 [F_BATFET_DLY] = REG_FIELD(0x09, 3, 3), 215 [F_BATFET_RST_EN] = REG_FIELD(0x09, 2, 2), 216 [F_PUMPX_UP] = REG_FIELD(0x09, 1, 1), 217 [F_PUMPX_DN] = REG_FIELD(0x09, 0, 0), 218 /* REG0A */ 219 [F_BOOSTV] = REG_FIELD(0x0A, 4, 7), 220 [F_BOOSTI] = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895 221 [F_PFM_OTG_DIS] = REG_FIELD(0x0A, 3, 3), // BQ25896 only 222 /* REG0B */ 223 [F_VBUS_STAT] = REG_FIELD(0x0B, 5, 7), 224 [F_CHG_STAT] = REG_FIELD(0x0B, 3, 4), 225 [F_PG_STAT] = REG_FIELD(0x0B, 2, 2), 226 [F_SDP_STAT] = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896 227 [F_VSYS_STAT] = REG_FIELD(0x0B, 0, 0), 228 /* REG0C */ 229 [F_WD_FAULT] = REG_FIELD(0x0C, 7, 7), 230 [F_BOOST_FAULT] = REG_FIELD(0x0C, 6, 6), 231 [F_CHG_FAULT] = REG_FIELD(0x0C, 4, 5), 232 [F_BAT_FAULT] = REG_FIELD(0x0C, 3, 3), 233 [F_NTC_FAULT] = REG_FIELD(0x0C, 0, 2), 234 /* REG0D */ 235 [F_FORCE_VINDPM] = REG_FIELD(0x0D, 7, 7), 236 [F_VINDPM] = REG_FIELD(0x0D, 0, 6), 237 /* REG0E */ 238 [F_THERM_STAT] = REG_FIELD(0x0E, 7, 7), 239 [F_BATV] = REG_FIELD(0x0E, 0, 6), 240 /* REG0F */ 241 [F_SYSV] = REG_FIELD(0x0F, 0, 6), 242 /* REG10 */ 243 [F_TSPCT] = REG_FIELD(0x10, 0, 6), 244 /* REG11 */ 245 [F_VBUS_GD] = REG_FIELD(0x11, 7, 7), 246 [F_VBUSV] = REG_FIELD(0x11, 0, 6), 247 /* REG12 */ 248 [F_ICHGR] = REG_FIELD(0x12, 0, 6), 249 /* REG13 */ 250 [F_VDPM_STAT] = REG_FIELD(0x13, 7, 7), 251 [F_IDPM_STAT] = REG_FIELD(0x13, 6, 6), 252 [F_IDPM_LIM] = REG_FIELD(0x13, 0, 5), 253 /* REG14 */ 254 [F_REG_RST] = REG_FIELD(0x14, 7, 7), 255 [F_ICO_OPTIMIZED] = REG_FIELD(0x14, 6, 6), 256 [F_PN] = REG_FIELD(0x14, 3, 5), 257 [F_TS_PROFILE] = REG_FIELD(0x14, 2, 2), 258 [F_DEV_REV] = REG_FIELD(0x14, 0, 1) 259 }; 260 261 /* 262 * Most of the val -> idx conversions can be computed, given the minimum, 263 * maximum and the step between values. For the rest of conversions, we use 264 * lookup tables. 265 */ 266 enum bq25890_table_ids { 267 /* range tables */ 268 TBL_ICHG, 269 TBL_ITERM, 270 TBL_IINLIM, 271 TBL_VREG, 272 TBL_BOOSTV, 273 TBL_SYSVMIN, 274 TBL_VBUSV, 275 TBL_VBATCOMP, 276 TBL_RBATCOMP, 277 278 /* lookup tables */ 279 TBL_TREG, 280 TBL_BOOSTI, 281 TBL_TSPCT, 282 }; 283 284 /* Thermal Regulation Threshold lookup table, in degrees Celsius */ 285 static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 }; 286 287 #define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl) 288 289 /* Boost mode current limit lookup table, in uA */ 290 static const u32 bq25890_boosti_tbl[] = { 291 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000 292 }; 293 294 #define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl) 295 296 /* NTC 10K temperature lookup table in tenths of a degree */ 297 static const u32 bq25890_tspct_tbl[] = { 298 850, 840, 830, 820, 810, 800, 790, 780, 299 770, 760, 750, 740, 730, 720, 710, 700, 300 690, 685, 680, 675, 670, 660, 650, 645, 301 640, 630, 620, 615, 610, 600, 590, 585, 302 580, 570, 565, 560, 550, 540, 535, 530, 303 520, 515, 510, 500, 495, 490, 480, 475, 304 470, 460, 455, 450, 440, 435, 430, 425, 305 420, 410, 405, 400, 390, 385, 380, 370, 306 365, 360, 355, 350, 340, 335, 330, 320, 307 310, 305, 300, 290, 285, 280, 275, 270, 308 260, 250, 245, 240, 230, 225, 220, 210, 309 205, 200, 190, 180, 175, 170, 160, 150, 310 145, 140, 130, 120, 115, 110, 100, 90, 311 80, 70, 60, 50, 40, 30, 20, 10, 312 0, -10, -20, -30, -40, -60, -70, -80, 313 -90, -10, -120, -140, -150, -170, -190, -210, 314 }; 315 316 #define BQ25890_TSPCT_TBL_SIZE ARRAY_SIZE(bq25890_tspct_tbl) 317 318 struct bq25890_range { 319 u32 min; 320 u32 max; 321 u32 step; 322 }; 323 324 struct bq25890_lookup { 325 const u32 *tbl; 326 u32 size; 327 }; 328 329 static const union { 330 struct bq25890_range rt; 331 struct bq25890_lookup lt; 332 } bq25890_tables[] = { 333 /* range tables */ 334 /* TODO: BQ25896 has max ICHG 3008 mA */ 335 [TBL_ICHG] = { .rt = {0, 5056000, 64000} }, /* uA */ 336 [TBL_ITERM] = { .rt = {64000, 1024000, 64000} }, /* uA */ 337 [TBL_IINLIM] = { .rt = {100000, 3250000, 50000} }, /* uA */ 338 [TBL_VREG] = { .rt = {3840000, 4608000, 16000} }, /* uV */ 339 [TBL_BOOSTV] = { .rt = {4550000, 5510000, 64000} }, /* uV */ 340 [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} }, /* uV */ 341 [TBL_VBUSV] = { .rt = {2600000, 15300000, 100000} }, /* uV */ 342 [TBL_VBATCOMP] = { .rt = {0, 224000, 32000} }, /* uV */ 343 [TBL_RBATCOMP] = { .rt = {0, 140000, 20000} }, /* uOhm */ 344 345 /* lookup tables */ 346 [TBL_TREG] = { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} }, 347 [TBL_BOOSTI] = { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} }, 348 [TBL_TSPCT] = { .lt = {bq25890_tspct_tbl, BQ25890_TSPCT_TBL_SIZE} } 349 }; 350 351 static int bq25890_field_read(struct bq25890_device *bq, 352 enum bq25890_fields field_id) 353 { 354 int ret; 355 int val; 356 357 ret = regmap_field_read(bq->rmap_fields[field_id], &val); 358 if (ret < 0) 359 return ret; 360 361 return val; 362 } 363 364 static int bq25890_field_write(struct bq25890_device *bq, 365 enum bq25890_fields field_id, u8 val) 366 { 367 return regmap_field_write(bq->rmap_fields[field_id], val); 368 } 369 370 static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id) 371 { 372 u8 idx; 373 374 if (id >= TBL_TREG) { 375 const u32 *tbl = bq25890_tables[id].lt.tbl; 376 u32 tbl_size = bq25890_tables[id].lt.size; 377 378 for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++) 379 ; 380 } else { 381 const struct bq25890_range *rtbl = &bq25890_tables[id].rt; 382 u8 rtbl_size; 383 384 rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1; 385 386 for (idx = 1; 387 idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value); 388 idx++) 389 ; 390 } 391 392 return idx - 1; 393 } 394 395 static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id) 396 { 397 const struct bq25890_range *rtbl; 398 399 /* lookup table? */ 400 if (id >= TBL_TREG) 401 return bq25890_tables[id].lt.tbl[idx]; 402 403 /* range table */ 404 rtbl = &bq25890_tables[id].rt; 405 406 return (rtbl->min + idx * rtbl->step); 407 } 408 409 enum bq25890_status { 410 STATUS_NOT_CHARGING, 411 STATUS_PRE_CHARGING, 412 STATUS_FAST_CHARGING, 413 STATUS_TERMINATION_DONE, 414 }; 415 416 enum bq25890_chrg_fault { 417 CHRG_FAULT_NORMAL, 418 CHRG_FAULT_INPUT, 419 CHRG_FAULT_THERMAL_SHUTDOWN, 420 CHRG_FAULT_TIMER_EXPIRED, 421 }; 422 423 enum bq25890_ntc_fault { 424 NTC_FAULT_NORMAL = 0, 425 NTC_FAULT_WARM = 2, 426 NTC_FAULT_COOL = 3, 427 NTC_FAULT_COLD = 5, 428 NTC_FAULT_HOT = 6, 429 }; 430 431 static bool bq25890_is_adc_property(enum power_supply_property psp) 432 { 433 switch (psp) { 434 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 435 case POWER_SUPPLY_PROP_CURRENT_NOW: 436 case POWER_SUPPLY_PROP_TEMP: 437 return true; 438 439 default: 440 return false; 441 } 442 } 443 444 static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq); 445 446 static int bq25890_get_vbus_voltage(struct bq25890_device *bq) 447 { 448 int ret; 449 450 ret = bq25890_field_read(bq, F_VBUSV); 451 if (ret < 0) 452 return ret; 453 454 return bq25890_find_val(ret, TBL_VBUSV); 455 } 456 457 static int bq25890_power_supply_get_property(struct power_supply *psy, 458 enum power_supply_property psp, 459 union power_supply_propval *val) 460 { 461 struct bq25890_device *bq = power_supply_get_drvdata(psy); 462 struct bq25890_state state; 463 bool do_adc_conv; 464 int ret; 465 466 mutex_lock(&bq->lock); 467 /* update state in case we lost an interrupt */ 468 __bq25890_handle_irq(bq); 469 state = bq->state; 470 do_adc_conv = !state.online && bq25890_is_adc_property(psp); 471 if (do_adc_conv) 472 bq25890_field_write(bq, F_CONV_START, 1); 473 mutex_unlock(&bq->lock); 474 475 if (do_adc_conv) 476 regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START], 477 ret, !ret, 25000, 1000000); 478 479 switch (psp) { 480 case POWER_SUPPLY_PROP_STATUS: 481 if (!state.online) 482 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 483 else if (state.chrg_status == STATUS_NOT_CHARGING) 484 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 485 else if (state.chrg_status == STATUS_PRE_CHARGING || 486 state.chrg_status == STATUS_FAST_CHARGING) 487 val->intval = POWER_SUPPLY_STATUS_CHARGING; 488 else if (state.chrg_status == STATUS_TERMINATION_DONE) 489 val->intval = POWER_SUPPLY_STATUS_FULL; 490 else 491 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; 492 493 break; 494 495 case POWER_SUPPLY_PROP_CHARGE_TYPE: 496 if (!state.online || state.chrg_status == STATUS_NOT_CHARGING || 497 state.chrg_status == STATUS_TERMINATION_DONE) 498 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; 499 else if (state.chrg_status == STATUS_PRE_CHARGING) 500 val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD; 501 else if (state.chrg_status == STATUS_FAST_CHARGING) 502 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; 503 else /* unreachable */ 504 val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; 505 break; 506 507 case POWER_SUPPLY_PROP_MANUFACTURER: 508 val->strval = BQ25890_MANUFACTURER; 509 break; 510 511 case POWER_SUPPLY_PROP_MODEL_NAME: 512 val->strval = bq25890_chip_name[bq->chip_version]; 513 break; 514 515 case POWER_SUPPLY_PROP_ONLINE: 516 val->intval = state.online; 517 break; 518 519 case POWER_SUPPLY_PROP_HEALTH: 520 if (!state.chrg_fault && !state.bat_fault && !state.boost_fault) 521 val->intval = POWER_SUPPLY_HEALTH_GOOD; 522 else if (state.bat_fault) 523 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 524 else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED) 525 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE; 526 else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN) 527 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 528 else 529 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 530 break; 531 532 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 533 val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM); 534 break; 535 536 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 537 val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM); 538 break; 539 540 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 541 ret = bq25890_field_read(bq, F_IINLIM); 542 if (ret < 0) 543 return ret; 544 545 val->intval = bq25890_find_val(ret, TBL_IINLIM); 546 break; 547 548 case POWER_SUPPLY_PROP_CURRENT_NOW: /* I_BAT now */ 549 /* 550 * This is ADC-sampled immediate charge current supplied 551 * from charger to battery. The property name is confusing, 552 * for clarification refer to: 553 * Documentation/ABI/testing/sysfs-class-power 554 * /sys/class/power_supply/<supply_name>/current_now 555 */ 556 ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */ 557 if (ret < 0) 558 return ret; 559 560 /* converted_val = ADC_val * 50mA (table 10.3.19) */ 561 val->intval = ret * -50000; 562 break; 563 564 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: /* I_BAT user limit */ 565 /* 566 * This is user-configured constant charge current supplied 567 * from charger to battery in first phase of charging, when 568 * battery voltage is below constant charge voltage. 569 * 570 * This value reflects the current hardware setting. 571 * 572 * The POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX is the 573 * maximum value of this property. 574 */ 575 ret = bq25890_field_read(bq, F_ICHG); 576 if (ret < 0) 577 return ret; 578 val->intval = bq25890_find_val(ret, TBL_ICHG); 579 580 /* When temperature is too low, charge current is decreased */ 581 if (bq->state.ntc_fault == NTC_FAULT_COOL) { 582 ret = bq25890_field_read(bq, F_JEITA_ISET); 583 if (ret < 0) 584 return ret; 585 586 if (ret) 587 val->intval /= 5; 588 else 589 val->intval /= 2; 590 } 591 break; 592 593 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: /* I_BAT max */ 594 /* 595 * This is maximum allowed constant charge current supplied 596 * from charger to battery in first phase of charging, when 597 * battery voltage is below constant charge voltage. 598 * 599 * This value is constant for each battery and set from DT. 600 */ 601 val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG); 602 break; 603 604 case POWER_SUPPLY_PROP_VOLTAGE_NOW: /* V_BAT now */ 605 /* 606 * This is ADC-sampled immediate charge voltage supplied 607 * from charger to battery. The property name is confusing, 608 * for clarification refer to: 609 * Documentation/ABI/testing/sysfs-class-power 610 * /sys/class/power_supply/<supply_name>/voltage_now 611 */ 612 ret = bq25890_field_read(bq, F_BATV); /* read measured value */ 613 if (ret < 0) 614 return ret; 615 616 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */ 617 val->intval = 2304000 + ret * 20000; 618 break; 619 620 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: /* V_BAT user limit */ 621 /* 622 * This is user-configured constant charge voltage supplied 623 * from charger to battery in second phase of charging, when 624 * battery voltage reached constant charge voltage. 625 * 626 * This value reflects the current hardware setting. 627 * 628 * The POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX is the 629 * maximum value of this property. 630 */ 631 ret = bq25890_field_read(bq, F_VREG); 632 if (ret < 0) 633 return ret; 634 635 val->intval = bq25890_find_val(ret, TBL_VREG); 636 break; 637 638 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: /* V_BAT max */ 639 /* 640 * This is maximum allowed constant charge voltage supplied 641 * from charger to battery in second phase of charging, when 642 * battery voltage reached constant charge voltage. 643 * 644 * This value is constant for each battery and set from DT. 645 */ 646 val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG); 647 break; 648 649 case POWER_SUPPLY_PROP_TEMP: 650 ret = bq25890_field_read(bq, F_TSPCT); 651 if (ret < 0) 652 return ret; 653 654 /* convert TS percentage into rough temperature */ 655 val->intval = bq25890_find_val(ret, TBL_TSPCT); 656 break; 657 658 default: 659 return -EINVAL; 660 } 661 662 return 0; 663 } 664 665 static int bq25890_power_supply_set_property(struct power_supply *psy, 666 enum power_supply_property psp, 667 const union power_supply_propval *val) 668 { 669 struct bq25890_device *bq = power_supply_get_drvdata(psy); 670 int maxval; 671 u8 lval; 672 673 switch (psp) { 674 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 675 maxval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG); 676 lval = bq25890_find_idx(min(val->intval, maxval), TBL_ICHG); 677 return bq25890_field_write(bq, F_ICHG, lval); 678 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 679 maxval = bq25890_find_val(bq->init_data.vreg, TBL_VREG); 680 lval = bq25890_find_idx(min(val->intval, maxval), TBL_VREG); 681 return bq25890_field_write(bq, F_VREG, lval); 682 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 683 lval = bq25890_find_idx(val->intval, TBL_IINLIM); 684 return bq25890_field_write(bq, F_IINLIM, lval); 685 default: 686 return -EINVAL; 687 } 688 } 689 690 static int bq25890_power_supply_property_is_writeable(struct power_supply *psy, 691 enum power_supply_property psp) 692 { 693 switch (psp) { 694 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 695 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 696 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 697 return true; 698 default: 699 return false; 700 } 701 } 702 703 /* On the BQ25892 try to get charger-type info from our supplier */ 704 static void bq25890_charger_external_power_changed(struct power_supply *psy) 705 { 706 struct bq25890_device *bq = power_supply_get_drvdata(psy); 707 union power_supply_propval val; 708 int input_current_limit, ret; 709 710 if (bq->chip_version != BQ25892) 711 return; 712 713 ret = power_supply_get_property_from_supplier(bq->charger, 714 POWER_SUPPLY_PROP_USB_TYPE, 715 &val); 716 if (ret) 717 return; 718 719 switch (val.intval) { 720 case POWER_SUPPLY_USB_TYPE_DCP: 721 input_current_limit = bq25890_find_idx(2000000, TBL_IINLIM); 722 if (bq->pump_express_vbus_max) { 723 queue_delayed_work(system_power_efficient_wq, 724 &bq->pump_express_work, 725 PUMP_EXPRESS_START_DELAY); 726 } 727 break; 728 case POWER_SUPPLY_USB_TYPE_CDP: 729 case POWER_SUPPLY_USB_TYPE_ACA: 730 input_current_limit = bq25890_find_idx(1500000, TBL_IINLIM); 731 break; 732 case POWER_SUPPLY_USB_TYPE_SDP: 733 default: 734 input_current_limit = bq25890_find_idx(500000, TBL_IINLIM); 735 } 736 737 bq25890_field_write(bq, F_IINLIM, input_current_limit); 738 } 739 740 static int bq25890_get_chip_state(struct bq25890_device *bq, 741 struct bq25890_state *state) 742 { 743 int i, ret; 744 745 struct { 746 enum bq25890_fields id; 747 u8 *data; 748 } state_fields[] = { 749 {F_CHG_STAT, &state->chrg_status}, 750 {F_PG_STAT, &state->online}, 751 {F_VSYS_STAT, &state->vsys_status}, 752 {F_BOOST_FAULT, &state->boost_fault}, 753 {F_BAT_FAULT, &state->bat_fault}, 754 {F_CHG_FAULT, &state->chrg_fault}, 755 {F_NTC_FAULT, &state->ntc_fault} 756 }; 757 758 for (i = 0; i < ARRAY_SIZE(state_fields); i++) { 759 ret = bq25890_field_read(bq, state_fields[i].id); 760 if (ret < 0) 761 return ret; 762 763 *state_fields[i].data = ret; 764 } 765 766 dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT/NTC=%d/%d/%d/%d\n", 767 state->chrg_status, state->online, state->vsys_status, 768 state->chrg_fault, state->boost_fault, state->bat_fault, 769 state->ntc_fault); 770 771 return 0; 772 } 773 774 static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq) 775 { 776 struct bq25890_state new_state; 777 int ret; 778 779 ret = bq25890_get_chip_state(bq, &new_state); 780 if (ret < 0) 781 return IRQ_NONE; 782 783 if (!memcmp(&bq->state, &new_state, sizeof(new_state))) 784 return IRQ_NONE; 785 786 if (!new_state.online && bq->state.online) { /* power removed */ 787 /* disable ADC */ 788 ret = bq25890_field_write(bq, F_CONV_RATE, 0); 789 if (ret < 0) 790 goto error; 791 } else if (new_state.online && !bq->state.online) { /* power inserted */ 792 /* enable ADC, to have control of charge current/voltage */ 793 ret = bq25890_field_write(bq, F_CONV_RATE, 1); 794 if (ret < 0) 795 goto error; 796 } 797 798 bq->state = new_state; 799 power_supply_changed(bq->charger); 800 801 return IRQ_HANDLED; 802 error: 803 dev_err(bq->dev, "Error communicating with the chip: %pe\n", 804 ERR_PTR(ret)); 805 return IRQ_HANDLED; 806 } 807 808 static irqreturn_t bq25890_irq_handler_thread(int irq, void *private) 809 { 810 struct bq25890_device *bq = private; 811 irqreturn_t ret; 812 813 mutex_lock(&bq->lock); 814 ret = __bq25890_handle_irq(bq); 815 mutex_unlock(&bq->lock); 816 817 return ret; 818 } 819 820 static int bq25890_chip_reset(struct bq25890_device *bq) 821 { 822 int ret; 823 int rst_check_counter = 10; 824 825 ret = bq25890_field_write(bq, F_REG_RST, 1); 826 if (ret < 0) 827 return ret; 828 829 do { 830 ret = bq25890_field_read(bq, F_REG_RST); 831 if (ret < 0) 832 return ret; 833 834 usleep_range(5, 10); 835 } while (ret == 1 && --rst_check_counter); 836 837 if (!rst_check_counter) 838 return -ETIMEDOUT; 839 840 return 0; 841 } 842 843 static int bq25890_rw_init_data(struct bq25890_device *bq) 844 { 845 bool write = !bq->read_back_init_data; 846 int ret; 847 int i; 848 849 const struct { 850 enum bq25890_fields id; 851 u8 *value; 852 } init_data[] = { 853 {F_ICHG, &bq->init_data.ichg}, 854 {F_VREG, &bq->init_data.vreg}, 855 {F_ITERM, &bq->init_data.iterm}, 856 {F_IPRECHG, &bq->init_data.iprechg}, 857 {F_SYSVMIN, &bq->init_data.sysvmin}, 858 {F_BOOSTV, &bq->init_data.boostv}, 859 {F_BOOSTI, &bq->init_data.boosti}, 860 {F_BOOSTF, &bq->init_data.boostf}, 861 {F_EN_ILIM, &bq->init_data.ilim_en}, 862 {F_TREG, &bq->init_data.treg}, 863 {F_BATCMP, &bq->init_data.rbatcomp}, 864 {F_VCLAMP, &bq->init_data.vclamp}, 865 }; 866 867 for (i = 0; i < ARRAY_SIZE(init_data); i++) { 868 if (write) { 869 ret = bq25890_field_write(bq, init_data[i].id, 870 *init_data[i].value); 871 } else { 872 ret = bq25890_field_read(bq, init_data[i].id); 873 if (ret >= 0) 874 *init_data[i].value = ret; 875 } 876 if (ret < 0) { 877 dev_dbg(bq->dev, "Accessing init data failed %d\n", ret); 878 return ret; 879 } 880 } 881 882 return 0; 883 } 884 885 static int bq25890_hw_init(struct bq25890_device *bq) 886 { 887 int ret; 888 889 if (!bq->skip_reset) { 890 ret = bq25890_chip_reset(bq); 891 if (ret < 0) { 892 dev_dbg(bq->dev, "Reset failed %d\n", ret); 893 return ret; 894 } 895 } else { 896 /* 897 * Ensure charging is enabled, on some boards where the fw 898 * takes care of initalizition F_CHG_CFG is set to 0 before 899 * handing control over to the OS. 900 */ 901 ret = bq25890_field_write(bq, F_CHG_CFG, 1); 902 if (ret < 0) { 903 dev_dbg(bq->dev, "Enabling charging failed %d\n", ret); 904 return ret; 905 } 906 } 907 908 /* disable watchdog */ 909 ret = bq25890_field_write(bq, F_WD, 0); 910 if (ret < 0) { 911 dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret); 912 return ret; 913 } 914 915 /* initialize currents/voltages and other parameters */ 916 ret = bq25890_rw_init_data(bq); 917 if (ret) 918 return ret; 919 920 ret = bq25890_get_chip_state(bq, &bq->state); 921 if (ret < 0) { 922 dev_dbg(bq->dev, "Get state failed %d\n", ret); 923 return ret; 924 } 925 926 /* Configure ADC for continuous conversions when charging */ 927 ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online); 928 if (ret < 0) { 929 dev_dbg(bq->dev, "Config ADC failed %d\n", ret); 930 return ret; 931 } 932 933 return 0; 934 } 935 936 static const enum power_supply_property bq25890_power_supply_props[] = { 937 POWER_SUPPLY_PROP_MANUFACTURER, 938 POWER_SUPPLY_PROP_MODEL_NAME, 939 POWER_SUPPLY_PROP_STATUS, 940 POWER_SUPPLY_PROP_CHARGE_TYPE, 941 POWER_SUPPLY_PROP_ONLINE, 942 POWER_SUPPLY_PROP_HEALTH, 943 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 944 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 945 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 946 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 947 POWER_SUPPLY_PROP_PRECHARGE_CURRENT, 948 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 949 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 950 POWER_SUPPLY_PROP_VOLTAGE_NOW, 951 POWER_SUPPLY_PROP_CURRENT_NOW, 952 POWER_SUPPLY_PROP_TEMP, 953 }; 954 955 static char *bq25890_charger_supplied_to[] = { 956 "main-battery", 957 }; 958 959 static const struct power_supply_desc bq25890_power_supply_desc = { 960 .name = "bq25890-charger", 961 .type = POWER_SUPPLY_TYPE_USB, 962 .properties = bq25890_power_supply_props, 963 .num_properties = ARRAY_SIZE(bq25890_power_supply_props), 964 .get_property = bq25890_power_supply_get_property, 965 .set_property = bq25890_power_supply_set_property, 966 .property_is_writeable = bq25890_power_supply_property_is_writeable, 967 .external_power_changed = bq25890_charger_external_power_changed, 968 }; 969 970 static int bq25890_power_supply_init(struct bq25890_device *bq) 971 { 972 struct power_supply_config psy_cfg = { .drv_data = bq, }; 973 974 psy_cfg.supplied_to = bq25890_charger_supplied_to; 975 psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to); 976 977 bq->charger = devm_power_supply_register(bq->dev, 978 &bq25890_power_supply_desc, 979 &psy_cfg); 980 981 return PTR_ERR_OR_ZERO(bq->charger); 982 } 983 984 static int bq25890_set_otg_cfg(struct bq25890_device *bq, u8 val) 985 { 986 int ret; 987 988 ret = bq25890_field_write(bq, F_OTG_CFG, val); 989 if (ret < 0) 990 dev_err(bq->dev, "Error switching to boost/charger mode: %d\n", ret); 991 992 return ret; 993 } 994 995 static void bq25890_pump_express_work(struct work_struct *data) 996 { 997 struct bq25890_device *bq = 998 container_of(data, struct bq25890_device, pump_express_work.work); 999 int voltage, i, ret; 1000 1001 dev_dbg(bq->dev, "Start to request input voltage increasing\n"); 1002 1003 /* Enable current pulse voltage control protocol */ 1004 ret = bq25890_field_write(bq, F_PUMPX_EN, 1); 1005 if (ret < 0) 1006 goto error_print; 1007 1008 for (i = 0; i < PUMP_EXPRESS_MAX_TRIES; i++) { 1009 voltage = bq25890_get_vbus_voltage(bq); 1010 if (voltage < 0) 1011 goto error_print; 1012 dev_dbg(bq->dev, "input voltage = %d uV\n", voltage); 1013 1014 if ((voltage + PUMP_EXPRESS_VBUS_MARGIN_uV) > 1015 bq->pump_express_vbus_max) 1016 break; 1017 1018 ret = bq25890_field_write(bq, F_PUMPX_UP, 1); 1019 if (ret < 0) 1020 goto error_print; 1021 1022 /* Note a single PUMPX up pulse-sequence takes 2.1s */ 1023 ret = regmap_field_read_poll_timeout(bq->rmap_fields[F_PUMPX_UP], 1024 ret, !ret, 100000, 3000000); 1025 if (ret < 0) 1026 goto error_print; 1027 1028 /* Make sure ADC has sampled Vbus before checking again */ 1029 msleep(1000); 1030 } 1031 1032 bq25890_field_write(bq, F_PUMPX_EN, 0); 1033 1034 dev_info(bq->dev, "Hi-voltage charging requested, input voltage is %d mV\n", 1035 voltage); 1036 1037 return; 1038 error_print: 1039 bq25890_field_write(bq, F_PUMPX_EN, 0); 1040 dev_err(bq->dev, "Failed to request hi-voltage charging\n"); 1041 } 1042 1043 static void bq25890_usb_work(struct work_struct *data) 1044 { 1045 int ret; 1046 struct bq25890_device *bq = 1047 container_of(data, struct bq25890_device, usb_work); 1048 1049 switch (bq->usb_event) { 1050 case USB_EVENT_ID: 1051 /* Enable boost mode */ 1052 bq25890_set_otg_cfg(bq, 1); 1053 break; 1054 1055 case USB_EVENT_NONE: 1056 /* Disable boost mode */ 1057 ret = bq25890_set_otg_cfg(bq, 0); 1058 if (ret == 0) 1059 power_supply_changed(bq->charger); 1060 break; 1061 } 1062 } 1063 1064 static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val, 1065 void *priv) 1066 { 1067 struct bq25890_device *bq = 1068 container_of(nb, struct bq25890_device, usb_nb); 1069 1070 bq->usb_event = val; 1071 queue_work(system_power_efficient_wq, &bq->usb_work); 1072 1073 return NOTIFY_OK; 1074 } 1075 1076 #ifdef CONFIG_REGULATOR 1077 static int bq25890_vbus_enable(struct regulator_dev *rdev) 1078 { 1079 struct bq25890_device *bq = rdev_get_drvdata(rdev); 1080 1081 return bq25890_set_otg_cfg(bq, 1); 1082 } 1083 1084 static int bq25890_vbus_disable(struct regulator_dev *rdev) 1085 { 1086 struct bq25890_device *bq = rdev_get_drvdata(rdev); 1087 1088 return bq25890_set_otg_cfg(bq, 0); 1089 } 1090 1091 static int bq25890_vbus_is_enabled(struct regulator_dev *rdev) 1092 { 1093 struct bq25890_device *bq = rdev_get_drvdata(rdev); 1094 1095 return bq25890_field_read(bq, F_OTG_CFG); 1096 } 1097 1098 static int bq25890_vbus_get_voltage(struct regulator_dev *rdev) 1099 { 1100 struct bq25890_device *bq = rdev_get_drvdata(rdev); 1101 1102 return bq25890_get_vbus_voltage(bq); 1103 } 1104 1105 static int bq25890_vsys_get_voltage(struct regulator_dev *rdev) 1106 { 1107 struct bq25890_device *bq = rdev_get_drvdata(rdev); 1108 int ret; 1109 1110 /* Should be some output voltage ? */ 1111 ret = bq25890_field_read(bq, F_SYSV); /* read measured value */ 1112 if (ret < 0) 1113 return ret; 1114 1115 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */ 1116 return 2304000 + ret * 20000; 1117 } 1118 1119 static const struct regulator_ops bq25890_vbus_ops = { 1120 .enable = bq25890_vbus_enable, 1121 .disable = bq25890_vbus_disable, 1122 .is_enabled = bq25890_vbus_is_enabled, 1123 .get_voltage = bq25890_vbus_get_voltage, 1124 }; 1125 1126 static const struct regulator_desc bq25890_vbus_desc = { 1127 .name = "usb_otg_vbus", 1128 .of_match = "usb-otg-vbus", 1129 .type = REGULATOR_VOLTAGE, 1130 .owner = THIS_MODULE, 1131 .ops = &bq25890_vbus_ops, 1132 }; 1133 1134 static const struct regulator_ops bq25890_vsys_ops = { 1135 .get_voltage = bq25890_vsys_get_voltage, 1136 }; 1137 1138 static const struct regulator_desc bq25890_vsys_desc = { 1139 .name = "vsys", 1140 .of_match = "vsys", 1141 .type = REGULATOR_VOLTAGE, 1142 .owner = THIS_MODULE, 1143 .ops = &bq25890_vsys_ops, 1144 }; 1145 1146 static int bq25890_register_regulator(struct bq25890_device *bq) 1147 { 1148 struct bq25890_platform_data *pdata = dev_get_platdata(bq->dev); 1149 struct regulator_config cfg = { 1150 .dev = bq->dev, 1151 .driver_data = bq, 1152 }; 1153 struct regulator_dev *reg; 1154 1155 if (pdata) 1156 cfg.init_data = pdata->regulator_init_data; 1157 1158 reg = devm_regulator_register(bq->dev, &bq25890_vbus_desc, &cfg); 1159 if (IS_ERR(reg)) { 1160 return dev_err_probe(bq->dev, PTR_ERR(reg), 1161 "registering vbus regulator"); 1162 } 1163 1164 /* pdata->regulator_init_data is for vbus only */ 1165 cfg.init_data = NULL; 1166 reg = devm_regulator_register(bq->dev, &bq25890_vsys_desc, &cfg); 1167 if (IS_ERR(reg)) { 1168 return dev_err_probe(bq->dev, PTR_ERR(reg), 1169 "registering vsys regulator"); 1170 } 1171 1172 return 0; 1173 } 1174 #else 1175 static inline int 1176 bq25890_register_regulator(struct bq25890_device *bq) 1177 { 1178 return 0; 1179 } 1180 #endif 1181 1182 static int bq25890_get_chip_version(struct bq25890_device *bq) 1183 { 1184 int id, rev; 1185 1186 id = bq25890_field_read(bq, F_PN); 1187 if (id < 0) { 1188 dev_err(bq->dev, "Cannot read chip ID: %d\n", id); 1189 return id; 1190 } 1191 1192 rev = bq25890_field_read(bq, F_DEV_REV); 1193 if (rev < 0) { 1194 dev_err(bq->dev, "Cannot read chip revision: %d\n", rev); 1195 return rev; 1196 } 1197 1198 switch (id) { 1199 case BQ25890_ID: 1200 bq->chip_version = BQ25890; 1201 break; 1202 1203 /* BQ25892 and BQ25896 share same ID 0 */ 1204 case BQ25896_ID: 1205 switch (rev) { 1206 case 2: 1207 bq->chip_version = BQ25896; 1208 break; 1209 case 1: 1210 bq->chip_version = BQ25892; 1211 break; 1212 default: 1213 dev_err(bq->dev, 1214 "Unknown device revision %d, assume BQ25892\n", 1215 rev); 1216 bq->chip_version = BQ25892; 1217 } 1218 break; 1219 1220 case BQ25895_ID: 1221 bq->chip_version = BQ25895; 1222 break; 1223 1224 default: 1225 dev_err(bq->dev, "Unknown chip ID %d\n", id); 1226 return -ENODEV; 1227 } 1228 1229 return 0; 1230 } 1231 1232 static int bq25890_irq_probe(struct bq25890_device *bq) 1233 { 1234 struct gpio_desc *irq; 1235 1236 irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN); 1237 if (IS_ERR(irq)) 1238 return dev_err_probe(bq->dev, PTR_ERR(irq), 1239 "Could not probe irq pin.\n"); 1240 1241 return gpiod_to_irq(irq); 1242 } 1243 1244 static int bq25890_fw_read_u32_props(struct bq25890_device *bq) 1245 { 1246 int ret; 1247 u32 property; 1248 int i; 1249 struct bq25890_init_data *init = &bq->init_data; 1250 struct { 1251 char *name; 1252 bool optional; 1253 enum bq25890_table_ids tbl_id; 1254 u8 *conv_data; /* holds converted value from given property */ 1255 } props[] = { 1256 /* required properties */ 1257 {"ti,charge-current", false, TBL_ICHG, &init->ichg}, 1258 {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg}, 1259 {"ti,termination-current", false, TBL_ITERM, &init->iterm}, 1260 {"ti,precharge-current", false, TBL_ITERM, &init->iprechg}, 1261 {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin}, 1262 {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv}, 1263 {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti}, 1264 1265 /* optional properties */ 1266 {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg}, 1267 {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp}, 1268 {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp}, 1269 }; 1270 1271 /* initialize data for optional properties */ 1272 init->treg = 3; /* 120 degrees Celsius */ 1273 init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */ 1274 1275 for (i = 0; i < ARRAY_SIZE(props); i++) { 1276 ret = device_property_read_u32(bq->dev, props[i].name, 1277 &property); 1278 if (ret < 0) { 1279 if (props[i].optional) 1280 continue; 1281 1282 dev_err(bq->dev, "Unable to read property %d %s\n", ret, 1283 props[i].name); 1284 1285 return ret; 1286 } 1287 1288 *props[i].conv_data = bq25890_find_idx(property, 1289 props[i].tbl_id); 1290 } 1291 1292 return 0; 1293 } 1294 1295 static int bq25890_fw_probe(struct bq25890_device *bq) 1296 { 1297 int ret; 1298 struct bq25890_init_data *init = &bq->init_data; 1299 1300 /* Optional, left at 0 if property is not present */ 1301 device_property_read_u32(bq->dev, "linux,pump-express-vbus-max", 1302 &bq->pump_express_vbus_max); 1303 1304 bq->skip_reset = device_property_read_bool(bq->dev, "linux,skip-reset"); 1305 bq->read_back_init_data = device_property_read_bool(bq->dev, 1306 "linux,read-back-settings"); 1307 if (bq->read_back_init_data) 1308 return 0; 1309 1310 ret = bq25890_fw_read_u32_props(bq); 1311 if (ret < 0) 1312 return ret; 1313 1314 init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin"); 1315 init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq"); 1316 1317 return 0; 1318 } 1319 1320 static void bq25890_non_devm_cleanup(void *data) 1321 { 1322 struct bq25890_device *bq = data; 1323 1324 cancel_delayed_work_sync(&bq->pump_express_work); 1325 } 1326 1327 static int bq25890_probe(struct i2c_client *client) 1328 { 1329 struct device *dev = &client->dev; 1330 struct bq25890_device *bq; 1331 int ret; 1332 1333 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL); 1334 if (!bq) 1335 return -ENOMEM; 1336 1337 bq->client = client; 1338 bq->dev = dev; 1339 1340 mutex_init(&bq->lock); 1341 INIT_DELAYED_WORK(&bq->pump_express_work, bq25890_pump_express_work); 1342 1343 bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config); 1344 if (IS_ERR(bq->rmap)) 1345 return dev_err_probe(dev, PTR_ERR(bq->rmap), 1346 "failed to allocate register map\n"); 1347 1348 ret = devm_regmap_field_bulk_alloc(dev, bq->rmap, bq->rmap_fields, 1349 bq25890_reg_fields, F_MAX_FIELDS); 1350 if (ret) 1351 return ret; 1352 1353 i2c_set_clientdata(client, bq); 1354 1355 ret = bq25890_get_chip_version(bq); 1356 if (ret) { 1357 dev_err(dev, "Cannot read chip ID or unknown chip: %d\n", ret); 1358 return ret; 1359 } 1360 1361 ret = bq25890_fw_probe(bq); 1362 if (ret < 0) 1363 return dev_err_probe(dev, ret, "reading device properties\n"); 1364 1365 ret = bq25890_hw_init(bq); 1366 if (ret < 0) { 1367 dev_err(dev, "Cannot initialize the chip: %d\n", ret); 1368 return ret; 1369 } 1370 1371 if (client->irq <= 0) 1372 client->irq = bq25890_irq_probe(bq); 1373 1374 if (client->irq < 0) { 1375 dev_err(dev, "No irq resource found.\n"); 1376 return client->irq; 1377 } 1378 1379 /* OTG reporting */ 1380 bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); 1381 1382 /* 1383 * This must be before bq25890_power_supply_init(), so that it runs 1384 * after devm unregisters the power_supply. 1385 */ 1386 ret = devm_add_action_or_reset(dev, bq25890_non_devm_cleanup, bq); 1387 if (ret) 1388 return ret; 1389 1390 ret = bq25890_register_regulator(bq); 1391 if (ret) 1392 return ret; 1393 1394 ret = bq25890_power_supply_init(bq); 1395 if (ret < 0) 1396 return dev_err_probe(dev, ret, "registering power supply\n"); 1397 1398 ret = devm_request_threaded_irq(dev, client->irq, NULL, 1399 bq25890_irq_handler_thread, 1400 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 1401 BQ25890_IRQ_PIN, bq); 1402 if (ret) 1403 return ret; 1404 1405 if (!IS_ERR_OR_NULL(bq->usb_phy)) { 1406 INIT_WORK(&bq->usb_work, bq25890_usb_work); 1407 bq->usb_nb.notifier_call = bq25890_usb_notifier; 1408 usb_register_notifier(bq->usb_phy, &bq->usb_nb); 1409 } 1410 1411 return 0; 1412 } 1413 1414 static void bq25890_remove(struct i2c_client *client) 1415 { 1416 struct bq25890_device *bq = i2c_get_clientdata(client); 1417 1418 if (!IS_ERR_OR_NULL(bq->usb_phy)) { 1419 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb); 1420 cancel_work_sync(&bq->usb_work); 1421 } 1422 1423 if (!bq->skip_reset) { 1424 /* reset all registers to default values */ 1425 bq25890_chip_reset(bq); 1426 } 1427 } 1428 1429 static void bq25890_shutdown(struct i2c_client *client) 1430 { 1431 struct bq25890_device *bq = i2c_get_clientdata(client); 1432 1433 /* 1434 * TODO this if + return should probably be removed, but that would 1435 * introduce a function change for boards using the usb-phy framework. 1436 * This needs to be tested on such a board before making this change. 1437 */ 1438 if (!IS_ERR_OR_NULL(bq->usb_phy)) 1439 return; 1440 1441 /* 1442 * Turn off the 5v Boost regulator which outputs Vbus to the device's 1443 * Micro-USB or Type-C USB port. Leaving this on drains power and 1444 * this avoids the PMIC on some device-models seeing this as Vbus 1445 * getting inserted after shutdown, causing the device to immediately 1446 * power-up again. 1447 */ 1448 bq25890_set_otg_cfg(bq, 0); 1449 } 1450 1451 #ifdef CONFIG_PM_SLEEP 1452 static int bq25890_suspend(struct device *dev) 1453 { 1454 struct bq25890_device *bq = dev_get_drvdata(dev); 1455 1456 /* 1457 * If charger is removed, while in suspend, make sure ADC is diabled 1458 * since it consumes slightly more power. 1459 */ 1460 return bq25890_field_write(bq, F_CONV_RATE, 0); 1461 } 1462 1463 static int bq25890_resume(struct device *dev) 1464 { 1465 int ret; 1466 struct bq25890_device *bq = dev_get_drvdata(dev); 1467 1468 mutex_lock(&bq->lock); 1469 1470 ret = bq25890_get_chip_state(bq, &bq->state); 1471 if (ret < 0) 1472 goto unlock; 1473 1474 /* Re-enable ADC only if charger is plugged in. */ 1475 if (bq->state.online) { 1476 ret = bq25890_field_write(bq, F_CONV_RATE, 1); 1477 if (ret < 0) 1478 goto unlock; 1479 } 1480 1481 /* signal userspace, maybe state changed while suspended */ 1482 power_supply_changed(bq->charger); 1483 1484 unlock: 1485 mutex_unlock(&bq->lock); 1486 1487 return ret; 1488 } 1489 #endif 1490 1491 static const struct dev_pm_ops bq25890_pm = { 1492 SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume) 1493 }; 1494 1495 static const struct i2c_device_id bq25890_i2c_ids[] = { 1496 { "bq25890", 0 }, 1497 { "bq25892", 0 }, 1498 { "bq25895", 0 }, 1499 { "bq25896", 0 }, 1500 {}, 1501 }; 1502 MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids); 1503 1504 static const struct of_device_id bq25890_of_match[] = { 1505 { .compatible = "ti,bq25890", }, 1506 { .compatible = "ti,bq25892", }, 1507 { .compatible = "ti,bq25895", }, 1508 { .compatible = "ti,bq25896", }, 1509 { }, 1510 }; 1511 MODULE_DEVICE_TABLE(of, bq25890_of_match); 1512 1513 #ifdef CONFIG_ACPI 1514 static const struct acpi_device_id bq25890_acpi_match[] = { 1515 {"BQ258900", 0}, 1516 {}, 1517 }; 1518 MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match); 1519 #endif 1520 1521 static struct i2c_driver bq25890_driver = { 1522 .driver = { 1523 .name = "bq25890-charger", 1524 .of_match_table = of_match_ptr(bq25890_of_match), 1525 .acpi_match_table = ACPI_PTR(bq25890_acpi_match), 1526 .pm = &bq25890_pm, 1527 }, 1528 .probe_new = bq25890_probe, 1529 .remove = bq25890_remove, 1530 .shutdown = bq25890_shutdown, 1531 .id_table = bq25890_i2c_ids, 1532 }; 1533 module_i2c_driver(bq25890_driver); 1534 1535 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>"); 1536 MODULE_DESCRIPTION("bq25890 charger driver"); 1537 MODULE_LICENSE("GPL"); 1538