Loading...
   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
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2015-2017 Google, Inc
 *
 * USB Power Delivery protocol stack.
 */

#include <asm/gpio.h>
#include <asm/io.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <dm/device-internal.h>
#include <dm/devres.h>
#include <linux/compat.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/iopoll.h>
#include <time.h>
#include <usb/tcpm.h>
#include "tcpm-internal.h"

DECLARE_GLOBAL_DATA_PTR;

const char * const tcpm_states[] = {
	FOREACH_TCPM_STATE(GENERATE_TCPM_STRING)
};

const char * const typec_pd_rev_name[] = {
	[PD_REV10]		= "rev1",
	[PD_REV20]		= "rev2",
	[PD_REV30]		= "rev3",
};

const char * const typec_role_name[] = {
	[TYPEC_SINK]		= "sink",
	[TYPEC_SOURCE]		= "source",
};

const char * const typec_data_role_name[] = {
	[TYPEC_DEVICE]		= "device",
	[TYPEC_HOST]		= "host",
};

const char * const typec_orientation_name[] = {
	[TYPEC_ORIENTATION_NONE]	= "none",
	[TYPEC_ORIENTATION_NORMAL]	= "normal",
	[TYPEC_ORIENTATION_REVERSE]	= "reverse",
};

const char * const typec_cc_status_name[] = {
	[TYPEC_CC_OPEN]		= "open",
	[TYPEC_CC_RA]		= "ra",
	[TYPEC_CC_RD]		= "rd",
	[TYPEC_CC_RP_DEF]	= "rp-def",
	[TYPEC_CC_RP_1_5]	= "rp-1.5",
	[TYPEC_CC_RP_3_0]	= "rp-3.0",
};

static inline bool tcpm_cc_is_sink(enum typec_cc_status cc)
{
	return cc == TYPEC_CC_RP_DEF ||
	       cc == TYPEC_CC_RP_1_5 ||
	       cc == TYPEC_CC_RP_3_0;
}

static inline bool tcpm_port_is_sink(struct tcpm_port *port)
{
	bool cc1_is_snk = tcpm_cc_is_sink(port->cc1);
	bool cc2_is_snk = tcpm_cc_is_sink(port->cc2);

	return (cc1_is_snk && !cc2_is_snk) ||
	       (cc2_is_snk && !cc1_is_snk);
}

static inline bool tcpm_cc_is_source(enum typec_cc_status cc)
{
	return cc == TYPEC_CC_RD;
}

static inline bool tcpm_port_is_source(struct tcpm_port *port)
{
	bool cc1_is_src = tcpm_cc_is_source(port->cc1);
	bool cc2_is_src = tcpm_cc_is_source(port->cc2);

	return (cc1_is_src && !cc2_is_src) ||
	       (cc2_is_src && !cc1_is_src);
}

static inline bool tcpm_try_src(struct tcpm_port *port)
{
	return port->try_role == TYPEC_SOURCE &&
	       port->port_type == TYPEC_PORT_DRP;
}

static inline void tcpm_reset_event_cnt(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	port->poll_event_cnt = 0;
}

static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
{
	if (port->port_type == TYPEC_PORT_DRP) {
		if (port->try_role == TYPEC_SINK)
			return SNK_UNATTACHED;
		else if (port->try_role == TYPEC_SOURCE)
			return SRC_UNATTACHED;
	} else if (port->port_type == TYPEC_PORT_SNK) {
		return SNK_UNATTACHED;
	}
	return SRC_UNATTACHED;
}

static bool tcpm_port_is_disconnected(struct tcpm_port *port)
{
	return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
		port->cc2 == TYPEC_CC_OPEN) ||
	       (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
				    port->cc1 == TYPEC_CC_OPEN) ||
				   (port->polarity == TYPEC_POLARITY_CC2 &&
				    port->cc2 == TYPEC_CC_OPEN)));
}

static void tcpm_set_cc(struct udevice *dev, enum typec_cc_status cc)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	dev_dbg(dev, "TCPM: set cc = %d\n", cc);
	port->cc_req = cc;
	drvops->set_cc(dev, cc);
}

/*
 * Determine RP value to set based on maximum current supported
 * by a port if configured as source.
 * Returns CC value to report to link partner.
 */
static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
{
	const u32 *src_pdo = port->src_pdo;
	int nr_pdo = port->nr_src_pdo;
	int i;

	/*
	 * Search for first entry with matching voltage.
	 * It should report the maximum supported current.
	 */
	for (i = 0; i < nr_pdo; i++) {
		const u32 pdo = src_pdo[i];

		if (pdo_type(pdo) == PDO_TYPE_FIXED &&
		    pdo_fixed_voltage(pdo) == 5000) {
			unsigned int curr = pdo_max_current(pdo);

			if (curr >= 3000)
				return TYPEC_CC_RP_3_0;
			else if (curr >= 1500)
				return TYPEC_CC_RP_1_5;
			return TYPEC_CC_RP_DEF;
		}
	}

	return TYPEC_CC_RP_DEF;
}

static void tcpm_check_and_run_delayed_work(struct udevice *dev);

static bool tcpm_transmit_helper(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	drvops->poll_event(dev);
	udelay(500);
	tcpm_check_and_run_delayed_work(dev);
	return port->tx_complete;
}

static int tcpm_pd_transmit(struct udevice *dev,
			    enum tcpm_transmit_type type,
			    const struct pd_message *msg)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	u32 timeout_us = PD_T_TCPC_TX_TIMEOUT * 1000;
	bool tx_complete;
	int ret;

	if (msg)
		dev_dbg(dev, "TCPM: PD TX, header: %#x\n",
			le16_to_cpu(msg->header));
	else
		dev_dbg(dev, "TCPM: PD TX, type: %#x\n", type);

	port->tx_complete = false;
	ret = drvops->pd_transmit(dev, type, msg, port->negotiated_rev);
	if (ret < 0)
		return ret;

	/*
	 * At this point we basically need to block until the TCPM controller
	 * returns successful transmission. Since this is usually done using
	 * the generic interrupt status bits, we poll for any events. That
	 * will clear the interrupt status, so we also need to process any
	 * of the incoming events. This means we will do more processing and
	 * thus let's give everything a bit more time.
	 */
	timeout_us *= 5;
	ret = read_poll_timeout(tcpm_transmit_helper, tx_complete,
				!tx_complete, false, timeout_us, dev);
	if (ret < 0) {
		dev_err(dev, "TCPM: PD transmit data failed: %d\n", ret);
		return ret;
	}

	switch (port->tx_status) {
	case TCPC_TX_SUCCESS:
		port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
		break;
	case TCPC_TX_DISCARDED:
		ret = -EAGAIN;
		break;
	case TCPC_TX_FAILED:
	default:
		ret = -EIO;
		break;
	}

	return ret;
}

void tcpm_pd_transmit_complete(struct udevice *dev,
			       enum tcpm_transmit_status status)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	dev_dbg(dev, "TCPM: PD TX complete, status: %u\n", status);
	tcpm_reset_event_cnt(dev);
	port->tx_status = status;
	port->tx_complete = true;
}

static int tcpm_set_polarity(struct udevice *dev,
			     enum typec_cc_polarity polarity)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	int ret;

	dev_dbg(dev, "TCPM: set polarity = %d\n", polarity);

	if (drvops->set_polarity) {
		ret = drvops->set_polarity(dev, polarity);
		if (ret < 0)
			return ret;
	}

	port->polarity = polarity;

	return 0;
}

static int tcpm_set_vconn(struct udevice *dev, bool enable)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	int ret;

	dev_dbg(dev, "TCPM: set vconn = %d\n", enable);

	ret = drvops->set_vconn(dev, enable);
	if (!ret)
		port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;

	return ret;
}

static inline u32 tcpm_get_current_limit(struct tcpm_port *port)
{
	switch (port->polarity ? port->cc2 : port->cc1) {
	case TYPEC_CC_RP_1_5:
		return 1500;
	case TYPEC_CC_RP_3_0:
		return 3000;
	case TYPEC_CC_RP_DEF:
	default:
		return 0;
	}
}

static int tcpm_set_current_limit(struct udevice *dev, u32 max_ma, u32 mv)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	int ret = -EOPNOTSUPP;

	dev_info(dev, "TCPM: set voltage limit = %u mV\n", mv);
	dev_info(dev, "TCPM: set current limit = %u mA\n", max_ma);

	port->supply_voltage = mv;
	port->current_limit = max_ma;

	return ret;
}

static int tcpm_set_attached_state(struct udevice *dev, bool attached)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	return drvops->set_roles(dev, attached, port->pwr_role,
				 port->data_role);
}

static int tcpm_set_roles(struct udevice *dev, bool attached,
			  enum typec_role role, enum typec_data_role data)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	int ret;

	ret = drvops->set_roles(dev, attached, role, data);
	if (ret < 0)
		return ret;

	port->pwr_role = role;
	port->data_role = data;

	return 0;
}

static int tcpm_pd_send_source_caps(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	struct pd_message msg;
	int i;

	memset(&msg, 0, sizeof(msg));

	if (!port->nr_src_pdo) {
		/* No source capabilities defined, sink only */
		msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
					  port->pwr_role,
					  port->data_role,
					  port->negotiated_rev,
					  port->message_id, 0);
	} else {
		msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
					  port->pwr_role,
					  port->data_role,
					  port->negotiated_rev,
					  port->message_id,
					  port->nr_src_pdo);
	}

	for (i = 0; i < port->nr_src_pdo; i++)
		msg.payload[i] = cpu_to_le32(port->src_pdo[i]);

	return tcpm_pd_transmit(dev, TCPC_TX_SOP, &msg);
}

static int tcpm_pd_send_sink_caps(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	struct pd_message msg;
	unsigned int i;

	memset(&msg, 0, sizeof(msg));

	if (!port->nr_snk_pdo) {
		/* No sink capabilities defined, source only */
		msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
					  port->pwr_role,
					  port->data_role,
					  port->negotiated_rev,
					  port->message_id, 0);
	} else {
		msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
					  port->pwr_role,
					  port->data_role,
					  port->negotiated_rev,
					  port->message_id,
					  port->nr_snk_pdo);
	}

	for (i = 0; i < port->nr_snk_pdo; i++)
		msg.payload[i] = cpu_to_le32(port->snk_pdo[i]);

	return tcpm_pd_transmit(dev, TCPC_TX_SOP, &msg);
}

static void tcpm_state_machine(struct udevice *dev);

static inline void tcpm_timer_uninit(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	port->delay_target = 0;
}

static void tcpm_timer_init(struct udevice *dev, uint32_t ms)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	unsigned long time_us = ms * 1000;

	port->delay_target = timer_get_us() + time_us;
}

static void tcpm_check_and_run_delayed_work(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	/* no delayed state changes scheduled */
	if (port->delay_target == 0)
		return;

	/* it's not yet time */
	if (timer_get_us() < port->delay_target)
		return;

	tcpm_timer_uninit(dev);
	tcpm_state_machine(dev);
}

static void mod_tcpm_delayed_work(struct udevice *dev, unsigned int delay_ms)
{
	if (delay_ms) {
		tcpm_timer_init(dev, delay_ms);
	} else {
		tcpm_timer_uninit(dev);
		tcpm_state_machine(dev);
	}
}

static void tcpm_set_state(struct udevice *dev, enum tcpm_state state,
			   unsigned int delay_ms)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	if (delay_ms) {
		dev_dbg(dev, "TCPM: pending state change %s -> %s @ %u ms [%s]\n",
			tcpm_states[port->state], tcpm_states[state], delay_ms,
			typec_pd_rev_name[port->negotiated_rev]);
		port->delayed_state = state;
		mod_tcpm_delayed_work(dev, delay_ms);
		port->delay_ms = delay_ms;
	} else {
		dev_dbg(dev, "TCPM: state change %s -> %s\n",
			tcpm_states[port->state], tcpm_states[state]);
		port->delayed_state = INVALID_STATE;
		port->prev_state = port->state;
		port->state = state;
		/*
		 * Don't re-queue the state machine work item if we're currently
		 * in the state machine and we're immediately changing states.
		 * tcpm_state_machine_work() will continue running the state
		 * machine.
		 */
		if (!port->state_machine_running)
			mod_tcpm_delayed_work(dev, 0);
	}
}

static void tcpm_set_state_cond(struct udevice *dev, enum tcpm_state state,
				unsigned int delay_ms)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	if (port->enter_state == port->state)
		tcpm_set_state(dev, state, delay_ms);
	else
		dev_dbg(dev, "TCPM: skipped %sstate change %s -> %s [%u ms], context state %s [%s]\n",
			delay_ms ? "delayed " : "",
			tcpm_states[port->state], tcpm_states[state],
			delay_ms, tcpm_states[port->enter_state],
			typec_pd_rev_name[port->negotiated_rev]);
}

static void tcpm_queue_message(struct udevice *dev,
			       enum pd_msg_request message)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	port->queued_message = message;
	mod_tcpm_delayed_work(dev, 0);
}

enum pdo_err {
	PDO_NO_ERR,
	PDO_ERR_NO_VSAFE5V,
	PDO_ERR_VSAFE5V_NOT_FIRST,
	PDO_ERR_PDO_TYPE_NOT_IN_ORDER,
	PDO_ERR_FIXED_NOT_SORTED,
	PDO_ERR_VARIABLE_BATT_NOT_SORTED,
	PDO_ERR_DUPE_PDO,
	PDO_ERR_PPS_APDO_NOT_SORTED,
	PDO_ERR_DUPE_PPS_APDO,
};

static const char * const pdo_err_msg[] = {
	[PDO_ERR_NO_VSAFE5V] =
	" err: source/sink caps should at least have vSafe5V",
	[PDO_ERR_VSAFE5V_NOT_FIRST] =
	" err: vSafe5V Fixed Supply Object Shall always be the first object",
	[PDO_ERR_PDO_TYPE_NOT_IN_ORDER] =
	" err: PDOs should be in the following order: Fixed; Battery; Variable",
	[PDO_ERR_FIXED_NOT_SORTED] =
	" err: Fixed supply pdos should be in increasing order of their fixed voltage",
	[PDO_ERR_VARIABLE_BATT_NOT_SORTED] =
	" err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
	[PDO_ERR_DUPE_PDO] =
	" err: Variable/Batt supply pdos cannot have same min/max voltage",
	[PDO_ERR_PPS_APDO_NOT_SORTED] =
	" err: Programmable power supply apdos should be in increasing order of their maximum voltage",
	[PDO_ERR_DUPE_PPS_APDO] =
	" err: Programmable power supply apdos cannot have same min/max voltage and max current",
};

static enum pdo_err tcpm_caps_err(struct udevice *dev, const u32 *pdo,
				  unsigned int nr_pdo)
{
	unsigned int i;

	/* Should at least contain vSafe5v */
	if (nr_pdo < 1)
		return PDO_ERR_NO_VSAFE5V;

	/* The vSafe5V Fixed Supply Object Shall always be the first object */
	if (pdo_type(pdo[0]) != PDO_TYPE_FIXED ||
	    pdo_fixed_voltage(pdo[0]) != VSAFE5V)
		return PDO_ERR_VSAFE5V_NOT_FIRST;

	for (i = 1; i < nr_pdo; i++) {
		if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) {
			return PDO_ERR_PDO_TYPE_NOT_IN_ORDER;
		} else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) {
			enum pd_pdo_type type = pdo_type(pdo[i]);

			switch (type) {
			/*
			 * The remaining Fixed Supply Objects, if
			 * present, shall be sent in voltage order;
			 * lowest to highest.
			 */
			case PDO_TYPE_FIXED:
				if (pdo_fixed_voltage(pdo[i]) <=
				    pdo_fixed_voltage(pdo[i - 1]))
					return PDO_ERR_FIXED_NOT_SORTED;
				break;
			/*
			 * The Battery Supply Objects and Variable
			 * supply, if present shall be sent in Minimum
			 * Voltage order; lowest to highest.
			 */
			case PDO_TYPE_VAR:
			case PDO_TYPE_BATT:
				if (pdo_min_voltage(pdo[i]) <
				    pdo_min_voltage(pdo[i - 1]))
					return PDO_ERR_VARIABLE_BATT_NOT_SORTED;
				else if ((pdo_min_voltage(pdo[i]) ==
					  pdo_min_voltage(pdo[i - 1])) &&
					 (pdo_max_voltage(pdo[i]) ==
					  pdo_max_voltage(pdo[i - 1])))
					return PDO_ERR_DUPE_PDO;
				break;
			/*
			 * The Programmable Power Supply APDOs, if present,
			 * shall be sent in Maximum Voltage order;
			 * lowest to highest.
			 */
			case PDO_TYPE_APDO:
				if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS)
					break;

				if (pdo_pps_apdo_max_voltage(pdo[i]) <
				    pdo_pps_apdo_max_voltage(pdo[i - 1]))
					return PDO_ERR_PPS_APDO_NOT_SORTED;
				else if (pdo_pps_apdo_min_voltage(pdo[i]) ==
					  pdo_pps_apdo_min_voltage(pdo[i - 1]) &&
					 pdo_pps_apdo_max_voltage(pdo[i]) ==
					  pdo_pps_apdo_max_voltage(pdo[i - 1]) &&
					 pdo_pps_apdo_max_current(pdo[i]) ==
					  pdo_pps_apdo_max_current(pdo[i - 1]))
					return PDO_ERR_DUPE_PPS_APDO;
				break;
			default:
				dev_err(dev, "TCPM: Unknown pdo type\n");
			}
		}
	}

	return PDO_NO_ERR;
}

static int tcpm_validate_caps(struct udevice *dev, const u32 *pdo,
			      unsigned int nr_pdo)
{
	enum pdo_err err_index = tcpm_caps_err(dev, pdo, nr_pdo);

	if (err_index != PDO_NO_ERR) {
		dev_err(dev, "TCPM:%s\n", pdo_err_msg[err_index]);
		return -EINVAL;
	}

	return 0;
}

/*
 * PD (data, control) command handling functions
 */
static inline enum tcpm_state ready_state(struct tcpm_port *port)
{
	if (port->pwr_role == TYPEC_SOURCE)
		return SRC_READY;
	else
		return SNK_READY;
}

static void tcpm_pd_data_request(struct udevice *dev,
				 const struct pd_message *msg)
{
	enum pd_data_msg_type type = pd_header_type_le(msg->header);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	unsigned int cnt = pd_header_cnt_le(msg->header);
	unsigned int rev = pd_header_rev_le(msg->header);
	unsigned int i;

	switch (type) {
	case PD_DATA_SOURCE_CAP:
		for (i = 0; i < cnt; i++)
			port->source_caps[i] = le32_to_cpu(msg->payload[i]);

		port->nr_source_caps = cnt;

		tcpm_validate_caps(dev, port->source_caps,
				   port->nr_source_caps);

		/*
		 * Adjust revision in subsequent message headers, as required,
		 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
		 * support Rev 1.0 so just do nothing in that scenario.
		 */
		if (rev == PD_REV10)
			break;

		if (rev < PD_MAX_REV)
			port->negotiated_rev = rev;

		if ((pdo_type(port->source_caps[0]) == PDO_TYPE_FIXED) &&
		    (port->source_caps[0] & PDO_FIXED_DUAL_ROLE) &&
		    (port->source_caps[0] & PDO_FIXED_DATA_SWAP)) {
			/* Dual role power and data, eg: self-powered Type-C */
			port->wait_dr_swap_message = true;
		} else {
			/* Non-Dual role power, eg: adapter */
			port->wait_dr_swap_message = false;
		}

		/*
		 * This message may be received even if VBUS is not
		 * present. This is quite unexpected; see USB PD
		 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
		 * However, at the same time, we must be ready to
		 * receive this message and respond to it 15ms after
		 * receiving PS_RDY during power swap operations, no matter
		 * if VBUS is available or not (USB PD specification,
		 * section 6.5.9.2).
		 * So we need to accept the message either way,
		 * but be prepared to keep waiting for VBUS after it was
		 * handled.
		 */
		tcpm_set_state(dev, SNK_NEGOTIATE_CAPABILITIES, 0);
		break;
	case PD_DATA_REQUEST:
		/*
		 * Adjust revision in subsequent message headers, as required,
		 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
		 * support Rev 1.0 so just reject in that scenario.
		 */
		if (rev == PD_REV10) {
			tcpm_queue_message(dev, PD_MSG_CTRL_REJECT);
			break;
		}

		if (rev < PD_MAX_REV)
			port->negotiated_rev = rev;

		port->sink_request = le32_to_cpu(msg->payload[0]);

		tcpm_set_state(dev, SRC_NEGOTIATE_CAPABILITIES, 0);
		break;
	case PD_DATA_SINK_CAP:
		/* We don't do anything with this at the moment... */
		for (i = 0; i < cnt; i++)
			port->sink_caps[i] = le32_to_cpu(msg->payload[i]);

		port->nr_sink_caps = cnt;
		break;
	default:
		break;
	}
}

static void tcpm_pd_ctrl_request(struct udevice *dev,
				 const struct pd_message *msg)
{
	enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	enum tcpm_state next_state;

	switch (type) {
	case PD_CTRL_GOOD_CRC:
	case PD_CTRL_PING:
		break;
	case PD_CTRL_GET_SOURCE_CAP:
		switch (port->state) {
		case SRC_READY:
		case SNK_READY:
			tcpm_queue_message(dev, PD_MSG_DATA_SOURCE_CAP);
			break;
		default:
			tcpm_queue_message(dev, PD_MSG_CTRL_REJECT);
			break;
		}
		break;
	case PD_CTRL_GET_SINK_CAP:
		switch (port->state) {
		case SRC_READY:
		case SNK_READY:
			tcpm_queue_message(dev, PD_MSG_DATA_SINK_CAP);
			break;
		default:
			tcpm_queue_message(dev, PD_MSG_CTRL_REJECT);
			break;
		}
		break;
	case PD_CTRL_GOTO_MIN:
		break;
	case PD_CTRL_PS_RDY:
		switch (port->state) {
		case SNK_TRANSITION_SINK:
			if (port->vbus_present) {
				tcpm_set_current_limit(dev,
						       port->req_current_limit,
						       port->req_supply_voltage);
				port->explicit_contract = true;
				tcpm_set_state(dev, SNK_READY, 0);
			} else {
				/*
				 * Seen after power swap. Keep waiting for VBUS
				 * in a transitional state.
				 */
				tcpm_set_state(dev,
					       SNK_TRANSITION_SINK_VBUS, 0);
			}
			break;
		default:
			break;
		}
		break;
	case PD_CTRL_REJECT:
	case PD_CTRL_WAIT:
	case PD_CTRL_NOT_SUPP:
		switch (port->state) {
		case SNK_NEGOTIATE_CAPABILITIES:
			/* USB PD specification, Figure 8-43 */
			if (port->explicit_contract)
				next_state = SNK_READY;
			else
				next_state = SNK_WAIT_CAPABILITIES;

			tcpm_set_state(dev, next_state, 0);
			break;
		default:
			break;
		}
		break;
	case PD_CTRL_ACCEPT:
		switch (port->state) {
		case SNK_NEGOTIATE_CAPABILITIES:
			tcpm_set_state(dev, SNK_TRANSITION_SINK, 0);
			break;
		case SOFT_RESET_SEND:
			port->message_id = 0;
			port->rx_msgid = -1;
			if (port->pwr_role == TYPEC_SOURCE)
				next_state = SRC_SEND_CAPABILITIES;
			else
				next_state = SNK_WAIT_CAPABILITIES;
			tcpm_set_state(dev, next_state, 0);
			break;
		default:
			break;
		}
		break;
	case PD_CTRL_SOFT_RESET:
		tcpm_set_state(dev, SOFT_RESET, 0);
		break;
	case PD_CTRL_DR_SWAP:
		if (port->port_type != TYPEC_PORT_DRP) {
			tcpm_queue_message(dev, PD_MSG_CTRL_REJECT);
			break;
		}
		/*
		 * 6.3.9: If an alternate mode is active, a request to swap
		 * alternate modes shall trigger a port reset.
		 */
		switch (port->state) {
		case SRC_READY:
		case SNK_READY:
			tcpm_set_state(dev, DR_SWAP_ACCEPT, 0);
			break;
		default:
			tcpm_queue_message(dev, PD_MSG_CTRL_WAIT);
			break;
		}
		break;
	case PD_CTRL_PR_SWAP:
	case PD_CTRL_VCONN_SWAP:
	case PD_CTRL_GET_SOURCE_CAP_EXT:
	case PD_CTRL_GET_STATUS:
	case PD_CTRL_FR_SWAP:
	case PD_CTRL_GET_PPS_STATUS:
	case PD_CTRL_GET_COUNTRY_CODES:
		/* Currently not supported */
		dev_err(dev, "TCPM: Currently not supported type %#x\n", type);
		tcpm_queue_message(dev, PD_MSG_CTRL_NOT_SUPP);
		break;
	default:
		dev_err(dev, "TCPM: Unrecognized ctrl message type %#x\n", type);
		break;
	}
}

static void tcpm_pd_rx_handler(struct udevice *dev,
			       const struct pd_message *msg)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	unsigned int cnt = pd_header_cnt_le(msg->header);
	bool remote_is_host, local_is_host;

	dev_dbg(dev, "TCPM: PD RX, header: %#x [%d]\n",
		le16_to_cpu(msg->header), port->attached);

	if (port->attached) {
		enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
		unsigned int msgid = pd_header_msgid_le(msg->header);

		/*
		 * USB PD standard, 6.6.1.2:
		 * "... if MessageID value in a received Message is the
		 * same as the stored value, the receiver shall return a
		 * GoodCRC Message with that MessageID value and drop
		 * the Message (this is a retry of an already received
		 * Message). Note: this shall not apply to the Soft_Reset
		 * Message which always has a MessageID value of zero."
		 */
		if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
			return;
		port->rx_msgid = msgid;

		/*
		 * If both ends believe to be DFP/host, we have a data role
		 * mismatch.
		 */
		remote_is_host = !!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE);
		local_is_host = port->data_role == TYPEC_HOST;
		if (remote_is_host == local_is_host) {
			dev_err(dev, "TCPM: data role mismatch, initiating error recovery\n");
			tcpm_set_state(dev, ERROR_RECOVERY, 0);
		} else {
			if (cnt)
				tcpm_pd_data_request(dev, msg);
			else
				tcpm_pd_ctrl_request(dev, msg);
		}
	}
}

void tcpm_pd_receive(struct udevice *dev, const struct pd_message *msg)
{
	tcpm_reset_event_cnt(dev);
	tcpm_pd_rx_handler(dev, msg);
}

static int tcpm_pd_send_control(struct udevice *dev,
				enum pd_ctrl_msg_type type)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	struct pd_message msg;

	memset(&msg, 0, sizeof(msg));
	msg.header = PD_HEADER_LE(type, port->pwr_role,
				  port->data_role,
				  port->negotiated_rev,
				  port->message_id, 0);

	return tcpm_pd_transmit(dev, TCPC_TX_SOP, &msg);
}

/*
 * Send queued message without affecting state.
 * Return true if state machine should go back to sleep,
 * false otherwise.
 */
static bool tcpm_send_queued_message(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	enum pd_msg_request queued_message;
	int max_messages = 100;

	do {
		queued_message = port->queued_message;
		port->queued_message = PD_MSG_NONE;
		max_messages--;

		switch (queued_message) {
		case PD_MSG_CTRL_WAIT:
			tcpm_pd_send_control(dev, PD_CTRL_WAIT);
			break;
		case PD_MSG_CTRL_REJECT:
			tcpm_pd_send_control(dev, PD_CTRL_REJECT);
			break;
		case PD_MSG_CTRL_NOT_SUPP:
			tcpm_pd_send_control(dev, PD_CTRL_NOT_SUPP);
			break;
		case PD_MSG_DATA_SINK_CAP:
			tcpm_pd_send_sink_caps(dev);
			break;
		case PD_MSG_DATA_SOURCE_CAP:
			tcpm_pd_send_source_caps(dev);
			break;
		default:
			break;
		}
	} while (max_messages > 0 && port->queued_message != PD_MSG_NONE);

	if (!max_messages)
		dev_err(dev, "Aborted sending of too many queued messages\n");

	return false;
}

static int tcpm_pd_check_request(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	u32 pdo, rdo = port->sink_request;
	unsigned int max, op, pdo_max, index;
	enum pd_pdo_type type;

	index = rdo_index(rdo);
	if (!index || index > port->nr_src_pdo)
		return -EINVAL;

	pdo = port->src_pdo[index - 1];
	type = pdo_type(pdo);
	switch (type) {
	case PDO_TYPE_FIXED:
	case PDO_TYPE_VAR:
		max = rdo_max_current(rdo);
		op = rdo_op_current(rdo);
		pdo_max = pdo_max_current(pdo);

		if (op > pdo_max)
			return -EINVAL;
		if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
			return -EINVAL;

		if (type == PDO_TYPE_FIXED)
			dev_dbg(dev, "TCPM: Requested %u mV, %u mA for %u / %u mA\n",
				pdo_fixed_voltage(pdo), pdo_max, op, max);
		else
			dev_dbg(dev, "TCPM: Requested %u -> %u mV, %u mA for %u / %u mA\n",
				pdo_min_voltage(pdo), pdo_max_voltage(pdo),
				pdo_max, op, max);
		break;
	case PDO_TYPE_BATT:
		max = rdo_max_power(rdo);
		op = rdo_op_power(rdo);
		pdo_max = pdo_max_power(pdo);

		if (op > pdo_max)
			return -EINVAL;
		if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
			return -EINVAL;
		dev_info(dev, "TCPM: Requested %u -> %u mV, %u mW for %u / %u mW\n",
			 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
			 pdo_max, op, max);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

#define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
#define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))

static int tcpm_pd_select_pdo(struct udevice *dev, int *sink_pdo,
			      int *src_pdo)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
		     max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
		     min_snk_mv = 0;
	int ret = -EINVAL;

	/*
	 * Select the source PDO providing the most power which has a
	 * matchig sink cap.
	 */
	for (i = 0; i < port->nr_source_caps; i++) {
		u32 pdo = port->source_caps[i];
		enum pd_pdo_type type = pdo_type(pdo);

		switch (type) {
		case PDO_TYPE_FIXED:
			max_src_mv = pdo_fixed_voltage(pdo);
			min_src_mv = max_src_mv;
			break;
		case PDO_TYPE_BATT:
		case PDO_TYPE_VAR:
			max_src_mv = pdo_max_voltage(pdo);
			min_src_mv = pdo_min_voltage(pdo);
			break;
		case PDO_TYPE_APDO:
			continue;
		default:
			dev_err(dev, "TCPM: Invalid source PDO type, ignoring\n");
			continue;
		}

		switch (type) {
		case PDO_TYPE_FIXED:
		case PDO_TYPE_VAR:
			src_ma = pdo_max_current(pdo);
			src_mw = src_ma * min_src_mv / 1000;
			break;
		case PDO_TYPE_BATT:
			src_mw = pdo_max_power(pdo);
			break;
		case PDO_TYPE_APDO:
			continue;
		default:
			dev_err(dev, "TCPM: Invalid source PDO type, ignoring\n");
			continue;
		}

		for (j = 0; j < port->nr_snk_pdo; j++) {
			pdo = port->snk_pdo[j];

			switch (pdo_type(pdo)) {
			case PDO_TYPE_FIXED:
				max_snk_mv = pdo_fixed_voltage(pdo);
				min_snk_mv = max_snk_mv;
				break;
			case PDO_TYPE_BATT:
			case PDO_TYPE_VAR:
				max_snk_mv = pdo_max_voltage(pdo);
				min_snk_mv = pdo_min_voltage(pdo);
				break;
			case PDO_TYPE_APDO:
				continue;
			default:
				dev_err(dev, "TCPM: Invalid sink PDO type, ignoring\n");
				continue;
			}

			if (max_src_mv <= max_snk_mv && min_src_mv >= min_snk_mv) {
				/* Prefer higher voltages if available */
				if ((src_mw == max_mw && min_src_mv > max_mv) ||
				    src_mw > max_mw) {
					*src_pdo = i;
					*sink_pdo = j;
					max_mw = src_mw;
					max_mv = min_src_mv;
					ret = 0;
				}
			}
		}
	}

	return ret;
}

static int tcpm_pd_build_request(struct udevice *dev, u32 *rdo)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	unsigned int mv, ma, mw, flags;
	unsigned int max_ma, max_mw;
	enum pd_pdo_type type;
	u32 pdo, matching_snk_pdo;
	int src_pdo_index = 0;
	int snk_pdo_index = 0;
	int ret;

	ret = tcpm_pd_select_pdo(dev, &snk_pdo_index, &src_pdo_index);
	if (ret < 0)
		return ret;

	pdo = port->source_caps[src_pdo_index];
	matching_snk_pdo = port->snk_pdo[snk_pdo_index];
	type = pdo_type(pdo);

	switch (type) {
	case PDO_TYPE_FIXED:
		mv = pdo_fixed_voltage(pdo);
		break;
	case PDO_TYPE_BATT:
	case PDO_TYPE_VAR:
		mv = pdo_min_voltage(pdo);
		break;
	default:
		dev_err(dev, "TCPM: Invalid PDO selected!\n");
		return -EINVAL;
	}

	/* Select maximum available current within the sink pdo's limit */
	if (type == PDO_TYPE_BATT) {
		mw = min_power(pdo, matching_snk_pdo);
		ma = 1000 * mw / mv;
	} else {
		ma = min_current(pdo, matching_snk_pdo);
		mw = ma * mv / 1000;
	}

	flags = RDO_USB_COMM | RDO_NO_SUSPEND;

	/* Set mismatch bit if offered power is less than operating power */
	max_ma = ma;
	max_mw = mw;
	if (mw < port->operating_snk_mw) {
		flags |= RDO_CAP_MISMATCH;
		if (type == PDO_TYPE_BATT &&
		    (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo)))
			max_mw = pdo_max_power(matching_snk_pdo);
		else if (pdo_max_current(matching_snk_pdo) >
			 pdo_max_current(pdo))
			max_ma = pdo_max_current(matching_snk_pdo);
	}

	dev_dbg(dev, "TCPM: cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d\n",
		port->cc_req, port->cc1, port->cc2, port->vbus_source,
		port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
		port->polarity);

	if (type == PDO_TYPE_BATT) {
		*rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags);

		dev_info(dev, "TCPM: requesting PDO %d: %u mV, %u mW%s\n",
			 src_pdo_index, mv, mw,
			 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
	} else {
		*rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags);

		dev_info(dev, "TCPM: requesting PDO %d: %u mV, %u mA%s\n",
			 src_pdo_index, mv, ma,
			 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
	}

	port->req_current_limit = ma;
	port->req_supply_voltage = mv;

	return 0;
}

static int tcpm_pd_send_request(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	struct pd_message msg;
	int ret;
	u32 rdo;

	ret = tcpm_pd_build_request(dev, &rdo);
	if (ret < 0)
		return ret;

	memset(&msg, 0, sizeof(msg));
	msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
				  port->pwr_role,
				  port->data_role,
				  port->negotiated_rev,
				  port->message_id, 1);
	msg.payload[0] = cpu_to_le32(rdo);

	return tcpm_pd_transmit(dev, TCPC_TX_SOP, &msg);
}

static int tcpm_set_vbus(struct udevice *dev, bool enable)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	int ret;

	if (enable && port->vbus_charge)
		return -EINVAL;

	dev_dbg(dev, "TCPM: set vbus = %d charge = %d\n",
		enable, port->vbus_charge);

	ret = drvops->set_vbus(dev, enable, port->vbus_charge);
	if (ret < 0)
		return ret;

	port->vbus_source = enable;
	return 0;
}

static int tcpm_set_charge(struct udevice *dev, bool charge)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	int ret;

	if (charge && port->vbus_source)
		return -EINVAL;

	if (charge != port->vbus_charge) {
		dev_dbg(dev, "TCPM: set vbus = %d charge = %d\n",
			port->vbus_source, charge);
		ret = drvops->set_vbus(dev, port->vbus_source,
					   charge);
		if (ret < 0)
			return ret;
	}
	port->vbus_charge = charge;
	return 0;
}

static bool tcpm_start_toggling(struct udevice *dev, enum typec_cc_status cc)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	int ret;

	if (!drvops->start_toggling)
		return false;

	dev_dbg(dev, "TCPM: Start toggling\n");
	ret = drvops->start_toggling(dev, port->port_type, cc);
	return ret == 0;
}

static int tcpm_init_vbus(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	int ret;

	ret = drvops->set_vbus(dev, false, false);
	port->vbus_source = false;
	port->vbus_charge = false;
	return ret;
}

static int tcpm_init_vconn(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	int ret;

	ret = drvops->set_vconn(dev, false);
	port->vconn_role = TYPEC_SINK;
	return ret;
}

static inline void tcpm_typec_connect(struct tcpm_port *port)
{
	if (!port->connected)
		port->connected = true;
}

static int tcpm_src_attach(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	enum typec_cc_polarity polarity =
				port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
							 : TYPEC_POLARITY_CC1;
	int ret;

	if (port->attached)
		return 0;

	ret = tcpm_set_polarity(dev, polarity);
	if (ret < 0)
		return ret;

	ret = tcpm_set_roles(dev, true, TYPEC_SOURCE, TYPEC_HOST);
	if (ret < 0)
		return ret;

	ret = drvops->set_pd_rx(dev, true);
	if (ret < 0)
		goto out_disable_mux;

	/*
	 * USB Type-C specification, version 1.2,
	 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
	 * Enable VCONN only if the non-RD port is set to RA.
	 */
	if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
	    (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
		ret = tcpm_set_vconn(dev, true);
		if (ret < 0)
			goto out_disable_pd;
	}

	ret = tcpm_set_vbus(dev, true);
	if (ret < 0)
		goto out_disable_vconn;

	port->pd_capable = false;

	port->partner = NULL;

	port->attached = true;

	return 0;

out_disable_vconn:
	tcpm_set_vconn(dev, false);
out_disable_pd:
	drvops->set_pd_rx(dev, false);
out_disable_mux:
	dev_err(dev, "TCPM: CC connected in %s as DFP\n",
		polarity ? "CC2" : "CC1");
	return 0;
}

static inline void tcpm_typec_disconnect(struct tcpm_port *port)
{
	if (port->connected) {
		port->partner = NULL;
		port->connected = false;
	}
}

static void tcpm_reset_port(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	tcpm_timer_uninit(dev);
	tcpm_typec_disconnect(port);
	tcpm_reset_event_cnt(dev);
	port->wait_dr_swap_message = false;
	port->attached = false;
	port->pd_capable = false;

	/*
	 * First Rx ID should be 0; set this to a sentinel of -1 so that
	 * we can check tcpm_pd_rx_handler() if we had seen it before.
	 */
	port->rx_msgid = -1;

	drvops->set_pd_rx(dev, false);
	tcpm_init_vbus(dev);	/* also disables charging */
	tcpm_init_vconn(dev);
	tcpm_set_current_limit(dev, 0, 0);
	tcpm_set_polarity(dev, TYPEC_POLARITY_CC1);
	tcpm_set_attached_state(dev, false);
	port->nr_sink_caps = 0;
}

static void tcpm_detach(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	if (tcpm_port_is_disconnected(port))
		port->hard_reset_count = 0;

	if (!port->attached)
		return;

	tcpm_reset_port(dev);
}

static void tcpm_src_detach(struct udevice *dev)
{
	tcpm_detach(dev);
}

static int tcpm_snk_attach(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	int ret;

	if (port->attached)
		return 0;

	ret = tcpm_set_polarity(dev, port->cc2 != TYPEC_CC_OPEN ?
				TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
	if (ret < 0)
		return ret;

	ret = tcpm_set_roles(dev, true, TYPEC_SINK, TYPEC_DEVICE);
	if (ret < 0)
		return ret;

	port->pd_capable = false;

	port->partner = NULL;

	port->attached = true;
	dev_info(dev, "TCPM: CC connected in %s as UFP\n",
		 port->cc1 != TYPEC_CC_OPEN ? "CC1" : "CC2");

	return 0;
}

static void tcpm_snk_detach(struct udevice *dev)
{
	tcpm_detach(dev);
}

static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
{
	if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
		return HARD_RESET_SEND;
	if (port->pd_capable)
		return ERROR_RECOVERY;
	if (port->pwr_role == TYPEC_SOURCE)
		return SRC_UNATTACHED;
	if (port->state == SNK_WAIT_CAPABILITIES)
		return SNK_READY;
	return SNK_UNATTACHED;
}

static inline enum tcpm_state unattached_state(struct tcpm_port *port)
{
	if (port->port_type == TYPEC_PORT_DRP) {
		if (port->pwr_role == TYPEC_SOURCE)
			return SRC_UNATTACHED;
		else
			return SNK_UNATTACHED;
	} else if (port->port_type == TYPEC_PORT_SRC) {
		return SRC_UNATTACHED;
	}

	return SNK_UNATTACHED;
}

static void run_state_machine(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	int ret;

	port->enter_state = port->state;
	switch (port->state) {
	case TOGGLING:
		break;
	/* SRC states */
	case SRC_UNATTACHED:
		tcpm_src_detach(dev);
		if (tcpm_start_toggling(dev, tcpm_rp_cc(port))) {
			tcpm_set_state(dev, TOGGLING, 0);
			break;
		}
		tcpm_set_cc(dev, tcpm_rp_cc(port));
		if (port->port_type == TYPEC_PORT_DRP)
			tcpm_set_state(dev, SNK_UNATTACHED, PD_T_DRP_SNK);
		break;
	case SRC_ATTACH_WAIT:
		if (tcpm_port_is_source(port))
			tcpm_set_state(dev, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
		break;

	case SRC_ATTACHED:
		ret = tcpm_src_attach(dev);
		/*
		 * Currently, vbus control is not implemented,
		 * and the SRC detection process cannot be fully implemented.
		 */
		tcpm_set_state(dev, SRC_READY, 0);
		break;
	case SRC_STARTUP:
		port->caps_count = 0;
		port->negotiated_rev = PD_MAX_REV;
		port->message_id = 0;
		port->rx_msgid = -1;
		port->explicit_contract = false;
		tcpm_set_state(dev, SRC_SEND_CAPABILITIES, 0);
		break;
	case SRC_SEND_CAPABILITIES:
		port->caps_count++;
		if (port->caps_count > PD_N_CAPS_COUNT) {
			tcpm_set_state(dev, SRC_READY, 0);
			break;
		}
		ret = tcpm_pd_send_source_caps(dev);
		if (ret < 0) {
			tcpm_set_state(dev, SRC_SEND_CAPABILITIES,
				       PD_T_SEND_SOURCE_CAP);
		} else {
			/*
			 * Per standard, we should clear the reset counter here.
			 * However, that can result in state machine hang-ups.
			 * Reset it only in READY state to improve stability.
			 */
			/* port->hard_reset_count = 0; */
			port->caps_count = 0;
			port->pd_capable = true;
			tcpm_set_state_cond(dev, SRC_SEND_CAPABILITIES_TIMEOUT,
					    PD_T_SEND_SOURCE_CAP);
		}
		break;
	case SRC_SEND_CAPABILITIES_TIMEOUT:
		/*
		 * Error recovery for a PD_DATA_SOURCE_CAP reply timeout.
		 *
		 * PD 2.0 sinks are supposed to accept src-capabilities with a
		 * 3.0 header and simply ignore any src PDOs which the sink does
		 * not understand such as PPS but some 2.0 sinks instead ignore
		 * the entire PD_DATA_SOURCE_CAP message, causing contract
		 * negotiation to fail.
		 *
		 * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try
		 * sending src-capabilities with a lower PD revision to
		 * make these broken sinks work.
		 */
		if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) {
			tcpm_set_state(dev, HARD_RESET_SEND, 0);
		} else if (port->negotiated_rev > PD_REV20) {
			port->negotiated_rev--;
			port->hard_reset_count = 0;
			tcpm_set_state(dev, SRC_SEND_CAPABILITIES, 0);
		} else {
			tcpm_set_state(dev, hard_reset_state(port), 0);
		}
		break;
	case SRC_NEGOTIATE_CAPABILITIES:
		ret = tcpm_pd_check_request(dev);
		if (ret < 0) {
			tcpm_pd_send_control(dev, PD_CTRL_REJECT);
			if (!port->explicit_contract) {
				tcpm_set_state(dev,
					       SRC_WAIT_NEW_CAPABILITIES, 0);
			} else {
				tcpm_set_state(dev, SRC_READY, 0);
			}
		} else {
			tcpm_pd_send_control(dev, PD_CTRL_ACCEPT);
			tcpm_set_state(dev, SRC_TRANSITION_SUPPLY,
				       PD_T_SRC_TRANSITION);
		}
		break;
	case SRC_TRANSITION_SUPPLY:
		/* XXX: regulator_set_voltage(vbus, ...) */
		tcpm_pd_send_control(dev, PD_CTRL_PS_RDY);
		port->explicit_contract = true;
		tcpm_set_state_cond(dev, SRC_READY, 0);
		break;
	case SRC_READY:
		port->hard_reset_count = 0;

		tcpm_typec_connect(port);
		break;
	case SRC_WAIT_NEW_CAPABILITIES:
		/* Nothing to do... */
		break;

	/* SNK states */
	case SNK_UNATTACHED:
		tcpm_snk_detach(dev);
		if (tcpm_start_toggling(dev, TYPEC_CC_RD)) {
			tcpm_set_state(dev, TOGGLING, 0);
			break;
		}
		tcpm_set_cc(dev, TYPEC_CC_RD);
		if (port->port_type == TYPEC_PORT_DRP)
			tcpm_set_state(dev, SRC_UNATTACHED, PD_T_DRP_SRC);
		break;
	case SNK_ATTACH_WAIT:
		if ((port->cc1 == TYPEC_CC_OPEN &&
		     port->cc2 != TYPEC_CC_OPEN) ||
		    (port->cc1 != TYPEC_CC_OPEN &&
		     port->cc2 == TYPEC_CC_OPEN))
			tcpm_set_state(dev, SNK_DEBOUNCED,
				       PD_T_CC_DEBOUNCE);
		else if (tcpm_port_is_disconnected(port))
			tcpm_set_state(dev, SNK_UNATTACHED,
				       PD_T_CC_DEBOUNCE);
		break;
	case SNK_DEBOUNCED:
		if (tcpm_port_is_disconnected(port))
			tcpm_set_state(dev, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
		else if (port->vbus_present)
			tcpm_set_state(dev, SNK_ATTACHED, 0);
		else
			/* Wait for VBUS, but not forever */
			tcpm_set_state(dev, PORT_RESET, PD_T_PS_SOURCE_ON);
		break;
	case SNK_ATTACHED:
		ret = tcpm_snk_attach(dev);
		if (ret < 0)
			tcpm_set_state(dev, SNK_UNATTACHED, 0);
		else
			tcpm_set_state(dev, SNK_STARTUP, 0);
		break;
	case SNK_STARTUP:
		port->negotiated_rev = PD_MAX_REV;
		port->message_id = 0;
		port->rx_msgid = -1;
		port->explicit_contract = false;
		tcpm_set_state(dev, SNK_DISCOVERY, 0);
		break;
	case SNK_DISCOVERY:
		if (port->vbus_present) {
			tcpm_set_current_limit(dev,
					       tcpm_get_current_limit(port),
					       5000);
			tcpm_set_charge(dev, true);
			tcpm_set_state(dev, SNK_WAIT_CAPABILITIES, 0);
			break;
		}
		/*
		 * For DRP, timeouts differ. Also, handling is supposed to be
		 * different and much more complex (dead battery detection;
		 * see USB power delivery specification, section 8.3.3.6.1.5.1).
		 */
		tcpm_set_state(dev, hard_reset_state(port),
			       port->port_type == TYPEC_PORT_DRP ?
					PD_T_DB_DETECT : PD_T_NO_RESPONSE);
		break;
	case SNK_DISCOVERY_DEBOUNCE:
		tcpm_set_state(dev, SNK_DISCOVERY_DEBOUNCE_DONE,
			       PD_T_CC_DEBOUNCE);
		break;
	case SNK_DISCOVERY_DEBOUNCE_DONE:
		tcpm_set_state(dev, unattached_state(port), 0);
		break;
	case SNK_WAIT_CAPABILITIES:
		ret = drvops->set_pd_rx(dev, true);
		if (ret < 0) {
			tcpm_set_state(dev, SNK_READY, 0);
			break;
		}
		/*
		 * If VBUS has never been low, and we time out waiting
		 * for source cap, try a soft reset first, in case we
		 * were already in a stable contract before this boot.
		 * Do this only once.
		 */
		if (port->vbus_never_low) {
			port->vbus_never_low = false;
			tcpm_set_state(dev, SOFT_RESET_SEND,
				       PD_T_SINK_WAIT_CAP);
		} else {
			tcpm_set_state(dev, hard_reset_state(port),
				       PD_T_SINK_WAIT_CAP);
		}
		break;
	case SNK_NEGOTIATE_CAPABILITIES:
		port->pd_capable = true;
		port->hard_reset_count = 0;
		ret = tcpm_pd_send_request(dev);
		if (ret < 0) {
			/* Let the Source send capabilities again. */
			tcpm_set_state(dev, SNK_WAIT_CAPABILITIES, 0);
		} else {
			tcpm_set_state_cond(dev, hard_reset_state(port),
					    PD_T_SENDER_RESPONSE);
		}
		break;
	case SNK_TRANSITION_SINK:
	case SNK_TRANSITION_SINK_VBUS:
		tcpm_set_state(dev, hard_reset_state(port),
			       PD_T_PS_TRANSITION);
		break;
	case SNK_READY:
		port->update_sink_caps = false;
		tcpm_typec_connect(port);
		/*
		 * Here poll_event_cnt is cleared, waiting for self-powered Type-C devices
		 * to send DR_swap Messge until 1s (TCPM_POLL_EVENT_TIME_OUT * 500us)timeout
		 */
		if (port->wait_dr_swap_message)
			tcpm_reset_event_cnt(dev);

		break;

	/* Hard_Reset states */
	case HARD_RESET_SEND:
		tcpm_pd_transmit(dev, TCPC_TX_HARD_RESET, NULL);
		tcpm_set_state(dev, HARD_RESET_START, 0);
		port->wait_dr_swap_message = false;
		break;
	case HARD_RESET_START:
		port->hard_reset_count++;
		drvops->set_pd_rx(dev, false);
		port->nr_sink_caps = 0;
		if (port->pwr_role == TYPEC_SOURCE)
			tcpm_set_state(dev, SRC_HARD_RESET_VBUS_OFF,
				       PD_T_PS_HARD_RESET);
		else
			tcpm_set_state(dev, SNK_HARD_RESET_SINK_OFF, 0);
		break;
	case SRC_HARD_RESET_VBUS_OFF:
		tcpm_set_vconn(dev, true);
		tcpm_set_vbus(dev, false);
		tcpm_set_roles(dev, port->self_powered, TYPEC_SOURCE,
			       TYPEC_HOST);
		tcpm_set_state(dev, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
		break;
	case SRC_HARD_RESET_VBUS_ON:
		tcpm_set_vconn(dev, true);
		tcpm_set_vbus(dev, true);
		drvops->set_pd_rx(dev, true);
		tcpm_set_attached_state(dev, true);
		tcpm_set_state(dev, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
		break;
	case SNK_HARD_RESET_SINK_OFF:
		tcpm_set_vconn(dev, false);
		if (port->pd_capable)
			tcpm_set_charge(dev, false);
		tcpm_set_roles(dev, port->self_powered, TYPEC_SINK,
			       TYPEC_DEVICE);
		/*
		 * VBUS may or may not toggle, depending on the adapter.
		 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
		 * directly after timeout.
		 */
		tcpm_set_state(dev, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
		break;
	case SNK_HARD_RESET_WAIT_VBUS:
		/* Assume we're disconnected if VBUS doesn't come back. */
		tcpm_set_state(dev, SNK_UNATTACHED,
			       PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
		break;
	case SNK_HARD_RESET_SINK_ON:
		/* Note: There is no guarantee that VBUS is on in this state */
		/*
		 * XXX:
		 * The specification suggests that dual mode ports in sink
		 * mode should transition to state PE_SRC_Transition_to_default.
		 * See USB power delivery specification chapter 8.3.3.6.1.3.
		 * This would mean to
		 * - turn off VCONN, reset power supply
		 * - request hardware reset
		 * - turn on VCONN
		 * - Transition to state PE_Src_Startup
		 * SNK only ports shall transition to state Snk_Startup
		 * (see chapter 8.3.3.3.8).
		 * Similar, dual-mode ports in source mode should transition
		 * to PE_SNK_Transition_to_default.
		 */
		if (port->pd_capable) {
			tcpm_set_current_limit(dev,
					       tcpm_get_current_limit(port),
					       5000);
			tcpm_set_charge(dev, true);
		}
		tcpm_set_attached_state(dev, true);
		tcpm_set_state(dev, SNK_STARTUP, 0);
		break;

	/* Soft_Reset states */
	case SOFT_RESET:
		port->message_id = 0;
		port->rx_msgid = -1;
		tcpm_pd_send_control(dev, PD_CTRL_ACCEPT);
		if (port->pwr_role == TYPEC_SOURCE)
			tcpm_set_state(dev, SRC_SEND_CAPABILITIES, 0);
		else
			tcpm_set_state(dev, SNK_WAIT_CAPABILITIES, 0);
		break;
	case SOFT_RESET_SEND:
		port->message_id = 0;
		port->rx_msgid = -1;
		if (tcpm_pd_send_control(dev, PD_CTRL_SOFT_RESET))
			tcpm_set_state_cond(dev, hard_reset_state(port), 0);
		else
			tcpm_set_state_cond(dev, hard_reset_state(port),
					    PD_T_SENDER_RESPONSE);
		break;

	/* DR_Swap states */
	case DR_SWAP_ACCEPT:
		tcpm_pd_send_control(dev, PD_CTRL_ACCEPT);
		tcpm_set_state_cond(dev, DR_SWAP_CHANGE_DR, 0);
		break;
	case DR_SWAP_CHANGE_DR:
		if (port->data_role == TYPEC_HOST) {
			tcpm_set_roles(dev, true, port->pwr_role,
				       TYPEC_DEVICE);
		} else {
			tcpm_set_roles(dev, true, port->pwr_role,
				       TYPEC_HOST);
		}
		/* DR_swap process complete, wait_dr_swap_message is cleared */
		port->wait_dr_swap_message = false;
		tcpm_set_state(dev, ready_state(port), 0);
		break;
	case ERROR_RECOVERY:
		tcpm_set_state(dev, PORT_RESET, 0);
		break;
	case PORT_RESET:
		tcpm_reset_port(dev);
		if (port->self_powered)
			tcpm_set_cc(dev, TYPEC_CC_OPEN);
		else
			tcpm_set_cc(dev, tcpm_default_state(port) == SNK_UNATTACHED ?
				    TYPEC_CC_RD : tcpm_rp_cc(port));
		tcpm_set_state(dev, PORT_RESET_WAIT_OFF,
			       PD_T_ERROR_RECOVERY);
		break;
	case PORT_RESET_WAIT_OFF:
		tcpm_set_state(dev,
			       tcpm_default_state(port),
			       port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
		break;
	default:
		dev_err(dev, "TCPM: Unexpected port state %d\n", port->state);
		break;
	}
}

static void tcpm_state_machine(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	enum tcpm_state prev_state;

	mutex_lock(&port->lock);
	port->state_machine_running = true;

	if (port->queued_message && tcpm_send_queued_message(dev))
		goto done;

	/* If we were queued due to a delayed state change, update it now */
	if (port->delayed_state) {
		dev_dbg(dev, "TCPM: state change %s -> %s [delayed %ld ms]\n",
			tcpm_states[port->state],
			tcpm_states[port->delayed_state], port->delay_ms);
		port->prev_state = port->state;
		port->state = port->delayed_state;
		port->delayed_state = INVALID_STATE;
	}

	/*
	 * Continue running as long as we have (non-delayed) state changes
	 * to make.
	 */
	do {
		prev_state = port->state;
		run_state_machine(dev);
		if (port->queued_message)
			tcpm_send_queued_message(dev);
	} while (port->state != prev_state && !port->delayed_state);

done:
	port->state_machine_running = false;
	mutex_unlock(&port->lock);
}

static void _tcpm_cc_change(struct udevice *dev, enum typec_cc_status cc1,
			    enum typec_cc_status cc2)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	enum typec_cc_status old_cc1, old_cc2;
	enum tcpm_state new_state;

	old_cc1 = port->cc1;
	old_cc2 = port->cc2;
	port->cc1 = cc1;
	port->cc2 = cc2;

	dev_dbg(dev, "TCPM: CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]\n",
		old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
		port->polarity,
		tcpm_port_is_disconnected(port) ? "disconnected" : "connected");

	switch (port->state) {
	case TOGGLING:
		if (tcpm_port_is_source(port))
			tcpm_set_state(dev, SRC_ATTACH_WAIT, 0);
		else if (tcpm_port_is_sink(port))
			tcpm_set_state(dev, SNK_ATTACH_WAIT, 0);
		break;
	case SRC_UNATTACHED:
	case SRC_ATTACH_WAIT:
		if (tcpm_port_is_disconnected(port))
			tcpm_set_state(dev, SRC_UNATTACHED, 0);
		else if (cc1 != old_cc1 || cc2 != old_cc2)
			tcpm_set_state(dev, SRC_ATTACH_WAIT, 0);
		break;
	case SRC_ATTACHED:
	case SRC_SEND_CAPABILITIES:
	case SRC_READY:
		if (tcpm_port_is_disconnected(port) ||
		    !tcpm_port_is_source(port))
			tcpm_set_state(dev, SRC_UNATTACHED, 0);
		break;
	case SNK_UNATTACHED:
		if (tcpm_port_is_sink(port))
			tcpm_set_state(dev, SNK_ATTACH_WAIT, 0);
		break;
	case SNK_ATTACH_WAIT:
		if ((port->cc1 == TYPEC_CC_OPEN &&
		     port->cc2 != TYPEC_CC_OPEN) ||
		    (port->cc1 != TYPEC_CC_OPEN &&
		     port->cc2 == TYPEC_CC_OPEN))
			new_state = SNK_DEBOUNCED;
		else if (tcpm_port_is_disconnected(port))
			new_state = SNK_UNATTACHED;
		else
			break;
		if (new_state != port->delayed_state)
			tcpm_set_state(dev, SNK_ATTACH_WAIT, 0);
		break;
	case SNK_DEBOUNCED:
		if (tcpm_port_is_disconnected(port))
			new_state = SNK_UNATTACHED;
		else if (port->vbus_present)
			new_state = tcpm_try_src(port) ? INVALID_STATE : SNK_ATTACHED;
		else
			new_state = SNK_UNATTACHED;
		if (new_state != port->delayed_state)
			tcpm_set_state(dev, SNK_DEBOUNCED, 0);
		break;
	case SNK_READY:
		if (tcpm_port_is_disconnected(port))
			tcpm_set_state(dev, unattached_state(port), 0);
		else if (!port->pd_capable &&
			 (cc1 != old_cc1 || cc2 != old_cc2))
			tcpm_set_current_limit(dev,
					       tcpm_get_current_limit(port),
					       5000);
		break;

	case SNK_DISCOVERY:
		/* CC line is unstable, wait for debounce */
		if (tcpm_port_is_disconnected(port))
			tcpm_set_state(dev, SNK_DISCOVERY_DEBOUNCE, 0);
		break;
	case SNK_DISCOVERY_DEBOUNCE:
		break;

	case PORT_RESET:
	case PORT_RESET_WAIT_OFF:
		/*
		 * State set back to default mode once the timer completes.
		 * Ignore CC changes here.
		 */
		break;
	default:
		/*
		 * While acting as sink and auto vbus discharge is enabled, Allow disconnect
		 * to be driven by vbus disconnect.
		 */
		if (tcpm_port_is_disconnected(port))
			tcpm_set_state(dev, unattached_state(port), 0);
		break;
	}
}

static void _tcpm_pd_vbus_on(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	dev_dbg(dev, "TCPM: VBUS on event\n");
	port->vbus_present = true;
	/*
	 * When vbus_present is true i.e. Voltage at VBUS is greater than VSAFE5V implicitly
	 * states that vbus is not at VSAFE0V, hence clear the vbus_vsafe0v flag here.
	 */
	port->vbus_vsafe0v = false;

	switch (port->state) {
	case SNK_TRANSITION_SINK_VBUS:
		port->explicit_contract = true;
		tcpm_set_state(dev, SNK_READY, 0);
		break;
	case SNK_DISCOVERY:
		tcpm_set_state(dev, SNK_DISCOVERY, 0);
		break;
	case SNK_DEBOUNCED:
		tcpm_set_state(dev, SNK_ATTACHED, 0);
		break;
	case SNK_HARD_RESET_WAIT_VBUS:
		tcpm_set_state(dev, SNK_HARD_RESET_SINK_ON, 0);
		break;
	case SRC_ATTACHED:
		tcpm_set_state(dev, SRC_STARTUP, 0);
		break;
	case SRC_HARD_RESET_VBUS_ON:
		tcpm_set_state(dev, SRC_STARTUP, 0);
		break;

	case PORT_RESET:
	case PORT_RESET_WAIT_OFF:
		/*
		 * State set back to default mode once the timer completes.
		 * Ignore vbus changes here.
		 */
		break;

	default:
		break;
	}
}

static void _tcpm_pd_vbus_off(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	dev_dbg(dev, "TCPM: VBUS off event\n");
	port->vbus_present = false;
	port->vbus_never_low = false;
	switch (port->state) {
	case SNK_HARD_RESET_SINK_OFF:
		tcpm_set_state(dev, SNK_HARD_RESET_WAIT_VBUS, 0);
		break;
	case HARD_RESET_SEND:
		break;
	case SNK_ATTACH_WAIT:
		tcpm_set_state(dev, SNK_UNATTACHED, 0);
		break;

	case SNK_NEGOTIATE_CAPABILITIES:
		break;

	case PORT_RESET_WAIT_OFF:
		tcpm_set_state(dev, tcpm_default_state(port), 0);
		break;

	case PORT_RESET:
		/*
		 * State set back to default mode once the timer completes.
		 * Ignore vbus changes here.
		 */
		break;

	default:
		if (port->pwr_role == TYPEC_SINK && port->attached)
			tcpm_set_state(dev, SNK_UNATTACHED, 0);
		break;
	}
}

void tcpm_cc_change(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	enum typec_cc_status cc1, cc2;

	tcpm_reset_event_cnt(dev);
	if (drvops->get_cc(dev, &cc1, &cc2) == 0)
		_tcpm_cc_change(dev, cc1, cc2);
}

void tcpm_vbus_change(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	bool vbus;

	tcpm_reset_event_cnt(dev);
	vbus = drvops->get_vbus(dev);
	if (vbus)
		_tcpm_pd_vbus_on(dev);
	else
		_tcpm_pd_vbus_off(dev);
}

void tcpm_pd_hard_reset(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	tcpm_reset_event_cnt(dev);
	dev_dbg(dev, "TCPM: Received hard reset\n");

	/* If a hard reset message is received during the port reset process,
	 * we should ignore it, that is, do not set port->state to HARD_RESET_START.
	 */
	if (port->state == PORT_RESET || port->state == PORT_RESET_WAIT_OFF)
		return;

	/*
	 * If we keep receiving hard reset requests, executing the hard reset
	 * must have failed. Revert to error recovery if that happens.
	 */
	tcpm_set_state(dev,
		       port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
				HARD_RESET_START : ERROR_RECOVERY,
		       0);
}

static void tcpm_init(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	enum typec_cc_status cc1, cc2;

	drvops->init(dev);

	tcpm_reset_port(dev);

	/*
	 * XXX
	 * Should possibly wait for VBUS to settle if it was enabled locally
	 * since tcpm_reset_port() will disable VBUS.
	 */
	port->vbus_present = drvops->get_vbus(dev);
	if (port->vbus_present)
		port->vbus_never_low = true;

	/*
	 * 1. When vbus_present is true, voltage on VBUS is already at VSAFE5V.
	 * So implicitly vbus_vsafe0v = false.
	 *
	 * 2. When vbus_present is false and TCPC does NOT support querying
	 * vsafe0v status, then, it's best to assume vbus is at VSAFE0V i.e.
	 * vbus_vsafe0v is true.
	 *
	 * 3. When vbus_present is false and TCPC does support querying vsafe0v,
	 * then, query tcpc for vsafe0v status.
	 */
	if (port->vbus_present)
		port->vbus_vsafe0v = false;
	else
		port->vbus_vsafe0v = true;

	tcpm_set_state(dev, tcpm_default_state(port), 0);

	if (drvops->get_cc(dev, &cc1, &cc2) == 0)
		_tcpm_cc_change(dev, cc1, cc2);
}

static int tcpm_fw_get_caps(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	ofnode node;
	const char *cap_str;
	int ret;
	u32 mw;

	ret = drvops->get_connector_node(dev, &node);
	if (ret)
		return ret;

	cap_str = ofnode_read_string(node, "power-role");
	if (!cap_str)
		return -EINVAL;

	if (!strcmp("dual", cap_str))
		port->typec_type = TYPEC_PORT_DRP;
	else if (!strcmp("source", cap_str))
		port->typec_type = TYPEC_PORT_SRC;
	else if (!strcmp("sink", cap_str))
		port->typec_type = TYPEC_PORT_SNK;
	else
		return -EINVAL;

	port->port_type = port->typec_type;

	if (port->port_type == TYPEC_PORT_SNK)
		goto sink;

	/* Get source pdos */
	ret = ofnode_read_size(node, "source-pdos") / sizeof(u32);
	if (ret <= 0)
		return -EINVAL;

	port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
	ret = ofnode_read_u32_array(node, "source-pdos",
				    port->src_pdo, port->nr_src_pdo);
	if (ret || tcpm_validate_caps(dev, port->src_pdo, port->nr_src_pdo))
		return -EINVAL;

	if (port->port_type == TYPEC_PORT_SRC)
		return 0;

	/* Get the preferred power role for DRP */
	cap_str = ofnode_read_string(node, "try-power-role");
	if (!cap_str)
		return -EINVAL;

	if (!strcmp("sink", cap_str))
		port->typec_prefer_role = TYPEC_SINK;
	else if (!strcmp("source", cap_str))
		port->typec_prefer_role = TYPEC_SOURCE;
	else
		return -EINVAL;

	if (port->typec_prefer_role < 0)
		return -EINVAL;
sink:
	/* Get sink pdos */
	ret = ofnode_read_size(node, "sink-pdos") / sizeof(u32);
	if (ret <= 0)
		return -EINVAL;

	port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
	ret = ofnode_read_u32_array(node, "sink-pdos",
				    port->snk_pdo, port->nr_snk_pdo);
	if (ret || tcpm_validate_caps(dev, port->snk_pdo, port->nr_snk_pdo))
		return -EINVAL;

	if (ofnode_read_u32_array(node, "op-sink-microwatt", &mw, 1))
		return -EINVAL;
	port->operating_snk_mw = mw / 1000;

	port->self_powered = ofnode_read_bool(node, "self-powered");

	return 0;
}

static int tcpm_port_init(struct udevice *dev)
{
	struct tcpm_port *port = dev_get_uclass_plat(dev);
	int err;

	err = tcpm_fw_get_caps(dev);
	if (err < 0) {
		dev_err(dev, "TCPM: please check the dts config: %d\n", err);
		return err;
	}

	port->try_role = port->typec_prefer_role;
	port->port_type = port->typec_type;

	tcpm_init(dev);

	dev_info(dev, "TCPM: init finished\n");

	return 0;
}

static void tcpm_poll_event(struct udevice *dev)
{
	const struct dm_tcpm_ops *drvops = dev_get_driver_ops(dev);
	struct tcpm_port *port = dev_get_uclass_plat(dev);

	if (!drvops->get_vbus(dev))
		return;

	while (port->poll_event_cnt < TCPM_POLL_EVENT_TIME_OUT) {
		if (!port->wait_dr_swap_message &&
		    (port->state == SNK_READY || port->state == SRC_READY))
			break;

		drvops->poll_event(dev);
		port->poll_event_cnt++;
		udelay(500);
		tcpm_check_and_run_delayed_work(dev);
	}

	if (port->state != SNK_READY && port->state != SRC_READY)
		dev_warn(dev, "TCPM: exit in state %s\n",
			 tcpm_states[port->state]);

	/*
	 * At this time, call the callback function of the respective pd chip
	 * to enter the low-power mode. In order to reduce the time spent on
	 * the PD chip driver as much as possible, the tcpm framework does not
	 * fully process the communication initiated by the device,so it should
	 * be noted that we can disable the internal oscillator, etc., but do
	 * not turn off the power of the transceiver module, otherwise the
	 * self-powered Type-C device will initiate a Message(eg: self-powered
	 * Type-C hub initiates a SINK capability request(PD_CTRL_GET_SINK_CAP))
	 * and the pd chip cannot reply to GoodCRC, causing the self-powered Type-C
	 * device to switch vbus to vSafe5v, or even turn off vbus.
	 */
	if (!drvops->enter_low_power_mode)
		return;

	if (drvops->enter_low_power_mode(dev, port->attached, port->pd_capable))
		dev_err(dev, "TCPM: failed to enter low power\n");
	else
		dev_info(dev, "TCPM: PD chip enter low power mode\n");
}

int tcpm_post_probe(struct udevice *dev)
{
	int ret = tcpm_port_init(dev);

	if (ret < 0) {
		dev_err(dev, "failed to tcpm port init\n");
		return ret;
	}

	tcpm_poll_event(dev);

	return 0;
}