1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Summit Microelectronics SMB347 Battery Charger Driver 4 * 5 * Copyright (C) 2011, Intel Corporation 6 * 7 * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com> 8 * Mika Westerberg <mika.westerberg@linux.intel.com> 9 */ 10 11 #include <linux/delay.h> 12 #include <linux/err.h> 13 #include <linux/gpio.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/init.h> 17 #include <linux/interrupt.h> 18 #include <linux/i2c.h> 19 #include <linux/mutex.h> 20 #include <linux/power_supply.h> 21 #include <linux/power/smb347-charger.h> 22 #include <linux/regmap.h> 23 24 /* 25 * Configuration registers. These are mirrored to volatile RAM and can be 26 * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be 27 * reloaded from non-volatile registers after POR. 28 */ 29 #define CFG_CHARGE_CURRENT 0x00 30 #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0 31 #define CFG_CHARGE_CURRENT_FCC_SHIFT 5 32 #define CFG_CHARGE_CURRENT_PCC_MASK 0x18 33 #define CFG_CHARGE_CURRENT_PCC_SHIFT 3 34 #define CFG_CHARGE_CURRENT_TC_MASK 0x07 35 #define CFG_CURRENT_LIMIT 0x01 36 #define CFG_CURRENT_LIMIT_DC_MASK 0xf0 37 #define CFG_CURRENT_LIMIT_DC_SHIFT 4 38 #define CFG_CURRENT_LIMIT_USB_MASK 0x0f 39 #define CFG_FLOAT_VOLTAGE 0x03 40 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f 41 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0 42 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6 43 #define CFG_STAT 0x05 44 #define CFG_STAT_DISABLED BIT(5) 45 #define CFG_STAT_ACTIVE_HIGH BIT(7) 46 #define CFG_PIN 0x06 47 #define CFG_PIN_EN_CTRL_MASK 0x60 48 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40 49 #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60 50 #define CFG_PIN_EN_APSD_IRQ BIT(1) 51 #define CFG_PIN_EN_CHARGER_ERROR BIT(2) 52 #define CFG_THERM 0x07 53 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03 54 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0 55 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c 56 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2 57 #define CFG_THERM_MONITOR_DISABLED BIT(4) 58 #define CFG_SYSOK 0x08 59 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2) 60 #define CFG_OTHER 0x09 61 #define CFG_OTHER_RID_MASK 0xc0 62 #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0 63 #define CFG_OTG 0x0a 64 #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30 65 #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4 66 #define CFG_OTG_CC_COMPENSATION_MASK 0xc0 67 #define CFG_OTG_CC_COMPENSATION_SHIFT 6 68 #define CFG_TEMP_LIMIT 0x0b 69 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03 70 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0 71 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c 72 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2 73 #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30 74 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4 75 #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0 76 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6 77 #define CFG_FAULT_IRQ 0x0c 78 #define CFG_FAULT_IRQ_DCIN_UV BIT(2) 79 #define CFG_STATUS_IRQ 0x0d 80 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4) 81 #define CFG_STATUS_IRQ_CHARGE_TIMEOUT BIT(7) 82 #define CFG_ADDRESS 0x0e 83 84 /* Command registers */ 85 #define CMD_A 0x30 86 #define CMD_A_CHG_ENABLED BIT(1) 87 #define CMD_A_SUSPEND_ENABLED BIT(2) 88 #define CMD_A_ALLOW_WRITE BIT(7) 89 #define CMD_B 0x31 90 #define CMD_C 0x33 91 92 /* Interrupt Status registers */ 93 #define IRQSTAT_A 0x35 94 #define IRQSTAT_C 0x37 95 #define IRQSTAT_C_TERMINATION_STAT BIT(0) 96 #define IRQSTAT_C_TERMINATION_IRQ BIT(1) 97 #define IRQSTAT_C_TAPER_IRQ BIT(3) 98 #define IRQSTAT_D 0x38 99 #define IRQSTAT_D_CHARGE_TIMEOUT_STAT BIT(2) 100 #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ BIT(3) 101 #define IRQSTAT_E 0x39 102 #define IRQSTAT_E_USBIN_UV_STAT BIT(0) 103 #define IRQSTAT_E_USBIN_UV_IRQ BIT(1) 104 #define IRQSTAT_E_DCIN_UV_STAT BIT(4) 105 #define IRQSTAT_E_DCIN_UV_IRQ BIT(5) 106 #define IRQSTAT_F 0x3a 107 108 /* Status registers */ 109 #define STAT_A 0x3b 110 #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f 111 #define STAT_B 0x3c 112 #define STAT_C 0x3d 113 #define STAT_C_CHG_ENABLED BIT(0) 114 #define STAT_C_HOLDOFF_STAT BIT(3) 115 #define STAT_C_CHG_MASK 0x06 116 #define STAT_C_CHG_SHIFT 1 117 #define STAT_C_CHG_TERM BIT(5) 118 #define STAT_C_CHARGER_ERROR BIT(6) 119 #define STAT_E 0x3f 120 121 #define SMB347_MAX_REGISTER 0x3f 122 123 /** 124 * struct smb347_charger - smb347 charger instance 125 * @lock: protects concurrent access to online variables 126 * @dev: pointer to device 127 * @regmap: pointer to driver regmap 128 * @mains: power_supply instance for AC/DC power 129 * @usb: power_supply instance for USB power 130 * @battery: power_supply instance for battery 131 * @mains_online: is AC/DC input connected 132 * @usb_online: is USB input connected 133 * @charging_enabled: is charging enabled 134 * @pdata: pointer to platform data 135 */ 136 struct smb347_charger { 137 struct mutex lock; 138 struct device *dev; 139 struct regmap *regmap; 140 struct power_supply *mains; 141 struct power_supply *usb; 142 struct power_supply *battery; 143 bool mains_online; 144 bool usb_online; 145 bool charging_enabled; 146 const struct smb347_charger_platform_data *pdata; 147 }; 148 149 /* Fast charge current in uA */ 150 static const unsigned int fcc_tbl[] = { 151 700000, 152 900000, 153 1200000, 154 1500000, 155 1800000, 156 2000000, 157 2200000, 158 2500000, 159 }; 160 161 /* Pre-charge current in uA */ 162 static const unsigned int pcc_tbl[] = { 163 100000, 164 150000, 165 200000, 166 250000, 167 }; 168 169 /* Termination current in uA */ 170 static const unsigned int tc_tbl[] = { 171 37500, 172 50000, 173 100000, 174 150000, 175 200000, 176 250000, 177 500000, 178 600000, 179 }; 180 181 /* Input current limit in uA */ 182 static const unsigned int icl_tbl[] = { 183 300000, 184 500000, 185 700000, 186 900000, 187 1200000, 188 1500000, 189 1800000, 190 2000000, 191 2200000, 192 2500000, 193 }; 194 195 /* Charge current compensation in uA */ 196 static const unsigned int ccc_tbl[] = { 197 250000, 198 700000, 199 900000, 200 1200000, 201 }; 202 203 /* Convert register value to current using lookup table */ 204 static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val) 205 { 206 if (val >= size) 207 return -EINVAL; 208 return tbl[val]; 209 } 210 211 /* Convert current to register value using lookup table */ 212 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val) 213 { 214 size_t i; 215 216 for (i = 0; i < size; i++) 217 if (val < tbl[i]) 218 break; 219 return i > 0 ? i - 1 : -EINVAL; 220 } 221 222 /** 223 * smb347_update_ps_status - refreshes the power source status 224 * @smb: pointer to smb347 charger instance 225 * 226 * Function checks whether any power source is connected to the charger and 227 * updates internal state accordingly. If there is a change to previous state 228 * function returns %1, otherwise %0 and negative errno in case of errror. 229 */ 230 static int smb347_update_ps_status(struct smb347_charger *smb) 231 { 232 bool usb = false; 233 bool dc = false; 234 unsigned int val; 235 int ret; 236 237 ret = regmap_read(smb->regmap, IRQSTAT_E, &val); 238 if (ret < 0) 239 return ret; 240 241 /* 242 * Dc and usb are set depending on whether they are enabled in 243 * platform data _and_ whether corresponding undervoltage is set. 244 */ 245 if (smb->pdata->use_mains) 246 dc = !(val & IRQSTAT_E_DCIN_UV_STAT); 247 if (smb->pdata->use_usb) 248 usb = !(val & IRQSTAT_E_USBIN_UV_STAT); 249 250 mutex_lock(&smb->lock); 251 ret = smb->mains_online != dc || smb->usb_online != usb; 252 smb->mains_online = dc; 253 smb->usb_online = usb; 254 mutex_unlock(&smb->lock); 255 256 return ret; 257 } 258 259 /* 260 * smb347_is_ps_online - returns whether input power source is connected 261 * @smb: pointer to smb347 charger instance 262 * 263 * Returns %true if input power source is connected. Note that this is 264 * dependent on what platform has configured for usable power sources. For 265 * example if USB is disabled, this will return %false even if the USB cable 266 * is connected. 267 */ 268 static bool smb347_is_ps_online(struct smb347_charger *smb) 269 { 270 bool ret; 271 272 mutex_lock(&smb->lock); 273 ret = smb->usb_online || smb->mains_online; 274 mutex_unlock(&smb->lock); 275 276 return ret; 277 } 278 279 /** 280 * smb347_charging_status - returns status of charging 281 * @smb: pointer to smb347 charger instance 282 * 283 * Function returns charging status. %0 means no charging is in progress, 284 * %1 means pre-charging, %2 fast-charging and %3 taper-charging. 285 */ 286 static int smb347_charging_status(struct smb347_charger *smb) 287 { 288 unsigned int val; 289 int ret; 290 291 if (!smb347_is_ps_online(smb)) 292 return 0; 293 294 ret = regmap_read(smb->regmap, STAT_C, &val); 295 if (ret < 0) 296 return 0; 297 298 return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT; 299 } 300 301 static int smb347_charging_set(struct smb347_charger *smb, bool enable) 302 { 303 int ret = 0; 304 305 if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) { 306 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n"); 307 return 0; 308 } 309 310 mutex_lock(&smb->lock); 311 if (smb->charging_enabled != enable) { 312 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED, 313 enable ? CMD_A_CHG_ENABLED : 0); 314 if (!ret) 315 smb->charging_enabled = enable; 316 } 317 mutex_unlock(&smb->lock); 318 return ret; 319 } 320 321 static inline int smb347_charging_enable(struct smb347_charger *smb) 322 { 323 return smb347_charging_set(smb, true); 324 } 325 326 static inline int smb347_charging_disable(struct smb347_charger *smb) 327 { 328 return smb347_charging_set(smb, false); 329 } 330 331 static int smb347_start_stop_charging(struct smb347_charger *smb) 332 { 333 int ret; 334 335 /* 336 * Depending on whether valid power source is connected or not, we 337 * disable or enable the charging. We do it manually because it 338 * depends on how the platform has configured the valid inputs. 339 */ 340 if (smb347_is_ps_online(smb)) { 341 ret = smb347_charging_enable(smb); 342 if (ret < 0) 343 dev_err(smb->dev, "failed to enable charging\n"); 344 } else { 345 ret = smb347_charging_disable(smb); 346 if (ret < 0) 347 dev_err(smb->dev, "failed to disable charging\n"); 348 } 349 350 return ret; 351 } 352 353 static int smb347_set_charge_current(struct smb347_charger *smb) 354 { 355 int ret; 356 357 if (smb->pdata->max_charge_current) { 358 ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl), 359 smb->pdata->max_charge_current); 360 if (ret < 0) 361 return ret; 362 363 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, 364 CFG_CHARGE_CURRENT_FCC_MASK, 365 ret << CFG_CHARGE_CURRENT_FCC_SHIFT); 366 if (ret < 0) 367 return ret; 368 } 369 370 if (smb->pdata->pre_charge_current) { 371 ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl), 372 smb->pdata->pre_charge_current); 373 if (ret < 0) 374 return ret; 375 376 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, 377 CFG_CHARGE_CURRENT_PCC_MASK, 378 ret << CFG_CHARGE_CURRENT_PCC_SHIFT); 379 if (ret < 0) 380 return ret; 381 } 382 383 if (smb->pdata->termination_current) { 384 ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl), 385 smb->pdata->termination_current); 386 if (ret < 0) 387 return ret; 388 389 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, 390 CFG_CHARGE_CURRENT_TC_MASK, ret); 391 if (ret < 0) 392 return ret; 393 } 394 395 return 0; 396 } 397 398 static int smb347_set_current_limits(struct smb347_charger *smb) 399 { 400 int ret; 401 402 if (smb->pdata->mains_current_limit) { 403 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), 404 smb->pdata->mains_current_limit); 405 if (ret < 0) 406 return ret; 407 408 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT, 409 CFG_CURRENT_LIMIT_DC_MASK, 410 ret << CFG_CURRENT_LIMIT_DC_SHIFT); 411 if (ret < 0) 412 return ret; 413 } 414 415 if (smb->pdata->usb_hc_current_limit) { 416 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), 417 smb->pdata->usb_hc_current_limit); 418 if (ret < 0) 419 return ret; 420 421 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT, 422 CFG_CURRENT_LIMIT_USB_MASK, ret); 423 if (ret < 0) 424 return ret; 425 } 426 427 return 0; 428 } 429 430 static int smb347_set_voltage_limits(struct smb347_charger *smb) 431 { 432 int ret; 433 434 if (smb->pdata->pre_to_fast_voltage) { 435 ret = smb->pdata->pre_to_fast_voltage; 436 437 /* uV */ 438 ret = clamp_val(ret, 2400000, 3000000) - 2400000; 439 ret /= 200000; 440 441 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE, 442 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK, 443 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT); 444 if (ret < 0) 445 return ret; 446 } 447 448 if (smb->pdata->max_charge_voltage) { 449 ret = smb->pdata->max_charge_voltage; 450 451 /* uV */ 452 ret = clamp_val(ret, 3500000, 4500000) - 3500000; 453 ret /= 20000; 454 455 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE, 456 CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret); 457 if (ret < 0) 458 return ret; 459 } 460 461 return 0; 462 } 463 464 static int smb347_set_temp_limits(struct smb347_charger *smb) 465 { 466 bool enable_therm_monitor = false; 467 int ret = 0; 468 int val; 469 470 if (smb->pdata->chip_temp_threshold) { 471 val = smb->pdata->chip_temp_threshold; 472 473 /* degree C */ 474 val = clamp_val(val, 100, 130) - 100; 475 val /= 10; 476 477 ret = regmap_update_bits(smb->regmap, CFG_OTG, 478 CFG_OTG_TEMP_THRESHOLD_MASK, 479 val << CFG_OTG_TEMP_THRESHOLD_SHIFT); 480 if (ret < 0) 481 return ret; 482 } 483 484 if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) { 485 val = smb->pdata->soft_cold_temp_limit; 486 487 val = clamp_val(val, 0, 15); 488 val /= 5; 489 /* this goes from higher to lower so invert the value */ 490 val = ~val & 0x3; 491 492 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, 493 CFG_TEMP_LIMIT_SOFT_COLD_MASK, 494 val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT); 495 if (ret < 0) 496 return ret; 497 498 enable_therm_monitor = true; 499 } 500 501 if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) { 502 val = smb->pdata->soft_hot_temp_limit; 503 504 val = clamp_val(val, 40, 55) - 40; 505 val /= 5; 506 507 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, 508 CFG_TEMP_LIMIT_SOFT_HOT_MASK, 509 val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT); 510 if (ret < 0) 511 return ret; 512 513 enable_therm_monitor = true; 514 } 515 516 if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) { 517 val = smb->pdata->hard_cold_temp_limit; 518 519 val = clamp_val(val, -5, 10) + 5; 520 val /= 5; 521 /* this goes from higher to lower so invert the value */ 522 val = ~val & 0x3; 523 524 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, 525 CFG_TEMP_LIMIT_HARD_COLD_MASK, 526 val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT); 527 if (ret < 0) 528 return ret; 529 530 enable_therm_monitor = true; 531 } 532 533 if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) { 534 val = smb->pdata->hard_hot_temp_limit; 535 536 val = clamp_val(val, 50, 65) - 50; 537 val /= 5; 538 539 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, 540 CFG_TEMP_LIMIT_HARD_HOT_MASK, 541 val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT); 542 if (ret < 0) 543 return ret; 544 545 enable_therm_monitor = true; 546 } 547 548 /* 549 * If any of the temperature limits are set, we also enable the 550 * thermistor monitoring. 551 * 552 * When soft limits are hit, the device will start to compensate 553 * current and/or voltage depending on the configuration. 554 * 555 * When hard limit is hit, the device will suspend charging 556 * depending on the configuration. 557 */ 558 if (enable_therm_monitor) { 559 ret = regmap_update_bits(smb->regmap, CFG_THERM, 560 CFG_THERM_MONITOR_DISABLED, 0); 561 if (ret < 0) 562 return ret; 563 } 564 565 if (smb->pdata->suspend_on_hard_temp_limit) { 566 ret = regmap_update_bits(smb->regmap, CFG_SYSOK, 567 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0); 568 if (ret < 0) 569 return ret; 570 } 571 572 if (smb->pdata->soft_temp_limit_compensation != 573 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) { 574 val = smb->pdata->soft_temp_limit_compensation & 0x3; 575 576 ret = regmap_update_bits(smb->regmap, CFG_THERM, 577 CFG_THERM_SOFT_HOT_COMPENSATION_MASK, 578 val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT); 579 if (ret < 0) 580 return ret; 581 582 ret = regmap_update_bits(smb->regmap, CFG_THERM, 583 CFG_THERM_SOFT_COLD_COMPENSATION_MASK, 584 val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT); 585 if (ret < 0) 586 return ret; 587 } 588 589 if (smb->pdata->charge_current_compensation) { 590 val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl), 591 smb->pdata->charge_current_compensation); 592 if (val < 0) 593 return val; 594 595 ret = regmap_update_bits(smb->regmap, CFG_OTG, 596 CFG_OTG_CC_COMPENSATION_MASK, 597 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT); 598 if (ret < 0) 599 return ret; 600 } 601 602 return ret; 603 } 604 605 /* 606 * smb347_set_writable - enables/disables writing to non-volatile registers 607 * @smb: pointer to smb347 charger instance 608 * 609 * You can enable/disable writing to the non-volatile configuration 610 * registers by calling this function. 611 * 612 * Returns %0 on success and negative errno in case of failure. 613 */ 614 static int smb347_set_writable(struct smb347_charger *smb, bool writable) 615 { 616 return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE, 617 writable ? CMD_A_ALLOW_WRITE : 0); 618 } 619 620 static int smb347_hw_init(struct smb347_charger *smb) 621 { 622 unsigned int val; 623 int ret; 624 625 ret = smb347_set_writable(smb, true); 626 if (ret < 0) 627 return ret; 628 629 /* 630 * Program the platform specific configuration values to the device 631 * first. 632 */ 633 ret = smb347_set_charge_current(smb); 634 if (ret < 0) 635 goto fail; 636 637 ret = smb347_set_current_limits(smb); 638 if (ret < 0) 639 goto fail; 640 641 ret = smb347_set_voltage_limits(smb); 642 if (ret < 0) 643 goto fail; 644 645 ret = smb347_set_temp_limits(smb); 646 if (ret < 0) 647 goto fail; 648 649 /* If USB charging is disabled we put the USB in suspend mode */ 650 if (!smb->pdata->use_usb) { 651 ret = regmap_update_bits(smb->regmap, CMD_A, 652 CMD_A_SUSPEND_ENABLED, 653 CMD_A_SUSPEND_ENABLED); 654 if (ret < 0) 655 goto fail; 656 } 657 658 /* 659 * If configured by platform data, we enable hardware Auto-OTG 660 * support for driving VBUS. Otherwise we disable it. 661 */ 662 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK, 663 smb->pdata->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0); 664 if (ret < 0) 665 goto fail; 666 667 /* 668 * Make the charging functionality controllable by a write to the 669 * command register unless pin control is specified in the platform 670 * data. 671 */ 672 switch (smb->pdata->enable_control) { 673 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW: 674 val = CFG_PIN_EN_CTRL_ACTIVE_LOW; 675 break; 676 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH: 677 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH; 678 break; 679 default: 680 val = 0; 681 break; 682 } 683 684 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK, 685 val); 686 if (ret < 0) 687 goto fail; 688 689 /* Disable Automatic Power Source Detection (APSD) interrupt. */ 690 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0); 691 if (ret < 0) 692 goto fail; 693 694 ret = smb347_update_ps_status(smb); 695 if (ret < 0) 696 goto fail; 697 698 ret = smb347_start_stop_charging(smb); 699 700 fail: 701 smb347_set_writable(smb, false); 702 return ret; 703 } 704 705 static irqreturn_t smb347_interrupt(int irq, void *data) 706 { 707 struct smb347_charger *smb = data; 708 unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e; 709 bool handled = false; 710 int ret; 711 712 /* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */ 713 usleep_range(25000, 35000); 714 715 ret = regmap_read(smb->regmap, STAT_C, &stat_c); 716 if (ret < 0) { 717 dev_warn(smb->dev, "reading STAT_C failed\n"); 718 return IRQ_NONE; 719 } 720 721 ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c); 722 if (ret < 0) { 723 dev_warn(smb->dev, "reading IRQSTAT_C failed\n"); 724 return IRQ_NONE; 725 } 726 727 ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d); 728 if (ret < 0) { 729 dev_warn(smb->dev, "reading IRQSTAT_D failed\n"); 730 return IRQ_NONE; 731 } 732 733 ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e); 734 if (ret < 0) { 735 dev_warn(smb->dev, "reading IRQSTAT_E failed\n"); 736 return IRQ_NONE; 737 } 738 739 /* 740 * If we get charger error we report the error back to user. 741 * If the error is recovered charging will resume again. 742 */ 743 if (stat_c & STAT_C_CHARGER_ERROR) { 744 dev_err(smb->dev, "charging stopped due to charger error\n"); 745 power_supply_changed(smb->battery); 746 handled = true; 747 } 748 749 /* 750 * If we reached the termination current the battery is charged and 751 * we can update the status now. Charging is automatically 752 * disabled by the hardware. 753 */ 754 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) { 755 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) 756 power_supply_changed(smb->battery); 757 dev_dbg(smb->dev, "going to HW maintenance mode\n"); 758 handled = true; 759 } 760 761 /* 762 * If we got a charger timeout INT that means the charge 763 * full is not detected with in charge timeout value. 764 */ 765 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) { 766 dev_dbg(smb->dev, "total Charge Timeout INT received\n"); 767 768 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT) 769 dev_warn(smb->dev, "charging stopped due to timeout\n"); 770 power_supply_changed(smb->battery); 771 handled = true; 772 } 773 774 /* 775 * If we got an under voltage interrupt it means that AC/USB input 776 * was connected or disconnected. 777 */ 778 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) { 779 if (smb347_update_ps_status(smb) > 0) { 780 smb347_start_stop_charging(smb); 781 if (smb->pdata->use_mains) 782 power_supply_changed(smb->mains); 783 if (smb->pdata->use_usb) 784 power_supply_changed(smb->usb); 785 } 786 handled = true; 787 } 788 789 return handled ? IRQ_HANDLED : IRQ_NONE; 790 } 791 792 static int smb347_irq_set(struct smb347_charger *smb, bool enable) 793 { 794 int ret; 795 796 ret = smb347_set_writable(smb, true); 797 if (ret < 0) 798 return ret; 799 800 /* 801 * Enable/disable interrupts for: 802 * - under voltage 803 * - termination current reached 804 * - charger timeout 805 * - charger error 806 */ 807 ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff, 808 enable ? CFG_FAULT_IRQ_DCIN_UV : 0); 809 if (ret < 0) 810 goto fail; 811 812 ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff, 813 enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER | 814 CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0); 815 if (ret < 0) 816 goto fail; 817 818 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR, 819 enable ? CFG_PIN_EN_CHARGER_ERROR : 0); 820 fail: 821 smb347_set_writable(smb, false); 822 return ret; 823 } 824 825 static inline int smb347_irq_enable(struct smb347_charger *smb) 826 { 827 return smb347_irq_set(smb, true); 828 } 829 830 static inline int smb347_irq_disable(struct smb347_charger *smb) 831 { 832 return smb347_irq_set(smb, false); 833 } 834 835 static int smb347_irq_init(struct smb347_charger *smb, 836 struct i2c_client *client) 837 { 838 const struct smb347_charger_platform_data *pdata = smb->pdata; 839 int ret, irq = gpio_to_irq(pdata->irq_gpio); 840 841 ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name); 842 if (ret < 0) 843 goto fail; 844 845 ret = request_threaded_irq(irq, NULL, smb347_interrupt, 846 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 847 client->name, smb); 848 if (ret < 0) 849 goto fail_gpio; 850 851 ret = smb347_set_writable(smb, true); 852 if (ret < 0) 853 goto fail_irq; 854 855 /* 856 * Configure the STAT output to be suitable for interrupts: disable 857 * all other output (except interrupts) and make it active low. 858 */ 859 ret = regmap_update_bits(smb->regmap, CFG_STAT, 860 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED, 861 CFG_STAT_DISABLED); 862 if (ret < 0) 863 goto fail_readonly; 864 865 smb347_set_writable(smb, false); 866 client->irq = irq; 867 return 0; 868 869 fail_readonly: 870 smb347_set_writable(smb, false); 871 fail_irq: 872 free_irq(irq, smb); 873 fail_gpio: 874 gpio_free(pdata->irq_gpio); 875 fail: 876 client->irq = 0; 877 return ret; 878 } 879 880 /* 881 * Returns the constant charge current programmed 882 * into the charger in uA. 883 */ 884 static int get_const_charge_current(struct smb347_charger *smb) 885 { 886 int ret, intval; 887 unsigned int v; 888 889 if (!smb347_is_ps_online(smb)) 890 return -ENODATA; 891 892 ret = regmap_read(smb->regmap, STAT_B, &v); 893 if (ret < 0) 894 return ret; 895 896 /* 897 * The current value is composition of FCC and PCC values 898 * and we can detect which table to use from bit 5. 899 */ 900 if (v & 0x20) { 901 intval = hw_to_current(fcc_tbl, ARRAY_SIZE(fcc_tbl), v & 7); 902 } else { 903 v >>= 3; 904 intval = hw_to_current(pcc_tbl, ARRAY_SIZE(pcc_tbl), v & 7); 905 } 906 907 return intval; 908 } 909 910 /* 911 * Returns the constant charge voltage programmed 912 * into the charger in uV. 913 */ 914 static int get_const_charge_voltage(struct smb347_charger *smb) 915 { 916 int ret, intval; 917 unsigned int v; 918 919 if (!smb347_is_ps_online(smb)) 920 return -ENODATA; 921 922 ret = regmap_read(smb->regmap, STAT_A, &v); 923 if (ret < 0) 924 return ret; 925 926 v &= STAT_A_FLOAT_VOLTAGE_MASK; 927 if (v > 0x3d) 928 v = 0x3d; 929 930 intval = 3500000 + v * 20000; 931 932 return intval; 933 } 934 935 static int smb347_mains_get_property(struct power_supply *psy, 936 enum power_supply_property prop, 937 union power_supply_propval *val) 938 { 939 struct smb347_charger *smb = power_supply_get_drvdata(psy); 940 int ret; 941 942 switch (prop) { 943 case POWER_SUPPLY_PROP_ONLINE: 944 val->intval = smb->mains_online; 945 break; 946 947 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 948 ret = get_const_charge_voltage(smb); 949 if (ret < 0) 950 return ret; 951 else 952 val->intval = ret; 953 break; 954 955 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 956 ret = get_const_charge_current(smb); 957 if (ret < 0) 958 return ret; 959 else 960 val->intval = ret; 961 break; 962 963 default: 964 return -EINVAL; 965 } 966 967 return 0; 968 } 969 970 static enum power_supply_property smb347_mains_properties[] = { 971 POWER_SUPPLY_PROP_ONLINE, 972 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 973 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 974 }; 975 976 static int smb347_usb_get_property(struct power_supply *psy, 977 enum power_supply_property prop, 978 union power_supply_propval *val) 979 { 980 struct smb347_charger *smb = power_supply_get_drvdata(psy); 981 int ret; 982 983 switch (prop) { 984 case POWER_SUPPLY_PROP_ONLINE: 985 val->intval = smb->usb_online; 986 break; 987 988 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 989 ret = get_const_charge_voltage(smb); 990 if (ret < 0) 991 return ret; 992 else 993 val->intval = ret; 994 break; 995 996 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 997 ret = get_const_charge_current(smb); 998 if (ret < 0) 999 return ret; 1000 else 1001 val->intval = ret; 1002 break; 1003 1004 default: 1005 return -EINVAL; 1006 } 1007 1008 return 0; 1009 } 1010 1011 static enum power_supply_property smb347_usb_properties[] = { 1012 POWER_SUPPLY_PROP_ONLINE, 1013 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 1014 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 1015 }; 1016 1017 static int smb347_get_charging_status(struct smb347_charger *smb) 1018 { 1019 int ret, status; 1020 unsigned int val; 1021 1022 if (!smb347_is_ps_online(smb)) 1023 return POWER_SUPPLY_STATUS_DISCHARGING; 1024 1025 ret = regmap_read(smb->regmap, STAT_C, &val); 1026 if (ret < 0) 1027 return ret; 1028 1029 if ((val & STAT_C_CHARGER_ERROR) || 1030 (val & STAT_C_HOLDOFF_STAT)) { 1031 /* 1032 * set to NOT CHARGING upon charger error 1033 * or charging has stopped. 1034 */ 1035 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1036 } else { 1037 if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) { 1038 /* 1039 * set to charging if battery is in pre-charge, 1040 * fast charge or taper charging mode. 1041 */ 1042 status = POWER_SUPPLY_STATUS_CHARGING; 1043 } else if (val & STAT_C_CHG_TERM) { 1044 /* 1045 * set the status to FULL if battery is not in pre 1046 * charge, fast charge or taper charging mode AND 1047 * charging is terminated at least once. 1048 */ 1049 status = POWER_SUPPLY_STATUS_FULL; 1050 } else { 1051 /* 1052 * in this case no charger error or termination 1053 * occured but charging is not in progress!!! 1054 */ 1055 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1056 } 1057 } 1058 1059 return status; 1060 } 1061 1062 static int smb347_battery_get_property(struct power_supply *psy, 1063 enum power_supply_property prop, 1064 union power_supply_propval *val) 1065 { 1066 struct smb347_charger *smb = power_supply_get_drvdata(psy); 1067 const struct smb347_charger_platform_data *pdata = smb->pdata; 1068 int ret; 1069 1070 ret = smb347_update_ps_status(smb); 1071 if (ret < 0) 1072 return ret; 1073 1074 switch (prop) { 1075 case POWER_SUPPLY_PROP_STATUS: 1076 ret = smb347_get_charging_status(smb); 1077 if (ret < 0) 1078 return ret; 1079 val->intval = ret; 1080 break; 1081 1082 case POWER_SUPPLY_PROP_CHARGE_TYPE: 1083 if (!smb347_is_ps_online(smb)) 1084 return -ENODATA; 1085 1086 /* 1087 * We handle trickle and pre-charging the same, and taper 1088 * and none the same. 1089 */ 1090 switch (smb347_charging_status(smb)) { 1091 case 1: 1092 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; 1093 break; 1094 case 2: 1095 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; 1096 break; 1097 default: 1098 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; 1099 break; 1100 } 1101 break; 1102 1103 case POWER_SUPPLY_PROP_TECHNOLOGY: 1104 val->intval = pdata->battery_info.technology; 1105 break; 1106 1107 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 1108 val->intval = pdata->battery_info.voltage_min_design; 1109 break; 1110 1111 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 1112 val->intval = pdata->battery_info.voltage_max_design; 1113 break; 1114 1115 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 1116 val->intval = pdata->battery_info.charge_full_design; 1117 break; 1118 1119 case POWER_SUPPLY_PROP_MODEL_NAME: 1120 val->strval = pdata->battery_info.name; 1121 break; 1122 1123 default: 1124 return -EINVAL; 1125 } 1126 1127 return 0; 1128 } 1129 1130 static enum power_supply_property smb347_battery_properties[] = { 1131 POWER_SUPPLY_PROP_STATUS, 1132 POWER_SUPPLY_PROP_CHARGE_TYPE, 1133 POWER_SUPPLY_PROP_TECHNOLOGY, 1134 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 1135 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 1136 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 1137 POWER_SUPPLY_PROP_MODEL_NAME, 1138 }; 1139 1140 static bool smb347_volatile_reg(struct device *dev, unsigned int reg) 1141 { 1142 switch (reg) { 1143 case IRQSTAT_A: 1144 case IRQSTAT_C: 1145 case IRQSTAT_D: 1146 case IRQSTAT_E: 1147 case IRQSTAT_F: 1148 case STAT_A: 1149 case STAT_B: 1150 case STAT_C: 1151 case STAT_E: 1152 return true; 1153 } 1154 1155 return false; 1156 } 1157 1158 static bool smb347_readable_reg(struct device *dev, unsigned int reg) 1159 { 1160 switch (reg) { 1161 case CFG_CHARGE_CURRENT: 1162 case CFG_CURRENT_LIMIT: 1163 case CFG_FLOAT_VOLTAGE: 1164 case CFG_STAT: 1165 case CFG_PIN: 1166 case CFG_THERM: 1167 case CFG_SYSOK: 1168 case CFG_OTHER: 1169 case CFG_OTG: 1170 case CFG_TEMP_LIMIT: 1171 case CFG_FAULT_IRQ: 1172 case CFG_STATUS_IRQ: 1173 case CFG_ADDRESS: 1174 case CMD_A: 1175 case CMD_B: 1176 case CMD_C: 1177 return true; 1178 } 1179 1180 return smb347_volatile_reg(dev, reg); 1181 } 1182 1183 static const struct regmap_config smb347_regmap = { 1184 .reg_bits = 8, 1185 .val_bits = 8, 1186 .max_register = SMB347_MAX_REGISTER, 1187 .volatile_reg = smb347_volatile_reg, 1188 .readable_reg = smb347_readable_reg, 1189 }; 1190 1191 static const struct power_supply_desc smb347_mains_desc = { 1192 .name = "smb347-mains", 1193 .type = POWER_SUPPLY_TYPE_MAINS, 1194 .get_property = smb347_mains_get_property, 1195 .properties = smb347_mains_properties, 1196 .num_properties = ARRAY_SIZE(smb347_mains_properties), 1197 }; 1198 1199 static const struct power_supply_desc smb347_usb_desc = { 1200 .name = "smb347-usb", 1201 .type = POWER_SUPPLY_TYPE_USB, 1202 .get_property = smb347_usb_get_property, 1203 .properties = smb347_usb_properties, 1204 .num_properties = ARRAY_SIZE(smb347_usb_properties), 1205 }; 1206 1207 static const struct power_supply_desc smb347_battery_desc = { 1208 .name = "smb347-battery", 1209 .type = POWER_SUPPLY_TYPE_BATTERY, 1210 .get_property = smb347_battery_get_property, 1211 .properties = smb347_battery_properties, 1212 .num_properties = ARRAY_SIZE(smb347_battery_properties), 1213 }; 1214 1215 static int smb347_probe(struct i2c_client *client, 1216 const struct i2c_device_id *id) 1217 { 1218 static char *battery[] = { "smb347-battery" }; 1219 const struct smb347_charger_platform_data *pdata; 1220 struct power_supply_config mains_usb_cfg = {}, battery_cfg = {}; 1221 struct device *dev = &client->dev; 1222 struct smb347_charger *smb; 1223 int ret; 1224 1225 pdata = dev->platform_data; 1226 if (!pdata) 1227 return -EINVAL; 1228 1229 if (!pdata->use_mains && !pdata->use_usb) 1230 return -EINVAL; 1231 1232 smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL); 1233 if (!smb) 1234 return -ENOMEM; 1235 1236 i2c_set_clientdata(client, smb); 1237 1238 mutex_init(&smb->lock); 1239 smb->dev = &client->dev; 1240 smb->pdata = pdata; 1241 1242 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap); 1243 if (IS_ERR(smb->regmap)) 1244 return PTR_ERR(smb->regmap); 1245 1246 ret = smb347_hw_init(smb); 1247 if (ret < 0) 1248 return ret; 1249 1250 mains_usb_cfg.supplied_to = battery; 1251 mains_usb_cfg.num_supplicants = ARRAY_SIZE(battery); 1252 mains_usb_cfg.drv_data = smb; 1253 if (smb->pdata->use_mains) { 1254 smb->mains = power_supply_register(dev, &smb347_mains_desc, 1255 &mains_usb_cfg); 1256 if (IS_ERR(smb->mains)) 1257 return PTR_ERR(smb->mains); 1258 } 1259 1260 if (smb->pdata->use_usb) { 1261 smb->usb = power_supply_register(dev, &smb347_usb_desc, 1262 &mains_usb_cfg); 1263 if (IS_ERR(smb->usb)) { 1264 if (smb->pdata->use_mains) 1265 power_supply_unregister(smb->mains); 1266 return PTR_ERR(smb->usb); 1267 } 1268 } 1269 1270 battery_cfg.drv_data = smb; 1271 smb->battery = power_supply_register(dev, &smb347_battery_desc, 1272 &battery_cfg); 1273 if (IS_ERR(smb->battery)) { 1274 if (smb->pdata->use_usb) 1275 power_supply_unregister(smb->usb); 1276 if (smb->pdata->use_mains) 1277 power_supply_unregister(smb->mains); 1278 return PTR_ERR(smb->battery); 1279 } 1280 1281 /* 1282 * Interrupt pin is optional. If it is connected, we setup the 1283 * interrupt support here. 1284 */ 1285 if (pdata->irq_gpio >= 0) { 1286 ret = smb347_irq_init(smb, client); 1287 if (ret < 0) { 1288 dev_warn(dev, "failed to initialize IRQ: %d\n", ret); 1289 dev_warn(dev, "disabling IRQ support\n"); 1290 } else { 1291 smb347_irq_enable(smb); 1292 } 1293 } 1294 1295 return 0; 1296 } 1297 1298 static int smb347_remove(struct i2c_client *client) 1299 { 1300 struct smb347_charger *smb = i2c_get_clientdata(client); 1301 1302 if (client->irq) { 1303 smb347_irq_disable(smb); 1304 free_irq(client->irq, smb); 1305 gpio_free(smb->pdata->irq_gpio); 1306 } 1307 1308 power_supply_unregister(smb->battery); 1309 if (smb->pdata->use_usb) 1310 power_supply_unregister(smb->usb); 1311 if (smb->pdata->use_mains) 1312 power_supply_unregister(smb->mains); 1313 return 0; 1314 } 1315 1316 static const struct i2c_device_id smb347_id[] = { 1317 { "smb347", 0 }, 1318 { } 1319 }; 1320 MODULE_DEVICE_TABLE(i2c, smb347_id); 1321 1322 static struct i2c_driver smb347_driver = { 1323 .driver = { 1324 .name = "smb347", 1325 }, 1326 .probe = smb347_probe, 1327 .remove = smb347_remove, 1328 .id_table = smb347_id, 1329 }; 1330 1331 module_i2c_driver(smb347_driver); 1332 1333 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>"); 1334 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); 1335 MODULE_DESCRIPTION("SMB347 battery charger driver"); 1336 MODULE_LICENSE("GPL"); 1337