forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bq2415x_charger.c
1751 lines (1507 loc) · 44.3 KB
/
bq2415x_charger.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
/*
* bq2415x charger driver
*
* Copyright (C) 2011-2013 Pali Rohár <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* Datasheets:
* http://www.ti.com/product/bq24150
* http://www.ti.com/product/bq24150a
* http://www.ti.com/product/bq24152
* http://www.ti.com/product/bq24153
* http://www.ti.com/product/bq24153a
* http://www.ti.com/product/bq24155
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/param.h>
#include <linux/err.h>
#include <linux/workqueue.h>
#include <linux/sysfs.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/idr.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/power/bq2415x_charger.h>
/* timeout for resetting chip timer */
#define BQ2415X_TIMER_TIMEOUT 10
#define BQ2415X_REG_STATUS 0x00
#define BQ2415X_REG_CONTROL 0x01
#define BQ2415X_REG_VOLTAGE 0x02
#define BQ2415X_REG_VENDER 0x03
#define BQ2415X_REG_CURRENT 0x04
/* reset state for all registers */
#define BQ2415X_RESET_STATUS BIT(6)
#define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5))
#define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3))
#define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7))
/* status register */
#define BQ2415X_BIT_TMR_RST 7
#define BQ2415X_BIT_OTG 7
#define BQ2415X_BIT_EN_STAT 6
#define BQ2415X_MASK_STAT (BIT(4)|BIT(5))
#define BQ2415X_SHIFT_STAT 4
#define BQ2415X_BIT_BOOST 3
#define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2))
#define BQ2415X_SHIFT_FAULT 0
/* control register */
#define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7))
#define BQ2415X_SHIFT_LIMIT 6
#define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5))
#define BQ2415X_SHIFT_VLOWV 4
#define BQ2415X_BIT_TE 3
#define BQ2415X_BIT_CE 2
#define BQ2415X_BIT_HZ_MODE 1
#define BQ2415X_BIT_OPA_MODE 0
/* voltage register */
#define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
#define BQ2415X_SHIFT_VO 2
#define BQ2415X_BIT_OTG_PL 1
#define BQ2415X_BIT_OTG_EN 0
/* vender register */
#define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7))
#define BQ2415X_SHIFT_VENDER 5
#define BQ2415X_MASK_PN (BIT(3)|BIT(4))
#define BQ2415X_SHIFT_PN 3
#define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2))
#define BQ2415X_SHIFT_REVISION 0
/* current register */
#define BQ2415X_MASK_RESET BIT(7)
#define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6))
#define BQ2415X_SHIFT_VI_CHRG 4
/* N/A BIT(3) */
#define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2))
#define BQ2415X_SHIFT_VI_TERM 0
enum bq2415x_command {
BQ2415X_TIMER_RESET,
BQ2415X_OTG_STATUS,
BQ2415X_STAT_PIN_STATUS,
BQ2415X_STAT_PIN_ENABLE,
BQ2415X_STAT_PIN_DISABLE,
BQ2415X_CHARGE_STATUS,
BQ2415X_BOOST_STATUS,
BQ2415X_FAULT_STATUS,
BQ2415X_CHARGE_TERMINATION_STATUS,
BQ2415X_CHARGE_TERMINATION_ENABLE,
BQ2415X_CHARGE_TERMINATION_DISABLE,
BQ2415X_CHARGER_STATUS,
BQ2415X_CHARGER_ENABLE,
BQ2415X_CHARGER_DISABLE,
BQ2415X_HIGH_IMPEDANCE_STATUS,
BQ2415X_HIGH_IMPEDANCE_ENABLE,
BQ2415X_HIGH_IMPEDANCE_DISABLE,
BQ2415X_BOOST_MODE_STATUS,
BQ2415X_BOOST_MODE_ENABLE,
BQ2415X_BOOST_MODE_DISABLE,
BQ2415X_OTG_LEVEL,
BQ2415X_OTG_ACTIVATE_HIGH,
BQ2415X_OTG_ACTIVATE_LOW,
BQ2415X_OTG_PIN_STATUS,
BQ2415X_OTG_PIN_ENABLE,
BQ2415X_OTG_PIN_DISABLE,
BQ2415X_VENDER_CODE,
BQ2415X_PART_NUMBER,
BQ2415X_REVISION,
};
enum bq2415x_chip {
BQUNKNOWN,
BQ24150,
BQ24150A,
BQ24151,
BQ24151A,
BQ24152,
BQ24153,
BQ24153A,
BQ24155,
BQ24156,
BQ24156A,
BQ24158,
};
static char *bq2415x_chip_name[] = {
"unknown",
"bq24150",
"bq24150a",
"bq24151",
"bq24151a",
"bq24152",
"bq24153",
"bq24153a",
"bq24155",
"bq24156",
"bq24156a",
"bq24158",
};
struct bq2415x_device {
struct device *dev;
struct bq2415x_platform_data init_data;
struct power_supply charger;
struct delayed_work work;
struct power_supply *notify_psy;
struct notifier_block nb;
enum bq2415x_mode reported_mode;/* mode reported by hook function */
enum bq2415x_mode mode; /* current configured mode */
enum bq2415x_chip chip;
const char *timer_error;
char *model;
char *name;
int autotimer; /* 1 - if driver automatically reset timer, 0 - not */
int automode; /* 1 - enabled, 0 - disabled; -1 - not supported */
int id;
};
/* each registered chip must have unique id */
static DEFINE_IDR(bq2415x_id);
static DEFINE_MUTEX(bq2415x_id_mutex);
static DEFINE_MUTEX(bq2415x_timer_mutex);
static DEFINE_MUTEX(bq2415x_i2c_mutex);
/**** i2c read functions ****/
/* read value from register */
static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
{
struct i2c_client *client = to_i2c_client(bq->dev);
struct i2c_msg msg[2];
u8 val;
int ret;
if (!client->adapter)
return -ENODEV;
msg[0].addr = client->addr;
msg[0].flags = 0;
msg[0].buf = ®
msg[0].len = sizeof(reg);
msg[1].addr = client->addr;
msg[1].flags = I2C_M_RD;
msg[1].buf = &val;
msg[1].len = sizeof(val);
mutex_lock(&bq2415x_i2c_mutex);
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
mutex_unlock(&bq2415x_i2c_mutex);
if (ret < 0)
return ret;
return val;
}
/* read value from register, apply mask and right shift it */
static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
u8 mask, u8 shift)
{
int ret;
if (shift > 8)
return -EINVAL;
ret = bq2415x_i2c_read(bq, reg);
if (ret < 0)
return ret;
return (ret & mask) >> shift;
}
/* read value from register and return one specified bit */
static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
{
if (bit > 8)
return -EINVAL;
return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
}
/**** i2c write functions ****/
/* write value to register */
static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
{
struct i2c_client *client = to_i2c_client(bq->dev);
struct i2c_msg msg[1];
u8 data[2];
int ret;
data[0] = reg;
data[1] = val;
msg[0].addr = client->addr;
msg[0].flags = 0;
msg[0].buf = data;
msg[0].len = ARRAY_SIZE(data);
mutex_lock(&bq2415x_i2c_mutex);
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
mutex_unlock(&bq2415x_i2c_mutex);
/* i2c_transfer returns number of messages transferred */
if (ret < 0)
return ret;
else if (ret != 1)
return -EIO;
return 0;
}
/* read value from register, change it with mask left shifted and write back */
static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
u8 mask, u8 shift)
{
int ret;
if (shift > 8)
return -EINVAL;
ret = bq2415x_i2c_read(bq, reg);
if (ret < 0)
return ret;
ret &= ~mask;
ret |= val << shift;
return bq2415x_i2c_write(bq, reg, ret);
}
/* change only one bit in register */
static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
bool val, u8 bit)
{
if (bit > 8)
return -EINVAL;
return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
}
/**** global functions ****/
/* exec command function */
static int bq2415x_exec_command(struct bq2415x_device *bq,
enum bq2415x_command command)
{
int ret;
switch (command) {
case BQ2415X_TIMER_RESET:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
1, BQ2415X_BIT_TMR_RST);
case BQ2415X_OTG_STATUS:
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
BQ2415X_BIT_OTG);
case BQ2415X_STAT_PIN_STATUS:
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
BQ2415X_BIT_EN_STAT);
case BQ2415X_STAT_PIN_ENABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
BQ2415X_BIT_EN_STAT);
case BQ2415X_STAT_PIN_DISABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
BQ2415X_BIT_EN_STAT);
case BQ2415X_CHARGE_STATUS:
return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
case BQ2415X_BOOST_STATUS:
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
BQ2415X_BIT_BOOST);
case BQ2415X_FAULT_STATUS:
return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
case BQ2415X_CHARGE_TERMINATION_STATUS:
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
BQ2415X_BIT_TE);
case BQ2415X_CHARGE_TERMINATION_ENABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
1, BQ2415X_BIT_TE);
case BQ2415X_CHARGE_TERMINATION_DISABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
0, BQ2415X_BIT_TE);
case BQ2415X_CHARGER_STATUS:
ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
BQ2415X_BIT_CE);
if (ret < 0)
return ret;
else
return ret > 0 ? 0 : 1;
case BQ2415X_CHARGER_ENABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
0, BQ2415X_BIT_CE);
case BQ2415X_CHARGER_DISABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
1, BQ2415X_BIT_CE);
case BQ2415X_HIGH_IMPEDANCE_STATUS:
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
BQ2415X_BIT_HZ_MODE);
case BQ2415X_HIGH_IMPEDANCE_ENABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
1, BQ2415X_BIT_HZ_MODE);
case BQ2415X_HIGH_IMPEDANCE_DISABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
0, BQ2415X_BIT_HZ_MODE);
case BQ2415X_BOOST_MODE_STATUS:
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
BQ2415X_BIT_OPA_MODE);
case BQ2415X_BOOST_MODE_ENABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
1, BQ2415X_BIT_OPA_MODE);
case BQ2415X_BOOST_MODE_DISABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
0, BQ2415X_BIT_OPA_MODE);
case BQ2415X_OTG_LEVEL:
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
BQ2415X_BIT_OTG_PL);
case BQ2415X_OTG_ACTIVATE_HIGH:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
1, BQ2415X_BIT_OTG_PL);
case BQ2415X_OTG_ACTIVATE_LOW:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
0, BQ2415X_BIT_OTG_PL);
case BQ2415X_OTG_PIN_STATUS:
return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
BQ2415X_BIT_OTG_EN);
case BQ2415X_OTG_PIN_ENABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
1, BQ2415X_BIT_OTG_EN);
case BQ2415X_OTG_PIN_DISABLE:
return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
0, BQ2415X_BIT_OTG_EN);
case BQ2415X_VENDER_CODE:
return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER);
case BQ2415X_PART_NUMBER:
return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
BQ2415X_MASK_PN, BQ2415X_SHIFT_PN);
case BQ2415X_REVISION:
return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION);
}
return -EINVAL;
}
/* detect chip type */
static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq)
{
struct i2c_client *client = to_i2c_client(bq->dev);
int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER);
if (ret < 0)
return ret;
switch (client->addr) {
case 0x6b:
switch (ret) {
case 0:
if (bq->chip == BQ24151A)
return bq->chip;
else
return BQ24151;
case 1:
if (bq->chip == BQ24150A ||
bq->chip == BQ24152 ||
bq->chip == BQ24155)
return bq->chip;
else
return BQ24150;
case 2:
if (bq->chip == BQ24153A)
return bq->chip;
else
return BQ24153;
default:
return BQUNKNOWN;
}
break;
case 0x6a:
switch (ret) {
case 0:
if (bq->chip == BQ24156A)
return bq->chip;
else
return BQ24156;
case 2:
return BQ24158;
default:
return BQUNKNOWN;
}
break;
}
return BQUNKNOWN;
}
/* detect chip revision */
static int bq2415x_detect_revision(struct bq2415x_device *bq)
{
int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
int chip = bq2415x_detect_chip(bq);
if (ret < 0 || chip < 0)
return -1;
switch (chip) {
case BQ24150:
case BQ24150A:
case BQ24151:
case BQ24151A:
case BQ24152:
if (ret >= 0 && ret <= 3)
return ret;
else
return -1;
case BQ24153:
case BQ24153A:
case BQ24156:
case BQ24156A:
case BQ24158:
if (ret == 3)
return 0;
else if (ret == 1)
return 1;
else
return -1;
case BQ24155:
if (ret == 3)
return 3;
else
return -1;
case BQUNKNOWN:
return -1;
}
return -1;
}
/* return chip vender code */
static int bq2415x_get_vender_code(struct bq2415x_device *bq)
{
int ret;
ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
if (ret < 0)
return 0;
/* convert to binary */
return (ret & 0x1) +
((ret >> 1) & 0x1) * 10 +
((ret >> 2) & 0x1) * 100;
}
/* reset all chip registers to default state */
static void bq2415x_reset_chip(struct bq2415x_device *bq)
{
bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
bq->timer_error = NULL;
}
/**** properties functions ****/
/* set current limit in mA */
static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
{
int val;
if (mA <= 100)
val = 0;
else if (mA <= 500)
val = 1;
else if (mA <= 800)
val = 2;
else
val = 3;
return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
}
/* get current limit in mA */
static int bq2415x_get_current_limit(struct bq2415x_device *bq)
{
int ret;
ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
if (ret < 0)
return ret;
else if (ret == 0)
return 100;
else if (ret == 1)
return 500;
else if (ret == 2)
return 800;
else if (ret == 3)
return 1800;
return -EINVAL;
}
/* set weak battery voltage in mV */
static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
{
int val;
/* round to 100mV */
if (mV <= 3400 + 50)
val = 0;
else if (mV <= 3500 + 50)
val = 1;
else if (mV <= 3600 + 50)
val = 2;
else
val = 3;
return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
}
/* get weak battery voltage in mV */
static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
{
int ret;
ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
if (ret < 0)
return ret;
return 100 * (34 + ret);
}
/* set battery regulation voltage in mV */
static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
int mV)
{
int val = (mV/10 - 350) / 2;
/*
* According to datasheet, maximum battery regulation voltage is
* 4440mV which is b101111 = 47.
*/
if (val < 0)
val = 0;
else if (val > 47)
return -EINVAL;
return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
}
/* get battery regulation voltage in mV */
static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
{
int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
if (ret < 0)
return ret;
return 10 * (350 + 2*ret);
}
/* set charge current in mA (platform data must provide resistor sense) */
static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA)
{
int val;
if (bq->init_data.resistor_sense <= 0)
return -ENOSYS;
val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
if (val < 0)
val = 0;
else if (val > 7)
val = 7;
return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
BQ2415X_SHIFT_VI_CHRG);
}
/* get charge current in mA (platform data must provide resistor sense) */
static int bq2415x_get_charge_current(struct bq2415x_device *bq)
{
int ret;
if (bq->init_data.resistor_sense <= 0)
return -ENOSYS;
ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
if (ret < 0)
return ret;
return (37400 + 6800*ret) / bq->init_data.resistor_sense;
}
/* set termination current in mA (platform data must provide resistor sense) */
static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA)
{
int val;
if (bq->init_data.resistor_sense <= 0)
return -ENOSYS;
val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
if (val < 0)
val = 0;
else if (val > 7)
val = 7;
return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
BQ2415X_SHIFT_VI_TERM);
}
/* get termination current in mA (platform data must provide resistor sense) */
static int bq2415x_get_termination_current(struct bq2415x_device *bq)
{
int ret;
if (bq->init_data.resistor_sense <= 0)
return -ENOSYS;
ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
if (ret < 0)
return ret;
return (3400 + 3400*ret) / bq->init_data.resistor_sense;
}
/* set default value of property */
#define bq2415x_set_default_value(bq, prop) \
do { \
int ret = 0; \
if (bq->init_data.prop != -1) \
ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
if (ret < 0) \
return ret; \
} while (0)
/* set default values of all properties */
static int bq2415x_set_defaults(struct bq2415x_device *bq)
{
bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE);
bq2415x_set_default_value(bq, current_limit);
bq2415x_set_default_value(bq, weak_battery_voltage);
bq2415x_set_default_value(bq, battery_regulation_voltage);
if (bq->init_data.resistor_sense > 0) {
bq2415x_set_default_value(bq, charge_current);
bq2415x_set_default_value(bq, termination_current);
bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
}
bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
return 0;
}
/**** charger mode functions ****/
/* set charger mode */
static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
{
int ret = 0;
int charger = 0;
int boost = 0;
if (mode == BQ2415X_MODE_BOOST)
boost = 1;
else if (mode != BQ2415X_MODE_OFF)
charger = 1;
if (!charger)
ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
if (!boost)
ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
if (ret < 0)
return ret;
switch (mode) {
case BQ2415X_MODE_OFF:
dev_dbg(bq->dev, "changing mode to: Offline\n");
ret = bq2415x_set_current_limit(bq, 100);
break;
case BQ2415X_MODE_NONE:
dev_dbg(bq->dev, "changing mode to: N/A\n");
ret = bq2415x_set_current_limit(bq, 100);
break;
case BQ2415X_MODE_HOST_CHARGER:
dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n");
ret = bq2415x_set_current_limit(bq, 500);
break;
case BQ2415X_MODE_DEDICATED_CHARGER:
dev_dbg(bq->dev, "changing mode to: Dedicated charger\n");
ret = bq2415x_set_current_limit(bq, 1800);
break;
case BQ2415X_MODE_BOOST: /* Boost mode */
dev_dbg(bq->dev, "changing mode to: Boost\n");
ret = bq2415x_set_current_limit(bq, 100);
break;
}
if (ret < 0)
return ret;
if (charger)
ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
else if (boost)
ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
if (ret < 0)
return ret;
bq2415x_set_default_value(bq, weak_battery_voltage);
bq2415x_set_default_value(bq, battery_regulation_voltage);
bq->mode = mode;
sysfs_notify(&bq->charger.dev->kobj, NULL, "mode");
return 0;
}
static int bq2415x_notifier_call(struct notifier_block *nb,
unsigned long val, void *v)
{
struct bq2415x_device *bq =
container_of(nb, struct bq2415x_device, nb);
struct power_supply *psy = v;
enum bq2415x_mode mode;
union power_supply_propval prop;
int ret;
int mA;
if (val != PSY_EVENT_PROP_CHANGED)
return NOTIFY_OK;
if (psy != bq->notify_psy)
return NOTIFY_OK;
dev_dbg(bq->dev, "notifier call was called\n");
ret = psy->get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
if (ret != 0)
return NOTIFY_OK;
mA = prop.intval;
if (mA == 0)
mode = BQ2415X_MODE_OFF;
else if (mA < 500)
mode = BQ2415X_MODE_NONE;
else if (mA < 1800)
mode = BQ2415X_MODE_HOST_CHARGER;
else
mode = BQ2415X_MODE_DEDICATED_CHARGER;
if (bq->reported_mode == mode)
return NOTIFY_OK;
bq->reported_mode = mode;
/* if automode is not enabled do not tell about reported_mode */
if (bq->automode < 1)
return NOTIFY_OK;
schedule_delayed_work(&bq->work, 0);
return NOTIFY_OK;
}
/**** timer functions ****/
/* enable/disable auto resetting chip timer */
static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
{
mutex_lock(&bq2415x_timer_mutex);
if (bq->autotimer == state) {
mutex_unlock(&bq2415x_timer_mutex);
return;
}
bq->autotimer = state;
if (state) {
schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
bq->timer_error = NULL;
} else {
cancel_delayed_work_sync(&bq->work);
}
mutex_unlock(&bq2415x_timer_mutex);
}
/* called by bq2415x_timer_work on timer error */
static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
{
bq->timer_error = msg;
sysfs_notify(&bq->charger.dev->kobj, NULL, "timer");
dev_err(bq->dev, "%s\n", msg);
if (bq->automode > 0)
bq->automode = 0;
bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
bq2415x_set_autotimer(bq, 0);
}
/* delayed work function for auto resetting chip timer */
static void bq2415x_timer_work(struct work_struct *work)
{
struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
work.work);
int ret;
int error;
int boost;
if (bq->automode > 0 && (bq->reported_mode != bq->mode)) {
sysfs_notify(&bq->charger.dev->kobj, NULL, "reported_mode");
bq2415x_set_mode(bq, bq->reported_mode);
}
if (!bq->autotimer)
return;
ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
if (ret < 0) {
bq2415x_timer_error(bq, "Resetting timer failed");
return;
}
boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
if (boost < 0) {
bq2415x_timer_error(bq, "Unknown error");
return;
}
error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
if (error < 0) {
bq2415x_timer_error(bq, "Unknown error");
return;
}
if (boost) {
switch (error) {
/* Non fatal errors, chip is OK */
case 0: /* No error */
break;
case 6: /* Timer expired */
dev_err(bq->dev, "Timer expired\n");
break;
case 3: /* Battery voltage too low */
dev_err(bq->dev, "Battery voltage to low\n");
break;
/* Fatal errors, disable and reset chip */
case 1: /* Overvoltage protection (chip fried) */
bq2415x_timer_error(bq,
"Overvoltage protection (chip fried)");
return;
case 2: /* Overload */
bq2415x_timer_error(bq, "Overload");
return;
case 4: /* Battery overvoltage protection */
bq2415x_timer_error(bq,
"Battery overvoltage protection");
return;
case 5: /* Thermal shutdown (too hot) */
bq2415x_timer_error(bq,
"Thermal shutdown (too hot)");
return;
case 7: /* N/A */
bq2415x_timer_error(bq, "Unknown error");
return;
}
} else {
switch (error) {
/* Non fatal errors, chip is OK */
case 0: /* No error */
break;
case 2: /* Sleep mode */
dev_err(bq->dev, "Sleep mode\n");
break;
case 3: /* Poor input source */
dev_err(bq->dev, "Poor input source\n");
break;
case 6: /* Timer expired */
dev_err(bq->dev, "Timer expired\n");
break;
case 7: /* No battery */
dev_err(bq->dev, "No battery\n");
break;
/* Fatal errors, disable and reset chip */
case 1: /* Overvoltage protection (chip fried) */
bq2415x_timer_error(bq,
"Overvoltage protection (chip fried)");
return;
case 4: /* Battery overvoltage protection */
bq2415x_timer_error(bq,
"Battery overvoltage protection");
return;
case 5: /* Thermal shutdown (too hot) */
bq2415x_timer_error(bq,
"Thermal shutdown (too hot)");
return;
}
}
schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
}
/**** power supply interface code ****/
static enum power_supply_property bq2415x_power_supply_props[] = {
/* TODO: maybe add more power supply properties */
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_MODEL_NAME,
};
static int bq2415x_power_supply_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{