-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbq40z50_fg.c
1403 lines (1163 loc) · 32.4 KB
/
bq40z50_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
/*
* bq40z50 fuel gauge driver
*
* Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
*
* 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.
*
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
* kind, whether express or implied; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define pr_fmt(fmt) "[bq40z50] %s: " fmt, __func__
#include <linux/module.h>
#include <linux/param.h>
#include <linux/jiffies.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/interrupt.h>
#include <linux/gpio/consumer.h>
#define bq_info pr_info
#define bq_dbg pr_debug
#define bq_err pr_err
#ifdef CONFIG_BQ40Z50_DEBUG
#define bq_log pr_err
#else
#define bq_log pr_info
#endif
#define INVALID_REG_ADDR 0xFF
#define FG_FLAGS_FD BIT(4)
#define FG_FLAGS_FC BIT(5)
#define FG_FLAGS_DSG BIT(6)
#define FG_FLAGS_RCA BIT(9)
#define FG_BM_CAPM BIT(15)
#define FG_MBA_SAFE_CUV BIT(0)
#define FG_MBA_SAFE_COV BIT(1)
#define FG_MBA_SAFE_OCC1 BIT(2)
#define FG_MBA_SAFE_OCC2 BIT(3)
#define FG_MBA_SAFE_OCD1 BIT(4)
#define FG_MBA_SAFE_OCD2 BIT(5)
#define FG_MBA_SAFE_AOLD BIT(7)
#define FG_MBA_SAFE_ASCCL BIT(9)
#define FG_MBA_SAFE_ASCDL BIT(11)
#define FG_MBA_SAFE_OTC BIT(12)
#define FG_MBA_SAFE_OTD BIT(13)
#define FG_MBA_SAFE_CUVC BIT(14)
#define FG_MBA_SAFE_OTF BIT(16)
#define FG_MBA_SAFE_PTO BIT(18)
#define FG_MBA_SAFE_PTOS BIT(19)
#define FG_MBA_SAFE_CTO BIT(20)
#define FG_MBA_SAFE_CTOS BIT(21)
#define FG_MBA_SAFE_OC BIT(22)
#define FG_MBA_SAFE_CHGC BIT(23)
#define FG_MBA_SAFE_CHGV BIT(24)
#define FG_MBA_SAFE_PCHGC BIT(25)
#define FG_MBA_SAFE_UTC BIT(26)
#define FG_MBA_SAFE_UTD BIT(27)
#define FG_MBA_OPS_DSG BIT(1)
#define FG_MBA_OPS_CHG BIT(2)
#define FG_MBA_OPS_PCHG BIT(3)
#define FG_MBA_OPS_FUSE BIT(5)
#define FG_MBA_OPS_BTP_INT BIT(7)
#define FG_MBA_OPS_SEC0 BIT(8)
#define FG_MBA_OPS_SEC1 BIT(9)
#define FG_MBA_OPS_SDV BIT(10)
#define FG_MBA_OPS_SS BIT(11)
#define FG_MBA_OPS_PF BIT(12)
#define FG_MBA_OPS_XDSG BIT(13)
#define FG_MBA_OPS_XDHG BIT(14)
#define FG_MBA_OPS_SLEEP BIT(15)
#define FG_MBA_OPS_SDM BIT(16)
#define FG_MBA_OPS_LED BIT(17)
#define FG_MBA_OPS_AUTH BIT(18)
#define FG_MBA_OPS_AUTOCALM BIT(19)
#define FG_MBA_OPS_CAL BIT(20)
#define FG_MBA_OPS_CAL_OFF BIT(21)
#define FG_MBA_OPS_XL BIT(22)
#define FG_MBA_OPS_SLEEPM BIT(23)
#define FG_MBA_OPS_INIT BIT(24)
#define FG_MBA_OPS_SMBLCAL BIT(25)
#define FG_MBA_OPS_SLPAD BIT(26)
#define FG_MBA_OPS_SLPCC BIT(27)
#define FG_MBA_OPS_CB BIT(28)
#define FG_MBA_OPS_EMSHUT BIT(29)
#define FLAG_TO_BOOL(flag, var) (!!(flag & var))
// Once a minute
#define DEFAULT_POLL_INTERVAL 60
static unsigned int poll_interval = DEFAULT_POLL_INTERVAL;
module_param(poll_interval, uint, 0644);
MODULE_PARM_DESC(poll_interval, "battery polling interval in seconds. 0 disables polling");
enum bq_fg_reg_idx {
BQ_FG_REG_MAC = 0,
BQ_FG_REG_TEMP, /* Battery Temperature */
BQ_FG_REG_VOLT, /* Battery Voltage */
BQ_FG_REG_AI, /* Average Current */
BQ_FG_REG_BATT_STATUS, /* BatteryStatus */
BQ_FG_REG_TTE, /* Time to Empty */
BQ_FG_REG_TTF, /* Time to Full */
BQ_FG_REG_FCC, /* Full Charge Capacity */
BQ_FG_REG_RM, /* Remaining Capacity */
BQ_FG_REG_CC, /* Cycle Count */
BQ_FG_REG_SOC, /* Relative State of Charge */
BQ_FG_REG_SOH, /* State of Health */
BQ_FG_REG_DC, /* Design Capacity */
BQ_FG_REG_MBA, /* ManufacturerBlockAccess*/
BQ_FG_REG_SN, /* Serial Number */
BQ_FG_REG_DV, /* Design Voltage */
BQ_FG_REG_CCVM, /* Constant Max Voltage */
BQ_FG_REG_CCCM, /* Constant Max Current */
BQ_FG_REG_I, /* Momentary current */
BQ_FG_REG_BM, /* Battery Mode */
BQ_FG_REG_OP_STATUS,/* Operation Status */
NUM_REGS,
};
static u8 bq40z50_regs[NUM_REGS] = {
0x00, /* CONTROL */
0x08, /* TEMP */
0x09, /* VOLT */
0x0B, /* AVG CURRENT */
0x16, /* FLAGS */
0x12, /* Time to empty */
0x13, /* Time to full */
0x10, /* Full charge capacity */
0x0F, /* Remaining Capacity */
0x17, /* CycleCount */
0x0D, /* State of Charge */
0x4F, /* State of Health */
0x18, /* Design Capacity */
0x44, /* ManufacturerBlockAccess*/
0x1C, /* Serial Number */
0x19, /* Design Voltage */
0x15, /* Max Charge Voltage */
0x14, /* Max Charge Current */
0x0A, /* Momentary Current */
0x03, /* Battery Mode */
0x54, /* Operation Status */
};
char * bq_fg_reg_cmd_names[] = {
"Control",
"Temperature",
"Battery Voltage",
"Average Current",
"Battery Status",
"Time To Empty",
"Time To Full",
"Full Charge Capacity",
"Remaining Capacity",
"Cycle Count",
"Relative State of Charge",
"State of Health",
"Design Capacity",
"ManufacturerBlockAccess",
"Serial Number",
"Design Voltage",
"Constant Max Voltage",
"Constant Max Current",
"Momentary Current",
"Battery Mode",
"Operation Status"
};
enum bq_fg_mac_cmd {
FG_MAC_CMD_OP_STATUS_L = 0x0000,
FG_MAC_CMD_DEV_TYPE = 0x0001,
FG_MAC_CMD_FW_VER = 0x0002,
FG_MAC_CMD_HW_VER = 0x0003,
FG_MAC_CMD_IF_SIG = 0x0004,
FG_MAC_CMD_CHEM_ID = 0x0006,
FG_MAC_CMD_GAUGING = 0x0021,
FG_MAC_CMD_SEAL = 0x0030,
FG_MAC_CMD_DEV_RESET = 0x0041,
FG_MAC_CMD_SAFE_ALERT = 0x0050,
FG_MAC_CMD_SAFE_STATUS = 0x0051,
FG_MAC_CMD_GSTATUS_3 = 0x0075,
FG_MAC_CMD_OP_STATUS = 0x0054,
};
enum {
SEAL_STATE_RSVED,
SEAL_STATE_UNSEALED,
SEAL_STATE_SEALED,
SEAL_STATE_FA,
};
enum bq_fg_device {
BQ40Z50,
};
static const unsigned char *device2str[] = {
"bq40z50",
};
struct bq_fg_chip {
struct device *dev;
struct i2c_client *client;
struct mutex i2c_rw_lock;
struct mutex data_lock;
struct mutex irq_complete;
bool irq_waiting;
bool irq_disabled;
bool resume_completed;
int fw_ver;
int df_ver;
u8 chip;
u8 regs[NUM_REGS];
/* status tracking */
bool batt_fc;
bool batt_fd; /* full depleted */
bool batt_dsg;
bool batt_rca; /* remaining capacity alarm */
union{
u16 bm_intval;
struct{
unsigned bm_icc : 1; /* Internal Charge Controller */
unsigned bm_pbs : 1; /* Primary Battery Support */
unsigned bm_rsvd1 : 5; /* Reserved, do not use */
unsigned bm_cf : 1; /* Conditioning flag */
unsigned bm_cc : 1; /* Condition flag */
unsigned bm_pb : 1; /* Primary battery */
unsigned bm_rdvd2 : 3; /* Reserved, do not use */
unsigned bm_am : 1; /* Alarm Mode */
unsigned bm_chgm : 1; /* Charger Mode */
unsigned bm_capm : 1; /* Capacity reporting units */
};
};
int seal_state; /* 0 - Full Access, 1 - Unsealed, 2 - Sealed */
u16 batt_tte;
u16 batt_ttf;
u16 batt_soc;
u16 batt_fcc; /* Full charge capacity */
u16 batt_rm; /* Remaining capacity */
u16 batt_dc; /* Design Capacity */
u16 batt_dv; /* Design Volt */
u16 batt_volt;
u16 batt_temp;
s16 batt_curr;
s16 batt_curr_avg;
u16 batt_sn;
u16 batt_cccm;
u16 batt_ccvm;
u16 batt_cyclecnt; /* cycle count */
/* debug */
int skip_reads;
int skip_writes;
int fake_soc;
int fake_temp;
struct delayed_work monitor_work;
struct power_supply fg_psy;
};
static int __fg_read_word(struct i2c_client *client, u8 reg, u16 *val)
{
s32 ret;
ret = i2c_smbus_read_word_data(client, reg);
if (ret < 0) {
bq_dbg("i2c read word fail: can't read from reg 0x%02X\n", reg);
return ret;
}
*val = (u16)ret;
return 0;
}
static int __fg_write_word(struct i2c_client *client, u8 reg, u16 val)
{
s32 ret;
ret = i2c_smbus_write_word_data(client, reg, val);
if (ret < 0) {
bq_dbg("i2c write word fail: can't write 0x%02X to reg 0x%02X\n",
val, reg);
return ret;
}
return 0;
}
static int __fg_read_block(struct i2c_client *client, u8 reg, u8 *buf, u8 len)
{
int ret;
/* len is ignored due to smbus block reading contains Num of bytes to be returned */
ret = i2c_smbus_read_block_data(client, reg, buf);
return ret;
}
static int __fg_write_block(struct i2c_client *client, u8 reg, u8 *buf, u8 len)
{
int ret;
ret = i2c_smbus_write_block_data(client, reg, len, buf);
return ret;
}
static int fg_read_word(struct bq_fg_chip *bq, u8 reg, u16 *val)
{
int ret;
if (bq->skip_reads) {
*val = 0;
return 0;
}
mutex_lock(&bq->i2c_rw_lock);
ret = __fg_read_word(bq->client, reg, val);
mutex_unlock(&bq->i2c_rw_lock);
return ret;
}
static int fg_write_word(struct bq_fg_chip *bq, u8 reg, u16 val)
{
int ret;
if (bq->skip_writes)
return 0;
mutex_lock(&bq->i2c_rw_lock);
ret = __fg_write_word(bq->client, reg, val);
mutex_unlock(&bq->i2c_rw_lock);
return ret;
}
static int fg_read_block(struct bq_fg_chip *bq, u8 reg, u8 *buf, u8 len)
{
int ret;
if (bq->skip_reads)
return 0;
mutex_lock(&bq->i2c_rw_lock);
ret = __fg_read_block(bq->client, reg, buf, len);
mutex_unlock(&bq->i2c_rw_lock);
return ret;
}
static int fg_write_block(struct bq_fg_chip *bq, u8 reg, u8 *data, u8 len)
{
int ret;
if (bq->skip_writes)
return 0;
mutex_lock(&bq->i2c_rw_lock);
ret = __fg_write_block(bq->client, reg, data, len);
mutex_unlock(&bq->i2c_rw_lock);
return ret;
}
#ifdef CONFIG_BQ40Z50_DEBUG
static u8 checksum(u8 *data, u8 len)
{
u8 i;
u16 sum = 0;
for (i = 0; i < len; i++)
sum += data[i];
sum &= 0xFF;
return 0xFF - sum;
}
static void fg_print_buf(const char *msg, u8 *buf, u8 len)
{
int i;
int idx = 0;
int num;
u8 strbuf[128];
bq_err("%s buf: ", msg);
for (i = 0; i < len; i++) {
num = sprintf(&strbuf[idx], "%02X ", buf[i]);
idx += num;
}
bq_err("%s\n", strbuf);
}
#else
static void fg_print_buf(const char *msg, u8 *buf, u8 len)
{}
#endif
static int fg_mac_read_block(struct bq_fg_chip *bq, u16 cmd, u8 *buf, u8 len)
{
int ret;
u8 t_buf[40];
u8 t_len;
int i;
t_buf[0] = (u8)(cmd >> 8);
t_buf[1] = (u8)cmd;
ret = fg_write_block(bq, bq->regs[BQ_FG_REG_MBA], t_buf, 2);
if (ret < 0)
return ret;
msleep(100);
ret = fg_read_block(bq, bq->regs[BQ_FG_REG_MBA], t_buf, 36);
if (ret < 0)
return ret;
t_len = ret;
/* ret contains number of data bytes in gauge's response*/
fg_print_buf("mac_read_block", t_buf, t_len);
for (i = 0; i < t_len - 2; i++)
buf[i] = t_buf[i+2];
return 0;
}
#if 0
static int fg_mac_write_block(struct bq_fg_chip *bq, u16 cmd, u8 *data, u8 len)
{
int ret;
u8 cksum;
u8 t_buf[40];
int i;
if (len > 32)
return -1;
t_buf[0] = (u8)(cmd >> 8);
t_buf[1] = (u8)cmd;
for (i = 0; i < len; i++)
t_buf[i+2] = data[i];
/*write command/addr, data*/
ret = fg_write_block(bq, bq->regs[BQ_FG_REG_MBA], t_buf, len + 2);
if (ret < 0)
return ret;
return ret;
}
#endif
static int fg_reset(struct bq_fg_chip *bq){
int ret;
ret = fg_write_word(bq, bq->regs[BQ_FG_REG_MBA], FG_MAC_CMD_DEV_RESET);
if (ret < 0) {
bq_err("Failed to send firmware reset subcommand:%d\n", ret);
}
return ret;
}
static void fg_read_fw_version(struct bq_fg_chip *bq)
{
int ret;
u8 buf[I2C_SMBUS_BLOCK_MAX + 4];
ret = fg_write_word(bq, bq->regs[BQ_FG_REG_MBA], FG_MAC_CMD_FW_VER);
if (ret < 0) {
bq_err("Failed to send firmware version subcommand:%d\n", ret);
return;
}
mdelay(2);
ret = fg_mac_read_block(bq, bq->regs[BQ_FG_REG_MBA], buf, 11);
if (ret < 0) {
bq_err("Failed to read firmware version:%d\n", ret);
return;
}
bq_log("FW Ver:%04X, Build:%04X\n",
buf[2] << 8 | buf[3], buf[4] << 8 | buf[5]);
bq_log("Ztrack Ver:%04X\n", buf[7] << 8 | buf[8]);
}
static int fg_read_word_cmd(struct bq_fg_chip *bq, int reg_id, u16 *value){
int ret;
u16 v = 0xFFFF;
if (bq->regs[reg_id] == INVALID_REG_ADDR) {
bq_err("command id = %02x not supported!\n",reg_id);
return 0;
}
ret = fg_read_word(bq, bq->regs[reg_id], &v);
// Lock moves here - less calls to lock and less code locked
mutex_lock(&bq->data_lock);
*value = (int)v;
mutex_unlock(&bq->data_lock);
if (ret < 0)
bq_dbg("could not read %s, ret = %d\n",bq_fg_reg_cmd_names[reg_id], ret);
return ret;
}
static int fg_read_status(struct bq_fg_chip *bq)
{
int ret;
u16 flags;
ret = fg_read_word_cmd(bq,BQ_FG_REG_BATT_STATUS, &flags);
if (ret < 0)
return ret;
mutex_lock(&bq->data_lock);
bq->batt_fc = FLAG_TO_BOOL(flags, FG_FLAGS_FC);
bq->batt_fd = FLAG_TO_BOOL(flags, FG_FLAGS_FD);
bq->batt_rca = FLAG_TO_BOOL(flags, FG_FLAGS_RCA);
bq->batt_dsg = FLAG_TO_BOOL(flags, FG_FLAGS_DSG);
mutex_unlock(&bq->data_lock);
return 0;
}
static int fg_read_bm(struct bq_fg_chip *bq){
int ret;
u16 flags;
ret = fg_read_word_cmd(bq, BQ_FG_REG_BM, &flags);
if (ret < 0)
return ret;
mutex_lock(&bq->data_lock);
bq->bm_intval = flags;
mutex_unlock(&bq->data_lock);
return 0;
}
static inline int fg_read_ccvm(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_CCVM, &bq->batt_ccvm);
}
static inline int fg_read_cccm(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_CCCM, &bq->batt_cccm);
}
static inline int fg_read_rsoc(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_SOC, &bq->batt_soc);
}
static inline int fg_read_temperature(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_TEMP, &bq->batt_temp);
}
static inline int fg_read_volt(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_VOLT, &bq->batt_volt);
}
static int _fg_read_current(struct bq_fg_chip *bq, s16* val)
{
int ret;
u16 c = 0;
ret = fg_read_word(bq, bq->regs[BQ_FG_REG_I], &c);
if (ret < 0) {
bq_dbg("could not read current, ret = %d\n", ret);
return ret;
}
*val = c;
return ret;
}
static inline int fg_read_current(struct bq_fg_chip *bq)
{
return _fg_read_current(bq,&bq->batt_curr);
}
static inline int fg_read_current_avg(struct bq_fg_chip *bq)
{
return _fg_read_current(bq,&bq->batt_curr_avg);
}
static inline int fg_read_fcc(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_FCC, &bq->batt_fcc);
}
static inline int fg_read_dv(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_DV, &bq->batt_dv);
}
static inline int fg_read_dc(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_DC, &bq->batt_dc);
}
static inline int fg_read_rm(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_RM, &bq->batt_rm);
}
static inline int fg_read_cyclecount(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_CC, &bq->batt_cyclecnt);
}
static int fg_read_tte(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_TTE, &bq->batt_tte);
}
static int fg_read_ttf(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_TTF, &bq->batt_ttf);
}
static int fg_read_sn(struct bq_fg_chip *bq)
{
return fg_read_word_cmd(bq, BQ_FG_REG_SN, &bq->batt_sn);
}
static int fg_get_batt_status(struct bq_fg_chip *bq)
{
if (fg_read_status(bq) < 0)
return POWER_SUPPLY_STATUS_UNKNOWN;
if (bq->batt_fc)
return POWER_SUPPLY_STATUS_FULL;
else if (bq->batt_dsg)
return POWER_SUPPLY_STATUS_DISCHARGING;
else if (bq->batt_curr > 0)
return POWER_SUPPLY_STATUS_CHARGING;
else
return POWER_SUPPLY_STATUS_NOT_CHARGING;
}
static int fg_get_batt_capacity_level(struct bq_fg_chip *bq)
{
if (bq->batt_fc)
return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
else if (bq->batt_rca)
return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
else if (bq->batt_fd)
return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
else
return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
}
static enum power_supply_property fg_props[] = {
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_VOLTAGE_NOW,
POWER_SUPPLY_PROP_CURRENT_NOW,
POWER_SUPPLY_PROP_CAPACITY,
POWER_SUPPLY_PROP_CAPACITY_LEVEL,
POWER_SUPPLY_PROP_TEMP,
POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
/*POWER_SUPPLY_PROP_HEALTH,*//*implement it in battery power_supply*/
POWER_SUPPLY_PROP_CHARGE_FULL,
POWER_SUPPLY_PROP_CHARGE_NOW,
POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
POWER_SUPPLY_PROP_CYCLE_COUNT,
POWER_SUPPLY_PROP_TECHNOLOGY,
POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
POWER_SUPPLY_PROP_SERIAL_NUMBER,
POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_CURRENT_AVG,
POWER_SUPPLY_PROP_TYPE,
// POWER_SUPPLY_PROP_RESISTANCE_ID,
// POWER_SUPPLY_PROP_UPDATE_NOW,
};
static char bq_serial[5];
#define check_no_data() { if (val->intval == 0xFFFF) val->intval = -1; }
#define FG_KELVIN_TO_CELSIUS_DEC(x) (x - 2730)
#define MILLI_TO_MICRO(x) (x * 1000)
static int fg_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct bq_fg_chip *bq = container_of(psy, struct bq_fg_chip, fg_psy);
int ret = 0;
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
val->intval = fg_get_batt_status(bq);
break;
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
ret = fg_read_volt(bq);
val->intval = MILLI_TO_MICRO(bq->batt_volt);
break;
case POWER_SUPPLY_PROP_PRESENT:
if (fg_get_batt_status(bq) > 0)
val->intval = 1;
else
val->intval = 0;
break;
case POWER_SUPPLY_PROP_CURRENT_NOW:
ret = fg_read_current(bq);
val->intval = MILLI_TO_MICRO(-bq->batt_curr);
break;
case POWER_SUPPLY_PROP_CURRENT_AVG:
ret = fg_read_current_avg(bq);
val->intval = MILLI_TO_MICRO(-bq->batt_curr_avg);
break;
case POWER_SUPPLY_PROP_CAPACITY:
ret = fg_read_rsoc(bq);
val->intval = bq->batt_soc;
break;
case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
val->intval = fg_get_batt_capacity_level(bq);
break;
case POWER_SUPPLY_PROP_TEMP:
ret = fg_read_temperature(bq);
val->intval = FG_KELVIN_TO_CELSIUS_DEC(bq->batt_temp);
break;
case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
ret = fg_read_tte(bq);
val->intval = bq->batt_tte;
check_no_data();
break;
case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
ret = fg_read_ttf(bq);
val->intval = bq->batt_ttf;
check_no_data();
break;
case POWER_SUPPLY_PROP_CHARGE_FULL:
ret = fg_read_fcc(bq);
val->intval = MILLI_TO_MICRO(bq->batt_fcc);
break;
case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
ret = fg_read_dc(bq);
val->intval = MILLI_TO_MICRO(bq->batt_dc);
break;
case POWER_SUPPLY_PROP_CYCLE_COUNT:
ret = fg_read_cyclecount(bq);
val->intval = bq->batt_cyclecnt;
break;
case POWER_SUPPLY_PROP_TECHNOLOGY:
val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO;
break;
case POWER_SUPPLY_PROP_SERIAL_NUMBER:
ret = sprintf(bq_serial,"%04x", bq->batt_sn);
val->strval = bq_serial;
break;
case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
ret = fg_read_dv(bq);
val->intval = MILLI_TO_MICRO(bq->batt_dv);
break;
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
ret = fg_read_cccm(bq);
val->intval = MILLI_TO_MICRO(bq->batt_cccm);
break;
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
ret = fg_read_ccvm(bq);
val->intval = MILLI_TO_MICRO(bq->batt_ccvm);
break;
case POWER_SUPPLY_PROP_TYPE:
val->intval = POWER_SUPPLY_TYPE_BATTERY;
break;
case POWER_SUPPLY_PROP_CHARGE_NOW:
ret = fg_read_rm(bq);
val->intval = MILLI_TO_MICRO(bq->batt_rm);
break;
default:
return -EINVAL;
}
return ret;
}
#ifdef CONFIG_BQ40Z50_DEBUG
static void fg_dump_registers(struct bq_fg_chip *bq);
#endif
static int fg_set_property(struct power_supply *psy,
enum power_supply_property prop,
const union power_supply_propval *val)
{
#ifdef CONFIG_BQ40Z50_DEBUG
struct bq_fg_chip *bq = container_of(psy, struct bq_fg_chip, fg_psy);
switch (prop) {
case POWER_SUPPLY_PROP_TEMP:
bq->fake_temp = val->intval;
break;
case POWER_SUPPLY_PROP_CAPACITY:
bq->fake_soc = val->intval;
power_supply_changed(&bq->fg_psy);
break;
default:
return -EINVAL;
}
return 0;
#else
// Not supported in this driver
return -EINVAL;
#endif
}
static int fg_prop_is_writeable(struct power_supply *psy,
enum power_supply_property prop)
{
int ret;
switch (prop) {
#ifdef CONFIG_BQ40Z50_DEBUG
case POWER_SUPPLY_PROP_TEMP:
case POWER_SUPPLY_PROP_CAPACITY:
ret = 1;
break;
#endif
default:
ret = 0;
break;
}
return ret;
}
static int fg_psy_register(struct bq_fg_chip *bq)
{
int ret;
bq->fg_psy.name = "BQ40Z50";
bq->fg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
bq->fg_psy.properties = fg_props;
bq->fg_psy.num_properties = ARRAY_SIZE(fg_props);
bq->fg_psy.get_property = fg_get_property;
bq->fg_psy.set_property = fg_set_property;
bq->fg_psy.property_is_writeable = fg_prop_is_writeable;
#ifdef CONFIG_BQ40Z50_DEBUG
bq_info("properties filled");
#endif
ret = power_supply_register(bq->dev, &bq->fg_psy);
if (ret) {
bq_err("Failed to register fg_psy");
return ret;
}
return 0;
}
static void fg_psy_unregister(struct bq_fg_chip *bq)
{
power_supply_unregister(&bq->fg_psy);
}
static const u8 fg_dump_regs[] = {
0x08, 0x09, 0x0A, 0x0D,
0x0F, 0x10, 0x16, 0x3C,
0x3D, 0x3E, 0x3F, 0x54,
0x55, 0x56, 0x57, 0x71,
0x72, 0x74, 0x75, 0x76,
0x77, 0x78,
};
static ssize_t fg_attr_show_Ra_table(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
struct bq_fg_chip *bq = i2c_get_clientdata(client);
u8 t_buf[40];
u8 temp_buf[40];
int ret;
int i, idx, len;
ret = fg_mac_read_block(bq, 0x4102, t_buf, 32);
if (ret < 0)
return 0;
idx = 0;
len = sprintf(temp_buf, "RaTable:\n");
memcpy(&buf[idx], temp_buf, len);
idx += len;
for (i = 0; i < 15; i++) {
len = sprintf(temp_buf, "%d ", t_buf[i*2] << 8 | t_buf[i*2 + 1]);
memcpy(&buf[idx], temp_buf, len);
idx += len;
}
return idx;
}
static ssize_t fg_attr_show_Qmax(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
struct bq_fg_chip *bq = i2c_get_clientdata(client);
int ret;
union {
u16 _data[I2C_SMBUS_BLOCK_MAX];
struct {
u8 t_buf[I2C_SMBUS_BLOCK_MAX];
u8 temp_buf[I2C_SMBUS_BLOCK_MAX];
};
} block;
int i, idx, len;
memset(block._data, 0, I2C_SMBUS_BLOCK_MAX);
/* GaugingStatus3 register contains Qmax value */
ret = fg_mac_read_block(bq, FG_MAC_CMD_GSTATUS_3, block.t_buf, 24);
if (ret < 0)
return 0;
idx = 0;
for (i = 0; i < 4; i++) {
len = sprintf(block.temp_buf, "Qmax Cell %d = %d\n", i, (block.t_buf[i*2] << 8) | block.t_buf[i*2+1]);
memcpy(&buf[idx], block.temp_buf, len);
idx += len;
}
return idx;
}
static DEVICE_ATTR(RaTable, S_IRUGO, fg_attr_show_Ra_table, NULL);
static DEVICE_ATTR(Qmax, S_IRUGO, fg_attr_show_Qmax, NULL);
static ssize_t fg_attr_reset(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
struct bq_fg_chip *bq = i2c_get_clientdata(client);
int ret;
ret = fg_reset(bq);
if(ret < 0)
return ret;
return count;
}
static DEVICE_ATTR(reset, S_IRUGO, NULL, fg_attr_reset);
#define LITTLE_ENDIAN_DECODE_H4(buf) (buf[3] | (buf[2] << 8) | (buf[1] << 16) | (buf[0] << 24))
static ssize_t fg_attr_show_safe_flags_hex(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
struct bq_fg_chip *bq = i2c_get_clientdata(client);
int ret, len = 0;
u32 flags;
u8 t_buf[34];
memset(t_buf, 0, 34);
ret = fg_mac_read_block(bq, FG_MAC_CMD_SAFE_STATUS, t_buf, 32);
if (ret < 0)
return 0;
flags = LITTLE_ENDIAN_DECODE_H4(t_buf);
len += scnprintf(buf, PAGE_SIZE, "0x%04x\n", flags);
return len;
}