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