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