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 soc_tr |= (soc - off); 864 regmap_write(map, MAX17042_SALRT_Th, soc_tr); 865 } 866 867 static irqreturn_t max17042_thread_handler(int id, void *dev) 868 { 869 struct max17042_chip *chip = dev; 870 u32 val; 871 int ret; 872 873 ret = regmap_read(chip->regmap, MAX17042_STATUS, &val); 874 if (ret) 875 return IRQ_HANDLED; 876 877 if ((val & STATUS_SMN_BIT) || (val & STATUS_SMX_BIT)) { 878 dev_dbg(&chip->client->dev, "SOC threshold INTR\n"); 879 max17042_set_soc_threshold(chip, 1); 880 } 881 882 power_supply_changed(chip->battery); 883 return IRQ_HANDLED; 884 } 885 886 static void max17042_init_worker(struct work_struct *work) 887 { 888 struct max17042_chip *chip = container_of(work, 889 struct max17042_chip, work); 890 int ret; 891 892 /* Initialize registers according to values from the platform data */ 893 if (chip->pdata->enable_por_init && chip->pdata->config_data) { 894 ret = max17042_init_chip(chip); 895 if (ret) 896 return; 897 } 898 899 chip->init_complete = 1; 900 } 901 902 #ifdef CONFIG_OF 903 static struct max17042_platform_data * 904 max17042_get_of_pdata(struct max17042_chip *chip) 905 { 906 struct device *dev = &chip->client->dev; 907 struct device_node *np = dev->of_node; 908 u32 prop; 909 struct max17042_platform_data *pdata; 910 911 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 912 if (!pdata) 913 return NULL; 914 915 /* 916 * Require current sense resistor value to be specified for 917 * current-sense functionality to be enabled at all. 918 */ 919 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) { 920 pdata->r_sns = prop; 921 pdata->enable_current_sense = true; 922 } 923 924 if (of_property_read_s32(np, "maxim,cold-temp", &pdata->temp_min)) 925 pdata->temp_min = INT_MIN; 926 if (of_property_read_s32(np, "maxim,over-heat-temp", &pdata->temp_max)) 927 pdata->temp_max = INT_MAX; 928 if (of_property_read_s32(np, "maxim,dead-volt", &pdata->vmin)) 929 pdata->vmin = INT_MIN; 930 if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax)) 931 pdata->vmax = INT_MAX; 932 933 return pdata; 934 } 935 #endif 936 937 static struct max17042_reg_data max17047_default_pdata_init_regs[] = { 938 /* 939 * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection 940 * when the voltage FG reports 95%, as recommended in the datasheet. 941 */ 942 { MAX17047_FullSOCThr, MAX17042_BATTERY_FULL << 8 }, 943 }; 944 945 static struct max17042_platform_data * 946 max17042_get_default_pdata(struct max17042_chip *chip) 947 { 948 struct device *dev = &chip->client->dev; 949 struct max17042_platform_data *pdata; 950 int ret, misc_cfg; 951 952 /* 953 * The MAX17047 gets used on x86 where we might not have pdata, assume 954 * the firmware will already have initialized the fuel-gauge and provide 955 * default values for the non init bits to make things work. 956 */ 957 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 958 if (!pdata) 959 return pdata; 960 961 if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) || 962 (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)) { 963 pdata->init_data = max17047_default_pdata_init_regs; 964 pdata->num_init_data = 965 ARRAY_SIZE(max17047_default_pdata_init_regs); 966 } 967 968 ret = regmap_read(chip->regmap, MAX17042_MiscCFG, &misc_cfg); 969 if (ret < 0) 970 return NULL; 971 972 /* If bits 0-1 are set to 3 then only Voltage readings are used */ 973 if ((misc_cfg & 0x3) == 0x3) 974 pdata->enable_current_sense = false; 975 else 976 pdata->enable_current_sense = true; 977 978 pdata->vmin = MAX17042_DEFAULT_VMIN; 979 pdata->vmax = MAX17042_DEFAULT_VMAX; 980 pdata->temp_min = MAX17042_DEFAULT_TEMP_MIN; 981 pdata->temp_max = MAX17042_DEFAULT_TEMP_MAX; 982 983 return pdata; 984 } 985 986 static struct max17042_platform_data * 987 max17042_get_pdata(struct max17042_chip *chip) 988 { 989 struct device *dev = &chip->client->dev; 990 991 #ifdef CONFIG_OF 992 if (dev->of_node) 993 return max17042_get_of_pdata(chip); 994 #endif 995 if (dev->platform_data) 996 return dev->platform_data; 997 998 return max17042_get_default_pdata(chip); 999 } 1000 1001 static const struct regmap_config max17042_regmap_config = { 1002 .reg_bits = 8, 1003 .val_bits = 16, 1004 .val_format_endian = REGMAP_ENDIAN_NATIVE, 1005 }; 1006 1007 static const struct power_supply_desc max17042_psy_desc = { 1008 .name = "max170xx_battery", 1009 .type = POWER_SUPPLY_TYPE_BATTERY, 1010 .get_property = max17042_get_property, 1011 .set_property = max17042_set_property, 1012 .property_is_writeable = max17042_property_is_writeable, 1013 .external_power_changed = max17042_external_power_changed, 1014 .properties = max17042_battery_props, 1015 .num_properties = ARRAY_SIZE(max17042_battery_props), 1016 }; 1017 1018 static const struct power_supply_desc max17042_no_current_sense_psy_desc = { 1019 .name = "max170xx_battery", 1020 .type = POWER_SUPPLY_TYPE_BATTERY, 1021 .get_property = max17042_get_property, 1022 .set_property = max17042_set_property, 1023 .property_is_writeable = max17042_property_is_writeable, 1024 .properties = max17042_battery_props, 1025 .num_properties = ARRAY_SIZE(max17042_battery_props) - 2, 1026 }; 1027 1028 static void max17042_stop_work(void *data) 1029 { 1030 struct max17042_chip *chip = data; 1031 1032 cancel_work_sync(&chip->work); 1033 } 1034 1035 static int max17042_probe(struct i2c_client *client, 1036 const struct i2c_device_id *id) 1037 { 1038 struct i2c_adapter *adapter = client->adapter; 1039 const struct power_supply_desc *max17042_desc = &max17042_psy_desc; 1040 struct power_supply_config psy_cfg = {}; 1041 const struct acpi_device_id *acpi_id = NULL; 1042 struct device *dev = &client->dev; 1043 struct max17042_chip *chip; 1044 int ret; 1045 int i; 1046 u32 val; 1047 1048 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) 1049 return -EIO; 1050 1051 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); 1052 if (!chip) 1053 return -ENOMEM; 1054 1055 chip->client = client; 1056 if (id) { 1057 chip->chip_type = id->driver_data; 1058 } else { 1059 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev); 1060 if (!acpi_id) 1061 return -ENODEV; 1062 1063 chip->chip_type = acpi_id->driver_data; 1064 } 1065 chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config); 1066 if (IS_ERR(chip->regmap)) { 1067 dev_err(&client->dev, "Failed to initialize regmap\n"); 1068 return -EINVAL; 1069 } 1070 1071 chip->pdata = max17042_get_pdata(chip); 1072 if (!chip->pdata) { 1073 dev_err(&client->dev, "no platform data provided\n"); 1074 return -EINVAL; 1075 } 1076 1077 i2c_set_clientdata(client, chip); 1078 psy_cfg.drv_data = chip; 1079 psy_cfg.of_node = dev->of_node; 1080 1081 /* When current is not measured, 1082 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */ 1083 if (!chip->pdata->enable_current_sense) 1084 max17042_desc = &max17042_no_current_sense_psy_desc; 1085 1086 if (chip->pdata->r_sns == 0) 1087 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR; 1088 1089 if (chip->pdata->init_data) 1090 for (i = 0; i < chip->pdata->num_init_data; i++) 1091 regmap_write(chip->regmap, 1092 chip->pdata->init_data[i].addr, 1093 chip->pdata->init_data[i].data); 1094 1095 if (!chip->pdata->enable_current_sense) { 1096 regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000); 1097 regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003); 1098 regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007); 1099 } 1100 1101 chip->battery = devm_power_supply_register(&client->dev, max17042_desc, 1102 &psy_cfg); 1103 if (IS_ERR(chip->battery)) { 1104 dev_err(&client->dev, "failed: power supply register\n"); 1105 return PTR_ERR(chip->battery); 1106 } 1107 1108 if (client->irq) { 1109 unsigned int flags = IRQF_ONESHOT; 1110 1111 /* 1112 * On ACPI systems the IRQ may be handled by ACPI-event code, 1113 * so we need to share (if the ACPI code is willing to share). 1114 */ 1115 if (acpi_id) 1116 flags |= IRQF_SHARED | IRQF_PROBE_SHARED; 1117 1118 ret = devm_request_threaded_irq(&client->dev, client->irq, 1119 NULL, 1120 max17042_thread_handler, flags, 1121 chip->battery->desc->name, 1122 chip); 1123 if (!ret) { 1124 regmap_update_bits(chip->regmap, MAX17042_CONFIG, 1125 CONFIG_ALRT_BIT_ENBL, 1126 CONFIG_ALRT_BIT_ENBL); 1127 max17042_set_soc_threshold(chip, 1); 1128 } else { 1129 client->irq = 0; 1130 if (ret != -EBUSY) 1131 dev_err(&client->dev, "Failed to get IRQ\n"); 1132 } 1133 } 1134 /* Not able to update the charge threshold when exceeded? -> disable */ 1135 if (!client->irq) 1136 regmap_write(chip->regmap, MAX17042_SALRT_Th, 0xff00); 1137 1138 regmap_read(chip->regmap, MAX17042_STATUS, &val); 1139 if (val & STATUS_POR_BIT) { 1140 INIT_WORK(&chip->work, max17042_init_worker); 1141 ret = devm_add_action(&client->dev, max17042_stop_work, chip); 1142 if (ret) 1143 return ret; 1144 schedule_work(&chip->work); 1145 } else { 1146 chip->init_complete = 1; 1147 } 1148 1149 return 0; 1150 } 1151 1152 #ifdef CONFIG_PM_SLEEP 1153 static int max17042_suspend(struct device *dev) 1154 { 1155 struct max17042_chip *chip = dev_get_drvdata(dev); 1156 1157 /* 1158 * disable the irq and enable irq_wake 1159 * capability to the interrupt line. 1160 */ 1161 if (chip->client->irq) { 1162 disable_irq(chip->client->irq); 1163 enable_irq_wake(chip->client->irq); 1164 } 1165 1166 return 0; 1167 } 1168 1169 static int max17042_resume(struct device *dev) 1170 { 1171 struct max17042_chip *chip = dev_get_drvdata(dev); 1172 1173 if (chip->client->irq) { 1174 disable_irq_wake(chip->client->irq); 1175 enable_irq(chip->client->irq); 1176 /* re-program the SOC thresholds to 1% change */ 1177 max17042_set_soc_threshold(chip, 1); 1178 } 1179 1180 return 0; 1181 } 1182 #endif 1183 1184 static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend, 1185 max17042_resume); 1186 1187 #ifdef CONFIG_ACPI 1188 static const struct acpi_device_id max17042_acpi_match[] = { 1189 { "MAX17047", MAXIM_DEVICE_TYPE_MAX17047 }, 1190 { } 1191 }; 1192 MODULE_DEVICE_TABLE(acpi, max17042_acpi_match); 1193 #endif 1194 1195 #ifdef CONFIG_OF 1196 static const struct of_device_id max17042_dt_match[] = { 1197 { .compatible = "maxim,max17042" }, 1198 { .compatible = "maxim,max17047" }, 1199 { .compatible = "maxim,max17050" }, 1200 { .compatible = "maxim,max17055" }, 1201 { .compatible = "maxim,max77849-battery" }, 1202 { }, 1203 }; 1204 MODULE_DEVICE_TABLE(of, max17042_dt_match); 1205 #endif 1206 1207 static const struct i2c_device_id max17042_id[] = { 1208 { "max17042", MAXIM_DEVICE_TYPE_MAX17042 }, 1209 { "max17047", MAXIM_DEVICE_TYPE_MAX17047 }, 1210 { "max17050", MAXIM_DEVICE_TYPE_MAX17050 }, 1211 { "max17055", MAXIM_DEVICE_TYPE_MAX17055 }, 1212 { "max77849-battery", MAXIM_DEVICE_TYPE_MAX17047 }, 1213 { } 1214 }; 1215 MODULE_DEVICE_TABLE(i2c, max17042_id); 1216 1217 static struct i2c_driver max17042_i2c_driver = { 1218 .driver = { 1219 .name = "max17042", 1220 .acpi_match_table = ACPI_PTR(max17042_acpi_match), 1221 .of_match_table = of_match_ptr(max17042_dt_match), 1222 .pm = &max17042_pm_ops, 1223 }, 1224 .probe = max17042_probe, 1225 .id_table = max17042_id, 1226 }; 1227 module_i2c_driver(max17042_i2c_driver); 1228 1229 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); 1230 MODULE_DESCRIPTION("MAX17042 Fuel Gauge"); 1231 MODULE_LICENSE("GPL"); 1232