1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // Fuel gauge driver for Maxim 17042 / 8966 / 8997 4 // Note that Maxim 8966 and 8997 are mfd and this is its subdevice. 5 // 6 // Copyright (C) 2011 Samsung Electronics 7 // MyungJoo Ham <myungjoo.ham@samsung.com> 8 // 9 // This driver is based on max17040_battery.c 10 11 #include <linux/acpi.h> 12 #include <linux/init.h> 13 #include <linux/module.h> 14 #include <linux/slab.h> 15 #include <linux/i2c.h> 16 #include <linux/delay.h> 17 #include <linux/interrupt.h> 18 #include <linux/pm.h> 19 #include <linux/mod_devicetable.h> 20 #include <linux/power_supply.h> 21 #include <linux/power/max17042_battery.h> 22 #include <linux/of.h> 23 #include <linux/regmap.h> 24 25 /* Status register bits */ 26 #define STATUS_POR_BIT (1 << 1) 27 #define STATUS_BST_BIT (1 << 3) 28 #define STATUS_VMN_BIT (1 << 8) 29 #define STATUS_TMN_BIT (1 << 9) 30 #define STATUS_SMN_BIT (1 << 10) 31 #define STATUS_BI_BIT (1 << 11) 32 #define STATUS_VMX_BIT (1 << 12) 33 #define STATUS_TMX_BIT (1 << 13) 34 #define STATUS_SMX_BIT (1 << 14) 35 #define STATUS_BR_BIT (1 << 15) 36 37 /* Interrupt mask bits */ 38 #define CONFIG_ALRT_BIT_ENBL (1 << 2) 39 40 #define VFSOC0_LOCK 0x0000 41 #define VFSOC0_UNLOCK 0x0080 42 #define MODEL_UNLOCK1 0X0059 43 #define MODEL_UNLOCK2 0X00C4 44 #define MODEL_LOCK1 0X0000 45 #define MODEL_LOCK2 0X0000 46 47 #define dQ_ACC_DIV 0x4 48 #define dP_ACC_100 0x1900 49 #define dP_ACC_200 0x3200 50 51 #define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */ 52 53 struct max17042_chip { 54 struct i2c_client *client; 55 struct regmap *regmap; 56 struct power_supply *battery; 57 enum max170xx_chip_type chip_type; 58 struct max17042_platform_data *pdata; 59 struct work_struct work; 60 int init_complete; 61 }; 62 63 static enum power_supply_property max17042_battery_props[] = { 64 POWER_SUPPLY_PROP_STATUS, 65 POWER_SUPPLY_PROP_PRESENT, 66 POWER_SUPPLY_PROP_TECHNOLOGY, 67 POWER_SUPPLY_PROP_CYCLE_COUNT, 68 POWER_SUPPLY_PROP_VOLTAGE_MAX, 69 POWER_SUPPLY_PROP_VOLTAGE_MIN, 70 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 71 POWER_SUPPLY_PROP_VOLTAGE_NOW, 72 POWER_SUPPLY_PROP_VOLTAGE_AVG, 73 POWER_SUPPLY_PROP_VOLTAGE_OCV, 74 POWER_SUPPLY_PROP_CAPACITY, 75 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 76 POWER_SUPPLY_PROP_CHARGE_FULL, 77 POWER_SUPPLY_PROP_CHARGE_NOW, 78 POWER_SUPPLY_PROP_CHARGE_COUNTER, 79 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 80 POWER_SUPPLY_PROP_TEMP, 81 POWER_SUPPLY_PROP_TEMP_ALERT_MIN, 82 POWER_SUPPLY_PROP_TEMP_ALERT_MAX, 83 POWER_SUPPLY_PROP_TEMP_MIN, 84 POWER_SUPPLY_PROP_TEMP_MAX, 85 POWER_SUPPLY_PROP_HEALTH, 86 POWER_SUPPLY_PROP_SCOPE, 87 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 88 // these two have to be at the end on the list 89 POWER_SUPPLY_PROP_CURRENT_NOW, 90 POWER_SUPPLY_PROP_CURRENT_AVG, 91 }; 92 93 static int max17042_get_temperature(struct max17042_chip *chip, int *temp) 94 { 95 int ret; 96 u32 data; 97 struct regmap *map = chip->regmap; 98 99 ret = regmap_read(map, MAX17042_TEMP, &data); 100 if (ret < 0) 101 return ret; 102 103 *temp = sign_extend32(data, 15); 104 /* The value is converted into deci-centigrade scale */ 105 /* Units of LSB = 1 / 256 degree Celsius */ 106 *temp = *temp * 10 / 256; 107 return 0; 108 } 109 110 static int max17042_get_status(struct max17042_chip *chip, int *status) 111 { 112 int ret, charge_full, charge_now; 113 int avg_current; 114 u32 data; 115 116 ret = power_supply_am_i_supplied(chip->battery); 117 if (ret < 0) { 118 *status = POWER_SUPPLY_STATUS_UNKNOWN; 119 return 0; 120 } 121 if (ret == 0) { 122 *status = POWER_SUPPLY_STATUS_DISCHARGING; 123 return 0; 124 } 125 126 /* 127 * The MAX170xx has builtin end-of-charge detection and will update 128 * FullCAP to match RepCap when it detects end of charging. 129 * 130 * When this cycle the battery gets charged to a higher (calculated) 131 * capacity then the previous cycle then FullCAP will get updated 132 * continuously once end-of-charge detection kicks in, so allow the 133 * 2 to differ a bit. 134 */ 135 136 ret = regmap_read(chip->regmap, MAX17042_FullCAP, &charge_full); 137 if (ret < 0) 138 return ret; 139 140 ret = regmap_read(chip->regmap, MAX17042_RepCap, &charge_now); 141 if (ret < 0) 142 return ret; 143 144 if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD) { 145 *status = POWER_SUPPLY_STATUS_FULL; 146 return 0; 147 } 148 149 /* 150 * Even though we are supplied, we may still be discharging if the 151 * supply is e.g. only delivering 5V 0.5A. Check current if available. 152 */ 153 if (!chip->pdata->enable_current_sense) { 154 *status = POWER_SUPPLY_STATUS_CHARGING; 155 return 0; 156 } 157 158 ret = regmap_read(chip->regmap, MAX17042_AvgCurrent, &data); 159 if (ret < 0) 160 return ret; 161 162 avg_current = sign_extend32(data, 15); 163 avg_current *= 1562500 / chip->pdata->r_sns; 164 165 if (avg_current > 0) 166 *status = POWER_SUPPLY_STATUS_CHARGING; 167 else 168 *status = POWER_SUPPLY_STATUS_DISCHARGING; 169 170 return 0; 171 } 172 173 static int max17042_get_battery_health(struct max17042_chip *chip, int *health) 174 { 175 int temp, vavg, vbatt, ret; 176 u32 val; 177 178 ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, &val); 179 if (ret < 0) 180 goto health_error; 181 182 /* bits [0-3] unused */ 183 vavg = val * 625 / 8; 184 /* Convert to millivolts */ 185 vavg /= 1000; 186 187 ret = regmap_read(chip->regmap, MAX17042_VCELL, &val); 188 if (ret < 0) 189 goto health_error; 190 191 /* bits [0-3] unused */ 192 vbatt = val * 625 / 8; 193 /* Convert to millivolts */ 194 vbatt /= 1000; 195 196 if (vavg < chip->pdata->vmin) { 197 *health = POWER_SUPPLY_HEALTH_DEAD; 198 goto out; 199 } 200 201 if (vbatt > chip->pdata->vmax + MAX17042_VMAX_TOLERANCE) { 202 *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 203 goto out; 204 } 205 206 ret = max17042_get_temperature(chip, &temp); 207 if (ret < 0) 208 goto health_error; 209 210 if (temp < chip->pdata->temp_min) { 211 *health = POWER_SUPPLY_HEALTH_COLD; 212 goto out; 213 } 214 215 if (temp > chip->pdata->temp_max) { 216 *health = POWER_SUPPLY_HEALTH_OVERHEAT; 217 goto out; 218 } 219 220 *health = POWER_SUPPLY_HEALTH_GOOD; 221 222 out: 223 return 0; 224 225 health_error: 226 return ret; 227 } 228 229 static int max17042_get_property(struct power_supply *psy, 230 enum power_supply_property psp, 231 union power_supply_propval *val) 232 { 233 struct max17042_chip *chip = power_supply_get_drvdata(psy); 234 struct regmap *map = chip->regmap; 235 int ret; 236 u32 data; 237 u64 data64; 238 239 if (!chip->init_complete) 240 return -EAGAIN; 241 242 switch (psp) { 243 case POWER_SUPPLY_PROP_STATUS: 244 ret = max17042_get_status(chip, &val->intval); 245 if (ret < 0) 246 return ret; 247 break; 248 case POWER_SUPPLY_PROP_PRESENT: 249 ret = regmap_read(map, MAX17042_STATUS, &data); 250 if (ret < 0) 251 return ret; 252 253 if (data & MAX17042_STATUS_BattAbsent) 254 val->intval = 0; 255 else 256 val->intval = 1; 257 break; 258 case POWER_SUPPLY_PROP_TECHNOLOGY: 259 val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 260 break; 261 case POWER_SUPPLY_PROP_CYCLE_COUNT: 262 ret = regmap_read(map, MAX17042_Cycles, &data); 263 if (ret < 0) 264 return ret; 265 266 val->intval = data; 267 break; 268 case POWER_SUPPLY_PROP_VOLTAGE_MAX: 269 ret = regmap_read(map, MAX17042_MinMaxVolt, &data); 270 if (ret < 0) 271 return ret; 272 273 val->intval = data >> 8; 274 val->intval *= 20000; /* Units of LSB = 20mV */ 275 break; 276 case POWER_SUPPLY_PROP_VOLTAGE_MIN: 277 ret = regmap_read(map, MAX17042_MinMaxVolt, &data); 278 if (ret < 0) 279 return ret; 280 281 val->intval = (data & 0xff) * 20000; /* Units of 20mV */ 282 break; 283 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 284 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) 285 ret = regmap_read(map, MAX17042_V_empty, &data); 286 else 287 ret = regmap_read(map, MAX17047_V_empty, &data); 288 if (ret < 0) 289 return ret; 290 291 val->intval = data >> 7; 292 val->intval *= 10000; /* Units of LSB = 10mV */ 293 break; 294 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 295 ret = regmap_read(map, MAX17042_VCELL, &data); 296 if (ret < 0) 297 return ret; 298 299 val->intval = data * 625 / 8; 300 break; 301 case POWER_SUPPLY_PROP_VOLTAGE_AVG: 302 ret = regmap_read(map, MAX17042_AvgVCELL, &data); 303 if (ret < 0) 304 return ret; 305 306 val->intval = data * 625 / 8; 307 break; 308 case POWER_SUPPLY_PROP_VOLTAGE_OCV: 309 ret = regmap_read(map, MAX17042_OCVInternal, &data); 310 if (ret < 0) 311 return ret; 312 313 val->intval = data * 625 / 8; 314 break; 315 case POWER_SUPPLY_PROP_CAPACITY: 316 if (chip->pdata->enable_current_sense) 317 ret = regmap_read(map, MAX17042_RepSOC, &data); 318 else 319 ret = regmap_read(map, MAX17042_VFSOC, &data); 320 if (ret < 0) 321 return ret; 322 323 val->intval = data >> 8; 324 break; 325 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 326 ret = regmap_read(map, MAX17042_DesignCap, &data); 327 if (ret < 0) 328 return ret; 329 330 data64 = data * 5000000ll; 331 do_div(data64, chip->pdata->r_sns); 332 val->intval = data64; 333 break; 334 case POWER_SUPPLY_PROP_CHARGE_FULL: 335 ret = regmap_read(map, MAX17042_FullCAP, &data); 336 if (ret < 0) 337 return ret; 338 339 data64 = data * 5000000ll; 340 do_div(data64, chip->pdata->r_sns); 341 val->intval = data64; 342 break; 343 case POWER_SUPPLY_PROP_CHARGE_NOW: 344 ret = regmap_read(map, MAX17042_RepCap, &data); 345 if (ret < 0) 346 return ret; 347 348 data64 = data * 5000000ll; 349 do_div(data64, chip->pdata->r_sns); 350 val->intval = data64; 351 break; 352 case POWER_SUPPLY_PROP_CHARGE_COUNTER: 353 ret = regmap_read(map, MAX17042_QH, &data); 354 if (ret < 0) 355 return ret; 356 357 data64 = sign_extend64(data, 15) * 5000000ll; 358 val->intval = div_s64(data64, chip->pdata->r_sns); 359 break; 360 case POWER_SUPPLY_PROP_TEMP: 361 ret = max17042_get_temperature(chip, &val->intval); 362 if (ret < 0) 363 return ret; 364 break; 365 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: 366 ret = regmap_read(map, MAX17042_TALRT_Th, &data); 367 if (ret < 0) 368 return ret; 369 /* LSB is Alert Minimum. In deci-centigrade */ 370 val->intval = sign_extend32(data & 0xff, 7) * 10; 371 break; 372 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 373 ret = regmap_read(map, MAX17042_TALRT_Th, &data); 374 if (ret < 0) 375 return ret; 376 /* MSB is Alert Maximum. In deci-centigrade */ 377 val->intval = sign_extend32(data >> 8, 7) * 10; 378 break; 379 case POWER_SUPPLY_PROP_TEMP_MIN: 380 val->intval = chip->pdata->temp_min; 381 break; 382 case POWER_SUPPLY_PROP_TEMP_MAX: 383 val->intval = chip->pdata->temp_max; 384 break; 385 case POWER_SUPPLY_PROP_HEALTH: 386 ret = max17042_get_battery_health(chip, &val->intval); 387 if (ret < 0) 388 return ret; 389 break; 390 case POWER_SUPPLY_PROP_SCOPE: 391 val->intval = POWER_SUPPLY_SCOPE_SYSTEM; 392 break; 393 case POWER_SUPPLY_PROP_CURRENT_NOW: 394 if (chip->pdata->enable_current_sense) { 395 ret = regmap_read(map, MAX17042_Current, &data); 396 if (ret < 0) 397 return ret; 398 399 data64 = sign_extend64(data, 15) * 1562500ll; 400 val->intval = div_s64(data64, chip->pdata->r_sns); 401 } else { 402 return -EINVAL; 403 } 404 break; 405 case POWER_SUPPLY_PROP_CURRENT_AVG: 406 if (chip->pdata->enable_current_sense) { 407 ret = regmap_read(map, MAX17042_AvgCurrent, &data); 408 if (ret < 0) 409 return ret; 410 411 data64 = sign_extend64(data, 15) * 1562500ll; 412 val->intval = div_s64(data64, chip->pdata->r_sns); 413 } else { 414 return -EINVAL; 415 } 416 break; 417 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 418 ret = regmap_read(map, MAX17042_ICHGTerm, &data); 419 if (ret < 0) 420 return ret; 421 422 data64 = data * 1562500ll; 423 val->intval = div_s64(data64, chip->pdata->r_sns); 424 break; 425 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 426 ret = regmap_read(map, MAX17042_TTE, &data); 427 if (ret < 0) 428 return ret; 429 430 val->intval = data * 5625 / 1000; 431 break; 432 default: 433 return -EINVAL; 434 } 435 return 0; 436 } 437 438 static int max17042_set_property(struct power_supply *psy, 439 enum power_supply_property psp, 440 const union power_supply_propval *val) 441 { 442 struct max17042_chip *chip = power_supply_get_drvdata(psy); 443 struct regmap *map = chip->regmap; 444 int ret = 0; 445 u32 data; 446 int8_t temp; 447 448 switch (psp) { 449 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: 450 ret = regmap_read(map, MAX17042_TALRT_Th, &data); 451 if (ret < 0) 452 return ret; 453 454 /* Input in deci-centigrade, convert to centigrade */ 455 temp = val->intval / 10; 456 /* force min < max */ 457 if (temp >= (int8_t)(data >> 8)) 458 temp = (int8_t)(data >> 8) - 1; 459 /* Write both MAX and MIN ALERT */ 460 data = (data & 0xff00) + temp; 461 ret = regmap_write(map, MAX17042_TALRT_Th, data); 462 break; 463 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 464 ret = regmap_read(map, MAX17042_TALRT_Th, &data); 465 if (ret < 0) 466 return ret; 467 468 /* Input in Deci-Centigrade, convert to centigrade */ 469 temp = val->intval / 10; 470 /* force max > min */ 471 if (temp <= (int8_t)(data & 0xff)) 472 temp = (int8_t)(data & 0xff) + 1; 473 /* Write both MAX and MIN ALERT */ 474 data = (data & 0xff) + (temp << 8); 475 ret = regmap_write(map, MAX17042_TALRT_Th, data); 476 break; 477 default: 478 ret = -EINVAL; 479 } 480 481 return ret; 482 } 483 484 static int max17042_property_is_writeable(struct power_supply *psy, 485 enum power_supply_property psp) 486 { 487 int ret; 488 489 switch (psp) { 490 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: 491 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: 492 ret = 1; 493 break; 494 default: 495 ret = 0; 496 } 497 498 return ret; 499 } 500 501 static void max17042_external_power_changed(struct power_supply *psy) 502 { 503 power_supply_changed(psy); 504 } 505 506 static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value) 507 { 508 int retries = 8; 509 int ret; 510 u32 read_value; 511 512 do { 513 ret = regmap_write(map, reg, value); 514 regmap_read(map, reg, &read_value); 515 if (read_value != value) { 516 ret = -EIO; 517 retries--; 518 } 519 } while (retries && read_value != value); 520 521 if (ret < 0) 522 pr_err("%s: err %d\n", __func__, ret); 523 524 return ret; 525 } 526 527 static inline void max17042_override_por(struct regmap *map, 528 u8 reg, u16 value) 529 { 530 if (value) 531 regmap_write(map, reg, value); 532 } 533 534 static inline void max17042_unlock_model(struct max17042_chip *chip) 535 { 536 struct regmap *map = chip->regmap; 537 538 regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1); 539 regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2); 540 } 541 542 static inline void max17042_lock_model(struct max17042_chip *chip) 543 { 544 struct regmap *map = chip->regmap; 545 546 regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1); 547 regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2); 548 } 549 550 static inline void max17042_write_model_data(struct max17042_chip *chip, 551 u8 addr, int size) 552 { 553 struct regmap *map = chip->regmap; 554 int i; 555 556 for (i = 0; i < size; i++) 557 regmap_write(map, addr + i, 558 chip->pdata->config_data->cell_char_tbl[i]); 559 } 560 561 static inline void max17042_read_model_data(struct max17042_chip *chip, 562 u8 addr, u16 *data, int size) 563 { 564 struct regmap *map = chip->regmap; 565 int i; 566 u32 tmp; 567 568 for (i = 0; i < size; i++) { 569 regmap_read(map, addr + i, &tmp); 570 data[i] = (u16)tmp; 571 } 572 } 573 574 static inline int max17042_model_data_compare(struct max17042_chip *chip, 575 u16 *data1, u16 *data2, int size) 576 { 577 int i; 578 579 if (memcmp(data1, data2, size)) { 580 dev_err(&chip->client->dev, "%s compare failed\n", __func__); 581 for (i = 0; i < size; i++) 582 dev_info(&chip->client->dev, "0x%x, 0x%x", 583 data1[i], data2[i]); 584 dev_info(&chip->client->dev, "\n"); 585 return -EINVAL; 586 } 587 return 0; 588 } 589 590 static int max17042_init_model(struct max17042_chip *chip) 591 { 592 int ret; 593 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); 594 u16 *temp_data; 595 596 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL); 597 if (!temp_data) 598 return -ENOMEM; 599 600 max17042_unlock_model(chip); 601 max17042_write_model_data(chip, MAX17042_MODELChrTbl, 602 table_size); 603 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data, 604 table_size); 605 606 ret = max17042_model_data_compare( 607 chip, 608 chip->pdata->config_data->cell_char_tbl, 609 temp_data, 610 table_size); 611 612 max17042_lock_model(chip); 613 kfree(temp_data); 614 615 return ret; 616 } 617 618 static int max17042_verify_model_lock(struct max17042_chip *chip) 619 { 620 int i; 621 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); 622 u16 *temp_data; 623 int ret = 0; 624 625 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL); 626 if (!temp_data) 627 return -ENOMEM; 628 629 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data, 630 table_size); 631 for (i = 0; i < table_size; i++) 632 if (temp_data[i]) 633 ret = -EINVAL; 634 635 kfree(temp_data); 636 return ret; 637 } 638 639 static void max17042_write_config_regs(struct max17042_chip *chip) 640 { 641 struct max17042_config_data *config = chip->pdata->config_data; 642 struct regmap *map = chip->regmap; 643 644 regmap_write(map, MAX17042_CONFIG, config->config); 645 regmap_write(map, MAX17042_LearnCFG, config->learn_cfg); 646 regmap_write(map, MAX17042_FilterCFG, 647 config->filter_cfg); 648 regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg); 649 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 || 650 chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050 || 651 chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055) 652 regmap_write(map, MAX17047_FullSOCThr, 653 config->full_soc_thresh); 654 } 655 656 static void max17042_write_custom_regs(struct max17042_chip *chip) 657 { 658 struct max17042_config_data *config = chip->pdata->config_data; 659 struct regmap *map = chip->regmap; 660 661 max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0); 662 max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0); 663 max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term); 664 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) { 665 regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco); 666 max17042_write_verify_reg(map, MAX17042_K_empty0, 667 config->kempty0); 668 } else { 669 max17042_write_verify_reg(map, MAX17047_QRTbl00, 670 config->qrtbl00); 671 max17042_write_verify_reg(map, MAX17047_QRTbl10, 672 config->qrtbl10); 673 max17042_write_verify_reg(map, MAX17047_QRTbl20, 674 config->qrtbl20); 675 max17042_write_verify_reg(map, MAX17047_QRTbl30, 676 config->qrtbl30); 677 } 678 } 679 680 static void max17042_update_capacity_regs(struct max17042_chip *chip) 681 { 682 struct max17042_config_data *config = chip->pdata->config_data; 683 struct regmap *map = chip->regmap; 684 685 max17042_write_verify_reg(map, MAX17042_FullCAP, 686 config->fullcap); 687 regmap_write(map, MAX17042_DesignCap, config->design_cap); 688 max17042_write_verify_reg(map, MAX17042_FullCAPNom, 689 config->fullcapnom); 690 } 691 692 static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip) 693 { 694 unsigned int vfSoc; 695 struct regmap *map = chip->regmap; 696 697 regmap_read(map, MAX17042_VFSOC, &vfSoc); 698 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK); 699 max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc); 700 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK); 701 } 702 703 static void max17042_load_new_capacity_params(struct max17042_chip *chip) 704 { 705 u32 full_cap0, rep_cap, dq_acc, vfSoc; 706 u32 rem_cap; 707 708 struct max17042_config_data *config = chip->pdata->config_data; 709 struct regmap *map = chip->regmap; 710 711 regmap_read(map, MAX17042_FullCAP0, &full_cap0); 712 regmap_read(map, MAX17042_VFSOC, &vfSoc); 713 714 /* fg_vfSoc needs to shifted by 8 bits to get the 715 * perc in 1% accuracy, to get the right rem_cap multiply 716 * full_cap0, fg_vfSoc and devide by 100 717 */ 718 rem_cap = ((vfSoc >> 8) * full_cap0) / 100; 719 max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap); 720 721 rep_cap = rem_cap; 722 max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap); 723 724 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */ 725 dq_acc = config->fullcap / dQ_ACC_DIV; 726 max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc); 727 max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200); 728 729 max17042_write_verify_reg(map, MAX17042_FullCAP, 730 config->fullcap); 731 regmap_write(map, MAX17042_DesignCap, 732 config->design_cap); 733 max17042_write_verify_reg(map, MAX17042_FullCAPNom, 734 config->fullcapnom); 735 /* Update SOC register with new SOC */ 736 regmap_write(map, MAX17042_RepSOC, vfSoc); 737 } 738 739 /* 740 * Block write all the override values coming from platform data. 741 * This function MUST be called before the POR initialization procedure 742 * specified by maxim. 743 */ 744 static inline void max17042_override_por_values(struct max17042_chip *chip) 745 { 746 struct regmap *map = chip->regmap; 747 struct max17042_config_data *config = chip->pdata->config_data; 748 749 max17042_override_por(map, MAX17042_TGAIN, config->tgain); 750 max17042_override_por(map, MAX17042_TOFF, config->toff); 751 max17042_override_por(map, MAX17042_CGAIN, config->cgain); 752 max17042_override_por(map, MAX17042_COFF, config->coff); 753 754 max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh); 755 max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh); 756 max17042_override_por(map, MAX17042_SALRT_Th, 757 config->soc_alrt_thresh); 758 max17042_override_por(map, MAX17042_CONFIG, config->config); 759 max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer); 760 761 max17042_override_por(map, MAX17042_DesignCap, config->design_cap); 762 max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term); 763 764 max17042_override_por(map, MAX17042_AtRate, config->at_rate); 765 max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg); 766 max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg); 767 max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg); 768 max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg); 769 770 max17042_override_por(map, MAX17042_FullCAP, config->fullcap); 771 max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom); 772 max17042_override_por(map, MAX17042_dQacc, config->dqacc); 773 max17042_override_por(map, MAX17042_dPacc, config->dpacc); 774 775 max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0); 776 max17042_override_por(map, MAX17042_TempCo, config->tcompc0); 777 778 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) { 779 max17042_override_por(map, MAX17042_MaskSOC, config->masksoc); 780 max17042_override_por(map, MAX17042_SOC_empty, config->socempty); 781 max17042_override_por(map, MAX17042_V_empty, config->vempty); 782 max17042_override_por(map, MAX17042_EmptyTempCo, config->empty_tempco); 783 max17042_override_por(map, MAX17042_K_empty0, config->kempty0); 784 } 785 786 if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) || 787 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || 788 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)) { 789 max17042_override_por(map, MAX17042_IAvg_empty, config->iavg_empty); 790 max17042_override_por(map, MAX17042_TempNom, config->temp_nom); 791 max17042_override_por(map, MAX17042_TempLim, config->temp_lim); 792 max17042_override_por(map, MAX17042_FCTC, config->fctc); 793 } 794 795 if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || 796 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050) || 797 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055)) { 798 max17042_override_por(map, MAX17047_V_empty, config->vempty); 799 } 800 } 801 802 static int max17042_init_chip(struct max17042_chip *chip) 803 { 804 struct regmap *map = chip->regmap; 805 int ret; 806 807 max17042_override_por_values(chip); 808 /* After Power up, the MAX17042 requires 500mS in order 809 * to perform signal debouncing and initial SOC reporting 810 */ 811 msleep(500); 812 813 /* Initialize configuration */ 814 max17042_write_config_regs(chip); 815 816 /* write cell characterization data */ 817 ret = max17042_init_model(chip); 818 if (ret) { 819 dev_err(&chip->client->dev, "%s init failed\n", 820 __func__); 821 return -EIO; 822 } 823 824 ret = max17042_verify_model_lock(chip); 825 if (ret) { 826 dev_err(&chip->client->dev, "%s lock verify failed\n", 827 __func__); 828 return -EIO; 829 } 830 /* write custom parameters */ 831 max17042_write_custom_regs(chip); 832 833 /* update capacity params */ 834 max17042_update_capacity_regs(chip); 835 836 /* delay must be atleast 350mS to allow VFSOC 837 * to be calculated from the new configuration 838 */ 839 msleep(350); 840 841 /* reset vfsoc0 reg */ 842 max17042_reset_vfsoc0_reg(chip); 843 844 /* load new capacity params */ 845 max17042_load_new_capacity_params(chip); 846 847 /* Init complete, Clear the POR bit */ 848 regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0); 849 return 0; 850 } 851 852 static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off) 853 { 854 struct regmap *map = chip->regmap; 855 u32 soc, soc_tr; 856 857 /* program interrupt thresholds such that we should 858 * get interrupt for every 'off' perc change in the soc 859 */ 860 regmap_read(map, MAX17042_RepSOC, &soc); 861 soc >>= 8; 862 soc_tr = (soc + off) << 8; 863 if (off < soc) 864 soc_tr |= soc - off; 865 regmap_write(map, MAX17042_SALRT_Th, soc_tr); 866 } 867 868 static irqreturn_t max17042_thread_handler(int id, void *dev) 869 { 870 struct max17042_chip *chip = dev; 871 u32 val; 872 int ret; 873 874 ret = regmap_read(chip->regmap, MAX17042_STATUS, &val); 875 if (ret) 876 return IRQ_HANDLED; 877 878 if ((val & STATUS_SMN_BIT) || (val & STATUS_SMX_BIT)) { 879 dev_dbg(&chip->client->dev, "SOC threshold INTR\n"); 880 max17042_set_soc_threshold(chip, 1); 881 } 882 883 /* we implicitly handle all alerts via power_supply_changed */ 884 regmap_clear_bits(chip->regmap, MAX17042_STATUS, 885 0xFFFF & ~(STATUS_POR_BIT | STATUS_BST_BIT)); 886 887 power_supply_changed(chip->battery); 888 return IRQ_HANDLED; 889 } 890 891 static void max17042_init_worker(struct work_struct *work) 892 { 893 struct max17042_chip *chip = container_of(work, 894 struct max17042_chip, work); 895 int ret; 896 897 /* Initialize registers according to values from the platform data */ 898 if (chip->pdata->enable_por_init && chip->pdata->config_data) { 899 ret = max17042_init_chip(chip); 900 if (ret) 901 return; 902 } 903 904 chip->init_complete = 1; 905 } 906 907 #ifdef CONFIG_OF 908 static struct max17042_platform_data * 909 max17042_get_of_pdata(struct max17042_chip *chip) 910 { 911 struct device *dev = &chip->client->dev; 912 struct device_node *np = dev->of_node; 913 u32 prop; 914 struct max17042_platform_data *pdata; 915 916 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 917 if (!pdata) 918 return NULL; 919 920 /* 921 * Require current sense resistor value to be specified for 922 * current-sense functionality to be enabled at all. 923 */ 924 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) { 925 pdata->r_sns = prop; 926 pdata->enable_current_sense = true; 927 } 928 929 if (of_property_read_s32(np, "maxim,cold-temp", &pdata->temp_min)) 930 pdata->temp_min = INT_MIN; 931 if (of_property_read_s32(np, "maxim,over-heat-temp", &pdata->temp_max)) 932 pdata->temp_max = INT_MAX; 933 if (of_property_read_s32(np, "maxim,dead-volt", &pdata->vmin)) 934 pdata->vmin = INT_MIN; 935 if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax)) 936 pdata->vmax = INT_MAX; 937 938 return pdata; 939 } 940 #endif 941 942 static struct max17042_reg_data max17047_default_pdata_init_regs[] = { 943 /* 944 * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection 945 * when the voltage FG reports 95%, as recommended in the datasheet. 946 */ 947 { MAX17047_FullSOCThr, MAX17042_BATTERY_FULL << 8 }, 948 }; 949 950 static struct max17042_platform_data * 951 max17042_get_default_pdata(struct max17042_chip *chip) 952 { 953 struct device *dev = &chip->client->dev; 954 struct max17042_platform_data *pdata; 955 int ret, misc_cfg; 956 957 /* 958 * The MAX17047 gets used on x86 where we might not have pdata, assume 959 * the firmware will already have initialized the fuel-gauge and provide 960 * default values for the non init bits to make things work. 961 */ 962 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 963 if (!pdata) 964 return pdata; 965 966 if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || 967 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)) { 968 pdata->init_data = max17047_default_pdata_init_regs; 969 pdata->num_init_data = 970 ARRAY_SIZE(max17047_default_pdata_init_regs); 971 } 972 973 ret = regmap_read(chip->regmap, MAX17042_MiscCFG, &misc_cfg); 974 if (ret < 0) 975 return NULL; 976 977 /* If bits 0-1 are set to 3 then only Voltage readings are used */ 978 if ((misc_cfg & 0x3) == 0x3) 979 pdata->enable_current_sense = false; 980 else 981 pdata->enable_current_sense = true; 982 983 pdata->vmin = MAX17042_DEFAULT_VMIN; 984 pdata->vmax = MAX17042_DEFAULT_VMAX; 985 pdata->temp_min = MAX17042_DEFAULT_TEMP_MIN; 986 pdata->temp_max = MAX17042_DEFAULT_TEMP_MAX; 987 988 return pdata; 989 } 990 991 static struct max17042_platform_data * 992 max17042_get_pdata(struct max17042_chip *chip) 993 { 994 struct device *dev = &chip->client->dev; 995 996 #ifdef CONFIG_OF 997 if (dev->of_node) 998 return max17042_get_of_pdata(chip); 999 #endif 1000 if (dev->platform_data) 1001 return dev->platform_data; 1002 1003 return max17042_get_default_pdata(chip); 1004 } 1005 1006 static const struct regmap_config max17042_regmap_config = { 1007 .reg_bits = 8, 1008 .val_bits = 16, 1009 .val_format_endian = REGMAP_ENDIAN_NATIVE, 1010 }; 1011 1012 static const struct power_supply_desc max17042_psy_desc = { 1013 .name = "max170xx_battery", 1014 .type = POWER_SUPPLY_TYPE_BATTERY, 1015 .get_property = max17042_get_property, 1016 .set_property = max17042_set_property, 1017 .property_is_writeable = max17042_property_is_writeable, 1018 .external_power_changed = max17042_external_power_changed, 1019 .properties = max17042_battery_props, 1020 .num_properties = ARRAY_SIZE(max17042_battery_props), 1021 }; 1022 1023 static const struct power_supply_desc max17042_no_current_sense_psy_desc = { 1024 .name = "max170xx_battery", 1025 .type = POWER_SUPPLY_TYPE_BATTERY, 1026 .get_property = max17042_get_property, 1027 .set_property = max17042_set_property, 1028 .property_is_writeable = max17042_property_is_writeable, 1029 .properties = max17042_battery_props, 1030 .num_properties = ARRAY_SIZE(max17042_battery_props) - 2, 1031 }; 1032 1033 static void max17042_stop_work(void *data) 1034 { 1035 struct max17042_chip *chip = data; 1036 1037 cancel_work_sync(&chip->work); 1038 } 1039 1040 static int max17042_probe(struct i2c_client *client, 1041 const struct i2c_device_id *id) 1042 { 1043 struct i2c_adapter *adapter = client->adapter; 1044 const struct power_supply_desc *max17042_desc = &max17042_psy_desc; 1045 struct power_supply_config psy_cfg = {}; 1046 const struct acpi_device_id *acpi_id = NULL; 1047 struct device *dev = &client->dev; 1048 struct max17042_chip *chip; 1049 int ret; 1050 int i; 1051 u32 val; 1052 1053 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) 1054 return -EIO; 1055 1056 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); 1057 if (!chip) 1058 return -ENOMEM; 1059 1060 chip->client = client; 1061 if (id) { 1062 chip->chip_type = id->driver_data; 1063 } else { 1064 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev); 1065 if (!acpi_id) 1066 return -ENODEV; 1067 1068 chip->chip_type = acpi_id->driver_data; 1069 } 1070 chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config); 1071 if (IS_ERR(chip->regmap)) { 1072 dev_err(&client->dev, "Failed to initialize regmap\n"); 1073 return -EINVAL; 1074 } 1075 1076 chip->pdata = max17042_get_pdata(chip); 1077 if (!chip->pdata) { 1078 dev_err(&client->dev, "no platform data provided\n"); 1079 return -EINVAL; 1080 } 1081 1082 i2c_set_clientdata(client, chip); 1083 psy_cfg.drv_data = chip; 1084 psy_cfg.of_node = dev->of_node; 1085 1086 /* When current is not measured, 1087 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */ 1088 if (!chip->pdata->enable_current_sense) 1089 max17042_desc = &max17042_no_current_sense_psy_desc; 1090 1091 if (chip->pdata->r_sns == 0) 1092 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR; 1093 1094 if (chip->pdata->init_data) 1095 for (i = 0; i < chip->pdata->num_init_data; i++) 1096 regmap_write(chip->regmap, 1097 chip->pdata->init_data[i].addr, 1098 chip->pdata->init_data[i].data); 1099 1100 if (!chip->pdata->enable_current_sense) { 1101 regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000); 1102 regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003); 1103 regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007); 1104 } 1105 1106 chip->battery = devm_power_supply_register(&client->dev, max17042_desc, 1107 &psy_cfg); 1108 if (IS_ERR(chip->battery)) { 1109 dev_err(&client->dev, "failed: power supply register\n"); 1110 return PTR_ERR(chip->battery); 1111 } 1112 1113 if (client->irq) { 1114 unsigned int flags = IRQF_ONESHOT; 1115 1116 /* 1117 * On ACPI systems the IRQ may be handled by ACPI-event code, 1118 * so we need to share (if the ACPI code is willing to share). 1119 */ 1120 if (acpi_id) 1121 flags |= IRQF_SHARED | IRQF_PROBE_SHARED; 1122 1123 ret = devm_request_threaded_irq(&client->dev, client->irq, 1124 NULL, 1125 max17042_thread_handler, flags, 1126 chip->battery->desc->name, 1127 chip); 1128 if (!ret) { 1129 regmap_update_bits(chip->regmap, MAX17042_CONFIG, 1130 CONFIG_ALRT_BIT_ENBL, 1131 CONFIG_ALRT_BIT_ENBL); 1132 max17042_set_soc_threshold(chip, 1); 1133 } else { 1134 client->irq = 0; 1135 if (ret != -EBUSY) 1136 dev_err(&client->dev, "Failed to get IRQ\n"); 1137 } 1138 } 1139 /* Not able to update the charge threshold when exceeded? -> disable */ 1140 if (!client->irq) 1141 regmap_write(chip->regmap, MAX17042_SALRT_Th, 0xff00); 1142 1143 regmap_read(chip->regmap, MAX17042_STATUS, &val); 1144 if (val & STATUS_POR_BIT) { 1145 INIT_WORK(&chip->work, max17042_init_worker); 1146 ret = devm_add_action(&client->dev, max17042_stop_work, chip); 1147 if (ret) 1148 return ret; 1149 schedule_work(&chip->work); 1150 } else { 1151 chip->init_complete = 1; 1152 } 1153 1154 return 0; 1155 } 1156 1157 #ifdef CONFIG_PM_SLEEP 1158 static int max17042_suspend(struct device *dev) 1159 { 1160 struct max17042_chip *chip = dev_get_drvdata(dev); 1161 1162 /* 1163 * disable the irq and enable irq_wake 1164 * capability to the interrupt line. 1165 */ 1166 if (chip->client->irq) { 1167 disable_irq(chip->client->irq); 1168 enable_irq_wake(chip->client->irq); 1169 } 1170 1171 return 0; 1172 } 1173 1174 static int max17042_resume(struct device *dev) 1175 { 1176 struct max17042_chip *chip = dev_get_drvdata(dev); 1177 1178 if (chip->client->irq) { 1179 disable_irq_wake(chip->client->irq); 1180 enable_irq(chip->client->irq); 1181 /* re-program the SOC thresholds to 1% change */ 1182 max17042_set_soc_threshold(chip, 1); 1183 } 1184 1185 return 0; 1186 } 1187 #endif 1188 1189 static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend, 1190 max17042_resume); 1191 1192 #ifdef CONFIG_ACPI 1193 static const struct acpi_device_id max17042_acpi_match[] = { 1194 { "MAX17047", MAXIM_DEVICE_TYPE_MAX17047 }, 1195 { } 1196 }; 1197 MODULE_DEVICE_TABLE(acpi, max17042_acpi_match); 1198 #endif 1199 1200 #ifdef CONFIG_OF 1201 static const struct of_device_id max17042_dt_match[] = { 1202 { .compatible = "maxim,max17042" }, 1203 { .compatible = "maxim,max17047" }, 1204 { .compatible = "maxim,max17050" }, 1205 { .compatible = "maxim,max17055" }, 1206 { .compatible = "maxim,max77849-battery" }, 1207 { }, 1208 }; 1209 MODULE_DEVICE_TABLE(of, max17042_dt_match); 1210 #endif 1211 1212 static const struct i2c_device_id max17042_id[] = { 1213 { "max17042", MAXIM_DEVICE_TYPE_MAX17042 }, 1214 { "max17047", MAXIM_DEVICE_TYPE_MAX17047 }, 1215 { "max17050", MAXIM_DEVICE_TYPE_MAX17050 }, 1216 { "max17055", MAXIM_DEVICE_TYPE_MAX17055 }, 1217 { "max77849-battery", MAXIM_DEVICE_TYPE_MAX17047 }, 1218 { } 1219 }; 1220 MODULE_DEVICE_TABLE(i2c, max17042_id); 1221 1222 static struct i2c_driver max17042_i2c_driver = { 1223 .driver = { 1224 .name = "max17042", 1225 .acpi_match_table = ACPI_PTR(max17042_acpi_match), 1226 .of_match_table = of_match_ptr(max17042_dt_match), 1227 .pm = &max17042_pm_ops, 1228 }, 1229 .probe = max17042_probe, 1230 .id_table = max17042_id, 1231 }; 1232 module_i2c_driver(max17042_i2c_driver); 1233 1234 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); 1235 MODULE_DESCRIPTION("MAX17042 Fuel Gauge"); 1236 MODULE_LICENSE("GPL"); 1237