1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson AB 2012
5 * Main and Back-up battery management driver.
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
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/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>
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>
39 #include "ab8500-bm.h"
41 #define FG_LSB_IN_MA 1627
42 #define QLSB_NANO_AMP_HOURS_X10 1071
43 #define INS_CURR_TIMEOUT (3 * HZ)
45 #define SEC_TO_SAMPLE(S) (S * 4)
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
52 #define LOW_BAT_CHECK_INTERVAL (HZ / 16) /* 62.5 ms */
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
63 * struct ab8500_fg_interrupts - ab8500 fg interrupts
64 * @name: name of the interrupt
65 * @isr function pointer to the isr
67 struct ab8500_fg_interrupts {
69 irqreturn_t (*isr)(int irq, void *data);
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,
82 static char *discharge_state[] = {
84 "DISCHARGE_INITMEASURING",
85 "DISCHARGE_INIT_RECOVERY",
87 "DISCHARGE_READOUT_INIT",
92 enum ab8500_fg_charge_state {
93 AB8500_FG_CHARGE_INIT,
94 AB8500_FG_CHARGE_READOUT,
97 static char *charge_state[] = {
102 enum ab8500_fg_calibration_state {
103 AB8500_FG_CALIB_INIT,
104 AB8500_FG_CALIB_WAIT,
108 struct ab8500_fg_avg_cap {
110 int samples[NBR_AVG_SAMPLES];
111 time64_t time_stamps[NBR_AVG_SAMPLES];
117 struct ab8500_fg_cap_scaling {
120 int disable_cap_level;
124 struct ab8500_fg_battery_capacity {
134 struct ab8500_fg_cap_scaling cap_scale;
137 struct ab8500_fg_flags {
149 bool batt_id_received;
152 struct inst_curr_result_list {
153 struct list_head list;
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
202 struct list_head node;
215 int nbr_cceoc_irq_cnt;
216 u32 line_impedance_uohm;
217 bool recovery_needed;
221 enum ab8500_fg_calibration_state calib_state;
222 enum ab8500_fg_discharge_state discharge_state;
223 enum ab8500_fg_charge_state charge_state;
224 struct completion ab8500_fg_started;
225 struct completion ab8500_fg_complete;
226 struct ab8500_fg_flags flags;
227 struct ab8500_fg_battery_capacity bat_cap;
228 struct ab8500_fg_avg_cap avg_cap;
229 struct ab8500 *parent;
230 struct iio_channel *main_bat_v;
231 struct ab8500_bm_data *bm;
232 struct power_supply *fg_psy;
233 struct workqueue_struct *fg_wq;
234 struct delayed_work fg_periodic_work;
235 struct delayed_work fg_low_bat_work;
236 struct delayed_work fg_reinit_work;
237 struct work_struct fg_work;
238 struct work_struct fg_acc_cur_work;
239 struct delayed_work fg_check_hw_failure_work;
240 struct mutex cc_lock;
241 struct kobject fg_kobject;
243 static LIST_HEAD(ab8500_fg_list);
246 * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
247 * (i.e. the first fuel gauge in the instance list)
249 struct ab8500_fg *ab8500_fg_get(void)
251 return list_first_entry_or_null(&ab8500_fg_list, struct ab8500_fg,
255 /* Main battery properties */
256 static enum power_supply_property ab8500_fg_props[] = {
257 POWER_SUPPLY_PROP_VOLTAGE_NOW,
258 POWER_SUPPLY_PROP_CURRENT_NOW,
259 POWER_SUPPLY_PROP_CURRENT_AVG,
260 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
261 POWER_SUPPLY_PROP_ENERGY_FULL,
262 POWER_SUPPLY_PROP_ENERGY_NOW,
263 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
264 POWER_SUPPLY_PROP_CHARGE_FULL,
265 POWER_SUPPLY_PROP_CHARGE_NOW,
266 POWER_SUPPLY_PROP_CAPACITY,
267 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
271 * This array maps the raw hex value to lowbat voltage used by the AB8500
272 * Values taken from the UM0836, in microvolts.
274 static int ab8500_fg_lowbat_voltage_map[] = {
341 static u8 ab8500_volt_to_regval(int voltage_uv)
345 if (voltage_uv < ab8500_fg_lowbat_voltage_map[0])
348 for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) {
349 if (voltage_uv < ab8500_fg_lowbat_voltage_map[i])
353 /* If not captured above, return index of last element */
354 return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1;
358 * ab8500_fg_is_low_curr() - Low or high current mode
359 * @di: pointer to the ab8500_fg structure
360 * @curr_ua: the current to base or our decision on in microampere
362 * Low current mode if the current consumption is below a certain threshold
364 static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr_ua)
367 * We want to know if we're in low current mode
369 if (curr_ua > -di->bm->fg_params->high_curr_threshold_ua)
376 * ab8500_fg_add_cap_sample() - Add capacity to average filter
377 * @di: pointer to the ab8500_fg structure
378 * @sample: the capacity in mAh to add to the filter
380 * A capacity is added to the filter and a new mean capacity is calculated and
383 static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample)
385 time64_t now = ktime_get_boottime_seconds();
386 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
389 avg->sum += sample - avg->samples[avg->pos];
390 avg->samples[avg->pos] = sample;
391 avg->time_stamps[avg->pos] = now;
394 if (avg->pos == NBR_AVG_SAMPLES)
397 if (avg->nbr_samples < NBR_AVG_SAMPLES)
401 * Check the time stamp for each sample. If too old,
402 * replace with latest sample
404 } while (now - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]);
406 avg->avg = avg->sum / avg->nbr_samples;
412 * ab8500_fg_clear_cap_samples() - Clear average filter
413 * @di: pointer to the ab8500_fg structure
415 * The capacity filter is reset to zero.
417 static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di)
420 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
423 avg->nbr_samples = 0;
427 for (i = 0; i < NBR_AVG_SAMPLES; i++) {
429 avg->time_stamps[i] = 0;
434 * ab8500_fg_fill_cap_sample() - Fill average filter
435 * @di: pointer to the ab8500_fg structure
436 * @sample: the capacity in mAh to fill the filter with
438 * The capacity filter is filled with a capacity in mAh
440 static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample)
444 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
446 now = ktime_get_boottime_seconds();
448 for (i = 0; i < NBR_AVG_SAMPLES; i++) {
449 avg->samples[i] = sample;
450 avg->time_stamps[i] = now;
454 avg->nbr_samples = NBR_AVG_SAMPLES;
455 avg->sum = sample * NBR_AVG_SAMPLES;
460 * ab8500_fg_coulomb_counter() - enable coulomb counter
461 * @di: pointer to the ab8500_fg structure
462 * @enable: enable/disable
464 * Enable/Disable coulomb counter.
465 * On failure returns negative value.
467 static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable)
470 mutex_lock(&di->cc_lock);
472 /* To be able to reprogram the number of samples, we have to
473 * first stop the CC and then enable it again */
474 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
475 AB8500_RTC_CC_CONF_REG, 0x00);
479 /* Program the samples */
480 ret = abx500_set_register_interruptible(di->dev,
481 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
487 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
488 AB8500_RTC_CC_CONF_REG,
489 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
493 di->flags.fg_enabled = true;
495 /* Clear any pending read requests */
496 ret = abx500_mask_and_set_register_interruptible(di->dev,
497 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
498 (RESET_ACCU | READ_REQ), 0);
502 ret = abx500_set_register_interruptible(di->dev,
503 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0);
508 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
509 AB8500_RTC_CC_CONF_REG, 0);
513 di->flags.fg_enabled = false;
516 dev_dbg(di->dev, " CC enabled: %d Samples: %d\n",
517 enable, di->fg_samples);
519 mutex_unlock(&di->cc_lock);
523 dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__);
524 mutex_unlock(&di->cc_lock);
529 * ab8500_fg_inst_curr_start() - start battery instantaneous current
530 * @di: pointer to the ab8500_fg structure
532 * Returns 0 or error code
533 * Note: This is part "one" and has to be called before
534 * ab8500_fg_inst_curr_finalize()
536 int ab8500_fg_inst_curr_start(struct ab8500_fg *di)
541 mutex_lock(&di->cc_lock);
543 di->nbr_cceoc_irq_cnt = 0;
544 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
545 AB8500_RTC_CC_CONF_REG, ®_val);
549 if (!(reg_val & CC_PWR_UP_ENA)) {
550 dev_dbg(di->dev, "%s Enable FG\n", __func__);
551 di->turn_off_fg = true;
553 /* Program the samples */
554 ret = abx500_set_register_interruptible(di->dev,
555 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
561 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
562 AB8500_RTC_CC_CONF_REG,
563 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
567 di->turn_off_fg = false;
571 reinit_completion(&di->ab8500_fg_started);
572 reinit_completion(&di->ab8500_fg_complete);
575 /* Note: cc_lock is still locked */
578 mutex_unlock(&di->cc_lock);
583 * ab8500_fg_inst_curr_started() - check if fg conversion has started
584 * @di: pointer to the ab8500_fg structure
586 * Returns 1 if conversion started, 0 if still waiting
588 int ab8500_fg_inst_curr_started(struct ab8500_fg *di)
590 return completion_done(&di->ab8500_fg_started);
594 * ab8500_fg_inst_curr_done() - check if fg conversion is done
595 * @di: pointer to the ab8500_fg structure
597 * Returns 1 if conversion done, 0 if still waiting
599 int ab8500_fg_inst_curr_done(struct ab8500_fg *di)
601 return completion_done(&di->ab8500_fg_complete);
605 * ab8500_fg_inst_curr_finalize() - battery instantaneous current
606 * @di: pointer to the ab8500_fg structure
607 * @curr_ua: battery instantenous current in microampere (on success)
609 * Returns 0 or an error code
610 * Note: This is part "two" and has to be called at earliest 250 ms
611 * after ab8500_fg_inst_curr_start()
613 int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *curr_ua)
618 unsigned long timeout;
620 if (!completion_done(&di->ab8500_fg_complete)) {
621 timeout = wait_for_completion_timeout(
622 &di->ab8500_fg_complete,
624 dev_dbg(di->dev, "Finalize time: %d ms\n",
625 jiffies_to_msecs(INS_CURR_TIMEOUT - timeout));
628 disable_irq(di->irq);
629 di->nbr_cceoc_irq_cnt = 0;
630 dev_err(di->dev, "completion timed out [%d]\n",
636 disable_irq(di->irq);
637 di->nbr_cceoc_irq_cnt = 0;
639 ret = abx500_mask_and_set_register_interruptible(di->dev,
640 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
643 /* 100uS between read request and read is needed */
644 usleep_range(100, 100);
646 /* Read CC Sample conversion value Low and high */
647 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
648 AB8500_GASG_CC_SMPL_CNVL_REG, &low);
652 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
653 AB8500_GASG_CC_SMPL_CNVH_REG, &high);
658 * negative value for Discharging
659 * convert 2's complement into decimal
662 val = (low | (high << 8) | 0xFFFFE000);
664 val = (low | (high << 8));
667 * Convert to unit value in mA
668 * Full scale input voltage is
669 * 63.160mV => LSB = 63.160mV/(4096*res) = 1.542.000 uA
670 * Given a 250ms conversion cycle time the LSB corresponds
671 * to 107.1 nAh. Convert to current by dividing by the conversion
672 * time in hours (250ms = 1 / (3600 * 4)h)
673 * 107.1nAh assumes 10mOhm, but fg_res is in 0.1mOhm
675 val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) / 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
705 * Returns battery instantenous current in microampere (on success)
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, &curr_ua);
737 dev_err(di->dev, "Failed to finalize fg_inst\n");
741 dev_dbg(di->dev, "%s instant current: %d uA", __func__, curr_ua);
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 complement */
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 uA
803 * by dividing by the conversion
804 * time in hours (= samples / (3600 * 4)h)
806 di->avg_curr_ua = (val * QLSB_NANO_AMP_HOURS_X10 * 36) /
807 (di->bm->fg_res * (di->fg_samples / 4));
809 di->flags.conv_done = true;
811 mutex_unlock(&di->cc_lock);
813 queue_work(di->fg_wq, &di->fg_work);
815 dev_dbg(di->dev, "fg_res: %d, fg_samples: %d, gasg: %d, accu_charge: %d \n",
816 di->bm->fg_res, di->fg_samples, val, di->accu_charge);
820 "Failed to read or write gas gauge registers\n");
821 mutex_unlock(&di->cc_lock);
822 queue_work(di->fg_wq, &di->fg_work);
826 * ab8500_fg_bat_voltage() - get battery voltage
827 * @di: pointer to the ab8500_fg structure
829 * Returns battery voltage in microvolts (on success) else error code
831 static int ab8500_fg_bat_voltage(struct ab8500_fg *di)
836 ret = iio_read_channel_processed(di->main_bat_v, &vbat);
839 "%s ADC conversion failed, using previous value\n",
844 /* IIO returns millivolts but we want microvolts */
851 * ab8500_fg_volt_to_capacity() - Voltage based capacity
852 * @di: pointer to the ab8500_fg structure
853 * @voltage_uv: The voltage to convert to a capacity in microvolt
855 * Returns battery capacity in per mille based on voltage
857 static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage_uv)
859 struct power_supply_battery_info *bi = di->bm->bi;
861 /* Multiply by 10 because the capacity is tracked in per mille */
862 return power_supply_batinfo_ocv2cap(bi, voltage_uv, di->bat_temp) * 10;
866 * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
867 * @di: pointer to the ab8500_fg structure
869 * Returns battery capacity based on battery voltage that is not compensated
870 * for the voltage drop due to the load
872 static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di)
874 di->vbat_uv = ab8500_fg_bat_voltage(di);
875 return ab8500_fg_volt_to_capacity(di, di->vbat_uv);
879 * ab8500_fg_battery_resistance() - Returns the battery inner resistance
880 * @di: pointer to the ab8500_fg structure
881 * @vbat_uncomp_uv: Uncompensated VBAT voltage
883 * Returns battery inner resistance added with the fuel gauge resistor value
884 * to get the total resistance in the whole link from gnd to bat+ node
887 static int ab8500_fg_battery_resistance(struct ab8500_fg *di, int vbat_uncomp_uv)
889 struct power_supply_battery_info *bi = di->bm->bi;
890 int resistance_percent = 0;
894 * Determine the resistance at this voltage. First try VBAT-to-Ri else
895 * just infer it from the surrounding temperature, if nothing works just
896 * use the internal resistance.
898 if (power_supply_supports_vbat2ri(bi)) {
899 resistance = power_supply_vbat2ri(bi, vbat_uncomp_uv, di->flags.charging);
900 /* Convert to milliohm */
901 resistance = resistance / 1000;
902 } else if (power_supply_supports_temp2ri(bi)) {
903 resistance_percent = power_supply_temp2resist_simple(bi->resist_table,
904 bi->resist_table_size,
906 /* Convert to milliohm */
907 resistance = bi->factory_internal_resistance_uohm / 1000;
908 resistance = resistance * resistance_percent / 100;
911 resistance = bi->factory_internal_resistance_uohm / 1000;
914 /* Compensate for line impedance */
915 resistance += (di->line_impedance_uohm / 1000);
917 dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d"
918 " fg resistance %d, total: %d (mOhm)\n",
919 __func__, di->bat_temp, resistance, di->bm->fg_res / 10,
920 (di->bm->fg_res / 10) + resistance);
922 /* fg_res variable is in 0.1mOhm */
923 resistance += di->bm->fg_res / 10;
929 * ab8500_load_comp_fg_bat_voltage() - get load compensated battery voltage
930 * @di: pointer to the ab8500_fg structure
931 * @always: always return a voltage, also uncompensated
933 * Returns compensated battery voltage (on success) else error code.
934 * If always is specified, we always return a voltage but it may be
937 static int ab8500_load_comp_fg_bat_voltage(struct ab8500_fg *di, bool always)
943 /* Average the instant current to get a stable current measurement */
944 ab8500_fg_inst_curr_start(di);
947 vbat_uv += ab8500_fg_bat_voltage(di);
949 usleep_range(5000, 6000);
950 } while (!ab8500_fg_inst_curr_done(di) &&
951 i <= WAIT_FOR_INST_CURRENT_MAX);
953 if (i > WAIT_FOR_INST_CURRENT_MAX) {
955 "TIMEOUT: return uncompensated measurement of VBAT\n");
956 di->vbat_uv = vbat_uv / i;
960 ab8500_fg_inst_curr_finalize(di, &di->inst_curr_ua);
963 * If there is too high current dissipation, the compensation cannot be
964 * trusted so return an error unless we must return something here, as
965 * enforced by the "always" parameter.
967 if (!always && di->inst_curr_ua < IGNORE_VBAT_HIGHCUR)
970 vbat_uv = vbat_uv / i;
972 /* Next we apply voltage compensation from internal resistance */
973 rcomp = ab8500_fg_battery_resistance(di, vbat_uv);
974 vbat_uv = vbat_uv - (di->inst_curr_ua * rcomp) / 1000;
976 /* Always keep this state at latest measurement */
977 di->vbat_uv = vbat_uv;
983 * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
984 * @di: pointer to the ab8500_fg structure
986 * Returns battery capacity based on battery voltage that is load compensated
987 * for the voltage drop
989 static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di)
993 vbat_comp_uv = ab8500_load_comp_fg_bat_voltage(di, true);
995 return ab8500_fg_volt_to_capacity(di, vbat_comp_uv);
999 * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
1000 * @di: pointer to the ab8500_fg structure
1001 * @cap_mah: capacity in mAh
1003 * Converts capacity in mAh to capacity in permille
1005 static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah)
1007 return (cap_mah * 1000) / di->bat_cap.max_mah_design;
1011 * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
1012 * @di: pointer to the ab8500_fg structure
1013 * @cap_pm: capacity in permille
1015 * Converts capacity in permille to capacity in mAh
1017 static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm)
1019 return cap_pm * di->bat_cap.max_mah_design / 1000;
1023 * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
1024 * @di: pointer to the ab8500_fg structure
1025 * @cap_mah: capacity in mAh
1027 * Converts capacity in mAh to capacity in uWh
1029 static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah)
1035 * Capacity is in milli ampere hours (10^-3)Ah
1036 * Nominal voltage is in microvolts (10^-6)V
1037 * divide by 1000000 after multiplication to get to mWh
1039 div_res = ((u64) cap_mah) * ((u64) di->vbat_nom_uv);
1040 div_rem = do_div(div_res, 1000000);
1042 /* Make sure to round upwards if necessary */
1043 if (div_rem >= 1000000 / 2)
1046 return (int) div_res;
1050 * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
1051 * @di: pointer to the ab8500_fg structure
1053 * Return the capacity in mAh based on previous calculated capcity and the FG
1054 * accumulator register value. The filter is filled with this capacity
1056 static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di)
1058 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1063 /* Capacity should not be less than 0 */
1064 if (di->bat_cap.mah + di->accu_charge > 0)
1065 di->bat_cap.mah += di->accu_charge;
1067 di->bat_cap.mah = 0;
1069 * We force capacity to 100% once when the algorithm
1070 * reports that it's full.
1072 if (di->bat_cap.mah >= di->bat_cap.max_mah_design ||
1073 di->flags.force_full) {
1074 di->bat_cap.mah = di->bat_cap.max_mah_design;
1077 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1078 di->bat_cap.permille =
1079 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1081 /* We need to update battery voltage and inst current when charging */
1082 di->vbat_uv = ab8500_fg_bat_voltage(di);
1083 di->inst_curr_ua = ab8500_fg_inst_curr_blocking(di);
1085 return di->bat_cap.mah;
1089 * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage
1090 * @di: pointer to the ab8500_fg structure
1092 * Return the capacity in mAh based on the load compensated battery voltage.
1093 * This value is added to the filter and a new mean value is calculated and
1096 static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di)
1100 permille = ab8500_fg_load_comp_volt_to_capacity(di);
1102 mah = ab8500_fg_convert_permille_to_mah(di, permille);
1104 di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah);
1105 di->bat_cap.permille =
1106 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1108 return di->bat_cap.mah;
1112 * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG
1113 * @di: pointer to the ab8500_fg structure
1115 * Return the capacity in mAh based on previous calculated capcity and the FG
1116 * accumulator register value. This value is added to the filter and a
1117 * new mean value is calculated and returned.
1119 static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di)
1121 int permille_volt, permille;
1123 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1128 /* Capacity should not be less than 0 */
1129 if (di->bat_cap.mah + di->accu_charge > 0)
1130 di->bat_cap.mah += di->accu_charge;
1132 di->bat_cap.mah = 0;
1134 if (di->bat_cap.mah >= di->bat_cap.max_mah_design)
1135 di->bat_cap.mah = di->bat_cap.max_mah_design;
1138 * Check against voltage based capacity. It can not be lower
1139 * than what the uncompensated voltage says
1141 permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1142 permille_volt = ab8500_fg_uncomp_volt_to_capacity(di);
1144 if (permille < permille_volt) {
1145 di->bat_cap.permille = permille_volt;
1146 di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di,
1147 di->bat_cap.permille);
1149 dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n",
1154 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1156 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1157 di->bat_cap.permille =
1158 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1161 return di->bat_cap.mah;
1165 * ab8500_fg_capacity_level() - Get the battery capacity level
1166 * @di: pointer to the ab8500_fg structure
1168 * Get the battery capacity level based on the capacity in percent
1170 static int ab8500_fg_capacity_level(struct ab8500_fg *di)
1174 percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10);
1176 if (percent <= di->bm->cap_levels->critical ||
1178 ret = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1179 else if (percent <= di->bm->cap_levels->low)
1180 ret = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1181 else if (percent <= di->bm->cap_levels->normal)
1182 ret = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1183 else if (percent <= di->bm->cap_levels->high)
1184 ret = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
1186 ret = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1192 * ab8500_fg_calculate_scaled_capacity() - Capacity scaling
1193 * @di: pointer to the ab8500_fg structure
1195 * Calculates the capacity to be shown to upper layers. Scales the capacity
1196 * to have 100% as a reference from the actual capacity upon removal of charger
1197 * when charging is in maintenance mode.
1199 static int ab8500_fg_calculate_scaled_capacity(struct ab8500_fg *di)
1201 struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale;
1202 int capacity = di->bat_cap.prev_percent;
1208 * As long as we are in fully charge mode scale the capacity
1211 if (di->flags.fully_charged) {
1212 cs->cap_to_scale[0] = 100;
1213 cs->cap_to_scale[1] =
1214 max(capacity, di->bm->fg_params->maint_thres);
1215 dev_dbg(di->dev, "Scale cap with %d/%d\n",
1216 cs->cap_to_scale[0], cs->cap_to_scale[1]);
1219 /* Calculates the scaled capacity. */
1220 if ((cs->cap_to_scale[0] != cs->cap_to_scale[1])
1221 && (cs->cap_to_scale[1] > 0))
1223 DIV_ROUND_CLOSEST(di->bat_cap.prev_percent *
1224 cs->cap_to_scale[0],
1225 cs->cap_to_scale[1]));
1227 if (di->flags.charging) {
1228 if (capacity < cs->disable_cap_level) {
1229 cs->disable_cap_level = capacity;
1230 dev_dbg(di->dev, "Cap to stop scale lowered %d%%\n",
1231 cs->disable_cap_level);
1232 } else if (!di->flags.fully_charged) {
1233 if (di->bat_cap.prev_percent >=
1234 cs->disable_cap_level) {
1235 dev_dbg(di->dev, "Disabling scaled capacity\n");
1237 capacity = di->bat_cap.prev_percent;
1240 "Waiting in cap to level %d%%\n",
1241 cs->disable_cap_level);
1242 capacity = cs->disable_cap_level;
1251 * ab8500_fg_update_cap_scalers() - Capacity scaling
1252 * @di: pointer to the ab8500_fg structure
1254 * To be called when state change from charge<->discharge to update
1255 * the capacity scalers.
1257 static void ab8500_fg_update_cap_scalers(struct ab8500_fg *di)
1259 struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale;
1263 if (di->flags.charging) {
1264 di->bat_cap.cap_scale.disable_cap_level =
1265 di->bat_cap.cap_scale.scaled_cap;
1266 dev_dbg(di->dev, "Cap to stop scale at charge %d%%\n",
1267 di->bat_cap.cap_scale.disable_cap_level);
1269 if (cs->scaled_cap != 100) {
1270 cs->cap_to_scale[0] = cs->scaled_cap;
1271 cs->cap_to_scale[1] = di->bat_cap.prev_percent;
1273 cs->cap_to_scale[0] = 100;
1274 cs->cap_to_scale[1] =
1275 max(di->bat_cap.prev_percent,
1276 di->bm->fg_params->maint_thres);
1279 dev_dbg(di->dev, "Cap to scale at discharge %d/%d\n",
1280 cs->cap_to_scale[0], cs->cap_to_scale[1]);
1285 * ab8500_fg_check_capacity_limits() - Check if capacity has changed
1286 * @di: pointer to the ab8500_fg structure
1287 * @init: capacity is allowed to go up in init mode
1289 * Check if capacity or capacity limit has changed and notify the system
1290 * about it using the power_supply framework
1292 static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init)
1294 bool changed = false;
1295 int percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10);
1297 di->bat_cap.level = ab8500_fg_capacity_level(di);
1299 if (di->bat_cap.level != di->bat_cap.prev_level) {
1301 * We do not allow reported capacity level to go up
1302 * unless we're charging or if we're in init
1304 if (!(!di->flags.charging && di->bat_cap.level >
1305 di->bat_cap.prev_level) || init) {
1306 dev_dbg(di->dev, "level changed from %d to %d\n",
1307 di->bat_cap.prev_level,
1309 di->bat_cap.prev_level = di->bat_cap.level;
1312 dev_dbg(di->dev, "level not allowed to go up "
1313 "since no charger is connected: %d to %d\n",
1314 di->bat_cap.prev_level,
1320 * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate
1323 if (di->flags.low_bat) {
1324 dev_dbg(di->dev, "Battery low, set capacity to 0\n");
1325 di->bat_cap.prev_percent = 0;
1326 di->bat_cap.permille = 0;
1328 di->bat_cap.prev_mah = 0;
1329 di->bat_cap.mah = 0;
1331 } else if (di->flags.fully_charged) {
1333 * We report 100% if algorithm reported fully charged
1334 * and show 100% during maintenance charging (scaling).
1336 if (di->flags.force_full) {
1337 di->bat_cap.prev_percent = percent;
1338 di->bat_cap.prev_mah = di->bat_cap.mah;
1342 if (!di->bat_cap.cap_scale.enable &&
1343 di->bm->capacity_scaling) {
1344 di->bat_cap.cap_scale.enable = true;
1345 di->bat_cap.cap_scale.cap_to_scale[0] = 100;
1346 di->bat_cap.cap_scale.cap_to_scale[1] =
1347 di->bat_cap.prev_percent;
1348 di->bat_cap.cap_scale.disable_cap_level = 100;
1350 } else if (di->bat_cap.prev_percent != percent) {
1352 "battery reported full "
1353 "but capacity dropping: %d\n",
1355 di->bat_cap.prev_percent = percent;
1356 di->bat_cap.prev_mah = di->bat_cap.mah;
1360 } else if (di->bat_cap.prev_percent != percent) {
1363 * We will not report 0% unless we've got
1364 * the LOW_BAT IRQ, no matter what the FG
1367 di->bat_cap.prev_percent = 1;
1371 } else if (!(!di->flags.charging &&
1372 percent > di->bat_cap.prev_percent) || init) {
1374 * We do not allow reported capacity to go up
1375 * unless we're charging or if we're in init
1378 "capacity changed from %d to %d (%d)\n",
1379 di->bat_cap.prev_percent,
1381 di->bat_cap.permille);
1382 di->bat_cap.prev_percent = percent;
1383 di->bat_cap.prev_mah = di->bat_cap.mah;
1387 dev_dbg(di->dev, "capacity not allowed to go up since "
1388 "no charger is connected: %d to %d (%d)\n",
1389 di->bat_cap.prev_percent,
1391 di->bat_cap.permille);
1396 if (di->bm->capacity_scaling) {
1397 di->bat_cap.cap_scale.scaled_cap =
1398 ab8500_fg_calculate_scaled_capacity(di);
1400 dev_info(di->dev, "capacity=%d (%d)\n",
1401 di->bat_cap.prev_percent,
1402 di->bat_cap.cap_scale.scaled_cap);
1404 power_supply_changed(di->fg_psy);
1405 if (di->flags.fully_charged && di->flags.force_full) {
1406 dev_dbg(di->dev, "Battery full, notifying.\n");
1407 di->flags.force_full = false;
1408 sysfs_notify(&di->fg_kobject, NULL, "charge_full");
1410 sysfs_notify(&di->fg_kobject, NULL, "charge_now");
1414 static void ab8500_fg_charge_state_to(struct ab8500_fg *di,
1415 enum ab8500_fg_charge_state new_state)
1417 dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n",
1419 charge_state[di->charge_state],
1421 charge_state[new_state]);
1423 di->charge_state = new_state;
1426 static void ab8500_fg_discharge_state_to(struct ab8500_fg *di,
1427 enum ab8500_fg_discharge_state new_state)
1429 dev_dbg(di->dev, "Discharge state from %d [%s] to %d [%s]\n",
1430 di->discharge_state,
1431 discharge_state[di->discharge_state],
1433 discharge_state[new_state]);
1435 di->discharge_state = new_state;
1439 * ab8500_fg_algorithm_charging() - FG algorithm for when charging
1440 * @di: pointer to the ab8500_fg structure
1442 * Battery capacity calculation state machine for when we're charging
1444 static void ab8500_fg_algorithm_charging(struct ab8500_fg *di)
1447 * If we change to discharge mode
1448 * we should start with recovery
1450 if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY)
1451 ab8500_fg_discharge_state_to(di,
1452 AB8500_FG_DISCHARGE_INIT_RECOVERY);
1454 switch (di->charge_state) {
1455 case AB8500_FG_CHARGE_INIT:
1456 di->fg_samples = SEC_TO_SAMPLE(
1457 di->bm->fg_params->accu_charging);
1459 ab8500_fg_coulomb_counter(di, true);
1460 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT);
1464 case AB8500_FG_CHARGE_READOUT:
1466 * Read the FG and calculate the new capacity
1468 mutex_lock(&di->cc_lock);
1469 if (!di->flags.conv_done && !di->flags.force_full) {
1470 /* Wasn't the CC IRQ that got us here */
1471 mutex_unlock(&di->cc_lock);
1472 dev_dbg(di->dev, "%s CC conv not done\n",
1477 di->flags.conv_done = false;
1478 mutex_unlock(&di->cc_lock);
1480 ab8500_fg_calc_cap_charging(di);
1488 /* Check capacity limits */
1489 ab8500_fg_check_capacity_limits(di, false);
1492 static void force_capacity(struct ab8500_fg *di)
1496 ab8500_fg_clear_cap_samples(di);
1497 cap = di->bat_cap.user_mah;
1498 if (cap > di->bat_cap.max_mah_design) {
1499 dev_dbg(di->dev, "Remaining cap %d can't be bigger than total"
1500 " %d\n", cap, di->bat_cap.max_mah_design);
1501 cap = di->bat_cap.max_mah_design;
1503 ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah);
1504 di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap);
1505 di->bat_cap.mah = cap;
1506 ab8500_fg_check_capacity_limits(di, true);
1509 static bool check_sysfs_capacity(struct ab8500_fg *di)
1511 int cap, lower, upper;
1514 cap = di->bat_cap.user_mah;
1516 cap_permille = ab8500_fg_convert_mah_to_permille(di,
1517 di->bat_cap.user_mah);
1519 lower = di->bat_cap.permille - di->bm->fg_params->user_cap_limit * 10;
1520 upper = di->bat_cap.permille + di->bm->fg_params->user_cap_limit * 10;
1524 /* 1000 is permille, -> 100 percent */
1528 dev_dbg(di->dev, "Capacity limits:"
1529 " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
1530 lower, cap_permille, upper, cap, di->bat_cap.mah);
1532 /* If within limits, use the saved capacity and exit estimation...*/
1533 if (cap_permille > lower && cap_permille < upper) {
1534 dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap);
1538 dev_dbg(di->dev, "Capacity from user out of limits, ignoring");
1543 * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
1544 * @di: pointer to the ab8500_fg structure
1546 * Battery capacity calculation state machine for when we're discharging
1548 static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di)
1552 /* If we change to charge mode we should start with init */
1553 if (di->charge_state != AB8500_FG_CHARGE_INIT)
1554 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
1556 switch (di->discharge_state) {
1557 case AB8500_FG_DISCHARGE_INIT:
1558 /* We use the FG IRQ to work on */
1560 di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
1561 ab8500_fg_coulomb_counter(di, true);
1562 ab8500_fg_discharge_state_to(di,
1563 AB8500_FG_DISCHARGE_INITMEASURING);
1566 case AB8500_FG_DISCHARGE_INITMEASURING:
1568 * Discard a number of samples during startup.
1569 * After that, use compensated voltage for a few
1570 * samples to get an initial capacity.
1571 * Then go to READOUT
1573 sleep_time = di->bm->fg_params->init_timer;
1575 /* Discard the first [x] seconds */
1576 if (di->init_cnt > di->bm->fg_params->init_discard_time) {
1577 ab8500_fg_calc_cap_discharge_voltage(di);
1579 ab8500_fg_check_capacity_limits(di, true);
1582 di->init_cnt += sleep_time;
1583 if (di->init_cnt > di->bm->fg_params->init_total_time)
1584 ab8500_fg_discharge_state_to(di,
1585 AB8500_FG_DISCHARGE_READOUT_INIT);
1589 case AB8500_FG_DISCHARGE_INIT_RECOVERY:
1590 di->recovery_cnt = 0;
1591 di->recovery_needed = true;
1592 ab8500_fg_discharge_state_to(di,
1593 AB8500_FG_DISCHARGE_RECOVERY);
1597 case AB8500_FG_DISCHARGE_RECOVERY:
1598 sleep_time = di->bm->fg_params->recovery_sleep_timer;
1601 * We should check the power consumption
1602 * If low, go to READOUT (after x min) or
1603 * RECOVERY_SLEEP if time left.
1604 * If high, go to READOUT
1606 di->inst_curr_ua = ab8500_fg_inst_curr_blocking(di);
1608 if (ab8500_fg_is_low_curr(di, di->inst_curr_ua)) {
1609 if (di->recovery_cnt >
1610 di->bm->fg_params->recovery_total_time) {
1611 di->fg_samples = SEC_TO_SAMPLE(
1612 di->bm->fg_params->accu_high_curr);
1613 ab8500_fg_coulomb_counter(di, true);
1614 ab8500_fg_discharge_state_to(di,
1615 AB8500_FG_DISCHARGE_READOUT);
1616 di->recovery_needed = false;
1618 queue_delayed_work(di->fg_wq,
1619 &di->fg_periodic_work,
1622 di->recovery_cnt += sleep_time;
1624 di->fg_samples = SEC_TO_SAMPLE(
1625 di->bm->fg_params->accu_high_curr);
1626 ab8500_fg_coulomb_counter(di, true);
1627 ab8500_fg_discharge_state_to(di,
1628 AB8500_FG_DISCHARGE_READOUT);
1632 case AB8500_FG_DISCHARGE_READOUT_INIT:
1633 di->fg_samples = SEC_TO_SAMPLE(
1634 di->bm->fg_params->accu_high_curr);
1635 ab8500_fg_coulomb_counter(di, true);
1636 ab8500_fg_discharge_state_to(di,
1637 AB8500_FG_DISCHARGE_READOUT);
1640 case AB8500_FG_DISCHARGE_READOUT:
1641 di->inst_curr_ua = ab8500_fg_inst_curr_blocking(di);
1643 if (ab8500_fg_is_low_curr(di, di->inst_curr_ua)) {
1644 /* Detect mode change */
1645 if (di->high_curr_mode) {
1646 di->high_curr_mode = false;
1647 di->high_curr_cnt = 0;
1650 if (di->recovery_needed) {
1651 ab8500_fg_discharge_state_to(di,
1652 AB8500_FG_DISCHARGE_INIT_RECOVERY);
1654 queue_delayed_work(di->fg_wq,
1655 &di->fg_periodic_work, 0);
1660 ab8500_fg_calc_cap_discharge_voltage(di);
1662 mutex_lock(&di->cc_lock);
1663 if (!di->flags.conv_done) {
1664 /* Wasn't the CC IRQ that got us here */
1665 mutex_unlock(&di->cc_lock);
1666 dev_dbg(di->dev, "%s CC conv not done\n",
1671 di->flags.conv_done = false;
1672 mutex_unlock(&di->cc_lock);
1674 /* Detect mode change */
1675 if (!di->high_curr_mode) {
1676 di->high_curr_mode = true;
1677 di->high_curr_cnt = 0;
1680 di->high_curr_cnt +=
1681 di->bm->fg_params->accu_high_curr;
1682 if (di->high_curr_cnt >
1683 di->bm->fg_params->high_curr_time)
1684 di->recovery_needed = true;
1686 ab8500_fg_calc_cap_discharge_fg(di);
1689 ab8500_fg_check_capacity_limits(di, false);
1693 case AB8500_FG_DISCHARGE_WAKEUP:
1694 ab8500_fg_calc_cap_discharge_voltage(di);
1696 di->fg_samples = SEC_TO_SAMPLE(
1697 di->bm->fg_params->accu_high_curr);
1698 ab8500_fg_coulomb_counter(di, true);
1699 ab8500_fg_discharge_state_to(di,
1700 AB8500_FG_DISCHARGE_READOUT);
1702 ab8500_fg_check_capacity_limits(di, false);
1712 * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
1713 * @di: pointer to the ab8500_fg structure
1716 static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di)
1720 switch (di->calib_state) {
1721 case AB8500_FG_CALIB_INIT:
1722 dev_dbg(di->dev, "Calibration ongoing...\n");
1724 ret = abx500_mask_and_set_register_interruptible(di->dev,
1725 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1726 CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8);
1730 ret = abx500_mask_and_set_register_interruptible(di->dev,
1731 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1732 CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA);
1735 di->calib_state = AB8500_FG_CALIB_WAIT;
1737 case AB8500_FG_CALIB_END:
1738 ret = abx500_mask_and_set_register_interruptible(di->dev,
1739 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1740 CC_MUXOFFSET, CC_MUXOFFSET);
1743 di->flags.calibrate = false;
1744 dev_dbg(di->dev, "Calibration done...\n");
1745 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1747 case AB8500_FG_CALIB_WAIT:
1748 dev_dbg(di->dev, "Calibration WFI\n");
1755 /* Something went wrong, don't calibrate then */
1756 dev_err(di->dev, "failed to calibrate the CC\n");
1757 di->flags.calibrate = false;
1758 di->calib_state = AB8500_FG_CALIB_INIT;
1759 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1763 * ab8500_fg_algorithm() - Entry point for the FG algorithm
1764 * @di: pointer to the ab8500_fg structure
1766 * Entry point for the battery capacity calculation state machine
1768 static void ab8500_fg_algorithm(struct ab8500_fg *di)
1770 if (di->flags.calibrate)
1771 ab8500_fg_algorithm_calibrate(di);
1773 if (di->flags.charging)
1774 ab8500_fg_algorithm_charging(di);
1776 ab8500_fg_algorithm_discharging(di);
1779 dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d %d "
1780 "%d %d %d %d %d %d %d\n",
1781 di->bat_cap.max_mah_design,
1782 di->bat_cap.max_mah,
1784 di->bat_cap.permille,
1786 di->bat_cap.prev_mah,
1787 di->bat_cap.prev_percent,
1788 di->bat_cap.prev_level,
1795 di->discharge_state,
1797 di->recovery_needed);
1801 * ab8500_fg_periodic_work() - Run the FG state machine periodically
1802 * @work: pointer to the work_struct structure
1804 * Work queue function for periodic work
1806 static void ab8500_fg_periodic_work(struct work_struct *work)
1808 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1809 fg_periodic_work.work);
1811 if (di->init_capacity) {
1812 /* Get an initial capacity calculation */
1813 ab8500_fg_calc_cap_discharge_voltage(di);
1814 ab8500_fg_check_capacity_limits(di, true);
1815 di->init_capacity = false;
1817 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1818 } else if (di->flags.user_cap) {
1819 if (check_sysfs_capacity(di)) {
1820 ab8500_fg_check_capacity_limits(di, true);
1821 if (di->flags.charging)
1822 ab8500_fg_charge_state_to(di,
1823 AB8500_FG_CHARGE_INIT);
1825 ab8500_fg_discharge_state_to(di,
1826 AB8500_FG_DISCHARGE_READOUT_INIT);
1828 di->flags.user_cap = false;
1829 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1831 ab8500_fg_algorithm(di);
1836 * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
1837 * @work: pointer to the work_struct structure
1839 * Work queue function for checking the OVV_BAT condition
1841 static void ab8500_fg_check_hw_failure_work(struct work_struct *work)
1846 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1847 fg_check_hw_failure_work.work);
1850 * If we have had a battery over-voltage situation,
1851 * check ovv-bit to see if it should be reset.
1853 ret = abx500_get_register_interruptible(di->dev,
1854 AB8500_CHARGER, AB8500_CH_STAT_REG,
1857 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1860 if ((reg_value & BATT_OVV) == BATT_OVV) {
1861 if (!di->flags.bat_ovv) {
1862 dev_dbg(di->dev, "Battery OVV\n");
1863 di->flags.bat_ovv = true;
1864 power_supply_changed(di->fg_psy);
1866 /* Not yet recovered from ovv, reschedule this test */
1867 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work,
1870 dev_dbg(di->dev, "Battery recovered from OVV\n");
1871 di->flags.bat_ovv = false;
1872 power_supply_changed(di->fg_psy);
1877 * ab8500_fg_low_bat_work() - Check LOW_BAT condition
1878 * @work: pointer to the work_struct structure
1880 * Work queue function for checking the LOW_BAT condition
1882 static void ab8500_fg_low_bat_work(struct work_struct *work)
1886 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1887 fg_low_bat_work.work);
1889 vbat_uv = ab8500_fg_bat_voltage(di);
1891 /* Check if LOW_BAT still fulfilled */
1892 if (vbat_uv < di->bm->fg_params->lowbat_threshold_uv) {
1893 /* Is it time to shut down? */
1894 if (di->low_bat_cnt < 1) {
1895 di->flags.low_bat = true;
1896 dev_warn(di->dev, "Shut down pending...\n");
1899 * Else we need to re-schedule this check to be able to detect
1900 * if the voltage increases again during charging or
1901 * due to decreasing load.
1904 dev_warn(di->dev, "Battery voltage still LOW\n");
1905 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
1906 round_jiffies(LOW_BAT_CHECK_INTERVAL));
1909 di->flags.low_bat_delay = false;
1910 di->low_bat_cnt = 10;
1911 dev_warn(di->dev, "Battery voltage OK again\n");
1914 /* This is needed to dispatch LOW_BAT */
1915 ab8500_fg_check_capacity_limits(di, false);
1919 * ab8500_fg_battok_calc - calculate the bit pattern corresponding
1920 * to the target voltage.
1921 * @di: pointer to the ab8500_fg structure
1922 * @target: target voltage
1924 * Returns bit pattern closest to the target voltage
1925 * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
1928 static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target)
1930 if (target > BATT_OK_MIN +
1931 (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS))
1932 return BATT_OK_MAX_NR_INCREMENTS;
1933 if (target < BATT_OK_MIN)
1935 return (target - BATT_OK_MIN) / BATT_OK_INCREMENT;
1939 * ab8500_fg_battok_init_hw_register - init battok levels
1940 * @di: pointer to the ab8500_fg structure
1944 static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di)
1954 sel0 = di->bm->fg_params->battok_falling_th_sel0;
1955 sel1 = di->bm->fg_params->battok_raising_th_sel1;
1957 cbp_sel0 = ab8500_fg_battok_calc(di, sel0);
1958 cbp_sel1 = ab8500_fg_battok_calc(di, sel1);
1960 selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT;
1962 if (selected != sel0)
1963 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1964 sel0, selected, cbp_sel0);
1966 selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT;
1968 if (selected != sel1)
1969 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1970 sel1, selected, cbp_sel1);
1972 new_val = cbp_sel0 | (cbp_sel1 << 4);
1974 dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1);
1975 ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK,
1976 AB8500_BATT_OK_REG, new_val);
1981 * ab8500_fg_instant_work() - Run the FG state machine instantly
1982 * @work: pointer to the work_struct structure
1984 * Work queue function for instant work
1986 static void ab8500_fg_instant_work(struct work_struct *work)
1988 struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work);
1990 ab8500_fg_algorithm(di);
1994 * ab8500_fg_cc_data_end_handler() - end of data conversion isr.
1995 * @irq: interrupt number
1996 * @_di: pointer to the ab8500_fg structure
1998 * Returns IRQ status(IRQ_HANDLED)
2000 static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di)
2002 struct ab8500_fg *di = _di;
2003 if (!di->nbr_cceoc_irq_cnt) {
2004 di->nbr_cceoc_irq_cnt++;
2005 complete(&di->ab8500_fg_started);
2007 di->nbr_cceoc_irq_cnt = 0;
2008 complete(&di->ab8500_fg_complete);
2014 * ab8500_fg_cc_int_calib_handler () - end of calibration isr.
2015 * @irq: interrupt number
2016 * @_di: pointer to the ab8500_fg structure
2018 * Returns IRQ status(IRQ_HANDLED)
2020 static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di)
2022 struct ab8500_fg *di = _di;
2023 di->calib_state = AB8500_FG_CALIB_END;
2024 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2029 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
2030 * @irq: interrupt number
2031 * @_di: pointer to the ab8500_fg structure
2033 * Returns IRQ status(IRQ_HANDLED)
2035 static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di)
2037 struct ab8500_fg *di = _di;
2039 queue_work(di->fg_wq, &di->fg_acc_cur_work);
2045 * ab8500_fg_batt_ovv_handler() - Battery OVV occured
2046 * @irq: interrupt number
2047 * @_di: pointer to the ab8500_fg structure
2049 * Returns IRQ status(IRQ_HANDLED)
2051 static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di)
2053 struct ab8500_fg *di = _di;
2055 dev_dbg(di->dev, "Battery OVV\n");
2057 /* Schedule a new HW failure check */
2058 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0);
2064 * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
2065 * @irq: interrupt number
2066 * @_di: pointer to the ab8500_fg structure
2068 * Returns IRQ status(IRQ_HANDLED)
2070 static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di)
2072 struct ab8500_fg *di = _di;
2074 /* Initiate handling in ab8500_fg_low_bat_work() if not already initiated. */
2075 if (!di->flags.low_bat_delay) {
2076 dev_warn(di->dev, "Battery voltage is below LOW threshold\n");
2077 di->flags.low_bat_delay = true;
2079 * Start a timer to check LOW_BAT again after some time
2080 * This is done to avoid shutdown on single voltage dips
2082 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
2083 round_jiffies(LOW_BAT_CHECK_INTERVAL));
2089 * ab8500_fg_get_property() - get the fg properties
2090 * @psy: pointer to the power_supply structure
2091 * @psp: pointer to the power_supply_property structure
2092 * @val: pointer to the power_supply_propval union
2094 * This function gets called when an application tries to get the
2095 * fg properties by reading the sysfs files.
2096 * voltage_now: battery voltage
2097 * current_now: battery instant current
2098 * current_avg: battery average current
2099 * charge_full_design: capacity where battery is considered full
2100 * charge_now: battery capacity in nAh
2101 * capacity: capacity in percent
2102 * capacity_level: capacity level
2104 * Returns error code in case of failure else 0 on success
2106 static int ab8500_fg_get_property(struct power_supply *psy,
2107 enum power_supply_property psp,
2108 union power_supply_propval *val)
2110 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2113 * If battery is identified as unknown and charging of unknown
2114 * batteries is disabled, we always report 100% capacity and
2115 * capacity level UNKNOWN, since we can't calculate
2116 * remaining capacity
2120 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2121 if (di->flags.bat_ovv)
2122 val->intval = BATT_OVV_VALUE;
2124 val->intval = di->vbat_uv;
2126 case POWER_SUPPLY_PROP_CURRENT_NOW:
2127 val->intval = di->inst_curr_ua;
2129 case POWER_SUPPLY_PROP_CURRENT_AVG:
2130 val->intval = di->avg_curr_ua;
2132 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
2133 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2134 di->bat_cap.max_mah_design);
2136 case POWER_SUPPLY_PROP_ENERGY_FULL:
2137 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2138 di->bat_cap.max_mah);
2140 case POWER_SUPPLY_PROP_ENERGY_NOW:
2141 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2142 di->flags.batt_id_received)
2143 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2144 di->bat_cap.max_mah);
2146 val->intval = ab8500_fg_convert_mah_to_uwh(di,
2147 di->bat_cap.prev_mah);
2149 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
2150 val->intval = di->bat_cap.max_mah_design;
2152 case POWER_SUPPLY_PROP_CHARGE_FULL:
2153 val->intval = di->bat_cap.max_mah;
2155 case POWER_SUPPLY_PROP_CHARGE_NOW:
2156 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2157 di->flags.batt_id_received)
2158 val->intval = di->bat_cap.max_mah;
2160 val->intval = di->bat_cap.prev_mah;
2162 case POWER_SUPPLY_PROP_CAPACITY:
2163 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2164 di->flags.batt_id_received)
2167 val->intval = di->bat_cap.prev_percent;
2169 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
2170 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2171 di->flags.batt_id_received)
2172 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
2174 val->intval = di->bat_cap.prev_level;
2182 static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
2184 struct power_supply *psy;
2185 struct power_supply *ext = dev_get_drvdata(dev);
2186 const char **supplicants = (const char **)ext->supplied_to;
2187 struct ab8500_fg *di;
2188 struct power_supply_battery_info *bi;
2189 union power_supply_propval ret;
2192 psy = (struct power_supply *)data;
2193 di = power_supply_get_drvdata(psy);
2197 * For all psy where the name of your driver
2198 * appears in any supplied_to
2200 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
2204 /* Go through all properties for the psy */
2205 for (j = 0; j < ext->desc->num_properties; j++) {
2206 enum power_supply_property prop;
2207 prop = ext->desc->properties[j];
2209 if (power_supply_get_property(ext, prop, &ret))
2213 case POWER_SUPPLY_PROP_STATUS:
2214 switch (ext->desc->type) {
2215 case POWER_SUPPLY_TYPE_BATTERY:
2216 switch (ret.intval) {
2217 case POWER_SUPPLY_STATUS_UNKNOWN:
2218 case POWER_SUPPLY_STATUS_DISCHARGING:
2219 case POWER_SUPPLY_STATUS_NOT_CHARGING:
2220 if (!di->flags.charging)
2222 di->flags.charging = false;
2223 di->flags.fully_charged = false;
2224 if (di->bm->capacity_scaling)
2225 ab8500_fg_update_cap_scalers(di);
2226 queue_work(di->fg_wq, &di->fg_work);
2228 case POWER_SUPPLY_STATUS_FULL:
2229 if (di->flags.fully_charged)
2231 di->flags.fully_charged = true;
2232 di->flags.force_full = true;
2233 /* Save current capacity as maximum */
2234 di->bat_cap.max_mah = di->bat_cap.mah;
2235 queue_work(di->fg_wq, &di->fg_work);
2237 case POWER_SUPPLY_STATUS_CHARGING:
2238 if (di->flags.charging &&
2239 !di->flags.fully_charged)
2241 di->flags.charging = true;
2242 di->flags.fully_charged = false;
2243 if (di->bm->capacity_scaling)
2244 ab8500_fg_update_cap_scalers(di);
2245 queue_work(di->fg_wq, &di->fg_work);
2253 case POWER_SUPPLY_PROP_TECHNOLOGY:
2254 switch (ext->desc->type) {
2255 case POWER_SUPPLY_TYPE_BATTERY:
2256 if (!di->flags.batt_id_received &&
2257 (bi && (bi->technology !=
2258 POWER_SUPPLY_TECHNOLOGY_UNKNOWN))) {
2259 di->flags.batt_id_received = true;
2261 di->bat_cap.max_mah_design =
2262 di->bm->bi->charge_full_design_uah;
2264 di->bat_cap.max_mah =
2265 di->bat_cap.max_mah_design;
2268 di->bm->bi->voltage_max_design_uv;
2272 di->flags.batt_unknown = false;
2274 di->flags.batt_unknown = true;
2280 case POWER_SUPPLY_PROP_TEMP:
2281 switch (ext->desc->type) {
2282 case POWER_SUPPLY_TYPE_BATTERY:
2283 if (di->flags.batt_id_received)
2284 di->bat_temp = ret.intval;
2298 * ab8500_fg_init_hw_registers() - Set up FG related registers
2299 * @di: pointer to the ab8500_fg structure
2301 * Set up battery OVV, low battery voltage registers
2303 static int ab8500_fg_init_hw_registers(struct ab8500_fg *di)
2308 * Set VBAT OVV (overvoltage) threshold to 4.75V (typ) this is what
2309 * the hardware supports, nothing else can be configured in hardware.
2310 * See this as an "outer limit" where the charger will certainly
2311 * shut down. Other (lower) overvoltage levels need to be implemented
2314 ret = abx500_mask_and_set_register_interruptible(di->dev,
2320 dev_err(di->dev, "failed to set BATT_OVV\n");
2324 /* Enable VBAT OVV detection */
2325 ret = abx500_mask_and_set_register_interruptible(di->dev,
2331 dev_err(di->dev, "failed to enable BATT_OVV\n");
2335 /* Low Battery Voltage */
2336 ret = abx500_set_register_interruptible(di->dev,
2337 AB8500_SYS_CTRL2_BLOCK,
2339 ab8500_volt_to_regval(
2340 di->bm->fg_params->lowbat_threshold_uv) << 1 |
2343 dev_err(di->dev, "%s write failed\n", __func__);
2347 /* Battery OK threshold */
2348 ret = ab8500_fg_battok_init_hw_register(di);
2350 dev_err(di->dev, "BattOk init write failed.\n");
2354 if (is_ab8505(di->parent)) {
2355 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2356 AB8505_RTC_PCUT_MAX_TIME_REG, di->bm->fg_params->pcut_max_time);
2359 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_MAX_TIME_REG\n", __func__);
2363 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2364 AB8505_RTC_PCUT_FLAG_TIME_REG, di->bm->fg_params->pcut_flag_time);
2367 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_FLAG_TIME_REG\n", __func__);
2371 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2372 AB8505_RTC_PCUT_RESTART_REG, di->bm->fg_params->pcut_max_restart);
2375 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_RESTART_REG\n", __func__);
2379 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2380 AB8505_RTC_PCUT_DEBOUNCE_REG, di->bm->fg_params->pcut_debounce_time);
2383 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_DEBOUNCE_REG\n", __func__);
2387 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2388 AB8505_RTC_PCUT_CTL_STATUS_REG, di->bm->fg_params->pcut_enable);
2391 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_CTL_STATUS_REG\n", __func__);
2400 * ab8500_fg_external_power_changed() - callback for power supply changes
2401 * @psy: pointer to the structure power_supply
2403 * This function is the entry point of the pointer external_power_changed
2404 * of the structure power_supply.
2405 * This function gets executed when there is a change in any external power
2406 * supply that this driver needs to be notified of.
2408 static void ab8500_fg_external_power_changed(struct power_supply *psy)
2410 class_for_each_device(power_supply_class, NULL, psy,
2411 ab8500_fg_get_ext_psy_data);
2415 * ab8500_fg_reinit_work() - work to reset the FG algorithm
2416 * @work: pointer to the work_struct structure
2418 * Used to reset the current battery capacity to be able to
2419 * retrigger a new voltage base capacity calculation. For
2420 * test and verification purpose.
2422 static void ab8500_fg_reinit_work(struct work_struct *work)
2424 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
2425 fg_reinit_work.work);
2427 if (!di->flags.calibrate) {
2428 dev_dbg(di->dev, "Resetting FG state machine to init.\n");
2429 ab8500_fg_clear_cap_samples(di);
2430 ab8500_fg_calc_cap_discharge_voltage(di);
2431 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
2432 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
2433 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2436 dev_err(di->dev, "Residual offset calibration ongoing "
2438 /* Wait one second until next try*/
2439 queue_delayed_work(di->fg_wq, &di->fg_reinit_work,
2444 /* Exposure to the sysfs interface */
2446 struct ab8500_fg_sysfs_entry {
2447 struct attribute attr;
2448 ssize_t (*show)(struct ab8500_fg *, char *);
2449 ssize_t (*store)(struct ab8500_fg *, const char *, size_t);
2452 static ssize_t charge_full_show(struct ab8500_fg *di, char *buf)
2454 return sysfs_emit(buf, "%d\n", di->bat_cap.max_mah);
2457 static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf,
2460 unsigned long charge_full;
2463 ret = kstrtoul(buf, 10, &charge_full);
2467 di->bat_cap.max_mah = (int) charge_full;
2471 static ssize_t charge_now_show(struct ab8500_fg *di, char *buf)
2473 return sysfs_emit(buf, "%d\n", di->bat_cap.prev_mah);
2476 static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf,
2479 unsigned long charge_now;
2482 ret = kstrtoul(buf, 10, &charge_now);
2486 di->bat_cap.user_mah = (int) charge_now;
2487 di->flags.user_cap = true;
2488 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2492 static struct ab8500_fg_sysfs_entry charge_full_attr =
2493 __ATTR(charge_full, 0644, charge_full_show, charge_full_store);
2495 static struct ab8500_fg_sysfs_entry charge_now_attr =
2496 __ATTR(charge_now, 0644, charge_now_show, charge_now_store);
2499 ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf)
2501 struct ab8500_fg_sysfs_entry *entry;
2502 struct ab8500_fg *di;
2504 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2505 di = container_of(kobj, struct ab8500_fg, fg_kobject);
2510 return entry->show(di, buf);
2513 ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf,
2516 struct ab8500_fg_sysfs_entry *entry;
2517 struct ab8500_fg *di;
2519 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2520 di = container_of(kobj, struct ab8500_fg, fg_kobject);
2525 return entry->store(di, buf, count);
2528 static const struct sysfs_ops ab8500_fg_sysfs_ops = {
2529 .show = ab8500_fg_show,
2530 .store = ab8500_fg_store,
2533 static struct attribute *ab8500_fg_attrs[] = {
2534 &charge_full_attr.attr,
2535 &charge_now_attr.attr,
2538 ATTRIBUTE_GROUPS(ab8500_fg);
2540 static struct kobj_type ab8500_fg_ktype = {
2541 .sysfs_ops = &ab8500_fg_sysfs_ops,
2542 .default_groups = ab8500_fg_groups,
2546 * ab8500_fg_sysfs_exit() - de-init of sysfs entry
2547 * @di: pointer to the struct ab8500_chargalg
2549 * This function removes the entry in sysfs.
2551 static void ab8500_fg_sysfs_exit(struct ab8500_fg *di)
2553 kobject_del(&di->fg_kobject);
2557 * ab8500_fg_sysfs_init() - init of sysfs entry
2558 * @di: pointer to the struct ab8500_chargalg
2560 * This function adds an entry in sysfs.
2561 * Returns error code in case of failure else 0(on success)
2563 static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
2567 ret = kobject_init_and_add(&di->fg_kobject,
2571 kobject_put(&di->fg_kobject);
2572 dev_err(di->dev, "failed to create sysfs entry\n");
2578 static ssize_t ab8505_powercut_flagtime_read(struct device *dev,
2579 struct device_attribute *attr,
2584 struct power_supply *psy = dev_get_drvdata(dev);
2585 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2587 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2588 AB8505_RTC_PCUT_FLAG_TIME_REG, ®_value);
2591 dev_err(dev, "Failed to read AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2595 return sysfs_emit(buf, "%d\n", (reg_value & 0x7F));
2601 static ssize_t ab8505_powercut_flagtime_write(struct device *dev,
2602 struct device_attribute *attr,
2603 const char *buf, size_t count)
2607 struct power_supply *psy = dev_get_drvdata(dev);
2608 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2610 if (kstrtoint(buf, 10, ®_value))
2613 if (reg_value > 0x7F) {
2614 dev_err(dev, "Incorrect parameter, echo 0 (1.98s) - 127 (15.625ms) for flagtime\n");
2618 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2619 AB8505_RTC_PCUT_FLAG_TIME_REG, (u8)reg_value);
2622 dev_err(dev, "Failed to set AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2628 static ssize_t ab8505_powercut_maxtime_read(struct device *dev,
2629 struct device_attribute *attr,
2634 struct power_supply *psy = dev_get_drvdata(dev);
2635 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2637 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2638 AB8505_RTC_PCUT_MAX_TIME_REG, ®_value);
2641 dev_err(dev, "Failed to read AB8505_RTC_PCUT_MAX_TIME_REG\n");
2645 return sysfs_emit(buf, "%d\n", (reg_value & 0x7F));
2652 static ssize_t ab8505_powercut_maxtime_write(struct device *dev,
2653 struct device_attribute *attr,
2654 const char *buf, size_t count)
2658 struct power_supply *psy = dev_get_drvdata(dev);
2659 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2661 if (kstrtoint(buf, 10, ®_value))
2664 if (reg_value > 0x7F) {
2665 dev_err(dev, "Incorrect parameter, echo 0 (0.0s) - 127 (1.98s) for maxtime\n");
2669 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2670 AB8505_RTC_PCUT_MAX_TIME_REG, (u8)reg_value);
2673 dev_err(dev, "Failed to set AB8505_RTC_PCUT_MAX_TIME_REG\n");
2679 static ssize_t ab8505_powercut_restart_read(struct device *dev,
2680 struct device_attribute *attr,
2685 struct power_supply *psy = dev_get_drvdata(dev);
2686 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2688 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2689 AB8505_RTC_PCUT_RESTART_REG, ®_value);
2692 dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2696 return sysfs_emit(buf, "%d\n", (reg_value & 0xF));
2702 static ssize_t ab8505_powercut_restart_write(struct device *dev,
2703 struct device_attribute *attr,
2704 const char *buf, size_t count)
2708 struct power_supply *psy = dev_get_drvdata(dev);
2709 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2711 if (kstrtoint(buf, 10, ®_value))
2714 if (reg_value > 0xF) {
2715 dev_err(dev, "Incorrect parameter, echo 0 - 15 for number of restart\n");
2719 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2720 AB8505_RTC_PCUT_RESTART_REG, (u8)reg_value);
2723 dev_err(dev, "Failed to set AB8505_RTC_PCUT_RESTART_REG\n");
2730 static ssize_t ab8505_powercut_timer_read(struct device *dev,
2731 struct device_attribute *attr,
2736 struct power_supply *psy = dev_get_drvdata(dev);
2737 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2739 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2740 AB8505_RTC_PCUT_TIME_REG, ®_value);
2743 dev_err(dev, "Failed to read AB8505_RTC_PCUT_TIME_REG\n");
2747 return sysfs_emit(buf, "%d\n", (reg_value & 0x7F));
2753 static ssize_t ab8505_powercut_restart_counter_read(struct device *dev,
2754 struct device_attribute *attr,
2759 struct power_supply *psy = dev_get_drvdata(dev);
2760 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2762 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2763 AB8505_RTC_PCUT_RESTART_REG, ®_value);
2766 dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2770 return sysfs_emit(buf, "%d\n", (reg_value & 0xF0) >> 4);
2776 static ssize_t ab8505_powercut_read(struct device *dev,
2777 struct device_attribute *attr,
2782 struct power_supply *psy = dev_get_drvdata(dev);
2783 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2785 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2786 AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value);
2791 return sysfs_emit(buf, "%d\n", (reg_value & 0x1));
2797 static ssize_t ab8505_powercut_write(struct device *dev,
2798 struct device_attribute *attr,
2799 const char *buf, size_t count)
2803 struct power_supply *psy = dev_get_drvdata(dev);
2804 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2806 if (kstrtoint(buf, 10, ®_value))
2809 if (reg_value > 0x1) {
2810 dev_err(dev, "Incorrect parameter, echo 0/1 to disable/enable Pcut feature\n");
2814 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2815 AB8505_RTC_PCUT_CTL_STATUS_REG, (u8)reg_value);
2818 dev_err(dev, "Failed to set AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2824 static ssize_t ab8505_powercut_flag_read(struct device *dev,
2825 struct device_attribute *attr,
2831 struct power_supply *psy = dev_get_drvdata(dev);
2832 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2834 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2835 AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value);
2838 dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2842 return sysfs_emit(buf, "%d\n", ((reg_value & 0x10) >> 4));
2848 static ssize_t ab8505_powercut_debounce_read(struct device *dev,
2849 struct device_attribute *attr,
2854 struct power_supply *psy = dev_get_drvdata(dev);
2855 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2857 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2858 AB8505_RTC_PCUT_DEBOUNCE_REG, ®_value);
2861 dev_err(dev, "Failed to read AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2865 return sysfs_emit(buf, "%d\n", (reg_value & 0x7));
2871 static ssize_t ab8505_powercut_debounce_write(struct device *dev,
2872 struct device_attribute *attr,
2873 const char *buf, size_t count)
2877 struct power_supply *psy = dev_get_drvdata(dev);
2878 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2880 if (kstrtoint(buf, 10, ®_value))
2883 if (reg_value > 0x7) {
2884 dev_err(dev, "Incorrect parameter, echo 0 to 7 for debounce setting\n");
2888 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2889 AB8505_RTC_PCUT_DEBOUNCE_REG, (u8)reg_value);
2892 dev_err(dev, "Failed to set AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2898 static ssize_t ab8505_powercut_enable_status_read(struct device *dev,
2899 struct device_attribute *attr,
2904 struct power_supply *psy = dev_get_drvdata(dev);
2905 struct ab8500_fg *di = power_supply_get_drvdata(psy);
2907 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2908 AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value);
2911 dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2915 return sysfs_emit(buf, "%d\n", ((reg_value & 0x20) >> 5));
2921 static struct device_attribute ab8505_fg_sysfs_psy_attrs[] = {
2922 __ATTR(powercut_flagtime, (S_IRUGO | S_IWUSR | S_IWGRP),
2923 ab8505_powercut_flagtime_read, ab8505_powercut_flagtime_write),
2924 __ATTR(powercut_maxtime, (S_IRUGO | S_IWUSR | S_IWGRP),
2925 ab8505_powercut_maxtime_read, ab8505_powercut_maxtime_write),
2926 __ATTR(powercut_restart_max, (S_IRUGO | S_IWUSR | S_IWGRP),
2927 ab8505_powercut_restart_read, ab8505_powercut_restart_write),
2928 __ATTR(powercut_timer, S_IRUGO, ab8505_powercut_timer_read, NULL),
2929 __ATTR(powercut_restart_counter, S_IRUGO,
2930 ab8505_powercut_restart_counter_read, NULL),
2931 __ATTR(powercut_enable, (S_IRUGO | S_IWUSR | S_IWGRP),
2932 ab8505_powercut_read, ab8505_powercut_write),
2933 __ATTR(powercut_flag, S_IRUGO, ab8505_powercut_flag_read, NULL),
2934 __ATTR(powercut_debounce_time, (S_IRUGO | S_IWUSR | S_IWGRP),
2935 ab8505_powercut_debounce_read, ab8505_powercut_debounce_write),
2936 __ATTR(powercut_enable_status, S_IRUGO,
2937 ab8505_powercut_enable_status_read, NULL),
2940 static int ab8500_fg_sysfs_psy_create_attrs(struct ab8500_fg *di)
2944 if (is_ab8505(di->parent)) {
2945 for (i = 0; i < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); i++)
2946 if (device_create_file(&di->fg_psy->dev,
2947 &ab8505_fg_sysfs_psy_attrs[i]))
2948 goto sysfs_psy_create_attrs_failed_ab8505;
2951 sysfs_psy_create_attrs_failed_ab8505:
2952 dev_err(&di->fg_psy->dev, "Failed creating sysfs psy attrs for ab8505.\n");
2954 device_remove_file(&di->fg_psy->dev,
2955 &ab8505_fg_sysfs_psy_attrs[i]);
2960 static void ab8500_fg_sysfs_psy_remove_attrs(struct ab8500_fg *di)
2964 if (is_ab8505(di->parent)) {
2965 for (i = 0; i < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); i++)
2966 (void)device_remove_file(&di->fg_psy->dev,
2967 &ab8505_fg_sysfs_psy_attrs[i]);
2971 /* Exposure to the sysfs interface <<END>> */
2973 static int __maybe_unused ab8500_fg_resume(struct device *dev)
2975 struct ab8500_fg *di = dev_get_drvdata(dev);
2978 * Change state if we're not charging. If we're charging we will wake
2981 if (!di->flags.charging) {
2982 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP);
2983 queue_work(di->fg_wq, &di->fg_work);
2989 static int __maybe_unused ab8500_fg_suspend(struct device *dev)
2991 struct ab8500_fg *di = dev_get_drvdata(dev);
2993 flush_delayed_work(&di->fg_periodic_work);
2994 flush_work(&di->fg_work);
2995 flush_work(&di->fg_acc_cur_work);
2996 flush_delayed_work(&di->fg_reinit_work);
2997 flush_delayed_work(&di->fg_low_bat_work);
2998 flush_delayed_work(&di->fg_check_hw_failure_work);
3001 * If the FG is enabled we will disable it before going to suspend
3002 * only if we're not charging
3004 if (di->flags.fg_enabled && !di->flags.charging)
3005 ab8500_fg_coulomb_counter(di, false);
3010 /* ab8500 fg driver interrupts and their respective isr */
3011 static struct ab8500_fg_interrupts ab8500_fg_irq[] = {
3012 {"NCONV_ACCU", ab8500_fg_cc_convend_handler},
3013 {"BATT_OVV", ab8500_fg_batt_ovv_handler},
3014 {"LOW_BAT_F", ab8500_fg_lowbatf_handler},
3015 {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler},
3016 {"CCEOC", ab8500_fg_cc_data_end_handler},
3019 static char *supply_interface[] = {
3024 static const struct power_supply_desc ab8500_fg_desc = {
3025 .name = "ab8500_fg",
3026 .type = POWER_SUPPLY_TYPE_BATTERY,
3027 .properties = ab8500_fg_props,
3028 .num_properties = ARRAY_SIZE(ab8500_fg_props),
3029 .get_property = ab8500_fg_get_property,
3030 .external_power_changed = ab8500_fg_external_power_changed,
3033 static int ab8500_fg_bind(struct device *dev, struct device *master,
3036 struct ab8500_fg *di = dev_get_drvdata(dev);
3038 di->bat_cap.max_mah_design = di->bm->bi->charge_full_design_uah;
3039 di->bat_cap.max_mah = di->bat_cap.max_mah_design;
3040 di->vbat_nom_uv = di->bm->bi->voltage_max_design_uv;
3042 /* Start the coulomb counter */
3043 ab8500_fg_coulomb_counter(di, true);
3044 /* Run the FG algorithm */
3045 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
3050 static void ab8500_fg_unbind(struct device *dev, struct device *master,
3053 struct ab8500_fg *di = dev_get_drvdata(dev);
3056 /* Disable coulomb counter */
3057 ret = ab8500_fg_coulomb_counter(di, false);
3059 dev_err(dev, "failed to disable coulomb counter\n");
3061 flush_workqueue(di->fg_wq);
3064 static const struct component_ops ab8500_fg_component_ops = {
3065 .bind = ab8500_fg_bind,
3066 .unbind = ab8500_fg_unbind,
3069 static int ab8500_fg_probe(struct platform_device *pdev)
3071 struct device *dev = &pdev->dev;
3072 struct power_supply_config psy_cfg = {};
3073 struct ab8500_fg *di;
3077 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
3081 di->bm = &ab8500_bm_data;
3083 mutex_init(&di->cc_lock);
3085 /* get parent data */
3087 di->parent = dev_get_drvdata(pdev->dev.parent);
3089 di->main_bat_v = devm_iio_channel_get(dev, "main_bat_v");
3090 if (IS_ERR(di->main_bat_v)) {
3091 ret = dev_err_probe(dev, PTR_ERR(di->main_bat_v),
3092 "failed to get main battery ADC channel\n");
3096 if (!of_property_read_u32(dev->of_node, "line-impedance-micro-ohms",
3097 &di->line_impedance_uohm))
3098 dev_info(dev, "line impedance: %u uOhm\n",
3099 di->line_impedance_uohm);
3101 psy_cfg.supplied_to = supply_interface;
3102 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3103 psy_cfg.drv_data = di;
3105 di->init_capacity = true;
3107 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
3108 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
3110 /* Create a work queue for running the FG algorithm */
3111 di->fg_wq = alloc_ordered_workqueue("ab8500_fg_wq", WQ_MEM_RECLAIM);
3112 if (di->fg_wq == NULL) {
3113 dev_err(dev, "failed to create work queue\n");
3117 /* Init work for running the fg algorithm instantly */
3118 INIT_WORK(&di->fg_work, ab8500_fg_instant_work);
3120 /* Init work for getting the battery accumulated current */
3121 INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work);
3123 /* Init work for reinitialising the fg algorithm */
3124 INIT_DEFERRABLE_WORK(&di->fg_reinit_work,
3125 ab8500_fg_reinit_work);
3127 /* Work delayed Queue to run the state machine */
3128 INIT_DEFERRABLE_WORK(&di->fg_periodic_work,
3129 ab8500_fg_periodic_work);
3131 /* Work to check low battery condition */
3132 INIT_DEFERRABLE_WORK(&di->fg_low_bat_work,
3133 ab8500_fg_low_bat_work);
3135 /* Init work for HW failure check */
3136 INIT_DEFERRABLE_WORK(&di->fg_check_hw_failure_work,
3137 ab8500_fg_check_hw_failure_work);
3139 /* Reset battery low voltage flag */
3140 di->flags.low_bat = false;
3142 /* Initialize low battery counter */
3143 di->low_bat_cnt = 10;
3145 /* Initialize OVV, and other registers */
3146 ret = ab8500_fg_init_hw_registers(di);
3148 dev_err(dev, "failed to initialize registers\n");
3149 destroy_workqueue(di->fg_wq);
3153 /* Consider battery unknown until we're informed otherwise */
3154 di->flags.batt_unknown = true;
3155 di->flags.batt_id_received = false;
3157 /* Register FG power supply class */
3158 di->fg_psy = devm_power_supply_register(dev, &ab8500_fg_desc, &psy_cfg);
3159 if (IS_ERR(di->fg_psy)) {
3160 dev_err(dev, "failed to register FG psy\n");
3161 destroy_workqueue(di->fg_wq);
3162 return PTR_ERR(di->fg_psy);
3165 di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
3168 * Initialize completion used to notify completion and start
3171 init_completion(&di->ab8500_fg_started);
3172 init_completion(&di->ab8500_fg_complete);
3174 /* Register primary interrupt handlers */
3175 for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq); i++) {
3176 irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
3178 destroy_workqueue(di->fg_wq);
3182 ret = devm_request_threaded_irq(dev, irq, NULL,
3183 ab8500_fg_irq[i].isr,
3184 IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
3185 ab8500_fg_irq[i].name, di);
3188 dev_err(dev, "failed to request %s IRQ %d: %d\n",
3189 ab8500_fg_irq[i].name, irq, ret);
3190 destroy_workqueue(di->fg_wq);
3193 dev_dbg(dev, "Requested %s IRQ %d: %d\n",
3194 ab8500_fg_irq[i].name, irq, ret);
3197 di->irq = platform_get_irq_byname(pdev, "CCEOC");
3198 disable_irq(di->irq);
3199 di->nbr_cceoc_irq_cnt = 0;
3201 platform_set_drvdata(pdev, di);
3203 ret = ab8500_fg_sysfs_init(di);
3205 dev_err(dev, "failed to create sysfs entry\n");
3206 destroy_workqueue(di->fg_wq);
3210 ret = ab8500_fg_sysfs_psy_create_attrs(di);
3212 dev_err(dev, "failed to create FG psy\n");
3213 ab8500_fg_sysfs_exit(di);
3214 destroy_workqueue(di->fg_wq);
3218 /* Calibrate the fg first time */
3219 di->flags.calibrate = true;
3220 di->calib_state = AB8500_FG_CALIB_INIT;
3222 /* Use room temp as default value until we get an update from driver. */
3225 list_add_tail(&di->node, &ab8500_fg_list);
3227 return component_add(dev, &ab8500_fg_component_ops);
3230 static void ab8500_fg_remove(struct platform_device *pdev)
3232 struct ab8500_fg *di = platform_get_drvdata(pdev);
3234 destroy_workqueue(di->fg_wq);
3235 component_del(&pdev->dev, &ab8500_fg_component_ops);
3236 list_del(&di->node);
3237 ab8500_fg_sysfs_exit(di);
3238 ab8500_fg_sysfs_psy_remove_attrs(di);
3241 static SIMPLE_DEV_PM_OPS(ab8500_fg_pm_ops, ab8500_fg_suspend, ab8500_fg_resume);
3243 static const struct of_device_id ab8500_fg_match[] = {
3244 { .compatible = "stericsson,ab8500-fg", },
3247 MODULE_DEVICE_TABLE(of, ab8500_fg_match);
3249 struct platform_driver ab8500_fg_driver = {
3250 .probe = ab8500_fg_probe,
3251 .remove_new = ab8500_fg_remove,
3253 .name = "ab8500-fg",
3254 .of_match_table = ab8500_fg_match,
3255 .pm = &ab8500_fg_pm_ops,
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");