summaryrefslogtreecommitdiff
path: root/samsung/exynos_drm_dp.c
blob: 135ec26b78574e1fe651d0f8eb3d7bc67a049c90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
 *
 * Samsung DisplayPort driver.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/clk.h>
#include <linux/component.h>
#include <linux/dma-mapping.h>
#include <linux/pm_runtime.h>
#include <linux/of_address.h>
#include <linux/irq.h>
#include <linux/hdmi.h>
#include <video/videomode.h>

#include <drm/drm_hdcp.h>
#include <drm/drm_modes.h>
#include <drm/drm_probe_helper.h>
#include <drm/drm_atomic_state_helper.h>
#include <drm/drm_modeset_helper_vtables.h>

#include "exynos_drm_dp.h"

struct dp_device *dp_drvdata;
EXPORT_SYMBOL(dp_drvdata);

struct blocking_notifier_head dp_ado_notifier_head =
		BLOCKING_NOTIFIER_INIT(dp_ado_notifier_head);
EXPORT_SYMBOL(dp_ado_notifier_head);

#define DP_SUPPORT_TPS(_v) BIT((_v)-1)

static inline struct dp_device *encoder_to_dp(struct drm_encoder *e)
{
	return container_of(e, struct dp_device, encoder);
}

static inline struct dp_device *connector_to_dp(struct drm_connector *c)
{
	return container_of(c, struct dp_device, connector);
}

static inline struct dp_device *dp_aux_to_dp(struct drm_dp_aux *a)
{
	return container_of(a, struct dp_device, dp_aux);
}

static u32 dp_get_audio_state(struct dp_device *dp)
{
	u32 audio_state;

	mutex_lock(&dp->audio_lock);
	audio_state = dp->audio_state;
	mutex_unlock(&dp->audio_lock);

	return audio_state;
}

static void dp_set_audio_state(struct dp_device *dp, u32 state)
{
	mutex_lock(&dp->audio_lock);
	dp->audio_state = state;
	mutex_unlock(&dp->audio_lock);
}

static enum hotplug_state dp_get_hpd_state(struct dp_device *dp)
{
	enum hotplug_state hpd_current_state;

	mutex_lock(&dp->hpd_state_lock);
	hpd_current_state = dp->hpd_current_state;
	mutex_unlock(&dp->hpd_state_lock);

	return hpd_current_state;
}

static void dp_set_hpd_state(struct dp_device *dp,
			     enum hotplug_state hpd_current_state)
{
	mutex_lock(&dp->hpd_state_lock);
	dp_info(dp, "DP HPD changed to %d\n", hpd_current_state);
	dp->hpd_current_state = hpd_current_state;
	mutex_unlock(&dp->hpd_state_lock);
}

static void dp_init_info(struct dp_device *dp)
{
	dp->state = DP_STATE_INIT;
	dp->hpd_current_state = EXYNOS_HPD_UNPLUG;
	dp->audio_state = DP_AUDIO_DISABLE;
	dp->output_type = EXYNOS_DISPLAY_TYPE_DP0_SST1;
	dp->dp_link_crc_enabled = false;
}

static int dp_get_max_link_rate(struct dp_device *dp)
{
	/*
	 * When the host is limited in software to RBR and
	 * the sink supports only max 2 lanes, it leads to
	 * best-case link of RBR/2 lanes. Such link cannot
	 * support 1920x1080@60 video resolution, which is
	 * not an optimal experience.
	 *
	 * For max 2-lane devices, such as USB 3.0 hubs,
	 * let's bump the link speed to HBR, so we can get
	 * HBR/2 lanes link for 1920x1080@60 support.
	 */
	if (dp->host.link_rate == drm_dp_bw_code_to_link_rate(DP_LINK_BW_1_62) &&
	    dp->sink.num_lanes < 4) {
		return min(drm_dp_bw_code_to_link_rate(DP_LINK_BW_2_7), dp->sink.link_rate);
	} else {
		return min(dp->host.link_rate, dp->sink.link_rate);
	}
}

static u8 dp_get_max_num_lanes(struct dp_device *dp)
{
	return min(dp->host.num_lanes, dp->sink.num_lanes);
}

static u8 dp_get_supported_pattern(struct dp_device *dp)
{
	return (dp->host.support_tps & dp->sink.support_tps);
}

static bool dp_get_enhanced_mode(struct dp_device *dp)
{
	return (dp->host.enhanced_frame && dp->sink.enhanced_frame);
}

static bool dp_get_ssc(struct dp_device *dp)
{
	return (dp->host.ssc && dp->sink.ssc);
}

static bool dp_get_fec(struct dp_device *dp)
{
	return (dp->host.fec && dp->sink.fec);
}

static bool dp_get_fast_training(struct dp_device *dp)
{
	return (dp->host.fast_training && dp->sink.fast_training);
}

#define MAX_VOLTAGE_LEVEL 3
#define MAX_PREEMPH_LEVEL 3

#define DP_LINK_RATE_RBR  0
#define DP_LINK_RATE_HBR  1
#define DP_LINK_RATE_HBR2 2
#define DP_LINK_RATE_HBR3 3

static unsigned long dp_rate = DP_LINK_RATE_RBR;    /* RBR is the default */
module_param(dp_rate, ulong, 0664);
MODULE_PARM_DESC(dp_rate, "use specific DP link rate by setting dp_rate=x");

static unsigned long dp_lanes = 4;    /* 4 lanes is the default */
module_param(dp_lanes, ulong, 0664);
MODULE_PARM_DESC(dp_lanes, "use specific number of DP lanes by setting dp_lanes=x");

static unsigned long dp_bpc = 8;    /* 8 bpc is the default */
module_param(dp_bpc, ulong, 0664);
MODULE_PARM_DESC(dp_bpc, "use specific BPC by setting dp_bpc=x");

static bool dp_fec = false;
module_param(dp_fec, bool, 0664);
MODULE_PARM_DESC(dp_fec, "Enable/disable DP link forward error correction");

static bool dp_ssc = true;
module_param(dp_ssc, bool, 0664);
MODULE_PARM_DESC(dp_ssc, "Enable/disable DP link spread spectrum clocking");

static bool dp_phy_boost = true;
module_param(dp_phy_boost, bool, 0664);
MODULE_PARM_DESC(dp_phy_boost, "Enable/disable DP PHY current boost");

#define DP_BIST_OFF     0
#define DP_BIST_ON      1
#define DP_BIST_ON_HDCP 2

static unsigned long dp_bist_mode = DP_BIST_OFF;
module_param(dp_bist_mode, ulong, 0664);
MODULE_PARM_DESC(dp_bist_mode, "use BIST mode by setting dp_bist_mode=x");

static int dp_emulation_mode;

static void dp_fill_host_caps(struct dp_device *dp)
{
	switch (dp_rate) {
	case DP_LINK_RATE_RBR:
		dp->host.link_rate = drm_dp_bw_code_to_link_rate(DP_LINK_BW_1_62);
		break;
	case DP_LINK_RATE_HBR:
		dp->host.link_rate = drm_dp_bw_code_to_link_rate(DP_LINK_BW_2_7);
		break;
	case DP_LINK_RATE_HBR3:
		dp->host.link_rate = drm_dp_bw_code_to_link_rate(DP_LINK_BW_8_1);
		break;
	case DP_LINK_RATE_HBR2:
	default:
		dp->host.link_rate = drm_dp_bw_code_to_link_rate(DP_LINK_BW_5_4);
		break;
	}

	switch (dp_lanes) {
	case 1:
		dp->host.num_lanes = 1;
		break;
	case 2:
		dp->host.num_lanes = 2;
		break;
	case 4:
	default:
		dp->host.num_lanes = 4;
		break;
	}

	switch (dp_bpc) {
	case 10:
		dp->host.max_bpc = 10;
		break;
	case 8:
		dp->host.max_bpc = 8;
		break;
	case 6:
	default:
		dp->host.max_bpc = 6;
		break;
	}

	switch (dp_bist_mode) {
	case DP_BIST_ON_HDCP:
		dp->bist_mode = DP_BIST_ON_HDCP;
		break;
	case DP_BIST_ON:
		dp->bist_mode = DP_BIST_ON;
		break;
	case DP_BIST_OFF:
	default:
		dp->bist_mode = DP_BIST_OFF;
		break;
	}

	dp->host.volt_swing_max = MAX_VOLTAGE_LEVEL;
	dp->host.pre_emphasis_max = MAX_PREEMPH_LEVEL;
	dp->host.support_tps = DP_SUPPORT_TPS(1) | DP_SUPPORT_TPS(2) |
			       DP_SUPPORT_TPS(3) | DP_SUPPORT_TPS(4);
	dp->host.fast_training = false;
	dp->host.enhanced_frame = true;
	dp->host.scrambler = true;
	dp->host.fec = dp_fec;
	dp->host.ssc = dp_ssc;
}

static bool dp_check_fec_caps(struct dp_device *dp, u8 fec_dpcd)
{
	u8 fec_data;

	if (!drm_dp_sink_supports_fec(fec_dpcd))
		return false;

	fec_data = DP_FEC_DECODE_EN_DETECTED | DP_FEC_DECODE_DIS_DETECTED;
	if (drm_dp_dpcd_writeb(&dp->dp_aux, DP_FEC_STATUS, fec_data) < 0)
		return false;

	fec_data = DP_FEC_READY;
	if (drm_dp_dpcd_writeb(&dp->dp_aux, DP_FEC_CONFIGURATION, fec_data) < 0)
		return false;

	if (drm_dp_dpcd_readb(&dp->dp_aux, DP_FEC_CONFIGURATION, &fec_data) < 0)
		return false;

	return true;
}

static void dp_fill_sink_caps(struct dp_device *dp, u8 dpcd[DP_RECEIVER_CAP_SIZE])
{
	u8 fec_dpcd = 0;
	u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE + 1];

	memset(&dp->sink, 0, sizeof(struct dp_sink));

	dp->sink.revision = dpcd[0];
	dp->sink.link_rate = drm_dp_max_link_rate(dpcd);
	dp->sink.num_lanes = drm_dp_max_lane_count(dpcd);
	dp->sink.enhanced_frame = drm_dp_enhanced_frame_cap(dpcd);

	/* Set SSC support */
	dp->sink.ssc = !!(dpcd[DP_MAX_DOWNSPREAD] & DP_MAX_DOWNSPREAD_0_5);

	/* Set TPS support */
	dp->sink.support_tps = DP_SUPPORT_TPS(1) | DP_SUPPORT_TPS(2);
	if (drm_dp_tps3_supported(dpcd))
		dp->sink.support_tps |= DP_SUPPORT_TPS(3);
	if (drm_dp_tps4_supported(dpcd))
		dp->sink.support_tps |= DP_SUPPORT_TPS(4);

	/* Set fast link support */
	dp->sink.fast_training = drm_dp_fast_training_cap(dpcd);

	/* Set FEC support */
	if (drm_dp_dpcd_readb(&dp->dp_aux, DP_FEC_CAPABILITY, &fec_dpcd) == 1) {
		dp->sink.fec = dp_check_fec_caps(dp, fec_dpcd);
	} else {
		dp->stats.dpcd_read_failures++;
		dp_warn(dp, "DP Sink: failed to read FEC support register\n");
	}

	/* Set DSC support */
	if (drm_dp_dpcd_read(&dp->dp_aux, DP_DSC_SUPPORT, dsc_dpcd, sizeof(dsc_dpcd)) ==
			sizeof(dsc_dpcd)) {
		dp->sink.dsc = !!dsc_dpcd[0];
	} else {
		dp->stats.dpcd_read_failures++;
		dp_warn(dp, "DP Sink: failed to read DSC support registers\n");
	}
}

// Callback function for DRM DP Helper
static ssize_t dp_aux_transfer(struct drm_dp_aux *dp_aux,
			       struct drm_dp_aux_msg *msg)
{
	struct dp_device *dp = dp_aux_to_dp(dp_aux);
	int ret;

	switch (msg->request) {
	case DP_AUX_NATIVE_WRITE:
		ret = dp_hw_write_dpcd_burst(msg->address, msg->size,
					     msg->buffer);
		break;
	case DP_AUX_NATIVE_READ:
		ret = dp_hw_read_dpcd_burst(msg->address, msg->size,
					    msg->buffer);
		break;
	default:
		dp_err(dp, "unsupported DP Request(0x%X)\n", msg->request);
		return -EINVAL;
	}

	if (ret)
		msg->reply = DP_AUX_NATIVE_REPLY_NACK;
	else {
		msg->reply = DP_AUX_NATIVE_REPLY_ACK;
		ret = msg->size;
	}

	// Should be return size or error code
	return ret;
}

// Callback function for DRM EDID Helper
static int dp_get_edid_block(void *data, u8 *edid, unsigned int block,
			     size_t length)
{
	// Hard-coded EDID for DP emulation mode
	static const unsigned char emul_edid_block0[] = {
		0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1e, 0x6d, 0x01, 0x00, 0x01,
		0x01, 0x01, 0x01, 0x01, 0x1b, 0x01, 0x03, 0x80, 0xa0, 0x5a, 0x78, 0x0a, 0xee,
		0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26, 0x0f, 0x50, 0x54, 0xa1, 0x08, 0x00, 0x31,
		0x40, 0x45, 0x40, 0x61, 0x40, 0x71, 0x40, 0x81, 0x80, 0x01, 0x01, 0x01, 0x01,
		0x01, 0x01, 0x08, 0xe8, 0x00, 0x30, 0xf2, 0x70, 0x5a, 0x80, 0xb0, 0x58, 0x8a,
		0x00, 0x40, 0x84, 0x63, 0x00, 0x00, 0x1e, 0x02, 0x3a, 0x80, 0x18, 0x71, 0x38,
		0x2d, 0x40, 0x58, 0x2c, 0x45, 0x00, 0x40, 0x84, 0x63, 0x00, 0x00, 0x1e, 0x00,
		0x00, 0x00, 0xfd, 0x00, 0x3a, 0x79, 0x1e, 0x88, 0x3c, 0x00, 0x0a, 0x20, 0x20,
		0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x4c, 0x47, 0x20, 0x54,
		0x56, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01, 0x63
	};
	static const unsigned char emul_edid_block1[] = {
		0x02, 0x03, 0x60, 0xf1, 0x5a, 0x61, 0x60, 0x10, 0x1f, 0x66, 0x65, 0x04, 0x13,
		0x05, 0x14, 0x03, 0x02, 0x12, 0x20, 0x21, 0x22, 0x15, 0x01, 0x5d, 0x5e, 0x5f,
		0x62, 0x63, 0x64, 0x3f, 0x40, 0x2f, 0x09, 0x57, 0x07, 0x15, 0x07, 0x50, 0x57,
		0x07, 0x01, 0x3d, 0x06, 0xc0, 0x67, 0x04, 0x03, 0x6e, 0x03, 0x0c, 0x00, 0x30,
		0x00, 0xb8, 0x3c, 0x20, 0x00, 0x80, 0x01, 0x02, 0x03, 0x04, 0x67, 0xd8, 0x5d,
		0xc4, 0x01, 0x78, 0x80, 0x03, 0xe2, 0x00, 0xcf, 0xe3, 0x05, 0xc0, 0x00, 0xe3,
		0x06, 0x0d, 0x01, 0xe2, 0x0f, 0x33, 0xeb, 0x01, 0x46, 0xd0, 0x00, 0x26, 0x0a,
		0x09, 0x75, 0x80, 0x5b, 0x6c, 0x66, 0x21, 0x50, 0xb0, 0x51, 0x00, 0x1b, 0x30,
		0x40, 0x70, 0x36, 0x00, 0x40, 0x84, 0x63, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe1
	};

	if (!dp_emulation_mode) {
		dp_hw_read_edid(block, length, edid);
		return 0;
	}

	if (block <= 1) {
		const unsigned char *blk = block ? emul_edid_block1 : emul_edid_block0;
		size_t blk_size = block ? sizeof(emul_edid_block1) : sizeof(emul_edid_block0);

		memcpy(edid, blk, min(length, blk_size));
		return 0;
	}

	dp_warn(dp_drvdata, "%s: unknown edid block %d requested\n", __func__, block);
	return -1;
}

//------------------------------------------------------------------------------
//
static int dp_sink_power_up(struct dp_device *dp, bool up)
{
	u8 val = 0;
	int ret;

	if (dp->sink.revision >= DP_DPCD_REV_11) {
		ret = drm_dp_dpcd_readb(&dp->dp_aux, DP_SET_POWER, &val);
		if (ret < 0) {
			dp_err(dp,
			       "DP Sink: failed to read sink power state\n");
			return ret;
		}

		val &= ~DP_SET_POWER_MASK;
		if (up)
			val |= DP_SET_POWER_D0;
		else
			val |= DP_SET_POWER_D3;

		ret = drm_dp_dpcd_writeb(&dp->dp_aux, DP_SET_POWER, val);
		if (ret < 0) {
			dp_err(dp,
			       "DP Sink: failed to write sink power state\n");
			return ret;
		}

		usleep_range(1000, 2000);
	}

	dp_info(dp, "DP Sink: sink power state set to %s\n", up ? "D0" : "D3");
	return 0;
}



static u32 dp_get_training_interval_us(struct dp_device *dp, u32 interval)
{
	if (interval == 0)
		return 400;
	if (interval < 5)
		return 4000 << (interval - 1);

	dp_warn(dp, "invalid link training AUX read interval value(%u)\n", interval);
	return 4000;
}

static void dp_print_swing_level(struct dp_device *dp)
{
	u8 *vol_swing_level = dp->host.vol_swing_level;
	u8 *pre_empha_level = dp->host.pre_empha_level;

	dp_info(dp, "Voltage_Swing: %02X %02X %02X %02X\n", vol_swing_level[0],
		 vol_swing_level[1], vol_swing_level[2], vol_swing_level[3]);
	dp_info(dp, "Pre_Emphasis : %02X %02X %02X %02X\n", pre_empha_level[0],
		 pre_empha_level[1], pre_empha_level[2], pre_empha_level[3]);
}

static void dp_validate_link_training(struct dp_device *dp, bool *cr_done,
				      bool *same_before_adjust,
				      bool *max_swing_reached,
				      u8 before_cr[MAX_LANE_CNT],
				      u8 after_cr[DP_LINK_STATUS_SIZE])
{
	u8 *req_vol = dp->host.vol_swing_level;
	u8 *req_pre = dp->host.pre_empha_level;
	u8 *req_max = dp->host.max_reach_value;

	u8 adjust_volt, adjust_pre;
	bool same_pre, same_volt;
	int i;

	*same_before_adjust = false;
	*max_swing_reached = false;
	*cr_done = drm_dp_clock_recovery_ok(after_cr, dp->link.num_lanes);

	for (i = 0; i < dp->link.num_lanes; i++) {
		req_max[i] = 0;

		adjust_volt = drm_dp_get_adjust_request_voltage(after_cr, i);
		req_vol[i] = min(adjust_volt, dp->host.volt_swing_max);
		if (req_vol[i] == dp->host.volt_swing_max)
			req_max[i] |= DP_TRAIN_MAX_SWING_REACHED;

		adjust_pre =
			drm_dp_get_adjust_request_pre_emphasis(after_cr, i) >>
			DP_TRAIN_PRE_EMPHASIS_SHIFT;
		req_pre[i] = min(adjust_pre, dp->host.pre_emphasis_max);
		if (req_pre[i] == dp->host.pre_emphasis_max)
			req_max[i] |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;

		same_pre = (before_cr[i] & DP_TRAIN_PRE_EMPHASIS_MASK) ==
			   req_pre[i] << DP_TRAIN_PRE_EMPHASIS_SHIFT;
		same_volt = (before_cr[i] & DP_TRAIN_VOLTAGE_SWING_MASK) ==
			    req_vol[i];
		if (same_pre && same_volt)
			*same_before_adjust = true;

		if (!*cr_done && req_max[i] != 0) {
			*max_swing_reached = true;
		}
	}
}

static void dp_set_hwconfig_dplink(struct dp_device *dp)
{
	u8 link_rate = drm_dp_link_rate_to_bw_code(dp->link.link_rate);

	if (link_rate == DP_LINK_BW_1_62)
		dp->hw_config.link_rate = LINK_RATE_RBR;
	else if (link_rate == DP_LINK_BW_2_7)
		dp->hw_config.link_rate = LINK_RATE_HBR;
	else if (link_rate == DP_LINK_BW_5_4)
		dp->hw_config.link_rate = LINK_RATE_HBR2;
	else if (link_rate == DP_LINK_BW_8_1)
		dp->hw_config.link_rate = LINK_RATE_HBR3;

	dp->hw_config.num_lanes = dp->link.num_lanes;
}

static void dp_set_hwconfig_video(struct dp_device *dp)
{
	dp->hw_config.enhanced_mode = dp_get_enhanced_mode(dp);
	dp->hw_config.use_fec = dp_get_fec(dp);
	dp->hw_config.use_ssc = dp_get_ssc(dp);
}

static int dp_link_configure(struct dp_device *dp)
{
	u8 value;
	int err;

	value = drm_dp_link_rate_to_bw_code(dp->link.link_rate);
	err = drm_dp_dpcd_writeb(&dp->dp_aux, DP_LINK_BW_SET, value);
	if (err < 0) {
		dp_err(dp, "Failed to write DP_LINK_BW_SET");
		return err;
	}

	value = dp->link.num_lanes;

	if (dp_get_enhanced_mode(dp))
		value |= DP_LANE_COUNT_ENHANCED_FRAME_EN;

	err = drm_dp_dpcd_writeb(&dp->dp_aux, DP_LANE_COUNT_SET, value);
	if (err < 0) {
		dp_err(dp, "Failed to write DP_LANE_COUNT_SET");
		return err;
	}

	value = dp->link.ssc ? DP_SPREAD_AMP_0_5 : 0;
	err = drm_dp_dpcd_writeb(&dp->dp_aux, DP_DOWNSPREAD_CTRL, value);
	if (err < 0) {
		dp_err(dp, "Failed to write DP_DOWNSPREAD_CTRL");
		return err;
	}

	value = DP_SET_ANSI_8B10B;
	err = drm_dp_dpcd_writeb(&dp->dp_aux, DP_MAIN_LINK_CHANNEL_CODING_SET, value);
	if (err < 0) {
		dp_err(dp, "Failed to write DP_MAIN_LINK_CHANNEL_CODING_SET");
		return err;
	}

	return 0;
}

static int dp_init_link_training_cr(struct dp_device *dp)
{
	int ret;

	/* Disable DP Training Pattern */
	drm_dp_dpcd_writeb(&dp->dp_aux, DP_TRAINING_PATTERN_SET,
			   DP_TRAINING_PATTERN_DISABLE);

	/* Reconfigure DP HW */
	dp_set_hwconfig_dplink(dp);
	dp_set_hwconfig_video(dp);
	ret = dp_hw_reinit(&dp->hw_config);
	if (ret) {
		dp_err(dp, "dp_hw_reinit() failed\n");
		return ret;
	}

	dp_info(dp, "HW configured with Rate(%d) and Lanes(%u)\n",
		dp->hw_config.link_rate, dp->hw_config.num_lanes);

	/* Configure FEC before link training */
	dp_hw_set_fec(dp->link.fec);

	/* Reconfigure DP Link */
	dp_link_configure(dp);

	/* Set DP Training Pattern in DP PHY */
	dp_info(dp, "enable DP training pattern 1\n");
	dp_hw_set_training_pattern(TRAINING_PATTERN_1);

	/* Enable DP Training Pattern */
	drm_dp_dpcd_writeb(&dp->dp_aux, DP_TRAINING_PATTERN_SET,
			   DP_TRAINING_PATTERN_1 | DP_LINK_SCRAMBLING_DISABLE);

	return 0;
}

static bool dp_do_link_training_cr(struct dp_device *dp, u32 interval_us)
{
	u8 *vol_swing_level = dp->host.vol_swing_level;
	u8 *pre_empha_level = dp->host.pre_empha_level;
	u8 *max_reach_value = dp->host.max_reach_value;

	u8 lanes_data[MAX_LANE_CNT]; // before_cr
	u8 link_status[DP_LINK_STATUS_SIZE]; // after_cr
	bool cr_done;
	bool same_before_adjust, max_swing_reached, try_max_swing = false;
	u8 fail_counter_short = 0, fail_counter_long = 0;
	int i;

	dp_info(dp, "Link Training CR Phase with Rate(%d) and Lanes(%u)\n",
		dp->link.link_rate / 100, dp->link.num_lanes);

	for (i = 0; i < MAX_LANE_CNT; i++) {
		vol_swing_level[i] = 0;
		pre_empha_level[i] = 0;
		max_reach_value[i] = 0;
	}

	if (dp_init_link_training_cr(dp))
		goto err;

	// Voltage Swing
	do {
		// Set Voltage
		dp_print_swing_level(dp);
		dp_hw_set_voltage_and_pre_emphasis(&dp->hw_config,
						   vol_swing_level,
						   pre_empha_level);

		// Write Link Training
		for (i = 0; i < dp->link.num_lanes; i++)
			lanes_data[i] = (pre_empha_level[i]
					 << DP_TRAIN_PRE_EMPHASIS_SHIFT) |
					vol_swing_level[i] | max_reach_value[i];
		drm_dp_dpcd_write(&dp->dp_aux, DP_TRAINING_LANE0_SET,
				  lanes_data, dp->link.num_lanes);

		udelay(interval_us);

		// Read Link Status
		drm_dp_dpcd_read_link_status(&dp->dp_aux, link_status);

		// Validate CR
		dp_validate_link_training(dp, &cr_done, &same_before_adjust,
					  &max_swing_reached, lanes_data,
					  link_status);

		if (cr_done) {
			dp_info(dp, "CR Done. Move to Training_EQ.\n");
			return true;
		}

		fail_counter_long++;

		/*
		 * Per DP spec, if the sink requests adjustment to
		 * max voltage swing or max pre-emphasis value, it
		 * will be retried only once.
		 */
		if (max_swing_reached) {
			if (!try_max_swing) {
				dp_info(dp, "adjust to max swing level\n");
				try_max_swing = true;
				continue;
			} else {
				dp_err(dp, "reached max swing level\n");
				goto err;
			}
		}

		/*
		 * Per DP spec, if the sink requests the exact same
		 * voltage swing and pre-emphasis levels again, it
		 * will be retried only max 5 times.
		 */
		if (same_before_adjust) {
			dp_info(dp, "requested same level. Retry...\n");
			fail_counter_short++;
			continue;
		}

		fail_counter_short = 0;
	} while (fail_counter_short < 5 && fail_counter_long < 10);

err:
	dp_err(dp, "failed Link Training CR phase with Rate(%d) and Lanes(%u)\n",
	       dp->link.link_rate / 100, dp->link.num_lanes);
	return false;
}

static void dp_init_link_training_eq(struct dp_device *dp, u8 tps)
{
	/* Set DP Training Pattern */
	if ((tps & DP_SUPPORT_TPS(4)) &&
	    drm_dp_link_rate_to_bw_code(dp->link.link_rate) == DP_LINK_BW_8_1) {
		dp_info(dp, "enable DP training pattern 4\n");
		dp_hw_set_training_pattern(TRAINING_PATTERN_4);
		drm_dp_dpcd_writeb(&dp->dp_aux, DP_TRAINING_PATTERN_SET,
				   DP_TRAINING_PATTERN_4);
	} else if (tps & DP_SUPPORT_TPS(3)) {
		dp_info(dp, "enable DP training pattern 3\n");
		dp_hw_set_training_pattern(TRAINING_PATTERN_3);
		drm_dp_dpcd_writeb(&dp->dp_aux, DP_TRAINING_PATTERN_SET,
				   DP_TRAINING_PATTERN_3 |
					   DP_LINK_SCRAMBLING_DISABLE);
	} else {
		dp_info(dp, "enable DP training pattern 2\n");
		dp_hw_set_training_pattern(TRAINING_PATTERN_2);
		drm_dp_dpcd_writeb(&dp->dp_aux, DP_TRAINING_PATTERN_SET,
				   DP_TRAINING_PATTERN_2 |
					   DP_LINK_SCRAMBLING_DISABLE);
	}
}

static bool dp_do_link_training_eq(struct dp_device *dp, u32 interval_us,
				   u8 tps)
{
	u8 *vol_swing_level = dp->host.vol_swing_level;
	u8 *pre_empha_level = dp->host.pre_empha_level;
	u8 *max_reach_value = dp->host.max_reach_value;

	u8 lanes_data[MAX_LANE_CNT]; // before_cr
	u8 link_status[DP_LINK_STATUS_SIZE]; // after_cr
	bool cr_done;
	bool same_before_adjust, max_swing_reached = false;
	u8 fail_counter = 0;
	int i;

	dp_info(dp, "Link Training EQ Phase with Rate(%d) and Lanes(%u)\n",
		dp->link.link_rate / 100, dp->link.num_lanes);

	dp_init_link_training_eq(dp, tps);

	do {
		// Set Voltage
		dp_print_swing_level(dp);
		dp_hw_set_voltage_and_pre_emphasis(&dp->hw_config,
						   vol_swing_level,
						   pre_empha_level);

		// Write Link Training
		for (i = 0; i < dp->link.num_lanes; i++)
			lanes_data[i] = (pre_empha_level[i]
					 << DP_TRAIN_PRE_EMPHASIS_SHIFT) |
					vol_swing_level[i] | max_reach_value[i];
		drm_dp_dpcd_write(&dp->dp_aux, DP_TRAINING_LANE0_SET,
				  lanes_data, dp->link.num_lanes);

		udelay(interval_us);

		// Read Link Status
		drm_dp_dpcd_read_link_status(&dp->dp_aux, link_status);

		// Validate EQ
		dp_validate_link_training(dp, &cr_done, &same_before_adjust,
					  &max_swing_reached, lanes_data,
					  link_status);

		if (cr_done) {
			if (drm_dp_channel_eq_ok(link_status, dp->link.num_lanes)) {
				dp_info(dp, "EQ Done. Move to Training_Done.\n");
				return true;
			}
		} else {
			dp_err(dp, "CR failed during EQ phase\n");
			goto err;
		}

		fail_counter++;
	} while (fail_counter < 6);

err:
	dp_err(dp, "failed Link Training EQ phase with Rate(%d) and Lanes(%u)\n",
	       dp->link.link_rate / 100, dp->link.num_lanes);
	return false;
}

static void dp_get_lower_link_rate(struct dp_link *link)
{
	switch (drm_dp_link_rate_to_bw_code(link->link_rate)) {
	case DP_LINK_BW_2_7:
		link->link_rate = drm_dp_bw_code_to_link_rate(DP_LINK_BW_1_62);
		break;
	case DP_LINK_BW_5_4:
		link->link_rate = drm_dp_bw_code_to_link_rate(DP_LINK_BW_2_7);
		break;
	case DP_LINK_BW_8_1:
		link->link_rate = drm_dp_bw_code_to_link_rate(DP_LINK_BW_5_4);
		break;
	}
}

static int dp_do_full_link_training(struct dp_device *dp, u32 interval_us)
{
	const u8 supported_tps = dp_get_supported_pattern(dp);
	bool training_done = false;

	do {
		// Link Training: CR (Clock Revovery)
		if (!dp_do_link_training_cr(dp, interval_us)) {
			if (drm_dp_link_rate_to_bw_code(dp->link.link_rate) !=
			    DP_LINK_BW_1_62) {
				dp_get_lower_link_rate(&dp->link);

				dp_info(dp,
					"reducing link rate to %d during CR phase\n",
					dp->link.link_rate / 100);
				continue;
			} else if (dp->link.num_lanes > 1) {
				dp->link.num_lanes >>= 1;
				dp->link.link_rate = dp_get_max_link_rate(dp);

				dp_info(dp,
					"reducing lanes number to %u during CR phase\n",
					dp->link.num_lanes);
				continue;
			}

			dp_err(dp, "Link training failed during CR phase\n");
			goto err;
		}

		// Link Training: Channel Equalization
		if (!dp_do_link_training_eq(dp, interval_us, supported_tps)) {
			if (drm_dp_link_rate_to_bw_code(dp->link.link_rate) !=
			    DP_LINK_BW_1_62) {
				dp_get_lower_link_rate(&dp->link);

				dp_info(dp,
					"reducing link rate to %d during EQ phase\n",
					dp->link.link_rate / 100);
				continue;
			} else if (dp->link.num_lanes > 1) {
				dp->link.num_lanes >>= 1;
				dp->link.link_rate = dp_get_max_link_rate(dp);

				dp_info(dp,
					"reducing lanes number to %u during EQ phase\n",
					dp->link.num_lanes);
				continue;
			}

			dp_err(dp, "Link training failed during EQ phase\n");
			goto err;
		}

		training_done = true;
	} while (!training_done);

	dp_info(dp, "DP Link: training done: Rate(%d Mbps) and Lanes(%u)\n",
		dp->link.link_rate / 100, dp->link.num_lanes);

	dp_hw_set_training_pattern(NORMAL_DATA);
	drm_dp_dpcd_writeb(&dp->dp_aux, DP_TRAINING_PATTERN_SET,
			   dp->host.scrambler ? 0 : DP_LINK_SCRAMBLING_DISABLE);

	return 0;
err:
	dp_info(dp, "DP Link: training failed\n");

	dp_hw_set_training_pattern(NORMAL_DATA);
	drm_dp_dpcd_writeb(&dp->dp_aux, DP_TRAINING_PATTERN_SET,
			   DP_TRAINING_PATTERN_DISABLE);

	return -EIO;
}

static int dp_link_up(struct dp_device *dp)
{
	u8 dpcd[DP_RECEIVER_CAP_SIZE + 1];
	u8 dfp_info[DP_MAX_DOWNSTREAM_PORTS];
	u32 interval, interval_us;
	int ret;

	mutex_lock(&dp->training_lock);

	/* Fill host capabilities again, as they can be modified via sysfs */
	dp_fill_host_caps(dp);

	/* Update max BPC settings */
	dp->connector.max_bpc_property->values[1] = dp->host.max_bpc;
	dp->connector.state->max_bpc = dp->host.max_bpc;
	dp->connector.state->max_requested_bpc = dp->host.max_bpc;

	if (dp_emulation_mode) {
		dp_debug(dp, "dp_emulation_mode=1, skipped link training\n");

		// dp_emulation_mode is enabled, use hard-coded sink params.
		dp->sink.revision = DP_DPCD_REV_12;
		dp->sink.link_rate = drm_dp_bw_code_to_link_rate(DP_LINK_BW_5_4);
		dp->sink.num_lanes = 4;
		dp->sink.enhanced_frame = false;
		dp->sink.fec = false;
		dp->sink.ssc = false;
		dp->sink.support_tps = DP_SUPPORT_TPS(1) | DP_SUPPORT_TPS(2) |
				       DP_SUPPORT_TPS(3) | DP_SUPPORT_TPS(4);
		dp->sink.fast_training = true;
		dp->sink_count = 1;

		dp->link.link_rate = dp_get_max_link_rate(dp);
		dp->link.num_lanes = dp_get_max_num_lanes(dp);
		dp->link.enhanced_frame = dp_get_enhanced_mode(dp);
		dp->link.fec = dp_get_fec(dp);
		dp->link.ssc = dp_get_ssc(dp);
		dp->link.support_tps = dp_get_supported_pattern(dp);
		dp->link.fast_training = dp_get_fast_training(dp);

		/* Reconfigure DP HW for emulation mode */
		dp_set_hwconfig_dplink(dp);
		dp_set_hwconfig_video(dp);

		dp->hw_config.dp_emul = true;
		if (!dp_hw_reinit(&dp->hw_config)) {
			dp_info(dp, "HW configured with Rate(%d) and Lanes(%u)\n",
				dp->hw_config.link_rate, dp->hw_config.num_lanes);
		} else {
			dp_err(dp, "dp_hw_reinit() failed\n");
		}

		mutex_unlock(&dp->training_lock);
		return 0;
	}

	/* Read DP Sink device's Capabilities */
	ret = drm_dp_dpcd_read(&dp->dp_aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE + 1);
	if (ret < 0) {
		dp_err(dp, "failed to read DP Sink device capabilities\n");
		mutex_unlock(&dp->training_lock);
		dp->stats.dpcd_read_failures++;
		return -EIO;
	}

	if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] & DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT) {
		ret = drm_dp_dpcd_read(&dp->dp_aux, DP_DP13_DPCD_REV, dpcd,
				       DP_RECEIVER_CAP_SIZE + 1);
		if (ret < 0) {
			dp_err(dp, "failed to read DP Sink device capabilities\n");
			mutex_unlock(&dp->training_lock);
			dp->stats.dpcd_read_failures++;
			return -EIO;
		}
	}

	/* Fill Sink Capabilities */
	dp_fill_sink_caps(dp, dpcd);

	/* Dump Sink Capabilities */
	dp_info(dp, "DP Sink: DPCD_%X Rate(%d Mbps) Lanes(%u) EF(%d) SSC(%d) FEC(%d) DSC(%d)\n",
		dp->sink.revision, dp->sink.link_rate / 100, dp->sink.num_lanes,
		dp->sink.enhanced_frame, dp->sink.ssc, dp->sink.fec, dp->sink.dsc);

	/* Power DP Sink device Up */
	dp_sink_power_up(dp, true);

	/* Get sink count */
	dp->sink_count = drm_dp_read_sink_count(&dp->dp_aux);
	if (dp->sink_count < 0) {
		dp_err(dp, "DP Sink: failed to read sink count\n");
		mutex_unlock(&dp->training_lock);
		dp->stats.dpcd_read_failures++;
		return -EIO;
	}

	/* Get DFP count */
	if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT) {
		dp->dfp_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
		ret = drm_dp_dpcd_read(&dp->dp_aux, DP_DOWNSTREAM_PORT_0, dfp_info,
				       DP_MAX_DOWNSTREAM_PORTS);
		if (ret < 0) {
			dp_err(dp, "DP Branch Device: failed to read DP Downstream Port info\n");
			mutex_unlock(&dp->training_lock);
			dp->stats.dpcd_read_failures++;
			return -EIO;
		}

		dp_info(dp, "DP Branch Device: DFP count = %d, sink count = %d\n",
			dp->dfp_count, dp->sink_count);
	} else {
		dp->dfp_count = 0;
		dp_info(dp, "DP Sink: sink count = %d\n", dp->sink_count);
	}

	if (dp->sink_count == 0) {
		if (dp->dfp_count > 0) {
			dp_info(dp, "DP Link: training defer: DP Branch Device, sink count = 0\n");
			mutex_unlock(&dp->training_lock);
			return 0;
		} else {
			dp_err(dp, "DP Sink: invalid sink count = 0\n");
			mutex_unlock(&dp->training_lock);
			dp->stats.sink_count_invalid_failures++;
			return -EINVAL;
		}
	}

	/* Pick link parameters */
	dp->link.link_rate = dp_get_max_link_rate(dp);
	dp->link.num_lanes = dp_get_max_num_lanes(dp);
	dp->link.enhanced_frame = dp_get_enhanced_mode(dp);
	dp->link.fec = dp_get_fec(dp);
	dp->link.ssc = dp_get_ssc(dp);
	dp->link.support_tps = dp_get_supported_pattern(dp);
	dp->link.fast_training = dp_get_fast_training(dp);
	dp_info(dp, "DP Link: training start: Rate(%d Mbps) Lanes(%u) EF(%d) SSC(%d) FEC(%d)\n",
		dp->link.link_rate / 100, dp->link.num_lanes, dp->link.enhanced_frame,
		dp->link.ssc, dp->link.fec);

	/* Link Training */
	interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] & DP_TRAINING_AUX_RD_MASK;
	interval_us = dp_get_training_interval_us(dp, interval);
	if (!interval_us || dp_do_full_link_training(dp, interval_us)) {
		dp_err(dp, "failed to train DP Link\n");
		mutex_unlock(&dp->training_lock);
		dp->stats.link_negotiation_failures++;
		return -ENOLINK;
	}

	mutex_unlock(&dp->training_lock);
	return 0;
}

static int dp_link_down(struct dp_device *dp)
{
	if (dp_get_hpd_state(dp) == EXYNOS_HPD_PLUG) {
		dp_sink_power_up(dp, false);
	}

	return 0;
}

static enum bit_depth dp_get_bpc(struct dp_device *dp)
{
	struct drm_connector *connector = &dp->connector;
	struct drm_display_info *display_info = &connector->display_info;

	/*
	 * drm_atomic_connector_check() has been called.
	 * We can use connector->state->max_bpc directly.
	 */
	u8 bpc = connector->state->max_bpc;

	dp_info(dp, "display_info->bpc = %u, bpc = %u\n", display_info->bpc, bpc);

	switch (bpc) {
	case 10:
		return BPC_10;
	case 8:
		return BPC_8;
	default:
		return BPC_6;
	}
}

static void dp_set_video_timing(struct dp_device *dp)
{
	struct videomode vm;

	drm_display_mode_to_videomode(&dp->cur_mode, &vm);

	dp->hw_config.vtiming.clock = dp->cur_mode.clock;

	dp->hw_config.vtiming.htotal = dp->cur_mode.htotal;
	dp->hw_config.vtiming.vtotal = dp->cur_mode.vtotal;
	dp->hw_config.vtiming.hfp = vm.hfront_porch;
	dp->hw_config.vtiming.hbp = vm.hback_porch;
	dp->hw_config.vtiming.hactive = vm.hactive;
	dp->hw_config.vtiming.vfp = vm.vfront_porch;
	dp->hw_config.vtiming.vbp = vm.vback_porch;
	dp->hw_config.vtiming.vactive = vm.vactive;

	dp->hw_config.vtiming.vsync_polarity =
		(dp->cur_mode.flags & DRM_MODE_FLAG_NVSYNC) ? SYNC_NEGATIVE :
							      SYNC_POSITIVE;
	dp->hw_config.vtiming.hsync_polarity =
		(dp->cur_mode.flags & DRM_MODE_FLAG_NHSYNC) ? SYNC_NEGATIVE :
							      SYNC_POSITIVE;
}

static u8 dp_get_vic(struct dp_device *dp)
{
	dp->cur_mode_vic = drm_match_cea_mode(&dp->cur_mode);
	return dp->cur_mode_vic;
}

static int dp_make_avi_infoframe_data(struct dp_device *dp,
				      struct infoframe *avi_infoframe)
{
	int i;

	avi_infoframe->type_code = INFOFRAME_PACKET_TYPE_AVI;
	avi_infoframe->version_number = AVI_INFOFRAME_VERSION;
	avi_infoframe->length = AVI_INFOFRAME_LENGTH;

	for (i = 0; i < AVI_INFOFRAME_LENGTH; i++)
		avi_infoframe->data[i] = 0x00;

	avi_infoframe->data[0] |= ACTIVE_FORMAT_INFO_PRESENT;
	avi_infoframe->data[1] |= ACTIVE_PORTION_ASPECT_RATIO;
	avi_infoframe->data[3] = dp_get_vic(dp);

	return 0;
}

static int dp_set_avi_infoframe(struct dp_device *dp)
{
	struct infoframe avi_infoframe;

	dp_make_avi_infoframe_data(dp, &avi_infoframe);
	dp_hw_send_avi_infoframe(avi_infoframe);

	return 0;
}

static int dp_make_spd_infoframe_data(struct infoframe *spd_infoframe)
{
	spd_infoframe->type_code = 0x83;
	spd_infoframe->version_number = 0x1;
	spd_infoframe->length = 25;

	strncpy(&spd_infoframe->data[0], "SEC.GED", 8);

	return 0;
}

static int dp_set_spd_infoframe(void)
{
	struct infoframe spd_infoframe;

	memset(&spd_infoframe, 0, sizeof(spd_infoframe));
	dp_make_spd_infoframe_data(&spd_infoframe);
	dp_hw_send_spd_infoframe(spd_infoframe);

	return 0;
}


static int dp_make_audio_infoframe_data(struct dp_device *dp,
					struct infoframe *audio_infoframe)
{
	int i;

	audio_infoframe->type_code = INFOFRAME_PACKET_TYPE_AUDIO;
	audio_infoframe->version_number = AUDIO_INFOFRAME_VERSION;
	audio_infoframe->length = AUDIO_INFOFRAME_LENGTH;

	for (i = 0; i < AUDIO_INFOFRAME_LENGTH; i++)
		audio_infoframe->data[i] = 0x00;

	/* Data Byte 1, PCM type and audio channel count */
	audio_infoframe->data[0] = ((u8)dp->hw_config.num_audio_ch - 1);

	/* Data Byte 4, how various speaker locations are allocated */
	if (dp->hw_config.num_audio_ch == 8)
		audio_infoframe->data[3] = 0x13;
	else if (dp->hw_config.num_audio_ch == 6)
		audio_infoframe->data[3] = 0x0b;
	else
		audio_infoframe->data[3] = 0;

	dp_info(dp,
		"audio_infoframe: type and ch_cnt %02x, SF and bit size %02x, ch_allocation %02x\n",
		audio_infoframe->data[0], audio_infoframe->data[1], audio_infoframe->data[3]);

	return 0;
}

static int dp_set_audio_infoframe(struct dp_device *dp)
{
	struct infoframe audio_infoframe;

	memset(&audio_infoframe, 0, sizeof(audio_infoframe));
	dp_make_audio_infoframe_data(dp, &audio_infoframe);
	dp_hw_send_audio_infoframe(audio_infoframe);

	return 0;
}

static void dp_enable(struct drm_encoder *encoder)
{
	struct dp_device *dp = encoder_to_dp(encoder);

	mutex_lock(&dp->cmd_lock);

	dp->hw_config.bpc = dp_get_bpc(dp);
	dp->hw_config.range = VESA_RANGE;
	dp_set_video_timing(dp);

	if (dp->bist_mode == DP_BIST_OFF) {
		dp_hw_set_video_config(&dp->hw_config);
	} else {
		/* BIST mode */
		dp->hw_config.bist_mode = true;
		dp->hw_config.bist_type = COLOR_BAR;
		dp_hw_set_bist_video_config(&dp->hw_config);
	}

	dp_set_avi_infoframe(dp);
	dp_set_spd_infoframe();

	dp->dp_link_crc_enabled = false;
	enable_irq(dp->res.irq);
	dp_hw_start();
	dp_info(dp, "enabled DP as cur_mode = %s@%d\n", dp->cur_mode.name,
		drm_mode_vrefresh(&dp->cur_mode));

	if (dp->bist_mode != DP_BIST_OFF) {
		/* BIST mode */
		dp->hw_config.num_audio_ch = 2;
		dp->hw_config.audio_fs = FS_48KHZ;
		dp->hw_config.audio_bit = AUDIO_16_BIT;
		dp->hw_config.audio_packed_mode = NORMAL_MODE;
		dp->hw_config.audio_word_length = WORD_LENGTH_1;

		dp_hw_init_audio();
		dp_hw_set_bist_audio_config(&dp->hw_config);
		dp_hw_start_audio();

		dp_set_audio_infoframe(dp);
	}

	dp->state = DP_STATE_RUN;
	dp_info(dp, "%s: DP State changed to RUN\n", __func__);

	mutex_unlock(&dp->cmd_lock);
}

static void dp_disable(struct drm_encoder *encoder)
{
	struct dp_device *dp = encoder_to_dp(encoder);

	if (!pm_runtime_get_if_in_use(dp->dev)) {
		dp_debug(dp, "%s: DP is already disabled\n", __func__);
		return;
	}

	mutex_lock(&dp->cmd_lock);

	if (dp->state == DP_STATE_RUN) {
		disable_irq(dp->res.irq);
		if (dp->bist_mode != DP_BIST_OFF) {
			/* BIST mode */
			dp_hw_stop_audio();
			dp_hw_deinit_audio();
		}
		dp_hw_stop();
		dp_info(dp, "disabled DP\n");

		dp->state = DP_STATE_ON;
		dp_info(dp, "%s: DP State changed to ON\n", __func__);
	} else
		dp_info(dp, "%s: DP State is not RUN\n", __func__);

	mutex_unlock(&dp->cmd_lock);
	pm_runtime_put(dp->dev);
}

// For BIST
static void dp_parse_edid(struct dp_device *dp, struct edid *edid)
{
	u8 *edid_vendor = dp->sink.edid_manufacturer;
	u32 edid_prod_id = 0;

	if (edid == NULL)
		return;

	edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
	edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
			  ((edid->mfg_id[1] & 0xe0) >> 5)) +
			 '@';
	edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';

	edid_prod_id |= EDID_PRODUCT_ID(edid);

	dp->sink.edid_product = edid_prod_id;
	dp->sink.edid_serial = edid->serial;

	drm_edid_get_monitor_name(edid, dp->sink.sink_name, SINK_NAME_LEN);

	dp_info(dp, "EDID: Sink Manufacturer: %s\n", dp->sink.edid_manufacturer);
	dp_info(dp, "EDID: Sink Product: %x\n", dp->sink.edid_product);
	dp_info(dp, "EDID: Sink Serial: %x\n", dp->sink.edid_serial);
	dp_info(dp, "EDID: Sink Name: %s\n", dp->sink.sink_name);
}

static void dp_clean_drm_modes(struct dp_device *dp)
{
	struct drm_display_mode *mode, *t;
	struct drm_connector *connector = &dp->connector;

	memset(&dp->cur_mode, 0, sizeof(struct drm_display_mode));

	list_for_each_entry_safe (mode, t, &connector->probed_modes, head) {
		list_del(&mode->head);
		drm_mode_destroy(connector->dev, mode);
	}
}

static const struct drm_display_mode failsafe_mode[1] = {
	/* 1 - 640x480@60Hz 4:3 */
	{
		DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656, 752,
			 800, 0, 480, 490, 492, 525, 0,
			 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
		.picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3,
	}
};

static void dp_sad_to_audio_info(struct dp_device *dp, struct cea_sad *sads)
{
	int i;

	dp->sink.has_pcm_audio = false;

	/* enum hdmi_audio_coding_type & enum hdmi_audio_sample_frequency are defined in hdmi.h */
	for (i = 0; i < dp->num_sads; i++) {
		dp_info(dp, "EDID: SAD %d: fmt: 0x%02x, ch: 0x%02x, freq: 0x%02x, byte2: 0x%02x\n",
			i + 1, sads[i].format, sads[i].channels, sads[i].freq, sads[i].byte2);

		if (sads[i].format == HDMI_AUDIO_CODING_TYPE_PCM) {
			dp->sink.has_pcm_audio = true;
			dp->sink.audio_ch_num = sads[i].channels + 1;
			dp->sink.audio_sample_rates = sads[i].freq;
			dp->sink.audio_bit_rates = sads[i].byte2;
		}
	}

	if (dp->sink.has_pcm_audio)
		dp_info(dp, "EDID: PCM audio: ch: %u, sample_rates: 0x%02x, bit_rates: 0x%02x\n",
			dp->sink.audio_ch_num, dp->sink.audio_sample_rates,
			dp->sink.audio_bit_rates);
}

static const u8 dp_fake_edid[EDID_LENGTH] = {
	/* header */
	0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0,
	/* vendor/product info */
	0x1d, 0xef, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0x21,
	/* EDID version */
	0x1, 0x2,
	/* basic display parameters */
	0xa5, 0x46, 0x27, 0x78, 0x0,
	/* color characteristics */
	0xba, 0xc5, 0xa9, 0x53, 0x4e, 0xa6, 0x25, 0xe, 0x50, 0x54,
	/* established timings: 640x480 @ 60 */
	0x20, 0x0, 0x0,
	/* standard timings: none */
	0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
	0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
	/* detailed timings: none */
	0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	/* extension flag + checksum */
	0x0, 0x97,
};

static enum drm_mode_status dp_conn_mode_valid(struct drm_connector *connector,
						struct drm_display_mode *mode);

static void dp_select_bist_mode(struct dp_device *dp)
{
	struct drm_connector *connector = &dp->connector;
	struct drm_display_mode *mode;

	drm_mode_sort(&connector->probed_modes);

	/* pick the largest @ 60 Hz mode that works */
	list_for_each_entry(mode, &connector->probed_modes, head) {
		if (dp_conn_mode_valid(connector, mode) == MODE_OK &&
		    drm_mode_vrefresh(mode) == 60) {
			goto done;
		}
	}

	/* fallback: pick the largest mode that works */
	list_for_each_entry(mode, &connector->probed_modes, head) {
		if (dp_conn_mode_valid(connector, mode) == MODE_OK) {
			goto done;
		}
	}

	/* last resort: failsafe mode */
	mode = (struct drm_display_mode *)failsafe_mode;

done:
	dp_info(dp, "Mode(" DRM_MODE_FMT ") is requested\n", DRM_MODE_ARG(mode));
	drm_mode_copy(&dp->cur_mode, mode);
}

static void dp_on_by_hpd_plug(struct dp_device *dp)
{
	struct drm_connector *connector = &dp->connector;
	struct drm_device *dev = connector->dev;
	struct edid *edid;
	struct cea_sad *sads;
	struct drm_display_mode *fs_mode;

	edid = drm_do_get_edid(connector, dp_get_edid_block, dp);
	if (!edid) {
		dp_err(dp, "EDID: failed to read EDID from sink, using fake EDID\n");
		dp->stats.edid_read_failures++;
		edid = kmemdup(dp_fake_edid, EDID_LENGTH, GFP_KERNEL);
	} else if (!drm_edid_is_valid(edid)) {
		dp_err(dp, "EDID: invalid EDID, using fake EDID\n");
		dp->stats.edid_invalid_failures++;
		kfree(edid);
		edid = kmemdup(dp_fake_edid, EDID_LENGTH, GFP_KERNEL);
	}

	if (drm_connector_update_edid_property(connector, edid))
		dp_err(dp, "EDID: drm_connector_update_edid_property() failed\n");

	dp_parse_edid(dp, edid);
	mutex_lock(&dev->mode_config.mutex);
	dp_clean_drm_modes(dp);
	dp->num_modes = drm_add_edid_modes(connector, edid);
	fs_mode = drm_mode_duplicate(connector->dev, failsafe_mode);
	if (fs_mode) {
		drm_mode_probed_add(connector, fs_mode);
		dp->num_modes++;
	}
	mutex_unlock(&dev->mode_config.mutex);

	dp->num_sads = drm_edid_to_sad(edid, &sads);
	dp_sad_to_audio_info(dp, sads);
	if (dp->num_sads > 0)
		kfree(sads);
	kfree(edid);

	dp->state = DP_STATE_ON;
	dp_info(dp, "%s: DP State changed to ON\n", __func__);

	if (dp->bist_mode == DP_BIST_OFF) {
		/*
		 * Notify userspace only about DP video path here.
		 * Once the DP connection usage has been confirmed,
		 * then enable HDCP and DP audio in dp_conn_atomic_check.
		 */
		if (dev) {
			connector->status = connector_status_connected;
			dp_info(dp,
				"call drm_kms_helper_hotplug_event (connected)\n");
			drm_kms_helper_hotplug_event(dev);
		}
	} else {
		/* BIST mode */
		dp_select_bist_mode(dp);
		dp_enable(&dp->encoder);
		if (dp->bist_mode == DP_BIST_ON_HDCP)
			hdcp_dplink_connect_state(DP_CONNECT);
	}
}

static int dp_wait_audio_state_change(struct dp_device *dp, int max_wait_time,
				      enum dp_audio_state state)
{
	int ret = 0;
	int wait_cnt = max_wait_time;

	do {
		wait_cnt--;
		usleep_range(1000, 1030);
	} while ((dp_get_audio_state(dp) != state) && (wait_cnt > 0));

	dp_info(dp, "dp_wait_audio_state_change: time = %d ms, state = %d\n",
		max_wait_time - wait_cnt, state);

	if (wait_cnt == 0) {
		dp_err(dp, "dp_wait_audio_state_change: timeout\n");
		ret =  -ETIME;
	} else
		ret = wait_cnt;

	return ret;

}

static int dp_wait_state_change(struct dp_device *dp, int max_wait_time,
				enum dp_state state)
{
	int ret = 0;
	int wait_cnt = max_wait_time;

	do {
		wait_cnt--;
		usleep_range(1000, 1030);
	} while ((state != dp->state) && (wait_cnt > 0));

	dp_info(dp, "dp_wait_state_change: time = %d ms, state = %d\n",
		max_wait_time - wait_cnt, state);

	if (wait_cnt == 0) {
		dp_err(dp, "dp_wait_state_change: timeout\n");
		ret = -ETIME;
	} else
		ret = wait_cnt;

	return ret;
}

static void dp_off_by_hpd_plug(struct dp_device *dp)
{
	struct drm_connector *connector = &dp->connector;
	struct drm_device *dev = connector->dev;
	int timeout = 0;

	if (dp->state >= DP_STATE_ON) {
		if (dp->bist_mode == DP_BIST_OFF) {
			hdcp_dplink_connect_state(DP_DISCONNECT);

			if (dev) {
				connector->status =
					connector_status_disconnected;
				dp_info(dp,
					"call drm_kms_helper_hotplug_event (disconnected)\n");
				drm_kms_helper_hotplug_event(dev);
			}

			if (dp->sink.has_pcm_audio) {
				dp_info(dp, "call DP audio notifier (disconnected)\n");
				blocking_notifier_call_chain(&dp_ado_notifier_head, -1UL, NULL);
			}

			dp->hdcp_and_audio_enabled = false;

			/* Wait Audio is stopped if Audio is working. */
			if (dp_get_audio_state(dp) != DP_AUDIO_DISABLE) {
				timeout = dp_wait_audio_state_change(dp, 3000, DP_AUDIO_DISABLE);
				if (timeout == -ETIME)
					dp_err(dp, "dp_wait_audio_state_change: timeout for disable\n");
			}

			/* Wait DRM/KMS Stop */
			timeout = dp_wait_state_change(dp, 3000, DP_STATE_ON);
			if (timeout == -ETIME) {
				dp_err(dp, "dp_wait_state_change: timeout for disable\n");
				dp_disable(&dp->encoder);
			}
		} else {
			/* BIST mode */
			if (dp->bist_mode == DP_BIST_ON_HDCP)
				hdcp_dplink_connect_state(DP_DISCONNECT);
			dp_disable(&dp->encoder);
		}
	}
}

static void dp_get_voltage_and_pre_emphasis_max_reach(u8 *voltage_swing,
						    u8 *pre_emphasis, u8 *max_reach_value)
{
	int i;

	for (i = 0; i < MAX_LANE_CNT; i++) {
		if (voltage_swing[i] >= MAX_VOLTAGE_LEVEL)
			max_reach_value[i] |= DP_TRAIN_MAX_SWING_REACHED;

		if (pre_emphasis[i] >= MAX_PREEMPH_LEVEL)
			max_reach_value[i] |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
	}
}

static void dp_automated_test_set_lane_req(struct dp_device *dp, u8 *val)
{
	u8 voltage_swing[MAX_LANE_CNT];
	u8 pre_emphasis[MAX_LANE_CNT];
	u8 max_reach_value[MAX_LANE_CNT] = {0, };
	u8 lanes_data[MAX_LANE_CNT];
	int i;

	voltage_swing[0] = (val[0] & DP_ADJUST_VOLTAGE_SWING_LANE0_MASK) >>
			   DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT;
	pre_emphasis[0]  = (val[0] & DP_ADJUST_PRE_EMPHASIS_LANE0_MASK) >>
			   DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT;
	voltage_swing[1] = (val[0] & DP_ADJUST_VOLTAGE_SWING_LANE1_MASK) >>
			   DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT;
	pre_emphasis[1]  = (val[0] & DP_ADJUST_PRE_EMPHASIS_LANE1_MASK) >>
			   DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT;
	voltage_swing[2] = (val[1] & DP_ADJUST_VOLTAGE_SWING_LANE0_MASK) >>
			   DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT;
	pre_emphasis[2]  = (val[1] & DP_ADJUST_PRE_EMPHASIS_LANE0_MASK) >>
			   DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT;
	voltage_swing[3] = (val[1] & DP_ADJUST_VOLTAGE_SWING_LANE1_MASK) >>
			   DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT;
	pre_emphasis[3]  = (val[1] & DP_ADJUST_PRE_EMPHASIS_LANE1_MASK) >>
			   DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT;

	for (i = 0; i < MAX_LANE_CNT; i++) {
		dp_info(dp, "AutoTest: voltage swing[%d] = %x\n", i, voltage_swing[i]);
		dp_info(dp, "AutoTest: pre_emphasis[%d] = %x\n", i, pre_emphasis[i]);
	}

	dp_hw_set_voltage_and_pre_emphasis(&dp->hw_config, voltage_swing, pre_emphasis);
	dp_get_voltage_and_pre_emphasis_max_reach(voltage_swing, pre_emphasis,
						max_reach_value);

	lanes_data[0] = (pre_emphasis[0] << DP_TRAIN_PRE_EMPHASIS_SHIFT) |
			voltage_swing[0] | max_reach_value[0];
	lanes_data[1] = (pre_emphasis[1] << DP_TRAIN_PRE_EMPHASIS_SHIFT) |
			voltage_swing[1] | max_reach_value[1];
	lanes_data[2] = (pre_emphasis[2] << DP_TRAIN_PRE_EMPHASIS_SHIFT) |
			voltage_swing[2] | max_reach_value[2];
	lanes_data[3] = (pre_emphasis[3] << DP_TRAIN_PRE_EMPHASIS_SHIFT) |
			voltage_swing[3] | max_reach_value[3];

	dp_info(dp, "AutoTest: set %02x %02x %02x %02x\n",
		lanes_data[0], lanes_data[1], lanes_data[2], lanes_data[3]);

	drm_dp_dpcd_write(&dp->dp_aux, DP_TRAINING_LANE0_SET, lanes_data, MAX_LANE_CNT);
}

static int dp_automated_test_irq_handler(struct dp_device *dp)
{
	u8 dpcd_test_req = 0, dpcd_test_res = 0;
	u8 dpcd_req_lane[2], dpcd_phy_test_pattern = 0;

	drm_dp_dpcd_readb(&dp->dp_aux, DP_TEST_REQUEST, &dpcd_test_req);

	dpcd_test_res = DP_TEST_ACK;
	drm_dp_dpcd_writeb(&dp->dp_aux, DP_TEST_RESPONSE, dpcd_test_res);

	if (dpcd_test_req & DP_TEST_LINK_PHY_TEST_PATTERN) {
		dp_hw_stop();
		msleep(120);

		/* Set Swing & Preemp */
		drm_dp_dpcd_read(&dp->dp_aux, DP_ADJUST_REQUEST_LANE0_1, dpcd_req_lane, 2);
		dp_automated_test_set_lane_req(dp, dpcd_req_lane);

		/* Set Quality Pattern */
		drm_dp_dpcd_readb(&dp->dp_aux, DP_PHY_TEST_PATTERN, &dpcd_phy_test_pattern);
		switch (dpcd_phy_test_pattern & DP_PHY_TEST_PATTERN_SEL_MASK) {
		case DP_PHY_TEST_PATTERN_NONE:
			dp_hw_set_quality_pattern(DISABLE_PATTERN, ENABLE_SCRAM);
			break;
		case DP_PHY_TEST_PATTERN_D10_2:
			dp_hw_set_quality_pattern(D10_2_PATTERN, DISABLE_SCRAM);
			break;
		case DP_PHY_TEST_PATTERN_ERROR_COUNT:
			dp_hw_set_quality_pattern(SERP_PATTERN, ENABLE_SCRAM);
			break;
		case DP_PHY_TEST_PATTERN_PRBS7:
			dp_hw_set_quality_pattern(PRBS7, DISABLE_SCRAM);
			break;
		case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
			dp_hw_set_quality_pattern(CUSTOM_80BIT, DISABLE_SCRAM);
			break;
		case DP_PHY_TEST_PATTERN_CP2520:
			dp_hw_set_quality_pattern(HBR2_COMPLIANCE, ENABLE_SCRAM);
			break;
		default:
			dp_err(dp, "Not Supported PHY_TEST_PATTERN: %02x\n", dpcd_phy_test_pattern);
			break;
		}
	} else {
		dp_err(dp, "Not Supported AUTOMATED_TEST_REQUEST: %02x\n", dpcd_test_req);
		return -EINVAL;
	}

	return 0;
}

static void dp_update_link_status(struct dp_device *dp, enum link_training_status link_status)
{
	if (link_status != dp->typec_link_training_status) {
		dp->typec_link_training_status = link_status;
		sysfs_notify(&dp->dev->kobj, "drm-displayport", "link_status");
	}
}

static int dp_link_down_event_handler(struct dp_device *dp)
{
	int ret;

	/* Step_1. DP Off */
	dp_off_by_hpd_plug(dp);

	/* Step_2. DP Link Up again */
	ret = dp_link_up(dp);
	if (ret < 0) {
		if (ret == -ENOLINK)
			dp_update_link_status(dp, LINK_TRAINING_FAILURE);
		else
			dp_update_link_status(dp, LINK_TRAINING_FAILURE_SINK);
		dp_err(dp, "failed to DP Link Up during re-negotiation\n");
		return ret;
	}

	/* Step_3. DP On */
	dp_update_link_status(dp, LINK_TRAINING_SUCCESS);
	dp_on_by_hpd_plug(dp);

	return 0;
}

static int dp_downstream_port_event_handler(struct dp_device *dp, int new_sink_count)
{
	int ret;

	if (dp->sink_count == 0 && new_sink_count > 0) {
		/* establish DP link */
		dp->sink_count = new_sink_count;
		ret = dp_link_up(dp);
		if (ret < 0) {
			if (ret == -ENOLINK)
				dp_update_link_status(dp, LINK_TRAINING_FAILURE);
			else
				dp_update_link_status(dp, LINK_TRAINING_FAILURE_SINK);
			dp_err(dp, "failed to DP Link Up during DFP event\n");
			return ret;
		}

		dp_update_link_status(dp, LINK_TRAINING_SUCCESS);
		dp_on_by_hpd_plug(dp);
	} else if (dp->sink_count > 0 && new_sink_count == 0) {
		/* tear down DP link */
		dp->sink_count = new_sink_count;
		dp_off_by_hpd_plug(dp);
		dp_link_down(dp);
	} else {
		dp->sink_count = new_sink_count;
	}

	return 0;
}

/* Works */
static void dp_work_hpd(struct work_struct *work)
{
	struct dp_device *dp = get_dp_drvdata();
	struct drm_connector *connector = &dp->connector;
	struct drm_device *dev = connector->dev;
	struct exynos_drm_private *private = drm_to_exynos_dev(dev);
	enum link_training_status link_status = LINK_TRAINING_UNKNOWN;
	int ret;

	mutex_lock(&dp->hpd_lock);

	if (dp_get_hpd_state(dp) == EXYNOS_HPD_PLUG) {
		if (mutex_trylock(&private->dp_tui_lock) == 0) {
			/* TUI is active, bail out */
			dp_info(dp, "unable to handle HPD_PLUG, TUI is active\n");
			mutex_unlock(&dp->hpd_lock);
			return;
		}

		pm_runtime_get_sync(dp->dev);
		dp_debug(dp, "pm_rtm_get_sync usage_cnt(%d)\n",
			 atomic_read(&dp->dev->power.usage_count));
		dp_enable_dposc(dp);
		pm_stay_awake(dp->dev);

		/* PHY power on */
		usleep_range(10000, 10030);
		ret = dp_hw_init(&dp->hw_config); /* for AUX ch read/write. */
		if (ret) {
			dp_err(dp, "dp_hw_init() failed\n");
			goto HPD_FAIL;
		}
		usleep_range(10000, 11000);

		ret = dp_link_up(dp);
		if (ret < 0) {
			if (ret == -ENOLINK)
				link_status = LINK_TRAINING_FAILURE;
			else
				link_status = LINK_TRAINING_FAILURE_SINK;
			dp_err(dp, "failed to DP Link Up\n");
			goto HPD_FAIL;
		}

		if (dp->sink_count > 0) {
			dp_update_link_status(dp, LINK_TRAINING_SUCCESS);
			dp_on_by_hpd_plug(dp);
		}
	} else if (dp_get_hpd_state(dp) == EXYNOS_HPD_UNPLUG) {
		if ((!pm_runtime_get_if_in_use(dp->dev)) ||
		    (dp->state == DP_STATE_INIT)) {
			dp_info(dp, "%s: DP is not ON\n", __func__);
			mutex_unlock(&dp->hpd_lock);
			return;
		}

		if (dp->sink_count > 0) {
			dp_off_by_hpd_plug(dp);
			dp_link_down(dp);
		}

		/* PHY power off */
		dp_hw_deinit(&dp->hw_config);
		dp_disable_dposc(dp);

		pm_runtime_put(dp->dev);
		/* put runtime power obtained during HPD_PLUG */
		pm_runtime_put_sync(dp->dev);
		dp_debug(dp, "pm_rtm_put_sync usage_cnt(%d)\n",
			 atomic_read(&dp->dev->power.usage_count));
		pm_relax(dp->dev);

		dp->state = DP_STATE_INIT;
		dp_info(dp, "%s: DP State changed to INIT\n", __func__);

		mutex_unlock(&private->dp_tui_lock);
	}

	mutex_unlock(&dp->hpd_lock);

	return;

HPD_FAIL:
	dp_err(dp, "HPD FAIL Check CCIC or USB!!\n");
	dp_set_hpd_state(dp, EXYNOS_HPD_UNPLUG);
	dp_info(dp, "DP HPD changed to EXYNOS_HPD_UNPLUG\n");
	hdcp_dplink_connect_state(DP_DISCONNECT);
	dp_hw_deinit(&dp->hw_config);
	dp_disable_dposc(dp);
	pm_relax(dp->dev);

	dp_init_info(dp);
	pm_runtime_put_sync(dp->dev);
	dp_debug(dp, "pm_rtm_put_sync usage_cnt(%d)\n",
		 atomic_read(&dp->dev->power.usage_count));

	/* in error case, add delay to avoid very short interval reconnection */
	msleep(300);

	// Use cached error so LINK_TRAINING_FAILURE doesn't retrigger hpd immediately.
	dp_update_link_status(dp, link_status);

	// TODO: We need to define more error codes, but for now use just 1 for generic error code
	dp->dp_hotplug_error_code = 1;
	dp_info(dp, "HPD_FAIL, call drm_kms_helper_hotplug_event(dp_hotplug_error_code=%d)\n",
		dp->dp_hotplug_error_code);
	drm_kms_helper_hotplug_event(dp->connector.dev);

	mutex_unlock(&private->dp_tui_lock);
	mutex_unlock(&dp->hpd_lock);
}

static u8 sysfs_triggered_irq = 0;

static void dp_work_hpd_irq(struct work_struct *work)
{
	struct dp_device *dp = get_dp_drvdata();
	u8 sink_count;
	u8 irq = 0, irq2 = 0, irq3 = 0;
	u8 link_status[DP_LINK_STATUS_SIZE];

	if (!pm_runtime_get_if_in_use(dp->dev)) {
		dp_debug(dp, "[HPD IRQ] IRQ work skipped as power is off\n");
		return;
	}

	mutex_lock(&dp->hpd_lock);

	if (sysfs_triggered_irq != 0) {
		irq = sysfs_triggered_irq;
		sysfs_triggered_irq = 0;
		goto process_irq;
	}

	if (dp->sink.revision <= DP_DPCD_REV_12) {
		/*
		 * Some DPCD 1.2 sinks/hubs haven't properly implemented the IRQ ESI registers.
		 * Thus, we will force all DPCD 1.2 sinks/hubs to use the legacy IRQ registers.
		 */
		sink_count = drm_dp_read_sink_count(&dp->dp_aux);
		dp_info(dp, "[HPD IRQ] sink count = %u\n", sink_count);

		if (drm_dp_dpcd_readb(&dp->dp_aux, DP_DEVICE_SERVICE_IRQ_VECTOR, &irq) == 1)
			dp_info(dp, "[HPD IRQ] device irq vector = %02x\n", irq);
		else
			dp_err(dp, "[HPD IRQ] cannot read DP_DEVICE_SERVICE_IRQ_VECTOR\n");

		if (drm_dp_dpcd_read_link_status(&dp->dp_aux, link_status) == DP_LINK_STATUS_SIZE)
			dp_info(dp, "[HPD IRQ] link status = %02x %02x %02x %02x\n",
				link_status[0], link_status[1], link_status[2], link_status[3]);
		else
			dp_err(dp, "[HPD IRQ] cannot read link status\n");
	} else {
		if (drm_dp_dpcd_readb(&dp->dp_aux, DP_SINK_COUNT_ESI, &sink_count) == 1) {
			sink_count = DP_GET_SINK_COUNT(sink_count);
			dp_info(dp, "[HPD IRQ] sink count = %u\n", sink_count);
		} else
			dp_err(dp, "[HPD IRQ] cannot read DP_SINK_COUNT_ESI\n");

		if (drm_dp_dpcd_readb(&dp->dp_aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI0, &irq) == 1)
			dp_info(dp, "[HPD IRQ] device irq vector esi0 = %02x\n", irq);
		else
			dp_err(dp, "[HPD IRQ] cannot read DP_DEVICE_SERVICE_IRQ_VECTOR_ESI0\n");

		if (drm_dp_dpcd_readb(&dp->dp_aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1, &irq2) == 1)
			dp_info(dp, "[HPD IRQ] device irq vector esi1 = %02x\n", irq2);
		else
			dp_err(dp, "[HPD IRQ] cannot read DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1\n");

		if (drm_dp_dpcd_readb(&dp->dp_aux, DP_LINK_SERVICE_IRQ_VECTOR_ESI0, &irq3) == 1)
			dp_info(dp, "[HPD IRQ] link irq vector esi0 = %02x\n", irq3);
		else
			dp_err(dp, "[HPD IRQ] cannot read DP_LINK_SERVICE_IRQ_VECTOR_ESI0\n");

		if (drm_dp_dpcd_read(&dp->dp_aux, DP_LANE0_1_STATUS_ESI, link_status,
				     DP_LINK_STATUS_SIZE) == DP_LINK_STATUS_SIZE)
			dp_info(dp, "[HPD IRQ] link status = %02x %02x %02x %02x\n",
				link_status[0], link_status[1], link_status[2], link_status[3]);
		else
			dp_err(dp, "[HPD IRQ] cannot read link status\n");
	}

	if (dp->dfp_count > 0) {
		if ((link_status[2] & DP_DOWNSTREAM_PORT_STATUS_CHANGED) ||
		    (dp->sink_count != sink_count)) {
			dp_info(dp, "[HPD IRQ] DP downstream port status change\n");
			dp_downstream_port_event_handler(dp, sink_count);
			goto release_irq_resource;
		}

		if (sink_count == 0) {
			goto release_irq_resource;
		}
	}

	if (!drm_dp_channel_eq_ok(link_status, dp->link.num_lanes)) {
		dp_info(dp, "[HPD IRQ] DP link is down, re-establish the link\n");
		dp_link_down_event_handler(dp);
		dp->stats.link_unstable_failures++;
		goto release_irq_resource;
	}

process_irq:
	if (irq & DP_CP_IRQ) {
		dp_info(dp, "[HPD IRQ] Content Protection\n");
		hdcp_dplink_handle_irq();
	} else if (irq & DP_AUTOMATED_TEST_REQUEST) {
		dp_info(dp, "[HPD IRQ] Automated Test Request\n");
		dp_automated_test_irq_handler(dp);
	} else if (irq & DP_SINK_SPECIFIC_IRQ) {
		dp_info(dp, "[HPD IRQ] Sink Specific\n");
		dp_link_down_event_handler(dp);
	} else
		dp_info(dp, "[HPD IRQ] unknown IRQ (0x%X)\n", irq);

release_irq_resource:
	mutex_unlock(&dp->hpd_lock);
	pm_runtime_put(dp->dev);
}

/* Type-C Handshaking Functions */
static void dp_hpd_changed(struct dp_device *dp, enum hotplug_state state)
{
	if (dp_get_hpd_state(dp) == state) {
		dp_debug(dp, "DP HPD is same state (%x): Skip\n", state);
		return;
	}

	if ((state == EXYNOS_HPD_PLUG) || (state == EXYNOS_HPD_UNPLUG)) {
		dp_set_hpd_state(dp, state);
		queue_work(dp->dp_wq, &dp->hpd_work);
	} else
		dp_err(dp, "DP HPD changed to abnormal state(%d)\n", state);
}

static bool dp_enabled = false;
module_param(dp_enabled, bool, 0664);
MODULE_PARM_DESC(dp_enabled, "Enable/disable DP notification processing");

static int param_dp_emulation_mode_get(char *buf, const struct kernel_param *kp)
{
	return sysfs_emit(buf, "%d\n", dp_emulation_mode);
}

static int param_dp_emulation_mode_set(const char *val, const struct kernel_param *kp)
{
	u32 new_value = 0;

	if (kstrtou32(val, 10, &new_value)) {
		dp_warn(dp_drvdata, "%s: dp_emulation_mode parse error\n", __func__);
		return -EINVAL;
	}

	if (new_value != dp_emulation_mode) {
		dp_info(dp_drvdata, "%s: dp_emulation_mode=%d\n", __func__, new_value);
		dp_emulation_mode = new_value;
		if (dp_emulation_mode) {
			memset(&dp_drvdata->hw_config, 0, sizeof(struct dp_hw_config));
			dp_drvdata->hw_config.orient_type = PLUG_NORMAL;
			dp_drvdata->hw_config.num_lanes = 4;
			dp_drvdata->hw_config.dp_emul = true;
			dp_hpd_changed(dp_drvdata, EXYNOS_HPD_PLUG);
		} else
			dp_hpd_changed(dp_drvdata, EXYNOS_HPD_UNPLUG);
	}
	return 0;
}

static const struct kernel_param_ops param_ops_dp_emulation_mode = {
	.get = param_dp_emulation_mode_get,
	.set = param_dp_emulation_mode_set,
};

module_param_cb(dp_emulation_mode, &param_ops_dp_emulation_mode, NULL, 0664);
MODULE_PARM_DESC(dp_emulation_mode, "Enable DisplayPort emulation mode");

/*
 * Function should be called with typec_notification_lock held.
 */
static int usb_typec_dp_notification_locked(struct dp_device *dp, enum hotplug_state hpd)
{
	if (!dp_enabled) {
		dp_info(dp, "%s: DP is disabled, ignoring DP notifications\n", __func__);
		return NOTIFY_OK;
	}

	if (hpd == EXYNOS_HPD_PLUG) {
		if (dp_get_hpd_state(dp) == EXYNOS_HPD_UNPLUG) {
			dp_info(dp, "%s: USB Type-C is HPD PLUG status\n", __func__);

			memset(&dp->hw_config, 0, sizeof(struct dp_hw_config));
			dp_info(dp, "%s: disp_orientation = %d\n", __func__, dp->typec_orientation);
			dp->hw_config.orient_type = dp->typec_orientation;

			dp_info(dp, "%s: disp_pin_config = %d\n", __func__,
				dp->typec_pin_assignment);
			dp->hw_config.pin_type = dp->typec_pin_assignment;

			dp->hw_config.phy_boost = dp_phy_boost;
			dp_hpd_changed(dp, EXYNOS_HPD_PLUG);
		}
	} else if (hpd == EXYNOS_HPD_IRQ) {
		if (dp_get_hpd_state(dp) == EXYNOS_HPD_PLUG) {
			dp_info(dp, "%s: Service IRQ from sink\n", __func__);
			queue_work(dp->dp_wq, &dp->hpd_irq_work);
		}
	} else {
		dp_info(dp, "%s: USB Type-C is HPD UNPLUG status, or not in display ALT mode\n",
			__func__);

		if (dp_get_hpd_state(dp) == EXYNOS_HPD_PLUG)
			dp_hpd_changed(dp, EXYNOS_HPD_UNPLUG);

		/* Mark unknown on HPD UNPLUG */
		dp_update_link_status(dp, LINK_TRAINING_UNKNOWN);
	}

	return NOTIFY_OK;
}

/* Audio(ALSA) Handshaking Functions */
int dp_audio_config(struct dp_audio_config *audio_config)
{
	struct dp_device *dp = get_dp_drvdata();

	if (dp->state == DP_STATE_INIT) {
		dp_warn(dp, "DP Power Status is off\n");
		return -EINVAL;
	}

	dp_info(dp, "audio state (%d ==> %d)\n",
		dp_get_audio_state(dp), audio_config->audio_state);

	if (audio_config->audio_state == dp_get_audio_state(dp))
		return 0;

	dp_set_audio_state(dp, audio_config->audio_state);

	switch (dp_get_audio_state(dp)) {
	case DP_AUDIO_ENABLE:
		dp_hw_init_audio();
		break;
	case DP_AUDIO_START:
		dp_info(dp, "audio_config: ch(%d), fs(%d), bit(%d), packed(%d), word_len(%d)\n",
				audio_config->num_audio_ch, audio_config->audio_fs,
				audio_config->audio_bit, audio_config->audio_packed_mode,
				audio_config->audio_word_length);

		dp->hw_config.num_audio_ch = audio_config->num_audio_ch;
		dp->hw_config.audio_fs = audio_config->audio_fs;
		dp->hw_config.audio_bit = audio_config->audio_bit;
		dp->hw_config.audio_packed_mode = audio_config->audio_packed_mode;
		dp->hw_config.audio_word_length = audio_config->audio_word_length;

		dp_hw_set_audio_config(&dp->hw_config);
		dp_hw_start_audio();
		dp_set_audio_infoframe(dp);
		break;
	case DP_AUDIO_REQ_BUF_READ:
		dp_hw_set_audio_dma(1);
		break;
	case DP_AUDIO_WAIT_BUF_FULL:
		dp_hw_set_audio_dma(0);
		break;
	case DP_AUDIO_STOP:
		dp_hw_stop_audio();
		dp_set_audio_infoframe(dp);
		break;
	case DP_AUDIO_DISABLE:
		dp_hw_deinit_audio();
		break;
	}

	return 0;
}
EXPORT_SYMBOL(dp_audio_config);

/* HDCP Driver Handshaking Functions */
void dp_hdcp_update_cp(u32 drm_cp_status)
{
	struct dp_device *dp = get_dp_drvdata();
	struct drm_connector *connector = &dp->connector;

	dp_info(dp, "dp_hdcp_update_cp to %d\n", drm_cp_status);

	drm_modeset_lock(&connector->dev->mode_config.connection_mutex, NULL);
	drm_hdcp_update_content_protection(connector, drm_cp_status);
	drm_modeset_unlock(&connector->dev->mode_config.connection_mutex);
}
EXPORT_SYMBOL(dp_hdcp_update_cp);

int dp_dpcd_read_for_hdcp22(u32 address, u32 length, u8 *data)
{
	struct dp_device *dp = get_dp_drvdata();
	int ret = -EFAULT;

	if (pm_runtime_get_if_in_use(dp->dev)) {
		ret = drm_dp_dpcd_read(&dp->dp_aux, address, data, length);
		pm_runtime_put(dp->dev);
	}

	if (ret == length)
		return 0;

	dp_err(dp, "dpcd_read_for_hdcp22 fail(%d): 0x%X\n", ret, address);
	return (ret < 0) ? ret : -EIO;
}
EXPORT_SYMBOL(dp_dpcd_read_for_hdcp22);

int dp_dpcd_write_for_hdcp22(u32 address, u32 length, u8 *data)
{
	struct dp_device *dp = get_dp_drvdata();
	int ret = -EFAULT;

	if (pm_runtime_get_if_in_use(dp->dev)) {
		ret = drm_dp_dpcd_write(&dp->dp_aux, address, data, length);
		pm_runtime_put(dp->dev);
	}

	if (ret == length)
		return 0;

	dp_err(dp, "dpcd_write_for_hdcp22 fail(%d): 0x%X\n", ret, address);
	return (ret < 0) ? ret : -EIO;
}
EXPORT_SYMBOL(dp_dpcd_write_for_hdcp22);

/* DP DRM Connector Helper Functions */
static enum drm_mode_status dp_mode_valid(struct drm_encoder *encoder,
					  const struct drm_display_mode *mode)
{
	return MODE_OK;
}

static void dp_atomic_mode_set(struct drm_encoder *encoder,
			       struct drm_crtc_state *crtc_state,
			       struct drm_connector_state *conn_state)
{
	struct dp_device *dp = encoder_to_dp(encoder);
	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;

	dp_info(dp, "Mode(" DRM_MODE_FMT ") is requested\n",
		DRM_MODE_ARG(adjusted_mode));

	if (!drm_mode_equal(&dp->cur_mode, adjusted_mode)) {
		drm_mode_copy(&dp->cur_mode, adjusted_mode);
	}
}

static int dp_atomic_check(struct drm_encoder *encoder,
			   struct drm_crtc_state *crtc_state,
			   struct drm_connector_state *state)
{
	return 0;
}

static const struct drm_encoder_helper_funcs dp_encoder_helper_funcs = {
	.mode_valid = dp_mode_valid,
	.atomic_mode_set = dp_atomic_mode_set,
	.enable = dp_enable,
	.disable = dp_disable,
	.atomic_check = dp_atomic_check,
};

/* DP DRM Encoder Functions */
static const struct drm_encoder_funcs dp_encoder_funcs = {
	.destroy = drm_encoder_cleanup,
};

static void dp_connector_reset(struct drm_connector *connector)
{
	/* Need to preserve max BPC settings over reset */
	u8 max_bpc = connector->state->max_bpc;
	u8 max_req_bpc = connector->state->max_requested_bpc;

	drm_atomic_helper_connector_reset(connector);

	connector->state->max_bpc = max_bpc;
	connector->state->max_requested_bpc = max_req_bpc;
}

/* DP DRM Connector Functions */
static ssize_t dp_crc_enabled_write(struct file *file, const char __user *buffer, size_t len,
				    loff_t *ppos)
{
	struct seq_file *s = file->private_data;
	struct dp_device *dp = s->private;
	int res;
	int enable = 0;

	res = kstrtoint_from_user(buffer, len, 0, &enable);
	if (res) {
		dp_warn(dp, "%s: invalid value %s\n", __func__, buffer);
		return len;
	}

	res = dp_crc_set_enabled(SST1, enable);
	if (res == 0) {
		mutex_lock(&dp->cmd_lock);
		dp->dp_link_crc_enabled = enable ? true : false;
		mutex_unlock(&dp->cmd_lock);
		dp_debug(dp, "%s: dp_crc_set_enabled(SST1, %d) successfully\n", __func__, enable);
	} else
		dp_warn(dp, "%s: dp_crc_set_enabled(SST1, %d) failed res=%d\n", __func__, enable,
			res);

	return len;
}

static int dp_crc_enabled_show(struct seq_file *s, void *unused)
{
	struct dp_device *dp = s->private;

	mutex_lock(&dp->cmd_lock);
	seq_printf(s, "%d\n", dp->dp_link_crc_enabled);
	mutex_unlock(&dp->cmd_lock);
	return 0;
}

static int dp_crc_enabled_open(struct inode *inode, struct file *file)
{
	return single_open(file, dp_crc_enabled_show, inode->i_private);
}

static const struct file_operations dp_crc_enabled_fops = {
	.owner = THIS_MODULE,
	.open = dp_crc_enabled_open,
	.read = seq_read,
	.write = dp_crc_enabled_write,
	.llseek = seq_lseek,
	.release = single_release,
};

static int dp_crc_values_show(struct seq_file *s, void *unused)
{
	struct dp_device *dp = s->private;
	bool crc_enabled = false;
	int res = 0;
	u32 crc_data[3];

	mutex_lock(&dp->cmd_lock);
	crc_enabled = dp->dp_link_crc_enabled;
	mutex_unlock(&dp->cmd_lock);
	if (!crc_enabled) {
		seq_puts(s, "CRCs are disabled\n");
		return 0;
	}

	res = dp_crc_get(SST1, crc_data);
	if (res != 0) {
		seq_printf(s, "dp_crc_get failed, res=%d\n", res);
		dp_warn(dp, "%s: dp_crc_get failed, res=%d\n", __func__, res);
		return 0;
	}

	dp_debug(dp, "%s: Got CRCs R:%04X G:%04X B:%04X\n", __func__, crc_data[0], crc_data[1],
		 crc_data[2]);
	seq_printf(s, "R:%04X G:%04X B:%04X\n", crc_data[0], crc_data[1], crc_data[2]);
	return 0;
}

static int dp_crc_values_open(struct inode *inode, struct file *file)
{
	return single_open(file, dp_crc_values_show, inode->i_private);
}

static ssize_t dp_crc_values_write(struct file *file, const char __user *buffer, size_t len,
				   loff_t *ppos)
{
	struct seq_file *s = file->private_data;
	struct dp_device *dp = s->private;
	int res = 0;

	res = dp_crc_reset(SST1);
	if (res)
		dp_warn(dp, "%s: dp_crc_reset failed, res=%d\n", __func__, res);
	else
		dp_debug(dp, "%s: dp_crc_reset finished successfully\n", __func__);

	return len;
}

static const struct file_operations dp_crc_values_fops = {
	.owner = THIS_MODULE,
	.open = dp_crc_values_open,
	.read = seq_read,
	.write = dp_crc_values_write,
	.llseek = seq_lseek,
	.release = single_release,
};

static int drm_dp_late_register(struct drm_connector *connector)
{
	struct dentry *root = connector->debugfs_entry;

	dp_drvdata->dp_crc_enabled_debugfs_file =
		debugfs_create_file("dp_crc_enabled", 0644, root, dp_drvdata, &dp_crc_enabled_fops);
	dp_drvdata->dp_crc_values_debugfs_file =
		debugfs_create_file("dp_crc_values", 0644, root, dp_drvdata, &dp_crc_values_fops);
	return 0;
}

static void drm_dp_early_unregister(struct drm_connector *connector)
{
	debugfs_remove(dp_drvdata->dp_crc_enabled_debugfs_file);
	debugfs_remove(dp_drvdata->dp_crc_values_debugfs_file);
}

static const struct drm_connector_funcs dp_connector_funcs = {
	.reset = dp_connector_reset,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = drm_connector_cleanup,
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
	.late_register = drm_dp_late_register,
	.early_unregister = drm_dp_early_unregister,
};

/* DP DRM Connector Helper Functions */
static int dp_detect(struct drm_connector *connector,
		     struct drm_modeset_acquire_ctx *ctx, bool force)
{
	struct dp_device *dp = connector_to_dp(connector);

	dp_info(dp, "%s: Connector Status = %d\n", __func__, connector->status);

	return (int)connector->status;
}

static int dp_get_modes(struct drm_connector *connector)
{
	struct dp_device *dp = connector_to_dp(connector);

	if (dp->state == DP_STATE_INIT) {
		dp_warn(dp, "%s: DP is not ON\n", __func__);
		return 0;
	}

	dp_info(dp, "dp->num_modes = %d\n", dp->num_modes);
	return dp->num_modes;
}

static enum drm_mode_status dp_conn_mode_valid(struct drm_connector *connector, struct drm_display_mode *mode)
{
	struct dp_device *dp = connector_to_dp(connector);
	struct drm_display_info *display_info = &connector->display_info;

	/*
	 * drm_atomic_connector_check() hasn't been called yet.
	 * We can't use connector->state->max_bpc directly.
	 * Need to do the same math here.
	 */
	u8 dbpc = display_info->bpc ? display_info->bpc : 8;
	u8 bpc = min(dbpc, connector->state->max_requested_bpc);

	/*
	 * DP link max data rate in Kbps
	 *
	 * dp->link.link_rate unit is weird: Mbps * 100
	 * The calculation becomes:
	 * (dp->link.link_rate * 10) * dp->link.num_lanes * (8 / 10)
	 */
	u32 link_data_rate = dp->link.link_rate * dp->link.num_lanes * 8;

	/* DRM display mode data rate in Kbps */
	u32 mode_data_rate = mode->clock * 3 * bpc;

	if (link_data_rate < mode_data_rate) {
		dp_info(dp, "DROP: " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
		return MODE_CLOCK_HIGH;
	}

	dp_info(dp, "PICK: " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
	return MODE_OK;
}

static int dp_conn_atomic_check(struct drm_connector *c, struct drm_atomic_state *state)
{
	struct dp_device *dp = connector_to_dp(c);

	dp_debug(dp, "%s: c->status=%d dp->state=%d c->dpms=%d dp->hdcp_and_audio_enabled=%d\n",
		 __func__, c->status, dp->state, c->dpms, dp->hdcp_and_audio_enabled);

	if (c->status == connector_status_connected && dp->state == DP_STATE_RUN &&
	    c->dpms == DRM_MODE_DPMS_ON && !dp->hdcp_and_audio_enabled) {
		/*
		 * Connector has transitioned to DRM_MODE_DPMS_ON.
		 * This means DP connection usage has been confirmed.
		 * Enable HDCP and DP audio.
		 */
		hdcp_dplink_connect_state(DP_CONNECT);

		if (dp->sink.has_pcm_audio) {
			dp_info(dp, "call DP audio notifier (connected)\n");
			blocking_notifier_call_chain(&dp_ado_notifier_head, 1UL, NULL);
		}

		dp->hdcp_and_audio_enabled = true;
	}

	return 0;
}

static const struct drm_connector_helper_funcs dp_connector_helper_funcs = {
	.detect_ctx = dp_detect,
	.get_modes = dp_get_modes,
	.mode_valid = dp_conn_mode_valid,
	.atomic_check = dp_conn_atomic_check,
};

/* DP DRM Component Functions */
static int dp_create_connector(struct drm_encoder *encoder)
{
	struct dp_device *dp = encoder_to_dp(encoder);
	struct drm_connector *connector = &dp->connector;
	int ret;

	connector->polled = DRM_CONNECTOR_POLL_HPD;

	ret = drm_connector_init(encoder->dev, connector, &dp_connector_funcs,
				 DRM_MODE_CONNECTOR_DisplayPort);
	if (ret) {
		dp_err(dp, "failed to initialize connector with drm\n");
		return ret;
	}

	connector->status = connector_status_disconnected;
	connector->dpms = DRM_MODE_DPMS_OFF;
	drm_connector_helper_add(connector, &dp_connector_helper_funcs);
	drm_connector_register(connector);
	drm_connector_attach_encoder(connector, encoder);

	return 0;
}

static int dp_create_dp_aux(struct dp_device *dp)
{
	drm_dp_aux_init(&dp->dp_aux);
	dp->dp_aux.dev = dp->dev;
	dp->dp_aux.transfer = dp_aux_transfer;

	return 0;
}

static int dp_bind(struct device *dev, struct device *master, void *data)
{
	struct drm_encoder *encoder = dev_get_drvdata(dev);
	struct dp_device *dp = encoder_to_dp(encoder);
	struct drm_device *drm_dev = data;
	int ret = 0;

	drm_encoder_init(drm_dev, encoder, &dp_encoder_funcs,
			 DRM_MODE_ENCODER_LVDS, NULL);
	drm_encoder_helper_add(encoder, &dp_encoder_helper_funcs);

	encoder->possible_crtcs =
		exynos_drm_get_possible_crtcs(encoder, dp->output_type);
	if (!encoder->possible_crtcs) {
		dp_err(dp, "failed to get possible crtc, ret = %d\n", ret);
		drm_encoder_cleanup(encoder);
		return -ENOTSUPP;
	}

	ret = dp_create_connector(encoder);
	if (ret) {
		dp_err(dp, "failed to create connector ret = %d\n", ret);
		drm_encoder_cleanup(encoder);
		return ret;
	}

	ret = dp_create_dp_aux(dp);
	if (ret) {
		dp_err(dp, "failed to create dp_aux ret = %d\n", ret);
		return ret;
	}

	dp_fill_host_caps(dp);

	drm_atomic_helper_connector_reset(&dp->connector);
	drm_connector_attach_max_bpc_property(&dp->connector, 6, dp->host.max_bpc);
	drm_connector_attach_content_protection_property(&dp->connector,
		DRM_MODE_HDCP_CONTENT_TYPE0);

	ret = sysfs_create_link(&encoder->dev->dev->kobj, &dp->dev->kobj, "displayport");
	if (ret)
		dp_err(dp, "unable to link displayport sysfs (%d)\n", ret);

	dp_info(dp, "DP Driver has been binded\n");

	return ret;
}

static void dp_unbind(struct device *dev, struct device *master, void *data)
{
	struct drm_encoder *encoder = dev_get_drvdata(dev);
	struct dp_device *dp = encoder_to_dp(encoder);

	dp_debug(dp, "%s +\n", __func__);
	sysfs_remove_link(&encoder->dev->dev->kobj, "displayport");
	dp_debug(dp, "%s -\n", __func__);
}

static const struct component_ops dp_component_ops = {
	.bind = dp_bind,
	.unbind = dp_unbind,
};

/* DP DRM Driver */
static int dp_parse_dt(struct dp_device *dp, struct device *dev)
{
	if (IS_ERR_OR_NULL(dev->of_node)) {
		dp_err(dp, "no device tree information\n");
		return -EINVAL;
	}

	return 0;
}

static irqreturn_t dp_irq_handler(int irq, void *dev_data)
{
	struct dp_device *dp = dev_data;
	bool common = false, str = false, pcs = false;
	unsigned int intr_val = 0;

	dp_hw_get_intr_source(&common, &str, &pcs);

	if (common) {
		intr_val = dp_hw_get_and_clear_common_intr();
		dp_debug(dp, "Common Intr = 0x%08X\n", intr_val);
	}

	if (str) {
		intr_val = dp_hw_get_and_clear_video_intr();
		dp_debug(dp, "Video Intr = 0x%08X\n", intr_val);
	}

	if (pcs) {
		dp_debug(dp, "triggered intr from PCS block\n");
	}

	return IRQ_HANDLED;
}

static int dp_remap_regs(struct dp_device *dp, struct platform_device *pdev)
{
	struct resource res;
	struct device *dev = dp->dev;
	struct device_node *np = dev->of_node;
	int i, ret = 0;

	/* DP Link SFR */
	i = of_property_match_string(np, "reg-names", "link");
	if (of_address_to_resource(np, i, &res)) {
		dp_err(dp, "failed to get DP Link resource\n");
		goto err;
	}

	dp->res.link_regs = devm_platform_ioremap_resource_byname(pdev, "link");
	if (IS_ERR(dp->res.link_regs)) {
		dp_err(dp, "failed to remap DP LINK SFR region\n");
		ret = PTR_ERR(dp->res.link_regs);
		goto err;
	}
	dp_regs_desc_init(dp->res.link_regs, res.start, "LINK", REGS_LINK, SST1);

	/* USBDP Combo PHY SFR */
	/*
	 * PHY HW is a Combo PHY for USB and DP.
	 * USB is master IP for this PHY and controlled the life cycle of it.
	 * To avoid abnormal clean-up the resource, it doesn't use managed resource.
	 */
	i = of_property_match_string(np, "reg-names", "phy");
	if (of_address_to_resource(np, i, &res)) {
		dp_err(dp, "failed to get USB/DP Combo PHY resource\n");
		goto err;
	}

	dp->res.phy_regs = ioremap(res.start, resource_size(&res));
	if (IS_ERR(dp->res.phy_regs)) {
		dp_err(dp, "failed to remap USB/DP Combo PHY SFR region\n");
		ret = PTR_ERR(dp->res.phy_regs);
		goto err;
	}
	dp_regs_desc_init(dp->res.phy_regs, res.start, "PHY", REGS_PHY, SST1);

	if (dp_remap_regs_other(dp))
		goto err_phy;

	return 0;

err_phy:
	if (dp->res.phy_regs)
		iounmap(dp->res.phy_regs);
err:
	return ret;
}

static int dp_register_irq(struct dp_device *dp, struct platform_device *pdev)
{
	struct resource *res;
	int ret = 0;

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!res) {
		dp_err(dp, "failed to get irq resource\n");
		return -ENOENT;
	}

	dp->res.irq = res->start;
	ret = devm_request_irq(dp->dev, res->start, dp_irq_handler, 0,
			       pdev->name, dp);
	if (ret) {
		dp_err(dp, "failed to install DP irq\n");
		return -EINVAL;
	}
	disable_irq(dp->res.irq);

	return 0;
}

static int dp_init_resources(struct dp_device *dp, struct platform_device *pdev)
{
	int ret = 0;

	ret = dp_remap_regs(dp, pdev);
	if (ret) {
		dp_err(dp, "failed to remap DP registers\n");
		return -EINVAL;
	}

	ret = dp_get_clock(dp);
	if (ret) {
		dp_err(dp, "failed to get DP clks\n");
		return -EINVAL;
	}

	ret = dp_register_irq(dp, pdev);
	if (ret) {
		dp_err(dp, "failed to get DP interrupts\n");
		return -EINVAL;
	}

	return 0;
}

static const char *const orientations[] = {
	[PLUG_NONE] = "unknown",
	[PLUG_NORMAL] = "normal",
	[PLUG_FLIPPED] = "reverse",
};

static ssize_t orientation_store(struct device *dev, struct device_attribute *attr, const char *buf,
				 size_t size)
{
	int orientation;
	struct dp_device *dp = dev_get_drvdata(dev);

	orientation = sysfs_match_string(orientations, buf);
	if (orientation < 0)
		return orientation;

	dp->typec_orientation = (enum plug_orientation)orientation;
	return size;
}

static ssize_t orientation_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp->hw_config.orient_type);
}
static DEVICE_ATTR_RW(orientation);

static const char *const pin_assignments[] = {
	// Two blanks required because PIN_TYPE_A starts at 2, and
	// sysfs_match_string requires all indices to be filled.
	[0] = "NA",	    [1] = "NA",		[PIN_TYPE_A] = "A", [PIN_TYPE_B] = "B",
	[PIN_TYPE_C] = "C", [PIN_TYPE_D] = "D", [PIN_TYPE_E] = "E", [PIN_TYPE_F] = "F",
};

static ssize_t pin_assignment_store(struct device *dev, struct device_attribute *attr,
				    const char *buf, size_t size)
{
	int pin_assign;
	struct dp_device *dp = dev_get_drvdata(dev);

	pin_assign = sysfs_match_string(pin_assignments, buf);
	if (pin_assign < (int)PIN_TYPE_A)
		return -EINVAL;

	dp->typec_pin_assignment = (enum pin_assignment)pin_assign;
	return size;
}

static ssize_t pin_assignment_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp->hw_config.pin_type);
}
static DEVICE_ATTR_RW(pin_assignment);

static const char *const hpds[] = {
	[EXYNOS_HPD_UNPLUG] = "0",
	[EXYNOS_HPD_PLUG] = "1",
};

static ssize_t hpd_store(struct device *dev, struct device_attribute *attr, const char *buf,
			 size_t size)
{
	int hpd;
	struct dp_device *dp = dev_get_drvdata(dev);

	hpd = sysfs_match_string(hpds, buf);
	if (hpd < 0)
		return hpd;

	mutex_lock(&dp->typec_notification_lock);
	usb_typec_dp_notification_locked(dp, (enum hotplug_state)hpd);
	mutex_unlock(&dp->typec_notification_lock);
	return size;
}

static ssize_t hpd_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp_get_hpd_state(dp));
}
static DEVICE_ATTR_RW(hpd);

static ssize_t dp_hotplug_error_code_show(struct device *dev, struct device_attribute *attr,
					  char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp->dp_hotplug_error_code);
}

static ssize_t dp_hotplug_error_code_store(struct device *dev, struct device_attribute *attr,
					   const char *buf, size_t size)
{
	struct dp_device *dp = get_dp_drvdata();
	int new_value = 0;

	if (kstrtoint(buf, 0, &new_value) < 0) {
		dp_warn(dp, "%s: parse error, buf=%s\n", __func__, buf);
		return -EINVAL;
	}
	dp->dp_hotplug_error_code = new_value;
	return size;
}

static DEVICE_ATTR_RW(dp_hotplug_error_code);

static ssize_t link_status_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp->typec_link_training_status);
}
static DEVICE_ATTR_RO(link_status);

static ssize_t irq_hpd_store(struct device *dev, struct device_attribute *attr, const char *buf,
			     size_t size)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	mutex_lock(&dp->typec_notification_lock);
	usb_typec_dp_notification_locked(dp, EXYNOS_HPD_IRQ);
	mutex_unlock(&dp->typec_notification_lock);
	return size;
}
static DEVICE_ATTR_WO(irq_hpd);

static ssize_t usbc_cable_disconnect_store(struct device *dev, struct device_attribute *attr,
					   const char *buf, size_t size)
{
	hdcp_dplink_connect_state(DP_PHYSICAL_DISCONNECT);

	return size;
}
static DEVICE_ATTR_WO(usbc_cable_disconnect);

static struct attribute *dp_attrs[] = { &dev_attr_orientation.attr,
					&dev_attr_pin_assignment.attr,
					&dev_attr_hpd.attr,
					&dev_attr_dp_hotplug_error_code.attr,
					&dev_attr_link_status.attr,
					&dev_attr_irq_hpd.attr,
					&dev_attr_usbc_cable_disconnect.attr,
					NULL };

static const struct attribute_group dp_group = {
	.name = "drm-displayport",
	.attrs = dp_attrs,
};

static ssize_t link_negotiation_failures_show(struct device *dev, struct device_attribute *attr,
					      char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp->stats.link_negotiation_failures);
}
static DEVICE_ATTR_RO(link_negotiation_failures);

static ssize_t edid_read_failures_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp->stats.edid_read_failures);
}
static DEVICE_ATTR_RO(edid_read_failures);

static ssize_t dpcd_read_failures_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp->stats.dpcd_read_failures);
}
static DEVICE_ATTR_RO(dpcd_read_failures);

static ssize_t edid_invalid_failures_show(struct device *dev, struct device_attribute *attr,
					  char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp->stats.edid_invalid_failures);
}
static DEVICE_ATTR_RO(edid_invalid_failures);

static ssize_t sink_count_invalid_failures_show(struct device *dev, struct device_attribute *attr,
						char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp->stats.sink_count_invalid_failures);
}
static DEVICE_ATTR_RO(sink_count_invalid_failures);

static ssize_t link_unstable_failures_show(struct device *dev, struct device_attribute *attr,
					   char *buf)
{
	struct dp_device *dp = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", dp->stats.link_unstable_failures);
}
static DEVICE_ATTR_RO(link_unstable_failures);

static struct attribute *dp_stats_attrs[] = { &dev_attr_link_negotiation_failures.attr,
					      &dev_attr_edid_read_failures.attr,
					      &dev_attr_dpcd_read_failures.attr,
					      &dev_attr_edid_invalid_failures.attr,
					      &dev_attr_sink_count_invalid_failures.attr,
					      &dev_attr_link_unstable_failures.attr,
					      NULL };

static const struct attribute_group dp_stats_group = {
	.name = "drm-displayport-stats",
	.attrs = dp_stats_attrs,
};

static int dp_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct dp_device *dp = NULL;
	int ret = 0;

	ret = sysfs_create_group(&dev->kobj, &dp_group);
	if (ret) {
		dev_err(dev, "failed to allocate dp attributes\n");
		return ret;
	}

	ret = sysfs_create_group(&dev->kobj, &dp_stats_group);
	if (ret) {
		dev_err(dev, "failed to allocate dp stats attributes\n");
		return ret;
	}

	dp = devm_kzalloc(dev, sizeof(struct dp_device), GFP_KERNEL);
	if (!dp) {
		dev_err(dev, "failed to allocate dp device.\n");
		return -ENOMEM;
	}
	dp->dev = dev;

	ret = dp_parse_dt(dp, dev);
	if (ret) {
		dp_err(dp, "failed to parse dp device tree.\n");
		return ret;
	}

	ret = dp_init_resources(dp, pdev);
	if (ret) {
		dp_err(dp, "failed to init dp resources.\n");
		return ret;
	}

	platform_set_drvdata(pdev, dp);

	/* Driver Initialization */
	dp_drvdata = dp;
	dp_init_info(dp);

	dma_set_mask(dev, DMA_BIT_MASK(32));

	mutex_init(&dp->cmd_lock);
	mutex_init(&dp->hpd_lock);
	mutex_init(&dp->hpd_state_lock);
	mutex_init(&dp->audio_lock);
	mutex_init(&dp->training_lock);
	mutex_init(&dp->typec_notification_lock);

	/* Create WorkQueue & Works for HPD */
	dp->dp_wq = create_singlethread_workqueue(dev_name(&pdev->dev));
	if (!dp->dp_wq) {
		dp_err(dp, "create wq failed.\n");
		ret = -ENOMEM;
		goto err;
	}

	INIT_WORK(&dp->hpd_work, dp_work_hpd);
	INIT_WORK(&dp->hpd_irq_work, dp_work_hpd_irq);

	pm_runtime_enable(dev);

	/* Register callback to HDCP */
	dp_register_func_for_hdcp22(dp_hdcp_update_cp, dp_dpcd_read_for_hdcp22,
		dp_dpcd_write_for_hdcp22);

	dp_info(dp, "DP Driver has been probed.\n");
	return component_add(dp->dev, &dp_component_ops);

err:
	sysfs_remove_group(&dev->kobj, &dp_group);
	sysfs_remove_group(&dev->kobj, &dp_stats_group);
	return ret;
}

static int dp_remove(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct dp_device *dp = platform_get_drvdata(pdev);

	pm_runtime_disable(&pdev->dev);

	mutex_destroy(&dp->cmd_lock);
	mutex_destroy(&dp->hpd_lock);
	mutex_destroy(&dp->hpd_state_lock);
	mutex_destroy(&dp->training_lock);
	mutex_destroy(&dp->typec_notification_lock);

	sysfs_remove_group(&dev->kobj, &dp_group);
	sysfs_remove_group(&dev->kobj, &dp_stats_group);

	destroy_workqueue(dp->dp_wq);

	dp_info(dp, "DP Driver has been removed\n");
	return 0;
}

static const struct of_device_id dp_of_match[] = {
	{ .compatible = "samsung,exynos-dp" },
	{},
};
MODULE_DEVICE_TABLE(of, dp_of_match);

struct platform_driver dp_driver
	__refdata = { .probe = dp_probe,
		      .remove = dp_remove,
		      .driver = {
			      .name = "exynos-drmdp",
			      .owner = THIS_MODULE,
			      .of_match_table = of_match_ptr(dp_of_match),
		      } };

MODULE_AUTHOR("YongWook Shin <yongwook.shin@samsung.com>");
MODULE_DESCRIPTION("Samusung DisplayPort driver");
MODULE_LICENSE("GPL");