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/mfd/abx500/ab8500.h>
26 #include <linux/mfd/abx500.h>
27 #include <linux/slab.h>
28 #include <linux/mfd/abx500/ab8500-bm.h>
29 #include <linux/delay.h>
30 #include <linux/mfd/abx500/ab8500-gpadc.h>
31 #include <linux/mfd/abx500.h>
32 #include <linux/time.h>
33 #include <linux/completion.h>
35 #define MILLI_TO_MICRO 1000
36 #define FG_LSB_IN_MA 1627
37 #define QLSB_NANO_AMP_HOURS_X10 1129
38 #define INS_CURR_TIMEOUT (3 * HZ)
40 #define SEC_TO_SAMPLE(S) (S * 4)
42 #define NBR_AVG_SAMPLES 20
44 #define LOW_BAT_CHECK_INTERVAL (2 * HZ)
46 #define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */
47 #define BATT_OK_MIN 2360 /* mV */
48 #define BATT_OK_INCREMENT 50 /* mV */
49 #define BATT_OK_MAX_NR_INCREMENTS 0xE
54 #define interpolate(x, x1, y1, x2, y2) \
55 ((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1))));
57 #define to_ab8500_fg_device_info(x) container_of((x), \
58 struct ab8500_fg, fg_psy);
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 __kernel_time_t time_stamps[NBR_AVG_SAMPLES];
115 struct ab8500_fg_battery_capacity {
127 struct ab8500_fg_flags {
139 bool batt_id_received;
142 struct inst_curr_result_list {
143 struct list_head list;
148 * struct ab8500_fg - ab8500 FG device information
149 * @dev: Pointer to the structure device
150 * @node: a list of AB8500 FGs, hence prepared for reentrance
151 * @irq holds the CCEOC interrupt number
152 * @vbat: Battery voltage in mV
153 * @vbat_nom: Nominal battery voltage in mV
154 * @inst_curr: Instantenous battery current in mA
155 * @avg_curr: Average battery current in mA
156 * @bat_temp battery temperature
157 * @fg_samples: Number of samples used in the FG accumulation
158 * @accu_charge: Accumulated charge from the last conversion
159 * @recovery_cnt: Counter for recovery mode
160 * @high_curr_cnt: Counter for high current mode
161 * @init_cnt: Counter for init mode
162 * @recovery_needed: Indicate if recovery is needed
163 * @high_curr_mode: Indicate if we're in high current mode
164 * @init_capacity: Indicate if initial capacity measuring should be done
165 * @turn_off_fg: True if fg was off before current measurement
166 * @calib_state State during offset calibration
167 * @discharge_state: Current discharge state
168 * @charge_state: Current charge state
169 * @ab8500_fg_complete Completion struct used for the instant current reading
170 * @flags: Structure for information about events triggered
171 * @bat_cap: Structure for battery capacity specific parameters
172 * @avg_cap: Average capacity filter
173 * @parent: Pointer to the struct ab8500
174 * @gpadc: Pointer to the struct gpadc
175 * @pdata: Pointer to the abx500_fg platform data
176 * @bat: Pointer to the abx500_bm platform data
177 * @fg_psy: Structure that holds the FG specific battery properties
178 * @fg_wq: Work queue for running the FG algorithm
179 * @fg_periodic_work: Work to run the FG algorithm periodically
180 * @fg_low_bat_work: Work to check low bat condition
181 * @fg_reinit_work Work used to reset and reinitialise the FG algorithm
182 * @fg_work: Work to run the FG algorithm instantly
183 * @fg_acc_cur_work: Work to read the FG accumulator
184 * @fg_check_hw_failure_work: Work for checking HW state
185 * @cc_lock: Mutex for locking the CC
186 * @fg_kobject: Structure of type kobject
190 struct list_head node;
202 bool recovery_needed;
206 enum ab8500_fg_calibration_state calib_state;
207 enum ab8500_fg_discharge_state discharge_state;
208 enum ab8500_fg_charge_state charge_state;
209 struct completion ab8500_fg_complete;
210 struct ab8500_fg_flags flags;
211 struct ab8500_fg_battery_capacity bat_cap;
212 struct ab8500_fg_avg_cap avg_cap;
213 struct ab8500 *parent;
214 struct ab8500_gpadc *gpadc;
215 struct abx500_fg_platform_data *pdata;
216 struct abx500_bm_data *bat;
217 struct power_supply fg_psy;
218 struct workqueue_struct *fg_wq;
219 struct delayed_work fg_periodic_work;
220 struct delayed_work fg_low_bat_work;
221 struct delayed_work fg_reinit_work;
222 struct work_struct fg_work;
223 struct work_struct fg_acc_cur_work;
224 struct delayed_work fg_check_hw_failure_work;
225 struct mutex cc_lock;
226 struct kobject fg_kobject;
228 static LIST_HEAD(ab8500_fg_list);
231 * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
232 * (i.e. the first fuel gauge in the instance list)
234 struct ab8500_fg *ab8500_fg_get(void)
236 struct ab8500_fg *fg;
238 if (list_empty(&ab8500_fg_list))
241 fg = list_first_entry(&ab8500_fg_list, struct ab8500_fg, node);
245 /* Main battery properties */
246 static enum power_supply_property ab8500_fg_props[] = {
247 POWER_SUPPLY_PROP_VOLTAGE_NOW,
248 POWER_SUPPLY_PROP_CURRENT_NOW,
249 POWER_SUPPLY_PROP_CURRENT_AVG,
250 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
251 POWER_SUPPLY_PROP_ENERGY_FULL,
252 POWER_SUPPLY_PROP_ENERGY_NOW,
253 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
254 POWER_SUPPLY_PROP_CHARGE_FULL,
255 POWER_SUPPLY_PROP_CHARGE_NOW,
256 POWER_SUPPLY_PROP_CAPACITY,
257 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
261 * This array maps the raw hex value to lowbat voltage used by the AB8500
262 * Values taken from the UM0836
264 static int ab8500_fg_lowbat_voltage_map[] = {
331 static u8 ab8500_volt_to_regval(int voltage)
335 if (voltage < ab8500_fg_lowbat_voltage_map[0])
338 for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) {
339 if (voltage < ab8500_fg_lowbat_voltage_map[i])
343 /* If not captured above, return index of last element */
344 return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1;
348 * ab8500_fg_is_low_curr() - Low or high current mode
349 * @di: pointer to the ab8500_fg structure
350 * @curr: the current to base or our decision on
352 * Low current mode if the current consumption is below a certain threshold
354 static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr)
357 * We want to know if we're in low current mode
359 if (curr > -di->bat->fg_params->high_curr_threshold)
366 * ab8500_fg_add_cap_sample() - Add capacity to average filter
367 * @di: pointer to the ab8500_fg structure
368 * @sample: the capacity in mAh to add to the filter
370 * A capacity is added to the filter and a new mean capacity is calculated and
373 static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample)
376 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
381 avg->sum += sample - avg->samples[avg->pos];
382 avg->samples[avg->pos] = sample;
383 avg->time_stamps[avg->pos] = ts.tv_sec;
386 if (avg->pos == NBR_AVG_SAMPLES)
389 if (avg->nbr_samples < NBR_AVG_SAMPLES)
393 * Check the time stamp for each sample. If too old,
394 * replace with latest sample
396 } while (ts.tv_sec - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]);
398 avg->avg = avg->sum / avg->nbr_samples;
404 * ab8500_fg_clear_cap_samples() - Clear average filter
405 * @di: pointer to the ab8500_fg structure
407 * The capacity filter is is reset to zero.
409 static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di)
412 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
415 avg->nbr_samples = 0;
419 for (i = 0; i < NBR_AVG_SAMPLES; i++) {
421 avg->time_stamps[i] = 0;
426 * ab8500_fg_fill_cap_sample() - Fill average filter
427 * @di: pointer to the ab8500_fg structure
428 * @sample: the capacity in mAh to fill the filter with
430 * The capacity filter is filled with a capacity in mAh
432 static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample)
436 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
440 for (i = 0; i < NBR_AVG_SAMPLES; i++) {
441 avg->samples[i] = sample;
442 avg->time_stamps[i] = ts.tv_sec;
446 avg->nbr_samples = NBR_AVG_SAMPLES;
447 avg->sum = sample * NBR_AVG_SAMPLES;
452 * ab8500_fg_coulomb_counter() - enable coulomb counter
453 * @di: pointer to the ab8500_fg structure
454 * @enable: enable/disable
456 * Enable/Disable coulomb counter.
457 * On failure returns negative value.
459 static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable)
462 mutex_lock(&di->cc_lock);
464 /* To be able to reprogram the number of samples, we have to
465 * first stop the CC and then enable it again */
466 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
467 AB8500_RTC_CC_CONF_REG, 0x00);
471 /* Program the samples */
472 ret = abx500_set_register_interruptible(di->dev,
473 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
479 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
480 AB8500_RTC_CC_CONF_REG,
481 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
485 di->flags.fg_enabled = true;
487 /* Clear any pending read requests */
488 ret = abx500_set_register_interruptible(di->dev,
489 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0);
493 ret = abx500_set_register_interruptible(di->dev,
494 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0);
499 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
500 AB8500_RTC_CC_CONF_REG, 0);
504 di->flags.fg_enabled = false;
507 dev_dbg(di->dev, " CC enabled: %d Samples: %d\n",
508 enable, di->fg_samples);
510 mutex_unlock(&di->cc_lock);
514 dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__);
515 mutex_unlock(&di->cc_lock);
520 * ab8500_fg_inst_curr_start() - start battery instantaneous current
521 * @di: pointer to the ab8500_fg structure
523 * Returns 0 or error code
524 * Note: This is part "one" and has to be called before
525 * ab8500_fg_inst_curr_finalize()
527 int ab8500_fg_inst_curr_start(struct ab8500_fg *di)
532 mutex_lock(&di->cc_lock);
534 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
535 AB8500_RTC_CC_CONF_REG, ®_val);
539 if (!(reg_val & CC_PWR_UP_ENA)) {
540 dev_dbg(di->dev, "%s Enable FG\n", __func__);
541 di->turn_off_fg = true;
543 /* Program the samples */
544 ret = abx500_set_register_interruptible(di->dev,
545 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
551 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
552 AB8500_RTC_CC_CONF_REG,
553 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
557 di->turn_off_fg = false;
561 INIT_COMPLETION(di->ab8500_fg_complete);
564 /* Note: cc_lock is still locked */
567 mutex_unlock(&di->cc_lock);
572 * ab8500_fg_inst_curr_done() - check if fg conversion is done
573 * @di: pointer to the ab8500_fg structure
575 * Returns 1 if conversion done, 0 if still waiting
577 int ab8500_fg_inst_curr_done(struct ab8500_fg *di)
579 return completion_done(&di->ab8500_fg_complete);
583 * ab8500_fg_inst_curr_finalize() - battery instantaneous current
584 * @di: pointer to the ab8500_fg structure
585 * @res: battery instantenous current(on success)
587 * Returns 0 or an error code
588 * Note: This is part "two" and has to be called at earliest 250 ms
589 * after ab8500_fg_inst_curr_start()
591 int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res)
598 if (!completion_done(&di->ab8500_fg_complete)) {
599 timeout = wait_for_completion_timeout(&di->ab8500_fg_complete,
601 dev_dbg(di->dev, "Finalize time: %d ms\n",
602 ((INS_CURR_TIMEOUT - timeout) * 1000) / HZ);
605 disable_irq(di->irq);
606 dev_err(di->dev, "completion timed out [%d]\n",
612 disable_irq(di->irq);
614 ret = abx500_mask_and_set_register_interruptible(di->dev,
615 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
618 /* 100uS between read request and read is needed */
619 usleep_range(100, 100);
621 /* Read CC Sample conversion value Low and high */
622 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
623 AB8500_GASG_CC_SMPL_CNVL_REG, &low);
627 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
628 AB8500_GASG_CC_SMPL_CNVH_REG, &high);
633 * negative value for Discharging
634 * convert 2's compliment into decimal
637 val = (low | (high << 8) | 0xFFFFE000);
639 val = (low | (high << 8));
642 * Convert to unit value in mA
643 * Full scale input voltage is
644 * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA
645 * Given a 250ms conversion cycle time the LSB corresponds
646 * to 112.9 nAh. Convert to current by dividing by the conversion
647 * time in hours (250ms = 1 / (3600 * 4)h)
648 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
650 val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) /
651 (1000 * di->bat->fg_res);
653 if (di->turn_off_fg) {
654 dev_dbg(di->dev, "%s Disable FG\n", __func__);
656 /* Clear any pending read requests */
657 ret = abx500_set_register_interruptible(di->dev,
658 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0);
663 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
664 AB8500_RTC_CC_CONF_REG, 0);
668 mutex_unlock(&di->cc_lock);
673 mutex_unlock(&di->cc_lock);
678 * ab8500_fg_inst_curr_blocking() - battery instantaneous current
679 * @di: pointer to the ab8500_fg structure
680 * @res: battery instantenous current(on success)
682 * Returns 0 else error code
684 int ab8500_fg_inst_curr_blocking(struct ab8500_fg *di)
689 ret = ab8500_fg_inst_curr_start(di);
691 dev_err(di->dev, "Failed to initialize fg_inst\n");
695 ret = ab8500_fg_inst_curr_finalize(di, &res);
697 dev_err(di->dev, "Failed to finalize fg_inst\n");
705 * ab8500_fg_acc_cur_work() - average battery current
706 * @work: pointer to the work_struct structure
708 * Updated the average battery current obtained from the
711 static void ab8500_fg_acc_cur_work(struct work_struct *work)
717 struct ab8500_fg *di = container_of(work,
718 struct ab8500_fg, fg_acc_cur_work);
720 mutex_lock(&di->cc_lock);
721 ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE,
722 AB8500_GASG_CC_NCOV_ACCU_CTRL, RD_NCONV_ACCU_REQ);
726 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
727 AB8500_GASG_CC_NCOV_ACCU_LOW, &low);
731 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
732 AB8500_GASG_CC_NCOV_ACCU_MED, &med);
736 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
737 AB8500_GASG_CC_NCOV_ACCU_HIGH, &high);
741 /* Check for sign bit in case of negative value, 2's compliment */
743 val = (low | (med << 8) | (high << 16) | 0xFFE00000);
745 val = (low | (med << 8) | (high << 16));
749 * Given a 250ms conversion cycle time the LSB corresponds
751 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
753 di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) /
754 (100 * di->bat->fg_res);
757 * Convert to unit value in mA
758 * Full scale input voltage is
759 * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA
760 * Given a 250ms conversion cycle time the LSB corresponds
761 * to 112.9 nAh. Convert to current by dividing by the conversion
762 * time in hours (= samples / (3600 * 4)h)
763 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
765 di->avg_curr = (val * QLSB_NANO_AMP_HOURS_X10 * 36) /
766 (1000 * di->bat->fg_res * (di->fg_samples / 4));
768 di->flags.conv_done = true;
770 mutex_unlock(&di->cc_lock);
772 queue_work(di->fg_wq, &di->fg_work);
777 "Failed to read or write gas gauge registers\n");
778 mutex_unlock(&di->cc_lock);
779 queue_work(di->fg_wq, &di->fg_work);
783 * ab8500_fg_bat_voltage() - get battery voltage
784 * @di: pointer to the ab8500_fg structure
786 * Returns battery voltage(on success) else error code
788 static int ab8500_fg_bat_voltage(struct ab8500_fg *di)
793 vbat = ab8500_gpadc_convert(di->gpadc, MAIN_BAT_V);
796 "%s gpadc conversion failed, using previous value\n",
806 * ab8500_fg_volt_to_capacity() - Voltage based capacity
807 * @di: pointer to the ab8500_fg structure
808 * @voltage: The voltage to convert to a capacity
810 * Returns battery capacity in per mille based on voltage
812 static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage)
815 struct abx500_v_to_cap *tbl;
818 tbl = di->bat->bat_type[di->bat->batt_id].v_to_cap_tbl,
819 tbl_size = di->bat->bat_type[di->bat->batt_id].n_v_cap_tbl_elements;
821 for (i = 0; i < tbl_size; ++i) {
822 if (voltage > tbl[i].voltage)
826 if ((i > 0) && (i < tbl_size)) {
827 cap = interpolate(voltage,
829 tbl[i].capacity * 10,
831 tbl[i-1].capacity * 10);
838 dev_dbg(di->dev, "%s Vbat: %d, Cap: %d per mille",
839 __func__, voltage, cap);
845 * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
846 * @di: pointer to the ab8500_fg structure
848 * Returns battery capacity based on battery voltage that is not compensated
849 * for the voltage drop due to the load
851 static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di)
853 di->vbat = ab8500_fg_bat_voltage(di);
854 return ab8500_fg_volt_to_capacity(di, di->vbat);
858 * ab8500_fg_battery_resistance() - Returns the battery inner resistance
859 * @di: pointer to the ab8500_fg structure
861 * Returns battery inner resistance added with the fuel gauge resistor value
862 * to get the total resistance in the whole link from gnd to bat+ node.
864 static int ab8500_fg_battery_resistance(struct ab8500_fg *di)
867 struct batres_vs_temp *tbl;
870 tbl = di->bat->bat_type[di->bat->batt_id].batres_tbl;
871 tbl_size = di->bat->bat_type[di->bat->batt_id].n_batres_tbl_elements;
873 for (i = 0; i < tbl_size; ++i) {
874 if (di->bat_temp / 10 > tbl[i].temp)
878 if ((i > 0) && (i < tbl_size)) {
879 resist = interpolate(di->bat_temp / 10,
885 resist = tbl[0].resist;
887 resist = tbl[tbl_size - 1].resist;
890 dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d"
891 " fg resistance %d, total: %d (mOhm)\n",
892 __func__, di->bat_temp, resist, di->bat->fg_res / 10,
893 (di->bat->fg_res / 10) + resist);
895 /* fg_res variable is in 0.1mOhm */
896 resist += di->bat->fg_res / 10;
902 * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
903 * @di: pointer to the ab8500_fg structure
905 * Returns battery capacity based on battery voltage that is load compensated
906 * for the voltage drop
908 static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di)
914 ab8500_fg_inst_curr_start(di);
917 vbat += ab8500_fg_bat_voltage(di);
920 } while (!ab8500_fg_inst_curr_done(di));
922 ab8500_fg_inst_curr_finalize(di, &di->inst_curr);
925 res = ab8500_fg_battery_resistance(di);
927 /* Use Ohms law to get the load compensated voltage */
928 vbat_comp = di->vbat - (di->inst_curr * res) / 1000;
930 dev_dbg(di->dev, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, "
931 "R: %dmOhm, Current: %dmA Vbat Samples: %d\n",
932 __func__, di->vbat, vbat_comp, res, di->inst_curr, i);
934 return ab8500_fg_volt_to_capacity(di, vbat_comp);
938 * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
939 * @di: pointer to the ab8500_fg structure
940 * @cap_mah: capacity in mAh
942 * Converts capacity in mAh to capacity in permille
944 static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah)
946 return (cap_mah * 1000) / di->bat_cap.max_mah_design;
950 * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
951 * @di: pointer to the ab8500_fg structure
952 * @cap_pm: capacity in permille
954 * Converts capacity in permille to capacity in mAh
956 static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm)
958 return cap_pm * di->bat_cap.max_mah_design / 1000;
962 * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
963 * @di: pointer to the ab8500_fg structure
964 * @cap_mah: capacity in mAh
966 * Converts capacity in mAh to capacity in uWh
968 static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah)
973 div_res = ((u64) cap_mah) * ((u64) di->vbat_nom);
974 div_rem = do_div(div_res, 1000);
976 /* Make sure to round upwards if necessary */
977 if (div_rem >= 1000 / 2)
980 return (int) div_res;
984 * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
985 * @di: pointer to the ab8500_fg structure
987 * Return the capacity in mAh based on previous calculated capcity and the FG
988 * accumulator register value. The filter is filled with this capacity
990 static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di)
992 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
997 /* Capacity should not be less than 0 */
998 if (di->bat_cap.mah + di->accu_charge > 0)
999 di->bat_cap.mah += di->accu_charge;
1001 di->bat_cap.mah = 0;
1003 * We force capacity to 100% once when the algorithm
1004 * reports that it's full.
1006 if (di->bat_cap.mah >= di->bat_cap.max_mah_design ||
1007 di->flags.force_full) {
1008 di->bat_cap.mah = di->bat_cap.max_mah_design;
1011 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1012 di->bat_cap.permille =
1013 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1015 /* We need to update battery voltage and inst current when charging */
1016 di->vbat = ab8500_fg_bat_voltage(di);
1017 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1019 return di->bat_cap.mah;
1023 * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage
1024 * @di: pointer to the ab8500_fg structure
1025 * @comp: if voltage should be load compensated before capacity calc
1027 * Return the capacity in mAh based on the battery voltage. The voltage can
1028 * either be load compensated or not. This value is added to the filter and a
1029 * new mean value is calculated and returned.
1031 static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di, bool comp)
1036 permille = ab8500_fg_load_comp_volt_to_capacity(di);
1038 permille = ab8500_fg_uncomp_volt_to_capacity(di);
1040 mah = ab8500_fg_convert_permille_to_mah(di, permille);
1042 di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah);
1043 di->bat_cap.permille =
1044 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1046 return di->bat_cap.mah;
1050 * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG
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. This value is added to the filter and a
1055 * new mean value is calculated and returned.
1057 static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di)
1059 int permille_volt, permille;
1061 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1066 /* Capacity should not be less than 0 */
1067 if (di->bat_cap.mah + di->accu_charge > 0)
1068 di->bat_cap.mah += di->accu_charge;
1070 di->bat_cap.mah = 0;
1072 if (di->bat_cap.mah >= di->bat_cap.max_mah_design)
1073 di->bat_cap.mah = di->bat_cap.max_mah_design;
1076 * Check against voltage based capacity. It can not be lower
1077 * than what the uncompensated voltage says
1079 permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1080 permille_volt = ab8500_fg_uncomp_volt_to_capacity(di);
1082 if (permille < permille_volt) {
1083 di->bat_cap.permille = permille_volt;
1084 di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di,
1085 di->bat_cap.permille);
1087 dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n",
1092 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1094 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1095 di->bat_cap.permille =
1096 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1099 return di->bat_cap.mah;
1103 * ab8500_fg_capacity_level() - Get the battery capacity level
1104 * @di: pointer to the ab8500_fg structure
1106 * Get the battery capacity level based on the capacity in percent
1108 static int ab8500_fg_capacity_level(struct ab8500_fg *di)
1112 percent = di->bat_cap.permille / 10;
1114 if (percent <= di->bat->cap_levels->critical ||
1116 ret = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1117 else if (percent <= di->bat->cap_levels->low)
1118 ret = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1119 else if (percent <= di->bat->cap_levels->normal)
1120 ret = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1121 else if (percent <= di->bat->cap_levels->high)
1122 ret = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
1124 ret = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1130 * ab8500_fg_check_capacity_limits() - Check if capacity has changed
1131 * @di: pointer to the ab8500_fg structure
1132 * @init: capacity is allowed to go up in init mode
1134 * Check if capacity or capacity limit has changed and notify the system
1135 * about it using the power_supply framework
1137 static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init)
1139 bool changed = false;
1141 di->bat_cap.level = ab8500_fg_capacity_level(di);
1143 if (di->bat_cap.level != di->bat_cap.prev_level) {
1145 * We do not allow reported capacity level to go up
1146 * unless we're charging or if we're in init
1148 if (!(!di->flags.charging && di->bat_cap.level >
1149 di->bat_cap.prev_level) || init) {
1150 dev_dbg(di->dev, "level changed from %d to %d\n",
1151 di->bat_cap.prev_level,
1153 di->bat_cap.prev_level = di->bat_cap.level;
1156 dev_dbg(di->dev, "level not allowed to go up "
1157 "since no charger is connected: %d to %d\n",
1158 di->bat_cap.prev_level,
1164 * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate
1167 if (di->flags.low_bat) {
1168 dev_dbg(di->dev, "Battery low, set capacity to 0\n");
1169 di->bat_cap.prev_percent = 0;
1170 di->bat_cap.permille = 0;
1171 di->bat_cap.prev_mah = 0;
1172 di->bat_cap.mah = 0;
1174 } else if (di->flags.fully_charged) {
1176 * We report 100% if algorithm reported fully charged
1177 * unless capacity drops too much
1179 if (di->flags.force_full) {
1180 di->bat_cap.prev_percent = di->bat_cap.permille / 10;
1181 di->bat_cap.prev_mah = di->bat_cap.mah;
1182 } else if (!di->flags.force_full &&
1183 di->bat_cap.prev_percent !=
1184 (di->bat_cap.permille) / 10 &&
1185 (di->bat_cap.permille / 10) <
1186 di->bat->fg_params->maint_thres) {
1188 "battery reported full "
1189 "but capacity dropping: %d\n",
1190 di->bat_cap.permille / 10);
1191 di->bat_cap.prev_percent = di->bat_cap.permille / 10;
1192 di->bat_cap.prev_mah = di->bat_cap.mah;
1196 } else if (di->bat_cap.prev_percent != di->bat_cap.permille / 10) {
1197 if (di->bat_cap.permille / 10 == 0) {
1199 * We will not report 0% unless we've got
1200 * the LOW_BAT IRQ, no matter what the FG
1203 di->bat_cap.prev_percent = 1;
1204 di->bat_cap.permille = 1;
1205 di->bat_cap.prev_mah = 1;
1206 di->bat_cap.mah = 1;
1209 } else if (!(!di->flags.charging &&
1210 (di->bat_cap.permille / 10) >
1211 di->bat_cap.prev_percent) || init) {
1213 * We do not allow reported capacity to go up
1214 * unless we're charging or if we're in init
1217 "capacity changed from %d to %d (%d)\n",
1218 di->bat_cap.prev_percent,
1219 di->bat_cap.permille / 10,
1220 di->bat_cap.permille);
1221 di->bat_cap.prev_percent = di->bat_cap.permille / 10;
1222 di->bat_cap.prev_mah = di->bat_cap.mah;
1226 dev_dbg(di->dev, "capacity not allowed to go up since "
1227 "no charger is connected: %d to %d (%d)\n",
1228 di->bat_cap.prev_percent,
1229 di->bat_cap.permille / 10,
1230 di->bat_cap.permille);
1235 power_supply_changed(&di->fg_psy);
1236 if (di->flags.fully_charged && di->flags.force_full) {
1237 dev_dbg(di->dev, "Battery full, notifying.\n");
1238 di->flags.force_full = false;
1239 sysfs_notify(&di->fg_kobject, NULL, "charge_full");
1241 sysfs_notify(&di->fg_kobject, NULL, "charge_now");
1245 static void ab8500_fg_charge_state_to(struct ab8500_fg *di,
1246 enum ab8500_fg_charge_state new_state)
1248 dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n",
1250 charge_state[di->charge_state],
1252 charge_state[new_state]);
1254 di->charge_state = new_state;
1257 static void ab8500_fg_discharge_state_to(struct ab8500_fg *di,
1258 enum ab8500_fg_discharge_state new_state)
1260 dev_dbg(di->dev, "Disharge state from %d [%s] to %d [%s]\n",
1261 di->discharge_state,
1262 discharge_state[di->discharge_state],
1264 discharge_state[new_state]);
1266 di->discharge_state = new_state;
1270 * ab8500_fg_algorithm_charging() - FG algorithm for when charging
1271 * @di: pointer to the ab8500_fg structure
1273 * Battery capacity calculation state machine for when we're charging
1275 static void ab8500_fg_algorithm_charging(struct ab8500_fg *di)
1278 * If we change to discharge mode
1279 * we should start with recovery
1281 if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY)
1282 ab8500_fg_discharge_state_to(di,
1283 AB8500_FG_DISCHARGE_INIT_RECOVERY);
1285 switch (di->charge_state) {
1286 case AB8500_FG_CHARGE_INIT:
1287 di->fg_samples = SEC_TO_SAMPLE(
1288 di->bat->fg_params->accu_charging);
1290 ab8500_fg_coulomb_counter(di, true);
1291 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT);
1295 case AB8500_FG_CHARGE_READOUT:
1297 * Read the FG and calculate the new capacity
1299 mutex_lock(&di->cc_lock);
1300 if (!di->flags.conv_done) {
1301 /* Wasn't the CC IRQ that got us here */
1302 mutex_unlock(&di->cc_lock);
1303 dev_dbg(di->dev, "%s CC conv not done\n",
1308 di->flags.conv_done = false;
1309 mutex_unlock(&di->cc_lock);
1311 ab8500_fg_calc_cap_charging(di);
1319 /* Check capacity limits */
1320 ab8500_fg_check_capacity_limits(di, false);
1323 static void force_capacity(struct ab8500_fg *di)
1327 ab8500_fg_clear_cap_samples(di);
1328 cap = di->bat_cap.user_mah;
1329 if (cap > di->bat_cap.max_mah_design) {
1330 dev_dbg(di->dev, "Remaining cap %d can't be bigger than total"
1331 " %d\n", cap, di->bat_cap.max_mah_design);
1332 cap = di->bat_cap.max_mah_design;
1334 ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah);
1335 di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap);
1336 di->bat_cap.mah = cap;
1337 ab8500_fg_check_capacity_limits(di, true);
1340 static bool check_sysfs_capacity(struct ab8500_fg *di)
1342 int cap, lower, upper;
1345 cap = di->bat_cap.user_mah;
1347 cap_permille = ab8500_fg_convert_mah_to_permille(di,
1348 di->bat_cap.user_mah);
1350 lower = di->bat_cap.permille - di->bat->fg_params->user_cap_limit * 10;
1351 upper = di->bat_cap.permille + di->bat->fg_params->user_cap_limit * 10;
1355 /* 1000 is permille, -> 100 percent */
1359 dev_dbg(di->dev, "Capacity limits:"
1360 " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
1361 lower, cap_permille, upper, cap, di->bat_cap.mah);
1363 /* If within limits, use the saved capacity and exit estimation...*/
1364 if (cap_permille > lower && cap_permille < upper) {
1365 dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap);
1369 dev_dbg(di->dev, "Capacity from user out of limits, ignoring");
1374 * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
1375 * @di: pointer to the ab8500_fg structure
1377 * Battery capacity calculation state machine for when we're discharging
1379 static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di)
1383 /* If we change to charge mode we should start with init */
1384 if (di->charge_state != AB8500_FG_CHARGE_INIT)
1385 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
1387 switch (di->discharge_state) {
1388 case AB8500_FG_DISCHARGE_INIT:
1389 /* We use the FG IRQ to work on */
1391 di->fg_samples = SEC_TO_SAMPLE(di->bat->fg_params->init_timer);
1392 ab8500_fg_coulomb_counter(di, true);
1393 ab8500_fg_discharge_state_to(di,
1394 AB8500_FG_DISCHARGE_INITMEASURING);
1396 /* Intentional fallthrough */
1397 case AB8500_FG_DISCHARGE_INITMEASURING:
1399 * Discard a number of samples during startup.
1400 * After that, use compensated voltage for a few
1401 * samples to get an initial capacity.
1402 * Then go to READOUT
1404 sleep_time = di->bat->fg_params->init_timer;
1406 /* Discard the first [x] seconds */
1408 di->bat->fg_params->init_discard_time) {
1409 ab8500_fg_calc_cap_discharge_voltage(di, true);
1411 ab8500_fg_check_capacity_limits(di, true);
1414 di->init_cnt += sleep_time;
1415 if (di->init_cnt > di->bat->fg_params->init_total_time)
1416 ab8500_fg_discharge_state_to(di,
1417 AB8500_FG_DISCHARGE_READOUT_INIT);
1421 case AB8500_FG_DISCHARGE_INIT_RECOVERY:
1422 di->recovery_cnt = 0;
1423 di->recovery_needed = true;
1424 ab8500_fg_discharge_state_to(di,
1425 AB8500_FG_DISCHARGE_RECOVERY);
1427 /* Intentional fallthrough */
1429 case AB8500_FG_DISCHARGE_RECOVERY:
1430 sleep_time = di->bat->fg_params->recovery_sleep_timer;
1433 * We should check the power consumption
1434 * If low, go to READOUT (after x min) or
1435 * RECOVERY_SLEEP if time left.
1436 * If high, go to READOUT
1438 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1440 if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1441 if (di->recovery_cnt >
1442 di->bat->fg_params->recovery_total_time) {
1443 di->fg_samples = SEC_TO_SAMPLE(
1444 di->bat->fg_params->accu_high_curr);
1445 ab8500_fg_coulomb_counter(di, true);
1446 ab8500_fg_discharge_state_to(di,
1447 AB8500_FG_DISCHARGE_READOUT);
1448 di->recovery_needed = false;
1450 queue_delayed_work(di->fg_wq,
1451 &di->fg_periodic_work,
1454 di->recovery_cnt += sleep_time;
1456 di->fg_samples = SEC_TO_SAMPLE(
1457 di->bat->fg_params->accu_high_curr);
1458 ab8500_fg_coulomb_counter(di, true);
1459 ab8500_fg_discharge_state_to(di,
1460 AB8500_FG_DISCHARGE_READOUT);
1464 case AB8500_FG_DISCHARGE_READOUT_INIT:
1465 di->fg_samples = SEC_TO_SAMPLE(
1466 di->bat->fg_params->accu_high_curr);
1467 ab8500_fg_coulomb_counter(di, true);
1468 ab8500_fg_discharge_state_to(di,
1469 AB8500_FG_DISCHARGE_READOUT);
1472 case AB8500_FG_DISCHARGE_READOUT:
1473 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1475 if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1476 /* Detect mode change */
1477 if (di->high_curr_mode) {
1478 di->high_curr_mode = false;
1479 di->high_curr_cnt = 0;
1482 if (di->recovery_needed) {
1483 ab8500_fg_discharge_state_to(di,
1484 AB8500_FG_DISCHARGE_RECOVERY);
1486 queue_delayed_work(di->fg_wq,
1487 &di->fg_periodic_work, 0);
1492 ab8500_fg_calc_cap_discharge_voltage(di, true);
1494 mutex_lock(&di->cc_lock);
1495 if (!di->flags.conv_done) {
1496 /* Wasn't the CC IRQ that got us here */
1497 mutex_unlock(&di->cc_lock);
1498 dev_dbg(di->dev, "%s CC conv not done\n",
1503 di->flags.conv_done = false;
1504 mutex_unlock(&di->cc_lock);
1506 /* Detect mode change */
1507 if (!di->high_curr_mode) {
1508 di->high_curr_mode = true;
1509 di->high_curr_cnt = 0;
1512 di->high_curr_cnt +=
1513 di->bat->fg_params->accu_high_curr;
1514 if (di->high_curr_cnt >
1515 di->bat->fg_params->high_curr_time)
1516 di->recovery_needed = true;
1518 ab8500_fg_calc_cap_discharge_fg(di);
1521 ab8500_fg_check_capacity_limits(di, false);
1525 case AB8500_FG_DISCHARGE_WAKEUP:
1526 ab8500_fg_coulomb_counter(di, true);
1527 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1529 ab8500_fg_calc_cap_discharge_voltage(di, true);
1531 di->fg_samples = SEC_TO_SAMPLE(
1532 di->bat->fg_params->accu_high_curr);
1533 ab8500_fg_coulomb_counter(di, true);
1534 ab8500_fg_discharge_state_to(di,
1535 AB8500_FG_DISCHARGE_READOUT);
1537 ab8500_fg_check_capacity_limits(di, false);
1547 * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
1548 * @di: pointer to the ab8500_fg structure
1551 static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di)
1555 switch (di->calib_state) {
1556 case AB8500_FG_CALIB_INIT:
1557 dev_dbg(di->dev, "Calibration ongoing...\n");
1559 ret = abx500_mask_and_set_register_interruptible(di->dev,
1560 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1561 CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8);
1565 ret = abx500_mask_and_set_register_interruptible(di->dev,
1566 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1567 CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA);
1570 di->calib_state = AB8500_FG_CALIB_WAIT;
1572 case AB8500_FG_CALIB_END:
1573 ret = abx500_mask_and_set_register_interruptible(di->dev,
1574 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1575 CC_MUXOFFSET, CC_MUXOFFSET);
1578 di->flags.calibrate = false;
1579 dev_dbg(di->dev, "Calibration done...\n");
1580 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1582 case AB8500_FG_CALIB_WAIT:
1583 dev_dbg(di->dev, "Calibration WFI\n");
1589 /* Something went wrong, don't calibrate then */
1590 dev_err(di->dev, "failed to calibrate the CC\n");
1591 di->flags.calibrate = false;
1592 di->calib_state = AB8500_FG_CALIB_INIT;
1593 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1597 * ab8500_fg_algorithm() - Entry point for the FG algorithm
1598 * @di: pointer to the ab8500_fg structure
1600 * Entry point for the battery capacity calculation state machine
1602 static void ab8500_fg_algorithm(struct ab8500_fg *di)
1604 if (di->flags.calibrate)
1605 ab8500_fg_algorithm_calibrate(di);
1607 if (di->flags.charging)
1608 ab8500_fg_algorithm_charging(di);
1610 ab8500_fg_algorithm_discharging(di);
1613 dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d "
1614 "%d %d %d %d %d %d %d\n",
1615 di->bat_cap.max_mah_design,
1617 di->bat_cap.permille,
1619 di->bat_cap.prev_mah,
1620 di->bat_cap.prev_percent,
1621 di->bat_cap.prev_level,
1628 di->discharge_state,
1630 di->recovery_needed);
1634 * ab8500_fg_periodic_work() - Run the FG state machine periodically
1635 * @work: pointer to the work_struct structure
1637 * Work queue function for periodic work
1639 static void ab8500_fg_periodic_work(struct work_struct *work)
1641 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1642 fg_periodic_work.work);
1644 if (di->init_capacity) {
1645 /* A dummy read that will return 0 */
1646 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1647 /* Get an initial capacity calculation */
1648 ab8500_fg_calc_cap_discharge_voltage(di, true);
1649 ab8500_fg_check_capacity_limits(di, true);
1650 di->init_capacity = false;
1652 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1653 } else if (di->flags.user_cap) {
1654 if (check_sysfs_capacity(di)) {
1655 ab8500_fg_check_capacity_limits(di, true);
1656 if (di->flags.charging)
1657 ab8500_fg_charge_state_to(di,
1658 AB8500_FG_CHARGE_INIT);
1660 ab8500_fg_discharge_state_to(di,
1661 AB8500_FG_DISCHARGE_READOUT_INIT);
1663 di->flags.user_cap = false;
1664 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1666 ab8500_fg_algorithm(di);
1671 * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
1672 * @work: pointer to the work_struct structure
1674 * Work queue function for checking the OVV_BAT condition
1676 static void ab8500_fg_check_hw_failure_work(struct work_struct *work)
1681 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1682 fg_check_hw_failure_work.work);
1685 * If we have had a battery over-voltage situation,
1686 * check ovv-bit to see if it should be reset.
1688 if (di->flags.bat_ovv) {
1689 ret = abx500_get_register_interruptible(di->dev,
1690 AB8500_CHARGER, AB8500_CH_STAT_REG,
1693 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1696 if ((reg_value & BATT_OVV) != BATT_OVV) {
1697 dev_dbg(di->dev, "Battery recovered from OVV\n");
1698 di->flags.bat_ovv = false;
1699 power_supply_changed(&di->fg_psy);
1703 /* Not yet recovered from ovv, reschedule this test */
1704 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work,
1710 * ab8500_fg_low_bat_work() - Check LOW_BAT condition
1711 * @work: pointer to the work_struct structure
1713 * Work queue function for checking the LOW_BAT condition
1715 static void ab8500_fg_low_bat_work(struct work_struct *work)
1719 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1720 fg_low_bat_work.work);
1722 vbat = ab8500_fg_bat_voltage(di);
1724 /* Check if LOW_BAT still fulfilled */
1725 if (vbat < di->bat->fg_params->lowbat_threshold) {
1726 di->flags.low_bat = true;
1727 dev_warn(di->dev, "Battery voltage still LOW\n");
1730 * We need to re-schedule this check to be able to detect
1731 * if the voltage increases again during charging
1733 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
1734 round_jiffies(LOW_BAT_CHECK_INTERVAL));
1736 di->flags.low_bat = false;
1737 dev_warn(di->dev, "Battery voltage OK again\n");
1740 /* This is needed to dispatch LOW_BAT */
1741 ab8500_fg_check_capacity_limits(di, false);
1743 /* Set this flag to check if LOW_BAT IRQ still occurs */
1744 di->flags.low_bat_delay = false;
1748 * ab8500_fg_battok_calc - calculate the bit pattern corresponding
1749 * to the target voltage.
1750 * @di: pointer to the ab8500_fg structure
1751 * @target target voltage
1753 * Returns bit pattern closest to the target voltage
1754 * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
1757 static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target)
1759 if (target > BATT_OK_MIN +
1760 (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS))
1761 return BATT_OK_MAX_NR_INCREMENTS;
1762 if (target < BATT_OK_MIN)
1764 return (target - BATT_OK_MIN) / BATT_OK_INCREMENT;
1768 * ab8500_fg_battok_init_hw_register - init battok levels
1769 * @di: pointer to the ab8500_fg structure
1773 static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di)
1783 sel0 = di->bat->fg_params->battok_falling_th_sel0;
1784 sel1 = di->bat->fg_params->battok_raising_th_sel1;
1786 cbp_sel0 = ab8500_fg_battok_calc(di, sel0);
1787 cbp_sel1 = ab8500_fg_battok_calc(di, sel1);
1789 selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT;
1791 if (selected != sel0)
1792 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1793 sel0, selected, cbp_sel0);
1795 selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT;
1797 if (selected != sel1)
1798 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1799 sel1, selected, cbp_sel1);
1801 new_val = cbp_sel0 | (cbp_sel1 << 4);
1803 dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1);
1804 ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK,
1805 AB8500_BATT_OK_REG, new_val);
1810 * ab8500_fg_instant_work() - Run the FG state machine instantly
1811 * @work: pointer to the work_struct structure
1813 * Work queue function for instant work
1815 static void ab8500_fg_instant_work(struct work_struct *work)
1817 struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work);
1819 ab8500_fg_algorithm(di);
1823 * ab8500_fg_cc_data_end_handler() - isr to get battery avg current.
1824 * @irq: interrupt number
1825 * @_di: pointer to the ab8500_fg structure
1827 * Returns IRQ status(IRQ_HANDLED)
1829 static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di)
1831 struct ab8500_fg *di = _di;
1832 complete(&di->ab8500_fg_complete);
1837 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
1838 * @irq: interrupt number
1839 * @_di: pointer to the ab8500_fg structure
1841 * Returns IRQ status(IRQ_HANDLED)
1843 static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di)
1845 struct ab8500_fg *di = _di;
1846 di->calib_state = AB8500_FG_CALIB_END;
1847 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1852 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
1853 * @irq: interrupt number
1854 * @_di: pointer to the ab8500_fg structure
1856 * Returns IRQ status(IRQ_HANDLED)
1858 static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di)
1860 struct ab8500_fg *di = _di;
1862 queue_work(di->fg_wq, &di->fg_acc_cur_work);
1868 * ab8500_fg_batt_ovv_handler() - Battery OVV occured
1869 * @irq: interrupt number
1870 * @_di: pointer to the ab8500_fg structure
1872 * Returns IRQ status(IRQ_HANDLED)
1874 static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di)
1876 struct ab8500_fg *di = _di;
1878 dev_dbg(di->dev, "Battery OVV\n");
1879 di->flags.bat_ovv = true;
1880 power_supply_changed(&di->fg_psy);
1882 /* Schedule a new HW failure check */
1883 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0);
1889 * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
1890 * @irq: interrupt number
1891 * @_di: pointer to the ab8500_fg structure
1893 * Returns IRQ status(IRQ_HANDLED)
1895 static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di)
1897 struct ab8500_fg *di = _di;
1899 if (!di->flags.low_bat_delay) {
1900 dev_warn(di->dev, "Battery voltage is below LOW threshold\n");
1901 di->flags.low_bat_delay = true;
1903 * Start a timer to check LOW_BAT again after some time
1904 * This is done to avoid shutdown on single voltage dips
1906 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
1907 round_jiffies(LOW_BAT_CHECK_INTERVAL));
1913 * ab8500_fg_get_property() - get the fg properties
1914 * @psy: pointer to the power_supply structure
1915 * @psp: pointer to the power_supply_property structure
1916 * @val: pointer to the power_supply_propval union
1918 * This function gets called when an application tries to get the
1919 * fg properties by reading the sysfs files.
1920 * voltage_now: battery voltage
1921 * current_now: battery instant current
1922 * current_avg: battery average current
1923 * charge_full_design: capacity where battery is considered full
1924 * charge_now: battery capacity in nAh
1925 * capacity: capacity in percent
1926 * capacity_level: capacity level
1928 * Returns error code in case of failure else 0 on success
1930 static int ab8500_fg_get_property(struct power_supply *psy,
1931 enum power_supply_property psp,
1932 union power_supply_propval *val)
1934 struct ab8500_fg *di;
1936 di = to_ab8500_fg_device_info(psy);
1939 * If battery is identified as unknown and charging of unknown
1940 * batteries is disabled, we always report 100% capacity and
1941 * capacity level UNKNOWN, since we can't calculate
1942 * remaining capacity
1946 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1947 if (di->flags.bat_ovv)
1948 val->intval = BATT_OVV_VALUE * 1000;
1950 val->intval = di->vbat * 1000;
1952 case POWER_SUPPLY_PROP_CURRENT_NOW:
1953 val->intval = di->inst_curr * 1000;
1955 case POWER_SUPPLY_PROP_CURRENT_AVG:
1956 val->intval = di->avg_curr * 1000;
1958 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
1959 val->intval = ab8500_fg_convert_mah_to_uwh(di,
1960 di->bat_cap.max_mah_design);
1962 case POWER_SUPPLY_PROP_ENERGY_FULL:
1963 val->intval = ab8500_fg_convert_mah_to_uwh(di,
1964 di->bat_cap.max_mah);
1966 case POWER_SUPPLY_PROP_ENERGY_NOW:
1967 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1968 di->flags.batt_id_received)
1969 val->intval = ab8500_fg_convert_mah_to_uwh(di,
1970 di->bat_cap.max_mah);
1972 val->intval = ab8500_fg_convert_mah_to_uwh(di,
1973 di->bat_cap.prev_mah);
1975 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
1976 val->intval = di->bat_cap.max_mah_design;
1978 case POWER_SUPPLY_PROP_CHARGE_FULL:
1979 val->intval = di->bat_cap.max_mah;
1981 case POWER_SUPPLY_PROP_CHARGE_NOW:
1982 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1983 di->flags.batt_id_received)
1984 val->intval = di->bat_cap.max_mah;
1986 val->intval = di->bat_cap.prev_mah;
1988 case POWER_SUPPLY_PROP_CAPACITY:
1989 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1990 di->flags.batt_id_received)
1993 val->intval = di->bat_cap.prev_percent;
1995 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1996 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1997 di->flags.batt_id_received)
1998 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
2000 val->intval = di->bat_cap.prev_level;
2008 static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
2010 struct power_supply *psy;
2011 struct power_supply *ext;
2012 struct ab8500_fg *di;
2013 union power_supply_propval ret;
2015 bool psy_found = false;
2017 psy = (struct power_supply *)data;
2018 ext = dev_get_drvdata(dev);
2019 di = to_ab8500_fg_device_info(psy);
2022 * For all psy where the name of your driver
2023 * appears in any supplied_to
2025 for (i = 0; i < ext->num_supplicants; i++) {
2026 if (!strcmp(ext->supplied_to[i], psy->name))
2033 /* Go through all properties for the psy */
2034 for (j = 0; j < ext->num_properties; j++) {
2035 enum power_supply_property prop;
2036 prop = ext->properties[j];
2038 if (ext->get_property(ext, prop, &ret))
2042 case POWER_SUPPLY_PROP_STATUS:
2043 switch (ext->type) {
2044 case POWER_SUPPLY_TYPE_BATTERY:
2045 switch (ret.intval) {
2046 case POWER_SUPPLY_STATUS_UNKNOWN:
2047 case POWER_SUPPLY_STATUS_DISCHARGING:
2048 case POWER_SUPPLY_STATUS_NOT_CHARGING:
2049 if (!di->flags.charging)
2051 di->flags.charging = false;
2052 di->flags.fully_charged = false;
2053 queue_work(di->fg_wq, &di->fg_work);
2055 case POWER_SUPPLY_STATUS_FULL:
2056 if (di->flags.fully_charged)
2058 di->flags.fully_charged = true;
2059 di->flags.force_full = true;
2060 /* Save current capacity as maximum */
2061 di->bat_cap.max_mah = di->bat_cap.mah;
2062 queue_work(di->fg_wq, &di->fg_work);
2064 case POWER_SUPPLY_STATUS_CHARGING:
2065 if (di->flags.charging)
2067 di->flags.charging = true;
2068 di->flags.fully_charged = false;
2069 queue_work(di->fg_wq, &di->fg_work);
2076 case POWER_SUPPLY_PROP_TECHNOLOGY:
2077 switch (ext->type) {
2078 case POWER_SUPPLY_TYPE_BATTERY:
2079 if (!di->flags.batt_id_received) {
2080 const struct abx500_battery_type *b;
2082 b = &(di->bat->bat_type[di->bat->batt_id]);
2084 di->flags.batt_id_received = true;
2086 di->bat_cap.max_mah_design =
2088 b->charge_full_design;
2090 di->bat_cap.max_mah =
2091 di->bat_cap.max_mah_design;
2093 di->vbat_nom = b->nominal_voltage;
2097 di->flags.batt_unknown = false;
2099 di->flags.batt_unknown = true;
2105 case POWER_SUPPLY_PROP_TEMP:
2106 switch (ext->type) {
2107 case POWER_SUPPLY_TYPE_BATTERY:
2108 if (di->flags.batt_id_received)
2109 di->bat_temp = ret.intval;
2123 * ab8500_fg_init_hw_registers() - Set up FG related registers
2124 * @di: pointer to the ab8500_fg structure
2126 * Set up battery OVV, low battery voltage registers
2128 static int ab8500_fg_init_hw_registers(struct ab8500_fg *di)
2132 /* Set VBAT OVV threshold */
2133 ret = abx500_mask_and_set_register_interruptible(di->dev,
2139 dev_err(di->dev, "failed to set BATT_OVV\n");
2143 /* Enable VBAT OVV detection */
2144 ret = abx500_mask_and_set_register_interruptible(di->dev,
2150 dev_err(di->dev, "failed to enable BATT_OVV\n");
2154 /* Low Battery Voltage */
2155 ret = abx500_set_register_interruptible(di->dev,
2156 AB8500_SYS_CTRL2_BLOCK,
2158 ab8500_volt_to_regval(
2159 di->bat->fg_params->lowbat_threshold) << 1 |
2162 dev_err(di->dev, "%s write failed\n", __func__);
2166 /* Battery OK threshold */
2167 ret = ab8500_fg_battok_init_hw_register(di);
2169 dev_err(di->dev, "BattOk init write failed.\n");
2177 * ab8500_fg_external_power_changed() - callback for power supply changes
2178 * @psy: pointer to the structure power_supply
2180 * This function is the entry point of the pointer external_power_changed
2181 * of the structure power_supply.
2182 * This function gets executed when there is a change in any external power
2183 * supply that this driver needs to be notified of.
2185 static void ab8500_fg_external_power_changed(struct power_supply *psy)
2187 struct ab8500_fg *di = to_ab8500_fg_device_info(psy);
2189 class_for_each_device(power_supply_class, NULL,
2190 &di->fg_psy, ab8500_fg_get_ext_psy_data);
2194 * abab8500_fg_reinit_work() - work to reset the FG algorithm
2195 * @work: pointer to the work_struct structure
2197 * Used to reset the current battery capacity to be able to
2198 * retrigger a new voltage base capacity calculation. For
2199 * test and verification purpose.
2201 static void ab8500_fg_reinit_work(struct work_struct *work)
2203 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
2204 fg_reinit_work.work);
2206 if (di->flags.calibrate == false) {
2207 dev_dbg(di->dev, "Resetting FG state machine to init.\n");
2208 ab8500_fg_clear_cap_samples(di);
2209 ab8500_fg_calc_cap_discharge_voltage(di, true);
2210 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
2211 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
2212 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2215 dev_err(di->dev, "Residual offset calibration ongoing "
2217 /* Wait one second until next try*/
2218 queue_delayed_work(di->fg_wq, &di->fg_reinit_work,
2224 * ab8500_fg_reinit() - forces FG algorithm to reinitialize with current values
2226 * This function can be used to force the FG algorithm to recalculate a new
2227 * voltage based battery capacity.
2229 void ab8500_fg_reinit(void)
2231 struct ab8500_fg *di = ab8500_fg_get();
2232 /* User won't be notified if a null pointer returned. */
2234 queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 0);
2237 /* Exposure to the sysfs interface */
2239 struct ab8500_fg_sysfs_entry {
2240 struct attribute attr;
2241 ssize_t (*show)(struct ab8500_fg *, char *);
2242 ssize_t (*store)(struct ab8500_fg *, const char *, size_t);
2245 static ssize_t charge_full_show(struct ab8500_fg *di, char *buf)
2247 return sprintf(buf, "%d\n", di->bat_cap.max_mah);
2250 static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf,
2253 unsigned long charge_full;
2254 ssize_t ret = -EINVAL;
2256 ret = strict_strtoul(buf, 10, &charge_full);
2258 dev_dbg(di->dev, "Ret %zd charge_full %lu", ret, charge_full);
2261 di->bat_cap.max_mah = (int) charge_full;
2267 static ssize_t charge_now_show(struct ab8500_fg *di, char *buf)
2269 return sprintf(buf, "%d\n", di->bat_cap.prev_mah);
2272 static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf,
2275 unsigned long charge_now;
2278 ret = strict_strtoul(buf, 10, &charge_now);
2280 dev_dbg(di->dev, "Ret %zd charge_now %lu was %d",
2281 ret, charge_now, di->bat_cap.prev_mah);
2284 di->bat_cap.user_mah = (int) charge_now;
2285 di->flags.user_cap = true;
2287 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2292 static struct ab8500_fg_sysfs_entry charge_full_attr =
2293 __ATTR(charge_full, 0644, charge_full_show, charge_full_store);
2295 static struct ab8500_fg_sysfs_entry charge_now_attr =
2296 __ATTR(charge_now, 0644, charge_now_show, charge_now_store);
2299 ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf)
2301 struct ab8500_fg_sysfs_entry *entry;
2302 struct ab8500_fg *di;
2304 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2305 di = container_of(kobj, struct ab8500_fg, fg_kobject);
2310 return entry->show(di, buf);
2313 ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf,
2316 struct ab8500_fg_sysfs_entry *entry;
2317 struct ab8500_fg *di;
2319 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2320 di = container_of(kobj, struct ab8500_fg, fg_kobject);
2325 return entry->store(di, buf, count);
2328 static const struct sysfs_ops ab8500_fg_sysfs_ops = {
2329 .show = ab8500_fg_show,
2330 .store = ab8500_fg_store,
2333 static struct attribute *ab8500_fg_attrs[] = {
2334 &charge_full_attr.attr,
2335 &charge_now_attr.attr,
2339 static struct kobj_type ab8500_fg_ktype = {
2340 .sysfs_ops = &ab8500_fg_sysfs_ops,
2341 .default_attrs = ab8500_fg_attrs,
2345 * ab8500_chargalg_sysfs_exit() - de-init of sysfs entry
2346 * @di: pointer to the struct ab8500_chargalg
2348 * This function removes the entry in sysfs.
2350 static void ab8500_fg_sysfs_exit(struct ab8500_fg *di)
2352 kobject_del(&di->fg_kobject);
2356 * ab8500_chargalg_sysfs_init() - init of sysfs entry
2357 * @di: pointer to the struct ab8500_chargalg
2359 * This function adds an entry in sysfs.
2360 * Returns error code in case of failure else 0(on success)
2362 static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
2366 ret = kobject_init_and_add(&di->fg_kobject,
2370 dev_err(di->dev, "failed to create sysfs entry\n");
2374 /* Exposure to the sysfs interface <<END>> */
2376 #if defined(CONFIG_PM)
2377 static int ab8500_fg_resume(struct platform_device *pdev)
2379 struct ab8500_fg *di = platform_get_drvdata(pdev);
2382 * Change state if we're not charging. If we're charging we will wake
2385 if (!di->flags.charging) {
2386 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP);
2387 queue_work(di->fg_wq, &di->fg_work);
2393 static int ab8500_fg_suspend(struct platform_device *pdev,
2396 struct ab8500_fg *di = platform_get_drvdata(pdev);
2398 flush_delayed_work(&di->fg_periodic_work);
2401 * If the FG is enabled we will disable it before going to suspend
2402 * only if we're not charging
2404 if (di->flags.fg_enabled && !di->flags.charging)
2405 ab8500_fg_coulomb_counter(di, false);
2410 #define ab8500_fg_suspend NULL
2411 #define ab8500_fg_resume NULL
2414 static int __devexit ab8500_fg_remove(struct platform_device *pdev)
2417 struct ab8500_fg *di = platform_get_drvdata(pdev);
2419 list_del(&di->node);
2421 /* Disable coulomb counter */
2422 ret = ab8500_fg_coulomb_counter(di, false);
2424 dev_err(di->dev, "failed to disable coulomb counter\n");
2426 destroy_workqueue(di->fg_wq);
2427 ab8500_fg_sysfs_exit(di);
2429 flush_scheduled_work();
2430 power_supply_unregister(&di->fg_psy);
2431 platform_set_drvdata(pdev, NULL);
2436 /* ab8500 fg driver interrupts and their respective isr */
2437 static struct ab8500_fg_interrupts ab8500_fg_irq[] = {
2438 {"NCONV_ACCU", ab8500_fg_cc_convend_handler},
2439 {"BATT_OVV", ab8500_fg_batt_ovv_handler},
2440 {"LOW_BAT_F", ab8500_fg_lowbatf_handler},
2441 {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler},
2442 {"CCEOC", ab8500_fg_cc_data_end_handler},
2445 static int __devinit ab8500_fg_probe(struct platform_device *pdev)
2449 struct abx500_bm_plat_data *plat_data;
2451 struct ab8500_fg *di =
2452 kzalloc(sizeof(struct ab8500_fg), GFP_KERNEL);
2456 mutex_init(&di->cc_lock);
2458 /* get parent data */
2459 di->dev = &pdev->dev;
2460 di->parent = dev_get_drvdata(pdev->dev.parent);
2461 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2463 /* get fg specific platform data */
2464 plat_data = pdev->dev.platform_data;
2465 if (!plat_data || !plat_data->fg) {
2466 dev_err(di->dev, "no fg platform data supplied\n");
2468 goto free_device_info;
2470 di->pdata = plat_data->fg;
2472 /* get battery specific platform data */
2473 di->bat = plat_data->battery;
2475 dev_err(di->dev, "no battery platform data supplied\n");
2477 goto free_device_info;
2480 di->fg_psy.name = "ab8500_fg";
2481 di->fg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
2482 di->fg_psy.properties = ab8500_fg_props;
2483 di->fg_psy.num_properties = ARRAY_SIZE(ab8500_fg_props);
2484 di->fg_psy.get_property = ab8500_fg_get_property;
2485 di->fg_psy.supplied_to = di->pdata->supplied_to;
2486 di->fg_psy.num_supplicants = di->pdata->num_supplicants;
2487 di->fg_psy.external_power_changed = ab8500_fg_external_power_changed;
2489 di->bat_cap.max_mah_design = MILLI_TO_MICRO *
2490 di->bat->bat_type[di->bat->batt_id].charge_full_design;
2492 di->bat_cap.max_mah = di->bat_cap.max_mah_design;
2494 di->vbat_nom = di->bat->bat_type[di->bat->batt_id].nominal_voltage;
2496 di->init_capacity = true;
2498 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
2499 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
2501 /* Create a work queue for running the FG algorithm */
2502 di->fg_wq = create_singlethread_workqueue("ab8500_fg_wq");
2503 if (di->fg_wq == NULL) {
2504 dev_err(di->dev, "failed to create work queue\n");
2505 goto free_device_info;
2508 /* Init work for running the fg algorithm instantly */
2509 INIT_WORK(&di->fg_work, ab8500_fg_instant_work);
2511 /* Init work for getting the battery accumulated current */
2512 INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work);
2514 /* Init work for reinitialising the fg algorithm */
2515 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_reinit_work,
2516 ab8500_fg_reinit_work);
2518 /* Work delayed Queue to run the state machine */
2519 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_periodic_work,
2520 ab8500_fg_periodic_work);
2522 /* Work to check low battery condition */
2523 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_low_bat_work,
2524 ab8500_fg_low_bat_work);
2526 /* Init work for HW failure check */
2527 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_check_hw_failure_work,
2528 ab8500_fg_check_hw_failure_work);
2530 /* Initialize OVV, and other registers */
2531 ret = ab8500_fg_init_hw_registers(di);
2533 dev_err(di->dev, "failed to initialize registers\n");
2534 goto free_inst_curr_wq;
2537 /* Consider battery unknown until we're informed otherwise */
2538 di->flags.batt_unknown = true;
2539 di->flags.batt_id_received = false;
2541 /* Register FG power supply class */
2542 ret = power_supply_register(di->dev, &di->fg_psy);
2544 dev_err(di->dev, "failed to register FG psy\n");
2545 goto free_inst_curr_wq;
2548 di->fg_samples = SEC_TO_SAMPLE(di->bat->fg_params->init_timer);
2549 ab8500_fg_coulomb_counter(di, true);
2551 /* Initialize completion used to notify completion of inst current */
2552 init_completion(&di->ab8500_fg_complete);
2554 /* Register interrupts */
2555 for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq); i++) {
2556 irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
2557 ret = request_threaded_irq(irq, NULL, ab8500_fg_irq[i].isr,
2558 IRQF_SHARED | IRQF_NO_SUSPEND,
2559 ab8500_fg_irq[i].name, di);
2562 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
2563 , ab8500_fg_irq[i].name, irq, ret);
2566 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
2567 ab8500_fg_irq[i].name, irq, ret);
2569 di->irq = platform_get_irq_byname(pdev, "CCEOC");
2570 disable_irq(di->irq);
2572 platform_set_drvdata(pdev, di);
2574 ret = ab8500_fg_sysfs_init(di);
2576 dev_err(di->dev, "failed to create sysfs entry\n");
2580 /* Calibrate the fg first time */
2581 di->flags.calibrate = true;
2582 di->calib_state = AB8500_FG_CALIB_INIT;
2584 /* Use room temp as default value until we get an update from driver. */
2587 /* Run the FG algorithm */
2588 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2590 list_add_tail(&di->node, &ab8500_fg_list);
2595 power_supply_unregister(&di->fg_psy);
2597 /* We also have to free all successfully registered irqs */
2598 for (i = i - 1; i >= 0; i--) {
2599 irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
2603 destroy_workqueue(di->fg_wq);
2610 static struct platform_driver ab8500_fg_driver = {
2611 .probe = ab8500_fg_probe,
2612 .remove = __devexit_p(ab8500_fg_remove),
2613 .suspend = ab8500_fg_suspend,
2614 .resume = ab8500_fg_resume,
2616 .name = "ab8500-fg",
2617 .owner = THIS_MODULE,
2621 static int __init ab8500_fg_init(void)
2623 return platform_driver_register(&ab8500_fg_driver);
2626 static void __exit ab8500_fg_exit(void)
2628 platform_driver_unregister(&ab8500_fg_driver);
2631 subsys_initcall_sync(ab8500_fg_init);
2632 module_exit(ab8500_fg_exit);
2634 MODULE_LICENSE("GPL v2");
2635 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2636 MODULE_ALIAS("platform:ab8500-fg");
2637 MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");