forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ab8500_fg.c
2642 lines (2274 loc) · 66.7 KB
/
ab8500_fg.c
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
/*
* Copyright (C) ST-Ericsson AB 2012
*
* Main and Back-up battery management driver.
*
* Note: Backup battery management is required in case of Li-Ion battery and not
* for capacitive battery. HREF boards have capacitive battery and hence backup
* battery management is not used and the supported code is available in this
* driver.
*
* License Terms: GNU General Public License v2
* Author:
* Johan Palsson <[email protected]>
* Karl Komierowski <[email protected]>
* Arun R Murthy <[email protected]>
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/kobject.h>
#include <linux/mfd/abx500/ab8500.h>
#include <linux/mfd/abx500.h>
#include <linux/slab.h>
#include <linux/mfd/abx500/ab8500-bm.h>
#include <linux/delay.h>
#include <linux/mfd/abx500/ab8500-gpadc.h>
#include <linux/mfd/abx500.h>
#include <linux/time.h>
#include <linux/completion.h>
#define MILLI_TO_MICRO 1000
#define FG_LSB_IN_MA 1627
#define QLSB_NANO_AMP_HOURS_X10 1129
#define INS_CURR_TIMEOUT (3 * HZ)
#define SEC_TO_SAMPLE(S) (S * 4)
#define NBR_AVG_SAMPLES 20
#define LOW_BAT_CHECK_INTERVAL (2 * HZ)
#define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */
#define BATT_OK_MIN 2360 /* mV */
#define BATT_OK_INCREMENT 50 /* mV */
#define BATT_OK_MAX_NR_INCREMENTS 0xE
/* FG constants */
#define BATT_OVV 0x01
#define interpolate(x, x1, y1, x2, y2) \
((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1))));
#define to_ab8500_fg_device_info(x) container_of((x), \
struct ab8500_fg, fg_psy);
/**
* struct ab8500_fg_interrupts - ab8500 fg interupts
* @name: name of the interrupt
* @isr function pointer to the isr
*/
struct ab8500_fg_interrupts {
char *name;
irqreturn_t (*isr)(int irq, void *data);
};
enum ab8500_fg_discharge_state {
AB8500_FG_DISCHARGE_INIT,
AB8500_FG_DISCHARGE_INITMEASURING,
AB8500_FG_DISCHARGE_INIT_RECOVERY,
AB8500_FG_DISCHARGE_RECOVERY,
AB8500_FG_DISCHARGE_READOUT_INIT,
AB8500_FG_DISCHARGE_READOUT,
AB8500_FG_DISCHARGE_WAKEUP,
};
static char *discharge_state[] = {
"DISCHARGE_INIT",
"DISCHARGE_INITMEASURING",
"DISCHARGE_INIT_RECOVERY",
"DISCHARGE_RECOVERY",
"DISCHARGE_READOUT_INIT",
"DISCHARGE_READOUT",
"DISCHARGE_WAKEUP",
};
enum ab8500_fg_charge_state {
AB8500_FG_CHARGE_INIT,
AB8500_FG_CHARGE_READOUT,
};
static char *charge_state[] = {
"CHARGE_INIT",
"CHARGE_READOUT",
};
enum ab8500_fg_calibration_state {
AB8500_FG_CALIB_INIT,
AB8500_FG_CALIB_WAIT,
AB8500_FG_CALIB_END,
};
struct ab8500_fg_avg_cap {
int avg;
int samples[NBR_AVG_SAMPLES];
__kernel_time_t time_stamps[NBR_AVG_SAMPLES];
int pos;
int nbr_samples;
int sum;
};
struct ab8500_fg_battery_capacity {
int max_mah_design;
int max_mah;
int mah;
int permille;
int level;
int prev_mah;
int prev_percent;
int prev_level;
int user_mah;
};
struct ab8500_fg_flags {
bool fg_enabled;
bool conv_done;
bool charging;
bool fully_charged;
bool force_full;
bool low_bat_delay;
bool low_bat;
bool bat_ovv;
bool batt_unknown;
bool calibrate;
bool user_cap;
bool batt_id_received;
};
struct inst_curr_result_list {
struct list_head list;
int *result;
};
/**
* struct ab8500_fg - ab8500 FG device information
* @dev: Pointer to the structure device
* @node: a list of AB8500 FGs, hence prepared for reentrance
* @irq holds the CCEOC interrupt number
* @vbat: Battery voltage in mV
* @vbat_nom: Nominal battery voltage in mV
* @inst_curr: Instantenous battery current in mA
* @avg_curr: Average battery current in mA
* @bat_temp battery temperature
* @fg_samples: Number of samples used in the FG accumulation
* @accu_charge: Accumulated charge from the last conversion
* @recovery_cnt: Counter for recovery mode
* @high_curr_cnt: Counter for high current mode
* @init_cnt: Counter for init mode
* @recovery_needed: Indicate if recovery is needed
* @high_curr_mode: Indicate if we're in high current mode
* @init_capacity: Indicate if initial capacity measuring should be done
* @turn_off_fg: True if fg was off before current measurement
* @calib_state State during offset calibration
* @discharge_state: Current discharge state
* @charge_state: Current charge state
* @ab8500_fg_complete Completion struct used for the instant current reading
* @flags: Structure for information about events triggered
* @bat_cap: Structure for battery capacity specific parameters
* @avg_cap: Average capacity filter
* @parent: Pointer to the struct ab8500
* @gpadc: Pointer to the struct gpadc
* @pdata: Pointer to the abx500_fg platform data
* @bat: Pointer to the abx500_bm platform data
* @fg_psy: Structure that holds the FG specific battery properties
* @fg_wq: Work queue for running the FG algorithm
* @fg_periodic_work: Work to run the FG algorithm periodically
* @fg_low_bat_work: Work to check low bat condition
* @fg_reinit_work Work used to reset and reinitialise the FG algorithm
* @fg_work: Work to run the FG algorithm instantly
* @fg_acc_cur_work: Work to read the FG accumulator
* @fg_check_hw_failure_work: Work for checking HW state
* @cc_lock: Mutex for locking the CC
* @fg_kobject: Structure of type kobject
*/
struct ab8500_fg {
struct device *dev;
struct list_head node;
int irq;
int vbat;
int vbat_nom;
int inst_curr;
int avg_curr;
int bat_temp;
int fg_samples;
int accu_charge;
int recovery_cnt;
int high_curr_cnt;
int init_cnt;
bool recovery_needed;
bool high_curr_mode;
bool init_capacity;
bool turn_off_fg;
enum ab8500_fg_calibration_state calib_state;
enum ab8500_fg_discharge_state discharge_state;
enum ab8500_fg_charge_state charge_state;
struct completion ab8500_fg_complete;
struct ab8500_fg_flags flags;
struct ab8500_fg_battery_capacity bat_cap;
struct ab8500_fg_avg_cap avg_cap;
struct ab8500 *parent;
struct ab8500_gpadc *gpadc;
struct abx500_fg_platform_data *pdata;
struct abx500_bm_data *bat;
struct power_supply fg_psy;
struct workqueue_struct *fg_wq;
struct delayed_work fg_periodic_work;
struct delayed_work fg_low_bat_work;
struct delayed_work fg_reinit_work;
struct work_struct fg_work;
struct work_struct fg_acc_cur_work;
struct delayed_work fg_check_hw_failure_work;
struct mutex cc_lock;
struct kobject fg_kobject;
};
static LIST_HEAD(ab8500_fg_list);
/**
* ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
* (i.e. the first fuel gauge in the instance list)
*/
struct ab8500_fg *ab8500_fg_get(void)
{
struct ab8500_fg *fg;
if (list_empty(&ab8500_fg_list))
return NULL;
fg = list_first_entry(&ab8500_fg_list, struct ab8500_fg, node);
return fg;
}
/* Main battery properties */
static enum power_supply_property ab8500_fg_props[] = {
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_CURRENT_AVG,
POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
POWER_SUPPLY_PROP_ENERGY_FULL,
POWER_SUPPLY_PROP_ENERGY_NOW,
POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
POWER_SUPPLY_PROP_CHARGE_FULL,
POWER_SUPPLY_PROP_CHARGE_NOW,
POWER_SUPPLY_PROP_CAPACITY,
POWER_SUPPLY_PROP_CAPACITY_LEVEL,
};
/*
* This array maps the raw hex value to lowbat voltage used by the AB8500
* Values taken from the UM0836
*/
static int ab8500_fg_lowbat_voltage_map[] = {
2300 ,
2325 ,
2350 ,
2375 ,
2400 ,
2425 ,
2450 ,
2475 ,
2500 ,
2525 ,
2550 ,
2575 ,
2600 ,
2625 ,
2650 ,
2675 ,
2700 ,
2725 ,
2750 ,
2775 ,
2800 ,
2825 ,
2850 ,
2875 ,
2900 ,
2925 ,
2950 ,
2975 ,
3000 ,
3025 ,
3050 ,
3075 ,
3100 ,
3125 ,
3150 ,
3175 ,
3200 ,
3225 ,
3250 ,
3275 ,
3300 ,
3325 ,
3350 ,
3375 ,
3400 ,
3425 ,
3450 ,
3475 ,
3500 ,
3525 ,
3550 ,
3575 ,
3600 ,
3625 ,
3650 ,
3675 ,
3700 ,
3725 ,
3750 ,
3775 ,
3800 ,
3825 ,
3850 ,
3850 ,
};
static u8 ab8500_volt_to_regval(int voltage)
{
int i;
if (voltage < ab8500_fg_lowbat_voltage_map[0])
return 0;
for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) {
if (voltage < ab8500_fg_lowbat_voltage_map[i])
return (u8) i - 1;
}
/* If not captured above, return index of last element */
return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1;
}
/**
* ab8500_fg_is_low_curr() - Low or high current mode
* @di: pointer to the ab8500_fg structure
* @curr: the current to base or our decision on
*
* Low current mode if the current consumption is below a certain threshold
*/
static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr)
{
/*
* We want to know if we're in low current mode
*/
if (curr > -di->bat->fg_params->high_curr_threshold)
return true;
else
return false;
}
/**
* ab8500_fg_add_cap_sample() - Add capacity to average filter
* @di: pointer to the ab8500_fg structure
* @sample: the capacity in mAh to add to the filter
*
* A capacity is added to the filter and a new mean capacity is calculated and
* returned
*/
static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample)
{
struct timespec ts;
struct ab8500_fg_avg_cap *avg = &di->avg_cap;
getnstimeofday(&ts);
do {
avg->sum += sample - avg->samples[avg->pos];
avg->samples[avg->pos] = sample;
avg->time_stamps[avg->pos] = ts.tv_sec;
avg->pos++;
if (avg->pos == NBR_AVG_SAMPLES)
avg->pos = 0;
if (avg->nbr_samples < NBR_AVG_SAMPLES)
avg->nbr_samples++;
/*
* Check the time stamp for each sample. If too old,
* replace with latest sample
*/
} while (ts.tv_sec - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]);
avg->avg = avg->sum / avg->nbr_samples;
return avg->avg;
}
/**
* ab8500_fg_clear_cap_samples() - Clear average filter
* @di: pointer to the ab8500_fg structure
*
* The capacity filter is is reset to zero.
*/
static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di)
{
int i;
struct ab8500_fg_avg_cap *avg = &di->avg_cap;
avg->pos = 0;
avg->nbr_samples = 0;
avg->sum = 0;
avg->avg = 0;
for (i = 0; i < NBR_AVG_SAMPLES; i++) {
avg->samples[i] = 0;
avg->time_stamps[i] = 0;
}
}
/**
* ab8500_fg_fill_cap_sample() - Fill average filter
* @di: pointer to the ab8500_fg structure
* @sample: the capacity in mAh to fill the filter with
*
* The capacity filter is filled with a capacity in mAh
*/
static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample)
{
int i;
struct timespec ts;
struct ab8500_fg_avg_cap *avg = &di->avg_cap;
getnstimeofday(&ts);
for (i = 0; i < NBR_AVG_SAMPLES; i++) {
avg->samples[i] = sample;
avg->time_stamps[i] = ts.tv_sec;
}
avg->pos = 0;
avg->nbr_samples = NBR_AVG_SAMPLES;
avg->sum = sample * NBR_AVG_SAMPLES;
avg->avg = sample;
}
/**
* ab8500_fg_coulomb_counter() - enable coulomb counter
* @di: pointer to the ab8500_fg structure
* @enable: enable/disable
*
* Enable/Disable coulomb counter.
* On failure returns negative value.
*/
static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable)
{
int ret = 0;
mutex_lock(&di->cc_lock);
if (enable) {
/* To be able to reprogram the number of samples, we have to
* first stop the CC and then enable it again */
ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
AB8500_RTC_CC_CONF_REG, 0x00);
if (ret)
goto cc_err;
/* Program the samples */
ret = abx500_set_register_interruptible(di->dev,
AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
di->fg_samples);
if (ret)
goto cc_err;
/* Start the CC */
ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
AB8500_RTC_CC_CONF_REG,
(CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
if (ret)
goto cc_err;
di->flags.fg_enabled = true;
} else {
/* Clear any pending read requests */
ret = abx500_set_register_interruptible(di->dev,
AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0);
if (ret)
goto cc_err;
ret = abx500_set_register_interruptible(di->dev,
AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0);
if (ret)
goto cc_err;
/* Stop the CC */
ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
AB8500_RTC_CC_CONF_REG, 0);
if (ret)
goto cc_err;
di->flags.fg_enabled = false;
}
dev_dbg(di->dev, " CC enabled: %d Samples: %d\n",
enable, di->fg_samples);
mutex_unlock(&di->cc_lock);
return ret;
cc_err:
dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__);
mutex_unlock(&di->cc_lock);
return ret;
}
/**
* ab8500_fg_inst_curr_start() - start battery instantaneous current
* @di: pointer to the ab8500_fg structure
*
* Returns 0 or error code
* Note: This is part "one" and has to be called before
* ab8500_fg_inst_curr_finalize()
*/
int ab8500_fg_inst_curr_start(struct ab8500_fg *di)
{
u8 reg_val;
int ret;
mutex_lock(&di->cc_lock);
ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
AB8500_RTC_CC_CONF_REG, ®_val);
if (ret < 0)
goto fail;
if (!(reg_val & CC_PWR_UP_ENA)) {
dev_dbg(di->dev, "%s Enable FG\n", __func__);
di->turn_off_fg = true;
/* Program the samples */
ret = abx500_set_register_interruptible(di->dev,
AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
SEC_TO_SAMPLE(10));
if (ret)
goto fail;
/* Start the CC */
ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
AB8500_RTC_CC_CONF_REG,
(CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
if (ret)
goto fail;
} else {
di->turn_off_fg = false;
}
/* Return and WFI */
INIT_COMPLETION(di->ab8500_fg_complete);
enable_irq(di->irq);
/* Note: cc_lock is still locked */
return 0;
fail:
mutex_unlock(&di->cc_lock);
return ret;
}
/**
* ab8500_fg_inst_curr_done() - check if fg conversion is done
* @di: pointer to the ab8500_fg structure
*
* Returns 1 if conversion done, 0 if still waiting
*/
int ab8500_fg_inst_curr_done(struct ab8500_fg *di)
{
return completion_done(&di->ab8500_fg_complete);
}
/**
* ab8500_fg_inst_curr_finalize() - battery instantaneous current
* @di: pointer to the ab8500_fg structure
* @res: battery instantenous current(on success)
*
* Returns 0 or an error code
* Note: This is part "two" and has to be called at earliest 250 ms
* after ab8500_fg_inst_curr_start()
*/
int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res)
{
u8 low, high;
int val;
int ret;
int timeout;
if (!completion_done(&di->ab8500_fg_complete)) {
timeout = wait_for_completion_timeout(&di->ab8500_fg_complete,
INS_CURR_TIMEOUT);
dev_dbg(di->dev, "Finalize time: %d ms\n",
((INS_CURR_TIMEOUT - timeout) * 1000) / HZ);
if (!timeout) {
ret = -ETIME;
disable_irq(di->irq);
dev_err(di->dev, "completion timed out [%d]\n",
__LINE__);
goto fail;
}
}
disable_irq(di->irq);
ret = abx500_mask_and_set_register_interruptible(di->dev,
AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
READ_REQ, READ_REQ);
/* 100uS between read request and read is needed */
usleep_range(100, 100);
/* Read CC Sample conversion value Low and high */
ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
AB8500_GASG_CC_SMPL_CNVL_REG, &low);
if (ret < 0)
goto fail;
ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
AB8500_GASG_CC_SMPL_CNVH_REG, &high);
if (ret < 0)
goto fail;
/*
* negative value for Discharging
* convert 2's compliment into decimal
*/
if (high & 0x10)
val = (low | (high << 8) | 0xFFFFE000);
else
val = (low | (high << 8));
/*
* Convert to unit value in mA
* Full scale input voltage is
* 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA
* Given a 250ms conversion cycle time the LSB corresponds
* to 112.9 nAh. Convert to current by dividing by the conversion
* time in hours (250ms = 1 / (3600 * 4)h)
* 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
*/
val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) /
(1000 * di->bat->fg_res);
if (di->turn_off_fg) {
dev_dbg(di->dev, "%s Disable FG\n", __func__);
/* Clear any pending read requests */
ret = abx500_set_register_interruptible(di->dev,
AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0);
if (ret)
goto fail;
/* Stop the CC */
ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
AB8500_RTC_CC_CONF_REG, 0);
if (ret)
goto fail;
}
mutex_unlock(&di->cc_lock);
(*res) = val;
return 0;
fail:
mutex_unlock(&di->cc_lock);
return ret;
}
/**
* ab8500_fg_inst_curr_blocking() - battery instantaneous current
* @di: pointer to the ab8500_fg structure
* @res: battery instantenous current(on success)
*
* Returns 0 else error code
*/
int ab8500_fg_inst_curr_blocking(struct ab8500_fg *di)
{
int ret;
int res = 0;
ret = ab8500_fg_inst_curr_start(di);
if (ret) {
dev_err(di->dev, "Failed to initialize fg_inst\n");
return 0;
}
ret = ab8500_fg_inst_curr_finalize(di, &res);
if (ret) {
dev_err(di->dev, "Failed to finalize fg_inst\n");
return 0;
}
return res;
}
/**
* ab8500_fg_acc_cur_work() - average battery current
* @work: pointer to the work_struct structure
*
* Updated the average battery current obtained from the
* coulomb counter.
*/
static void ab8500_fg_acc_cur_work(struct work_struct *work)
{
int val;
int ret;
u8 low, med, high;
struct ab8500_fg *di = container_of(work,
struct ab8500_fg, fg_acc_cur_work);
mutex_lock(&di->cc_lock);
ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE,
AB8500_GASG_CC_NCOV_ACCU_CTRL, RD_NCONV_ACCU_REQ);
if (ret)
goto exit;
ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
AB8500_GASG_CC_NCOV_ACCU_LOW, &low);
if (ret < 0)
goto exit;
ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
AB8500_GASG_CC_NCOV_ACCU_MED, &med);
if (ret < 0)
goto exit;
ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
AB8500_GASG_CC_NCOV_ACCU_HIGH, &high);
if (ret < 0)
goto exit;
/* Check for sign bit in case of negative value, 2's compliment */
if (high & 0x10)
val = (low | (med << 8) | (high << 16) | 0xFFE00000);
else
val = (low | (med << 8) | (high << 16));
/*
* Convert to uAh
* Given a 250ms conversion cycle time the LSB corresponds
* to 112.9 nAh.
* 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
*/
di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) /
(100 * di->bat->fg_res);
/*
* Convert to unit value in mA
* Full scale input voltage is
* 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA
* Given a 250ms conversion cycle time the LSB corresponds
* to 112.9 nAh. Convert to current by dividing by the conversion
* time in hours (= samples / (3600 * 4)h)
* 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
*/
di->avg_curr = (val * QLSB_NANO_AMP_HOURS_X10 * 36) /
(1000 * di->bat->fg_res * (di->fg_samples / 4));
di->flags.conv_done = true;
mutex_unlock(&di->cc_lock);
queue_work(di->fg_wq, &di->fg_work);
return;
exit:
dev_err(di->dev,
"Failed to read or write gas gauge registers\n");
mutex_unlock(&di->cc_lock);
queue_work(di->fg_wq, &di->fg_work);
}
/**
* ab8500_fg_bat_voltage() - get battery voltage
* @di: pointer to the ab8500_fg structure
*
* Returns battery voltage(on success) else error code
*/
static int ab8500_fg_bat_voltage(struct ab8500_fg *di)
{
int vbat;
static int prev;
vbat = ab8500_gpadc_convert(di->gpadc, MAIN_BAT_V);
if (vbat < 0) {
dev_err(di->dev,
"%s gpadc conversion failed, using previous value\n",
__func__);
return prev;
}
prev = vbat;
return vbat;
}
/**
* ab8500_fg_volt_to_capacity() - Voltage based capacity
* @di: pointer to the ab8500_fg structure
* @voltage: The voltage to convert to a capacity
*
* Returns battery capacity in per mille based on voltage
*/
static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage)
{
int i, tbl_size;
struct abx500_v_to_cap *tbl;
int cap = 0;
tbl = di->bat->bat_type[di->bat->batt_id].v_to_cap_tbl,
tbl_size = di->bat->bat_type[di->bat->batt_id].n_v_cap_tbl_elements;
for (i = 0; i < tbl_size; ++i) {
if (voltage > tbl[i].voltage)
break;
}
if ((i > 0) && (i < tbl_size)) {
cap = interpolate(voltage,
tbl[i].voltage,
tbl[i].capacity * 10,
tbl[i-1].voltage,
tbl[i-1].capacity * 10);
} else if (i == 0) {
cap = 1000;
} else {
cap = 0;
}
dev_dbg(di->dev, "%s Vbat: %d, Cap: %d per mille",
__func__, voltage, cap);
return cap;
}
/**
* ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
* @di: pointer to the ab8500_fg structure
*
* Returns battery capacity based on battery voltage that is not compensated
* for the voltage drop due to the load
*/
static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di)
{
di->vbat = ab8500_fg_bat_voltage(di);
return ab8500_fg_volt_to_capacity(di, di->vbat);
}
/**
* ab8500_fg_battery_resistance() - Returns the battery inner resistance
* @di: pointer to the ab8500_fg structure
*
* Returns battery inner resistance added with the fuel gauge resistor value
* to get the total resistance in the whole link from gnd to bat+ node.
*/
static int ab8500_fg_battery_resistance(struct ab8500_fg *di)
{
int i, tbl_size;
struct batres_vs_temp *tbl;
int resist = 0;
tbl = di->bat->bat_type[di->bat->batt_id].batres_tbl;
tbl_size = di->bat->bat_type[di->bat->batt_id].n_batres_tbl_elements;
for (i = 0; i < tbl_size; ++i) {
if (di->bat_temp / 10 > tbl[i].temp)
break;
}
if ((i > 0) && (i < tbl_size)) {
resist = interpolate(di->bat_temp / 10,
tbl[i].temp,
tbl[i].resist,
tbl[i-1].temp,
tbl[i-1].resist);
} else if (i == 0) {
resist = tbl[0].resist;
} else {
resist = tbl[tbl_size - 1].resist;
}
dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d"
" fg resistance %d, total: %d (mOhm)\n",
__func__, di->bat_temp, resist, di->bat->fg_res / 10,
(di->bat->fg_res / 10) + resist);
/* fg_res variable is in 0.1mOhm */
resist += di->bat->fg_res / 10;
return resist;
}
/**
* ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
* @di: pointer to the ab8500_fg structure
*
* Returns battery capacity based on battery voltage that is load compensated
* for the voltage drop
*/
static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di)
{
int vbat_comp, res;
int i = 0;
int vbat = 0;
ab8500_fg_inst_curr_start(di);
do {
vbat += ab8500_fg_bat_voltage(di);
i++;
msleep(5);
} while (!ab8500_fg_inst_curr_done(di));
ab8500_fg_inst_curr_finalize(di, &di->inst_curr);
di->vbat = vbat / i;
res = ab8500_fg_battery_resistance(di);
/* Use Ohms law to get the load compensated voltage */
vbat_comp = di->vbat - (di->inst_curr * res) / 1000;
dev_dbg(di->dev, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, "
"R: %dmOhm, Current: %dmA Vbat Samples: %d\n",
__func__, di->vbat, vbat_comp, res, di->inst_curr, i);
return ab8500_fg_volt_to_capacity(di, vbat_comp);
}
/**
* ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
* @di: pointer to the ab8500_fg structure
* @cap_mah: capacity in mAh
*
* Converts capacity in mAh to capacity in permille
*/
static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah)
{
return (cap_mah * 1000) / di->bat_cap.max_mah_design;
}
/**
* ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
* @di: pointer to the ab8500_fg structure
* @cap_pm: capacity in permille
*
* Converts capacity in permille to capacity in mAh
*/
static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm)
{
return cap_pm * di->bat_cap.max_mah_design / 1000;
}
/**
* ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
* @di: pointer to the ab8500_fg structure
* @cap_mah: capacity in mAh
*
* Converts capacity in mAh to capacity in uWh
*/
static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah)
{
u64 div_res;
u32 div_rem;
div_res = ((u64) cap_mah) * ((u64) di->vbat_nom);
div_rem = do_div(div_res, 1000);
/* Make sure to round upwards if necessary */
if (div_rem >= 1000 / 2)
div_res++;
return (int) div_res;
}
/**
* ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
* @di: pointer to the ab8500_fg structure
*
* Return the capacity in mAh based on previous calculated capcity and the FG
* accumulator register value. The filter is filled with this capacity
*/
static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di)
{
dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
__func__,
di->bat_cap.mah,
di->accu_charge);
/* Capacity should not be less than 0 */
if (di->bat_cap.mah + di->accu_charge > 0)
di->bat_cap.mah += di->accu_charge;
else