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
;
153 * struct ab8500_fg - ab8500 FG device information
154 * @dev: Pointer to the structure device
155 * @node: a list of AB8500 FGs, hence prepared for reentrance
156 * @irq holds the CCEOC interrupt number
157 * @vbat_uv: Battery voltage in uV
158 * @vbat_nom_uv: Nominal battery voltage in uV
159 * @inst_curr_ua: Instantenous battery current in uA
160 * @avg_curr_ua: Average battery current in uA
161 * @bat_temp battery temperature
162 * @fg_samples: Number of samples used in the FG accumulation
163 * @accu_charge: Accumulated charge from the last conversion
164 * @recovery_cnt: Counter for recovery mode
165 * @high_curr_cnt: Counter for high current mode
166 * @init_cnt: Counter for init mode
167 * @low_bat_cnt Counter for number of consecutive low battery measures
168 * @nbr_cceoc_irq_cnt Counter for number of CCEOC irqs received since enabled
169 * @recovery_needed: Indicate if recovery is needed
170 * @high_curr_mode: Indicate if we're in high current mode
171 * @init_capacity: Indicate if initial capacity measuring should be done
172 * @turn_off_fg: True if fg was off before current measurement
173 * @calib_state State during offset calibration
174 * @discharge_state: Current discharge state
175 * @charge_state: Current charge state
176 * @ab8500_fg_started Completion struct used for the instant current start
177 * @ab8500_fg_complete Completion struct used for the instant current reading
178 * @flags: Structure for information about events triggered
179 * @bat_cap: Structure for battery capacity specific parameters
180 * @avg_cap: Average capacity filter
181 * @parent: Pointer to the struct ab8500
182 * @main_bat_v: ADC channel for the main battery voltage
183 * @bm: Platform specific battery management information
184 * @fg_psy: Structure that holds the FG specific battery properties
185 * @fg_wq: Work queue for running the FG algorithm
186 * @fg_periodic_work: Work to run the FG algorithm periodically
187 * @fg_low_bat_work: Work to check low bat condition
188 * @fg_reinit_work Work used to reset and reinitialise the FG algorithm
189 * @fg_work: Work to run the FG algorithm instantly
190 * @fg_acc_cur_work: Work to read the FG accumulator
191 * @fg_check_hw_failure_work: Work for checking HW state
192 * @cc_lock: Mutex for locking the CC
193 * @fg_kobject: Structure of type kobject
197 struct list_head node
;
210 int nbr_cceoc_irq_cnt
;
211 u32 line_impedance_uohm
;
212 bool recovery_needed
;
216 enum ab8500_fg_calibration_state calib_state
;
217 enum ab8500_fg_discharge_state discharge_state
;
218 enum ab8500_fg_charge_state charge_state
;
219 struct completion ab8500_fg_started
;
220 struct completion ab8500_fg_complete
;
221 struct ab8500_fg_flags flags
;
222 struct ab8500_fg_battery_capacity bat_cap
;
223 struct ab8500_fg_avg_cap avg_cap
;
224 struct ab8500
*parent
;
225 struct iio_channel
*main_bat_v
;
226 struct ab8500_bm_data
*bm
;
227 struct power_supply
*fg_psy
;
228 struct workqueue_struct
*fg_wq
;
229 struct delayed_work fg_periodic_work
;
230 struct delayed_work fg_low_bat_work
;
231 struct delayed_work fg_reinit_work
;
232 struct work_struct fg_work
;
233 struct work_struct fg_acc_cur_work
;
234 struct delayed_work fg_check_hw_failure_work
;
235 struct mutex cc_lock
;
236 struct kobject fg_kobject
;
238 static LIST_HEAD(ab8500_fg_list
);
241 * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
242 * (i.e. the first fuel gauge in the instance list)
244 struct ab8500_fg
*ab8500_fg_get(void)
246 return list_first_entry_or_null(&ab8500_fg_list
, struct ab8500_fg
,
250 /* Main battery properties */
251 static enum power_supply_property ab8500_fg_props
[] = {
252 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
253 POWER_SUPPLY_PROP_CURRENT_NOW
,
254 POWER_SUPPLY_PROP_CURRENT_AVG
,
255 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
256 POWER_SUPPLY_PROP_ENERGY_FULL
,
257 POWER_SUPPLY_PROP_ENERGY_NOW
,
258 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
259 POWER_SUPPLY_PROP_CHARGE_FULL
,
260 POWER_SUPPLY_PROP_CHARGE_NOW
,
261 POWER_SUPPLY_PROP_CAPACITY
,
262 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
266 * This array maps the raw hex value to lowbat voltage used by the AB8500
267 * Values taken from the UM0836, in microvolts.
269 static int ab8500_fg_lowbat_voltage_map
[] = {
336 static u8
ab8500_volt_to_regval(int voltage_uv
)
340 if (voltage_uv
< ab8500_fg_lowbat_voltage_map
[0])
343 for (i
= 0; i
< ARRAY_SIZE(ab8500_fg_lowbat_voltage_map
); i
++) {
344 if (voltage_uv
< ab8500_fg_lowbat_voltage_map
[i
])
348 /* If not captured above, return index of last element */
349 return (u8
) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map
) - 1;
353 * ab8500_fg_is_low_curr() - Low or high current mode
354 * @di: pointer to the ab8500_fg structure
355 * @curr_ua: the current to base or our decision on in microampere
357 * Low current mode if the current consumption is below a certain threshold
359 static int ab8500_fg_is_low_curr(struct ab8500_fg
*di
, int curr_ua
)
362 * We want to know if we're in low current mode
364 if (curr_ua
> -di
->bm
->fg_params
->high_curr_threshold_ua
)
371 * ab8500_fg_add_cap_sample() - Add capacity to average filter
372 * @di: pointer to the ab8500_fg structure
373 * @sample: the capacity in mAh to add to the filter
375 * A capacity is added to the filter and a new mean capacity is calculated and
378 static int ab8500_fg_add_cap_sample(struct ab8500_fg
*di
, int sample
)
380 time64_t now
= ktime_get_boottime_seconds();
381 struct ab8500_fg_avg_cap
*avg
= &di
->avg_cap
;
384 avg
->sum
+= sample
- avg
->samples
[avg
->pos
];
385 avg
->samples
[avg
->pos
] = sample
;
386 avg
->time_stamps
[avg
->pos
] = now
;
389 if (avg
->pos
== NBR_AVG_SAMPLES
)
392 if (avg
->nbr_samples
< NBR_AVG_SAMPLES
)
396 * Check the time stamp for each sample. If too old,
397 * replace with latest sample
399 } while (now
- VALID_CAPACITY_SEC
> avg
->time_stamps
[avg
->pos
]);
401 avg
->avg
= avg
->sum
/ avg
->nbr_samples
;
407 * ab8500_fg_clear_cap_samples() - Clear average filter
408 * @di: pointer to the ab8500_fg structure
410 * The capacity filter is reset to zero.
412 static void ab8500_fg_clear_cap_samples(struct ab8500_fg
*di
)
415 struct ab8500_fg_avg_cap
*avg
= &di
->avg_cap
;
418 avg
->nbr_samples
= 0;
422 for (i
= 0; i
< NBR_AVG_SAMPLES
; i
++) {
424 avg
->time_stamps
[i
] = 0;
429 * ab8500_fg_fill_cap_sample() - Fill average filter
430 * @di: pointer to the ab8500_fg structure
431 * @sample: the capacity in mAh to fill the filter with
433 * The capacity filter is filled with a capacity in mAh
435 static void ab8500_fg_fill_cap_sample(struct ab8500_fg
*di
, int sample
)
439 struct ab8500_fg_avg_cap
*avg
= &di
->avg_cap
;
441 now
= ktime_get_boottime_seconds();
443 for (i
= 0; i
< NBR_AVG_SAMPLES
; i
++) {
444 avg
->samples
[i
] = sample
;
445 avg
->time_stamps
[i
] = now
;
449 avg
->nbr_samples
= NBR_AVG_SAMPLES
;
450 avg
->sum
= sample
* NBR_AVG_SAMPLES
;
455 * ab8500_fg_coulomb_counter() - enable coulomb counter
456 * @di: pointer to the ab8500_fg structure
457 * @enable: enable/disable
459 * Enable/Disable coulomb counter.
460 * On failure returns negative value.
462 static int ab8500_fg_coulomb_counter(struct ab8500_fg
*di
, bool enable
)
465 mutex_lock(&di
->cc_lock
);
467 /* To be able to reprogram the number of samples, we have to
468 * first stop the CC and then enable it again */
469 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
470 AB8500_RTC_CC_CONF_REG
, 0x00);
474 /* Program the samples */
475 ret
= abx500_set_register_interruptible(di
->dev
,
476 AB8500_GAS_GAUGE
, AB8500_GASG_CC_NCOV_ACCU
,
482 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
483 AB8500_RTC_CC_CONF_REG
,
484 (CC_DEEP_SLEEP_ENA
| CC_PWR_UP_ENA
));
488 di
->flags
.fg_enabled
= true;
490 /* Clear any pending read requests */
491 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
492 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
,
493 (RESET_ACCU
| READ_REQ
), 0);
497 ret
= abx500_set_register_interruptible(di
->dev
,
498 AB8500_GAS_GAUGE
, AB8500_GASG_CC_NCOV_ACCU_CTRL
, 0);
503 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
504 AB8500_RTC_CC_CONF_REG
, 0);
508 di
->flags
.fg_enabled
= false;
511 dev_dbg(di
->dev
, " CC enabled: %d Samples: %d\n",
512 enable
, di
->fg_samples
);
514 mutex_unlock(&di
->cc_lock
);
518 dev_err(di
->dev
, "%s Enabling coulomb counter failed\n", __func__
);
519 mutex_unlock(&di
->cc_lock
);
524 * ab8500_fg_inst_curr_start() - start battery instantaneous current
525 * @di: pointer to the ab8500_fg structure
527 * Returns 0 or error code
528 * Note: This is part "one" and has to be called before
529 * ab8500_fg_inst_curr_finalize()
531 int ab8500_fg_inst_curr_start(struct ab8500_fg
*di
)
536 mutex_lock(&di
->cc_lock
);
538 di
->nbr_cceoc_irq_cnt
= 0;
539 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
540 AB8500_RTC_CC_CONF_REG
, ®_val
);
544 if (!(reg_val
& CC_PWR_UP_ENA
)) {
545 dev_dbg(di
->dev
, "%s Enable FG\n", __func__
);
546 di
->turn_off_fg
= true;
548 /* Program the samples */
549 ret
= abx500_set_register_interruptible(di
->dev
,
550 AB8500_GAS_GAUGE
, AB8500_GASG_CC_NCOV_ACCU
,
556 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
557 AB8500_RTC_CC_CONF_REG
,
558 (CC_DEEP_SLEEP_ENA
| CC_PWR_UP_ENA
));
562 di
->turn_off_fg
= false;
566 reinit_completion(&di
->ab8500_fg_started
);
567 reinit_completion(&di
->ab8500_fg_complete
);
570 /* Note: cc_lock is still locked */
573 mutex_unlock(&di
->cc_lock
);
578 * ab8500_fg_inst_curr_started() - check if fg conversion has started
579 * @di: pointer to the ab8500_fg structure
581 * Returns 1 if conversion started, 0 if still waiting
583 int ab8500_fg_inst_curr_started(struct ab8500_fg
*di
)
585 return completion_done(&di
->ab8500_fg_started
);
589 * ab8500_fg_inst_curr_done() - check if fg conversion is done
590 * @di: pointer to the ab8500_fg structure
592 * Returns 1 if conversion done, 0 if still waiting
594 int ab8500_fg_inst_curr_done(struct ab8500_fg
*di
)
596 return completion_done(&di
->ab8500_fg_complete
);
600 * ab8500_fg_inst_curr_finalize() - battery instantaneous current
601 * @di: pointer to the ab8500_fg structure
602 * @curr_ua: battery instantenous current in microampere (on success)
604 * Returns 0 or an error code
605 * Note: This is part "two" and has to be called at earliest 250 ms
606 * after ab8500_fg_inst_curr_start()
608 int ab8500_fg_inst_curr_finalize(struct ab8500_fg
*di
, int *curr_ua
)
613 unsigned long timeout
;
615 if (!completion_done(&di
->ab8500_fg_complete
)) {
616 timeout
= wait_for_completion_timeout(
617 &di
->ab8500_fg_complete
,
619 dev_dbg(di
->dev
, "Finalize time: %d ms\n",
620 jiffies_to_msecs(INS_CURR_TIMEOUT
- timeout
));
623 disable_irq(di
->irq
);
624 di
->nbr_cceoc_irq_cnt
= 0;
625 dev_err(di
->dev
, "completion timed out [%d]\n",
631 disable_irq(di
->irq
);
632 di
->nbr_cceoc_irq_cnt
= 0;
634 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
635 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
,
638 /* 100uS between read request and read is needed */
639 usleep_range(100, 100);
641 /* Read CC Sample conversion value Low and high */
642 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
643 AB8500_GASG_CC_SMPL_CNVL_REG
, &low
);
647 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
648 AB8500_GASG_CC_SMPL_CNVH_REG
, &high
);
653 * negative value for Discharging
654 * convert 2's complement into decimal
657 val
= (low
| (high
<< 8) | 0xFFFFE000);
659 val
= (low
| (high
<< 8));
662 * Convert to unit value in mA
663 * Full scale input voltage is
664 * 63.160mV => LSB = 63.160mV/(4096*res) = 1.542.000 uA
665 * Given a 250ms conversion cycle time the LSB corresponds
666 * to 107.1 nAh. Convert to current by dividing by the conversion
667 * time in hours (250ms = 1 / (3600 * 4)h)
668 * 107.1nAh assumes 10mOhm, but fg_res is in 0.1mOhm
670 val
= (val
* QLSB_NANO_AMP_HOURS_X10
* 36 * 4) / di
->bm
->fg_res
;
672 if (di
->turn_off_fg
) {
673 dev_dbg(di
->dev
, "%s Disable FG\n", __func__
);
675 /* Clear any pending read requests */
676 ret
= abx500_set_register_interruptible(di
->dev
,
677 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
, 0);
682 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
683 AB8500_RTC_CC_CONF_REG
, 0);
687 mutex_unlock(&di
->cc_lock
);
692 mutex_unlock(&di
->cc_lock
);
697 * ab8500_fg_inst_curr_blocking() - battery instantaneous current
698 * @di: pointer to the ab8500_fg structure
700 * Returns battery instantenous current in microampere (on success)
703 int ab8500_fg_inst_curr_blocking(struct ab8500_fg
*di
)
706 unsigned long timeout
;
709 ret
= ab8500_fg_inst_curr_start(di
);
711 dev_err(di
->dev
, "Failed to initialize fg_inst\n");
715 /* Wait for CC to actually start */
716 if (!completion_done(&di
->ab8500_fg_started
)) {
717 timeout
= wait_for_completion_timeout(
718 &di
->ab8500_fg_started
,
720 dev_dbg(di
->dev
, "Start time: %d ms\n",
721 jiffies_to_msecs(INS_CURR_TIMEOUT
- timeout
));
724 dev_err(di
->dev
, "completion timed out [%d]\n",
730 ret
= ab8500_fg_inst_curr_finalize(di
, &curr_ua
);
732 dev_err(di
->dev
, "Failed to finalize fg_inst\n");
736 dev_dbg(di
->dev
, "%s instant current: %d uA", __func__
, curr_ua
);
739 disable_irq(di
->irq
);
740 mutex_unlock(&di
->cc_lock
);
745 * ab8500_fg_acc_cur_work() - average battery current
746 * @work: pointer to the work_struct structure
748 * Updated the average battery current obtained from the
751 static void ab8500_fg_acc_cur_work(struct work_struct
*work
)
757 struct ab8500_fg
*di
= container_of(work
,
758 struct ab8500_fg
, fg_acc_cur_work
);
760 mutex_lock(&di
->cc_lock
);
761 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
762 AB8500_GASG_CC_NCOV_ACCU_CTRL
, RD_NCONV_ACCU_REQ
);
766 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
767 AB8500_GASG_CC_NCOV_ACCU_LOW
, &low
);
771 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
772 AB8500_GASG_CC_NCOV_ACCU_MED
, &med
);
776 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
777 AB8500_GASG_CC_NCOV_ACCU_HIGH
, &high
);
781 /* Check for sign bit in case of negative value, 2's complement */
783 val
= (low
| (med
<< 8) | (high
<< 16) | 0xFFE00000);
785 val
= (low
| (med
<< 8) | (high
<< 16));
789 * Given a 250ms conversion cycle time the LSB corresponds
791 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
793 di
->accu_charge
= (val
* QLSB_NANO_AMP_HOURS_X10
) /
794 (100 * di
->bm
->fg_res
);
797 * Convert to unit value in uA
798 * by dividing by the conversion
799 * time in hours (= samples / (3600 * 4)h)
801 di
->avg_curr_ua
= (val
* QLSB_NANO_AMP_HOURS_X10
* 36) /
802 (di
->bm
->fg_res
* (di
->fg_samples
/ 4));
804 di
->flags
.conv_done
= true;
806 mutex_unlock(&di
->cc_lock
);
808 queue_work(di
->fg_wq
, &di
->fg_work
);
810 dev_dbg(di
->dev
, "fg_res: %d, fg_samples: %d, gasg: %d, accu_charge: %d \n",
811 di
->bm
->fg_res
, di
->fg_samples
, val
, di
->accu_charge
);
815 "Failed to read or write gas gauge registers\n");
816 mutex_unlock(&di
->cc_lock
);
817 queue_work(di
->fg_wq
, &di
->fg_work
);
821 * ab8500_fg_bat_voltage() - get battery voltage
822 * @di: pointer to the ab8500_fg structure
824 * Returns battery voltage in microvolts (on success) else error code
826 static int ab8500_fg_bat_voltage(struct ab8500_fg
*di
)
831 ret
= iio_read_channel_processed(di
->main_bat_v
, &vbat
);
834 "%s ADC conversion failed, using previous value\n",
839 /* IIO returns millivolts but we want microvolts */
846 * ab8500_fg_volt_to_capacity() - Voltage based capacity
847 * @di: pointer to the ab8500_fg structure
848 * @voltage_uv: The voltage to convert to a capacity in microvolt
850 * Returns battery capacity in per mille based on voltage
852 static int ab8500_fg_volt_to_capacity(struct ab8500_fg
*di
, int voltage_uv
)
854 struct power_supply_battery_info
*bi
= di
->bm
->bi
;
856 /* Multiply by 10 because the capacity is tracked in per mille */
857 return power_supply_batinfo_ocv2cap(bi
, voltage_uv
, di
->bat_temp
) * 10;
861 * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
862 * @di: pointer to the ab8500_fg structure
864 * Returns battery capacity based on battery voltage that is not compensated
865 * for the voltage drop due to the load
867 static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg
*di
)
869 di
->vbat_uv
= ab8500_fg_bat_voltage(di
);
870 return ab8500_fg_volt_to_capacity(di
, di
->vbat_uv
);
874 * ab8500_fg_battery_resistance() - Returns the battery inner resistance
875 * @di: pointer to the ab8500_fg structure
876 * @vbat_uncomp_uv: Uncompensated VBAT voltage
878 * Returns battery inner resistance added with the fuel gauge resistor value
879 * to get the total resistance in the whole link from gnd to bat+ node
882 static int ab8500_fg_battery_resistance(struct ab8500_fg
*di
, int vbat_uncomp_uv
)
884 struct power_supply_battery_info
*bi
= di
->bm
->bi
;
885 int resistance_percent
= 0;
889 * Determine the resistance at this voltage. First try VBAT-to-Ri else
890 * just infer it from the surrounding temperature, if nothing works just
891 * use the internal resistance.
893 if (power_supply_supports_vbat2ri(bi
)) {
894 resistance
= power_supply_vbat2ri(bi
, vbat_uncomp_uv
, di
->flags
.charging
);
895 /* Convert to milliohm */
896 resistance
= resistance
/ 1000;
897 } else if (power_supply_supports_temp2ri(bi
)) {
898 resistance_percent
= power_supply_temp2resist_simple(bi
->resist_table
,
899 bi
->resist_table_size
,
901 /* Convert to milliohm */
902 resistance
= bi
->factory_internal_resistance_uohm
/ 1000;
903 resistance
= resistance
* resistance_percent
/ 100;
906 resistance
= bi
->factory_internal_resistance_uohm
/ 1000;
909 /* Compensate for line impedance */
910 resistance
+= (di
->line_impedance_uohm
/ 1000);
912 dev_dbg(di
->dev
, "%s Temp: %d battery internal resistance: %d"
913 " fg resistance %d, total: %d (mOhm)\n",
914 __func__
, di
->bat_temp
, resistance
, di
->bm
->fg_res
/ 10,
915 (di
->bm
->fg_res
/ 10) + resistance
);
917 /* fg_res variable is in 0.1mOhm */
918 resistance
+= di
->bm
->fg_res
/ 10;
924 * ab8500_load_comp_fg_bat_voltage() - get load compensated battery voltage
925 * @di: pointer to the ab8500_fg structure
926 * @always: always return a voltage, also uncompensated
928 * Returns compensated battery voltage (on success) else error code.
929 * If always is specified, we always return a voltage but it may be
932 static int ab8500_load_comp_fg_bat_voltage(struct ab8500_fg
*di
, bool always
)
938 /* Average the instant current to get a stable current measurement */
939 ab8500_fg_inst_curr_start(di
);
942 vbat_uv
+= ab8500_fg_bat_voltage(di
);
944 usleep_range(5000, 6000);
945 } while (!ab8500_fg_inst_curr_done(di
) &&
946 i
<= WAIT_FOR_INST_CURRENT_MAX
);
948 if (i
> WAIT_FOR_INST_CURRENT_MAX
) {
950 "TIMEOUT: return uncompensated measurement of VBAT\n");
951 di
->vbat_uv
= vbat_uv
/ i
;
955 ab8500_fg_inst_curr_finalize(di
, &di
->inst_curr_ua
);
958 * If there is too high current dissipation, the compensation cannot be
959 * trusted so return an error unless we must return something here, as
960 * enforced by the "always" parameter.
962 if (!always
&& di
->inst_curr_ua
< IGNORE_VBAT_HIGHCUR
)
965 vbat_uv
= vbat_uv
/ i
;
967 /* Next we apply voltage compensation from internal resistance */
968 rcomp
= ab8500_fg_battery_resistance(di
, vbat_uv
);
969 vbat_uv
= vbat_uv
- (di
->inst_curr_ua
* rcomp
) / 1000;
971 /* Always keep this state at latest measurement */
972 di
->vbat_uv
= vbat_uv
;
978 * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
979 * @di: pointer to the ab8500_fg structure
981 * Returns battery capacity based on battery voltage that is load compensated
982 * for the voltage drop
984 static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg
*di
)
988 vbat_comp_uv
= ab8500_load_comp_fg_bat_voltage(di
, true);
990 return ab8500_fg_volt_to_capacity(di
, vbat_comp_uv
);
994 * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
995 * @di: pointer to the ab8500_fg structure
996 * @cap_mah: capacity in mAh
998 * Converts capacity in mAh to capacity in permille
1000 static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg
*di
, int cap_mah
)
1002 return (cap_mah
* 1000) / di
->bat_cap
.max_mah_design
;
1006 * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
1007 * @di: pointer to the ab8500_fg structure
1008 * @cap_pm: capacity in permille
1010 * Converts capacity in permille to capacity in mAh
1012 static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg
*di
, int cap_pm
)
1014 return cap_pm
* di
->bat_cap
.max_mah_design
/ 1000;
1018 * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
1019 * @di: pointer to the ab8500_fg structure
1020 * @cap_mah: capacity in mAh
1022 * Converts capacity in mAh to capacity in uWh
1024 static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg
*di
, int cap_mah
)
1030 * Capacity is in milli ampere hours (10^-3)Ah
1031 * Nominal voltage is in microvolts (10^-6)V
1032 * divide by 1000000 after multiplication to get to mWh
1034 div_res
= ((u64
) cap_mah
) * ((u64
) di
->vbat_nom_uv
);
1035 div_rem
= do_div(div_res
, 1000000);
1037 /* Make sure to round upwards if necessary */
1038 if (div_rem
>= 1000000 / 2)
1041 return (int) div_res
;
1045 * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
1046 * @di: pointer to the ab8500_fg structure
1048 * Return the capacity in mAh based on previous calculated capcity and the FG
1049 * accumulator register value. The filter is filled with this capacity
1051 static int ab8500_fg_calc_cap_charging(struct ab8500_fg
*di
)
1053 dev_dbg(di
->dev
, "%s cap_mah %d accu_charge %d\n",
1058 /* Capacity should not be less than 0 */
1059 if (di
->bat_cap
.mah
+ di
->accu_charge
> 0)
1060 di
->bat_cap
.mah
+= di
->accu_charge
;
1062 di
->bat_cap
.mah
= 0;
1064 * We force capacity to 100% once when the algorithm
1065 * reports that it's full.
1067 if (di
->bat_cap
.mah
>= di
->bat_cap
.max_mah_design
||
1068 di
->flags
.force_full
) {
1069 di
->bat_cap
.mah
= di
->bat_cap
.max_mah_design
;
1072 ab8500_fg_fill_cap_sample(di
, di
->bat_cap
.mah
);
1073 di
->bat_cap
.permille
=
1074 ab8500_fg_convert_mah_to_permille(di
, di
->bat_cap
.mah
);
1076 /* We need to update battery voltage and inst current when charging */
1077 di
->vbat_uv
= ab8500_fg_bat_voltage(di
);
1078 di
->inst_curr_ua
= ab8500_fg_inst_curr_blocking(di
);
1080 return di
->bat_cap
.mah
;
1084 * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage
1085 * @di: pointer to the ab8500_fg structure
1087 * Return the capacity in mAh based on the load compensated battery voltage.
1088 * This value is added to the filter and a new mean value is calculated and
1091 static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg
*di
)
1095 permille
= ab8500_fg_load_comp_volt_to_capacity(di
);
1097 mah
= ab8500_fg_convert_permille_to_mah(di
, permille
);
1099 di
->bat_cap
.mah
= ab8500_fg_add_cap_sample(di
, mah
);
1100 di
->bat_cap
.permille
=
1101 ab8500_fg_convert_mah_to_permille(di
, di
->bat_cap
.mah
);
1103 return di
->bat_cap
.mah
;
1107 * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG
1108 * @di: pointer to the ab8500_fg structure
1110 * Return the capacity in mAh based on previous calculated capcity and the FG
1111 * accumulator register value. This value is added to the filter and a
1112 * new mean value is calculated and returned.
1114 static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg
*di
)
1116 int permille_volt
, permille
;
1118 dev_dbg(di
->dev
, "%s cap_mah %d accu_charge %d\n",
1123 /* Capacity should not be less than 0 */
1124 if (di
->bat_cap
.mah
+ di
->accu_charge
> 0)
1125 di
->bat_cap
.mah
+= di
->accu_charge
;
1127 di
->bat_cap
.mah
= 0;
1129 if (di
->bat_cap
.mah
>= di
->bat_cap
.max_mah_design
)
1130 di
->bat_cap
.mah
= di
->bat_cap
.max_mah_design
;
1133 * Check against voltage based capacity. It can not be lower
1134 * than what the uncompensated voltage says
1136 permille
= ab8500_fg_convert_mah_to_permille(di
, di
->bat_cap
.mah
);
1137 permille_volt
= ab8500_fg_uncomp_volt_to_capacity(di
);
1139 if (permille
< permille_volt
) {
1140 di
->bat_cap
.permille
= permille_volt
;
1141 di
->bat_cap
.mah
= ab8500_fg_convert_permille_to_mah(di
,
1142 di
->bat_cap
.permille
);
1144 dev_dbg(di
->dev
, "%s voltage based: perm %d perm_volt %d\n",
1149 ab8500_fg_fill_cap_sample(di
, di
->bat_cap
.mah
);
1151 ab8500_fg_fill_cap_sample(di
, di
->bat_cap
.mah
);
1152 di
->bat_cap
.permille
=
1153 ab8500_fg_convert_mah_to_permille(di
, di
->bat_cap
.mah
);
1156 return di
->bat_cap
.mah
;
1160 * ab8500_fg_capacity_level() - Get the battery capacity level
1161 * @di: pointer to the ab8500_fg structure
1163 * Get the battery capacity level based on the capacity in percent
1165 static int ab8500_fg_capacity_level(struct ab8500_fg
*di
)
1169 percent
= DIV_ROUND_CLOSEST(di
->bat_cap
.permille
, 10);
1171 if (percent
<= di
->bm
->cap_levels
->critical
||
1173 ret
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
1174 else if (percent
<= di
->bm
->cap_levels
->low
)
1175 ret
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
1176 else if (percent
<= di
->bm
->cap_levels
->normal
)
1177 ret
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
1178 else if (percent
<= di
->bm
->cap_levels
->high
)
1179 ret
= POWER_SUPPLY_CAPACITY_LEVEL_HIGH
;
1181 ret
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
1187 * ab8500_fg_calculate_scaled_capacity() - Capacity scaling
1188 * @di: pointer to the ab8500_fg structure
1190 * Calculates the capacity to be shown to upper layers. Scales the capacity
1191 * to have 100% as a reference from the actual capacity upon removal of charger
1192 * when charging is in maintenance mode.
1194 static int ab8500_fg_calculate_scaled_capacity(struct ab8500_fg
*di
)
1196 struct ab8500_fg_cap_scaling
*cs
= &di
->bat_cap
.cap_scale
;
1197 int capacity
= di
->bat_cap
.prev_percent
;
1203 * As long as we are in fully charge mode scale the capacity
1206 if (di
->flags
.fully_charged
) {
1207 cs
->cap_to_scale
[0] = 100;
1208 cs
->cap_to_scale
[1] =
1209 max(capacity
, di
->bm
->fg_params
->maint_thres
);
1210 dev_dbg(di
->dev
, "Scale cap with %d/%d\n",
1211 cs
->cap_to_scale
[0], cs
->cap_to_scale
[1]);
1214 /* Calculates the scaled capacity. */
1215 if ((cs
->cap_to_scale
[0] != cs
->cap_to_scale
[1])
1216 && (cs
->cap_to_scale
[1] > 0))
1218 DIV_ROUND_CLOSEST(di
->bat_cap
.prev_percent
*
1219 cs
->cap_to_scale
[0],
1220 cs
->cap_to_scale
[1]));
1222 if (di
->flags
.charging
) {
1223 if (capacity
< cs
->disable_cap_level
) {
1224 cs
->disable_cap_level
= capacity
;
1225 dev_dbg(di
->dev
, "Cap to stop scale lowered %d%%\n",
1226 cs
->disable_cap_level
);
1227 } else if (!di
->flags
.fully_charged
) {
1228 if (di
->bat_cap
.prev_percent
>=
1229 cs
->disable_cap_level
) {
1230 dev_dbg(di
->dev
, "Disabling scaled capacity\n");
1232 capacity
= di
->bat_cap
.prev_percent
;
1235 "Waiting in cap to level %d%%\n",
1236 cs
->disable_cap_level
);
1237 capacity
= cs
->disable_cap_level
;
1246 * ab8500_fg_update_cap_scalers() - Capacity scaling
1247 * @di: pointer to the ab8500_fg structure
1249 * To be called when state change from charge<->discharge to update
1250 * the capacity scalers.
1252 static void ab8500_fg_update_cap_scalers(struct ab8500_fg
*di
)
1254 struct ab8500_fg_cap_scaling
*cs
= &di
->bat_cap
.cap_scale
;
1258 if (di
->flags
.charging
) {
1259 di
->bat_cap
.cap_scale
.disable_cap_level
=
1260 di
->bat_cap
.cap_scale
.scaled_cap
;
1261 dev_dbg(di
->dev
, "Cap to stop scale at charge %d%%\n",
1262 di
->bat_cap
.cap_scale
.disable_cap_level
);
1264 if (cs
->scaled_cap
!= 100) {
1265 cs
->cap_to_scale
[0] = cs
->scaled_cap
;
1266 cs
->cap_to_scale
[1] = di
->bat_cap
.prev_percent
;
1268 cs
->cap_to_scale
[0] = 100;
1269 cs
->cap_to_scale
[1] =
1270 max(di
->bat_cap
.prev_percent
,
1271 di
->bm
->fg_params
->maint_thres
);
1274 dev_dbg(di
->dev
, "Cap to scale at discharge %d/%d\n",
1275 cs
->cap_to_scale
[0], cs
->cap_to_scale
[1]);
1280 * ab8500_fg_check_capacity_limits() - Check if capacity has changed
1281 * @di: pointer to the ab8500_fg structure
1282 * @init: capacity is allowed to go up in init mode
1284 * Check if capacity or capacity limit has changed and notify the system
1285 * about it using the power_supply framework
1287 static void ab8500_fg_check_capacity_limits(struct ab8500_fg
*di
, bool init
)
1289 bool changed
= false;
1290 int percent
= DIV_ROUND_CLOSEST(di
->bat_cap
.permille
, 10);
1292 di
->bat_cap
.level
= ab8500_fg_capacity_level(di
);
1294 if (di
->bat_cap
.level
!= di
->bat_cap
.prev_level
) {
1296 * We do not allow reported capacity level to go up
1297 * unless we're charging or if we're in init
1299 if (!(!di
->flags
.charging
&& di
->bat_cap
.level
>
1300 di
->bat_cap
.prev_level
) || init
) {
1301 dev_dbg(di
->dev
, "level changed from %d to %d\n",
1302 di
->bat_cap
.prev_level
,
1304 di
->bat_cap
.prev_level
= di
->bat_cap
.level
;
1307 dev_dbg(di
->dev
, "level not allowed to go up "
1308 "since no charger is connected: %d to %d\n",
1309 di
->bat_cap
.prev_level
,
1315 * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate
1318 if (di
->flags
.low_bat
) {
1319 dev_dbg(di
->dev
, "Battery low, set capacity to 0\n");
1320 di
->bat_cap
.prev_percent
= 0;
1321 di
->bat_cap
.permille
= 0;
1323 di
->bat_cap
.prev_mah
= 0;
1324 di
->bat_cap
.mah
= 0;
1326 } else if (di
->flags
.fully_charged
) {
1328 * We report 100% if algorithm reported fully charged
1329 * and show 100% during maintenance charging (scaling).
1331 if (di
->flags
.force_full
) {
1332 di
->bat_cap
.prev_percent
= percent
;
1333 di
->bat_cap
.prev_mah
= di
->bat_cap
.mah
;
1337 if (!di
->bat_cap
.cap_scale
.enable
&&
1338 di
->bm
->capacity_scaling
) {
1339 di
->bat_cap
.cap_scale
.enable
= true;
1340 di
->bat_cap
.cap_scale
.cap_to_scale
[0] = 100;
1341 di
->bat_cap
.cap_scale
.cap_to_scale
[1] =
1342 di
->bat_cap
.prev_percent
;
1343 di
->bat_cap
.cap_scale
.disable_cap_level
= 100;
1345 } else if (di
->bat_cap
.prev_percent
!= percent
) {
1347 "battery reported full "
1348 "but capacity dropping: %d\n",
1350 di
->bat_cap
.prev_percent
= percent
;
1351 di
->bat_cap
.prev_mah
= di
->bat_cap
.mah
;
1355 } else if (di
->bat_cap
.prev_percent
!= percent
) {
1358 * We will not report 0% unless we've got
1359 * the LOW_BAT IRQ, no matter what the FG
1362 di
->bat_cap
.prev_percent
= 1;
1366 } else if (!(!di
->flags
.charging
&&
1367 percent
> di
->bat_cap
.prev_percent
) || init
) {
1369 * We do not allow reported capacity to go up
1370 * unless we're charging or if we're in init
1373 "capacity changed from %d to %d (%d)\n",
1374 di
->bat_cap
.prev_percent
,
1376 di
->bat_cap
.permille
);
1377 di
->bat_cap
.prev_percent
= percent
;
1378 di
->bat_cap
.prev_mah
= di
->bat_cap
.mah
;
1382 dev_dbg(di
->dev
, "capacity not allowed to go up since "
1383 "no charger is connected: %d to %d (%d)\n",
1384 di
->bat_cap
.prev_percent
,
1386 di
->bat_cap
.permille
);
1391 if (di
->bm
->capacity_scaling
) {
1392 di
->bat_cap
.cap_scale
.scaled_cap
=
1393 ab8500_fg_calculate_scaled_capacity(di
);
1395 dev_info(di
->dev
, "capacity=%d (%d)\n",
1396 di
->bat_cap
.prev_percent
,
1397 di
->bat_cap
.cap_scale
.scaled_cap
);
1399 power_supply_changed(di
->fg_psy
);
1400 if (di
->flags
.fully_charged
&& di
->flags
.force_full
) {
1401 dev_dbg(di
->dev
, "Battery full, notifying.\n");
1402 di
->flags
.force_full
= false;
1403 sysfs_notify(&di
->fg_kobject
, NULL
, "charge_full");
1405 sysfs_notify(&di
->fg_kobject
, NULL
, "charge_now");
1409 static void ab8500_fg_charge_state_to(struct ab8500_fg
*di
,
1410 enum ab8500_fg_charge_state new_state
)
1412 dev_dbg(di
->dev
, "Charge state from %d [%s] to %d [%s]\n",
1414 charge_state
[di
->charge_state
],
1416 charge_state
[new_state
]);
1418 di
->charge_state
= new_state
;
1421 static void ab8500_fg_discharge_state_to(struct ab8500_fg
*di
,
1422 enum ab8500_fg_discharge_state new_state
)
1424 dev_dbg(di
->dev
, "Discharge state from %d [%s] to %d [%s]\n",
1425 di
->discharge_state
,
1426 discharge_state
[di
->discharge_state
],
1428 discharge_state
[new_state
]);
1430 di
->discharge_state
= new_state
;
1434 * ab8500_fg_algorithm_charging() - FG algorithm for when charging
1435 * @di: pointer to the ab8500_fg structure
1437 * Battery capacity calculation state machine for when we're charging
1439 static void ab8500_fg_algorithm_charging(struct ab8500_fg
*di
)
1442 * If we change to discharge mode
1443 * we should start with recovery
1445 if (di
->discharge_state
!= AB8500_FG_DISCHARGE_INIT_RECOVERY
)
1446 ab8500_fg_discharge_state_to(di
,
1447 AB8500_FG_DISCHARGE_INIT_RECOVERY
);
1449 switch (di
->charge_state
) {
1450 case AB8500_FG_CHARGE_INIT
:
1451 di
->fg_samples
= SEC_TO_SAMPLE(
1452 di
->bm
->fg_params
->accu_charging
);
1454 ab8500_fg_coulomb_counter(di
, true);
1455 ab8500_fg_charge_state_to(di
, AB8500_FG_CHARGE_READOUT
);
1459 case AB8500_FG_CHARGE_READOUT
:
1461 * Read the FG and calculate the new capacity
1463 mutex_lock(&di
->cc_lock
);
1464 if (!di
->flags
.conv_done
&& !di
->flags
.force_full
) {
1465 /* Wasn't the CC IRQ that got us here */
1466 mutex_unlock(&di
->cc_lock
);
1467 dev_dbg(di
->dev
, "%s CC conv not done\n",
1472 di
->flags
.conv_done
= false;
1473 mutex_unlock(&di
->cc_lock
);
1475 ab8500_fg_calc_cap_charging(di
);
1483 /* Check capacity limits */
1484 ab8500_fg_check_capacity_limits(di
, false);
1487 static void force_capacity(struct ab8500_fg
*di
)
1491 ab8500_fg_clear_cap_samples(di
);
1492 cap
= di
->bat_cap
.user_mah
;
1493 if (cap
> di
->bat_cap
.max_mah_design
) {
1494 dev_dbg(di
->dev
, "Remaining cap %d can't be bigger than total"
1495 " %d\n", cap
, di
->bat_cap
.max_mah_design
);
1496 cap
= di
->bat_cap
.max_mah_design
;
1498 ab8500_fg_fill_cap_sample(di
, di
->bat_cap
.user_mah
);
1499 di
->bat_cap
.permille
= ab8500_fg_convert_mah_to_permille(di
, cap
);
1500 di
->bat_cap
.mah
= cap
;
1501 ab8500_fg_check_capacity_limits(di
, true);
1504 static bool check_sysfs_capacity(struct ab8500_fg
*di
)
1506 int cap
, lower
, upper
;
1509 cap
= di
->bat_cap
.user_mah
;
1511 cap_permille
= ab8500_fg_convert_mah_to_permille(di
,
1512 di
->bat_cap
.user_mah
);
1514 lower
= di
->bat_cap
.permille
- di
->bm
->fg_params
->user_cap_limit
* 10;
1515 upper
= di
->bat_cap
.permille
+ di
->bm
->fg_params
->user_cap_limit
* 10;
1519 /* 1000 is permille, -> 100 percent */
1523 dev_dbg(di
->dev
, "Capacity limits:"
1524 " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
1525 lower
, cap_permille
, upper
, cap
, di
->bat_cap
.mah
);
1527 /* If within limits, use the saved capacity and exit estimation...*/
1528 if (cap_permille
> lower
&& cap_permille
< upper
) {
1529 dev_dbg(di
->dev
, "OK! Using users cap %d uAh now\n", cap
);
1533 dev_dbg(di
->dev
, "Capacity from user out of limits, ignoring");
1538 * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
1539 * @di: pointer to the ab8500_fg structure
1541 * Battery capacity calculation state machine for when we're discharging
1543 static void ab8500_fg_algorithm_discharging(struct ab8500_fg
*di
)
1547 /* If we change to charge mode we should start with init */
1548 if (di
->charge_state
!= AB8500_FG_CHARGE_INIT
)
1549 ab8500_fg_charge_state_to(di
, AB8500_FG_CHARGE_INIT
);
1551 switch (di
->discharge_state
) {
1552 case AB8500_FG_DISCHARGE_INIT
:
1553 /* We use the FG IRQ to work on */
1555 di
->fg_samples
= SEC_TO_SAMPLE(di
->bm
->fg_params
->init_timer
);
1556 ab8500_fg_coulomb_counter(di
, true);
1557 ab8500_fg_discharge_state_to(di
,
1558 AB8500_FG_DISCHARGE_INITMEASURING
);
1561 case AB8500_FG_DISCHARGE_INITMEASURING
:
1563 * Discard a number of samples during startup.
1564 * After that, use compensated voltage for a few
1565 * samples to get an initial capacity.
1566 * Then go to READOUT
1568 sleep_time
= di
->bm
->fg_params
->init_timer
;
1570 /* Discard the first [x] seconds */
1571 if (di
->init_cnt
> di
->bm
->fg_params
->init_discard_time
) {
1572 ab8500_fg_calc_cap_discharge_voltage(di
);
1574 ab8500_fg_check_capacity_limits(di
, true);
1577 di
->init_cnt
+= sleep_time
;
1578 if (di
->init_cnt
> di
->bm
->fg_params
->init_total_time
)
1579 ab8500_fg_discharge_state_to(di
,
1580 AB8500_FG_DISCHARGE_READOUT_INIT
);
1584 case AB8500_FG_DISCHARGE_INIT_RECOVERY
:
1585 di
->recovery_cnt
= 0;
1586 di
->recovery_needed
= true;
1587 ab8500_fg_discharge_state_to(di
,
1588 AB8500_FG_DISCHARGE_RECOVERY
);
1592 case AB8500_FG_DISCHARGE_RECOVERY
:
1593 sleep_time
= di
->bm
->fg_params
->recovery_sleep_timer
;
1596 * We should check the power consumption
1597 * If low, go to READOUT (after x min) or
1598 * RECOVERY_SLEEP if time left.
1599 * If high, go to READOUT
1601 di
->inst_curr_ua
= ab8500_fg_inst_curr_blocking(di
);
1603 if (ab8500_fg_is_low_curr(di
, di
->inst_curr_ua
)) {
1604 if (di
->recovery_cnt
>
1605 di
->bm
->fg_params
->recovery_total_time
) {
1606 di
->fg_samples
= SEC_TO_SAMPLE(
1607 di
->bm
->fg_params
->accu_high_curr
);
1608 ab8500_fg_coulomb_counter(di
, true);
1609 ab8500_fg_discharge_state_to(di
,
1610 AB8500_FG_DISCHARGE_READOUT
);
1611 di
->recovery_needed
= false;
1613 queue_delayed_work(di
->fg_wq
,
1614 &di
->fg_periodic_work
,
1617 di
->recovery_cnt
+= sleep_time
;
1619 di
->fg_samples
= SEC_TO_SAMPLE(
1620 di
->bm
->fg_params
->accu_high_curr
);
1621 ab8500_fg_coulomb_counter(di
, true);
1622 ab8500_fg_discharge_state_to(di
,
1623 AB8500_FG_DISCHARGE_READOUT
);
1627 case AB8500_FG_DISCHARGE_READOUT_INIT
:
1628 di
->fg_samples
= SEC_TO_SAMPLE(
1629 di
->bm
->fg_params
->accu_high_curr
);
1630 ab8500_fg_coulomb_counter(di
, true);
1631 ab8500_fg_discharge_state_to(di
,
1632 AB8500_FG_DISCHARGE_READOUT
);
1635 case AB8500_FG_DISCHARGE_READOUT
:
1636 di
->inst_curr_ua
= ab8500_fg_inst_curr_blocking(di
);
1638 if (ab8500_fg_is_low_curr(di
, di
->inst_curr_ua
)) {
1639 /* Detect mode change */
1640 if (di
->high_curr_mode
) {
1641 di
->high_curr_mode
= false;
1642 di
->high_curr_cnt
= 0;
1645 if (di
->recovery_needed
) {
1646 ab8500_fg_discharge_state_to(di
,
1647 AB8500_FG_DISCHARGE_INIT_RECOVERY
);
1649 queue_delayed_work(di
->fg_wq
,
1650 &di
->fg_periodic_work
, 0);
1655 ab8500_fg_calc_cap_discharge_voltage(di
);
1657 mutex_lock(&di
->cc_lock
);
1658 if (!di
->flags
.conv_done
) {
1659 /* Wasn't the CC IRQ that got us here */
1660 mutex_unlock(&di
->cc_lock
);
1661 dev_dbg(di
->dev
, "%s CC conv not done\n",
1666 di
->flags
.conv_done
= false;
1667 mutex_unlock(&di
->cc_lock
);
1669 /* Detect mode change */
1670 if (!di
->high_curr_mode
) {
1671 di
->high_curr_mode
= true;
1672 di
->high_curr_cnt
= 0;
1675 di
->high_curr_cnt
+=
1676 di
->bm
->fg_params
->accu_high_curr
;
1677 if (di
->high_curr_cnt
>
1678 di
->bm
->fg_params
->high_curr_time
)
1679 di
->recovery_needed
= true;
1681 ab8500_fg_calc_cap_discharge_fg(di
);
1684 ab8500_fg_check_capacity_limits(di
, false);
1688 case AB8500_FG_DISCHARGE_WAKEUP
:
1689 ab8500_fg_calc_cap_discharge_voltage(di
);
1691 di
->fg_samples
= SEC_TO_SAMPLE(
1692 di
->bm
->fg_params
->accu_high_curr
);
1693 ab8500_fg_coulomb_counter(di
, true);
1694 ab8500_fg_discharge_state_to(di
,
1695 AB8500_FG_DISCHARGE_READOUT
);
1697 ab8500_fg_check_capacity_limits(di
, false);
1707 * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
1708 * @di: pointer to the ab8500_fg structure
1711 static void ab8500_fg_algorithm_calibrate(struct ab8500_fg
*di
)
1715 switch (di
->calib_state
) {
1716 case AB8500_FG_CALIB_INIT
:
1717 dev_dbg(di
->dev
, "Calibration ongoing...\n");
1719 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
1720 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
,
1721 CC_INT_CAL_N_AVG_MASK
, CC_INT_CAL_SAMPLES_8
);
1725 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
1726 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
,
1727 CC_INTAVGOFFSET_ENA
, CC_INTAVGOFFSET_ENA
);
1730 di
->calib_state
= AB8500_FG_CALIB_WAIT
;
1732 case AB8500_FG_CALIB_END
:
1733 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
1734 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
,
1735 CC_MUXOFFSET
, CC_MUXOFFSET
);
1738 di
->flags
.calibrate
= false;
1739 dev_dbg(di
->dev
, "Calibration done...\n");
1740 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
1742 case AB8500_FG_CALIB_WAIT
:
1743 dev_dbg(di
->dev
, "Calibration WFI\n");
1750 /* Something went wrong, don't calibrate then */
1751 dev_err(di
->dev
, "failed to calibrate the CC\n");
1752 di
->flags
.calibrate
= false;
1753 di
->calib_state
= AB8500_FG_CALIB_INIT
;
1754 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
1758 * ab8500_fg_algorithm() - Entry point for the FG algorithm
1759 * @di: pointer to the ab8500_fg structure
1761 * Entry point for the battery capacity calculation state machine
1763 static void ab8500_fg_algorithm(struct ab8500_fg
*di
)
1765 if (di
->flags
.calibrate
)
1766 ab8500_fg_algorithm_calibrate(di
);
1768 if (di
->flags
.charging
)
1769 ab8500_fg_algorithm_charging(di
);
1771 ab8500_fg_algorithm_discharging(di
);
1774 dev_dbg(di
->dev
, "[FG_DATA] %d %d %d %d %d %d %d %d %d %d "
1775 "%d %d %d %d %d %d %d\n",
1776 di
->bat_cap
.max_mah_design
,
1777 di
->bat_cap
.max_mah
,
1779 di
->bat_cap
.permille
,
1781 di
->bat_cap
.prev_mah
,
1782 di
->bat_cap
.prev_percent
,
1783 di
->bat_cap
.prev_level
,
1790 di
->discharge_state
,
1792 di
->recovery_needed
);
1796 * ab8500_fg_periodic_work() - Run the FG state machine periodically
1797 * @work: pointer to the work_struct structure
1799 * Work queue function for periodic work
1801 static void ab8500_fg_periodic_work(struct work_struct
*work
)
1803 struct ab8500_fg
*di
= container_of(work
, struct ab8500_fg
,
1804 fg_periodic_work
.work
);
1806 if (di
->init_capacity
) {
1807 /* Get an initial capacity calculation */
1808 ab8500_fg_calc_cap_discharge_voltage(di
);
1809 ab8500_fg_check_capacity_limits(di
, true);
1810 di
->init_capacity
= false;
1812 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
1813 } else if (di
->flags
.user_cap
) {
1814 if (check_sysfs_capacity(di
)) {
1815 ab8500_fg_check_capacity_limits(di
, true);
1816 if (di
->flags
.charging
)
1817 ab8500_fg_charge_state_to(di
,
1818 AB8500_FG_CHARGE_INIT
);
1820 ab8500_fg_discharge_state_to(di
,
1821 AB8500_FG_DISCHARGE_READOUT_INIT
);
1823 di
->flags
.user_cap
= false;
1824 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
1826 ab8500_fg_algorithm(di
);
1831 * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
1832 * @work: pointer to the work_struct structure
1834 * Work queue function for checking the OVV_BAT condition
1836 static void ab8500_fg_check_hw_failure_work(struct work_struct
*work
)
1841 struct ab8500_fg
*di
= container_of(work
, struct ab8500_fg
,
1842 fg_check_hw_failure_work
.work
);
1845 * If we have had a battery over-voltage situation,
1846 * check ovv-bit to see if it should be reset.
1848 ret
= abx500_get_register_interruptible(di
->dev
,
1849 AB8500_CHARGER
, AB8500_CH_STAT_REG
,
1852 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
1855 if ((reg_value
& BATT_OVV
) == BATT_OVV
) {
1856 if (!di
->flags
.bat_ovv
) {
1857 dev_dbg(di
->dev
, "Battery OVV\n");
1858 di
->flags
.bat_ovv
= true;
1859 power_supply_changed(di
->fg_psy
);
1861 /* Not yet recovered from ovv, reschedule this test */
1862 queue_delayed_work(di
->fg_wq
, &di
->fg_check_hw_failure_work
,
1865 dev_dbg(di
->dev
, "Battery recovered from OVV\n");
1866 di
->flags
.bat_ovv
= false;
1867 power_supply_changed(di
->fg_psy
);
1872 * ab8500_fg_low_bat_work() - Check LOW_BAT condition
1873 * @work: pointer to the work_struct structure
1875 * Work queue function for checking the LOW_BAT condition
1877 static void ab8500_fg_low_bat_work(struct work_struct
*work
)
1881 struct ab8500_fg
*di
= container_of(work
, struct ab8500_fg
,
1882 fg_low_bat_work
.work
);
1884 vbat_uv
= ab8500_fg_bat_voltage(di
);
1886 /* Check if LOW_BAT still fulfilled */
1887 if (vbat_uv
< di
->bm
->fg_params
->lowbat_threshold_uv
) {
1888 /* Is it time to shut down? */
1889 if (di
->low_bat_cnt
< 1) {
1890 di
->flags
.low_bat
= true;
1891 dev_warn(di
->dev
, "Shut down pending...\n");
1894 * Else we need to re-schedule this check to be able to detect
1895 * if the voltage increases again during charging or
1896 * due to decreasing load.
1899 dev_warn(di
->dev
, "Battery voltage still LOW\n");
1900 queue_delayed_work(di
->fg_wq
, &di
->fg_low_bat_work
,
1901 round_jiffies(LOW_BAT_CHECK_INTERVAL
));
1904 di
->flags
.low_bat_delay
= false;
1905 di
->low_bat_cnt
= 10;
1906 dev_warn(di
->dev
, "Battery voltage OK again\n");
1909 /* This is needed to dispatch LOW_BAT */
1910 ab8500_fg_check_capacity_limits(di
, false);
1914 * ab8500_fg_battok_calc - calculate the bit pattern corresponding
1915 * to the target voltage.
1916 * @di: pointer to the ab8500_fg structure
1917 * @target: target voltage
1919 * Returns bit pattern closest to the target voltage
1920 * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
1923 static int ab8500_fg_battok_calc(struct ab8500_fg
*di
, int target
)
1925 if (target
> BATT_OK_MIN
+
1926 (BATT_OK_INCREMENT
* BATT_OK_MAX_NR_INCREMENTS
))
1927 return BATT_OK_MAX_NR_INCREMENTS
;
1928 if (target
< BATT_OK_MIN
)
1930 return (target
- BATT_OK_MIN
) / BATT_OK_INCREMENT
;
1934 * ab8500_fg_battok_init_hw_register - init battok levels
1935 * @di: pointer to the ab8500_fg structure
1939 static int ab8500_fg_battok_init_hw_register(struct ab8500_fg
*di
)
1949 sel0
= di
->bm
->fg_params
->battok_falling_th_sel0
;
1950 sel1
= di
->bm
->fg_params
->battok_raising_th_sel1
;
1952 cbp_sel0
= ab8500_fg_battok_calc(di
, sel0
);
1953 cbp_sel1
= ab8500_fg_battok_calc(di
, sel1
);
1955 selected
= BATT_OK_MIN
+ cbp_sel0
* BATT_OK_INCREMENT
;
1957 if (selected
!= sel0
)
1958 dev_warn(di
->dev
, "Invalid voltage step:%d, using %d %d\n",
1959 sel0
, selected
, cbp_sel0
);
1961 selected
= BATT_OK_MIN
+ cbp_sel1
* BATT_OK_INCREMENT
;
1963 if (selected
!= sel1
)
1964 dev_warn(di
->dev
, "Invalid voltage step:%d, using %d %d\n",
1965 sel1
, selected
, cbp_sel1
);
1967 new_val
= cbp_sel0
| (cbp_sel1
<< 4);
1969 dev_dbg(di
->dev
, "using: %x %d %d\n", new_val
, cbp_sel0
, cbp_sel1
);
1970 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_SYS_CTRL2_BLOCK
,
1971 AB8500_BATT_OK_REG
, new_val
);
1976 * ab8500_fg_instant_work() - Run the FG state machine instantly
1977 * @work: pointer to the work_struct structure
1979 * Work queue function for instant work
1981 static void ab8500_fg_instant_work(struct work_struct
*work
)
1983 struct ab8500_fg
*di
= container_of(work
, struct ab8500_fg
, fg_work
);
1985 ab8500_fg_algorithm(di
);
1989 * ab8500_fg_cc_data_end_handler() - end of data conversion isr.
1990 * @irq: interrupt number
1991 * @_di: pointer to the ab8500_fg structure
1993 * Returns IRQ status(IRQ_HANDLED)
1995 static irqreturn_t
ab8500_fg_cc_data_end_handler(int irq
, void *_di
)
1997 struct ab8500_fg
*di
= _di
;
1998 if (!di
->nbr_cceoc_irq_cnt
) {
1999 di
->nbr_cceoc_irq_cnt
++;
2000 complete(&di
->ab8500_fg_started
);
2002 di
->nbr_cceoc_irq_cnt
= 0;
2003 complete(&di
->ab8500_fg_complete
);
2009 * ab8500_fg_cc_int_calib_handler () - end of calibration isr.
2010 * @irq: interrupt number
2011 * @_di: pointer to the ab8500_fg structure
2013 * Returns IRQ status(IRQ_HANDLED)
2015 static irqreturn_t
ab8500_fg_cc_int_calib_handler(int irq
, void *_di
)
2017 struct ab8500_fg
*di
= _di
;
2018 di
->calib_state
= AB8500_FG_CALIB_END
;
2019 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
2024 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
2025 * @irq: interrupt number
2026 * @_di: pointer to the ab8500_fg structure
2028 * Returns IRQ status(IRQ_HANDLED)
2030 static irqreturn_t
ab8500_fg_cc_convend_handler(int irq
, void *_di
)
2032 struct ab8500_fg
*di
= _di
;
2034 queue_work(di
->fg_wq
, &di
->fg_acc_cur_work
);
2040 * ab8500_fg_batt_ovv_handler() - Battery OVV occured
2041 * @irq: interrupt number
2042 * @_di: pointer to the ab8500_fg structure
2044 * Returns IRQ status(IRQ_HANDLED)
2046 static irqreturn_t
ab8500_fg_batt_ovv_handler(int irq
, void *_di
)
2048 struct ab8500_fg
*di
= _di
;
2050 dev_dbg(di
->dev
, "Battery OVV\n");
2052 /* Schedule a new HW failure check */
2053 queue_delayed_work(di
->fg_wq
, &di
->fg_check_hw_failure_work
, 0);
2059 * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
2060 * @irq: interrupt number
2061 * @_di: pointer to the ab8500_fg structure
2063 * Returns IRQ status(IRQ_HANDLED)
2065 static irqreturn_t
ab8500_fg_lowbatf_handler(int irq
, void *_di
)
2067 struct ab8500_fg
*di
= _di
;
2069 /* Initiate handling in ab8500_fg_low_bat_work() if not already initiated. */
2070 if (!di
->flags
.low_bat_delay
) {
2071 dev_warn(di
->dev
, "Battery voltage is below LOW threshold\n");
2072 di
->flags
.low_bat_delay
= true;
2074 * Start a timer to check LOW_BAT again after some time
2075 * This is done to avoid shutdown on single voltage dips
2077 queue_delayed_work(di
->fg_wq
, &di
->fg_low_bat_work
,
2078 round_jiffies(LOW_BAT_CHECK_INTERVAL
));
2084 * ab8500_fg_get_property() - get the fg properties
2085 * @psy: pointer to the power_supply structure
2086 * @psp: pointer to the power_supply_property structure
2087 * @val: pointer to the power_supply_propval union
2089 * This function gets called when an application tries to get the
2090 * fg properties by reading the sysfs files.
2091 * voltage_now: battery voltage
2092 * current_now: battery instant current
2093 * current_avg: battery average current
2094 * charge_full_design: capacity where battery is considered full
2095 * charge_now: battery capacity in nAh
2096 * capacity: capacity in percent
2097 * capacity_level: capacity level
2099 * Returns error code in case of failure else 0 on success
2101 static int ab8500_fg_get_property(struct power_supply
*psy
,
2102 enum power_supply_property psp
,
2103 union power_supply_propval
*val
)
2105 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2108 * If battery is identified as unknown and charging of unknown
2109 * batteries is disabled, we always report 100% capacity and
2110 * capacity level UNKNOWN, since we can't calculate
2111 * remaining capacity
2115 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
2116 if (di
->flags
.bat_ovv
)
2117 val
->intval
= BATT_OVV_VALUE
;
2119 val
->intval
= di
->vbat_uv
;
2121 case POWER_SUPPLY_PROP_CURRENT_NOW
:
2122 val
->intval
= di
->inst_curr_ua
;
2124 case POWER_SUPPLY_PROP_CURRENT_AVG
:
2125 val
->intval
= di
->avg_curr_ua
;
2127 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
2128 val
->intval
= ab8500_fg_convert_mah_to_uwh(di
,
2129 di
->bat_cap
.max_mah_design
);
2131 case POWER_SUPPLY_PROP_ENERGY_FULL
:
2132 val
->intval
= ab8500_fg_convert_mah_to_uwh(di
,
2133 di
->bat_cap
.max_mah
);
2135 case POWER_SUPPLY_PROP_ENERGY_NOW
:
2136 if (di
->flags
.batt_unknown
&& !di
->bm
->chg_unknown_bat
&&
2137 di
->flags
.batt_id_received
)
2138 val
->intval
= ab8500_fg_convert_mah_to_uwh(di
,
2139 di
->bat_cap
.max_mah
);
2141 val
->intval
= ab8500_fg_convert_mah_to_uwh(di
,
2142 di
->bat_cap
.prev_mah
);
2144 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
2145 val
->intval
= di
->bat_cap
.max_mah_design
;
2147 case POWER_SUPPLY_PROP_CHARGE_FULL
:
2148 val
->intval
= di
->bat_cap
.max_mah
;
2150 case POWER_SUPPLY_PROP_CHARGE_NOW
:
2151 if (di
->flags
.batt_unknown
&& !di
->bm
->chg_unknown_bat
&&
2152 di
->flags
.batt_id_received
)
2153 val
->intval
= di
->bat_cap
.max_mah
;
2155 val
->intval
= di
->bat_cap
.prev_mah
;
2157 case POWER_SUPPLY_PROP_CAPACITY
:
2158 if (di
->flags
.batt_unknown
&& !di
->bm
->chg_unknown_bat
&&
2159 di
->flags
.batt_id_received
)
2162 val
->intval
= di
->bat_cap
.prev_percent
;
2164 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
2165 if (di
->flags
.batt_unknown
&& !di
->bm
->chg_unknown_bat
&&
2166 di
->flags
.batt_id_received
)
2167 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
2169 val
->intval
= di
->bat_cap
.prev_level
;
2177 static int ab8500_fg_get_ext_psy_data(struct device
*dev
, void *data
)
2179 struct power_supply
*psy
;
2180 struct power_supply
*ext
= dev_get_drvdata(dev
);
2181 const char **supplicants
= (const char **)ext
->supplied_to
;
2182 struct ab8500_fg
*di
;
2183 struct power_supply_battery_info
*bi
;
2184 union power_supply_propval ret
;
2187 psy
= (struct power_supply
*)data
;
2188 di
= power_supply_get_drvdata(psy
);
2192 * For all psy where the name of your driver
2193 * appears in any supplied_to
2195 j
= match_string(supplicants
, ext
->num_supplicants
, psy
->desc
->name
);
2199 /* Go through all properties for the psy */
2200 for (j
= 0; j
< ext
->desc
->num_properties
; j
++) {
2201 enum power_supply_property prop
;
2202 prop
= ext
->desc
->properties
[j
];
2204 if (power_supply_get_property(ext
, prop
, &ret
))
2208 case POWER_SUPPLY_PROP_STATUS
:
2209 switch (ext
->desc
->type
) {
2210 case POWER_SUPPLY_TYPE_BATTERY
:
2211 switch (ret
.intval
) {
2212 case POWER_SUPPLY_STATUS_UNKNOWN
:
2213 case POWER_SUPPLY_STATUS_DISCHARGING
:
2214 case POWER_SUPPLY_STATUS_NOT_CHARGING
:
2215 if (!di
->flags
.charging
)
2217 di
->flags
.charging
= false;
2218 di
->flags
.fully_charged
= false;
2219 if (di
->bm
->capacity_scaling
)
2220 ab8500_fg_update_cap_scalers(di
);
2221 queue_work(di
->fg_wq
, &di
->fg_work
);
2223 case POWER_SUPPLY_STATUS_FULL
:
2224 if (di
->flags
.fully_charged
)
2226 di
->flags
.fully_charged
= true;
2227 di
->flags
.force_full
= true;
2228 /* Save current capacity as maximum */
2229 di
->bat_cap
.max_mah
= di
->bat_cap
.mah
;
2230 queue_work(di
->fg_wq
, &di
->fg_work
);
2232 case POWER_SUPPLY_STATUS_CHARGING
:
2233 if (di
->flags
.charging
&&
2234 !di
->flags
.fully_charged
)
2236 di
->flags
.charging
= true;
2237 di
->flags
.fully_charged
= false;
2238 if (di
->bm
->capacity_scaling
)
2239 ab8500_fg_update_cap_scalers(di
);
2240 queue_work(di
->fg_wq
, &di
->fg_work
);
2248 case POWER_SUPPLY_PROP_TECHNOLOGY
:
2249 switch (ext
->desc
->type
) {
2250 case POWER_SUPPLY_TYPE_BATTERY
:
2251 if (!di
->flags
.batt_id_received
&&
2252 (bi
&& (bi
->technology
!=
2253 POWER_SUPPLY_TECHNOLOGY_UNKNOWN
))) {
2254 di
->flags
.batt_id_received
= true;
2256 di
->bat_cap
.max_mah_design
=
2257 di
->bm
->bi
->charge_full_design_uah
;
2259 di
->bat_cap
.max_mah
=
2260 di
->bat_cap
.max_mah_design
;
2263 di
->bm
->bi
->voltage_max_design_uv
;
2267 di
->flags
.batt_unknown
= false;
2269 di
->flags
.batt_unknown
= true;
2275 case POWER_SUPPLY_PROP_TEMP
:
2276 switch (ext
->desc
->type
) {
2277 case POWER_SUPPLY_TYPE_BATTERY
:
2278 if (di
->flags
.batt_id_received
)
2279 di
->bat_temp
= ret
.intval
;
2293 * ab8500_fg_init_hw_registers() - Set up FG related registers
2294 * @di: pointer to the ab8500_fg structure
2296 * Set up battery OVV, low battery voltage registers
2298 static int ab8500_fg_init_hw_registers(struct ab8500_fg
*di
)
2303 * Set VBAT OVV (overvoltage) threshold to 4.75V (typ) this is what
2304 * the hardware supports, nothing else can be configured in hardware.
2305 * See this as an "outer limit" where the charger will certainly
2306 * shut down. Other (lower) overvoltage levels need to be implemented
2309 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
2315 dev_err(di
->dev
, "failed to set BATT_OVV\n");
2319 /* Enable VBAT OVV detection */
2320 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
2326 dev_err(di
->dev
, "failed to enable BATT_OVV\n");
2330 /* Low Battery Voltage */
2331 ret
= abx500_set_register_interruptible(di
->dev
,
2332 AB8500_SYS_CTRL2_BLOCK
,
2334 ab8500_volt_to_regval(
2335 di
->bm
->fg_params
->lowbat_threshold_uv
) << 1 |
2338 dev_err(di
->dev
, "%s write failed\n", __func__
);
2342 /* Battery OK threshold */
2343 ret
= ab8500_fg_battok_init_hw_register(di
);
2345 dev_err(di
->dev
, "BattOk init write failed.\n");
2349 if (is_ab8505(di
->parent
)) {
2350 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2351 AB8505_RTC_PCUT_MAX_TIME_REG
, di
->bm
->fg_params
->pcut_max_time
);
2354 dev_err(di
->dev
, "%s write failed AB8505_RTC_PCUT_MAX_TIME_REG\n", __func__
);
2358 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2359 AB8505_RTC_PCUT_FLAG_TIME_REG
, di
->bm
->fg_params
->pcut_flag_time
);
2362 dev_err(di
->dev
, "%s write failed AB8505_RTC_PCUT_FLAG_TIME_REG\n", __func__
);
2366 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2367 AB8505_RTC_PCUT_RESTART_REG
, di
->bm
->fg_params
->pcut_max_restart
);
2370 dev_err(di
->dev
, "%s write failed AB8505_RTC_PCUT_RESTART_REG\n", __func__
);
2374 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2375 AB8505_RTC_PCUT_DEBOUNCE_REG
, di
->bm
->fg_params
->pcut_debounce_time
);
2378 dev_err(di
->dev
, "%s write failed AB8505_RTC_PCUT_DEBOUNCE_REG\n", __func__
);
2382 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2383 AB8505_RTC_PCUT_CTL_STATUS_REG
, di
->bm
->fg_params
->pcut_enable
);
2386 dev_err(di
->dev
, "%s write failed AB8505_RTC_PCUT_CTL_STATUS_REG\n", __func__
);
2395 * ab8500_fg_external_power_changed() - callback for power supply changes
2396 * @psy: pointer to the structure power_supply
2398 * This function is the entry point of the pointer external_power_changed
2399 * of the structure power_supply.
2400 * This function gets executed when there is a change in any external power
2401 * supply that this driver needs to be notified of.
2403 static void ab8500_fg_external_power_changed(struct power_supply
*psy
)
2405 power_supply_for_each_device(psy
, ab8500_fg_get_ext_psy_data
);
2409 * ab8500_fg_reinit_work() - work to reset the FG algorithm
2410 * @work: pointer to the work_struct structure
2412 * Used to reset the current battery capacity to be able to
2413 * retrigger a new voltage base capacity calculation. For
2414 * test and verification purpose.
2416 static void ab8500_fg_reinit_work(struct work_struct
*work
)
2418 struct ab8500_fg
*di
= container_of(work
, struct ab8500_fg
,
2419 fg_reinit_work
.work
);
2421 if (!di
->flags
.calibrate
) {
2422 dev_dbg(di
->dev
, "Resetting FG state machine to init.\n");
2423 ab8500_fg_clear_cap_samples(di
);
2424 ab8500_fg_calc_cap_discharge_voltage(di
);
2425 ab8500_fg_charge_state_to(di
, AB8500_FG_CHARGE_INIT
);
2426 ab8500_fg_discharge_state_to(di
, AB8500_FG_DISCHARGE_INIT
);
2427 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
2430 dev_err(di
->dev
, "Residual offset calibration ongoing "
2432 /* Wait one second until next try*/
2433 queue_delayed_work(di
->fg_wq
, &di
->fg_reinit_work
,
2438 /* Exposure to the sysfs interface */
2440 struct ab8500_fg_sysfs_entry
{
2441 struct attribute attr
;
2442 ssize_t (*show
)(struct ab8500_fg
*, char *);
2443 ssize_t (*store
)(struct ab8500_fg
*, const char *, size_t);
2446 static ssize_t
charge_full_show(struct ab8500_fg
*di
, char *buf
)
2448 return sysfs_emit(buf
, "%d\n", di
->bat_cap
.max_mah
);
2451 static ssize_t
charge_full_store(struct ab8500_fg
*di
, const char *buf
,
2454 unsigned long charge_full
;
2457 ret
= kstrtoul(buf
, 10, &charge_full
);
2461 di
->bat_cap
.max_mah
= (int) charge_full
;
2465 static ssize_t
charge_now_show(struct ab8500_fg
*di
, char *buf
)
2467 return sysfs_emit(buf
, "%d\n", di
->bat_cap
.prev_mah
);
2470 static ssize_t
charge_now_store(struct ab8500_fg
*di
, const char *buf
,
2473 unsigned long charge_now
;
2476 ret
= kstrtoul(buf
, 10, &charge_now
);
2480 di
->bat_cap
.user_mah
= (int) charge_now
;
2481 di
->flags
.user_cap
= true;
2482 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
2486 static struct ab8500_fg_sysfs_entry charge_full_attr
=
2487 __ATTR(charge_full
, 0644, charge_full_show
, charge_full_store
);
2489 static struct ab8500_fg_sysfs_entry charge_now_attr
=
2490 __ATTR(charge_now
, 0644, charge_now_show
, charge_now_store
);
2493 ab8500_fg_show(struct kobject
*kobj
, struct attribute
*attr
, char *buf
)
2495 struct ab8500_fg_sysfs_entry
*entry
;
2496 struct ab8500_fg
*di
;
2498 entry
= container_of(attr
, struct ab8500_fg_sysfs_entry
, attr
);
2499 di
= container_of(kobj
, struct ab8500_fg
, fg_kobject
);
2504 return entry
->show(di
, buf
);
2507 ab8500_fg_store(struct kobject
*kobj
, struct attribute
*attr
, const char *buf
,
2510 struct ab8500_fg_sysfs_entry
*entry
;
2511 struct ab8500_fg
*di
;
2513 entry
= container_of(attr
, struct ab8500_fg_sysfs_entry
, attr
);
2514 di
= container_of(kobj
, struct ab8500_fg
, fg_kobject
);
2519 return entry
->store(di
, buf
, count
);
2522 static const struct sysfs_ops ab8500_fg_sysfs_ops
= {
2523 .show
= ab8500_fg_show
,
2524 .store
= ab8500_fg_store
,
2527 static struct attribute
*ab8500_fg_attrs
[] = {
2528 &charge_full_attr
.attr
,
2529 &charge_now_attr
.attr
,
2532 ATTRIBUTE_GROUPS(ab8500_fg
);
2534 static const struct kobj_type ab8500_fg_ktype
= {
2535 .sysfs_ops
= &ab8500_fg_sysfs_ops
,
2536 .default_groups
= ab8500_fg_groups
,
2540 * ab8500_fg_sysfs_exit() - de-init of sysfs entry
2541 * @di: pointer to the struct ab8500_chargalg
2543 * This function removes the entry in sysfs.
2545 static void ab8500_fg_sysfs_exit(struct ab8500_fg
*di
)
2547 kobject_del(&di
->fg_kobject
);
2551 * ab8500_fg_sysfs_init() - init of sysfs entry
2552 * @di: pointer to the struct ab8500_chargalg
2554 * This function adds an entry in sysfs.
2555 * Returns error code in case of failure else 0(on success)
2557 static int ab8500_fg_sysfs_init(struct ab8500_fg
*di
)
2561 ret
= kobject_init_and_add(&di
->fg_kobject
,
2565 kobject_put(&di
->fg_kobject
);
2566 dev_err(di
->dev
, "failed to create sysfs entry\n");
2572 static ssize_t
ab8505_powercut_flagtime_read(struct device
*dev
,
2573 struct device_attribute
*attr
,
2578 struct power_supply
*psy
= dev_get_drvdata(dev
);
2579 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2581 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2582 AB8505_RTC_PCUT_FLAG_TIME_REG
, ®_value
);
2585 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2589 return sysfs_emit(buf
, "%d\n", (reg_value
& 0x7F));
2595 static ssize_t
ab8505_powercut_flagtime_write(struct device
*dev
,
2596 struct device_attribute
*attr
,
2597 const char *buf
, size_t count
)
2601 struct power_supply
*psy
= dev_get_drvdata(dev
);
2602 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2604 if (kstrtoint(buf
, 10, ®_value
))
2607 if (reg_value
> 0x7F) {
2608 dev_err(dev
, "Incorrect parameter, echo 0 (1.98s) - 127 (15.625ms) for flagtime\n");
2612 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2613 AB8505_RTC_PCUT_FLAG_TIME_REG
, (u8
)reg_value
);
2616 dev_err(dev
, "Failed to set AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2622 static ssize_t
ab8505_powercut_maxtime_read(struct device
*dev
,
2623 struct device_attribute
*attr
,
2628 struct power_supply
*psy
= dev_get_drvdata(dev
);
2629 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2631 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2632 AB8505_RTC_PCUT_MAX_TIME_REG
, ®_value
);
2635 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_MAX_TIME_REG\n");
2639 return sysfs_emit(buf
, "%d\n", (reg_value
& 0x7F));
2646 static ssize_t
ab8505_powercut_maxtime_write(struct device
*dev
,
2647 struct device_attribute
*attr
,
2648 const char *buf
, size_t count
)
2652 struct power_supply
*psy
= dev_get_drvdata(dev
);
2653 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2655 if (kstrtoint(buf
, 10, ®_value
))
2658 if (reg_value
> 0x7F) {
2659 dev_err(dev
, "Incorrect parameter, echo 0 (0.0s) - 127 (1.98s) for maxtime\n");
2663 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2664 AB8505_RTC_PCUT_MAX_TIME_REG
, (u8
)reg_value
);
2667 dev_err(dev
, "Failed to set AB8505_RTC_PCUT_MAX_TIME_REG\n");
2673 static ssize_t
ab8505_powercut_restart_read(struct device
*dev
,
2674 struct device_attribute
*attr
,
2679 struct power_supply
*psy
= dev_get_drvdata(dev
);
2680 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2682 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2683 AB8505_RTC_PCUT_RESTART_REG
, ®_value
);
2686 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2690 return sysfs_emit(buf
, "%d\n", (reg_value
& 0xF));
2696 static ssize_t
ab8505_powercut_restart_write(struct device
*dev
,
2697 struct device_attribute
*attr
,
2698 const char *buf
, size_t count
)
2702 struct power_supply
*psy
= dev_get_drvdata(dev
);
2703 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2705 if (kstrtoint(buf
, 10, ®_value
))
2708 if (reg_value
> 0xF) {
2709 dev_err(dev
, "Incorrect parameter, echo 0 - 15 for number of restart\n");
2713 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2714 AB8505_RTC_PCUT_RESTART_REG
, (u8
)reg_value
);
2717 dev_err(dev
, "Failed to set AB8505_RTC_PCUT_RESTART_REG\n");
2724 static ssize_t
ab8505_powercut_timer_read(struct device
*dev
,
2725 struct device_attribute
*attr
,
2730 struct power_supply
*psy
= dev_get_drvdata(dev
);
2731 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2733 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2734 AB8505_RTC_PCUT_TIME_REG
, ®_value
);
2737 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_TIME_REG\n");
2741 return sysfs_emit(buf
, "%d\n", (reg_value
& 0x7F));
2747 static ssize_t
ab8505_powercut_restart_counter_read(struct device
*dev
,
2748 struct device_attribute
*attr
,
2753 struct power_supply
*psy
= dev_get_drvdata(dev
);
2754 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2756 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2757 AB8505_RTC_PCUT_RESTART_REG
, ®_value
);
2760 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2764 return sysfs_emit(buf
, "%d\n", (reg_value
& 0xF0) >> 4);
2770 static ssize_t
ab8505_powercut_read(struct device
*dev
,
2771 struct device_attribute
*attr
,
2776 struct power_supply
*psy
= dev_get_drvdata(dev
);
2777 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2779 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2780 AB8505_RTC_PCUT_CTL_STATUS_REG
, ®_value
);
2785 return sysfs_emit(buf
, "%d\n", (reg_value
& 0x1));
2791 static ssize_t
ab8505_powercut_write(struct device
*dev
,
2792 struct device_attribute
*attr
,
2793 const char *buf
, size_t count
)
2797 struct power_supply
*psy
= dev_get_drvdata(dev
);
2798 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2800 if (kstrtoint(buf
, 10, ®_value
))
2803 if (reg_value
> 0x1) {
2804 dev_err(dev
, "Incorrect parameter, echo 0/1 to disable/enable Pcut feature\n");
2808 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2809 AB8505_RTC_PCUT_CTL_STATUS_REG
, (u8
)reg_value
);
2812 dev_err(dev
, "Failed to set AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2818 static ssize_t
ab8505_powercut_flag_read(struct device
*dev
,
2819 struct device_attribute
*attr
,
2825 struct power_supply
*psy
= dev_get_drvdata(dev
);
2826 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2828 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2829 AB8505_RTC_PCUT_CTL_STATUS_REG
, ®_value
);
2832 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2836 return sysfs_emit(buf
, "%d\n", ((reg_value
& 0x10) >> 4));
2842 static ssize_t
ab8505_powercut_debounce_read(struct device
*dev
,
2843 struct device_attribute
*attr
,
2848 struct power_supply
*psy
= dev_get_drvdata(dev
);
2849 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2851 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2852 AB8505_RTC_PCUT_DEBOUNCE_REG
, ®_value
);
2855 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2859 return sysfs_emit(buf
, "%d\n", (reg_value
& 0x7));
2865 static ssize_t
ab8505_powercut_debounce_write(struct device
*dev
,
2866 struct device_attribute
*attr
,
2867 const char *buf
, size_t count
)
2871 struct power_supply
*psy
= dev_get_drvdata(dev
);
2872 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2874 if (kstrtoint(buf
, 10, ®_value
))
2877 if (reg_value
> 0x7) {
2878 dev_err(dev
, "Incorrect parameter, echo 0 to 7 for debounce setting\n");
2882 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2883 AB8505_RTC_PCUT_DEBOUNCE_REG
, (u8
)reg_value
);
2886 dev_err(dev
, "Failed to set AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2892 static ssize_t
ab8505_powercut_enable_status_read(struct device
*dev
,
2893 struct device_attribute
*attr
,
2898 struct power_supply
*psy
= dev_get_drvdata(dev
);
2899 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2901 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2902 AB8505_RTC_PCUT_CTL_STATUS_REG
, ®_value
);
2905 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2909 return sysfs_emit(buf
, "%d\n", ((reg_value
& 0x20) >> 5));
2915 static struct device_attribute ab8505_fg_sysfs_psy_attrs
[] = {
2916 __ATTR(powercut_flagtime
, (S_IRUGO
| S_IWUSR
| S_IWGRP
),
2917 ab8505_powercut_flagtime_read
, ab8505_powercut_flagtime_write
),
2918 __ATTR(powercut_maxtime
, (S_IRUGO
| S_IWUSR
| S_IWGRP
),
2919 ab8505_powercut_maxtime_read
, ab8505_powercut_maxtime_write
),
2920 __ATTR(powercut_restart_max
, (S_IRUGO
| S_IWUSR
| S_IWGRP
),
2921 ab8505_powercut_restart_read
, ab8505_powercut_restart_write
),
2922 __ATTR(powercut_timer
, S_IRUGO
, ab8505_powercut_timer_read
, NULL
),
2923 __ATTR(powercut_restart_counter
, S_IRUGO
,
2924 ab8505_powercut_restart_counter_read
, NULL
),
2925 __ATTR(powercut_enable
, (S_IRUGO
| S_IWUSR
| S_IWGRP
),
2926 ab8505_powercut_read
, ab8505_powercut_write
),
2927 __ATTR(powercut_flag
, S_IRUGO
, ab8505_powercut_flag_read
, NULL
),
2928 __ATTR(powercut_debounce_time
, (S_IRUGO
| S_IWUSR
| S_IWGRP
),
2929 ab8505_powercut_debounce_read
, ab8505_powercut_debounce_write
),
2930 __ATTR(powercut_enable_status
, S_IRUGO
,
2931 ab8505_powercut_enable_status_read
, NULL
),
2934 static int ab8500_fg_sysfs_psy_create_attrs(struct ab8500_fg
*di
)
2938 if (is_ab8505(di
->parent
)) {
2939 for (i
= 0; i
< ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs
); i
++)
2940 if (device_create_file(&di
->fg_psy
->dev
,
2941 &ab8505_fg_sysfs_psy_attrs
[i
]))
2942 goto sysfs_psy_create_attrs_failed_ab8505
;
2945 sysfs_psy_create_attrs_failed_ab8505
:
2946 dev_err(&di
->fg_psy
->dev
, "Failed creating sysfs psy attrs for ab8505.\n");
2948 device_remove_file(&di
->fg_psy
->dev
,
2949 &ab8505_fg_sysfs_psy_attrs
[i
]);
2954 static void ab8500_fg_sysfs_psy_remove_attrs(struct ab8500_fg
*di
)
2958 if (is_ab8505(di
->parent
)) {
2959 for (i
= 0; i
< ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs
); i
++)
2960 (void)device_remove_file(&di
->fg_psy
->dev
,
2961 &ab8505_fg_sysfs_psy_attrs
[i
]);
2965 /* Exposure to the sysfs interface <<END>> */
2967 static int __maybe_unused
ab8500_fg_resume(struct device
*dev
)
2969 struct ab8500_fg
*di
= dev_get_drvdata(dev
);
2972 * Change state if we're not charging. If we're charging we will wake
2975 if (!di
->flags
.charging
) {
2976 ab8500_fg_discharge_state_to(di
, AB8500_FG_DISCHARGE_WAKEUP
);
2977 queue_work(di
->fg_wq
, &di
->fg_work
);
2983 static int __maybe_unused
ab8500_fg_suspend(struct device
*dev
)
2985 struct ab8500_fg
*di
= dev_get_drvdata(dev
);
2987 flush_delayed_work(&di
->fg_periodic_work
);
2988 flush_work(&di
->fg_work
);
2989 flush_work(&di
->fg_acc_cur_work
);
2990 flush_delayed_work(&di
->fg_reinit_work
);
2991 flush_delayed_work(&di
->fg_low_bat_work
);
2992 flush_delayed_work(&di
->fg_check_hw_failure_work
);
2995 * If the FG is enabled we will disable it before going to suspend
2996 * only if we're not charging
2998 if (di
->flags
.fg_enabled
&& !di
->flags
.charging
)
2999 ab8500_fg_coulomb_counter(di
, false);
3004 /* ab8500 fg driver interrupts and their respective isr */
3005 static struct ab8500_fg_interrupts ab8500_fg_irq
[] = {
3006 {"NCONV_ACCU", ab8500_fg_cc_convend_handler
},
3007 {"BATT_OVV", ab8500_fg_batt_ovv_handler
},
3008 {"LOW_BAT_F", ab8500_fg_lowbatf_handler
},
3009 {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler
},
3010 {"CCEOC", ab8500_fg_cc_data_end_handler
},
3013 static char *supply_interface
[] = {
3018 static const struct power_supply_desc ab8500_fg_desc
= {
3019 .name
= "ab8500_fg",
3020 .type
= POWER_SUPPLY_TYPE_BATTERY
,
3021 .properties
= ab8500_fg_props
,
3022 .num_properties
= ARRAY_SIZE(ab8500_fg_props
),
3023 .get_property
= ab8500_fg_get_property
,
3024 .external_power_changed
= ab8500_fg_external_power_changed
,
3027 static int ab8500_fg_bind(struct device
*dev
, struct device
*master
,
3030 struct ab8500_fg
*di
= dev_get_drvdata(dev
);
3032 di
->bat_cap
.max_mah_design
= di
->bm
->bi
->charge_full_design_uah
;
3033 di
->bat_cap
.max_mah
= di
->bat_cap
.max_mah_design
;
3034 di
->vbat_nom_uv
= di
->bm
->bi
->voltage_max_design_uv
;
3036 /* Start the coulomb counter */
3037 ab8500_fg_coulomb_counter(di
, true);
3038 /* Run the FG algorithm */
3039 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
3044 static void ab8500_fg_unbind(struct device
*dev
, struct device
*master
,
3047 struct ab8500_fg
*di
= dev_get_drvdata(dev
);
3050 /* Disable coulomb counter */
3051 ret
= ab8500_fg_coulomb_counter(di
, false);
3053 dev_err(dev
, "failed to disable coulomb counter\n");
3055 flush_workqueue(di
->fg_wq
);
3058 static const struct component_ops ab8500_fg_component_ops
= {
3059 .bind
= ab8500_fg_bind
,
3060 .unbind
= ab8500_fg_unbind
,
3063 static int ab8500_fg_probe(struct platform_device
*pdev
)
3065 struct device
*dev
= &pdev
->dev
;
3066 struct power_supply_config psy_cfg
= {};
3067 struct ab8500_fg
*di
;
3071 di
= devm_kzalloc(dev
, sizeof(*di
), GFP_KERNEL
);
3075 di
->bm
= &ab8500_bm_data
;
3077 mutex_init(&di
->cc_lock
);
3079 /* get parent data */
3081 di
->parent
= dev_get_drvdata(pdev
->dev
.parent
);
3083 di
->main_bat_v
= devm_iio_channel_get(dev
, "main_bat_v");
3084 if (IS_ERR(di
->main_bat_v
)) {
3085 ret
= dev_err_probe(dev
, PTR_ERR(di
->main_bat_v
),
3086 "failed to get main battery ADC channel\n");
3090 if (!of_property_read_u32(dev
->of_node
, "line-impedance-micro-ohms",
3091 &di
->line_impedance_uohm
))
3092 dev_info(dev
, "line impedance: %u uOhm\n",
3093 di
->line_impedance_uohm
);
3095 psy_cfg
.supplied_to
= supply_interface
;
3096 psy_cfg
.num_supplicants
= ARRAY_SIZE(supply_interface
);
3097 psy_cfg
.drv_data
= di
;
3099 di
->init_capacity
= true;
3101 ab8500_fg_charge_state_to(di
, AB8500_FG_CHARGE_INIT
);
3102 ab8500_fg_discharge_state_to(di
, AB8500_FG_DISCHARGE_INIT
);
3104 /* Create a work queue for running the FG algorithm */
3105 di
->fg_wq
= alloc_ordered_workqueue("ab8500_fg_wq", WQ_MEM_RECLAIM
);
3106 if (di
->fg_wq
== NULL
) {
3107 dev_err(dev
, "failed to create work queue\n");
3111 /* Init work for running the fg algorithm instantly */
3112 INIT_WORK(&di
->fg_work
, ab8500_fg_instant_work
);
3114 /* Init work for getting the battery accumulated current */
3115 INIT_WORK(&di
->fg_acc_cur_work
, ab8500_fg_acc_cur_work
);
3117 /* Init work for reinitialising the fg algorithm */
3118 INIT_DEFERRABLE_WORK(&di
->fg_reinit_work
,
3119 ab8500_fg_reinit_work
);
3121 /* Work delayed Queue to run the state machine */
3122 INIT_DEFERRABLE_WORK(&di
->fg_periodic_work
,
3123 ab8500_fg_periodic_work
);
3125 /* Work to check low battery condition */
3126 INIT_DEFERRABLE_WORK(&di
->fg_low_bat_work
,
3127 ab8500_fg_low_bat_work
);
3129 /* Init work for HW failure check */
3130 INIT_DEFERRABLE_WORK(&di
->fg_check_hw_failure_work
,
3131 ab8500_fg_check_hw_failure_work
);
3133 /* Reset battery low voltage flag */
3134 di
->flags
.low_bat
= false;
3136 /* Initialize low battery counter */
3137 di
->low_bat_cnt
= 10;
3139 /* Initialize OVV, and other registers */
3140 ret
= ab8500_fg_init_hw_registers(di
);
3142 dev_err(dev
, "failed to initialize registers\n");
3143 destroy_workqueue(di
->fg_wq
);
3147 /* Consider battery unknown until we're informed otherwise */
3148 di
->flags
.batt_unknown
= true;
3149 di
->flags
.batt_id_received
= false;
3151 /* Register FG power supply class */
3152 di
->fg_psy
= devm_power_supply_register(dev
, &ab8500_fg_desc
, &psy_cfg
);
3153 if (IS_ERR(di
->fg_psy
)) {
3154 dev_err(dev
, "failed to register FG psy\n");
3155 destroy_workqueue(di
->fg_wq
);
3156 return PTR_ERR(di
->fg_psy
);
3159 di
->fg_samples
= SEC_TO_SAMPLE(di
->bm
->fg_params
->init_timer
);
3162 * Initialize completion used to notify completion and start
3165 init_completion(&di
->ab8500_fg_started
);
3166 init_completion(&di
->ab8500_fg_complete
);
3168 /* Register primary interrupt handlers */
3169 for (i
= 0; i
< ARRAY_SIZE(ab8500_fg_irq
); i
++) {
3170 irq
= platform_get_irq_byname(pdev
, ab8500_fg_irq
[i
].name
);
3172 destroy_workqueue(di
->fg_wq
);
3176 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
3177 ab8500_fg_irq
[i
].isr
,
3178 IRQF_SHARED
| IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
3179 ab8500_fg_irq
[i
].name
, di
);
3182 dev_err(dev
, "failed to request %s IRQ %d: %d\n",
3183 ab8500_fg_irq
[i
].name
, irq
, ret
);
3184 destroy_workqueue(di
->fg_wq
);
3187 dev_dbg(dev
, "Requested %s IRQ %d: %d\n",
3188 ab8500_fg_irq
[i
].name
, irq
, ret
);
3191 di
->irq
= platform_get_irq_byname(pdev
, "CCEOC");
3192 disable_irq(di
->irq
);
3193 di
->nbr_cceoc_irq_cnt
= 0;
3195 platform_set_drvdata(pdev
, di
);
3197 ret
= ab8500_fg_sysfs_init(di
);
3199 dev_err(dev
, "failed to create sysfs entry\n");
3200 destroy_workqueue(di
->fg_wq
);
3204 ret
= ab8500_fg_sysfs_psy_create_attrs(di
);
3206 dev_err(dev
, "failed to create FG psy\n");
3207 ab8500_fg_sysfs_exit(di
);
3208 destroy_workqueue(di
->fg_wq
);
3212 /* Calibrate the fg first time */
3213 di
->flags
.calibrate
= true;
3214 di
->calib_state
= AB8500_FG_CALIB_INIT
;
3216 /* Use room temp as default value until we get an update from driver. */
3219 list_add_tail(&di
->node
, &ab8500_fg_list
);
3221 return component_add(dev
, &ab8500_fg_component_ops
);
3224 static void ab8500_fg_remove(struct platform_device
*pdev
)
3226 struct ab8500_fg
*di
= platform_get_drvdata(pdev
);
3228 destroy_workqueue(di
->fg_wq
);
3229 component_del(&pdev
->dev
, &ab8500_fg_component_ops
);
3230 list_del(&di
->node
);
3231 ab8500_fg_sysfs_exit(di
);
3232 ab8500_fg_sysfs_psy_remove_attrs(di
);
3235 static SIMPLE_DEV_PM_OPS(ab8500_fg_pm_ops
, ab8500_fg_suspend
, ab8500_fg_resume
);
3237 static const struct of_device_id ab8500_fg_match
[] = {
3238 { .compatible
= "stericsson,ab8500-fg", },
3241 MODULE_DEVICE_TABLE(of
, ab8500_fg_match
);
3243 struct platform_driver ab8500_fg_driver
= {
3244 .probe
= ab8500_fg_probe
,
3245 .remove
= ab8500_fg_remove
,
3247 .name
= "ab8500-fg",
3248 .of_match_table
= ab8500_fg_match
,
3249 .pm
= &ab8500_fg_pm_ops
,
3252 MODULE_LICENSE("GPL v2");
3253 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
3254 MODULE_ALIAS("platform:ab8500-fg");
3255 MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");