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