2 * Copyright (C) ST-Ericsson AB 2012
4 * Main and Back-up battery management driver.
6 * Note: Backup battery management is required in case of Li-Ion battery and not
7 * for capacitive battery. HREF boards have capacitive battery and hence backup
8 * battery management is not used and the supported code is available in this
11 * License Terms: GNU General Public License v2
13 * Johan Palsson <johan.palsson@stericsson.com>
14 * Karl Komierowski <karl.komierowski@stericsson.com>
15 * Arun R Murthy <arun.murthy@stericsson.com>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
23 #include <linux/power_supply.h>
24 #include <linux/kobject.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/time.h>
28 #include <linux/time64.h>
30 #include <linux/completion.h>
31 #include <linux/mfd/core.h>
32 #include <linux/mfd/abx500.h>
33 #include <linux/mfd/abx500/ab8500.h>
34 #include <linux/mfd/abx500/ab8500-bm.h>
35 #include <linux/mfd/abx500/ab8500-gpadc.h>
36 #include <linux/kernel.h>
38 #define MILLI_TO_MICRO 1000
39 #define FG_LSB_IN_MA 1627
40 #define QLSB_NANO_AMP_HOURS_X10 1071
41 #define INS_CURR_TIMEOUT (3 * HZ)
43 #define SEC_TO_SAMPLE(S) (S * 4)
45 #define NBR_AVG_SAMPLES 20
47 #define LOW_BAT_CHECK_INTERVAL (HZ / 16) /* 62.5 ms */
49 #define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */
50 #define BATT_OK_MIN 2360 /* mV */
51 #define BATT_OK_INCREMENT 50 /* mV */
52 #define BATT_OK_MAX_NR_INCREMENTS 0xE
57 #define interpolate(x, x1, y1, x2, y2) \
58 ((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1))));
61 * struct ab8500_fg_interrupts - ab8500 fg interupts
62 * @name: name of the interrupt
63 * @isr function pointer to the isr
65 struct ab8500_fg_interrupts {
67 irqreturn_t (*isr)(int irq, void *data);
70 enum ab8500_fg_discharge_state {
71 AB8500_FG_DISCHARGE_INIT,
72 AB8500_FG_DISCHARGE_INITMEASURING,
73 AB8500_FG_DISCHARGE_INIT_RECOVERY,
74 AB8500_FG_DISCHARGE_RECOVERY,
75 AB8500_FG_DISCHARGE_READOUT_INIT,
76 AB8500_FG_DISCHARGE_READOUT,
77 AB8500_FG_DISCHARGE_WAKEUP,
80 static char *discharge_state[] = {
82 "DISCHARGE_INITMEASURING",
83 "DISCHARGE_INIT_RECOVERY",
85 "DISCHARGE_READOUT_INIT",
90 enum ab8500_fg_charge_state {
91 AB8500_FG_CHARGE_INIT,
92 AB8500_FG_CHARGE_READOUT,
95 static char *charge_state[] = {
100 enum ab8500_fg_calibration_state {
101 AB8500_FG_CALIB_INIT,
102 AB8500_FG_CALIB_WAIT,
106 struct ab8500_fg_avg_cap {
108 int samples[NBR_AVG_SAMPLES];
109 time64_t time_stamps[NBR_AVG_SAMPLES];
115 struct ab8500_fg_cap_scaling {
118 int disable_cap_level;
122 struct ab8500_fg_battery_capacity {
132 struct ab8500_fg_cap_scaling cap_scale;
135 struct ab8500_fg_flags {
147 bool batt_id_received;
150 struct inst_curr_result_list {
151 struct list_head list;
156 * struct ab8500_fg - ab8500 FG device information
157 * @dev: Pointer to the structure device
158 * @node: a list of AB8500 FGs, hence prepared for reentrance
159 * @irq holds the CCEOC interrupt number
160 * @vbat: Battery voltage in mV
161 * @vbat_nom: Nominal battery voltage in mV
162 * @inst_curr: Instantenous battery current in mA
163 * @avg_curr: Average battery current in mA
164 * @bat_temp battery temperature
165 * @fg_samples: Number of samples used in the FG accumulation
166 * @accu_charge: Accumulated charge from the last conversion
167 * @recovery_cnt: Counter for recovery mode
168 * @high_curr_cnt: Counter for high current mode
169 * @init_cnt: Counter for init mode
170 * @low_bat_cnt Counter for number of consecutive low battery measures
171 * @nbr_cceoc_irq_cnt Counter for number of CCEOC irqs received since enabled
172 * @recovery_needed: Indicate if recovery is needed
173 * @high_curr_mode: Indicate if we're in high current mode
174 * @init_capacity: Indicate if initial capacity measuring should be done
175 * @turn_off_fg: True if fg was off before current measurement
176 * @calib_state State during offset calibration
177 * @discharge_state: Current discharge state
178 * @charge_state: Current charge state
179 * @ab8500_fg_started Completion struct used for the instant current start
180 * @ab8500_fg_complete Completion struct used for the instant current reading
181 * @flags: Structure for information about events triggered
182 * @bat_cap: Structure for battery capacity specific parameters
183 * @avg_cap: Average capacity filter
184 * @parent: Pointer to the struct ab8500
185 * @gpadc: Pointer to the struct gpadc
186 * @bm: Platform specific battery management information
187 * @fg_psy: Structure that holds the FG specific battery properties
188 * @fg_wq: Work queue for running the FG algorithm
189 * @fg_periodic_work: Work to run the FG algorithm periodically
190 * @fg_low_bat_work: Work to check low bat condition
191 * @fg_reinit_work Work used to reset and reinitialise the FG algorithm
192 * @fg_work: Work to run the FG algorithm instantly
193 * @fg_acc_cur_work: Work to read the FG accumulator
194 * @fg_check_hw_failure_work: Work for checking HW state
195 * @cc_lock: Mutex for locking the CC
196 * @fg_kobject: Structure of type kobject
200 struct list_head node;
213 int nbr_cceoc_irq_cnt;
214 bool recovery_needed;
218 enum ab8500_fg_calibration_state calib_state;
219 enum ab8500_fg_discharge_state discharge_state;
220 enum ab8500_fg_charge_state charge_state;
221 struct completion ab8500_fg_started;
222 struct completion ab8500_fg_complete;
223 struct ab8500_fg_flags flags;
224 struct ab8500_fg_battery_capacity bat_cap;
225 struct ab8500_fg_avg_cap avg_cap;
226 struct ab8500 *parent;
227 struct ab8500_gpadc *gpadc;
228 struct abx500_bm_data *bm;
229 struct power_supply *fg_psy;
230 struct workqueue_struct *fg_wq;
231 struct delayed_work fg_periodic_work;
232 struct delayed_work fg_low_bat_work;
233 struct delayed_work fg_reinit_work;
234 struct work_struct fg_work;
235 struct work_struct fg_acc_cur_work;
236 struct delayed_work fg_check_hw_failure_work;
237 struct mutex cc_lock;
238 struct kobject fg_kobject;
240 static LIST_HEAD(ab8500_fg_list);
243 * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
244 * (i.e. the first fuel gauge in the instance list)
246 struct ab8500_fg *ab8500_fg_get(void)
248 return list_first_entry_or_null(&ab8500_fg_list, struct ab8500_fg,
252 /* Main battery properties */
253 static enum power_supply_property ab8500_fg_props[] = {
254 POWER_SUPPLY_PROP_VOLTAGE_NOW,
255 POWER_SUPPLY_PROP_CURRENT_NOW,
256 POWER_SUPPLY_PROP_CURRENT_AVG,
257 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
258 POWER_SUPPLY_PROP_ENERGY_FULL,
259 POWER_SUPPLY_PROP_ENERGY_NOW,
260 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
261 POWER_SUPPLY_PROP_CHARGE_FULL,
262 POWER_SUPPLY_PROP_CHARGE_NOW,
263 POWER_SUPPLY_PROP_CAPACITY,
264 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
268 * This array maps the raw hex value to lowbat voltage used by the AB8500
269 * Values taken from the UM0836
271 static int ab8500_fg_lowbat_voltage_map[] = {
338 static u8 ab8500_volt_to_regval(int voltage)
342 if (voltage < ab8500_fg_lowbat_voltage_map[0])
345 for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) {
346 if (voltage < ab8500_fg_lowbat_voltage_map[i])
350 /* If not captured above, return index of last element */
351 return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1;
355 * ab8500_fg_is_low_curr() - Low or high current mode
356 * @di: pointer to the ab8500_fg structure
357 * @curr: the current to base or our decision on
359 * Low current mode if the current consumption is below a certain threshold
361 static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr)
364 * We want to know if we're in low current mode
366 if (curr > -di->bm->fg_params->high_curr_threshold)
373 * ab8500_fg_add_cap_sample() - Add capacity to average filter
374 * @di: pointer to the ab8500_fg structure
375 * @sample: the capacity in mAh to add to the filter
377 * A capacity is added to the filter and a new mean capacity is calculated and
380 static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample)
382 struct timespec64 ts64;
383 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
385 getnstimeofday64(&ts64);
388 avg->sum += sample - avg->samples[avg->pos];
389 avg->samples[avg->pos] = sample;
390 avg->time_stamps[avg->pos] = ts64.tv_sec;
393 if (avg->pos == NBR_AVG_SAMPLES)
396 if (avg->nbr_samples < NBR_AVG_SAMPLES)
400 * Check the time stamp for each sample. If too old,
401 * replace with latest sample
403 } while (ts64.tv_sec - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]);
405 avg->avg = avg->sum / avg->nbr_samples;
411 * ab8500_fg_clear_cap_samples() - Clear average filter
412 * @di: pointer to the ab8500_fg structure
414 * The capacity filter is is reset to zero.
416 static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di)
419 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
422 avg->nbr_samples = 0;
426 for (i = 0; i < NBR_AVG_SAMPLES; i++) {
428 avg->time_stamps[i] = 0;
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
437 * The capacity filter is filled with a capacity in mAh
439 static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample)
442 struct timespec64 ts64;
443 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
445 getnstimeofday64(&ts64);
447 for (i = 0; i < NBR_AVG_SAMPLES; i++) {
448 avg->samples[i] = sample;
449 avg->time_stamps[i] = ts64.tv_sec;
453 avg->nbr_samples = NBR_AVG_SAMPLES;
454 avg->sum = sample * NBR_AVG_SAMPLES;
459 * ab8500_fg_coulomb_counter() - enable coulomb counter
460 * @di: pointer to the ab8500_fg structure
461 * @enable: enable/disable
463 * Enable/Disable coulomb counter.
464 * On failure returns negative value.
466 static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable)
469 mutex_lock(&di->cc_lock);
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);
478 /* Program the samples */
479 ret = abx500_set_register_interruptible(di->dev,
480 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
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));
492 di->flags.fg_enabled = true;
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);
501 ret = abx500_set_register_interruptible(di->dev,
502 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0);
507 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
508 AB8500_RTC_CC_CONF_REG, 0);
512 di->flags.fg_enabled = false;
515 dev_dbg(di->dev, " CC enabled: %d Samples: %d\n",
516 enable, di->fg_samples);
518 mutex_unlock(&di->cc_lock);
522 dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__);
523 mutex_unlock(&di->cc_lock);
528 * ab8500_fg_inst_curr_start() - start battery instantaneous current
529 * @di: pointer to the ab8500_fg structure
531 * Returns 0 or error code
532 * Note: This is part "one" and has to be called before
533 * ab8500_fg_inst_curr_finalize()
535 int ab8500_fg_inst_curr_start(struct ab8500_fg *di)
540 mutex_lock(&di->cc_lock);
542 di->nbr_cceoc_irq_cnt = 0;
543 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
544 AB8500_RTC_CC_CONF_REG, ®_val);
548 if (!(reg_val & CC_PWR_UP_ENA)) {
549 dev_dbg(di->dev, "%s Enable FG\n", __func__);
550 di->turn_off_fg = true;
552 /* Program the samples */
553 ret = abx500_set_register_interruptible(di->dev,
554 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
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));
566 di->turn_off_fg = false;
570 reinit_completion(&di->ab8500_fg_started);
571 reinit_completion(&di->ab8500_fg_complete);
574 /* Note: cc_lock is still locked */
577 mutex_unlock(&di->cc_lock);
582 * ab8500_fg_inst_curr_started() - check if fg conversion has started
583 * @di: pointer to the ab8500_fg structure
585 * Returns 1 if conversion started, 0 if still waiting
587 int ab8500_fg_inst_curr_started(struct ab8500_fg *di)
589 return completion_done(&di->ab8500_fg_started);
593 * ab8500_fg_inst_curr_done() - check if fg conversion is done
594 * @di: pointer to the ab8500_fg structure
596 * Returns 1 if conversion done, 0 if still waiting
598 int ab8500_fg_inst_curr_done(struct ab8500_fg *di)
600 return completion_done(&di->ab8500_fg_complete);
604 * ab8500_fg_inst_curr_finalize() - battery instantaneous current
605 * @di: pointer to the ab8500_fg structure
606 * @res: battery instantenous current(on success)
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()
612 int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res)
617 unsigned long timeout;
619 if (!completion_done(&di->ab8500_fg_complete)) {
620 timeout = wait_for_completion_timeout(
621 &di->ab8500_fg_complete,
623 dev_dbg(di->dev, "Finalize time: %d ms\n",
624 jiffies_to_msecs(INS_CURR_TIMEOUT - timeout));
627 disable_irq(di->irq);
628 di->nbr_cceoc_irq_cnt = 0;
629 dev_err(di->dev, "completion timed out [%d]\n",
635 disable_irq(di->irq);
636 di->nbr_cceoc_irq_cnt = 0;
638 ret = abx500_mask_and_set_register_interruptible(di->dev,
639 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
642 /* 100uS between read request and read is needed */
643 usleep_range(100, 100);
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);
651 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
652 AB8500_GASG_CC_SMPL_CNVH_REG, &high);
657 * negative value for Discharging
658 * convert 2's compliment into decimal
661 val = (low | (high << 8) | 0xFFFFE000);
663 val = (low | (high << 8));
666 * Convert to unit value in mA
667 * Full scale input voltage is
668 * 63.160mV => LSB = 63.160mV/(4096*res) = 1.542mA
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
674 val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) /
675 (1000 * di->bm->fg_res);
677 if (di->turn_off_fg) {
678 dev_dbg(di->dev, "%s Disable FG\n", __func__);
680 /* Clear any pending read requests */
681 ret = abx500_set_register_interruptible(di->dev,
682 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0);
687 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
688 AB8500_RTC_CC_CONF_REG, 0);
692 mutex_unlock(&di->cc_lock);
697 mutex_unlock(&di->cc_lock);
702 * ab8500_fg_inst_curr_blocking() - battery instantaneous current
703 * @di: pointer to the ab8500_fg structure
704 * @res: battery instantenous current(on success)
706 * Returns 0 else error code
708 int ab8500_fg_inst_curr_blocking(struct ab8500_fg *di)
711 unsigned long timeout;
714 ret = ab8500_fg_inst_curr_start(di);
716 dev_err(di->dev, "Failed to initialize fg_inst\n");
720 /* Wait for CC to actually start */
721 if (!completion_done(&di->ab8500_fg_started)) {
722 timeout = wait_for_completion_timeout(
723 &di->ab8500_fg_started,
725 dev_dbg(di->dev, "Start time: %d ms\n",
726 jiffies_to_msecs(INS_CURR_TIMEOUT - timeout));
729 dev_err(di->dev, "completion timed out [%d]\n",
735 ret = ab8500_fg_inst_curr_finalize(di, &res);
737 dev_err(di->dev, "Failed to finalize fg_inst\n");
741 dev_dbg(di->dev, "%s instant current: %d", __func__, res);
744 disable_irq(di->irq);
745 mutex_unlock(&di->cc_lock);
750 * ab8500_fg_acc_cur_work() - average battery current
751 * @work: pointer to the work_struct structure
753 * Updated the average battery current obtained from the
756 static void ab8500_fg_acc_cur_work(struct work_struct *work)
762 struct ab8500_fg *di = container_of(work,
763 struct ab8500_fg, fg_acc_cur_work);
765 mutex_lock(&di->cc_lock);
766 ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE,
767 AB8500_GASG_CC_NCOV_ACCU_CTRL, RD_NCONV_ACCU_REQ);
771 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
772 AB8500_GASG_CC_NCOV_ACCU_LOW, &low);
776 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
777 AB8500_GASG_CC_NCOV_ACCU_MED, &med);
781 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
782 AB8500_GASG_CC_NCOV_ACCU_HIGH, &high);
786 /* Check for sign bit in case of negative value, 2's compliment */
788 val = (low | (med << 8) | (high << 16) | 0xFFE00000);
790 val = (low | (med << 8) | (high << 16));
794 * Given a 250ms conversion cycle time the LSB corresponds
796 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
798 di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) /
799 (100 * di->bm->fg_res);
802 * Convert to unit value in mA
803 * by dividing by the conversion
804 * time in hours (= samples / (3600 * 4)h)
805 * and multiply with 1000
807 di->avg_curr = (val * QLSB_NANO_AMP_HOURS_X10 * 36) /
808 (1000 * di->bm->fg_res * (di->fg_samples / 4));
810 di->flags.conv_done = true;
812 mutex_unlock(&di->cc_lock);
814 queue_work(di->fg_wq, &di->fg_work);
816 dev_dbg(di->dev, "fg_res: %d, fg_samples: %d, gasg: %d, accu_charge: %d \n",
817 di->bm->fg_res, di->fg_samples, val, di->accu_charge);
821 "Failed to read or write gas gauge registers\n");
822 mutex_unlock(&di->cc_lock);
823 queue_work(di->fg_wq, &di->fg_work);
827 * ab8500_fg_bat_voltage() - get battery voltage
828 * @di: pointer to the ab8500_fg structure
830 * Returns battery voltage(on success) else error code
832 static int ab8500_fg_bat_voltage(struct ab8500_fg *di)
837 vbat = ab8500_gpadc_convert(di->gpadc, MAIN_BAT_V);
840 "%s gpadc conversion failed, using previous value\n",
850 * ab8500_fg_volt_to_capacity() - Voltage based capacity
851 * @di: pointer to the ab8500_fg structure
852 * @voltage: The voltage to convert to a capacity
854 * Returns battery capacity in per mille based on voltage
856 static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage)
859 const struct abx500_v_to_cap *tbl;
862 tbl = di->bm->bat_type[di->bm->batt_id].v_to_cap_tbl,
863 tbl_size = di->bm->bat_type[di->bm->batt_id].n_v_cap_tbl_elements;
865 for (i = 0; i < tbl_size; ++i) {
866 if (voltage > tbl[i].voltage)
870 if ((i > 0) && (i < tbl_size)) {
871 cap = interpolate(voltage,
873 tbl[i].capacity * 10,
875 tbl[i-1].capacity * 10);
882 dev_dbg(di->dev, "%s Vbat: %d, Cap: %d per mille",
883 __func__, voltage, cap);
889 * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
890 * @di: pointer to the ab8500_fg structure
892 * Returns battery capacity based on battery voltage that is not compensated
893 * for the voltage drop due to the load
895 static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di)
897 di->vbat = ab8500_fg_bat_voltage(di);
898 return ab8500_fg_volt_to_capacity(di, di->vbat);
902 * ab8500_fg_battery_resistance() - Returns the battery inner resistance
903 * @di: pointer to the ab8500_fg structure
905 * Returns battery inner resistance added with the fuel gauge resistor value
906 * to get the total resistance in the whole link from gnd to bat+ node.
908 static int ab8500_fg_battery_resistance(struct ab8500_fg *di)
911 const struct batres_vs_temp *tbl;
914 tbl = di->bm->bat_type[di->bm->batt_id].batres_tbl;
915 tbl_size = di->bm->bat_type[di->bm->batt_id].n_batres_tbl_elements;
917 for (i = 0; i < tbl_size; ++i) {
918 if (di->bat_temp / 10 > tbl[i].temp)
922 if ((i > 0) && (i < tbl_size)) {
923 resist = interpolate(di->bat_temp / 10,
929 resist = tbl[0].resist;
931 resist = tbl[tbl_size - 1].resist;
934 dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d"
935 " fg resistance %d, total: %d (mOhm)\n",
936 __func__, di->bat_temp, resist, di->bm->fg_res / 10,
937 (di->bm->fg_res / 10) + resist);
939 /* fg_res variable is in 0.1mOhm */
940 resist += di->bm->fg_res / 10;
946 * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
947 * @di: pointer to the ab8500_fg structure
949 * Returns battery capacity based on battery voltage that is load compensated
950 * for the voltage drop
952 static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di)
958 ab8500_fg_inst_curr_start(di);
961 vbat += ab8500_fg_bat_voltage(di);
963 usleep_range(5000, 6000);
964 } while (!ab8500_fg_inst_curr_done(di));
966 ab8500_fg_inst_curr_finalize(di, &di->inst_curr);
969 res = ab8500_fg_battery_resistance(di);
971 /* Use Ohms law to get the load compensated voltage */
972 vbat_comp = di->vbat - (di->inst_curr * res) / 1000;
974 dev_dbg(di->dev, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, "
975 "R: %dmOhm, Current: %dmA Vbat Samples: %d\n",
976 __func__, di->vbat, vbat_comp, res, di->inst_curr, i);
978 return ab8500_fg_volt_to_capacity(di, vbat_comp);
982 * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
983 * @di: pointer to the ab8500_fg structure
984 * @cap_mah: capacity in mAh
986 * Converts capacity in mAh to capacity in permille
988 static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah)
990 return (cap_mah * 1000) / di->bat_cap.max_mah_design;
994 * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
995 * @di: pointer to the ab8500_fg structure
996 * @cap_pm: capacity in permille
998 * Converts capacity in permille to capacity in mAh
1000 static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm)
1002 return cap_pm * di->bat_cap.max_mah_design / 1000;
1006 * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
1007 * @di: pointer to the ab8500_fg structure
1008 * @cap_mah: capacity in mAh
1010 * Converts capacity in mAh to capacity in uWh
1012 static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah)
1017 div_res = ((u64) cap_mah) * ((u64) di->vbat_nom);
1018 div_rem = do_div(div_res, 1000);
1020 /* Make sure to round upwards if necessary */
1021 if (div_rem >= 1000 / 2)
1024 return (int) div_res;
1028 * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
1029 * @di: pointer to the ab8500_fg structure
1031 * Return the capacity in mAh based on previous calculated capcity and the FG
1032 * accumulator register value. The filter is filled with this capacity
1034 static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di)
1036 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1041 /* Capacity should not be less than 0 */
1042 if (di->bat_cap.mah + di->accu_charge > 0)
1043 di->bat_cap.mah += di->accu_charge;
1045 di->bat_cap.mah = 0;
1047 * We force capacity to 100% once when the algorithm
1048 * reports that it's full.
1050 if (di->bat_cap.mah >= di->bat_cap.max_mah_design ||
1051 di->flags.force_full) {
1052 di->bat_cap.mah = di->bat_cap.max_mah_design;
1055 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1056 di->bat_cap.permille =
1057 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1059 /* We need to update battery voltage and inst current when charging */
1060 di->vbat = ab8500_fg_bat_voltage(di);
1061 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1063 return di->bat_cap.mah;
1067 * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage
1068 * @di: pointer to the ab8500_fg structure
1069 * @comp: if voltage should be load compensated before capacity calc
1071 * Return the capacity in mAh based on the battery voltage. The voltage can
1072 * either be load compensated or not. This value is added to the filter and a
1073 * new mean value is calculated and returned.
1075 static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di, bool comp)
1080 permille = ab8500_fg_load_comp_volt_to_capacity(di);
1082 permille = ab8500_fg_uncomp_volt_to_capacity(di);
1084 mah = ab8500_fg_convert_permille_to_mah(di, permille);
1086 di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah);
1087 di->bat_cap.permille =
1088 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1090 return di->bat_cap.mah;
1094 * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG
1095 * @di: pointer to the ab8500_fg structure
1097 * Return the capacity in mAh based on previous calculated capcity and the FG
1098 * accumulator register value. This value is added to the filter and a
1099 * new mean value is calculated and returned.
1101 static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di)
1103 int permille_volt, permille;
1105 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1110 /* Capacity should not be less than 0 */
1111 if (di->bat_cap.mah + di->accu_charge > 0)
1112 di->bat_cap.mah += di->accu_charge;
1114 di->bat_cap.mah = 0;
1116 if (di->bat_cap.mah >= di->bat_cap.max_mah_design)
1117 di->bat_cap.mah = di->bat_cap.max_mah_design;
1120 * Check against voltage based capacity. It can not be lower
1121 * than what the uncompensated voltage says
1123 permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1124 permille_volt = ab8500_fg_uncomp_volt_to_capacity(di);
1126 if (permille < permille_volt) {
1127 di->bat_cap.permille = permille_volt;
1128 di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di,
1129 di->bat_cap.permille);
1131 dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n",
1136 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1138 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1139 di->bat_cap.permille =
1140 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1143 return di->bat_cap.mah;
1147 * ab8500_fg_capacity_level() - Get the battery capacity level
1148 * @di: pointer to the ab8500_fg structure
1150 * Get the battery capacity level based on the capacity in percent
1152 static int ab8500_fg_capacity_level(struct ab8500_fg *di)
1156 percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10);
1158 if (percent <= di->bm->cap_levels->critical ||
1160 ret = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1161 else if (percent <= di->bm->cap_levels->low)
1162 ret = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1163 else if (percent <= di->bm->cap_levels->normal)
1164 ret = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1165 else if (percent <= di->bm->cap_levels->high)
1166 ret = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
1168 ret = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1174 * ab8500_fg_calculate_scaled_capacity() - Capacity scaling
1175 * @di: pointer to the ab8500_fg structure
1177 * Calculates the capacity to be shown to upper layers. Scales the capacity
1178 * to have 100% as a reference from the actual capacity upon removal of charger
1179 * when charging is in maintenance mode.
1181 static int ab8500_fg_calculate_scaled_capacity(struct ab8500_fg *di)
1183 struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale;
1184 int capacity = di->bat_cap.prev_percent;
1190 * As long as we are in fully charge mode scale the capacity
1193 if (di->flags.fully_charged) {
1194 cs->cap_to_scale[0] = 100;
1195 cs->cap_to_scale[1] =
1196 max(capacity, di->bm->fg_params->maint_thres);
1197 dev_dbg(di->dev, "Scale cap with %d/%d\n",
1198 cs->cap_to_scale[0], cs->cap_to_scale[1]);
1201 /* Calculates the scaled capacity. */
1202 if ((cs->cap_to_scale[0] != cs->cap_to_scale[1])
1203 && (cs->cap_to_scale[1] > 0))
1205 DIV_ROUND_CLOSEST(di->bat_cap.prev_percent *
1206 cs->cap_to_scale[0],
1207 cs->cap_to_scale[1]));
1209 if (di->flags.charging) {
1210 if (capacity < cs->disable_cap_level) {
1211 cs->disable_cap_level = capacity;
1212 dev_dbg(di->dev, "Cap to stop scale lowered %d%%\n",
1213 cs->disable_cap_level);
1214 } else if (!di->flags.fully_charged) {
1215 if (di->bat_cap.prev_percent >=
1216 cs->disable_cap_level) {
1217 dev_dbg(di->dev, "Disabling scaled capacity\n");
1219 capacity = di->bat_cap.prev_percent;
1222 "Waiting in cap to level %d%%\n",
1223 cs->disable_cap_level);
1224 capacity = cs->disable_cap_level;
1233 * ab8500_fg_update_cap_scalers() - Capacity scaling
1234 * @di: pointer to the ab8500_fg structure
1236 * To be called when state change from charge<->discharge to update
1237 * the capacity scalers.
1239 static void ab8500_fg_update_cap_scalers(struct ab8500_fg *di)
1241 struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale;
1245 if (di->flags.charging) {
1246 di->bat_cap.cap_scale.disable_cap_level =
1247 di->bat_cap.cap_scale.scaled_cap;
1248 dev_dbg(di->dev, "Cap to stop scale at charge %d%%\n",
1249 di->bat_cap.cap_scale.disable_cap_level);
1251 if (cs->scaled_cap != 100) {
1252 cs->cap_to_scale[0] = cs->scaled_cap;
1253 cs->cap_to_scale[1] = di->bat_cap.prev_percent;
1255 cs->cap_to_scale[0] = 100;
1256 cs->cap_to_scale[1] =
1257 max(di->bat_cap.prev_percent,
1258 di->bm->fg_params->maint_thres);
1261 dev_dbg(di->dev, "Cap to scale at discharge %d/%d\n",
1262 cs->cap_to_scale[0], cs->cap_to_scale[1]);
1267 * ab8500_fg_check_capacity_limits() - Check if capacity has changed
1268 * @di: pointer to the ab8500_fg structure
1269 * @init: capacity is allowed to go up in init mode
1271 * Check if capacity or capacity limit has changed and notify the system
1272 * about it using the power_supply framework
1274 static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init)
1276 bool changed = false;
1277 int percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10);
1279 di->bat_cap.level = ab8500_fg_capacity_level(di);
1281 if (di->bat_cap.level != di->bat_cap.prev_level) {
1283 * We do not allow reported capacity level to go up
1284 * unless we're charging or if we're in init
1286 if (!(!di->flags.charging && di->bat_cap.level >
1287 di->bat_cap.prev_level) || init) {
1288 dev_dbg(di->dev, "level changed from %d to %d\n",
1289 di->bat_cap.prev_level,
1291 di->bat_cap.prev_level = di->bat_cap.level;
1294 dev_dbg(di->dev, "level not allowed to go up "
1295 "since no charger is connected: %d to %d\n",
1296 di->bat_cap.prev_level,
1302 * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate
1305 if (di->flags.low_bat) {
1306 dev_dbg(di->dev, "Battery low, set capacity to 0\n");
1307 di->bat_cap.prev_percent = 0;
1308 di->bat_cap.permille = 0;
1310 di->bat_cap.prev_mah = 0;
1311 di->bat_cap.mah = 0;
1313 } else if (di->flags.fully_charged) {
1315 * We report 100% if algorithm reported fully charged
1316 * and show 100% during maintenance charging (scaling).
1318 if (di->flags.force_full) {
1319 di->bat_cap.prev_percent = percent;
1320 di->bat_cap.prev_mah = di->bat_cap.mah;
1324 if (!di->bat_cap.cap_scale.enable &&
1325 di->bm->capacity_scaling) {
1326 di->bat_cap.cap_scale.enable = true;
1327 di->bat_cap.cap_scale.cap_to_scale[0] = 100;
1328 di->bat_cap.cap_scale.cap_to_scale[1] =
1329 di->bat_cap.prev_percent;
1330 di->bat_cap.cap_scale.disable_cap_level = 100;
1332 } else if (di->bat_cap.prev_percent != percent) {
1334 "battery reported full "
1335 "but capacity dropping: %d\n",
1337 di->bat_cap.prev_percent = percent;
1338 di->bat_cap.prev_mah = di->bat_cap.mah;
1342 } else if (di->bat_cap.prev_percent != percent) {
1345 * We will not report 0% unless we've got
1346 * the LOW_BAT IRQ, no matter what the FG
1349 di->bat_cap.prev_percent = 1;
1353 } else if (!(!di->flags.charging &&
1354 percent > di->bat_cap.prev_percent) || init) {
1356 * We do not allow reported capacity to go up
1357 * unless we're charging or if we're in init
1360 "capacity changed from %d to %d (%d)\n",
1361 di->bat_cap.prev_percent,
1363 di->bat_cap.permille);
1364 di->bat_cap.prev_percent = percent;
1365 di->bat_cap.prev_mah = di->bat_cap.mah;
1369 dev_dbg(di->dev, "capacity not allowed to go up since "
1370 "no charger is connected: %d to %d (%d)\n",
1371 di->bat_cap.prev_percent,
1373 di->bat_cap.permille);
1378 if (di->bm->capacity_scaling) {
1379 di->bat_cap.cap_scale.scaled_cap =
1380 ab8500_fg_calculate_scaled_capacity(di);
1382 dev_info(di->dev, "capacity=%d (%d)\n",
1383 di->bat_cap.prev_percent,
1384 di->bat_cap.cap_scale.scaled_cap);
1386 power_supply_changed(di->fg_psy);
1387 if (di->flags.fully_charged && di->flags.force_full) {
1388 dev_dbg(di->dev, "Battery full, notifying.\n");
1389 di->flags.force_full = false;
1390 sysfs_notify(&di->fg_kobject, NULL, "charge_full");
1392 sysfs_notify(&di->fg_kobject, NULL, "charge_now");
1396 static void ab8500_fg_charge_state_to(struct ab8500_fg *di,
1397 enum ab8500_fg_charge_state new_state)
1399 dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n",
1401 charge_state[di->charge_state],
1403 charge_state[new_state]);
1405 di->charge_state = new_state;
1408 static void ab8500_fg_discharge_state_to(struct ab8500_fg *di,
1409 enum ab8500_fg_discharge_state new_state)
1411 dev_dbg(di->dev, "Disharge state from %d [%s] to %d [%s]\n",
1412 di->discharge_state,
1413 discharge_state[di->discharge_state],
1415 discharge_state[new_state]);
1417 di->discharge_state = new_state;
1421 * ab8500_fg_algorithm_charging() - FG algorithm for when charging
1422 * @di: pointer to the ab8500_fg structure
1424 * Battery capacity calculation state machine for when we're charging
1426 static void ab8500_fg_algorithm_charging(struct ab8500_fg *di)
1429 * If we change to discharge mode
1430 * we should start with recovery
1432 if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY)
1433 ab8500_fg_discharge_state_to(di,
1434 AB8500_FG_DISCHARGE_INIT_RECOVERY);
1436 switch (di->charge_state) {
1437 case AB8500_FG_CHARGE_INIT:
1438 di->fg_samples = SEC_TO_SAMPLE(
1439 di->bm->fg_params->accu_charging);
1441 ab8500_fg_coulomb_counter(di, true);
1442 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT);
1446 case AB8500_FG_CHARGE_READOUT:
1448 * Read the FG and calculate the new capacity
1450 mutex_lock(&di->cc_lock);
1451 if (!di->flags.conv_done && !di->flags.force_full) {
1452 /* Wasn't the CC IRQ that got us here */
1453 mutex_unlock(&di->cc_lock);
1454 dev_dbg(di->dev, "%s CC conv not done\n",
1459 di->flags.conv_done = false;
1460 mutex_unlock(&di->cc_lock);
1462 ab8500_fg_calc_cap_charging(di);
1470 /* Check capacity limits */
1471 ab8500_fg_check_capacity_limits(di, false);
1474 static void force_capacity(struct ab8500_fg *di)
1478 ab8500_fg_clear_cap_samples(di);
1479 cap = di->bat_cap.user_mah;
1480 if (cap > di->bat_cap.max_mah_design) {
1481 dev_dbg(di->dev, "Remaining cap %d can't be bigger than total"
1482 " %d\n", cap, di->bat_cap.max_mah_design);
1483 cap = di->bat_cap.max_mah_design;
1485 ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah);
1486 di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap);
1487 di->bat_cap.mah = cap;
1488 ab8500_fg_check_capacity_limits(di, true);
1491 static bool check_sysfs_capacity(struct ab8500_fg *di)
1493 int cap, lower, upper;
1496 cap = di->bat_cap.user_mah;
1498 cap_permille = ab8500_fg_convert_mah_to_permille(di,
1499 di->bat_cap.user_mah);
1501 lower = di->bat_cap.permille - di->bm->fg_params->user_cap_limit * 10;
1502 upper = di->bat_cap.permille + di->bm->fg_params->user_cap_limit * 10;
1506 /* 1000 is permille, -> 100 percent */
1510 dev_dbg(di->dev, "Capacity limits:"
1511 " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
1512 lower, cap_permille, upper, cap, di->bat_cap.mah);
1514 /* If within limits, use the saved capacity and exit estimation...*/
1515 if (cap_permille > lower && cap_permille < upper) {
1516 dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap);
1520 dev_dbg(di->dev, "Capacity from user out of limits, ignoring");
1525 * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
1526 * @di: pointer to the ab8500_fg structure
1528 * Battery capacity calculation state machine for when we're discharging
1530 static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di)
1534 /* If we change to charge mode we should start with init */
1535 if (di->charge_state != AB8500_FG_CHARGE_INIT)
1536 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
1538 switch (di->discharge_state) {
1539 case AB8500_FG_DISCHARGE_INIT:
1540 /* We use the FG IRQ to work on */
1542 di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
1543 ab8500_fg_coulomb_counter(di, true);
1544 ab8500_fg_discharge_state_to(di,
1545 AB8500_FG_DISCHARGE_INITMEASURING);
1547 /* Intentional fallthrough */
1548 case AB8500_FG_DISCHARGE_INITMEASURING:
1550 * Discard a number of samples during startup.
1551 * After that, use compensated voltage for a few
1552 * samples to get an initial capacity.
1553 * Then go to READOUT
1555 sleep_time = di->bm->fg_params->init_timer;
1557 /* Discard the first [x] seconds */
1558 if (di->init_cnt > di->bm->fg_params->init_discard_time) {
1559 ab8500_fg_calc_cap_discharge_voltage(di, true);
1561 ab8500_fg_check_capacity_limits(di, true);
1564 di->init_cnt += sleep_time;
1565 if (di->init_cnt > di->bm->fg_params->init_total_time)
1566 ab8500_fg_discharge_state_to(di,
1567 AB8500_FG_DISCHARGE_READOUT_INIT);
1571 case AB8500_FG_DISCHARGE_INIT_RECOVERY:
1572 di->recovery_cnt = 0;
1573 di->recovery_needed = true;
1574 ab8500_fg_discharge_state_to(di,
1575 AB8500_FG_DISCHARGE_RECOVERY);
1577 /* Intentional fallthrough */
1579 case AB8500_FG_DISCHARGE_RECOVERY:
1580 sleep_time = di->bm->fg_params->recovery_sleep_timer;
1583 * We should check the power consumption
1584 * If low, go to READOUT (after x min) or
1585 * RECOVERY_SLEEP if time left.
1586 * If high, go to READOUT
1588 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1590 if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1591 if (di->recovery_cnt >
1592 di->bm->fg_params->recovery_total_time) {
1593 di->fg_samples = SEC_TO_SAMPLE(
1594 di->bm->fg_params->accu_high_curr);
1595 ab8500_fg_coulomb_counter(di, true);
1596 ab8500_fg_discharge_state_to(di,
1597 AB8500_FG_DISCHARGE_READOUT);
1598 di->recovery_needed = false;
1600 queue_delayed_work(di->fg_wq,
1601 &di->fg_periodic_work,
1604 di->recovery_cnt += sleep_time;
1606 di->fg_samples = SEC_TO_SAMPLE(
1607 di->bm->fg_params->accu_high_curr);
1608 ab8500_fg_coulomb_counter(di, true);
1609 ab8500_fg_discharge_state_to(di,
1610 AB8500_FG_DISCHARGE_READOUT);
1614 case AB8500_FG_DISCHARGE_READOUT_INIT:
1615 di->fg_samples = SEC_TO_SAMPLE(
1616 di->bm->fg_params->accu_high_curr);
1617 ab8500_fg_coulomb_counter(di, true);
1618 ab8500_fg_discharge_state_to(di,
1619 AB8500_FG_DISCHARGE_READOUT);
1622 case AB8500_FG_DISCHARGE_READOUT:
1623 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1625 if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1626 /* Detect mode change */
1627 if (di->high_curr_mode) {
1628 di->high_curr_mode = false;
1629 di->high_curr_cnt = 0;
1632 if (di->recovery_needed) {
1633 ab8500_fg_discharge_state_to(di,
1634 AB8500_FG_DISCHARGE_INIT_RECOVERY);
1636 queue_delayed_work(di->fg_wq,
1637 &di->fg_periodic_work, 0);
1642 ab8500_fg_calc_cap_discharge_voltage(di, true);
1644 mutex_lock(&di->cc_lock);
1645 if (!di->flags.conv_done) {
1646 /* Wasn't the CC IRQ that got us here */
1647 mutex_unlock(&di->cc_lock);
1648 dev_dbg(di->dev, "%s CC conv not done\n",
1653 di->flags.conv_done = false;
1654 mutex_unlock(&di->cc_lock);
1656 /* Detect mode change */
1657 if (!di->high_curr_mode) {
1658 di->high_curr_mode = true;
1659 di->high_curr_cnt = 0;
1662 di->high_curr_cnt +=
1663 di->bm->fg_params->accu_high_curr;
1664 if (di->high_curr_cnt >
1665 di->bm->fg_params->high_curr_time)
1666 di->recovery_needed = true;
1668 ab8500_fg_calc_cap_discharge_fg(di);
1671 ab8500_fg_check_capacity_limits(di, false);
1675 case AB8500_FG_DISCHARGE_WAKEUP:
1676 ab8500_fg_calc_cap_discharge_voltage(di, true);
1678 di->fg_samples = SEC_TO_SAMPLE(
1679 di->bm->fg_params->accu_high_curr);
1680 ab8500_fg_coulomb_counter(di, true);
1681 ab8500_fg_discharge_state_to(di,
1682 AB8500_FG_DISCHARGE_READOUT);
1684 ab8500_fg_check_capacity_limits(di, false);
1694 * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
1695 * @di: pointer to the ab8500_fg structure
1698 static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di)
1702 switch (di->calib_state) {
1703 case AB8500_FG_CALIB_INIT:
1704 dev_dbg(di->dev, "Calibration ongoing...\n");
1706 ret = abx500_mask_and_set_register_interruptible(di->dev,
1707 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1708 CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8);
1712 ret = abx500_mask_and_set_register_interruptible(di->dev,
1713 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1714 CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA);
1717 di->calib_state = AB8500_FG_CALIB_WAIT;
1719 case AB8500_FG_CALIB_END:
1720 ret = abx500_mask_and_set_register_interruptible(di->dev,
1721 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1722 CC_MUXOFFSET, CC_MUXOFFSET);
1725 di->flags.calibrate = false;
1726 dev_dbg(di->dev, "Calibration done...\n");
1727 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1729 case AB8500_FG_CALIB_WAIT:
1730 dev_dbg(di->dev, "Calibration WFI\n");
1736 /* Something went wrong, don't calibrate then */
1737 dev_err(di->dev, "failed to calibrate the CC\n");
1738 di->flags.calibrate = false;
1739 di->calib_state = AB8500_FG_CALIB_INIT;
1740 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1744 * ab8500_fg_algorithm() - Entry point for the FG algorithm
1745 * @di: pointer to the ab8500_fg structure
1747 * Entry point for the battery capacity calculation state machine
1749 static void ab8500_fg_algorithm(struct ab8500_fg *di)
1751 if (di->flags.calibrate)
1752 ab8500_fg_algorithm_calibrate(di);
1754 if (di->flags.charging)
1755 ab8500_fg_algorithm_charging(di);
1757 ab8500_fg_algorithm_discharging(di);
1760 dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d %d "
1761 "%d %d %d %d %d %d %d\n",
1762 di->bat_cap.max_mah_design,
1763 di->bat_cap.max_mah,
1765 di->bat_cap.permille,
1767 di->bat_cap.prev_mah,
1768 di->bat_cap.prev_percent,
1769 di->bat_cap.prev_level,
1776 di->discharge_state,
1778 di->recovery_needed);
1782 * ab8500_fg_periodic_work() - Run the FG state machine periodically
1783 * @work: pointer to the work_struct structure
1785 * Work queue function for periodic work
1787 static void ab8500_fg_periodic_work(struct work_struct *work)
1789 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1790 fg_periodic_work.work);
1792 if (di->init_capacity) {
1793 /* Get an initial capacity calculation */
1794 ab8500_fg_calc_cap_discharge_voltage(di, true);
1795 ab8500_fg_check_capacity_limits(di, true);
1796 di->init_capacity = false;
1798 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1799 } else if (di->flags.user_cap) {
1800 if (check_sysfs_capacity(di)) {
1801 ab8500_fg_check_capacity_limits(di, true);
1802 if (di->flags.charging)
1803 ab8500_fg_charge_state_to(di,
1804 AB8500_FG_CHARGE_INIT);
1806 ab8500_fg_discharge_state_to(di,
1807 AB8500_FG_DISCHARGE_READOUT_INIT);
1809 di->flags.user_cap = false;
1810 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1812 ab8500_fg_algorithm(di);
1817 * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
1818 * @work: pointer to the work_struct structure
1820 * Work queue function for checking the OVV_BAT condition
1822 static void ab8500_fg_check_hw_failure_work(struct work_struct *work)
1827 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1828 fg_check_hw_failure_work.work);
1831 * If we have had a battery over-voltage situation,
1832 * check ovv-bit to see if it should be reset.
1834 ret = abx500_get_register_interruptible(di->dev,
1835 AB8500_CHARGER, AB8500_CH_STAT_REG,
1838 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1841 if ((reg_value & BATT_OVV) == BATT_OVV) {
1842 if (!di->flags.bat_ovv) {
1843 dev_dbg(di->dev, "Battery OVV\n");
1844 di->flags.bat_ovv = true;
1845 power_supply_changed(di->fg_psy);
1847 /* Not yet recovered from ovv, reschedule this test */
1848 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work,
1851 dev_dbg(di->dev, "Battery recovered from OVV\n");
1852 di->flags.bat_ovv = false;
1853 power_supply_changed(di->fg_psy);
1858 * ab8500_fg_low_bat_work() - Check LOW_BAT condition
1859 * @work: pointer to the work_struct structure
1861 * Work queue function for checking the LOW_BAT condition
1863 static void ab8500_fg_low_bat_work(struct work_struct *work)
1867 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1868 fg_low_bat_work.work);
1870 vbat = ab8500_fg_bat_voltage(di);
1872 /* Check if LOW_BAT still fulfilled */
1873 if (vbat < di->bm->fg_params->lowbat_threshold) {
1874 /* Is it time to shut down? */
1875 if (di->low_bat_cnt < 1) {
1876 di->flags.low_bat = true;
1877 dev_warn(di->dev, "Shut down pending...\n");
1880 * Else we need to re-schedule this check to be able to detect
1881 * if the voltage increases again during charging or
1882 * due to decreasing load.
1885 dev_warn(di->dev, "Battery voltage still LOW\n");
1886 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
1887 round_jiffies(LOW_BAT_CHECK_INTERVAL));
1890 di->flags.low_bat_delay = false;
1891 di->low_bat_cnt = 10;
1892 dev_warn(di->dev, "Battery voltage OK again\n");
1895 /* This is needed to dispatch LOW_BAT */
1896 ab8500_fg_check_capacity_limits(di, false);
1900 * ab8500_fg_battok_calc - calculate the bit pattern corresponding
1901 * to the target voltage.
1902 * @di: pointer to the ab8500_fg structure
1903 * @target: target voltage
1905 * Returns bit pattern closest to the target voltage
1906 * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
1909 static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target)
1911 if (target > BATT_OK_MIN +
1912 (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS))
1913 return BATT_OK_MAX_NR_INCREMENTS;
1914 if (target < BATT_OK_MIN)
1916 return (target - BATT_OK_MIN) / BATT_OK_INCREMENT;
1920 * ab8500_fg_battok_init_hw_register - init battok levels
1921 * @di: pointer to the ab8500_fg structure
1925 static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di)
1935 sel0 = di->bm->fg_params->battok_falling_th_sel0;
1936 sel1 = di->bm->fg_params->battok_raising_th_sel1;
1938 cbp_sel0 = ab8500_fg_battok_calc(di, sel0);
1939 cbp_sel1 = ab8500_fg_battok_calc(di, sel1);
1941 selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT;
1943 if (selected != sel0)
1944 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1945 sel0, selected, cbp_sel0);
1947 selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT;
1949 if (selected != sel1)
1950 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1951 sel1, selected, cbp_sel1);
1953 new_val = cbp_sel0 | (cbp_sel1 << 4);
1955 dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1);
1956 ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK,
1957 AB8500_BATT_OK_REG, new_val);
1962 * ab8500_fg_instant_work() - Run the FG state machine instantly
1963 * @work: pointer to the work_struct structure
1965 * Work queue function for instant work
1967 static void ab8500_fg_instant_work(struct work_struct *work)
1969 struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work);
1971 ab8500_fg_algorithm(di);
1975 * ab8500_fg_cc_data_end_handler() - end of data conversion isr.
1976 * @irq: interrupt number
1977 * @_di: pointer to the ab8500_fg structure
1979 * Returns IRQ status(IRQ_HANDLED)
1981 static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di)
1983 struct ab8500_fg *di = _di;
1984 if (!di->nbr_cceoc_irq_cnt) {
1985 di->nbr_cceoc_irq_cnt++;
1986 complete(&di->ab8500_fg_started);
1988 di->nbr_cceoc_irq_cnt = 0;
1989 complete(&di->ab8500_fg_complete);
1995 * ab8500_fg_cc_int_calib_handler () - end of calibration isr.
1996 * @irq: interrupt number
1997 * @_di: pointer to the ab8500_fg structure
1999 * Returns IRQ status(IRQ_HANDLED)
2001 static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di)
2003 struct ab8500_fg *di = _di;
2004 di->calib_state = AB8500_FG_CALIB_END;
2005 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2010 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
2011 * @irq: interrupt number
2012 * @_di: pointer to the ab8500_fg structure
2014 * Returns IRQ status(IRQ_HANDLED)
2016 static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di)
2018 struct ab8500_fg *di = _di;
2020 queue_work(di->fg_wq, &di->fg_acc_cur_work);
2026 * ab8500_fg_batt_ovv_handler() - Battery OVV occured
2027 * @irq: interrupt number
2028 * @_di: pointer to the ab8500_fg structure
2030 * Returns IRQ status(IRQ_HANDLED)
2032 static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di)
2034 struct ab8500_fg *di = _di;
2036 dev_dbg(di->dev, "Battery OVV\n");
2038 /* Schedule a new HW failure check */
2039 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0);
2045 * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
2046 * @irq: interrupt number
2047 * @_di: pointer to the ab8500_fg structure
2049 * Returns IRQ status(IRQ_HANDLED)
2051 static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di)
2053 struct ab8500_fg *di = _di;
2055 /* Initiate handling in ab8500_fg_low_bat_work() if not already initiated. */
2056 if (!di->flags.low_bat_delay) {
2057 dev_warn(di->dev, "Battery voltage is below LOW threshold\n");
2058 di->flags.low_bat_delay = true;
2060 * Start a timer to check LOW_BAT again after some time
2061 * This is done to avoid shutdown on single voltage dips
2063 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
2064 round_jiffies(LOW_BAT_CHECK_INTERVAL));
2070 * ab8500_fg_get_property() - get the fg properties
2071 * @psy: pointer to the power_supply structure
2072 * @psp: pointer to the power_supply_property structure
2073 * @val: pointer to the power_supply_propval union
2075 * This function gets called when an application tries to get the
2076 * fg properties by reading the sysfs files.
2077 * voltage_now: battery voltage
2078 * current_now: battery instant current
2079 * current_avg: battery average current
2080 * charge_full_design: capacity where battery is considered full
2081 * charge_now: battery capacity in nAh
2082 * capacity: capacity in percent
2083 * capacity_level: capacity level
2085 * Returns error code in case of failure else 0 on success
2087 static int ab8500_fg_get_property(struct power_supply *psy,
2088 enum power_supply_property psp,
2089 union power_supply_propval *val)
2091 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2094 * If battery is identified as unknown and charging of unknown
2095 * batteries is disabled, we always report 100% capacity and
2096 * capacity level UNKNOWN, since we can't calculate
2097 * remaining capacity
2101 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2102 if (di->flags.bat_ovv)
2103 val->intval = BATT_OVV_VALUE * 1000;
2105 val->intval = di->vbat * 1000;
2107 case POWER_SUPPLY_PROP_CURRENT_NOW:
2108 val->intval = di->inst_curr * 1000;
2110 case POWER_SUPPLY_PROP_CURRENT_AVG:
2111 val->intval = di->avg_curr * 1000;
2113 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
2114 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2115 di->bat_cap.max_mah_design);
2117 case POWER_SUPPLY_PROP_ENERGY_FULL:
2118 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2119 di->bat_cap.max_mah);
2121 case POWER_SUPPLY_PROP_ENERGY_NOW:
2122 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2123 di->flags.batt_id_received)
2124 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2125 di->bat_cap.max_mah);
2127 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2128 di->bat_cap.prev_mah);
2130 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
2131 val->intval = di->bat_cap.max_mah_design;
2133 case POWER_SUPPLY_PROP_CHARGE_FULL:
2134 val->intval = di->bat_cap.max_mah;
2136 case POWER_SUPPLY_PROP_CHARGE_NOW:
2137 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2138 di->flags.batt_id_received)
2139 val->intval = di->bat_cap.max_mah;
2141 val->intval = di->bat_cap.prev_mah;
2143 case POWER_SUPPLY_PROP_CAPACITY:
2144 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2145 di->flags.batt_id_received)
2148 val->intval = di->bat_cap.prev_percent;
2150 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
2151 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2152 di->flags.batt_id_received)
2153 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
2155 val->intval = di->bat_cap.prev_level;
2163 static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
2165 struct power_supply *psy;
2166 struct power_supply *ext = dev_get_drvdata(dev);
2167 const char **supplicants = (const char **)ext->supplied_to;
2168 struct ab8500_fg *di;
2169 union power_supply_propval ret;
2172 psy = (struct power_supply *)data;
2173 di = power_supply_get_drvdata(psy);
2176 * For all psy where the name of your driver
2177 * appears in any supplied_to
2179 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
2183 /* Go through all properties for the psy */
2184 for (j = 0; j < ext->desc->num_properties; j++) {
2185 enum power_supply_property prop;
2186 prop = ext->desc->properties[j];
2188 if (power_supply_get_property(ext, prop, &ret))
2192 case POWER_SUPPLY_PROP_STATUS:
2193 switch (ext->desc->type) {
2194 case POWER_SUPPLY_TYPE_BATTERY:
2195 switch (ret.intval) {
2196 case POWER_SUPPLY_STATUS_UNKNOWN:
2197 case POWER_SUPPLY_STATUS_DISCHARGING:
2198 case POWER_SUPPLY_STATUS_NOT_CHARGING:
2199 if (!di->flags.charging)
2201 di->flags.charging = false;
2202 di->flags.fully_charged = false;
2203 if (di->bm->capacity_scaling)
2204 ab8500_fg_update_cap_scalers(di);
2205 queue_work(di->fg_wq, &di->fg_work);
2207 case POWER_SUPPLY_STATUS_FULL:
2208 if (di->flags.fully_charged)
2210 di->flags.fully_charged = true;
2211 di->flags.force_full = true;
2212 /* Save current capacity as maximum */
2213 di->bat_cap.max_mah = di->bat_cap.mah;
2214 queue_work(di->fg_wq, &di->fg_work);
2216 case POWER_SUPPLY_STATUS_CHARGING:
2217 if (di->flags.charging &&
2218 !di->flags.fully_charged)
2220 di->flags.charging = true;
2221 di->flags.fully_charged = false;
2222 if (di->bm->capacity_scaling)
2223 ab8500_fg_update_cap_scalers(di);
2224 queue_work(di->fg_wq, &di->fg_work);
2231 case POWER_SUPPLY_PROP_TECHNOLOGY:
2232 switch (ext->desc->type) {
2233 case POWER_SUPPLY_TYPE_BATTERY:
2234 if (!di->flags.batt_id_received &&
2235 di->bm->batt_id != BATTERY_UNKNOWN) {
2236 const struct abx500_battery_type *b;
2238 b = &(di->bm->bat_type[di->bm->batt_id]);
2240 di->flags.batt_id_received = true;
2242 di->bat_cap.max_mah_design =
2244 b->charge_full_design;
2246 di->bat_cap.max_mah =
2247 di->bat_cap.max_mah_design;
2249 di->vbat_nom = b->nominal_voltage;
2253 di->flags.batt_unknown = false;
2255 di->flags.batt_unknown = true;
2261 case POWER_SUPPLY_PROP_TEMP:
2262 switch (ext->desc->type) {
2263 case POWER_SUPPLY_TYPE_BATTERY:
2264 if (di->flags.batt_id_received)
2265 di->bat_temp = ret.intval;
2279 * ab8500_fg_init_hw_registers() - Set up FG related registers
2280 * @di: pointer to the ab8500_fg structure
2282 * Set up battery OVV, low battery voltage registers
2284 static int ab8500_fg_init_hw_registers(struct ab8500_fg *di)
2288 /* Set VBAT OVV threshold */
2289 ret = abx500_mask_and_set_register_interruptible(di->dev,
2295 dev_err(di->dev, "failed to set BATT_OVV\n");
2299 /* Enable VBAT OVV detection */
2300 ret = abx500_mask_and_set_register_interruptible(di->dev,
2306 dev_err(di->dev, "failed to enable BATT_OVV\n");
2310 /* Low Battery Voltage */
2311 ret = abx500_set_register_interruptible(di->dev,
2312 AB8500_SYS_CTRL2_BLOCK,
2314 ab8500_volt_to_regval(
2315 di->bm->fg_params->lowbat_threshold) << 1 |
2318 dev_err(di->dev, "%s write failed\n", __func__);
2322 /* Battery OK threshold */
2323 ret = ab8500_fg_battok_init_hw_register(di);
2325 dev_err(di->dev, "BattOk init write failed.\n");
2329 if (((is_ab8505(di->parent) || is_ab9540(di->parent)) &&
2330 abx500_get_chip_id(di->dev) >= AB8500_CUT2P0)
2331 || is_ab8540(di->parent)) {
2332 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2333 AB8505_RTC_PCUT_MAX_TIME_REG, di->bm->fg_params->pcut_max_time);
2336 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_MAX_TIME_REG\n", __func__);
2340 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2341 AB8505_RTC_PCUT_FLAG_TIME_REG, di->bm->fg_params->pcut_flag_time);
2344 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_FLAG_TIME_REG\n", __func__);
2348 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2349 AB8505_RTC_PCUT_RESTART_REG, di->bm->fg_params->pcut_max_restart);
2352 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_RESTART_REG\n", __func__);
2356 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2357 AB8505_RTC_PCUT_DEBOUNCE_REG, di->bm->fg_params->pcut_debounce_time);
2360 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_DEBOUNCE_REG\n", __func__);
2364 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2365 AB8505_RTC_PCUT_CTL_STATUS_REG, di->bm->fg_params->pcut_enable);
2368 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_CTL_STATUS_REG\n", __func__);
2377 * ab8500_fg_external_power_changed() - callback for power supply changes
2378 * @psy: pointer to the structure power_supply
2380 * This function is the entry point of the pointer external_power_changed
2381 * of the structure power_supply.
2382 * This function gets executed when there is a change in any external power
2383 * supply that this driver needs to be notified of.
2385 static void ab8500_fg_external_power_changed(struct power_supply *psy)
2387 class_for_each_device(power_supply_class, NULL, psy,
2388 ab8500_fg_get_ext_psy_data);
2392 * ab8500_fg_reinit_work() - work to reset the FG algorithm
2393 * @work: pointer to the work_struct structure
2395 * Used to reset the current battery capacity to be able to
2396 * retrigger a new voltage base capacity calculation. For
2397 * test and verification purpose.
2399 static void ab8500_fg_reinit_work(struct work_struct *work)
2401 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
2402 fg_reinit_work.work);
2404 if (di->flags.calibrate == false) {
2405 dev_dbg(di->dev, "Resetting FG state machine to init.\n");
2406 ab8500_fg_clear_cap_samples(di);
2407 ab8500_fg_calc_cap_discharge_voltage(di, true);
2408 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
2409 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
2410 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2413 dev_err(di->dev, "Residual offset calibration ongoing "
2415 /* Wait one second until next try*/
2416 queue_delayed_work(di->fg_wq, &di->fg_reinit_work,
2421 /* Exposure to the sysfs interface */
2423 struct ab8500_fg_sysfs_entry {
2424 struct attribute attr;
2425 ssize_t (*show)(struct ab8500_fg *, char *);
2426 ssize_t (*store)(struct ab8500_fg *, const char *, size_t);
2429 static ssize_t charge_full_show(struct ab8500_fg *di, char *buf)
2431 return sprintf(buf, "%d\n", di->bat_cap.max_mah);
2434 static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf,
2437 unsigned long charge_full;
2440 ret = kstrtoul(buf, 10, &charge_full);
2444 di->bat_cap.max_mah = (int) charge_full;
2448 static ssize_t charge_now_show(struct ab8500_fg *di, char *buf)
2450 return sprintf(buf, "%d\n", di->bat_cap.prev_mah);
2453 static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf,
2456 unsigned long charge_now;
2459 ret = kstrtoul(buf, 10, &charge_now);
2463 di->bat_cap.user_mah = (int) charge_now;
2464 di->flags.user_cap = true;
2465 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2469 static struct ab8500_fg_sysfs_entry charge_full_attr =
2470 __ATTR(charge_full, 0644, charge_full_show, charge_full_store);
2472 static struct ab8500_fg_sysfs_entry charge_now_attr =
2473 __ATTR(charge_now, 0644, charge_now_show, charge_now_store);
2476 ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf)
2478 struct ab8500_fg_sysfs_entry *entry;
2479 struct ab8500_fg *di;
2481 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2482 di = container_of(kobj, struct ab8500_fg, fg_kobject);
2487 return entry->show(di, buf);
2490 ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf,
2493 struct ab8500_fg_sysfs_entry *entry;
2494 struct ab8500_fg *di;
2496 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2497 di = container_of(kobj, struct ab8500_fg, fg_kobject);
2502 return entry->store(di, buf, count);
2505 static const struct sysfs_ops ab8500_fg_sysfs_ops = {
2506 .show = ab8500_fg_show,
2507 .store = ab8500_fg_store,
2510 static struct attribute *ab8500_fg_attrs[] = {
2511 &charge_full_attr.attr,
2512 &charge_now_attr.attr,
2516 static struct kobj_type ab8500_fg_ktype = {
2517 .sysfs_ops = &ab8500_fg_sysfs_ops,
2518 .default_attrs = ab8500_fg_attrs,
2522 * ab8500_fg_sysfs_exit() - de-init of sysfs entry
2523 * @di: pointer to the struct ab8500_chargalg
2525 * This function removes the entry in sysfs.
2527 static void ab8500_fg_sysfs_exit(struct ab8500_fg *di)
2529 kobject_del(&di->fg_kobject);
2533 * ab8500_fg_sysfs_init() - init of sysfs entry
2534 * @di: pointer to the struct ab8500_chargalg
2536 * This function adds an entry in sysfs.
2537 * Returns error code in case of failure else 0(on success)
2539 static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
2543 ret = kobject_init_and_add(&di->fg_kobject,
2547 kobject_put(&di->fg_kobject);
2548 dev_err(di->dev, "failed to create sysfs entry\n");
2554 static ssize_t ab8505_powercut_flagtime_read(struct device *dev,
2555 struct device_attribute *attr,
2560 struct power_supply *psy = dev_get_drvdata(dev);
2561 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2563 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2564 AB8505_RTC_PCUT_FLAG_TIME_REG, ®_value);
2567 dev_err(dev, "Failed to read AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2571 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
2577 static ssize_t ab8505_powercut_flagtime_write(struct device *dev,
2578 struct device_attribute *attr,
2579 const char *buf, size_t count)
2582 long unsigned reg_value;
2583 struct power_supply *psy = dev_get_drvdata(dev);
2584 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2586 reg_value = simple_strtoul(buf, NULL, 10);
2588 if (reg_value > 0x7F) {
2589 dev_err(dev, "Incorrect parameter, echo 0 (1.98s) - 127 (15.625ms) for flagtime\n");
2593 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2594 AB8505_RTC_PCUT_FLAG_TIME_REG, (u8)reg_value);
2597 dev_err(dev, "Failed to set AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2603 static ssize_t ab8505_powercut_maxtime_read(struct device *dev,
2604 struct device_attribute *attr,
2609 struct power_supply *psy = dev_get_drvdata(dev);
2610 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2612 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2613 AB8505_RTC_PCUT_MAX_TIME_REG, ®_value);
2616 dev_err(dev, "Failed to read AB8505_RTC_PCUT_MAX_TIME_REG\n");
2620 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
2627 static ssize_t ab8505_powercut_maxtime_write(struct device *dev,
2628 struct device_attribute *attr,
2629 const char *buf, size_t count)
2633 struct power_supply *psy = dev_get_drvdata(dev);
2634 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2636 reg_value = simple_strtoul(buf, NULL, 10);
2637 if (reg_value > 0x7F) {
2638 dev_err(dev, "Incorrect parameter, echo 0 (0.0s) - 127 (1.98s) for maxtime\n");
2642 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2643 AB8505_RTC_PCUT_MAX_TIME_REG, (u8)reg_value);
2646 dev_err(dev, "Failed to set AB8505_RTC_PCUT_MAX_TIME_REG\n");
2652 static ssize_t ab8505_powercut_restart_read(struct device *dev,
2653 struct device_attribute *attr,
2658 struct power_supply *psy = dev_get_drvdata(dev);
2659 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2661 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2662 AB8505_RTC_PCUT_RESTART_REG, ®_value);
2665 dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2669 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0xF));
2675 static ssize_t ab8505_powercut_restart_write(struct device *dev,
2676 struct device_attribute *attr,
2677 const char *buf, size_t count)
2681 struct power_supply *psy = dev_get_drvdata(dev);
2682 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2684 reg_value = simple_strtoul(buf, NULL, 10);
2685 if (reg_value > 0xF) {
2686 dev_err(dev, "Incorrect parameter, echo 0 - 15 for number of restart\n");
2690 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2691 AB8505_RTC_PCUT_RESTART_REG, (u8)reg_value);
2694 dev_err(dev, "Failed to set AB8505_RTC_PCUT_RESTART_REG\n");
2701 static ssize_t ab8505_powercut_timer_read(struct device *dev,
2702 struct device_attribute *attr,
2707 struct power_supply *psy = dev_get_drvdata(dev);
2708 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2710 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2711 AB8505_RTC_PCUT_TIME_REG, ®_value);
2714 dev_err(dev, "Failed to read AB8505_RTC_PCUT_TIME_REG\n");
2718 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
2724 static ssize_t ab8505_powercut_restart_counter_read(struct device *dev,
2725 struct device_attribute *attr,
2730 struct power_supply *psy = dev_get_drvdata(dev);
2731 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2733 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2734 AB8505_RTC_PCUT_RESTART_REG, ®_value);
2737 dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2741 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0xF0) >> 4);
2747 static ssize_t ab8505_powercut_read(struct device *dev,
2748 struct device_attribute *attr,
2753 struct power_supply *psy = dev_get_drvdata(dev);
2754 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2756 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2757 AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value);
2762 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x1));
2768 static ssize_t ab8505_powercut_write(struct device *dev,
2769 struct device_attribute *attr,
2770 const char *buf, size_t count)
2774 struct power_supply *psy = dev_get_drvdata(dev);
2775 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2777 reg_value = simple_strtoul(buf, NULL, 10);
2778 if (reg_value > 0x1) {
2779 dev_err(dev, "Incorrect parameter, echo 0/1 to disable/enable Pcut feature\n");
2783 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2784 AB8505_RTC_PCUT_CTL_STATUS_REG, (u8)reg_value);
2787 dev_err(dev, "Failed to set AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2793 static ssize_t ab8505_powercut_flag_read(struct device *dev,
2794 struct device_attribute *attr,
2800 struct power_supply *psy = dev_get_drvdata(dev);
2801 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2803 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2804 AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value);
2807 dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2811 return scnprintf(buf, PAGE_SIZE, "%d\n", ((reg_value & 0x10) >> 4));
2817 static ssize_t ab8505_powercut_debounce_read(struct device *dev,
2818 struct device_attribute *attr,
2823 struct power_supply *psy = dev_get_drvdata(dev);
2824 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2826 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2827 AB8505_RTC_PCUT_DEBOUNCE_REG, ®_value);
2830 dev_err(dev, "Failed to read AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2834 return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7));
2840 static ssize_t ab8505_powercut_debounce_write(struct device *dev,
2841 struct device_attribute *attr,
2842 const char *buf, size_t count)
2846 struct power_supply *psy = dev_get_drvdata(dev);
2847 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2849 reg_value = simple_strtoul(buf, NULL, 10);
2850 if (reg_value > 0x7) {
2851 dev_err(dev, "Incorrect parameter, echo 0 to 7 for debounce setting\n");
2855 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2856 AB8505_RTC_PCUT_DEBOUNCE_REG, (u8)reg_value);
2859 dev_err(dev, "Failed to set AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2865 static ssize_t ab8505_powercut_enable_status_read(struct device *dev,
2866 struct device_attribute *attr,
2871 struct power_supply *psy = dev_get_drvdata(dev);
2872 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2874 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2875 AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value);
2878 dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2882 return scnprintf(buf, PAGE_SIZE, "%d\n", ((reg_value & 0x20) >> 5));
2888 static struct device_attribute ab8505_fg_sysfs_psy_attrs[] = {
2889 __ATTR(powercut_flagtime, (S_IRUGO | S_IWUSR | S_IWGRP),
2890 ab8505_powercut_flagtime_read, ab8505_powercut_flagtime_write),
2891 __ATTR(powercut_maxtime, (S_IRUGO | S_IWUSR | S_IWGRP),
2892 ab8505_powercut_maxtime_read, ab8505_powercut_maxtime_write),
2893 __ATTR(powercut_restart_max, (S_IRUGO | S_IWUSR | S_IWGRP),
2894 ab8505_powercut_restart_read, ab8505_powercut_restart_write),
2895 __ATTR(powercut_timer, S_IRUGO, ab8505_powercut_timer_read, NULL),
2896 __ATTR(powercut_restart_counter, S_IRUGO,
2897 ab8505_powercut_restart_counter_read, NULL),
2898 __ATTR(powercut_enable, (S_IRUGO | S_IWUSR | S_IWGRP),
2899 ab8505_powercut_read, ab8505_powercut_write),
2900 __ATTR(powercut_flag, S_IRUGO, ab8505_powercut_flag_read, NULL),
2901 __ATTR(powercut_debounce_time, (S_IRUGO | S_IWUSR | S_IWGRP),
2902 ab8505_powercut_debounce_read, ab8505_powercut_debounce_write),
2903 __ATTR(powercut_enable_status, S_IRUGO,
2904 ab8505_powercut_enable_status_read, NULL),
2907 static int ab8500_fg_sysfs_psy_create_attrs(struct ab8500_fg *di)
2911 if (((is_ab8505(di->parent) || is_ab9540(di->parent)) &&
2912 abx500_get_chip_id(di->dev) >= AB8500_CUT2P0)
2913 || is_ab8540(di->parent)) {
2914 for (i = 0; i < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); i++)
2915 if (device_create_file(&di->fg_psy->dev,
2916 &ab8505_fg_sysfs_psy_attrs[i]))
2917 goto sysfs_psy_create_attrs_failed_ab8505;
2920 sysfs_psy_create_attrs_failed_ab8505:
2921 dev_err(&di->fg_psy->dev, "Failed creating sysfs psy attrs for ab8505.\n");
2923 device_remove_file(&di->fg_psy->dev,
2924 &ab8505_fg_sysfs_psy_attrs[i]);
2929 static void ab8500_fg_sysfs_psy_remove_attrs(struct ab8500_fg *di)
2933 if (((is_ab8505(di->parent) || is_ab9540(di->parent)) &&
2934 abx500_get_chip_id(di->dev) >= AB8500_CUT2P0)
2935 || is_ab8540(di->parent)) {
2936 for (i = 0; i < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); i++)
2937 (void)device_remove_file(&di->fg_psy->dev,
2938 &ab8505_fg_sysfs_psy_attrs[i]);
2942 /* Exposure to the sysfs interface <<END>> */
2944 #if defined(CONFIG_PM)
2945 static int ab8500_fg_resume(struct platform_device *pdev)
2947 struct ab8500_fg *di = platform_get_drvdata(pdev);
2950 * Change state if we're not charging. If we're charging we will wake
2953 if (!di->flags.charging) {
2954 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP);
2955 queue_work(di->fg_wq, &di->fg_work);
2961 static int ab8500_fg_suspend(struct platform_device *pdev,
2964 struct ab8500_fg *di = platform_get_drvdata(pdev);
2966 flush_delayed_work(&di->fg_periodic_work);
2967 flush_work(&di->fg_work);
2968 flush_work(&di->fg_acc_cur_work);
2969 flush_delayed_work(&di->fg_reinit_work);
2970 flush_delayed_work(&di->fg_low_bat_work);
2971 flush_delayed_work(&di->fg_check_hw_failure_work);
2974 * If the FG is enabled we will disable it before going to suspend
2975 * only if we're not charging
2977 if (di->flags.fg_enabled && !di->flags.charging)
2978 ab8500_fg_coulomb_counter(di, false);
2983 #define ab8500_fg_suspend NULL
2984 #define ab8500_fg_resume NULL
2987 static int ab8500_fg_remove(struct platform_device *pdev)
2990 struct ab8500_fg *di = platform_get_drvdata(pdev);
2992 list_del(&di->node);
2994 /* Disable coulomb counter */
2995 ret = ab8500_fg_coulomb_counter(di, false);
2997 dev_err(di->dev, "failed to disable coulomb counter\n");
2999 destroy_workqueue(di->fg_wq);
3000 ab8500_fg_sysfs_exit(di);
3002 flush_scheduled_work();
3003 ab8500_fg_sysfs_psy_remove_attrs(di);
3004 power_supply_unregister(di->fg_psy);
3008 /* ab8500 fg driver interrupts and their respective isr */
3009 static struct ab8500_fg_interrupts ab8500_fg_irq_th[] = {
3010 {"NCONV_ACCU", ab8500_fg_cc_convend_handler},
3011 {"BATT_OVV", ab8500_fg_batt_ovv_handler},
3012 {"LOW_BAT_F", ab8500_fg_lowbatf_handler},
3013 {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler},
3016 static struct ab8500_fg_interrupts ab8500_fg_irq_bh[] = {
3017 {"CCEOC", ab8500_fg_cc_data_end_handler},
3020 static char *supply_interface[] = {
3025 static const struct power_supply_desc ab8500_fg_desc = {
3026 .name = "ab8500_fg",
3027 .type = POWER_SUPPLY_TYPE_BATTERY,
3028 .properties = ab8500_fg_props,
3029 .num_properties = ARRAY_SIZE(ab8500_fg_props),
3030 .get_property = ab8500_fg_get_property,
3031 .external_power_changed = ab8500_fg_external_power_changed,
3034 static int ab8500_fg_probe(struct platform_device *pdev)
3036 struct device_node *np = pdev->dev.of_node;
3037 struct abx500_bm_data *plat = pdev->dev.platform_data;
3038 struct power_supply_config psy_cfg = {};
3039 struct ab8500_fg *di;
3043 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3045 dev_err(&pdev->dev, "%s no mem for ab8500_fg\n", __func__);
3050 dev_err(&pdev->dev, "no battery management data supplied\n");
3056 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3058 dev_err(&pdev->dev, "failed to get battery information\n");
3063 mutex_init(&di->cc_lock);
3065 /* get parent data */
3066 di->dev = &pdev->dev;
3067 di->parent = dev_get_drvdata(pdev->dev.parent);
3068 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3070 psy_cfg.supplied_to = supply_interface;
3071 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3072 psy_cfg.drv_data = di;
3074 di->bat_cap.max_mah_design = MILLI_TO_MICRO *
3075 di->bm->bat_type[di->bm->batt_id].charge_full_design;
3077 di->bat_cap.max_mah = di->bat_cap.max_mah_design;
3079 di->vbat_nom = di->bm->bat_type[di->bm->batt_id].nominal_voltage;
3081 di->init_capacity = true;
3083 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
3084 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
3086 /* Create a work queue for running the FG algorithm */
3087 di->fg_wq = alloc_ordered_workqueue("ab8500_fg_wq", WQ_MEM_RECLAIM);
3088 if (di->fg_wq == NULL) {
3089 dev_err(di->dev, "failed to create work queue\n");
3093 /* Init work for running the fg algorithm instantly */
3094 INIT_WORK(&di->fg_work, ab8500_fg_instant_work);
3096 /* Init work for getting the battery accumulated current */
3097 INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work);
3099 /* Init work for reinitialising the fg algorithm */
3100 INIT_DEFERRABLE_WORK(&di->fg_reinit_work,
3101 ab8500_fg_reinit_work);
3103 /* Work delayed Queue to run the state machine */
3104 INIT_DEFERRABLE_WORK(&di->fg_periodic_work,
3105 ab8500_fg_periodic_work);
3107 /* Work to check low battery condition */
3108 INIT_DEFERRABLE_WORK(&di->fg_low_bat_work,
3109 ab8500_fg_low_bat_work);
3111 /* Init work for HW failure check */
3112 INIT_DEFERRABLE_WORK(&di->fg_check_hw_failure_work,
3113 ab8500_fg_check_hw_failure_work);
3115 /* Reset battery low voltage flag */
3116 di->flags.low_bat = false;
3118 /* Initialize low battery counter */
3119 di->low_bat_cnt = 10;
3121 /* Initialize OVV, and other registers */
3122 ret = ab8500_fg_init_hw_registers(di);
3124 dev_err(di->dev, "failed to initialize registers\n");
3125 goto free_inst_curr_wq;
3128 /* Consider battery unknown until we're informed otherwise */
3129 di->flags.batt_unknown = true;
3130 di->flags.batt_id_received = false;
3132 /* Register FG power supply class */
3133 di->fg_psy = power_supply_register(di->dev, &ab8500_fg_desc, &psy_cfg);
3134 if (IS_ERR(di->fg_psy)) {
3135 dev_err(di->dev, "failed to register FG psy\n");
3136 ret = PTR_ERR(di->fg_psy);
3137 goto free_inst_curr_wq;
3140 di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
3141 ab8500_fg_coulomb_counter(di, true);
3144 * Initialize completion used to notify completion and start
3147 init_completion(&di->ab8500_fg_started);
3148 init_completion(&di->ab8500_fg_complete);
3150 /* Register primary interrupt handlers */
3151 for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq_th); i++) {
3152 irq = platform_get_irq_byname(pdev, ab8500_fg_irq_th[i].name);
3153 ret = request_irq(irq, ab8500_fg_irq_th[i].isr,
3154 IRQF_SHARED | IRQF_NO_SUSPEND,
3155 ab8500_fg_irq_th[i].name, di);
3158 dev_err(di->dev, "failed to request %s IRQ %d: %d\n",
3159 ab8500_fg_irq_th[i].name, irq, ret);
3162 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3163 ab8500_fg_irq_th[i].name, irq, ret);
3166 /* Register threaded interrupt handler */
3167 irq = platform_get_irq_byname(pdev, ab8500_fg_irq_bh[0].name);
3168 ret = request_threaded_irq(irq, NULL, ab8500_fg_irq_bh[0].isr,
3169 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
3170 ab8500_fg_irq_bh[0].name, di);
3173 dev_err(di->dev, "failed to request %s IRQ %d: %d\n",
3174 ab8500_fg_irq_bh[0].name, irq, ret);
3177 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3178 ab8500_fg_irq_bh[0].name, irq, ret);
3180 di->irq = platform_get_irq_byname(pdev, "CCEOC");
3181 disable_irq(di->irq);
3182 di->nbr_cceoc_irq_cnt = 0;
3184 platform_set_drvdata(pdev, di);
3186 ret = ab8500_fg_sysfs_init(di);
3188 dev_err(di->dev, "failed to create sysfs entry\n");
3192 ret = ab8500_fg_sysfs_psy_create_attrs(di);
3194 dev_err(di->dev, "failed to create FG psy\n");
3195 ab8500_fg_sysfs_exit(di);
3199 /* Calibrate the fg first time */
3200 di->flags.calibrate = true;
3201 di->calib_state = AB8500_FG_CALIB_INIT;
3203 /* Use room temp as default value until we get an update from driver. */
3206 /* Run the FG algorithm */
3207 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
3209 list_add_tail(&di->node, &ab8500_fg_list);
3214 power_supply_unregister(di->fg_psy);
3216 /* We also have to free all registered irqs */
3217 for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq_th); i++) {
3218 irq = platform_get_irq_byname(pdev, ab8500_fg_irq_th[i].name);
3221 irq = platform_get_irq_byname(pdev, ab8500_fg_irq_bh[0].name);
3224 destroy_workqueue(di->fg_wq);
3228 static const struct of_device_id ab8500_fg_match[] = {
3229 { .compatible = "stericsson,ab8500-fg", },
3232 MODULE_DEVICE_TABLE(of, ab8500_fg_match);
3234 static struct platform_driver ab8500_fg_driver = {
3235 .probe = ab8500_fg_probe,
3236 .remove = ab8500_fg_remove,
3237 .suspend = ab8500_fg_suspend,
3238 .resume = ab8500_fg_resume,
3240 .name = "ab8500-fg",
3241 .of_match_table = ab8500_fg_match,
3245 static int __init ab8500_fg_init(void)
3247 return platform_driver_register(&ab8500_fg_driver);
3250 static void __exit ab8500_fg_exit(void)
3252 platform_driver_unregister(&ab8500_fg_driver);
3255 subsys_initcall_sync(ab8500_fg_init);
3256 module_exit(ab8500_fg_exit);
3258 MODULE_LICENSE("GPL v2");
3259 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
3260 MODULE_ALIAS("platform:ab8500-fg");
3261 MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");