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