1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) ST-Ericsson AB 2012
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Main and Back-up battery management driver.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Note: Backup battery management is required in case of Li-Ion battery and not
8*4882a593Smuzhiyun * for capacitive battery. HREF boards have capacitive battery and hence backup
9*4882a593Smuzhiyun * battery management is not used and the supported code is available in this
10*4882a593Smuzhiyun * driver.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Author:
13*4882a593Smuzhiyun * Johan Palsson <johan.palsson@stericsson.com>
14*4882a593Smuzhiyun * Karl Komierowski <karl.komierowski@stericsson.com>
15*4882a593Smuzhiyun * Arun R Murthy <arun.murthy@stericsson.com>
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/init.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/device.h>
21*4882a593Smuzhiyun #include <linux/interrupt.h>
22*4882a593Smuzhiyun #include <linux/platform_device.h>
23*4882a593Smuzhiyun #include <linux/power_supply.h>
24*4882a593Smuzhiyun #include <linux/kobject.h>
25*4882a593Smuzhiyun #include <linux/slab.h>
26*4882a593Smuzhiyun #include <linux/delay.h>
27*4882a593Smuzhiyun #include <linux/time.h>
28*4882a593Smuzhiyun #include <linux/time64.h>
29*4882a593Smuzhiyun #include <linux/of.h>
30*4882a593Smuzhiyun #include <linux/completion.h>
31*4882a593Smuzhiyun #include <linux/mfd/core.h>
32*4882a593Smuzhiyun #include <linux/mfd/abx500.h>
33*4882a593Smuzhiyun #include <linux/mfd/abx500/ab8500.h>
34*4882a593Smuzhiyun #include <linux/mfd/abx500/ab8500-bm.h>
35*4882a593Smuzhiyun #include <linux/iio/consumer.h>
36*4882a593Smuzhiyun #include <linux/kernel.h>
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #define MILLI_TO_MICRO 1000
39*4882a593Smuzhiyun #define FG_LSB_IN_MA 1627
40*4882a593Smuzhiyun #define QLSB_NANO_AMP_HOURS_X10 1071
41*4882a593Smuzhiyun #define INS_CURR_TIMEOUT (3 * HZ)
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #define SEC_TO_SAMPLE(S) (S * 4)
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #define NBR_AVG_SAMPLES 20
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #define LOW_BAT_CHECK_INTERVAL (HZ / 16) /* 62.5 ms */
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */
50*4882a593Smuzhiyun #define BATT_OK_MIN 2360 /* mV */
51*4882a593Smuzhiyun #define BATT_OK_INCREMENT 50 /* mV */
52*4882a593Smuzhiyun #define BATT_OK_MAX_NR_INCREMENTS 0xE
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* FG constants */
55*4882a593Smuzhiyun #define BATT_OVV 0x01
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #define interpolate(x, x1, y1, x2, y2) \
58*4882a593Smuzhiyun ((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1))));
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /**
61*4882a593Smuzhiyun * struct ab8500_fg_interrupts - ab8500 fg interupts
62*4882a593Smuzhiyun * @name: name of the interrupt
63*4882a593Smuzhiyun * @isr function pointer to the isr
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun struct ab8500_fg_interrupts {
66*4882a593Smuzhiyun char *name;
67*4882a593Smuzhiyun irqreturn_t (*isr)(int irq, void *data);
68*4882a593Smuzhiyun };
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun enum ab8500_fg_discharge_state {
71*4882a593Smuzhiyun AB8500_FG_DISCHARGE_INIT,
72*4882a593Smuzhiyun AB8500_FG_DISCHARGE_INITMEASURING,
73*4882a593Smuzhiyun AB8500_FG_DISCHARGE_INIT_RECOVERY,
74*4882a593Smuzhiyun AB8500_FG_DISCHARGE_RECOVERY,
75*4882a593Smuzhiyun AB8500_FG_DISCHARGE_READOUT_INIT,
76*4882a593Smuzhiyun AB8500_FG_DISCHARGE_READOUT,
77*4882a593Smuzhiyun AB8500_FG_DISCHARGE_WAKEUP,
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun static char *discharge_state[] = {
81*4882a593Smuzhiyun "DISCHARGE_INIT",
82*4882a593Smuzhiyun "DISCHARGE_INITMEASURING",
83*4882a593Smuzhiyun "DISCHARGE_INIT_RECOVERY",
84*4882a593Smuzhiyun "DISCHARGE_RECOVERY",
85*4882a593Smuzhiyun "DISCHARGE_READOUT_INIT",
86*4882a593Smuzhiyun "DISCHARGE_READOUT",
87*4882a593Smuzhiyun "DISCHARGE_WAKEUP",
88*4882a593Smuzhiyun };
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun enum ab8500_fg_charge_state {
91*4882a593Smuzhiyun AB8500_FG_CHARGE_INIT,
92*4882a593Smuzhiyun AB8500_FG_CHARGE_READOUT,
93*4882a593Smuzhiyun };
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun static char *charge_state[] = {
96*4882a593Smuzhiyun "CHARGE_INIT",
97*4882a593Smuzhiyun "CHARGE_READOUT",
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun enum ab8500_fg_calibration_state {
101*4882a593Smuzhiyun AB8500_FG_CALIB_INIT,
102*4882a593Smuzhiyun AB8500_FG_CALIB_WAIT,
103*4882a593Smuzhiyun AB8500_FG_CALIB_END,
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun struct ab8500_fg_avg_cap {
107*4882a593Smuzhiyun int avg;
108*4882a593Smuzhiyun int samples[NBR_AVG_SAMPLES];
109*4882a593Smuzhiyun time64_t time_stamps[NBR_AVG_SAMPLES];
110*4882a593Smuzhiyun int pos;
111*4882a593Smuzhiyun int nbr_samples;
112*4882a593Smuzhiyun int sum;
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun struct ab8500_fg_cap_scaling {
116*4882a593Smuzhiyun bool enable;
117*4882a593Smuzhiyun int cap_to_scale[2];
118*4882a593Smuzhiyun int disable_cap_level;
119*4882a593Smuzhiyun int scaled_cap;
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun struct ab8500_fg_battery_capacity {
123*4882a593Smuzhiyun int max_mah_design;
124*4882a593Smuzhiyun int max_mah;
125*4882a593Smuzhiyun int mah;
126*4882a593Smuzhiyun int permille;
127*4882a593Smuzhiyun int level;
128*4882a593Smuzhiyun int prev_mah;
129*4882a593Smuzhiyun int prev_percent;
130*4882a593Smuzhiyun int prev_level;
131*4882a593Smuzhiyun int user_mah;
132*4882a593Smuzhiyun struct ab8500_fg_cap_scaling cap_scale;
133*4882a593Smuzhiyun };
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun struct ab8500_fg_flags {
136*4882a593Smuzhiyun bool fg_enabled;
137*4882a593Smuzhiyun bool conv_done;
138*4882a593Smuzhiyun bool charging;
139*4882a593Smuzhiyun bool fully_charged;
140*4882a593Smuzhiyun bool force_full;
141*4882a593Smuzhiyun bool low_bat_delay;
142*4882a593Smuzhiyun bool low_bat;
143*4882a593Smuzhiyun bool bat_ovv;
144*4882a593Smuzhiyun bool batt_unknown;
145*4882a593Smuzhiyun bool calibrate;
146*4882a593Smuzhiyun bool user_cap;
147*4882a593Smuzhiyun bool batt_id_received;
148*4882a593Smuzhiyun };
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun struct inst_curr_result_list {
151*4882a593Smuzhiyun struct list_head list;
152*4882a593Smuzhiyun int *result;
153*4882a593Smuzhiyun };
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /**
156*4882a593Smuzhiyun * struct ab8500_fg - ab8500 FG device information
157*4882a593Smuzhiyun * @dev: Pointer to the structure device
158*4882a593Smuzhiyun * @node: a list of AB8500 FGs, hence prepared for reentrance
159*4882a593Smuzhiyun * @irq holds the CCEOC interrupt number
160*4882a593Smuzhiyun * @vbat: Battery voltage in mV
161*4882a593Smuzhiyun * @vbat_nom: Nominal battery voltage in mV
162*4882a593Smuzhiyun * @inst_curr: Instantenous battery current in mA
163*4882a593Smuzhiyun * @avg_curr: Average battery current in mA
164*4882a593Smuzhiyun * @bat_temp battery temperature
165*4882a593Smuzhiyun * @fg_samples: Number of samples used in the FG accumulation
166*4882a593Smuzhiyun * @accu_charge: Accumulated charge from the last conversion
167*4882a593Smuzhiyun * @recovery_cnt: Counter for recovery mode
168*4882a593Smuzhiyun * @high_curr_cnt: Counter for high current mode
169*4882a593Smuzhiyun * @init_cnt: Counter for init mode
170*4882a593Smuzhiyun * @low_bat_cnt Counter for number of consecutive low battery measures
171*4882a593Smuzhiyun * @nbr_cceoc_irq_cnt Counter for number of CCEOC irqs received since enabled
172*4882a593Smuzhiyun * @recovery_needed: Indicate if recovery is needed
173*4882a593Smuzhiyun * @high_curr_mode: Indicate if we're in high current mode
174*4882a593Smuzhiyun * @init_capacity: Indicate if initial capacity measuring should be done
175*4882a593Smuzhiyun * @turn_off_fg: True if fg was off before current measurement
176*4882a593Smuzhiyun * @calib_state State during offset calibration
177*4882a593Smuzhiyun * @discharge_state: Current discharge state
178*4882a593Smuzhiyun * @charge_state: Current charge state
179*4882a593Smuzhiyun * @ab8500_fg_started Completion struct used for the instant current start
180*4882a593Smuzhiyun * @ab8500_fg_complete Completion struct used for the instant current reading
181*4882a593Smuzhiyun * @flags: Structure for information about events triggered
182*4882a593Smuzhiyun * @bat_cap: Structure for battery capacity specific parameters
183*4882a593Smuzhiyun * @avg_cap: Average capacity filter
184*4882a593Smuzhiyun * @parent: Pointer to the struct ab8500
185*4882a593Smuzhiyun * @main_bat_v: ADC channel for the main battery voltage
186*4882a593Smuzhiyun * @bm: Platform specific battery management information
187*4882a593Smuzhiyun * @fg_psy: Structure that holds the FG specific battery properties
188*4882a593Smuzhiyun * @fg_wq: Work queue for running the FG algorithm
189*4882a593Smuzhiyun * @fg_periodic_work: Work to run the FG algorithm periodically
190*4882a593Smuzhiyun * @fg_low_bat_work: Work to check low bat condition
191*4882a593Smuzhiyun * @fg_reinit_work Work used to reset and reinitialise the FG algorithm
192*4882a593Smuzhiyun * @fg_work: Work to run the FG algorithm instantly
193*4882a593Smuzhiyun * @fg_acc_cur_work: Work to read the FG accumulator
194*4882a593Smuzhiyun * @fg_check_hw_failure_work: Work for checking HW state
195*4882a593Smuzhiyun * @cc_lock: Mutex for locking the CC
196*4882a593Smuzhiyun * @fg_kobject: Structure of type kobject
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun struct ab8500_fg {
199*4882a593Smuzhiyun struct device *dev;
200*4882a593Smuzhiyun struct list_head node;
201*4882a593Smuzhiyun int irq;
202*4882a593Smuzhiyun int vbat;
203*4882a593Smuzhiyun int vbat_nom;
204*4882a593Smuzhiyun int inst_curr;
205*4882a593Smuzhiyun int avg_curr;
206*4882a593Smuzhiyun int bat_temp;
207*4882a593Smuzhiyun int fg_samples;
208*4882a593Smuzhiyun int accu_charge;
209*4882a593Smuzhiyun int recovery_cnt;
210*4882a593Smuzhiyun int high_curr_cnt;
211*4882a593Smuzhiyun int init_cnt;
212*4882a593Smuzhiyun int low_bat_cnt;
213*4882a593Smuzhiyun int nbr_cceoc_irq_cnt;
214*4882a593Smuzhiyun bool recovery_needed;
215*4882a593Smuzhiyun bool high_curr_mode;
216*4882a593Smuzhiyun bool init_capacity;
217*4882a593Smuzhiyun bool turn_off_fg;
218*4882a593Smuzhiyun enum ab8500_fg_calibration_state calib_state;
219*4882a593Smuzhiyun enum ab8500_fg_discharge_state discharge_state;
220*4882a593Smuzhiyun enum ab8500_fg_charge_state charge_state;
221*4882a593Smuzhiyun struct completion ab8500_fg_started;
222*4882a593Smuzhiyun struct completion ab8500_fg_complete;
223*4882a593Smuzhiyun struct ab8500_fg_flags flags;
224*4882a593Smuzhiyun struct ab8500_fg_battery_capacity bat_cap;
225*4882a593Smuzhiyun struct ab8500_fg_avg_cap avg_cap;
226*4882a593Smuzhiyun struct ab8500 *parent;
227*4882a593Smuzhiyun struct iio_channel *main_bat_v;
228*4882a593Smuzhiyun struct abx500_bm_data *bm;
229*4882a593Smuzhiyun struct power_supply *fg_psy;
230*4882a593Smuzhiyun struct workqueue_struct *fg_wq;
231*4882a593Smuzhiyun struct delayed_work fg_periodic_work;
232*4882a593Smuzhiyun struct delayed_work fg_low_bat_work;
233*4882a593Smuzhiyun struct delayed_work fg_reinit_work;
234*4882a593Smuzhiyun struct work_struct fg_work;
235*4882a593Smuzhiyun struct work_struct fg_acc_cur_work;
236*4882a593Smuzhiyun struct delayed_work fg_check_hw_failure_work;
237*4882a593Smuzhiyun struct mutex cc_lock;
238*4882a593Smuzhiyun struct kobject fg_kobject;
239*4882a593Smuzhiyun };
240*4882a593Smuzhiyun static LIST_HEAD(ab8500_fg_list);
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /**
243*4882a593Smuzhiyun * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
244*4882a593Smuzhiyun * (i.e. the first fuel gauge in the instance list)
245*4882a593Smuzhiyun */
ab8500_fg_get(void)246*4882a593Smuzhiyun struct ab8500_fg *ab8500_fg_get(void)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun return list_first_entry_or_null(&ab8500_fg_list, struct ab8500_fg,
249*4882a593Smuzhiyun node);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* Main battery properties */
253*4882a593Smuzhiyun static enum power_supply_property ab8500_fg_props[] = {
254*4882a593Smuzhiyun POWER_SUPPLY_PROP_VOLTAGE_NOW,
255*4882a593Smuzhiyun POWER_SUPPLY_PROP_CURRENT_NOW,
256*4882a593Smuzhiyun POWER_SUPPLY_PROP_CURRENT_AVG,
257*4882a593Smuzhiyun POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
258*4882a593Smuzhiyun POWER_SUPPLY_PROP_ENERGY_FULL,
259*4882a593Smuzhiyun POWER_SUPPLY_PROP_ENERGY_NOW,
260*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
261*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_FULL,
262*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_NOW,
263*4882a593Smuzhiyun POWER_SUPPLY_PROP_CAPACITY,
264*4882a593Smuzhiyun POWER_SUPPLY_PROP_CAPACITY_LEVEL,
265*4882a593Smuzhiyun };
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /*
268*4882a593Smuzhiyun * This array maps the raw hex value to lowbat voltage used by the AB8500
269*4882a593Smuzhiyun * Values taken from the UM0836
270*4882a593Smuzhiyun */
271*4882a593Smuzhiyun static int ab8500_fg_lowbat_voltage_map[] = {
272*4882a593Smuzhiyun 2300 ,
273*4882a593Smuzhiyun 2325 ,
274*4882a593Smuzhiyun 2350 ,
275*4882a593Smuzhiyun 2375 ,
276*4882a593Smuzhiyun 2400 ,
277*4882a593Smuzhiyun 2425 ,
278*4882a593Smuzhiyun 2450 ,
279*4882a593Smuzhiyun 2475 ,
280*4882a593Smuzhiyun 2500 ,
281*4882a593Smuzhiyun 2525 ,
282*4882a593Smuzhiyun 2550 ,
283*4882a593Smuzhiyun 2575 ,
284*4882a593Smuzhiyun 2600 ,
285*4882a593Smuzhiyun 2625 ,
286*4882a593Smuzhiyun 2650 ,
287*4882a593Smuzhiyun 2675 ,
288*4882a593Smuzhiyun 2700 ,
289*4882a593Smuzhiyun 2725 ,
290*4882a593Smuzhiyun 2750 ,
291*4882a593Smuzhiyun 2775 ,
292*4882a593Smuzhiyun 2800 ,
293*4882a593Smuzhiyun 2825 ,
294*4882a593Smuzhiyun 2850 ,
295*4882a593Smuzhiyun 2875 ,
296*4882a593Smuzhiyun 2900 ,
297*4882a593Smuzhiyun 2925 ,
298*4882a593Smuzhiyun 2950 ,
299*4882a593Smuzhiyun 2975 ,
300*4882a593Smuzhiyun 3000 ,
301*4882a593Smuzhiyun 3025 ,
302*4882a593Smuzhiyun 3050 ,
303*4882a593Smuzhiyun 3075 ,
304*4882a593Smuzhiyun 3100 ,
305*4882a593Smuzhiyun 3125 ,
306*4882a593Smuzhiyun 3150 ,
307*4882a593Smuzhiyun 3175 ,
308*4882a593Smuzhiyun 3200 ,
309*4882a593Smuzhiyun 3225 ,
310*4882a593Smuzhiyun 3250 ,
311*4882a593Smuzhiyun 3275 ,
312*4882a593Smuzhiyun 3300 ,
313*4882a593Smuzhiyun 3325 ,
314*4882a593Smuzhiyun 3350 ,
315*4882a593Smuzhiyun 3375 ,
316*4882a593Smuzhiyun 3400 ,
317*4882a593Smuzhiyun 3425 ,
318*4882a593Smuzhiyun 3450 ,
319*4882a593Smuzhiyun 3475 ,
320*4882a593Smuzhiyun 3500 ,
321*4882a593Smuzhiyun 3525 ,
322*4882a593Smuzhiyun 3550 ,
323*4882a593Smuzhiyun 3575 ,
324*4882a593Smuzhiyun 3600 ,
325*4882a593Smuzhiyun 3625 ,
326*4882a593Smuzhiyun 3650 ,
327*4882a593Smuzhiyun 3675 ,
328*4882a593Smuzhiyun 3700 ,
329*4882a593Smuzhiyun 3725 ,
330*4882a593Smuzhiyun 3750 ,
331*4882a593Smuzhiyun 3775 ,
332*4882a593Smuzhiyun 3800 ,
333*4882a593Smuzhiyun 3825 ,
334*4882a593Smuzhiyun 3850 ,
335*4882a593Smuzhiyun 3850 ,
336*4882a593Smuzhiyun };
337*4882a593Smuzhiyun
ab8500_volt_to_regval(int voltage)338*4882a593Smuzhiyun static u8 ab8500_volt_to_regval(int voltage)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun int i;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun if (voltage < ab8500_fg_lowbat_voltage_map[0])
343*4882a593Smuzhiyun return 0;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) {
346*4882a593Smuzhiyun if (voltage < ab8500_fg_lowbat_voltage_map[i])
347*4882a593Smuzhiyun return (u8) i - 1;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun /* If not captured above, return index of last element */
351*4882a593Smuzhiyun return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun /**
355*4882a593Smuzhiyun * ab8500_fg_is_low_curr() - Low or high current mode
356*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
357*4882a593Smuzhiyun * @curr: the current to base or our decision on
358*4882a593Smuzhiyun *
359*4882a593Smuzhiyun * Low current mode if the current consumption is below a certain threshold
360*4882a593Smuzhiyun */
ab8500_fg_is_low_curr(struct ab8500_fg * di,int curr)361*4882a593Smuzhiyun static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun /*
364*4882a593Smuzhiyun * We want to know if we're in low current mode
365*4882a593Smuzhiyun */
366*4882a593Smuzhiyun if (curr > -di->bm->fg_params->high_curr_threshold)
367*4882a593Smuzhiyun return true;
368*4882a593Smuzhiyun else
369*4882a593Smuzhiyun return false;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun /**
373*4882a593Smuzhiyun * ab8500_fg_add_cap_sample() - Add capacity to average filter
374*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
375*4882a593Smuzhiyun * @sample: the capacity in mAh to add to the filter
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * A capacity is added to the filter and a new mean capacity is calculated and
378*4882a593Smuzhiyun * returned
379*4882a593Smuzhiyun */
ab8500_fg_add_cap_sample(struct ab8500_fg * di,int sample)380*4882a593Smuzhiyun static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun time64_t now = ktime_get_boottime_seconds();
383*4882a593Smuzhiyun struct ab8500_fg_avg_cap *avg = &di->avg_cap;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun do {
386*4882a593Smuzhiyun avg->sum += sample - avg->samples[avg->pos];
387*4882a593Smuzhiyun avg->samples[avg->pos] = sample;
388*4882a593Smuzhiyun avg->time_stamps[avg->pos] = now;
389*4882a593Smuzhiyun avg->pos++;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun if (avg->pos == NBR_AVG_SAMPLES)
392*4882a593Smuzhiyun avg->pos = 0;
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun if (avg->nbr_samples < NBR_AVG_SAMPLES)
395*4882a593Smuzhiyun avg->nbr_samples++;
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /*
398*4882a593Smuzhiyun * Check the time stamp for each sample. If too old,
399*4882a593Smuzhiyun * replace with latest sample
400*4882a593Smuzhiyun */
401*4882a593Smuzhiyun } while (now - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]);
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun avg->avg = avg->sum / avg->nbr_samples;
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun return avg->avg;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun /**
409*4882a593Smuzhiyun * ab8500_fg_clear_cap_samples() - Clear average filter
410*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
411*4882a593Smuzhiyun *
412*4882a593Smuzhiyun * The capacity filter is is reset to zero.
413*4882a593Smuzhiyun */
ab8500_fg_clear_cap_samples(struct ab8500_fg * di)414*4882a593Smuzhiyun static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun int i;
417*4882a593Smuzhiyun struct ab8500_fg_avg_cap *avg = &di->avg_cap;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun avg->pos = 0;
420*4882a593Smuzhiyun avg->nbr_samples = 0;
421*4882a593Smuzhiyun avg->sum = 0;
422*4882a593Smuzhiyun avg->avg = 0;
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun for (i = 0; i < NBR_AVG_SAMPLES; i++) {
425*4882a593Smuzhiyun avg->samples[i] = 0;
426*4882a593Smuzhiyun avg->time_stamps[i] = 0;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun /**
431*4882a593Smuzhiyun * ab8500_fg_fill_cap_sample() - Fill average filter
432*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
433*4882a593Smuzhiyun * @sample: the capacity in mAh to fill the filter with
434*4882a593Smuzhiyun *
435*4882a593Smuzhiyun * The capacity filter is filled with a capacity in mAh
436*4882a593Smuzhiyun */
ab8500_fg_fill_cap_sample(struct ab8500_fg * di,int sample)437*4882a593Smuzhiyun static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun int i;
440*4882a593Smuzhiyun time64_t now;
441*4882a593Smuzhiyun struct ab8500_fg_avg_cap *avg = &di->avg_cap;
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun now = ktime_get_boottime_seconds();
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun for (i = 0; i < NBR_AVG_SAMPLES; i++) {
446*4882a593Smuzhiyun avg->samples[i] = sample;
447*4882a593Smuzhiyun avg->time_stamps[i] = now;
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun avg->pos = 0;
451*4882a593Smuzhiyun avg->nbr_samples = NBR_AVG_SAMPLES;
452*4882a593Smuzhiyun avg->sum = sample * NBR_AVG_SAMPLES;
453*4882a593Smuzhiyun avg->avg = sample;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun /**
457*4882a593Smuzhiyun * ab8500_fg_coulomb_counter() - enable coulomb counter
458*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
459*4882a593Smuzhiyun * @enable: enable/disable
460*4882a593Smuzhiyun *
461*4882a593Smuzhiyun * Enable/Disable coulomb counter.
462*4882a593Smuzhiyun * On failure returns negative value.
463*4882a593Smuzhiyun */
ab8500_fg_coulomb_counter(struct ab8500_fg * di,bool enable)464*4882a593Smuzhiyun static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun int ret = 0;
467*4882a593Smuzhiyun mutex_lock(&di->cc_lock);
468*4882a593Smuzhiyun if (enable) {
469*4882a593Smuzhiyun /* To be able to reprogram the number of samples, we have to
470*4882a593Smuzhiyun * first stop the CC and then enable it again */
471*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
472*4882a593Smuzhiyun AB8500_RTC_CC_CONF_REG, 0x00);
473*4882a593Smuzhiyun if (ret)
474*4882a593Smuzhiyun goto cc_err;
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun /* Program the samples */
477*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev,
478*4882a593Smuzhiyun AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
479*4882a593Smuzhiyun di->fg_samples);
480*4882a593Smuzhiyun if (ret)
481*4882a593Smuzhiyun goto cc_err;
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun /* Start the CC */
484*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
485*4882a593Smuzhiyun AB8500_RTC_CC_CONF_REG,
486*4882a593Smuzhiyun (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
487*4882a593Smuzhiyun if (ret)
488*4882a593Smuzhiyun goto cc_err;
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun di->flags.fg_enabled = true;
491*4882a593Smuzhiyun } else {
492*4882a593Smuzhiyun /* Clear any pending read requests */
493*4882a593Smuzhiyun ret = abx500_mask_and_set_register_interruptible(di->dev,
494*4882a593Smuzhiyun AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
495*4882a593Smuzhiyun (RESET_ACCU | READ_REQ), 0);
496*4882a593Smuzhiyun if (ret)
497*4882a593Smuzhiyun goto cc_err;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev,
500*4882a593Smuzhiyun AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0);
501*4882a593Smuzhiyun if (ret)
502*4882a593Smuzhiyun goto cc_err;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun /* Stop the CC */
505*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
506*4882a593Smuzhiyun AB8500_RTC_CC_CONF_REG, 0);
507*4882a593Smuzhiyun if (ret)
508*4882a593Smuzhiyun goto cc_err;
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun di->flags.fg_enabled = false;
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun dev_dbg(di->dev, " CC enabled: %d Samples: %d\n",
514*4882a593Smuzhiyun enable, di->fg_samples);
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun return ret;
519*4882a593Smuzhiyun cc_err:
520*4882a593Smuzhiyun dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__);
521*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
522*4882a593Smuzhiyun return ret;
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun /**
526*4882a593Smuzhiyun * ab8500_fg_inst_curr_start() - start battery instantaneous current
527*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
528*4882a593Smuzhiyun *
529*4882a593Smuzhiyun * Returns 0 or error code
530*4882a593Smuzhiyun * Note: This is part "one" and has to be called before
531*4882a593Smuzhiyun * ab8500_fg_inst_curr_finalize()
532*4882a593Smuzhiyun */
ab8500_fg_inst_curr_start(struct ab8500_fg * di)533*4882a593Smuzhiyun int ab8500_fg_inst_curr_start(struct ab8500_fg *di)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun u8 reg_val;
536*4882a593Smuzhiyun int ret;
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun mutex_lock(&di->cc_lock);
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun di->nbr_cceoc_irq_cnt = 0;
541*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
542*4882a593Smuzhiyun AB8500_RTC_CC_CONF_REG, ®_val);
543*4882a593Smuzhiyun if (ret < 0)
544*4882a593Smuzhiyun goto fail;
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun if (!(reg_val & CC_PWR_UP_ENA)) {
547*4882a593Smuzhiyun dev_dbg(di->dev, "%s Enable FG\n", __func__);
548*4882a593Smuzhiyun di->turn_off_fg = true;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun /* Program the samples */
551*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev,
552*4882a593Smuzhiyun AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
553*4882a593Smuzhiyun SEC_TO_SAMPLE(10));
554*4882a593Smuzhiyun if (ret)
555*4882a593Smuzhiyun goto fail;
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun /* Start the CC */
558*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
559*4882a593Smuzhiyun AB8500_RTC_CC_CONF_REG,
560*4882a593Smuzhiyun (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
561*4882a593Smuzhiyun if (ret)
562*4882a593Smuzhiyun goto fail;
563*4882a593Smuzhiyun } else {
564*4882a593Smuzhiyun di->turn_off_fg = false;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun /* Return and WFI */
568*4882a593Smuzhiyun reinit_completion(&di->ab8500_fg_started);
569*4882a593Smuzhiyun reinit_completion(&di->ab8500_fg_complete);
570*4882a593Smuzhiyun enable_irq(di->irq);
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun /* Note: cc_lock is still locked */
573*4882a593Smuzhiyun return 0;
574*4882a593Smuzhiyun fail:
575*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
576*4882a593Smuzhiyun return ret;
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun /**
580*4882a593Smuzhiyun * ab8500_fg_inst_curr_started() - check if fg conversion has started
581*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
582*4882a593Smuzhiyun *
583*4882a593Smuzhiyun * Returns 1 if conversion started, 0 if still waiting
584*4882a593Smuzhiyun */
ab8500_fg_inst_curr_started(struct ab8500_fg * di)585*4882a593Smuzhiyun int ab8500_fg_inst_curr_started(struct ab8500_fg *di)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun return completion_done(&di->ab8500_fg_started);
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun /**
591*4882a593Smuzhiyun * ab8500_fg_inst_curr_done() - check if fg conversion is done
592*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
593*4882a593Smuzhiyun *
594*4882a593Smuzhiyun * Returns 1 if conversion done, 0 if still waiting
595*4882a593Smuzhiyun */
ab8500_fg_inst_curr_done(struct ab8500_fg * di)596*4882a593Smuzhiyun int ab8500_fg_inst_curr_done(struct ab8500_fg *di)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun return completion_done(&di->ab8500_fg_complete);
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun /**
602*4882a593Smuzhiyun * ab8500_fg_inst_curr_finalize() - battery instantaneous current
603*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
604*4882a593Smuzhiyun * @res: battery instantenous current(on success)
605*4882a593Smuzhiyun *
606*4882a593Smuzhiyun * Returns 0 or an error code
607*4882a593Smuzhiyun * Note: This is part "two" and has to be called at earliest 250 ms
608*4882a593Smuzhiyun * after ab8500_fg_inst_curr_start()
609*4882a593Smuzhiyun */
ab8500_fg_inst_curr_finalize(struct ab8500_fg * di,int * res)610*4882a593Smuzhiyun int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun u8 low, high;
613*4882a593Smuzhiyun int val;
614*4882a593Smuzhiyun int ret;
615*4882a593Smuzhiyun unsigned long timeout;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun if (!completion_done(&di->ab8500_fg_complete)) {
618*4882a593Smuzhiyun timeout = wait_for_completion_timeout(
619*4882a593Smuzhiyun &di->ab8500_fg_complete,
620*4882a593Smuzhiyun INS_CURR_TIMEOUT);
621*4882a593Smuzhiyun dev_dbg(di->dev, "Finalize time: %d ms\n",
622*4882a593Smuzhiyun jiffies_to_msecs(INS_CURR_TIMEOUT - timeout));
623*4882a593Smuzhiyun if (!timeout) {
624*4882a593Smuzhiyun ret = -ETIME;
625*4882a593Smuzhiyun disable_irq(di->irq);
626*4882a593Smuzhiyun di->nbr_cceoc_irq_cnt = 0;
627*4882a593Smuzhiyun dev_err(di->dev, "completion timed out [%d]\n",
628*4882a593Smuzhiyun __LINE__);
629*4882a593Smuzhiyun goto fail;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun disable_irq(di->irq);
634*4882a593Smuzhiyun di->nbr_cceoc_irq_cnt = 0;
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun ret = abx500_mask_and_set_register_interruptible(di->dev,
637*4882a593Smuzhiyun AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
638*4882a593Smuzhiyun READ_REQ, READ_REQ);
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun /* 100uS between read request and read is needed */
641*4882a593Smuzhiyun usleep_range(100, 100);
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun /* Read CC Sample conversion value Low and high */
644*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
645*4882a593Smuzhiyun AB8500_GASG_CC_SMPL_CNVL_REG, &low);
646*4882a593Smuzhiyun if (ret < 0)
647*4882a593Smuzhiyun goto fail;
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
650*4882a593Smuzhiyun AB8500_GASG_CC_SMPL_CNVH_REG, &high);
651*4882a593Smuzhiyun if (ret < 0)
652*4882a593Smuzhiyun goto fail;
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun /*
655*4882a593Smuzhiyun * negative value for Discharging
656*4882a593Smuzhiyun * convert 2's complement into decimal
657*4882a593Smuzhiyun */
658*4882a593Smuzhiyun if (high & 0x10)
659*4882a593Smuzhiyun val = (low | (high << 8) | 0xFFFFE000);
660*4882a593Smuzhiyun else
661*4882a593Smuzhiyun val = (low | (high << 8));
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun /*
664*4882a593Smuzhiyun * Convert to unit value in mA
665*4882a593Smuzhiyun * Full scale input voltage is
666*4882a593Smuzhiyun * 63.160mV => LSB = 63.160mV/(4096*res) = 1.542mA
667*4882a593Smuzhiyun * Given a 250ms conversion cycle time the LSB corresponds
668*4882a593Smuzhiyun * to 107.1 nAh. Convert to current by dividing by the conversion
669*4882a593Smuzhiyun * time in hours (250ms = 1 / (3600 * 4)h)
670*4882a593Smuzhiyun * 107.1nAh assumes 10mOhm, but fg_res is in 0.1mOhm
671*4882a593Smuzhiyun */
672*4882a593Smuzhiyun val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) /
673*4882a593Smuzhiyun (1000 * di->bm->fg_res);
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun if (di->turn_off_fg) {
676*4882a593Smuzhiyun dev_dbg(di->dev, "%s Disable FG\n", __func__);
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun /* Clear any pending read requests */
679*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev,
680*4882a593Smuzhiyun AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0);
681*4882a593Smuzhiyun if (ret)
682*4882a593Smuzhiyun goto fail;
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun /* Stop the CC */
685*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
686*4882a593Smuzhiyun AB8500_RTC_CC_CONF_REG, 0);
687*4882a593Smuzhiyun if (ret)
688*4882a593Smuzhiyun goto fail;
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
691*4882a593Smuzhiyun (*res) = val;
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun return 0;
694*4882a593Smuzhiyun fail:
695*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
696*4882a593Smuzhiyun return ret;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun /**
700*4882a593Smuzhiyun * ab8500_fg_inst_curr_blocking() - battery instantaneous current
701*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
702*4882a593Smuzhiyun * @res: battery instantenous current(on success)
703*4882a593Smuzhiyun *
704*4882a593Smuzhiyun * Returns 0 else error code
705*4882a593Smuzhiyun */
ab8500_fg_inst_curr_blocking(struct ab8500_fg * di)706*4882a593Smuzhiyun int ab8500_fg_inst_curr_blocking(struct ab8500_fg *di)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun int ret;
709*4882a593Smuzhiyun unsigned long timeout;
710*4882a593Smuzhiyun int res = 0;
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun ret = ab8500_fg_inst_curr_start(di);
713*4882a593Smuzhiyun if (ret) {
714*4882a593Smuzhiyun dev_err(di->dev, "Failed to initialize fg_inst\n");
715*4882a593Smuzhiyun return 0;
716*4882a593Smuzhiyun }
717*4882a593Smuzhiyun
718*4882a593Smuzhiyun /* Wait for CC to actually start */
719*4882a593Smuzhiyun if (!completion_done(&di->ab8500_fg_started)) {
720*4882a593Smuzhiyun timeout = wait_for_completion_timeout(
721*4882a593Smuzhiyun &di->ab8500_fg_started,
722*4882a593Smuzhiyun INS_CURR_TIMEOUT);
723*4882a593Smuzhiyun dev_dbg(di->dev, "Start time: %d ms\n",
724*4882a593Smuzhiyun jiffies_to_msecs(INS_CURR_TIMEOUT - timeout));
725*4882a593Smuzhiyun if (!timeout) {
726*4882a593Smuzhiyun ret = -ETIME;
727*4882a593Smuzhiyun dev_err(di->dev, "completion timed out [%d]\n",
728*4882a593Smuzhiyun __LINE__);
729*4882a593Smuzhiyun goto fail;
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun
733*4882a593Smuzhiyun ret = ab8500_fg_inst_curr_finalize(di, &res);
734*4882a593Smuzhiyun if (ret) {
735*4882a593Smuzhiyun dev_err(di->dev, "Failed to finalize fg_inst\n");
736*4882a593Smuzhiyun return 0;
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun dev_dbg(di->dev, "%s instant current: %d", __func__, res);
740*4882a593Smuzhiyun return res;
741*4882a593Smuzhiyun fail:
742*4882a593Smuzhiyun disable_irq(di->irq);
743*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
744*4882a593Smuzhiyun return ret;
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun /**
748*4882a593Smuzhiyun * ab8500_fg_acc_cur_work() - average battery current
749*4882a593Smuzhiyun * @work: pointer to the work_struct structure
750*4882a593Smuzhiyun *
751*4882a593Smuzhiyun * Updated the average battery current obtained from the
752*4882a593Smuzhiyun * coulomb counter.
753*4882a593Smuzhiyun */
ab8500_fg_acc_cur_work(struct work_struct * work)754*4882a593Smuzhiyun static void ab8500_fg_acc_cur_work(struct work_struct *work)
755*4882a593Smuzhiyun {
756*4882a593Smuzhiyun int val;
757*4882a593Smuzhiyun int ret;
758*4882a593Smuzhiyun u8 low, med, high;
759*4882a593Smuzhiyun
760*4882a593Smuzhiyun struct ab8500_fg *di = container_of(work,
761*4882a593Smuzhiyun struct ab8500_fg, fg_acc_cur_work);
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun mutex_lock(&di->cc_lock);
764*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE,
765*4882a593Smuzhiyun AB8500_GASG_CC_NCOV_ACCU_CTRL, RD_NCONV_ACCU_REQ);
766*4882a593Smuzhiyun if (ret)
767*4882a593Smuzhiyun goto exit;
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
770*4882a593Smuzhiyun AB8500_GASG_CC_NCOV_ACCU_LOW, &low);
771*4882a593Smuzhiyun if (ret < 0)
772*4882a593Smuzhiyun goto exit;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
775*4882a593Smuzhiyun AB8500_GASG_CC_NCOV_ACCU_MED, &med);
776*4882a593Smuzhiyun if (ret < 0)
777*4882a593Smuzhiyun goto exit;
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
780*4882a593Smuzhiyun AB8500_GASG_CC_NCOV_ACCU_HIGH, &high);
781*4882a593Smuzhiyun if (ret < 0)
782*4882a593Smuzhiyun goto exit;
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun /* Check for sign bit in case of negative value, 2's complement */
785*4882a593Smuzhiyun if (high & 0x10)
786*4882a593Smuzhiyun val = (low | (med << 8) | (high << 16) | 0xFFE00000);
787*4882a593Smuzhiyun else
788*4882a593Smuzhiyun val = (low | (med << 8) | (high << 16));
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun /*
791*4882a593Smuzhiyun * Convert to uAh
792*4882a593Smuzhiyun * Given a 250ms conversion cycle time the LSB corresponds
793*4882a593Smuzhiyun * to 112.9 nAh.
794*4882a593Smuzhiyun * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
795*4882a593Smuzhiyun */
796*4882a593Smuzhiyun di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) /
797*4882a593Smuzhiyun (100 * di->bm->fg_res);
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun /*
800*4882a593Smuzhiyun * Convert to unit value in mA
801*4882a593Smuzhiyun * by dividing by the conversion
802*4882a593Smuzhiyun * time in hours (= samples / (3600 * 4)h)
803*4882a593Smuzhiyun * and multiply with 1000
804*4882a593Smuzhiyun */
805*4882a593Smuzhiyun di->avg_curr = (val * QLSB_NANO_AMP_HOURS_X10 * 36) /
806*4882a593Smuzhiyun (1000 * di->bm->fg_res * (di->fg_samples / 4));
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun di->flags.conv_done = true;
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun queue_work(di->fg_wq, &di->fg_work);
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun dev_dbg(di->dev, "fg_res: %d, fg_samples: %d, gasg: %d, accu_charge: %d \n",
815*4882a593Smuzhiyun di->bm->fg_res, di->fg_samples, val, di->accu_charge);
816*4882a593Smuzhiyun return;
817*4882a593Smuzhiyun exit:
818*4882a593Smuzhiyun dev_err(di->dev,
819*4882a593Smuzhiyun "Failed to read or write gas gauge registers\n");
820*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
821*4882a593Smuzhiyun queue_work(di->fg_wq, &di->fg_work);
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun /**
825*4882a593Smuzhiyun * ab8500_fg_bat_voltage() - get battery voltage
826*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
827*4882a593Smuzhiyun *
828*4882a593Smuzhiyun * Returns battery voltage(on success) else error code
829*4882a593Smuzhiyun */
ab8500_fg_bat_voltage(struct ab8500_fg * di)830*4882a593Smuzhiyun static int ab8500_fg_bat_voltage(struct ab8500_fg *di)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun int vbat, ret;
833*4882a593Smuzhiyun static int prev;
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun ret = iio_read_channel_processed(di->main_bat_v, &vbat);
836*4882a593Smuzhiyun if (ret < 0) {
837*4882a593Smuzhiyun dev_err(di->dev,
838*4882a593Smuzhiyun "%s ADC conversion failed, using previous value\n",
839*4882a593Smuzhiyun __func__);
840*4882a593Smuzhiyun return prev;
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun prev = vbat;
844*4882a593Smuzhiyun return vbat;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun /**
848*4882a593Smuzhiyun * ab8500_fg_volt_to_capacity() - Voltage based capacity
849*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
850*4882a593Smuzhiyun * @voltage: The voltage to convert to a capacity
851*4882a593Smuzhiyun *
852*4882a593Smuzhiyun * Returns battery capacity in per mille based on voltage
853*4882a593Smuzhiyun */
ab8500_fg_volt_to_capacity(struct ab8500_fg * di,int voltage)854*4882a593Smuzhiyun static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage)
855*4882a593Smuzhiyun {
856*4882a593Smuzhiyun int i, tbl_size;
857*4882a593Smuzhiyun const struct abx500_v_to_cap *tbl;
858*4882a593Smuzhiyun int cap = 0;
859*4882a593Smuzhiyun
860*4882a593Smuzhiyun tbl = di->bm->bat_type[di->bm->batt_id].v_to_cap_tbl,
861*4882a593Smuzhiyun tbl_size = di->bm->bat_type[di->bm->batt_id].n_v_cap_tbl_elements;
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun for (i = 0; i < tbl_size; ++i) {
864*4882a593Smuzhiyun if (voltage > tbl[i].voltage)
865*4882a593Smuzhiyun break;
866*4882a593Smuzhiyun }
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun if ((i > 0) && (i < tbl_size)) {
869*4882a593Smuzhiyun cap = interpolate(voltage,
870*4882a593Smuzhiyun tbl[i].voltage,
871*4882a593Smuzhiyun tbl[i].capacity * 10,
872*4882a593Smuzhiyun tbl[i-1].voltage,
873*4882a593Smuzhiyun tbl[i-1].capacity * 10);
874*4882a593Smuzhiyun } else if (i == 0) {
875*4882a593Smuzhiyun cap = 1000;
876*4882a593Smuzhiyun } else {
877*4882a593Smuzhiyun cap = 0;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun dev_dbg(di->dev, "%s Vbat: %d, Cap: %d per mille",
881*4882a593Smuzhiyun __func__, voltage, cap);
882*4882a593Smuzhiyun
883*4882a593Smuzhiyun return cap;
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun /**
887*4882a593Smuzhiyun * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
888*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
889*4882a593Smuzhiyun *
890*4882a593Smuzhiyun * Returns battery capacity based on battery voltage that is not compensated
891*4882a593Smuzhiyun * for the voltage drop due to the load
892*4882a593Smuzhiyun */
ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg * di)893*4882a593Smuzhiyun static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di)
894*4882a593Smuzhiyun {
895*4882a593Smuzhiyun di->vbat = ab8500_fg_bat_voltage(di);
896*4882a593Smuzhiyun return ab8500_fg_volt_to_capacity(di, di->vbat);
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun /**
900*4882a593Smuzhiyun * ab8500_fg_battery_resistance() - Returns the battery inner resistance
901*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
902*4882a593Smuzhiyun *
903*4882a593Smuzhiyun * Returns battery inner resistance added with the fuel gauge resistor value
904*4882a593Smuzhiyun * to get the total resistance in the whole link from gnd to bat+ node.
905*4882a593Smuzhiyun */
ab8500_fg_battery_resistance(struct ab8500_fg * di)906*4882a593Smuzhiyun static int ab8500_fg_battery_resistance(struct ab8500_fg *di)
907*4882a593Smuzhiyun {
908*4882a593Smuzhiyun int i, tbl_size;
909*4882a593Smuzhiyun const struct batres_vs_temp *tbl;
910*4882a593Smuzhiyun int resist = 0;
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun tbl = di->bm->bat_type[di->bm->batt_id].batres_tbl;
913*4882a593Smuzhiyun tbl_size = di->bm->bat_type[di->bm->batt_id].n_batres_tbl_elements;
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun for (i = 0; i < tbl_size; ++i) {
916*4882a593Smuzhiyun if (di->bat_temp / 10 > tbl[i].temp)
917*4882a593Smuzhiyun break;
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun if ((i > 0) && (i < tbl_size)) {
921*4882a593Smuzhiyun resist = interpolate(di->bat_temp / 10,
922*4882a593Smuzhiyun tbl[i].temp,
923*4882a593Smuzhiyun tbl[i].resist,
924*4882a593Smuzhiyun tbl[i-1].temp,
925*4882a593Smuzhiyun tbl[i-1].resist);
926*4882a593Smuzhiyun } else if (i == 0) {
927*4882a593Smuzhiyun resist = tbl[0].resist;
928*4882a593Smuzhiyun } else {
929*4882a593Smuzhiyun resist = tbl[tbl_size - 1].resist;
930*4882a593Smuzhiyun }
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d"
933*4882a593Smuzhiyun " fg resistance %d, total: %d (mOhm)\n",
934*4882a593Smuzhiyun __func__, di->bat_temp, resist, di->bm->fg_res / 10,
935*4882a593Smuzhiyun (di->bm->fg_res / 10) + resist);
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun /* fg_res variable is in 0.1mOhm */
938*4882a593Smuzhiyun resist += di->bm->fg_res / 10;
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun return resist;
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun /**
944*4882a593Smuzhiyun * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
945*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
946*4882a593Smuzhiyun *
947*4882a593Smuzhiyun * Returns battery capacity based on battery voltage that is load compensated
948*4882a593Smuzhiyun * for the voltage drop
949*4882a593Smuzhiyun */
ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg * di)950*4882a593Smuzhiyun static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di)
951*4882a593Smuzhiyun {
952*4882a593Smuzhiyun int vbat_comp, res;
953*4882a593Smuzhiyun int i = 0;
954*4882a593Smuzhiyun int vbat = 0;
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun ab8500_fg_inst_curr_start(di);
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun do {
959*4882a593Smuzhiyun vbat += ab8500_fg_bat_voltage(di);
960*4882a593Smuzhiyun i++;
961*4882a593Smuzhiyun usleep_range(5000, 6000);
962*4882a593Smuzhiyun } while (!ab8500_fg_inst_curr_done(di));
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun ab8500_fg_inst_curr_finalize(di, &di->inst_curr);
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun di->vbat = vbat / i;
967*4882a593Smuzhiyun res = ab8500_fg_battery_resistance(di);
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun /* Use Ohms law to get the load compensated voltage */
970*4882a593Smuzhiyun vbat_comp = di->vbat - (di->inst_curr * res) / 1000;
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun dev_dbg(di->dev, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, "
973*4882a593Smuzhiyun "R: %dmOhm, Current: %dmA Vbat Samples: %d\n",
974*4882a593Smuzhiyun __func__, di->vbat, vbat_comp, res, di->inst_curr, i);
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun return ab8500_fg_volt_to_capacity(di, vbat_comp);
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun /**
980*4882a593Smuzhiyun * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
981*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
982*4882a593Smuzhiyun * @cap_mah: capacity in mAh
983*4882a593Smuzhiyun *
984*4882a593Smuzhiyun * Converts capacity in mAh to capacity in permille
985*4882a593Smuzhiyun */
ab8500_fg_convert_mah_to_permille(struct ab8500_fg * di,int cap_mah)986*4882a593Smuzhiyun static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun return (cap_mah * 1000) / di->bat_cap.max_mah_design;
989*4882a593Smuzhiyun }
990*4882a593Smuzhiyun
991*4882a593Smuzhiyun /**
992*4882a593Smuzhiyun * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
993*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
994*4882a593Smuzhiyun * @cap_pm: capacity in permille
995*4882a593Smuzhiyun *
996*4882a593Smuzhiyun * Converts capacity in permille to capacity in mAh
997*4882a593Smuzhiyun */
ab8500_fg_convert_permille_to_mah(struct ab8500_fg * di,int cap_pm)998*4882a593Smuzhiyun static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm)
999*4882a593Smuzhiyun {
1000*4882a593Smuzhiyun return cap_pm * di->bat_cap.max_mah_design / 1000;
1001*4882a593Smuzhiyun }
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun /**
1004*4882a593Smuzhiyun * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
1005*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1006*4882a593Smuzhiyun * @cap_mah: capacity in mAh
1007*4882a593Smuzhiyun *
1008*4882a593Smuzhiyun * Converts capacity in mAh to capacity in uWh
1009*4882a593Smuzhiyun */
ab8500_fg_convert_mah_to_uwh(struct ab8500_fg * di,int cap_mah)1010*4882a593Smuzhiyun static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun u64 div_res;
1013*4882a593Smuzhiyun u32 div_rem;
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun div_res = ((u64) cap_mah) * ((u64) di->vbat_nom);
1016*4882a593Smuzhiyun div_rem = do_div(div_res, 1000);
1017*4882a593Smuzhiyun
1018*4882a593Smuzhiyun /* Make sure to round upwards if necessary */
1019*4882a593Smuzhiyun if (div_rem >= 1000 / 2)
1020*4882a593Smuzhiyun div_res++;
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun return (int) div_res;
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun /**
1026*4882a593Smuzhiyun * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
1027*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1028*4882a593Smuzhiyun *
1029*4882a593Smuzhiyun * Return the capacity in mAh based on previous calculated capcity and the FG
1030*4882a593Smuzhiyun * accumulator register value. The filter is filled with this capacity
1031*4882a593Smuzhiyun */
ab8500_fg_calc_cap_charging(struct ab8500_fg * di)1032*4882a593Smuzhiyun static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di)
1033*4882a593Smuzhiyun {
1034*4882a593Smuzhiyun dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1035*4882a593Smuzhiyun __func__,
1036*4882a593Smuzhiyun di->bat_cap.mah,
1037*4882a593Smuzhiyun di->accu_charge);
1038*4882a593Smuzhiyun
1039*4882a593Smuzhiyun /* Capacity should not be less than 0 */
1040*4882a593Smuzhiyun if (di->bat_cap.mah + di->accu_charge > 0)
1041*4882a593Smuzhiyun di->bat_cap.mah += di->accu_charge;
1042*4882a593Smuzhiyun else
1043*4882a593Smuzhiyun di->bat_cap.mah = 0;
1044*4882a593Smuzhiyun /*
1045*4882a593Smuzhiyun * We force capacity to 100% once when the algorithm
1046*4882a593Smuzhiyun * reports that it's full.
1047*4882a593Smuzhiyun */
1048*4882a593Smuzhiyun if (di->bat_cap.mah >= di->bat_cap.max_mah_design ||
1049*4882a593Smuzhiyun di->flags.force_full) {
1050*4882a593Smuzhiyun di->bat_cap.mah = di->bat_cap.max_mah_design;
1051*4882a593Smuzhiyun }
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1054*4882a593Smuzhiyun di->bat_cap.permille =
1055*4882a593Smuzhiyun ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun /* We need to update battery voltage and inst current when charging */
1058*4882a593Smuzhiyun di->vbat = ab8500_fg_bat_voltage(di);
1059*4882a593Smuzhiyun di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun return di->bat_cap.mah;
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun /**
1065*4882a593Smuzhiyun * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage
1066*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1067*4882a593Smuzhiyun * @comp: if voltage should be load compensated before capacity calc
1068*4882a593Smuzhiyun *
1069*4882a593Smuzhiyun * Return the capacity in mAh based on the battery voltage. The voltage can
1070*4882a593Smuzhiyun * either be load compensated or not. This value is added to the filter and a
1071*4882a593Smuzhiyun * new mean value is calculated and returned.
1072*4882a593Smuzhiyun */
ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg * di,bool comp)1073*4882a593Smuzhiyun static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di, bool comp)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun int permille, mah;
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun if (comp)
1078*4882a593Smuzhiyun permille = ab8500_fg_load_comp_volt_to_capacity(di);
1079*4882a593Smuzhiyun else
1080*4882a593Smuzhiyun permille = ab8500_fg_uncomp_volt_to_capacity(di);
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun mah = ab8500_fg_convert_permille_to_mah(di, permille);
1083*4882a593Smuzhiyun
1084*4882a593Smuzhiyun di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah);
1085*4882a593Smuzhiyun di->bat_cap.permille =
1086*4882a593Smuzhiyun ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1087*4882a593Smuzhiyun
1088*4882a593Smuzhiyun return di->bat_cap.mah;
1089*4882a593Smuzhiyun }
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun /**
1092*4882a593Smuzhiyun * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG
1093*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1094*4882a593Smuzhiyun *
1095*4882a593Smuzhiyun * Return the capacity in mAh based on previous calculated capcity and the FG
1096*4882a593Smuzhiyun * accumulator register value. This value is added to the filter and a
1097*4882a593Smuzhiyun * new mean value is calculated and returned.
1098*4882a593Smuzhiyun */
ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg * di)1099*4882a593Smuzhiyun static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di)
1100*4882a593Smuzhiyun {
1101*4882a593Smuzhiyun int permille_volt, permille;
1102*4882a593Smuzhiyun
1103*4882a593Smuzhiyun dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1104*4882a593Smuzhiyun __func__,
1105*4882a593Smuzhiyun di->bat_cap.mah,
1106*4882a593Smuzhiyun di->accu_charge);
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun /* Capacity should not be less than 0 */
1109*4882a593Smuzhiyun if (di->bat_cap.mah + di->accu_charge > 0)
1110*4882a593Smuzhiyun di->bat_cap.mah += di->accu_charge;
1111*4882a593Smuzhiyun else
1112*4882a593Smuzhiyun di->bat_cap.mah = 0;
1113*4882a593Smuzhiyun
1114*4882a593Smuzhiyun if (di->bat_cap.mah >= di->bat_cap.max_mah_design)
1115*4882a593Smuzhiyun di->bat_cap.mah = di->bat_cap.max_mah_design;
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun /*
1118*4882a593Smuzhiyun * Check against voltage based capacity. It can not be lower
1119*4882a593Smuzhiyun * than what the uncompensated voltage says
1120*4882a593Smuzhiyun */
1121*4882a593Smuzhiyun permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1122*4882a593Smuzhiyun permille_volt = ab8500_fg_uncomp_volt_to_capacity(di);
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun if (permille < permille_volt) {
1125*4882a593Smuzhiyun di->bat_cap.permille = permille_volt;
1126*4882a593Smuzhiyun di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di,
1127*4882a593Smuzhiyun di->bat_cap.permille);
1128*4882a593Smuzhiyun
1129*4882a593Smuzhiyun dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n",
1130*4882a593Smuzhiyun __func__,
1131*4882a593Smuzhiyun permille,
1132*4882a593Smuzhiyun permille_volt);
1133*4882a593Smuzhiyun
1134*4882a593Smuzhiyun ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1135*4882a593Smuzhiyun } else {
1136*4882a593Smuzhiyun ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1137*4882a593Smuzhiyun di->bat_cap.permille =
1138*4882a593Smuzhiyun ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1139*4882a593Smuzhiyun }
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun return di->bat_cap.mah;
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun /**
1145*4882a593Smuzhiyun * ab8500_fg_capacity_level() - Get the battery capacity level
1146*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1147*4882a593Smuzhiyun *
1148*4882a593Smuzhiyun * Get the battery capacity level based on the capacity in percent
1149*4882a593Smuzhiyun */
ab8500_fg_capacity_level(struct ab8500_fg * di)1150*4882a593Smuzhiyun static int ab8500_fg_capacity_level(struct ab8500_fg *di)
1151*4882a593Smuzhiyun {
1152*4882a593Smuzhiyun int ret, percent;
1153*4882a593Smuzhiyun
1154*4882a593Smuzhiyun percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10);
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun if (percent <= di->bm->cap_levels->critical ||
1157*4882a593Smuzhiyun di->flags.low_bat)
1158*4882a593Smuzhiyun ret = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1159*4882a593Smuzhiyun else if (percent <= di->bm->cap_levels->low)
1160*4882a593Smuzhiyun ret = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1161*4882a593Smuzhiyun else if (percent <= di->bm->cap_levels->normal)
1162*4882a593Smuzhiyun ret = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1163*4882a593Smuzhiyun else if (percent <= di->bm->cap_levels->high)
1164*4882a593Smuzhiyun ret = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
1165*4882a593Smuzhiyun else
1166*4882a593Smuzhiyun ret = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1167*4882a593Smuzhiyun
1168*4882a593Smuzhiyun return ret;
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun /**
1172*4882a593Smuzhiyun * ab8500_fg_calculate_scaled_capacity() - Capacity scaling
1173*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1174*4882a593Smuzhiyun *
1175*4882a593Smuzhiyun * Calculates the capacity to be shown to upper layers. Scales the capacity
1176*4882a593Smuzhiyun * to have 100% as a reference from the actual capacity upon removal of charger
1177*4882a593Smuzhiyun * when charging is in maintenance mode.
1178*4882a593Smuzhiyun */
ab8500_fg_calculate_scaled_capacity(struct ab8500_fg * di)1179*4882a593Smuzhiyun static int ab8500_fg_calculate_scaled_capacity(struct ab8500_fg *di)
1180*4882a593Smuzhiyun {
1181*4882a593Smuzhiyun struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale;
1182*4882a593Smuzhiyun int capacity = di->bat_cap.prev_percent;
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun if (!cs->enable)
1185*4882a593Smuzhiyun return capacity;
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun /*
1188*4882a593Smuzhiyun * As long as we are in fully charge mode scale the capacity
1189*4882a593Smuzhiyun * to show 100%.
1190*4882a593Smuzhiyun */
1191*4882a593Smuzhiyun if (di->flags.fully_charged) {
1192*4882a593Smuzhiyun cs->cap_to_scale[0] = 100;
1193*4882a593Smuzhiyun cs->cap_to_scale[1] =
1194*4882a593Smuzhiyun max(capacity, di->bm->fg_params->maint_thres);
1195*4882a593Smuzhiyun dev_dbg(di->dev, "Scale cap with %d/%d\n",
1196*4882a593Smuzhiyun cs->cap_to_scale[0], cs->cap_to_scale[1]);
1197*4882a593Smuzhiyun }
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun /* Calculates the scaled capacity. */
1200*4882a593Smuzhiyun if ((cs->cap_to_scale[0] != cs->cap_to_scale[1])
1201*4882a593Smuzhiyun && (cs->cap_to_scale[1] > 0))
1202*4882a593Smuzhiyun capacity = min(100,
1203*4882a593Smuzhiyun DIV_ROUND_CLOSEST(di->bat_cap.prev_percent *
1204*4882a593Smuzhiyun cs->cap_to_scale[0],
1205*4882a593Smuzhiyun cs->cap_to_scale[1]));
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun if (di->flags.charging) {
1208*4882a593Smuzhiyun if (capacity < cs->disable_cap_level) {
1209*4882a593Smuzhiyun cs->disable_cap_level = capacity;
1210*4882a593Smuzhiyun dev_dbg(di->dev, "Cap to stop scale lowered %d%%\n",
1211*4882a593Smuzhiyun cs->disable_cap_level);
1212*4882a593Smuzhiyun } else if (!di->flags.fully_charged) {
1213*4882a593Smuzhiyun if (di->bat_cap.prev_percent >=
1214*4882a593Smuzhiyun cs->disable_cap_level) {
1215*4882a593Smuzhiyun dev_dbg(di->dev, "Disabling scaled capacity\n");
1216*4882a593Smuzhiyun cs->enable = false;
1217*4882a593Smuzhiyun capacity = di->bat_cap.prev_percent;
1218*4882a593Smuzhiyun } else {
1219*4882a593Smuzhiyun dev_dbg(di->dev,
1220*4882a593Smuzhiyun "Waiting in cap to level %d%%\n",
1221*4882a593Smuzhiyun cs->disable_cap_level);
1222*4882a593Smuzhiyun capacity = cs->disable_cap_level;
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun }
1225*4882a593Smuzhiyun }
1226*4882a593Smuzhiyun
1227*4882a593Smuzhiyun return capacity;
1228*4882a593Smuzhiyun }
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun /**
1231*4882a593Smuzhiyun * ab8500_fg_update_cap_scalers() - Capacity scaling
1232*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1233*4882a593Smuzhiyun *
1234*4882a593Smuzhiyun * To be called when state change from charge<->discharge to update
1235*4882a593Smuzhiyun * the capacity scalers.
1236*4882a593Smuzhiyun */
ab8500_fg_update_cap_scalers(struct ab8500_fg * di)1237*4882a593Smuzhiyun static void ab8500_fg_update_cap_scalers(struct ab8500_fg *di)
1238*4882a593Smuzhiyun {
1239*4882a593Smuzhiyun struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale;
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun if (!cs->enable)
1242*4882a593Smuzhiyun return;
1243*4882a593Smuzhiyun if (di->flags.charging) {
1244*4882a593Smuzhiyun di->bat_cap.cap_scale.disable_cap_level =
1245*4882a593Smuzhiyun di->bat_cap.cap_scale.scaled_cap;
1246*4882a593Smuzhiyun dev_dbg(di->dev, "Cap to stop scale at charge %d%%\n",
1247*4882a593Smuzhiyun di->bat_cap.cap_scale.disable_cap_level);
1248*4882a593Smuzhiyun } else {
1249*4882a593Smuzhiyun if (cs->scaled_cap != 100) {
1250*4882a593Smuzhiyun cs->cap_to_scale[0] = cs->scaled_cap;
1251*4882a593Smuzhiyun cs->cap_to_scale[1] = di->bat_cap.prev_percent;
1252*4882a593Smuzhiyun } else {
1253*4882a593Smuzhiyun cs->cap_to_scale[0] = 100;
1254*4882a593Smuzhiyun cs->cap_to_scale[1] =
1255*4882a593Smuzhiyun max(di->bat_cap.prev_percent,
1256*4882a593Smuzhiyun di->bm->fg_params->maint_thres);
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun dev_dbg(di->dev, "Cap to scale at discharge %d/%d\n",
1260*4882a593Smuzhiyun cs->cap_to_scale[0], cs->cap_to_scale[1]);
1261*4882a593Smuzhiyun }
1262*4882a593Smuzhiyun }
1263*4882a593Smuzhiyun
1264*4882a593Smuzhiyun /**
1265*4882a593Smuzhiyun * ab8500_fg_check_capacity_limits() - Check if capacity has changed
1266*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1267*4882a593Smuzhiyun * @init: capacity is allowed to go up in init mode
1268*4882a593Smuzhiyun *
1269*4882a593Smuzhiyun * Check if capacity or capacity limit has changed and notify the system
1270*4882a593Smuzhiyun * about it using the power_supply framework
1271*4882a593Smuzhiyun */
ab8500_fg_check_capacity_limits(struct ab8500_fg * di,bool init)1272*4882a593Smuzhiyun static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init)
1273*4882a593Smuzhiyun {
1274*4882a593Smuzhiyun bool changed = false;
1275*4882a593Smuzhiyun int percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10);
1276*4882a593Smuzhiyun
1277*4882a593Smuzhiyun di->bat_cap.level = ab8500_fg_capacity_level(di);
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun if (di->bat_cap.level != di->bat_cap.prev_level) {
1280*4882a593Smuzhiyun /*
1281*4882a593Smuzhiyun * We do not allow reported capacity level to go up
1282*4882a593Smuzhiyun * unless we're charging or if we're in init
1283*4882a593Smuzhiyun */
1284*4882a593Smuzhiyun if (!(!di->flags.charging && di->bat_cap.level >
1285*4882a593Smuzhiyun di->bat_cap.prev_level) || init) {
1286*4882a593Smuzhiyun dev_dbg(di->dev, "level changed from %d to %d\n",
1287*4882a593Smuzhiyun di->bat_cap.prev_level,
1288*4882a593Smuzhiyun di->bat_cap.level);
1289*4882a593Smuzhiyun di->bat_cap.prev_level = di->bat_cap.level;
1290*4882a593Smuzhiyun changed = true;
1291*4882a593Smuzhiyun } else {
1292*4882a593Smuzhiyun dev_dbg(di->dev, "level not allowed to go up "
1293*4882a593Smuzhiyun "since no charger is connected: %d to %d\n",
1294*4882a593Smuzhiyun di->bat_cap.prev_level,
1295*4882a593Smuzhiyun di->bat_cap.level);
1296*4882a593Smuzhiyun }
1297*4882a593Smuzhiyun }
1298*4882a593Smuzhiyun
1299*4882a593Smuzhiyun /*
1300*4882a593Smuzhiyun * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate
1301*4882a593Smuzhiyun * shutdown
1302*4882a593Smuzhiyun */
1303*4882a593Smuzhiyun if (di->flags.low_bat) {
1304*4882a593Smuzhiyun dev_dbg(di->dev, "Battery low, set capacity to 0\n");
1305*4882a593Smuzhiyun di->bat_cap.prev_percent = 0;
1306*4882a593Smuzhiyun di->bat_cap.permille = 0;
1307*4882a593Smuzhiyun percent = 0;
1308*4882a593Smuzhiyun di->bat_cap.prev_mah = 0;
1309*4882a593Smuzhiyun di->bat_cap.mah = 0;
1310*4882a593Smuzhiyun changed = true;
1311*4882a593Smuzhiyun } else if (di->flags.fully_charged) {
1312*4882a593Smuzhiyun /*
1313*4882a593Smuzhiyun * We report 100% if algorithm reported fully charged
1314*4882a593Smuzhiyun * and show 100% during maintenance charging (scaling).
1315*4882a593Smuzhiyun */
1316*4882a593Smuzhiyun if (di->flags.force_full) {
1317*4882a593Smuzhiyun di->bat_cap.prev_percent = percent;
1318*4882a593Smuzhiyun di->bat_cap.prev_mah = di->bat_cap.mah;
1319*4882a593Smuzhiyun
1320*4882a593Smuzhiyun changed = true;
1321*4882a593Smuzhiyun
1322*4882a593Smuzhiyun if (!di->bat_cap.cap_scale.enable &&
1323*4882a593Smuzhiyun di->bm->capacity_scaling) {
1324*4882a593Smuzhiyun di->bat_cap.cap_scale.enable = true;
1325*4882a593Smuzhiyun di->bat_cap.cap_scale.cap_to_scale[0] = 100;
1326*4882a593Smuzhiyun di->bat_cap.cap_scale.cap_to_scale[1] =
1327*4882a593Smuzhiyun di->bat_cap.prev_percent;
1328*4882a593Smuzhiyun di->bat_cap.cap_scale.disable_cap_level = 100;
1329*4882a593Smuzhiyun }
1330*4882a593Smuzhiyun } else if (di->bat_cap.prev_percent != percent) {
1331*4882a593Smuzhiyun dev_dbg(di->dev,
1332*4882a593Smuzhiyun "battery reported full "
1333*4882a593Smuzhiyun "but capacity dropping: %d\n",
1334*4882a593Smuzhiyun percent);
1335*4882a593Smuzhiyun di->bat_cap.prev_percent = percent;
1336*4882a593Smuzhiyun di->bat_cap.prev_mah = di->bat_cap.mah;
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun changed = true;
1339*4882a593Smuzhiyun }
1340*4882a593Smuzhiyun } else if (di->bat_cap.prev_percent != percent) {
1341*4882a593Smuzhiyun if (percent == 0) {
1342*4882a593Smuzhiyun /*
1343*4882a593Smuzhiyun * We will not report 0% unless we've got
1344*4882a593Smuzhiyun * the LOW_BAT IRQ, no matter what the FG
1345*4882a593Smuzhiyun * algorithm says.
1346*4882a593Smuzhiyun */
1347*4882a593Smuzhiyun di->bat_cap.prev_percent = 1;
1348*4882a593Smuzhiyun percent = 1;
1349*4882a593Smuzhiyun
1350*4882a593Smuzhiyun changed = true;
1351*4882a593Smuzhiyun } else if (!(!di->flags.charging &&
1352*4882a593Smuzhiyun percent > di->bat_cap.prev_percent) || init) {
1353*4882a593Smuzhiyun /*
1354*4882a593Smuzhiyun * We do not allow reported capacity to go up
1355*4882a593Smuzhiyun * unless we're charging or if we're in init
1356*4882a593Smuzhiyun */
1357*4882a593Smuzhiyun dev_dbg(di->dev,
1358*4882a593Smuzhiyun "capacity changed from %d to %d (%d)\n",
1359*4882a593Smuzhiyun di->bat_cap.prev_percent,
1360*4882a593Smuzhiyun percent,
1361*4882a593Smuzhiyun di->bat_cap.permille);
1362*4882a593Smuzhiyun di->bat_cap.prev_percent = percent;
1363*4882a593Smuzhiyun di->bat_cap.prev_mah = di->bat_cap.mah;
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun changed = true;
1366*4882a593Smuzhiyun } else {
1367*4882a593Smuzhiyun dev_dbg(di->dev, "capacity not allowed to go up since "
1368*4882a593Smuzhiyun "no charger is connected: %d to %d (%d)\n",
1369*4882a593Smuzhiyun di->bat_cap.prev_percent,
1370*4882a593Smuzhiyun percent,
1371*4882a593Smuzhiyun di->bat_cap.permille);
1372*4882a593Smuzhiyun }
1373*4882a593Smuzhiyun }
1374*4882a593Smuzhiyun
1375*4882a593Smuzhiyun if (changed) {
1376*4882a593Smuzhiyun if (di->bm->capacity_scaling) {
1377*4882a593Smuzhiyun di->bat_cap.cap_scale.scaled_cap =
1378*4882a593Smuzhiyun ab8500_fg_calculate_scaled_capacity(di);
1379*4882a593Smuzhiyun
1380*4882a593Smuzhiyun dev_info(di->dev, "capacity=%d (%d)\n",
1381*4882a593Smuzhiyun di->bat_cap.prev_percent,
1382*4882a593Smuzhiyun di->bat_cap.cap_scale.scaled_cap);
1383*4882a593Smuzhiyun }
1384*4882a593Smuzhiyun power_supply_changed(di->fg_psy);
1385*4882a593Smuzhiyun if (di->flags.fully_charged && di->flags.force_full) {
1386*4882a593Smuzhiyun dev_dbg(di->dev, "Battery full, notifying.\n");
1387*4882a593Smuzhiyun di->flags.force_full = false;
1388*4882a593Smuzhiyun sysfs_notify(&di->fg_kobject, NULL, "charge_full");
1389*4882a593Smuzhiyun }
1390*4882a593Smuzhiyun sysfs_notify(&di->fg_kobject, NULL, "charge_now");
1391*4882a593Smuzhiyun }
1392*4882a593Smuzhiyun }
1393*4882a593Smuzhiyun
ab8500_fg_charge_state_to(struct ab8500_fg * di,enum ab8500_fg_charge_state new_state)1394*4882a593Smuzhiyun static void ab8500_fg_charge_state_to(struct ab8500_fg *di,
1395*4882a593Smuzhiyun enum ab8500_fg_charge_state new_state)
1396*4882a593Smuzhiyun {
1397*4882a593Smuzhiyun dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n",
1398*4882a593Smuzhiyun di->charge_state,
1399*4882a593Smuzhiyun charge_state[di->charge_state],
1400*4882a593Smuzhiyun new_state,
1401*4882a593Smuzhiyun charge_state[new_state]);
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun di->charge_state = new_state;
1404*4882a593Smuzhiyun }
1405*4882a593Smuzhiyun
ab8500_fg_discharge_state_to(struct ab8500_fg * di,enum ab8500_fg_discharge_state new_state)1406*4882a593Smuzhiyun static void ab8500_fg_discharge_state_to(struct ab8500_fg *di,
1407*4882a593Smuzhiyun enum ab8500_fg_discharge_state new_state)
1408*4882a593Smuzhiyun {
1409*4882a593Smuzhiyun dev_dbg(di->dev, "Discharge state from %d [%s] to %d [%s]\n",
1410*4882a593Smuzhiyun di->discharge_state,
1411*4882a593Smuzhiyun discharge_state[di->discharge_state],
1412*4882a593Smuzhiyun new_state,
1413*4882a593Smuzhiyun discharge_state[new_state]);
1414*4882a593Smuzhiyun
1415*4882a593Smuzhiyun di->discharge_state = new_state;
1416*4882a593Smuzhiyun }
1417*4882a593Smuzhiyun
1418*4882a593Smuzhiyun /**
1419*4882a593Smuzhiyun * ab8500_fg_algorithm_charging() - FG algorithm for when charging
1420*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1421*4882a593Smuzhiyun *
1422*4882a593Smuzhiyun * Battery capacity calculation state machine for when we're charging
1423*4882a593Smuzhiyun */
ab8500_fg_algorithm_charging(struct ab8500_fg * di)1424*4882a593Smuzhiyun static void ab8500_fg_algorithm_charging(struct ab8500_fg *di)
1425*4882a593Smuzhiyun {
1426*4882a593Smuzhiyun /*
1427*4882a593Smuzhiyun * If we change to discharge mode
1428*4882a593Smuzhiyun * we should start with recovery
1429*4882a593Smuzhiyun */
1430*4882a593Smuzhiyun if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY)
1431*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di,
1432*4882a593Smuzhiyun AB8500_FG_DISCHARGE_INIT_RECOVERY);
1433*4882a593Smuzhiyun
1434*4882a593Smuzhiyun switch (di->charge_state) {
1435*4882a593Smuzhiyun case AB8500_FG_CHARGE_INIT:
1436*4882a593Smuzhiyun di->fg_samples = SEC_TO_SAMPLE(
1437*4882a593Smuzhiyun di->bm->fg_params->accu_charging);
1438*4882a593Smuzhiyun
1439*4882a593Smuzhiyun ab8500_fg_coulomb_counter(di, true);
1440*4882a593Smuzhiyun ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT);
1441*4882a593Smuzhiyun
1442*4882a593Smuzhiyun break;
1443*4882a593Smuzhiyun
1444*4882a593Smuzhiyun case AB8500_FG_CHARGE_READOUT:
1445*4882a593Smuzhiyun /*
1446*4882a593Smuzhiyun * Read the FG and calculate the new capacity
1447*4882a593Smuzhiyun */
1448*4882a593Smuzhiyun mutex_lock(&di->cc_lock);
1449*4882a593Smuzhiyun if (!di->flags.conv_done && !di->flags.force_full) {
1450*4882a593Smuzhiyun /* Wasn't the CC IRQ that got us here */
1451*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
1452*4882a593Smuzhiyun dev_dbg(di->dev, "%s CC conv not done\n",
1453*4882a593Smuzhiyun __func__);
1454*4882a593Smuzhiyun
1455*4882a593Smuzhiyun break;
1456*4882a593Smuzhiyun }
1457*4882a593Smuzhiyun di->flags.conv_done = false;
1458*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
1459*4882a593Smuzhiyun
1460*4882a593Smuzhiyun ab8500_fg_calc_cap_charging(di);
1461*4882a593Smuzhiyun
1462*4882a593Smuzhiyun break;
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun default:
1465*4882a593Smuzhiyun break;
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun /* Check capacity limits */
1469*4882a593Smuzhiyun ab8500_fg_check_capacity_limits(di, false);
1470*4882a593Smuzhiyun }
1471*4882a593Smuzhiyun
force_capacity(struct ab8500_fg * di)1472*4882a593Smuzhiyun static void force_capacity(struct ab8500_fg *di)
1473*4882a593Smuzhiyun {
1474*4882a593Smuzhiyun int cap;
1475*4882a593Smuzhiyun
1476*4882a593Smuzhiyun ab8500_fg_clear_cap_samples(di);
1477*4882a593Smuzhiyun cap = di->bat_cap.user_mah;
1478*4882a593Smuzhiyun if (cap > di->bat_cap.max_mah_design) {
1479*4882a593Smuzhiyun dev_dbg(di->dev, "Remaining cap %d can't be bigger than total"
1480*4882a593Smuzhiyun " %d\n", cap, di->bat_cap.max_mah_design);
1481*4882a593Smuzhiyun cap = di->bat_cap.max_mah_design;
1482*4882a593Smuzhiyun }
1483*4882a593Smuzhiyun ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah);
1484*4882a593Smuzhiyun di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap);
1485*4882a593Smuzhiyun di->bat_cap.mah = cap;
1486*4882a593Smuzhiyun ab8500_fg_check_capacity_limits(di, true);
1487*4882a593Smuzhiyun }
1488*4882a593Smuzhiyun
check_sysfs_capacity(struct ab8500_fg * di)1489*4882a593Smuzhiyun static bool check_sysfs_capacity(struct ab8500_fg *di)
1490*4882a593Smuzhiyun {
1491*4882a593Smuzhiyun int cap, lower, upper;
1492*4882a593Smuzhiyun int cap_permille;
1493*4882a593Smuzhiyun
1494*4882a593Smuzhiyun cap = di->bat_cap.user_mah;
1495*4882a593Smuzhiyun
1496*4882a593Smuzhiyun cap_permille = ab8500_fg_convert_mah_to_permille(di,
1497*4882a593Smuzhiyun di->bat_cap.user_mah);
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun lower = di->bat_cap.permille - di->bm->fg_params->user_cap_limit * 10;
1500*4882a593Smuzhiyun upper = di->bat_cap.permille + di->bm->fg_params->user_cap_limit * 10;
1501*4882a593Smuzhiyun
1502*4882a593Smuzhiyun if (lower < 0)
1503*4882a593Smuzhiyun lower = 0;
1504*4882a593Smuzhiyun /* 1000 is permille, -> 100 percent */
1505*4882a593Smuzhiyun if (upper > 1000)
1506*4882a593Smuzhiyun upper = 1000;
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun dev_dbg(di->dev, "Capacity limits:"
1509*4882a593Smuzhiyun " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
1510*4882a593Smuzhiyun lower, cap_permille, upper, cap, di->bat_cap.mah);
1511*4882a593Smuzhiyun
1512*4882a593Smuzhiyun /* If within limits, use the saved capacity and exit estimation...*/
1513*4882a593Smuzhiyun if (cap_permille > lower && cap_permille < upper) {
1514*4882a593Smuzhiyun dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap);
1515*4882a593Smuzhiyun force_capacity(di);
1516*4882a593Smuzhiyun return true;
1517*4882a593Smuzhiyun }
1518*4882a593Smuzhiyun dev_dbg(di->dev, "Capacity from user out of limits, ignoring");
1519*4882a593Smuzhiyun return false;
1520*4882a593Smuzhiyun }
1521*4882a593Smuzhiyun
1522*4882a593Smuzhiyun /**
1523*4882a593Smuzhiyun * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
1524*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1525*4882a593Smuzhiyun *
1526*4882a593Smuzhiyun * Battery capacity calculation state machine for when we're discharging
1527*4882a593Smuzhiyun */
ab8500_fg_algorithm_discharging(struct ab8500_fg * di)1528*4882a593Smuzhiyun static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di)
1529*4882a593Smuzhiyun {
1530*4882a593Smuzhiyun int sleep_time;
1531*4882a593Smuzhiyun
1532*4882a593Smuzhiyun /* If we change to charge mode we should start with init */
1533*4882a593Smuzhiyun if (di->charge_state != AB8500_FG_CHARGE_INIT)
1534*4882a593Smuzhiyun ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
1535*4882a593Smuzhiyun
1536*4882a593Smuzhiyun switch (di->discharge_state) {
1537*4882a593Smuzhiyun case AB8500_FG_DISCHARGE_INIT:
1538*4882a593Smuzhiyun /* We use the FG IRQ to work on */
1539*4882a593Smuzhiyun di->init_cnt = 0;
1540*4882a593Smuzhiyun di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
1541*4882a593Smuzhiyun ab8500_fg_coulomb_counter(di, true);
1542*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di,
1543*4882a593Smuzhiyun AB8500_FG_DISCHARGE_INITMEASURING);
1544*4882a593Smuzhiyun
1545*4882a593Smuzhiyun fallthrough;
1546*4882a593Smuzhiyun case AB8500_FG_DISCHARGE_INITMEASURING:
1547*4882a593Smuzhiyun /*
1548*4882a593Smuzhiyun * Discard a number of samples during startup.
1549*4882a593Smuzhiyun * After that, use compensated voltage for a few
1550*4882a593Smuzhiyun * samples to get an initial capacity.
1551*4882a593Smuzhiyun * Then go to READOUT
1552*4882a593Smuzhiyun */
1553*4882a593Smuzhiyun sleep_time = di->bm->fg_params->init_timer;
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun /* Discard the first [x] seconds */
1556*4882a593Smuzhiyun if (di->init_cnt > di->bm->fg_params->init_discard_time) {
1557*4882a593Smuzhiyun ab8500_fg_calc_cap_discharge_voltage(di, true);
1558*4882a593Smuzhiyun
1559*4882a593Smuzhiyun ab8500_fg_check_capacity_limits(di, true);
1560*4882a593Smuzhiyun }
1561*4882a593Smuzhiyun
1562*4882a593Smuzhiyun di->init_cnt += sleep_time;
1563*4882a593Smuzhiyun if (di->init_cnt > di->bm->fg_params->init_total_time)
1564*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di,
1565*4882a593Smuzhiyun AB8500_FG_DISCHARGE_READOUT_INIT);
1566*4882a593Smuzhiyun
1567*4882a593Smuzhiyun break;
1568*4882a593Smuzhiyun
1569*4882a593Smuzhiyun case AB8500_FG_DISCHARGE_INIT_RECOVERY:
1570*4882a593Smuzhiyun di->recovery_cnt = 0;
1571*4882a593Smuzhiyun di->recovery_needed = true;
1572*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di,
1573*4882a593Smuzhiyun AB8500_FG_DISCHARGE_RECOVERY);
1574*4882a593Smuzhiyun
1575*4882a593Smuzhiyun fallthrough;
1576*4882a593Smuzhiyun
1577*4882a593Smuzhiyun case AB8500_FG_DISCHARGE_RECOVERY:
1578*4882a593Smuzhiyun sleep_time = di->bm->fg_params->recovery_sleep_timer;
1579*4882a593Smuzhiyun
1580*4882a593Smuzhiyun /*
1581*4882a593Smuzhiyun * We should check the power consumption
1582*4882a593Smuzhiyun * If low, go to READOUT (after x min) or
1583*4882a593Smuzhiyun * RECOVERY_SLEEP if time left.
1584*4882a593Smuzhiyun * If high, go to READOUT
1585*4882a593Smuzhiyun */
1586*4882a593Smuzhiyun di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1587*4882a593Smuzhiyun
1588*4882a593Smuzhiyun if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1589*4882a593Smuzhiyun if (di->recovery_cnt >
1590*4882a593Smuzhiyun di->bm->fg_params->recovery_total_time) {
1591*4882a593Smuzhiyun di->fg_samples = SEC_TO_SAMPLE(
1592*4882a593Smuzhiyun di->bm->fg_params->accu_high_curr);
1593*4882a593Smuzhiyun ab8500_fg_coulomb_counter(di, true);
1594*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di,
1595*4882a593Smuzhiyun AB8500_FG_DISCHARGE_READOUT);
1596*4882a593Smuzhiyun di->recovery_needed = false;
1597*4882a593Smuzhiyun } else {
1598*4882a593Smuzhiyun queue_delayed_work(di->fg_wq,
1599*4882a593Smuzhiyun &di->fg_periodic_work,
1600*4882a593Smuzhiyun sleep_time * HZ);
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun di->recovery_cnt += sleep_time;
1603*4882a593Smuzhiyun } else {
1604*4882a593Smuzhiyun di->fg_samples = SEC_TO_SAMPLE(
1605*4882a593Smuzhiyun di->bm->fg_params->accu_high_curr);
1606*4882a593Smuzhiyun ab8500_fg_coulomb_counter(di, true);
1607*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di,
1608*4882a593Smuzhiyun AB8500_FG_DISCHARGE_READOUT);
1609*4882a593Smuzhiyun }
1610*4882a593Smuzhiyun break;
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun case AB8500_FG_DISCHARGE_READOUT_INIT:
1613*4882a593Smuzhiyun di->fg_samples = SEC_TO_SAMPLE(
1614*4882a593Smuzhiyun di->bm->fg_params->accu_high_curr);
1615*4882a593Smuzhiyun ab8500_fg_coulomb_counter(di, true);
1616*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di,
1617*4882a593Smuzhiyun AB8500_FG_DISCHARGE_READOUT);
1618*4882a593Smuzhiyun break;
1619*4882a593Smuzhiyun
1620*4882a593Smuzhiyun case AB8500_FG_DISCHARGE_READOUT:
1621*4882a593Smuzhiyun di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1622*4882a593Smuzhiyun
1623*4882a593Smuzhiyun if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1624*4882a593Smuzhiyun /* Detect mode change */
1625*4882a593Smuzhiyun if (di->high_curr_mode) {
1626*4882a593Smuzhiyun di->high_curr_mode = false;
1627*4882a593Smuzhiyun di->high_curr_cnt = 0;
1628*4882a593Smuzhiyun }
1629*4882a593Smuzhiyun
1630*4882a593Smuzhiyun if (di->recovery_needed) {
1631*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di,
1632*4882a593Smuzhiyun AB8500_FG_DISCHARGE_INIT_RECOVERY);
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun queue_delayed_work(di->fg_wq,
1635*4882a593Smuzhiyun &di->fg_periodic_work, 0);
1636*4882a593Smuzhiyun
1637*4882a593Smuzhiyun break;
1638*4882a593Smuzhiyun }
1639*4882a593Smuzhiyun
1640*4882a593Smuzhiyun ab8500_fg_calc_cap_discharge_voltage(di, true);
1641*4882a593Smuzhiyun } else {
1642*4882a593Smuzhiyun mutex_lock(&di->cc_lock);
1643*4882a593Smuzhiyun if (!di->flags.conv_done) {
1644*4882a593Smuzhiyun /* Wasn't the CC IRQ that got us here */
1645*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
1646*4882a593Smuzhiyun dev_dbg(di->dev, "%s CC conv not done\n",
1647*4882a593Smuzhiyun __func__);
1648*4882a593Smuzhiyun
1649*4882a593Smuzhiyun break;
1650*4882a593Smuzhiyun }
1651*4882a593Smuzhiyun di->flags.conv_done = false;
1652*4882a593Smuzhiyun mutex_unlock(&di->cc_lock);
1653*4882a593Smuzhiyun
1654*4882a593Smuzhiyun /* Detect mode change */
1655*4882a593Smuzhiyun if (!di->high_curr_mode) {
1656*4882a593Smuzhiyun di->high_curr_mode = true;
1657*4882a593Smuzhiyun di->high_curr_cnt = 0;
1658*4882a593Smuzhiyun }
1659*4882a593Smuzhiyun
1660*4882a593Smuzhiyun di->high_curr_cnt +=
1661*4882a593Smuzhiyun di->bm->fg_params->accu_high_curr;
1662*4882a593Smuzhiyun if (di->high_curr_cnt >
1663*4882a593Smuzhiyun di->bm->fg_params->high_curr_time)
1664*4882a593Smuzhiyun di->recovery_needed = true;
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun ab8500_fg_calc_cap_discharge_fg(di);
1667*4882a593Smuzhiyun }
1668*4882a593Smuzhiyun
1669*4882a593Smuzhiyun ab8500_fg_check_capacity_limits(di, false);
1670*4882a593Smuzhiyun
1671*4882a593Smuzhiyun break;
1672*4882a593Smuzhiyun
1673*4882a593Smuzhiyun case AB8500_FG_DISCHARGE_WAKEUP:
1674*4882a593Smuzhiyun ab8500_fg_calc_cap_discharge_voltage(di, true);
1675*4882a593Smuzhiyun
1676*4882a593Smuzhiyun di->fg_samples = SEC_TO_SAMPLE(
1677*4882a593Smuzhiyun di->bm->fg_params->accu_high_curr);
1678*4882a593Smuzhiyun ab8500_fg_coulomb_counter(di, true);
1679*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di,
1680*4882a593Smuzhiyun AB8500_FG_DISCHARGE_READOUT);
1681*4882a593Smuzhiyun
1682*4882a593Smuzhiyun ab8500_fg_check_capacity_limits(di, false);
1683*4882a593Smuzhiyun
1684*4882a593Smuzhiyun break;
1685*4882a593Smuzhiyun
1686*4882a593Smuzhiyun default:
1687*4882a593Smuzhiyun break;
1688*4882a593Smuzhiyun }
1689*4882a593Smuzhiyun }
1690*4882a593Smuzhiyun
1691*4882a593Smuzhiyun /**
1692*4882a593Smuzhiyun * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
1693*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1694*4882a593Smuzhiyun *
1695*4882a593Smuzhiyun */
ab8500_fg_algorithm_calibrate(struct ab8500_fg * di)1696*4882a593Smuzhiyun static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di)
1697*4882a593Smuzhiyun {
1698*4882a593Smuzhiyun int ret;
1699*4882a593Smuzhiyun
1700*4882a593Smuzhiyun switch (di->calib_state) {
1701*4882a593Smuzhiyun case AB8500_FG_CALIB_INIT:
1702*4882a593Smuzhiyun dev_dbg(di->dev, "Calibration ongoing...\n");
1703*4882a593Smuzhiyun
1704*4882a593Smuzhiyun ret = abx500_mask_and_set_register_interruptible(di->dev,
1705*4882a593Smuzhiyun AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1706*4882a593Smuzhiyun CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8);
1707*4882a593Smuzhiyun if (ret < 0)
1708*4882a593Smuzhiyun goto err;
1709*4882a593Smuzhiyun
1710*4882a593Smuzhiyun ret = abx500_mask_and_set_register_interruptible(di->dev,
1711*4882a593Smuzhiyun AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1712*4882a593Smuzhiyun CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA);
1713*4882a593Smuzhiyun if (ret < 0)
1714*4882a593Smuzhiyun goto err;
1715*4882a593Smuzhiyun di->calib_state = AB8500_FG_CALIB_WAIT;
1716*4882a593Smuzhiyun break;
1717*4882a593Smuzhiyun case AB8500_FG_CALIB_END:
1718*4882a593Smuzhiyun ret = abx500_mask_and_set_register_interruptible(di->dev,
1719*4882a593Smuzhiyun AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1720*4882a593Smuzhiyun CC_MUXOFFSET, CC_MUXOFFSET);
1721*4882a593Smuzhiyun if (ret < 0)
1722*4882a593Smuzhiyun goto err;
1723*4882a593Smuzhiyun di->flags.calibrate = false;
1724*4882a593Smuzhiyun dev_dbg(di->dev, "Calibration done...\n");
1725*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1726*4882a593Smuzhiyun break;
1727*4882a593Smuzhiyun case AB8500_FG_CALIB_WAIT:
1728*4882a593Smuzhiyun dev_dbg(di->dev, "Calibration WFI\n");
1729*4882a593Smuzhiyun default:
1730*4882a593Smuzhiyun break;
1731*4882a593Smuzhiyun }
1732*4882a593Smuzhiyun return;
1733*4882a593Smuzhiyun err:
1734*4882a593Smuzhiyun /* Something went wrong, don't calibrate then */
1735*4882a593Smuzhiyun dev_err(di->dev, "failed to calibrate the CC\n");
1736*4882a593Smuzhiyun di->flags.calibrate = false;
1737*4882a593Smuzhiyun di->calib_state = AB8500_FG_CALIB_INIT;
1738*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1739*4882a593Smuzhiyun }
1740*4882a593Smuzhiyun
1741*4882a593Smuzhiyun /**
1742*4882a593Smuzhiyun * ab8500_fg_algorithm() - Entry point for the FG algorithm
1743*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1744*4882a593Smuzhiyun *
1745*4882a593Smuzhiyun * Entry point for the battery capacity calculation state machine
1746*4882a593Smuzhiyun */
ab8500_fg_algorithm(struct ab8500_fg * di)1747*4882a593Smuzhiyun static void ab8500_fg_algorithm(struct ab8500_fg *di)
1748*4882a593Smuzhiyun {
1749*4882a593Smuzhiyun if (di->flags.calibrate)
1750*4882a593Smuzhiyun ab8500_fg_algorithm_calibrate(di);
1751*4882a593Smuzhiyun else {
1752*4882a593Smuzhiyun if (di->flags.charging)
1753*4882a593Smuzhiyun ab8500_fg_algorithm_charging(di);
1754*4882a593Smuzhiyun else
1755*4882a593Smuzhiyun ab8500_fg_algorithm_discharging(di);
1756*4882a593Smuzhiyun }
1757*4882a593Smuzhiyun
1758*4882a593Smuzhiyun dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d %d "
1759*4882a593Smuzhiyun "%d %d %d %d %d %d %d\n",
1760*4882a593Smuzhiyun di->bat_cap.max_mah_design,
1761*4882a593Smuzhiyun di->bat_cap.max_mah,
1762*4882a593Smuzhiyun di->bat_cap.mah,
1763*4882a593Smuzhiyun di->bat_cap.permille,
1764*4882a593Smuzhiyun di->bat_cap.level,
1765*4882a593Smuzhiyun di->bat_cap.prev_mah,
1766*4882a593Smuzhiyun di->bat_cap.prev_percent,
1767*4882a593Smuzhiyun di->bat_cap.prev_level,
1768*4882a593Smuzhiyun di->vbat,
1769*4882a593Smuzhiyun di->inst_curr,
1770*4882a593Smuzhiyun di->avg_curr,
1771*4882a593Smuzhiyun di->accu_charge,
1772*4882a593Smuzhiyun di->flags.charging,
1773*4882a593Smuzhiyun di->charge_state,
1774*4882a593Smuzhiyun di->discharge_state,
1775*4882a593Smuzhiyun di->high_curr_mode,
1776*4882a593Smuzhiyun di->recovery_needed);
1777*4882a593Smuzhiyun }
1778*4882a593Smuzhiyun
1779*4882a593Smuzhiyun /**
1780*4882a593Smuzhiyun * ab8500_fg_periodic_work() - Run the FG state machine periodically
1781*4882a593Smuzhiyun * @work: pointer to the work_struct structure
1782*4882a593Smuzhiyun *
1783*4882a593Smuzhiyun * Work queue function for periodic work
1784*4882a593Smuzhiyun */
ab8500_fg_periodic_work(struct work_struct * work)1785*4882a593Smuzhiyun static void ab8500_fg_periodic_work(struct work_struct *work)
1786*4882a593Smuzhiyun {
1787*4882a593Smuzhiyun struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1788*4882a593Smuzhiyun fg_periodic_work.work);
1789*4882a593Smuzhiyun
1790*4882a593Smuzhiyun if (di->init_capacity) {
1791*4882a593Smuzhiyun /* Get an initial capacity calculation */
1792*4882a593Smuzhiyun ab8500_fg_calc_cap_discharge_voltage(di, true);
1793*4882a593Smuzhiyun ab8500_fg_check_capacity_limits(di, true);
1794*4882a593Smuzhiyun di->init_capacity = false;
1795*4882a593Smuzhiyun
1796*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1797*4882a593Smuzhiyun } else if (di->flags.user_cap) {
1798*4882a593Smuzhiyun if (check_sysfs_capacity(di)) {
1799*4882a593Smuzhiyun ab8500_fg_check_capacity_limits(di, true);
1800*4882a593Smuzhiyun if (di->flags.charging)
1801*4882a593Smuzhiyun ab8500_fg_charge_state_to(di,
1802*4882a593Smuzhiyun AB8500_FG_CHARGE_INIT);
1803*4882a593Smuzhiyun else
1804*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di,
1805*4882a593Smuzhiyun AB8500_FG_DISCHARGE_READOUT_INIT);
1806*4882a593Smuzhiyun }
1807*4882a593Smuzhiyun di->flags.user_cap = false;
1808*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1809*4882a593Smuzhiyun } else
1810*4882a593Smuzhiyun ab8500_fg_algorithm(di);
1811*4882a593Smuzhiyun
1812*4882a593Smuzhiyun }
1813*4882a593Smuzhiyun
1814*4882a593Smuzhiyun /**
1815*4882a593Smuzhiyun * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
1816*4882a593Smuzhiyun * @work: pointer to the work_struct structure
1817*4882a593Smuzhiyun *
1818*4882a593Smuzhiyun * Work queue function for checking the OVV_BAT condition
1819*4882a593Smuzhiyun */
ab8500_fg_check_hw_failure_work(struct work_struct * work)1820*4882a593Smuzhiyun static void ab8500_fg_check_hw_failure_work(struct work_struct *work)
1821*4882a593Smuzhiyun {
1822*4882a593Smuzhiyun int ret;
1823*4882a593Smuzhiyun u8 reg_value;
1824*4882a593Smuzhiyun
1825*4882a593Smuzhiyun struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1826*4882a593Smuzhiyun fg_check_hw_failure_work.work);
1827*4882a593Smuzhiyun
1828*4882a593Smuzhiyun /*
1829*4882a593Smuzhiyun * If we have had a battery over-voltage situation,
1830*4882a593Smuzhiyun * check ovv-bit to see if it should be reset.
1831*4882a593Smuzhiyun */
1832*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev,
1833*4882a593Smuzhiyun AB8500_CHARGER, AB8500_CH_STAT_REG,
1834*4882a593Smuzhiyun ®_value);
1835*4882a593Smuzhiyun if (ret < 0) {
1836*4882a593Smuzhiyun dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1837*4882a593Smuzhiyun return;
1838*4882a593Smuzhiyun }
1839*4882a593Smuzhiyun if ((reg_value & BATT_OVV) == BATT_OVV) {
1840*4882a593Smuzhiyun if (!di->flags.bat_ovv) {
1841*4882a593Smuzhiyun dev_dbg(di->dev, "Battery OVV\n");
1842*4882a593Smuzhiyun di->flags.bat_ovv = true;
1843*4882a593Smuzhiyun power_supply_changed(di->fg_psy);
1844*4882a593Smuzhiyun }
1845*4882a593Smuzhiyun /* Not yet recovered from ovv, reschedule this test */
1846*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work,
1847*4882a593Smuzhiyun HZ);
1848*4882a593Smuzhiyun } else {
1849*4882a593Smuzhiyun dev_dbg(di->dev, "Battery recovered from OVV\n");
1850*4882a593Smuzhiyun di->flags.bat_ovv = false;
1851*4882a593Smuzhiyun power_supply_changed(di->fg_psy);
1852*4882a593Smuzhiyun }
1853*4882a593Smuzhiyun }
1854*4882a593Smuzhiyun
1855*4882a593Smuzhiyun /**
1856*4882a593Smuzhiyun * ab8500_fg_low_bat_work() - Check LOW_BAT condition
1857*4882a593Smuzhiyun * @work: pointer to the work_struct structure
1858*4882a593Smuzhiyun *
1859*4882a593Smuzhiyun * Work queue function for checking the LOW_BAT condition
1860*4882a593Smuzhiyun */
ab8500_fg_low_bat_work(struct work_struct * work)1861*4882a593Smuzhiyun static void ab8500_fg_low_bat_work(struct work_struct *work)
1862*4882a593Smuzhiyun {
1863*4882a593Smuzhiyun int vbat;
1864*4882a593Smuzhiyun
1865*4882a593Smuzhiyun struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1866*4882a593Smuzhiyun fg_low_bat_work.work);
1867*4882a593Smuzhiyun
1868*4882a593Smuzhiyun vbat = ab8500_fg_bat_voltage(di);
1869*4882a593Smuzhiyun
1870*4882a593Smuzhiyun /* Check if LOW_BAT still fulfilled */
1871*4882a593Smuzhiyun if (vbat < di->bm->fg_params->lowbat_threshold) {
1872*4882a593Smuzhiyun /* Is it time to shut down? */
1873*4882a593Smuzhiyun if (di->low_bat_cnt < 1) {
1874*4882a593Smuzhiyun di->flags.low_bat = true;
1875*4882a593Smuzhiyun dev_warn(di->dev, "Shut down pending...\n");
1876*4882a593Smuzhiyun } else {
1877*4882a593Smuzhiyun /*
1878*4882a593Smuzhiyun * Else we need to re-schedule this check to be able to detect
1879*4882a593Smuzhiyun * if the voltage increases again during charging or
1880*4882a593Smuzhiyun * due to decreasing load.
1881*4882a593Smuzhiyun */
1882*4882a593Smuzhiyun di->low_bat_cnt--;
1883*4882a593Smuzhiyun dev_warn(di->dev, "Battery voltage still LOW\n");
1884*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
1885*4882a593Smuzhiyun round_jiffies(LOW_BAT_CHECK_INTERVAL));
1886*4882a593Smuzhiyun }
1887*4882a593Smuzhiyun } else {
1888*4882a593Smuzhiyun di->flags.low_bat_delay = false;
1889*4882a593Smuzhiyun di->low_bat_cnt = 10;
1890*4882a593Smuzhiyun dev_warn(di->dev, "Battery voltage OK again\n");
1891*4882a593Smuzhiyun }
1892*4882a593Smuzhiyun
1893*4882a593Smuzhiyun /* This is needed to dispatch LOW_BAT */
1894*4882a593Smuzhiyun ab8500_fg_check_capacity_limits(di, false);
1895*4882a593Smuzhiyun }
1896*4882a593Smuzhiyun
1897*4882a593Smuzhiyun /**
1898*4882a593Smuzhiyun * ab8500_fg_battok_calc - calculate the bit pattern corresponding
1899*4882a593Smuzhiyun * to the target voltage.
1900*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1901*4882a593Smuzhiyun * @target: target voltage
1902*4882a593Smuzhiyun *
1903*4882a593Smuzhiyun * Returns bit pattern closest to the target voltage
1904*4882a593Smuzhiyun * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
1905*4882a593Smuzhiyun */
1906*4882a593Smuzhiyun
ab8500_fg_battok_calc(struct ab8500_fg * di,int target)1907*4882a593Smuzhiyun static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target)
1908*4882a593Smuzhiyun {
1909*4882a593Smuzhiyun if (target > BATT_OK_MIN +
1910*4882a593Smuzhiyun (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS))
1911*4882a593Smuzhiyun return BATT_OK_MAX_NR_INCREMENTS;
1912*4882a593Smuzhiyun if (target < BATT_OK_MIN)
1913*4882a593Smuzhiyun return 0;
1914*4882a593Smuzhiyun return (target - BATT_OK_MIN) / BATT_OK_INCREMENT;
1915*4882a593Smuzhiyun }
1916*4882a593Smuzhiyun
1917*4882a593Smuzhiyun /**
1918*4882a593Smuzhiyun * ab8500_fg_battok_init_hw_register - init battok levels
1919*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
1920*4882a593Smuzhiyun *
1921*4882a593Smuzhiyun */
1922*4882a593Smuzhiyun
ab8500_fg_battok_init_hw_register(struct ab8500_fg * di)1923*4882a593Smuzhiyun static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di)
1924*4882a593Smuzhiyun {
1925*4882a593Smuzhiyun int selected;
1926*4882a593Smuzhiyun int sel0;
1927*4882a593Smuzhiyun int sel1;
1928*4882a593Smuzhiyun int cbp_sel0;
1929*4882a593Smuzhiyun int cbp_sel1;
1930*4882a593Smuzhiyun int ret;
1931*4882a593Smuzhiyun int new_val;
1932*4882a593Smuzhiyun
1933*4882a593Smuzhiyun sel0 = di->bm->fg_params->battok_falling_th_sel0;
1934*4882a593Smuzhiyun sel1 = di->bm->fg_params->battok_raising_th_sel1;
1935*4882a593Smuzhiyun
1936*4882a593Smuzhiyun cbp_sel0 = ab8500_fg_battok_calc(di, sel0);
1937*4882a593Smuzhiyun cbp_sel1 = ab8500_fg_battok_calc(di, sel1);
1938*4882a593Smuzhiyun
1939*4882a593Smuzhiyun selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT;
1940*4882a593Smuzhiyun
1941*4882a593Smuzhiyun if (selected != sel0)
1942*4882a593Smuzhiyun dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1943*4882a593Smuzhiyun sel0, selected, cbp_sel0);
1944*4882a593Smuzhiyun
1945*4882a593Smuzhiyun selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT;
1946*4882a593Smuzhiyun
1947*4882a593Smuzhiyun if (selected != sel1)
1948*4882a593Smuzhiyun dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1949*4882a593Smuzhiyun sel1, selected, cbp_sel1);
1950*4882a593Smuzhiyun
1951*4882a593Smuzhiyun new_val = cbp_sel0 | (cbp_sel1 << 4);
1952*4882a593Smuzhiyun
1953*4882a593Smuzhiyun dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1);
1954*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK,
1955*4882a593Smuzhiyun AB8500_BATT_OK_REG, new_val);
1956*4882a593Smuzhiyun return ret;
1957*4882a593Smuzhiyun }
1958*4882a593Smuzhiyun
1959*4882a593Smuzhiyun /**
1960*4882a593Smuzhiyun * ab8500_fg_instant_work() - Run the FG state machine instantly
1961*4882a593Smuzhiyun * @work: pointer to the work_struct structure
1962*4882a593Smuzhiyun *
1963*4882a593Smuzhiyun * Work queue function for instant work
1964*4882a593Smuzhiyun */
ab8500_fg_instant_work(struct work_struct * work)1965*4882a593Smuzhiyun static void ab8500_fg_instant_work(struct work_struct *work)
1966*4882a593Smuzhiyun {
1967*4882a593Smuzhiyun struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work);
1968*4882a593Smuzhiyun
1969*4882a593Smuzhiyun ab8500_fg_algorithm(di);
1970*4882a593Smuzhiyun }
1971*4882a593Smuzhiyun
1972*4882a593Smuzhiyun /**
1973*4882a593Smuzhiyun * ab8500_fg_cc_data_end_handler() - end of data conversion isr.
1974*4882a593Smuzhiyun * @irq: interrupt number
1975*4882a593Smuzhiyun * @_di: pointer to the ab8500_fg structure
1976*4882a593Smuzhiyun *
1977*4882a593Smuzhiyun * Returns IRQ status(IRQ_HANDLED)
1978*4882a593Smuzhiyun */
ab8500_fg_cc_data_end_handler(int irq,void * _di)1979*4882a593Smuzhiyun static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di)
1980*4882a593Smuzhiyun {
1981*4882a593Smuzhiyun struct ab8500_fg *di = _di;
1982*4882a593Smuzhiyun if (!di->nbr_cceoc_irq_cnt) {
1983*4882a593Smuzhiyun di->nbr_cceoc_irq_cnt++;
1984*4882a593Smuzhiyun complete(&di->ab8500_fg_started);
1985*4882a593Smuzhiyun } else {
1986*4882a593Smuzhiyun di->nbr_cceoc_irq_cnt = 0;
1987*4882a593Smuzhiyun complete(&di->ab8500_fg_complete);
1988*4882a593Smuzhiyun }
1989*4882a593Smuzhiyun return IRQ_HANDLED;
1990*4882a593Smuzhiyun }
1991*4882a593Smuzhiyun
1992*4882a593Smuzhiyun /**
1993*4882a593Smuzhiyun * ab8500_fg_cc_int_calib_handler () - end of calibration isr.
1994*4882a593Smuzhiyun * @irq: interrupt number
1995*4882a593Smuzhiyun * @_di: pointer to the ab8500_fg structure
1996*4882a593Smuzhiyun *
1997*4882a593Smuzhiyun * Returns IRQ status(IRQ_HANDLED)
1998*4882a593Smuzhiyun */
ab8500_fg_cc_int_calib_handler(int irq,void * _di)1999*4882a593Smuzhiyun static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di)
2000*4882a593Smuzhiyun {
2001*4882a593Smuzhiyun struct ab8500_fg *di = _di;
2002*4882a593Smuzhiyun di->calib_state = AB8500_FG_CALIB_END;
2003*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2004*4882a593Smuzhiyun return IRQ_HANDLED;
2005*4882a593Smuzhiyun }
2006*4882a593Smuzhiyun
2007*4882a593Smuzhiyun /**
2008*4882a593Smuzhiyun * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
2009*4882a593Smuzhiyun * @irq: interrupt number
2010*4882a593Smuzhiyun * @_di: pointer to the ab8500_fg structure
2011*4882a593Smuzhiyun *
2012*4882a593Smuzhiyun * Returns IRQ status(IRQ_HANDLED)
2013*4882a593Smuzhiyun */
ab8500_fg_cc_convend_handler(int irq,void * _di)2014*4882a593Smuzhiyun static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di)
2015*4882a593Smuzhiyun {
2016*4882a593Smuzhiyun struct ab8500_fg *di = _di;
2017*4882a593Smuzhiyun
2018*4882a593Smuzhiyun queue_work(di->fg_wq, &di->fg_acc_cur_work);
2019*4882a593Smuzhiyun
2020*4882a593Smuzhiyun return IRQ_HANDLED;
2021*4882a593Smuzhiyun }
2022*4882a593Smuzhiyun
2023*4882a593Smuzhiyun /**
2024*4882a593Smuzhiyun * ab8500_fg_batt_ovv_handler() - Battery OVV occured
2025*4882a593Smuzhiyun * @irq: interrupt number
2026*4882a593Smuzhiyun * @_di: pointer to the ab8500_fg structure
2027*4882a593Smuzhiyun *
2028*4882a593Smuzhiyun * Returns IRQ status(IRQ_HANDLED)
2029*4882a593Smuzhiyun */
ab8500_fg_batt_ovv_handler(int irq,void * _di)2030*4882a593Smuzhiyun static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di)
2031*4882a593Smuzhiyun {
2032*4882a593Smuzhiyun struct ab8500_fg *di = _di;
2033*4882a593Smuzhiyun
2034*4882a593Smuzhiyun dev_dbg(di->dev, "Battery OVV\n");
2035*4882a593Smuzhiyun
2036*4882a593Smuzhiyun /* Schedule a new HW failure check */
2037*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0);
2038*4882a593Smuzhiyun
2039*4882a593Smuzhiyun return IRQ_HANDLED;
2040*4882a593Smuzhiyun }
2041*4882a593Smuzhiyun
2042*4882a593Smuzhiyun /**
2043*4882a593Smuzhiyun * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
2044*4882a593Smuzhiyun * @irq: interrupt number
2045*4882a593Smuzhiyun * @_di: pointer to the ab8500_fg structure
2046*4882a593Smuzhiyun *
2047*4882a593Smuzhiyun * Returns IRQ status(IRQ_HANDLED)
2048*4882a593Smuzhiyun */
ab8500_fg_lowbatf_handler(int irq,void * _di)2049*4882a593Smuzhiyun static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di)
2050*4882a593Smuzhiyun {
2051*4882a593Smuzhiyun struct ab8500_fg *di = _di;
2052*4882a593Smuzhiyun
2053*4882a593Smuzhiyun /* Initiate handling in ab8500_fg_low_bat_work() if not already initiated. */
2054*4882a593Smuzhiyun if (!di->flags.low_bat_delay) {
2055*4882a593Smuzhiyun dev_warn(di->dev, "Battery voltage is below LOW threshold\n");
2056*4882a593Smuzhiyun di->flags.low_bat_delay = true;
2057*4882a593Smuzhiyun /*
2058*4882a593Smuzhiyun * Start a timer to check LOW_BAT again after some time
2059*4882a593Smuzhiyun * This is done to avoid shutdown on single voltage dips
2060*4882a593Smuzhiyun */
2061*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
2062*4882a593Smuzhiyun round_jiffies(LOW_BAT_CHECK_INTERVAL));
2063*4882a593Smuzhiyun }
2064*4882a593Smuzhiyun return IRQ_HANDLED;
2065*4882a593Smuzhiyun }
2066*4882a593Smuzhiyun
2067*4882a593Smuzhiyun /**
2068*4882a593Smuzhiyun * ab8500_fg_get_property() - get the fg properties
2069*4882a593Smuzhiyun * @psy: pointer to the power_supply structure
2070*4882a593Smuzhiyun * @psp: pointer to the power_supply_property structure
2071*4882a593Smuzhiyun * @val: pointer to the power_supply_propval union
2072*4882a593Smuzhiyun *
2073*4882a593Smuzhiyun * This function gets called when an application tries to get the
2074*4882a593Smuzhiyun * fg properties by reading the sysfs files.
2075*4882a593Smuzhiyun * voltage_now: battery voltage
2076*4882a593Smuzhiyun * current_now: battery instant current
2077*4882a593Smuzhiyun * current_avg: battery average current
2078*4882a593Smuzhiyun * charge_full_design: capacity where battery is considered full
2079*4882a593Smuzhiyun * charge_now: battery capacity in nAh
2080*4882a593Smuzhiyun * capacity: capacity in percent
2081*4882a593Smuzhiyun * capacity_level: capacity level
2082*4882a593Smuzhiyun *
2083*4882a593Smuzhiyun * Returns error code in case of failure else 0 on success
2084*4882a593Smuzhiyun */
ab8500_fg_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)2085*4882a593Smuzhiyun static int ab8500_fg_get_property(struct power_supply *psy,
2086*4882a593Smuzhiyun enum power_supply_property psp,
2087*4882a593Smuzhiyun union power_supply_propval *val)
2088*4882a593Smuzhiyun {
2089*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2090*4882a593Smuzhiyun
2091*4882a593Smuzhiyun /*
2092*4882a593Smuzhiyun * If battery is identified as unknown and charging of unknown
2093*4882a593Smuzhiyun * batteries is disabled, we always report 100% capacity and
2094*4882a593Smuzhiyun * capacity level UNKNOWN, since we can't calculate
2095*4882a593Smuzhiyun * remaining capacity
2096*4882a593Smuzhiyun */
2097*4882a593Smuzhiyun
2098*4882a593Smuzhiyun switch (psp) {
2099*4882a593Smuzhiyun case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2100*4882a593Smuzhiyun if (di->flags.bat_ovv)
2101*4882a593Smuzhiyun val->intval = BATT_OVV_VALUE * 1000;
2102*4882a593Smuzhiyun else
2103*4882a593Smuzhiyun val->intval = di->vbat * 1000;
2104*4882a593Smuzhiyun break;
2105*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CURRENT_NOW:
2106*4882a593Smuzhiyun val->intval = di->inst_curr * 1000;
2107*4882a593Smuzhiyun break;
2108*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CURRENT_AVG:
2109*4882a593Smuzhiyun val->intval = di->avg_curr * 1000;
2110*4882a593Smuzhiyun break;
2111*4882a593Smuzhiyun case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
2112*4882a593Smuzhiyun val->intval = ab8500_fg_convert_mah_to_uwh(di,
2113*4882a593Smuzhiyun di->bat_cap.max_mah_design);
2114*4882a593Smuzhiyun break;
2115*4882a593Smuzhiyun case POWER_SUPPLY_PROP_ENERGY_FULL:
2116*4882a593Smuzhiyun val->intval = ab8500_fg_convert_mah_to_uwh(di,
2117*4882a593Smuzhiyun di->bat_cap.max_mah);
2118*4882a593Smuzhiyun break;
2119*4882a593Smuzhiyun case POWER_SUPPLY_PROP_ENERGY_NOW:
2120*4882a593Smuzhiyun if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2121*4882a593Smuzhiyun di->flags.batt_id_received)
2122*4882a593Smuzhiyun val->intval = ab8500_fg_convert_mah_to_uwh(di,
2123*4882a593Smuzhiyun di->bat_cap.max_mah);
2124*4882a593Smuzhiyun else
2125*4882a593Smuzhiyun val->intval = ab8500_fg_convert_mah_to_uwh(di,
2126*4882a593Smuzhiyun di->bat_cap.prev_mah);
2127*4882a593Smuzhiyun break;
2128*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
2129*4882a593Smuzhiyun val->intval = di->bat_cap.max_mah_design;
2130*4882a593Smuzhiyun break;
2131*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_FULL:
2132*4882a593Smuzhiyun val->intval = di->bat_cap.max_mah;
2133*4882a593Smuzhiyun break;
2134*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_NOW:
2135*4882a593Smuzhiyun if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2136*4882a593Smuzhiyun di->flags.batt_id_received)
2137*4882a593Smuzhiyun val->intval = di->bat_cap.max_mah;
2138*4882a593Smuzhiyun else
2139*4882a593Smuzhiyun val->intval = di->bat_cap.prev_mah;
2140*4882a593Smuzhiyun break;
2141*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CAPACITY:
2142*4882a593Smuzhiyun if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2143*4882a593Smuzhiyun di->flags.batt_id_received)
2144*4882a593Smuzhiyun val->intval = 100;
2145*4882a593Smuzhiyun else
2146*4882a593Smuzhiyun val->intval = di->bat_cap.prev_percent;
2147*4882a593Smuzhiyun break;
2148*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
2149*4882a593Smuzhiyun if (di->flags.batt_unknown && !di->bm->chg_unknown_bat &&
2150*4882a593Smuzhiyun di->flags.batt_id_received)
2151*4882a593Smuzhiyun val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
2152*4882a593Smuzhiyun else
2153*4882a593Smuzhiyun val->intval = di->bat_cap.prev_level;
2154*4882a593Smuzhiyun break;
2155*4882a593Smuzhiyun default:
2156*4882a593Smuzhiyun return -EINVAL;
2157*4882a593Smuzhiyun }
2158*4882a593Smuzhiyun return 0;
2159*4882a593Smuzhiyun }
2160*4882a593Smuzhiyun
ab8500_fg_get_ext_psy_data(struct device * dev,void * data)2161*4882a593Smuzhiyun static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
2162*4882a593Smuzhiyun {
2163*4882a593Smuzhiyun struct power_supply *psy;
2164*4882a593Smuzhiyun struct power_supply *ext = dev_get_drvdata(dev);
2165*4882a593Smuzhiyun const char **supplicants = (const char **)ext->supplied_to;
2166*4882a593Smuzhiyun struct ab8500_fg *di;
2167*4882a593Smuzhiyun union power_supply_propval ret;
2168*4882a593Smuzhiyun int j;
2169*4882a593Smuzhiyun
2170*4882a593Smuzhiyun psy = (struct power_supply *)data;
2171*4882a593Smuzhiyun di = power_supply_get_drvdata(psy);
2172*4882a593Smuzhiyun
2173*4882a593Smuzhiyun /*
2174*4882a593Smuzhiyun * For all psy where the name of your driver
2175*4882a593Smuzhiyun * appears in any supplied_to
2176*4882a593Smuzhiyun */
2177*4882a593Smuzhiyun j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
2178*4882a593Smuzhiyun if (j < 0)
2179*4882a593Smuzhiyun return 0;
2180*4882a593Smuzhiyun
2181*4882a593Smuzhiyun /* Go through all properties for the psy */
2182*4882a593Smuzhiyun for (j = 0; j < ext->desc->num_properties; j++) {
2183*4882a593Smuzhiyun enum power_supply_property prop;
2184*4882a593Smuzhiyun prop = ext->desc->properties[j];
2185*4882a593Smuzhiyun
2186*4882a593Smuzhiyun if (power_supply_get_property(ext, prop, &ret))
2187*4882a593Smuzhiyun continue;
2188*4882a593Smuzhiyun
2189*4882a593Smuzhiyun switch (prop) {
2190*4882a593Smuzhiyun case POWER_SUPPLY_PROP_STATUS:
2191*4882a593Smuzhiyun switch (ext->desc->type) {
2192*4882a593Smuzhiyun case POWER_SUPPLY_TYPE_BATTERY:
2193*4882a593Smuzhiyun switch (ret.intval) {
2194*4882a593Smuzhiyun case POWER_SUPPLY_STATUS_UNKNOWN:
2195*4882a593Smuzhiyun case POWER_SUPPLY_STATUS_DISCHARGING:
2196*4882a593Smuzhiyun case POWER_SUPPLY_STATUS_NOT_CHARGING:
2197*4882a593Smuzhiyun if (!di->flags.charging)
2198*4882a593Smuzhiyun break;
2199*4882a593Smuzhiyun di->flags.charging = false;
2200*4882a593Smuzhiyun di->flags.fully_charged = false;
2201*4882a593Smuzhiyun if (di->bm->capacity_scaling)
2202*4882a593Smuzhiyun ab8500_fg_update_cap_scalers(di);
2203*4882a593Smuzhiyun queue_work(di->fg_wq, &di->fg_work);
2204*4882a593Smuzhiyun break;
2205*4882a593Smuzhiyun case POWER_SUPPLY_STATUS_FULL:
2206*4882a593Smuzhiyun if (di->flags.fully_charged)
2207*4882a593Smuzhiyun break;
2208*4882a593Smuzhiyun di->flags.fully_charged = true;
2209*4882a593Smuzhiyun di->flags.force_full = true;
2210*4882a593Smuzhiyun /* Save current capacity as maximum */
2211*4882a593Smuzhiyun di->bat_cap.max_mah = di->bat_cap.mah;
2212*4882a593Smuzhiyun queue_work(di->fg_wq, &di->fg_work);
2213*4882a593Smuzhiyun break;
2214*4882a593Smuzhiyun case POWER_SUPPLY_STATUS_CHARGING:
2215*4882a593Smuzhiyun if (di->flags.charging &&
2216*4882a593Smuzhiyun !di->flags.fully_charged)
2217*4882a593Smuzhiyun break;
2218*4882a593Smuzhiyun di->flags.charging = true;
2219*4882a593Smuzhiyun di->flags.fully_charged = false;
2220*4882a593Smuzhiyun if (di->bm->capacity_scaling)
2221*4882a593Smuzhiyun ab8500_fg_update_cap_scalers(di);
2222*4882a593Smuzhiyun queue_work(di->fg_wq, &di->fg_work);
2223*4882a593Smuzhiyun break;
2224*4882a593Smuzhiyun }
2225*4882a593Smuzhiyun default:
2226*4882a593Smuzhiyun break;
2227*4882a593Smuzhiyun }
2228*4882a593Smuzhiyun break;
2229*4882a593Smuzhiyun case POWER_SUPPLY_PROP_TECHNOLOGY:
2230*4882a593Smuzhiyun switch (ext->desc->type) {
2231*4882a593Smuzhiyun case POWER_SUPPLY_TYPE_BATTERY:
2232*4882a593Smuzhiyun if (!di->flags.batt_id_received &&
2233*4882a593Smuzhiyun di->bm->batt_id != BATTERY_UNKNOWN) {
2234*4882a593Smuzhiyun const struct abx500_battery_type *b;
2235*4882a593Smuzhiyun
2236*4882a593Smuzhiyun b = &(di->bm->bat_type[di->bm->batt_id]);
2237*4882a593Smuzhiyun
2238*4882a593Smuzhiyun di->flags.batt_id_received = true;
2239*4882a593Smuzhiyun
2240*4882a593Smuzhiyun di->bat_cap.max_mah_design =
2241*4882a593Smuzhiyun MILLI_TO_MICRO *
2242*4882a593Smuzhiyun b->charge_full_design;
2243*4882a593Smuzhiyun
2244*4882a593Smuzhiyun di->bat_cap.max_mah =
2245*4882a593Smuzhiyun di->bat_cap.max_mah_design;
2246*4882a593Smuzhiyun
2247*4882a593Smuzhiyun di->vbat_nom = b->nominal_voltage;
2248*4882a593Smuzhiyun }
2249*4882a593Smuzhiyun
2250*4882a593Smuzhiyun if (ret.intval)
2251*4882a593Smuzhiyun di->flags.batt_unknown = false;
2252*4882a593Smuzhiyun else
2253*4882a593Smuzhiyun di->flags.batt_unknown = true;
2254*4882a593Smuzhiyun break;
2255*4882a593Smuzhiyun default:
2256*4882a593Smuzhiyun break;
2257*4882a593Smuzhiyun }
2258*4882a593Smuzhiyun break;
2259*4882a593Smuzhiyun case POWER_SUPPLY_PROP_TEMP:
2260*4882a593Smuzhiyun switch (ext->desc->type) {
2261*4882a593Smuzhiyun case POWER_SUPPLY_TYPE_BATTERY:
2262*4882a593Smuzhiyun if (di->flags.batt_id_received)
2263*4882a593Smuzhiyun di->bat_temp = ret.intval;
2264*4882a593Smuzhiyun break;
2265*4882a593Smuzhiyun default:
2266*4882a593Smuzhiyun break;
2267*4882a593Smuzhiyun }
2268*4882a593Smuzhiyun break;
2269*4882a593Smuzhiyun default:
2270*4882a593Smuzhiyun break;
2271*4882a593Smuzhiyun }
2272*4882a593Smuzhiyun }
2273*4882a593Smuzhiyun return 0;
2274*4882a593Smuzhiyun }
2275*4882a593Smuzhiyun
2276*4882a593Smuzhiyun /**
2277*4882a593Smuzhiyun * ab8500_fg_init_hw_registers() - Set up FG related registers
2278*4882a593Smuzhiyun * @di: pointer to the ab8500_fg structure
2279*4882a593Smuzhiyun *
2280*4882a593Smuzhiyun * Set up battery OVV, low battery voltage registers
2281*4882a593Smuzhiyun */
ab8500_fg_init_hw_registers(struct ab8500_fg * di)2282*4882a593Smuzhiyun static int ab8500_fg_init_hw_registers(struct ab8500_fg *di)
2283*4882a593Smuzhiyun {
2284*4882a593Smuzhiyun int ret;
2285*4882a593Smuzhiyun
2286*4882a593Smuzhiyun /* Set VBAT OVV threshold */
2287*4882a593Smuzhiyun ret = abx500_mask_and_set_register_interruptible(di->dev,
2288*4882a593Smuzhiyun AB8500_CHARGER,
2289*4882a593Smuzhiyun AB8500_BATT_OVV,
2290*4882a593Smuzhiyun BATT_OVV_TH_4P75,
2291*4882a593Smuzhiyun BATT_OVV_TH_4P75);
2292*4882a593Smuzhiyun if (ret) {
2293*4882a593Smuzhiyun dev_err(di->dev, "failed to set BATT_OVV\n");
2294*4882a593Smuzhiyun goto out;
2295*4882a593Smuzhiyun }
2296*4882a593Smuzhiyun
2297*4882a593Smuzhiyun /* Enable VBAT OVV detection */
2298*4882a593Smuzhiyun ret = abx500_mask_and_set_register_interruptible(di->dev,
2299*4882a593Smuzhiyun AB8500_CHARGER,
2300*4882a593Smuzhiyun AB8500_BATT_OVV,
2301*4882a593Smuzhiyun BATT_OVV_ENA,
2302*4882a593Smuzhiyun BATT_OVV_ENA);
2303*4882a593Smuzhiyun if (ret) {
2304*4882a593Smuzhiyun dev_err(di->dev, "failed to enable BATT_OVV\n");
2305*4882a593Smuzhiyun goto out;
2306*4882a593Smuzhiyun }
2307*4882a593Smuzhiyun
2308*4882a593Smuzhiyun /* Low Battery Voltage */
2309*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev,
2310*4882a593Smuzhiyun AB8500_SYS_CTRL2_BLOCK,
2311*4882a593Smuzhiyun AB8500_LOW_BAT_REG,
2312*4882a593Smuzhiyun ab8500_volt_to_regval(
2313*4882a593Smuzhiyun di->bm->fg_params->lowbat_threshold) << 1 |
2314*4882a593Smuzhiyun LOW_BAT_ENABLE);
2315*4882a593Smuzhiyun if (ret) {
2316*4882a593Smuzhiyun dev_err(di->dev, "%s write failed\n", __func__);
2317*4882a593Smuzhiyun goto out;
2318*4882a593Smuzhiyun }
2319*4882a593Smuzhiyun
2320*4882a593Smuzhiyun /* Battery OK threshold */
2321*4882a593Smuzhiyun ret = ab8500_fg_battok_init_hw_register(di);
2322*4882a593Smuzhiyun if (ret) {
2323*4882a593Smuzhiyun dev_err(di->dev, "BattOk init write failed.\n");
2324*4882a593Smuzhiyun goto out;
2325*4882a593Smuzhiyun }
2326*4882a593Smuzhiyun
2327*4882a593Smuzhiyun if (is_ab8505(di->parent)) {
2328*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2329*4882a593Smuzhiyun AB8505_RTC_PCUT_MAX_TIME_REG, di->bm->fg_params->pcut_max_time);
2330*4882a593Smuzhiyun
2331*4882a593Smuzhiyun if (ret) {
2332*4882a593Smuzhiyun dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_MAX_TIME_REG\n", __func__);
2333*4882a593Smuzhiyun goto out;
2334*4882a593Smuzhiyun }
2335*4882a593Smuzhiyun
2336*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2337*4882a593Smuzhiyun AB8505_RTC_PCUT_FLAG_TIME_REG, di->bm->fg_params->pcut_flag_time);
2338*4882a593Smuzhiyun
2339*4882a593Smuzhiyun if (ret) {
2340*4882a593Smuzhiyun dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_FLAG_TIME_REG\n", __func__);
2341*4882a593Smuzhiyun goto out;
2342*4882a593Smuzhiyun }
2343*4882a593Smuzhiyun
2344*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2345*4882a593Smuzhiyun AB8505_RTC_PCUT_RESTART_REG, di->bm->fg_params->pcut_max_restart);
2346*4882a593Smuzhiyun
2347*4882a593Smuzhiyun if (ret) {
2348*4882a593Smuzhiyun dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_RESTART_REG\n", __func__);
2349*4882a593Smuzhiyun goto out;
2350*4882a593Smuzhiyun }
2351*4882a593Smuzhiyun
2352*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2353*4882a593Smuzhiyun AB8505_RTC_PCUT_DEBOUNCE_REG, di->bm->fg_params->pcut_debounce_time);
2354*4882a593Smuzhiyun
2355*4882a593Smuzhiyun if (ret) {
2356*4882a593Smuzhiyun dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_DEBOUNCE_REG\n", __func__);
2357*4882a593Smuzhiyun goto out;
2358*4882a593Smuzhiyun }
2359*4882a593Smuzhiyun
2360*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2361*4882a593Smuzhiyun AB8505_RTC_PCUT_CTL_STATUS_REG, di->bm->fg_params->pcut_enable);
2362*4882a593Smuzhiyun
2363*4882a593Smuzhiyun if (ret) {
2364*4882a593Smuzhiyun dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_CTL_STATUS_REG\n", __func__);
2365*4882a593Smuzhiyun goto out;
2366*4882a593Smuzhiyun }
2367*4882a593Smuzhiyun }
2368*4882a593Smuzhiyun out:
2369*4882a593Smuzhiyun return ret;
2370*4882a593Smuzhiyun }
2371*4882a593Smuzhiyun
2372*4882a593Smuzhiyun /**
2373*4882a593Smuzhiyun * ab8500_fg_external_power_changed() - callback for power supply changes
2374*4882a593Smuzhiyun * @psy: pointer to the structure power_supply
2375*4882a593Smuzhiyun *
2376*4882a593Smuzhiyun * This function is the entry point of the pointer external_power_changed
2377*4882a593Smuzhiyun * of the structure power_supply.
2378*4882a593Smuzhiyun * This function gets executed when there is a change in any external power
2379*4882a593Smuzhiyun * supply that this driver needs to be notified of.
2380*4882a593Smuzhiyun */
ab8500_fg_external_power_changed(struct power_supply * psy)2381*4882a593Smuzhiyun static void ab8500_fg_external_power_changed(struct power_supply *psy)
2382*4882a593Smuzhiyun {
2383*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2384*4882a593Smuzhiyun
2385*4882a593Smuzhiyun class_for_each_device(power_supply_class, NULL,
2386*4882a593Smuzhiyun di->fg_psy, ab8500_fg_get_ext_psy_data);
2387*4882a593Smuzhiyun }
2388*4882a593Smuzhiyun
2389*4882a593Smuzhiyun /**
2390*4882a593Smuzhiyun * ab8500_fg_reinit_work() - work to reset the FG algorithm
2391*4882a593Smuzhiyun * @work: pointer to the work_struct structure
2392*4882a593Smuzhiyun *
2393*4882a593Smuzhiyun * Used to reset the current battery capacity to be able to
2394*4882a593Smuzhiyun * retrigger a new voltage base capacity calculation. For
2395*4882a593Smuzhiyun * test and verification purpose.
2396*4882a593Smuzhiyun */
ab8500_fg_reinit_work(struct work_struct * work)2397*4882a593Smuzhiyun static void ab8500_fg_reinit_work(struct work_struct *work)
2398*4882a593Smuzhiyun {
2399*4882a593Smuzhiyun struct ab8500_fg *di = container_of(work, struct ab8500_fg,
2400*4882a593Smuzhiyun fg_reinit_work.work);
2401*4882a593Smuzhiyun
2402*4882a593Smuzhiyun if (!di->flags.calibrate) {
2403*4882a593Smuzhiyun dev_dbg(di->dev, "Resetting FG state machine to init.\n");
2404*4882a593Smuzhiyun ab8500_fg_clear_cap_samples(di);
2405*4882a593Smuzhiyun ab8500_fg_calc_cap_discharge_voltage(di, true);
2406*4882a593Smuzhiyun ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
2407*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
2408*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2409*4882a593Smuzhiyun
2410*4882a593Smuzhiyun } else {
2411*4882a593Smuzhiyun dev_err(di->dev, "Residual offset calibration ongoing "
2412*4882a593Smuzhiyun "retrying..\n");
2413*4882a593Smuzhiyun /* Wait one second until next try*/
2414*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_reinit_work,
2415*4882a593Smuzhiyun round_jiffies(1));
2416*4882a593Smuzhiyun }
2417*4882a593Smuzhiyun }
2418*4882a593Smuzhiyun
2419*4882a593Smuzhiyun /* Exposure to the sysfs interface */
2420*4882a593Smuzhiyun
2421*4882a593Smuzhiyun struct ab8500_fg_sysfs_entry {
2422*4882a593Smuzhiyun struct attribute attr;
2423*4882a593Smuzhiyun ssize_t (*show)(struct ab8500_fg *, char *);
2424*4882a593Smuzhiyun ssize_t (*store)(struct ab8500_fg *, const char *, size_t);
2425*4882a593Smuzhiyun };
2426*4882a593Smuzhiyun
charge_full_show(struct ab8500_fg * di,char * buf)2427*4882a593Smuzhiyun static ssize_t charge_full_show(struct ab8500_fg *di, char *buf)
2428*4882a593Smuzhiyun {
2429*4882a593Smuzhiyun return sprintf(buf, "%d\n", di->bat_cap.max_mah);
2430*4882a593Smuzhiyun }
2431*4882a593Smuzhiyun
charge_full_store(struct ab8500_fg * di,const char * buf,size_t count)2432*4882a593Smuzhiyun static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf,
2433*4882a593Smuzhiyun size_t count)
2434*4882a593Smuzhiyun {
2435*4882a593Smuzhiyun unsigned long charge_full;
2436*4882a593Smuzhiyun int ret;
2437*4882a593Smuzhiyun
2438*4882a593Smuzhiyun ret = kstrtoul(buf, 10, &charge_full);
2439*4882a593Smuzhiyun if (ret)
2440*4882a593Smuzhiyun return ret;
2441*4882a593Smuzhiyun
2442*4882a593Smuzhiyun di->bat_cap.max_mah = (int) charge_full;
2443*4882a593Smuzhiyun return count;
2444*4882a593Smuzhiyun }
2445*4882a593Smuzhiyun
charge_now_show(struct ab8500_fg * di,char * buf)2446*4882a593Smuzhiyun static ssize_t charge_now_show(struct ab8500_fg *di, char *buf)
2447*4882a593Smuzhiyun {
2448*4882a593Smuzhiyun return sprintf(buf, "%d\n", di->bat_cap.prev_mah);
2449*4882a593Smuzhiyun }
2450*4882a593Smuzhiyun
charge_now_store(struct ab8500_fg * di,const char * buf,size_t count)2451*4882a593Smuzhiyun static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf,
2452*4882a593Smuzhiyun size_t count)
2453*4882a593Smuzhiyun {
2454*4882a593Smuzhiyun unsigned long charge_now;
2455*4882a593Smuzhiyun int ret;
2456*4882a593Smuzhiyun
2457*4882a593Smuzhiyun ret = kstrtoul(buf, 10, &charge_now);
2458*4882a593Smuzhiyun if (ret)
2459*4882a593Smuzhiyun return ret;
2460*4882a593Smuzhiyun
2461*4882a593Smuzhiyun di->bat_cap.user_mah = (int) charge_now;
2462*4882a593Smuzhiyun di->flags.user_cap = true;
2463*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2464*4882a593Smuzhiyun return count;
2465*4882a593Smuzhiyun }
2466*4882a593Smuzhiyun
2467*4882a593Smuzhiyun static struct ab8500_fg_sysfs_entry charge_full_attr =
2468*4882a593Smuzhiyun __ATTR(charge_full, 0644, charge_full_show, charge_full_store);
2469*4882a593Smuzhiyun
2470*4882a593Smuzhiyun static struct ab8500_fg_sysfs_entry charge_now_attr =
2471*4882a593Smuzhiyun __ATTR(charge_now, 0644, charge_now_show, charge_now_store);
2472*4882a593Smuzhiyun
2473*4882a593Smuzhiyun static ssize_t
ab8500_fg_show(struct kobject * kobj,struct attribute * attr,char * buf)2474*4882a593Smuzhiyun ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf)
2475*4882a593Smuzhiyun {
2476*4882a593Smuzhiyun struct ab8500_fg_sysfs_entry *entry;
2477*4882a593Smuzhiyun struct ab8500_fg *di;
2478*4882a593Smuzhiyun
2479*4882a593Smuzhiyun entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2480*4882a593Smuzhiyun di = container_of(kobj, struct ab8500_fg, fg_kobject);
2481*4882a593Smuzhiyun
2482*4882a593Smuzhiyun if (!entry->show)
2483*4882a593Smuzhiyun return -EIO;
2484*4882a593Smuzhiyun
2485*4882a593Smuzhiyun return entry->show(di, buf);
2486*4882a593Smuzhiyun }
2487*4882a593Smuzhiyun static ssize_t
ab8500_fg_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)2488*4882a593Smuzhiyun ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf,
2489*4882a593Smuzhiyun size_t count)
2490*4882a593Smuzhiyun {
2491*4882a593Smuzhiyun struct ab8500_fg_sysfs_entry *entry;
2492*4882a593Smuzhiyun struct ab8500_fg *di;
2493*4882a593Smuzhiyun
2494*4882a593Smuzhiyun entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2495*4882a593Smuzhiyun di = container_of(kobj, struct ab8500_fg, fg_kobject);
2496*4882a593Smuzhiyun
2497*4882a593Smuzhiyun if (!entry->store)
2498*4882a593Smuzhiyun return -EIO;
2499*4882a593Smuzhiyun
2500*4882a593Smuzhiyun return entry->store(di, buf, count);
2501*4882a593Smuzhiyun }
2502*4882a593Smuzhiyun
2503*4882a593Smuzhiyun static const struct sysfs_ops ab8500_fg_sysfs_ops = {
2504*4882a593Smuzhiyun .show = ab8500_fg_show,
2505*4882a593Smuzhiyun .store = ab8500_fg_store,
2506*4882a593Smuzhiyun };
2507*4882a593Smuzhiyun
2508*4882a593Smuzhiyun static struct attribute *ab8500_fg_attrs[] = {
2509*4882a593Smuzhiyun &charge_full_attr.attr,
2510*4882a593Smuzhiyun &charge_now_attr.attr,
2511*4882a593Smuzhiyun NULL,
2512*4882a593Smuzhiyun };
2513*4882a593Smuzhiyun
2514*4882a593Smuzhiyun static struct kobj_type ab8500_fg_ktype = {
2515*4882a593Smuzhiyun .sysfs_ops = &ab8500_fg_sysfs_ops,
2516*4882a593Smuzhiyun .default_attrs = ab8500_fg_attrs,
2517*4882a593Smuzhiyun };
2518*4882a593Smuzhiyun
2519*4882a593Smuzhiyun /**
2520*4882a593Smuzhiyun * ab8500_fg_sysfs_exit() - de-init of sysfs entry
2521*4882a593Smuzhiyun * @di: pointer to the struct ab8500_chargalg
2522*4882a593Smuzhiyun *
2523*4882a593Smuzhiyun * This function removes the entry in sysfs.
2524*4882a593Smuzhiyun */
ab8500_fg_sysfs_exit(struct ab8500_fg * di)2525*4882a593Smuzhiyun static void ab8500_fg_sysfs_exit(struct ab8500_fg *di)
2526*4882a593Smuzhiyun {
2527*4882a593Smuzhiyun kobject_del(&di->fg_kobject);
2528*4882a593Smuzhiyun }
2529*4882a593Smuzhiyun
2530*4882a593Smuzhiyun /**
2531*4882a593Smuzhiyun * ab8500_fg_sysfs_init() - init of sysfs entry
2532*4882a593Smuzhiyun * @di: pointer to the struct ab8500_chargalg
2533*4882a593Smuzhiyun *
2534*4882a593Smuzhiyun * This function adds an entry in sysfs.
2535*4882a593Smuzhiyun * Returns error code in case of failure else 0(on success)
2536*4882a593Smuzhiyun */
ab8500_fg_sysfs_init(struct ab8500_fg * di)2537*4882a593Smuzhiyun static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
2538*4882a593Smuzhiyun {
2539*4882a593Smuzhiyun int ret = 0;
2540*4882a593Smuzhiyun
2541*4882a593Smuzhiyun ret = kobject_init_and_add(&di->fg_kobject,
2542*4882a593Smuzhiyun &ab8500_fg_ktype,
2543*4882a593Smuzhiyun NULL, "battery");
2544*4882a593Smuzhiyun if (ret < 0) {
2545*4882a593Smuzhiyun kobject_put(&di->fg_kobject);
2546*4882a593Smuzhiyun dev_err(di->dev, "failed to create sysfs entry\n");
2547*4882a593Smuzhiyun }
2548*4882a593Smuzhiyun
2549*4882a593Smuzhiyun return ret;
2550*4882a593Smuzhiyun }
2551*4882a593Smuzhiyun
ab8505_powercut_flagtime_read(struct device * dev,struct device_attribute * attr,char * buf)2552*4882a593Smuzhiyun static ssize_t ab8505_powercut_flagtime_read(struct device *dev,
2553*4882a593Smuzhiyun struct device_attribute *attr,
2554*4882a593Smuzhiyun char *buf)
2555*4882a593Smuzhiyun {
2556*4882a593Smuzhiyun int ret;
2557*4882a593Smuzhiyun u8 reg_value;
2558*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2559*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2560*4882a593Smuzhiyun
2561*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2562*4882a593Smuzhiyun AB8505_RTC_PCUT_FLAG_TIME_REG, ®_value);
2563*4882a593Smuzhiyun
2564*4882a593Smuzhiyun if (ret < 0) {
2565*4882a593Smuzhiyun dev_err(dev, "Failed to read AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2566*4882a593Smuzhiyun goto fail;
2567*4882a593Smuzhiyun }
2568*4882a593Smuzhiyun
2569*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
2570*4882a593Smuzhiyun
2571*4882a593Smuzhiyun fail:
2572*4882a593Smuzhiyun return ret;
2573*4882a593Smuzhiyun }
2574*4882a593Smuzhiyun
ab8505_powercut_flagtime_write(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2575*4882a593Smuzhiyun static ssize_t ab8505_powercut_flagtime_write(struct device *dev,
2576*4882a593Smuzhiyun struct device_attribute *attr,
2577*4882a593Smuzhiyun const char *buf, size_t count)
2578*4882a593Smuzhiyun {
2579*4882a593Smuzhiyun int ret;
2580*4882a593Smuzhiyun int reg_value;
2581*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2582*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2583*4882a593Smuzhiyun
2584*4882a593Smuzhiyun if (kstrtoint(buf, 10, ®_value))
2585*4882a593Smuzhiyun goto fail;
2586*4882a593Smuzhiyun
2587*4882a593Smuzhiyun if (reg_value > 0x7F) {
2588*4882a593Smuzhiyun dev_err(dev, "Incorrect parameter, echo 0 (1.98s) - 127 (15.625ms) for flagtime\n");
2589*4882a593Smuzhiyun goto fail;
2590*4882a593Smuzhiyun }
2591*4882a593Smuzhiyun
2592*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2593*4882a593Smuzhiyun AB8505_RTC_PCUT_FLAG_TIME_REG, (u8)reg_value);
2594*4882a593Smuzhiyun
2595*4882a593Smuzhiyun if (ret < 0)
2596*4882a593Smuzhiyun dev_err(dev, "Failed to set AB8505_RTC_PCUT_FLAG_TIME_REG\n");
2597*4882a593Smuzhiyun
2598*4882a593Smuzhiyun fail:
2599*4882a593Smuzhiyun return count;
2600*4882a593Smuzhiyun }
2601*4882a593Smuzhiyun
ab8505_powercut_maxtime_read(struct device * dev,struct device_attribute * attr,char * buf)2602*4882a593Smuzhiyun static ssize_t ab8505_powercut_maxtime_read(struct device *dev,
2603*4882a593Smuzhiyun struct device_attribute *attr,
2604*4882a593Smuzhiyun char *buf)
2605*4882a593Smuzhiyun {
2606*4882a593Smuzhiyun int ret;
2607*4882a593Smuzhiyun u8 reg_value;
2608*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2609*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2610*4882a593Smuzhiyun
2611*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2612*4882a593Smuzhiyun AB8505_RTC_PCUT_MAX_TIME_REG, ®_value);
2613*4882a593Smuzhiyun
2614*4882a593Smuzhiyun if (ret < 0) {
2615*4882a593Smuzhiyun dev_err(dev, "Failed to read AB8505_RTC_PCUT_MAX_TIME_REG\n");
2616*4882a593Smuzhiyun goto fail;
2617*4882a593Smuzhiyun }
2618*4882a593Smuzhiyun
2619*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
2620*4882a593Smuzhiyun
2621*4882a593Smuzhiyun fail:
2622*4882a593Smuzhiyun return ret;
2623*4882a593Smuzhiyun
2624*4882a593Smuzhiyun }
2625*4882a593Smuzhiyun
ab8505_powercut_maxtime_write(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2626*4882a593Smuzhiyun static ssize_t ab8505_powercut_maxtime_write(struct device *dev,
2627*4882a593Smuzhiyun struct device_attribute *attr,
2628*4882a593Smuzhiyun const char *buf, size_t count)
2629*4882a593Smuzhiyun {
2630*4882a593Smuzhiyun int ret;
2631*4882a593Smuzhiyun int reg_value;
2632*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2633*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2634*4882a593Smuzhiyun
2635*4882a593Smuzhiyun if (kstrtoint(buf, 10, ®_value))
2636*4882a593Smuzhiyun goto fail;
2637*4882a593Smuzhiyun
2638*4882a593Smuzhiyun if (reg_value > 0x7F) {
2639*4882a593Smuzhiyun dev_err(dev, "Incorrect parameter, echo 0 (0.0s) - 127 (1.98s) for maxtime\n");
2640*4882a593Smuzhiyun goto fail;
2641*4882a593Smuzhiyun }
2642*4882a593Smuzhiyun
2643*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2644*4882a593Smuzhiyun AB8505_RTC_PCUT_MAX_TIME_REG, (u8)reg_value);
2645*4882a593Smuzhiyun
2646*4882a593Smuzhiyun if (ret < 0)
2647*4882a593Smuzhiyun dev_err(dev, "Failed to set AB8505_RTC_PCUT_MAX_TIME_REG\n");
2648*4882a593Smuzhiyun
2649*4882a593Smuzhiyun fail:
2650*4882a593Smuzhiyun return count;
2651*4882a593Smuzhiyun }
2652*4882a593Smuzhiyun
ab8505_powercut_restart_read(struct device * dev,struct device_attribute * attr,char * buf)2653*4882a593Smuzhiyun static ssize_t ab8505_powercut_restart_read(struct device *dev,
2654*4882a593Smuzhiyun struct device_attribute *attr,
2655*4882a593Smuzhiyun char *buf)
2656*4882a593Smuzhiyun {
2657*4882a593Smuzhiyun int ret;
2658*4882a593Smuzhiyun u8 reg_value;
2659*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2660*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2661*4882a593Smuzhiyun
2662*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2663*4882a593Smuzhiyun AB8505_RTC_PCUT_RESTART_REG, ®_value);
2664*4882a593Smuzhiyun
2665*4882a593Smuzhiyun if (ret < 0) {
2666*4882a593Smuzhiyun dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2667*4882a593Smuzhiyun goto fail;
2668*4882a593Smuzhiyun }
2669*4882a593Smuzhiyun
2670*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0xF));
2671*4882a593Smuzhiyun
2672*4882a593Smuzhiyun fail:
2673*4882a593Smuzhiyun return ret;
2674*4882a593Smuzhiyun }
2675*4882a593Smuzhiyun
ab8505_powercut_restart_write(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2676*4882a593Smuzhiyun static ssize_t ab8505_powercut_restart_write(struct device *dev,
2677*4882a593Smuzhiyun struct device_attribute *attr,
2678*4882a593Smuzhiyun const char *buf, size_t count)
2679*4882a593Smuzhiyun {
2680*4882a593Smuzhiyun int ret;
2681*4882a593Smuzhiyun int reg_value;
2682*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2683*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2684*4882a593Smuzhiyun
2685*4882a593Smuzhiyun if (kstrtoint(buf, 10, ®_value))
2686*4882a593Smuzhiyun goto fail;
2687*4882a593Smuzhiyun
2688*4882a593Smuzhiyun if (reg_value > 0xF) {
2689*4882a593Smuzhiyun dev_err(dev, "Incorrect parameter, echo 0 - 15 for number of restart\n");
2690*4882a593Smuzhiyun goto fail;
2691*4882a593Smuzhiyun }
2692*4882a593Smuzhiyun
2693*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2694*4882a593Smuzhiyun AB8505_RTC_PCUT_RESTART_REG, (u8)reg_value);
2695*4882a593Smuzhiyun
2696*4882a593Smuzhiyun if (ret < 0)
2697*4882a593Smuzhiyun dev_err(dev, "Failed to set AB8505_RTC_PCUT_RESTART_REG\n");
2698*4882a593Smuzhiyun
2699*4882a593Smuzhiyun fail:
2700*4882a593Smuzhiyun return count;
2701*4882a593Smuzhiyun
2702*4882a593Smuzhiyun }
2703*4882a593Smuzhiyun
ab8505_powercut_timer_read(struct device * dev,struct device_attribute * attr,char * buf)2704*4882a593Smuzhiyun static ssize_t ab8505_powercut_timer_read(struct device *dev,
2705*4882a593Smuzhiyun struct device_attribute *attr,
2706*4882a593Smuzhiyun char *buf)
2707*4882a593Smuzhiyun {
2708*4882a593Smuzhiyun int ret;
2709*4882a593Smuzhiyun u8 reg_value;
2710*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2711*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2712*4882a593Smuzhiyun
2713*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2714*4882a593Smuzhiyun AB8505_RTC_PCUT_TIME_REG, ®_value);
2715*4882a593Smuzhiyun
2716*4882a593Smuzhiyun if (ret < 0) {
2717*4882a593Smuzhiyun dev_err(dev, "Failed to read AB8505_RTC_PCUT_TIME_REG\n");
2718*4882a593Smuzhiyun goto fail;
2719*4882a593Smuzhiyun }
2720*4882a593Smuzhiyun
2721*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7F));
2722*4882a593Smuzhiyun
2723*4882a593Smuzhiyun fail:
2724*4882a593Smuzhiyun return ret;
2725*4882a593Smuzhiyun }
2726*4882a593Smuzhiyun
ab8505_powercut_restart_counter_read(struct device * dev,struct device_attribute * attr,char * buf)2727*4882a593Smuzhiyun static ssize_t ab8505_powercut_restart_counter_read(struct device *dev,
2728*4882a593Smuzhiyun struct device_attribute *attr,
2729*4882a593Smuzhiyun char *buf)
2730*4882a593Smuzhiyun {
2731*4882a593Smuzhiyun int ret;
2732*4882a593Smuzhiyun u8 reg_value;
2733*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2734*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2735*4882a593Smuzhiyun
2736*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2737*4882a593Smuzhiyun AB8505_RTC_PCUT_RESTART_REG, ®_value);
2738*4882a593Smuzhiyun
2739*4882a593Smuzhiyun if (ret < 0) {
2740*4882a593Smuzhiyun dev_err(dev, "Failed to read AB8505_RTC_PCUT_RESTART_REG\n");
2741*4882a593Smuzhiyun goto fail;
2742*4882a593Smuzhiyun }
2743*4882a593Smuzhiyun
2744*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0xF0) >> 4);
2745*4882a593Smuzhiyun
2746*4882a593Smuzhiyun fail:
2747*4882a593Smuzhiyun return ret;
2748*4882a593Smuzhiyun }
2749*4882a593Smuzhiyun
ab8505_powercut_read(struct device * dev,struct device_attribute * attr,char * buf)2750*4882a593Smuzhiyun static ssize_t ab8505_powercut_read(struct device *dev,
2751*4882a593Smuzhiyun struct device_attribute *attr,
2752*4882a593Smuzhiyun char *buf)
2753*4882a593Smuzhiyun {
2754*4882a593Smuzhiyun int ret;
2755*4882a593Smuzhiyun u8 reg_value;
2756*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2757*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2758*4882a593Smuzhiyun
2759*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2760*4882a593Smuzhiyun AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value);
2761*4882a593Smuzhiyun
2762*4882a593Smuzhiyun if (ret < 0)
2763*4882a593Smuzhiyun goto fail;
2764*4882a593Smuzhiyun
2765*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x1));
2766*4882a593Smuzhiyun
2767*4882a593Smuzhiyun fail:
2768*4882a593Smuzhiyun return ret;
2769*4882a593Smuzhiyun }
2770*4882a593Smuzhiyun
ab8505_powercut_write(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2771*4882a593Smuzhiyun static ssize_t ab8505_powercut_write(struct device *dev,
2772*4882a593Smuzhiyun struct device_attribute *attr,
2773*4882a593Smuzhiyun const char *buf, size_t count)
2774*4882a593Smuzhiyun {
2775*4882a593Smuzhiyun int ret;
2776*4882a593Smuzhiyun int reg_value;
2777*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2778*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2779*4882a593Smuzhiyun
2780*4882a593Smuzhiyun if (kstrtoint(buf, 10, ®_value))
2781*4882a593Smuzhiyun goto fail;
2782*4882a593Smuzhiyun
2783*4882a593Smuzhiyun if (reg_value > 0x1) {
2784*4882a593Smuzhiyun dev_err(dev, "Incorrect parameter, echo 0/1 to disable/enable Pcut feature\n");
2785*4882a593Smuzhiyun goto fail;
2786*4882a593Smuzhiyun }
2787*4882a593Smuzhiyun
2788*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2789*4882a593Smuzhiyun AB8505_RTC_PCUT_CTL_STATUS_REG, (u8)reg_value);
2790*4882a593Smuzhiyun
2791*4882a593Smuzhiyun if (ret < 0)
2792*4882a593Smuzhiyun dev_err(dev, "Failed to set AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2793*4882a593Smuzhiyun
2794*4882a593Smuzhiyun fail:
2795*4882a593Smuzhiyun return count;
2796*4882a593Smuzhiyun }
2797*4882a593Smuzhiyun
ab8505_powercut_flag_read(struct device * dev,struct device_attribute * attr,char * buf)2798*4882a593Smuzhiyun static ssize_t ab8505_powercut_flag_read(struct device *dev,
2799*4882a593Smuzhiyun struct device_attribute *attr,
2800*4882a593Smuzhiyun char *buf)
2801*4882a593Smuzhiyun {
2802*4882a593Smuzhiyun
2803*4882a593Smuzhiyun int ret;
2804*4882a593Smuzhiyun u8 reg_value;
2805*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2806*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2807*4882a593Smuzhiyun
2808*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2809*4882a593Smuzhiyun AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value);
2810*4882a593Smuzhiyun
2811*4882a593Smuzhiyun if (ret < 0) {
2812*4882a593Smuzhiyun dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2813*4882a593Smuzhiyun goto fail;
2814*4882a593Smuzhiyun }
2815*4882a593Smuzhiyun
2816*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%d\n", ((reg_value & 0x10) >> 4));
2817*4882a593Smuzhiyun
2818*4882a593Smuzhiyun fail:
2819*4882a593Smuzhiyun return ret;
2820*4882a593Smuzhiyun }
2821*4882a593Smuzhiyun
ab8505_powercut_debounce_read(struct device * dev,struct device_attribute * attr,char * buf)2822*4882a593Smuzhiyun static ssize_t ab8505_powercut_debounce_read(struct device *dev,
2823*4882a593Smuzhiyun struct device_attribute *attr,
2824*4882a593Smuzhiyun char *buf)
2825*4882a593Smuzhiyun {
2826*4882a593Smuzhiyun int ret;
2827*4882a593Smuzhiyun u8 reg_value;
2828*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2829*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2830*4882a593Smuzhiyun
2831*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2832*4882a593Smuzhiyun AB8505_RTC_PCUT_DEBOUNCE_REG, ®_value);
2833*4882a593Smuzhiyun
2834*4882a593Smuzhiyun if (ret < 0) {
2835*4882a593Smuzhiyun dev_err(dev, "Failed to read AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2836*4882a593Smuzhiyun goto fail;
2837*4882a593Smuzhiyun }
2838*4882a593Smuzhiyun
2839*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%d\n", (reg_value & 0x7));
2840*4882a593Smuzhiyun
2841*4882a593Smuzhiyun fail:
2842*4882a593Smuzhiyun return ret;
2843*4882a593Smuzhiyun }
2844*4882a593Smuzhiyun
ab8505_powercut_debounce_write(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2845*4882a593Smuzhiyun static ssize_t ab8505_powercut_debounce_write(struct device *dev,
2846*4882a593Smuzhiyun struct device_attribute *attr,
2847*4882a593Smuzhiyun const char *buf, size_t count)
2848*4882a593Smuzhiyun {
2849*4882a593Smuzhiyun int ret;
2850*4882a593Smuzhiyun int reg_value;
2851*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2852*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2853*4882a593Smuzhiyun
2854*4882a593Smuzhiyun if (kstrtoint(buf, 10, ®_value))
2855*4882a593Smuzhiyun goto fail;
2856*4882a593Smuzhiyun
2857*4882a593Smuzhiyun if (reg_value > 0x7) {
2858*4882a593Smuzhiyun dev_err(dev, "Incorrect parameter, echo 0 to 7 for debounce setting\n");
2859*4882a593Smuzhiyun goto fail;
2860*4882a593Smuzhiyun }
2861*4882a593Smuzhiyun
2862*4882a593Smuzhiyun ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
2863*4882a593Smuzhiyun AB8505_RTC_PCUT_DEBOUNCE_REG, (u8)reg_value);
2864*4882a593Smuzhiyun
2865*4882a593Smuzhiyun if (ret < 0)
2866*4882a593Smuzhiyun dev_err(dev, "Failed to set AB8505_RTC_PCUT_DEBOUNCE_REG\n");
2867*4882a593Smuzhiyun
2868*4882a593Smuzhiyun fail:
2869*4882a593Smuzhiyun return count;
2870*4882a593Smuzhiyun }
2871*4882a593Smuzhiyun
ab8505_powercut_enable_status_read(struct device * dev,struct device_attribute * attr,char * buf)2872*4882a593Smuzhiyun static ssize_t ab8505_powercut_enable_status_read(struct device *dev,
2873*4882a593Smuzhiyun struct device_attribute *attr,
2874*4882a593Smuzhiyun char *buf)
2875*4882a593Smuzhiyun {
2876*4882a593Smuzhiyun int ret;
2877*4882a593Smuzhiyun u8 reg_value;
2878*4882a593Smuzhiyun struct power_supply *psy = dev_get_drvdata(dev);
2879*4882a593Smuzhiyun struct ab8500_fg *di = power_supply_get_drvdata(psy);
2880*4882a593Smuzhiyun
2881*4882a593Smuzhiyun ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
2882*4882a593Smuzhiyun AB8505_RTC_PCUT_CTL_STATUS_REG, ®_value);
2883*4882a593Smuzhiyun
2884*4882a593Smuzhiyun if (ret < 0) {
2885*4882a593Smuzhiyun dev_err(dev, "Failed to read AB8505_RTC_PCUT_CTL_STATUS_REG\n");
2886*4882a593Smuzhiyun goto fail;
2887*4882a593Smuzhiyun }
2888*4882a593Smuzhiyun
2889*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%d\n", ((reg_value & 0x20) >> 5));
2890*4882a593Smuzhiyun
2891*4882a593Smuzhiyun fail:
2892*4882a593Smuzhiyun return ret;
2893*4882a593Smuzhiyun }
2894*4882a593Smuzhiyun
2895*4882a593Smuzhiyun static struct device_attribute ab8505_fg_sysfs_psy_attrs[] = {
2896*4882a593Smuzhiyun __ATTR(powercut_flagtime, (S_IRUGO | S_IWUSR | S_IWGRP),
2897*4882a593Smuzhiyun ab8505_powercut_flagtime_read, ab8505_powercut_flagtime_write),
2898*4882a593Smuzhiyun __ATTR(powercut_maxtime, (S_IRUGO | S_IWUSR | S_IWGRP),
2899*4882a593Smuzhiyun ab8505_powercut_maxtime_read, ab8505_powercut_maxtime_write),
2900*4882a593Smuzhiyun __ATTR(powercut_restart_max, (S_IRUGO | S_IWUSR | S_IWGRP),
2901*4882a593Smuzhiyun ab8505_powercut_restart_read, ab8505_powercut_restart_write),
2902*4882a593Smuzhiyun __ATTR(powercut_timer, S_IRUGO, ab8505_powercut_timer_read, NULL),
2903*4882a593Smuzhiyun __ATTR(powercut_restart_counter, S_IRUGO,
2904*4882a593Smuzhiyun ab8505_powercut_restart_counter_read, NULL),
2905*4882a593Smuzhiyun __ATTR(powercut_enable, (S_IRUGO | S_IWUSR | S_IWGRP),
2906*4882a593Smuzhiyun ab8505_powercut_read, ab8505_powercut_write),
2907*4882a593Smuzhiyun __ATTR(powercut_flag, S_IRUGO, ab8505_powercut_flag_read, NULL),
2908*4882a593Smuzhiyun __ATTR(powercut_debounce_time, (S_IRUGO | S_IWUSR | S_IWGRP),
2909*4882a593Smuzhiyun ab8505_powercut_debounce_read, ab8505_powercut_debounce_write),
2910*4882a593Smuzhiyun __ATTR(powercut_enable_status, S_IRUGO,
2911*4882a593Smuzhiyun ab8505_powercut_enable_status_read, NULL),
2912*4882a593Smuzhiyun };
2913*4882a593Smuzhiyun
ab8500_fg_sysfs_psy_create_attrs(struct ab8500_fg * di)2914*4882a593Smuzhiyun static int ab8500_fg_sysfs_psy_create_attrs(struct ab8500_fg *di)
2915*4882a593Smuzhiyun {
2916*4882a593Smuzhiyun unsigned int i;
2917*4882a593Smuzhiyun
2918*4882a593Smuzhiyun if (is_ab8505(di->parent)) {
2919*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); i++)
2920*4882a593Smuzhiyun if (device_create_file(&di->fg_psy->dev,
2921*4882a593Smuzhiyun &ab8505_fg_sysfs_psy_attrs[i]))
2922*4882a593Smuzhiyun goto sysfs_psy_create_attrs_failed_ab8505;
2923*4882a593Smuzhiyun }
2924*4882a593Smuzhiyun return 0;
2925*4882a593Smuzhiyun sysfs_psy_create_attrs_failed_ab8505:
2926*4882a593Smuzhiyun dev_err(&di->fg_psy->dev, "Failed creating sysfs psy attrs for ab8505.\n");
2927*4882a593Smuzhiyun while (i--)
2928*4882a593Smuzhiyun device_remove_file(&di->fg_psy->dev,
2929*4882a593Smuzhiyun &ab8505_fg_sysfs_psy_attrs[i]);
2930*4882a593Smuzhiyun
2931*4882a593Smuzhiyun return -EIO;
2932*4882a593Smuzhiyun }
2933*4882a593Smuzhiyun
ab8500_fg_sysfs_psy_remove_attrs(struct ab8500_fg * di)2934*4882a593Smuzhiyun static void ab8500_fg_sysfs_psy_remove_attrs(struct ab8500_fg *di)
2935*4882a593Smuzhiyun {
2936*4882a593Smuzhiyun unsigned int i;
2937*4882a593Smuzhiyun
2938*4882a593Smuzhiyun if (is_ab8505(di->parent)) {
2939*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(ab8505_fg_sysfs_psy_attrs); i++)
2940*4882a593Smuzhiyun (void)device_remove_file(&di->fg_psy->dev,
2941*4882a593Smuzhiyun &ab8505_fg_sysfs_psy_attrs[i]);
2942*4882a593Smuzhiyun }
2943*4882a593Smuzhiyun }
2944*4882a593Smuzhiyun
2945*4882a593Smuzhiyun /* Exposure to the sysfs interface <<END>> */
2946*4882a593Smuzhiyun
2947*4882a593Smuzhiyun #if defined(CONFIG_PM)
ab8500_fg_resume(struct platform_device * pdev)2948*4882a593Smuzhiyun static int ab8500_fg_resume(struct platform_device *pdev)
2949*4882a593Smuzhiyun {
2950*4882a593Smuzhiyun struct ab8500_fg *di = platform_get_drvdata(pdev);
2951*4882a593Smuzhiyun
2952*4882a593Smuzhiyun /*
2953*4882a593Smuzhiyun * Change state if we're not charging. If we're charging we will wake
2954*4882a593Smuzhiyun * up on the FG IRQ
2955*4882a593Smuzhiyun */
2956*4882a593Smuzhiyun if (!di->flags.charging) {
2957*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP);
2958*4882a593Smuzhiyun queue_work(di->fg_wq, &di->fg_work);
2959*4882a593Smuzhiyun }
2960*4882a593Smuzhiyun
2961*4882a593Smuzhiyun return 0;
2962*4882a593Smuzhiyun }
2963*4882a593Smuzhiyun
ab8500_fg_suspend(struct platform_device * pdev,pm_message_t state)2964*4882a593Smuzhiyun static int ab8500_fg_suspend(struct platform_device *pdev,
2965*4882a593Smuzhiyun pm_message_t state)
2966*4882a593Smuzhiyun {
2967*4882a593Smuzhiyun struct ab8500_fg *di = platform_get_drvdata(pdev);
2968*4882a593Smuzhiyun
2969*4882a593Smuzhiyun flush_delayed_work(&di->fg_periodic_work);
2970*4882a593Smuzhiyun flush_work(&di->fg_work);
2971*4882a593Smuzhiyun flush_work(&di->fg_acc_cur_work);
2972*4882a593Smuzhiyun flush_delayed_work(&di->fg_reinit_work);
2973*4882a593Smuzhiyun flush_delayed_work(&di->fg_low_bat_work);
2974*4882a593Smuzhiyun flush_delayed_work(&di->fg_check_hw_failure_work);
2975*4882a593Smuzhiyun
2976*4882a593Smuzhiyun /*
2977*4882a593Smuzhiyun * If the FG is enabled we will disable it before going to suspend
2978*4882a593Smuzhiyun * only if we're not charging
2979*4882a593Smuzhiyun */
2980*4882a593Smuzhiyun if (di->flags.fg_enabled && !di->flags.charging)
2981*4882a593Smuzhiyun ab8500_fg_coulomb_counter(di, false);
2982*4882a593Smuzhiyun
2983*4882a593Smuzhiyun return 0;
2984*4882a593Smuzhiyun }
2985*4882a593Smuzhiyun #else
2986*4882a593Smuzhiyun #define ab8500_fg_suspend NULL
2987*4882a593Smuzhiyun #define ab8500_fg_resume NULL
2988*4882a593Smuzhiyun #endif
2989*4882a593Smuzhiyun
ab8500_fg_remove(struct platform_device * pdev)2990*4882a593Smuzhiyun static int ab8500_fg_remove(struct platform_device *pdev)
2991*4882a593Smuzhiyun {
2992*4882a593Smuzhiyun int ret = 0;
2993*4882a593Smuzhiyun struct ab8500_fg *di = platform_get_drvdata(pdev);
2994*4882a593Smuzhiyun
2995*4882a593Smuzhiyun list_del(&di->node);
2996*4882a593Smuzhiyun
2997*4882a593Smuzhiyun /* Disable coulomb counter */
2998*4882a593Smuzhiyun ret = ab8500_fg_coulomb_counter(di, false);
2999*4882a593Smuzhiyun if (ret)
3000*4882a593Smuzhiyun dev_err(di->dev, "failed to disable coulomb counter\n");
3001*4882a593Smuzhiyun
3002*4882a593Smuzhiyun destroy_workqueue(di->fg_wq);
3003*4882a593Smuzhiyun ab8500_fg_sysfs_exit(di);
3004*4882a593Smuzhiyun
3005*4882a593Smuzhiyun flush_scheduled_work();
3006*4882a593Smuzhiyun ab8500_fg_sysfs_psy_remove_attrs(di);
3007*4882a593Smuzhiyun power_supply_unregister(di->fg_psy);
3008*4882a593Smuzhiyun return ret;
3009*4882a593Smuzhiyun }
3010*4882a593Smuzhiyun
3011*4882a593Smuzhiyun /* ab8500 fg driver interrupts and their respective isr */
3012*4882a593Smuzhiyun static struct ab8500_fg_interrupts ab8500_fg_irq_th[] = {
3013*4882a593Smuzhiyun {"NCONV_ACCU", ab8500_fg_cc_convend_handler},
3014*4882a593Smuzhiyun {"BATT_OVV", ab8500_fg_batt_ovv_handler},
3015*4882a593Smuzhiyun {"LOW_BAT_F", ab8500_fg_lowbatf_handler},
3016*4882a593Smuzhiyun {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler},
3017*4882a593Smuzhiyun };
3018*4882a593Smuzhiyun
3019*4882a593Smuzhiyun static struct ab8500_fg_interrupts ab8500_fg_irq_bh[] = {
3020*4882a593Smuzhiyun {"CCEOC", ab8500_fg_cc_data_end_handler},
3021*4882a593Smuzhiyun };
3022*4882a593Smuzhiyun
3023*4882a593Smuzhiyun static char *supply_interface[] = {
3024*4882a593Smuzhiyun "ab8500_chargalg",
3025*4882a593Smuzhiyun "ab8500_usb",
3026*4882a593Smuzhiyun };
3027*4882a593Smuzhiyun
3028*4882a593Smuzhiyun static const struct power_supply_desc ab8500_fg_desc = {
3029*4882a593Smuzhiyun .name = "ab8500_fg",
3030*4882a593Smuzhiyun .type = POWER_SUPPLY_TYPE_BATTERY,
3031*4882a593Smuzhiyun .properties = ab8500_fg_props,
3032*4882a593Smuzhiyun .num_properties = ARRAY_SIZE(ab8500_fg_props),
3033*4882a593Smuzhiyun .get_property = ab8500_fg_get_property,
3034*4882a593Smuzhiyun .external_power_changed = ab8500_fg_external_power_changed,
3035*4882a593Smuzhiyun };
3036*4882a593Smuzhiyun
ab8500_fg_probe(struct platform_device * pdev)3037*4882a593Smuzhiyun static int ab8500_fg_probe(struct platform_device *pdev)
3038*4882a593Smuzhiyun {
3039*4882a593Smuzhiyun struct device_node *np = pdev->dev.of_node;
3040*4882a593Smuzhiyun struct abx500_bm_data *plat = pdev->dev.platform_data;
3041*4882a593Smuzhiyun struct power_supply_config psy_cfg = {};
3042*4882a593Smuzhiyun struct ab8500_fg *di;
3043*4882a593Smuzhiyun int i, irq;
3044*4882a593Smuzhiyun int ret = 0;
3045*4882a593Smuzhiyun
3046*4882a593Smuzhiyun di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3047*4882a593Smuzhiyun if (!di) {
3048*4882a593Smuzhiyun dev_err(&pdev->dev, "%s no mem for ab8500_fg\n", __func__);
3049*4882a593Smuzhiyun return -ENOMEM;
3050*4882a593Smuzhiyun }
3051*4882a593Smuzhiyun
3052*4882a593Smuzhiyun if (!plat) {
3053*4882a593Smuzhiyun dev_err(&pdev->dev, "no battery management data supplied\n");
3054*4882a593Smuzhiyun return -EINVAL;
3055*4882a593Smuzhiyun }
3056*4882a593Smuzhiyun di->bm = plat;
3057*4882a593Smuzhiyun
3058*4882a593Smuzhiyun if (np) {
3059*4882a593Smuzhiyun ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3060*4882a593Smuzhiyun if (ret) {
3061*4882a593Smuzhiyun dev_err(&pdev->dev, "failed to get battery information\n");
3062*4882a593Smuzhiyun return ret;
3063*4882a593Smuzhiyun }
3064*4882a593Smuzhiyun }
3065*4882a593Smuzhiyun
3066*4882a593Smuzhiyun mutex_init(&di->cc_lock);
3067*4882a593Smuzhiyun
3068*4882a593Smuzhiyun /* get parent data */
3069*4882a593Smuzhiyun di->dev = &pdev->dev;
3070*4882a593Smuzhiyun di->parent = dev_get_drvdata(pdev->dev.parent);
3071*4882a593Smuzhiyun
3072*4882a593Smuzhiyun di->main_bat_v = devm_iio_channel_get(&pdev->dev, "main_bat_v");
3073*4882a593Smuzhiyun if (IS_ERR(di->main_bat_v)) {
3074*4882a593Smuzhiyun if (PTR_ERR(di->main_bat_v) == -ENODEV)
3075*4882a593Smuzhiyun return -EPROBE_DEFER;
3076*4882a593Smuzhiyun dev_err(&pdev->dev, "failed to get main battery ADC channel\n");
3077*4882a593Smuzhiyun return PTR_ERR(di->main_bat_v);
3078*4882a593Smuzhiyun }
3079*4882a593Smuzhiyun
3080*4882a593Smuzhiyun psy_cfg.supplied_to = supply_interface;
3081*4882a593Smuzhiyun psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
3082*4882a593Smuzhiyun psy_cfg.drv_data = di;
3083*4882a593Smuzhiyun
3084*4882a593Smuzhiyun di->bat_cap.max_mah_design = MILLI_TO_MICRO *
3085*4882a593Smuzhiyun di->bm->bat_type[di->bm->batt_id].charge_full_design;
3086*4882a593Smuzhiyun
3087*4882a593Smuzhiyun di->bat_cap.max_mah = di->bat_cap.max_mah_design;
3088*4882a593Smuzhiyun
3089*4882a593Smuzhiyun di->vbat_nom = di->bm->bat_type[di->bm->batt_id].nominal_voltage;
3090*4882a593Smuzhiyun
3091*4882a593Smuzhiyun di->init_capacity = true;
3092*4882a593Smuzhiyun
3093*4882a593Smuzhiyun ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
3094*4882a593Smuzhiyun ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
3095*4882a593Smuzhiyun
3096*4882a593Smuzhiyun /* Create a work queue for running the FG algorithm */
3097*4882a593Smuzhiyun di->fg_wq = alloc_ordered_workqueue("ab8500_fg_wq", WQ_MEM_RECLAIM);
3098*4882a593Smuzhiyun if (di->fg_wq == NULL) {
3099*4882a593Smuzhiyun dev_err(di->dev, "failed to create work queue\n");
3100*4882a593Smuzhiyun return -ENOMEM;
3101*4882a593Smuzhiyun }
3102*4882a593Smuzhiyun
3103*4882a593Smuzhiyun /* Init work for running the fg algorithm instantly */
3104*4882a593Smuzhiyun INIT_WORK(&di->fg_work, ab8500_fg_instant_work);
3105*4882a593Smuzhiyun
3106*4882a593Smuzhiyun /* Init work for getting the battery accumulated current */
3107*4882a593Smuzhiyun INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work);
3108*4882a593Smuzhiyun
3109*4882a593Smuzhiyun /* Init work for reinitialising the fg algorithm */
3110*4882a593Smuzhiyun INIT_DEFERRABLE_WORK(&di->fg_reinit_work,
3111*4882a593Smuzhiyun ab8500_fg_reinit_work);
3112*4882a593Smuzhiyun
3113*4882a593Smuzhiyun /* Work delayed Queue to run the state machine */
3114*4882a593Smuzhiyun INIT_DEFERRABLE_WORK(&di->fg_periodic_work,
3115*4882a593Smuzhiyun ab8500_fg_periodic_work);
3116*4882a593Smuzhiyun
3117*4882a593Smuzhiyun /* Work to check low battery condition */
3118*4882a593Smuzhiyun INIT_DEFERRABLE_WORK(&di->fg_low_bat_work,
3119*4882a593Smuzhiyun ab8500_fg_low_bat_work);
3120*4882a593Smuzhiyun
3121*4882a593Smuzhiyun /* Init work for HW failure check */
3122*4882a593Smuzhiyun INIT_DEFERRABLE_WORK(&di->fg_check_hw_failure_work,
3123*4882a593Smuzhiyun ab8500_fg_check_hw_failure_work);
3124*4882a593Smuzhiyun
3125*4882a593Smuzhiyun /* Reset battery low voltage flag */
3126*4882a593Smuzhiyun di->flags.low_bat = false;
3127*4882a593Smuzhiyun
3128*4882a593Smuzhiyun /* Initialize low battery counter */
3129*4882a593Smuzhiyun di->low_bat_cnt = 10;
3130*4882a593Smuzhiyun
3131*4882a593Smuzhiyun /* Initialize OVV, and other registers */
3132*4882a593Smuzhiyun ret = ab8500_fg_init_hw_registers(di);
3133*4882a593Smuzhiyun if (ret) {
3134*4882a593Smuzhiyun dev_err(di->dev, "failed to initialize registers\n");
3135*4882a593Smuzhiyun goto free_inst_curr_wq;
3136*4882a593Smuzhiyun }
3137*4882a593Smuzhiyun
3138*4882a593Smuzhiyun /* Consider battery unknown until we're informed otherwise */
3139*4882a593Smuzhiyun di->flags.batt_unknown = true;
3140*4882a593Smuzhiyun di->flags.batt_id_received = false;
3141*4882a593Smuzhiyun
3142*4882a593Smuzhiyun /* Register FG power supply class */
3143*4882a593Smuzhiyun di->fg_psy = power_supply_register(di->dev, &ab8500_fg_desc, &psy_cfg);
3144*4882a593Smuzhiyun if (IS_ERR(di->fg_psy)) {
3145*4882a593Smuzhiyun dev_err(di->dev, "failed to register FG psy\n");
3146*4882a593Smuzhiyun ret = PTR_ERR(di->fg_psy);
3147*4882a593Smuzhiyun goto free_inst_curr_wq;
3148*4882a593Smuzhiyun }
3149*4882a593Smuzhiyun
3150*4882a593Smuzhiyun di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer);
3151*4882a593Smuzhiyun ab8500_fg_coulomb_counter(di, true);
3152*4882a593Smuzhiyun
3153*4882a593Smuzhiyun /*
3154*4882a593Smuzhiyun * Initialize completion used to notify completion and start
3155*4882a593Smuzhiyun * of inst current
3156*4882a593Smuzhiyun */
3157*4882a593Smuzhiyun init_completion(&di->ab8500_fg_started);
3158*4882a593Smuzhiyun init_completion(&di->ab8500_fg_complete);
3159*4882a593Smuzhiyun
3160*4882a593Smuzhiyun /* Register primary interrupt handlers */
3161*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq_th); i++) {
3162*4882a593Smuzhiyun irq = platform_get_irq_byname(pdev, ab8500_fg_irq_th[i].name);
3163*4882a593Smuzhiyun if (irq < 0) {
3164*4882a593Smuzhiyun ret = irq;
3165*4882a593Smuzhiyun goto free_irq_th;
3166*4882a593Smuzhiyun }
3167*4882a593Smuzhiyun
3168*4882a593Smuzhiyun ret = request_irq(irq, ab8500_fg_irq_th[i].isr,
3169*4882a593Smuzhiyun IRQF_SHARED | IRQF_NO_SUSPEND,
3170*4882a593Smuzhiyun ab8500_fg_irq_th[i].name, di);
3171*4882a593Smuzhiyun
3172*4882a593Smuzhiyun if (ret != 0) {
3173*4882a593Smuzhiyun dev_err(di->dev, "failed to request %s IRQ %d: %d\n",
3174*4882a593Smuzhiyun ab8500_fg_irq_th[i].name, irq, ret);
3175*4882a593Smuzhiyun goto free_irq_th;
3176*4882a593Smuzhiyun }
3177*4882a593Smuzhiyun dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3178*4882a593Smuzhiyun ab8500_fg_irq_th[i].name, irq, ret);
3179*4882a593Smuzhiyun }
3180*4882a593Smuzhiyun
3181*4882a593Smuzhiyun /* Register threaded interrupt handler */
3182*4882a593Smuzhiyun irq = platform_get_irq_byname(pdev, ab8500_fg_irq_bh[0].name);
3183*4882a593Smuzhiyun if (irq < 0) {
3184*4882a593Smuzhiyun ret = irq;
3185*4882a593Smuzhiyun goto free_irq_th;
3186*4882a593Smuzhiyun }
3187*4882a593Smuzhiyun
3188*4882a593Smuzhiyun ret = request_threaded_irq(irq, NULL, ab8500_fg_irq_bh[0].isr,
3189*4882a593Smuzhiyun IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
3190*4882a593Smuzhiyun ab8500_fg_irq_bh[0].name, di);
3191*4882a593Smuzhiyun
3192*4882a593Smuzhiyun if (ret != 0) {
3193*4882a593Smuzhiyun dev_err(di->dev, "failed to request %s IRQ %d: %d\n",
3194*4882a593Smuzhiyun ab8500_fg_irq_bh[0].name, irq, ret);
3195*4882a593Smuzhiyun goto free_irq_th;
3196*4882a593Smuzhiyun }
3197*4882a593Smuzhiyun dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3198*4882a593Smuzhiyun ab8500_fg_irq_bh[0].name, irq, ret);
3199*4882a593Smuzhiyun
3200*4882a593Smuzhiyun di->irq = platform_get_irq_byname(pdev, "CCEOC");
3201*4882a593Smuzhiyun disable_irq(di->irq);
3202*4882a593Smuzhiyun di->nbr_cceoc_irq_cnt = 0;
3203*4882a593Smuzhiyun
3204*4882a593Smuzhiyun platform_set_drvdata(pdev, di);
3205*4882a593Smuzhiyun
3206*4882a593Smuzhiyun ret = ab8500_fg_sysfs_init(di);
3207*4882a593Smuzhiyun if (ret) {
3208*4882a593Smuzhiyun dev_err(di->dev, "failed to create sysfs entry\n");
3209*4882a593Smuzhiyun goto free_irq;
3210*4882a593Smuzhiyun }
3211*4882a593Smuzhiyun
3212*4882a593Smuzhiyun ret = ab8500_fg_sysfs_psy_create_attrs(di);
3213*4882a593Smuzhiyun if (ret) {
3214*4882a593Smuzhiyun dev_err(di->dev, "failed to create FG psy\n");
3215*4882a593Smuzhiyun ab8500_fg_sysfs_exit(di);
3216*4882a593Smuzhiyun goto free_irq;
3217*4882a593Smuzhiyun }
3218*4882a593Smuzhiyun
3219*4882a593Smuzhiyun /* Calibrate the fg first time */
3220*4882a593Smuzhiyun di->flags.calibrate = true;
3221*4882a593Smuzhiyun di->calib_state = AB8500_FG_CALIB_INIT;
3222*4882a593Smuzhiyun
3223*4882a593Smuzhiyun /* Use room temp as default value until we get an update from driver. */
3224*4882a593Smuzhiyun di->bat_temp = 210;
3225*4882a593Smuzhiyun
3226*4882a593Smuzhiyun /* Run the FG algorithm */
3227*4882a593Smuzhiyun queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
3228*4882a593Smuzhiyun
3229*4882a593Smuzhiyun list_add_tail(&di->node, &ab8500_fg_list);
3230*4882a593Smuzhiyun
3231*4882a593Smuzhiyun return ret;
3232*4882a593Smuzhiyun
3233*4882a593Smuzhiyun free_irq:
3234*4882a593Smuzhiyun /* We also have to free all registered irqs */
3235*4882a593Smuzhiyun irq = platform_get_irq_byname(pdev, ab8500_fg_irq_bh[0].name);
3236*4882a593Smuzhiyun free_irq(irq, di);
3237*4882a593Smuzhiyun free_irq_th:
3238*4882a593Smuzhiyun while (--i >= 0) {
3239*4882a593Smuzhiyun /* Last assignment of i from primary interrupt handlers */
3240*4882a593Smuzhiyun irq = platform_get_irq_byname(pdev, ab8500_fg_irq_th[i].name);
3241*4882a593Smuzhiyun free_irq(irq, di);
3242*4882a593Smuzhiyun }
3243*4882a593Smuzhiyun
3244*4882a593Smuzhiyun power_supply_unregister(di->fg_psy);
3245*4882a593Smuzhiyun free_inst_curr_wq:
3246*4882a593Smuzhiyun destroy_workqueue(di->fg_wq);
3247*4882a593Smuzhiyun return ret;
3248*4882a593Smuzhiyun }
3249*4882a593Smuzhiyun
3250*4882a593Smuzhiyun static const struct of_device_id ab8500_fg_match[] = {
3251*4882a593Smuzhiyun { .compatible = "stericsson,ab8500-fg", },
3252*4882a593Smuzhiyun { },
3253*4882a593Smuzhiyun };
3254*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ab8500_fg_match);
3255*4882a593Smuzhiyun
3256*4882a593Smuzhiyun static struct platform_driver ab8500_fg_driver = {
3257*4882a593Smuzhiyun .probe = ab8500_fg_probe,
3258*4882a593Smuzhiyun .remove = ab8500_fg_remove,
3259*4882a593Smuzhiyun .suspend = ab8500_fg_suspend,
3260*4882a593Smuzhiyun .resume = ab8500_fg_resume,
3261*4882a593Smuzhiyun .driver = {
3262*4882a593Smuzhiyun .name = "ab8500-fg",
3263*4882a593Smuzhiyun .of_match_table = ab8500_fg_match,
3264*4882a593Smuzhiyun },
3265*4882a593Smuzhiyun };
3266*4882a593Smuzhiyun
ab8500_fg_init(void)3267*4882a593Smuzhiyun static int __init ab8500_fg_init(void)
3268*4882a593Smuzhiyun {
3269*4882a593Smuzhiyun return platform_driver_register(&ab8500_fg_driver);
3270*4882a593Smuzhiyun }
3271*4882a593Smuzhiyun
ab8500_fg_exit(void)3272*4882a593Smuzhiyun static void __exit ab8500_fg_exit(void)
3273*4882a593Smuzhiyun {
3274*4882a593Smuzhiyun platform_driver_unregister(&ab8500_fg_driver);
3275*4882a593Smuzhiyun }
3276*4882a593Smuzhiyun
3277*4882a593Smuzhiyun subsys_initcall_sync(ab8500_fg_init);
3278*4882a593Smuzhiyun module_exit(ab8500_fg_exit);
3279*4882a593Smuzhiyun
3280*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
3281*4882a593Smuzhiyun MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
3282*4882a593Smuzhiyun MODULE_ALIAS("platform:ab8500-fg");
3283*4882a593Smuzhiyun MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");
3284