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/device.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
23 #include <linux/power_supply.h>
24 #include <linux/kobject.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/time.h>
28 #include <linux/time64.h>
30 #include <linux/completion.h>
31 #include <linux/mfd/core.h>
32 #include <linux/mfd/abx500.h>
33 #include <linux/mfd/abx500/ab8500.h>
34 #include <linux/mfd/abx500/ab8500-bm.h>
35 #include <linux/iio/consumer.h>
36 #include <linux/kernel.h>
38 #define MILLI_TO_MICRO 1000
39 #define FG_LSB_IN_MA 1627
40 #define QLSB_NANO_AMP_HOURS_X10 1071
41 #define INS_CURR_TIMEOUT (3 * HZ)
43 #define SEC_TO_SAMPLE(S) (S * 4)
45 #define NBR_AVG_SAMPLES 20
47 #define LOW_BAT_CHECK_INTERVAL (HZ / 16) /* 62.5 ms */
49 #define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */
50 #define BATT_OK_MIN 2360 /* mV */
51 #define BATT_OK_INCREMENT 50 /* mV */
52 #define BATT_OK_MAX_NR_INCREMENTS 0xE
57 #define interpolate(x, x1, y1, x2, y2) \
58 ((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1))));
61 * struct ab8500_fg_interrupts - ab8500 fg interupts
62 * @name: name of the interrupt
63 * @isr function pointer to the isr
65 struct ab8500_fg_interrupts
{
67 irqreturn_t (*isr
)(int irq
, void *data
);
70 enum ab8500_fg_discharge_state
{
71 AB8500_FG_DISCHARGE_INIT
,
72 AB8500_FG_DISCHARGE_INITMEASURING
,
73 AB8500_FG_DISCHARGE_INIT_RECOVERY
,
74 AB8500_FG_DISCHARGE_RECOVERY
,
75 AB8500_FG_DISCHARGE_READOUT_INIT
,
76 AB8500_FG_DISCHARGE_READOUT
,
77 AB8500_FG_DISCHARGE_WAKEUP
,
80 static char *discharge_state
[] = {
82 "DISCHARGE_INITMEASURING",
83 "DISCHARGE_INIT_RECOVERY",
85 "DISCHARGE_READOUT_INIT",
90 enum ab8500_fg_charge_state
{
91 AB8500_FG_CHARGE_INIT
,
92 AB8500_FG_CHARGE_READOUT
,
95 static char *charge_state
[] = {
100 enum ab8500_fg_calibration_state
{
101 AB8500_FG_CALIB_INIT
,
102 AB8500_FG_CALIB_WAIT
,
106 struct ab8500_fg_avg_cap
{
108 int samples
[NBR_AVG_SAMPLES
];
109 time64_t time_stamps
[NBR_AVG_SAMPLES
];
115 struct ab8500_fg_cap_scaling
{
118 int disable_cap_level
;
122 struct ab8500_fg_battery_capacity
{
132 struct ab8500_fg_cap_scaling cap_scale
;
135 struct ab8500_fg_flags
{
147 bool batt_id_received
;
150 struct inst_curr_result_list
{
151 struct list_head list
;
156 * struct ab8500_fg - ab8500 FG device information
157 * @dev: Pointer to the structure device
158 * @node: a list of AB8500 FGs, hence prepared for reentrance
159 * @irq holds the CCEOC interrupt number
160 * @vbat: Battery voltage in mV
161 * @vbat_nom: Nominal battery voltage in mV
162 * @inst_curr: Instantenous battery current in mA
163 * @avg_curr: Average battery current in mA
164 * @bat_temp battery temperature
165 * @fg_samples: Number of samples used in the FG accumulation
166 * @accu_charge: Accumulated charge from the last conversion
167 * @recovery_cnt: Counter for recovery mode
168 * @high_curr_cnt: Counter for high current mode
169 * @init_cnt: Counter for init mode
170 * @low_bat_cnt Counter for number of consecutive low battery measures
171 * @nbr_cceoc_irq_cnt Counter for number of CCEOC irqs received since enabled
172 * @recovery_needed: Indicate if recovery is needed
173 * @high_curr_mode: Indicate if we're in high current mode
174 * @init_capacity: Indicate if initial capacity measuring should be done
175 * @turn_off_fg: True if fg was off before current measurement
176 * @calib_state State during offset calibration
177 * @discharge_state: Current discharge state
178 * @charge_state: Current charge state
179 * @ab8500_fg_started Completion struct used for the instant current start
180 * @ab8500_fg_complete Completion struct used for the instant current reading
181 * @flags: Structure for information about events triggered
182 * @bat_cap: Structure for battery capacity specific parameters
183 * @avg_cap: Average capacity filter
184 * @parent: Pointer to the struct ab8500
185 * @main_bat_v: ADC channel for the main battery voltage
186 * @bm: Platform specific battery management information
187 * @fg_psy: Structure that holds the FG specific battery properties
188 * @fg_wq: Work queue for running the FG algorithm
189 * @fg_periodic_work: Work to run the FG algorithm periodically
190 * @fg_low_bat_work: Work to check low bat condition
191 * @fg_reinit_work Work used to reset and reinitialise the FG algorithm
192 * @fg_work: Work to run the FG algorithm instantly
193 * @fg_acc_cur_work: Work to read the FG accumulator
194 * @fg_check_hw_failure_work: Work for checking HW state
195 * @cc_lock: Mutex for locking the CC
196 * @fg_kobject: Structure of type kobject
200 struct list_head node
;
213 int nbr_cceoc_irq_cnt
;
214 bool recovery_needed
;
218 enum ab8500_fg_calibration_state calib_state
;
219 enum ab8500_fg_discharge_state discharge_state
;
220 enum ab8500_fg_charge_state charge_state
;
221 struct completion ab8500_fg_started
;
222 struct completion ab8500_fg_complete
;
223 struct ab8500_fg_flags flags
;
224 struct ab8500_fg_battery_capacity bat_cap
;
225 struct ab8500_fg_avg_cap avg_cap
;
226 struct ab8500
*parent
;
227 struct iio_channel
*main_bat_v
;
228 struct abx500_bm_data
*bm
;
229 struct power_supply
*fg_psy
;
230 struct workqueue_struct
*fg_wq
;
231 struct delayed_work fg_periodic_work
;
232 struct delayed_work fg_low_bat_work
;
233 struct delayed_work fg_reinit_work
;
234 struct work_struct fg_work
;
235 struct work_struct fg_acc_cur_work
;
236 struct delayed_work fg_check_hw_failure_work
;
237 struct mutex cc_lock
;
238 struct kobject fg_kobject
;
240 static LIST_HEAD(ab8500_fg_list
);
243 * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
244 * (i.e. the first fuel gauge in the instance list)
246 struct ab8500_fg
*ab8500_fg_get(void)
248 return list_first_entry_or_null(&ab8500_fg_list
, struct ab8500_fg
,
252 /* Main battery properties */
253 static enum power_supply_property ab8500_fg_props
[] = {
254 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
255 POWER_SUPPLY_PROP_CURRENT_NOW
,
256 POWER_SUPPLY_PROP_CURRENT_AVG
,
257 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
258 POWER_SUPPLY_PROP_ENERGY_FULL
,
259 POWER_SUPPLY_PROP_ENERGY_NOW
,
260 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
261 POWER_SUPPLY_PROP_CHARGE_FULL
,
262 POWER_SUPPLY_PROP_CHARGE_NOW
,
263 POWER_SUPPLY_PROP_CAPACITY
,
264 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
268 * This array maps the raw hex value to lowbat voltage used by the AB8500
269 * Values taken from the UM0836
271 static int ab8500_fg_lowbat_voltage_map
[] = {
338 static u8
ab8500_volt_to_regval(int voltage
)
342 if (voltage
< ab8500_fg_lowbat_voltage_map
[0])
345 for (i
= 0; i
< ARRAY_SIZE(ab8500_fg_lowbat_voltage_map
); i
++) {
346 if (voltage
< ab8500_fg_lowbat_voltage_map
[i
])
350 /* If not captured above, return index of last element */
351 return (u8
) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map
) - 1;
355 * ab8500_fg_is_low_curr() - Low or high current mode
356 * @di: pointer to the ab8500_fg structure
357 * @curr: the current to base or our decision on
359 * Low current mode if the current consumption is below a certain threshold
361 static int ab8500_fg_is_low_curr(struct ab8500_fg
*di
, int curr
)
364 * We want to know if we're in low current mode
366 if (curr
> -di
->bm
->fg_params
->high_curr_threshold
)
373 * ab8500_fg_add_cap_sample() - Add capacity to average filter
374 * @di: pointer to the ab8500_fg structure
375 * @sample: the capacity in mAh to add to the filter
377 * A capacity is added to the filter and a new mean capacity is calculated and
380 static int ab8500_fg_add_cap_sample(struct ab8500_fg
*di
, int sample
)
382 time64_t now
= ktime_get_boottime_seconds();
383 struct ab8500_fg_avg_cap
*avg
= &di
->avg_cap
;
386 avg
->sum
+= sample
- avg
->samples
[avg
->pos
];
387 avg
->samples
[avg
->pos
] = sample
;
388 avg
->time_stamps
[avg
->pos
] = now
;
391 if (avg
->pos
== NBR_AVG_SAMPLES
)
394 if (avg
->nbr_samples
< NBR_AVG_SAMPLES
)
398 * Check the time stamp for each sample. If too old,
399 * replace with latest sample
401 } while (now
- VALID_CAPACITY_SEC
> avg
->time_stamps
[avg
->pos
]);
403 avg
->avg
= avg
->sum
/ avg
->nbr_samples
;
409 * ab8500_fg_clear_cap_samples() - Clear average filter
410 * @di: pointer to the ab8500_fg structure
412 * The capacity filter is is reset to zero.
414 static void ab8500_fg_clear_cap_samples(struct ab8500_fg
*di
)
417 struct ab8500_fg_avg_cap
*avg
= &di
->avg_cap
;
420 avg
->nbr_samples
= 0;
424 for (i
= 0; i
< NBR_AVG_SAMPLES
; i
++) {
426 avg
->time_stamps
[i
] = 0;
431 * ab8500_fg_fill_cap_sample() - Fill average filter
432 * @di: pointer to the ab8500_fg structure
433 * @sample: the capacity in mAh to fill the filter with
435 * The capacity filter is filled with a capacity in mAh
437 static void ab8500_fg_fill_cap_sample(struct ab8500_fg
*di
, int sample
)
441 struct ab8500_fg_avg_cap
*avg
= &di
->avg_cap
;
443 now
= ktime_get_boottime_seconds();
445 for (i
= 0; i
< NBR_AVG_SAMPLES
; i
++) {
446 avg
->samples
[i
] = sample
;
447 avg
->time_stamps
[i
] = now
;
451 avg
->nbr_samples
= NBR_AVG_SAMPLES
;
452 avg
->sum
= sample
* NBR_AVG_SAMPLES
;
457 * ab8500_fg_coulomb_counter() - enable coulomb counter
458 * @di: pointer to the ab8500_fg structure
459 * @enable: enable/disable
461 * Enable/Disable coulomb counter.
462 * On failure returns negative value.
464 static int ab8500_fg_coulomb_counter(struct ab8500_fg
*di
, bool enable
)
467 mutex_lock(&di
->cc_lock
);
469 /* To be able to reprogram the number of samples, we have to
470 * first stop the CC and then enable it again */
471 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
472 AB8500_RTC_CC_CONF_REG
, 0x00);
476 /* Program the samples */
477 ret
= abx500_set_register_interruptible(di
->dev
,
478 AB8500_GAS_GAUGE
, AB8500_GASG_CC_NCOV_ACCU
,
484 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
485 AB8500_RTC_CC_CONF_REG
,
486 (CC_DEEP_SLEEP_ENA
| CC_PWR_UP_ENA
));
490 di
->flags
.fg_enabled
= true;
492 /* Clear any pending read requests */
493 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
494 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
,
495 (RESET_ACCU
| READ_REQ
), 0);
499 ret
= abx500_set_register_interruptible(di
->dev
,
500 AB8500_GAS_GAUGE
, AB8500_GASG_CC_NCOV_ACCU_CTRL
, 0);
505 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
506 AB8500_RTC_CC_CONF_REG
, 0);
510 di
->flags
.fg_enabled
= false;
513 dev_dbg(di
->dev
, " CC enabled: %d Samples: %d\n",
514 enable
, di
->fg_samples
);
516 mutex_unlock(&di
->cc_lock
);
520 dev_err(di
->dev
, "%s Enabling coulomb counter failed\n", __func__
);
521 mutex_unlock(&di
->cc_lock
);
526 * ab8500_fg_inst_curr_start() - start battery instantaneous current
527 * @di: pointer to the ab8500_fg structure
529 * Returns 0 or error code
530 * Note: This is part "one" and has to be called before
531 * ab8500_fg_inst_curr_finalize()
533 int ab8500_fg_inst_curr_start(struct ab8500_fg
*di
)
538 mutex_lock(&di
->cc_lock
);
540 di
->nbr_cceoc_irq_cnt
= 0;
541 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
542 AB8500_RTC_CC_CONF_REG
, ®_val
);
546 if (!(reg_val
& CC_PWR_UP_ENA
)) {
547 dev_dbg(di
->dev
, "%s Enable FG\n", __func__
);
548 di
->turn_off_fg
= true;
550 /* Program the samples */
551 ret
= abx500_set_register_interruptible(di
->dev
,
552 AB8500_GAS_GAUGE
, AB8500_GASG_CC_NCOV_ACCU
,
558 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
559 AB8500_RTC_CC_CONF_REG
,
560 (CC_DEEP_SLEEP_ENA
| CC_PWR_UP_ENA
));
564 di
->turn_off_fg
= false;
568 reinit_completion(&di
->ab8500_fg_started
);
569 reinit_completion(&di
->ab8500_fg_complete
);
572 /* Note: cc_lock is still locked */
575 mutex_unlock(&di
->cc_lock
);
580 * ab8500_fg_inst_curr_started() - check if fg conversion has started
581 * @di: pointer to the ab8500_fg structure
583 * Returns 1 if conversion started, 0 if still waiting
585 int ab8500_fg_inst_curr_started(struct ab8500_fg
*di
)
587 return completion_done(&di
->ab8500_fg_started
);
591 * ab8500_fg_inst_curr_done() - check if fg conversion is done
592 * @di: pointer to the ab8500_fg structure
594 * Returns 1 if conversion done, 0 if still waiting
596 int ab8500_fg_inst_curr_done(struct ab8500_fg
*di
)
598 return completion_done(&di
->ab8500_fg_complete
);
602 * ab8500_fg_inst_curr_finalize() - battery instantaneous current
603 * @di: pointer to the ab8500_fg structure
604 * @res: battery instantenous current(on success)
606 * Returns 0 or an error code
607 * Note: This is part "two" and has to be called at earliest 250 ms
608 * after ab8500_fg_inst_curr_start()
610 int ab8500_fg_inst_curr_finalize(struct ab8500_fg
*di
, int *res
)
615 unsigned long timeout
;
617 if (!completion_done(&di
->ab8500_fg_complete
)) {
618 timeout
= wait_for_completion_timeout(
619 &di
->ab8500_fg_complete
,
621 dev_dbg(di
->dev
, "Finalize time: %d ms\n",
622 jiffies_to_msecs(INS_CURR_TIMEOUT
- timeout
));
625 disable_irq(di
->irq
);
626 di
->nbr_cceoc_irq_cnt
= 0;
627 dev_err(di
->dev
, "completion timed out [%d]\n",
633 disable_irq(di
->irq
);
634 di
->nbr_cceoc_irq_cnt
= 0;
636 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
637 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
,
640 /* 100uS between read request and read is needed */
641 usleep_range(100, 100);
643 /* Read CC Sample conversion value Low and high */
644 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
645 AB8500_GASG_CC_SMPL_CNVL_REG
, &low
);
649 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
650 AB8500_GASG_CC_SMPL_CNVH_REG
, &high
);
655 * negative value for Discharging
656 * convert 2's compliment into decimal
659 val
= (low
| (high
<< 8) | 0xFFFFE000);
661 val
= (low
| (high
<< 8));
664 * Convert to unit value in mA
665 * Full scale input voltage is
666 * 63.160mV => LSB = 63.160mV/(4096*res) = 1.542mA
667 * Given a 250ms conversion cycle time the LSB corresponds
668 * to 107.1 nAh. Convert to current by dividing by the conversion
669 * time in hours (250ms = 1 / (3600 * 4)h)
670 * 107.1nAh assumes 10mOhm, but fg_res is in 0.1mOhm
672 val
= (val
* QLSB_NANO_AMP_HOURS_X10
* 36 * 4) /
673 (1000 * di
->bm
->fg_res
);
675 if (di
->turn_off_fg
) {
676 dev_dbg(di
->dev
, "%s Disable FG\n", __func__
);
678 /* Clear any pending read requests */
679 ret
= abx500_set_register_interruptible(di
->dev
,
680 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
, 0);
685 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
686 AB8500_RTC_CC_CONF_REG
, 0);
690 mutex_unlock(&di
->cc_lock
);
695 mutex_unlock(&di
->cc_lock
);
700 * ab8500_fg_inst_curr_blocking() - battery instantaneous current
701 * @di: pointer to the ab8500_fg structure
702 * @res: battery instantenous current(on success)
704 * Returns 0 else error code
706 int ab8500_fg_inst_curr_blocking(struct ab8500_fg
*di
)
709 unsigned long timeout
;
712 ret
= ab8500_fg_inst_curr_start(di
);
714 dev_err(di
->dev
, "Failed to initialize fg_inst\n");
718 /* Wait for CC to actually start */
719 if (!completion_done(&di
->ab8500_fg_started
)) {
720 timeout
= wait_for_completion_timeout(
721 &di
->ab8500_fg_started
,
723 dev_dbg(di
->dev
, "Start time: %d ms\n",
724 jiffies_to_msecs(INS_CURR_TIMEOUT
- timeout
));
727 dev_err(di
->dev
, "completion timed out [%d]\n",
733 ret
= ab8500_fg_inst_curr_finalize(di
, &res
);
735 dev_err(di
->dev
, "Failed to finalize fg_inst\n");
739 dev_dbg(di
->dev
, "%s instant current: %d", __func__
, res
);
742 disable_irq(di
->irq
);
743 mutex_unlock(&di
->cc_lock
);
748 * ab8500_fg_acc_cur_work() - average battery current
749 * @work: pointer to the work_struct structure
751 * Updated the average battery current obtained from the
754 static void ab8500_fg_acc_cur_work(struct work_struct
*work
)
760 struct ab8500_fg
*di
= container_of(work
,
761 struct ab8500_fg
, fg_acc_cur_work
);
763 mutex_lock(&di
->cc_lock
);
764 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
765 AB8500_GASG_CC_NCOV_ACCU_CTRL
, RD_NCONV_ACCU_REQ
);
769 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
770 AB8500_GASG_CC_NCOV_ACCU_LOW
, &low
);
774 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
775 AB8500_GASG_CC_NCOV_ACCU_MED
, &med
);
779 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_GAS_GAUGE
,
780 AB8500_GASG_CC_NCOV_ACCU_HIGH
, &high
);
784 /* Check for sign bit in case of negative value, 2's compliment */
786 val
= (low
| (med
<< 8) | (high
<< 16) | 0xFFE00000);
788 val
= (low
| (med
<< 8) | (high
<< 16));
792 * Given a 250ms conversion cycle time the LSB corresponds
794 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
796 di
->accu_charge
= (val
* QLSB_NANO_AMP_HOURS_X10
) /
797 (100 * di
->bm
->fg_res
);
800 * Convert to unit value in mA
801 * by dividing by the conversion
802 * time in hours (= samples / (3600 * 4)h)
803 * and multiply with 1000
805 di
->avg_curr
= (val
* QLSB_NANO_AMP_HOURS_X10
* 36) /
806 (1000 * di
->bm
->fg_res
* (di
->fg_samples
/ 4));
808 di
->flags
.conv_done
= true;
810 mutex_unlock(&di
->cc_lock
);
812 queue_work(di
->fg_wq
, &di
->fg_work
);
814 dev_dbg(di
->dev
, "fg_res: %d, fg_samples: %d, gasg: %d, accu_charge: %d \n",
815 di
->bm
->fg_res
, di
->fg_samples
, val
, di
->accu_charge
);
819 "Failed to read or write gas gauge registers\n");
820 mutex_unlock(&di
->cc_lock
);
821 queue_work(di
->fg_wq
, &di
->fg_work
);
825 * ab8500_fg_bat_voltage() - get battery voltage
826 * @di: pointer to the ab8500_fg structure
828 * Returns battery voltage(on success) else error code
830 static int ab8500_fg_bat_voltage(struct ab8500_fg
*di
)
835 ret
= iio_read_channel_processed(di
->main_bat_v
, &vbat
);
838 "%s ADC conversion failed, using previous value\n",
848 * ab8500_fg_volt_to_capacity() - Voltage based capacity
849 * @di: pointer to the ab8500_fg structure
850 * @voltage: The voltage to convert to a capacity
852 * Returns battery capacity in per mille based on voltage
854 static int ab8500_fg_volt_to_capacity(struct ab8500_fg
*di
, int voltage
)
857 const struct abx500_v_to_cap
*tbl
;
860 tbl
= di
->bm
->bat_type
[di
->bm
->batt_id
].v_to_cap_tbl
,
861 tbl_size
= di
->bm
->bat_type
[di
->bm
->batt_id
].n_v_cap_tbl_elements
;
863 for (i
= 0; i
< tbl_size
; ++i
) {
864 if (voltage
> tbl
[i
].voltage
)
868 if ((i
> 0) && (i
< tbl_size
)) {
869 cap
= interpolate(voltage
,
871 tbl
[i
].capacity
* 10,
873 tbl
[i
-1].capacity
* 10);
880 dev_dbg(di
->dev
, "%s Vbat: %d, Cap: %d per mille",
881 __func__
, voltage
, cap
);
887 * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
888 * @di: pointer to the ab8500_fg structure
890 * Returns battery capacity based on battery voltage that is not compensated
891 * for the voltage drop due to the load
893 static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg
*di
)
895 di
->vbat
= ab8500_fg_bat_voltage(di
);
896 return ab8500_fg_volt_to_capacity(di
, di
->vbat
);
900 * ab8500_fg_battery_resistance() - Returns the battery inner resistance
901 * @di: pointer to the ab8500_fg structure
903 * Returns battery inner resistance added with the fuel gauge resistor value
904 * to get the total resistance in the whole link from gnd to bat+ node.
906 static int ab8500_fg_battery_resistance(struct ab8500_fg
*di
)
909 const struct batres_vs_temp
*tbl
;
912 tbl
= di
->bm
->bat_type
[di
->bm
->batt_id
].batres_tbl
;
913 tbl_size
= di
->bm
->bat_type
[di
->bm
->batt_id
].n_batres_tbl_elements
;
915 for (i
= 0; i
< tbl_size
; ++i
) {
916 if (di
->bat_temp
/ 10 > tbl
[i
].temp
)
920 if ((i
> 0) && (i
< tbl_size
)) {
921 resist
= interpolate(di
->bat_temp
/ 10,
927 resist
= tbl
[0].resist
;
929 resist
= tbl
[tbl_size
- 1].resist
;
932 dev_dbg(di
->dev
, "%s Temp: %d battery internal resistance: %d"
933 " fg resistance %d, total: %d (mOhm)\n",
934 __func__
, di
->bat_temp
, resist
, di
->bm
->fg_res
/ 10,
935 (di
->bm
->fg_res
/ 10) + resist
);
937 /* fg_res variable is in 0.1mOhm */
938 resist
+= di
->bm
->fg_res
/ 10;
944 * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
945 * @di: pointer to the ab8500_fg structure
947 * Returns battery capacity based on battery voltage that is load compensated
948 * for the voltage drop
950 static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg
*di
)
956 ab8500_fg_inst_curr_start(di
);
959 vbat
+= ab8500_fg_bat_voltage(di
);
961 usleep_range(5000, 6000);
962 } while (!ab8500_fg_inst_curr_done(di
));
964 ab8500_fg_inst_curr_finalize(di
, &di
->inst_curr
);
967 res
= ab8500_fg_battery_resistance(di
);
969 /* Use Ohms law to get the load compensated voltage */
970 vbat_comp
= di
->vbat
- (di
->inst_curr
* res
) / 1000;
972 dev_dbg(di
->dev
, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, "
973 "R: %dmOhm, Current: %dmA Vbat Samples: %d\n",
974 __func__
, di
->vbat
, vbat_comp
, res
, di
->inst_curr
, i
);
976 return ab8500_fg_volt_to_capacity(di
, vbat_comp
);
980 * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
981 * @di: pointer to the ab8500_fg structure
982 * @cap_mah: capacity in mAh
984 * Converts capacity in mAh to capacity in permille
986 static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg
*di
, int cap_mah
)
988 return (cap_mah
* 1000) / di
->bat_cap
.max_mah_design
;
992 * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
993 * @di: pointer to the ab8500_fg structure
994 * @cap_pm: capacity in permille
996 * Converts capacity in permille to capacity in mAh
998 static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg
*di
, int cap_pm
)
1000 return cap_pm
* di
->bat_cap
.max_mah_design
/ 1000;
1004 * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
1005 * @di: pointer to the ab8500_fg structure
1006 * @cap_mah: capacity in mAh
1008 * Converts capacity in mAh to capacity in uWh
1010 static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg
*di
, int cap_mah
)
1015 div_res
= ((u64
) cap_mah
) * ((u64
) di
->vbat_nom
);
1016 div_rem
= do_div(div_res
, 1000);
1018 /* Make sure to round upwards if necessary */
1019 if (div_rem
>= 1000 / 2)
1022 return (int) div_res
;
1026 * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
1027 * @di: pointer to the ab8500_fg structure
1029 * Return the capacity in mAh based on previous calculated capcity and the FG
1030 * accumulator register value. The filter is filled with this capacity
1032 static int ab8500_fg_calc_cap_charging(struct ab8500_fg
*di
)
1034 dev_dbg(di
->dev
, "%s cap_mah %d accu_charge %d\n",
1039 /* Capacity should not be less than 0 */
1040 if (di
->bat_cap
.mah
+ di
->accu_charge
> 0)
1041 di
->bat_cap
.mah
+= di
->accu_charge
;
1043 di
->bat_cap
.mah
= 0;
1045 * We force capacity to 100% once when the algorithm
1046 * reports that it's full.
1048 if (di
->bat_cap
.mah
>= di
->bat_cap
.max_mah_design
||
1049 di
->flags
.force_full
) {
1050 di
->bat_cap
.mah
= di
->bat_cap
.max_mah_design
;
1053 ab8500_fg_fill_cap_sample(di
, di
->bat_cap
.mah
);
1054 di
->bat_cap
.permille
=
1055 ab8500_fg_convert_mah_to_permille(di
, di
->bat_cap
.mah
);
1057 /* We need to update battery voltage and inst current when charging */
1058 di
->vbat
= ab8500_fg_bat_voltage(di
);
1059 di
->inst_curr
= ab8500_fg_inst_curr_blocking(di
);
1061 return di
->bat_cap
.mah
;
1065 * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage
1066 * @di: pointer to the ab8500_fg structure
1067 * @comp: if voltage should be load compensated before capacity calc
1069 * Return the capacity in mAh based on the battery voltage. The voltage can
1070 * either be load compensated or not. This value is added to the filter and a
1071 * new mean value is calculated and returned.
1073 static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg
*di
, bool comp
)
1078 permille
= ab8500_fg_load_comp_volt_to_capacity(di
);
1080 permille
= ab8500_fg_uncomp_volt_to_capacity(di
);
1082 mah
= ab8500_fg_convert_permille_to_mah(di
, permille
);
1084 di
->bat_cap
.mah
= ab8500_fg_add_cap_sample(di
, mah
);
1085 di
->bat_cap
.permille
=
1086 ab8500_fg_convert_mah_to_permille(di
, di
->bat_cap
.mah
);
1088 return di
->bat_cap
.mah
;
1092 * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG
1093 * @di: pointer to the ab8500_fg structure
1095 * Return the capacity in mAh based on previous calculated capcity and the FG
1096 * accumulator register value. This value is added to the filter and a
1097 * new mean value is calculated and returned.
1099 static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg
*di
)
1101 int permille_volt
, permille
;
1103 dev_dbg(di
->dev
, "%s cap_mah %d accu_charge %d\n",
1108 /* Capacity should not be less than 0 */
1109 if (di
->bat_cap
.mah
+ di
->accu_charge
> 0)
1110 di
->bat_cap
.mah
+= di
->accu_charge
;
1112 di
->bat_cap
.mah
= 0;
1114 if (di
->bat_cap
.mah
>= di
->bat_cap
.max_mah_design
)
1115 di
->bat_cap
.mah
= di
->bat_cap
.max_mah_design
;
1118 * Check against voltage based capacity. It can not be lower
1119 * than what the uncompensated voltage says
1121 permille
= ab8500_fg_convert_mah_to_permille(di
, di
->bat_cap
.mah
);
1122 permille_volt
= ab8500_fg_uncomp_volt_to_capacity(di
);
1124 if (permille
< permille_volt
) {
1125 di
->bat_cap
.permille
= permille_volt
;
1126 di
->bat_cap
.mah
= ab8500_fg_convert_permille_to_mah(di
,
1127 di
->bat_cap
.permille
);
1129 dev_dbg(di
->dev
, "%s voltage based: perm %d perm_volt %d\n",
1134 ab8500_fg_fill_cap_sample(di
, di
->bat_cap
.mah
);
1136 ab8500_fg_fill_cap_sample(di
, di
->bat_cap
.mah
);
1137 di
->bat_cap
.permille
=
1138 ab8500_fg_convert_mah_to_permille(di
, di
->bat_cap
.mah
);
1141 return di
->bat_cap
.mah
;
1145 * ab8500_fg_capacity_level() - Get the battery capacity level
1146 * @di: pointer to the ab8500_fg structure
1148 * Get the battery capacity level based on the capacity in percent
1150 static int ab8500_fg_capacity_level(struct ab8500_fg
*di
)
1154 percent
= DIV_ROUND_CLOSEST(di
->bat_cap
.permille
, 10);
1156 if (percent
<= di
->bm
->cap_levels
->critical
||
1158 ret
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
1159 else if (percent
<= di
->bm
->cap_levels
->low
)
1160 ret
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
1161 else if (percent
<= di
->bm
->cap_levels
->normal
)
1162 ret
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
1163 else if (percent
<= di
->bm
->cap_levels
->high
)
1164 ret
= POWER_SUPPLY_CAPACITY_LEVEL_HIGH
;
1166 ret
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
1172 * ab8500_fg_calculate_scaled_capacity() - Capacity scaling
1173 * @di: pointer to the ab8500_fg structure
1175 * Calculates the capacity to be shown to upper layers. Scales the capacity
1176 * to have 100% as a reference from the actual capacity upon removal of charger
1177 * when charging is in maintenance mode.
1179 static int ab8500_fg_calculate_scaled_capacity(struct ab8500_fg
*di
)
1181 struct ab8500_fg_cap_scaling
*cs
= &di
->bat_cap
.cap_scale
;
1182 int capacity
= di
->bat_cap
.prev_percent
;
1188 * As long as we are in fully charge mode scale the capacity
1191 if (di
->flags
.fully_charged
) {
1192 cs
->cap_to_scale
[0] = 100;
1193 cs
->cap_to_scale
[1] =
1194 max(capacity
, di
->bm
->fg_params
->maint_thres
);
1195 dev_dbg(di
->dev
, "Scale cap with %d/%d\n",
1196 cs
->cap_to_scale
[0], cs
->cap_to_scale
[1]);
1199 /* Calculates the scaled capacity. */
1200 if ((cs
->cap_to_scale
[0] != cs
->cap_to_scale
[1])
1201 && (cs
->cap_to_scale
[1] > 0))
1203 DIV_ROUND_CLOSEST(di
->bat_cap
.prev_percent
*
1204 cs
->cap_to_scale
[0],
1205 cs
->cap_to_scale
[1]));
1207 if (di
->flags
.charging
) {
1208 if (capacity
< cs
->disable_cap_level
) {
1209 cs
->disable_cap_level
= capacity
;
1210 dev_dbg(di
->dev
, "Cap to stop scale lowered %d%%\n",
1211 cs
->disable_cap_level
);
1212 } else if (!di
->flags
.fully_charged
) {
1213 if (di
->bat_cap
.prev_percent
>=
1214 cs
->disable_cap_level
) {
1215 dev_dbg(di
->dev
, "Disabling scaled capacity\n");
1217 capacity
= di
->bat_cap
.prev_percent
;
1220 "Waiting in cap to level %d%%\n",
1221 cs
->disable_cap_level
);
1222 capacity
= cs
->disable_cap_level
;
1231 * ab8500_fg_update_cap_scalers() - Capacity scaling
1232 * @di: pointer to the ab8500_fg structure
1234 * To be called when state change from charge<->discharge to update
1235 * the capacity scalers.
1237 static void ab8500_fg_update_cap_scalers(struct ab8500_fg
*di
)
1239 struct ab8500_fg_cap_scaling
*cs
= &di
->bat_cap
.cap_scale
;
1243 if (di
->flags
.charging
) {
1244 di
->bat_cap
.cap_scale
.disable_cap_level
=
1245 di
->bat_cap
.cap_scale
.scaled_cap
;
1246 dev_dbg(di
->dev
, "Cap to stop scale at charge %d%%\n",
1247 di
->bat_cap
.cap_scale
.disable_cap_level
);
1249 if (cs
->scaled_cap
!= 100) {
1250 cs
->cap_to_scale
[0] = cs
->scaled_cap
;
1251 cs
->cap_to_scale
[1] = di
->bat_cap
.prev_percent
;
1253 cs
->cap_to_scale
[0] = 100;
1254 cs
->cap_to_scale
[1] =
1255 max(di
->bat_cap
.prev_percent
,
1256 di
->bm
->fg_params
->maint_thres
);
1259 dev_dbg(di
->dev
, "Cap to scale at discharge %d/%d\n",
1260 cs
->cap_to_scale
[0], cs
->cap_to_scale
[1]);
1265 * ab8500_fg_check_capacity_limits() - Check if capacity has changed
1266 * @di: pointer to the ab8500_fg structure
1267 * @init: capacity is allowed to go up in init mode
1269 * Check if capacity or capacity limit has changed and notify the system
1270 * about it using the power_supply framework
1272 static void ab8500_fg_check_capacity_limits(struct ab8500_fg
*di
, bool init
)
1274 bool changed
= false;
1275 int percent
= DIV_ROUND_CLOSEST(di
->bat_cap
.permille
, 10);
1277 di
->bat_cap
.level
= ab8500_fg_capacity_level(di
);
1279 if (di
->bat_cap
.level
!= di
->bat_cap
.prev_level
) {
1281 * We do not allow reported capacity level to go up
1282 * unless we're charging or if we're in init
1284 if (!(!di
->flags
.charging
&& di
->bat_cap
.level
>
1285 di
->bat_cap
.prev_level
) || init
) {
1286 dev_dbg(di
->dev
, "level changed from %d to %d\n",
1287 di
->bat_cap
.prev_level
,
1289 di
->bat_cap
.prev_level
= di
->bat_cap
.level
;
1292 dev_dbg(di
->dev
, "level not allowed to go up "
1293 "since no charger is connected: %d to %d\n",
1294 di
->bat_cap
.prev_level
,
1300 * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate
1303 if (di
->flags
.low_bat
) {
1304 dev_dbg(di
->dev
, "Battery low, set capacity to 0\n");
1305 di
->bat_cap
.prev_percent
= 0;
1306 di
->bat_cap
.permille
= 0;
1308 di
->bat_cap
.prev_mah
= 0;
1309 di
->bat_cap
.mah
= 0;
1311 } else if (di
->flags
.fully_charged
) {
1313 * We report 100% if algorithm reported fully charged
1314 * and show 100% during maintenance charging (scaling).
1316 if (di
->flags
.force_full
) {
1317 di
->bat_cap
.prev_percent
= percent
;
1318 di
->bat_cap
.prev_mah
= di
->bat_cap
.mah
;
1322 if (!di
->bat_cap
.cap_scale
.enable
&&
1323 di
->bm
->capacity_scaling
) {
1324 di
->bat_cap
.cap_scale
.enable
= true;
1325 di
->bat_cap
.cap_scale
.cap_to_scale
[0] = 100;
1326 di
->bat_cap
.cap_scale
.cap_to_scale
[1] =
1327 di
->bat_cap
.prev_percent
;
1328 di
->bat_cap
.cap_scale
.disable_cap_level
= 100;
1330 } else if (di
->bat_cap
.prev_percent
!= percent
) {
1332 "battery reported full "
1333 "but capacity dropping: %d\n",
1335 di
->bat_cap
.prev_percent
= percent
;
1336 di
->bat_cap
.prev_mah
= di
->bat_cap
.mah
;
1340 } else if (di
->bat_cap
.prev_percent
!= percent
) {
1343 * We will not report 0% unless we've got
1344 * the LOW_BAT IRQ, no matter what the FG
1347 di
->bat_cap
.prev_percent
= 1;
1351 } else if (!(!di
->flags
.charging
&&
1352 percent
> di
->bat_cap
.prev_percent
) || init
) {
1354 * We do not allow reported capacity to go up
1355 * unless we're charging or if we're in init
1358 "capacity changed from %d to %d (%d)\n",
1359 di
->bat_cap
.prev_percent
,
1361 di
->bat_cap
.permille
);
1362 di
->bat_cap
.prev_percent
= percent
;
1363 di
->bat_cap
.prev_mah
= di
->bat_cap
.mah
;
1367 dev_dbg(di
->dev
, "capacity not allowed to go up since "
1368 "no charger is connected: %d to %d (%d)\n",
1369 di
->bat_cap
.prev_percent
,
1371 di
->bat_cap
.permille
);
1376 if (di
->bm
->capacity_scaling
) {
1377 di
->bat_cap
.cap_scale
.scaled_cap
=
1378 ab8500_fg_calculate_scaled_capacity(di
);
1380 dev_info(di
->dev
, "capacity=%d (%d)\n",
1381 di
->bat_cap
.prev_percent
,
1382 di
->bat_cap
.cap_scale
.scaled_cap
);
1384 power_supply_changed(di
->fg_psy
);
1385 if (di
->flags
.fully_charged
&& di
->flags
.force_full
) {
1386 dev_dbg(di
->dev
, "Battery full, notifying.\n");
1387 di
->flags
.force_full
= false;
1388 sysfs_notify(&di
->fg_kobject
, NULL
, "charge_full");
1390 sysfs_notify(&di
->fg_kobject
, NULL
, "charge_now");
1394 static void ab8500_fg_charge_state_to(struct ab8500_fg
*di
,
1395 enum ab8500_fg_charge_state new_state
)
1397 dev_dbg(di
->dev
, "Charge state from %d [%s] to %d [%s]\n",
1399 charge_state
[di
->charge_state
],
1401 charge_state
[new_state
]);
1403 di
->charge_state
= new_state
;
1406 static void ab8500_fg_discharge_state_to(struct ab8500_fg
*di
,
1407 enum ab8500_fg_discharge_state new_state
)
1409 dev_dbg(di
->dev
, "Discharge state from %d [%s] to %d [%s]\n",
1410 di
->discharge_state
,
1411 discharge_state
[di
->discharge_state
],
1413 discharge_state
[new_state
]);
1415 di
->discharge_state
= new_state
;
1419 * ab8500_fg_algorithm_charging() - FG algorithm for when charging
1420 * @di: pointer to the ab8500_fg structure
1422 * Battery capacity calculation state machine for when we're charging
1424 static void ab8500_fg_algorithm_charging(struct ab8500_fg
*di
)
1427 * If we change to discharge mode
1428 * we should start with recovery
1430 if (di
->discharge_state
!= AB8500_FG_DISCHARGE_INIT_RECOVERY
)
1431 ab8500_fg_discharge_state_to(di
,
1432 AB8500_FG_DISCHARGE_INIT_RECOVERY
);
1434 switch (di
->charge_state
) {
1435 case AB8500_FG_CHARGE_INIT
:
1436 di
->fg_samples
= SEC_TO_SAMPLE(
1437 di
->bm
->fg_params
->accu_charging
);
1439 ab8500_fg_coulomb_counter(di
, true);
1440 ab8500_fg_charge_state_to(di
, AB8500_FG_CHARGE_READOUT
);
1444 case AB8500_FG_CHARGE_READOUT
:
1446 * Read the FG and calculate the new capacity
1448 mutex_lock(&di
->cc_lock
);
1449 if (!di
->flags
.conv_done
&& !di
->flags
.force_full
) {
1450 /* Wasn't the CC IRQ that got us here */
1451 mutex_unlock(&di
->cc_lock
);
1452 dev_dbg(di
->dev
, "%s CC conv not done\n",
1457 di
->flags
.conv_done
= false;
1458 mutex_unlock(&di
->cc_lock
);
1460 ab8500_fg_calc_cap_charging(di
);
1468 /* Check capacity limits */
1469 ab8500_fg_check_capacity_limits(di
, false);
1472 static void force_capacity(struct ab8500_fg
*di
)
1476 ab8500_fg_clear_cap_samples(di
);
1477 cap
= di
->bat_cap
.user_mah
;
1478 if (cap
> di
->bat_cap
.max_mah_design
) {
1479 dev_dbg(di
->dev
, "Remaining cap %d can't be bigger than total"
1480 " %d\n", cap
, di
->bat_cap
.max_mah_design
);
1481 cap
= di
->bat_cap
.max_mah_design
;
1483 ab8500_fg_fill_cap_sample(di
, di
->bat_cap
.user_mah
);
1484 di
->bat_cap
.permille
= ab8500_fg_convert_mah_to_permille(di
, cap
);
1485 di
->bat_cap
.mah
= cap
;
1486 ab8500_fg_check_capacity_limits(di
, true);
1489 static bool check_sysfs_capacity(struct ab8500_fg
*di
)
1491 int cap
, lower
, upper
;
1494 cap
= di
->bat_cap
.user_mah
;
1496 cap_permille
= ab8500_fg_convert_mah_to_permille(di
,
1497 di
->bat_cap
.user_mah
);
1499 lower
= di
->bat_cap
.permille
- di
->bm
->fg_params
->user_cap_limit
* 10;
1500 upper
= di
->bat_cap
.permille
+ di
->bm
->fg_params
->user_cap_limit
* 10;
1504 /* 1000 is permille, -> 100 percent */
1508 dev_dbg(di
->dev
, "Capacity limits:"
1509 " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
1510 lower
, cap_permille
, upper
, cap
, di
->bat_cap
.mah
);
1512 /* If within limits, use the saved capacity and exit estimation...*/
1513 if (cap_permille
> lower
&& cap_permille
< upper
) {
1514 dev_dbg(di
->dev
, "OK! Using users cap %d uAh now\n", cap
);
1518 dev_dbg(di
->dev
, "Capacity from user out of limits, ignoring");
1523 * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
1524 * @di: pointer to the ab8500_fg structure
1526 * Battery capacity calculation state machine for when we're discharging
1528 static void ab8500_fg_algorithm_discharging(struct ab8500_fg
*di
)
1532 /* If we change to charge mode we should start with init */
1533 if (di
->charge_state
!= AB8500_FG_CHARGE_INIT
)
1534 ab8500_fg_charge_state_to(di
, AB8500_FG_CHARGE_INIT
);
1536 switch (di
->discharge_state
) {
1537 case AB8500_FG_DISCHARGE_INIT
:
1538 /* We use the FG IRQ to work on */
1540 di
->fg_samples
= SEC_TO_SAMPLE(di
->bm
->fg_params
->init_timer
);
1541 ab8500_fg_coulomb_counter(di
, true);
1542 ab8500_fg_discharge_state_to(di
,
1543 AB8500_FG_DISCHARGE_INITMEASURING
);
1545 /* Intentional fallthrough */
1546 case AB8500_FG_DISCHARGE_INITMEASURING
:
1548 * Discard a number of samples during startup.
1549 * After that, use compensated voltage for a few
1550 * samples to get an initial capacity.
1551 * Then go to READOUT
1553 sleep_time
= di
->bm
->fg_params
->init_timer
;
1555 /* Discard the first [x] seconds */
1556 if (di
->init_cnt
> di
->bm
->fg_params
->init_discard_time
) {
1557 ab8500_fg_calc_cap_discharge_voltage(di
, true);
1559 ab8500_fg_check_capacity_limits(di
, true);
1562 di
->init_cnt
+= sleep_time
;
1563 if (di
->init_cnt
> di
->bm
->fg_params
->init_total_time
)
1564 ab8500_fg_discharge_state_to(di
,
1565 AB8500_FG_DISCHARGE_READOUT_INIT
);
1569 case AB8500_FG_DISCHARGE_INIT_RECOVERY
:
1570 di
->recovery_cnt
= 0;
1571 di
->recovery_needed
= true;
1572 ab8500_fg_discharge_state_to(di
,
1573 AB8500_FG_DISCHARGE_RECOVERY
);
1575 /* Intentional fallthrough */
1577 case AB8500_FG_DISCHARGE_RECOVERY
:
1578 sleep_time
= di
->bm
->fg_params
->recovery_sleep_timer
;
1581 * We should check the power consumption
1582 * If low, go to READOUT (after x min) or
1583 * RECOVERY_SLEEP if time left.
1584 * If high, go to READOUT
1586 di
->inst_curr
= ab8500_fg_inst_curr_blocking(di
);
1588 if (ab8500_fg_is_low_curr(di
, di
->inst_curr
)) {
1589 if (di
->recovery_cnt
>
1590 di
->bm
->fg_params
->recovery_total_time
) {
1591 di
->fg_samples
= SEC_TO_SAMPLE(
1592 di
->bm
->fg_params
->accu_high_curr
);
1593 ab8500_fg_coulomb_counter(di
, true);
1594 ab8500_fg_discharge_state_to(di
,
1595 AB8500_FG_DISCHARGE_READOUT
);
1596 di
->recovery_needed
= false;
1598 queue_delayed_work(di
->fg_wq
,
1599 &di
->fg_periodic_work
,
1602 di
->recovery_cnt
+= sleep_time
;
1604 di
->fg_samples
= SEC_TO_SAMPLE(
1605 di
->bm
->fg_params
->accu_high_curr
);
1606 ab8500_fg_coulomb_counter(di
, true);
1607 ab8500_fg_discharge_state_to(di
,
1608 AB8500_FG_DISCHARGE_READOUT
);
1612 case AB8500_FG_DISCHARGE_READOUT_INIT
:
1613 di
->fg_samples
= SEC_TO_SAMPLE(
1614 di
->bm
->fg_params
->accu_high_curr
);
1615 ab8500_fg_coulomb_counter(di
, true);
1616 ab8500_fg_discharge_state_to(di
,
1617 AB8500_FG_DISCHARGE_READOUT
);
1620 case AB8500_FG_DISCHARGE_READOUT
:
1621 di
->inst_curr
= ab8500_fg_inst_curr_blocking(di
);
1623 if (ab8500_fg_is_low_curr(di
, di
->inst_curr
)) {
1624 /* Detect mode change */
1625 if (di
->high_curr_mode
) {
1626 di
->high_curr_mode
= false;
1627 di
->high_curr_cnt
= 0;
1630 if (di
->recovery_needed
) {
1631 ab8500_fg_discharge_state_to(di
,
1632 AB8500_FG_DISCHARGE_INIT_RECOVERY
);
1634 queue_delayed_work(di
->fg_wq
,
1635 &di
->fg_periodic_work
, 0);
1640 ab8500_fg_calc_cap_discharge_voltage(di
, true);
1642 mutex_lock(&di
->cc_lock
);
1643 if (!di
->flags
.conv_done
) {
1644 /* Wasn't the CC IRQ that got us here */
1645 mutex_unlock(&di
->cc_lock
);
1646 dev_dbg(di
->dev
, "%s CC conv not done\n",
1651 di
->flags
.conv_done
= false;
1652 mutex_unlock(&di
->cc_lock
);
1654 /* Detect mode change */
1655 if (!di
->high_curr_mode
) {
1656 di
->high_curr_mode
= true;
1657 di
->high_curr_cnt
= 0;
1660 di
->high_curr_cnt
+=
1661 di
->bm
->fg_params
->accu_high_curr
;
1662 if (di
->high_curr_cnt
>
1663 di
->bm
->fg_params
->high_curr_time
)
1664 di
->recovery_needed
= true;
1666 ab8500_fg_calc_cap_discharge_fg(di
);
1669 ab8500_fg_check_capacity_limits(di
, false);
1673 case AB8500_FG_DISCHARGE_WAKEUP
:
1674 ab8500_fg_calc_cap_discharge_voltage(di
, true);
1676 di
->fg_samples
= SEC_TO_SAMPLE(
1677 di
->bm
->fg_params
->accu_high_curr
);
1678 ab8500_fg_coulomb_counter(di
, true);
1679 ab8500_fg_discharge_state_to(di
,
1680 AB8500_FG_DISCHARGE_READOUT
);
1682 ab8500_fg_check_capacity_limits(di
, false);
1692 * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
1693 * @di: pointer to the ab8500_fg structure
1696 static void ab8500_fg_algorithm_calibrate(struct ab8500_fg
*di
)
1700 switch (di
->calib_state
) {
1701 case AB8500_FG_CALIB_INIT
:
1702 dev_dbg(di
->dev
, "Calibration ongoing...\n");
1704 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
1705 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
,
1706 CC_INT_CAL_N_AVG_MASK
, CC_INT_CAL_SAMPLES_8
);
1710 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
1711 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
,
1712 CC_INTAVGOFFSET_ENA
, CC_INTAVGOFFSET_ENA
);
1715 di
->calib_state
= AB8500_FG_CALIB_WAIT
;
1717 case AB8500_FG_CALIB_END
:
1718 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
1719 AB8500_GAS_GAUGE
, AB8500_GASG_CC_CTRL_REG
,
1720 CC_MUXOFFSET
, CC_MUXOFFSET
);
1723 di
->flags
.calibrate
= false;
1724 dev_dbg(di
->dev
, "Calibration done...\n");
1725 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
1727 case AB8500_FG_CALIB_WAIT
:
1728 dev_dbg(di
->dev
, "Calibration WFI\n");
1734 /* Something went wrong, don't calibrate then */
1735 dev_err(di
->dev
, "failed to calibrate the CC\n");
1736 di
->flags
.calibrate
= false;
1737 di
->calib_state
= AB8500_FG_CALIB_INIT
;
1738 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
1742 * ab8500_fg_algorithm() - Entry point for the FG algorithm
1743 * @di: pointer to the ab8500_fg structure
1745 * Entry point for the battery capacity calculation state machine
1747 static void ab8500_fg_algorithm(struct ab8500_fg
*di
)
1749 if (di
->flags
.calibrate
)
1750 ab8500_fg_algorithm_calibrate(di
);
1752 if (di
->flags
.charging
)
1753 ab8500_fg_algorithm_charging(di
);
1755 ab8500_fg_algorithm_discharging(di
);
1758 dev_dbg(di
->dev
, "[FG_DATA] %d %d %d %d %d %d %d %d %d %d "
1759 "%d %d %d %d %d %d %d\n",
1760 di
->bat_cap
.max_mah_design
,
1761 di
->bat_cap
.max_mah
,
1763 di
->bat_cap
.permille
,
1765 di
->bat_cap
.prev_mah
,
1766 di
->bat_cap
.prev_percent
,
1767 di
->bat_cap
.prev_level
,
1774 di
->discharge_state
,
1776 di
->recovery_needed
);
1780 * ab8500_fg_periodic_work() - Run the FG state machine periodically
1781 * @work: pointer to the work_struct structure
1783 * Work queue function for periodic work
1785 static void ab8500_fg_periodic_work(struct work_struct
*work
)
1787 struct ab8500_fg
*di
= container_of(work
, struct ab8500_fg
,
1788 fg_periodic_work
.work
);
1790 if (di
->init_capacity
) {
1791 /* Get an initial capacity calculation */
1792 ab8500_fg_calc_cap_discharge_voltage(di
, true);
1793 ab8500_fg_check_capacity_limits(di
, true);
1794 di
->init_capacity
= false;
1796 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
1797 } else if (di
->flags
.user_cap
) {
1798 if (check_sysfs_capacity(di
)) {
1799 ab8500_fg_check_capacity_limits(di
, true);
1800 if (di
->flags
.charging
)
1801 ab8500_fg_charge_state_to(di
,
1802 AB8500_FG_CHARGE_INIT
);
1804 ab8500_fg_discharge_state_to(di
,
1805 AB8500_FG_DISCHARGE_READOUT_INIT
);
1807 di
->flags
.user_cap
= false;
1808 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
1810 ab8500_fg_algorithm(di
);
1815 * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
1816 * @work: pointer to the work_struct structure
1818 * Work queue function for checking the OVV_BAT condition
1820 static void ab8500_fg_check_hw_failure_work(struct work_struct
*work
)
1825 struct ab8500_fg
*di
= container_of(work
, struct ab8500_fg
,
1826 fg_check_hw_failure_work
.work
);
1829 * If we have had a battery over-voltage situation,
1830 * check ovv-bit to see if it should be reset.
1832 ret
= abx500_get_register_interruptible(di
->dev
,
1833 AB8500_CHARGER
, AB8500_CH_STAT_REG
,
1836 dev_err(di
->dev
, "%s ab8500 read failed\n", __func__
);
1839 if ((reg_value
& BATT_OVV
) == BATT_OVV
) {
1840 if (!di
->flags
.bat_ovv
) {
1841 dev_dbg(di
->dev
, "Battery OVV\n");
1842 di
->flags
.bat_ovv
= true;
1843 power_supply_changed(di
->fg_psy
);
1845 /* Not yet recovered from ovv, reschedule this test */
1846 queue_delayed_work(di
->fg_wq
, &di
->fg_check_hw_failure_work
,
1849 dev_dbg(di
->dev
, "Battery recovered from OVV\n");
1850 di
->flags
.bat_ovv
= false;
1851 power_supply_changed(di
->fg_psy
);
1856 * ab8500_fg_low_bat_work() - Check LOW_BAT condition
1857 * @work: pointer to the work_struct structure
1859 * Work queue function for checking the LOW_BAT condition
1861 static void ab8500_fg_low_bat_work(struct work_struct
*work
)
1865 struct ab8500_fg
*di
= container_of(work
, struct ab8500_fg
,
1866 fg_low_bat_work
.work
);
1868 vbat
= ab8500_fg_bat_voltage(di
);
1870 /* Check if LOW_BAT still fulfilled */
1871 if (vbat
< di
->bm
->fg_params
->lowbat_threshold
) {
1872 /* Is it time to shut down? */
1873 if (di
->low_bat_cnt
< 1) {
1874 di
->flags
.low_bat
= true;
1875 dev_warn(di
->dev
, "Shut down pending...\n");
1878 * Else we need to re-schedule this check to be able to detect
1879 * if the voltage increases again during charging or
1880 * due to decreasing load.
1883 dev_warn(di
->dev
, "Battery voltage still LOW\n");
1884 queue_delayed_work(di
->fg_wq
, &di
->fg_low_bat_work
,
1885 round_jiffies(LOW_BAT_CHECK_INTERVAL
));
1888 di
->flags
.low_bat_delay
= false;
1889 di
->low_bat_cnt
= 10;
1890 dev_warn(di
->dev
, "Battery voltage OK again\n");
1893 /* This is needed to dispatch LOW_BAT */
1894 ab8500_fg_check_capacity_limits(di
, false);
1898 * ab8500_fg_battok_calc - calculate the bit pattern corresponding
1899 * to the target voltage.
1900 * @di: pointer to the ab8500_fg structure
1901 * @target: target voltage
1903 * Returns bit pattern closest to the target voltage
1904 * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
1907 static int ab8500_fg_battok_calc(struct ab8500_fg
*di
, int target
)
1909 if (target
> BATT_OK_MIN
+
1910 (BATT_OK_INCREMENT
* BATT_OK_MAX_NR_INCREMENTS
))
1911 return BATT_OK_MAX_NR_INCREMENTS
;
1912 if (target
< BATT_OK_MIN
)
1914 return (target
- BATT_OK_MIN
) / BATT_OK_INCREMENT
;
1918 * ab8500_fg_battok_init_hw_register - init battok levels
1919 * @di: pointer to the ab8500_fg structure
1923 static int ab8500_fg_battok_init_hw_register(struct ab8500_fg
*di
)
1933 sel0
= di
->bm
->fg_params
->battok_falling_th_sel0
;
1934 sel1
= di
->bm
->fg_params
->battok_raising_th_sel1
;
1936 cbp_sel0
= ab8500_fg_battok_calc(di
, sel0
);
1937 cbp_sel1
= ab8500_fg_battok_calc(di
, sel1
);
1939 selected
= BATT_OK_MIN
+ cbp_sel0
* BATT_OK_INCREMENT
;
1941 if (selected
!= sel0
)
1942 dev_warn(di
->dev
, "Invalid voltage step:%d, using %d %d\n",
1943 sel0
, selected
, cbp_sel0
);
1945 selected
= BATT_OK_MIN
+ cbp_sel1
* BATT_OK_INCREMENT
;
1947 if (selected
!= sel1
)
1948 dev_warn(di
->dev
, "Invalid voltage step:%d, using %d %d\n",
1949 sel1
, selected
, cbp_sel1
);
1951 new_val
= cbp_sel0
| (cbp_sel1
<< 4);
1953 dev_dbg(di
->dev
, "using: %x %d %d\n", new_val
, cbp_sel0
, cbp_sel1
);
1954 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_SYS_CTRL2_BLOCK
,
1955 AB8500_BATT_OK_REG
, new_val
);
1960 * ab8500_fg_instant_work() - Run the FG state machine instantly
1961 * @work: pointer to the work_struct structure
1963 * Work queue function for instant work
1965 static void ab8500_fg_instant_work(struct work_struct
*work
)
1967 struct ab8500_fg
*di
= container_of(work
, struct ab8500_fg
, fg_work
);
1969 ab8500_fg_algorithm(di
);
1973 * ab8500_fg_cc_data_end_handler() - end of data conversion isr.
1974 * @irq: interrupt number
1975 * @_di: pointer to the ab8500_fg structure
1977 * Returns IRQ status(IRQ_HANDLED)
1979 static irqreturn_t
ab8500_fg_cc_data_end_handler(int irq
, void *_di
)
1981 struct ab8500_fg
*di
= _di
;
1982 if (!di
->nbr_cceoc_irq_cnt
) {
1983 di
->nbr_cceoc_irq_cnt
++;
1984 complete(&di
->ab8500_fg_started
);
1986 di
->nbr_cceoc_irq_cnt
= 0;
1987 complete(&di
->ab8500_fg_complete
);
1993 * ab8500_fg_cc_int_calib_handler () - end of calibration isr.
1994 * @irq: interrupt number
1995 * @_di: pointer to the ab8500_fg structure
1997 * Returns IRQ status(IRQ_HANDLED)
1999 static irqreturn_t
ab8500_fg_cc_int_calib_handler(int irq
, void *_di
)
2001 struct ab8500_fg
*di
= _di
;
2002 di
->calib_state
= AB8500_FG_CALIB_END
;
2003 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
2008 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
2009 * @irq: interrupt number
2010 * @_di: pointer to the ab8500_fg structure
2012 * Returns IRQ status(IRQ_HANDLED)
2014 static irqreturn_t
ab8500_fg_cc_convend_handler(int irq
, void *_di
)
2016 struct ab8500_fg
*di
= _di
;
2018 queue_work(di
->fg_wq
, &di
->fg_acc_cur_work
);
2024 * ab8500_fg_batt_ovv_handler() - Battery OVV occured
2025 * @irq: interrupt number
2026 * @_di: pointer to the ab8500_fg structure
2028 * Returns IRQ status(IRQ_HANDLED)
2030 static irqreturn_t
ab8500_fg_batt_ovv_handler(int irq
, void *_di
)
2032 struct ab8500_fg
*di
= _di
;
2034 dev_dbg(di
->dev
, "Battery OVV\n");
2036 /* Schedule a new HW failure check */
2037 queue_delayed_work(di
->fg_wq
, &di
->fg_check_hw_failure_work
, 0);
2043 * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
2044 * @irq: interrupt number
2045 * @_di: pointer to the ab8500_fg structure
2047 * Returns IRQ status(IRQ_HANDLED)
2049 static irqreturn_t
ab8500_fg_lowbatf_handler(int irq
, void *_di
)
2051 struct ab8500_fg
*di
= _di
;
2053 /* Initiate handling in ab8500_fg_low_bat_work() if not already initiated. */
2054 if (!di
->flags
.low_bat_delay
) {
2055 dev_warn(di
->dev
, "Battery voltage is below LOW threshold\n");
2056 di
->flags
.low_bat_delay
= true;
2058 * Start a timer to check LOW_BAT again after some time
2059 * This is done to avoid shutdown on single voltage dips
2061 queue_delayed_work(di
->fg_wq
, &di
->fg_low_bat_work
,
2062 round_jiffies(LOW_BAT_CHECK_INTERVAL
));
2068 * ab8500_fg_get_property() - get the fg properties
2069 * @psy: pointer to the power_supply structure
2070 * @psp: pointer to the power_supply_property structure
2071 * @val: pointer to the power_supply_propval union
2073 * This function gets called when an application tries to get the
2074 * fg properties by reading the sysfs files.
2075 * voltage_now: battery voltage
2076 * current_now: battery instant current
2077 * current_avg: battery average current
2078 * charge_full_design: capacity where battery is considered full
2079 * charge_now: battery capacity in nAh
2080 * capacity: capacity in percent
2081 * capacity_level: capacity level
2083 * Returns error code in case of failure else 0 on success
2085 static int ab8500_fg_get_property(struct power_supply
*psy
,
2086 enum power_supply_property psp
,
2087 union power_supply_propval
*val
)
2089 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2092 * If battery is identified as unknown and charging of unknown
2093 * batteries is disabled, we always report 100% capacity and
2094 * capacity level UNKNOWN, since we can't calculate
2095 * remaining capacity
2099 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
2100 if (di
->flags
.bat_ovv
)
2101 val
->intval
= BATT_OVV_VALUE
* 1000;
2103 val
->intval
= di
->vbat
* 1000;
2105 case POWER_SUPPLY_PROP_CURRENT_NOW
:
2106 val
->intval
= di
->inst_curr
* 1000;
2108 case POWER_SUPPLY_PROP_CURRENT_AVG
:
2109 val
->intval
= di
->avg_curr
* 1000;
2111 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
2112 val
->intval
= ab8500_fg_convert_mah_to_uwh(di
,
2113 di
->bat_cap
.max_mah_design
);
2115 case POWER_SUPPLY_PROP_ENERGY_FULL
:
2116 val
->intval
= ab8500_fg_convert_mah_to_uwh(di
,
2117 di
->bat_cap
.max_mah
);
2119 case POWER_SUPPLY_PROP_ENERGY_NOW
:
2120 if (di
->flags
.batt_unknown
&& !di
->bm
->chg_unknown_bat
&&
2121 di
->flags
.batt_id_received
)
2122 val
->intval
= ab8500_fg_convert_mah_to_uwh(di
,
2123 di
->bat_cap
.max_mah
);
2125 val
->intval
= ab8500_fg_convert_mah_to_uwh(di
,
2126 di
->bat_cap
.prev_mah
);
2128 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
2129 val
->intval
= di
->bat_cap
.max_mah_design
;
2131 case POWER_SUPPLY_PROP_CHARGE_FULL
:
2132 val
->intval
= di
->bat_cap
.max_mah
;
2134 case POWER_SUPPLY_PROP_CHARGE_NOW
:
2135 if (di
->flags
.batt_unknown
&& !di
->bm
->chg_unknown_bat
&&
2136 di
->flags
.batt_id_received
)
2137 val
->intval
= di
->bat_cap
.max_mah
;
2139 val
->intval
= di
->bat_cap
.prev_mah
;
2141 case POWER_SUPPLY_PROP_CAPACITY
:
2142 if (di
->flags
.batt_unknown
&& !di
->bm
->chg_unknown_bat
&&
2143 di
->flags
.batt_id_received
)
2146 val
->intval
= di
->bat_cap
.prev_percent
;
2148 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
2149 if (di
->flags
.batt_unknown
&& !di
->bm
->chg_unknown_bat
&&
2150 di
->flags
.batt_id_received
)
2151 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
2153 val
->intval
= di
->bat_cap
.prev_level
;
2161 static int ab8500_fg_get_ext_psy_data(struct device
*dev
, void *data
)
2163 struct power_supply
*psy
;
2164 struct power_supply
*ext
= dev_get_drvdata(dev
);
2165 const char **supplicants
= (const char **)ext
->supplied_to
;
2166 struct ab8500_fg
*di
;
2167 union power_supply_propval ret
;
2170 psy
= (struct power_supply
*)data
;
2171 di
= power_supply_get_drvdata(psy
);
2174 * For all psy where the name of your driver
2175 * appears in any supplied_to
2177 j
= match_string(supplicants
, ext
->num_supplicants
, psy
->desc
->name
);
2181 /* Go through all properties for the psy */
2182 for (j
= 0; j
< ext
->desc
->num_properties
; j
++) {
2183 enum power_supply_property prop
;
2184 prop
= ext
->desc
->properties
[j
];
2186 if (power_supply_get_property(ext
, prop
, &ret
))
2190 case POWER_SUPPLY_PROP_STATUS
:
2191 switch (ext
->desc
->type
) {
2192 case POWER_SUPPLY_TYPE_BATTERY
:
2193 switch (ret
.intval
) {
2194 case POWER_SUPPLY_STATUS_UNKNOWN
:
2195 case POWER_SUPPLY_STATUS_DISCHARGING
:
2196 case POWER_SUPPLY_STATUS_NOT_CHARGING
:
2197 if (!di
->flags
.charging
)
2199 di
->flags
.charging
= false;
2200 di
->flags
.fully_charged
= false;
2201 if (di
->bm
->capacity_scaling
)
2202 ab8500_fg_update_cap_scalers(di
);
2203 queue_work(di
->fg_wq
, &di
->fg_work
);
2205 case POWER_SUPPLY_STATUS_FULL
:
2206 if (di
->flags
.fully_charged
)
2208 di
->flags
.fully_charged
= true;
2209 di
->flags
.force_full
= true;
2210 /* Save current capacity as maximum */
2211 di
->bat_cap
.max_mah
= di
->bat_cap
.mah
;
2212 queue_work(di
->fg_wq
, &di
->fg_work
);
2214 case POWER_SUPPLY_STATUS_CHARGING
:
2215 if (di
->flags
.charging
&&
2216 !di
->flags
.fully_charged
)
2218 di
->flags
.charging
= true;
2219 di
->flags
.fully_charged
= false;
2220 if (di
->bm
->capacity_scaling
)
2221 ab8500_fg_update_cap_scalers(di
);
2222 queue_work(di
->fg_wq
, &di
->fg_work
);
2229 case POWER_SUPPLY_PROP_TECHNOLOGY
:
2230 switch (ext
->desc
->type
) {
2231 case POWER_SUPPLY_TYPE_BATTERY
:
2232 if (!di
->flags
.batt_id_received
&&
2233 di
->bm
->batt_id
!= BATTERY_UNKNOWN
) {
2234 const struct abx500_battery_type
*b
;
2236 b
= &(di
->bm
->bat_type
[di
->bm
->batt_id
]);
2238 di
->flags
.batt_id_received
= true;
2240 di
->bat_cap
.max_mah_design
=
2242 b
->charge_full_design
;
2244 di
->bat_cap
.max_mah
=
2245 di
->bat_cap
.max_mah_design
;
2247 di
->vbat_nom
= b
->nominal_voltage
;
2251 di
->flags
.batt_unknown
= false;
2253 di
->flags
.batt_unknown
= true;
2259 case POWER_SUPPLY_PROP_TEMP
:
2260 switch (ext
->desc
->type
) {
2261 case POWER_SUPPLY_TYPE_BATTERY
:
2262 if (di
->flags
.batt_id_received
)
2263 di
->bat_temp
= ret
.intval
;
2277 * ab8500_fg_init_hw_registers() - Set up FG related registers
2278 * @di: pointer to the ab8500_fg structure
2280 * Set up battery OVV, low battery voltage registers
2282 static int ab8500_fg_init_hw_registers(struct ab8500_fg
*di
)
2286 /* Set VBAT OVV threshold */
2287 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
2293 dev_err(di
->dev
, "failed to set BATT_OVV\n");
2297 /* Enable VBAT OVV detection */
2298 ret
= abx500_mask_and_set_register_interruptible(di
->dev
,
2304 dev_err(di
->dev
, "failed to enable BATT_OVV\n");
2308 /* Low Battery Voltage */
2309 ret
= abx500_set_register_interruptible(di
->dev
,
2310 AB8500_SYS_CTRL2_BLOCK
,
2312 ab8500_volt_to_regval(
2313 di
->bm
->fg_params
->lowbat_threshold
) << 1 |
2316 dev_err(di
->dev
, "%s write failed\n", __func__
);
2320 /* Battery OK threshold */
2321 ret
= ab8500_fg_battok_init_hw_register(di
);
2323 dev_err(di
->dev
, "BattOk init write failed.\n");
2327 if (is_ab8505(di
->parent
)) {
2328 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2329 AB8505_RTC_PCUT_MAX_TIME_REG
, di
->bm
->fg_params
->pcut_max_time
);
2332 dev_err(di
->dev
, "%s write failed AB8505_RTC_PCUT_MAX_TIME_REG\n", __func__
);
2336 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2337 AB8505_RTC_PCUT_FLAG_TIME_REG
, di
->bm
->fg_params
->pcut_flag_time
);
2340 dev_err(di
->dev
, "%s write failed AB8505_RTC_PCUT_FLAG_TIME_REG\n", __func__
);
2344 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2345 AB8505_RTC_PCUT_RESTART_REG
, di
->bm
->fg_params
->pcut_max_restart
);
2348 dev_err(di
->dev
, "%s write failed AB8505_RTC_PCUT_RESTART_REG\n", __func__
);
2352 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2353 AB8505_RTC_PCUT_DEBOUNCE_REG
, di
->bm
->fg_params
->pcut_debounce_time
);
2356 dev_err(di
->dev
, "%s write failed AB8505_RTC_PCUT_DEBOUNCE_REG\n", __func__
);
2360 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2361 AB8505_RTC_PCUT_CTL_STATUS_REG
, di
->bm
->fg_params
->pcut_enable
);
2364 dev_err(di
->dev
, "%s write failed AB8505_RTC_PCUT_CTL_STATUS_REG\n", __func__
);
2373 * ab8500_fg_external_power_changed() - callback for power supply changes
2374 * @psy: pointer to the structure power_supply
2376 * This function is the entry point of the pointer external_power_changed
2377 * of the structure power_supply.
2378 * This function gets executed when there is a change in any external power
2379 * supply that this driver needs to be notified of.
2381 static void ab8500_fg_external_power_changed(struct power_supply
*psy
)
2383 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2385 class_for_each_device(power_supply_class
, NULL
,
2386 di
->fg_psy
, ab8500_fg_get_ext_psy_data
);
2390 * ab8500_fg_reinit_work() - work to reset the FG algorithm
2391 * @work: pointer to the work_struct structure
2393 * Used to reset the current battery capacity to be able to
2394 * retrigger a new voltage base capacity calculation. For
2395 * test and verification purpose.
2397 static void ab8500_fg_reinit_work(struct work_struct
*work
)
2399 struct ab8500_fg
*di
= container_of(work
, struct ab8500_fg
,
2400 fg_reinit_work
.work
);
2402 if (di
->flags
.calibrate
== false) {
2403 dev_dbg(di
->dev
, "Resetting FG state machine to init.\n");
2404 ab8500_fg_clear_cap_samples(di
);
2405 ab8500_fg_calc_cap_discharge_voltage(di
, true);
2406 ab8500_fg_charge_state_to(di
, AB8500_FG_CHARGE_INIT
);
2407 ab8500_fg_discharge_state_to(di
, AB8500_FG_DISCHARGE_INIT
);
2408 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
2411 dev_err(di
->dev
, "Residual offset calibration ongoing "
2413 /* Wait one second until next try*/
2414 queue_delayed_work(di
->fg_wq
, &di
->fg_reinit_work
,
2419 /* Exposure to the sysfs interface */
2421 struct ab8500_fg_sysfs_entry
{
2422 struct attribute attr
;
2423 ssize_t (*show
)(struct ab8500_fg
*, char *);
2424 ssize_t (*store
)(struct ab8500_fg
*, const char *, size_t);
2427 static ssize_t
charge_full_show(struct ab8500_fg
*di
, char *buf
)
2429 return sprintf(buf
, "%d\n", di
->bat_cap
.max_mah
);
2432 static ssize_t
charge_full_store(struct ab8500_fg
*di
, const char *buf
,
2435 unsigned long charge_full
;
2438 ret
= kstrtoul(buf
, 10, &charge_full
);
2442 di
->bat_cap
.max_mah
= (int) charge_full
;
2446 static ssize_t
charge_now_show(struct ab8500_fg
*di
, char *buf
)
2448 return sprintf(buf
, "%d\n", di
->bat_cap
.prev_mah
);
2451 static ssize_t
charge_now_store(struct ab8500_fg
*di
, const char *buf
,
2454 unsigned long charge_now
;
2457 ret
= kstrtoul(buf
, 10, &charge_now
);
2461 di
->bat_cap
.user_mah
= (int) charge_now
;
2462 di
->flags
.user_cap
= true;
2463 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
2467 static struct ab8500_fg_sysfs_entry charge_full_attr
=
2468 __ATTR(charge_full
, 0644, charge_full_show
, charge_full_store
);
2470 static struct ab8500_fg_sysfs_entry charge_now_attr
=
2471 __ATTR(charge_now
, 0644, charge_now_show
, charge_now_store
);
2474 ab8500_fg_show(struct kobject
*kobj
, struct attribute
*attr
, char *buf
)
2476 struct ab8500_fg_sysfs_entry
*entry
;
2477 struct ab8500_fg
*di
;
2479 entry
= container_of(attr
, struct ab8500_fg_sysfs_entry
, attr
);
2480 di
= container_of(kobj
, struct ab8500_fg
, fg_kobject
);
2485 return entry
->show(di
, buf
);
2488 ab8500_fg_store(struct kobject
*kobj
, struct attribute
*attr
, const char *buf
,
2491 struct ab8500_fg_sysfs_entry
*entry
;
2492 struct ab8500_fg
*di
;
2494 entry
= container_of(attr
, struct ab8500_fg_sysfs_entry
, attr
);
2495 di
= container_of(kobj
, struct ab8500_fg
, fg_kobject
);
2500 return entry
->store(di
, buf
, count
);
2503 static const struct sysfs_ops ab8500_fg_sysfs_ops
= {
2504 .show
= ab8500_fg_show
,
2505 .store
= ab8500_fg_store
,
2508 static struct attribute
*ab8500_fg_attrs
[] = {
2509 &charge_full_attr
.attr
,
2510 &charge_now_attr
.attr
,
2514 static struct kobj_type ab8500_fg_ktype
= {
2515 .sysfs_ops
= &ab8500_fg_sysfs_ops
,
2516 .default_attrs
= ab8500_fg_attrs
,
2520 * ab8500_fg_sysfs_exit() - de-init of sysfs entry
2521 * @di: pointer to the struct ab8500_chargalg
2523 * This function removes the entry in sysfs.
2525 static void ab8500_fg_sysfs_exit(struct ab8500_fg
*di
)
2527 kobject_del(&di
->fg_kobject
);
2531 * ab8500_fg_sysfs_init() - init of sysfs entry
2532 * @di: pointer to the struct ab8500_chargalg
2534 * This function adds an entry in sysfs.
2535 * Returns error code in case of failure else 0(on success)
2537 static int ab8500_fg_sysfs_init(struct ab8500_fg
*di
)
2541 ret
= kobject_init_and_add(&di
->fg_kobject
,
2545 dev_err(di
->dev
, "failed to create sysfs entry\n");
2550 static ssize_t
ab8505_powercut_flagtime_read(struct device
*dev
,
2551 struct device_attribute
*attr
,
2556 struct power_supply
*psy
= dev_get_drvdata(dev
);
2557 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2559 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2560 AB8505_RTC_PCUT_FLAG_TIME_REG
, ®_value
);
2563 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2567 return scnprintf(buf
, PAGE_SIZE
, "%d\n", (reg_value
& 0x7F));
2573 static ssize_t
ab8505_powercut_flagtime_write(struct device
*dev
,
2574 struct device_attribute
*attr
,
2575 const char *buf
, size_t count
)
2579 struct power_supply
*psy
= dev_get_drvdata(dev
);
2580 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2582 if (kstrtoint(buf
, 10, ®_value
))
2585 if (reg_value
> 0x7F) {
2586 dev_err(dev
, "Incorrect parameter, echo 0 (1.98s) - 127 (15.625ms) for flagtime\n");
2590 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2591 AB8505_RTC_PCUT_FLAG_TIME_REG
, (u8
)reg_value
);
2594 dev_err(dev
, "Failed to set AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2600 static ssize_t
ab8505_powercut_maxtime_read(struct device
*dev
,
2601 struct device_attribute
*attr
,
2606 struct power_supply
*psy
= dev_get_drvdata(dev
);
2607 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2609 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2610 AB8505_RTC_PCUT_MAX_TIME_REG
, ®_value
);
2613 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_MAX_TIME_REG\n");
2617 return scnprintf(buf
, PAGE_SIZE
, "%d\n", (reg_value
& 0x7F));
2624 static ssize_t
ab8505_powercut_maxtime_write(struct device
*dev
,
2625 struct device_attribute
*attr
,
2626 const char *buf
, size_t count
)
2630 struct power_supply
*psy
= dev_get_drvdata(dev
);
2631 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2633 if (kstrtoint(buf
, 10, ®_value
))
2636 if (reg_value
> 0x7F) {
2637 dev_err(dev
, "Incorrect parameter, echo 0 (0.0s) - 127 (1.98s) for maxtime\n");
2641 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2642 AB8505_RTC_PCUT_MAX_TIME_REG
, (u8
)reg_value
);
2645 dev_err(dev
, "Failed to set AB8505_RTC_PCUT_MAX_TIME_REG\n");
2651 static ssize_t
ab8505_powercut_restart_read(struct device
*dev
,
2652 struct device_attribute
*attr
,
2657 struct power_supply
*psy
= dev_get_drvdata(dev
);
2658 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2660 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2661 AB8505_RTC_PCUT_RESTART_REG
, ®_value
);
2664 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2668 return scnprintf(buf
, PAGE_SIZE
, "%d\n", (reg_value
& 0xF));
2674 static ssize_t
ab8505_powercut_restart_write(struct device
*dev
,
2675 struct device_attribute
*attr
,
2676 const char *buf
, size_t count
)
2680 struct power_supply
*psy
= dev_get_drvdata(dev
);
2681 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2683 if (kstrtoint(buf
, 10, ®_value
))
2686 if (reg_value
> 0xF) {
2687 dev_err(dev
, "Incorrect parameter, echo 0 - 15 for number of restart\n");
2691 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2692 AB8505_RTC_PCUT_RESTART_REG
, (u8
)reg_value
);
2695 dev_err(dev
, "Failed to set AB8505_RTC_PCUT_RESTART_REG\n");
2702 static ssize_t
ab8505_powercut_timer_read(struct device
*dev
,
2703 struct device_attribute
*attr
,
2708 struct power_supply
*psy
= dev_get_drvdata(dev
);
2709 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2711 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2712 AB8505_RTC_PCUT_TIME_REG
, ®_value
);
2715 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_TIME_REG\n");
2719 return scnprintf(buf
, PAGE_SIZE
, "%d\n", (reg_value
& 0x7F));
2725 static ssize_t
ab8505_powercut_restart_counter_read(struct device
*dev
,
2726 struct device_attribute
*attr
,
2731 struct power_supply
*psy
= dev_get_drvdata(dev
);
2732 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2734 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2735 AB8505_RTC_PCUT_RESTART_REG
, ®_value
);
2738 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2742 return scnprintf(buf
, PAGE_SIZE
, "%d\n", (reg_value
& 0xF0) >> 4);
2748 static ssize_t
ab8505_powercut_read(struct device
*dev
,
2749 struct device_attribute
*attr
,
2754 struct power_supply
*psy
= dev_get_drvdata(dev
);
2755 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2757 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2758 AB8505_RTC_PCUT_CTL_STATUS_REG
, ®_value
);
2763 return scnprintf(buf
, PAGE_SIZE
, "%d\n", (reg_value
& 0x1));
2769 static ssize_t
ab8505_powercut_write(struct device
*dev
,
2770 struct device_attribute
*attr
,
2771 const char *buf
, size_t count
)
2775 struct power_supply
*psy
= dev_get_drvdata(dev
);
2776 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2778 if (kstrtoint(buf
, 10, ®_value
))
2781 if (reg_value
> 0x1) {
2782 dev_err(dev
, "Incorrect parameter, echo 0/1 to disable/enable Pcut feature\n");
2786 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2787 AB8505_RTC_PCUT_CTL_STATUS_REG
, (u8
)reg_value
);
2790 dev_err(dev
, "Failed to set AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2796 static ssize_t
ab8505_powercut_flag_read(struct device
*dev
,
2797 struct device_attribute
*attr
,
2803 struct power_supply
*psy
= dev_get_drvdata(dev
);
2804 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2806 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2807 AB8505_RTC_PCUT_CTL_STATUS_REG
, ®_value
);
2810 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2814 return scnprintf(buf
, PAGE_SIZE
, "%d\n", ((reg_value
& 0x10) >> 4));
2820 static ssize_t
ab8505_powercut_debounce_read(struct device
*dev
,
2821 struct device_attribute
*attr
,
2826 struct power_supply
*psy
= dev_get_drvdata(dev
);
2827 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2829 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2830 AB8505_RTC_PCUT_DEBOUNCE_REG
, ®_value
);
2833 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2837 return scnprintf(buf
, PAGE_SIZE
, "%d\n", (reg_value
& 0x7));
2843 static ssize_t
ab8505_powercut_debounce_write(struct device
*dev
,
2844 struct device_attribute
*attr
,
2845 const char *buf
, size_t count
)
2849 struct power_supply
*psy
= dev_get_drvdata(dev
);
2850 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2852 if (kstrtoint(buf
, 10, ®_value
))
2855 if (reg_value
> 0x7) {
2856 dev_err(dev
, "Incorrect parameter, echo 0 to 7 for debounce setting\n");
2860 ret
= abx500_set_register_interruptible(di
->dev
, AB8500_RTC
,
2861 AB8505_RTC_PCUT_DEBOUNCE_REG
, (u8
)reg_value
);
2864 dev_err(dev
, "Failed to set AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2870 static ssize_t
ab8505_powercut_enable_status_read(struct device
*dev
,
2871 struct device_attribute
*attr
,
2876 struct power_supply
*psy
= dev_get_drvdata(dev
);
2877 struct ab8500_fg
*di
= power_supply_get_drvdata(psy
);
2879 ret
= abx500_get_register_interruptible(di
->dev
, AB8500_RTC
,
2880 AB8505_RTC_PCUT_CTL_STATUS_REG
, ®_value
);
2883 dev_err(dev
, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2887 return scnprintf(buf
, PAGE_SIZE
, "%d\n", ((reg_value
& 0x20) >> 5));
2893 static struct device_attribute ab8505_fg_sysfs_psy_attrs
[] = {
2894 __ATTR(powercut_flagtime
, (S_IRUGO
| S_IWUSR
| S_IWGRP
),
2895 ab8505_powercut_flagtime_read
, ab8505_powercut_flagtime_write
),
2896 __ATTR(powercut_maxtime
, (S_IRUGO
| S_IWUSR
| S_IWGRP
),
2897 ab8505_powercut_maxtime_read
, ab8505_powercut_maxtime_write
),
2898 __ATTR(powercut_restart_max
, (S_IRUGO
| S_IWUSR
| S_IWGRP
),
2899 ab8505_powercut_restart_read
, ab8505_powercut_restart_write
),
2900 __ATTR(powercut_timer
, S_IRUGO
, ab8505_powercut_timer_read
, NULL
),
2901 __ATTR(powercut_restart_counter
, S_IRUGO
,
2902 ab8505_powercut_restart_counter_read
, NULL
),
2903 __ATTR(powercut_enable
, (S_IRUGO
| S_IWUSR
| S_IWGRP
),
2904 ab8505_powercut_read
, ab8505_powercut_write
),
2905 __ATTR(powercut_flag
, S_IRUGO
, ab8505_powercut_flag_read
, NULL
),
2906 __ATTR(powercut_debounce_time
, (S_IRUGO
| S_IWUSR
| S_IWGRP
),
2907 ab8505_powercut_debounce_read
, ab8505_powercut_debounce_write
),
2908 __ATTR(powercut_enable_status
, S_IRUGO
,
2909 ab8505_powercut_enable_status_read
, NULL
),
2912 static int ab8500_fg_sysfs_psy_create_attrs(struct ab8500_fg
*di
)
2916 if (is_ab8505(di
->parent
)) {
2917 for (i
= 0; i
< ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs
); i
++)
2918 if (device_create_file(&di
->fg_psy
->dev
,
2919 &ab8505_fg_sysfs_psy_attrs
[i
]))
2920 goto sysfs_psy_create_attrs_failed_ab8505
;
2923 sysfs_psy_create_attrs_failed_ab8505
:
2924 dev_err(&di
->fg_psy
->dev
, "Failed creating sysfs psy attrs for ab8505.\n");
2926 device_remove_file(&di
->fg_psy
->dev
,
2927 &ab8505_fg_sysfs_psy_attrs
[i
]);
2932 static void ab8500_fg_sysfs_psy_remove_attrs(struct ab8500_fg
*di
)
2936 if (is_ab8505(di
->parent
)) {
2937 for (i
= 0; i
< ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs
); i
++)
2938 (void)device_remove_file(&di
->fg_psy
->dev
,
2939 &ab8505_fg_sysfs_psy_attrs
[i
]);
2943 /* Exposure to the sysfs interface <<END>> */
2945 #if defined(CONFIG_PM)
2946 static int ab8500_fg_resume(struct platform_device
*pdev
)
2948 struct ab8500_fg
*di
= platform_get_drvdata(pdev
);
2951 * Change state if we're not charging. If we're charging we will wake
2954 if (!di
->flags
.charging
) {
2955 ab8500_fg_discharge_state_to(di
, AB8500_FG_DISCHARGE_WAKEUP
);
2956 queue_work(di
->fg_wq
, &di
->fg_work
);
2962 static int ab8500_fg_suspend(struct platform_device
*pdev
,
2965 struct ab8500_fg
*di
= platform_get_drvdata(pdev
);
2967 flush_delayed_work(&di
->fg_periodic_work
);
2968 flush_work(&di
->fg_work
);
2969 flush_work(&di
->fg_acc_cur_work
);
2970 flush_delayed_work(&di
->fg_reinit_work
);
2971 flush_delayed_work(&di
->fg_low_bat_work
);
2972 flush_delayed_work(&di
->fg_check_hw_failure_work
);
2975 * If the FG is enabled we will disable it before going to suspend
2976 * only if we're not charging
2978 if (di
->flags
.fg_enabled
&& !di
->flags
.charging
)
2979 ab8500_fg_coulomb_counter(di
, false);
2984 #define ab8500_fg_suspend NULL
2985 #define ab8500_fg_resume NULL
2988 static int ab8500_fg_remove(struct platform_device
*pdev
)
2991 struct ab8500_fg
*di
= platform_get_drvdata(pdev
);
2993 list_del(&di
->node
);
2995 /* Disable coulomb counter */
2996 ret
= ab8500_fg_coulomb_counter(di
, false);
2998 dev_err(di
->dev
, "failed to disable coulomb counter\n");
3000 destroy_workqueue(di
->fg_wq
);
3001 ab8500_fg_sysfs_exit(di
);
3003 flush_scheduled_work();
3004 ab8500_fg_sysfs_psy_remove_attrs(di
);
3005 power_supply_unregister(di
->fg_psy
);
3009 /* ab8500 fg driver interrupts and their respective isr */
3010 static struct ab8500_fg_interrupts ab8500_fg_irq_th
[] = {
3011 {"NCONV_ACCU", ab8500_fg_cc_convend_handler
},
3012 {"BATT_OVV", ab8500_fg_batt_ovv_handler
},
3013 {"LOW_BAT_F", ab8500_fg_lowbatf_handler
},
3014 {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler
},
3017 static struct ab8500_fg_interrupts ab8500_fg_irq_bh
[] = {
3018 {"CCEOC", ab8500_fg_cc_data_end_handler
},
3021 static char *supply_interface
[] = {
3026 static const struct power_supply_desc ab8500_fg_desc
= {
3027 .name
= "ab8500_fg",
3028 .type
= POWER_SUPPLY_TYPE_BATTERY
,
3029 .properties
= ab8500_fg_props
,
3030 .num_properties
= ARRAY_SIZE(ab8500_fg_props
),
3031 .get_property
= ab8500_fg_get_property
,
3032 .external_power_changed
= ab8500_fg_external_power_changed
,
3035 static int ab8500_fg_probe(struct platform_device
*pdev
)
3037 struct device_node
*np
= pdev
->dev
.of_node
;
3038 struct abx500_bm_data
*plat
= pdev
->dev
.platform_data
;
3039 struct power_supply_config psy_cfg
= {};
3040 struct ab8500_fg
*di
;
3044 di
= devm_kzalloc(&pdev
->dev
, sizeof(*di
), GFP_KERNEL
);
3046 dev_err(&pdev
->dev
, "%s no mem for ab8500_fg\n", __func__
);
3051 dev_err(&pdev
->dev
, "no battery management data supplied\n");
3057 ret
= ab8500_bm_of_probe(&pdev
->dev
, np
, di
->bm
);
3059 dev_err(&pdev
->dev
, "failed to get battery information\n");
3064 mutex_init(&di
->cc_lock
);
3066 /* get parent data */
3067 di
->dev
= &pdev
->dev
;
3068 di
->parent
= dev_get_drvdata(pdev
->dev
.parent
);
3070 di
->main_bat_v
= devm_iio_channel_get(&pdev
->dev
, "main_bat_v");
3071 if (IS_ERR(di
->main_bat_v
)) {
3072 if (PTR_ERR(di
->main_bat_v
) == -ENODEV
)
3073 return -EPROBE_DEFER
;
3074 dev_err(&pdev
->dev
, "failed to get main battery ADC channel\n");
3075 return PTR_ERR(di
->main_bat_v
);
3078 psy_cfg
.supplied_to
= supply_interface
;
3079 psy_cfg
.num_supplicants
= ARRAY_SIZE(supply_interface
);
3080 psy_cfg
.drv_data
= di
;
3082 di
->bat_cap
.max_mah_design
= MILLI_TO_MICRO
*
3083 di
->bm
->bat_type
[di
->bm
->batt_id
].charge_full_design
;
3085 di
->bat_cap
.max_mah
= di
->bat_cap
.max_mah_design
;
3087 di
->vbat_nom
= di
->bm
->bat_type
[di
->bm
->batt_id
].nominal_voltage
;
3089 di
->init_capacity
= true;
3091 ab8500_fg_charge_state_to(di
, AB8500_FG_CHARGE_INIT
);
3092 ab8500_fg_discharge_state_to(di
, AB8500_FG_DISCHARGE_INIT
);
3094 /* Create a work queue for running the FG algorithm */
3095 di
->fg_wq
= alloc_ordered_workqueue("ab8500_fg_wq", WQ_MEM_RECLAIM
);
3096 if (di
->fg_wq
== NULL
) {
3097 dev_err(di
->dev
, "failed to create work queue\n");
3101 /* Init work for running the fg algorithm instantly */
3102 INIT_WORK(&di
->fg_work
, ab8500_fg_instant_work
);
3104 /* Init work for getting the battery accumulated current */
3105 INIT_WORK(&di
->fg_acc_cur_work
, ab8500_fg_acc_cur_work
);
3107 /* Init work for reinitialising the fg algorithm */
3108 INIT_DEFERRABLE_WORK(&di
->fg_reinit_work
,
3109 ab8500_fg_reinit_work
);
3111 /* Work delayed Queue to run the state machine */
3112 INIT_DEFERRABLE_WORK(&di
->fg_periodic_work
,
3113 ab8500_fg_periodic_work
);
3115 /* Work to check low battery condition */
3116 INIT_DEFERRABLE_WORK(&di
->fg_low_bat_work
,
3117 ab8500_fg_low_bat_work
);
3119 /* Init work for HW failure check */
3120 INIT_DEFERRABLE_WORK(&di
->fg_check_hw_failure_work
,
3121 ab8500_fg_check_hw_failure_work
);
3123 /* Reset battery low voltage flag */
3124 di
->flags
.low_bat
= false;
3126 /* Initialize low battery counter */
3127 di
->low_bat_cnt
= 10;
3129 /* Initialize OVV, and other registers */
3130 ret
= ab8500_fg_init_hw_registers(di
);
3132 dev_err(di
->dev
, "failed to initialize registers\n");
3133 goto free_inst_curr_wq
;
3136 /* Consider battery unknown until we're informed otherwise */
3137 di
->flags
.batt_unknown
= true;
3138 di
->flags
.batt_id_received
= false;
3140 /* Register FG power supply class */
3141 di
->fg_psy
= power_supply_register(di
->dev
, &ab8500_fg_desc
, &psy_cfg
);
3142 if (IS_ERR(di
->fg_psy
)) {
3143 dev_err(di
->dev
, "failed to register FG psy\n");
3144 ret
= PTR_ERR(di
->fg_psy
);
3145 goto free_inst_curr_wq
;
3148 di
->fg_samples
= SEC_TO_SAMPLE(di
->bm
->fg_params
->init_timer
);
3149 ab8500_fg_coulomb_counter(di
, true);
3152 * Initialize completion used to notify completion and start
3155 init_completion(&di
->ab8500_fg_started
);
3156 init_completion(&di
->ab8500_fg_complete
);
3158 /* Register primary interrupt handlers */
3159 for (i
= 0; i
< ARRAY_SIZE(ab8500_fg_irq_th
); i
++) {
3160 irq
= platform_get_irq_byname(pdev
, ab8500_fg_irq_th
[i
].name
);
3166 ret
= request_irq(irq
, ab8500_fg_irq_th
[i
].isr
,
3167 IRQF_SHARED
| IRQF_NO_SUSPEND
,
3168 ab8500_fg_irq_th
[i
].name
, di
);
3171 dev_err(di
->dev
, "failed to request %s IRQ %d: %d\n",
3172 ab8500_fg_irq_th
[i
].name
, irq
, ret
);
3175 dev_dbg(di
->dev
, "Requested %s IRQ %d: %d\n",
3176 ab8500_fg_irq_th
[i
].name
, irq
, ret
);
3179 /* Register threaded interrupt handler */
3180 irq
= platform_get_irq_byname(pdev
, ab8500_fg_irq_bh
[0].name
);
3186 ret
= request_threaded_irq(irq
, NULL
, ab8500_fg_irq_bh
[0].isr
,
3187 IRQF_SHARED
| IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
3188 ab8500_fg_irq_bh
[0].name
, di
);
3191 dev_err(di
->dev
, "failed to request %s IRQ %d: %d\n",
3192 ab8500_fg_irq_bh
[0].name
, irq
, ret
);
3195 dev_dbg(di
->dev
, "Requested %s IRQ %d: %d\n",
3196 ab8500_fg_irq_bh
[0].name
, irq
, ret
);
3198 di
->irq
= platform_get_irq_byname(pdev
, "CCEOC");
3199 disable_irq(di
->irq
);
3200 di
->nbr_cceoc_irq_cnt
= 0;
3202 platform_set_drvdata(pdev
, di
);
3204 ret
= ab8500_fg_sysfs_init(di
);
3206 dev_err(di
->dev
, "failed to create sysfs entry\n");
3210 ret
= ab8500_fg_sysfs_psy_create_attrs(di
);
3212 dev_err(di
->dev
, "failed to create FG psy\n");
3213 ab8500_fg_sysfs_exit(di
);
3217 /* Calibrate the fg first time */
3218 di
->flags
.calibrate
= true;
3219 di
->calib_state
= AB8500_FG_CALIB_INIT
;
3221 /* Use room temp as default value until we get an update from driver. */
3224 /* Run the FG algorithm */
3225 queue_delayed_work(di
->fg_wq
, &di
->fg_periodic_work
, 0);
3227 list_add_tail(&di
->node
, &ab8500_fg_list
);
3232 /* We also have to free all registered irqs */
3233 irq
= platform_get_irq_byname(pdev
, ab8500_fg_irq_bh
[0].name
);
3237 /* Last assignment of i from primary interrupt handlers */
3238 irq
= platform_get_irq_byname(pdev
, ab8500_fg_irq_th
[i
].name
);
3242 power_supply_unregister(di
->fg_psy
);
3244 destroy_workqueue(di
->fg_wq
);
3248 static const struct of_device_id ab8500_fg_match
[] = {
3249 { .compatible
= "stericsson,ab8500-fg", },
3253 static struct platform_driver ab8500_fg_driver
= {
3254 .probe
= ab8500_fg_probe
,
3255 .remove
= ab8500_fg_remove
,
3256 .suspend
= ab8500_fg_suspend
,
3257 .resume
= ab8500_fg_resume
,
3259 .name
= "ab8500-fg",
3260 .of_match_table
= ab8500_fg_match
,
3264 static int __init
ab8500_fg_init(void)
3266 return platform_driver_register(&ab8500_fg_driver
);
3269 static void __exit
ab8500_fg_exit(void)
3271 platform_driver_unregister(&ab8500_fg_driver
);
3274 subsys_initcall_sync(ab8500_fg_init
);
3275 module_exit(ab8500_fg_exit
);
3277 MODULE_LICENSE("GPL v2");
3278 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
3279 MODULE_ALIAS("platform:ab8500-fg");
3280 MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");