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