1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) ST-Ericsson AB 2012 4 * 5 * Main and Back-up battery management driver. 6 * 7 * Note: Backup battery management is required in case of Li-Ion battery and not 8 * for capacitive battery. HREF boards have capacitive battery and hence backup 9 * battery management is not used and the supported code is available in this 10 * driver. 11 * 12 * Author: 13 * Johan Palsson <johan.palsson@stericsson.com> 14 * Karl Komierowski <karl.komierowski@stericsson.com> 15 * Arun R Murthy <arun.murthy@stericsson.com> 16 */ 17 18 #include <linux/init.h> 19 #include <linux/module.h> 20 #include <linux/component.h> 21 #include <linux/device.h> 22 #include <linux/interrupt.h> 23 #include <linux/platform_device.h> 24 #include <linux/power_supply.h> 25 #include <linux/kobject.h> 26 #include <linux/slab.h> 27 #include <linux/delay.h> 28 #include <linux/time.h> 29 #include <linux/time64.h> 30 #include <linux/of.h> 31 #include <linux/completion.h> 32 #include <linux/mfd/core.h> 33 #include <linux/mfd/abx500.h> 34 #include <linux/mfd/abx500/ab8500.h> 35 #include <linux/iio/consumer.h> 36 #include <linux/kernel.h> 37 #include <linux/fixp-arith.h> 38 39 #include "ab8500-bm.h" 40 41 #define FG_LSB_IN_MA 1627 42 #define QLSB_NANO_AMP_HOURS_X10 1071 43 #define INS_CURR_TIMEOUT (3 * HZ) 44 45 #define SEC_TO_SAMPLE(S) (S * 4) 46 47 #define NBR_AVG_SAMPLES 20 48 #define WAIT_FOR_INST_CURRENT_MAX 70 49 50 #define LOW_BAT_CHECK_INTERVAL (HZ / 16) /* 62.5 ms */ 51 52 #define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */ 53 #define BATT_OK_MIN 2360 /* mV */ 54 #define BATT_OK_INCREMENT 50 /* mV */ 55 #define BATT_OK_MAX_NR_INCREMENTS 0xE 56 57 /* FG constants */ 58 #define BATT_OVV 0x01 59 60 /** 61 * struct ab8500_fg_interrupts - ab8500 fg interrupts 62 * @name: name of the interrupt 63 * @isr function pointer to the isr 64 */ 65 struct ab8500_fg_interrupts { 66 char *name; 67 irqreturn_t (*isr)(int irq, void *data); 68 }; 69 70 enum ab8500_fg_discharge_state { 71 AB8500_FG_DISCHARGE_INIT, 72 AB8500_FG_DISCHARGE_INITMEASURING, 73 AB8500_FG_DISCHARGE_INIT_RECOVERY, 74 AB8500_FG_DISCHARGE_RECOVERY, 75 AB8500_FG_DISCHARGE_READOUT_INIT, 76 AB8500_FG_DISCHARGE_READOUT, 77 AB8500_FG_DISCHARGE_WAKEUP, 78 }; 79 80 static char *discharge_state[] = { 81 "DISCHARGE_INIT", 82 "DISCHARGE_INITMEASURING", 83 "DISCHARGE_INIT_RECOVERY", 84 "DISCHARGE_RECOVERY", 85 "DISCHARGE_READOUT_INIT", 86 "DISCHARGE_READOUT", 87 "DISCHARGE_WAKEUP", 88 }; 89 90 enum ab8500_fg_charge_state { 91 AB8500_FG_CHARGE_INIT, 92 AB8500_FG_CHARGE_READOUT, 93 }; 94 95 static char *charge_state[] = { 96 "CHARGE_INIT", 97 "CHARGE_READOUT", 98 }; 99 100 enum ab8500_fg_calibration_state { 101 AB8500_FG_CALIB_INIT, 102 AB8500_FG_CALIB_WAIT, 103 AB8500_FG_CALIB_END, 104 }; 105 106 struct ab8500_fg_avg_cap { 107 int avg; 108 int samples[NBR_AVG_SAMPLES]; 109 time64_t time_stamps[NBR_AVG_SAMPLES]; 110 int pos; 111 int nbr_samples; 112 int sum; 113 }; 114 115 struct ab8500_fg_cap_scaling { 116 bool enable; 117 int cap_to_scale[2]; 118 int disable_cap_level; 119 int scaled_cap; 120 }; 121 122 struct ab8500_fg_battery_capacity { 123 int max_mah_design; 124 int max_mah; 125 int mah; 126 int permille; 127 int level; 128 int prev_mah; 129 int prev_percent; 130 int prev_level; 131 int user_mah; 132 struct ab8500_fg_cap_scaling cap_scale; 133 }; 134 135 struct ab8500_fg_flags { 136 bool fg_enabled; 137 bool conv_done; 138 bool charging; 139 bool fully_charged; 140 bool force_full; 141 bool low_bat_delay; 142 bool low_bat; 143 bool bat_ovv; 144 bool batt_unknown; 145 bool calibrate; 146 bool user_cap; 147 bool batt_id_received; 148 }; 149 150 struct inst_curr_result_list { 151 struct list_head list; 152 int *result; 153 }; 154 155 /** 156 * struct ab8500_fg - ab8500 FG device information 157 * @dev: Pointer to the structure device 158 * @node: a list of AB8500 FGs, hence prepared for reentrance 159 * @irq holds the CCEOC interrupt number 160 * @vbat_uv: Battery voltage in uV 161 * @vbat_nom_uv: Nominal battery voltage in uV 162 * @inst_curr_ua: Instantenous battery current in uA 163 * @avg_curr_ua: Average battery current in uA 164 * @bat_temp battery temperature 165 * @fg_samples: Number of samples used in the FG accumulation 166 * @accu_charge: Accumulated charge from the last conversion 167 * @recovery_cnt: Counter for recovery mode 168 * @high_curr_cnt: Counter for high current mode 169 * @init_cnt: Counter for init mode 170 * @low_bat_cnt Counter for number of consecutive low battery measures 171 * @nbr_cceoc_irq_cnt Counter for number of CCEOC irqs received since enabled 172 * @recovery_needed: Indicate if recovery is needed 173 * @high_curr_mode: Indicate if we're in high current mode 174 * @init_capacity: Indicate if initial capacity measuring should be done 175 * @turn_off_fg: True if fg was off before current measurement 176 * @calib_state State during offset calibration 177 * @discharge_state: Current discharge state 178 * @charge_state: Current charge state 179 * @ab8500_fg_started Completion struct used for the instant current start 180 * @ab8500_fg_complete Completion struct used for the instant current reading 181 * @flags: Structure for information about events triggered 182 * @bat_cap: Structure for battery capacity specific parameters 183 * @avg_cap: Average capacity filter 184 * @parent: Pointer to the struct ab8500 185 * @main_bat_v: ADC channel for the main battery voltage 186 * @bm: Platform specific battery management information 187 * @fg_psy: Structure that holds the FG specific battery properties 188 * @fg_wq: Work queue for running the FG algorithm 189 * @fg_periodic_work: Work to run the FG algorithm periodically 190 * @fg_low_bat_work: Work to check low bat condition 191 * @fg_reinit_work Work used to reset and reinitialise the FG algorithm 192 * @fg_work: Work to run the FG algorithm instantly 193 * @fg_acc_cur_work: Work to read the FG accumulator 194 * @fg_check_hw_failure_work: Work for checking HW state 195 * @cc_lock: Mutex for locking the CC 196 * @fg_kobject: Structure of type kobject 197 */ 198 struct ab8500_fg { 199 struct device *dev; 200 struct list_head node; 201 int irq; 202 int vbat_uv; 203 int vbat_nom_uv; 204 int inst_curr_ua; 205 int avg_curr_ua; 206 int bat_temp; 207 int fg_samples; 208 int accu_charge; 209 int recovery_cnt; 210 int high_curr_cnt; 211 int init_cnt; 212 int low_bat_cnt; 213 int nbr_cceoc_irq_cnt; 214 bool recovery_needed; 215 bool high_curr_mode; 216 bool init_capacity; 217 bool turn_off_fg; 218 enum ab8500_fg_calibration_state calib_state; 219 enum ab8500_fg_discharge_state discharge_state; 220 enum ab8500_fg_charge_state charge_state; 221 struct completion ab8500_fg_started; 222 struct completion ab8500_fg_complete; 223 struct ab8500_fg_flags flags; 224 struct ab8500_fg_battery_capacity bat_cap; 225 struct ab8500_fg_avg_cap avg_cap; 226 struct ab8500 *parent; 227 struct iio_channel *main_bat_v; 228 struct ab8500_bm_data *bm; 229 struct power_supply *fg_psy; 230 struct workqueue_struct *fg_wq; 231 struct delayed_work fg_periodic_work; 232 struct delayed_work fg_low_bat_work; 233 struct delayed_work fg_reinit_work; 234 struct work_struct fg_work; 235 struct work_struct fg_acc_cur_work; 236 struct delayed_work fg_check_hw_failure_work; 237 struct mutex cc_lock; 238 struct kobject fg_kobject; 239 }; 240 static LIST_HEAD(ab8500_fg_list); 241 242 /** 243 * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge 244 * (i.e. the first fuel gauge in the instance list) 245 */ 246 struct ab8500_fg *ab8500_fg_get(void) 247 { 248 return list_first_entry_or_null(&ab8500_fg_list, struct ab8500_fg, 249 node); 250 } 251 252 /* Main battery properties */ 253 static enum power_supply_property ab8500_fg_props[] = { 254 POWER_SUPPLY_PROP_VOLTAGE_NOW, 255 POWER_SUPPLY_PROP_CURRENT_NOW, 256 POWER_SUPPLY_PROP_CURRENT_AVG, 257 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 258 POWER_SUPPLY_PROP_ENERGY_FULL, 259 POWER_SUPPLY_PROP_ENERGY_NOW, 260 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 261 POWER_SUPPLY_PROP_CHARGE_FULL, 262 POWER_SUPPLY_PROP_CHARGE_NOW, 263 POWER_SUPPLY_PROP_CAPACITY, 264 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 265 }; 266 267 /* 268 * This array maps the raw hex value to lowbat voltage used by the AB8500 269 * Values taken from the UM0836, in microvolts. 270 */ 271 static int ab8500_fg_lowbat_voltage_map[] = { 272 2300000, 273 2325000, 274 2350000, 275 2375000, 276 2400000, 277 2425000, 278 2450000, 279 2475000, 280 2500000, 281 2525000, 282 2550000, 283 2575000, 284 2600000, 285 2625000, 286 2650000, 287 2675000, 288 2700000, 289 2725000, 290 2750000, 291 2775000, 292 2800000, 293 2825000, 294 2850000, 295 2875000, 296 2900000, 297 2925000, 298 2950000, 299 2975000, 300 3000000, 301 3025000, 302 3050000, 303 3075000, 304 3100000, 305 3125000, 306 3150000, 307 3175000, 308 3200000, 309 3225000, 310 3250000, 311 3275000, 312 3300000, 313 3325000, 314 3350000, 315 3375000, 316 3400000, 317 3425000, 318 3450000, 319 3475000, 320 3500000, 321 3525000, 322 3550000, 323 3575000, 324 3600000, 325 3625000, 326 3650000, 327 3675000, 328 3700000, 329 3725000, 330 3750000, 331 3775000, 332 3800000, 333 3825000, 334 3850000, 335 3850000, 336 }; 337 338 static u8 ab8500_volt_to_regval(int voltage_uv) 339 { 340 int i; 341 342 if (voltage_uv < ab8500_fg_lowbat_voltage_map[0]) 343 return 0; 344 345 for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) { 346 if (voltage_uv < ab8500_fg_lowbat_voltage_map[i]) 347 return (u8) i - 1; 348 } 349 350 /* If not captured above, return index of last element */ 351 return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1; 352 } 353 354 /** 355 * ab8500_fg_is_low_curr() - Low or high current mode 356 * @di: pointer to the ab8500_fg structure 357 * @curr_ua: the current to base or our decision on in microampere 358 * 359 * Low current mode if the current consumption is below a certain threshold 360 */ 361 static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr_ua) 362 { 363 /* 364 * We want to know if we're in low current mode 365 */ 366 if (curr_ua > -di->bm->fg_params->high_curr_threshold_ua) 367 return true; 368 else 369 return false; 370 } 371 372 /** 373 * ab8500_fg_add_cap_sample() - Add capacity to average filter 374 * @di: pointer to the ab8500_fg structure 375 * @sample: the capacity in mAh to add to the filter 376 * 377 * A capacity is added to the filter and a new mean capacity is calculated and 378 * returned 379 */ 380 static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample) 381 { 382 time64_t now = ktime_get_boottime_seconds(); 383 struct ab8500_fg_avg_cap *avg = &di->avg_cap; 384 385 do { 386 avg->sum += sample - avg->samples[avg->pos]; 387 avg->samples[avg->pos] = sample; 388 avg->time_stamps[avg->pos] = now; 389 avg->pos++; 390 391 if (avg->pos == NBR_AVG_SAMPLES) 392 avg->pos = 0; 393 394 if (avg->nbr_samples < NBR_AVG_SAMPLES) 395 avg->nbr_samples++; 396 397 /* 398 * Check the time stamp for each sample. If too old, 399 * replace with latest sample 400 */ 401 } while (now - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]); 402 403 avg->avg = avg->sum / avg->nbr_samples; 404 405 return avg->avg; 406 } 407 408 /** 409 * ab8500_fg_clear_cap_samples() - Clear average filter 410 * @di: pointer to the ab8500_fg structure 411 * 412 * The capacity filter is is reset to zero. 413 */ 414 static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di) 415 { 416 int i; 417 struct ab8500_fg_avg_cap *avg = &di->avg_cap; 418 419 avg->pos = 0; 420 avg->nbr_samples = 0; 421 avg->sum = 0; 422 avg->avg = 0; 423 424 for (i = 0; i < NBR_AVG_SAMPLES; i++) { 425 avg->samples[i] = 0; 426 avg->time_stamps[i] = 0; 427 } 428 } 429 430 /** 431 * ab8500_fg_fill_cap_sample() - Fill average filter 432 * @di: pointer to the ab8500_fg structure 433 * @sample: the capacity in mAh to fill the filter with 434 * 435 * The capacity filter is filled with a capacity in mAh 436 */ 437 static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample) 438 { 439 int i; 440 time64_t now; 441 struct ab8500_fg_avg_cap *avg = &di->avg_cap; 442 443 now = ktime_get_boottime_seconds(); 444 445 for (i = 0; i < NBR_AVG_SAMPLES; i++) { 446 avg->samples[i] = sample; 447 avg->time_stamps[i] = now; 448 } 449 450 avg->pos = 0; 451 avg->nbr_samples = NBR_AVG_SAMPLES; 452 avg->sum = sample * NBR_AVG_SAMPLES; 453 avg->avg = sample; 454 } 455 456 /** 457 * ab8500_fg_coulomb_counter() - enable coulomb counter 458 * @di: pointer to the ab8500_fg structure 459 * @enable: enable/disable 460 * 461 * Enable/Disable coulomb counter. 462 * On failure returns negative value. 463 */ 464 static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable) 465 { 466 int ret = 0; 467 mutex_lock(&di->cc_lock); 468 if (enable) { 469 /* To be able to reprogram the number of samples, we have to 470 * first stop the CC and then enable it again */ 471 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 472 AB8500_RTC_CC_CONF_REG, 0x00); 473 if (ret) 474 goto cc_err; 475 476 /* Program the samples */ 477 ret = abx500_set_register_interruptible(di->dev, 478 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU, 479 di->fg_samples); 480 if (ret) 481 goto cc_err; 482 483 /* Start the CC */ 484 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 485 AB8500_RTC_CC_CONF_REG, 486 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA)); 487 if (ret) 488 goto cc_err; 489 490 di->flags.fg_enabled = true; 491 } else { 492 /* Clear any pending read requests */ 493 ret = abx500_mask_and_set_register_interruptible(di->dev, 494 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 495 (RESET_ACCU | READ_REQ), 0); 496 if (ret) 497 goto cc_err; 498 499 ret = abx500_set_register_interruptible(di->dev, 500 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0); 501 if (ret) 502 goto cc_err; 503 504 /* Stop the CC */ 505 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 506 AB8500_RTC_CC_CONF_REG, 0); 507 if (ret) 508 goto cc_err; 509 510 di->flags.fg_enabled = false; 511 512 } 513 dev_dbg(di->dev, " CC enabled: %d Samples: %d\n", 514 enable, di->fg_samples); 515 516 mutex_unlock(&di->cc_lock); 517 518 return ret; 519 cc_err: 520 dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__); 521 mutex_unlock(&di->cc_lock); 522 return ret; 523 } 524 525 /** 526 * ab8500_fg_inst_curr_start() - start battery instantaneous current 527 * @di: pointer to the ab8500_fg structure 528 * 529 * Returns 0 or error code 530 * Note: This is part "one" and has to be called before 531 * ab8500_fg_inst_curr_finalize() 532 */ 533 int ab8500_fg_inst_curr_start(struct ab8500_fg *di) 534 { 535 u8 reg_val; 536 int ret; 537 538 mutex_lock(&di->cc_lock); 539 540 di->nbr_cceoc_irq_cnt = 0; 541 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 542 AB8500_RTC_CC_CONF_REG, ®_val); 543 if (ret < 0) 544 goto fail; 545 546 if (!(reg_val & CC_PWR_UP_ENA)) { 547 dev_dbg(di->dev, "%s Enable FG\n", __func__); 548 di->turn_off_fg = true; 549 550 /* Program the samples */ 551 ret = abx500_set_register_interruptible(di->dev, 552 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU, 553 SEC_TO_SAMPLE(10)); 554 if (ret) 555 goto fail; 556 557 /* Start the CC */ 558 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 559 AB8500_RTC_CC_CONF_REG, 560 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA)); 561 if (ret) 562 goto fail; 563 } else { 564 di->turn_off_fg = false; 565 } 566 567 /* Return and WFI */ 568 reinit_completion(&di->ab8500_fg_started); 569 reinit_completion(&di->ab8500_fg_complete); 570 enable_irq(di->irq); 571 572 /* Note: cc_lock is still locked */ 573 return 0; 574 fail: 575 mutex_unlock(&di->cc_lock); 576 return ret; 577 } 578 579 /** 580 * ab8500_fg_inst_curr_started() - check if fg conversion has started 581 * @di: pointer to the ab8500_fg structure 582 * 583 * Returns 1 if conversion started, 0 if still waiting 584 */ 585 int ab8500_fg_inst_curr_started(struct ab8500_fg *di) 586 { 587 return completion_done(&di->ab8500_fg_started); 588 } 589 590 /** 591 * ab8500_fg_inst_curr_done() - check if fg conversion is done 592 * @di: pointer to the ab8500_fg structure 593 * 594 * Returns 1 if conversion done, 0 if still waiting 595 */ 596 int ab8500_fg_inst_curr_done(struct ab8500_fg *di) 597 { 598 return completion_done(&di->ab8500_fg_complete); 599 } 600 601 /** 602 * ab8500_fg_inst_curr_finalize() - battery instantaneous current 603 * @di: pointer to the ab8500_fg structure 604 * @curr_ua: battery instantenous current in microampere (on success) 605 * 606 * Returns 0 or an error code 607 * Note: This is part "two" and has to be called at earliest 250 ms 608 * after ab8500_fg_inst_curr_start() 609 */ 610 int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *curr_ua) 611 { 612 u8 low, high; 613 int val; 614 int ret; 615 unsigned long timeout; 616 617 if (!completion_done(&di->ab8500_fg_complete)) { 618 timeout = wait_for_completion_timeout( 619 &di->ab8500_fg_complete, 620 INS_CURR_TIMEOUT); 621 dev_dbg(di->dev, "Finalize time: %d ms\n", 622 jiffies_to_msecs(INS_CURR_TIMEOUT - timeout)); 623 if (!timeout) { 624 ret = -ETIME; 625 disable_irq(di->irq); 626 di->nbr_cceoc_irq_cnt = 0; 627 dev_err(di->dev, "completion timed out [%d]\n", 628 __LINE__); 629 goto fail; 630 } 631 } 632 633 disable_irq(di->irq); 634 di->nbr_cceoc_irq_cnt = 0; 635 636 ret = abx500_mask_and_set_register_interruptible(di->dev, 637 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 638 READ_REQ, READ_REQ); 639 640 /* 100uS between read request and read is needed */ 641 usleep_range(100, 100); 642 643 /* Read CC Sample conversion value Low and high */ 644 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 645 AB8500_GASG_CC_SMPL_CNVL_REG, &low); 646 if (ret < 0) 647 goto fail; 648 649 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 650 AB8500_GASG_CC_SMPL_CNVH_REG, &high); 651 if (ret < 0) 652 goto fail; 653 654 /* 655 * negative value for Discharging 656 * convert 2's complement into decimal 657 */ 658 if (high & 0x10) 659 val = (low | (high << 8) | 0xFFFFE000); 660 else 661 val = (low | (high << 8)); 662 663 /* 664 * Convert to unit value in mA 665 * Full scale input voltage is 666 * 63.160mV => LSB = 63.160mV/(4096*res) = 1.542.000 uA 667 * Given a 250ms conversion cycle time the LSB corresponds 668 * to 107.1 nAh. Convert to current by dividing by the conversion 669 * time in hours (250ms = 1 / (3600 * 4)h) 670 * 107.1nAh assumes 10mOhm, but fg_res is in 0.1mOhm 671 */ 672 val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) / di->bm->fg_res; 673 674 if (di->turn_off_fg) { 675 dev_dbg(di->dev, "%s Disable FG\n", __func__); 676 677 /* Clear any pending read requests */ 678 ret = abx500_set_register_interruptible(di->dev, 679 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0); 680 if (ret) 681 goto fail; 682 683 /* Stop the CC */ 684 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 685 AB8500_RTC_CC_CONF_REG, 0); 686 if (ret) 687 goto fail; 688 } 689 mutex_unlock(&di->cc_lock); 690 *curr_ua = val; 691 692 return 0; 693 fail: 694 mutex_unlock(&di->cc_lock); 695 return ret; 696 } 697 698 /** 699 * ab8500_fg_inst_curr_blocking() - battery instantaneous current 700 * @di: pointer to the ab8500_fg structure 701 * 702 * Returns battery instantenous current in microampere (on success) 703 * else error code 704 */ 705 int ab8500_fg_inst_curr_blocking(struct ab8500_fg *di) 706 { 707 int ret; 708 unsigned long timeout; 709 int curr_ua = 0; 710 711 ret = ab8500_fg_inst_curr_start(di); 712 if (ret) { 713 dev_err(di->dev, "Failed to initialize fg_inst\n"); 714 return 0; 715 } 716 717 /* Wait for CC to actually start */ 718 if (!completion_done(&di->ab8500_fg_started)) { 719 timeout = wait_for_completion_timeout( 720 &di->ab8500_fg_started, 721 INS_CURR_TIMEOUT); 722 dev_dbg(di->dev, "Start time: %d ms\n", 723 jiffies_to_msecs(INS_CURR_TIMEOUT - timeout)); 724 if (!timeout) { 725 ret = -ETIME; 726 dev_err(di->dev, "completion timed out [%d]\n", 727 __LINE__); 728 goto fail; 729 } 730 } 731 732 ret = ab8500_fg_inst_curr_finalize(di, &curr_ua); 733 if (ret) { 734 dev_err(di->dev, "Failed to finalize fg_inst\n"); 735 return 0; 736 } 737 738 dev_dbg(di->dev, "%s instant current: %d uA", __func__, curr_ua); 739 return curr_ua; 740 fail: 741 disable_irq(di->irq); 742 mutex_unlock(&di->cc_lock); 743 return ret; 744 } 745 746 /** 747 * ab8500_fg_acc_cur_work() - average battery current 748 * @work: pointer to the work_struct structure 749 * 750 * Updated the average battery current obtained from the 751 * coulomb counter. 752 */ 753 static void ab8500_fg_acc_cur_work(struct work_struct *work) 754 { 755 int val; 756 int ret; 757 u8 low, med, high; 758 759 struct ab8500_fg *di = container_of(work, 760 struct ab8500_fg, fg_acc_cur_work); 761 762 mutex_lock(&di->cc_lock); 763 ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE, 764 AB8500_GASG_CC_NCOV_ACCU_CTRL, RD_NCONV_ACCU_REQ); 765 if (ret) 766 goto exit; 767 768 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 769 AB8500_GASG_CC_NCOV_ACCU_LOW, &low); 770 if (ret < 0) 771 goto exit; 772 773 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 774 AB8500_GASG_CC_NCOV_ACCU_MED, &med); 775 if (ret < 0) 776 goto exit; 777 778 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 779 AB8500_GASG_CC_NCOV_ACCU_HIGH, &high); 780 if (ret < 0) 781 goto exit; 782 783 /* Check for sign bit in case of negative value, 2's complement */ 784 if (high & 0x10) 785 val = (low | (med << 8) | (high << 16) | 0xFFE00000); 786 else 787 val = (low | (med << 8) | (high << 16)); 788 789 /* 790 * Convert to uAh 791 * Given a 250ms conversion cycle time the LSB corresponds 792 * to 112.9 nAh. 793 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm 794 */ 795 di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) / 796 (100 * di->bm->fg_res); 797 798 /* 799 * Convert to unit value in uA 800 * by dividing by the conversion 801 * time in hours (= samples / (3600 * 4)h) 802 */ 803 di->avg_curr_ua = (val * QLSB_NANO_AMP_HOURS_X10 * 36) / 804 (di->bm->fg_res * (di->fg_samples / 4)); 805 806 di->flags.conv_done = true; 807 808 mutex_unlock(&di->cc_lock); 809 810 queue_work(di->fg_wq, &di->fg_work); 811 812 dev_dbg(di->dev, "fg_res: %d, fg_samples: %d, gasg: %d, accu_charge: %d \n", 813 di->bm->fg_res, di->fg_samples, val, di->accu_charge); 814 return; 815 exit: 816 dev_err(di->dev, 817 "Failed to read or write gas gauge registers\n"); 818 mutex_unlock(&di->cc_lock); 819 queue_work(di->fg_wq, &di->fg_work); 820 } 821 822 /** 823 * ab8500_fg_bat_voltage() - get battery voltage 824 * @di: pointer to the ab8500_fg structure 825 * 826 * Returns battery voltage in microvolts (on success) else error code 827 */ 828 static int ab8500_fg_bat_voltage(struct ab8500_fg *di) 829 { 830 int vbat, ret; 831 static int prev; 832 833 ret = iio_read_channel_processed(di->main_bat_v, &vbat); 834 if (ret < 0) { 835 dev_err(di->dev, 836 "%s ADC conversion failed, using previous value\n", 837 __func__); 838 return prev; 839 } 840 841 /* IIO returns millivolts but we want microvolts */ 842 vbat *= 1000; 843 prev = vbat; 844 return vbat; 845 } 846 847 /** 848 * ab8500_fg_volt_to_capacity() - Voltage based capacity 849 * @di: pointer to the ab8500_fg structure 850 * @voltage_uv: The voltage to convert to a capacity in microvolt 851 * 852 * Returns battery capacity in per mille based on voltage 853 */ 854 static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage_uv) 855 { 856 struct power_supply_battery_info *bi = di->bm->bi; 857 858 /* Multiply by 10 because the capacity is tracked in per mille */ 859 return power_supply_batinfo_ocv2cap(bi, voltage_uv, di->bat_temp) * 10; 860 } 861 862 /** 863 * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity 864 * @di: pointer to the ab8500_fg structure 865 * 866 * Returns battery capacity based on battery voltage that is not compensated 867 * for the voltage drop due to the load 868 */ 869 static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di) 870 { 871 di->vbat_uv = ab8500_fg_bat_voltage(di); 872 return ab8500_fg_volt_to_capacity(di, di->vbat_uv); 873 } 874 875 /** 876 * ab8500_fg_battery_resistance() - Returns the battery inner resistance 877 * @di: pointer to the ab8500_fg structure 878 * 879 * Returns battery inner resistance added with the fuel gauge resistor value 880 * to get the total resistance in the whole link from gnd to bat+ node 881 * in milliohm. 882 */ 883 static int ab8500_fg_battery_resistance(struct ab8500_fg *di) 884 { 885 struct power_supply_battery_info *bi = di->bm->bi; 886 int resistance_percent = 0; 887 int resistance; 888 889 resistance_percent = power_supply_temp2resist_simple(bi->resist_table, 890 bi->resist_table_size, 891 di->bat_temp / 10); 892 /* 893 * We get a percentage of factory resistance here so first get 894 * the factory resistance in milliohms then calculate how much 895 * resistance we have at this temperature. 896 */ 897 resistance = (bi->factory_internal_resistance_uohm / 1000); 898 resistance = resistance * resistance_percent / 100; 899 900 dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d" 901 " fg resistance %d, total: %d (mOhm)\n", 902 __func__, di->bat_temp, resistance, di->bm->fg_res / 10, 903 (di->bm->fg_res / 10) + resistance); 904 905 /* fg_res variable is in 0.1mOhm */ 906 resistance += di->bm->fg_res / 10; 907 908 return resistance; 909 } 910 911 /** 912 * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity 913 * @di: pointer to the ab8500_fg structure 914 * 915 * Returns battery capacity based on battery voltage that is load compensated 916 * for the voltage drop 917 */ 918 static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di) 919 { 920 int vbat_comp_uv, res; 921 int i = 0; 922 int vbat_uv = 0; 923 924 ab8500_fg_inst_curr_start(di); 925 926 do { 927 vbat_uv += ab8500_fg_bat_voltage(di); 928 i++; 929 usleep_range(5000, 6000); 930 } while (!ab8500_fg_inst_curr_done(di) && 931 i <= WAIT_FOR_INST_CURRENT_MAX); 932 933 if (i > WAIT_FOR_INST_CURRENT_MAX) { 934 dev_err(di->dev, 935 "TIMEOUT: return capacity based on uncompensated measurement of VBAT\n"); 936 goto calc_cap; 937 } 938 939 ab8500_fg_inst_curr_finalize(di, &di->inst_curr_ua); 940 941 calc_cap: 942 di->vbat_uv = vbat_uv / i; 943 res = ab8500_fg_battery_resistance(di); 944 945 /* 946 * Use Ohms law to get the load compensated voltage. 947 * Divide by 1000 to get from milliohms to ohms. 948 */ 949 vbat_comp_uv = di->vbat_uv - (di->inst_curr_ua * res) / 1000; 950 951 dev_dbg(di->dev, "%s Measured Vbat: %d uV,Compensated Vbat %d uV, " 952 "R: %d mOhm, Current: %d uA Vbat Samples: %d\n", 953 __func__, di->vbat_uv, vbat_comp_uv, res, di->inst_curr_ua, i); 954 955 return ab8500_fg_volt_to_capacity(di, vbat_comp_uv); 956 } 957 958 /** 959 * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille 960 * @di: pointer to the ab8500_fg structure 961 * @cap_mah: capacity in mAh 962 * 963 * Converts capacity in mAh to capacity in permille 964 */ 965 static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah) 966 { 967 return (cap_mah * 1000) / di->bat_cap.max_mah_design; 968 } 969 970 /** 971 * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh 972 * @di: pointer to the ab8500_fg structure 973 * @cap_pm: capacity in permille 974 * 975 * Converts capacity in permille to capacity in mAh 976 */ 977 static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm) 978 { 979 return cap_pm * di->bat_cap.max_mah_design / 1000; 980 } 981 982 /** 983 * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh 984 * @di: pointer to the ab8500_fg structure 985 * @cap_mah: capacity in mAh 986 * 987 * Converts capacity in mAh to capacity in uWh 988 */ 989 static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah) 990 { 991 u64 div_res; 992 u32 div_rem; 993 994 /* 995 * Capacity is in milli ampere hours (10^-3)Ah 996 * Nominal voltage is in microvolts (10^-6)V 997 * divide by 1000000 after multiplication to get to mWh 998 */ 999 div_res = ((u64) cap_mah) * ((u64) di->vbat_nom_uv); 1000 div_rem = do_div(div_res, 1000000); 1001 1002 /* Make sure to round upwards if necessary */ 1003 if (div_rem >= 1000000 / 2) 1004 div_res++; 1005 1006 return (int) div_res; 1007 } 1008 1009 /** 1010 * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging 1011 * @di: pointer to the ab8500_fg structure 1012 * 1013 * Return the capacity in mAh based on previous calculated capcity and the FG 1014 * accumulator register value. The filter is filled with this capacity 1015 */ 1016 static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di) 1017 { 1018 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n", 1019 __func__, 1020 di->bat_cap.mah, 1021 di->accu_charge); 1022 1023 /* Capacity should not be less than 0 */ 1024 if (di->bat_cap.mah + di->accu_charge > 0) 1025 di->bat_cap.mah += di->accu_charge; 1026 else 1027 di->bat_cap.mah = 0; 1028 /* 1029 * We force capacity to 100% once when the algorithm 1030 * reports that it's full. 1031 */ 1032 if (di->bat_cap.mah >= di->bat_cap.max_mah_design || 1033 di->flags.force_full) { 1034 di->bat_cap.mah = di->bat_cap.max_mah_design; 1035 } 1036 1037 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); 1038 di->bat_cap.permille = 1039 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1040 1041 /* We need to update battery voltage and inst current when charging */ 1042 di->vbat_uv = ab8500_fg_bat_voltage(di); 1043 di->inst_curr_ua = ab8500_fg_inst_curr_blocking(di); 1044 1045 return di->bat_cap.mah; 1046 } 1047 1048 /** 1049 * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage 1050 * @di: pointer to the ab8500_fg structure 1051 * @comp: if voltage should be load compensated before capacity calc 1052 * 1053 * Return the capacity in mAh based on the battery voltage. The voltage can 1054 * either be load compensated or not. This value is added to the filter and a 1055 * new mean value is calculated and returned. 1056 */ 1057 static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di, bool comp) 1058 { 1059 int permille, mah; 1060 1061 if (comp) 1062 permille = ab8500_fg_load_comp_volt_to_capacity(di); 1063 else 1064 permille = ab8500_fg_uncomp_volt_to_capacity(di); 1065 1066 mah = ab8500_fg_convert_permille_to_mah(di, permille); 1067 1068 di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah); 1069 di->bat_cap.permille = 1070 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1071 1072 return di->bat_cap.mah; 1073 } 1074 1075 /** 1076 * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG 1077 * @di: pointer to the ab8500_fg structure 1078 * 1079 * Return the capacity in mAh based on previous calculated capcity and the FG 1080 * accumulator register value. This value is added to the filter and a 1081 * new mean value is calculated and returned. 1082 */ 1083 static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di) 1084 { 1085 int permille_volt, permille; 1086 1087 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n", 1088 __func__, 1089 di->bat_cap.mah, 1090 di->accu_charge); 1091 1092 /* Capacity should not be less than 0 */ 1093 if (di->bat_cap.mah + di->accu_charge > 0) 1094 di->bat_cap.mah += di->accu_charge; 1095 else 1096 di->bat_cap.mah = 0; 1097 1098 if (di->bat_cap.mah >= di->bat_cap.max_mah_design) 1099 di->bat_cap.mah = di->bat_cap.max_mah_design; 1100 1101 /* 1102 * Check against voltage based capacity. It can not be lower 1103 * than what the uncompensated voltage says 1104 */ 1105 permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1106 permille_volt = ab8500_fg_uncomp_volt_to_capacity(di); 1107 1108 if (permille < permille_volt) { 1109 di->bat_cap.permille = permille_volt; 1110 di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di, 1111 di->bat_cap.permille); 1112 1113 dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n", 1114 __func__, 1115 permille, 1116 permille_volt); 1117 1118 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); 1119 } else { 1120 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); 1121 di->bat_cap.permille = 1122 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1123 } 1124 1125 return di->bat_cap.mah; 1126 } 1127 1128 /** 1129 * ab8500_fg_capacity_level() - Get the battery capacity level 1130 * @di: pointer to the ab8500_fg structure 1131 * 1132 * Get the battery capacity level based on the capacity in percent 1133 */ 1134 static int ab8500_fg_capacity_level(struct ab8500_fg *di) 1135 { 1136 int ret, percent; 1137 1138 percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10); 1139 1140 if (percent <= di->bm->cap_levels->critical || 1141 di->flags.low_bat) 1142 ret = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 1143 else if (percent <= di->bm->cap_levels->low) 1144 ret = POWER_SUPPLY_CAPACITY_LEVEL_LOW; 1145 else if (percent <= di->bm->cap_levels->normal) 1146 ret = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 1147 else if (percent <= di->bm->cap_levels->high) 1148 ret = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; 1149 else 1150 ret = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 1151 1152 return ret; 1153 } 1154 1155 /** 1156 * ab8500_fg_calculate_scaled_capacity() - Capacity scaling 1157 * @di: pointer to the ab8500_fg structure 1158 * 1159 * Calculates the capacity to be shown to upper layers. Scales the capacity 1160 * to have 100% as a reference from the actual capacity upon removal of charger 1161 * when charging is in maintenance mode. 1162 */ 1163 static int ab8500_fg_calculate_scaled_capacity(struct ab8500_fg *di) 1164 { 1165 struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale; 1166 int capacity = di->bat_cap.prev_percent; 1167 1168 if (!cs->enable) 1169 return capacity; 1170 1171 /* 1172 * As long as we are in fully charge mode scale the capacity 1173 * to show 100%. 1174 */ 1175 if (di->flags.fully_charged) { 1176 cs->cap_to_scale[0] = 100; 1177 cs->cap_to_scale[1] = 1178 max(capacity, di->bm->fg_params->maint_thres); 1179 dev_dbg(di->dev, "Scale cap with %d/%d\n", 1180 cs->cap_to_scale[0], cs->cap_to_scale[1]); 1181 } 1182 1183 /* Calculates the scaled capacity. */ 1184 if ((cs->cap_to_scale[0] != cs->cap_to_scale[1]) 1185 && (cs->cap_to_scale[1] > 0)) 1186 capacity = min(100, 1187 DIV_ROUND_CLOSEST(di->bat_cap.prev_percent * 1188 cs->cap_to_scale[0], 1189 cs->cap_to_scale[1])); 1190 1191 if (di->flags.charging) { 1192 if (capacity < cs->disable_cap_level) { 1193 cs->disable_cap_level = capacity; 1194 dev_dbg(di->dev, "Cap to stop scale lowered %d%%\n", 1195 cs->disable_cap_level); 1196 } else if (!di->flags.fully_charged) { 1197 if (di->bat_cap.prev_percent >= 1198 cs->disable_cap_level) { 1199 dev_dbg(di->dev, "Disabling scaled capacity\n"); 1200 cs->enable = false; 1201 capacity = di->bat_cap.prev_percent; 1202 } else { 1203 dev_dbg(di->dev, 1204 "Waiting in cap to level %d%%\n", 1205 cs->disable_cap_level); 1206 capacity = cs->disable_cap_level; 1207 } 1208 } 1209 } 1210 1211 return capacity; 1212 } 1213 1214 /** 1215 * ab8500_fg_update_cap_scalers() - Capacity scaling 1216 * @di: pointer to the ab8500_fg structure 1217 * 1218 * To be called when state change from charge<->discharge to update 1219 * the capacity scalers. 1220 */ 1221 static void ab8500_fg_update_cap_scalers(struct ab8500_fg *di) 1222 { 1223 struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale; 1224 1225 if (!cs->enable) 1226 return; 1227 if (di->flags.charging) { 1228 di->bat_cap.cap_scale.disable_cap_level = 1229 di->bat_cap.cap_scale.scaled_cap; 1230 dev_dbg(di->dev, "Cap to stop scale at charge %d%%\n", 1231 di->bat_cap.cap_scale.disable_cap_level); 1232 } else { 1233 if (cs->scaled_cap != 100) { 1234 cs->cap_to_scale[0] = cs->scaled_cap; 1235 cs->cap_to_scale[1] = di->bat_cap.prev_percent; 1236 } else { 1237 cs->cap_to_scale[0] = 100; 1238 cs->cap_to_scale[1] = 1239 max(di->bat_cap.prev_percent, 1240 di->bm->fg_params->maint_thres); 1241 } 1242 1243 dev_dbg(di->dev, "Cap to scale at discharge %d/%d\n", 1244 cs->cap_to_scale[0], cs->cap_to_scale[1]); 1245 } 1246 } 1247 1248 /** 1249 * ab8500_fg_check_capacity_limits() - Check if capacity has changed 1250 * @di: pointer to the ab8500_fg structure 1251 * @init: capacity is allowed to go up in init mode 1252 * 1253 * Check if capacity or capacity limit has changed and notify the system 1254 * about it using the power_supply framework 1255 */ 1256 static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init) 1257 { 1258 bool changed = false; 1259 int percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10); 1260 1261 di->bat_cap.level = ab8500_fg_capacity_level(di); 1262 1263 if (di->bat_cap.level != di->bat_cap.prev_level) { 1264 /* 1265 * We do not allow reported capacity level to go up 1266 * unless we're charging or if we're in init 1267 */ 1268 if (!(!di->flags.charging && di->bat_cap.level > 1269 di->bat_cap.prev_level) || init) { 1270 dev_dbg(di->dev, "level changed from %d to %d\n", 1271 di->bat_cap.prev_level, 1272 di->bat_cap.level); 1273 di->bat_cap.prev_level = di->bat_cap.level; 1274 changed = true; 1275 } else { 1276 dev_dbg(di->dev, "level not allowed to go up " 1277 "since no charger is connected: %d to %d\n", 1278 di->bat_cap.prev_level, 1279 di->bat_cap.level); 1280 } 1281 } 1282 1283 /* 1284 * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate 1285 * shutdown 1286 */ 1287 if (di->flags.low_bat) { 1288 dev_dbg(di->dev, "Battery low, set capacity to 0\n"); 1289 di->bat_cap.prev_percent = 0; 1290 di->bat_cap.permille = 0; 1291 percent = 0; 1292 di->bat_cap.prev_mah = 0; 1293 di->bat_cap.mah = 0; 1294 changed = true; 1295 } else if (di->flags.fully_charged) { 1296 /* 1297 * We report 100% if algorithm reported fully charged 1298 * and show 100% during maintenance charging (scaling). 1299 */ 1300 if (di->flags.force_full) { 1301 di->bat_cap.prev_percent = percent; 1302 di->bat_cap.prev_mah = di->bat_cap.mah; 1303 1304 changed = true; 1305 1306 if (!di->bat_cap.cap_scale.enable && 1307 di->bm->capacity_scaling) { 1308 di->bat_cap.cap_scale.enable = true; 1309 di->bat_cap.cap_scale.cap_to_scale[0] = 100; 1310 di->bat_cap.cap_scale.cap_to_scale[1] = 1311 di->bat_cap.prev_percent; 1312 di->bat_cap.cap_scale.disable_cap_level = 100; 1313 } 1314 } else if (di->bat_cap.prev_percent != percent) { 1315 dev_dbg(di->dev, 1316 "battery reported full " 1317 "but capacity dropping: %d\n", 1318 percent); 1319 di->bat_cap.prev_percent = percent; 1320 di->bat_cap.prev_mah = di->bat_cap.mah; 1321 1322 changed = true; 1323 } 1324 } else if (di->bat_cap.prev_percent != percent) { 1325 if (percent == 0) { 1326 /* 1327 * We will not report 0% unless we've got 1328 * the LOW_BAT IRQ, no matter what the FG 1329 * algorithm says. 1330 */ 1331 di->bat_cap.prev_percent = 1; 1332 percent = 1; 1333 1334 changed = true; 1335 } else if (!(!di->flags.charging && 1336 percent > di->bat_cap.prev_percent) || init) { 1337 /* 1338 * We do not allow reported capacity to go up 1339 * unless we're charging or if we're in init 1340 */ 1341 dev_dbg(di->dev, 1342 "capacity changed from %d to %d (%d)\n", 1343 di->bat_cap.prev_percent, 1344 percent, 1345 di->bat_cap.permille); 1346 di->bat_cap.prev_percent = percent; 1347 di->bat_cap.prev_mah = di->bat_cap.mah; 1348 1349 changed = true; 1350 } else { 1351 dev_dbg(di->dev, "capacity not allowed to go up since " 1352 "no charger is connected: %d to %d (%d)\n", 1353 di->bat_cap.prev_percent, 1354 percent, 1355 di->bat_cap.permille); 1356 } 1357 } 1358 1359 if (changed) { 1360 if (di->bm->capacity_scaling) { 1361 di->bat_cap.cap_scale.scaled_cap = 1362 ab8500_fg_calculate_scaled_capacity(di); 1363 1364 dev_info(di->dev, "capacity=%d (%d)\n", 1365 di->bat_cap.prev_percent, 1366 di->bat_cap.cap_scale.scaled_cap); 1367 } 1368 power_supply_changed(di->fg_psy); 1369 if (di->flags.fully_charged && di->flags.force_full) { 1370 dev_dbg(di->dev, "Battery full, notifying.\n"); 1371 di->flags.force_full = false; 1372 sysfs_notify(&di->fg_kobject, NULL, "charge_full"); 1373 } 1374 sysfs_notify(&di->fg_kobject, NULL, "charge_now"); 1375 } 1376 } 1377 1378 static void ab8500_fg_charge_state_to(struct ab8500_fg *di, 1379 enum ab8500_fg_charge_state new_state) 1380 { 1381 dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n", 1382 di->charge_state, 1383 charge_state[di->charge_state], 1384 new_state, 1385 charge_state[new_state]); 1386 1387 di->charge_state = new_state; 1388 } 1389 1390 static void ab8500_fg_discharge_state_to(struct ab8500_fg *di, 1391 enum ab8500_fg_discharge_state new_state) 1392 { 1393 dev_dbg(di->dev, "Discharge state from %d [%s] to %d [%s]\n", 1394 di->discharge_state, 1395 discharge_state[di->discharge_state], 1396 new_state, 1397 discharge_state[new_state]); 1398 1399 di->discharge_state = new_state; 1400 } 1401 1402 /** 1403 * ab8500_fg_algorithm_charging() - FG algorithm for when charging 1404 * @di: pointer to the ab8500_fg structure 1405 * 1406 * Battery capacity calculation state machine for when we're charging 1407 */ 1408 static void ab8500_fg_algorithm_charging(struct ab8500_fg *di) 1409 { 1410 /* 1411 * If we change to discharge mode 1412 * we should start with recovery 1413 */ 1414 if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY) 1415 ab8500_fg_discharge_state_to(di, 1416 AB8500_FG_DISCHARGE_INIT_RECOVERY); 1417 1418 switch (di->charge_state) { 1419 case AB8500_FG_CHARGE_INIT: 1420 di->fg_samples = SEC_TO_SAMPLE( 1421 di->bm->fg_params->accu_charging); 1422 1423 ab8500_fg_coulomb_counter(di, true); 1424 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT); 1425 1426 break; 1427 1428 case AB8500_FG_CHARGE_READOUT: 1429 /* 1430 * Read the FG and calculate the new capacity 1431 */ 1432 mutex_lock(&di->cc_lock); 1433 if (!di->flags.conv_done && !di->flags.force_full) { 1434 /* Wasn't the CC IRQ that got us here */ 1435 mutex_unlock(&di->cc_lock); 1436 dev_dbg(di->dev, "%s CC conv not done\n", 1437 __func__); 1438 1439 break; 1440 } 1441 di->flags.conv_done = false; 1442 mutex_unlock(&di->cc_lock); 1443 1444 ab8500_fg_calc_cap_charging(di); 1445 1446 break; 1447 1448 default: 1449 break; 1450 } 1451 1452 /* Check capacity limits */ 1453 ab8500_fg_check_capacity_limits(di, false); 1454 } 1455 1456 static void force_capacity(struct ab8500_fg *di) 1457 { 1458 int cap; 1459 1460 ab8500_fg_clear_cap_samples(di); 1461 cap = di->bat_cap.user_mah; 1462 if (cap > di->bat_cap.max_mah_design) { 1463 dev_dbg(di->dev, "Remaining cap %d can't be bigger than total" 1464 " %d\n", cap, di->bat_cap.max_mah_design); 1465 cap = di->bat_cap.max_mah_design; 1466 } 1467 ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah); 1468 di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap); 1469 di->bat_cap.mah = cap; 1470 ab8500_fg_check_capacity_limits(di, true); 1471 } 1472 1473 static bool check_sysfs_capacity(struct ab8500_fg *di) 1474 { 1475 int cap, lower, upper; 1476 int cap_permille; 1477 1478 cap = di->bat_cap.user_mah; 1479 1480 cap_permille = ab8500_fg_convert_mah_to_permille(di, 1481 di->bat_cap.user_mah); 1482 1483 lower = di->bat_cap.permille - di->bm->fg_params->user_cap_limit * 10; 1484 upper = di->bat_cap.permille + di->bm->fg_params->user_cap_limit * 10; 1485 1486 if (lower < 0) 1487 lower = 0; 1488 /* 1000 is permille, -> 100 percent */ 1489 if (upper > 1000) 1490 upper = 1000; 1491 1492 dev_dbg(di->dev, "Capacity limits:" 1493 " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n", 1494 lower, cap_permille, upper, cap, di->bat_cap.mah); 1495 1496 /* If within limits, use the saved capacity and exit estimation...*/ 1497 if (cap_permille > lower && cap_permille < upper) { 1498 dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap); 1499 force_capacity(di); 1500 return true; 1501 } 1502 dev_dbg(di->dev, "Capacity from user out of limits, ignoring"); 1503 return false; 1504 } 1505 1506 /** 1507 * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging 1508 * @di: pointer to the ab8500_fg structure 1509 * 1510 * Battery capacity calculation state machine for when we're discharging 1511 */ 1512 static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di) 1513 { 1514 int sleep_time; 1515 1516 /* If we change to charge mode we should start with init */ 1517 if (di->charge_state != AB8500_FG_CHARGE_INIT) 1518 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT); 1519 1520 switch (di->discharge_state) { 1521 case AB8500_FG_DISCHARGE_INIT: 1522 /* We use the FG IRQ to work on */ 1523 di->init_cnt = 0; 1524 di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer); 1525 ab8500_fg_coulomb_counter(di, true); 1526 ab8500_fg_discharge_state_to(di, 1527 AB8500_FG_DISCHARGE_INITMEASURING); 1528 1529 fallthrough; 1530 case AB8500_FG_DISCHARGE_INITMEASURING: 1531 /* 1532 * Discard a number of samples during startup. 1533 * After that, use compensated voltage for a few 1534 * samples to get an initial capacity. 1535 * Then go to READOUT 1536 */ 1537 sleep_time = di->bm->fg_params->init_timer; 1538 1539 /* Discard the first [x] seconds */ 1540 if (di->init_cnt > di->bm->fg_params->init_discard_time) { 1541 ab8500_fg_calc_cap_discharge_voltage(di, true); 1542 1543 ab8500_fg_check_capacity_limits(di, true); 1544 } 1545 1546 di->init_cnt += sleep_time; 1547 if (di->init_cnt > di->bm->fg_params->init_total_time) 1548 ab8500_fg_discharge_state_to(di, 1549 AB8500_FG_DISCHARGE_READOUT_INIT); 1550 1551 break; 1552 1553 case AB8500_FG_DISCHARGE_INIT_RECOVERY: 1554 di->recovery_cnt = 0; 1555 di->recovery_needed = true; 1556 ab8500_fg_discharge_state_to(di, 1557 AB8500_FG_DISCHARGE_RECOVERY); 1558 1559 fallthrough; 1560 1561 case AB8500_FG_DISCHARGE_RECOVERY: 1562 sleep_time = di->bm->fg_params->recovery_sleep_timer; 1563 1564 /* 1565 * We should check the power consumption 1566 * If low, go to READOUT (after x min) or 1567 * RECOVERY_SLEEP if time left. 1568 * If high, go to READOUT 1569 */ 1570 di->inst_curr_ua = ab8500_fg_inst_curr_blocking(di); 1571 1572 if (ab8500_fg_is_low_curr(di, di->inst_curr_ua)) { 1573 if (di->recovery_cnt > 1574 di->bm->fg_params->recovery_total_time) { 1575 di->fg_samples = SEC_TO_SAMPLE( 1576 di->bm->fg_params->accu_high_curr); 1577 ab8500_fg_coulomb_counter(di, true); 1578 ab8500_fg_discharge_state_to(di, 1579 AB8500_FG_DISCHARGE_READOUT); 1580 di->recovery_needed = false; 1581 } else { 1582 queue_delayed_work(di->fg_wq, 1583 &di->fg_periodic_work, 1584 sleep_time * HZ); 1585 } 1586 di->recovery_cnt += sleep_time; 1587 } else { 1588 di->fg_samples = SEC_TO_SAMPLE( 1589 di->bm->fg_params->accu_high_curr); 1590 ab8500_fg_coulomb_counter(di, true); 1591 ab8500_fg_discharge_state_to(di, 1592 AB8500_FG_DISCHARGE_READOUT); 1593 } 1594 break; 1595 1596 case AB8500_FG_DISCHARGE_READOUT_INIT: 1597 di->fg_samples = SEC_TO_SAMPLE( 1598 di->bm->fg_params->accu_high_curr); 1599 ab8500_fg_coulomb_counter(di, true); 1600 ab8500_fg_discharge_state_to(di, 1601 AB8500_FG_DISCHARGE_READOUT); 1602 break; 1603 1604 case AB8500_FG_DISCHARGE_READOUT: 1605 di->inst_curr_ua = ab8500_fg_inst_curr_blocking(di); 1606 1607 if (ab8500_fg_is_low_curr(di, di->inst_curr_ua)) { 1608 /* Detect mode change */ 1609 if (di->high_curr_mode) { 1610 di->high_curr_mode = false; 1611 di->high_curr_cnt = 0; 1612 } 1613 1614 if (di->recovery_needed) { 1615 ab8500_fg_discharge_state_to(di, 1616 AB8500_FG_DISCHARGE_INIT_RECOVERY); 1617 1618 queue_delayed_work(di->fg_wq, 1619 &di->fg_periodic_work, 0); 1620 1621 break; 1622 } 1623 1624 ab8500_fg_calc_cap_discharge_voltage(di, true); 1625 } else { 1626 mutex_lock(&di->cc_lock); 1627 if (!di->flags.conv_done) { 1628 /* Wasn't the CC IRQ that got us here */ 1629 mutex_unlock(&di->cc_lock); 1630 dev_dbg(di->dev, "%s CC conv not done\n", 1631 __func__); 1632 1633 break; 1634 } 1635 di->flags.conv_done = false; 1636 mutex_unlock(&di->cc_lock); 1637 1638 /* Detect mode change */ 1639 if (!di->high_curr_mode) { 1640 di->high_curr_mode = true; 1641 di->high_curr_cnt = 0; 1642 } 1643 1644 di->high_curr_cnt += 1645 di->bm->fg_params->accu_high_curr; 1646 if (di->high_curr_cnt > 1647 di->bm->fg_params->high_curr_time) 1648 di->recovery_needed = true; 1649 1650 ab8500_fg_calc_cap_discharge_fg(di); 1651 } 1652 1653 ab8500_fg_check_capacity_limits(di, false); 1654 1655 break; 1656 1657 case AB8500_FG_DISCHARGE_WAKEUP: 1658 ab8500_fg_calc_cap_discharge_voltage(di, true); 1659 1660 di->fg_samples = SEC_TO_SAMPLE( 1661 di->bm->fg_params->accu_high_curr); 1662 ab8500_fg_coulomb_counter(di, true); 1663 ab8500_fg_discharge_state_to(di, 1664 AB8500_FG_DISCHARGE_READOUT); 1665 1666 ab8500_fg_check_capacity_limits(di, false); 1667 1668 break; 1669 1670 default: 1671 break; 1672 } 1673 } 1674 1675 /** 1676 * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration 1677 * @di: pointer to the ab8500_fg structure 1678 * 1679 */ 1680 static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di) 1681 { 1682 int ret; 1683 1684 switch (di->calib_state) { 1685 case AB8500_FG_CALIB_INIT: 1686 dev_dbg(di->dev, "Calibration ongoing...\n"); 1687 1688 ret = abx500_mask_and_set_register_interruptible(di->dev, 1689 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 1690 CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8); 1691 if (ret < 0) 1692 goto err; 1693 1694 ret = abx500_mask_and_set_register_interruptible(di->dev, 1695 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 1696 CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA); 1697 if (ret < 0) 1698 goto err; 1699 di->calib_state = AB8500_FG_CALIB_WAIT; 1700 break; 1701 case AB8500_FG_CALIB_END: 1702 ret = abx500_mask_and_set_register_interruptible(di->dev, 1703 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 1704 CC_MUXOFFSET, CC_MUXOFFSET); 1705 if (ret < 0) 1706 goto err; 1707 di->flags.calibrate = false; 1708 dev_dbg(di->dev, "Calibration done...\n"); 1709 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1710 break; 1711 case AB8500_FG_CALIB_WAIT: 1712 dev_dbg(di->dev, "Calibration WFI\n"); 1713 break; 1714 default: 1715 break; 1716 } 1717 return; 1718 err: 1719 /* Something went wrong, don't calibrate then */ 1720 dev_err(di->dev, "failed to calibrate the CC\n"); 1721 di->flags.calibrate = false; 1722 di->calib_state = AB8500_FG_CALIB_INIT; 1723 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1724 } 1725 1726 /** 1727 * ab8500_fg_algorithm() - Entry point for the FG algorithm 1728 * @di: pointer to the ab8500_fg structure 1729 * 1730 * Entry point for the battery capacity calculation state machine 1731 */ 1732 static void ab8500_fg_algorithm(struct ab8500_fg *di) 1733 { 1734 if (di->flags.calibrate) 1735 ab8500_fg_algorithm_calibrate(di); 1736 else { 1737 if (di->flags.charging) 1738 ab8500_fg_algorithm_charging(di); 1739 else 1740 ab8500_fg_algorithm_discharging(di); 1741 } 1742 1743 dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d %d " 1744 "%d %d %d %d %d %d %d\n", 1745 di->bat_cap.max_mah_design, 1746 di->bat_cap.max_mah, 1747 di->bat_cap.mah, 1748 di->bat_cap.permille, 1749 di->bat_cap.level, 1750 di->bat_cap.prev_mah, 1751 di->bat_cap.prev_percent, 1752 di->bat_cap.prev_level, 1753 di->vbat_uv, 1754 di->inst_curr_ua, 1755 di->avg_curr_ua, 1756 di->accu_charge, 1757 di->flags.charging, 1758 di->charge_state, 1759 di->discharge_state, 1760 di->high_curr_mode, 1761 di->recovery_needed); 1762 } 1763 1764 /** 1765 * ab8500_fg_periodic_work() - Run the FG state machine periodically 1766 * @work: pointer to the work_struct structure 1767 * 1768 * Work queue function for periodic work 1769 */ 1770 static void ab8500_fg_periodic_work(struct work_struct *work) 1771 { 1772 struct ab8500_fg *di = container_of(work, struct ab8500_fg, 1773 fg_periodic_work.work); 1774 1775 if (di->init_capacity) { 1776 /* Get an initial capacity calculation */ 1777 ab8500_fg_calc_cap_discharge_voltage(di, true); 1778 ab8500_fg_check_capacity_limits(di, true); 1779 di->init_capacity = false; 1780 1781 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1782 } else if (di->flags.user_cap) { 1783 if (check_sysfs_capacity(di)) { 1784 ab8500_fg_check_capacity_limits(di, true); 1785 if (di->flags.charging) 1786 ab8500_fg_charge_state_to(di, 1787 AB8500_FG_CHARGE_INIT); 1788 else 1789 ab8500_fg_discharge_state_to(di, 1790 AB8500_FG_DISCHARGE_READOUT_INIT); 1791 } 1792 di->flags.user_cap = false; 1793 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1794 } else 1795 ab8500_fg_algorithm(di); 1796 1797 } 1798 1799 /** 1800 * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition 1801 * @work: pointer to the work_struct structure 1802 * 1803 * Work queue function for checking the OVV_BAT condition 1804 */ 1805 static void ab8500_fg_check_hw_failure_work(struct work_struct *work) 1806 { 1807 int ret; 1808 u8 reg_value; 1809 1810 struct ab8500_fg *di = container_of(work, struct ab8500_fg, 1811 fg_check_hw_failure_work.work); 1812 1813 /* 1814 * If we have had a battery over-voltage situation, 1815 * check ovv-bit to see if it should be reset. 1816 */ 1817 ret = abx500_get_register_interruptible(di->dev, 1818 AB8500_CHARGER, AB8500_CH_STAT_REG, 1819 ®_value); 1820 if (ret < 0) { 1821 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1822 return; 1823 } 1824 if ((reg_value & BATT_OVV) == BATT_OVV) { 1825 if (!di->flags.bat_ovv) { 1826 dev_dbg(di->dev, "Battery OVV\n"); 1827 di->flags.bat_ovv = true; 1828 power_supply_changed(di->fg_psy); 1829 } 1830 /* Not yet recovered from ovv, reschedule this test */ 1831 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 1832 HZ); 1833 } else { 1834 dev_dbg(di->dev, "Battery recovered from OVV\n"); 1835 di->flags.bat_ovv = false; 1836 power_supply_changed(di->fg_psy); 1837 } 1838 } 1839 1840 /** 1841 * ab8500_fg_low_bat_work() - Check LOW_BAT condition 1842 * @work: pointer to the work_struct structure 1843 * 1844 * Work queue function for checking the LOW_BAT condition 1845 */ 1846 static void ab8500_fg_low_bat_work(struct work_struct *work) 1847 { 1848 int vbat_uv; 1849 1850 struct ab8500_fg *di = container_of(work, struct ab8500_fg, 1851 fg_low_bat_work.work); 1852 1853 vbat_uv = ab8500_fg_bat_voltage(di); 1854 1855 /* Check if LOW_BAT still fulfilled */ 1856 if (vbat_uv < di->bm->fg_params->lowbat_threshold_uv) { 1857 /* Is it time to shut down? */ 1858 if (di->low_bat_cnt < 1) { 1859 di->flags.low_bat = true; 1860 dev_warn(di->dev, "Shut down pending...\n"); 1861 } else { 1862 /* 1863 * Else we need to re-schedule this check to be able to detect 1864 * if the voltage increases again during charging or 1865 * due to decreasing load. 1866 */ 1867 di->low_bat_cnt--; 1868 dev_warn(di->dev, "Battery voltage still LOW\n"); 1869 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work, 1870 round_jiffies(LOW_BAT_CHECK_INTERVAL)); 1871 } 1872 } else { 1873 di->flags.low_bat_delay = false; 1874 di->low_bat_cnt = 10; 1875 dev_warn(di->dev, "Battery voltage OK again\n"); 1876 } 1877 1878 /* This is needed to dispatch LOW_BAT */ 1879 ab8500_fg_check_capacity_limits(di, false); 1880 } 1881 1882 /** 1883 * ab8500_fg_battok_calc - calculate the bit pattern corresponding 1884 * to the target voltage. 1885 * @di: pointer to the ab8500_fg structure 1886 * @target: target voltage 1887 * 1888 * Returns bit pattern closest to the target voltage 1889 * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS) 1890 */ 1891 1892 static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target) 1893 { 1894 if (target > BATT_OK_MIN + 1895 (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS)) 1896 return BATT_OK_MAX_NR_INCREMENTS; 1897 if (target < BATT_OK_MIN) 1898 return 0; 1899 return (target - BATT_OK_MIN) / BATT_OK_INCREMENT; 1900 } 1901 1902 /** 1903 * ab8500_fg_battok_init_hw_register - init battok levels 1904 * @di: pointer to the ab8500_fg structure 1905 * 1906 */ 1907 1908 static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di) 1909 { 1910 int selected; 1911 int sel0; 1912 int sel1; 1913 int cbp_sel0; 1914 int cbp_sel1; 1915 int ret; 1916 int new_val; 1917 1918 sel0 = di->bm->fg_params->battok_falling_th_sel0; 1919 sel1 = di->bm->fg_params->battok_raising_th_sel1; 1920 1921 cbp_sel0 = ab8500_fg_battok_calc(di, sel0); 1922 cbp_sel1 = ab8500_fg_battok_calc(di, sel1); 1923 1924 selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT; 1925 1926 if (selected != sel0) 1927 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n", 1928 sel0, selected, cbp_sel0); 1929 1930 selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT; 1931 1932 if (selected != sel1) 1933 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n", 1934 sel1, selected, cbp_sel1); 1935 1936 new_val = cbp_sel0 | (cbp_sel1 << 4); 1937 1938 dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1); 1939 ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK, 1940 AB8500_BATT_OK_REG, new_val); 1941 return ret; 1942 } 1943 1944 /** 1945 * ab8500_fg_instant_work() - Run the FG state machine instantly 1946 * @work: pointer to the work_struct structure 1947 * 1948 * Work queue function for instant work 1949 */ 1950 static void ab8500_fg_instant_work(struct work_struct *work) 1951 { 1952 struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work); 1953 1954 ab8500_fg_algorithm(di); 1955 } 1956 1957 /** 1958 * ab8500_fg_cc_data_end_handler() - end of data conversion isr. 1959 * @irq: interrupt number 1960 * @_di: pointer to the ab8500_fg structure 1961 * 1962 * Returns IRQ status(IRQ_HANDLED) 1963 */ 1964 static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di) 1965 { 1966 struct ab8500_fg *di = _di; 1967 if (!di->nbr_cceoc_irq_cnt) { 1968 di->nbr_cceoc_irq_cnt++; 1969 complete(&di->ab8500_fg_started); 1970 } else { 1971 di->nbr_cceoc_irq_cnt = 0; 1972 complete(&di->ab8500_fg_complete); 1973 } 1974 return IRQ_HANDLED; 1975 } 1976 1977 /** 1978 * ab8500_fg_cc_int_calib_handler () - end of calibration isr. 1979 * @irq: interrupt number 1980 * @_di: pointer to the ab8500_fg structure 1981 * 1982 * Returns IRQ status(IRQ_HANDLED) 1983 */ 1984 static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di) 1985 { 1986 struct ab8500_fg *di = _di; 1987 di->calib_state = AB8500_FG_CALIB_END; 1988 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1989 return IRQ_HANDLED; 1990 } 1991 1992 /** 1993 * ab8500_fg_cc_convend_handler() - isr to get battery avg current. 1994 * @irq: interrupt number 1995 * @_di: pointer to the ab8500_fg structure 1996 * 1997 * Returns IRQ status(IRQ_HANDLED) 1998 */ 1999 static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di) 2000 { 2001 struct ab8500_fg *di = _di; 2002 2003 queue_work(di->fg_wq, &di->fg_acc_cur_work); 2004 2005 return IRQ_HANDLED; 2006 } 2007 2008 /** 2009 * ab8500_fg_batt_ovv_handler() - Battery OVV occured 2010 * @irq: interrupt number 2011 * @_di: pointer to the ab8500_fg structure 2012 * 2013 * Returns IRQ status(IRQ_HANDLED) 2014 */ 2015 static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di) 2016 { 2017 struct ab8500_fg *di = _di; 2018 2019 dev_dbg(di->dev, "Battery OVV\n"); 2020 2021 /* Schedule a new HW failure check */ 2022 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0); 2023 2024 return IRQ_HANDLED; 2025 } 2026 2027 /** 2028 * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold 2029 * @irq: interrupt number 2030 * @_di: pointer to the ab8500_fg structure 2031 * 2032 * Returns IRQ status(IRQ_HANDLED) 2033 */ 2034 static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di) 2035 { 2036 struct ab8500_fg *di = _di; 2037 2038 /* Initiate handling in ab8500_fg_low_bat_work() if not already initiated. */ 2039 if (!di->flags.low_bat_delay) { 2040 dev_warn(di->dev, "Battery voltage is below LOW threshold\n"); 2041 di->flags.low_bat_delay = true; 2042 /* 2043 * Start a timer to check LOW_BAT again after some time 2044 * This is done to avoid shutdown on single voltage dips 2045 */ 2046 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work, 2047 round_jiffies(LOW_BAT_CHECK_INTERVAL)); 2048 } 2049 return IRQ_HANDLED; 2050 } 2051 2052 /** 2053 * ab8500_fg_get_property() - get the fg properties 2054 * @psy: pointer to the power_supply structure 2055 * @psp: pointer to the power_supply_property structure 2056 * @val: pointer to the power_supply_propval union 2057 * 2058 * This function gets called when an application tries to get the 2059 * fg properties by reading the sysfs files. 2060 * voltage_now: battery voltage 2061 * current_now: battery instant current 2062 * current_avg: battery average current 2063 * charge_full_design: capacity where battery is considered full 2064 * charge_now: battery capacity in nAh 2065 * capacity: capacity in percent 2066 * capacity_level: capacity level 2067 * 2068 * Returns error code in case of failure else 0 on success 2069 */ 2070 static int ab8500_fg_get_property(struct power_supply *psy, 2071 enum power_supply_property psp, 2072 union power_supply_propval *val) 2073 { 2074 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2075 2076 /* 2077 * If battery is identified as unknown and charging of unknown 2078 * batteries is disabled, we always report 100% capacity and 2079 * capacity level UNKNOWN, since we can't calculate 2080 * remaining capacity 2081 */ 2082 2083 switch (psp) { 2084 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 2085 if (di->flags.bat_ovv) 2086 val->intval = BATT_OVV_VALUE; 2087 else 2088 val->intval = di->vbat_uv; 2089 break; 2090 case POWER_SUPPLY_PROP_CURRENT_NOW: 2091 val->intval = di->inst_curr_ua; 2092 break; 2093 case POWER_SUPPLY_PROP_CURRENT_AVG: 2094 val->intval = di->avg_curr_ua; 2095 break; 2096 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 2097 val->intval = ab8500_fg_convert_mah_to_uwh(di, 2098 di->bat_cap.max_mah_design); 2099 break; 2100 case POWER_SUPPLY_PROP_ENERGY_FULL: 2101 val->intval = ab8500_fg_convert_mah_to_uwh(di, 2102 di->bat_cap.max_mah); 2103 break; 2104 case POWER_SUPPLY_PROP_ENERGY_NOW: 2105 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat && 2106 di->flags.batt_id_received) 2107 val->intval = ab8500_fg_convert_mah_to_uwh(di, 2108 di->bat_cap.max_mah); 2109 else 2110 val->intval = ab8500_fg_convert_mah_to_uwh(di, 2111 di->bat_cap.prev_mah); 2112 break; 2113 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 2114 val->intval = di->bat_cap.max_mah_design; 2115 break; 2116 case POWER_SUPPLY_PROP_CHARGE_FULL: 2117 val->intval = di->bat_cap.max_mah; 2118 break; 2119 case POWER_SUPPLY_PROP_CHARGE_NOW: 2120 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat && 2121 di->flags.batt_id_received) 2122 val->intval = di->bat_cap.max_mah; 2123 else 2124 val->intval = di->bat_cap.prev_mah; 2125 break; 2126 case POWER_SUPPLY_PROP_CAPACITY: 2127 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat && 2128 di->flags.batt_id_received) 2129 val->intval = 100; 2130 else 2131 val->intval = di->bat_cap.prev_percent; 2132 break; 2133 case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 2134 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat && 2135 di->flags.batt_id_received) 2136 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 2137 else 2138 val->intval = di->bat_cap.prev_level; 2139 break; 2140 default: 2141 return -EINVAL; 2142 } 2143 return 0; 2144 } 2145 2146 static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data) 2147 { 2148 struct power_supply *psy; 2149 struct power_supply *ext = dev_get_drvdata(dev); 2150 const char **supplicants = (const char **)ext->supplied_to; 2151 struct ab8500_fg *di; 2152 struct power_supply_battery_info *bi; 2153 union power_supply_propval ret; 2154 int j; 2155 2156 psy = (struct power_supply *)data; 2157 di = power_supply_get_drvdata(psy); 2158 bi = di->bm->bi; 2159 2160 /* 2161 * For all psy where the name of your driver 2162 * appears in any supplied_to 2163 */ 2164 j = match_string(supplicants, ext->num_supplicants, psy->desc->name); 2165 if (j < 0) 2166 return 0; 2167 2168 /* Go through all properties for the psy */ 2169 for (j = 0; j < ext->desc->num_properties; j++) { 2170 enum power_supply_property prop; 2171 prop = ext->desc->properties[j]; 2172 2173 if (power_supply_get_property(ext, prop, &ret)) 2174 continue; 2175 2176 switch (prop) { 2177 case POWER_SUPPLY_PROP_STATUS: 2178 switch (ext->desc->type) { 2179 case POWER_SUPPLY_TYPE_BATTERY: 2180 switch (ret.intval) { 2181 case POWER_SUPPLY_STATUS_UNKNOWN: 2182 case POWER_SUPPLY_STATUS_DISCHARGING: 2183 case POWER_SUPPLY_STATUS_NOT_CHARGING: 2184 if (!di->flags.charging) 2185 break; 2186 di->flags.charging = false; 2187 di->flags.fully_charged = false; 2188 if (di->bm->capacity_scaling) 2189 ab8500_fg_update_cap_scalers(di); 2190 queue_work(di->fg_wq, &di->fg_work); 2191 break; 2192 case POWER_SUPPLY_STATUS_FULL: 2193 if (di->flags.fully_charged) 2194 break; 2195 di->flags.fully_charged = true; 2196 di->flags.force_full = true; 2197 /* Save current capacity as maximum */ 2198 di->bat_cap.max_mah = di->bat_cap.mah; 2199 queue_work(di->fg_wq, &di->fg_work); 2200 break; 2201 case POWER_SUPPLY_STATUS_CHARGING: 2202 if (di->flags.charging && 2203 !di->flags.fully_charged) 2204 break; 2205 di->flags.charging = true; 2206 di->flags.fully_charged = false; 2207 if (di->bm->capacity_scaling) 2208 ab8500_fg_update_cap_scalers(di); 2209 queue_work(di->fg_wq, &di->fg_work); 2210 break; 2211 } 2212 break; 2213 default: 2214 break; 2215 } 2216 break; 2217 case POWER_SUPPLY_PROP_TECHNOLOGY: 2218 switch (ext->desc->type) { 2219 case POWER_SUPPLY_TYPE_BATTERY: 2220 if (!di->flags.batt_id_received && 2221 (bi && (bi->technology != 2222 POWER_SUPPLY_TECHNOLOGY_UNKNOWN))) { 2223 const struct ab8500_battery_type *b; 2224 2225 b = di->bm->bat_type; 2226 2227 di->flags.batt_id_received = true; 2228 2229 di->bat_cap.max_mah_design = 2230 di->bm->bi->charge_full_design_uah; 2231 2232 di->bat_cap.max_mah = 2233 di->bat_cap.max_mah_design; 2234 2235 di->vbat_nom_uv = 2236 di->bm->bi->voltage_max_design_uv; 2237 } 2238 2239 if (ret.intval) 2240 di->flags.batt_unknown = false; 2241 else 2242 di->flags.batt_unknown = true; 2243 break; 2244 default: 2245 break; 2246 } 2247 break; 2248 case POWER_SUPPLY_PROP_TEMP: 2249 switch (ext->desc->type) { 2250 case POWER_SUPPLY_TYPE_BATTERY: 2251 if (di->flags.batt_id_received) 2252 di->bat_temp = ret.intval; 2253 break; 2254 default: 2255 break; 2256 } 2257 break; 2258 default: 2259 break; 2260 } 2261 } 2262 return 0; 2263 } 2264 2265 /** 2266 * ab8500_fg_init_hw_registers() - Set up FG related registers 2267 * @di: pointer to the ab8500_fg structure 2268 * 2269 * Set up battery OVV, low battery voltage registers 2270 */ 2271 static int ab8500_fg_init_hw_registers(struct ab8500_fg *di) 2272 { 2273 int ret; 2274 2275 /* 2276 * Set VBAT OVV (overvoltage) threshold to 4.75V (typ) this is what 2277 * the hardware supports, nothing else can be configured in hardware. 2278 * See this as an "outer limit" where the charger will certainly 2279 * shut down. Other (lower) overvoltage levels need to be implemented 2280 * in software. 2281 */ 2282 ret = abx500_mask_and_set_register_interruptible(di->dev, 2283 AB8500_CHARGER, 2284 AB8500_BATT_OVV, 2285 BATT_OVV_TH_4P75, 2286 BATT_OVV_TH_4P75); 2287 if (ret) { 2288 dev_err(di->dev, "failed to set BATT_OVV\n"); 2289 goto out; 2290 } 2291 2292 /* Enable VBAT OVV detection */ 2293 ret = abx500_mask_and_set_register_interruptible(di->dev, 2294 AB8500_CHARGER, 2295 AB8500_BATT_OVV, 2296 BATT_OVV_ENA, 2297 BATT_OVV_ENA); 2298 if (ret) { 2299 dev_err(di->dev, "failed to enable BATT_OVV\n"); 2300 goto out; 2301 } 2302 2303 /* Low Battery Voltage */ 2304 ret = abx500_set_register_interruptible(di->dev, 2305 AB8500_SYS_CTRL2_BLOCK, 2306 AB8500_LOW_BAT_REG, 2307 ab8500_volt_to_regval( 2308 di->bm->fg_params->lowbat_threshold_uv) << 1 | 2309 LOW_BAT_ENABLE); 2310 if (ret) { 2311 dev_err(di->dev, "%s write failed\n", __func__); 2312 goto out; 2313 } 2314 2315 /* Battery OK threshold */ 2316 ret = ab8500_fg_battok_init_hw_register(di); 2317 if (ret) { 2318 dev_err(di->dev, "BattOk init write failed.\n"); 2319 goto out; 2320 } 2321 2322 if (is_ab8505(di->parent)) { 2323 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 2324 AB8505_RTC_PCUT_MAX_TIME_REG, di->bm->fg_params->pcut_max_time); 2325 2326 if (ret) { 2327 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_MAX_TIME_REG\n", __func__); 2328 goto out; 2329 } 2330 2331 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 2332 AB8505_RTC_PCUT_FLAG_TIME_REG, di->bm->fg_params->pcut_flag_time); 2333 2334 if (ret) { 2335 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_FLAG_TIME_REG\n", __func__); 2336 goto out; 2337 } 2338 2339 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 2340 AB8505_RTC_PCUT_RESTART_REG, di->bm->fg_params->pcut_max_restart); 2341 2342 if (ret) { 2343 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_RESTART_REG\n", __func__); 2344 goto out; 2345 } 2346 2347 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 2348 AB8505_RTC_PCUT_DEBOUNCE_REG, di->bm->fg_params->pcut_debounce_time); 2349 2350 if (ret) { 2351 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_DEBOUNCE_REG\n", __func__); 2352 goto out; 2353 } 2354 2355 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 2356 AB8505_RTC_PCUT_CTL_STATUS_REG, di->bm->fg_params->pcut_enable); 2357 2358 if (ret) { 2359 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_CTL_STATUS_REG\n", __func__); 2360 goto out; 2361 } 2362 } 2363 out: 2364 return ret; 2365 } 2366 2367 /** 2368 * ab8500_fg_external_power_changed() - callback for power supply changes 2369 * @psy: pointer to the structure power_supply 2370 * 2371 * This function is the entry point of the pointer external_power_changed 2372 * of the structure power_supply. 2373 * This function gets executed when there is a change in any external power 2374 * supply that this driver needs to be notified of. 2375 */ 2376 static void ab8500_fg_external_power_changed(struct power_supply *psy) 2377 { 2378 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2379 2380 class_for_each_device(power_supply_class, NULL, 2381 di->fg_psy, ab8500_fg_get_ext_psy_data); 2382 } 2383 2384 /** 2385 * ab8500_fg_reinit_work() - work to reset the FG algorithm 2386 * @work: pointer to the work_struct structure 2387 * 2388 * Used to reset the current battery capacity to be able to 2389 * retrigger a new voltage base capacity calculation. For 2390 * test and verification purpose. 2391 */ 2392 static void ab8500_fg_reinit_work(struct work_struct *work) 2393 { 2394 struct ab8500_fg *di = container_of(work, struct ab8500_fg, 2395 fg_reinit_work.work); 2396 2397 if (!di->flags.calibrate) { 2398 dev_dbg(di->dev, "Resetting FG state machine to init.\n"); 2399 ab8500_fg_clear_cap_samples(di); 2400 ab8500_fg_calc_cap_discharge_voltage(di, true); 2401 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT); 2402 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT); 2403 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 2404 2405 } else { 2406 dev_err(di->dev, "Residual offset calibration ongoing " 2407 "retrying..\n"); 2408 /* Wait one second until next try*/ 2409 queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 2410 round_jiffies(1)); 2411 } 2412 } 2413 2414 /* Exposure to the sysfs interface */ 2415 2416 struct ab8500_fg_sysfs_entry { 2417 struct attribute attr; 2418 ssize_t (*show)(struct ab8500_fg *, char *); 2419 ssize_t (*store)(struct ab8500_fg *, const char *, size_t); 2420 }; 2421 2422 static ssize_t charge_full_show(struct ab8500_fg *di, char *buf) 2423 { 2424 return sprintf(buf, "%d\n", di->bat_cap.max_mah); 2425 } 2426 2427 static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf, 2428 size_t count) 2429 { 2430 unsigned long charge_full; 2431 int ret; 2432 2433 ret = kstrtoul(buf, 10, &charge_full); 2434 if (ret) 2435 return ret; 2436 2437 di->bat_cap.max_mah = (int) charge_full; 2438 return count; 2439 } 2440 2441 static ssize_t charge_now_show(struct ab8500_fg *di, char *buf) 2442 { 2443 return sprintf(buf, "%d\n", di->bat_cap.prev_mah); 2444 } 2445 2446 static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf, 2447 size_t count) 2448 { 2449 unsigned long charge_now; 2450 int ret; 2451 2452 ret = kstrtoul(buf, 10, &charge_now); 2453 if (ret) 2454 return ret; 2455 2456 di->bat_cap.user_mah = (int) charge_now; 2457 di->flags.user_cap = true; 2458 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 2459 return count; 2460 } 2461 2462 static struct ab8500_fg_sysfs_entry charge_full_attr = 2463 __ATTR(charge_full, 0644, charge_full_show, charge_full_store); 2464 2465 static struct ab8500_fg_sysfs_entry charge_now_attr = 2466 __ATTR(charge_now, 0644, charge_now_show, charge_now_store); 2467 2468 static ssize_t 2469 ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf) 2470 { 2471 struct ab8500_fg_sysfs_entry *entry; 2472 struct ab8500_fg *di; 2473 2474 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr); 2475 di = container_of(kobj, struct ab8500_fg, fg_kobject); 2476 2477 if (!entry->show) 2478 return -EIO; 2479 2480 return entry->show(di, buf); 2481 } 2482 static ssize_t 2483 ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf, 2484 size_t count) 2485 { 2486 struct ab8500_fg_sysfs_entry *entry; 2487 struct ab8500_fg *di; 2488 2489 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr); 2490 di = container_of(kobj, struct ab8500_fg, fg_kobject); 2491 2492 if (!entry->store) 2493 return -EIO; 2494 2495 return entry->store(di, buf, count); 2496 } 2497 2498 static const struct sysfs_ops ab8500_fg_sysfs_ops = { 2499 .show = ab8500_fg_show, 2500 .store = ab8500_fg_store, 2501 }; 2502 2503 static struct attribute *ab8500_fg_attrs[] = { 2504 &charge_full_attr.attr, 2505 &charge_now_attr.attr, 2506 NULL, 2507 }; 2508 2509 static struct kobj_type ab8500_fg_ktype = { 2510 .sysfs_ops = &ab8500_fg_sysfs_ops, 2511 .default_attrs = ab8500_fg_attrs, 2512 }; 2513 2514 /** 2515 * ab8500_fg_sysfs_exit() - de-init of sysfs entry 2516 * @di: pointer to the struct ab8500_chargalg 2517 * 2518 * This function removes the entry in sysfs. 2519 */ 2520 static void ab8500_fg_sysfs_exit(struct ab8500_fg *di) 2521 { 2522 kobject_del(&di->fg_kobject); 2523 } 2524 2525 /** 2526 * ab8500_fg_sysfs_init() - init of sysfs entry 2527 * @di: pointer to the struct ab8500_chargalg 2528 * 2529 * This function adds an entry in sysfs. 2530 * Returns error code in case of failure else 0(on success) 2531 */ 2532 static int ab8500_fg_sysfs_init(struct ab8500_fg *di) 2533 { 2534 int ret = 0; 2535 2536 ret = kobject_init_and_add(&di->fg_kobject, 2537 &ab8500_fg_ktype, 2538 NULL, "battery"); 2539 if (ret < 0) 2540 dev_err(di->dev, "failed to create sysfs entry\n"); 2541 2542 return ret; 2543 } 2544 2545 static ssize_t ab8505_powercut_flagtime_read(struct device *dev, 2546 struct device_attribute *attr, 2547 char *buf) 2548 { 2549 int ret; 2550 u8 reg_value; 2551 struct power_supply *psy = dev_get_drvdata(dev); 2552 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2553 2554 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 2555 AB8505_RTC_PCUT_FLAG_TIME_REG, ®_value); 2556 2557 if (ret < 0) { 2558 dev_err(dev, "Failed to read AB8505_RTC_PCUT_FLAG_TIME_REG\n"); 2559 goto fail; 2560 } 2561 2562 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F)); 2563 2564 fail: 2565 return ret; 2566 } 2567 2568 static ssize_t ab8505_powercut_flagtime_write(struct device *dev, 2569 struct device_attribute *attr, 2570 const char *buf, size_t count) 2571 { 2572 int ret; 2573 int reg_value; 2574 struct power_supply *psy = dev_get_drvdata(dev); 2575 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2576 2577 if (kstrtoint(buf, 10, ®_value)) 2578 goto fail; 2579 2580 if (reg_value > 0x7F) { 2581 dev_err(dev, "Incorrect parameter, echo 0 (1.98s) - 127 (15.625ms) for flagtime\n"); 2582 goto fail; 2583 } 2584 2585 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 2586 AB8505_RTC_PCUT_FLAG_TIME_REG, (u8)reg_value); 2587 2588 if (ret < 0) 2589 dev_err(dev, "Failed to set AB8505_RTC_PCUT_FLAG_TIME_REG\n"); 2590 2591 fail: 2592 return count; 2593 } 2594 2595 static ssize_t ab8505_powercut_maxtime_read(struct device *dev, 2596 struct device_attribute *attr, 2597 char *buf) 2598 { 2599 int ret; 2600 u8 reg_value; 2601 struct power_supply *psy = dev_get_drvdata(dev); 2602 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2603 2604 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 2605 AB8505_RTC_PCUT_MAX_TIME_REG, ®_value); 2606 2607 if (ret < 0) { 2608 dev_err(dev, "Failed to read AB8505_RTC_PCUT_MAX_TIME_REG\n"); 2609 goto fail; 2610 } 2611 2612 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F)); 2613 2614 fail: 2615 return ret; 2616 2617 } 2618 2619 static ssize_t ab8505_powercut_maxtime_write(struct device *dev, 2620 struct device_attribute *attr, 2621 const char *buf, size_t count) 2622 { 2623 int ret; 2624 int reg_value; 2625 struct power_supply *psy = dev_get_drvdata(dev); 2626 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2627 2628 if (kstrtoint(buf, 10, ®_value)) 2629 goto fail; 2630 2631 if (reg_value > 0x7F) { 2632 dev_err(dev, "Incorrect parameter, echo 0 (0.0s) - 127 (1.98s) for maxtime\n"); 2633 goto fail; 2634 } 2635 2636 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 2637 AB8505_RTC_PCUT_MAX_TIME_REG, (u8)reg_value); 2638 2639 if (ret < 0) 2640 dev_err(dev, "Failed to set AB8505_RTC_PCUT_MAX_TIME_REG\n"); 2641 2642 fail: 2643 return count; 2644 } 2645 2646 static ssize_t ab8505_powercut_restart_read(struct device *dev, 2647 struct device_attribute *attr, 2648 char *buf) 2649 { 2650 int ret; 2651 u8 reg_value; 2652 struct power_supply *psy = dev_get_drvdata(dev); 2653 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2654 2655 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 2656 AB8505_RTC_PCUT_RESTART_REG, ®_value); 2657 2658 if (ret < 0) { 2659 dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n"); 2660 goto fail; 2661 } 2662 2663 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0xF)); 2664 2665 fail: 2666 return ret; 2667 } 2668 2669 static ssize_t ab8505_powercut_restart_write(struct device *dev, 2670 struct device_attribute *attr, 2671 const char *buf, size_t count) 2672 { 2673 int ret; 2674 int reg_value; 2675 struct power_supply *psy = dev_get_drvdata(dev); 2676 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2677 2678 if (kstrtoint(buf, 10, ®_value)) 2679 goto fail; 2680 2681 if (reg_value > 0xF) { 2682 dev_err(dev, "Incorrect parameter, echo 0 - 15 for number of restart\n"); 2683 goto fail; 2684 } 2685 2686 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 2687 AB8505_RTC_PCUT_RESTART_REG, (u8)reg_value); 2688 2689 if (ret < 0) 2690 dev_err(dev, "Failed to set AB8505_RTC_PCUT_RESTART_REG\n"); 2691 2692 fail: 2693 return count; 2694 2695 } 2696 2697 static ssize_t ab8505_powercut_timer_read(struct device *dev, 2698 struct device_attribute *attr, 2699 char *buf) 2700 { 2701 int ret; 2702 u8 reg_value; 2703 struct power_supply *psy = dev_get_drvdata(dev); 2704 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2705 2706 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 2707 AB8505_RTC_PCUT_TIME_REG, ®_value); 2708 2709 if (ret < 0) { 2710 dev_err(dev, "Failed to read AB8505_RTC_PCUT_TIME_REG\n"); 2711 goto fail; 2712 } 2713 2714 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F)); 2715 2716 fail: 2717 return ret; 2718 } 2719 2720 static ssize_t ab8505_powercut_restart_counter_read(struct device *dev, 2721 struct device_attribute *attr, 2722 char *buf) 2723 { 2724 int ret; 2725 u8 reg_value; 2726 struct power_supply *psy = dev_get_drvdata(dev); 2727 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2728 2729 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 2730 AB8505_RTC_PCUT_RESTART_REG, ®_value); 2731 2732 if (ret < 0) { 2733 dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n"); 2734 goto fail; 2735 } 2736 2737 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0xF0) >> 4); 2738 2739 fail: 2740 return ret; 2741 } 2742 2743 static ssize_t ab8505_powercut_read(struct device *dev, 2744 struct device_attribute *attr, 2745 char *buf) 2746 { 2747 int ret; 2748 u8 reg_value; 2749 struct power_supply *psy = dev_get_drvdata(dev); 2750 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2751 2752 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 2753 AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value); 2754 2755 if (ret < 0) 2756 goto fail; 2757 2758 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x1)); 2759 2760 fail: 2761 return ret; 2762 } 2763 2764 static ssize_t ab8505_powercut_write(struct device *dev, 2765 struct device_attribute *attr, 2766 const char *buf, size_t count) 2767 { 2768 int ret; 2769 int reg_value; 2770 struct power_supply *psy = dev_get_drvdata(dev); 2771 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2772 2773 if (kstrtoint(buf, 10, ®_value)) 2774 goto fail; 2775 2776 if (reg_value > 0x1) { 2777 dev_err(dev, "Incorrect parameter, echo 0/1 to disable/enable Pcut feature\n"); 2778 goto fail; 2779 } 2780 2781 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 2782 AB8505_RTC_PCUT_CTL_STATUS_REG, (u8)reg_value); 2783 2784 if (ret < 0) 2785 dev_err(dev, "Failed to set AB8505_RTC_PCUT_CTL_STATUS_REG\n"); 2786 2787 fail: 2788 return count; 2789 } 2790 2791 static ssize_t ab8505_powercut_flag_read(struct device *dev, 2792 struct device_attribute *attr, 2793 char *buf) 2794 { 2795 2796 int ret; 2797 u8 reg_value; 2798 struct power_supply *psy = dev_get_drvdata(dev); 2799 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2800 2801 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 2802 AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value); 2803 2804 if (ret < 0) { 2805 dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n"); 2806 goto fail; 2807 } 2808 2809 return scnprintf(buf, PAGE_SIZE, "%d\n", ((reg_value & 0x10) >> 4)); 2810 2811 fail: 2812 return ret; 2813 } 2814 2815 static ssize_t ab8505_powercut_debounce_read(struct device *dev, 2816 struct device_attribute *attr, 2817 char *buf) 2818 { 2819 int ret; 2820 u8 reg_value; 2821 struct power_supply *psy = dev_get_drvdata(dev); 2822 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2823 2824 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 2825 AB8505_RTC_PCUT_DEBOUNCE_REG, ®_value); 2826 2827 if (ret < 0) { 2828 dev_err(dev, "Failed to read AB8505_RTC_PCUT_DEBOUNCE_REG\n"); 2829 goto fail; 2830 } 2831 2832 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7)); 2833 2834 fail: 2835 return ret; 2836 } 2837 2838 static ssize_t ab8505_powercut_debounce_write(struct device *dev, 2839 struct device_attribute *attr, 2840 const char *buf, size_t count) 2841 { 2842 int ret; 2843 int reg_value; 2844 struct power_supply *psy = dev_get_drvdata(dev); 2845 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2846 2847 if (kstrtoint(buf, 10, ®_value)) 2848 goto fail; 2849 2850 if (reg_value > 0x7) { 2851 dev_err(dev, "Incorrect parameter, echo 0 to 7 for debounce setting\n"); 2852 goto fail; 2853 } 2854 2855 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 2856 AB8505_RTC_PCUT_DEBOUNCE_REG, (u8)reg_value); 2857 2858 if (ret < 0) 2859 dev_err(dev, "Failed to set AB8505_RTC_PCUT_DEBOUNCE_REG\n"); 2860 2861 fail: 2862 return count; 2863 } 2864 2865 static ssize_t ab8505_powercut_enable_status_read(struct device *dev, 2866 struct device_attribute *attr, 2867 char *buf) 2868 { 2869 int ret; 2870 u8 reg_value; 2871 struct power_supply *psy = dev_get_drvdata(dev); 2872 struct ab8500_fg *di = power_supply_get_drvdata(psy); 2873 2874 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 2875 AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value); 2876 2877 if (ret < 0) { 2878 dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n"); 2879 goto fail; 2880 } 2881 2882 return scnprintf(buf, PAGE_SIZE, "%d\n", ((reg_value & 0x20) >> 5)); 2883 2884 fail: 2885 return ret; 2886 } 2887 2888 static struct device_attribute ab8505_fg_sysfs_psy_attrs[] = { 2889 __ATTR(powercut_flagtime, (S_IRUGO | S_IWUSR | S_IWGRP), 2890 ab8505_powercut_flagtime_read, ab8505_powercut_flagtime_write), 2891 __ATTR(powercut_maxtime, (S_IRUGO | S_IWUSR | S_IWGRP), 2892 ab8505_powercut_maxtime_read, ab8505_powercut_maxtime_write), 2893 __ATTR(powercut_restart_max, (S_IRUGO | S_IWUSR | S_IWGRP), 2894 ab8505_powercut_restart_read, ab8505_powercut_restart_write), 2895 __ATTR(powercut_timer, S_IRUGO, ab8505_powercut_timer_read, NULL), 2896 __ATTR(powercut_restart_counter, S_IRUGO, 2897 ab8505_powercut_restart_counter_read, NULL), 2898 __ATTR(powercut_enable, (S_IRUGO | S_IWUSR | S_IWGRP), 2899 ab8505_powercut_read, ab8505_powercut_write), 2900 __ATTR(powercut_flag, S_IRUGO, ab8505_powercut_flag_read, NULL), 2901 __ATTR(powercut_debounce_time, (S_IRUGO | S_IWUSR | S_IWGRP), 2902 ab8505_powercut_debounce_read, ab8505_powercut_debounce_write), 2903 __ATTR(powercut_enable_status, S_IRUGO, 2904 ab8505_powercut_enable_status_read, NULL), 2905 }; 2906 2907 static int ab8500_fg_sysfs_psy_create_attrs(struct ab8500_fg *di) 2908 { 2909 unsigned int i; 2910 2911 if (is_ab8505(di->parent)) { 2912 for (i = 0; i < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); i++) 2913 if (device_create_file(&di->fg_psy->dev, 2914 &ab8505_fg_sysfs_psy_attrs[i])) 2915 goto sysfs_psy_create_attrs_failed_ab8505; 2916 } 2917 return 0; 2918 sysfs_psy_create_attrs_failed_ab8505: 2919 dev_err(&di->fg_psy->dev, "Failed creating sysfs psy attrs for ab8505.\n"); 2920 while (i--) 2921 device_remove_file(&di->fg_psy->dev, 2922 &ab8505_fg_sysfs_psy_attrs[i]); 2923 2924 return -EIO; 2925 } 2926 2927 static void ab8500_fg_sysfs_psy_remove_attrs(struct ab8500_fg *di) 2928 { 2929 unsigned int i; 2930 2931 if (is_ab8505(di->parent)) { 2932 for (i = 0; i < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); i++) 2933 (void)device_remove_file(&di->fg_psy->dev, 2934 &ab8505_fg_sysfs_psy_attrs[i]); 2935 } 2936 } 2937 2938 /* Exposure to the sysfs interface <<END>> */ 2939 2940 static int __maybe_unused ab8500_fg_resume(struct device *dev) 2941 { 2942 struct ab8500_fg *di = dev_get_drvdata(dev); 2943 2944 /* 2945 * Change state if we're not charging. If we're charging we will wake 2946 * up on the FG IRQ 2947 */ 2948 if (!di->flags.charging) { 2949 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP); 2950 queue_work(di->fg_wq, &di->fg_work); 2951 } 2952 2953 return 0; 2954 } 2955 2956 static int __maybe_unused ab8500_fg_suspend(struct device *dev) 2957 { 2958 struct ab8500_fg *di = dev_get_drvdata(dev); 2959 2960 flush_delayed_work(&di->fg_periodic_work); 2961 flush_work(&di->fg_work); 2962 flush_work(&di->fg_acc_cur_work); 2963 flush_delayed_work(&di->fg_reinit_work); 2964 flush_delayed_work(&di->fg_low_bat_work); 2965 flush_delayed_work(&di->fg_check_hw_failure_work); 2966 2967 /* 2968 * If the FG is enabled we will disable it before going to suspend 2969 * only if we're not charging 2970 */ 2971 if (di->flags.fg_enabled && !di->flags.charging) 2972 ab8500_fg_coulomb_counter(di, false); 2973 2974 return 0; 2975 } 2976 2977 /* ab8500 fg driver interrupts and their respective isr */ 2978 static struct ab8500_fg_interrupts ab8500_fg_irq[] = { 2979 {"NCONV_ACCU", ab8500_fg_cc_convend_handler}, 2980 {"BATT_OVV", ab8500_fg_batt_ovv_handler}, 2981 {"LOW_BAT_F", ab8500_fg_lowbatf_handler}, 2982 {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler}, 2983 {"CCEOC", ab8500_fg_cc_data_end_handler}, 2984 }; 2985 2986 static char *supply_interface[] = { 2987 "ab8500_chargalg", 2988 "ab8500_usb", 2989 }; 2990 2991 static const struct power_supply_desc ab8500_fg_desc = { 2992 .name = "ab8500_fg", 2993 .type = POWER_SUPPLY_TYPE_BATTERY, 2994 .properties = ab8500_fg_props, 2995 .num_properties = ARRAY_SIZE(ab8500_fg_props), 2996 .get_property = ab8500_fg_get_property, 2997 .external_power_changed = ab8500_fg_external_power_changed, 2998 }; 2999 3000 static int ab8500_fg_bind(struct device *dev, struct device *master, 3001 void *data) 3002 { 3003 struct ab8500_fg *di = dev_get_drvdata(dev); 3004 3005 /* Create a work queue for running the FG algorithm */ 3006 di->fg_wq = alloc_ordered_workqueue("ab8500_fg_wq", WQ_MEM_RECLAIM); 3007 if (di->fg_wq == NULL) { 3008 dev_err(dev, "failed to create work queue\n"); 3009 return -ENOMEM; 3010 } 3011 3012 di->bat_cap.max_mah_design = di->bm->bi->charge_full_design_uah; 3013 di->bat_cap.max_mah = di->bat_cap.max_mah_design; 3014 di->vbat_nom_uv = di->bm->bi->voltage_max_design_uv; 3015 3016 /* Start the coulomb counter */ 3017 ab8500_fg_coulomb_counter(di, true); 3018 /* Run the FG algorithm */ 3019 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 3020 3021 return 0; 3022 } 3023 3024 static void ab8500_fg_unbind(struct device *dev, struct device *master, 3025 void *data) 3026 { 3027 struct ab8500_fg *di = dev_get_drvdata(dev); 3028 int ret; 3029 3030 /* Disable coulomb counter */ 3031 ret = ab8500_fg_coulomb_counter(di, false); 3032 if (ret) 3033 dev_err(dev, "failed to disable coulomb counter\n"); 3034 3035 destroy_workqueue(di->fg_wq); 3036 flush_scheduled_work(); 3037 } 3038 3039 static const struct component_ops ab8500_fg_component_ops = { 3040 .bind = ab8500_fg_bind, 3041 .unbind = ab8500_fg_unbind, 3042 }; 3043 3044 static int ab8500_fg_probe(struct platform_device *pdev) 3045 { 3046 struct device *dev = &pdev->dev; 3047 struct power_supply_config psy_cfg = {}; 3048 struct ab8500_fg *di; 3049 int i, irq; 3050 int ret = 0; 3051 3052 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL); 3053 if (!di) 3054 return -ENOMEM; 3055 3056 di->bm = &ab8500_bm_data; 3057 3058 mutex_init(&di->cc_lock); 3059 3060 /* get parent data */ 3061 di->dev = dev; 3062 di->parent = dev_get_drvdata(pdev->dev.parent); 3063 3064 di->main_bat_v = devm_iio_channel_get(dev, "main_bat_v"); 3065 if (IS_ERR(di->main_bat_v)) { 3066 ret = dev_err_probe(dev, PTR_ERR(di->main_bat_v), 3067 "failed to get main battery ADC channel\n"); 3068 return ret; 3069 } 3070 3071 psy_cfg.supplied_to = supply_interface; 3072 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface); 3073 psy_cfg.drv_data = di; 3074 3075 di->init_capacity = true; 3076 3077 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT); 3078 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT); 3079 3080 /* Init work for running the fg algorithm instantly */ 3081 INIT_WORK(&di->fg_work, ab8500_fg_instant_work); 3082 3083 /* Init work for getting the battery accumulated current */ 3084 INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work); 3085 3086 /* Init work for reinitialising the fg algorithm */ 3087 INIT_DEFERRABLE_WORK(&di->fg_reinit_work, 3088 ab8500_fg_reinit_work); 3089 3090 /* Work delayed Queue to run the state machine */ 3091 INIT_DEFERRABLE_WORK(&di->fg_periodic_work, 3092 ab8500_fg_periodic_work); 3093 3094 /* Work to check low battery condition */ 3095 INIT_DEFERRABLE_WORK(&di->fg_low_bat_work, 3096 ab8500_fg_low_bat_work); 3097 3098 /* Init work for HW failure check */ 3099 INIT_DEFERRABLE_WORK(&di->fg_check_hw_failure_work, 3100 ab8500_fg_check_hw_failure_work); 3101 3102 /* Reset battery low voltage flag */ 3103 di->flags.low_bat = false; 3104 3105 /* Initialize low battery counter */ 3106 di->low_bat_cnt = 10; 3107 3108 /* Initialize OVV, and other registers */ 3109 ret = ab8500_fg_init_hw_registers(di); 3110 if (ret) { 3111 dev_err(dev, "failed to initialize registers\n"); 3112 return ret; 3113 } 3114 3115 /* Consider battery unknown until we're informed otherwise */ 3116 di->flags.batt_unknown = true; 3117 di->flags.batt_id_received = false; 3118 3119 /* Register FG power supply class */ 3120 di->fg_psy = devm_power_supply_register(dev, &ab8500_fg_desc, &psy_cfg); 3121 if (IS_ERR(di->fg_psy)) { 3122 dev_err(dev, "failed to register FG psy\n"); 3123 return PTR_ERR(di->fg_psy); 3124 } 3125 3126 di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer); 3127 3128 /* 3129 * Initialize completion used to notify completion and start 3130 * of inst current 3131 */ 3132 init_completion(&di->ab8500_fg_started); 3133 init_completion(&di->ab8500_fg_complete); 3134 3135 /* Register primary interrupt handlers */ 3136 for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq); i++) { 3137 irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name); 3138 if (irq < 0) 3139 return irq; 3140 3141 ret = devm_request_threaded_irq(dev, irq, NULL, 3142 ab8500_fg_irq[i].isr, 3143 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT, 3144 ab8500_fg_irq[i].name, di); 3145 3146 if (ret != 0) { 3147 dev_err(dev, "failed to request %s IRQ %d: %d\n", 3148 ab8500_fg_irq[i].name, irq, ret); 3149 return ret; 3150 } 3151 dev_dbg(dev, "Requested %s IRQ %d: %d\n", 3152 ab8500_fg_irq[i].name, irq, ret); 3153 } 3154 3155 di->irq = platform_get_irq_byname(pdev, "CCEOC"); 3156 disable_irq(di->irq); 3157 di->nbr_cceoc_irq_cnt = 0; 3158 3159 platform_set_drvdata(pdev, di); 3160 3161 ret = ab8500_fg_sysfs_init(di); 3162 if (ret) { 3163 dev_err(dev, "failed to create sysfs entry\n"); 3164 return ret; 3165 } 3166 3167 ret = ab8500_fg_sysfs_psy_create_attrs(di); 3168 if (ret) { 3169 dev_err(dev, "failed to create FG psy\n"); 3170 ab8500_fg_sysfs_exit(di); 3171 return ret; 3172 } 3173 3174 /* Calibrate the fg first time */ 3175 di->flags.calibrate = true; 3176 di->calib_state = AB8500_FG_CALIB_INIT; 3177 3178 /* Use room temp as default value until we get an update from driver. */ 3179 di->bat_temp = 210; 3180 3181 list_add_tail(&di->node, &ab8500_fg_list); 3182 3183 return component_add(dev, &ab8500_fg_component_ops); 3184 } 3185 3186 static int ab8500_fg_remove(struct platform_device *pdev) 3187 { 3188 int ret = 0; 3189 struct ab8500_fg *di = platform_get_drvdata(pdev); 3190 3191 component_del(&pdev->dev, &ab8500_fg_component_ops); 3192 list_del(&di->node); 3193 ab8500_fg_sysfs_exit(di); 3194 ab8500_fg_sysfs_psy_remove_attrs(di); 3195 3196 return ret; 3197 } 3198 3199 static SIMPLE_DEV_PM_OPS(ab8500_fg_pm_ops, ab8500_fg_suspend, ab8500_fg_resume); 3200 3201 static const struct of_device_id ab8500_fg_match[] = { 3202 { .compatible = "stericsson,ab8500-fg", }, 3203 { }, 3204 }; 3205 MODULE_DEVICE_TABLE(of, ab8500_fg_match); 3206 3207 struct platform_driver ab8500_fg_driver = { 3208 .probe = ab8500_fg_probe, 3209 .remove = ab8500_fg_remove, 3210 .driver = { 3211 .name = "ab8500-fg", 3212 .of_match_table = ab8500_fg_match, 3213 .pm = &ab8500_fg_pm_ops, 3214 }, 3215 }; 3216 MODULE_LICENSE("GPL v2"); 3217 MODULE_AUTHOR("Johan Palsson, Karl Komierowski"); 3218 MODULE_ALIAS("platform:ab8500-fg"); 3219 MODULE_DESCRIPTION("AB8500 Fuel Gauge driver"); 3220