1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/export.h> 3 #include <linux/power_supply.h> 4 #include <linux/of.h> 5 #include <linux/mfd/abx500.h> 6 #include <linux/mfd/abx500/ab8500.h> 7 #include <linux/mfd/abx500/ab8500-bm.h> 8 9 /* 10 * These are the defined batteries that uses a NTC and ID resistor placed 11 * inside of the battery pack. 12 * Note that the res_to_temp table must be strictly sorted by falling resistance 13 * values to work. 14 */ 15 const struct abx500_res_to_temp ab8500_temp_tbl_a_thermistor[] = { 16 {-5, 53407}, 17 { 0, 48594}, 18 { 5, 43804}, 19 {10, 39188}, 20 {15, 34870}, 21 {20, 30933}, 22 {25, 27422}, 23 {30, 24347}, 24 {35, 21694}, 25 {40, 19431}, 26 {45, 17517}, 27 {50, 15908}, 28 {55, 14561}, 29 {60, 13437}, 30 {65, 12500}, 31 }; 32 EXPORT_SYMBOL(ab8500_temp_tbl_a_thermistor); 33 34 const int ab8500_temp_tbl_a_size = ARRAY_SIZE(ab8500_temp_tbl_a_thermistor); 35 EXPORT_SYMBOL(ab8500_temp_tbl_a_size); 36 37 const struct abx500_res_to_temp ab8500_temp_tbl_b_thermistor[] = { 38 {-5, 200000}, 39 { 0, 159024}, 40 { 5, 151921}, 41 {10, 144300}, 42 {15, 136424}, 43 {20, 128565}, 44 {25, 120978}, 45 {30, 113875}, 46 {35, 107397}, 47 {40, 101629}, 48 {45, 96592}, 49 {50, 92253}, 50 {55, 88569}, 51 {60, 85461}, 52 {65, 82869}, 53 }; 54 EXPORT_SYMBOL(ab8500_temp_tbl_b_thermistor); 55 56 const int ab8500_temp_tbl_b_size = ARRAY_SIZE(ab8500_temp_tbl_b_thermistor); 57 EXPORT_SYMBOL(ab8500_temp_tbl_b_size); 58 59 static const struct abx500_v_to_cap cap_tbl_a_thermistor[] = { 60 {4171, 100}, 61 {4114, 95}, 62 {4009, 83}, 63 {3947, 74}, 64 {3907, 67}, 65 {3863, 59}, 66 {3830, 56}, 67 {3813, 53}, 68 {3791, 46}, 69 {3771, 33}, 70 {3754, 25}, 71 {3735, 20}, 72 {3717, 17}, 73 {3681, 13}, 74 {3664, 8}, 75 {3651, 6}, 76 {3635, 5}, 77 {3560, 3}, 78 {3408, 1}, 79 {3247, 0}, 80 }; 81 82 static const struct abx500_v_to_cap cap_tbl_b_thermistor[] = { 83 {4161, 100}, 84 {4124, 98}, 85 {4044, 90}, 86 {4003, 85}, 87 {3966, 80}, 88 {3933, 75}, 89 {3888, 67}, 90 {3849, 60}, 91 {3813, 55}, 92 {3787, 47}, 93 {3772, 30}, 94 {3751, 25}, 95 {3718, 20}, 96 {3681, 16}, 97 {3660, 14}, 98 {3589, 10}, 99 {3546, 7}, 100 {3495, 4}, 101 {3404, 2}, 102 {3250, 0}, 103 }; 104 105 static const struct abx500_v_to_cap cap_tbl[] = { 106 {4186, 100}, 107 {4163, 99}, 108 {4114, 95}, 109 {4068, 90}, 110 {3990, 80}, 111 {3926, 70}, 112 {3898, 65}, 113 {3866, 60}, 114 {3833, 55}, 115 {3812, 50}, 116 {3787, 40}, 117 {3768, 30}, 118 {3747, 25}, 119 {3730, 20}, 120 {3705, 15}, 121 {3699, 14}, 122 {3684, 12}, 123 {3672, 9}, 124 {3657, 7}, 125 {3638, 6}, 126 {3556, 4}, 127 {3424, 2}, 128 {3317, 1}, 129 {3094, 0}, 130 }; 131 132 /* 133 * Note that the res_to_temp table must be strictly sorted by falling 134 * resistance values to work. 135 */ 136 static const struct abx500_res_to_temp temp_tbl[] = { 137 {-5, 214834}, 138 { 0, 162943}, 139 { 5, 124820}, 140 {10, 96520}, 141 {15, 75306}, 142 {20, 59254}, 143 {25, 47000}, 144 {30, 37566}, 145 {35, 30245}, 146 {40, 24520}, 147 {45, 20010}, 148 {50, 16432}, 149 {55, 13576}, 150 {60, 11280}, 151 {65, 9425}, 152 }; 153 154 /* 155 * Note that the batres_vs_temp table must be strictly sorted by falling 156 * temperature values to work. 157 */ 158 static const struct batres_vs_temp temp_to_batres_tbl_thermistor[] = { 159 { 40, 120}, 160 { 30, 135}, 161 { 20, 165}, 162 { 10, 230}, 163 { 00, 325}, 164 {-10, 445}, 165 {-20, 595}, 166 }; 167 168 /* 169 * Note that the batres_vs_temp table must be strictly sorted by falling 170 * temperature values to work. 171 */ 172 static const struct batres_vs_temp temp_to_batres_tbl_ext_thermistor[] = { 173 { 60, 300}, 174 { 30, 300}, 175 { 20, 300}, 176 { 10, 300}, 177 { 00, 300}, 178 {-10, 300}, 179 {-20, 300}, 180 }; 181 182 /* battery resistance table for LI ION 9100 battery */ 183 static const struct batres_vs_temp temp_to_batres_tbl_9100[] = { 184 { 60, 180}, 185 { 30, 180}, 186 { 20, 180}, 187 { 10, 180}, 188 { 00, 180}, 189 {-10, 180}, 190 {-20, 180}, 191 }; 192 193 static struct abx500_battery_type bat_type_thermistor[] = { 194 [BATTERY_UNKNOWN] = { 195 /* First element always represent the UNKNOWN battery */ 196 .name = POWER_SUPPLY_TECHNOLOGY_UNKNOWN, 197 .resis_high = 0, 198 .resis_low = 0, 199 .battery_resistance = 300, 200 .charge_full_design = 612, 201 .nominal_voltage = 3700, 202 .termination_vol = 4050, 203 .termination_curr = 200, 204 .recharge_cap = 95, 205 .normal_cur_lvl = 400, 206 .normal_vol_lvl = 4100, 207 .maint_a_cur_lvl = 400, 208 .maint_a_vol_lvl = 4050, 209 .maint_a_chg_timer_h = 60, 210 .maint_b_cur_lvl = 400, 211 .maint_b_vol_lvl = 4000, 212 .maint_b_chg_timer_h = 200, 213 .low_high_cur_lvl = 300, 214 .low_high_vol_lvl = 4000, 215 .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl), 216 .r_to_t_tbl = temp_tbl, 217 .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl), 218 .v_to_cap_tbl = cap_tbl, 219 .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor), 220 .batres_tbl = temp_to_batres_tbl_thermistor, 221 }, 222 { 223 .name = POWER_SUPPLY_TECHNOLOGY_LIPO, 224 .resis_high = 53407, 225 .resis_low = 12500, 226 .battery_resistance = 300, 227 .charge_full_design = 900, 228 .nominal_voltage = 3600, 229 .termination_vol = 4150, 230 .termination_curr = 80, 231 .recharge_cap = 95, 232 .normal_cur_lvl = 700, 233 .normal_vol_lvl = 4200, 234 .maint_a_cur_lvl = 600, 235 .maint_a_vol_lvl = 4150, 236 .maint_a_chg_timer_h = 60, 237 .maint_b_cur_lvl = 600, 238 .maint_b_vol_lvl = 4100, 239 .maint_b_chg_timer_h = 200, 240 .low_high_cur_lvl = 300, 241 .low_high_vol_lvl = 4000, 242 .n_temp_tbl_elements = ARRAY_SIZE(ab8500_temp_tbl_a_thermistor), 243 .r_to_t_tbl = ab8500_temp_tbl_a_thermistor, 244 .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl_a_thermistor), 245 .v_to_cap_tbl = cap_tbl_a_thermistor, 246 .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor), 247 .batres_tbl = temp_to_batres_tbl_thermistor, 248 249 }, 250 { 251 .name = POWER_SUPPLY_TECHNOLOGY_LIPO, 252 .resis_high = 200000, 253 .resis_low = 82869, 254 .battery_resistance = 300, 255 .charge_full_design = 900, 256 .nominal_voltage = 3600, 257 .termination_vol = 4150, 258 .termination_curr = 80, 259 .recharge_cap = 95, 260 .normal_cur_lvl = 700, 261 .normal_vol_lvl = 4200, 262 .maint_a_cur_lvl = 600, 263 .maint_a_vol_lvl = 4150, 264 .maint_a_chg_timer_h = 60, 265 .maint_b_cur_lvl = 600, 266 .maint_b_vol_lvl = 4100, 267 .maint_b_chg_timer_h = 200, 268 .low_high_cur_lvl = 300, 269 .low_high_vol_lvl = 4000, 270 .n_temp_tbl_elements = ARRAY_SIZE(ab8500_temp_tbl_b_thermistor), 271 .r_to_t_tbl = ab8500_temp_tbl_b_thermistor, 272 .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl_b_thermistor), 273 .v_to_cap_tbl = cap_tbl_b_thermistor, 274 .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor), 275 .batres_tbl = temp_to_batres_tbl_thermistor, 276 }, 277 }; 278 279 static struct abx500_battery_type bat_type_ext_thermistor[] = { 280 [BATTERY_UNKNOWN] = { 281 /* First element always represent the UNKNOWN battery */ 282 .name = POWER_SUPPLY_TECHNOLOGY_UNKNOWN, 283 .resis_high = 0, 284 .resis_low = 0, 285 .battery_resistance = 300, 286 .charge_full_design = 612, 287 .nominal_voltage = 3700, 288 .termination_vol = 4050, 289 .termination_curr = 200, 290 .recharge_cap = 95, 291 .normal_cur_lvl = 400, 292 .normal_vol_lvl = 4100, 293 .maint_a_cur_lvl = 400, 294 .maint_a_vol_lvl = 4050, 295 .maint_a_chg_timer_h = 60, 296 .maint_b_cur_lvl = 400, 297 .maint_b_vol_lvl = 4000, 298 .maint_b_chg_timer_h = 200, 299 .low_high_cur_lvl = 300, 300 .low_high_vol_lvl = 4000, 301 .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl), 302 .r_to_t_tbl = temp_tbl, 303 .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl), 304 .v_to_cap_tbl = cap_tbl, 305 .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor), 306 .batres_tbl = temp_to_batres_tbl_thermistor, 307 }, 308 /* 309 * These are the batteries that doesn't have an internal NTC resistor to measure 310 * its temperature. The temperature in this case is measure with a NTC placed 311 * near the battery but on the PCB. 312 */ 313 { 314 .name = POWER_SUPPLY_TECHNOLOGY_LIPO, 315 .resis_high = 76000, 316 .resis_low = 53000, 317 .battery_resistance = 300, 318 .charge_full_design = 900, 319 .nominal_voltage = 3700, 320 .termination_vol = 4150, 321 .termination_curr = 100, 322 .recharge_cap = 95, 323 .normal_cur_lvl = 700, 324 .normal_vol_lvl = 4200, 325 .maint_a_cur_lvl = 600, 326 .maint_a_vol_lvl = 4150, 327 .maint_a_chg_timer_h = 60, 328 .maint_b_cur_lvl = 600, 329 .maint_b_vol_lvl = 4100, 330 .maint_b_chg_timer_h = 200, 331 .low_high_cur_lvl = 300, 332 .low_high_vol_lvl = 4000, 333 .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl), 334 .r_to_t_tbl = temp_tbl, 335 .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl), 336 .v_to_cap_tbl = cap_tbl, 337 .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor), 338 .batres_tbl = temp_to_batres_tbl_thermistor, 339 }, 340 { 341 .name = POWER_SUPPLY_TECHNOLOGY_LION, 342 .resis_high = 30000, 343 .resis_low = 10000, 344 .battery_resistance = 300, 345 .charge_full_design = 950, 346 .nominal_voltage = 3700, 347 .termination_vol = 4150, 348 .termination_curr = 100, 349 .recharge_cap = 95, 350 .normal_cur_lvl = 700, 351 .normal_vol_lvl = 4200, 352 .maint_a_cur_lvl = 600, 353 .maint_a_vol_lvl = 4150, 354 .maint_a_chg_timer_h = 60, 355 .maint_b_cur_lvl = 600, 356 .maint_b_vol_lvl = 4100, 357 .maint_b_chg_timer_h = 200, 358 .low_high_cur_lvl = 300, 359 .low_high_vol_lvl = 4000, 360 .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl), 361 .r_to_t_tbl = temp_tbl, 362 .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl), 363 .v_to_cap_tbl = cap_tbl, 364 .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor), 365 .batres_tbl = temp_to_batres_tbl_thermistor, 366 }, 367 { 368 .name = POWER_SUPPLY_TECHNOLOGY_LION, 369 .resis_high = 95000, 370 .resis_low = 76001, 371 .battery_resistance = 300, 372 .charge_full_design = 950, 373 .nominal_voltage = 3700, 374 .termination_vol = 4150, 375 .termination_curr = 100, 376 .recharge_cap = 95, 377 .normal_cur_lvl = 700, 378 .normal_vol_lvl = 4200, 379 .maint_a_cur_lvl = 600, 380 .maint_a_vol_lvl = 4150, 381 .maint_a_chg_timer_h = 60, 382 .maint_b_cur_lvl = 600, 383 .maint_b_vol_lvl = 4100, 384 .maint_b_chg_timer_h = 200, 385 .low_high_cur_lvl = 300, 386 .low_high_vol_lvl = 4000, 387 .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl), 388 .r_to_t_tbl = temp_tbl, 389 .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl), 390 .v_to_cap_tbl = cap_tbl, 391 .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor), 392 .batres_tbl = temp_to_batres_tbl_thermistor, 393 }, 394 }; 395 396 static const struct abx500_bm_capacity_levels cap_levels = { 397 .critical = 2, 398 .low = 10, 399 .normal = 70, 400 .high = 95, 401 .full = 100, 402 }; 403 404 static const struct abx500_fg_parameters fg = { 405 .recovery_sleep_timer = 10, 406 .recovery_total_time = 100, 407 .init_timer = 1, 408 .init_discard_time = 5, 409 .init_total_time = 40, 410 .high_curr_time = 60, 411 .accu_charging = 30, 412 .accu_high_curr = 30, 413 .high_curr_threshold = 50, 414 .lowbat_threshold = 3100, 415 .battok_falling_th_sel0 = 2860, 416 .battok_raising_th_sel1 = 2860, 417 .maint_thres = 95, 418 .user_cap_limit = 15, 419 .pcut_enable = 1, 420 .pcut_max_time = 127, 421 .pcut_flag_time = 112, 422 .pcut_max_restart = 15, 423 .pcut_debounce_time = 2, 424 }; 425 426 static const struct abx500_maxim_parameters ab8500_maxi_params = { 427 .ena_maxi = true, 428 .chg_curr = 910, 429 .wait_cycles = 10, 430 .charger_curr_step = 100, 431 }; 432 433 static const struct abx500_maxim_parameters abx540_maxi_params = { 434 .ena_maxi = true, 435 .chg_curr = 3000, 436 .wait_cycles = 10, 437 .charger_curr_step = 200, 438 }; 439 440 static const struct abx500_bm_charger_parameters chg = { 441 .usb_volt_max = 5500, 442 .usb_curr_max = 1500, 443 .ac_volt_max = 7500, 444 .ac_curr_max = 1500, 445 }; 446 447 /* 448 * This array maps the raw hex value to charger output current used by the 449 * AB8500 values 450 */ 451 static int ab8500_charge_output_curr_map[] = { 452 100, 200, 300, 400, 500, 600, 700, 800, 453 900, 1000, 1100, 1200, 1300, 1400, 1500, 1500, 454 }; 455 456 static int ab8540_charge_output_curr_map[] = { 457 0, 0, 0, 75, 100, 125, 150, 175, 458 200, 225, 250, 275, 300, 325, 350, 375, 459 400, 425, 450, 475, 500, 525, 550, 575, 460 600, 625, 650, 675, 700, 725, 750, 775, 461 800, 825, 850, 875, 900, 925, 950, 975, 462 1000, 1025, 1050, 1075, 1100, 1125, 1150, 1175, 463 1200, 1225, 1250, 1275, 1300, 1325, 1350, 1375, 464 1400, 1425, 1450, 1500, 1600, 1700, 1900, 2000, 465 }; 466 467 /* 468 * This array maps the raw hex value to charger input current used by the 469 * AB8500 values 470 */ 471 static int ab8500_charge_input_curr_map[] = { 472 50, 98, 193, 290, 380, 450, 500, 600, 473 700, 800, 900, 1000, 1100, 1300, 1400, 1500, 474 }; 475 476 static int ab8540_charge_input_curr_map[] = { 477 25, 50, 75, 100, 125, 150, 175, 200, 478 225, 250, 275, 300, 325, 350, 375, 400, 479 425, 450, 475, 500, 525, 550, 575, 600, 480 625, 650, 675, 700, 725, 750, 775, 800, 481 825, 850, 875, 900, 925, 950, 975, 1000, 482 1025, 1050, 1075, 1100, 1125, 1150, 1175, 1200, 483 1225, 1250, 1275, 1300, 1325, 1350, 1375, 1400, 484 1425, 1450, 1475, 1500, 1500, 1500, 1500, 1500, 485 }; 486 487 struct abx500_bm_data ab8500_bm_data = { 488 .temp_under = 3, 489 .temp_low = 8, 490 .temp_high = 43, 491 .temp_over = 48, 492 .main_safety_tmr_h = 4, 493 .temp_interval_chg = 20, 494 .temp_interval_nochg = 120, 495 .usb_safety_tmr_h = 4, 496 .bkup_bat_v = BUP_VCH_SEL_2P6V, 497 .bkup_bat_i = BUP_ICH_SEL_150UA, 498 .no_maintenance = false, 499 .capacity_scaling = false, 500 .adc_therm = ABx500_ADC_THERM_BATCTRL, 501 .chg_unknown_bat = false, 502 .enable_overshoot = false, 503 .fg_res = 100, 504 .cap_levels = &cap_levels, 505 .bat_type = bat_type_thermistor, 506 .n_btypes = ARRAY_SIZE(bat_type_thermistor), 507 .batt_id = 0, 508 .interval_charging = 5, 509 .interval_not_charging = 120, 510 .temp_hysteresis = 3, 511 .gnd_lift_resistance = 34, 512 .chg_output_curr = ab8500_charge_output_curr_map, 513 .n_chg_out_curr = ARRAY_SIZE(ab8500_charge_output_curr_map), 514 .maxi = &ab8500_maxi_params, 515 .chg_params = &chg, 516 .fg_params = &fg, 517 .chg_input_curr = ab8500_charge_input_curr_map, 518 .n_chg_in_curr = ARRAY_SIZE(ab8500_charge_input_curr_map), 519 }; 520 521 struct abx500_bm_data ab8540_bm_data = { 522 .temp_under = 3, 523 .temp_low = 8, 524 .temp_high = 43, 525 .temp_over = 48, 526 .main_safety_tmr_h = 4, 527 .temp_interval_chg = 20, 528 .temp_interval_nochg = 120, 529 .usb_safety_tmr_h = 4, 530 .bkup_bat_v = BUP_VCH_SEL_2P6V, 531 .bkup_bat_i = BUP_ICH_SEL_150UA, 532 .no_maintenance = false, 533 .capacity_scaling = false, 534 .adc_therm = ABx500_ADC_THERM_BATCTRL, 535 .chg_unknown_bat = false, 536 .enable_overshoot = false, 537 .fg_res = 100, 538 .cap_levels = &cap_levels, 539 .bat_type = bat_type_thermistor, 540 .n_btypes = ARRAY_SIZE(bat_type_thermistor), 541 .batt_id = 0, 542 .interval_charging = 5, 543 .interval_not_charging = 120, 544 .temp_hysteresis = 3, 545 .gnd_lift_resistance = 0, 546 .maxi = &abx540_maxi_params, 547 .chg_params = &chg, 548 .fg_params = &fg, 549 .chg_output_curr = ab8540_charge_output_curr_map, 550 .n_chg_out_curr = ARRAY_SIZE(ab8540_charge_output_curr_map), 551 .chg_input_curr = ab8540_charge_input_curr_map, 552 .n_chg_in_curr = ARRAY_SIZE(ab8540_charge_input_curr_map), 553 }; 554 555 int ab8500_bm_of_probe(struct device *dev, 556 struct device_node *np, 557 struct abx500_bm_data *bm) 558 { 559 const struct batres_vs_temp *tmp_batres_tbl; 560 struct device_node *battery_node; 561 const char *btech; 562 int i; 563 564 /* get phandle to 'battery-info' node */ 565 battery_node = of_parse_phandle(np, "battery", 0); 566 if (!battery_node) { 567 dev_err(dev, "battery node or reference missing\n"); 568 return -EINVAL; 569 } 570 571 btech = of_get_property(battery_node, "stericsson,battery-type", NULL); 572 if (!btech) { 573 dev_warn(dev, "missing property battery-name/type\n"); 574 return -EINVAL; 575 } 576 577 if (strncmp(btech, "LION", 4) == 0) { 578 bm->no_maintenance = true; 579 bm->chg_unknown_bat = true; 580 bm->bat_type[BATTERY_UNKNOWN].charge_full_design = 2600; 581 bm->bat_type[BATTERY_UNKNOWN].termination_vol = 4150; 582 bm->bat_type[BATTERY_UNKNOWN].recharge_cap = 95; 583 bm->bat_type[BATTERY_UNKNOWN].normal_cur_lvl = 520; 584 bm->bat_type[BATTERY_UNKNOWN].normal_vol_lvl = 4200; 585 } 586 587 if (of_property_read_bool(battery_node, "thermistor-on-batctrl")) { 588 if (strncmp(btech, "LION", 4) == 0) 589 tmp_batres_tbl = temp_to_batres_tbl_9100; 590 else 591 tmp_batres_tbl = temp_to_batres_tbl_thermistor; 592 } else { 593 bm->n_btypes = 4; 594 bm->bat_type = bat_type_ext_thermistor; 595 bm->adc_therm = ABx500_ADC_THERM_BATTEMP; 596 tmp_batres_tbl = temp_to_batres_tbl_ext_thermistor; 597 } 598 599 /* select the battery resolution table */ 600 for (i = 0; i < bm->n_btypes; ++i) 601 bm->bat_type[i].batres_tbl = tmp_batres_tbl; 602 603 of_node_put(battery_node); 604 605 return 0; 606 } 607