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