forked from bestmjh47/android_kernel_samsung_exynos5433
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmax77843_fuelgauge.c
More file actions
2024 lines (1686 loc) · 52.1 KB
/
Copy pathmax77843_fuelgauge.c
File metadata and controls
2024 lines (1686 loc) · 52.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* max77843_fuelgauge.c
* Samsung MAX77843 Fuel Gauge Driver
*
* Copyright (C) 2012 Samsung Electronics
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#define DEBUG
/* #define BATTERY_LOG_MESSAGE */
#include <linux/mfd/max77843-private.h>
#include <linux/of_gpio.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
static enum power_supply_property max77843_fuelgauge_props[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_VOLTAGE_AVG,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_CURRENT_AVG,
POWER_SUPPLY_PROP_CHARGE_FULL,
POWER_SUPPLY_PROP_ENERGY_NOW,
POWER_SUPPLY_PROP_CAPACITY,
POWER_SUPPLY_PROP_TEMP,
POWER_SUPPLY_PROP_TEMP_AMBIENT,
#if defined(CONFIG_EN_OOPS)
POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
#endif
POWER_SUPPLY_PROP_ENERGY_FULL,
};
#if !defined(CONFIG_SEC_FACTORY)
static void fg_test_print(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
u32 average_vcell;
u16 w_data;
u32 temp;
u32 temp2;
#ifdef BATTERY_LOG_MESSAGE
u16 reg_data;
#endif
if (max77843_bulk_read(fuelgauge->i2c,
AVR_VCELL_REG, 2, data) < 0) {
pr_err("%s: Failed to read VCELL\n", __func__);
return;
}
w_data = (data[1]<<8) | data[0];
temp = (w_data & 0xFFF) * 78125;
average_vcell = temp / 1000000;
temp = ((w_data & 0xF000) >> 4) * 78125;
temp2 = temp / 1000000;
average_vcell += (temp2 << 4);
#ifdef BATTERY_LOG_MESSAGE
pr_info("%s: AVG_VCELL(%d), data(0x%04x)\n", __func__,
average_vcell, (data[1]<<8) | data[0]);
reg_data = max77843_read_word(fuelgauge->i2c, FULLCAP_REG);
pr_info("%s: FULLCAP(%d), data(0x%04x)\n", __func__,
reg_data/2, reg_data);
reg_data = max77843_read_word(fuelgauge->i2c, REMCAP_REP_REG);
pr_info("%s: REMCAP_REP(%d), data(0x%04x)\n", __func__,
reg_data/2, reg_data);
reg_data = max77843_read_word(fuelgauge->i2c, REMCAP_MIX_REG);
pr_info("%s: REMCAP_MIX(%d), data(0x%04x)\n", __func__,
reg_data/2, reg_data);
reg_data = max77843_read_word(fuelgauge->i2c, REMCAP_AV_REG);
pr_info("%s: REMCAP_AV(%d), data(0x%04x)\n", __func__,
reg_data/2, reg_data);
reg_data = max77843_read_word(fuelgauge->i2c, CONFIG_REG);
pr_info("%s: CONFIG_REG(0x%02x), data(0x%04x)\n", __func__,
CONFIG_REG, reg_data);
#endif
}
static void fg_periodic_read(struct max77843_fuelgauge_data *fuelgauge)
{
u8 reg;
int i;
int data[0x10];
char *str = NULL;
str = kzalloc(sizeof(char)*1024, GFP_KERNEL);
if (!str)
return;
for (i = 0; i < 16; i++) {
for (reg = 0; reg < 0x10; reg++) {
data[reg] = max77843_read_word(fuelgauge->i2c, reg + i * 0x10);
if (data[reg] < 0) {
kfree(str);
return;
}
}
if (i == 12)
continue;
sprintf(str+strlen(str),
"%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,",
data[0x00], data[0x01], data[0x02], data[0x03],
data[0x04], data[0x05], data[0x06], data[0x07]);
sprintf(str+strlen(str),
"%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,",
data[0x08], data[0x09], data[0x0a], data[0x0b],
data[0x0c], data[0x0d], data[0x0e], data[0x0f]);
if (i == 4)
i = 10;
}
pr_info("[FG] %s\n", str);
kfree(str);
}
#endif
static int fg_read_vcell(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
u32 vcell;
u16 w_data;
u32 temp;
u32 temp2;
if (max77843_bulk_read(fuelgauge->i2c, VCELL_REG, 2, data) < 0) {
pr_err("%s: Failed to read VCELL\n", __func__);
return -1;
}
w_data = (data[1]<<8) | data[0];
temp = (w_data & 0xFFF) * 78125;
vcell = temp / 1000000;
temp = ((w_data & 0xF000) >> 4) * 78125;
temp2 = temp / 1000000;
vcell += (temp2 << 4);
if (!(fuelgauge->info.pr_cnt % PRINT_COUNT))
pr_info("%s: VCELL(%d), data(0x%04x)\n",
__func__, vcell, (data[1]<<8) | data[0]);
return vcell;
}
static int fg_read_vfocv(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
u32 vfocv = 0;
u16 w_data;
u32 temp;
u32 temp2;
if (max77843_bulk_read(fuelgauge->i2c, VFOCV_REG, 2, data) < 0) {
pr_err("%s: Failed to read VFOCV\n", __func__);
return -1;
}
w_data = (data[1]<<8) | data[0];
temp = (w_data & 0xFFF) * 78125;
vfocv = temp / 1000000;
temp = ((w_data & 0xF000) >> 4) * 78125;
temp2 = temp / 1000000;
vfocv += (temp2 << 4);
return vfocv;
}
static int fg_read_avg_vcell(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
u32 avg_vcell = 0;
u16 w_data;
u32 temp;
u32 temp2;
if (max77843_bulk_read(fuelgauge->i2c, AVR_VCELL_REG,
2, data) < 0) {
pr_err("%s: Failed to read AVG_VCELL\n", __func__);
return -1;
}
w_data = (data[1]<<8) | data[0];
temp = (w_data & 0xFFF) * 78125;
avg_vcell = temp / 1000000;
temp = ((w_data & 0xF000) >> 4) * 78125;
temp2 = temp / 1000000;
avg_vcell += (temp2 << 4);
return avg_vcell;
}
static int fg_check_battery_present(struct max77843_fuelgauge_data *fuelgauge)
{
u8 status_data[2];
int ret = 1;
/* 1. Check Bst bit */
if (max77843_bulk_read(fuelgauge->i2c, STATUS_REG,
2, status_data) < 0) {
pr_err("%s: Failed to read STATUS_REG\n", __func__);
return 0;
}
if (status_data[0] & (0x1 << 3)) {
pr_info("%s: addr(0x01), data(0x%04x)\n", __func__,
(status_data[1]<<8) | status_data[0]);
pr_info("%s: battery is absent!!\n", __func__);
ret = 0;
}
return ret;
}
static int fg_write_temp(struct max77843_fuelgauge_data *fuelgauge,
int temperature)
{
u8 data[2];
data[0] = (temperature%10) * 1000 / 39;
data[1] = temperature / 10;
max77843_bulk_write(fuelgauge->i2c, TEMPERATURE_REG,
2, data);
pr_debug("%s: temperature to (%d, 0x%02x%02x)\n",
__func__, temperature, data[1], data[0]);
return temperature;
}
static int fg_read_temp(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2] = {0, 0};
int temper = 0;
if (fg_check_battery_present(fuelgauge)) {
if (max77843_bulk_read(fuelgauge->i2c,
TEMPERATURE_REG, 2, data) < 0) {
pr_err("%s: Failed to read TEMPERATURE_REG\n",
__func__);
return -1;
}
if (data[1]&(0x1 << 7)) {
temper = ((~(data[1]))&0xFF)+1;
temper *= (-1000);
temper -= ((~((int)data[0]))+1) * 39 / 10;
} else {
temper = data[1] & 0x7f;
temper *= 1000;
temper += data[0] * 39 / 10;
}
} else
temper = 20000;
if (!(fuelgauge->info.pr_cnt % PRINT_COUNT))
pr_info("%s: TEMPERATURE(%d), data(0x%04x)\n",
__func__, temper, (data[1]<<8) | data[0]);
return temper/100;
}
/* soc should be 0.1% unit */
static int fg_read_vfsoc(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
int soc;
if (max77843_bulk_read(fuelgauge->i2c, VFSOC_REG,
2, data) < 0) {
pr_err("%s: Failed to read VFSOC\n", __func__);
return -1;
}
soc = ((data[1] * 100) + (data[0] * 100 / 256)) / 10;
return min(soc, 1000);
}
/* soc should be 0.1% unit */
static int fg_read_avsoc(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
int soc;
if (max77843_bulk_read(fuelgauge->i2c, SOCAV_REG,
2, data) < 0) {
pr_err("%s: Failed to read AVSOC\n", __func__);
return -1;
}
soc = ((data[1] * 100) + (data[0] * 100 / 256)) / 10;
return min(soc, 1000);
}
/* soc should be 0.1% unit */
static int fg_read_soc(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
int soc;
if (max77843_bulk_read(fuelgauge->i2c, SOCREP_REG, 2, data) < 0) {
pr_err("%s: Failed to read SOCREP\n", __func__);
return -1;
}
soc = ((data[1] * 100) + (data[0] * 100 / 256)) / 10;
#ifdef BATTERY_LOG_MESSAGE
pr_debug("%s: raw capacity (%d)\n", __func__, soc);
if (!(fuelgauge->info.pr_cnt % PRINT_COUNT))
pr_debug("%s: raw capacity (%d), data(0x%04x)\n",
__func__, soc, (data[1]<<8) | data[0]);
#endif
return min(soc, 1000);
}
/* soc should be 0.01% unit */
static int fg_read_rawsoc(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
int soc;
if (max77843_bulk_read(fuelgauge->i2c, SOCREP_REG,
2, data) < 0) {
pr_err("%s: Failed to read SOCREP\n", __func__);
return -1;
}
soc = (data[1] * 100) + (data[0] * 100 / 256);
pr_debug("%s: raw capacity (0.01%%) (%d)\n",
__func__, soc);
if (!(fuelgauge->info.pr_cnt % PRINT_COUNT))
pr_debug("%s: raw capacity (%d), data(0x%04x)\n",
__func__, soc, (data[1]<<8) | data[0]);
return min(soc, 10000);
}
static int fg_read_fullcap(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
int ret;
if (max77843_bulk_read(fuelgauge->i2c, FULLCAP_REG,
2, data) < 0) {
pr_err("%s: Failed to read FULLCAP\n", __func__);
return -1;
}
ret = (data[1] << 8) + data[0];
return ret;
}
static int fg_read_mixcap(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
int ret;
if (max77843_bulk_read(fuelgauge->i2c, REMCAP_MIX_REG,
2, data) < 0) {
pr_err("%s: Failed to read REMCAP_MIX_REG\n",
__func__);
return -1;
}
ret = (data[1] << 8) + data[0];
return ret;
}
static int fg_read_avcap(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
int ret;
if (max77843_bulk_read(fuelgauge->i2c, REMCAP_AV_REG,
2, data) < 0) {
pr_err("%s: Failed to read REMCAP_AV_REG\n",
__func__);
return -1;
}
ret = (data[1] << 8) + data[0];
return ret;
}
static int fg_read_repcap(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
int ret;
if (max77843_bulk_read(fuelgauge->i2c, REMCAP_REP_REG,
2, data) < 0) {
pr_err("%s: Failed to read REMCAP_REP_REG\n",
__func__);
return -1;
}
ret = (data[1] << 8) + data[0];
return ret;
}
static int fg_read_current(struct max77843_fuelgauge_data *fuelgauge, int unit)
{
u8 data1[2], data2[2];
u32 temp, sign;
s32 i_current;
s32 avg_current;
int vcell;
static int cnt;
if (max77843_bulk_read(fuelgauge->i2c, CURRENT_REG,
2, data1) < 0) {
pr_err("%s: Failed to read CURRENT\n", __func__);
return -1;
}
if (max77843_bulk_read(fuelgauge->i2c, AVG_CURRENT_REG,
2, data2) < 0) {
pr_err("%s: Failed to read AVERAGE CURRENT\n", __func__);
return -1;
}
temp = ((data1[1]<<8) | data1[0]) & 0xFFFF;
if (temp & (0x1 << 15)) {
sign = NEGATIVE;
temp = (~temp & 0xFFFF) + 1;
} else
sign = POSITIVE;
/* 1.5625uV/0.01Ohm(Rsense) = 156.25uA */
switch (unit) {
case SEC_BATTEY_CURRENT_UA:
i_current = temp * 15625 / 100;
break;
case SEC_BATTEY_CURRENT_MA:
default:
i_current = temp * 15625 / 100000;
}
if (sign)
i_current *= -1;
temp = ((data2[1]<<8) | data2[0]) & 0xFFFF;
if (temp & (0x1 << 15)) {
sign = NEGATIVE;
temp = (~temp & 0xFFFF) + 1;
} else
sign = POSITIVE;
/* 1.5625uV/0.01Ohm(Rsense) = 156.25uA */
avg_current = temp * 15625 / 100000;
if (sign)
avg_current *= -1;
vcell = fg_read_vcell(fuelgauge);
if ((vcell < 3500) && (cnt < 10) && (i_current < 0) &&
fuelgauge->is_charging) {
i_current = 1;
cnt++;
}
#if !defined(CONFIG_SEC_FACTORY)
fg_test_print(fuelgauge);
fg_periodic_read(fuelgauge);
#endif
return i_current;
}
static int fg_read_avg_current(struct max77843_fuelgauge_data *fuelgauge, int unit)
{
u8 data2[2];
u32 temp, sign;
s32 avg_current;
int vcell;
static int cnt;
if (max77843_bulk_read(fuelgauge->i2c, AVG_CURRENT_REG,
2, data2) < 0) {
pr_err("%s: Failed to read AVERAGE CURRENT\n",
__func__);
return -1;
}
temp = ((data2[1]<<8) | data2[0]) & 0xFFFF;
if (temp & (0x1 << 15)) {
sign = NEGATIVE;
temp = (~temp & 0xFFFF) + 1;
} else
sign = POSITIVE;
/* 1.5625uV/0.01Ohm(Rsense) = 156.25uA */
switch (unit) {
case SEC_BATTEY_CURRENT_UA:
avg_current = temp * 15625 / 100;
break;
case SEC_BATTEY_CURRENT_MA:
default:
avg_current = temp * 15625 / 100000;
}
if (sign)
avg_current *= -1;
vcell = fg_read_vcell(fuelgauge);
if ((vcell < 3500) && (cnt < 10) && (avg_current < 0) &&
fuelgauge->is_charging) {
avg_current = 1;
cnt++;
}
return avg_current;
}
int fg_reset_soc(struct max77843_fuelgauge_data *fuelgauge)
{
u8 data[2];
int vfocv, fullcap;
/* delay for current stablization */
msleep(500);
pr_info("%s: Before quick-start - VCELL(%d), VFOCV(%d), VfSOC(%d), RepSOC(%d)\n",
__func__, fg_read_vcell(fuelgauge), fg_read_vfocv(fuelgauge),
fg_read_vfsoc(fuelgauge), fg_read_soc(fuelgauge));
pr_info("%s: Before quick-start - current(%d), avg current(%d)\n",
__func__, fg_read_current(fuelgauge, SEC_BATTEY_CURRENT_MA),
fg_read_avg_current(fuelgauge, SEC_BATTEY_CURRENT_MA));
if (fuelgauge->pdata->check_jig_status &&
!fuelgauge->pdata->check_jig_status()) {
pr_info("%s : Return by No JIG_ON signal\n", __func__);
return 0;
}
max77843_write_word(fuelgauge->i2c, CYCLES_REG, 0);
if (max77843_bulk_read(fuelgauge->i2c, MISCCFG_REG,
2, data) < 0) {
pr_err("%s: Failed to read MiscCFG\n", __func__);
return -1;
}
data[1] |= (0x1 << 2);
if (max77843_bulk_write(fuelgauge->i2c, MISCCFG_REG,
2, data) < 0) {
pr_err("%s: Failed to write MiscCFG\n", __func__);
return -1;
}
msleep(250);
max77843_write_word(fuelgauge->i2c, FULLCAP_REG,
fuelgauge->battery_data->Capacity);
msleep(500);
pr_info("%s: After quick-start - VCELL(%d), VFOCV(%d), VfSOC(%d), RepSOC(%d)\n",
__func__, fg_read_vcell(fuelgauge), fg_read_vfocv(fuelgauge),
fg_read_vfsoc(fuelgauge), fg_read_soc(fuelgauge));
pr_info("%s: After quick-start - current(%d), avg current(%d)\n",
__func__, fg_read_current(fuelgauge, SEC_BATTEY_CURRENT_MA),
fg_read_avg_current(fuelgauge, SEC_BATTEY_CURRENT_MA));
max77843_write_word(fuelgauge->i2c, CYCLES_REG, 0x00a0);
/* P8 is not turned off by Quickstart @3.4V
* (It's not a problem, depend on mode data)
* Power off for factory test(File system, etc..) */
vfocv = fg_read_vfocv(fuelgauge);
if (vfocv < POWER_OFF_VOLTAGE_LOW_MARGIN) {
pr_info("%s: Power off condition(%d)\n", __func__, vfocv);
fullcap = max77843_read_word(fuelgauge->i2c, FULLCAP_REG);
/* FullCAP * 0.009 */
max77843_write_word(fuelgauge->i2c, REMCAP_REP_REG,
(u16)(fullcap * 9 / 1000));
msleep(200);
pr_info("%s: new soc=%d, vfocv=%d\n", __func__,
fg_read_soc(fuelgauge), vfocv);
}
pr_info("%s: Additional step - VfOCV(%d), VfSOC(%d), RepSOC(%d)\n",
__func__, fg_read_vfocv(fuelgauge),
fg_read_vfsoc(fuelgauge), fg_read_soc(fuelgauge));
return 0;
}
int fg_reset_capacity_by_jig_connection(struct max77843_fuelgauge_data *fuelgauge)
{
pr_info("%s: DesignCap = Capacity - 1 (Jig Connection)\n", __func__);
return max77843_write_word(fuelgauge->i2c, DESIGNCAP_REG,
fuelgauge->battery_data->Capacity-1);
}
void fg_low_batt_compensation(struct max77843_fuelgauge_data *fuelgauge,
u32 level)
{
int read_val;
u32 temp;
pr_info("%s: Adjust SOCrep to %d!!\n", __func__, level);
read_val = max77843_read_word(fuelgauge->i2c, FULLCAP_REG);
/* RemCapREP (05h) = FullCap(10h) x 0.0090 */
temp = read_val * (level*90) / 10000;
max77843_write_word(fuelgauge->i2c, REMCAP_REP_REG,
(u16)temp);
}
static int fg_check_status_reg(struct max77843_fuelgauge_data *fuelgauge)
{
u8 status_data[2];
int ret = 0;
/* 1. Check Smn was generatedread */
if (max77843_bulk_read(fuelgauge->i2c, STATUS_REG,
2, status_data) < 0) {
pr_err("%s: Failed to read STATUS_REG\n", __func__);
return -1;
}
#ifdef BATTERY_LOG_MESSAGE
pr_info("%s: addr(0x00), data(0x%04x)\n", __func__,
(status_data[1]<<8) | status_data[0]);
#endif
if (status_data[1] & (0x1 << 2))
ret = 1;
/* 2. clear Status reg */
status_data[1] = 0;
if (max77843_bulk_write(fuelgauge->i2c, STATUS_REG,
2, status_data) < 0) {
pr_info("%s: Failed to write STATUS_REG\n", __func__);
return -1;
}
return ret;
}
int get_fuelgauge_value(struct max77843_fuelgauge_data *fuelgauge, int data)
{
int ret;
switch (data) {
case FG_LEVEL:
ret = fg_read_soc(fuelgauge);
break;
case FG_TEMPERATURE:
ret = fg_read_temp(fuelgauge);
break;
case FG_VOLTAGE:
ret = fg_read_vcell(fuelgauge);
break;
case FG_CURRENT:
ret = fg_read_current(fuelgauge, SEC_BATTEY_CURRENT_MA);
break;
case FG_CURRENT_AVG:
ret = fg_read_avg_current(fuelgauge, SEC_BATTEY_CURRENT_MA);
break;
case FG_CHECK_STATUS:
ret = fg_check_status_reg(fuelgauge);
break;
case FG_RAW_SOC:
ret = fg_read_rawsoc(fuelgauge);
break;
case FG_VF_SOC:
ret = fg_read_vfsoc(fuelgauge);
break;
case FG_AV_SOC:
ret = fg_read_avsoc(fuelgauge);
break;
case FG_FULLCAP:
ret = fg_read_fullcap(fuelgauge);
break;
case FG_MIXCAP:
ret = fg_read_mixcap(fuelgauge);
break;
case FG_AVCAP:
ret = fg_read_avcap(fuelgauge);
break;
case FG_REPCAP:
ret = fg_read_repcap(fuelgauge);
break;
default:
ret = -1;
break;
}
return ret;
}
int max77843_alert_init(struct max77843_fuelgauge_data *fuelgauge, int soc)
{
u8 misccgf_data[2];
u8 salrt_data[2];
u8 config_data[2];
u8 valrt_data[2];
u8 talrt_data[2];
u16 read_data = 0;
fuelgauge->is_fuel_alerted = false;
/* Using RepSOC */
if (max77843_bulk_read(fuelgauge->i2c, MISCCFG_REG, 2,
misccgf_data) < 0) {
pr_err("%s: Failed to read MISCCFG_REG\n", __func__);
return -1;
}
misccgf_data[0] = misccgf_data[0] & ~(0x03);
if (max77843_bulk_write(fuelgauge->i2c, MISCCFG_REG,
2, misccgf_data) < 0) {
pr_info("%s: Failed to write MISCCFG_REG\n", __func__);
return -1;
}
/* SALRT Threshold setting */
salrt_data[1] = 0xff;
salrt_data[0] = soc;
if (max77843_bulk_write(fuelgauge->i2c, SALRT_THRESHOLD_REG,
2, salrt_data) < 0) {
pr_info("%s: Failed to write SALRT_THRESHOLD_REG\n", __func__);
return -1;
}
/* Reset VALRT Threshold setting (disable) */
valrt_data[1] = 0xFF;
valrt_data[0] = 0x00;
if (max77843_bulk_write(fuelgauge->i2c, VALRT_THRESHOLD_REG,
2, valrt_data) < 0) {
pr_info("%s: Failed to write VALRT_THRESHOLD_REG\n", __func__);
return -1;
}
read_data = max77843_read_word(fuelgauge->i2c, (u8)VALRT_THRESHOLD_REG);
if (read_data != 0xff00)
pr_err("%s: VALRT_THRESHOLD_REG is not valid (0x%x)\n",
__func__, read_data);
/* Reset TALRT Threshold setting (disable) */
talrt_data[1] = 0x7F;
talrt_data[0] = 0x80;
if (max77843_bulk_write(fuelgauge->i2c, TALRT_THRESHOLD_REG,
2, talrt_data) < 0) {
pr_info("%s: Failed to write TALRT_THRESHOLD_REG\n", __func__);
return -1;
}
read_data = max77843_read_word(fuelgauge->i2c, (u8)TALRT_THRESHOLD_REG);
if (read_data != 0x7f80)
pr_err("%s: TALRT_THRESHOLD_REG is not valid (0x%x)\n",
__func__, read_data);
/*mdelay(100);*/
/* Enable SOC alerts */
if (max77843_bulk_read(fuelgauge->i2c, CONFIG_REG,
2, config_data) < 0) {
pr_err("%s: Failed to read CONFIG_REG\n", __func__);
return -1;
}
config_data[0] = config_data[0] | (0x1 << 2);
if (max77843_bulk_write(fuelgauge->i2c, CONFIG_REG,
2, config_data) < 0) {
pr_info("%s: Failed to write CONFIG_REG\n", __func__);
return -1;
}
max77843_update_reg(fuelgauge->pmic,
MAX77843_PMIC_REG_INTSRC_MASK,
~MAX77843_IRQSRC_FG,
MAX77843_IRQSRC_FG);
pr_info("[%s] SALRT(0x%02x%02x), VALRT(0x%02x%02x), CONFIG(0x%02x%02x)\n",
__func__,
salrt_data[1], salrt_data[0],
valrt_data[1], valrt_data[0],
config_data[1], config_data[0]);
return 1;
}
static void display_low_batt_comp_cnt(struct max77843_fuelgauge_data *fuelgauge)
{
pr_info("[%d, %d], [%d, %d], ",
fuelgauge->info.low_batt_comp_cnt[0][0],
fuelgauge->info.low_batt_comp_cnt[0][1],
fuelgauge->info.low_batt_comp_cnt[1][0],
fuelgauge->info.low_batt_comp_cnt[1][1]);
pr_info("[%d, %d], [%d, %d], [%d, %d]\n",
fuelgauge->info.low_batt_comp_cnt[2][0],
fuelgauge->info.low_batt_comp_cnt[2][1],
fuelgauge->info.low_batt_comp_cnt[3][0],
fuelgauge->info.low_batt_comp_cnt[3][1],
fuelgauge->info.low_batt_comp_cnt[4][0],
fuelgauge->info.low_batt_comp_cnt[4][1]);
}
static void add_low_batt_comp_cnt(struct max77843_fuelgauge_data *fuelgauge,
int range, int level)
{
int i;
int j;
/* Increase the requested count value, and reset others. */
fuelgauge->info.low_batt_comp_cnt[range-1][level/2]++;
for (i = 0; i < LOW_BATT_COMP_RANGE_NUM; i++) {
for (j = 0; j < LOW_BATT_COMP_LEVEL_NUM; j++) {
if (i == range-1 && j == level/2)
continue;
else
fuelgauge->info.low_batt_comp_cnt[i][j] = 0;
}
}
}
void prevent_early_poweroff(struct max77843_fuelgauge_data *fuelgauge,
int vcell, int *fg_soc)
{
int soc = 0;
int read_val;
soc = fg_read_soc(fuelgauge);
/* No need to write REMCAP_REP in below normal cases */
if (soc > POWER_OFF_SOC_HIGH_MARGIN ||
vcell > fuelgauge->battery_data->low_battery_comp_voltage)
return;
pr_info("%s: soc=%d, vcell=%d\n", __func__, soc, vcell);
if (vcell > POWER_OFF_VOLTAGE_HIGH_MARGIN) {
read_val = max77843_read_word(fuelgauge->i2c, FULLCAP_REG);
/* FullCAP * 0.013 */
max77843_write_word(fuelgauge->i2c, REMCAP_REP_REG,
(u16)(read_val * 13 / 1000));
msleep(200);
*fg_soc = fg_read_soc(fuelgauge);
pr_info("%s: new soc=%d, vcell=%d\n", __func__, *fg_soc, vcell);
}
}
void reset_low_batt_comp_cnt(struct max77843_fuelgauge_data *fuelgauge)
{
memset(fuelgauge->info.low_batt_comp_cnt, 0,
sizeof(fuelgauge->info.low_batt_comp_cnt));
}
static int check_low_batt_comp_condition(
struct max77843_fuelgauge_data *fuelgauge,
int *nLevel)
{
int i;
int j;
int ret = 0;
for (i = 0; i < LOW_BATT_COMP_RANGE_NUM; i++) {
for (j = 0; j < LOW_BATT_COMP_LEVEL_NUM; j++) {
if (fuelgauge->info.low_batt_comp_cnt[i][j] >=
MAX_LOW_BATT_CHECK_CNT) {
display_low_batt_comp_cnt(fuelgauge);
ret = 1;
*nLevel = j*2 + 1;
break;
}
}
}
return ret;
}
static int get_low_batt_threshold(struct max77843_fuelgauge_data *fuelgauge,
int range, int nCurrent, int level)
{
int ret = 0;
ret = fuelgauge->battery_data->low_battery_table[range][OFFSET] +
((nCurrent *
fuelgauge->battery_data->low_battery_table[range][SLOPE]) /
1000);
return ret;
}
int low_batt_compensation(struct max77843_fuelgauge_data *fuelgauge,
int fg_soc, int fg_vcell, int fg_current)
{
int fg_avg_current = 0;
int fg_min_current = 0;
int new_level = 0;
int i, table_size;
/* Not charging, Under low battery comp voltage */
if (fg_vcell <= fuelgauge->battery_data->low_battery_comp_voltage) {
fg_avg_current = fg_read_avg_current(fuelgauge,
SEC_BATTEY_CURRENT_MA);
fg_min_current = min(fg_avg_current, fg_current);
table_size =
sizeof(fuelgauge->battery_data->low_battery_table) /
(sizeof(s16)*TABLE_MAX);
for (i = 1; i < CURRENT_RANGE_MAX_NUM; i++) {
if ((fg_min_current >= fuelgauge->battery_data->
low_battery_table[i-1][RANGE]) &&
(fg_min_current < fuelgauge->battery_data->
low_battery_table[i][RANGE])) {
if (fg_soc >= 10 && fg_vcell <
get_low_batt_threshold(fuelgauge,
i, fg_min_current, 1)) {
add_low_batt_comp_cnt(
fuelgauge, i, 1);
} else {
reset_low_batt_comp_cnt(fuelgauge);
}
}
}
if (check_low_batt_comp_condition(fuelgauge, &new_level)) {
fg_low_batt_compensation(fuelgauge, new_level);
reset_low_batt_comp_cnt(fuelgauge);
/* Do not update soc right after
* low battery compensation
* to prevent from powering-off suddenly
*/
pr_info("%s: SOC is set to %d by low compensation!!\n",
__func__, fg_read_soc(fuelgauge));
}
}
/* Prevent power off over 3500mV */
prevent_early_poweroff(fuelgauge, fg_vcell, &fg_soc);
return fg_soc;
}
static bool fuelgauge_recovery_handler(struct max77843_fuelgauge_data *fuelgauge)
{
if (fuelgauge->info.soc < LOW_BATTERY_SOC_REDUCE_UNIT) {
fuelgauge->info.is_low_batt_alarm = false;
} else {
pr_err("%s: Reduce the Reported SOC by 1%%\n",
__func__);
fuelgauge->info.soc -=
LOW_BATTERY_SOC_REDUCE_UNIT;
pr_err("%s: New Reduced RepSOC (%d)\n",
__func__, fuelgauge->info.soc);
}
return fuelgauge->info.is_low_batt_alarm;
}