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
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
|
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Silicon Mitus SM5803 Buck-Boost Charger
*/
#include "atomic.h"
#include "battery.h"
#include "battery_smart.h"
#include "charge_state_v2.h"
#include "charger.h"
#include "gpio.h"
#include "hooks.h"
#include "i2c.h"
#include "sm5803.h"
#include "system.h"
#include "throttle_ap.h"
#include "timer.h"
#include "usb_charge.h"
#include "usb_pd.h"
#include "util.h"
#include "watchdog.h"
#ifndef CONFIG_CHARGER_NARROW_VDC
#error "SM5803 is a NVDC charger, please enable CONFIG_CHARGER_NARROW_VDC."
#endif
/* Console output macros */
#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args)
#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args)
#define UNKNOWN_DEV_ID -1
static int dev_id = UNKNOWN_DEV_ID;
static const struct charger_info sm5803_charger_info = {
.name = CHARGER_NAME,
.voltage_max = CHARGE_V_MAX,
.voltage_min = CHARGE_V_MIN,
.voltage_step = CHARGE_V_STEP,
.current_max = CHARGE_I_MAX,
.current_min = CHARGE_I_MIN,
.current_step = CHARGE_I_STEP,
.input_current_max = INPUT_I_MAX,
.input_current_min = INPUT_I_MIN,
.input_current_step = INPUT_I_STEP,
};
static uint32_t irq_pending; /* Bitmask of chips with interrupts pending */
static struct mutex flow1_access_lock[CHARGER_NUM];
static struct mutex flow2_access_lock[CHARGER_NUM];
static int charger_vbus[CHARGER_NUM];
/* Tracker for charging failures per port */
struct {
int count;
timestamp_t time;
} failure_tracker[CHARGER_NUM] = {};
/* Port to restart charging on */
static int active_restart_port = CHARGE_PORT_NONE;
#define CHARGING_FAILURE_MAX_COUNT 2
#define CHARGING_FAILURE_INTERVAL MINUTE
static int sm5803_is_sourcing_otg_power(int chgnum, int port);
static enum ec_error_list sm5803_get_dev_id(int chgnum, int *id);
static inline enum ec_error_list chg_read8(int chgnum, int offset, int *value)
{
return i2c_read8(chg_chips[chgnum].i2c_port,
chg_chips[chgnum].i2c_addr_flags,
offset, value);
}
static inline enum ec_error_list chg_write8(int chgnum, int offset, int value)
{
return i2c_write8(chg_chips[chgnum].i2c_port,
chg_chips[chgnum].i2c_addr_flags,
offset, value);
}
static inline enum ec_error_list meas_read8(int chgnum, int offset, int *value)
{
return i2c_read8(chg_chips[chgnum].i2c_port,
SM5803_ADDR_MEAS_FLAGS,
offset, value);
}
static inline enum ec_error_list meas_write8(int chgnum, int offset, int value)
{
return i2c_write8(chg_chips[chgnum].i2c_port,
SM5803_ADDR_MEAS_FLAGS,
offset, value);
}
static inline enum ec_error_list main_read8(int chgnum, int offset, int *value)
{
return i2c_read8(chg_chips[chgnum].i2c_port,
SM5803_ADDR_MAIN_FLAGS,
offset, value);
}
static inline enum ec_error_list main_write8(int chgnum, int offset, int value)
{
return i2c_write8(chg_chips[chgnum].i2c_port,
SM5803_ADDR_MAIN_FLAGS,
offset, value);
}
static inline enum ec_error_list test_write8(int chgnum, int offset, int value)
{
return i2c_write8(chg_chips[chgnum].i2c_port,
SM5803_ADDR_TEST_FLAGS,
offset, value);
}
static inline enum ec_error_list test_update8(int chgnum, const int offset,
const uint8_t mask,
const enum mask_update_action action)
{
return i2c_update8(chg_chips[chgnum].i2c_port,
SM5803_ADDR_TEST_FLAGS, offset, mask, action);
}
static enum ec_error_list sm5803_flow1_update(int chgnum, const uint8_t mask,
const enum mask_update_action action)
{
int reg, rv, dev_id;
/*
* On Si rev 3, confirm that init value in 0x5C is intact before
* enabling charging.
*/
rv = sm5803_get_dev_id(chgnum, &dev_id);
if (rv)
return rv;
if (dev_id == 0x03) {
rv = chg_read8(chgnum, 0x5C, ®);
if (rv) {
CPRINTS("%s %d: Failed 0x5C read",
CHARGER_NAME, chgnum);
return rv;
}
if (reg != 0x7A) {
CPRINTS("%s %d: Unexpected 0x5C reg: 0x%02x. File bug",
CHARGER_NAME, chgnum, reg);
/* Fix it before enabling charging */
rv = chg_write8(chgnum, 0x5C, 0x7A);
}
}
/* Safety checks done, onto the actual register update */
mutex_lock(&flow1_access_lock[chgnum]);
rv = i2c_update8(chg_chips[chgnum].i2c_port,
chg_chips[chgnum].i2c_addr_flags,
SM5803_REG_FLOW1,
mask, action);
mutex_unlock(&flow1_access_lock[chgnum]);
return rv;
}
static enum ec_error_list sm5803_flow2_update(int chgnum, const uint8_t mask,
const enum mask_update_action action)
{
int rv;
mutex_lock(&flow2_access_lock[chgnum]);
rv = i2c_update8(chg_chips[chgnum].i2c_port,
chg_chips[chgnum].i2c_addr_flags,
SM5803_REG_FLOW2,
mask, action);
mutex_unlock(&flow2_access_lock[chgnum]);
return rv;
}
int sm5803_is_vbus_present(int chgnum)
{
return charger_vbus[chgnum];
}
enum ec_error_list sm5803_configure_gpio0(int chgnum,
enum sm5803_gpio0_modes mode, int od)
{
enum ec_error_list rv;
int reg;
rv = main_read8(chgnum, SM5803_REG_GPIO0_CTRL, ®);
if (rv)
return rv;
reg &= ~SM5803_GPIO0_MODE_MASK;
reg |= mode << 1;
if (od)
reg |= SM5803_GPIO0_OPEN_DRAIN_EN;
else
reg &= ~SM5803_GPIO0_OPEN_DRAIN_EN;
rv = main_write8(chgnum, SM5803_REG_GPIO0_CTRL, reg);
return rv;
}
enum ec_error_list sm5803_set_gpio0_level(int chgnum, int level)
{
enum ec_error_list rv;
int reg;
rv = main_read8(chgnum, SM5803_REG_GPIO0_CTRL, ®);
if (rv)
return rv;
if (level)
reg |= SM5803_GPIO0_VAL;
else
reg &= ~SM5803_GPIO0_VAL;
rv = main_write8(chgnum, SM5803_REG_GPIO0_CTRL, reg);
return rv;
}
enum ec_error_list sm5803_configure_chg_det_od(int chgnum, int enable)
{
enum ec_error_list rv;
int reg;
rv = main_read8(chgnum, SM5803_REG_GPIO0_CTRL, ®);
if (rv)
return rv;
if (enable)
reg |= SM5803_CHG_DET_OPEN_DRAIN_EN;
else
reg &= ~SM5803_CHG_DET_OPEN_DRAIN_EN;
rv = main_write8(chgnum, SM5803_REG_GPIO0_CTRL, reg);
return rv;
}
enum ec_error_list sm5803_get_chg_det(int chgnum, int *chg_det)
{
enum ec_error_list rv;
int reg;
rv = main_read8(chgnum, SM5803_REG_STATUS1, ®);
if (rv)
return rv;
*chg_det = (reg & SM5803_STATUS1_CHG_DET) != 0;
return EC_SUCCESS;
}
enum ec_error_list sm5803_set_vbus_disch(int chgnum, int enable)
{
enum ec_error_list rv;
int reg;
rv = main_read8(chgnum, SM5803_REG_PORTS_CTRL, ®);
if (rv)
return rv;
if (enable)
reg |= SM5803_PORTS_VBUS_DISCH;
else
reg &= ~SM5803_PORTS_VBUS_DISCH;
rv = main_write8(chgnum, SM5803_REG_PORTS_CTRL, reg);
return rv;
}
enum ec_error_list sm5803_vbus_sink_enable(int chgnum, int enable)
{
enum ec_error_list rv;
int regval;
rv = sm5803_get_dev_id(chgnum, &dev_id);
if (rv)
return rv;
if (enable) {
if (chgnum == CHARGER_PRIMARY) {
/* Magic for new silicon */
if (dev_id >= 3) {
rv |= main_write8(chgnum, 0x1F, 0x1);
rv |= test_write8(chgnum, 0x44, 0x2);
rv |= main_write8(chgnum, 0x1F, 0);
}
rv = sm5803_flow2_update(chgnum,
SM5803_FLOW2_AUTO_ENABLED,
MASK_SET);
} else {
if (dev_id >= 3) {
/* Touch of magic on the primary charger */
rv |= main_write8(CHARGER_PRIMARY, 0x1F, 0x1);
rv |= test_write8(CHARGER_PRIMARY, 0x44, 0x20);
rv |= main_write8(CHARGER_PRIMARY, 0x1F, 0x0);
/*
* Disable linear, pre-charge, and linear fast
* charge for primary charger.
*/
rv = chg_read8(CHARGER_PRIMARY,
SM5803_REG_FLOW3, ®val);
regval &= ~(BIT(6) | BIT(5) | BIT(4));
rv |= chg_write8(CHARGER_PRIMARY,
SM5803_REG_FLOW3, regval);
}
}
/* Last but not least, enable sinking */
rv |= sm5803_flow1_update(chgnum, CHARGER_MODE_SINK, MASK_SET);
} else {
if (chgnum == CHARGER_PRIMARY)
rv |= sm5803_flow2_update(chgnum,
SM5803_FLOW2_AUTO_ENABLED,
MASK_CLR);
if (chgnum == CHARGER_SECONDARY) {
rv |= sm5803_flow1_update(CHARGER_PRIMARY,
SM5803_FLOW1_LINEAR_CHARGE_EN,
MASK_CLR);
rv = chg_read8(CHARGER_PRIMARY, SM5803_REG_FLOW3,
®val);
regval &= ~(BIT(6) | BIT(5) | BIT(4));
rv |= chg_write8(CHARGER_PRIMARY, SM5803_REG_FLOW3,
regval);
}
/* Disable sink mode, unless currently sourcing out */
if (!sm5803_is_sourcing_otg_power(chgnum, chgnum))
rv |= sm5803_flow1_update(chgnum, CHARGER_MODE_SINK,
MASK_CLR);
}
return rv;
}
/*
* Track and store whether we've initialized the charger chips already on this
* boot. This should prevent us from re-running inits after sysjumps.
*/
static bool chip_inited[CHARGER_NUM];
#define SM5803_SYSJUMP_TAG 0x534D /* SM */
#define SM5803_HOOK_VERSION 1
static void init_status_preserve(void)
{
system_add_jump_tag(SM5803_SYSJUMP_TAG, SM5803_HOOK_VERSION,
sizeof(chip_inited), &chip_inited);
}
DECLARE_HOOK(HOOK_SYSJUMP, init_status_preserve, HOOK_PRIO_DEFAULT);
static void init_status_retrieve(void)
{
const uint8_t *tag_contents;
int version, size;
tag_contents = system_get_jump_tag(SM5803_SYSJUMP_TAG,
&version, &size);
if (tag_contents && (version == SM5803_HOOK_VERSION) &&
(size == sizeof(chip_inited)))
/* Valid init status found, restore before charger chip init */
memcpy(&chip_inited, tag_contents, size);
}
DECLARE_HOOK(HOOK_INIT, init_status_retrieve, HOOK_PRIO_FIRST);
static void sm5803_init(int chgnum)
{
enum ec_error_list rv;
int reg;
int vbus_mv;
const struct battery_info *batt_info;
int pre_term;
int cells;
/*
* If a charger is not currently present, disable switching per OCPC
* requirements
*/
rv = charger_get_vbus_voltage(chgnum, &vbus_mv);
if (rv == EC_SUCCESS) {
if (vbus_mv < 4000) {
/*
* No charger connected, disable CHG_EN
* (note other bits default to 0)
*/
rv = chg_write8(chgnum, SM5803_REG_FLOW1, 0);
} else if (!sm5803_is_sourcing_otg_power(chgnum, chgnum)) {
charger_vbus[chgnum] = 1;
}
} else {
CPRINTS("%s %d: Failed to read VBUS voltage during init",
CHARGER_NAME, chgnum);
return;
}
/*
* A previous boot already ran inits, safe to return now that we've
* checked i2c communication to the chip and cached Vbus presence
*/
if (chip_inited[chgnum]) {
CPRINTS("%s %d: Already initialized", CHARGER_NAME, chgnum);
return;
}
rv |= charger_device_id(®);
if (reg == 0x02) {
/* --- Special register init ---
* For early silicon (ID 2) with 3S batteries
*/
rv |= main_write8(chgnum, 0x20, 0x08);
rv |= main_write8(chgnum, 0x30, 0xC0);
rv |= main_write8(chgnum, 0x80, 0x01);
rv |= meas_write8(chgnum, 0x08, 0xC2);
rv |= chg_write8(chgnum, 0x1D, 0x40);
rv |= chg_write8(chgnum, 0x1F, 0x09);
rv |= chg_write8(chgnum, 0x22, 0xB3);
rv |= chg_write8(chgnum, 0x23, 0x81);
rv |= chg_write8(chgnum, 0x28, 0xB7);
rv |= chg_write8(chgnum, 0x4A, 0x82);
rv |= chg_write8(chgnum, 0x4B, 0xA3);
rv |= chg_write8(chgnum, 0x4C, 0xA8);
rv |= chg_write8(chgnum, 0x4D, 0xCA);
rv |= chg_write8(chgnum, 0x4E, 0x07);
rv |= chg_write8(chgnum, 0x4F, 0xFF);
rv |= chg_write8(chgnum, 0x50, 0x98);
rv |= chg_write8(chgnum, 0x51, 0x00);
rv |= chg_write8(chgnum, 0x52, 0x77);
rv |= chg_write8(chgnum, 0x53, 0xD2);
rv |= chg_write8(chgnum, 0x54, 0x02);
rv |= chg_write8(chgnum, 0x55, 0xD1);
rv |= chg_write8(chgnum, 0x56, 0x7F);
rv |= chg_write8(chgnum, 0x57, 0x02);
rv |= chg_write8(chgnum, 0x58, 0xD1);
rv |= chg_write8(chgnum, 0x59, 0x7F);
rv |= chg_write8(chgnum, 0x5A, 0x13);
rv |= chg_write8(chgnum, 0x5B, 0x50);
rv |= chg_write8(chgnum, 0x5C, 0x5B);
rv |= chg_write8(chgnum, 0x5D, 0xB0);
rv |= chg_write8(chgnum, 0x5E, 0x3C);
rv |= chg_write8(chgnum, 0x5F, 0x3C);
rv |= chg_write8(chgnum, 0x60, 0x44);
rv |= chg_write8(chgnum, 0x61, 0x20);
rv |= chg_write8(chgnum, 0x65, 0x35);
rv |= chg_write8(chgnum, 0x66, 0x29);
rv |= chg_write8(chgnum, 0x67, 0x64);
rv |= chg_write8(chgnum, 0x68, 0x88);
rv |= chg_write8(chgnum, 0x69, 0xC7);
/* Inits to access page 0x37 and enable trickle charging */
rv |= main_write8(chgnum, 0x1F, 0x01);
rv |= test_update8(chgnum, 0x8E, BIT(5), MASK_SET);
rv |= main_write8(chgnum, 0x1F, 0x00);
} else if (reg == 0x03) {
uint32_t platform_id;
rv = main_read8(chgnum, SM5803_REG_PLATFORM, &platform_id);
if (rv) {
CPRINTS("%s %d: Failed to read platform during init",
CHARGER_NAME, chgnum);
return;
}
platform_id &= SM5803_PLATFORM_ID;
if (platform_id >= 0x0E && platform_id <= 0x16) {
/* 3S Battery inits */
rv |= main_write8(chgnum, 0x30, 0xC0);
rv |= main_write8(chgnum, 0x80, 0x01);
rv |= main_write8(chgnum, 0x1A, 0x08);
rv |= meas_write8(chgnum, 0x08, 0xC2);
rv |= chg_write8(chgnum, 0x1D, 0x40);
rv |= chg_write8(chgnum, 0x22, 0xB3);
rv |= chg_write8(chgnum, 0x3E, 0x3C);
rv |= chg_write8(chgnum, 0x4B, 0xA6);
rv |= chg_write8(chgnum, 0x4F, 0xBF);
rv |= chg_write8(chgnum, 0x52, 0x77);
rv |= chg_write8(chgnum, 0x53, 0xD2);
rv |= chg_write8(chgnum, 0x54, 0x02);
rv |= chg_write8(chgnum, 0x55, 0xD1);
rv |= chg_write8(chgnum, 0x56, 0x7F);
rv |= chg_write8(chgnum, 0x57, 0x01);
rv |= chg_write8(chgnum, 0x58, 0x50);
rv |= chg_write8(chgnum, 0x59, 0x7F);
rv |= chg_write8(chgnum, 0x5A, 0x13);
rv |= chg_write8(chgnum, 0x5B, 0x50);
rv |= chg_write8(chgnum, 0x5D, 0xB0);
rv |= chg_write8(chgnum, 0x60, 0x44);
rv |= chg_write8(chgnum, 0x65, 0x35);
rv |= chg_write8(chgnum, 0x66, 0x29);
rv |= chg_write8(chgnum, 0x7D, 0x67);
rv |= chg_write8(chgnum, 0x7E, 0x04);
rv |= chg_write8(chgnum, 0x33, 0x3C);
rv |= chg_write8(chgnum, 0x5C, 0x7A);
} else if (platform_id >= 0x06 && platform_id <= 0x0D) {
/* 2S Battery inits */
rv |= main_write8(chgnum, 0x30, 0xC0);
rv |= main_write8(chgnum, 0x80, 0x01);
rv |= main_write8(chgnum, 0x1A, 0x08);
rv |= meas_write8(chgnum, 0x08, 0xC2);
rv |= chg_write8(chgnum, 0x1D, 0x40);
rv |= chg_write8(chgnum, 0x22, 0xB3);
rv |= chg_write8(chgnum, 0x3E, 0x3C);
rv |= chg_write8(chgnum, 0x4F, 0xBF);
rv |= chg_write8(chgnum, 0x52, 0x77);
rv |= chg_write8(chgnum, 0x53, 0xD2);
rv |= chg_write8(chgnum, 0x54, 0x02);
rv |= chg_write8(chgnum, 0x55, 0xD1);
rv |= chg_write8(chgnum, 0x56, 0x7F);
rv |= chg_write8(chgnum, 0x57, 0x01);
rv |= chg_write8(chgnum, 0x58, 0x50);
rv |= chg_write8(chgnum, 0x59, 0x7F);
rv |= chg_write8(chgnum, 0x5A, 0x13);
rv |= chg_write8(chgnum, 0x5B, 0x52);
rv |= chg_write8(chgnum, 0x5D, 0xD0);
rv |= chg_write8(chgnum, 0x60, 0x44);
rv |= chg_write8(chgnum, 0x65, 0x35);
rv |= chg_write8(chgnum, 0x66, 0x29);
rv |= chg_write8(chgnum, 0x7D, 0x97);
rv |= chg_write8(chgnum, 0x7E, 0x07);
rv |= chg_write8(chgnum, 0x33, 0x3C);
rv |= chg_write8(chgnum, 0x5C, 0x7A);
}
rv |= chg_write8(chgnum, 0x73, 0x22);
rv |= chg_write8(chgnum, 0x50, 0x88);
rv |= chg_read8(chgnum, 0x34, ®);
reg |= BIT(7);
rv |= chg_write8(chgnum, 0x34, reg);
rv |= main_write8(chgnum, 0x1F, 0x1);
rv |= test_write8(chgnum, 0x43, 0x10);
rv |= test_write8(chgnum, 0x47, 0x10);
rv |= test_write8(chgnum, 0x48, 0x04);
rv |= main_write8(chgnum, 0x1F, 0x0);
}
/* Enable LDO bits */
rv |= main_read8(chgnum, SM5803_REG_REFERENCE, ®);
reg &= ~(BIT(0) | BIT(1));
rv |= main_write8(chgnum, SM5803_REG_REFERENCE, reg);
/* Set a higher clock speed in case it was lowered for z-state */
rv |= main_read8(chgnum, SM5803_REG_CLOCK_SEL, ®);
reg &= ~SM5803_CLOCK_SEL_LOW;
rv |= main_write8(chgnum, SM5803_REG_CLOCK_SEL, reg);
/* Turn on GPADCs to default */
rv |= meas_write8(chgnum, SM5803_REG_GPADC_CONFIG1, 0xF3);
/* Enable Psys DAC */
rv |= meas_read8(chgnum, SM5803_REG_PSYS1, ®);
reg |= SM5803_PSYS1_DAC_EN;
rv |= meas_write8(chgnum, SM5803_REG_PSYS1, reg);
/* Enable ADC sigma delta */
rv |= chg_read8(chgnum, SM5803_REG_CC_CONFIG1, ®);
reg |= SM5803_CC_CONFIG1_SD_PWRUP;
rv |= chg_write8(chgnum, SM5803_REG_CC_CONFIG1, reg);
/* Enable PROCHOT comparators except Ibus */
rv |= chg_read8(chgnum, SM5803_REG_PHOT1, ®);
reg |= SM5803_PHOT1_COMPARATOR_EN;
reg &= ~SM5803_PHOT1_IBUS_PHOT_COMP_EN;
rv |= chg_write8(chgnum, SM5803_REG_PHOT1, reg);
if (chgnum != CHARGER_PRIMARY) {
/*
* Enable the IBAT_CHG adc in order to calculate
* system resistance.
*/
rv |= meas_read8(chgnum, SM5803_REG_GPADC_CONFIG1, ®);
reg |= SM5803_GPADCC1_IBAT_CHG_EN;
rv |= meas_write8(chgnum, SM5803_REG_GPADC_CONFIG1, reg);
}
/* Set default input current */
reg = SM5803_CURRENT_TO_REG(CONFIG_CHARGER_INPUT_CURRENT)
& SM5803_CHG_ILIM_RAW;
rv |= chg_write8(chgnum, SM5803_REG_CHG_ILIM, reg);
/* Configure charger insertion interrupts */
rv |= main_write8(chgnum, SM5803_REG_INT1_EN, SM5803_INT1_CHG);
/* Enable end of charge interrupts for logging */
rv |= main_write8(chgnum, SM5803_REG_INT4_EN, SM5803_INT4_CHG_FAIL |
SM5803_INT4_CHG_DONE);
/* Set TINT interrupts for 360 K and 330 K */
rv |= meas_write8(chgnum, SM5803_REG_TINT_HIGH_TH,
SM5803_TINT_HIGH_LEVEL);
rv |= meas_write8(chgnum, SM5803_REG_TINT_LOW_TH,
SM5803_TINT_LOW_LEVEL);
/* Configure TINT interrupts to fire after thresholds are set */
rv |= main_write8(chgnum, SM5803_REG_INT2_EN, SM5803_INT2_TINT);
/*
* Configure CHG_ENABLE to only be set through I2C by setting
* HOST_MODE_EN bit (all other register bits are 0 by default)
*/
rv |= chg_write8(chgnum, SM5803_REG_FLOW2, SM5803_FLOW2_HOST_MODE_EN);
if (chgnum == CHARGER_PRIMARY) {
int ibat_eoc_ma;
/* Set end of fast charge threshold */
batt_info = battery_get_info();
ibat_eoc_ma = batt_info->precharge_current - 50;
ibat_eoc_ma /= 100;
ibat_eoc_ma = CLAMP(ibat_eoc_ma, 0, SM5803_CONF5_IBAT_EOC_TH);
rv |= chg_read8(chgnum, SM5803_REG_FAST_CONF5, ®);
reg &= ~SM5803_CONF5_IBAT_EOC_TH;
reg |= ibat_eoc_ma;
rv |= chg_write8(CHARGER_PRIMARY, SM5803_REG_FAST_CONF5, reg);
/* Setup the proper precharge thresholds. */
cells = batt_info->voltage_max / 4;
pre_term = batt_info->voltage_min / cells;
pre_term /= 100; /* Convert to decivolts. */
pre_term = CLAMP(pre_term, SM5803_VBAT_PRE_TERM_MIN_DV,
SM5803_VBAT_PRE_TERM_MAX_DV);
pre_term -= SM5803_VBAT_PRE_TERM_MIN_DV; /* Convert to regval */
rv |= chg_read8(chgnum, SM5803_REG_PRE_FAST_CONF_REG1, ®);
reg &= ~SM5803_VBAT_PRE_TERM;
reg |= pre_term << SM5803_VBAT_PRE_TERM_SHIFT;
rv |= chg_write8(chgnum, SM5803_REG_PRE_FAST_CONF_REG1, reg);
/*
* Set up precharge current
* Note it is preferred to under-shoot the precharge current
* requested. Upper bits of this register are read/write 1 to
* clear
*/
reg = SM5803_CURRENT_TO_REG(batt_info->precharge_current);
reg = MIN(reg, SM5803_PRECHG_ICHG_PRE_SET);
rv |= chg_write8(chgnum, SM5803_REG_PRECHG, reg);
/*
* Set up BFET alerts
*
* We'll set the soft limit at 1.5W and the hard limit at 6W.
*
* The register is 29.2 mW per bit.
*/
reg = (1500 * 10) / 292;
rv |= meas_write8(chgnum, SM5803_REG_BFET_PWR_MAX_TH, reg);
reg = (6000 * 10) / 292;
rv |= meas_write8(chgnum, SM5803_REG_BFET_PWR_HWSAFE_MAX_TH,
reg);
rv |= main_read8(chgnum, SM5803_REG_INT3_EN, ®);
reg |= SM5803_INT3_BFET_PWR_LIMIT |
SM5803_INT3_BFET_PWR_HWSAFE_LIMIT;
rv |= main_write8(chgnum, SM5803_REG_INT3_EN, reg);
rv |= chg_read8(chgnum, SM5803_REG_FLOW3, ®);
reg &= ~SM5803_FLOW3_SWITCH_BCK_BST;
rv |= chg_write8(chgnum, SM5803_REG_FLOW3, reg);
rv |= chg_read8(chgnum, SM5803_REG_SWITCHER_CONF, ®);
reg |= SM5803_SW_BCK_BST_CONF_AUTO;
rv |= chg_write8(chgnum, SM5803_REG_SWITCHER_CONF, reg);
}
if (rv)
CPRINTS("%s %d: Failed initialization", CHARGER_NAME, chgnum);
else
chip_inited[chgnum] = true;
}
static enum ec_error_list sm5803_post_init(int chgnum)
{
/* Nothing to do, charger is always powered */
return EC_SUCCESS;
}
void sm5803_hibernate(int chgnum)
{
enum ec_error_list rv;
int reg;
rv = main_read8(chgnum, SM5803_REG_REFERENCE, ®);
if (rv) {
CPRINTS("%s %d: Failed to read REFERENCE reg", CHARGER_NAME,
chgnum);
return;
}
/* Disable LDO bits - note the primary LDO should not be disabled */
if (chgnum != CHARGER_PRIMARY) {
reg |= (BIT(0) | BIT(1));
rv |= main_write8(chgnum, SM5803_REG_REFERENCE, reg);
}
/* Slow the clock speed */
rv |= main_read8(chgnum, SM5803_REG_CLOCK_SEL, ®);
reg |= SM5803_CLOCK_SEL_LOW;
rv |= main_write8(chgnum, SM5803_REG_CLOCK_SEL, reg);
/* Turn off GPADCs */
rv |= meas_write8(chgnum, SM5803_REG_GPADC_CONFIG1, 0);
rv |= meas_write8(chgnum, SM5803_REG_GPADC_CONFIG2, 0);
/* Disable Psys DAC */
rv |= meas_read8(chgnum, SM5803_REG_PSYS1, ®);
reg &= ~SM5803_PSYS1_DAC_EN;
rv |= meas_write8(chgnum, SM5803_REG_PSYS1, reg);
/* Disable ADC sigma delta */
rv |= chg_read8(chgnum, SM5803_REG_CC_CONFIG1, ®);
reg &= ~SM5803_CC_CONFIG1_SD_PWRUP;
rv |= chg_write8(chgnum, SM5803_REG_CC_CONFIG1, reg);
/* Disable PROCHOT comparators */
rv |= chg_read8(chgnum, SM5803_REG_PHOT1, ®);
reg &= ~SM5803_PHOT1_COMPARATOR_EN;
rv |= chg_write8(chgnum, SM5803_REG_PHOT1, reg);
if (rv)
CPRINTS("%s %d: Failed to set hibernate", CHARGER_NAME, chgnum);
}
static void sm5803_disable_runtime_low_power_mode(void)
{
enum ec_error_list rv;
int reg;
int chgnum = TASK_ID_TO_PD_PORT(task_get_current());
CPRINTS("%s %d: disable runtime low power mode", CHARGER_NAME, chgnum);
rv = main_read8(chgnum, SM5803_REG_REFERENCE, ®);
if (rv) {
CPRINTS("%s %d: Failed to read REFERENCE reg", CHARGER_NAME,
chgnum);
return;
}
/* Set a higher clock speed */
rv |= main_read8(chgnum, SM5803_REG_CLOCK_SEL, ®);
reg &= ~SM5803_CLOCK_SEL_LOW;
rv |= main_write8(chgnum, SM5803_REG_CLOCK_SEL, reg);
/* Enable ADC sigma delta */
rv |= chg_read8(chgnum, SM5803_REG_CC_CONFIG1, ®);
reg |= SM5803_CC_CONFIG1_SD_PWRUP;
rv |= chg_write8(chgnum, SM5803_REG_CC_CONFIG1, reg);
if (rv)
CPRINTS("%s %d: Failed to set in disable runtime LPM",
CHARGER_NAME, chgnum);
}
DECLARE_HOOK(HOOK_USB_PD_CONNECT,
sm5803_disable_runtime_low_power_mode,
HOOK_PRIO_FIRST);
static void sm5803_enable_runtime_low_power_mode(void)
{
enum ec_error_list rv;
int reg;
int chgnum = TASK_ID_TO_PD_PORT(task_get_current());
CPRINTS("%s %d: enable runtime low power mode", CHARGER_NAME, chgnum);
rv = main_read8(chgnum, SM5803_REG_REFERENCE, ®);
if (rv) {
CPRINTS("%s %d: Failed to read REFERENCE reg", CHARGER_NAME,
chgnum);
return;
}
/* Slow the clock speed */
rv |= main_read8(chgnum, SM5803_REG_CLOCK_SEL, ®);
reg |= SM5803_CLOCK_SEL_LOW;
rv |= main_write8(chgnum, SM5803_REG_CLOCK_SEL, reg);
/* Disable ADC sigma delta */
rv |= chg_read8(chgnum, SM5803_REG_CC_CONFIG1, ®);
reg &= ~SM5803_CC_CONFIG1_SD_PWRUP;
rv |= chg_write8(chgnum, SM5803_REG_CC_CONFIG1, reg);
/* If the system is off, all PROCHOT comparators may be turned off */
if (chipset_in_state(CHIPSET_STATE_ANY_OFF |
CHIPSET_STATE_ANY_SUSPEND)) {
rv |= chg_read8(chgnum, SM5803_REG_PHOT1, ®);
reg &= ~SM5803_PHOT1_COMPARATOR_EN;
rv |= chg_write8(chgnum, SM5803_REG_PHOT1, reg);
}
if (rv)
CPRINTS("%s %d: Failed to set in enable runtime LPM",
CHARGER_NAME, chgnum);
}
DECLARE_HOOK(HOOK_USB_PD_DISCONNECT,
sm5803_enable_runtime_low_power_mode,
HOOK_PRIO_LAST);
void sm5803_disable_low_power_mode(int chgnum)
{
enum ec_error_list rv;
int reg;
CPRINTS("%s %d: disable low power mode", CHARGER_NAME, chgnum);
rv = main_read8(chgnum, SM5803_REG_REFERENCE, ®);
if (rv) {
CPRINTS("%s %d: Failed to read REFERENCE reg", CHARGER_NAME,
chgnum);
return;
}
/* Enable Psys DAC */
rv |= meas_read8(chgnum, SM5803_REG_PSYS1, ®);
reg |= SM5803_PSYS1_DAC_EN;
rv |= meas_write8(chgnum, SM5803_REG_PSYS1, reg);
/* Enable PROCHOT comparators except Ibus */
rv |= chg_read8(chgnum, SM5803_REG_PHOT1, ®);
reg |= SM5803_PHOT1_COMPARATOR_EN;
reg &= ~SM5803_PHOT1_IBUS_PHOT_COMP_EN;
rv |= chg_write8(chgnum, SM5803_REG_PHOT1, reg);
if (rv)
CPRINTS("%s %d: Failed to set in disable low power mode",
CHARGER_NAME, chgnum);
}
void sm5803_enable_low_power_mode(int chgnum)
{
enum ec_error_list rv;
int reg;
CPRINTS("%s %d: enable low power mode", CHARGER_NAME, chgnum);
rv = main_read8(chgnum, SM5803_REG_REFERENCE, ®);
if (rv) {
CPRINTS("%s %d: Failed to read REFERENCE reg", CHARGER_NAME,
chgnum);
return;
}
/* Disable Psys DAC */
rv |= meas_read8(chgnum, SM5803_REG_PSYS1, ®);
reg &= ~SM5803_PSYS1_DAC_EN;
rv |= meas_write8(chgnum, SM5803_REG_PSYS1, reg);
/*
* Disable all PROCHOT comparators only if port is inactive. Vbus
* sourcing requires that the Vbus comparator be enabled, and it
* cannot be enabled from HOOK_USB_PD_CONNECT since that is
* called after Vbus has turned on.
*/
rv |= chg_read8(chgnum, SM5803_REG_PHOT1, ®);
reg &= ~SM5803_PHOT1_COMPARATOR_EN;
if (pd_is_connected(chgnum))
reg |= SM5803_PHOT1_VBUS_MON_EN;
rv |= chg_write8(chgnum, SM5803_REG_PHOT1, reg);
if (rv)
CPRINTS("%s %d: Failed to set in enable low power mode",
CHARGER_NAME, chgnum);
}
/*
* Restart charging on the active port, if it's still active and it hasn't
* exceeded our maximum number of restarts.
*/
void sm5803_restart_charging(void)
{
int act_chg = charge_manager_get_active_charge_port();
timestamp_t now = get_time();
if (act_chg == active_restart_port) {
if (timestamp_expired(failure_tracker[act_chg].time, &now)) {
/*
* Enough time has passed since our last failure,
* restart the timing and count from now.
*/
failure_tracker[act_chg].time.val = now.val +
CHARGING_FAILURE_INTERVAL;
failure_tracker[act_chg].count = 1;
sm5803_vbus_sink_enable(act_chg, 1);
} else if (++failure_tracker[act_chg].count >
CHARGING_FAILURE_MAX_COUNT) {
CPRINTS("%s %d: Exceeded charging failure retries",
CHARGER_NAME, act_chg);
} else {
sm5803_vbus_sink_enable(act_chg, 1);
}
}
active_restart_port = CHARGE_PORT_NONE;
}
DECLARE_DEFERRED(sm5803_restart_charging);
/*
* Process interrupt registers and report any Vbus changes. Alert the AP if the
* charger has become too hot.
*/
void sm5803_handle_interrupt(int chgnum)
{
enum ec_error_list rv;
int int_reg, meas_reg;
static bool throttled;
struct batt_params bp;
int act_chg, val;
/* Note: Interrupt registers are clear on read */
rv = main_read8(chgnum, SM5803_REG_INT1_REQ, &int_reg);
if (rv) {
CPRINTS("%s %d: Failed read int1 register", CHARGER_NAME,
chgnum);
return;
}
if (int_reg & SM5803_INT1_CHG) {
rv = main_read8(chgnum, SM5803_REG_STATUS1, &meas_reg);
if (!(meas_reg & SM5803_STATUS1_CHG_DET)) {
charger_vbus[chgnum] = 0;
if (IS_ENABLED(CONFIG_USB_CHARGER))
usb_charger_vbus_change(chgnum, 0);
board_vbus_present_change();
} else {
charger_vbus[chgnum] = 1;
if (IS_ENABLED(CONFIG_USB_CHARGER))
usb_charger_vbus_change(chgnum, 1);
board_vbus_present_change();
}
}
rv = main_read8(chgnum, SM5803_REG_INT2_REQ, &int_reg);
if (rv) {
CPRINTS("%s %d: Failed read int2 register", CHARGER_NAME,
chgnum);
return;
}
if (int_reg & SM5803_INT2_TINT) {
/*
* Ignore any interrupts from the low threshold when not
* throttled in order to prevent console spam when the
* temperature is holding near the threshold.
*/
rv = meas_read8(chgnum, SM5803_REG_TINT_MEAS_MSB, &meas_reg);
if ((meas_reg <= SM5803_TINT_LOW_LEVEL) && throttled) {
throttled = false;
throttle_ap(THROTTLE_OFF, THROTTLE_HARD,
THROTTLE_SRC_THERMAL);
} else if (meas_reg >= SM5803_TINT_HIGH_LEVEL) {
throttled = true;
throttle_ap(THROTTLE_ON, THROTTLE_HARD,
THROTTLE_SRC_THERMAL);
}
/*
* If the interrupt came in and we're not currently throttling
* or the level is below the upper threshold, it can likely be
* ignored.
*/
}
/* TODO(b/159376384): Take action on fatal BFET power alert. */
rv = main_read8(chgnum, SM5803_REG_INT3_REQ, &int_reg);
if (rv) {
CPRINTS("%s %d: Failed to read int3 register", CHARGER_NAME,
chgnum);
return;
}
if ((int_reg & SM5803_INT3_BFET_PWR_LIMIT) ||
(int_reg & SM5803_INT3_BFET_PWR_HWSAFE_LIMIT)) {
battery_get_params(&bp);
act_chg = charge_manager_get_active_charge_port();
CPRINTS("%s BFET power limit reached! (%s)", CHARGER_NAME,
(int_reg & SM5803_INT3_BFET_PWR_LIMIT) ? "warn" :
"FATAL");
CPRINTS("\tVbat: %dmV", bp.voltage);
CPRINTS("\tIbat: %dmA", bp.current);
charger_get_voltage(act_chg, &val);
CPRINTS("\tVsys(aux): %dmV", val);
charger_get_current(act_chg, &val);
CPRINTS("\tIsys: %dmA", val);
cflush();
}
rv = main_read8(chgnum, SM5803_REG_INT4_REQ, &int_reg);
if (rv) {
CPRINTS("%s %d: Failed to read int4 register", CHARGER_NAME,
chgnum);
return;
}
if (int_reg & SM5803_INT4_CHG_FAIL) {
int status_reg;
act_chg = charge_manager_get_active_charge_port();
chg_read8(chgnum, SM5803_REG_STATUS_CHG_REG, &status_reg);
CPRINTS("%s %d: CHG_FAIL_INT fired. Status 0x%02x",
CHARGER_NAME, chgnum, status_reg);
/* Write 1 to clear status interrupts */
chg_write8(chgnum, SM5803_REG_STATUS_CHG_REG, status_reg);
/*
* If a survivable fault happened, re-start sinking on the
* active charger after an appropriate delay.
*/
if (status_reg & SM5803_STATUS_CHG_OV_ITEMP) {
active_restart_port = act_chg;
hook_call_deferred(&sm5803_restart_charging_data,
30 * SECOND);
} else if ((status_reg & SM5803_STATUS_CHG_OV_VBAT) &&
act_chg == CHARGER_PRIMARY) {
active_restart_port = act_chg;
hook_call_deferred(&sm5803_restart_charging_data,
1 * SECOND);
}
}
if (int_reg & SM5803_INT4_CHG_DONE)
CPRINTS("%s %d: CHG_DONE_INT fired!!!", CHARGER_NAME, chgnum);
}
static void sm5803_irq_deferred(void)
{
int i;
uint32_t pending = atomic_clear(&irq_pending);
for (i = 0; i < CHARGER_NUM; i++)
if (BIT(i) & pending)
sm5803_handle_interrupt(i);
}
DECLARE_DEFERRED(sm5803_irq_deferred);
void sm5803_interrupt(int chgnum)
{
atomic_or(&irq_pending, BIT(chgnum));
hook_call_deferred(&sm5803_irq_deferred_data, 0);
}
static enum ec_error_list sm5803_get_dev_id(int chgnum, int *id)
{
int rv = EC_SUCCESS;
if (dev_id == UNKNOWN_DEV_ID)
rv = main_read8(chgnum, SM5803_REG_CHIP_ID, &dev_id);
if (!rv)
*id = dev_id;
return rv;
}
static const struct charger_info *sm5803_get_info(int chgnum)
{
return &sm5803_charger_info;
}
static enum ec_error_list sm5803_get_status(int chgnum, int *status)
{
enum ec_error_list rv;
int reg;
/* Charger obeys smart battery requests - making it level 2 */
*status = CHARGER_LEVEL_2;
rv = chg_read8(chgnum, SM5803_REG_FLOW1, ®);
if (rv)
return rv;
if ((reg & SM5803_FLOW1_MODE) == CHARGER_MODE_DISABLED &&
!(reg & SM5803_FLOW1_LINEAR_CHARGE_EN))
*status |= CHARGER_CHARGE_INHIBITED;
return EC_SUCCESS;
}
static enum ec_error_list sm5803_set_mode(int chgnum, int mode)
{
enum ec_error_list rv = EC_SUCCESS;
if (mode & CHARGE_FLAG_INHIBIT_CHARGE) {
rv = sm5803_flow1_update(chgnum, 0xFF, MASK_CLR);
rv |= sm5803_flow2_update(chgnum, SM5803_FLOW2_AUTO_ENABLED,
MASK_CLR);
}
return rv;
}
static enum ec_error_list sm5803_get_current(int chgnum, int *current)
{
enum ec_error_list rv;
int reg;
int curr;
rv = meas_read8(chgnum, SM5803_REG_IBAT_CHG_AVG_MEAS_MSB, ®);
if (rv)
return rv;
curr = reg << 2;
rv = meas_read8(chgnum, SM5803_REG_IBAT_CHG_AVG_MEAS_LSB, ®);
if (rv)
return rv;
curr |= reg & SM5803_IBAT_CHG_MEAS_LSB;
/* The LSB is 7.32mA */
*current = curr * 732 / 100;
return EC_SUCCESS;
}
static enum ec_error_list sm5803_set_current(int chgnum, int current)
{
enum ec_error_list rv;
int reg;
rv = chg_read8(chgnum, SM5803_REG_FAST_CONF4, ®);
if (rv)
return rv;
reg &= ~SM5803_CONF4_ICHG_FAST;
reg |= SM5803_CURRENT_TO_REG(current);
rv = chg_write8(chgnum, SM5803_REG_FAST_CONF4, reg);
return rv;
}
static enum ec_error_list sm5803_get_voltage(int chgnum, int *voltage)
{
enum ec_error_list rv;
int reg;
int volt_bits;
rv = meas_read8(chgnum, SM5803_REG_VSYS_AVG_MEAS_MSB, ®);
if (rv)
return rv;
volt_bits = reg << 2;
rv = meas_read8(chgnum, SM5803_REG_VSYS_AVG_MEAS_LSB, ®);
if (rv)
return rv;
volt_bits |= reg & 0x3;
/* The LSB is 23.4mV */
*voltage = volt_bits * 234 / 10;
return EC_SUCCESS;
}
static enum ec_error_list sm5803_set_voltage(int chgnum, int voltage)
{
enum ec_error_list rv;
int regval;
static int attempt_bfet_enable;
regval = SM5803_VOLTAGE_TO_REG(voltage);
/*
* Note: Set both voltages on both chargers. Vbat will only be used on
* primary, which enables charging.
*/
rv = chg_write8(chgnum, SM5803_REG_VSYS_PREREG_MSB, (regval >> 3));
rv |= chg_write8(chgnum, SM5803_REG_VSYS_PREREG_LSB, (regval & 0x7));
rv |= chg_write8(chgnum, SM5803_REG_VBAT_FAST_MSB, (regval >> 3));
rv |= chg_write8(chgnum, SM5803_REG_VBAT_FAST_LSB, (regval & 0x7));
if (IS_ENABLED(CONFIG_OCPC) && chgnum != CHARGER_PRIMARY) {
/*
* Check to see if the BFET is enabled. If not, enable it by
* toggling linear mode on the primary charger. The BFET can be
* disabled if the system is powered up from an auxiliary charge
* port and the battery is dead.
*/
rv |= chg_read8(CHARGER_PRIMARY, SM5803_REG_LOG1, ®val);
if (!(regval & SM5803_BATFET_ON) && !attempt_bfet_enable) {
CPRINTS("SM5803: Attempting to turn on BFET");
cflush();
rv |= sm5803_flow1_update(CHARGER_PRIMARY,
SM5803_FLOW1_LINEAR_CHARGE_EN,
MASK_SET);
rv |= sm5803_flow1_update(CHARGER_PRIMARY,
SM5803_FLOW1_LINEAR_CHARGE_EN,
MASK_CLR);
attempt_bfet_enable = 1;
sm5803_vbus_sink_enable(chgnum, 1);
}
}
return rv;
}
static enum ec_error_list sm5803_discharge_on_ac(int chgnum, int enable)
{
enum ec_error_list rv = EC_SUCCESS;
if (enable) {
rv = sm5803_vbus_sink_enable(chgnum, 0);
} else {
if (chgnum == charge_manager_get_active_charge_port())
rv = sm5803_vbus_sink_enable(chgnum, 1);
}
return rv;
}
static enum ec_error_list sm5803_get_vbus_voltage(int chgnum, int port,
int *voltage)
{
enum ec_error_list rv;
int reg;
int volt_bits;
rv = meas_read8(chgnum, SM5803_REG_VBUS_MEAS_MSB, ®);
if (rv)
return rv;
volt_bits = reg << 2;
rv = meas_read8(chgnum, SM5803_REG_VBUS_MEAS_LSB, ®);
volt_bits |= reg & SM5803_VBUS_MEAS_LSB;
/* Vbus ADC is in 23.4 mV steps */
*voltage = (volt_bits * 234) / 10;
return rv;
}
static enum ec_error_list sm5803_set_input_current(int chgnum,
int input_current)
{
int reg;
reg = SM5803_CURRENT_TO_REG(input_current) & SM5803_CHG_ILIM_RAW;
return chg_write8(chgnum, SM5803_REG_CHG_ILIM, reg);
}
static enum ec_error_list sm5803_get_input_current(int chgnum,
int *input_current)
{
enum ec_error_list rv;
int reg;
int curr;
rv = meas_read8(chgnum, SM5803_REG_IBUS_CHG_MEAS_MSB, ®);
if (rv)
return rv;
curr = reg << 2;
rv = meas_read8(chgnum, SM5803_REG_IBUS_CHG_MEAS_LSB, ®);
if (rv)
return rv;
curr |= reg & 0x3;
/* The LSB is 7.32mA */
*input_current = curr * 732 / 100;
return EC_SUCCESS;
}
static enum ec_error_list sm5803_get_option(int chgnum, int *option)
{
enum ec_error_list rv;
uint32_t control;
int reg;
rv = chg_read8(chgnum, SM5803_REG_FLOW1, ®);
control = reg;
rv |= chg_read8(chgnum, SM5803_REG_FLOW2, ®);
control |= reg << 8;
rv |= chg_read8(chgnum, SM5803_REG_FLOW3, ®);
control |= reg << 16;
return rv;
}
static enum ec_error_list sm5803_set_option(int chgnum, int option)
{
enum ec_error_list rv;
int reg;
mutex_lock(&flow1_access_lock[chgnum]);
reg = option & 0xFF;
rv = chg_write8(chgnum, SM5803_REG_FLOW1, reg);
mutex_unlock(&flow1_access_lock[chgnum]);
if (rv)
return rv;
reg = (option >> 8) & 0xFF;
rv = chg_write8(chgnum, SM5803_REG_FLOW2, reg);
if (rv)
return rv;
reg = (option >> 16) & 0xFF;
rv = chg_write8(chgnum, SM5803_REG_FLOW3, reg);
return rv;
}
static enum ec_error_list sm5803_set_otg_current_voltage(int chgnum,
int output_current,
int output_voltage)
{
enum ec_error_list rv;
int reg;
rv = chg_read8(chgnum, SM5803_REG_DISCH_CONF5, ®);
if (rv)
return rv;
reg &= ~SM5803_DISCH_CONF5_CLS_LIMIT;
reg |= MIN((output_current / SM5803_CLS_CURRENT_STEP),
SM5803_DISCH_CONF5_CLS_LIMIT);
rv |= chg_write8(chgnum, SM5803_REG_DISCH_CONF5, reg);
reg = SM5803_VOLTAGE_TO_REG(output_voltage);
rv = chg_write8(chgnum, SM5803_REG_VPWR_MSB, (reg >> 3));
rv |= chg_write8(chgnum, SM5803_REG_DISCH_CONF2,
reg & SM5803_DISCH_CONF5_VPWR_LSB);
return rv;
}
static enum ec_error_list sm5803_enable_otg_power(int chgnum, int enabled)
{
enum ec_error_list rv;
int reg, status;
if (enabled) {
int selected_current;
rv = chg_read8(chgnum, SM5803_REG_ANA_EN1, ®);
if (rv)
return rv;
/* Enable current limit */
reg &= ~SM5803_ANA_EN1_CLS_DISABLE;
rv = chg_write8(chgnum, SM5803_REG_ANA_EN1, reg);
/* Disable ramps on current set in discharge */
rv |= chg_read8(chgnum, SM5803_REG_DISCH_CONF6, ®);
reg |= SM5803_DISCH_CONF6_RAMPS_DIS;
rv |= chg_write8(chgnum, SM5803_REG_DISCH_CONF6, reg);
/*
* In order to ensure the Vbus output doesn't overshoot too
* much, turn the starting voltage down to 4.8 V and ramp up
* after 4 ms
*/
rv = chg_read8(chgnum, SM5803_REG_DISCH_CONF5, ®);
if (rv)
return rv;
selected_current = (reg & SM5803_DISCH_CONF5_CLS_LIMIT) *
SM5803_CLS_CURRENT_STEP;
sm5803_set_otg_current_voltage(chgnum, selected_current, 4800);
/*
* Enable: SOURCE_MODE - enable sourcing out
* DIRECTCHG_SOURCE_EN - enable current loop
* (for designs with no external Vbus FET)
*/
rv = sm5803_flow1_update(chgnum, CHARGER_MODE_SOURCE |
SM5803_FLOW1_DIRECTCHG_SRC_EN,
MASK_SET);
usleep(4000);
sm5803_set_otg_current_voltage(chgnum, selected_current, 5000);
} else {
/* Always clear out discharge status before clearing FLOW1 */
rv = chg_read8(chgnum, SM5803_REG_STATUS_DISCHG, &status);
if (rv)
return rv;
if (status)
CPRINTS("%s %d: Discharge failure 0x%02x", CHARGER_NAME,
chgnum, status);
rv |= chg_write8(chgnum, SM5803_REG_STATUS_DISCHG, status);
/* Re-enable ramps on current set in discharge */
rv |= chg_read8(chgnum, SM5803_REG_DISCH_CONF6, ®);
reg &= ~SM5803_DISCH_CONF6_RAMPS_DIS;
rv |= chg_write8(chgnum, SM5803_REG_DISCH_CONF6, reg);
/*
* PD tasks will always turn off previous sourcing on init.
* Protect ourselves from brown out on init by checking if we're
* sinking right now. The init process should only leave sink
* mode enabled if a charger is plugged in; otherwise it's
* expected to be 0.
*
* Always clear out sourcing if the previous source-out failed.
*/
rv |= chg_read8(chgnum, SM5803_REG_FLOW1, ®);
if (rv)
return rv;
if ((reg & SM5803_FLOW1_MODE) != CHARGER_MODE_SINK || status)
rv = sm5803_flow1_update(chgnum, CHARGER_MODE_SOURCE |
SM5803_FLOW1_DIRECTCHG_SRC_EN,
MASK_CLR);
}
return rv;
}
static int sm5803_is_sourcing_otg_power(int chgnum, int port)
{
enum ec_error_list rv;
int reg;
rv = chg_read8(chgnum, SM5803_REG_FLOW1, ®);
if (rv)
return 0;
/*
* Note: In linear mode, MB charger will read a reserved mode when
* sourcing, so bit 1 is the most reliable way to detect sourcing.
*/
return (reg & BIT(1));
}
static enum ec_error_list sm5803_set_vsys_compensation(int chgnum,
struct ocpc_data *ocpc,
int current_ma,
int voltage_mv)
{
int rv;
int regval;
int r;
/* Set IR drop compensation */
r = ocpc->combined_rsys_rbatt_mo * 100 / 167; /* 1.67mOhm steps */
r = MAX(0, r);
rv = chg_write8(chgnum, SM5803_REG_IR_COMP2, r & 0x7F);
rv |= chg_read8(chgnum, SM5803_REG_IR_COMP1, ®val);
regval &= ~SM5803_IR_COMP_RES_SET_MSB;
r = r >> 8; /* Bits 9:8 */
regval |= (r & 0x3) << SM5803_IR_COMP_RES_SET_MSB_SHIFT;
regval |= SM5803_IR_COMP_EN;
rv |= chg_write8(chgnum, SM5803_REG_IR_COMP1, regval);
if (rv)
return EC_ERROR_UNKNOWN;
return EC_ERROR_UNIMPLEMENTED;
}
#ifdef CONFIG_CMD_CHARGER_DUMP
static int command_sm5803_dump(int argc, char **argv)
{
int reg;
int regval;
int chgnum = 0;
if (argc > 1)
chgnum = atoi(argv[1]);
/* Dump base regs */
ccprintf("BASE regs\n");
for (reg = 0x01; reg <= 0x30; reg++) {
if (!main_read8(chgnum, reg, ®val))
ccprintf("[0x%02X] = 0x%02x\n", reg, regval);
if (reg & 0xf) {
cflush(); /* Flush periodically */
watchdog_reload();
}
}
/* Dump measure regs */
ccprintf("MEAS regs\n");
for (reg = 0x01; reg <= 0xED; reg++) {
if (!meas_read8(chgnum, reg, ®val))
ccprintf("[0x%02X] = 0x%02x\n", reg, regval);
if (reg & 0xf) {
cflush(); /* Flush periodically */
watchdog_reload();
}
}
/* Dump Charger regs from 0x1C to 0x7F */
ccprintf("CHG regs\n");
for (reg = 0x1C; reg <= 0x7F; reg++) {
if (!chg_read8(chgnum, reg, ®val))
ccprintf("[0x%02X] = 0x%02x\n", reg, regval);
if (reg & 0xf) {
cflush(); /* Flush periodically */
watchdog_reload();
}
}
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(charger_dump, command_sm5803_dump,
"charger_dump [chgnum]", "Dumps SM5803 registers");
#endif /* CONFIG_CMD_CHARGER_DUMP */
const struct charger_drv sm5803_drv = {
.init = &sm5803_init,
.post_init = &sm5803_post_init,
.get_info = &sm5803_get_info,
.get_status = &sm5803_get_status,
.set_mode = &sm5803_set_mode,
.get_current = &sm5803_get_current,
.set_current = &sm5803_set_current,
.get_voltage = &sm5803_get_voltage,
.set_voltage = &sm5803_set_voltage,
.discharge_on_ac = &sm5803_discharge_on_ac,
.get_vbus_voltage = &sm5803_get_vbus_voltage,
.set_input_current = &sm5803_set_input_current,
.get_input_current = &sm5803_get_input_current,
.device_id = &sm5803_get_dev_id,
.get_option = &sm5803_get_option,
.set_option = &sm5803_set_option,
.set_otg_current_voltage = &sm5803_set_otg_current_voltage,
.enable_otg_power = &sm5803_enable_otg_power,
.is_sourcing_otg_power = &sm5803_is_sourcing_otg_power,
.set_vsys_compensation = &sm5803_set_vsys_compensation,
};
|