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