summaryrefslogtreecommitdiff
path: root/common/usbc/usb_tc_ctvpd_sm.c
blob: 045cca55c43db87678f3049be7fdafb00526ef3c (plain)
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
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
/* Copyright 2019 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "builtin/assert.h"
#include "common.h"
#include "console.h"
#include "system.h"
#include "task.h"
#include "tcpm/tcpm.h"
#include "typec_control.h"
#include "usb_pd.h"
#include "usb_tc_sm.h"
#include "vpd_api.h"

/* USB Type-C CTVPD module */

#ifdef CONFIG_COMMON_RUNTIME
#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ##args)
#define CPRINTS(format, args...) cprints(CC_USBPD, format, ##args)
#else /* CONFIG_COMMON_RUNTIME */
#define CPRINTF(format, args...)
#define CPRINTS(format, args...)
#endif

/* Type-C Layer Flags */
#define TC_FLAGS_VCONN_ON BIT(0)

#define SUPPORT_TIMER_RESET_INIT 0
#define SUPPORT_TIMER_RESET_REQUEST 1
#define SUPPORT_TIMER_RESET_COMPLETE 2

/**
 * This is the Type-C Port object that contains information needed to
 * implement a Charge Through VCONN Powered Device.
 */
static struct type_c {
	/* state machine context */
	struct sm_ctx ctx;
	/* Higher-level power deliver state machines are enabled if true. */
	uint8_t pd_enable;
	/* port flags, see TC_FLAGS_* */
	uint32_t flags;
	/*
	 * Time a charge-through port shall wait before it can determine it
	 * is attached
	 */
	uint64_t cc_debounce;
	/* Time a host port shall wait before it can determine it is attached */
	uint64_t host_cc_debounce;
	/* Time a Sink port shall wait before it can determine it is detached
	 * due to the potential for USB PD signaling on CC as described in
	 * the state definitions.
	 */
	uint64_t pd_debounce;
	/* Maintains state of billboard device */
	int billboard_presented;
	/*
	 * Time a port shall wait before it can determine it is
	 * re-attached during the try-wait process.
	 */
	uint64_t try_wait_debounce;
	/* charge-through support timer */
	uint64_t support_timer;
	/* reset the charge-through support timer */
	uint8_t support_timer_reset;
	/* VPD host port cc state */
	enum pd_cc_states host_cc_state;
	uint8_t ct_cc;
	/* The cc state */
	enum pd_cc_states cc_state;
	uint64_t next_role_swap;
} tc[CONFIG_USB_PD_PORT_MAX_COUNT];

/* List of all TypeC-level states */
enum usb_tc_state {
	/* Normal States */
	TC_DISABLED,
	TC_UNATTACHED_SNK,
	TC_ATTACH_WAIT_SNK,
	TC_ATTACHED_SNK,
	TC_ERROR_RECOVERY,
	TC_TRY_SNK,
	TC_UNATTACHED_SRC,
	TC_ATTACH_WAIT_SRC,
	TC_TRY_WAIT_SRC,
	TC_ATTACHED_SRC,
	TC_CT_TRY_SNK,
	TC_CT_ATTACH_WAIT_UNSUPPORTED,
	TC_CT_ATTACHED_UNSUPPORTED,
	TC_CT_UNATTACHED_UNSUPPORTED,
	TC_CT_UNATTACHED_VPD,
	TC_CT_DISABLED_VPD,
	TC_CT_ATTACHED_VPD,
	TC_CT_ATTACH_WAIT_VPD,
	/* Super States */
	TC_VBUS_CC_ISO,
	TC_HOST_RARD_CT_RD,
	TC_HOST_OPEN_CT_OPEN,
	TC_HOST_RP3_CT_RD,
	TC_HOST_RP3_CT_RPU,
	TC_HOST_RPU_CT_RD,
};

/* Forward declare the full list of states. This is indexed by usb_tc_state */
static const struct usb_state tc_states[];

/* List of human readable state names for console debugging */
__maybe_unused const char *const tc_state_names[] = {
#ifdef CONFIG_COMMON_RUNTIME
	[TC_DISABLED] = "Disabled",
	[TC_UNATTACHED_SNK] = "Unattached.SNK",
	[TC_ATTACH_WAIT_SNK] = "AttachWait.SNK",
	[TC_ATTACHED_SNK] = "Attached.SNK",
	[TC_ERROR_RECOVERY] = "ErrorRecovery",
	[TC_TRY_SNK] = "Try.SNK",
	[TC_UNATTACHED_SRC] = "Unattached.SRC",
	[TC_ATTACH_WAIT_SRC] = "AttachWait.SRC",
	[TC_TRY_WAIT_SRC] = "TryWait.SRC",
	[TC_ATTACHED_SRC] = "Attached.SRC",
	[TC_CT_TRY_SNK] = "CTTry.SNK",
	[TC_CT_ATTACH_WAIT_UNSUPPORTED] = "CTAttachWait.Unsupported",
	[TC_CT_ATTACHED_UNSUPPORTED] = "CTAttached.Unsupported",
	[TC_CT_UNATTACHED_UNSUPPORTED] = "CTUnattached.Unsupported",
	[TC_CT_UNATTACHED_VPD] = "CTUnattached.VPD",
	[TC_CT_DISABLED_VPD] = "CTDisabled.VPD",
	[TC_CT_ATTACHED_VPD] = "CTAttached.VPD",
	[TC_CT_ATTACH_WAIT_VPD] = "CTAttachWait.VPD",
#endif
};

/* Forward declare private, common functions */
static void set_state_tc(const int port, enum usb_tc_state new_state);

/* Public TypeC functions */

enum pd_power_role pd_get_power_role(int port)
{
	/* Vconn power device is always the sink */
	return PD_ROLE_SINK;
}

enum pd_cable_plug tc_get_cable_plug(int port)
{
	/* Vconn power device is always the cable */
	return PD_PLUG_FROM_CABLE;
}

enum pd_data_role pd_get_data_role(int port)
{
	/* Vconn power device doesn't have a data role, but UFP matches SNK */
	return PD_ROLE_UFP;
}

/* Note tc_set_power_role and tc_set_data_role are unimplemented */

uint8_t tc_get_polarity(int port)
{
	/* Does not track polarity */
	return 0;
}

uint8_t tc_get_pd_enabled(int port)
{
	return tc[port].pd_enable;
}

void tc_reset_support_timer(int port)
{
	tc[port].support_timer_reset |= SUPPORT_TIMER_RESET_REQUEST;
}

void tc_start_error_recovery(int port)
{
	assert(port == TASK_ID_TO_PD_PORT(task_get_current()));

	/*
	 *   The port should transition to the ErrorRecovery state
	 *   from any other state when directed.
	 */
	set_state_tc(port, TC_ERROR_RECOVERY);
}

/*
 * TCPC CC/Rp management
 *
 * Stub for linking purposes.
 * This is not supported for ctvpd, we are never the SOP partner.
 */
void typec_select_pull(int port, enum tcpc_cc_pull pull)
{
}
void typec_select_src_current_limit_rp(int port, enum tcpc_rp_value rp)
{
}
void typec_select_src_collision_rp(int port, enum tcpc_rp_value rp)
{
}
int typec_update_cc(int port)
{
	return EC_SUCCESS;
}

void tc_state_init(int port)
{
	int res = 0;

	res = tcpm_init(port);

	CPRINTS("C%d: init %s", port, res ? "failed" : "ready");

	/* Disable if restart failed, otherwise start in default state. */
	set_state_tc(port, res ? TC_DISABLED : TC_UNATTACHED_SNK);

	/* Disable pd state machines */
	tc[port].pd_enable = 0;
	tc[port].billboard_presented = 0;
	tc[port].flags = 0;
}

void tc_event_check(int port, int evt)
{
	/* Do Nothing */
}

void tc_run(const int port)
{
	run_state(port, &tc[port].ctx);
}

/* Internal Functions */

/* Set the TypeC state machine to a new state. */
static void set_state_tc(const int port, enum usb_tc_state new_state)
{
	set_state(port, &tc[port].ctx, &tc_states[new_state]);
}

/* Get the current TypeC state. */
test_export_static enum usb_tc_state get_state_tc(const int port)
{
	return tc[port].ctx.current - &tc_states[0];
}

/* Get the previous TypeC state. */
static enum usb_tc_state get_last_state_tc(const int port)
{
	return tc[port].ctx.previous - &tc_states[0];
}

test_mockable_static void print_current_state(const int port)
{
	CPRINTS("C%d: %s", port, tc_state_names[get_state_tc(port)]);
}

int pd_is_connected(int port)
{
	return (get_state_tc(port) == TC_ATTACHED_SNK) ||
	       (get_state_tc(port) == TC_ATTACHED_SRC) ||
	       (get_state_tc(port) == TC_CT_ATTACHED_UNSUPPORTED) ||
	       (get_state_tc(port) == TC_CT_ATTACHED_VPD);
}

bool pd_is_disconnected(int port)
{
	return !pd_is_connected(port);
}

/**
 * Disabled
 *
 * Super State Entry Actions:
 *   Isolate the Host-side port from the Charge-Through port
 *   Enable mcu communication
 *   Remove the terminations from Host
 *   Remove the terminations from Charge-Through
 */
static void tc_disabled_entry(const int port)
{
	print_current_state(port);
}

static void tc_disabled_run(const int port)
{
	task_wait_event(-1);
}

static void tc_disabled_exit(const int port)
{
	if (!IS_ENABLED(CONFIG_USB_PD_TCPC)) {
		if (tcpm_init(port) != 0) {
			CPRINTS("C%d: restart failed!", port);
			return;
		}
	}

	CPRINTS("C%d: resumed!", port);
}

void pd_set_suspend(int port, int suspend)
{
	/*
	 * This shouldn't happen. If it does, we need to send an event to the
	 * PD task to put the SM into the disabled state. It is not safe to
	 * directly set_state here since this may be in another task.
	 */
	assert(false);
}

/**
 * ErrorRecovery
 *
 * Super State Entry Actions:
 *   Isolate the Host-side port from the Charge-Through port
 *   Enable mcu communication
 *   Remove the terminations from Host
 *   Remove the terminations from Charge-Through
 */
static void tc_error_recovery_entry(const int port)
{
	print_current_state(port);
	/* Use cc_debounce state variable for error recovery timeout */
	tc[port].cc_debounce = get_time().val + PD_T_ERROR_RECOVERY;
}

static void tc_error_recovery_run(const int port)
{
	if (get_time().val > tc[port].cc_debounce)
		set_state_tc(port, TC_UNATTACHED_SNK);
}

/**
 * Unattached.SNK
 *
 * Super State Entry Actions:
 *   Isolate the Host-side port from the Charge-Through port
 *   Enable mcu communication
 *   Place Ra on VCONN and Rd on Host CC
 *   Place Rd on Charge-Through CCs
 */
static void tc_unattached_snk_entry(const int port)
{
	if (get_last_state_tc(port) != TC_UNATTACHED_SRC)
		print_current_state(port);

	tc[port].flags &= ~TC_FLAGS_VCONN_ON;
	tc[port].cc_state = PD_CC_UNSET;
}

static void tc_unattached_snk_run(const int port)
{
	int host_cc;
	int new_cc_state;
	int cc1;
	int cc2;

	/* Check Host CC for connection */
	vpd_host_get_cc(&host_cc);

	/*
	 * Transition to AttachWait.SNK when a Source connection is
	 * detected, as indicated by the SNK.Rp state on its Host-side
	 * port’s CC pin.
	 */
	if (cc_is_rp(host_cc)) {
		set_state_tc(port, TC_ATTACH_WAIT_SNK);
		return;
	}

	/* Check Charge-Through CCs for connection */
	vpd_ct_get_cc(&cc1, &cc2);

	if (cc_is_rp(cc1) != cc_is_rp(cc2))
		new_cc_state = PD_CC_DFP_ATTACHED;
	else
		new_cc_state = PD_CC_NONE;

	/* Debounce Charge-Through CC state */
	if (tc[port].cc_state != new_cc_state) {
		tc[port].cc_state = new_cc_state;
		tc[port].cc_debounce = get_time().val + PD_T_CC_DEBOUNCE;
	}

	/* If we are here, Host CC must be open */

	/* Wait for Charge-Through CC debounce */
	if (get_time().val < tc[port].cc_debounce)
		return;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * Unattached.SRC when the state of the Host-side port’s CC pin is
	 * SNK.Open for tDRP − dcSRC.DRP ∙ tDRP and both of the following
	 * is detected on the Charge-Through port.
	 *   1) SNK.Rp state is detected on exactly one of the CC1 or CC2
	 *      pins for at least tCCDebounce
	 *   2) VBUS is detected
	 */
	if (vpd_is_ct_vbus_present() &&
	    tc[port].cc_state == PD_CC_DFP_ATTACHED) {
		set_state_tc(port, TC_UNATTACHED_SRC);
		return;
	}
}

/**
 * AttachWait.SNK
 *
 * Super State Entry Actions:
 *   Isolate the Host-side port from the Charge-Through port
 *   Enable mcu communication
 *   Place Ra on VCONN and Rd on Host CC
 *   Place Rd on Charge-Through CCs
 */
static void tc_attach_wait_snk_entry(const int port)
{
	print_current_state(port);
	tc[port].host_cc_state = PD_CC_UNSET;
}

static void tc_attach_wait_snk_run(const int port)
{
	int host_new_cc_state;
	int host_cc;

	/* Check Host CC for connection */
	vpd_host_get_cc(&host_cc);

	if (cc_is_rp(host_cc))
		host_new_cc_state = PD_CC_DFP_ATTACHED;
	else
		host_new_cc_state = PD_CC_NONE;

	/* Debounce the Host CC state */
	if (tc[port].host_cc_state != host_new_cc_state) {
		tc[port].host_cc_state = host_new_cc_state;
		if (host_new_cc_state == PD_CC_DFP_ATTACHED)
			tc[port].host_cc_debounce =
				get_time().val + PD_T_CC_DEBOUNCE;
		else
			tc[port].host_cc_debounce =
				get_time().val + PD_T_PD_DEBOUNCE;
		return;
	}

	/* Wait for Host CC debounce */
	if (get_time().val < tc[port].host_cc_debounce)
		return;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * Attached.SNK after the state of the Host-side port’s CC pin is
	 * SNK.Rp for at least tCCDebounce and either host-side VCONN or
	 * VBUS is detected.
	 *
	 * Transition to Unattached.SNK when the state of both the CC1 and
	 * CC2 pins is SNK.Open for at least tPDDebounce.
	 */
	if (tc[port].host_cc_state == PD_CC_DFP_ATTACHED &&
	    (vpd_is_vconn_present() || vpd_is_host_vbus_present()))
		set_state_tc(port, TC_ATTACHED_SNK);
	else if (tc[port].host_cc_state == PD_CC_NONE)
		set_state_tc(port, TC_UNATTACHED_SNK);
}

/**
 * Attached.SNK
 */
static void tc_attached_snk_entry(const int port)
{
	print_current_state(port);

	/* Enable PD */
	tc[port].pd_enable = 1;
	typec_set_polarity(port, 0);

	/*
	 * This state can only be entered from states AttachWait.SNK
	 * and Try.SNK. So the Host port is isolated from the
	 * Charge-Through port. We only need to High-Z the
	 * Charge-Through ports CC1 and CC2 pins.
	 */
	vpd_ct_set_pull(TYPEC_CC_OPEN, 0);

	tc[port].host_cc_state = PD_CC_UNSET;

	/* Start Charge-Through support timer */
	tc[port].support_timer_reset = SUPPORT_TIMER_RESET_INIT;
	tc[port].support_timer = get_time().val + PD_T_AME;
}

static void tc_attached_snk_run(const int port)
{
	int host_new_cc_state;
	int host_cc;

	/* Has host vbus and vconn been removed */
	if (!vpd_is_host_vbus_present() && !vpd_is_vconn_present()) {
		set_state_tc(port, TC_UNATTACHED_SNK);
		return;
	}

	/*
	 * Reset the Charge-Through Support Timer when it first
	 * receives any USB PD Structured VDM Command it supports,
	 * which is the Discover Identity command. And this is only
	 * done one time.
	 */
	if (tc[port].support_timer_reset == SUPPORT_TIMER_RESET_REQUEST) {
		tc[port].support_timer_reset |= SUPPORT_TIMER_RESET_COMPLETE;
		tc[port].support_timer = get_time().val + PD_T_AME;
	}

	/* Check Host CC for connection */
	vpd_host_get_cc(&host_cc);

	if (cc_is_rp(host_cc))
		host_new_cc_state = PD_CC_DFP_ATTACHED;
	else
		host_new_cc_state = PD_CC_NONE;

	/* Debounce the Host CC state */
	if (tc[port].host_cc_state != host_new_cc_state) {
		tc[port].host_cc_state = host_new_cc_state;
		tc[port].host_cc_debounce = get_time().val + PD_T_VPDCTDD;
		return;
	}

	/* Wait for Host CC debounce */
	if (get_time().val < tc[port].host_cc_debounce)
		return;

	if (vpd_is_vconn_present()) {
		if (!(tc[port].flags & TC_FLAGS_VCONN_ON)) {
			/* VCONN detected. Remove RA */
			vpd_host_set_pull(TYPEC_CC_RD, 0);
			tc[port].flags |= TC_FLAGS_VCONN_ON;
		}

		/*
		 * A Charge-Through VCONN-Powered USB Device shall transition
		 * to CTUnattached.VPD if VCONN is present and the state of
		 * its Host-side port’s CC pin is SNK.Open for tVPDCTDD.
		 */
		if (tc[port].host_cc_state == PD_CC_NONE) {
			set_state_tc(port, TC_CT_UNATTACHED_VPD);
			return;
		}
	}

	/* Check the Support Timer */
	if (get_time().val > tc[port].support_timer &&
	    !tc[port].billboard_presented) {
		/*
		 * Present USB Billboard Device Class interface
		 * indicating that Charge-Through is not supported
		 */
		tc[port].billboard_presented = 1;
		vpd_present_billboard(BB_SNK);
	}
}

static void tc_attached_snk_exit(const int port)
{
	tc[port].billboard_presented = 0;
	vpd_present_billboard(BB_NONE);
}

/**
 * Super State HOST_RA_CT_RD
 */
static void tc_host_rard_ct_rd_entry(const int port)
{
	/* Place Ra on VCONN and Rd on Host CC */
	vpd_host_set_pull(TYPEC_CC_RA_RD, 0);

	/* Place Rd on Charge-Through CCs */
	vpd_ct_set_pull(TYPEC_CC_RD, 0);
}

/**
 * Super State HOST_OPEN_CT_OPEN
 */
static void tc_host_open_ct_open_entry(const int port)
{
	/* Remove the terminations from Host */
	vpd_host_set_pull(TYPEC_CC_OPEN, 0);

	/* Remove the terminations from Charge-Through */
	vpd_ct_set_pull(TYPEC_CC_OPEN, 0);
}

/**
 * Super State VBUS_CC_ISO
 */
static void tc_vbus_cc_iso_entry(const int port)
{
	/* Isolate the Host-side port from the Charge-Through port */
	vpd_vbus_pass_en(0);

	/* Remove Charge-Through side port CCs */
	vpd_ct_cc_sel(CT_OPEN);

	/* Enable mcu communication and cc */
	vpd_mcu_cc_en(1);
}

/**
 * Unattached.SRC
 *
 * Super State Entry Actions:
 *   Isolate the Host-side port from the Charge-Through port
 *   Enable mcu communication
 *   Place RpUSB on Host CC
 *   Place Rd on Charge-Through CCs
 */
static void tc_unattached_src_entry(const int port)
{
	if (get_last_state_tc(port) != TC_UNATTACHED_SNK)
		print_current_state(port);

	/* Get power from VBUS */
	vpd_vconn_pwr_sel_odl(PWR_VBUS);

	/* Make sure it's the Charge-Through Port's VBUS */
	if (!vpd_is_ct_vbus_present()) {
		set_state_tc(port, TC_ERROR_RECOVERY);
		return;
	}

	tc[port].next_role_swap = get_time().val + PD_T_DRP_SRC;
}

static void tc_unattached_src_run(const int port)
{
	int host_cc;

	/* Check Host CC for connection */
	vpd_host_get_cc(&host_cc);

	/*
	 * Transition to AttachWait.SRC when host-side VBUS is
	 * vSafe0V and SRC.Rd state is detected on the Host-side
	 * port’s CC pin.
	 */
	if (!vpd_is_host_vbus_present() && host_cc == TYPEC_CC_VOLT_RD) {
		set_state_tc(port, TC_ATTACH_WAIT_SRC);
		return;
	}

	/*
	 * Transition to Unattached.SNK within tDRPTransition or
	 * if Charge-Through VBUS is removed.
	 */
	if (!vpd_is_ct_vbus_present() ||
	    get_time().val > tc[port].next_role_swap) {
		set_state_tc(port, TC_UNATTACHED_SNK);
		return;
	}
}

/**
 * AttachWait.SRC
 *
 * Super State Entry Actions:
 *   Isolate the Host-side port from the Charge-Through port
 *   Enable mcu communication
 *   Place RpUSB on Host CC
 *   Place Rd on Charge-Through CCs
 */
static void tc_attach_wait_src_entry(const int port)
{
	print_current_state(port);

	tc[port].host_cc_state = PD_CC_UNSET;
}

static void tc_attach_wait_src_run(const int port)
{
	int host_new_cc_state;
	int host_cc;

	/* Check Host CC for connection */
	vpd_host_get_cc(&host_cc);

	if (host_cc == TYPEC_CC_VOLT_RD)
		host_new_cc_state = PD_CC_UFP_ATTACHED;
	else
		host_new_cc_state = PD_CC_NONE;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition
	 * to Unattached.SNK when the SRC.Open state is detected on the
	 * Host-side port’s CC or if Charge-Through VBUS falls below
	 * vSinkDisconnect. The Charge-Through VCONN-Powered USB Device
	 * shall detect the SRC.Open state within tSRCDisconnect, but
	 * should detect it as quickly as possible.
	 */
	if (host_new_cc_state == PD_CC_NONE || !vpd_is_ct_vbus_present()) {
		set_state_tc(port, TC_UNATTACHED_SNK);
		return;
	}

	/* Debounce the Host CC state */
	if (tc[port].host_cc_state != host_new_cc_state) {
		tc[port].host_cc_state = host_new_cc_state;
		tc[port].cc_debounce = get_time().val + PD_T_CC_DEBOUNCE;
		return;
	}

	/* Wait for Host CC debounce */
	if (get_time().val < tc[port].cc_debounce)
		return;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * Try.SNK when the host-side VBUS is at vSafe0V and the SRC.Rd
	 * state is on the Host-side port’s CC pin for at least tCCDebounce.
	 */
	if (tc[port].host_cc_state == PD_CC_UFP_ATTACHED &&
	    !vpd_is_host_vbus_present()) {
		set_state_tc(port, TC_TRY_SNK);
		return;
	}
}

/**
 * Attached.SRC
 */
static void tc_attached_src_entry(const int port)
{
	print_current_state(port);

	/* Enable PD */
	tc[port].pd_enable = 1;
	typec_set_polarity(port, 0);

	/* Connect Charge-Through VBUS to Host VBUS */
	vpd_vbus_pass_en(1);

	/*
	 * Get power from VBUS. No need to test because
	 * the Host VBUS is connected to the Charge-Through
	 * VBUS
	 */
	vpd_vconn_pwr_sel_odl(PWR_VBUS);
}

static void tc_attached_src_run(const int port)
{
	int host_cc;

	/* Check Host CC for connection */
	vpd_host_get_cc(&host_cc);

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * Unattached.SNK when VBUS falls below vSinkDisconnect or the
	 * Host-side port’s CC pin is SRC.Open. The Charge-Through
	 * VCONNPowered USB Device shall detect the SRC.Open state within
	 * tSRCDisconnect, but should detect it as quickly as possible.
	 */
	if (!vpd_is_ct_vbus_present() || host_cc == TYPEC_CC_VOLT_OPEN)
		set_state_tc(port, TC_UNATTACHED_SNK);
}

/**
 * Super State HOST_RPU_CT_RD
 */
static void tc_host_rpu_ct_rd_entry(const int port)
{
	/* Place RpUSB on Host CC */
	vpd_host_set_pull(TYPEC_CC_RP, TYPEC_RP_USB);

	/* Place Rd on Charge-Through CCs */
	vpd_ct_set_pull(TYPEC_CC_RD, 0);
}

/**
 * Try.SNK
 *
 * Super State Entry Actions:
 *   Isolate the Host-side port from the Charge-Through port
 *   Enable mcu communication
 *   Place Ra on VCONN and Rd on Host CC
 *   Place Rd on Charge-Through CCs
 */
static void tc_try_snk_entry(const int port)
{
	print_current_state(port);

	/* Get power from VBUS */
	vpd_vconn_pwr_sel_odl(PWR_VBUS);

	/* Make sure it's the Charge-Through Port's VBUS */
	if (!vpd_is_ct_vbus_present()) {
		set_state_tc(port, TC_ERROR_RECOVERY);
		return;
	}

	tc[port].host_cc_state = PD_CC_UNSET;

	/* Using next_role_swap timer as try_src timer */
	tc[port].next_role_swap = get_time().val + PD_T_DRP_TRY;
}

static void tc_try_snk_run(const int port)
{
	int host_new_cc_state;
	int host_cc;

	/*
	 * Wait for tDRPTry before monitoring the Charge-Through
	 * port’s CC pins for the SNK.Rp
	 */
	if (get_time().val < tc[port].next_role_swap)
		return;

	/* Check Host CC for connection */
	vpd_host_get_cc(&host_cc);

	if (cc_is_rp(host_cc))
		host_new_cc_state = PD_CC_DFP_ATTACHED;
	else
		host_new_cc_state = PD_CC_NONE;

	/* Debounce the Host CC state */
	if (tc[port].host_cc_state != host_new_cc_state) {
		tc[port].host_cc_state = host_new_cc_state;
		tc[port].cc_debounce = get_time().val + PD_T_DEBOUNCE;
		return;
	}

	/* Wait for Host CC debounce */
	if (get_time().val < tc[port].cc_debounce)
		return;

	/*
	 * The Charge-Through VCONN-Powered USB Device shall then transition to
	 * Attached.SNK when the SNK.Rp state is detected on the Host-side
	 * port’s CC pin for at least tTryCCDebounce and VBUS or VCONN is
	 * detected on Host-side port.
	 *
	 * Alternatively, the Charge-Through VCONN-Powered USB Device shall
	 * transition to TryWait.SRC if Host-side SNK.Rp state is not detected
	 * for tTryCCDebounce.
	 */
	if (tc[port].host_cc_state == PD_CC_DFP_ATTACHED &&
	    (vpd_is_host_vbus_present() || vpd_is_vconn_present()))
		set_state_tc(port, TC_ATTACHED_SNK);
	else if (tc[port].host_cc_state == PD_CC_NONE)
		set_state_tc(port, TC_TRY_WAIT_SRC);
}

/**
 * TryWait.SRC
 *
 * Super State Entry Actions:
 *   Isolate the Host-side port from the Charge-Through port
 *   Enable mcu communication
 *   Place RpUSB on Host CC
 *   Place Rd on Charge-Through CCs
 */
static void tc_try_wait_src_entry(const int port)
{
	print_current_state(port);

	tc[port].host_cc_state = PD_CC_UNSET;
	tc[port].next_role_swap = get_time().val + PD_T_DRP_TRY;
}

static void tc_try_wait_src_run(const int port)
{
	int host_new_cc_state;
	int host_cc;

	/* Check Host CC for connection */
	vpd_host_get_cc(&host_cc);

	if (host_cc == TYPEC_CC_VOLT_RD)
		host_new_cc_state = PD_CC_UFP_ATTACHED;
	else
		host_new_cc_state = PD_CC_NONE;

	/* Debounce the Host CC state */
	if (tc[port].host_cc_state != host_new_cc_state) {
		tc[port].host_cc_state = host_new_cc_state;
		tc[port].host_cc_debounce =
			get_time().val + PD_T_TRY_CC_DEBOUNCE;
		return;
	}

	if (get_time().val > tc[port].host_cc_debounce) {
		/*
		 * A Charge-Through VCONN-Powered USB Device shall transition
		 * to Attached.SRC when host-side VBUS is at vSafe0V and the
		 * SRC.Rd state is detected on the Host-side port’s CC pin for
		 * at least tTryCCDebounce.
		 */
		if (tc[port].host_cc_state == PD_CC_UFP_ATTACHED &&
		    !vpd_is_host_vbus_present()) {
			set_state_tc(port, TC_ATTACHED_SRC);
			return;
		}
	}

	if (get_time().val > tc[port].next_role_swap) {
		/*
		 * The Charge-Through VCONN-Powered USB Device shall transition
		 * to Unattached.SNK after tDRPTry if the Host-side port’s CC
		 * pin is not in the SRC.Rd state.
		 */
		if (tc[port].host_cc_state == PD_CC_NONE) {
			set_state_tc(port, TC_UNATTACHED_SNK);
			return;
		}
	}
}

/**
 * CTTry.SNK
 *
 * Super State Entry Actions:
 *   Isolate the Host-side port from the Charge-Through port
 *   Enable mcu communication
 *   Place RP3A0 on Host CC
 *   Connect Charge-Through Rd
 *   Get power from VCONN
 */
static void tc_ct_try_snk_entry(const int port)
{
	print_current_state(port);

	/* Enable PD */
	tc[port].pd_enable = 1;
	typec_set_polarity(port, 0);

	tc[port].cc_state = PD_CC_UNSET;
	tc[port].next_role_swap = get_time().val + PD_T_DRP_TRY;
}

static void tc_ct_try_snk_run(const int port)
{
	int new_cc_state;
	int cc1;
	int cc2;

	/*
	 * Wait for tDRPTry before monitoring the Charge-Through
	 * port’s CC pins for the SNK.Rp
	 */
	if (get_time().val < tc[port].next_role_swap)
		return;

	/* Check CT CC for connection */
	vpd_ct_get_cc(&cc1, &cc2);

	if (cc_is_rp(cc1) || cc_is_rp(cc2))
		new_cc_state = PD_CC_DFP_ATTACHED;
	else
		new_cc_state = PD_CC_NONE;

	/*
	 * The Charge-Through VCONN-Powered USB Device shall transition
	 * to Unattached.SNK if VCONN falls below vVCONNDisconnect.
	 */
	if (!vpd_is_vconn_present()) {
		set_state_tc(port, TC_UNATTACHED_SNK);
		return;
	}

	/* Debounce the CT CC state */
	if (tc[port].cc_state != new_cc_state) {
		tc[port].cc_state = new_cc_state;
		tc[port].cc_debounce = get_time().val + PD_T_DEBOUNCE;
		tc[port].try_wait_debounce = get_time().val + PD_T_TRY_WAIT;

		return;
	}

	if (get_time().val > tc[port].cc_debounce) {
		/*
		 * The Charge-Through VCONN-Powered USB Device shall then
		 * transition to CTAttached.VPD when the SNK.Rp state is
		 * detected on the Charge-Through port’s CC pins for at
		 * least tTryCCDebounce and VBUS is detected on
		 * Charge-Through port.
		 */
		if (tc[port].cc_state == PD_CC_DFP_ATTACHED &&
		    vpd_is_ct_vbus_present()) {
			set_state_tc(port, TC_CT_ATTACHED_VPD);
			return;
		}
	}

	if (get_time().val > tc[port].try_wait_debounce) {
		/*
		 * A Charge-Through VCONN-Powered USB Device shall transition
		 * to CTAttached.Unsupported if SNK.Rp state is not detected
		 * for tDRPTryWait.
		 */
		if (tc[port].cc_state == PD_CC_NONE) {
			set_state_tc(port, TC_CT_ATTACHED_UNSUPPORTED);
			return;
		}
	}
}

static void tc_ct_try_snk_exit(const int port)
{
	/* Disable PD */
	tc[port].pd_enable = 0;
}

/**
 * CTAttachWait.Unsupported
 *
 *  Super State Entry Actions:
 *    Isolate the Host-side port from the Charge-Through port
 *    Enable mcu communication
 *    Place RP3A0 on Host CC
 *    Place RPUSB on Charge-Through CC
 *    Get power from VCONN
 */
static void tc_ct_attach_wait_unsupported_entry(const int port)
{
	print_current_state(port);

	/* Enable PD */
	tc[port].pd_enable = 1;
	typec_set_polarity(port, 0);

	tc[port].cc_state = PD_CC_UNSET;
}

static void tc_ct_attach_wait_unsupported_run(const int port)
{
	int new_cc_state;
	int cc1;
	int cc2;

	/* Check CT CC for connection */
	vpd_ct_get_cc(&cc1, &cc2);

	if (cc_is_at_least_one_rd(cc1, cc2))
		new_cc_state = PD_CC_UFP_ATTACHED;
	else if (cc_is_audio_acc(cc1, cc2))
		new_cc_state = PD_CC_UFP_AUDIO_ACC;
	else /* (cc1 == TYPEC_CC_VOLT_OPEN or cc2 == TYPEC_CC_VOLT_OPEN */
		new_cc_state = PD_CC_NONE;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * Unattached.SNK if VCONN falls below vVCONNDisconnect.
	 */
	if (!vpd_is_vconn_present()) {
		set_state_tc(port, TC_UNATTACHED_SNK);
		return;
	}

	/* Debounce the cc state */
	if (tc[port].cc_state != new_cc_state) {
		tc[port].cc_state = new_cc_state;
		tc[port].cc_debounce = get_time().val + PD_T_CC_DEBOUNCE;
		return;
	}

	/* Wait for CC debounce */
	if (get_time().val < tc[port].cc_debounce)
		return;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * CTUnattached.VPD when the state of either the Charge-Through
	 * Port’s CC1 or CC2 pin is SRC.Open for at least tCCDebounce.
	 *
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * CTTry.SNK if the state of at least one of the Charge-Through
	 * port’s CC pins is SRC.Rd, or if the state of both the CC1 and CC2
	 * pins is SRC.Ra. for at least tCCDebounce.
	 */
	if (new_cc_state == PD_CC_NONE)
		set_state_tc(port, TC_CT_UNATTACHED_VPD);
	else /* PD_CC_UFP_ATTACHED or PD_CC_UFP_AUDIO_ACC */
		set_state_tc(port, TC_CT_TRY_SNK);
}

static void tc_ct_attach_wait_unsupported_exit(const int port)
{
	/* Disable PD */
	tc[port].pd_enable = 0;
}

/**
 * CTAttached.Unsupported
 *
 *  Super State Entry Actions:
 *    Isolate the Host-side port from the Charge-Through port
 *    Enable mcu communication
 *    Place RP3A0 on Host CC
 *    Place RPUSB on Charge-Through CC
 *    Get power from VCONN
 */
static void tc_ct_attached_unsupported_entry(const int port)
{
	print_current_state(port);

	/* Present Billboard device */
	vpd_present_billboard(BB_SNK);
}

static void tc_ct_attached_unsupported_run(const int port)
{
	int cc1;
	int cc2;

	/* Check CT CC for connection */
	vpd_ct_get_cc(&cc1, &cc2);

	if (!vpd_is_vconn_present()) {
		set_state_tc(port, TC_UNATTACHED_SNK);
		return;
	}

	/*
	 * The Charge-Through VCONN-Powered USB Device shall transition to
	 * CTUnattached.VPD when SRC.Open state is detected on both the
	 * Charge-Through port’s CC pins or the SRC.Open state is detected
	 * on one CC pin and SRC.Ra is detected on the other CC pin.
	 */
	if ((cc1 == TYPEC_CC_VOLT_OPEN && cc2 == TYPEC_CC_VOLT_OPEN) ||
	    (cc1 == TYPEC_CC_VOLT_OPEN && cc2 == TYPEC_CC_VOLT_RA) ||
	    (cc1 == TYPEC_CC_VOLT_RA && cc2 == TYPEC_CC_VOLT_OPEN)) {
		set_state_tc(port, TC_CT_UNATTACHED_VPD);
		return;
	}
}

static void tc_ct_attached_unsupported_exit(const int port)
{
	vpd_present_billboard(BB_NONE);
}

/**
 * CTUnattached.Unsupported
 *
 *  Super State Entry Actions:
 *    Isolate the Host-side port from the Charge-Through port
 *    Enable mcu communication
 *    Place RP3A0 on Host CC
 *    Place RPUSB on Charge-Through CC
 *    Get power from VCONN
 */
static void tc_ct_unattached_unsupported_entry(const int port)
{
	if (get_last_state_tc(port) != TC_CT_UNATTACHED_VPD)
		print_current_state(port);

	/* Enable PD */
	tc[port].pd_enable = 1;
	typec_set_polarity(port, 0);

	tc[port].next_role_swap = get_time().val + PD_T_DRP_SRC;
}

static void tc_ct_unattached_unsupported_run(const int port)
{
	int cc1;
	int cc2;

	/* Check CT CC for connection */
	vpd_ct_get_cc(&cc1, &cc2);

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * CTAttachWait.Unsupported when a Sink connection is detected on
	 * the Charge-Through port, as indicated by the SRC.Rd state on at
	 * least one of the Charge-Through port’s CC pins or SRC.Ra state
	 * on both the CC1 and CC2 pins.
	 */
	if (cc_is_at_least_one_rd(cc1, cc2) || cc_is_audio_acc(cc1, cc2)) {
		set_state_tc(port, TC_CT_ATTACH_WAIT_UNSUPPORTED);
		return;
	}

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * Unattached.SNK if VCONN falls below vVCONNDisconnect.
	 */
	if (!vpd_is_vconn_present()) {
		set_state_tc(port, TC_UNATTACHED_SNK);
		return;
	}

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * CTUnattached.VPD within tDRPTransition after dcSRC.DRP ∙ tDRP.
	 */
	if (get_time().val > tc[port].next_role_swap) {
		set_state_tc(port, TC_CT_UNATTACHED_VPD);
		return;
	}
}

static void tc_ct_unattached_unsupported_exit(const int port)
{
	/* Disable PD */
	tc[port].pd_enable = 0;
}

/**
 * CTUnattached.VPD
 *
 *  Super State Entry Actions:
 *    Isolate the Host-side port from the Charge-Through port
 *    Enable mcu communication
 *    Place RP3A0 on Host CC
 *    Connect Charge-Through Rd
 *    Get power from VCONN
 */
static void tc_ct_unattached_vpd_entry(const int port)
{
	if (get_last_state_tc(port) != TC_CT_UNATTACHED_UNSUPPORTED)
		print_current_state(port);

	/* Enable PD */
	tc[port].pd_enable = 1;
	typec_set_polarity(port, 0);

	tc[port].cc_state = PD_CC_UNSET;
}

static void tc_ct_unattached_vpd_run(const int port)
{
	int new_cc_state;
	int cc1;
	int cc2;

	/* Check CT CC for connection */
	vpd_ct_get_cc(&cc1, &cc2);

	if (cc_is_rp(cc1) != cc_is_rp(cc2))
		new_cc_state = PD_CC_DFP_ATTACHED;
	else if (!cc_is_rp(cc1) && !cc_is_rp(cc2))
		new_cc_state = PD_CC_NONE;
	else
		new_cc_state = PD_CC_UNSET;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * CTAttachWait.VPD when a Source connection is detected on the
	 * Charge-Through port, as indicated by the SNK.Rp state on
	 * exactly one of the Charge-Through port’s CC pins.
	 */
	if (new_cc_state == PD_CC_DFP_ATTACHED) {
		set_state_tc(port, TC_CT_ATTACH_WAIT_VPD);
		return;
	}

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * Unattached.SNK if VCONN falls below vVCONNDisconnect.
	 */
	if (!vpd_is_vconn_present()) {
		set_state_tc(port, TC_UNATTACHED_SNK);
		return;
	}

	/* Debounce the cc state */
	if (new_cc_state != tc[port].cc_state) {
		tc[port].cc_state = new_cc_state;
		tc[port].cc_debounce = get_time().val + PD_T_DRP_SRC;
		return;
	}

	if (get_time().val < tc[port].cc_debounce)
		return;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * CTUnattached.Unsupported within tDRPTransition after the state
	 * of both the Charge-Through port’s CC1 and CC2 pins is SNK.Open
	 * for tDRP-dcSRC.DRP ∙ tDRP, or if directed.
	 */
	if (tc[port].cc_state == PD_CC_NONE) {
		set_state_tc(port, TC_CT_UNATTACHED_UNSUPPORTED);
		return;
	}
}

static void tc_ct_unattached_vpd_exit(const int port)
{
	/* Disable PD */
	tc[port].pd_enable = 0;
}

/**
 * CTDisabled.VPD
 *
 * Super State Entry Actions:
 *   Isolate the Host-side port from the Charge-Through port
 *   Enable mcu communication
 *   Remove the terminations from Host
 *   Remove the terminations from Charge-Through
 */
static void tc_ct_disabled_vpd_entry(const int port)
{
	print_current_state(port);

	/* Get power from VBUS */
	vpd_vconn_pwr_sel_odl(PWR_VBUS);

	tc[port].next_role_swap = get_time().val + PD_T_VPDDISABLE;
}

static void tc_ct_disabled_vpd_run(const int port)
{
	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition
	 * to Unattached.SNK after tVPDDisable.
	 */
	if (get_time().val > tc[port].next_role_swap)
		set_state_tc(port, TC_UNATTACHED_SNK);
}

/**
 * CTAttached.VPD
 */
static void tc_ct_attached_vpd_entry(const int port)
{
	int cc1;
	int cc2;
	print_current_state(port);

	/* Get power from VCONN */
	vpd_vconn_pwr_sel_odl(PWR_VCONN);

	/*
	 * Detect which of the Charge-Through port’s CC1 or CC2
	 * pins is connected through the cable
	 */
	vpd_ct_get_cc(&cc1, &cc2);
	tc[port].ct_cc = cc_is_rp(cc2) ? CT_CC2 : CT_CC1;

	/*
	 * 1. Remove or reduce any additional capacitance on the
	 *    Host-side CC port
	 */
	vpd_mcu_cc_en(0);

	/*
	 * 2. Disable the Rp termination advertising 3.0 A on the
	 *    host port’s CC pin
	 */
	vpd_host_set_pull(TYPEC_CC_OPEN, 0);

	/*
	 * 3. Passively multiplex the detected Charge-Through port’s
	 *    CC pin through to the host port’s CC
	 */
	vpd_ct_cc_sel(tc[port].ct_cc);

	/*
	 * 4. Disable the Rd on the Charge-Through port’s CC1 and CC2
	 *    pins
	 */
	vpd_ct_set_pull(TYPEC_CC_OPEN, 0);

	/*
	 * 5. Connect the Charge-Through port’s VBUS through to the
	 *    host port’s VBUS
	 */
	vpd_vbus_pass_en(1);

	tc[port].cc_state = PD_CC_UNSET;
}

static void tc_ct_attached_vpd_run(const int port)
{
	int new_cc_state;
	int cc1;
	int cc2;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * CTDisabled.VPD if VCONN falls below vVCONNDisconnect.
	 */
	if (!vpd_is_vconn_present()) {
		set_state_tc(port, TC_CT_DISABLED_VPD);
		return;
	}

	/* Check CT CC for connection */
	vpd_ct_get_cc(&cc1, &cc2);
	if ((tc[port].ct_cc ? cc2 : cc1) == TYPEC_CC_VOLT_OPEN)
		new_cc_state = PD_CC_NONE;
	else
		new_cc_state = PD_CC_DFP_ATTACHED;

	/* Debounce the cc state */
	if (new_cc_state != tc[port].cc_state) {
		tc[port].cc_state = new_cc_state;
		tc[port].cc_debounce = get_time().val + PD_T_VPDCTDD;
		return;
	}

	if (get_time().val < tc[port].pd_debounce)
		return;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * CTUnattached.VPD when VBUS falls below vSinkDisconnect and the
	 * state of the passed-through CC pin is SNK.Open for tVPDCTDD.
	 */
	if (tc[port].cc_state == PD_CC_NONE && !vpd_is_ct_vbus_present())
		set_state_tc(port, TC_CT_UNATTACHED_VPD);
}

/**
 * CTAttachWait.VPD
 *
 *  Super State Entry Actions:
 *    Isolate the Host-side port from the Charge-Through port
 *    Enable mcu communication
 *    Place RP3A0 on Host CC
 *    Connect Charge-Through Rd
 *    Get power from VCONN
 */
static void tc_ct_attach_wait_vpd_entry(const int port)
{
	print_current_state(port);

	/* Enable PD */
	tc[port].pd_enable = 1;
	typec_set_polarity(port, 0);

	tc[port].cc_state = PD_CC_UNSET;
}

static void tc_ct_attach_wait_vpd_run(const int port)
{
	int new_cc_state;
	int cc1;
	int cc2;

	/* Check CT CC for connection */
	vpd_ct_get_cc(&cc1, &cc2);

	if (cc_is_rp(cc1) != cc_is_rp(cc2))
		new_cc_state = PD_CC_DFP_ATTACHED;
	else if (!cc_is_rp(cc1) && !cc_is_rp(cc2))
		new_cc_state = PD_CC_NONE;
	else
		new_cc_state = PD_CC_UNSET;

	/*
	 * A Charge-Through VCONN-Powered USB Device shall transition to
	 * CTDisabled.VPD if VCONN falls below vVCONNDisconnect.
	 */
	if (!vpd_is_vconn_present()) {
		set_state_tc(port, TC_CT_DISABLED_VPD);
		return;
	}

	/* Debounce the cc state */
	if (new_cc_state != tc[port].cc_state) {
		tc[port].cc_state = new_cc_state;
		tc[port].cc_debounce = get_time().val + PD_T_CC_DEBOUNCE;
		tc[port].pd_debounce = get_time().val + PD_T_PD_DEBOUNCE;
		return;
	}

	if (get_time().val > tc[port].pd_debounce) {
		/*
		 * A Charge-Through VCONN-Powered USB Device shall transition
		 * to CTUnattached.VPD when the state of both the Charge-Through
		 * port’s CC1 and CC2 pins are SNK.Open for at least
		 * tPDDebounce.
		 */
		if (tc[port].cc_state == PD_CC_NONE) {
			set_state_tc(port, TC_CT_UNATTACHED_VPD);
			return;
		}
	}

	if (get_time().val > tc[port].cc_debounce) {
		/*
		 * A Charge-Through VCONN-Powered USB Device shall transition to
		 * CTAttached.VPD after the state of only one of the
		 * Charge-Through port’s CC1 or CC2 pins is SNK.Rp for at
		 * least tCCDebounce and VBUS on the Charge-Through port is
		 * detected.
		 */
		if (tc[port].cc_state == PD_CC_DFP_ATTACHED &&
		    vpd_is_ct_vbus_present()) {
			set_state_tc(port, TC_CT_ATTACHED_VPD);
			return;
		}
	}
}

static void tc_ct_attach_wait_vpd_exit(const int port)
{
	/* Disable PD */
	tc[port].pd_enable = 0;
}

/**
 * Super State HOST_RP3_CT_RD
 */
static void tc_host_rp3_ct_rd_entry(const int port)
{
	/* Place RP3A0 on Host CC */
	vpd_host_set_pull(TYPEC_CC_RP, TYPEC_RP_3A0);

	/* Connect Charge-Through Rd */
	vpd_ct_set_pull(TYPEC_CC_RD, 0);

	/*
	 * A Charge-Through VCONN-Powered USB Device shall
	 * ensure that it is powered by VCONN
	 */

	/* Make sure vconn is on */
	if (!vpd_is_vconn_present())
		set_state_tc(port, TC_ERROR_RECOVERY);

	/* Get power from VCONN */
	vpd_vconn_pwr_sel_odl(PWR_VCONN);
}

/**
 * Super State HOST_RP3_CT_RPU
 */
static void tc_host_rp3_ct_rpu_entry(const int port)
{
	/* Place RP3A0 on Host CC */
	vpd_host_set_pull(TYPEC_CC_RP, TYPEC_RP_3A0);

	/* Place RPUSB on Charge-Through CC */
	vpd_ct_set_pull(TYPEC_CC_RP, TYPEC_RP_USB);

	/*
	 * A Charge-Through VCONN-Powered USB Device shall
	 * ensure that it is powered by VCONN
	 */

	/* Make sure vconn is on */
	if (!vpd_is_vconn_present())
		set_state_tc(port, TC_ERROR_RECOVERY);

	/* Get power from VCONN */
	vpd_vconn_pwr_sel_odl(PWR_VCONN);
}

/* All necessary Type-C states */

/*
 * Type-C State Hierarchy (Sub-States are listed inside the boxes)
 *
 * | TC_VBUS_CC_ISO ------------------------------------------------------|
 * |                                                                      |
 * |  | TC_HOST_RARD_CT_RD -----------| | TC_HOST_OPEN_CT_OPEN ---------| |
 * |  |                               | |                               | |
 * |  | TC_UNATTACHED_SNK             | | TC_DISABLED                   | |
 * |  | TC_ATTACH_WAIT_SNK            | | TC_ERROR_RECOVERY             | |
 * |  | TC_TRY_SNK                    | |-------------------------------| |
 * |  |-------------------------------|                                   |
 * |                                                                      |
 * |  | TC_HOST_RP3_CT_RD ------------| | TC_HOST_RPU_CT_RD ------------| |
 * |  |                               | |                               | |
 * |  | TC_CT_TRY_SNK                 | | TC_UNATTACHED_SRC             | |
 * |  | TC_CT_UNATTACHED_VPD          | | TC_ATTACH_WAIT_SRC            | |
 * |  | TC_CT_ATTACH_WAIT_VPD         | | TC_TRY_WAIT_SR                | |
 * |  |-------------------------------| |-------------------------------| |
 * |                                                                      |
 * |  | TC_HOST_RP3_CT_RPU -----------|                                   |
 * |  |                               |                                   |
 * |  | TC_CT_ATTACH_WAIT_UNSUPPORTED |                                   |
 * |  | TC_CT_ATTACHED_UNSUPPORTED    |                                   |
 * |  | TC_CT_UNATTACHED_UNSUPPORTED  |                                   |
 * |  |-------------------------------|                                   |
 * |----------------------------------------------------------------------|
 *
 * TC_ATTACHED_SNK
 * TC_ATTACHED_SRC
 * TC_CT_ATTACHED_VPD
 *
 */
static const struct usb_state tc_states[] = {
	/* Super States */
	[TC_VBUS_CC_ISO] = {
		.entry  = tc_vbus_cc_iso_entry,
	},
	[TC_HOST_RARD_CT_RD] = {
		.entry  = tc_host_rard_ct_rd_entry,
		.parent = &tc_states[TC_VBUS_CC_ISO],
	},
	[TC_HOST_OPEN_CT_OPEN] = {
		.entry  = tc_host_open_ct_open_entry,
		.parent = &tc_states[TC_VBUS_CC_ISO],
	},
	[TC_HOST_RP3_CT_RD] = {
		.entry  = tc_host_rp3_ct_rd_entry,
		.parent = &tc_states[TC_VBUS_CC_ISO],
	},
	[TC_HOST_RP3_CT_RPU] = {
		.entry  = tc_host_rp3_ct_rpu_entry,
		.parent = &tc_states[TC_VBUS_CC_ISO],
	},
	[TC_HOST_RPU_CT_RD] = {
		.entry  = tc_host_rpu_ct_rd_entry,
		.parent = &tc_states[TC_VBUS_CC_ISO],
	},
	/* Normal States */
	[TC_DISABLED] = {
		.entry  = tc_disabled_entry,
		.run    = tc_disabled_run,
		.exit   = tc_disabled_exit,
		.parent = &tc_states[TC_HOST_OPEN_CT_OPEN],
	},
	[TC_UNATTACHED_SNK] = {
		.entry  = tc_unattached_snk_entry,
		.run    = tc_unattached_snk_run,
		.parent = &tc_states[TC_HOST_RARD_CT_RD],
	},
	[TC_ATTACH_WAIT_SNK] = {
		.entry  = tc_attach_wait_snk_entry,
		.run    = tc_attach_wait_snk_run,
		.parent = &tc_states[TC_HOST_RARD_CT_RD],
	},
	[TC_ATTACHED_SNK] = {
		.entry  = tc_attached_snk_entry,
		.run    = tc_attached_snk_run,
		.exit   = tc_attached_snk_exit,
	},
	[TC_ERROR_RECOVERY] = {
		.entry  = tc_error_recovery_entry,
		.run    = tc_error_recovery_run,
		.parent = &tc_states[TC_HOST_OPEN_CT_OPEN],
	},
	[TC_TRY_SNK] = {
		.entry  = tc_try_snk_entry,
		.run    = tc_try_snk_run,
		.parent = &tc_states[TC_HOST_RARD_CT_RD],
	},
	[TC_UNATTACHED_SRC] = {
		.entry  = tc_unattached_src_entry,
		.run    = tc_unattached_src_run,
		.parent = &tc_states[TC_HOST_RPU_CT_RD],
	},
	[TC_ATTACH_WAIT_SRC] = {
		.entry  = tc_attach_wait_src_entry,
		.run    = tc_attach_wait_src_run,
		.parent = &tc_states[TC_HOST_RPU_CT_RD],
	},
	[TC_TRY_WAIT_SRC] = {
		.entry  = tc_try_wait_src_entry,
		.run    = tc_try_wait_src_run,
		.parent = &tc_states[TC_HOST_RPU_CT_RD],
	},
	[TC_ATTACHED_SRC] = {
		.entry  = tc_attached_src_entry,
		.run    = tc_attached_src_run,
	},
	[TC_CT_TRY_SNK] = {
		.entry  = tc_ct_try_snk_entry,
		.run    = tc_ct_try_snk_run,
		.exit   = tc_ct_try_snk_exit,
		.parent = &tc_states[TC_HOST_RP3_CT_RD],
	},
	[TC_CT_ATTACH_WAIT_UNSUPPORTED] = {
		.entry  = tc_ct_attach_wait_unsupported_entry,
		.run    = tc_ct_attach_wait_unsupported_run,
		.exit   = tc_ct_attach_wait_unsupported_exit,
		.parent = &tc_states[TC_HOST_RP3_CT_RPU],
	},
	[TC_CT_ATTACHED_UNSUPPORTED] = {
		.entry  = tc_ct_attached_unsupported_entry,
		.run    = tc_ct_attached_unsupported_run,
		.exit   = tc_ct_attached_unsupported_exit,
		.parent = &tc_states[TC_HOST_RP3_CT_RPU],
	},
	[TC_CT_UNATTACHED_UNSUPPORTED] = {
		.entry  = tc_ct_unattached_unsupported_entry,
		.run    = tc_ct_unattached_unsupported_run,
		.exit   = tc_ct_unattached_unsupported_exit,
		.parent = &tc_states[TC_HOST_RP3_CT_RPU],
	},
	[TC_CT_UNATTACHED_VPD] = {
		.entry  = tc_ct_unattached_vpd_entry,
		.run    = tc_ct_unattached_vpd_run,
		.exit   = tc_ct_unattached_vpd_exit,
		.parent = &tc_states[TC_HOST_RP3_CT_RD],
	},
	[TC_CT_DISABLED_VPD] = {
		.entry  = tc_ct_disabled_vpd_entry,
		.run    = tc_ct_disabled_vpd_run,
		.parent = &tc_states[TC_HOST_OPEN_CT_OPEN],
	},
	[TC_CT_ATTACHED_VPD] = {
		.entry  = tc_ct_attached_vpd_entry,
		.run    = tc_ct_attached_vpd_run,
	},
	[TC_CT_ATTACH_WAIT_VPD] = {
		.entry  = tc_ct_attach_wait_vpd_entry,
		.run    = tc_ct_attach_wait_vpd_run,
		.exit   = tc_ct_attach_wait_vpd_exit,
		.parent = &tc_states[TC_HOST_RP3_CT_RD],
	},
};

#ifdef TEST_BUILD
const struct test_sm_data test_tc_sm_data[] = {
	{
		.base = tc_states,
		.size = ARRAY_SIZE(tc_states),
		.names = tc_state_names,
		.names_size = ARRAY_SIZE(tc_state_names),
	},
};
const int test_tc_sm_data_size = ARRAY_SIZE(test_tc_sm_data);
#endif