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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
|
/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* 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.
*/
#define pr_fmt(fmt) "%s: " fmt, __func__
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/power_supply.h>
#include <linux/slab.h>
struct smb137c_chip {
struct i2c_client *client;
struct power_supply psy;
struct power_supply *usb_psy;
struct mutex lock;
int charge_current_limit_ua;
int input_current_limit_ua;
int term_current_ua;
bool charging_enabled;
bool otg_mode_enabled;
bool charging_allowed;
bool usb_suspend_enabled;
};
struct input_current_config {
int current_limit_ua;
u8 cmd_b_reg;
u8 var_func_reg;
u8 input_cur_reg;
};
struct term_current_config {
int term_current_ua;
u8 charge_cur_reg;
};
#define INPUT_CURRENT(_current_limit_ua, _cmd_b_reg, _var_func_reg, \
_input_cur_reg) \
{ \
.current_limit_ua = _current_limit_ua, \
.cmd_b_reg = _cmd_b_reg, \
.var_func_reg = _var_func_reg, \
.input_cur_reg = _input_cur_reg, \
}
#define CHARGE_CURRENT_REG 0x00
#define CHARGE_CURRENT_FAST_CHG_MASK 0xE0
#define CHARGE_CURRENT_FAST_CHG_SHIFT 5
#define CHARGE_CURRENT_PRE_CHG_MASK 0x18
#define CHARGE_CURRENT_PRE_CHG_SHIFT 3
#define CHARGE_CURRENT_TERM_CUR_MASK 0x06
#define INPUT_CURRENT_REG 0x01
#define INPUT_CURRENT_LIMIT_MASK 0xE0
#define FLOAT_VOLTAGE_REG 0x02
#define FLOAT_VOLTAGE_MASK 0x7F
#define FLOAT_VOLTAGE_SHIFT 0
#define CTRL_A_REG 0x03
#define CTRL_A_AUTO_RECHARGE_MASK 0x80
#define CTRL_A_AUTO_RECHARGE_ENABLED 0x00
#define CTRL_A_AUTO_RECHARGE_DISABLED 0x80
#define CTRL_A_TERM_CUR_MASK 0x40
#define CTRL_A_TERM_CUR_ENABLED 0x00
#define CTRL_A_TERM_CUR_DISABLED 0x40
#define CTRL_A_THRESH_VOLTAGE_MASK 0x38
#define CTRL_A_THRESH_VOLTAGE_SHIFT 3
#define CTRL_A_VOUTL_MASK 0x02
#define CTRL_A_VOUTL_4250MV 0x00
#define CTRL_A_VOUTL_4460MV 0x02
#define CTRL_A_THERM_MONITOR_MASK 0x01
#define CTRL_A_THERM_MONITOR_ENABLED 0x01
#define CTRL_A_THERM_MONITOR_DISABLED 0x00
#define PIN_CTRL_REG 0x05
#define PIN_CTRL_DEAD_BATT_CHG_MASK 0x80
#define PIN_CTRL_DEAD_BATT_CHG_ENABLED 0x80
#define PIN_CTRL_DEAD_BATT_CHG_DISABLED 0x00
#define PIN_CTRL_OTG_LBR_MASK 0x20
#define PIN_CTRL_OTG 0x00
#define PIN_CTRL_LBR 0x20
#define PIN_CTRL_USB_CUR_LIMIT_MASK 0x10
#define PIN_CTRL_USB_CUR_LIMIT_REG 0x00
#define PIN_CTRL_USB_CUR_LIMIT_PIN 0x10
#define PIN_CTRL_CHG_EN_MASK 0x0C
#define PIN_CTRL_CHG_EN_REG_LOW 0x00
#define PIN_CTRL_CHG_EN_REG_HIGH 0x04
#define PIN_CTRL_CHG_EN_PIN_LOW 0x08
#define PIN_CTRL_CHG_EN_PIN_HIGH 0x0C
#define PIN_CTRL_OTG_CTRL_MASK 0x02
#define PIN_CTRL_OTG_CTRL_REG 0x00
#define PIN_CTRL_OTG_CTRL_PIN 0x02
#define OTG_CTRL_REG 0x06
#define OTG_CTRL_BMD_MASK 0x80
#define OTG_CTRL_BMD_ENABLED 0x80
#define OTG_CTRL_BMD_DISABLED 0x00
#define OTG_CTRL_AUTO_RECHARGE_MASK 0x40
#define OTG_CTRL_AUTO_RECHARGE_75MV 0x00
#define OTG_CTRL_AUTO_RECHARGE_120MV 0x40
#define TEMP_MON_REG 0x08
#define TEMP_MON_THERM_CURRENT_MASK 0xC0
#define TEMP_MON_THERM_CURRENT_SHIFT 6
#define TEMP_MON_TEMP_LOW_MASK 0x38
#define TEMP_MON_TEMP_LOW_SHIFT 3
#define TEMP_MON_TEMP_HIGH_MASK 0x07
#define TEMP_MON_TEMP_HIGH_SHIFT 0
#define SAFETY_TIMER_REG 0x09
#define SAFETY_TIMER_RELOAD_MASK 0x40
#define SAFETY_TIMER_RELOAD_ENABLED 0x40
#define SAFETY_TIMER_RELOAD_DISABLED 0x00
#define SAFETY_TIMER_CHG_TIMEOUT_MASK 0x0C
#define SAFETY_TIMER_CHG_TIMEOUT_SHIFT 2
#define SAFETY_TIMER_PRE_CHG_TIME_MASK 0x03
#define SAFETY_TIMER_PRE_CHG_TIME_SHIFT 0
#define VAR_FUNC_REG 0x0C
#define VAR_FUNC_USB_MODE_MASK 0x80
#define VAR_FUNC_USB_SUSPEND_CTRL_MASK 0x20
#define VAR_FUNC_USB_SUSPEND_CTRL_REG 0x00
#define VAR_FUNC_USB_SUSPEND_CTRL_PIN 0x20
#define VAR_FUNC_BMD_MASK 0x0C
#define VAR_FUNC_BMD_DISABLED 0x00
#define VAR_FUNC_BMD_ALGO_PERIODIC 0x04
#define VAR_FUNC_BMD_ALGO 0x08
#define VAR_FUNC_BMD_THERM 0x0C
#define CMD_A_REG 0x30
#define CMD_A_VOLATILE_WRITE_MASK 0x80
#define CMD_A_VOLATILE_WRITE_ALLOW 0x80
#define CMD_A_VOLATILE_WRITE_DISALLOW 0x00
#define CMD_A_FAST_CHG_MASK 0x40
#define CMD_A_FAST_CHG_ALLOW 0x40
#define CMD_A_FAST_CHG_DISALLOW 0x00
#define CMD_A_OTG_MASK 0x10
#define CMD_A_OTG_ENABLED 0x10
#define CMD_A_OTG_DISABLED 0x00
#define CMD_A_USB_SUSPEND_MASK 0x04
#define CMD_A_USB_SUSPEND_DISABLED 0x00
#define CMD_A_USB_SUSPEND_ENABLED 0x04
#define CMD_A_CHARGING_MASK 0x02
#define CMD_A_CHARGING_ENABLED 0x00
#define CMD_A_CHARGING_DISABLED 0x02
#define CMD_B_REG 0x31
#define CMD_B_USB_MODE_MASK 0x03
#define DEV_ID_REG 0x33
#define DEV_ID_PART_MASK 0x80
#define DEV_ID_PART_SMB137C 0x00
#define DEV_ID_GUI_REV_MASK 0x70
#define DEV_ID_GUI_REV_SHIFT 4
#define DEV_ID_SILICON_REV_MASK 0x0F
#define DEV_ID_SILICON_REV_SHIFT 0
#define IRQ_STAT_A_REG 0x35
#define IRQ_STAT_A_BATT_HOT 0x40
#define IRQ_STAT_A_BATT_COLD 0x10
#define IRQ_STAT_B_REG 0x36
#define IRQ_STAT_B_BATT_OVERVOLT 0x40
#define IRQ_STAT_B_BATT_MISSING 0x10
#define IRQ_STAT_B_BATT_UNDERVOLT 0x04
#define STAT_C_REG 0x3D
#define STAT_C_CHG_ERROR 0x40
#define STAT_C_VBATT_LEVEL_BELOW_2P1V 0x10
#define STAT_C_CHG_STAT_MASK 0x06
#define STAT_C_CHG_STAT_SHIFT 1
#define STAT_C_CHG_ENABLED 0x01
/* Charge status register values */
enum smb137c_charge_status {
CHARGE_STAT_NO_CHG = 0,
CHARGE_STAT_PRE_CHG = 1,
CHARGE_STAT_FAST_CHG = 2,
CHARGE_STAT_TAPER_CHG = 3,
};
#define PRE_CHARGE_CURRENT_MIN_UA 50000
#define PRE_CHARGE_CURRENT_MAX_UA 200000
#define PRE_CHARGE_CURRENT_STEP_UA 50000
#define FLOAT_VOLTAGE_MIN_UV 3460000
#define FLOAT_VOLTAGE_MAX_UV 4730000
#define FLOAT_VOLTAGE_STEP_UV 10000
#define PRE_CHG_THRESH_VOLTAGE_MIN_UV 2400000
#define PRE_CHG_THRESH_VOLTAGE_MAX_UV 3100000
#define PRE_CHG_THRESH_VOLTAGE_STEP_UV 100000
#define USB_MIN_CURRENT_UA 100000
static int smb137c_read_reg(struct smb137c_chip *chip, u8 reg, u8 *val)
{
int rc;
rc = i2c_smbus_read_byte_data(chip->client, reg);
if (rc < 0) {
pr_err("i2c_smbus_read_byte_data failed. reg=0x%02X, rc=%d\n",
reg, rc);
} else {
*val = rc;
rc = 0;
pr_debug("read(0x%02X)=0x%02X\n", reg, *val);
}
return rc;
}
static int smb137c_write_reg(struct smb137c_chip *chip, u8 reg, u8 val)
{
int rc;
rc = i2c_smbus_write_byte_data(chip->client, reg, val);
if (rc < 0)
pr_err("i2c_smbus_write_byte_data failed. reg=0x%02X, rc=%d\n",
reg, rc);
else
pr_debug("write(0x%02X)=0x%02X\n", reg, val);
return rc;
}
static int smb137c_masked_write_reg(struct smb137c_chip *chip, u8 reg, u8 mask,
u8 val)
{
u8 reg_val;
int rc;
pr_debug("masked write(0x%02X), mask=0x%02X, value=0x%02X\n", reg, mask,
val);
rc = smb137c_read_reg(chip, reg, ®_val);
if (rc < 0)
return rc;
val = (reg_val & ~mask) | (val & mask);
if (val != reg_val)
rc = smb137c_write_reg(chip, reg, val);
return rc;
}
static int smb137c_enable_charging(struct smb137c_chip *chip)
{
int rc = 0;
chip->charging_allowed = true;
if (!chip->charging_enabled && chip->charge_current_limit_ua > 0) {
rc = smb137c_masked_write_reg(chip, CMD_A_REG,
CMD_A_CHARGING_MASK, CMD_A_CHARGING_ENABLED);
if (!rc)
chip->charging_enabled = true;
}
if (!rc)
dev_dbg(&chip->client->dev, "%s\n", __func__);
return rc;
}
static int smb137c_disable_charging(struct smb137c_chip *chip)
{
int rc = 0;
chip->charging_allowed = false;
if (chip->charging_enabled) {
rc = smb137c_masked_write_reg(chip, CMD_A_REG,
CMD_A_CHARGING_MASK, CMD_A_CHARGING_DISABLED);
if (!rc)
chip->charging_enabled = false;
}
if (!rc)
dev_dbg(&chip->client->dev, "%s\n", __func__);
return rc;
}
static int smb137c_enable_otg_mode(struct smb137c_chip *chip)
{
int rc = 0;
if (!chip->otg_mode_enabled) {
rc = smb137c_masked_write_reg(chip, CMD_A_REG, CMD_A_OTG_MASK,
CMD_A_OTG_ENABLED);
if (!rc)
chip->otg_mode_enabled = true;
}
if (!rc)
dev_dbg(&chip->client->dev, "%s\n", __func__);
return rc;
}
static int smb137c_disable_otg_mode(struct smb137c_chip *chip)
{
int rc = 0;
if (chip->otg_mode_enabled) {
rc = smb137c_masked_write_reg(chip, CMD_A_REG, CMD_A_OTG_MASK,
CMD_A_OTG_DISABLED);
if (!rc)
chip->otg_mode_enabled = false;
}
if (!rc)
dev_dbg(&chip->client->dev, "%s\n", __func__);
return rc;
}
static int smb137c_enable_usb_suspend(struct smb137c_chip *chip)
{
int rc = 0;
if (!chip->usb_suspend_enabled) {
rc = smb137c_masked_write_reg(chip, CMD_A_REG,
CMD_A_USB_SUSPEND_MASK, CMD_A_USB_SUSPEND_ENABLED);
if (!rc)
chip->usb_suspend_enabled = true;
}
if (!rc)
dev_dbg(&chip->client->dev, "%s\n", __func__);
return rc;
}
static int smb137c_disable_usb_suspend(struct smb137c_chip *chip)
{
int rc = 0;
if (chip->input_current_limit_ua > 0 && chip->usb_suspend_enabled) {
rc = smb137c_masked_write_reg(chip, CMD_A_REG,
CMD_A_USB_SUSPEND_MASK, CMD_A_USB_SUSPEND_DISABLED);
if (!rc)
chip->usb_suspend_enabled = false;
}
if (!rc)
dev_dbg(&chip->client->dev, "%s\n", __func__);
return rc;
}
static struct input_current_config supported_input_current[] = {
INPUT_CURRENT(100000, 0x00, 0x00, 0x00),
INPUT_CURRENT(150000, 0x00, 0x80, 0x00),
INPUT_CURRENT(500000, 0x02, 0x00, 0x00),
INPUT_CURRENT(700000, 0x01, 0x00, 0x00),
INPUT_CURRENT(800000, 0x01, 0x00, 0x20),
INPUT_CURRENT(900000, 0x01, 0x00, 0x40),
INPUT_CURRENT(1000000, 0x01, 0x00, 0x60),
INPUT_CURRENT(1100000, 0x01, 0x00, 0x80),
INPUT_CURRENT(1200000, 0x01, 0x00, 0xA0),
INPUT_CURRENT(1300000, 0x01, 0x00, 0xC0),
INPUT_CURRENT(1500000, 0x01, 0x00, 0xE0),
};
static int smb137c_set_usb_input_current_limit(struct smb137c_chip *chip,
int current_limit_ua)
{
struct input_current_config *config = NULL;
int rc = 0;
int i;
for (i = ARRAY_SIZE(supported_input_current) - 1; i >= 0; i--) {
if (current_limit_ua
>= supported_input_current[i].current_limit_ua) {
config = &supported_input_current[i];
break;
}
}
if (config) {
if (chip->input_current_limit_ua != config->current_limit_ua) {
rc = smb137c_masked_write_reg(chip, INPUT_CURRENT_REG,
INPUT_CURRENT_LIMIT_MASK, config->input_cur_reg);
if (rc)
return rc;
rc = smb137c_masked_write_reg(chip, VAR_FUNC_REG,
VAR_FUNC_USB_MODE_MASK, config->var_func_reg);
if (rc)
return rc;
rc = smb137c_masked_write_reg(chip, CMD_B_REG,
CMD_B_USB_MODE_MASK, config->cmd_b_reg);
if (rc)
return rc;
chip->input_current_limit_ua = config->current_limit_ua;
}
rc = smb137c_disable_usb_suspend(chip);
} else {
chip->input_current_limit_ua = 0;
rc = smb137c_enable_usb_suspend(chip);
}
if (!rc)
dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
chip->input_current_limit_ua);
return rc;
}
static int fast_charge_current_ua[] = {
500000,
650000,
750000,
850000,
950000,
1100000,
1300000,
1500000,
};
static int smb137c_set_charge_current_limit(struct smb137c_chip *chip,
int current_limit_ua)
{
int fast_charge_limit_ua = 0;
int rc = 0;
u8 val = 0;
int i;
for (i = ARRAY_SIZE(fast_charge_current_ua) - 1; i >= 0; i--) {
if (current_limit_ua >= fast_charge_current_ua[i]) {
val = i << CHARGE_CURRENT_FAST_CHG_SHIFT;
fast_charge_limit_ua = fast_charge_current_ua[i];
break;
}
}
if (fast_charge_limit_ua
&& chip->charge_current_limit_ua != fast_charge_limit_ua)
rc = smb137c_masked_write_reg(chip, CHARGE_CURRENT_REG,
CHARGE_CURRENT_FAST_CHG_MASK, val);
else if (fast_charge_limit_ua == 0)
rc = smb137c_disable_charging(chip);
chip->charge_current_limit_ua = fast_charge_limit_ua;
if (!rc)
dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
fast_charge_limit_ua);
return rc;
}
static int smb137c_get_charge_current_limit(struct smb137c_chip *chip)
{
int fast_charge_limit_ua = 0;
u8 val = 0;
int rc, i;
rc = smb137c_read_reg(chip, CHARGE_CURRENT_REG, &val);
if (rc)
return rc;
i = (val & CHARGE_CURRENT_FAST_CHG_MASK)
>> CHARGE_CURRENT_FAST_CHG_SHIFT;
if (i >= 0 && i < ARRAY_SIZE(fast_charge_current_ua))
fast_charge_limit_ua = fast_charge_current_ua[i];
dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
fast_charge_limit_ua);
return fast_charge_limit_ua;
}
static struct term_current_config term_current_ua[] = {
{ 35000, 0x06},
{ 50000, 0x00},
{100000, 0x02},
{150000, 0x04},
};
static int smb137c_set_term_current(struct smb137c_chip *chip,
int current_limit_ua)
{
int term_current_limit_ua = 0;
int rc = 0;
u8 val = 0;
int i;
for (i = ARRAY_SIZE(term_current_ua) - 1; i >= 0; i--) {
if (current_limit_ua >= term_current_ua[i].term_current_ua) {
val = term_current_ua[i].charge_cur_reg;
term_current_limit_ua
= term_current_ua[i].term_current_ua;
break;
}
}
if (term_current_limit_ua) {
rc = smb137c_masked_write_reg(chip, CHARGE_CURRENT_REG,
CHARGE_CURRENT_TERM_CUR_MASK, val);
if (rc)
return rc;
rc = smb137c_masked_write_reg(chip, CTRL_A_REG,
CTRL_A_TERM_CUR_MASK, CTRL_A_TERM_CUR_ENABLED);
} else {
rc = smb137c_masked_write_reg(chip, CTRL_A_REG,
CTRL_A_TERM_CUR_MASK, CTRL_A_TERM_CUR_DISABLED);
}
if (!rc)
dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
term_current_limit_ua);
return rc;
}
static int smb137c_set_pre_charge_current_limit(struct smb137c_chip *chip,
int current_limit_ua)
{
int setpoint, rc;
u8 val;
if (current_limit_ua < PRE_CHARGE_CURRENT_MIN_UA ||
current_limit_ua > PRE_CHARGE_CURRENT_MAX_UA) {
dev_err(&chip->client->dev, "%s: current limit out of bounds: %d\n",
__func__, current_limit_ua);
return -EINVAL;
}
setpoint = (current_limit_ua - PRE_CHARGE_CURRENT_MIN_UA)
/ PRE_CHARGE_CURRENT_STEP_UA;
val = setpoint << CHARGE_CURRENT_PRE_CHG_SHIFT;
rc = smb137c_masked_write_reg(chip, CHARGE_CURRENT_REG,
CHARGE_CURRENT_PRE_CHG_MASK, val);
if (!rc)
dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
setpoint * PRE_CHARGE_CURRENT_STEP_UA
+ PRE_CHARGE_CURRENT_MIN_UA);
return rc;
}
static int smb137c_set_float_voltage(struct smb137c_chip *chip, int voltage_uv)
{
int setpoint, rc;
u8 val;
if (voltage_uv < FLOAT_VOLTAGE_MIN_UV ||
voltage_uv > FLOAT_VOLTAGE_MAX_UV) {
dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
__func__, voltage_uv);
return -EINVAL;
}
setpoint = (voltage_uv - FLOAT_VOLTAGE_MIN_UV) / FLOAT_VOLTAGE_STEP_UV;
val = setpoint << FLOAT_VOLTAGE_SHIFT;
rc = smb137c_masked_write_reg(chip, FLOAT_VOLTAGE_REG,
FLOAT_VOLTAGE_MASK, val);
if (!rc)
dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
setpoint * FLOAT_VOLTAGE_STEP_UV + FLOAT_VOLTAGE_MIN_UV);
return rc;
}
static int smb137c_set_pre_charge_threshold_voltage(struct smb137c_chip *chip,
int voltage_uv)
{
int setpoint, rc;
u8 val;
if (voltage_uv < PRE_CHG_THRESH_VOLTAGE_MIN_UV ||
voltage_uv > PRE_CHG_THRESH_VOLTAGE_MAX_UV) {
dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
__func__, voltage_uv);
return -EINVAL;
}
setpoint = (voltage_uv - PRE_CHG_THRESH_VOLTAGE_MIN_UV)
/ PRE_CHG_THRESH_VOLTAGE_STEP_UV;
val = setpoint << CTRL_A_THRESH_VOLTAGE_SHIFT;
rc = smb137c_masked_write_reg(chip, CTRL_A_REG,
CTRL_A_THRESH_VOLTAGE_MASK, val);
if (!rc)
dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
setpoint * PRE_CHG_THRESH_VOLTAGE_STEP_UV
+ PRE_CHG_THRESH_VOLTAGE_MIN_UV);
return rc;
}
static int smb137c_set_recharge_threshold_voltage(struct smb137c_chip *chip,
int voltage_uv)
{
int rc;
u8 val;
if (voltage_uv == 75000) {
val = OTG_CTRL_AUTO_RECHARGE_75MV;
} else if (voltage_uv == 120000) {
val = OTG_CTRL_AUTO_RECHARGE_120MV;
} else {
dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
__func__, voltage_uv);
return -EINVAL;
}
rc = smb137c_masked_write_reg(chip, OTG_CTRL_REG,
OTG_CTRL_AUTO_RECHARGE_MASK, val);
if (!rc)
dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
voltage_uv);
return rc;
}
static int smb137c_set_system_voltage(struct smb137c_chip *chip, int voltage_uv)
{
int rc;
u8 val;
if (voltage_uv == 4250000) {
val = CTRL_A_VOUTL_4250MV;
} else if (voltage_uv == 4460000) {
val = CTRL_A_VOUTL_4460MV;
} else {
dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
__func__, voltage_uv);
return -EINVAL;
}
rc = smb137c_masked_write_reg(chip, CTRL_A_REG, CTRL_A_VOUTL_MASK, val);
if (!rc)
dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
voltage_uv);
return rc;
}
static int charging_timeout[] = {
382,
764,
1527,
};
static int smb137c_set_charging_timeout(struct smb137c_chip *chip, int timeout)
{
int timeout_chosen = 0;
u8 val = 3 << SAFETY_TIMER_CHG_TIMEOUT_SHIFT;
int rc, i;
for (i = ARRAY_SIZE(charging_timeout) - 1; i >= 0; i--) {
if (timeout >= charging_timeout[i]) {
val = i << SAFETY_TIMER_CHG_TIMEOUT_SHIFT;
timeout_chosen = charging_timeout[i];
break;
}
}
rc = smb137c_masked_write_reg(chip, SAFETY_TIMER_REG,
SAFETY_TIMER_CHG_TIMEOUT_MASK, val);
if (!rc)
dev_dbg(&chip->client->dev, "%s: timeout=%d min\n", __func__,
timeout_chosen);
return rc;
}
static int pre_charge_timeout[] = {
48,
95,
191,
};
static int smb137c_set_pre_charge_timeout(struct smb137c_chip *chip,
int timeout)
{
int timeout_chosen = 0;
u8 val = 3 << SAFETY_TIMER_PRE_CHG_TIME_SHIFT;
int rc, i;
for (i = ARRAY_SIZE(pre_charge_timeout) - 1; i >= 0; i--) {
if (timeout >= pre_charge_timeout[i]) {
val = i << SAFETY_TIMER_PRE_CHG_TIME_SHIFT;
timeout_chosen = pre_charge_timeout[i];
break;
}
}
rc = smb137c_masked_write_reg(chip, SAFETY_TIMER_REG,
SAFETY_TIMER_PRE_CHG_TIME_MASK, val);
if (!rc)
dev_dbg(&chip->client->dev, "%s: timeout=%d min\n", __func__,
timeout_chosen);
return rc;
}
static int thermistor_current[] = {
100,
40,
20,
10,
};
static int smb137c_set_thermistor_current(struct smb137c_chip *chip,
int current_ua)
{
bool found = false;
u8 val = 0;
int rc, i;
for (i = 0; i < ARRAY_SIZE(thermistor_current); i++) {
if (current_ua == thermistor_current[i]) {
found = true;
val = i << TEMP_MON_THERM_CURRENT_SHIFT;
}
}
if (!found) {
dev_err(&chip->client->dev, "%s: current out of bounds: %d\n",
__func__, current_ua);
return -EINVAL;
}
rc = smb137c_masked_write_reg(chip, TEMP_MON_REG,
TEMP_MON_THERM_CURRENT_MASK, val);
if (!rc)
dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
current_ua);
return 0;
}
static int smb137c_set_temperature_low_limit(struct smb137c_chip *chip,
int value)
{
int rc;
if (value < 0 || value > 7) {
dev_err(&chip->client->dev, "%s: temperature value out of bounds: %d\n",
__func__, value);
return -EINVAL;
}
rc = smb137c_masked_write_reg(chip, TEMP_MON_REG,
TEMP_MON_TEMP_LOW_MASK, value << TEMP_MON_TEMP_LOW_SHIFT);
if (!rc)
dev_dbg(&chip->client->dev, "%s: temperature value=%d\n",
__func__, value);
return rc;
}
static int smb137c_set_temperature_high_limit(struct smb137c_chip *chip,
int value)
{
int rc;
if (value < 0 || value > 7) {
dev_err(&chip->client->dev, "%s: temperature value out of bounds: %d\n",
__func__, value);
return -EINVAL;
}
rc = smb137c_masked_write_reg(chip, TEMP_MON_REG,
TEMP_MON_TEMP_HIGH_MASK, value << TEMP_MON_TEMP_HIGH_SHIFT);
if (!rc)
dev_dbg(&chip->client->dev, "%s: temperature value=%d\n",
__func__, value);
return rc;
}
static int charge_status_type_map[] = {
[CHARGE_STAT_NO_CHG] = POWER_SUPPLY_CHARGE_TYPE_NONE,
[CHARGE_STAT_PRE_CHG] = POWER_SUPPLY_CHARGE_TYPE_TRICKLE,
[CHARGE_STAT_FAST_CHG] = POWER_SUPPLY_CHARGE_TYPE_FAST,
[CHARGE_STAT_TAPER_CHG] = POWER_SUPPLY_CHARGE_TYPE_FAST,
};
static const char * const charge_status_name[] = {
[CHARGE_STAT_NO_CHG] = "none",
[CHARGE_STAT_PRE_CHG] = "pre-charge",
[CHARGE_STAT_FAST_CHG] = "fast-charge",
[CHARGE_STAT_TAPER_CHG] = "taper-charge",
};
static int smb137c_get_property_status(struct smb137c_chip *chip)
{
int status = POWER_SUPPLY_STATUS_DISCHARGING;
enum smb137c_charge_status charging_status;
bool charging_enabled;
bool charging_error;
int rc;
u8 val;
rc = smb137c_read_reg(chip, STAT_C_REG, &val);
if (rc)
return POWER_SUPPLY_STATUS_UNKNOWN;
charging_enabled = val & STAT_C_CHG_ENABLED;
charging_error = val & STAT_C_CHG_ERROR;
charging_status = (val & STAT_C_CHG_STAT_MASK) >> STAT_C_CHG_STAT_SHIFT;
if (charging_enabled && !charging_error
&& charging_status != CHARGE_STAT_NO_CHG)
status = POWER_SUPPLY_STATUS_CHARGING;
dev_dbg(&chip->client->dev, "%s: status=%s\n", __func__,
(status == POWER_SUPPLY_STATUS_CHARGING ? "charging"
: "discharging"));
return status;
}
static int smb137c_get_property_battery_present(struct smb137c_chip *chip)
{
int rc;
u8 val;
rc = smb137c_read_reg(chip, IRQ_STAT_B_REG, &val);
if (rc || (val & IRQ_STAT_B_BATT_MISSING))
return 0;
/* Treat battery voltage less than 2.1 V as battery not present. */
rc = smb137c_read_reg(chip, STAT_C_REG, &val);
if (rc || (val & STAT_C_VBATT_LEVEL_BELOW_2P1V))
return 0;
return 1;
}
static int smb137c_get_property_battery_health(struct smb137c_chip *chip)
{
int rc;
u8 val;
/* The health of a disconnected battery is unknown. */
if (!smb137c_get_property_battery_present(chip))
return POWER_SUPPLY_HEALTH_UNKNOWN;
rc = smb137c_read_reg(chip, IRQ_STAT_B_REG, &val);
if (rc)
return POWER_SUPPLY_HEALTH_UNKNOWN;
if (val & IRQ_STAT_B_BATT_OVERVOLT)
return POWER_SUPPLY_HEALTH_OVERVOLTAGE;
else if (val & IRQ_STAT_B_BATT_UNDERVOLT)
return POWER_SUPPLY_HEALTH_DEAD;
rc = smb137c_read_reg(chip, IRQ_STAT_A_REG, &val);
if (rc)
return POWER_SUPPLY_HEALTH_UNKNOWN;
if (val & IRQ_STAT_A_BATT_HOT)
return POWER_SUPPLY_HEALTH_OVERHEAT;
else if (val & IRQ_STAT_A_BATT_COLD)
return POWER_SUPPLY_HEALTH_COLD;
return POWER_SUPPLY_HEALTH_GOOD;
}
static int smb137c_get_property_charge_type(struct smb137c_chip *chip)
{
enum smb137c_charge_status status;
int charge_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
bool charging_enabled;
bool charging_error;
int rc;
u8 val;
rc = smb137c_read_reg(chip, STAT_C_REG, &val);
if (rc)
return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
charging_enabled = val & STAT_C_CHG_ENABLED;
charging_error = val & STAT_C_CHG_ERROR;
status = (val & STAT_C_CHG_STAT_MASK) >> STAT_C_CHG_STAT_SHIFT;
if (!charging_enabled) {
dev_dbg(&chip->client->dev, "%s: not charging\n", __func__);
} else if (charging_error) {
dev_warn(&chip->client->dev, "%s: charger error detected\n",
__func__);
} else {
charge_type = charge_status_type_map[status];
}
dev_dbg(&chip->client->dev, "%s: charging status=%s\n", __func__,
charge_status_name[status]);
return charge_type;
}
static enum power_supply_property smb137c_power_properties[] = {
POWER_SUPPLY_PROP_STATUS,
POWER_SUPPLY_PROP_HEALTH,
POWER_SUPPLY_PROP_PRESENT,
POWER_SUPPLY_PROP_CHARGING_ENABLED,
POWER_SUPPLY_PROP_CHARGE_TYPE,
POWER_SUPPLY_PROP_CURRENT_MAX,
POWER_SUPPLY_PROP_TECHNOLOGY,
POWER_SUPPLY_PROP_MODEL_NAME,
POWER_SUPPLY_PROP_MANUFACTURER,
};
static int smb137c_property_is_writeable(struct power_supply *psy,
enum power_supply_property psp)
{
switch (psp) {
case POWER_SUPPLY_PROP_CHARGING_ENABLED:
case POWER_SUPPLY_PROP_CURRENT_MAX:
return 1;
default:
break;
}
return 0;
}
static int smb137c_power_set_property(struct power_supply *psy,
enum power_supply_property psp,
const union power_supply_propval *val)
{
struct smb137c_chip *chip = container_of(psy, struct smb137c_chip, psy);
mutex_lock(&chip->lock);
switch (psp) {
case POWER_SUPPLY_PROP_CHARGING_ENABLED:
if (val->intval)
smb137c_enable_charging(chip);
else
smb137c_disable_charging(chip);
break;
case POWER_SUPPLY_PROP_CURRENT_MAX:
smb137c_set_charge_current_limit(chip, val->intval);
break;
default:
mutex_unlock(&chip->lock);
return -EINVAL;
}
mutex_unlock(&chip->lock);
power_supply_changed(&chip->psy);
return 0;
}
static int smb137c_power_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
struct smb137c_chip *chip = container_of(psy, struct smb137c_chip, psy);
mutex_lock(&chip->lock);
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
val->intval = smb137c_get_property_status(chip);
break;
case POWER_SUPPLY_PROP_HEALTH:
val->intval = smb137c_get_property_battery_health(chip);
break;
case POWER_SUPPLY_PROP_PRESENT:
val->intval = smb137c_get_property_battery_present(chip);
break;
case POWER_SUPPLY_PROP_CHARGING_ENABLED:
val->intval = chip->charging_enabled;
break;
case POWER_SUPPLY_PROP_CHARGE_TYPE:
val->intval = smb137c_get_property_charge_type(chip);
break;
case POWER_SUPPLY_PROP_CURRENT_MAX:
val->intval = chip->charge_current_limit_ua;
break;
case POWER_SUPPLY_PROP_TECHNOLOGY:
val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
break;
case POWER_SUPPLY_PROP_MODEL_NAME:
val->strval = "SMB137C";
break;
case POWER_SUPPLY_PROP_MANUFACTURER:
val->strval = "Summit Microelectronics";
break;
default:
mutex_unlock(&chip->lock);
return -EINVAL;
}
mutex_unlock(&chip->lock);
return 0;
}
static void smb137c_external_power_changed(struct power_supply *psy)
{
struct smb137c_chip *chip = container_of(psy, struct smb137c_chip, psy);
union power_supply_propval prop = {0,};
int scope = POWER_SUPPLY_SCOPE_DEVICE;
int current_limit = 0;
int online = 0;
int rc;
mutex_lock(&chip->lock);
dev_dbg(&chip->client->dev, "%s: start\n", __func__);
rc = chip->usb_psy->get_property(chip->usb_psy,
POWER_SUPPLY_PROP_ONLINE, &prop);
if (rc)
dev_err(&chip->client->dev, "%s: could not read USB online property, rc=%d\n",
__func__, rc);
else
online = prop.intval;
rc = chip->usb_psy->get_property(chip->usb_psy, POWER_SUPPLY_PROP_SCOPE,
&prop);
if (rc)
dev_err(&chip->client->dev, "%s: could not read USB scope property, rc=%d\n",
__func__, rc);
else
scope = prop.intval;
rc = chip->usb_psy->get_property(chip->usb_psy,
POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
if (rc)
dev_err(&chip->client->dev, "%s: could not read USB current_max property, rc=%d\n",
__func__, rc);
else
current_limit = prop.intval;
if (scope == POWER_SUPPLY_SCOPE_SYSTEM) {
/* USB host mode */
smb137c_disable_charging(chip);
smb137c_enable_otg_mode(chip);
} else if (online) {
/* USB online in device mode */
smb137c_set_usb_input_current_limit(chip, current_limit);
smb137c_enable_charging(chip);
smb137c_disable_otg_mode(chip);
} else {
/* USB offline */
smb137c_disable_charging(chip);
smb137c_disable_otg_mode(chip);
smb137c_set_usb_input_current_limit(chip,
min(current_limit, USB_MIN_CURRENT_UA));
}
dev_dbg(&chip->client->dev, "%s: end\n", __func__);
mutex_unlock(&chip->lock);
power_supply_changed(&chip->psy);
}
static int smb137c_set_register_defaults(struct smb137c_chip *chip)
{
int rc;
u8 val, mask;
/* Allow volatile register writes. */
rc = smb137c_masked_write_reg(chip, CMD_A_REG,
CMD_A_VOLATILE_WRITE_MASK, CMD_A_VOLATILE_WRITE_ALLOW);
if (rc)
return rc;
/* Do not reset register values on USB reinsertion. */
rc = smb137c_masked_write_reg(chip, SAFETY_TIMER_REG,
SAFETY_TIMER_RELOAD_MASK, SAFETY_TIMER_RELOAD_DISABLED);
if (rc)
return rc;
/* Set various default control parameters. */
val = PIN_CTRL_DEAD_BATT_CHG_ENABLED | PIN_CTRL_OTG
| PIN_CTRL_USB_CUR_LIMIT_REG | PIN_CTRL_CHG_EN_REG_LOW
| PIN_CTRL_OTG_CTRL_REG;
mask = PIN_CTRL_DEAD_BATT_CHG_MASK | PIN_CTRL_OTG_LBR_MASK
| PIN_CTRL_USB_CUR_LIMIT_MASK | PIN_CTRL_CHG_EN_MASK
| PIN_CTRL_OTG_CTRL_MASK;
rc = smb137c_masked_write_reg(chip, PIN_CTRL_REG, mask, val);
if (rc)
return rc;
/* Disable charging, disable OTG mode, and allow fast-charge current. */
val = CMD_A_CHARGING_DISABLED | CMD_A_OTG_DISABLED
| CMD_A_FAST_CHG_ALLOW;
mask = CMD_A_CHARGING_MASK | CMD_A_OTG_MASK | CMD_A_FAST_CHG_MASK;
rc = smb137c_masked_write_reg(chip, CMD_A_REG, mask, val);
if (rc)
return rc;
/* Enable auto recharging and full-time THERM monitor. */
val = CTRL_A_AUTO_RECHARGE_ENABLED | CTRL_A_THERM_MONITOR_ENABLED;
mask = CTRL_A_AUTO_RECHARGE_MASK | CTRL_A_THERM_MONITOR_MASK;
rc = smb137c_masked_write_reg(chip, CTRL_A_REG, mask, val);
if (rc)
return rc;
/* Use register value instead of pin to control USB suspend. */
rc = smb137c_masked_write_reg(chip, VAR_FUNC_REG,
VAR_FUNC_USB_SUSPEND_CTRL_MASK, VAR_FUNC_USB_SUSPEND_CTRL_REG);
if (rc)
return rc;
return rc;
}
static int smb137c_apply_dt_configs(struct smb137c_chip *chip)
{
struct device *dev = &chip->client->dev;
struct device_node *node = chip->client->dev.of_node;
int ret, current_ma, voltage_mv, timeout, value;
int rc = 0;
/*
* All device tree parameters are optional so it is ok if read calls
* fail.
*/
ret = of_property_read_u32(node, "summit,chg-current-ma", ¤t_ma);
if (ret == 0) {
rc = smb137c_set_charge_current_limit(chip, current_ma * 1000);
if (rc) {
dev_err(dev, "%s: Failed to set charge current, rc=%d\n",
__func__, rc);
return rc;
}
} else {
chip->charge_current_limit_ua
= smb137c_get_charge_current_limit(chip);
rc = chip->charge_current_limit_ua;
if (rc < 0) {
dev_err(dev, "%s: Failed to get charge current, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,term-current-ma", ¤t_ma);
if (ret == 0) {
rc = smb137c_set_term_current(chip, current_ma * 1000);
if (rc) {
dev_err(dev, "%s: Failed to set termination current, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,pre-chg-current-ma",
¤t_ma);
if (ret == 0) {
rc = smb137c_set_pre_charge_current_limit(chip,
current_ma * 1000);
if (rc) {
dev_err(dev, "%s: Failed to set pre-charge current limit, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,float-voltage-mv",
&voltage_mv);
if (ret == 0) {
rc = smb137c_set_float_voltage(chip, voltage_mv * 1000);
if (rc) {
dev_err(dev, "%s: Failed to set float voltage, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,thresh-voltage-mv",
&voltage_mv);
if (ret == 0) {
rc = smb137c_set_pre_charge_threshold_voltage(chip,
voltage_mv * 1000);
if (rc) {
dev_err(dev, "%s: Failed to set fast-charge threshold voltage, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,recharge-thresh-mv",
&voltage_mv);
if (ret == 0) {
rc = smb137c_set_recharge_threshold_voltage(chip,
voltage_mv * 1000);
if (rc) {
dev_err(dev, "%s: Failed to set recharge threshold voltage, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,system-voltage-mv",
&voltage_mv);
if (ret == 0) {
rc = smb137c_set_system_voltage(chip, voltage_mv * 1000);
if (rc) {
dev_err(dev, "%s: Failed to set system voltage, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,charging-timeout", &timeout);
if (ret == 0) {
rc = smb137c_set_charging_timeout(chip, timeout);
if (rc) {
dev_err(dev, "%s: Failed to set charging timeout, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,pre-charge-timeout", &timeout);
if (ret == 0) {
rc = smb137c_set_pre_charge_timeout(chip, timeout);
if (rc) {
dev_err(dev, "%s: Failed to set pre-charge timeout, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,therm-current-ua", &value);
if (ret == 0) {
rc = smb137c_set_thermistor_current(chip, value);
if (rc) {
dev_err(dev, "%s: Failed to set thermistor current, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,temperature-min", &value);
if (ret == 0) {
rc = smb137c_set_temperature_low_limit(chip, value);
if (rc) {
dev_err(dev, "%s: Failed to set low temperature limit, rc=%d\n",
__func__, rc);
return rc;
}
}
ret = of_property_read_u32(node, "summit,temperature-max", &value);
if (ret == 0) {
rc = smb137c_set_temperature_high_limit(chip, value);
if (rc) {
dev_err(dev, "%s: Failed to set high temperature limit, rc=%d\n",
__func__, rc);
return rc;
}
}
return rc;
}
static int smb137c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct smb137c_chip *chip;
struct device *dev = &client->dev;
struct device_node *node = client->dev.of_node;
int rc = 0;
int gui_rev, silicon_rev;
u8 dev_id;
if (!node) {
dev_err(dev, "%s: device tree information missing\n", __func__);
return -ENODEV;
}
if (!i2c_check_functionality(client->adapter,
I2C_FUNC_SMBUS_BYTE_DATA)) {
dev_err(dev, "%s: SMBUS_BYTE_DATA unsupported\n", __func__);
return -EIO;
}
chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
if (!chip) {
dev_err(dev, "%s: devm_kzalloc failed\n", __func__);
return -ENOMEM;
}
mutex_init(&chip->lock);
chip->client = client;
i2c_set_clientdata(client, chip);
chip->usb_psy = power_supply_get_by_name("usb");
if (!chip->usb_psy) {
dev_dbg(dev, "%s: USB supply not found; deferring charger probe\n",
__func__);
return -EPROBE_DEFER;
}
rc = smb137c_read_reg(chip, DEV_ID_REG, &dev_id);
if (rc)
return rc;
if ((dev_id & DEV_ID_PART_MASK) != DEV_ID_PART_SMB137C) {
dev_err(dev, "%s: invalid device ID=0x%02X\n", __func__,
dev_id);
return -ENODEV;
}
gui_rev = (dev_id & DEV_ID_GUI_REV_MASK) >> DEV_ID_GUI_REV_SHIFT;
silicon_rev = (dev_id & DEV_ID_SILICON_REV_MASK)
>> DEV_ID_SILICON_REV_SHIFT;
rc = smb137c_set_register_defaults(chip);
if (rc)
return rc;
rc = smb137c_apply_dt_configs(chip);
if (rc)
return rc;
chip->psy.name = "battery";
chip->psy.type = POWER_SUPPLY_TYPE_BATTERY;
chip->psy.properties = smb137c_power_properties;
chip->psy.num_properties = ARRAY_SIZE(smb137c_power_properties);
chip->psy.get_property = smb137c_power_get_property;
chip->psy.set_property = smb137c_power_set_property;
chip->psy.property_is_writeable = smb137c_property_is_writeable;
chip->psy.external_power_changed = smb137c_external_power_changed;
rc = power_supply_register(dev, &chip->psy);
if (rc < 0) {
dev_err(dev, "%s: power_supply_register failed, rc=%d\n",
__func__, rc);
return rc;
}
smb137c_external_power_changed(&chip->psy);
dev_info(dev, "%s: SMB137C charger probed successfully, gui_rev=%d, silicon_rev=%d\n",
__func__, gui_rev, silicon_rev);
return rc;
}
static int smb137c_remove(struct i2c_client *client)
{
return 0;
}
static const struct i2c_device_id smb137c_id[] = {
{ .name = "smb137c", },
{},
};
MODULE_DEVICE_TABLE(i2c, smb137c_id);
static const struct of_device_id smb137c_match[] = {
{ .compatible = "summit,smb137c", },
{ },
};
static struct i2c_driver smb137c_driver = {
.driver = {
.name = "smb137c",
.owner = THIS_MODULE,
.of_match_table = smb137c_match,
},
.probe = smb137c_probe,
.remove = smb137c_remove,
.id_table = smb137c_id,
};
static int __init smb137c_init(void)
{
return i2c_add_driver(&smb137c_driver);
}
module_init(smb137c_init);
static void __exit smb137c_exit(void)
{
return i2c_del_driver(&smb137c_driver);
}
module_exit(smb137c_exit);
MODULE_DESCRIPTION("SMB137C Charger");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("i2c:smb137c");
|