Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

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
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/*
 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
 */
#include <linux/bits.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/extcon.h>
#include <linux/fs.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/types.h>
#include <linux/wait.h>

#include <crypto/hash.h>

#include <drm/display/drm_dp_helper.h>
#include <drm/display/drm_hdcp_helper.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_bridge.h>
#include <drm/drm_crtc.h>
#include <drm/drm_edid.h>
#include <drm/drm_print.h>
#include <drm/drm_probe_helper.h>

#include <sound/hdmi-codec.h>

#define REG_IC_VER 0x04

#define REG_RESET_CTRL 0x05
#define VIDEO_RESET BIT(0)
#define AUDIO_RESET BIT(1)
#define ALL_LOGIC_RESET BIT(2)
#define AUX_RESET BIT(3)
#define HDCP_RESET BIT(4)

#define INT_STATUS_01 0x06
#define INT_MASK_01 0x09
#define INT_HPD_CHANGE 0
#define INT_RECEIVE_HPD_IRQ 1
#define INT_SCDT_CHANGE 2
#define INT_HDCP_FAIL 3
#define INT_HDCP_DONE 4
#define BIT_OFFSET(x) (((x) - INT_STATUS_01) * BITS_PER_BYTE)
#define BIT_INT_HPD INT_HPD_CHANGE
#define BIT_INT_HPD_IRQ INT_RECEIVE_HPD_IRQ
#define BIT_INT_SCDT INT_SCDT_CHANGE
#define BIT_INT_HDCP_FAIL INT_HDCP_FAIL
#define BIT_INT_HDCP_DONE INT_HDCP_DONE

#define INT_STATUS_02 0x07
#define INT_MASK_02 0x0A
#define INT_AUX_CMD_FAIL 0
#define INT_HDCP_KSV_CHECK 1
#define INT_AUDIO_FIFO_ERROR 2
#define BIT_INT_AUX_CMD_FAIL (BIT_OFFSET(0x07) + INT_AUX_CMD_FAIL)
#define BIT_INT_HDCP_KSV_CHECK (BIT_OFFSET(0x07) + INT_HDCP_KSV_CHECK)
#define BIT_INT_AUDIO_FIFO_ERROR (BIT_OFFSET(0x07) + INT_AUDIO_FIFO_ERROR)

#define INT_STATUS_03 0x08
#define INT_MASK_03 0x0B
#define INT_LINK_TRAIN_FAIL 4
#define INT_VID_FIFO_ERROR 5
#define INT_IO_LATCH_FIFO_OVERFLOW 7
#define BIT_INT_LINK_TRAIN_FAIL (BIT_OFFSET(0x08) + INT_LINK_TRAIN_FAIL)
#define BIT_INT_VID_FIFO_ERROR (BIT_OFFSET(0x08) + INT_VID_FIFO_ERROR)
#define BIT_INT_IO_FIFO_OVERFLOW (BIT_OFFSET(0x08) + INT_IO_LATCH_FIFO_OVERFLOW)

#define REG_SYSTEM_STS 0x0D
#define INT_STS BIT(0)
#define HPD_STS BIT(1)
#define VIDEO_STB BIT(2)

#define REG_LINK_TRAIN_STS 0x0E
#define LINK_STATE_CR BIT(2)
#define LINK_STATE_EQ BIT(3)
#define LINK_STATE_NORP BIT(4)

#define REG_BANK_SEL 0x0F
#define REG_CLK_CTRL0 0x10
#define M_PCLK_DELAY 0x03

#define REG_AUX_OPT 0x11
#define AUX_AUTO_RST BIT(0)
#define AUX_FIX_FREQ BIT(3)

#define REG_DATA_CTRL0 0x12
#define VIDEO_LATCH_EDGE BIT(4)
#define ENABLE_PCLK_COUNTER BIT(7)

#define REG_PCLK_COUNTER_VALUE 0x13

#define REG_501_FIFO_CTRL 0x15
#define RST_501_FIFO BIT(1)

#define REG_TRAIN_CTRL0 0x16
#define FORCE_LBR BIT(0)
#define LANE_COUNT_MASK 0x06
#define LANE_SWAP BIT(3)
#define SPREAD_AMP_5 BIT(4)
#define FORCE_CR_DONE BIT(5)
#define FORCE_EQ_DONE BIT(6)

#define REG_TRAIN_CTRL1 0x17
#define AUTO_TRAIN BIT(0)
#define MANUAL_TRAIN BIT(1)
#define FORCE_RETRAIN BIT(2)

#define REG_AUX_CTRL 0x23
#define CLR_EDID_FIFO BIT(0)
#define AUX_USER_MODE BIT(1)
#define AUX_NO_SEGMENT_WR BIT(6)
#define AUX_EN_FIFO_READ BIT(7)

#define REG_AUX_ADR_0_7 0x24
#define REG_AUX_ADR_8_15 0x25
#define REG_AUX_ADR_16_19 0x26
#define REG_AUX_OUT_DATA0 0x27

#define REG_AUX_CMD_REQ 0x2B
#define AUX_BUSY BIT(5)

#define REG_AUX_DATA_0_7 0x2C
#define REG_AUX_DATA_8_15 0x2D
#define REG_AUX_DATA_16_23 0x2E
#define REG_AUX_DATA_24_31 0x2F

#define REG_AUX_DATA_FIFO 0x2F

#define REG_AUX_ERROR_STS 0x9F
#define M_AUX_REQ_FAIL 0x03

#define REG_HDCP_CTRL1 0x38
#define HDCP_CP_ENABLE BIT(0)

#define REG_HDCP_TRIGGER 0x39
#define HDCP_TRIGGER_START  BIT(0)
#define HDCP_TRIGGER_CPIRQ  BIT(1)
#define HDCP_TRIGGER_KSV_DONE  BIT(4)
#define HDCP_TRIGGER_KSV_FAIL BIT(5)

#define REG_HDCP_CTRL2 0x3A
#define HDCP_AN_SEL BIT(0)
#define HDCP_AN_GEN BIT(1)
#define HDCP_HW_HPDIRQ_ACT BIT(2)
#define HDCP_EN_M0_READ BIT(5)

#define REG_M0_0_7 0x4C
#define REG_AN_0_7 0x4C
#define REG_SP_CTRL0 0x58
#define REG_IP_CTRL1 0x59
#define REG_IP_CTRL2 0x5A

#define REG_LINK_DRV 0x5C
#define DRV_HS BIT(1)

#define REG_DRV_LN_DATA_SEL 0x5D

#define REG_AUX 0x5E

#define REG_VID_BUS_CTRL0 0x60
#define IN_DDR BIT(2)
#define DDR_CD (0x01 << 6)

#define REG_VID_BUS_CTRL1 0x61
#define TX_FIFO_RESET BIT(1)

#define REG_INPUT_CTRL 0xA0
#define INPUT_HSYNC_POL BIT(0)
#define INPUT_VSYNC_POL BIT(2)
#define INPUT_INTERLACED BIT(4)

#define REG_INPUT_HTOTAL 0xA1
#define REG_INPUT_HACTIVE_START 0xA3
#define REG_INPUT_HACTIVE_WIDTH 0xA5
#define REG_INPUT_HFRONT_PORCH 0xA7
#define REG_INPUT_HSYNC_WIDTH 0xA9
#define REG_INPUT_VTOTAL 0xAB
#define REG_INPUT_VACTIVE_START 0xAD
#define REG_INPUT_VACTIVE_WIDTH 0xAF
#define REG_INPUT_VFRONT_PORCH 0xB1
#define REG_INPUT_VSYNC_WIDTH 0xB3

#define REG_AUDIO_SRC_CTRL 0xB8
#define M_AUDIO_I2S_EN 0x0F
#define EN_I2S0 BIT(0)
#define EN_I2S1 BIT(1)
#define EN_I2S2 BIT(2)
#define EN_I2S3 BIT(3)
#define AUDIO_FIFO_RESET BIT(7)

#define REG_AUDIO_FMT 0xB9
#define REG_AUDIO_FIFO_SEL 0xBA

#define REG_AUDIO_CTRL0 0xBB
#define AUDIO_FULL_PKT BIT(4)
#define AUDIO_16B_BOUND BIT(5)

#define REG_AUDIO_CTRL1 0xBC
#define REG_AUDIO_INPUT_FREQ 0xBE

#define REG_IEC958_STS0 0xBF
#define REG_IEC958_STS1 0xC0
#define REG_IEC958_STS2 0xC1
#define REG_IEC958_STS3 0xC2
#define REG_IEC958_STS4 0xC3

#define REG_HPD_IRQ_TIME 0xC9
#define REG_AUX_DEBUG_MODE 0xCA
#define REG_AUX_OPT2 0xCB
#define REG_HDCP_OPT 0xCE
#define REG_USER_DRV_PRE 0xCF

#define REG_DATA_MUTE_CTRL 0xD3
#define ENABLE_ENHANCED_FRAME BIT(0)
#define ENABLE_AUTO_VIDEO_FIFO_RESET BIT(1)
#define EN_VID_MUTE BIT(4)
#define EN_AUD_MUTE BIT(5)

#define REG_TIME_STMP_CTRL 0xD4
#define EN_ENHANCE_VID_STMP BIT(0)
#define EN_ENHANCE_AUD_STMP BIT(2)
#define M_STAMP_STEP 0x30
#define EN_SSC_GAT BIT(6)

#define REG_INFOFRAME_CTRL 0xE8
#define EN_AVI_PKT BIT(0)
#define EN_AUD_PKT BIT(1)
#define EN_MPG_PKT BIT(2)
#define EN_GEN_PKT BIT(3)
#define EN_VID_TIME_STMP BIT(4)
#define EN_AUD_TIME_STMP BIT(5)
#define EN_VID_CTRL_PKT (EN_AVI_PKT | EN_VID_TIME_STMP)
#define EN_AUD_CTRL_PKT (EN_AUD_PKT | EN_AUD_TIME_STMP)

#define REG_AUDIO_N_0_7 0xDE
#define REG_AUDIO_N_8_15 0xDF
#define REG_AUDIO_N_16_23 0xE0

#define REG_AVI_INFO_DB1 0xE9
#define REG_AVI_INFO_DB2 0xEA
#define REG_AVI_INFO_DB3 0xEB
#define REG_AVI_INFO_DB4 0xEC
#define REG_AVI_INFO_DB5 0xED
#define REG_AVI_INFO_SUM 0xF6

#define REG_AUD_INFOFRAM_DB1 0xF7
#define REG_AUD_INFOFRAM_DB2 0xF8
#define REG_AUD_INFOFRAM_DB3 0xF9
#define REG_AUD_INFOFRAM_DB4 0xFA
#define REG_AUD_INFOFRAM_SUM 0xFB

/* the following six registers are in bank1 */
#define REG_DRV_0_DB_800_MV 0x17E
#define REG_PRE_0_DB_800_MV 0x17F
#define REG_PRE_3P5_DB_800_MV 0x181
#define REG_SSC_CTRL0 0x188
#define REG_SSC_CTRL1 0x189
#define REG_SSC_CTRL2 0x18A

#define RBR DP_LINK_BW_1_62
#define HBR DP_LINK_BW_2_7
#define HBR2 DP_LINK_BW_5_4
#define HBR3 DP_LINK_BW_8_1

#define DPCD_V_1_1 0x11
#define MISC_VERB 0xF0
#define MISC_VERC 0x70
#define I2S_INPUT_FORMAT_STANDARD 0
#define I2S_INPUT_FORMAT_32BIT 1
#define I2S_INPUT_LEFT_JUSTIFIED 0
#define I2S_INPUT_RIGHT_JUSTIFIED 1
#define I2S_DATA_1T_DELAY 0
#define I2S_DATA_NO_DELAY 1
#define I2S_WS_LEFT_CHANNEL 0
#define I2S_WS_RIGHT_CHANNEL 1
#define I2S_DATA_MSB_FIRST 0
#define I2S_DATA_LSB_FIRST 1
#define WORD_LENGTH_16BIT 0
#define WORD_LENGTH_18BIT 1
#define WORD_LENGTH_20BIT 2
#define WORD_LENGTH_24BIT 3
#define DEBUGFS_DIR_NAME "it6505-debugfs"
#define READ_BUFFER_SIZE 400

/* Vendor option */
#define HDCP_DESIRED 1
#define MAX_LANE_COUNT 4
#define MAX_LINK_RATE HBR
#define AUTO_TRAIN_RETRY 3
#define MAX_HDCP_DOWN_STREAM_COUNT 10
#define MAX_CR_LEVEL 0x03
#define MAX_EQ_LEVEL 0x03
#define AUX_WAIT_TIMEOUT_MS 15
#define AUX_FIFO_MAX_SIZE 32
#define PIXEL_CLK_DELAY 1
#define PIXEL_CLK_INVERSE 0
#define ADJUST_PHASE_THRESHOLD 80000
#define DPI_PIXEL_CLK_MAX 95000
#define HDCP_SHA1_FIFO_LEN (MAX_HDCP_DOWN_STREAM_COUNT * 5 + 10)
#define DEFAULT_PWR_ON 0
#define DEFAULT_DRV_HOLD 0

#define AUDIO_SELECT I2S
#define AUDIO_TYPE LPCM
#define AUDIO_SAMPLE_RATE SAMPLE_RATE_48K
#define AUDIO_CHANNEL_COUNT 2
#define I2S_INPUT_FORMAT I2S_INPUT_FORMAT_32BIT
#define I2S_JUSTIFIED I2S_INPUT_LEFT_JUSTIFIED
#define I2S_DATA_DELAY I2S_DATA_1T_DELAY
#define I2S_WS_CHANNEL I2S_WS_LEFT_CHANNEL
#define I2S_DATA_SEQUENCE I2S_DATA_MSB_FIRST
#define AUDIO_WORD_LENGTH WORD_LENGTH_24BIT

enum aux_cmd_type {
	CMD_AUX_NATIVE_READ = 0x0,
	CMD_AUX_NATIVE_WRITE = 0x5,
	CMD_AUX_I2C_EDID_READ = 0xB,
};

enum aux_cmd_reply {
	REPLY_ACK,
	REPLY_NACK,
	REPLY_DEFER,
};

enum link_train_status {
	LINK_IDLE,
	LINK_BUSY,
	LINK_OK,
};

enum hdcp_state {
	HDCP_AUTH_IDLE,
	HDCP_AUTH_GOING,
	HDCP_AUTH_DONE,
};

struct it6505_platform_data {
	struct regulator *pwr18;
	struct regulator *ovdd;
	struct gpio_desc *gpiod_reset;
};

enum it6505_audio_select {
	I2S = 0,
	SPDIF,
};

enum it6505_audio_sample_rate {
	SAMPLE_RATE_24K = 0x6,
	SAMPLE_RATE_32K = 0x3,
	SAMPLE_RATE_48K = 0x2,
	SAMPLE_RATE_96K = 0xA,
	SAMPLE_RATE_192K = 0xE,
	SAMPLE_RATE_44_1K = 0x0,
	SAMPLE_RATE_88_2K = 0x8,
	SAMPLE_RATE_176_4K = 0xC,
};

enum it6505_audio_type {
	LPCM = 0,
	NLPCM,
	DSS,
};

struct it6505_audio_data {
	enum it6505_audio_select select;
	enum it6505_audio_sample_rate sample_rate;
	enum it6505_audio_type type;
	u8 word_length;
	u8 channel_count;
	u8 i2s_input_format;
	u8 i2s_justified;
	u8 i2s_data_delay;
	u8 i2s_ws_channel;
	u8 i2s_data_sequence;
};

struct it6505_audio_sample_rate_map {
	enum it6505_audio_sample_rate rate;
	int sample_rate_value;
};

struct it6505_drm_dp_link {
	unsigned char revision;
	unsigned int rate;
	unsigned int num_lanes;
	unsigned long capabilities;
};

struct debugfs_entries {
	char *name;
	const struct file_operations *fops;
};

struct it6505 {
	struct drm_dp_aux aux;
	struct drm_bridge bridge;
	struct i2c_client *client;
	struct it6505_drm_dp_link link;
	struct it6505_platform_data pdata;
	/*
	 * Mutex protects extcon and interrupt functions from interfering
	 * each other.
	 */
	struct mutex extcon_lock;
	struct mutex mode_lock; /* used to bridge_detect */
	struct mutex aux_lock; /* used to aux data transfers */
	struct regmap *regmap;
	struct drm_display_mode source_output_mode;
	struct drm_display_mode video_info;
	struct notifier_block event_nb;
	struct extcon_dev *extcon;
	struct work_struct extcon_wq;
	int extcon_state;
	enum drm_connector_status connector_status;
	enum link_train_status link_state;
	struct work_struct link_works;
	u8 dpcd[DP_RECEIVER_CAP_SIZE];
	u8 lane_count;
	u8 link_rate_bw_code;
	u8 sink_count;
	bool step_train;
	bool branch_device;
	bool enable_ssc;
	bool lane_swap_disabled;
	bool lane_swap;
	bool powered;
	bool hpd_state;
	u32 afe_setting;
	u32 max_dpi_pixel_clock;
	u32 max_lane_count;
	enum hdcp_state hdcp_status;
	struct delayed_work hdcp_work;
	struct work_struct hdcp_wait_ksv_list;
	struct completion extcon_completion;
	u8 auto_train_retry;
	bool hdcp_desired;
	bool is_repeater;
	u8 hdcp_down_stream_count;
	u8 bksvs[DRM_HDCP_KSV_LEN];
	u8 sha1_input[HDCP_SHA1_FIFO_LEN];
	bool enable_enhanced_frame;
	hdmi_codec_plugged_cb plugged_cb;
	struct device *codec_dev;
	struct delayed_work delayed_audio;
	struct it6505_audio_data audio;
	struct dentry *debugfs;

	/* it6505 driver hold option */
	bool enable_drv_hold;

	struct edid *cached_edid;
};

struct it6505_step_train_para {
	u8 voltage_swing[MAX_LANE_COUNT];
	u8 pre_emphasis[MAX_LANE_COUNT];
};

/*
 * Vendor option afe settings for different platforms
 * 0: without FPC cable
 * 1: with FPC cable
 */

static const u8 afe_setting_table[][3] = {
	{0x82, 0x00, 0x45},
	{0x93, 0x2A, 0x85}
};

static const struct it6505_audio_sample_rate_map audio_sample_rate_map[] = {
	{SAMPLE_RATE_24K, 24000},
	{SAMPLE_RATE_32K, 32000},
	{SAMPLE_RATE_48K, 48000},
	{SAMPLE_RATE_96K, 96000},
	{SAMPLE_RATE_192K, 192000},
	{SAMPLE_RATE_44_1K, 44100},
	{SAMPLE_RATE_88_2K, 88200},
	{SAMPLE_RATE_176_4K, 176400},
};

static const struct regmap_range it6505_bridge_volatile_ranges[] = {
	{ .range_min = 0, .range_max = 0x1FF },
};

static const struct regmap_access_table it6505_bridge_volatile_table = {
	.yes_ranges = it6505_bridge_volatile_ranges,
	.n_yes_ranges = ARRAY_SIZE(it6505_bridge_volatile_ranges),
};

static const struct regmap_range_cfg it6505_regmap_banks[] = {
	{
		.name = "it6505",
		.range_min = 0x00,
		.range_max = 0x1FF,
		.selector_reg = REG_BANK_SEL,
		.selector_mask = 0x1,
		.selector_shift = 0,
		.window_start = 0x00,
		.window_len = 0x100,
	},
};

static const struct regmap_config it6505_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.volatile_table = &it6505_bridge_volatile_table,
	.cache_type = REGCACHE_NONE,
	.ranges = it6505_regmap_banks,
	.num_ranges = ARRAY_SIZE(it6505_regmap_banks),
	.max_register = 0x1FF,
};

static int it6505_read(struct it6505 *it6505, unsigned int reg_addr)
{
	unsigned int value;
	int err;
	struct device *dev = &it6505->client->dev;

	if (!it6505->powered)
		return -ENODEV;

	err = regmap_read(it6505->regmap, reg_addr, &value);
	if (err < 0) {
		dev_err(dev, "read failed reg[0x%x] err: %d", reg_addr, err);
		return err;
	}

	return value;
}

static int it6505_write(struct it6505 *it6505, unsigned int reg_addr,
			unsigned int reg_val)
{
	int err;
	struct device *dev = &it6505->client->dev;

	if (!it6505->powered)
		return -ENODEV;

	err = regmap_write(it6505->regmap, reg_addr, reg_val);

	if (err < 0) {
		dev_err(dev, "write failed reg[0x%x] = 0x%x err = %d",
			reg_addr, reg_val, err);
		return err;
	}

	return 0;
}

static int it6505_set_bits(struct it6505 *it6505, unsigned int reg,
			   unsigned int mask, unsigned int value)
{
	int err;
	struct device *dev = &it6505->client->dev;

	if (!it6505->powered)
		return -ENODEV;

	err = regmap_update_bits(it6505->regmap, reg, mask, value);
	if (err < 0) {
		dev_err(dev, "write reg[0x%x] = 0x%x mask = 0x%x failed err %d",
			reg, value, mask, err);
		return err;
	}

	return 0;
}

static void it6505_debug_print(struct it6505 *it6505, unsigned int reg,
			       const char *prefix)
{
	struct device *dev = &it6505->client->dev;
	int val;

	if (!drm_debug_enabled(DRM_UT_DRIVER))
		return;

	val = it6505_read(it6505, reg);
	if (val < 0)
		DRM_DEV_DEBUG_DRIVER(dev, "%s reg[%02x] read error (%d)",
				     prefix, reg, val);
	else
		DRM_DEV_DEBUG_DRIVER(dev, "%s reg[%02x] = 0x%02x", prefix, reg,
				     val);
}

static int it6505_dpcd_read(struct it6505 *it6505, unsigned long offset)
{
	u8 value;
	int ret;
	struct device *dev = &it6505->client->dev;

	ret = drm_dp_dpcd_readb(&it6505->aux, offset, &value);
	if (ret < 0) {
		dev_err(dev, "DPCD read failed [0x%lx] ret: %d", offset, ret);
		return ret;
	}
	return value;
}

static int it6505_dpcd_write(struct it6505 *it6505, unsigned long offset,
			     u8 datain)
{
	int ret;
	struct device *dev = &it6505->client->dev;

	ret = drm_dp_dpcd_writeb(&it6505->aux, offset, datain);
	if (ret < 0) {
		dev_err(dev, "DPCD write failed [0x%lx] ret: %d", offset, ret);
		return ret;
	}
	return 0;
}

static int it6505_get_dpcd(struct it6505 *it6505, int offset, u8 *dpcd, int num)
{
	int ret;
	struct device *dev = &it6505->client->dev;

	ret = drm_dp_dpcd_read(&it6505->aux, offset, dpcd, num);

	if (ret < 0)
		return ret;

	DRM_DEV_DEBUG_DRIVER(dev, "ret = %d DPCD[0x%x] = 0x%*ph", ret, offset,
			     num, dpcd);

	return 0;
}

static void it6505_dump(struct it6505 *it6505)
{
	unsigned int i, j;
	u8 regs[16];
	struct device *dev = &it6505->client->dev;

	for (i = 0; i <= 0xff; i += 16) {
		for (j = 0; j < 16; j++)
			regs[j] = it6505_read(it6505, i + j);

		DRM_DEV_DEBUG_DRIVER(dev, "[0x%02x] = %16ph", i, regs);
	}
}

static bool it6505_get_sink_hpd_status(struct it6505 *it6505)
{
	int reg_0d;

	reg_0d = it6505_read(it6505, REG_SYSTEM_STS);

	if (reg_0d < 0)
		return false;

	return reg_0d & HPD_STS;
}

static int it6505_read_word(struct it6505 *it6505, unsigned int reg)
{
	int val0, val1;

	val0 = it6505_read(it6505, reg);
	if (val0 < 0)
		return val0;

	val1 = it6505_read(it6505, reg + 1);
	if (val1 < 0)
		return val1;

	return (val1 << 8) | val0;
}

static void it6505_calc_video_info(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	int hsync_pol, vsync_pol, interlaced;
	int htotal, hdes, hdew, hfph, hsyncw;
	int vtotal, vdes, vdew, vfph, vsyncw;
	int rddata, i, pclk, sum = 0;

	usleep_range(10000, 15000);
	rddata = it6505_read(it6505, REG_INPUT_CTRL);
	hsync_pol = rddata & INPUT_HSYNC_POL;
	vsync_pol = (rddata & INPUT_VSYNC_POL) >> 2;
	interlaced = (rddata & INPUT_INTERLACED) >> 4;

	htotal = it6505_read_word(it6505, REG_INPUT_HTOTAL) & 0x1FFF;
	hdes = it6505_read_word(it6505, REG_INPUT_HACTIVE_START) & 0x1FFF;
	hdew = it6505_read_word(it6505, REG_INPUT_HACTIVE_WIDTH) & 0x1FFF;
	hfph = it6505_read_word(it6505, REG_INPUT_HFRONT_PORCH) & 0x1FFF;
	hsyncw = it6505_read_word(it6505, REG_INPUT_HSYNC_WIDTH) & 0x1FFF;

	vtotal = it6505_read_word(it6505, REG_INPUT_VTOTAL) & 0xFFF;
	vdes = it6505_read_word(it6505, REG_INPUT_VACTIVE_START) & 0xFFF;
	vdew = it6505_read_word(it6505, REG_INPUT_VACTIVE_WIDTH) & 0xFFF;
	vfph = it6505_read_word(it6505, REG_INPUT_VFRONT_PORCH) & 0xFFF;
	vsyncw = it6505_read_word(it6505, REG_INPUT_VSYNC_WIDTH) & 0xFFF;

	DRM_DEV_DEBUG_DRIVER(dev, "hsync_pol:%d, vsync_pol:%d, interlaced:%d",
			     hsync_pol, vsync_pol, interlaced);
	DRM_DEV_DEBUG_DRIVER(dev, "hactive_start:%d, vactive_start:%d",
			     hdes, vdes);

	for (i = 0; i < 3; i++) {
		it6505_set_bits(it6505, REG_DATA_CTRL0, ENABLE_PCLK_COUNTER,
				ENABLE_PCLK_COUNTER);
		usleep_range(10000, 15000);
		it6505_set_bits(it6505, REG_DATA_CTRL0, ENABLE_PCLK_COUNTER,
				0x00);
		rddata = it6505_read_word(it6505, REG_PCLK_COUNTER_VALUE) &
			 0xFFF;

		sum += rddata;
	}

	if (sum == 0) {
		DRM_DEV_DEBUG_DRIVER(dev, "calc video timing error");
		return;
	}

	sum /= 3;
	pclk = 13500 * 2048 / sum;
	it6505->video_info.clock = pclk;
	it6505->video_info.hdisplay = hdew;
	it6505->video_info.hsync_start = hdew + hfph;
	it6505->video_info.hsync_end = hdew + hfph + hsyncw;
	it6505->video_info.htotal = htotal;
	it6505->video_info.vdisplay = vdew;
	it6505->video_info.vsync_start = vdew + vfph;
	it6505->video_info.vsync_end = vdew + vfph + vsyncw;
	it6505->video_info.vtotal = vtotal;

	DRM_DEV_DEBUG_DRIVER(dev, DRM_MODE_FMT,
			     DRM_MODE_ARG(&it6505->video_info));
}

static int it6505_drm_dp_link_set_power(struct drm_dp_aux *aux,
					struct it6505_drm_dp_link *link,
					u8 mode)
{
	u8 value;
	int err;

	/* DP_SET_POWER register is only available on DPCD v1.1 and later */
	if (link->revision < DPCD_V_1_1)
		return 0;

	err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
	if (err < 0)
		return err;

	value &= ~DP_SET_POWER_MASK;
	value |= mode;

	err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
	if (err < 0)
		return err;

	if (mode == DP_SET_POWER_D0) {
		/*
		 * According to the DP 1.1 specification, a "Sink Device must
		 * exit the power saving state within 1 ms" (Section 2.5.3.1,
		 * Table 5-52, "Sink Control Field" (register 0x600).
		 */
		usleep_range(1000, 2000);
	}

	return 0;
}

static void it6505_clear_int(struct it6505 *it6505)
{
	it6505_write(it6505, INT_STATUS_01, 0xFF);
	it6505_write(it6505, INT_STATUS_02, 0xFF);
	it6505_write(it6505, INT_STATUS_03, 0xFF);
}

static void it6505_int_mask_enable(struct it6505 *it6505)
{
	it6505_write(it6505, INT_MASK_01, BIT(INT_HPD_CHANGE) |
		     BIT(INT_RECEIVE_HPD_IRQ) | BIT(INT_SCDT_CHANGE) |
		     BIT(INT_HDCP_FAIL) | BIT(INT_HDCP_DONE));

	it6505_write(it6505, INT_MASK_02, BIT(INT_AUX_CMD_FAIL) |
		     BIT(INT_HDCP_KSV_CHECK) | BIT(INT_AUDIO_FIFO_ERROR));

	it6505_write(it6505, INT_MASK_03, BIT(INT_LINK_TRAIN_FAIL) |
		     BIT(INT_VID_FIFO_ERROR) | BIT(INT_IO_LATCH_FIFO_OVERFLOW));
}

static void it6505_int_mask_disable(struct it6505 *it6505)
{
	it6505_write(it6505, INT_MASK_01, 0x00);
	it6505_write(it6505, INT_MASK_02, 0x00);
	it6505_write(it6505, INT_MASK_03, 0x00);
}

static void it6505_lane_termination_on(struct it6505 *it6505)
{
	int regcf;

	regcf = it6505_read(it6505, REG_USER_DRV_PRE);

	if (regcf == MISC_VERB)
		it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL, 0x80, 0x00);

	if (regcf == MISC_VERC) {
		if (it6505->lane_swap) {
			switch (it6505->lane_count) {
			case 1:
			case 2:
				it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL,
						0x0C, 0x08);
				break;
			default:
				it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL,
						0x0C, 0x0C);
				break;
			}
		} else {
			switch (it6505->lane_count) {
			case 1:
			case 2:
				it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL,
						0x0C, 0x04);
				break;
			default:
				it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL,
						0x0C, 0x0C);
				break;
			}
		}
	}
}

static void it6505_lane_termination_off(struct it6505 *it6505)
{
	int regcf;

	regcf = it6505_read(it6505, REG_USER_DRV_PRE);

	if (regcf == MISC_VERB)
		it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL, 0x80, 0x80);

	if (regcf == MISC_VERC)
		it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL, 0x0C, 0x00);
}

static void it6505_lane_power_on(struct it6505 *it6505)
{
	it6505_set_bits(it6505, REG_LINK_DRV, 0xF1,
			(it6505->lane_swap ?
				 GENMASK(7, 8 - it6505->lane_count) :
				 GENMASK(3 + it6505->lane_count, 4)) |
				0x01);
}

static void it6505_lane_power_off(struct it6505 *it6505)
{
	it6505_set_bits(it6505, REG_LINK_DRV, 0xF0, 0x00);
}

static void it6505_lane_off(struct it6505 *it6505)
{
	it6505_lane_power_off(it6505);
	it6505_lane_termination_off(it6505);
}

static void it6505_aux_termination_on(struct it6505 *it6505)
{
	int regcf;

	regcf = it6505_read(it6505, REG_USER_DRV_PRE);

	if (regcf == MISC_VERB)
		it6505_lane_termination_on(it6505);

	if (regcf == MISC_VERC)
		it6505_set_bits(it6505, REG_DRV_LN_DATA_SEL, 0x80, 0x80);
}

static void it6505_aux_power_on(struct it6505 *it6505)
{
	it6505_set_bits(it6505, REG_AUX, 0x02, 0x02);
}

static void it6505_aux_on(struct it6505 *it6505)
{
	it6505_aux_power_on(it6505);
	it6505_aux_termination_on(it6505);
}

static void it6505_aux_reset(struct it6505 *it6505)
{
	it6505_set_bits(it6505, REG_RESET_CTRL, AUX_RESET, AUX_RESET);
	it6505_set_bits(it6505, REG_RESET_CTRL, AUX_RESET, 0x00);
}

static void it6505_reset_logic(struct it6505 *it6505)
{
	regmap_write(it6505->regmap, REG_RESET_CTRL, ALL_LOGIC_RESET);
	usleep_range(1000, 1500);
}

static bool it6505_aux_op_finished(struct it6505 *it6505)
{
	int reg2b = it6505_read(it6505, REG_AUX_CMD_REQ);

	if (reg2b < 0)
		return false;

	return (reg2b & AUX_BUSY) == 0;
}

static int it6505_aux_wait(struct it6505 *it6505)
{
	int status;
	unsigned long timeout;
	struct device *dev = &it6505->client->dev;

	timeout = jiffies + msecs_to_jiffies(AUX_WAIT_TIMEOUT_MS) + 1;

	while (!it6505_aux_op_finished(it6505)) {
		if (time_after(jiffies, timeout)) {
			dev_err(dev, "Timed out waiting AUX to finish");
			return -ETIMEDOUT;
		}
		usleep_range(1000, 2000);
	}

	status = it6505_read(it6505, REG_AUX_ERROR_STS);
	if (status < 0) {
		dev_err(dev, "Failed to read AUX channel: %d", status);
		return status;
	}

	return 0;
}

static ssize_t it6505_aux_operation(struct it6505 *it6505,
				    enum aux_cmd_type cmd,
				    unsigned int address, u8 *buffer,
				    size_t size, enum aux_cmd_reply *reply)
{
	int i, ret;
	bool aux_write_check = false;

	if (!it6505_get_sink_hpd_status(it6505))
		return -EIO;

	/* set AUX user mode */
	it6505_set_bits(it6505, REG_AUX_CTRL, AUX_USER_MODE, AUX_USER_MODE);

aux_op_start:
	if (cmd == CMD_AUX_I2C_EDID_READ) {
		/* AUX EDID FIFO has max length of AUX_FIFO_MAX_SIZE bytes. */
		size = min_t(size_t, size, AUX_FIFO_MAX_SIZE);
		/* Enable AUX FIFO read back and clear FIFO */
		it6505_set_bits(it6505, REG_AUX_CTRL,
				AUX_EN_FIFO_READ | CLR_EDID_FIFO,
				AUX_EN_FIFO_READ | CLR_EDID_FIFO);

		it6505_set_bits(it6505, REG_AUX_CTRL,
				AUX_EN_FIFO_READ | CLR_EDID_FIFO,
				AUX_EN_FIFO_READ);
	} else {
		/* The DP AUX transmit buffer has 4 bytes. */
		size = min_t(size_t, size, 4);
		it6505_set_bits(it6505, REG_AUX_CTRL, AUX_NO_SEGMENT_WR,
				AUX_NO_SEGMENT_WR);
	}

	/* Start Address[7:0] */
	it6505_write(it6505, REG_AUX_ADR_0_7, (address >> 0) & 0xFF);
	/* Start Address[15:8] */
	it6505_write(it6505, REG_AUX_ADR_8_15, (address >> 8) & 0xFF);
	/* WriteNum[3:0]+StartAdr[19:16] */
	it6505_write(it6505, REG_AUX_ADR_16_19,
		     ((address >> 16) & 0x0F) | ((size - 1) << 4));

	if (cmd == CMD_AUX_NATIVE_WRITE)
		regmap_bulk_write(it6505->regmap, REG_AUX_OUT_DATA0, buffer,
				  size);

	/* Aux Fire */
	it6505_write(it6505, REG_AUX_CMD_REQ, cmd);

	ret = it6505_aux_wait(it6505);
	if (ret < 0)
		goto aux_op_err;

	ret = it6505_read(it6505, REG_AUX_ERROR_STS);
	if (ret < 0)
		goto aux_op_err;

	switch ((ret >> 6) & 0x3) {
	case 0:
		*reply = REPLY_ACK;
		break;
	case 1:
		*reply = REPLY_DEFER;
		ret = -EAGAIN;
		goto aux_op_err;
	case 2:
		*reply = REPLY_NACK;
		ret = -EIO;
		goto aux_op_err;
	case 3:
		ret = -ETIMEDOUT;
		goto aux_op_err;
	}

	/* Read back Native Write data */
	if (cmd == CMD_AUX_NATIVE_WRITE) {
		aux_write_check = true;
		cmd = CMD_AUX_NATIVE_READ;
		goto aux_op_start;
	}

	if (cmd == CMD_AUX_I2C_EDID_READ) {
		for (i = 0; i < size; i++) {
			ret = it6505_read(it6505, REG_AUX_DATA_FIFO);
			if (ret < 0)
				goto aux_op_err;
			buffer[i] = ret;
		}
	} else {
		for (i = 0; i < size; i++) {
			ret = it6505_read(it6505, REG_AUX_DATA_0_7 + i);
			if (ret < 0)
				goto aux_op_err;

			if (aux_write_check && buffer[size - 1 - i] != ret) {
				ret = -EINVAL;
				goto aux_op_err;
			}

			buffer[size - 1 - i] = ret;
		}
	}

	ret = i;

aux_op_err:
	if (cmd == CMD_AUX_I2C_EDID_READ) {
		/* clear AUX FIFO */
		it6505_set_bits(it6505, REG_AUX_CTRL,
				AUX_EN_FIFO_READ | CLR_EDID_FIFO,
				AUX_EN_FIFO_READ | CLR_EDID_FIFO);
		it6505_set_bits(it6505, REG_AUX_CTRL,
				AUX_EN_FIFO_READ | CLR_EDID_FIFO, 0x00);
	}

	/* Leave AUX user mode */
	it6505_set_bits(it6505, REG_AUX_CTRL, AUX_USER_MODE, 0);

	return ret;
}

static ssize_t it6505_aux_do_transfer(struct it6505 *it6505,
				      enum aux_cmd_type cmd,
				      unsigned int address, u8 *buffer,
				      size_t size, enum aux_cmd_reply *reply)
{
	int i, ret_size, ret = 0, request_size;

	mutex_lock(&it6505->aux_lock);
	for (i = 0; i < size; i += 4) {
		request_size = min((int)size - i, 4);
		ret_size = it6505_aux_operation(it6505, cmd, address + i,
						buffer + i, request_size,
						reply);
		if (ret_size < 0) {
			ret = ret_size;
			goto aux_op_err;
		}

		ret += ret_size;
	}

aux_op_err:
	mutex_unlock(&it6505->aux_lock);
	return ret;
}

static ssize_t it6505_aux_transfer(struct drm_dp_aux *aux,
				   struct drm_dp_aux_msg *msg)
{
	struct it6505 *it6505 = container_of(aux, struct it6505, aux);
	u8 cmd;
	bool is_i2c = !(msg->request & DP_AUX_NATIVE_WRITE);
	int ret;
	enum aux_cmd_reply reply;

	/* IT6505 doesn't support arbitrary I2C read / write. */
	if (is_i2c)
		return -EINVAL;

	switch (msg->request) {
	case DP_AUX_NATIVE_READ:
		cmd = CMD_AUX_NATIVE_READ;
		break;
	case DP_AUX_NATIVE_WRITE:
		cmd = CMD_AUX_NATIVE_WRITE;
		break;
	default:
		return -EINVAL;
	}

	ret = it6505_aux_do_transfer(it6505, cmd, msg->address, msg->buffer,
				     msg->size, &reply);
	if (ret < 0)
		return ret;

	switch (reply) {
	case REPLY_ACK:
		msg->reply = DP_AUX_NATIVE_REPLY_ACK;
		break;
	case REPLY_NACK:
		msg->reply = DP_AUX_NATIVE_REPLY_NACK;
		break;
	case REPLY_DEFER:
		msg->reply = DP_AUX_NATIVE_REPLY_DEFER;
		break;
	}

	return ret;
}

static int it6505_get_edid_block(void *data, u8 *buf, unsigned int block,
				 size_t len)
{
	struct it6505 *it6505 = data;
	struct device *dev = &it6505->client->dev;
	enum aux_cmd_reply reply;
	int offset, ret, aux_retry = 100;

	it6505_aux_reset(it6505);
	DRM_DEV_DEBUG_DRIVER(dev, "block number = %d", block);

	for (offset = 0; offset < EDID_LENGTH;) {
		ret = it6505_aux_do_transfer(it6505, CMD_AUX_I2C_EDID_READ,
					     block * EDID_LENGTH + offset,
					     buf + offset, 8, &reply);

		if (ret < 0 && ret != -EAGAIN)
			return ret;

		switch (reply) {
		case REPLY_ACK:
			DRM_DEV_DEBUG_DRIVER(dev, "[0x%02x]: %8ph", offset,
					     buf + offset);
			offset += 8;
			aux_retry = 100;
			break;
		case REPLY_NACK:
			return -EIO;
		case REPLY_DEFER:
			msleep(20);
			if (!(--aux_retry))
				return -EIO;
		}
	}

	return 0;
}

static void it6505_variable_config(struct it6505 *it6505)
{
	it6505->link_rate_bw_code = HBR;
	it6505->lane_count = MAX_LANE_COUNT;
	it6505->link_state = LINK_IDLE;
	it6505->hdcp_desired = HDCP_DESIRED;
	it6505->auto_train_retry = AUTO_TRAIN_RETRY;
	it6505->audio.select = AUDIO_SELECT;
	it6505->audio.sample_rate = AUDIO_SAMPLE_RATE;
	it6505->audio.channel_count = AUDIO_CHANNEL_COUNT;
	it6505->audio.type = AUDIO_TYPE;
	it6505->audio.i2s_input_format = I2S_INPUT_FORMAT;
	it6505->audio.i2s_justified = I2S_JUSTIFIED;
	it6505->audio.i2s_data_delay = I2S_DATA_DELAY;
	it6505->audio.i2s_ws_channel = I2S_WS_CHANNEL;
	it6505->audio.i2s_data_sequence = I2S_DATA_SEQUENCE;
	it6505->audio.word_length = AUDIO_WORD_LENGTH;
	memset(it6505->sha1_input, 0, sizeof(it6505->sha1_input));
	memset(it6505->bksvs, 0, sizeof(it6505->bksvs));
}

static int it6505_send_video_infoframe(struct it6505 *it6505,
				       struct hdmi_avi_infoframe *frame)
{
	u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
	int err;
	struct device *dev = &it6505->client->dev;

	err = hdmi_avi_infoframe_pack(frame, buffer, sizeof(buffer));
	if (err < 0) {
		dev_err(dev, "Failed to pack AVI infoframe: %d", err);
		return err;
	}

	err = it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_AVI_PKT, 0x00);
	if (err)
		return err;

	err = regmap_bulk_write(it6505->regmap, REG_AVI_INFO_DB1,
				buffer + HDMI_INFOFRAME_HEADER_SIZE,
				frame->length);
	if (err)
		return err;

	err = it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_AVI_PKT,
			      EN_AVI_PKT);
	if (err)
		return err;

	return 0;
}

static void it6505_get_extcon_property(struct it6505 *it6505)
{
	int err;
	union extcon_property_value property;
	struct device *dev = &it6505->client->dev;

	if (it6505->extcon && !it6505->lane_swap_disabled) {
		err = extcon_get_property(it6505->extcon, EXTCON_DISP_DP,
					  EXTCON_PROP_USB_TYPEC_POLARITY,
					  &property);
		if (err) {
			dev_err(dev, "get property fail!");
			return;
		}
		it6505->lane_swap = property.intval;
	}
}

static void it6505_clk_phase_adjustment(struct it6505 *it6505,
					const struct drm_display_mode *mode)
{
	int clock = mode->clock;

	it6505_set_bits(it6505, REG_CLK_CTRL0, M_PCLK_DELAY,
			clock < ADJUST_PHASE_THRESHOLD ? PIXEL_CLK_DELAY : 0);
	it6505_set_bits(it6505, REG_DATA_CTRL0, VIDEO_LATCH_EDGE,
			PIXEL_CLK_INVERSE << 4);
}

static void it6505_link_reset_step_train(struct it6505 *it6505)
{
	it6505_set_bits(it6505, REG_TRAIN_CTRL0,
			FORCE_CR_DONE | FORCE_EQ_DONE, 0x00);
	it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
			  DP_TRAINING_PATTERN_DISABLE);
}

static void it6505_init(struct it6505 *it6505)
{
	it6505_write(it6505, REG_AUX_OPT, AUX_AUTO_RST | AUX_FIX_FREQ);
	it6505_write(it6505, REG_AUX_CTRL, AUX_NO_SEGMENT_WR);
	it6505_write(it6505, REG_HDCP_CTRL2, HDCP_AN_SEL | HDCP_HW_HPDIRQ_ACT);
	it6505_write(it6505, REG_VID_BUS_CTRL0, IN_DDR | DDR_CD);
	it6505_write(it6505, REG_VID_BUS_CTRL1, 0x01);
	it6505_write(it6505, REG_AUDIO_CTRL0, AUDIO_16B_BOUND);

	/* chip internal setting, don't modify */
	it6505_write(it6505, REG_HPD_IRQ_TIME, 0xF5);
	it6505_write(it6505, REG_AUX_DEBUG_MODE, 0x4D);
	it6505_write(it6505, REG_AUX_OPT2, 0x17);
	it6505_write(it6505, REG_HDCP_OPT, 0x60);
	it6505_write(it6505, REG_DATA_MUTE_CTRL,
		     EN_VID_MUTE | EN_AUD_MUTE | ENABLE_AUTO_VIDEO_FIFO_RESET);
	it6505_write(it6505, REG_TIME_STMP_CTRL,
		     EN_SSC_GAT | EN_ENHANCE_VID_STMP | EN_ENHANCE_AUD_STMP);
	it6505_write(it6505, REG_INFOFRAME_CTRL, 0x00);
	it6505_write(it6505, REG_DRV_0_DB_800_MV,
		     afe_setting_table[it6505->afe_setting][0]);
	it6505_write(it6505, REG_PRE_0_DB_800_MV,
		     afe_setting_table[it6505->afe_setting][1]);
	it6505_write(it6505, REG_PRE_3P5_DB_800_MV,
		     afe_setting_table[it6505->afe_setting][2]);
	it6505_write(it6505, REG_SSC_CTRL0, 0x9E);
	it6505_write(it6505, REG_SSC_CTRL1, 0x1C);
	it6505_write(it6505, REG_SSC_CTRL2, 0x42);
}

static void it6505_video_disable(struct it6505 *it6505)
{
	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_VID_MUTE, EN_VID_MUTE);
	it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_VID_CTRL_PKT, 0x00);
	it6505_set_bits(it6505, REG_RESET_CTRL, VIDEO_RESET, VIDEO_RESET);
}

static void it6505_video_reset(struct it6505 *it6505)
{
	it6505_link_reset_step_train(it6505);
	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_VID_MUTE, EN_VID_MUTE);
	it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_VID_CTRL_PKT, 0x00);
	it6505_set_bits(it6505, REG_RESET_CTRL, VIDEO_RESET, VIDEO_RESET);
	it6505_set_bits(it6505, REG_501_FIFO_CTRL, RST_501_FIFO, RST_501_FIFO);
	it6505_set_bits(it6505, REG_501_FIFO_CTRL, RST_501_FIFO, 0x00);
	it6505_set_bits(it6505, REG_RESET_CTRL, VIDEO_RESET, 0x00);
}

static void it6505_update_video_parameter(struct it6505 *it6505,
					  const struct drm_display_mode *mode)
{
	it6505_clk_phase_adjustment(it6505, mode);
	it6505_video_disable(it6505);
}

static bool it6505_audio_input(struct it6505 *it6505)
{
	int reg05, regbe;

	reg05 = it6505_read(it6505, REG_RESET_CTRL);
	it6505_set_bits(it6505, REG_RESET_CTRL, AUDIO_RESET, 0x00);
	usleep_range(3000, 4000);
	regbe = it6505_read(it6505, REG_AUDIO_INPUT_FREQ);
	it6505_write(it6505, REG_RESET_CTRL, reg05);

	return regbe != 0xFF;
}

static void it6505_setup_audio_channel_status(struct it6505 *it6505)
{
	enum it6505_audio_sample_rate sample_rate = it6505->audio.sample_rate;
	u8 audio_word_length_map[] = { 0x02, 0x04, 0x03, 0x0B };

	/* Channel Status */
	it6505_write(it6505, REG_IEC958_STS0, it6505->audio.type << 1);
	it6505_write(it6505, REG_IEC958_STS1, 0x00);
	it6505_write(it6505, REG_IEC958_STS2, 0x00);
	it6505_write(it6505, REG_IEC958_STS3, sample_rate);
	it6505_write(it6505, REG_IEC958_STS4, (~sample_rate << 4) |
		     audio_word_length_map[it6505->audio.word_length]);
}

static void it6505_setup_audio_format(struct it6505 *it6505)
{
	/* I2S MODE */
	it6505_write(it6505, REG_AUDIO_FMT,
		     (it6505->audio.word_length << 5) |
		     (it6505->audio.i2s_data_sequence << 4) |
		     (it6505->audio.i2s_ws_channel << 3) |
		     (it6505->audio.i2s_data_delay << 2) |
		     (it6505->audio.i2s_justified << 1) |
		     it6505->audio.i2s_input_format);
	if (it6505->audio.select == SPDIF) {
		it6505_write(it6505, REG_AUDIO_FIFO_SEL, 0x00);
		/* 0x30 = 128*FS */
		it6505_set_bits(it6505, REG_AUX_OPT, 0xF0, 0x30);
	} else {
		it6505_write(it6505, REG_AUDIO_FIFO_SEL, 0xE4);
	}

	it6505_write(it6505, REG_AUDIO_CTRL0, 0x20);
	it6505_write(it6505, REG_AUDIO_CTRL1, 0x00);
}

static void it6505_enable_audio_source(struct it6505 *it6505)
{
	unsigned int audio_source_count;

	audio_source_count = BIT(DIV_ROUND_UP(it6505->audio.channel_count, 2))
				 - 1;

	audio_source_count |= it6505->audio.select << 4;

	it6505_write(it6505, REG_AUDIO_SRC_CTRL, audio_source_count);
}

static void it6505_enable_audio_infoframe(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	u8 audio_info_ca[] = { 0x00, 0x00, 0x01, 0x03, 0x07, 0x0B, 0x0F, 0x1F };

	DRM_DEV_DEBUG_DRIVER(dev, "infoframe channel_allocation:0x%02x",
			     audio_info_ca[it6505->audio.channel_count - 1]);

	it6505_write(it6505, REG_AUD_INFOFRAM_DB1, it6505->audio.channel_count
		     - 1);
	it6505_write(it6505, REG_AUD_INFOFRAM_DB2, 0x00);
	it6505_write(it6505, REG_AUD_INFOFRAM_DB3,
		     audio_info_ca[it6505->audio.channel_count - 1]);
	it6505_write(it6505, REG_AUD_INFOFRAM_DB4, 0x00);
	it6505_write(it6505, REG_AUD_INFOFRAM_SUM, 0x00);

	/* Enable Audio InfoFrame */
	it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_AUD_CTRL_PKT,
			EN_AUD_CTRL_PKT);
}

static void it6505_disable_audio(struct it6505 *it6505)
{
	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_AUD_MUTE, EN_AUD_MUTE);
	it6505_set_bits(it6505, REG_AUDIO_SRC_CTRL, M_AUDIO_I2S_EN, 0x00);
	it6505_set_bits(it6505, REG_INFOFRAME_CTRL, EN_AUD_CTRL_PKT, 0x00);
	it6505_set_bits(it6505, REG_RESET_CTRL, AUDIO_RESET, AUDIO_RESET);
}

static void it6505_enable_audio(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	int regbe;

	DRM_DEV_DEBUG_DRIVER(dev, "start");
	it6505_disable_audio(it6505);

	it6505_setup_audio_channel_status(it6505);
	it6505_setup_audio_format(it6505);
	it6505_enable_audio_source(it6505);
	it6505_enable_audio_infoframe(it6505);

	it6505_write(it6505, REG_AUDIO_N_0_7, 0x00);
	it6505_write(it6505, REG_AUDIO_N_8_15, 0x80);
	it6505_write(it6505, REG_AUDIO_N_16_23, 0x00);

	it6505_set_bits(it6505, REG_AUDIO_SRC_CTRL, AUDIO_FIFO_RESET,
			AUDIO_FIFO_RESET);
	it6505_set_bits(it6505, REG_AUDIO_SRC_CTRL, AUDIO_FIFO_RESET, 0x00);
	it6505_set_bits(it6505, REG_RESET_CTRL, AUDIO_RESET, 0x00);
	regbe = it6505_read(it6505, REG_AUDIO_INPUT_FREQ);
	DRM_DEV_DEBUG_DRIVER(dev, "regbe:0x%02x audio input fs: %d.%d kHz",
			     regbe, 6750 / regbe, (6750 % regbe) * 10 / regbe);
	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_AUD_MUTE, 0x00);
}

static bool it6505_use_step_train_check(struct it6505 *it6505)
{
	if (it6505->link.revision >= 0x12)
		return it6505->dpcd[DP_TRAINING_AUX_RD_INTERVAL] >= 0x01;

	return true;
}

static void it6505_parse_link_capabilities(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	struct it6505_drm_dp_link *link = &it6505->link;
	int bcaps;

	if (it6505->dpcd[0] == 0) {
		dev_err(dev, "DPCD is not initialized");
		return;
	}

	memset(link, 0, sizeof(*link));

	link->revision = it6505->dpcd[0];
	link->rate = drm_dp_bw_code_to_link_rate(it6505->dpcd[1]);
	link->num_lanes = it6505->dpcd[2] & DP_MAX_LANE_COUNT_MASK;

	if (it6505->dpcd[2] & DP_ENHANCED_FRAME_CAP)
		link->capabilities = DP_ENHANCED_FRAME_CAP;

	DRM_DEV_DEBUG_DRIVER(dev, "DPCD Rev.: %d.%d",
			     link->revision >> 4, link->revision & 0x0F);

	DRM_DEV_DEBUG_DRIVER(dev, "Sink max link rate: %d.%02d Gbps per lane",
			     link->rate / 100000, link->rate / 1000 % 100);

	it6505->link_rate_bw_code = drm_dp_link_rate_to_bw_code(link->rate);
	DRM_DEV_DEBUG_DRIVER(dev, "link rate bw code:0x%02x",
			     it6505->link_rate_bw_code);
	it6505->link_rate_bw_code = min_t(int, it6505->link_rate_bw_code,
					  MAX_LINK_RATE);

	it6505->lane_count = link->num_lanes;
	DRM_DEV_DEBUG_DRIVER(dev, "Sink support %d lanes training",
			     it6505->lane_count);
	it6505->lane_count = min_t(int, it6505->lane_count,
				   it6505->max_lane_count);

	it6505->branch_device = drm_dp_is_branch(it6505->dpcd);
	DRM_DEV_DEBUG_DRIVER(dev, "Sink %sbranch device",
			     it6505->branch_device ? "" : "Not ");

	it6505->enable_enhanced_frame = link->capabilities;
	DRM_DEV_DEBUG_DRIVER(dev, "Sink %sSupport Enhanced Framing",
			     it6505->enable_enhanced_frame ? "" : "Not ");

	it6505->enable_ssc = (it6505->dpcd[DP_MAX_DOWNSPREAD] &
				DP_MAX_DOWNSPREAD_0_5);
	DRM_DEV_DEBUG_DRIVER(dev, "Maximum Down-Spread: %s, %ssupport SSC!",
			     it6505->enable_ssc ? "0.5" : "0",
			     it6505->enable_ssc ? "" : "Not ");

	it6505->step_train = it6505_use_step_train_check(it6505);
	if (it6505->step_train)
		DRM_DEV_DEBUG_DRIVER(dev, "auto train fail, will step train");

	bcaps = it6505_dpcd_read(it6505, DP_AUX_HDCP_BCAPS);
	DRM_DEV_DEBUG_DRIVER(dev, "bcaps:0x%02x", bcaps);
	if (bcaps & DP_BCAPS_HDCP_CAPABLE) {
		it6505->is_repeater = (bcaps & DP_BCAPS_REPEATER_PRESENT);
		DRM_DEV_DEBUG_DRIVER(dev, "Support HDCP! Downstream is %s!",
				     it6505->is_repeater ? "repeater" :
				     "receiver");
	} else {
		DRM_DEV_DEBUG_DRIVER(dev, "Sink not support HDCP!");
		it6505->hdcp_desired = false;
	}
	DRM_DEV_DEBUG_DRIVER(dev, "HDCP %s",
			     it6505->hdcp_desired ? "desired" : "undesired");
}

static void it6505_setup_ssc(struct it6505 *it6505)
{
	it6505_set_bits(it6505, REG_TRAIN_CTRL0, SPREAD_AMP_5,
			it6505->enable_ssc ? SPREAD_AMP_5 : 0x00);
	if (it6505->enable_ssc) {
		it6505_write(it6505, REG_SSC_CTRL0, 0x9E);
		it6505_write(it6505, REG_SSC_CTRL1, 0x1C);
		it6505_write(it6505, REG_SSC_CTRL2, 0x42);
		it6505_write(it6505, REG_SP_CTRL0, 0x07);
		it6505_write(it6505, REG_IP_CTRL1, 0x29);
		it6505_write(it6505, REG_IP_CTRL2, 0x03);
		/* Stamp Interrupt Step */
		it6505_set_bits(it6505, REG_TIME_STMP_CTRL, M_STAMP_STEP,
				0x10);
		it6505_dpcd_write(it6505, DP_DOWNSPREAD_CTRL,
				  DP_SPREAD_AMP_0_5);
	} else {
		it6505_dpcd_write(it6505, DP_DOWNSPREAD_CTRL, 0x00);
		it6505_set_bits(it6505, REG_TIME_STMP_CTRL, M_STAMP_STEP,
				0x00);
	}
}

static inline void it6505_link_rate_setup(struct it6505 *it6505)
{
	it6505_set_bits(it6505, REG_TRAIN_CTRL0, FORCE_LBR,
			(it6505->link_rate_bw_code == RBR) ? FORCE_LBR : 0x00);
	it6505_set_bits(it6505, REG_LINK_DRV, DRV_HS,
			(it6505->link_rate_bw_code == RBR) ? 0x00 : DRV_HS);
}

static void it6505_lane_count_setup(struct it6505 *it6505)
{
	it6505_get_extcon_property(it6505);
	it6505_set_bits(it6505, REG_TRAIN_CTRL0, LANE_SWAP,
			it6505->lane_swap ? LANE_SWAP : 0x00);
	it6505_set_bits(it6505, REG_TRAIN_CTRL0, LANE_COUNT_MASK,
			(it6505->lane_count - 1) << 1);
}

static void it6505_link_training_setup(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	if (it6505->enable_enhanced_frame)
		it6505_set_bits(it6505, REG_DATA_MUTE_CTRL,
				ENABLE_ENHANCED_FRAME, ENABLE_ENHANCED_FRAME);

	it6505_link_rate_setup(it6505);
	it6505_lane_count_setup(it6505);
	it6505_setup_ssc(it6505);
	DRM_DEV_DEBUG_DRIVER(dev,
			     "%s, %d lanes, %sable ssc, %sable enhanced frame",
			     it6505->link_rate_bw_code != RBR ? "HBR" : "RBR",
			     it6505->lane_count,
			     it6505->enable_ssc ? "en" : "dis",
			     it6505->enable_enhanced_frame ? "en" : "dis");
}

static bool it6505_link_start_auto_train(struct it6505 *it6505)
{
	int timeout = 500, link_training_state;
	bool state = false;

	mutex_lock(&it6505->aux_lock);
	it6505_set_bits(it6505, REG_TRAIN_CTRL0,
			FORCE_CR_DONE | FORCE_EQ_DONE, 0x00);
	it6505_write(it6505, REG_TRAIN_CTRL1, FORCE_RETRAIN);
	it6505_write(it6505, REG_TRAIN_CTRL1, AUTO_TRAIN);

	while (timeout > 0) {
		usleep_range(1000, 2000);
		link_training_state = it6505_read(it6505, REG_LINK_TRAIN_STS);

		if (link_training_state > 0 &&
		    (link_training_state & LINK_STATE_NORP)) {
			state = true;
			goto unlock;
		}

		timeout--;
	}
unlock:
	mutex_unlock(&it6505->aux_lock);

	return state;
}

static int it6505_drm_dp_link_configure(struct it6505 *it6505)
{
	u8 values[2];
	int err;
	struct drm_dp_aux *aux = &it6505->aux;

	values[0] = it6505->link_rate_bw_code;
	values[1] = it6505->lane_count;

	if (it6505->enable_enhanced_frame)
		values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;

	err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
	if (err < 0)
		return err;

	return 0;
}

static bool it6505_check_voltage_swing_max(u8 lane_voltage_swing_pre_emphasis)
{
	return ((lane_voltage_swing_pre_emphasis & 0x03) == MAX_CR_LEVEL);
}

static bool it6505_check_pre_emphasis_max(u8 lane_voltage_swing_pre_emphasis)
{
	return ((lane_voltage_swing_pre_emphasis & 0x03) == MAX_EQ_LEVEL);
}

static bool it6505_check_max_voltage_swing_reached(u8 *lane_voltage_swing,
						   u8 lane_count)
{
	u8 i;

	for (i = 0; i < lane_count; i++) {
		if (lane_voltage_swing[i] & DP_TRAIN_MAX_SWING_REACHED)
			return true;
	}

	return false;
}

static bool
step_train_lane_voltage_para_set(struct it6505 *it6505,
				 struct it6505_step_train_para
				 *lane_voltage_pre_emphasis,
				 u8 *lane_voltage_pre_emphasis_set)
{
	u8 *voltage_swing = lane_voltage_pre_emphasis->voltage_swing;
	u8 *pre_emphasis = lane_voltage_pre_emphasis->pre_emphasis;
	u8 i;

	for (i = 0; i < it6505->lane_count; i++) {
		voltage_swing[i] &= 0x03;
		lane_voltage_pre_emphasis_set[i] = voltage_swing[i];
		if (it6505_check_voltage_swing_max(voltage_swing[i]))
			lane_voltage_pre_emphasis_set[i] |=
				DP_TRAIN_MAX_SWING_REACHED;

		pre_emphasis[i] &= 0x03;
		lane_voltage_pre_emphasis_set[i] |= pre_emphasis[i]
			<< DP_TRAIN_PRE_EMPHASIS_SHIFT;
		if (it6505_check_pre_emphasis_max(pre_emphasis[i]))
			lane_voltage_pre_emphasis_set[i] |=
				DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
		it6505_dpcd_write(it6505, DP_TRAINING_LANE0_SET + i,
				  lane_voltage_pre_emphasis_set[i]);

		if (lane_voltage_pre_emphasis_set[i] !=
		    it6505_dpcd_read(it6505, DP_TRAINING_LANE0_SET + i))
			return false;
	}

	return true;
}

static bool
it6505_step_cr_train(struct it6505 *it6505,
		     struct it6505_step_train_para *lane_voltage_pre_emphasis)
{
	u8 loop_count = 0, i = 0, j;
	u8 link_status[DP_LINK_STATUS_SIZE] = { 0 };
	u8 lane_level_config[MAX_LANE_COUNT] = { 0 };
	int pre_emphasis_adjust = -1, voltage_swing_adjust = -1;
	const struct drm_dp_aux *aux = &it6505->aux;

	it6505_dpcd_write(it6505, DP_DOWNSPREAD_CTRL,
			  it6505->enable_ssc ? DP_SPREAD_AMP_0_5 : 0x00);
	it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
			  DP_TRAINING_PATTERN_1);

	while (loop_count < 5 && i < 10) {
		i++;
		if (!step_train_lane_voltage_para_set(it6505,
						      lane_voltage_pre_emphasis,
						      lane_level_config))
			continue;
		drm_dp_link_train_clock_recovery_delay(aux, it6505->dpcd);
		drm_dp_dpcd_read_link_status(&it6505->aux, link_status);

		if (drm_dp_clock_recovery_ok(link_status, it6505->lane_count)) {
			it6505_set_bits(it6505, REG_TRAIN_CTRL0, FORCE_CR_DONE,
					FORCE_CR_DONE);
			return true;
		}
		DRM_DEV_DEBUG_DRIVER(&it6505->client->dev, "cr not done");

		if (it6505_check_max_voltage_swing_reached(lane_level_config,
							   it6505->lane_count))
			goto cr_train_fail;

		for (j = 0; j < it6505->lane_count; j++) {
			lane_voltage_pre_emphasis->voltage_swing[j] =
				drm_dp_get_adjust_request_voltage(link_status,
								  j) >>
				DP_TRAIN_VOLTAGE_SWING_SHIFT;
			lane_voltage_pre_emphasis->pre_emphasis[j] =
			drm_dp_get_adjust_request_pre_emphasis(link_status,
							       j) >>
					DP_TRAIN_PRE_EMPHASIS_SHIFT;
			if (voltage_swing_adjust ==
			     lane_voltage_pre_emphasis->voltage_swing[j] &&
			    pre_emphasis_adjust ==
			     lane_voltage_pre_emphasis->pre_emphasis[j]) {
				loop_count++;
				continue;
			}

			voltage_swing_adjust =
				lane_voltage_pre_emphasis->voltage_swing[j];
			pre_emphasis_adjust =
				lane_voltage_pre_emphasis->pre_emphasis[j];
			loop_count = 0;

			if (voltage_swing_adjust + pre_emphasis_adjust >
			    MAX_EQ_LEVEL)
				lane_voltage_pre_emphasis->voltage_swing[j] =
					MAX_EQ_LEVEL -
					lane_voltage_pre_emphasis
						->pre_emphasis[j];
		}
	}

cr_train_fail:
	it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
			  DP_TRAINING_PATTERN_DISABLE);

	return false;
}

static bool
it6505_step_eq_train(struct it6505 *it6505,
		     struct it6505_step_train_para *lane_voltage_pre_emphasis)
{
	u8 loop_count = 0, i, link_status[DP_LINK_STATUS_SIZE] = { 0 };
	u8 lane_level_config[MAX_LANE_COUNT] = { 0 };
	const struct drm_dp_aux *aux = &it6505->aux;

	it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
			  DP_TRAINING_PATTERN_2);

	while (loop_count < 6) {
		loop_count++;

		if (!step_train_lane_voltage_para_set(it6505,
						      lane_voltage_pre_emphasis,
						      lane_level_config))
			continue;

		drm_dp_link_train_channel_eq_delay(aux, it6505->dpcd);
		drm_dp_dpcd_read_link_status(&it6505->aux, link_status);

		if (!drm_dp_clock_recovery_ok(link_status, it6505->lane_count))
			goto eq_train_fail;

		if (drm_dp_channel_eq_ok(link_status, it6505->lane_count)) {
			it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
					  DP_TRAINING_PATTERN_DISABLE);
			it6505_set_bits(it6505, REG_TRAIN_CTRL0, FORCE_EQ_DONE,
					FORCE_EQ_DONE);
			return true;
		}
		DRM_DEV_DEBUG_DRIVER(&it6505->client->dev, "eq not done");

		for (i = 0; i < it6505->lane_count; i++) {
			lane_voltage_pre_emphasis->voltage_swing[i] =
				drm_dp_get_adjust_request_voltage(link_status,
								  i) >>
				DP_TRAIN_VOLTAGE_SWING_SHIFT;
			lane_voltage_pre_emphasis->pre_emphasis[i] =
			drm_dp_get_adjust_request_pre_emphasis(link_status,
							       i) >>
					DP_TRAIN_PRE_EMPHASIS_SHIFT;

			if (lane_voltage_pre_emphasis->voltage_swing[i] +
				    lane_voltage_pre_emphasis->pre_emphasis[i] >
			    MAX_EQ_LEVEL)
				lane_voltage_pre_emphasis->voltage_swing[i] =
					0x03 - lane_voltage_pre_emphasis
						       ->pre_emphasis[i];
		}
	}

eq_train_fail:
	it6505_dpcd_write(it6505, DP_TRAINING_PATTERN_SET,
			  DP_TRAINING_PATTERN_DISABLE);
	return false;
}

static bool it6505_link_start_step_train(struct it6505 *it6505)
{
	int err;
	struct it6505_step_train_para lane_voltage_pre_emphasis = {
		.voltage_swing = { 0 },
		.pre_emphasis = { 0 },
	};

	DRM_DEV_DEBUG_DRIVER(&it6505->client->dev, "start");
	err = it6505_drm_dp_link_configure(it6505);

	if (err < 0)
		return false;
	if (!it6505_step_cr_train(it6505, &lane_voltage_pre_emphasis))
		return false;
	if (!it6505_step_eq_train(it6505, &lane_voltage_pre_emphasis))
		return false;
	return true;
}

static bool it6505_get_video_status(struct it6505 *it6505)
{
	int reg_0d;

	reg_0d = it6505_read(it6505, REG_SYSTEM_STS);

	if (reg_0d < 0)
		return false;

	return reg_0d & VIDEO_STB;
}

static void it6505_reset_hdcp(struct it6505 *it6505)
{
	it6505->hdcp_status = HDCP_AUTH_IDLE;
	/* Disable CP_Desired */
	it6505_set_bits(it6505, REG_HDCP_CTRL1, HDCP_CP_ENABLE, 0x00);
	it6505_set_bits(it6505, REG_RESET_CTRL, HDCP_RESET, HDCP_RESET);
}

static void it6505_start_hdcp(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "start");
	it6505_reset_hdcp(it6505);
	queue_delayed_work(system_wq, &it6505->hdcp_work,
			   msecs_to_jiffies(2400));
}

static void it6505_stop_hdcp(struct it6505 *it6505)
{
	it6505_reset_hdcp(it6505);
	cancel_delayed_work(&it6505->hdcp_work);
}

static bool it6505_hdcp_is_ksv_valid(u8 *ksv)
{
	int i, ones = 0;

	/* KSV has 20 1's and 20 0's */
	for (i = 0; i < DRM_HDCP_KSV_LEN; i++)
		ones += hweight8(ksv[i]);
	if (ones != 20)
		return false;
	return true;
}

static void it6505_hdcp_part1_auth(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	u8 hdcp_bcaps;

	it6505_set_bits(it6505, REG_RESET_CTRL, HDCP_RESET, 0x00);
	/* Disable CP_Desired */
	it6505_set_bits(it6505, REG_HDCP_CTRL1, HDCP_CP_ENABLE, 0x00);

	usleep_range(1000, 1500);
	hdcp_bcaps = it6505_dpcd_read(it6505, DP_AUX_HDCP_BCAPS);
	DRM_DEV_DEBUG_DRIVER(dev, "DPCD[0x68028]: 0x%02x",
			     hdcp_bcaps);

	if (!hdcp_bcaps)
		return;

	/* clear the repeater List Chk Done and fail bit */
	it6505_set_bits(it6505, REG_HDCP_TRIGGER,
			HDCP_TRIGGER_KSV_DONE | HDCP_TRIGGER_KSV_FAIL,
			0x00);

	/* Enable An Generator */
	it6505_set_bits(it6505, REG_HDCP_CTRL2, HDCP_AN_GEN, HDCP_AN_GEN);
	/* delay1ms(10);*/
	usleep_range(10000, 15000);
	/* Stop An Generator */
	it6505_set_bits(it6505, REG_HDCP_CTRL2, HDCP_AN_GEN, 0x00);

	it6505_set_bits(it6505, REG_HDCP_CTRL1, HDCP_CP_ENABLE, HDCP_CP_ENABLE);

	it6505_set_bits(it6505, REG_HDCP_TRIGGER, HDCP_TRIGGER_START,
			HDCP_TRIGGER_START);

	it6505->hdcp_status = HDCP_AUTH_GOING;
}

static int it6505_sha1_digest(struct it6505 *it6505, u8 *sha1_input,
			      unsigned int size, u8 *output_av)
{
	struct shash_desc *desc;
	struct crypto_shash *tfm;
	int err;
	struct device *dev = &it6505->client->dev;

	tfm = crypto_alloc_shash("sha1", 0, 0);
	if (IS_ERR(tfm)) {
		dev_err(dev, "crypto_alloc_shash sha1 failed");
		return PTR_ERR(tfm);
	}
	desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
	if (!desc) {
		crypto_free_shash(tfm);
		return -ENOMEM;
	}

	desc->tfm = tfm;
	err = crypto_shash_digest(desc, sha1_input, size, output_av);
	if (err)
		dev_err(dev, "crypto_shash_digest sha1 failed");

	crypto_free_shash(tfm);
	kfree(desc);
	return err;
}

static int it6505_setup_sha1_input(struct it6505 *it6505, u8 *sha1_input)
{
	struct device *dev = &it6505->client->dev;
	u8 binfo[2];
	int down_stream_count, i, err, msg_count = 0;

	err = it6505_get_dpcd(it6505, DP_AUX_HDCP_BINFO, binfo,
			      ARRAY_SIZE(binfo));

	if (err < 0) {
		dev_err(dev, "Read binfo value Fail");
		return err;
	}

	down_stream_count = binfo[0] & 0x7F;
	DRM_DEV_DEBUG_DRIVER(dev, "binfo:0x%*ph", (int)ARRAY_SIZE(binfo),
			     binfo);

	if ((binfo[0] & BIT(7)) || (binfo[1] & BIT(3))) {
		dev_err(dev, "HDCP max cascade device exceed");
		return 0;
	}

	if (!down_stream_count ||
	    down_stream_count > MAX_HDCP_DOWN_STREAM_COUNT) {
		dev_err(dev, "HDCP down stream count Error %d",
			down_stream_count);
		return 0;
	}

	for (i = 0; i < down_stream_count; i++) {
		err = it6505_get_dpcd(it6505, DP_AUX_HDCP_KSV_FIFO +
				      (i % 3) * DRM_HDCP_KSV_LEN,
				      sha1_input + msg_count,
				      DRM_HDCP_KSV_LEN);

		if (err < 0)
			return err;

		msg_count += 5;
	}

	it6505->hdcp_down_stream_count = down_stream_count;
	sha1_input[msg_count++] = binfo[0];
	sha1_input[msg_count++] = binfo[1];

	it6505_set_bits(it6505, REG_HDCP_CTRL2, HDCP_EN_M0_READ,
			HDCP_EN_M0_READ);

	err = regmap_bulk_read(it6505->regmap, REG_M0_0_7,
			       sha1_input + msg_count, 8);

	it6505_set_bits(it6505, REG_HDCP_CTRL2, HDCP_EN_M0_READ, 0x00);

	if (err < 0) {
		dev_err(dev, " Warning, Read M value Fail");
		return err;
	}

	msg_count += 8;

	return msg_count;
}

static bool it6505_hdcp_part2_ksvlist_check(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	u8 av[5][4], bv[5][4];
	int i, err;

	i = it6505_setup_sha1_input(it6505, it6505->sha1_input);
	if (i <= 0) {
		dev_err(dev, "SHA-1 Input length error %d", i);
		return false;
	}

	it6505_sha1_digest(it6505, it6505->sha1_input, i, (u8 *)av);

	err = it6505_get_dpcd(it6505, DP_AUX_HDCP_V_PRIME(0), (u8 *)bv,
			      sizeof(bv));

	if (err < 0) {
		dev_err(dev, "Read V' value Fail");
		return false;
	}

	for (i = 0; i < 5; i++)
		if (bv[i][3] != av[i][0] || bv[i][2] != av[i][1] ||
		    bv[i][1] != av[i][2] || bv[i][0] != av[i][3])
			return false;

	DRM_DEV_DEBUG_DRIVER(dev, "V' all match!!");
	return true;
}

static void it6505_hdcp_wait_ksv_list(struct work_struct *work)
{
	struct it6505 *it6505 = container_of(work, struct it6505,
					     hdcp_wait_ksv_list);
	struct device *dev = &it6505->client->dev;
	unsigned int timeout = 5000;
	u8 bstatus = 0;
	bool ksv_list_check;

	timeout /= 20;
	while (timeout > 0) {
		if (!it6505_get_sink_hpd_status(it6505))
			return;

		bstatus = it6505_dpcd_read(it6505, DP_AUX_HDCP_BSTATUS);

		if (bstatus & DP_BSTATUS_READY)
			break;

		msleep(20);
		timeout--;
	}

	if (timeout == 0) {
		DRM_DEV_DEBUG_DRIVER(dev, "timeout and ksv list wait failed");
		goto timeout;
	}

	ksv_list_check = it6505_hdcp_part2_ksvlist_check(it6505);
	DRM_DEV_DEBUG_DRIVER(dev, "ksv list ready, ksv list check %s",
			     ksv_list_check ? "pass" : "fail");
	if (ksv_list_check) {
		it6505_set_bits(it6505, REG_HDCP_TRIGGER,
				HDCP_TRIGGER_KSV_DONE, HDCP_TRIGGER_KSV_DONE);
		return;
	}
timeout:
	it6505_set_bits(it6505, REG_HDCP_TRIGGER,
			HDCP_TRIGGER_KSV_DONE | HDCP_TRIGGER_KSV_FAIL,
			HDCP_TRIGGER_KSV_DONE | HDCP_TRIGGER_KSV_FAIL);
}

static void it6505_hdcp_work(struct work_struct *work)
{
	struct it6505 *it6505 = container_of(work, struct it6505,
					     hdcp_work.work);
	struct device *dev = &it6505->client->dev;
	int ret;
	u8 link_status[DP_LINK_STATUS_SIZE] = { 0 };

	DRM_DEV_DEBUG_DRIVER(dev, "start");

	if (!it6505_get_sink_hpd_status(it6505))
		return;

	ret = drm_dp_dpcd_read_link_status(&it6505->aux, link_status);
	DRM_DEV_DEBUG_DRIVER(dev, "ret: %d link_status: %*ph", ret,
			     (int)sizeof(link_status), link_status);

	if (ret < 0 || !drm_dp_channel_eq_ok(link_status, it6505->lane_count) ||
	    !it6505_get_video_status(it6505)) {
		DRM_DEV_DEBUG_DRIVER(dev, "link train not done or no video");
		return;
	}

	ret = it6505_get_dpcd(it6505, DP_AUX_HDCP_BKSV, it6505->bksvs,
			      ARRAY_SIZE(it6505->bksvs));
	if (ret < 0) {
		dev_err(dev, "fail to get bksv  ret: %d", ret);
		it6505_set_bits(it6505, REG_HDCP_TRIGGER,
				HDCP_TRIGGER_KSV_FAIL, HDCP_TRIGGER_KSV_FAIL);
	}

	DRM_DEV_DEBUG_DRIVER(dev, "bksv = 0x%*ph",
			     (int)ARRAY_SIZE(it6505->bksvs), it6505->bksvs);

	if (!it6505_hdcp_is_ksv_valid(it6505->bksvs)) {
		dev_err(dev, "Display Port bksv not valid");
		it6505_set_bits(it6505, REG_HDCP_TRIGGER,
				HDCP_TRIGGER_KSV_FAIL, HDCP_TRIGGER_KSV_FAIL);
	}

	it6505_hdcp_part1_auth(it6505);
}

static void it6505_show_hdcp_info(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	int i;
	u8 *sha1 = it6505->sha1_input;

	DRM_DEV_DEBUG_DRIVER(dev, "hdcp_status: %d is_repeater: %d",
			     it6505->hdcp_status, it6505->is_repeater);
	DRM_DEV_DEBUG_DRIVER(dev, "bksv = 0x%*ph",
			     (int)ARRAY_SIZE(it6505->bksvs), it6505->bksvs);

	if (it6505->is_repeater) {
		DRM_DEV_DEBUG_DRIVER(dev, "hdcp_down_stream_count: %d",
				     it6505->hdcp_down_stream_count);
		DRM_DEV_DEBUG_DRIVER(dev, "sha1_input: 0x%*ph",
				     (int)ARRAY_SIZE(it6505->sha1_input),
				     it6505->sha1_input);
		for (i = 0; i < it6505->hdcp_down_stream_count; i++) {
			DRM_DEV_DEBUG_DRIVER(dev, "KSV_%d = 0x%*ph", i,
					     DRM_HDCP_KSV_LEN, sha1);
			sha1 += DRM_HDCP_KSV_LEN;
		}
		DRM_DEV_DEBUG_DRIVER(dev, "binfo: 0x%2ph M0: 0x%8ph",
				     sha1, sha1 + 2);
	}
}

static void it6505_stop_link_train(struct it6505 *it6505)
{
	it6505->link_state = LINK_IDLE;
	cancel_work_sync(&it6505->link_works);
	it6505_write(it6505, REG_TRAIN_CTRL1, FORCE_RETRAIN);
}

static void it6505_link_train_ok(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	it6505->link_state = LINK_OK;
	/* disalbe mute enable avi info frame */
	it6505_set_bits(it6505, REG_DATA_MUTE_CTRL, EN_VID_MUTE, 0x00);
	it6505_set_bits(it6505, REG_INFOFRAME_CTRL,
			EN_VID_CTRL_PKT, EN_VID_CTRL_PKT);

	if (it6505_audio_input(it6505)) {
		DRM_DEV_DEBUG_DRIVER(dev, "Enable audio!");
		it6505_enable_audio(it6505);
	}

	if (it6505->hdcp_desired)
		it6505_start_hdcp(it6505);
}

static void it6505_link_step_train_process(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	int ret, i, step_retry = 3;

	DRM_DEV_DEBUG_DRIVER(dev, "Start step train");

	if (it6505->sink_count == 0) {
		DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count:%d, force eq",
				     it6505->sink_count);
		it6505_set_bits(it6505,	REG_TRAIN_CTRL0, FORCE_EQ_DONE,
				FORCE_EQ_DONE);
		return;
	}

	if (!it6505->step_train) {
		DRM_DEV_DEBUG_DRIVER(dev, "not support step train");
		return;
	}

	/* step training start here */
	for (i = 0; i < step_retry; i++) {
		it6505_link_reset_step_train(it6505);
		ret = it6505_link_start_step_train(it6505);
		DRM_DEV_DEBUG_DRIVER(dev, "step train %s, retry:%d times",
				     ret ? "pass" : "failed", i + 1);
		if (ret) {
			it6505_link_train_ok(it6505);
			return;
		}
	}

	DRM_DEV_DEBUG_DRIVER(dev, "training fail");
	it6505->link_state = LINK_IDLE;
	it6505_video_reset(it6505);
}

static void it6505_link_training_work(struct work_struct *work)
{
	struct it6505 *it6505 = container_of(work, struct it6505, link_works);
	struct device *dev = &it6505->client->dev;
	int ret;

	DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count: %d",
			     it6505->sink_count);

	if (!it6505_get_sink_hpd_status(it6505))
		return;

	it6505_link_training_setup(it6505);
	it6505_reset_hdcp(it6505);
	it6505_aux_reset(it6505);

	if (it6505->auto_train_retry < 1) {
		it6505_link_step_train_process(it6505);
		return;
	}

	ret = it6505_link_start_auto_train(it6505);
	DRM_DEV_DEBUG_DRIVER(dev, "auto train %s, auto_train_retry: %d",
			     ret ? "pass" : "failed", it6505->auto_train_retry);
	it6505->auto_train_retry--;

	if (ret) {
		it6505_link_train_ok(it6505);
		return;
	}

	it6505_dump(it6505);
}

static void it6505_plugged_status_to_codec(struct it6505 *it6505)
{
	enum drm_connector_status status = it6505->connector_status;

	if (it6505->plugged_cb && it6505->codec_dev)
		it6505->plugged_cb(it6505->codec_dev,
				   status == connector_status_connected);
}

static void it6505_remove_edid(struct it6505 *it6505)
{
	kfree(it6505->cached_edid);
	it6505->cached_edid = NULL;
}

static int it6505_process_hpd_irq(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	int ret, dpcd_sink_count, dp_irq_vector, bstatus;
	u8 link_status[DP_LINK_STATUS_SIZE];

	if (!it6505_get_sink_hpd_status(it6505)) {
		DRM_DEV_DEBUG_DRIVER(dev, "HPD_IRQ HPD low");
		it6505->sink_count = 0;
		return 0;
	}

	ret = it6505_dpcd_read(it6505, DP_SINK_COUNT);
	if (ret < 0)
		return ret;

	dpcd_sink_count = DP_GET_SINK_COUNT(ret);
	DRM_DEV_DEBUG_DRIVER(dev, "dpcd_sink_count: %d it6505->sink_count:%d",
			     dpcd_sink_count, it6505->sink_count);

	if (it6505->branch_device && dpcd_sink_count != it6505->sink_count) {
		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
		it6505->sink_count = dpcd_sink_count;
		it6505_reset_logic(it6505);
		it6505_int_mask_enable(it6505);
		it6505_init(it6505);
		it6505_remove_edid(it6505);
		return 0;
	}

	dp_irq_vector = it6505_dpcd_read(it6505, DP_DEVICE_SERVICE_IRQ_VECTOR);
	if (dp_irq_vector < 0)
		return dp_irq_vector;

	DRM_DEV_DEBUG_DRIVER(dev, "dp_irq_vector = 0x%02x", dp_irq_vector);

	if (dp_irq_vector & DP_CP_IRQ) {
		it6505_set_bits(it6505, REG_HDCP_TRIGGER, HDCP_TRIGGER_CPIRQ,
				HDCP_TRIGGER_CPIRQ);

		bstatus = it6505_dpcd_read(it6505, DP_AUX_HDCP_BSTATUS);
		if (bstatus < 0)
			return bstatus;

		DRM_DEV_DEBUG_DRIVER(dev, "Bstatus = 0x%02x", bstatus);
	}

	ret = drm_dp_dpcd_read_link_status(&it6505->aux, link_status);
	if (ret < 0) {
		dev_err(dev, "Fail to read link status ret: %d", ret);
		return ret;
	}

	DRM_DEV_DEBUG_DRIVER(dev, "link status = 0x%*ph",
			     (int)ARRAY_SIZE(link_status), link_status);

	if (!drm_dp_channel_eq_ok(link_status, it6505->lane_count)) {
		it6505->auto_train_retry = AUTO_TRAIN_RETRY;
		it6505_video_reset(it6505);
	}

	return 0;
}

static void it6505_irq_hpd(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	int dp_sink_count;

	it6505->hpd_state = it6505_get_sink_hpd_status(it6505);
	DRM_DEV_DEBUG_DRIVER(dev, "hpd change interrupt, change to %s",
			     it6505->hpd_state ? "high" : "low");

	if (it6505->hpd_state) {
		wait_for_completion_timeout(&it6505->extcon_completion,
					    msecs_to_jiffies(1000));
		it6505_aux_on(it6505);
		if (it6505->dpcd[0] == 0) {
			it6505_get_dpcd(it6505, DP_DPCD_REV, it6505->dpcd,
					ARRAY_SIZE(it6505->dpcd));
			it6505_variable_config(it6505);
			it6505_parse_link_capabilities(it6505);
		}
		it6505->auto_train_retry = AUTO_TRAIN_RETRY;

		it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
					     DP_SET_POWER_D0);
		dp_sink_count = it6505_dpcd_read(it6505, DP_SINK_COUNT);
		it6505->sink_count = DP_GET_SINK_COUNT(dp_sink_count);

		DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count: %d",
				     it6505->sink_count);

		it6505_lane_termination_on(it6505);
		it6505_lane_power_on(it6505);

		/*
		 * for some dongle which issue HPD_irq
		 * when sink count change from  0->1
		 * it6505 not able to receive HPD_IRQ
		 * if HW never go into trainig done
		 */

		if (it6505->branch_device && it6505->sink_count == 0)
			schedule_work(&it6505->link_works);

		if (!it6505_get_video_status(it6505))
			it6505_video_reset(it6505);
	} else {
		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
		it6505_remove_edid(it6505);

		if (it6505->hdcp_desired)
			it6505_stop_hdcp(it6505);

		it6505_video_disable(it6505);
		it6505_disable_audio(it6505);
		it6505_stop_link_train(it6505);
		it6505_lane_off(it6505);
		it6505_link_reset_step_train(it6505);
	}

	if (it6505->bridge.dev)
		drm_helper_hpd_irq_event(it6505->bridge.dev);
}

static void it6505_irq_hpd_irq(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "hpd_irq interrupt");

	if (it6505_process_hpd_irq(it6505) < 0)
		DRM_DEV_DEBUG_DRIVER(dev, "process hpd_irq fail!");
}

static void it6505_irq_scdt(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	bool data;

	data = it6505_get_video_status(it6505);
	DRM_DEV_DEBUG_DRIVER(dev, "video stable change interrupt, %s",
			     data ? "stable" : "unstable");
	it6505_calc_video_info(it6505);
	it6505_link_reset_step_train(it6505);

	if (data)
		schedule_work(&it6505->link_works);
}

static void it6505_irq_hdcp_done(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "hdcp done interrupt");
	it6505->hdcp_status = HDCP_AUTH_DONE;
	it6505_show_hdcp_info(it6505);
}

static void it6505_irq_hdcp_fail(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "hdcp fail interrupt");
	it6505->hdcp_status = HDCP_AUTH_IDLE;
	it6505_show_hdcp_info(it6505);
	it6505_start_hdcp(it6505);
}

static void it6505_irq_aux_cmd_fail(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "AUX PC Request Fail Interrupt");
}

static void it6505_irq_hdcp_ksv_check(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "HDCP event Interrupt");
	schedule_work(&it6505->hdcp_wait_ksv_list);
}

static void it6505_irq_audio_fifo_error(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "audio fifo error Interrupt");

	if (it6505_audio_input(it6505))
		it6505_enable_audio(it6505);
}

static void it6505_irq_link_train_fail(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "link training fail interrupt");
	schedule_work(&it6505->link_works);
}

static void it6505_irq_video_fifo_error(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "video fifo overflow interrupt");
	it6505->auto_train_retry = AUTO_TRAIN_RETRY;
	flush_work(&it6505->link_works);
	it6505_stop_hdcp(it6505);
	it6505_video_reset(it6505);
}

static void it6505_irq_io_latch_fifo_overflow(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "IO latch fifo overflow interrupt");
	it6505->auto_train_retry = AUTO_TRAIN_RETRY;
	flush_work(&it6505->link_works);
	it6505_stop_hdcp(it6505);
	it6505_video_reset(it6505);
}

static bool it6505_test_bit(unsigned int bit, const unsigned int *addr)
{
	return 1 & (addr[bit / BITS_PER_BYTE] >> (bit % BITS_PER_BYTE));
}

static irqreturn_t it6505_int_threaded_handler(int unused, void *data)
{
	struct it6505 *it6505 = data;
	struct device *dev = &it6505->client->dev;
	static const struct {
		int bit;
		void (*handler)(struct it6505 *it6505);
	} irq_vec[] = {
		{ BIT_INT_HPD, it6505_irq_hpd },
		{ BIT_INT_HPD_IRQ, it6505_irq_hpd_irq },
		{ BIT_INT_SCDT, it6505_irq_scdt },
		{ BIT_INT_HDCP_FAIL, it6505_irq_hdcp_fail },
		{ BIT_INT_HDCP_DONE, it6505_irq_hdcp_done },
		{ BIT_INT_AUX_CMD_FAIL, it6505_irq_aux_cmd_fail },
		{ BIT_INT_HDCP_KSV_CHECK, it6505_irq_hdcp_ksv_check },
		{ BIT_INT_AUDIO_FIFO_ERROR, it6505_irq_audio_fifo_error },
		{ BIT_INT_LINK_TRAIN_FAIL, it6505_irq_link_train_fail },
		{ BIT_INT_VID_FIFO_ERROR, it6505_irq_video_fifo_error },
		{ BIT_INT_IO_FIFO_OVERFLOW, it6505_irq_io_latch_fifo_overflow },
	};
	int int_status[3], i;

	if (it6505->enable_drv_hold || !it6505->powered)
		return IRQ_HANDLED;

	pm_runtime_get_sync(dev);

	int_status[0] = it6505_read(it6505, INT_STATUS_01);
	int_status[1] = it6505_read(it6505, INT_STATUS_02);
	int_status[2] = it6505_read(it6505, INT_STATUS_03);

	it6505_write(it6505, INT_STATUS_01, int_status[0]);
	it6505_write(it6505, INT_STATUS_02, int_status[1]);
	it6505_write(it6505, INT_STATUS_03, int_status[2]);

	DRM_DEV_DEBUG_DRIVER(dev, "reg06 = 0x%02x", int_status[0]);
	DRM_DEV_DEBUG_DRIVER(dev, "reg07 = 0x%02x", int_status[1]);
	DRM_DEV_DEBUG_DRIVER(dev, "reg08 = 0x%02x", int_status[2]);
	it6505_debug_print(it6505, REG_SYSTEM_STS, "");

	if (it6505_test_bit(irq_vec[0].bit, (unsigned int *)int_status))
		irq_vec[0].handler(it6505);

	if (it6505->hpd_state) {
		for (i = 1; i < ARRAY_SIZE(irq_vec); i++) {
			if (it6505_test_bit(irq_vec[i].bit, (unsigned int *)int_status))
				irq_vec[i].handler(it6505);
		}
	}

	pm_runtime_put_sync(dev);

	return IRQ_HANDLED;
}

static int it6505_poweron(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	struct it6505_platform_data *pdata = &it6505->pdata;
	int err;

	DRM_DEV_DEBUG_DRIVER(dev, "it6505 start powered on");

	if (it6505->powered) {
		DRM_DEV_DEBUG_DRIVER(dev, "it6505 already powered on");
		return 0;
	}

	if (pdata->pwr18) {
		err = regulator_enable(pdata->pwr18);
		if (err) {
			DRM_DEV_DEBUG_DRIVER(dev, "Failed to enable VDD18: %d",
					     err);
			return err;
		}
	}

	if (pdata->ovdd) {
		/* time interval between IVDD and OVDD at least be 1ms */
		usleep_range(1000, 2000);
		err = regulator_enable(pdata->ovdd);
		if (err) {
			regulator_disable(pdata->pwr18);
			return err;
		}
	}
	/* time interval between OVDD and SYSRSTN at least be 10ms */
	if (pdata->gpiod_reset) {
		usleep_range(10000, 20000);
		gpiod_set_value_cansleep(pdata->gpiod_reset, 0);
		usleep_range(1000, 2000);
		gpiod_set_value_cansleep(pdata->gpiod_reset, 1);
		usleep_range(10000, 20000);
	}

	it6505->powered = true;
	it6505_reset_logic(it6505);
	it6505_int_mask_enable(it6505);
	it6505_init(it6505);
	it6505_lane_off(it6505);

	return 0;
}

static int it6505_poweroff(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	struct it6505_platform_data *pdata = &it6505->pdata;
	int err;

	DRM_DEV_DEBUG_DRIVER(dev, "it6505 start power off");

	if (!it6505->powered) {
		DRM_DEV_DEBUG_DRIVER(dev, "power had been already off");
		return 0;
	}

	if (pdata->gpiod_reset)
		gpiod_set_value_cansleep(pdata->gpiod_reset, 0);

	if (pdata->pwr18) {
		err = regulator_disable(pdata->pwr18);
		if (err)
			return err;
	}

	if (pdata->ovdd) {
		err = regulator_disable(pdata->ovdd);
		if (err)
			return err;
	}

	it6505->powered = false;
	it6505->sink_count = 0;

	return 0;
}

static enum drm_connector_status it6505_detect(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	enum drm_connector_status status = connector_status_disconnected;
	int dp_sink_count;

	DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count:%d powered:%d",
			     it6505->sink_count, it6505->powered);

	mutex_lock(&it6505->mode_lock);

	if (!it6505->powered)
		goto unlock;

	if (it6505->enable_drv_hold) {
		status = it6505->hpd_state ? connector_status_connected :
					     connector_status_disconnected;
		goto unlock;
	}

	if (it6505->hpd_state) {
		it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
					     DP_SET_POWER_D0);
		dp_sink_count = it6505_dpcd_read(it6505, DP_SINK_COUNT);
		it6505->sink_count = DP_GET_SINK_COUNT(dp_sink_count);
		DRM_DEV_DEBUG_DRIVER(dev, "it6505->sink_count:%d branch:%d",
				     it6505->sink_count, it6505->branch_device);

		if (it6505->branch_device) {
			status = (it6505->sink_count != 0) ?
				 connector_status_connected :
				 connector_status_disconnected;
		} else {
			status = connector_status_connected;
		}
	} else {
		it6505->sink_count = 0;
		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
	}

unlock:
	if (it6505->connector_status != status) {
		it6505->connector_status = status;
		it6505_plugged_status_to_codec(it6505);
	}

	mutex_unlock(&it6505->mode_lock);

	return status;
}

static int it6505_extcon_notifier(struct notifier_block *self,
				  unsigned long event, void *ptr)
{
	struct it6505 *it6505 = container_of(self, struct it6505, event_nb);

	schedule_work(&it6505->extcon_wq);
	return NOTIFY_DONE;
}

static void it6505_extcon_work(struct work_struct *work)
{
	struct it6505 *it6505 = container_of(work, struct it6505, extcon_wq);
	struct device *dev = &it6505->client->dev;
	int state, ret;

	if (it6505->enable_drv_hold)
		return;

	mutex_lock(&it6505->extcon_lock);

	state = extcon_get_state(it6505->extcon, EXTCON_DISP_DP);
	DRM_DEV_DEBUG_DRIVER(dev, "EXTCON_DISP_DP = 0x%02x", state);

	if (state == it6505->extcon_state || unlikely(state < 0))
		goto unlock;
	it6505->extcon_state = state;
	if (state) {
		DRM_DEV_DEBUG_DRIVER(dev, "start to power on");
		msleep(100);
		ret = pm_runtime_get_sync(dev);

		/*
		 * On system resume, extcon_work can be triggered before
		 * pm_runtime_force_resume re-enables runtime power management.
		 * Handling the error here to make sure the bridge is powered on.
		 */
		if (ret < 0)
			it6505_poweron(it6505);

		complete_all(&it6505->extcon_completion);
	} else {
		DRM_DEV_DEBUG_DRIVER(dev, "start to power off");
		pm_runtime_put_sync(dev);
		reinit_completion(&it6505->extcon_completion);

		drm_helper_hpd_irq_event(it6505->bridge.dev);
		memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
		DRM_DEV_DEBUG_DRIVER(dev, "power off it6505 success!");
	}

unlock:
	mutex_unlock(&it6505->extcon_lock);
}

static int it6505_use_notifier_module(struct it6505 *it6505)
{
	int ret;
	struct device *dev = &it6505->client->dev;

	it6505->event_nb.notifier_call = it6505_extcon_notifier;
	INIT_WORK(&it6505->extcon_wq, it6505_extcon_work);
	ret = devm_extcon_register_notifier(&it6505->client->dev,
					    it6505->extcon, EXTCON_DISP_DP,
					    &it6505->event_nb);
	if (ret) {
		dev_err(dev, "failed to register notifier for DP");
		return ret;
	}

	schedule_work(&it6505->extcon_wq);

	return 0;
}

static void it6505_remove_notifier_module(struct it6505 *it6505)
{
	if (it6505->extcon) {
		devm_extcon_unregister_notifier(&it6505->client->dev,
						it6505->extcon,	EXTCON_DISP_DP,
						&it6505->event_nb);

		flush_work(&it6505->extcon_wq);
	}
}

static void __maybe_unused it6505_delayed_audio(struct work_struct *work)
{
	struct it6505 *it6505 = container_of(work, struct it6505,
					     delayed_audio.work);

	DRM_DEV_DEBUG_DRIVER(&it6505->client->dev, "start");

	if (!it6505->powered)
		return;

	if (!it6505->enable_drv_hold)
		it6505_enable_audio(it6505);
}

static int __maybe_unused it6505_audio_setup_hw_params(struct it6505 *it6505,
						       struct hdmi_codec_params
						       *params)
{
	struct device *dev = &it6505->client->dev;
	int i = 0;

	DRM_DEV_DEBUG_DRIVER(dev, "%s %d Hz, %d bit, %d channels\n", __func__,
			     params->sample_rate, params->sample_width,
			     params->cea.channels);

	if (!it6505->bridge.encoder)
		return -ENODEV;

	if (params->cea.channels <= 1 || params->cea.channels > 8) {
		DRM_DEV_DEBUG_DRIVER(dev, "channel number: %d not support",
				     it6505->audio.channel_count);
		return -EINVAL;
	}

	it6505->audio.channel_count = params->cea.channels;

	while (i < ARRAY_SIZE(audio_sample_rate_map) &&
	       params->sample_rate !=
		       audio_sample_rate_map[i].sample_rate_value) {
		i++;
	}
	if (i == ARRAY_SIZE(audio_sample_rate_map)) {
		DRM_DEV_DEBUG_DRIVER(dev, "sample rate: %d Hz not support",
				     params->sample_rate);
		return -EINVAL;
	}
	it6505->audio.sample_rate = audio_sample_rate_map[i].rate;

	switch (params->sample_width) {
	case 16:
		it6505->audio.word_length = WORD_LENGTH_16BIT;
		break;
	case 18:
		it6505->audio.word_length = WORD_LENGTH_18BIT;
		break;
	case 20:
		it6505->audio.word_length = WORD_LENGTH_20BIT;
		break;
	case 24:
	case 32:
		it6505->audio.word_length = WORD_LENGTH_24BIT;
		break;
	default:
		DRM_DEV_DEBUG_DRIVER(dev, "wordlength: %d bit not support",
				     params->sample_width);
		return -EINVAL;
	}

	return 0;
}

static void __maybe_unused it6505_audio_shutdown(struct device *dev, void *data)
{
	struct it6505 *it6505 = dev_get_drvdata(dev);

	if (it6505->powered)
		it6505_disable_audio(it6505);
}

static int __maybe_unused it6505_audio_hook_plugged_cb(struct device *dev,
						       void *data,
						       hdmi_codec_plugged_cb fn,
						       struct device *codec_dev)
{
	struct it6505 *it6505 = data;

	it6505->plugged_cb = fn;
	it6505->codec_dev = codec_dev;
	it6505_plugged_status_to_codec(it6505);

	return 0;
}

static inline struct it6505 *bridge_to_it6505(struct drm_bridge *bridge)
{
	return container_of(bridge, struct it6505, bridge);
}

static int it6505_bridge_attach(struct drm_bridge *bridge,
				enum drm_bridge_attach_flags flags)
{
	struct it6505 *it6505 = bridge_to_it6505(bridge);
	struct device *dev = &it6505->client->dev;
	int ret;

	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
		DRM_ERROR("DRM_BRIDGE_ATTACH_NO_CONNECTOR must be supplied");
		return -EINVAL;
	}

	if (!bridge->encoder) {
		dev_err(dev, "Parent encoder object not found");
		return -ENODEV;
	}

	/* Register aux channel */
	it6505->aux.drm_dev = bridge->dev;

	ret = drm_dp_aux_register(&it6505->aux);

	if (ret < 0) {
		dev_err(dev, "Failed to register aux: %d", ret);
		return ret;
	}

	if (it6505->extcon) {
		ret = it6505_use_notifier_module(it6505);
		if (ret < 0) {
			dev_err(dev, "use notifier module failed");
			return ret;
		}
	}

	return 0;
}

static void it6505_bridge_detach(struct drm_bridge *bridge)
{
	struct it6505 *it6505 = bridge_to_it6505(bridge);

	flush_work(&it6505->link_works);
	it6505_remove_notifier_module(it6505);
}

static enum drm_mode_status
it6505_bridge_mode_valid(struct drm_bridge *bridge,
			 const struct drm_display_info *info,
			 const struct drm_display_mode *mode)
{
	struct it6505 *it6505 = bridge_to_it6505(bridge);

	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
		return MODE_NO_INTERLACE;

	if (mode->clock > it6505->max_dpi_pixel_clock)
		return MODE_CLOCK_HIGH;

	it6505->video_info.clock = mode->clock;

	return MODE_OK;
}

static void it6505_bridge_atomic_enable(struct drm_bridge *bridge,
					struct drm_bridge_state *old_state)
{
	struct it6505 *it6505 = bridge_to_it6505(bridge);
	struct device *dev = &it6505->client->dev;
	struct drm_atomic_state *state = old_state->base.state;
	struct hdmi_avi_infoframe frame;
	struct drm_crtc_state *crtc_state;
	struct drm_connector_state *conn_state;
	struct drm_display_mode *mode;
	struct drm_connector *connector;
	int ret;

	DRM_DEV_DEBUG_DRIVER(dev, "start");

	connector = drm_atomic_get_new_connector_for_encoder(state,
							     bridge->encoder);

	if (WARN_ON(!connector))
		return;

	conn_state = drm_atomic_get_new_connector_state(state, connector);

	if (WARN_ON(!conn_state))
		return;

	crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);

	if (WARN_ON(!crtc_state))
		return;

	mode = &crtc_state->adjusted_mode;

	if (WARN_ON(!mode))
		return;

	ret = drm_hdmi_avi_infoframe_from_display_mode(&frame,
						       connector,
						       mode);
	if (ret)
		dev_err(dev, "Failed to setup AVI infoframe: %d", ret);

	it6505_update_video_parameter(it6505, mode);

	ret = it6505_send_video_infoframe(it6505, &frame);

	if (ret)
		dev_err(dev, "Failed to send AVI infoframe: %d", ret);

	it6505_int_mask_enable(it6505);
	it6505_video_reset(it6505);

	it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
				     DP_SET_POWER_D0);
}

static void it6505_bridge_atomic_disable(struct drm_bridge *bridge,
					 struct drm_bridge_state *old_state)
{
	struct it6505 *it6505 = bridge_to_it6505(bridge);
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "start");

	if (it6505->powered) {
		it6505_drm_dp_link_set_power(&it6505->aux, &it6505->link,
					     DP_SET_POWER_D3);
		it6505_video_disable(it6505);
	}
}

static void it6505_bridge_atomic_pre_enable(struct drm_bridge *bridge,
					    struct drm_bridge_state *old_state)
{
	struct it6505 *it6505 = bridge_to_it6505(bridge);
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "start");

	pm_runtime_get_sync(dev);
}

static void it6505_bridge_atomic_post_disable(struct drm_bridge *bridge,
					      struct drm_bridge_state *old_state)
{
	struct it6505 *it6505 = bridge_to_it6505(bridge);
	struct device *dev = &it6505->client->dev;

	DRM_DEV_DEBUG_DRIVER(dev, "start");

	pm_runtime_put_sync(dev);
}

static enum drm_connector_status
it6505_bridge_detect(struct drm_bridge *bridge)
{
	struct it6505 *it6505 = bridge_to_it6505(bridge);

	return it6505_detect(it6505);
}

static struct edid *it6505_bridge_get_edid(struct drm_bridge *bridge,
					   struct drm_connector *connector)
{
	struct it6505 *it6505 = bridge_to_it6505(bridge);
	struct device *dev = &it6505->client->dev;

	if (!it6505->cached_edid) {
		it6505->cached_edid = drm_do_get_edid(connector, it6505_get_edid_block,
						      it6505);

		if (!it6505->cached_edid) {
			DRM_DEV_DEBUG_DRIVER(dev, "failed to get edid!");
			return NULL;
		}
	}

	return drm_edid_duplicate(it6505->cached_edid);
}

static const struct drm_bridge_funcs it6505_bridge_funcs = {
	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
	.atomic_reset = drm_atomic_helper_bridge_reset,
	.attach = it6505_bridge_attach,
	.detach = it6505_bridge_detach,
	.mode_valid = it6505_bridge_mode_valid,
	.atomic_enable = it6505_bridge_atomic_enable,
	.atomic_disable = it6505_bridge_atomic_disable,
	.atomic_pre_enable = it6505_bridge_atomic_pre_enable,
	.atomic_post_disable = it6505_bridge_atomic_post_disable,
	.detect = it6505_bridge_detect,
	.get_edid = it6505_bridge_get_edid,
};

static __maybe_unused int it6505_bridge_resume(struct device *dev)
{
	struct it6505 *it6505 = dev_get_drvdata(dev);

	return it6505_poweron(it6505);
}

static __maybe_unused int it6505_bridge_suspend(struct device *dev)
{
	struct it6505 *it6505 = dev_get_drvdata(dev);

	return it6505_poweroff(it6505);
}

static const struct dev_pm_ops it6505_bridge_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
	SET_RUNTIME_PM_OPS(it6505_bridge_suspend, it6505_bridge_resume, NULL)
};

static int it6505_init_pdata(struct it6505 *it6505)
{
	struct it6505_platform_data *pdata = &it6505->pdata;
	struct device *dev = &it6505->client->dev;

	/* 1.0V digital core power regulator  */
	pdata->pwr18 = devm_regulator_get(dev, "pwr18");
	if (IS_ERR(pdata->pwr18)) {
		dev_err(dev, "pwr18 regulator not found");
		return PTR_ERR(pdata->pwr18);
	}

	pdata->ovdd = devm_regulator_get(dev, "ovdd");
	if (IS_ERR(pdata->ovdd)) {
		dev_err(dev, "ovdd regulator not found");
		return PTR_ERR(pdata->ovdd);
	}

	pdata->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
	if (IS_ERR(pdata->gpiod_reset)) {
		dev_err(dev, "gpiod_reset gpio not found");
		return PTR_ERR(pdata->gpiod_reset);
	}

	return 0;
}

static int it6505_get_data_lanes_count(const struct device_node *endpoint,
				       const unsigned int min,
				       const unsigned int max)
{
	int ret;

	ret = of_property_count_u32_elems(endpoint, "data-lanes");
	if (ret < 0)
		return ret;

	if (ret < min || ret > max)
		return -EINVAL;

	return ret;
}

static void it6505_parse_dt(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;
	struct device_node *np = dev->of_node, *ep = NULL;
	int len;
	u64 link_frequencies;
	u32 data_lanes[4];
	u32 *afe_setting = &it6505->afe_setting;
	u32 *max_lane_count = &it6505->max_lane_count;
	u32 *max_dpi_pixel_clock = &it6505->max_dpi_pixel_clock;

	it6505->lane_swap_disabled =
		device_property_read_bool(dev, "no-laneswap");

	if (it6505->lane_swap_disabled)
		it6505->lane_swap = false;

	if (device_property_read_u32(dev, "afe-setting", afe_setting) == 0) {
		if (*afe_setting >= ARRAY_SIZE(afe_setting_table)) {
			dev_err(dev, "afe setting error, use default");
			*afe_setting = 0;
		}
	} else {
		*afe_setting = 0;
	}

	ep = of_graph_get_endpoint_by_regs(np, 1, 0);
	of_node_put(ep);

	if (ep) {
		len = it6505_get_data_lanes_count(ep, 1, 4);

		if (len > 0 && len != 3) {
			of_property_read_u32_array(ep, "data-lanes",
						   data_lanes, len);
			*max_lane_count = len;
		} else {
			*max_lane_count = MAX_LANE_COUNT;
			dev_err(dev, "error data-lanes, use default");
		}
	} else {
		*max_lane_count = MAX_LANE_COUNT;
		dev_err(dev, "error endpoint, use default");
	}

	ep = of_graph_get_endpoint_by_regs(np, 0, 0);
	of_node_put(ep);

	if (ep) {
		len = of_property_read_variable_u64_array(ep,
							  "link-frequencies",
							  &link_frequencies, 0,
							  1);
		if (len >= 0) {
			do_div(link_frequencies, 1000);
			if (link_frequencies > 297000) {
				dev_err(dev,
					"max pixel clock error, use default");
				*max_dpi_pixel_clock = DPI_PIXEL_CLK_MAX;
			} else {
				*max_dpi_pixel_clock = link_frequencies;
			}
		} else {
			dev_err(dev, "error link frequencies, use default");
			*max_dpi_pixel_clock = DPI_PIXEL_CLK_MAX;
		}
	} else {
		dev_err(dev, "error endpoint, use default");
		*max_dpi_pixel_clock = DPI_PIXEL_CLK_MAX;
	}

	DRM_DEV_DEBUG_DRIVER(dev, "using afe_setting: %u, max_lane_count: %u",
			     it6505->afe_setting, it6505->max_lane_count);
	DRM_DEV_DEBUG_DRIVER(dev, "using max_dpi_pixel_clock: %u kHz",
			     it6505->max_dpi_pixel_clock);
}

static ssize_t receive_timing_debugfs_show(struct file *file, char __user *buf,
					   size_t len, loff_t *ppos)
{
	struct it6505 *it6505 = file->private_data;
	struct drm_display_mode *vid;
	u8 read_buf[READ_BUFFER_SIZE];
	u8 *str = read_buf, *end = read_buf + READ_BUFFER_SIZE;
	ssize_t ret, count;

	if (!it6505)
		return -ENODEV;

	it6505_calc_video_info(it6505);
	vid = &it6505->video_info;
	str += scnprintf(str, end - str, "---video timing---\n");
	str += scnprintf(str, end - str, "PCLK:%d.%03dMHz\n",
			 vid->clock / 1000, vid->clock % 1000);
	str += scnprintf(str, end - str, "HTotal:%d\n", vid->htotal);
	str += scnprintf(str, end - str, "HActive:%d\n", vid->hdisplay);
	str += scnprintf(str, end - str, "HFrontPorch:%d\n",
			 vid->hsync_start - vid->hdisplay);
	str += scnprintf(str, end - str, "HSyncWidth:%d\n",
			 vid->hsync_end - vid->hsync_start);
	str += scnprintf(str, end - str, "HBackPorch:%d\n",
			 vid->htotal - vid->hsync_end);
	str += scnprintf(str, end - str, "VTotal:%d\n", vid->vtotal);
	str += scnprintf(str, end - str, "VActive:%d\n", vid->vdisplay);
	str += scnprintf(str, end - str, "VFrontPorch:%d\n",
			 vid->vsync_start - vid->vdisplay);
	str += scnprintf(str, end - str, "VSyncWidth:%d\n",
			 vid->vsync_end - vid->vsync_start);
	str += scnprintf(str, end - str, "VBackPorch:%d\n",
			 vid->vtotal - vid->vsync_end);

	count = str - read_buf;
	ret = simple_read_from_buffer(buf, len, ppos, read_buf, count);

	return ret;
}

static int force_power_on_off_debugfs_write(void *data, u64 value)
{
	struct it6505 *it6505 = data;

	if (!it6505)
		return -ENODEV;

	if (value)
		it6505_poweron(it6505);
	else
		it6505_poweroff(it6505);

	return 0;
}

static int enable_drv_hold_debugfs_show(void *data, u64 *buf)
{
	struct it6505 *it6505 = data;

	if (!it6505)
		return -ENODEV;

	*buf = it6505->enable_drv_hold;

	return 0;
}

static int enable_drv_hold_debugfs_write(void *data, u64 drv_hold)
{
	struct it6505 *it6505 = data;

	if (!it6505)
		return -ENODEV;

	it6505->enable_drv_hold = drv_hold;

	if (it6505->enable_drv_hold) {
		it6505_int_mask_disable(it6505);
	} else {
		it6505_clear_int(it6505);
		it6505_int_mask_enable(it6505);

		if (it6505->powered) {
			it6505->connector_status =
					it6505_get_sink_hpd_status(it6505) ?
					connector_status_connected :
					connector_status_disconnected;
		} else {
			it6505->connector_status =
					connector_status_disconnected;
		}
	}

	return 0;
}

static const struct file_operations receive_timing_fops = {
	.owner = THIS_MODULE,
	.open = simple_open,
	.read = receive_timing_debugfs_show,
	.llseek = default_llseek,
};

DEFINE_DEBUGFS_ATTRIBUTE(fops_force_power, NULL,
			 force_power_on_off_debugfs_write, "%llu\n");

DEFINE_DEBUGFS_ATTRIBUTE(fops_enable_drv_hold, enable_drv_hold_debugfs_show,
			 enable_drv_hold_debugfs_write, "%llu\n");

static const struct debugfs_entries debugfs_entry[] = {
	{ "receive_timing", &receive_timing_fops },
	{ "force_power_on_off", &fops_force_power },
	{ "enable_drv_hold", &fops_enable_drv_hold },
	{ NULL, NULL },
};

static void debugfs_create_files(struct it6505 *it6505)
{
	int i = 0;

	while (debugfs_entry[i].name && debugfs_entry[i].fops) {
		debugfs_create_file(debugfs_entry[i].name, 0644,
				    it6505->debugfs, it6505,
				    debugfs_entry[i].fops);
		i++;
	}
}

static void debugfs_init(struct it6505 *it6505)
{
	struct device *dev = &it6505->client->dev;

	it6505->debugfs = debugfs_create_dir(DEBUGFS_DIR_NAME, NULL);

	if (IS_ERR(it6505->debugfs)) {
		dev_err(dev, "failed to create debugfs root");
		return;
	}

	debugfs_create_files(it6505);
}

static void it6505_debugfs_remove(struct it6505 *it6505)
{
	debugfs_remove_recursive(it6505->debugfs);
}

static void it6505_shutdown(struct i2c_client *client)
{
	struct it6505 *it6505 = dev_get_drvdata(&client->dev);

	if (it6505->powered)
		it6505_lane_off(it6505);
}

static int it6505_i2c_probe(struct i2c_client *client)
{
	struct it6505 *it6505;
	struct device *dev = &client->dev;
	struct extcon_dev *extcon;
	int err, intp_irq;

	it6505 = devm_kzalloc(&client->dev, sizeof(*it6505), GFP_KERNEL);
	if (!it6505)
		return -ENOMEM;

	mutex_init(&it6505->extcon_lock);
	mutex_init(&it6505->mode_lock);
	mutex_init(&it6505->aux_lock);

	it6505->bridge.of_node = client->dev.of_node;
	it6505->connector_status = connector_status_disconnected;
	it6505->client = client;
	i2c_set_clientdata(client, it6505);

	/* get extcon device from DTS */
	extcon = extcon_get_edev_by_phandle(dev, 0);
	if (PTR_ERR(extcon) == -EPROBE_DEFER)
		return -EPROBE_DEFER;
	if (IS_ERR(extcon)) {
		dev_err(dev, "can not get extcon device!");
		return PTR_ERR(extcon);
	}

	it6505->extcon = extcon;

	it6505->regmap = devm_regmap_init_i2c(client, &it6505_regmap_config);
	if (IS_ERR(it6505->regmap)) {
		dev_err(dev, "regmap i2c init failed");
		err = PTR_ERR(it6505->regmap);
		return err;
	}

	err = it6505_init_pdata(it6505);
	if (err) {
		dev_err(dev, "Failed to initialize pdata: %d", err);
		return err;
	}

	it6505_parse_dt(it6505);

	intp_irq = client->irq;

	if (!intp_irq) {
		dev_err(dev, "Failed to get INTP IRQ");
		err = -ENODEV;
		return err;
	}

	err = devm_request_threaded_irq(&client->dev, intp_irq, NULL,
					it6505_int_threaded_handler,
					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
					"it6505-intp", it6505);
	if (err) {
		dev_err(dev, "Failed to request INTP threaded IRQ: %d", err);
		return err;
	}

	INIT_WORK(&it6505->link_works, it6505_link_training_work);
	INIT_WORK(&it6505->hdcp_wait_ksv_list, it6505_hdcp_wait_ksv_list);
	INIT_DELAYED_WORK(&it6505->hdcp_work, it6505_hdcp_work);
	init_completion(&it6505->extcon_completion);
	memset(it6505->dpcd, 0, sizeof(it6505->dpcd));
	it6505->powered = false;
	it6505->enable_drv_hold = DEFAULT_DRV_HOLD;

	if (DEFAULT_PWR_ON)
		it6505_poweron(it6505);

	DRM_DEV_DEBUG_DRIVER(dev, "it6505 device name: %s", dev_name(dev));
	debugfs_init(it6505);
	pm_runtime_enable(dev);

	it6505->aux.name = "DP-AUX";
	it6505->aux.dev = dev;
	it6505->aux.transfer = it6505_aux_transfer;
	drm_dp_aux_init(&it6505->aux);

	it6505->bridge.funcs = &it6505_bridge_funcs;
	it6505->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
	it6505->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID |
			     DRM_BRIDGE_OP_HPD;
	drm_bridge_add(&it6505->bridge);

	return 0;
}

static void it6505_i2c_remove(struct i2c_client *client)
{
	struct it6505 *it6505 = i2c_get_clientdata(client);

	drm_bridge_remove(&it6505->bridge);
	drm_dp_aux_unregister(&it6505->aux);
	it6505_debugfs_remove(it6505);
	it6505_poweroff(it6505);
	it6505_remove_edid(it6505);
}

static const struct i2c_device_id it6505_id[] = {
	{ "it6505", 0 },
	{ }
};

MODULE_DEVICE_TABLE(i2c, it6505_id);

static const struct of_device_id it6505_of_match[] = {
	{ .compatible = "ite,it6505" },
	{ }
};

static struct i2c_driver it6505_i2c_driver = {
	.driver = {
		.name = "it6505",
		.of_match_table = it6505_of_match,
		.pm = &it6505_bridge_pm_ops,
	},
	.probe_new = it6505_i2c_probe,
	.remove = it6505_i2c_remove,
	.shutdown = it6505_shutdown,
	.id_table = it6505_id,
};

module_i2c_driver(it6505_i2c_driver);

MODULE_AUTHOR("Allen Chen <allen.chen@ite.com.tw>");
MODULE_DESCRIPTION("IT6505 DisplayPort Transmitter driver");
MODULE_LICENSE("GPL v2");