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