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