summaryrefslogtreecommitdiff
path: root/samsung/exynos_drm_dsim.c
blob: 7ce6ce01dcba5847fd6ec734fef7872ee7c20a2b (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
// SPDX-License-Identifier: GPL-2.0-only
/* exynos_drm_dsi.h
 *
 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
 * Authors:
 *	Donghwa Lee <dh09.lee@samsung.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#define pr_fmt(fmt)  "%s: " fmt, __func__

#include <asm/unaligned.h>

#include <drm/drm_of.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_panel.h>
#include <drm/drm_atomic.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_modes.h>
#include <drm/drm_vblank.h>

#include <linux/clk.h>
#include <linux/console.h>
#include <linux/gpio/consumer.h>
#include <linux/irq.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/of_graph.h>
#include <linux/phy/phy.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/component.h>
#include <linux/iommu.h>

#include <video/mipi_display.h>

#if defined(CONFIG_CPU_IDLE)
#include <soc/google/exynos-cpupm.h>
#endif

#if IS_ENABLED(CONFIG_ARM_EXYNOS_DEVFREQ)
#include <soc/google/exynos-devfreq.h>
#if defined(CONFIG_SOC_GS101)
#include <dt-bindings/soc/google/gs101-devfreq.h>
#elif defined(CONFIG_SOC_GS201)
#include <dt-bindings/soc/google/gs201-devfreq.h>
#endif
#endif

#include <regs-dsim.h>

#include <trace/dpu_trace.h>
#define CREATE_TRACE_POINTS
#include <trace/panel_trace.h>

#include "exynos_drm_connector.h"
#include "exynos_drm_crtc.h"
#include "exynos_drm_decon.h"
#include "exynos_drm_dsim.h"

struct dsim_device *dsim_drvdata[MAX_DSI_CNT];

/*
 * This global mutex lock protects to initialize or de-initialize DSIM and DPHY
 * hardware when multi display is in operation
 */
DEFINE_MUTEX(g_dsim_lock);

#define PANEL_DRV_LEN 64
#define RETRY_READ_FIFO_MAX 10

static char panel_name[PANEL_DRV_LEN];
module_param_string(panel_name, panel_name, sizeof(panel_name), 0644);
MODULE_PARM_DESC(panel_name, "preferred panel name");

static char sec_panel_name[PANEL_DRV_LEN];
module_param_string(sec_panel_name, sec_panel_name, sizeof(sec_panel_name), 0644);
MODULE_PARM_DESC(sec_panel_name, "secondary panel name");

enum panel_priority_index {
	PANEL_PRIORITY_PRI_IDX = 0,
	PANEL_PRIORITY_SEC_IDX = 1,
};
static u8 panel_usage = {0};

#define dsim_info(dsim, fmt, ...)	\
pr_info("%s[%d]: "fmt, dsim->dev->driver->name, dsim->id, ##__VA_ARGS__)

#define dsim_warn(dsim, fmt, ...)	\
pr_warn("%s[%d]: "fmt, dsim->dev->driver->name, dsim->id, ##__VA_ARGS__)

#define dsim_err(dsim, fmt, ...)	\
pr_err("%s[%d]: "fmt, dsim->dev->driver->name, dsim->id, ##__VA_ARGS__)

#define dsim_debug(dsim, fmt, ...)	\
pr_debug("%s[%d]: "fmt, dsim->dev->driver->name, dsim->id, ##__VA_ARGS__)

#define host_to_dsi(host) container_of(host, struct dsim_device, dsi_host)

#define DSIM_ESCAPE_CLK_20MHZ	20

//#define DSIM_BIST

#define DEFAULT_TE_IDLE_US              1000
#define DEFAULT_TE_VARIATION            1

static const struct of_device_id dsim_of_match[] = {
	{ .compatible = "samsung,exynos-dsim",
	  .data = NULL },
	{ }
};
MODULE_DEVICE_TABLE(of, dsim_of_match);

static int dsim_calc_underrun(const struct dsim_device *dsim, uint32_t hs_clock_mhz,
		uint32_t *underrun);

static struct drm_crtc *drm_encoder_get_new_crtc(struct drm_encoder *encoder,
						 struct drm_atomic_state *state)
{
	struct drm_connector *connector;
	const struct drm_connector_state *conn_state;

	connector = drm_atomic_get_new_connector_for_encoder(state, encoder);
	if (!connector)
		return NULL;

	conn_state = drm_atomic_get_new_connector_state(state, connector);
	if (!conn_state)
		return NULL;

	return conn_state->crtc;
}

static struct drm_crtc *drm_encoder_get_old_crtc(struct drm_encoder *encoder,
						 struct drm_atomic_state *state)
{
	struct drm_connector *connector;
	const struct drm_connector_state *conn_state;

	connector = drm_atomic_get_old_connector_for_encoder(state, encoder);
	if (!connector)
		return NULL;

	conn_state = drm_atomic_get_old_connector_state(state, connector);
	if (!conn_state)
		return NULL;

	return conn_state->crtc;
}

static void dsim_dump(struct dsim_device *dsim)
{
	struct dsim_regs regs;
	struct drm_printer p = console_set_on_cmdline ?
		drm_debug_printer("[drm]") : drm_info_printer(dsim->dev);

	drm_printf(&p, "%s[%d]: === DSIM SFR DUMP ===\n",
		dsim->dev->driver->name, dsim->id);

	if (dsim->state != DSIM_STATE_HSCLKEN)
		return;

	regs.regs = dsim->res.regs;
	regs.ss_regs = dsim->res.ss_reg_base;
	regs.phy_regs = dsim->res.phy_regs;
	regs.phy_regs_ex = dsim->res.phy_regs_ex;
	__dsim_dump(&p, dsim->id, &regs);
}

static int dsim_phy_power_on(struct dsim_device *dsim)
{
	int ret;

	dsim_debug(dsim, "%s +\n", __func__);

	if (IS_ENABLED(CONFIG_BOARD_EMULATOR))
		return 0;

	ret = phy_power_on(dsim->res.phy);
	if (ret) {
		dsim_err(dsim, "failed to enable dphy(%d)\n", ret);
		return ret;
	}
	if (dsim->res.phy_ex) {
		ret = phy_power_on(dsim->res.phy_ex);
		if (ret) {
			dsim_err(dsim, "failed to enable ext dphy(%d)\n", ret);
			return ret;
		}
	}

	dsim_debug(dsim, "%s -\n", __func__);

	return 0;
}

static int dsim_phy_power_off(struct dsim_device *dsim)
{
	int ret;

	dsim_debug(dsim, "%s +\n", __func__);

	if (IS_ENABLED(CONFIG_BOARD_EMULATOR))
		return 0;

	ret = phy_power_off(dsim->res.phy);
	if (ret) {
		dsim_err(dsim, "failed to disable dphy(%d)\n", ret);
		return ret;
	}
	if (dsim->res.phy_ex) {
		ret = phy_power_off(dsim->res.phy_ex);
		if (ret) {
			dsim_err(dsim, "failed to disable ext dphy(%d)\n", ret);
			return ret;
		}
	}

	dsim_debug(dsim, "%s -\n", __func__);

	return 0;
}

static void _dsim_exit_ulps_locked(struct dsim_device *dsim)
{
	const struct decon_device *decon = dsim_get_decon(dsim);

	WARN_ON(!mutex_is_locked(&dsim->state_lock));

	if (dsim->state != DSIM_STATE_ULPS)
		return;

	dsim_debug(dsim, "+\n");

	DPU_ATRACE_BEGIN(__func__);

#if defined(CONFIG_CPU_IDLE)
	exynos_update_ip_idle_status(dsim->idle_ip_index, 0);
#endif

	dsim_phy_power_on(dsim);

	mutex_lock(&g_dsim_lock);
	dsim_reg_init(dsim->id, &dsim->config, &dsim->clk_param, false);
	dsim_reg_exit_ulps_and_start(dsim->id, 0, 0x1F);
	mutex_unlock(&g_dsim_lock);

	dsim->state = DSIM_STATE_HSCLKEN;
	enable_irq(dsim->irq);

	dsim_debug(dsim, "-\n");
	if (decon)
		DPU_EVENT_LOG(DPU_EVT_DSIM_ULPS_EXIT, decon->id, dsim);

	DPU_ATRACE_END(__func__);
}

static void dsim_set_te_pinctrl(struct dsim_device *dsim, bool en)
{
	int ret;

	if (!dsim->hw_trigger || !dsim->te_on || !dsim->te_off)
		return;

	ret = pinctrl_select_state(dsim->pinctrl, en ? dsim->te_on : dsim->te_off);
	if (ret)
		dsim_err(dsim, "failed to control decon TE(%d)\n", en);
}

static void _dsim_enable(struct dsim_device *dsim)
{
	const struct decon_device *decon = dsim_get_decon(dsim);
	struct dsim_device *sec_dsi;
	bool skip_init = false;

	pm_runtime_get_sync(dsim->dev);

	mutex_lock(&dsim->state_lock);
	if (dsim->state == DSIM_STATE_HSCLKEN) {
		mutex_unlock(&dsim->state_lock);
		pm_runtime_put_sync(dsim->dev);
		dsim_info(dsim, "already enabled(%d)\n", dsim->state);
		return;
	}

	dsim_debug(dsim, "%s +\n", __func__);

#if defined(CONFIG_CPU_IDLE)
	exynos_update_ip_idle_status(dsim->idle_ip_index, 0);
#endif

	dsim_phy_power_on(dsim);
	if (dsim->state == DSIM_STATE_HANDOVER) {
		skip_init = dsim_reg_is_pll_stable(dsim->id);
		dsim_info(dsim, "dsim handover. skip_init=%d\n", skip_init);
	}

	mutex_lock(&g_dsim_lock);
	if (!skip_init)
		dsim_reg_init(dsim->id, &dsim->config, &dsim->clk_param, true);

	dsim_reg_start(dsim->id);
	mutex_unlock(&g_dsim_lock);

	/* TODO: dsi start: enable irq, sfr configuration */
	dsim->state = DSIM_STATE_HSCLKEN;
	enable_irq(dsim->irq);
	mutex_unlock(&dsim->state_lock);

#if defined(DSIM_BIST)
	dsim_reg_set_bist(dsim->id, true, DSIM_GRAY_GRADATION);
	dsim_dump(dsim);
#endif

	if (decon)
		DPU_EVENT_LOG(DPU_EVT_DSIM_ENABLED, decon->id, dsim);

	dsim_debug(dsim, "%s -\n", __func__);

	if (dsim->dual_dsi == DSIM_DUAL_DSI_MAIN) {
		sec_dsi = exynos_get_dual_dsi(DSIM_DUAL_DSI_SEC);
		if (sec_dsi)
			_dsim_enable(sec_dsi);
		else
			dsim_err(dsim, "could not get secondary dsi\n");
	}
}

static void dsim_encoder_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
{
	struct dsim_device *dsim = encoder_to_dsim(encoder);
	struct drm_crtc *crtc = drm_encoder_get_new_crtc(encoder, state);
	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
	const struct decon_device *decon = to_exynos_crtc(crtc)->ctx;
	struct device *supplier = decon->dev;

	dsim_debug(dsim, "current state: %d\n", dsim->state);

	if (dsim->dev_link && (dsim->dev_link->supplier != supplier)) {
		device_link_del(dsim->dev_link);
		dsim->dev_link = NULL;
	}

	if (!dsim->dev_link) {
		const u32 dl_flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER;

		dsim->dev_link = device_link_add(dsim->dev, supplier, dl_flags);
		if (WARN(!dsim->dev_link, "unable to create dev link between decon/dsim\n"))
			return;
	}


	if (dsim->state == DSIM_STATE_SUSPEND || dsim->state == DSIM_STATE_HANDOVER) {
		_dsim_enable(dsim);
		dsim_set_te_pinctrl(dsim, 1);
	} else if (dsim->state == DSIM_STATE_BYPASS) {
		pm_runtime_set_suspended(dsim->dev);
		dsim->state = DSIM_STATE_SUSPEND;
		_dsim_enable(dsim);
	} else if (old_crtc_state->self_refresh_active) {
		/* get extra ref count dropped when going into self refresh */
		pm_runtime_get_sync(dsim->dev);
	} else {
		WARN(1, "unknown dsim state (%d)\n", dsim->state);
	}
}

static inline bool dsim_cmd_packetgo_is_enabled(const struct dsim_device *dsim)
{
	return dsim->total_pend_ph > 0;
}

static void __dsim_cmd_packetgo_enable_locked(struct dsim_device *dsim, bool en)
{
	if (en) {
		pm_runtime_get_sync(dsim->dev);
		dsim_debug(dsim, "enabling packetgo\n");
	}

	dsim_reg_enable_packetgo(dsim->id, en);
	if (!en) {
		pm_runtime_put(dsim->dev);

		dsim->total_pend_ph = 0;
		dsim->total_pend_pl = 0;
		dsim_debug(dsim, "packetgo disabled\n");
	}
}

static void __dsim_check_pend_cmd_locked(struct dsim_device *dsim)
{
	WARN_ON(!mutex_is_locked(&dsim->cmd_lock));

	if (WARN_ON(dsim_reg_has_pend_cmd(dsim->id)))
		dsim_dump(dsim);

	if (WARN(dsim_cmd_packetgo_is_enabled(dsim), "pending packets remaining ph(%u) pl(%u)\n",
		 dsim->total_pend_ph, dsim->total_pend_pl))
		__dsim_cmd_packetgo_enable_locked(dsim, false);
}

static void _dsim_enter_ulps_locked(struct dsim_device *dsim)
{
	const struct decon_device *decon = dsim_get_decon(dsim);

	WARN_ON(!mutex_is_locked(&dsim->state_lock));

	DPU_ATRACE_BEGIN(__func__);

	dsim_debug(dsim, "+\n");

	/* Wait for current read & write CMDs. */
	mutex_lock(&dsim->cmd_lock);
	__dsim_check_pend_cmd_locked(dsim);
	dsim->state = DSIM_STATE_ULPS;
	mutex_unlock(&dsim->cmd_lock);

	disable_irq(dsim->irq);
	mutex_lock(&g_dsim_lock);
	dsim_reg_stop_and_enter_ulps(dsim->id, 0, 0x1F);
	mutex_unlock(&g_dsim_lock);

	dsim_phy_power_off(dsim);

#if defined(CONFIG_CPU_IDLE)
	exynos_update_ip_idle_status(dsim->idle_ip_index, 1);
#endif
	if (decon)
		DPU_EVENT_LOG(DPU_EVT_DSIM_ULPS_ENTER, decon->id, dsim);

	dsim_debug(dsim, "-\n");

	DPU_ATRACE_END(__func__);
}

static void _dsim_disable(struct dsim_device *dsim)
{
	const struct decon_device *decon = dsim_get_decon(dsim);
	struct dsim_device *sec_dsi;

	if (dsim->dual_dsi == DSIM_DUAL_DSI_MAIN) {
		sec_dsi = exynos_get_dual_dsi(DSIM_DUAL_DSI_SEC);
		if (sec_dsi)
			_dsim_disable(sec_dsi);
		else
			dsim_err(dsim, "could not get secondary dsi\n");
	}

	dsim_debug(dsim, "+\n");
	mutex_lock(&dsim->state_lock);
	if (dsim->state == DSIM_STATE_SUSPEND) {
		mutex_unlock(&dsim->state_lock);
		dsim_info(dsim, "already disabled(%d)\n", dsim->state);
		return;
	}

	/* Wait for current read & write CMDs. */
	mutex_lock(&dsim->cmd_lock);
	mutex_lock(&g_dsim_lock);
	/* TODO: 0x1F will be changed */
	dsim_reg_stop(dsim->id, 0x1F);
	mutex_unlock(&g_dsim_lock);
	disable_irq(dsim->irq);

	dsim->state = DSIM_STATE_SUSPEND;
	__dsim_check_pend_cmd_locked(dsim);

	dsim->force_batching = false;
	mutex_unlock(&dsim->cmd_lock);
	mutex_unlock(&dsim->state_lock);

	dsim_phy_power_off(dsim);

#if defined(CONFIG_CPU_IDLE)
	exynos_update_ip_idle_status(dsim->idle_ip_index, 1);
#endif

	pm_runtime_put_sync(dsim->dev);

	if (decon)
		DPU_EVENT_LOG(DPU_EVT_DSIM_DISABLED, decon->id, dsim);

	dsim_debug(dsim, "-\n");
}

static void dsim_encoder_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
{
	struct dsim_device *dsim = encoder_to_dsim(encoder);
	struct drm_crtc *crtc = drm_encoder_get_old_crtc(encoder, state);
	bool self_refresh_active = false;
	bool was_in_self_refresh = false;

	if (crtc) {
		const struct drm_crtc_state *old_crtc_state =
			drm_atomic_get_old_crtc_state(state, crtc);
		const struct drm_crtc_state *new_crtc_state =
			drm_atomic_get_new_crtc_state(state, crtc);

		if (old_crtc_state && old_crtc_state->self_refresh_active)
			was_in_self_refresh = true;
		if (new_crtc_state && new_crtc_state->self_refresh_active)
			self_refresh_active = true;
	}

	dsim_debug(dsim, "state: %d self_refresh: %d->%d\n", dsim->state,
		   was_in_self_refresh, self_refresh_active);

	DPU_ATRACE_BEGIN(__func__);

	if (self_refresh_active) {
		const struct exynos_drm_crtc_state *exynos_crtc_state =
			to_exynos_crtc_state(crtc->state);

		if (was_in_self_refresh) {
			WARN(1, "already in self refresh state. state:%d\n", dsim->state);
		} else if (exynos_crtc_state->bypass) {
			/* in bypass mode dsim should get fully disabled */
			_dsim_disable(dsim);
			dsim->state = DSIM_STATE_BYPASS;
		} else {
			/* during regular self refresh just need to go into runtime idle */
			pm_runtime_put_sync(dsim->dev);
		}
	} else {
		if (was_in_self_refresh) {
			/* get extra ref count dropped when going into self refresh */
			pm_runtime_get_sync(dsim->dev);

			dsim_debug(dsim, "disable right after self refresh. state:%d\n",
				  dsim->state);
		}

		_dsim_disable(dsim);

		dsim_set_te_pinctrl(dsim, 0);
	}

	DPU_ATRACE_END(__func__);
}

static void dsim_modes_release(struct dsim_pll_params *pll_params)
{
	if (pll_params->params) {
		unsigned int i;

		for (i = 0; i < pll_params->num_modes; i++)
			kfree(pll_params->params[i]);
		kfree(pll_params->params);
	}
	kfree(pll_params->features);

	kfree(pll_params);
}

static struct dsim_pll_param *
dsim_get_clock_mode(const struct dsim_device *dsim,
		    const struct drm_display_mode *mode)
{
	int i;
	const struct dsim_pll_params *pll_params = dsim->pll_params;
	const size_t mlen = strnlen(mode->name, DRM_DISPLAY_MODE_LEN);
	struct dsim_pll_param *ret = NULL;
	size_t plen;

	for (i = 0; i < pll_params->num_modes; i++) {
		struct dsim_pll_param *p = pll_params->params[i];

		plen = strnlen(p->name, DRM_DISPLAY_MODE_LEN);

		if (!strncmp(mode->name, p->name, plen)) {
			ret = p;
			/*
			 * if it's not exact match, continue looking for exact
			 * match, use this as a fallback
			 */
			if (plen == mlen)
				break;
		}
	}

	return ret;
}

static void dsim_update_clock_config(struct dsim_device *dsim,
				     const struct dsim_pll_param *p)
{
	dsim->config.dphy_pms.p = p->p;
	dsim->config.dphy_pms.m = p->m;
	dsim->config.dphy_pms.s = p->s;
	dsim->config.dphy_pms.k = p->k;

	dsim->config.dphy_pms.mfr = p->mfr;
	dsim->config.dphy_pms.mrr = p->mrr;
	dsim->config.dphy_pms.sel_pf = p->sel_pf;
	dsim->config.dphy_pms.icp = p->icp;
	dsim->config.dphy_pms.afc_enb = p->afc_enb;
	dsim->config.dphy_pms.extafc = p->extafc;
	dsim->config.dphy_pms.feed_en = p->feed_en;
	dsim->config.dphy_pms.fsel = p->fsel;
	dsim->config.dphy_pms.fout_mask = p->fout_mask;
	dsim->config.dphy_pms.rsel = p->rsel;
	dsim->config.dphy_pms.dither_en = p->dither_en;

	dsim->clk_param.hs_clk = p->pll_freq;
	dsim->clk_param.esc_clk = p->esc_freq;

	dsim_debug(dsim, "found proper pll parameter\n");
	dsim_debug(dsim, "\t%s(p:0x%x,m:0x%x,s:0x%x,k:0x%x)\n", p->name,
		 dsim->config.dphy_pms.p, dsim->config.dphy_pms.m,
		 dsim->config.dphy_pms.s, dsim->config.dphy_pms.k);

	dsim_debug(dsim, "\t%s(hs:%d,esc:%d)\n", p->name, dsim->clk_param.hs_clk,
		 dsim->clk_param.esc_clk);

	if (p->cmd_underrun_cnt) {
		dsim->config.cmd_underrun_cnt[0] = p->cmd_underrun_cnt;
	} else {
		dsim_warn(dsim, "cmd_underrun_cnt is not set correctly\n");
		WARN_ON(1);
	}

	dsim_debug(dsim, "\tunderrun_lp_ref 0x%x\n", dsim->config.cmd_underrun_cnt[0]);
}

static int dsim_set_clock_mode(struct dsim_device *dsim,
			       const struct drm_display_mode *mode)
{
	struct dsim_pll_param *p = dsim_get_clock_mode(dsim, mode);
	uint32_t underrun_cnt;

	if (!p)
		return -ENOENT;

	if (!dsim_calc_underrun(dsim, p->pll_freq, &underrun_cnt))
		p->cmd_underrun_cnt = underrun_cnt;

	dsim_update_clock_config(dsim, p);
	dsim->current_pll_param = p;

	return 0;
}

static int dsim_of_parse_modes(struct device_node *entry,
		struct dsim_pll_param *pll_param)
{
	u32 res[14];
	int cnt;

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

	of_property_read_string(entry, "mode-name",
			(const char **)&pll_param->name);

	cnt = of_property_count_u32_elems(entry, "pmsk");
	if (cnt != 4 && cnt != 14) {
		pr_err("mode %s has wrong pmsk elements number %d\n",
				pll_param->name, cnt);
		return -EINVAL;
	}

	/* TODO: how dsi dither handle ? */
	of_property_read_u32_array(entry, "pmsk", res, cnt);
	pll_param->dither_en = false;
	pll_param->p = res[0];
	pll_param->m = res[1];
	pll_param->s = res[2];
	pll_param->k = res[3];
	if (cnt == 14) {
		pll_param->mfr = res[4];
		pll_param->mrr = res[5];
		pll_param->sel_pf = res[6];
		pll_param->icp = res[7];
		pll_param->afc_enb = res[8];
		pll_param->extafc = res[9];
		pll_param->feed_en = res[10];
		pll_param->fsel = res[11];
		pll_param->fout_mask = res[12];
		pll_param->rsel = res[13];
		pll_param->dither_en = true;
	}

	of_property_read_u32(entry, "hs-clk", &pll_param->pll_freq);
	of_property_read_u32(entry, "esc-clk", &pll_param->esc_freq);
	of_property_read_u32(entry, "cmd_underrun_cnt",
			&pll_param->cmd_underrun_cnt);

	return 0;
}

static struct dsim_pll_features *dsim_of_get_pll_features(
		struct dsim_device *dsim, struct device_node *np)
{
	u64 range64[2];
	u32 range32[2];
	struct dsim_pll_features *pll_features;

	pll_features = kzalloc(sizeof(*pll_features), GFP_KERNEL);
	if (!pll_features)
		return NULL;

	if (of_property_read_u64(np, "pll-input", &pll_features->finput) < 0) {
		dsim_err(dsim, "%s failed to get pll-input\n", __func__);
		goto read_node_fail;
	}

	if (of_property_read_u64(np, "pll-optimum",
				 &pll_features->foptimum) < 0) {
		dsim_err(dsim, "%s failed to get pll-optimum\n", __func__);
		goto read_node_fail;
	}

	if (of_property_read_u64_array(np, "pll-out-range", range64, 2) < 0) {
		dsim_err(dsim, "%s failed to get pll-out-range\n", __func__);
		goto read_node_fail;
	}
	pll_features->fout_min = range64[0];
	pll_features->fout_max = range64[1];

	if (of_property_read_u64_array(np, "pll-vco-range", range64, 2) < 0) {
		dsim_err(dsim, "%s failed to get pll-vco-range\n", __func__);
		goto read_node_fail;
	}
	pll_features->fvco_min = range64[0];
	pll_features->fvco_max = range64[1];

	if (of_property_read_u32_array(np, "p-range", range32, 2) < 0) {
		dsim_err(dsim, "%s failed to get p-range\n", __func__);
		goto read_node_fail;
	}
	pll_features->p_min = range32[0];
	pll_features->p_max = range32[1];

	if (of_property_read_u32_array(np, "m-range", range32, 2) < 0) {
		dsim_err(dsim, "%s failed to get m-range\n", __func__);
		goto read_node_fail;
	}
	pll_features->m_min = range32[0];
	pll_features->m_max = range32[1];

	if (of_property_read_u32_array(np, "s-range", range32, 2) < 0) {
		dsim_err(dsim, "%s failed to get s-range\n", __func__);
		goto read_node_fail;
	}
	pll_features->s_min = range32[0];
	pll_features->s_max = range32[1];

	if (of_property_read_u32(np, "k-bits", &pll_features->k_bits) < 0) {
		dsim_err(dsim, "%s failed to get k-bits\n", __func__);
		goto read_node_fail;
	}

	dsim_debug(dsim, "pll features: input %llu, optimum%llu\n",
		  pll_features->finput, pll_features->foptimum);
	dsim_debug(dsim, "pll features: output(%llu, %llu)\n",
		  pll_features->fout_min, pll_features->fout_max);
	dsim_debug(dsim, "pll features: vco (%llu, %llu)\n",
		  pll_features->fvco_min, pll_features->fout_max);
	dsim_debug(dsim, "pll limits: p(%u, %u), m(%u, %u), s(%u, %u), k(%u)\n",
		  pll_features->p_min, pll_features->p_max,
		  pll_features->m_min, pll_features->m_max,
		  pll_features->s_min, pll_features->s_max,
		  pll_features->k_bits);

	return pll_features;

read_node_fail:
	kfree(pll_features);
	return NULL;
}

static struct dsim_pll_params *dsim_of_get_clock_mode(struct dsim_device *dsim)
{
	struct device *dev = dsim->dev;
	struct device_node *np, *mode_np, *entry;
	struct dsim_pll_params *pll_params;

	np = of_parse_phandle(dev->of_node, "dsim_mode", 0);
	if (!np) {
		dsim_err(dsim, "could not get dsi modes\n");
		return NULL;
	}

	mode_np = of_get_child_by_name(np, "dsim-modes");
	if (!mode_np) {
		dsim_err(dsim, "%pOF: could not find dsim-modes node\n", np);
		goto getnode_fail;
	}

	pll_params = kzalloc(sizeof(*pll_params), GFP_KERNEL);
	if (!pll_params)
		goto getmode_fail;

	entry = of_get_next_child(mode_np, NULL);
	if (!entry) {
		dsim_err(dsim, "could not find child node of dsim-modes");
		goto getchild_fail;
	}

	pll_params->num_modes = of_get_child_count(mode_np);
	if (pll_params->num_modes == 0) {
		dsim_err(dsim, "%pOF: no modes specified\n", np);
		goto getchild_fail;
	}

	pll_params->params = kzalloc(sizeof(struct dsim_pll_param *) *
				pll_params->num_modes, GFP_KERNEL);
	if (!pll_params->params)
		goto getchild_fail;

	pll_params->num_modes = 0;

	for_each_child_of_node(mode_np, entry) {
		struct dsim_pll_param *pll_param;

		pll_param = kzalloc(sizeof(*pll_param), GFP_KERNEL);
		if (!pll_param)
			goto getpll_fail;

		if (dsim_of_parse_modes(entry, pll_param) < 0) {
			kfree(pll_param);
			continue;
		}

		pll_params->params[pll_params->num_modes] = pll_param;
		pll_params->num_modes++;
	}

	pll_params->features = dsim_of_get_pll_features(dsim, np);

	of_node_put(np);
	of_node_put(mode_np);
	of_node_put(entry);

	return pll_params;

getpll_fail:
	of_node_put(entry);
getchild_fail:
	dsim_modes_release(pll_params);
getmode_fail:
	of_node_put(mode_np);
getnode_fail:
	of_node_put(np);
	return NULL;
}

static void dsim_restart(struct dsim_device *dsim)
{
	mutex_lock(&dsim->cmd_lock);
	mutex_lock(&g_dsim_lock);
	dsim_reg_stop(dsim->id, 0x1F);
	mutex_unlock(&g_dsim_lock);
	disable_irq(dsim->irq);

	mutex_lock(&g_dsim_lock);
	dsim_reg_init(dsim->id, &dsim->config, &dsim->clk_param, true);
	dsim_reg_start(dsim->id);
	mutex_unlock(&g_dsim_lock);
	enable_irq(dsim->irq);
	mutex_unlock(&dsim->cmd_lock);
}

#ifdef CONFIG_DEBUG_FS

static int dsim_of_parse_diag(struct device_node *np, struct dsim_dphy_diag *diag)
{
        int count;
        u8 bit_range[2];
        const char *reg_base = NULL;

        of_property_read_string(np, "reg-base", &reg_base);
        if (!strcmp(reg_base, "dphy")) {
                diag->reg_base = REGS_DSIM_PHY;
        } else if (!strcmp(reg_base, "dphy-extra")) {
                diag->reg_base = REGS_DSIM_PHY_BIAS;
        } else {
                pr_err("%s: invalid reg-base: %s\n", __func__, reg_base);
                return -EINVAL;
        }

        of_property_read_string(np, "diag-name", &diag->name);
        if (!diag->name || !diag->name[0]) {
                pr_err("%s: empty diag-name\n", __func__);
                return -EINVAL;
        }

        of_property_read_string(np, "desc", &diag->desc);
        of_property_read_string(np, "help", &diag->help);

        count = of_property_count_u16_elems(np, "reg-offset");
        if (count <= 0 || count > MAX_DIAG_REG_NUM) {
                pr_err("%s: wrong number of reg-offset: %d\n", __func__, count);
                return -ERANGE;
        }

        if (of_property_read_u16_array(np, "reg-offset", diag->reg_offset, count) < 0) {
                pr_err("%s: failed to read reg-offset\n", __func__);
                return -EINVAL;
        }
        diag->num_reg = count;

        if (of_property_read_u8_array(np, "bit-range", bit_range, 2) < 0) {
                pr_err("%s: failed to read bit-range\n", __func__);
                return -EINVAL;
        }

        if (bit_range[0] >= 32 || bit_range[1] >= 32) {
                pr_err("%s: invalid bit range %d, %d\n", __func__, bit_range[0], bit_range[1]);
                return -EINVAL;
        }
        if (bit_range[0] < bit_range[1]) {
                diag->bit_start = bit_range[0];
                diag->bit_end = bit_range[1];
        } else {
                diag->bit_start = bit_range[1];
                diag->bit_end = bit_range[0];
        }
        diag->read_only = of_property_read_bool(np, "read_only");

        return 0;
}

static void dsim_of_get_pll_diags(struct dsim_device *dsim)
{
	struct device_node *np, *entry;
	struct device *dev = dsim->dev;
        uint32_t index = 0;

	np = of_parse_phandle(dev->of_node, "dphy_diag", 0);
        dsim->config.num_dphy_diags = of_get_child_count(np);
        if (dsim->config.num_dphy_diags == 0) {
                goto nochild;
        }

	dsim->config.dphy_diags = devm_kzalloc(dsim->dev, sizeof(struct dsim_dphy_diag) *
					dsim->config.num_dphy_diags, GFP_KERNEL);
	if (!dsim->config.dphy_diags) {
                dsim_warn(dsim, "%s: no memory for %u diag items\n",
                          __func__, dsim->config.num_dphy_diags);
                dsim->config.num_dphy_diags = 0;
                goto nochild;
        }

        for_each_child_of_node(np, entry) {
                if (index >= dsim->config.num_dphy_diags) {
                      dsim_warn(dsim, "%s: diag parsing error with unexpected index %u\n",
                                __func__, index);
                      goto get_diag_fail;
                }

                if (dsim_of_parse_diag(entry, &dsim->config.dphy_diags[index]) < 0) {
                      dsim_warn(dsim, "%s: diag parsing error for item %u\n",
                                __func__, index);
                      goto get_diag_fail;
                }
                ++index;
        }
        return;

get_diag_fail:
        dsim->config.num_dphy_diags = 0;
        devm_kfree(dsim->dev, dsim->config.dphy_diags);
        dsim->config.dphy_diags = NULL;
nochild:
        return;
}

int dsim_dphy_diag_get_reg(struct dsim_device *dsim,
                           struct dsim_dphy_diag *diag, uint32_t *vals)
{
	int ret;
	uint32_t ix, mask, val;

	ret = dsim_dphy_diag_mask_from_range(diag->bit_start, diag->bit_end,
					     &mask);
	if (ret < 0)
		return ret;

	mutex_lock(&dsim->state_lock);
	if (dsim->state != DSIM_STATE_HSCLKEN) {
		ret = -ENODEV;
		goto out;
	}

	for (ix = 0; ix < diag->num_reg; ++ix) {
		if (diag->reg_base == REGS_DSIM_PHY_BIAS)
			val = diag_dsim_dphy_extra_reg_read_mask(
				dsim->id, diag->reg_offset[ix], mask);
		else if (diag->reg_base == REGS_DSIM_PHY)
			val = diag_dsim_dphy_reg_read_mask(
				dsim->id, diag->reg_offset[ix], mask);
		else {
			pr_err("%s: invalid reg_base %u\n", __func__,
			       diag->reg_base);
			goto out;
		}
		vals[ix] = val >> diag->bit_start;
	}
out:
	mutex_unlock(&dsim->state_lock);
	return ret;
}

int dsim_dphy_diag_set_reg(struct dsim_device *dsim,
                           struct dsim_dphy_diag *diag, uint32_t val)
{
	int ret;
	u32 mask;

	ret = dsim_dphy_diag_mask_from_range(diag->bit_start, diag->bit_end,
					     &mask);
	if (ret < 0)
		return ret;

	diag->override = true;
	diag->user_value = (val << diag->bit_start) & mask;

	mutex_lock(&dsim->state_lock);
	if (dsim->state != DSIM_STATE_HSCLKEN)
		goto out;

	/* restart dsim to apply new config */
	dsim_restart(dsim);

out:
	mutex_unlock(&dsim->state_lock);
	return ret;
}

#else

static void dsim_of_get_pll_diags(struct dsim_device * /* dsim */)
{
}

#endif

static void dsim_update_config_for_mode(struct dsim_reg_config *config,
					const struct drm_display_mode *mode,
					const struct exynos_display_mode *exynos_mode)
{
	struct dpu_panel_timing *p_timing = &config->p_timing;
	struct videomode vm;

	drm_display_mode_to_videomode(mode, &vm);

	p_timing->vactive = vm.vactive;
	p_timing->vfp = vm.vfront_porch;
	p_timing->vbp = vm.vback_porch;
	p_timing->vsa = vm.vsync_len;

	p_timing->hactive = vm.hactive;
	if (config->dual_dsi)
		p_timing->hactive /= 2;
	p_timing->hfp = vm.hfront_porch;
	p_timing->hbp = vm.hback_porch;
	p_timing->hsa = vm.hsync_len;
	p_timing->vrefresh = drm_mode_vrefresh(mode);
	if (exynos_mode->underrun_param) {
		p_timing->te_idle_us = exynos_mode->underrun_param->te_idle_us;
		p_timing->te_var = exynos_mode->underrun_param->te_var;
	} else {
		p_timing->te_idle_us = DEFAULT_TE_IDLE_US;
		p_timing->te_var = DEFAULT_TE_VARIATION;
		pr_debug("%s: underrun_param for mode " DRM_MODE_FMT
			" not specified", __func__, DRM_MODE_ARG(mode));
	}

	/* TODO: This hard coded information will be defined in device tree */
	config->mres_mode = 0;
	config->mode = (exynos_mode->mode_flags & MIPI_DSI_MODE_VIDEO) ?
				DSIM_VIDEO_MODE : DSIM_COMMAND_MODE;
	config->bpp = exynos_mode->bpc * 3;

	config->dsc.enabled = exynos_mode->dsc.enabled;
	if (config->dsc.enabled) {
		config->dsc.dsc_count = exynos_mode->dsc.dsc_count;
		config->dsc.slice_count = exynos_mode->dsc.slice_count;
		config->dsc.slice_height = exynos_mode->dsc.slice_height;
		config->dsc.slice_width = DIV_ROUND_UP(
				config->p_timing.hactive,
				config->dsc.slice_count);
	}
}

static void dsim_set_display_mode(struct dsim_device *dsim,
				  const struct drm_display_mode *mode,
				  const struct exynos_display_mode *exynos_mode)
{
	struct dsim_device *sec_dsi;

	if (!dsim->dsi_device)
		return;

	mutex_lock(&dsim->state_lock);
	dsim->config.data_lane_cnt = dsim->dsi_device->lanes;
	dsim->hw_trigger = !exynos_mode->sw_trigger;

	dsim->config.dual_dsi = dsim->dual_dsi;
	dsim_update_config_for_mode(&dsim->config, mode, exynos_mode);

	dsim_set_clock_mode(dsim, mode);

	if (dsim->state == DSIM_STATE_HSCLKEN)
		dsim_reg_set_vrr_config(dsim->id, &dsim->config, &dsim->clk_param);

	dsim_debug(dsim, "dsim mode %s dsc is %s [%d %d %d %d]\n",
			dsim->config.mode == DSIM_VIDEO_MODE ? "video" : "cmd",
			dsim->config.dsc.enabled ? "enabled" : "disabled",
			dsim->config.dsc.dsc_count,
			dsim->config.dsc.slice_count,
			dsim->config.dsc.slice_width,
			dsim->config.dsc.slice_height);
	mutex_unlock(&dsim->state_lock);

	if (dsim->dual_dsi == DSIM_DUAL_DSI_MAIN) {
		sec_dsi = exynos_get_dual_dsi(DSIM_DUAL_DSI_SEC);
		if (sec_dsi) {
			sec_dsi->dsi_device = dsim->dsi_device;
			dsim_set_display_mode(sec_dsi, mode, exynos_mode);
		} else
			dsim_err(dsim, "could not get main dsi\n");
	}
}

static void dsim_atomic_mode_set(struct drm_encoder *encoder, struct drm_crtc_state *crtc_state,
				 struct drm_connector_state *conn_state)
{
	struct dsim_device *dsim = encoder_to_dsim(encoder);
	const struct exynos_drm_connector_state *exynos_conn_state =
		to_exynos_connector_state(conn_state);

	dsim_set_display_mode(dsim, &crtc_state->adjusted_mode, &exynos_conn_state->exynos_mode);
}

static enum drm_mode_status dsim_mode_valid(struct drm_encoder *encoder,
					    const struct drm_display_mode *mode)
{
	struct dsim_device *dsim = encoder_to_dsim(encoder);

	if (!dsim_get_clock_mode(dsim, mode))
		return MODE_NOMODE;

	return MODE_OK;
}

/*
 * Check whether mode change can happean seamlessly from dsim perspective.
 * Seamless mode switch from dsim perspective can only happen if there's no
 * need to change dsim configuration.
 */
static bool dsim_mode_is_seamless(const struct dsim_device *dsim,
				  const struct drm_display_mode *mode,
				  const struct exynos_display_mode *exynos_mode)
{
	struct dsim_reg_config new_config = dsim->config;

	if (dsim->current_pll_param != dsim_get_clock_mode(dsim, mode)) {
		dsim_debug(dsim, "clock mode change not allowed seamlessly\n");
		return false;
	}

	dsim_update_config_for_mode(&new_config, mode, exynos_mode);
	if (dsim->config.mode != new_config.mode) {
		dsim_debug(dsim, "op mode change not allowed seamlessly\n");
		return false;
	}

	if (memcmp(&dsim->config.dsc, &new_config.dsc, sizeof(new_config.dsc))) {
		dsim_debug(dsim, "dsc change not allowed seamlessly\n");
		return false;
	}

	return true;
}

static int dsim_atomic_check(struct drm_encoder *encoder,
			     struct drm_crtc_state *crtc_state,
			     struct drm_connector_state *connector_state)
{
	struct drm_display_mode *mode;
	const struct dsim_device *dsim = encoder_to_dsim(encoder);
	struct exynos_drm_connector_state *exynos_conn_state;

	if (crtc_state->mode_changed) {
		if (!is_exynos_drm_connector(connector_state->connector)) {
			dsim_warn(dsim, "%s: mode set is only supported w/exynos connector\n",
				  __func__);
			return -EINVAL;
		}

		exynos_conn_state = to_exynos_connector_state(connector_state);
		mode = &crtc_state->adjusted_mode;

		if (exynos_conn_state->seamless_possible &&
		    !dsim_mode_is_seamless(dsim, mode, &exynos_conn_state->exynos_mode)) {
			dsim_warn(dsim, "%s: seamless mode switch not supported for %s\n",
				  __func__, mode->name);
			exynos_conn_state->seamless_possible = false;
		}

		if (!exynos_conn_state->exynos_mode.sw_trigger) {
			if (!dsim->pinctrl) {
				dsim_err(dsim, "TE error: pinctrl not found\n");
				return -EINVAL;
			} else if ((dsim->te_gpio < 0) ||
				   (dsim->te_from >= MAX_DECON_TE_FROM_DDI)) {
				dsim_err(dsim, "invalid TE config for hw trigger mode\n");
				return -EINVAL;
			}

			exynos_conn_state->te_from = dsim->te_from;
			exynos_conn_state->te_gpio = dsim->te_gpio;
		}
	}

	return 0;
}

static const struct drm_encoder_helper_funcs dsim_encoder_helper_funcs = {
	.mode_valid = dsim_mode_valid,
	.atomic_mode_set = dsim_atomic_mode_set,
	.atomic_enable = dsim_encoder_enable,
	.atomic_disable = dsim_encoder_disable,
	.atomic_check = dsim_atomic_check,
};

#ifdef CONFIG_DEBUG_FS

static int dsim_encoder_late_register(struct drm_encoder *encoder)
{
        struct dsim_device *dsim = encoder_to_dsim(encoder);
        dsim_diag_create_debugfs(dsim);
        return 0;
}

static void dsim_encoder_early_unregister(struct drm_encoder *encoder)
{
        struct dsim_device *dsim = encoder_to_dsim(encoder);
        dsim_diag_remove_debugfs(dsim);
}

#else

static int dsim_encoder_late_register(struct drm_encoder * /* encoder */)
{
        return 0;
}

static void dsim_encoder_early_unregister(struct drm_encoder * /*encoder */)
{
}

#endif

static const struct drm_encoder_funcs dsim_encoder_funcs = {
	.destroy = drm_encoder_cleanup,
        .late_register = dsim_encoder_late_register,
        .early_unregister = dsim_encoder_early_unregister,
};

static int dsim_add_mipi_dsi_device(struct dsim_device *dsim,
	const char *pname, enum panel_priority_index idx)
{
	struct mipi_dsi_device_info info = {
		.node = NULL,
	};
	struct device_node *node;
	const char *name;
	const char *dual_dsi;

	dsim_debug(dsim, "preferred panel is %s\n", pname);

	for_each_available_child_of_node(dsim->dsi_host.dev->of_node, node) {
		bool found;

		/*
		 * panel w/ reg node will be added in mipi_dsi_host_register,
		 * abort panel detection in that case
		 */
		if (of_find_property(node, "reg", NULL)) {
			if (info.node)
				of_node_put(info.node);

			return -ENODEV;
		}

		/*
		 * We already detected panel we want but keep iterating
		 * in case there are devices with reg property
		 */
		if (info.node)
			continue;

		if (of_property_read_u32(node, "channel", &info.channel))
			continue;

		name = of_get_property(node, "label", NULL);
		if (!name)
			continue;

		/* if panel name is not specified pick the first device found */
		found = !strncmp(name, pname, PANEL_DRV_LEN);
		if (pname[0] == '\0' || found) {
			/*
			 * The default is primary panel. If not, add priority
			 * index in the panel name, i.e. "priority-index:panel-name"
			 */
			if (found && idx > PANEL_PRIORITY_PRI_IDX)
				scnprintf(info.type, sizeof(info.type),
					"%d:%s", idx, name);
			else
				strlcpy(info.type, name, sizeof(info.type));
			info.node = of_node_get(node);
		}
	}

	if (info.node) {
		if (!of_property_read_string(info.node, "dual-dsi", &dual_dsi)) {
			if (!strcmp(dual_dsi, "main"))
				dsim->dual_dsi = DSIM_DUAL_DSI_MAIN;
			else if (!strcmp(dual_dsi, "sec"))
				dsim->dual_dsi = DSIM_DUAL_DSI_SEC;
			else
				dsim->dual_dsi = DSIM_DUAL_DSI_NONE;
		}
		if (dsim->dual_dsi != DSIM_DUAL_DSI_SEC)
			mipi_dsi_device_register_full(&dsim->dsi_host, &info);

		if (dsim->dual_dsi == DSIM_DUAL_DSI_NONE)
			panel_usage |= BIT(idx);
		return 0;
	}

	return -ENODEV;
}

static const char *dsim_get_panel_name(const struct dsim_device *dsim, const char *name, size_t len)
{
	const char *p = strchr(name, ':');
	size_t pos;
	int dsim_id;

	/* if colon is found expect format which includes intended intf
	 * (ex. dsim0:panel_name), otherwise fallback to panel name only */
	if (!p)
		return name;

	if ((len < 5) || strncmp(name, "dsim", 4))
		return NULL;

	dsim_id = name[4] - '0';
	if (dsim->id != dsim_id)
		return NULL;

	p++;
	pos = p - name;
	if (pos >= len)
		return NULL;

	return p;
}

static int dsim_parse_panel_name(struct dsim_device *dsim)
{
	const char *name;
	enum panel_priority_index idx;

	name = dsim_get_panel_name(dsim, panel_name, PANEL_DRV_LEN);
	idx = PANEL_PRIORITY_PRI_IDX;
	if (name && !(panel_usage & BIT(idx)))
		return dsim_add_mipi_dsi_device(dsim, name, idx);

	name = dsim_get_panel_name(dsim, sec_panel_name, PANEL_DRV_LEN);
	idx = PANEL_PRIORITY_SEC_IDX;
	if (name && name[0] && !(panel_usage & BIT(idx)))
		return dsim_add_mipi_dsi_device(dsim, name, idx);

	return -ENODEV;
}

static int dsim_bind(struct device *dev, struct device *master, void *data)
{
	struct drm_encoder *encoder = dev_get_drvdata(dev);
	struct dsim_device *dsim = encoder_to_dsim(encoder);
	struct drm_device *drm_dev = data;
	int ret = 0;

	dsim_debug(dsim, "%s +\n", __func__);

	if (dsim->dual_dsi == DSIM_DUAL_DSI_SEC)
		return 0;

	drm_encoder_init(drm_dev, encoder, &dsim_encoder_funcs,
			 DRM_MODE_ENCODER_DSI, NULL);
	drm_encoder_helper_add(encoder, &dsim_encoder_helper_funcs);

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

	ret = mipi_dsi_host_register(&dsim->dsi_host);

	dsim_debug(dsim, "%s -\n", __func__);

	return ret;
}

static void dsim_unbind(struct device *dev, struct device *master,
				void *data)
{
	struct drm_encoder *encoder = dev_get_drvdata(dev);
	struct dsim_device *dsim = encoder_to_dsim(encoder);

	dsim_debug(dsim, "%s +\n", __func__);
	if (dsim->pll_params)
		dsim_modes_release(dsim->pll_params);

	if (dsim->dual_dsi == DSIM_DUAL_DSI_SEC)
		return;

	mipi_dsi_host_unregister(&dsim->dsi_host);
}

static const struct component_ops dsim_component_ops = {
	.bind	= dsim_bind,
	.unbind	= dsim_unbind,
};

static int dsim_parse_dt(struct dsim_device *dsim)
{
	struct device_node *np = dsim->dev->of_node;
	int ret;

	if (!np) {
		dsim_err(dsim, "no device tree information\n");
		return -ENOTSUPP;
	}

	of_property_read_u32(np, "dsim,id", &dsim->id);
	if (dsim->id < 0 || dsim->id >= MAX_DSI_CNT) {
		dsim_err(dsim, "wrong dsim id(%d)\n", dsim->id);
		return -ENODEV;
	}

	dsim->pll_params = dsim_of_get_clock_mode(dsim);
	dsim_of_get_pll_diags(dsim);

	ret = of_property_read_u32(np, "te_from", &dsim->te_from);
	if (ret) {
		dsim->te_from = MAX_DECON_TE_FROM_DDI;
		dsim_warn(dsim, "failed to get TE from DDI\n");
	}
	dsim_debug(dsim, "TE from DDI%d\n", dsim->te_from);

	if (!ret) {
		dsim->te_gpio = of_get_named_gpio(np, "te-gpio", 0);
		if (dsim->te_gpio < 0) {
			dsim_warn(dsim, "failed to get TE gpio\n");
			dsim->te_from = MAX_DECON_TE_FROM_DDI;
		}
	}

	dsim->err_fg_gpio = of_get_named_gpio(np, "errfg-gpio", 0);
	if (dsim->err_fg_gpio < 0)
		dsim_debug(dsim, "failed to get ERR_FG gpio\n");

	return 0;
}

static int dsim_remap_regs(struct dsim_device *dsim)
{
	struct resource res;
	struct device *dev = dsim->dev;
	struct device_node *np = dev->of_node;
	int i, ret = 0;

	i = of_property_match_string(np, "reg-names", "dsi");
	if (of_address_to_resource(np, i, &res)) {
		pr_err("failed to get dsi resource\n");
		goto err;
	}
	dsim->res.regs = ioremap(res.start, resource_size(&res));
	if (IS_ERR(dsim->res.regs)) {
		dsim_err(dsim, "failed to remap io region\n");
		ret = PTR_ERR(dsim->res.regs);
		goto err;
	}
	dsim_regs_desc_init(dsim->res.regs, res.start, "dsi", REGS_DSIM_DSI, dsim->id);

	i = of_property_match_string(np, "reg-names", "dphy");
	if (of_address_to_resource(np, i, &res)) {
		pr_err("failed to get dphy resource\n");
		goto err;
	}

	dsim->res.phy_regs = ioremap(res.start, resource_size(&res));
	if (IS_ERR(dsim->res.phy_regs)) {
		dsim_err(dsim, "failed to remap io region\n");
		ret = PTR_ERR(dsim->res.regs);
		goto err_dsi;
	}
	dsim_regs_desc_init(dsim->res.phy_regs, res.start, "dphy", REGS_DSIM_PHY,
			dsim->id);

	i = of_property_match_string(np, "reg-names", "dphy-extra");
	if (of_address_to_resource(np, i, &res)) {
		pr_err("failed to get dphy resource\n");
		goto err_dsi;
	}
	dsim->res.phy_regs_ex = ioremap(res.start, resource_size(&res));
	if (IS_ERR(dsim->res.phy_regs_ex))
		dsim_warn(dsim, "failed to remap io region. it's optional\n");
	dsim_regs_desc_init(dsim->res.phy_regs_ex, res.start, "dphy-extra",
			REGS_DSIM_PHY_BIAS, dsim->id);

	np = of_find_compatible_node(NULL, NULL, "samsung,exynos9-disp_ss");
	i = of_property_match_string(np, "reg-names", "sys");
	if (of_address_to_resource(np, i, &res)) {
		dsim_err(dsim, "failed to get sys resource\n");
		goto err_dphy_ext;
	}
	dsim->res.ss_reg_base = ioremap(res.start, resource_size(&res));
	if (!dsim->res.ss_reg_base) {
		dsim_err(dsim, "failed to map sysreg-disp address.");
		ret = PTR_ERR(dsim->res.ss_reg_base);
		goto err_dphy_ext;
	}
	dsim_regs_desc_init(dsim->res.ss_reg_base, res.start, np->name, REGS_DSIM_SYS,
			dsim->id);

	return ret;

err_dphy_ext:
	iounmap(dsim->res.phy_regs_ex);
	iounmap(dsim->res.phy_regs);
err_dsi:
	iounmap(dsim->res.regs);
err:
	return ret;
}


#if IS_ENABLED(CONFIG_ARM_EXYNOS_DEVFREQ)
static void dsim_underrun_info(struct dsim_device *dsim, u32 underrun_cnt)
{
	printk_ratelimited("underrun irq occurs(%u): MIF(%lu, %d), INT(%lu, %d), DISP(%lu, %d)\n",
			underrun_cnt,
			exynos_devfreq_get_domain_freq(DEVFREQ_MIF),
			exynos_pm_qos_request(PM_QOS_BUS_THROUGHPUT),
			exynos_devfreq_get_domain_freq(DEVFREQ_INT),
			exynos_pm_qos_request(PM_QOS_DEVICE_THROUGHPUT),
			exynos_devfreq_get_domain_freq(DEVFREQ_DISP),
			exynos_pm_qos_request(PM_QOS_DISPLAY_THROUGHPUT));
}
#else
static void dsim_underrun_info(struct dsim_device *dsim, u32 underrun_cnt)
{
	printk_ratelimited("underrun irq occurs(%u)\n", underrun_cnt);
}
#endif

static irqreturn_t dsim_irq_handler(int irq, void *dev_id)
{
	struct dsim_device *dsim = dev_id;
	struct decon_device *decon = (struct decon_device *)dsim_get_decon(dsim);
	unsigned int int_src;

	spin_lock(&dsim->slock);

	dsim_debug(dsim, "%s +\n", __func__);

	if (dsim->state != DSIM_STATE_HSCLKEN) {
		dsim_info(dsim, "dsim power is off state(0x%x)\n", dsim->state);
		spin_unlock(&dsim->slock);
		return IRQ_HANDLED;
	}

	int_src = dsim_reg_get_int_and_clear(dsim->id);
	if (int_src & DSIM_INTSRC_SFR_PH_FIFO_EMPTY) {
		complete(&dsim->ph_wr_comp);
		dsim_debug(dsim, "PH_FIFO_EMPTY irq occurs\n");
	}

	if (int_src & DSIM_INTSRC_SFR_PL_FIFO_EMPTY) {
		complete(&dsim->pl_wr_comp);
		dsim_debug(dsim, "PL_FIFO_EMPTY irq occurs\n");
	}

	if (int_src & DSIM_INTSRC_RX_DATA_DONE)
		complete(&dsim->rd_comp);
	if (int_src & DSIM_INTSRC_FRAME_DONE) {
		dsim_debug(dsim, "framedone irq occurs\n");
		if (decon)
			DPU_EVENT_LOG(DPU_EVT_DSIM_FRAMEDONE, decon->id, NULL);
	}

	if (int_src & DSIM_INTSRC_RX_CRC) {
		dsim_err(dsim, "RX CRC error was detected!\n");
		if (decon)
			DPU_EVENT_LOG(DPU_EVT_DSIM_CRC, decon->id, NULL);
	}

	if (int_src & DSIM_INTSRC_ERR_RX_ECC) {
		dsim_err(dsim, "RX ECC Multibit error was detected!\n");
		if (decon)
			DPU_EVENT_LOG(DPU_EVT_DSIM_ECC, decon->id, NULL);
	}

	if (int_src & DSIM_INTSRC_UNDER_RUN) {
		DPU_ATRACE_INT("DPU_UNDERRUN", 1);
		if (decon) {
			static unsigned long last_dumptime;

			dsim_underrun_info(dsim, decon->d.underrun_cnt + 1);
			DPU_EVENT_LOG(DPU_EVT_DSIM_UNDERRUN, decon->id, NULL);
			if (time_after(jiffies, last_dumptime + msecs_to_jiffies(5000))) {
				decon_dump_event_condition(decon, DPU_EVT_CONDITION_UNDERRUN);
				last_dumptime = jiffies;
			}
		} else {
			dsim_underrun_info(dsim, 0);
		}

		DPU_ATRACE_INT("DPU_UNDERRUN", 0);
	}

	spin_unlock(&dsim->slock);

	return IRQ_HANDLED;
}

static int dsim_register_irq(struct dsim_device *dsim)
{
	struct device *dev = dsim->dev;
	struct device_node *np = dev->of_node;
	struct platform_device *pdev;
	int ret = 0;

	pdev = container_of(dev, struct platform_device, dev);

	dsim->irq = of_irq_get_byname(np, "dsim");
	ret = devm_request_irq(dsim->dev, dsim->irq, dsim_irq_handler, 0,
			pdev->name, dsim);
	if (ret) {
		dsim_err(dsim, "failed to install DSIM irq\n");
		return -EINVAL;
	}
	disable_irq(dsim->irq);

	return 0;
}

static irqreturn_t dsim_err_fg_irq_handler(int irq, void *dev_id)
{
	struct dsim_device *dsim = dev_id;
	struct decon_device *decon;
	int recovering;

	dsim_debug(dsim, "%s +\n", __func__);

	decon = (struct decon_device *)dsim_get_decon(dsim);
	if (!decon) {
		dsim_warn(dsim, "%s: decon is NULL\n", __func__);
		goto end_err_fg_handler;
	}

	if (decon->state != DECON_STATE_ON && decon->state != DECON_STATE_HIBERNATION) {
		dsim_debug(dsim, "%s: decon state is %d\n", __func__, decon->state);
		goto end_err_fg_handler;
	}

	recovering = atomic_read(&decon->recovery.recovering);
	dsim_warn(dsim, "error flag detected (decon%d), try to recover (recovering=%d)",
		decon->id, recovering);

	decon_force_vblank_event(decon);
	if (!recovering)
		decon_trigger_recovery(decon);

end_err_fg_handler:
	dsim_debug(dsim, "%s -\n", __func__);

	return IRQ_HANDLED;
}

static int dsim_register_err_fg_irq(struct dsim_device *dsim)
{
	struct device *dev = dsim->dev;
	struct platform_device *pdev;
	int ret, irq;

	pdev = container_of(dev, struct platform_device, dev);
	dsim->irq_err_fg = -1;

	if (dsim->err_fg_gpio < 0) {
		/* If the project doesn't specify the errfg-gpio, just return here */
		dsim_debug(dsim, "No dedicated ERR_FG for dsim, skip irq registration\n");
		return 0;
	}

	ret = gpio_to_irq(dsim->err_fg_gpio);
	if (ret < 0) {
		dsim_err(dsim, "Failed to get irq number for err_fg_gpio: %d\n", ret);
		return ret;
	}

	irq = ret;
	irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
	ret = devm_request_irq(dsim->dev, irq, dsim_err_fg_irq_handler, IRQF_TRIGGER_RISING,
			pdev->name, dsim);
	if (ret) {
		dsim_err(dsim, "Request err_fg irq number(%d) failed: %d\n", irq, ret);
		return ret;
	}
	disable_irq(irq);
	dsim->irq_err_fg = irq;
	dsim_info(dsim, "Request err_fg irq number(%d) okay\n", irq);

	return ret;
}

static int dsim_get_phys(struct dsim_device *dsim)
{
	if (IS_ENABLED(CONFIG_BOARD_EMULATOR))
		return 0;

	dsim->res.phy = devm_phy_get(dsim->dev, "dsim_dphy");
	if (IS_ERR(dsim->res.phy)) {
		dsim_err(dsim, "failed to get dsim phy\n");
		return PTR_ERR(dsim->res.phy);
	}

	dsim->res.phy_ex = devm_phy_get(dsim->dev, "dsim_dphy_extra");
	if (IS_ERR(dsim->res.phy_ex)) {
		dsim_warn(dsim, "failed to get dsim extra phy\n");
		dsim->res.phy_ex = NULL;
	}

	return 0;
}

static int dsim_init_resources(struct dsim_device *dsim)
{
	int ret = 0;

	ret = dsim_remap_regs(dsim);
	if (ret)
		goto err;

	ret = dsim_register_irq(dsim);
	if (ret)
		goto err;

	dsim_register_err_fg_irq(dsim);

	ret = dsim_get_phys(dsim);
	if (ret)
		goto err;

err:
	return ret;
}

static int dsim_host_attach(struct mipi_dsi_host *host,
				  struct mipi_dsi_device *device)
{
	struct dsim_device *dsim = host_to_dsi(host);
	struct drm_bridge *bridge;
	int ret;

	dsim_debug(dsim, "%s +\n", __func__);

	bridge = of_drm_find_bridge(device->dev.of_node);
	if (!bridge) {
		struct drm_panel *panel;

		panel = of_drm_find_panel(device->dev.of_node);
		if (IS_ERR(panel)) {
			dsim_err(dsim, "failed to find panel\n");
			return PTR_ERR(panel);
		}

		bridge = devm_drm_panel_bridge_add_typed(host->dev, panel,
						   DRM_MODE_CONNECTOR_DSI);
		if (IS_ERR(bridge)) {
			dsim_err(dsim, "failed to create panel bridge\n");
			return PTR_ERR(bridge);
		}
	}

	if (IS_ERR_OR_NULL(dsim->encoder.dev)) {
		dsim_err(dsim, "encoder is not initialized\n");
		return PTR_ERR(dsim->encoder.dev);
	}

	ret = drm_bridge_attach(&dsim->encoder, bridge, NULL, 0);
	if (ret) {
		dsim_err(dsim, "Unable to attach panel bridge\n");
	} else {
		dsim->panel_bridge = bridge;
		dsim->dsi_device = device;
	}

	ret = sysfs_create_link(&device->dev.kobj, &host->dev->kobj, "dsim");
	if (ret)
		dev_warn(&device->dev, "unable to link %s sysfs (%d)\n", "dsim", ret);

	dsim_debug(dsim, "%s -\n", __func__);

	return ret;
}

static int dsim_host_detach(struct mipi_dsi_host *host,
				  struct mipi_dsi_device *device)
{
	struct dsim_device *dsim = host_to_dsi(host);

	dsim_info(dsim, "%s +\n", __func__);

	_dsim_disable(dsim);
	if (dsim->panel_bridge) {
		struct drm_bridge *bridge = dsim->panel_bridge;

		if (bridge->funcs && bridge->funcs->detach)
			bridge->funcs->detach(bridge);
		dsim->panel_bridge = NULL;
	}
	dsim->dsi_device = NULL;

	sysfs_remove_link(&device->dev.kobj, "dsim");
	dsim_info(dsim, "%s -\n", __func__);
	return 0;
}

static int __dsim_wait_for_ph_fifo_empty(struct dsim_device *dsim)
{
	const struct decon_device *decon = dsim_get_decon(dsim);

	if (dsim_reg_header_fifo_is_empty(dsim->id)) {
		dsim_debug(dsim, "no need to wait for packet header fifo empty\n");
		return 0;
	}

	dsim_debug(dsim, "wait for packet header fifo empty\n");

	if (!wait_for_completion_timeout(&dsim->ph_wr_comp, MIPI_WR_TIMEOUT)) {
		if (dsim_reg_header_fifo_is_empty(dsim->id)) {
			dsim_warn(dsim, "timed out but header fifo was empty\n");
			dsim_reg_clear_int(dsim->id,
					DSIM_INTSRC_SFR_PH_FIFO_EMPTY);
			return 0;
		}

		dsim_warn(dsim, "timeout: packet header fifo empty\n");
		if (decon) {
			DPU_EVENT_LOG(DPU_EVT_DSIM_PH_FIFO_TIMEOUT, decon->id,
					NULL);
			decon_dump_event_condition(decon,
						DPU_EVT_CONDITION_FIFO_TIMEOUT);
		}
		return -ETIMEDOUT;
	}

	return 0;
}

static int __dsim_wait_for_pl_fifo_empty(struct dsim_device *dsim)
{
	const struct decon_device *decon = dsim_get_decon(dsim);

	if (dsim_reg_payload_fifo_is_empty(dsim->id)) {
		dsim_debug(dsim, "no need to wait for payload fifo empty\n");
		return 0;
	}

	dsim_debug(dsim, "wait for packet payload fifo empty\n");

	if (!wait_for_completion_timeout(&dsim->pl_wr_comp, MIPI_WR_TIMEOUT)) {
		if (dsim_reg_payload_fifo_is_empty(dsim->id)) {
			dsim_warn(dsim, "timed out but payload fifo was empty\n");
			dsim_reg_clear_int(dsim->id,
					DSIM_INTSRC_SFR_PL_FIFO_EMPTY);
			return 0;
		}

		dsim_warn(dsim, "timeout: packet payload fifo empty\n");
		if (decon) {
			DPU_EVENT_LOG(DPU_EVT_DSIM_PL_FIFO_TIMEOUT, decon->id,
					NULL);
			decon_dump_event_condition(decon,
						DPU_EVT_CONDITION_FIFO_TIMEOUT);
		}
		return -ETIMEDOUT;
	}

	return 0;
}

static int dsim_wait_for_cmd_fifo_empty(struct dsim_device *dsim, bool is_long)
{
	int ret = 0;

	DPU_ATRACE_BEGIN(__func__);
	if (is_long)
		ret = __dsim_wait_for_pl_fifo_empty(dsim);

	ret |= __dsim_wait_for_ph_fifo_empty(dsim);

	if (ret)
		dsim_dump(dsim);

	DPU_ATRACE_END(__func__);
	return ret;
}

static void
dsim_write_payload(struct dsim_device *dsim, const u8* buf, size_t len)
{
	const u8 *p = buf;
	const u8 *end = buf + len;
	u32 payload;

	dsim_debug(dsim, "payload length(%lu)\n", len);

	while (p < end) {
		size_t pkt_size = min_t(size_t, 4, end - p);

		if (pkt_size >= 4)
			payload = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
		else if (pkt_size == 3)
			payload = p[0] | p[1] << 8 | p[2] << 16;
		else if (pkt_size == 2)
			payload = p[0] | p[1] << 8;
		else if (pkt_size == 1)
			payload = p[0];

		dsim_reg_wr_tx_payload(dsim->id, payload);
		dsim_debug(dsim, "payload: 0x%x\n", payload);

		p += pkt_size;
	}
}

static void __dsim_cmd_write_locked(struct dsim_device *dsim, const struct mipi_dsi_packet *packet)
{
	WARN_ON(!mutex_is_locked(&dsim->cmd_lock));

	if (packet->payload_length > 0)
		dsim_write_payload(dsim, packet->payload, packet->payload_length);
	dsim_reg_wr_tx_header(dsim->id, packet->header[0], packet->header[1], packet->header[2],
			      false);

	dsim_debug(dsim, "header(0x%x 0x%x 0x%x) size(%lu) ph fifo(%d)\n", packet->header[0],
		   packet->header[1], packet->header[2], packet->size,
		   dsim_reg_get_ph_cnt(dsim->id));
}

static void dsim_cmd_packetgo_queue_locked(struct dsim_device *dsim,
					   const struct mipi_dsi_packet *packet)
{
	/* if this is the first packet being queued, enable packet go feature */
	if (!dsim->total_pend_ph)
		__dsim_cmd_packetgo_enable_locked(dsim, true);

	dsim->total_pend_ph++;
	dsim->total_pend_pl += ALIGN(packet->payload_length, 4);

	__dsim_cmd_write_locked(dsim, packet);

	dsim_debug(dsim, "total pending packet header(%u) payload(%u)\n", dsim->total_pend_ph,
		   dsim->total_pend_pl);
}

static void __dsim_cmd_prepare(struct dsim_device *dsim)
{
	WARN_ON(!mutex_is_locked(&dsim->cmd_lock));

	dsim_reg_clear_int(dsim->id, DSIM_INTSRC_SFR_PH_FIFO_EMPTY | DSIM_INTSRC_SFR_PL_FIFO_EMPTY);

	reinit_completion(&dsim->ph_wr_comp);
	reinit_completion(&dsim->pl_wr_comp);
}

static int dsim_cmd_packetgo_flush_locked(struct dsim_device *dsim)
{
	int ret;

	/* this should only be called with pending packets */
	WARN_ON(!dsim->total_pend_ph);

	__dsim_cmd_prepare(dsim);

	dsim_reg_ready_packetgo(dsim->id, true);
	dsim_debug(dsim, "packet go ready (ph: %d, pl: %d)\n", dsim->total_pend_ph,
		   dsim->total_pend_pl);

	ret = dsim_wait_for_cmd_fifo_empty(dsim, dsim->total_pend_pl > 0);
	if (ret)
		dsim_warn(dsim, "packetgo failed on wait for cmd fifo empty (%d)\n", ret);

	/* clear packetgo pending (even if it timed out) */
	__dsim_cmd_packetgo_enable_locked(dsim, false);

	return ret;
}

static int dsim_write_single_cmd_locked(struct dsim_device *dsim,
					const struct mipi_dsi_packet *packet)
{
	WARN_ON(dsim_cmd_packetgo_is_enabled(dsim));

	__dsim_cmd_prepare(dsim);

	__dsim_cmd_write_locked(dsim, packet);

	return dsim_wait_for_cmd_fifo_empty(dsim, packet->payload_length > 0);
}

/*
 *		      <-- ACTIVE -->
 * data transfer ---|-**************------------------|--
 *				       <-CMD ALLOW->
 * CMD LOCK       __|-----------------|_____________|---
 *
 * TE PROTECT       <------- TE PROTECT ON -------->
 *
 * ready allow	    <---- ready allow ---->|<-1ms->|
 *
 * It is important to set packet-go ready to high to send multiple commnads
 * within one vblank interval at the same time. Because as soon as it becomes
 * high, the piled commands will start transmission. The ready_allow_period is
 * defined so that the stacked commands would not be sent in two vblank
 * intervals. The ready_allow_period is set from start of vblank to end of
 * CMD ALLOW period - 1ms. The 1ms is enough time to send the stacked commands
 * at once.
 */
#define PKTGO_READY_MARGIN_NS	1000000
static void need_wait_vblank(struct dsim_device *dsim)
{
	const struct decon_device *decon = dsim_get_decon(dsim);
	struct drm_vblank_crtc *vblank;
	struct drm_crtc *crtc;
	ktime_t last_vblanktime, diff, cur_time;
	int ready_allow_period;

	if (!decon)
		return;

	crtc = &decon->crtc->base;
	if (!crtc)
		return;

	if (drm_crtc_vblank_get(crtc))
		return;

	vblank = &crtc->dev->vblank[crtc->index];
	ready_allow_period =
		mult_frac(vblank->framedur_ns, 95, 100) - PKTGO_READY_MARGIN_NS;

	drm_crtc_vblank_count_and_time(crtc, &last_vblanktime);
	cur_time = ktime_get();
	diff = ktime_sub_ns(cur_time, last_vblanktime);

	dsim_debug(dsim, "last(%lld) cur(%lld) diff(%lld) ready allow period(%d)\n",
			last_vblanktime, cur_time, diff, ready_allow_period);

	if (diff > ready_allow_period) {
		DPU_ATRACE_BEGIN("dsim_pktgo_wait_vblank");
		drm_crtc_wait_one_vblank(crtc);
		DPU_ATRACE_END("dsim_pktgo_wait_vblank");
	}
	drm_crtc_vblank_put(crtc);
}

#define PL_FIFO_THRESHOLD	mult_frac(MAX_PL_FIFO, 75, 100) /* 75% */
#define IS_LAST(flags)		(((flags) & MIPI_DSI_MSG_LASTCOMMAND) != 0)
static int dsim_write_data_locked(struct dsim_device *dsim, const struct mipi_dsi_msg *msg)
{
	int ret = 0;
	const u16 flags = msg->flags;
	bool is_last;
	struct mipi_dsi_packet packet = { .size = 0 };

	WARN_ON(!mutex_is_locked(&dsim->cmd_lock));

	if (msg->tx_len > 0) {
		const u8 *tx_buf = msg->tx_buf;

		ret = mipi_dsi_create_packet(&packet, msg);
		if (ret) {
			dsim_err(dsim, "unable to create dsi packet (%d)\n", ret);
			return 0;
		}

		DPU_EVENT_LOG_CMD(dsim, msg->type, tx_buf[0], msg->tx_len);
	}
	DPU_ATRACE_BEGIN(__func__);

	if (dsim->config.mode == DSIM_VIDEO_MODE) {
		if (flags & (EXYNOS_DSI_MSG_FORCE_BATCH | EXYNOS_DSI_MSG_FORCE_FLUSH))
			dsim_warn(dsim, "force batching is attempted in video mode\n");
		if (packet.size)
			ret = dsim_write_single_cmd_locked(dsim, &packet);
		goto err;
	}

	if (flags & EXYNOS_DSI_MSG_FORCE_BATCH) {
		WARN_ON(dsim->force_batching);
		dsim->force_batching = true;
		goto err;
	}

	if (((dsim->total_pend_pl + packet.payload_length) > MAX_PL_FIFO) ||
	    (dsim->total_pend_ph >= MAX_PH_FIFO)) {
		dsim_err(dsim, "fifo would be full. ph(%u) pl(%lu) max(%d/%d)\n",
				dsim->total_pend_ph,
				dsim->total_pend_pl + msg->tx_len,
				MAX_PH_FIFO, MAX_PL_FIFO);
		ret = -EINVAL;
		goto err;
	}

	is_last = (IS_LAST(flags) && !dsim->force_batching) || (flags & EXYNOS_DSI_MSG_FORCE_FLUSH);

	if (flags & EXYNOS_DSI_MSG_FORCE_FLUSH) {
		dsim->force_batching = false;
		/* force batching should happen only with empty msg */
		WARN_ON(packet.size);
	}

	if (!is_last && packet.size &&
	    (((dsim->total_pend_ph + 1) >= MAX_PH_FIFO) ||
	     ((dsim->total_pend_pl + packet.payload_length) > PL_FIFO_THRESHOLD))) {
		dsim_warn(dsim, "warning. changed last command. pend pl/pl(%u,%u) new pl(%zu)\n",
			  dsim->total_pend_ph, dsim->total_pend_pl, packet.payload_length);
		is_last = true;
	}

	trace_dsi_tx(msg->type, msg->tx_buf, msg->tx_len, is_last);
	dsim_debug(dsim, "%s last command\n", is_last ? "" : "Not");

	if (is_last) {
		if (dsim_cmd_packetgo_is_enabled(dsim)) {
			if (packet.size > 0)
				dsim_cmd_packetgo_queue_locked(dsim, &packet);

			if (!(flags & EXYNOS_DSI_MSG_IGNORE_VBLANK))
				need_wait_vblank(dsim);

			ret = dsim_cmd_packetgo_flush_locked(dsim);
		} else if (packet.size > 0) {
			ret = dsim_write_single_cmd_locked(dsim, &packet);
		}
	} else if (packet.size > 0) {
		dsim_cmd_packetgo_queue_locked(dsim, &packet);
	}

err:
	trace_dsi_cmd_fifo_status(dsim->total_pend_ph, dsim->total_pend_pl);
	DPU_ATRACE_END(__func__);
	return ret;
}

static int
dsim_req_read_command(struct dsim_device *dsim, const struct mipi_dsi_msg *msg)
{
	struct mipi_dsi_packet packet;
	const u8 rx_len = msg->rx_len & 0xff;

	dsim_reg_clear_int(dsim->id, DSIM_INTSRC_SFR_PH_FIFO_EMPTY);
	reinit_completion(&dsim->ph_wr_comp);
	trace_dsi_tx(MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE, &rx_len, 1, true);
	/* set the maximum packet size returned */
	dsim_reg_wr_tx_header(dsim->id, MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
			msg->rx_len, 0, false);

	/* read request */
	mipi_dsi_create_packet(&packet, msg);
	trace_dsi_tx(msg->type, msg->tx_buf, msg->tx_len, true);
	dsim_reg_wr_tx_header(dsim->id, packet.header[0], packet.header[1],
						packet.header[2], true);

	return dsim_wait_for_cmd_fifo_empty(dsim, false);
}

static int
dsim_read_data(struct dsim_device *dsim, const struct mipi_dsi_msg *msg)
{
	u32 rx_fifo, rx_size = 0;
	int i = 0, ret = 0;
	u8 *rx_buf = msg->rx_buf;
	const u8 *tx_buf = msg->tx_buf;

	if (msg->rx_len > MAX_RX_FIFO) {
		dsim_err(dsim, "invalid rx len(%lu) max(%d)\n", msg->rx_len,
				MAX_RX_FIFO);
		return -EINVAL;
	}

	/* Init RX FIFO before read and clear DSIM_INTSRC */
	dsim_reg_clear_int(dsim->id, DSIM_INTSRC_RX_DATA_DONE);

	reinit_completion(&dsim->rd_comp);

	ret = dsim_req_read_command(dsim, msg);
	if (ret) {
		dsim_err(dsim, "failed to request dsi read command\n");
		return ret;
	}

	if (!wait_for_completion_timeout(&dsim->rd_comp, MIPI_RD_TIMEOUT)) {
		dsim_err(dsim, "read timeout\n");
		return -ETIMEDOUT;
	}

	rx_fifo = dsim_reg_get_rx_fifo(dsim->id);
	dsim_debug(dsim, "rx fifo:0x%8x, response:0x%x, rx_len:%lu\n", rx_fifo,
		 rx_fifo & 0xff, msg->rx_len);

	/* Parse the RX packet data types */
	switch (rx_fifo & 0xff) {
	case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
		ret = dsim_reg_rx_err_handler(dsim->id, rx_fifo);
		if (ret < 0) {
			dsim_dump(dsim);
			return ret;
		}
		break;
	case MIPI_DSI_RX_END_OF_TRANSMISSION:
		dsim_debug(dsim, "EoTp was received\n");
		break;
	case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
	case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
		WARN_ON(msg->rx_len > 2);
		rx_buf[1] = (rx_fifo >> 16) & 0xff;
	case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
	case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
		rx_buf[0] = (rx_fifo >> 8) & 0xff;
		dsim_debug(dsim, "short packet was received\n");
		rx_size = msg->rx_len;
		break;
	case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
	case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
		dsim_debug(dsim, "long packet was received\n");
		rx_size = (rx_fifo & 0x00ffff00) >> 8;

		while (i < rx_size) {
			const u32 rx_max =
				min_t(u32, rx_size, i + sizeof(rx_fifo));

			rx_fifo = dsim_reg_get_rx_fifo(dsim->id);
			dsim_debug(dsim, "payload: 0x%x i=%d max=%d\n", rx_fifo,
					i, rx_max);
			for (; i < rx_max; i++, rx_fifo >>= 8)
				rx_buf[i] = rx_fifo & 0xff;
		}
		break;
	default:
		dsim_err(dsim, "packet format is invalid.\n");
		dsim_dump(dsim);
		return -EBUSY;
	}

	if (!dsim_reg_rx_fifo_is_empty(dsim->id)) {
		u32 retry_cnt = RETRY_READ_FIFO_MAX;

		dsim_warn(dsim, "RX FIFO is not empty: rx_size:%u, rx_len:%lu\n",
			rx_size, msg->rx_len);
		dsim_dump(dsim);
		do {
			rx_fifo = dsim_reg_get_rx_fifo(dsim->id);
			dsim_info(dsim, "rx fifo:0x%8x, response:0x%x\n",
				rx_fifo, rx_fifo & 0xff);
		} while (!dsim_reg_rx_fifo_is_empty(dsim->id) && --retry_cnt);
	}

	trace_dsi_rx(tx_buf[0], rx_buf, rx_size);
	return rx_size;
}

static int
dsim_write_data_dual(struct dsim_device *dsim, const struct mipi_dsi_msg *msg)
{
	int ret;

	ret = pm_runtime_resume_and_get(dsim->dev);
	if (ret) {
		dsim_err(dsim, "runtime resume failed (%d). unable to transfer cmd\n", ret);
		return ret;
	}

	mutex_lock(&dsim->cmd_lock);

	ret = dsim_write_data_locked(dsim, msg);

	mutex_unlock(&dsim->cmd_lock);

	pm_runtime_mark_last_busy(dsim->dev);
	pm_runtime_put_sync_autosuspend(dsim->dev);

	return ret;
}
static ssize_t dsim_host_transfer(struct mipi_dsi_host *host,
			    const struct mipi_dsi_msg *msg)
{
	struct dsim_device *dsim = host_to_dsi(host);
	struct dsim_device *sec_dsi;
	int ret;

	DPU_ATRACE_BEGIN(__func__);

	ret = pm_runtime_resume_and_get(dsim->dev);
	if (ret) {
		dsim_err(dsim, "runtime resume failed (%d). unable to transfer cmd\n", ret);
		return ret;
	}

	mutex_lock(&dsim->cmd_lock);
	if (WARN_ON(dsim->state != DSIM_STATE_HSCLKEN)) {
		ret = -EPERM;
		goto abort;
	}

	switch (msg->type) {
	case MIPI_DSI_DCS_READ:
	case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
	case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
	case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
		ret = dsim_read_data(dsim, msg);
		break;
	default:
		ret = dsim_write_data_locked(dsim, msg);
		if (dsim->dual_dsi == DSIM_DUAL_DSI_MAIN) {
			sec_dsi = exynos_get_dual_dsi(DSIM_DUAL_DSI_SEC);
			if (sec_dsi)
				ret = dsim_write_data_dual(sec_dsi, msg);
			else
				dsim_err(dsim, "could not get secondary dsi\n");
		}
		break;
	}

abort:
	mutex_unlock(&dsim->cmd_lock);

	pm_runtime_mark_last_busy(dsim->dev);
	pm_runtime_put_sync_autosuspend(dsim->dev);

	DPU_ATRACE_END(__func__);

	return ret;
}

/* TODO: Below operation will be registered after panel driver is created. */
static const struct mipi_dsi_host_ops dsim_host_ops = {
	.attach = dsim_host_attach,
	.detach = dsim_host_detach,
	.transfer = dsim_host_transfer,
};

static int dsim_calc_pmsk(struct dsim_pll_features *pll_features,
			 struct stdphy_pms *pms, unsigned int hs_clock_mhz)
{
	uint64_t hs_clock;
	uint64_t fvco, q;
	uint32_t p, m, s, k;

	p = DIV_ROUND_CLOSEST(pll_features->finput, pll_features->foptimum);
	if (p == 0)
		p = 1;
	if ((p < pll_features->p_min) || (p > pll_features->p_max)) {
		pr_err("%s: p %u is out of range (%u, %u)\n",
		       __func__, p, pll_features->p_min, pll_features->p_max);
		return -EINVAL;
	}

	hs_clock = (uint64_t) hs_clock_mhz * 1000000;
	if ((hs_clock < pll_features->fout_min) ||
			(hs_clock > pll_features->fout_max)) {
		pr_err("%s: hs clock %llu out of range\n", __func__, hs_clock);
		return -EINVAL;
	}

	/* find s: vco_min <= fout * 2 ^ s <= vco_max */
	for (s = 0, fvco = 0; fvco < pll_features->fvco_min; s++)
		fvco = hs_clock * (1 << s);
	--s;

	if (fvco > pll_features->fvco_max) {
		pr_err("%s: no proper s found\n", __func__);
		return -EINVAL;
	}
	if ((s < pll_features->s_min) || (s > pll_features->s_max)) {
		pr_err("%s: s %u is out of range (%u, %u)\n",
		       __func__, s, pll_features->s_min, pll_features->s_max);
		return -EINVAL;
	}

	/* (hs_clk * 2^s / 2) / (fin / p) = m + k / 2^k_bits */
	fvco >>= 1;
	q = fvco << (pll_features->k_bits + 1); /* 1 extra bit for roundup */
	q /= pll_features->finput / p;

	/* m is the integer part, k is the fraction part */
	m = q >> (pll_features->k_bits + 1);
	if ((m < pll_features->m_min) || (m > pll_features->m_max)) {
		pr_err("%s: m %u is out of range (%u, %u)\n",
		       __func__, m, pll_features->m_min, pll_features->m_max);
		return -EINVAL;
	}

	k = q & ((1 << (pll_features->k_bits + 1)) - 1);
	k = DIV_ROUND_UP(k, 2);

	/* k is two's complement integer */
	if (k & (1 << (pll_features->k_bits - 1)))
		m++;

	pms->p = p;
	pms->m = m;
	pms->s = s;
	pms->k = k;

	return 0;
}

static int dsim_calc_underrun(const struct dsim_device *dsim, uint32_t hs_clock_mhz,
		uint32_t *underrun)
{
	const struct dsim_reg_config *config = &dsim->config;
	uint32_t lanes = config->data_lane_cnt;
	uint32_t number_of_transfer;
	uint32_t w_threshold;
	uint64_t wclk;
	uint64_t max_frame_time;
	uint64_t frame_data;
	uint64_t packet_header;
	uint64_t min_frame_transfer_time;
	uint64_t max_lp_time;

	number_of_transfer = config->p_timing.vactive;
	w_threshold = config->p_timing.hactive;
	if (config->dsc.enabled)
		w_threshold /= 3;
	wclk = (uint64_t) hs_clock_mhz * 1000000 / 16;

	/* max time to transfer one frame, in the unit of nanosecond */
	max_frame_time = NSEC_PER_SEC * 100 /
		(config->p_timing.vrefresh * (100 + config->p_timing.te_var)) -
		NSEC_PER_USEC * config->p_timing.te_idle_us;
	/* one frame pixel data (bytes) */
	frame_data = number_of_transfer * w_threshold * config->bpp / 8;
	/* packet header (bytes) */
	packet_header = number_of_transfer * 7;
	/* minimum time to transfer one frame, in nanosecond */
	min_frame_transfer_time = (frame_data + packet_header) *
					NSEC_PER_SEC / (2 * lanes * wclk);

	if (max_frame_time < min_frame_transfer_time) {
		pr_err("%s: max frame time %llu < min frame time %llu\n",
			__func__, max_frame_time, min_frame_transfer_time);
		return -EINVAL;
	}

	max_lp_time = max_frame_time - min_frame_transfer_time;
	/* underrun unit is 100 wclk, round up */
	*underrun = (uint32_t) DIV_ROUND_UP(max_lp_time * wclk / NSEC_PER_SEC, 100);

	return 0;
}

static int dsim_set_hs_clock(struct dsim_device *dsim, unsigned int hs_clock, bool apply_now)
{
	int ret;
	struct stdphy_pms pms;
	uint32_t lp_underrun = 0;
	struct dsim_pll_param *pll_param;

	if (!dsim->pll_params || !dsim->pll_params->features)
		return -ENODEV;

	memset(&pms, 0, sizeof(pms));
	ret = dsim_calc_pmsk(dsim->pll_params->features, &pms, hs_clock);
	if (ret < 0) {
		dsim_err(dsim, "Failed to update pll for hsclk %d\n", hs_clock);
		return -EINVAL;
	}

	mutex_lock(&dsim->state_lock);
	ret = dsim_calc_underrun(dsim, hs_clock, &lp_underrun);
	if (ret < 0) {
		dsim_err(dsim, "Failed to update underrun\n");
		goto out;
	}

	pll_param = dsim->current_pll_param;
	if (!pll_param) {
		ret = -EAGAIN;
		goto out;
	}

	pll_param->pll_freq = hs_clock;
	pll_param->p = pms.p;
	pll_param->m = pms.m;
	pll_param->s = pms.s;
	pll_param->k = pms.k;
	pll_param->cmd_underrun_cnt = lp_underrun;
	dsim_update_clock_config(dsim, pll_param);

	if (!apply_now || dsim->state != DSIM_STATE_HSCLKEN)
		goto out;

	/* Restart dsim to apply new clock settings */
	dsim_restart(dsim);
out:
	mutex_unlock(&dsim->state_lock);

	return ret;
}

static ssize_t bist_mode_show(struct device *dev,
				      struct device_attribute *attr, char *buf)
{

	struct dsim_device *dsim = dev_get_drvdata(dev);

	return scnprintf(buf, PAGE_SIZE, "%d\n", dsim->bist_mode);
}

static ssize_t bist_mode_store(struct device *dev,
				       struct device_attribute *attr,
				       const char *buf, size_t len)
{
	struct dsim_device *dsim = dev_get_drvdata(dev);
	int rc;
	unsigned int bist_mode;
	bool bist_en;

	rc = kstrtouint(buf, 0, &bist_mode);
	if (rc < 0)
		return rc;

	/*
	 * BIST modes:
	 * 0: Disable, 1: Color Bar, 2: GRAY Gradient, 3: User-Defined,
	 * 4: Prbs7 Random
	 */
	if (bist_mode > DSIM_BIST_MODE_MAX) {
		dsim_err(dsim, "invalid bist mode\n");
		return -EINVAL;
	}

	bist_en = bist_mode > 0;

	if (bist_en && dsim->state == DSIM_STATE_SUSPEND)
		_dsim_enable(dsim);

	dsim_reg_set_bist(dsim->id, bist_en, bist_mode - 1);
	dsim->bist_mode = bist_mode;

	if (!bist_en && dsim->state == DSIM_STATE_HSCLKEN)
		_dsim_disable(dsim);

	dsim_info(dsim, "0:Disable 1:ColorBar 2:GRAY Gradient 3:UserDefined\n");
	dsim_info(dsim, "4:Prbs7 Random (%d)\n", dsim->bist_mode);

	return len;
}
static DEVICE_ATTR_RW(bist_mode);

static ssize_t hs_clock_show(struct device *dev,
			     struct device_attribute *attr,
			     char *buf)
{
	struct dsim_device *dsim = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%d\n", dsim->clk_param.hs_clk);
}

static ssize_t hs_clock_store(struct device *dev,
			      struct device_attribute *attr,
			      const char *buf, size_t len)
{
	struct dsim_device *dsim = dev_get_drvdata(dev);
	int rc;
	unsigned int hs_clock;
	bool apply_now = true;

	char params[32];
	char *hs_clk_str;
	char *apply_now_str;
	char *p = params;

	strlcpy(params, buf, sizeof(params));
	hs_clk_str = strsep(&p, " ");
	apply_now_str = strsep(&p, " ");

	if (apply_now_str) {
		rc = kstrtobool(apply_now_str, &apply_now);
		if (rc < 0)
		    return rc;
	}

	rc = kstrtouint(hs_clk_str, 0, &hs_clock);
	if (rc < 0)
		return rc;

	/* ddr hs_clock unit: MHz */
	dsim_info(dsim, "%s: hs clock %u, apply now: %u\n", __func__, hs_clock, apply_now);
	rc = dsim_set_hs_clock(dsim, hs_clock, apply_now);
	if (rc < 0)
		return rc;

	return len;
}
static DEVICE_ATTR_RW(hs_clock);

static int dsim_get_pinctrl(struct dsim_device *dsim)
{
	int ret = 0;

	dsim->pinctrl = devm_pinctrl_get(dsim->dev);
	if (IS_ERR(dsim->pinctrl)) {
		ret = PTR_ERR(dsim->pinctrl);
		dsim_debug(dsim, "failed to get pinctrl (%d)\n", ret);
		dsim->pinctrl = NULL;
		/* optional in video mode */
		return 0;
	}

	dsim->te_on = pinctrl_lookup_state(dsim->pinctrl, "hw_te_on");
	if (IS_ERR(dsim->te_on)) {
		dsim_err(dsim, "failed to get hw_te_on pin state\n");
		ret = PTR_ERR(dsim->te_on);
		dsim->te_on = NULL;
		goto err;
	}
	dsim->te_off = pinctrl_lookup_state(dsim->pinctrl, "hw_te_off");
	if (IS_ERR(dsim->te_off)) {
		dsim_err(dsim, "failed to get hw_te_off pin state\n");
		ret = PTR_ERR(dsim->te_off);
		dsim->te_off = NULL;
		goto err;
	}

err:
	return ret;
}

static int dsim_probe(struct platform_device *pdev)
{
	struct dsim_device *dsim;
	int ret;

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

	dma_set_mask(&pdev->dev, DMA_BIT_MASK(36));

	dsim->dsi_host.ops = &dsim_host_ops;
	dsim->dsi_host.dev = &pdev->dev;
	dsim->dev = &pdev->dev;

	ret = dsim_parse_dt(dsim);
	if (ret)
		goto err;

	dsim_drvdata[dsim->id] = dsim;

	dsim->output_type = (dsim->id == 0) ?
			EXYNOS_DISPLAY_TYPE_DSI0 : EXYNOS_DISPLAY_TYPE_DSI1;

	spin_lock_init(&dsim->slock);
	mutex_init(&dsim->cmd_lock);
	mutex_init(&dsim->state_lock);
	init_completion(&dsim->ph_wr_comp);
	init_completion(&dsim->pl_wr_comp);
	init_completion(&dsim->rd_comp);

	ret = dsim_init_resources(dsim);
	if (ret)
		goto err;

	ret = dsim_get_pinctrl(dsim);
	if (ret)
		goto err;

	ret = device_create_file(dsim->dev, &dev_attr_bist_mode);
	if (ret < 0)
		dsim_err(dsim, "failed to add sysfs bist_mode entries\n");

	ret = device_create_file(dsim->dev, &dev_attr_hs_clock);
	if (ret < 0)
		dsim_err(dsim, "failed to add sysfs hs_clock entries\n");

	platform_set_drvdata(pdev, &dsim->encoder);

#if defined(CONFIG_CPU_IDLE)
	dsim->idle_ip_index = exynos_get_idle_ip_index(dev_name(&pdev->dev));
	dsim_info(dsim, "dsim idle_ip_index[%d]\n", dsim->idle_ip_index);
	if (dsim->idle_ip_index < 0)
		dsim_warn(dsim, "idle ip index is not provided\n");
	exynos_update_ip_idle_status(dsim->idle_ip_index, 1);
#endif
	dsim->state = DSIM_STATE_HANDOVER;

	/* parse the panel name to select the dsi device for the detected panel */
	dsim_parse_panel_name(dsim);

	// TODO: get which panel is active from bootloader?

	pm_runtime_use_autosuspend(dsim->dev);
	pm_runtime_set_autosuspend_delay(dsim->dev, 20);
	pm_runtime_enable(dsim->dev);

	if (!IS_ENABLED(CONFIG_BOARD_EMULATOR)) {
		phy_init(dsim->res.phy);
		if (dsim->res.phy_ex)
			phy_init(dsim->res.phy_ex);
	}

	dsim_info(dsim, "driver has been probed.\n");
	return component_add(dsim->dev, &dsim_component_ops);

err:
	dsim_err(dsim, "failed to probe exynos dsim driver\n");
	return ret;
}

static int dsim_remove(struct platform_device *pdev)
{
	struct dsim_device *dsim = platform_get_drvdata(pdev);

	device_remove_file(dsim->dev, &dev_attr_bist_mode);
	device_remove_file(dsim->dev, &dev_attr_hs_clock);
	pm_runtime_disable(&pdev->dev);

	component_del(&pdev->dev, &dsim_component_ops);

	iounmap(dsim->res.ss_reg_base);
	iounmap(dsim->res.phy_regs_ex);
	iounmap(dsim->res.phy_regs);
	iounmap(dsim->res.regs);

	return 0;
}

#ifdef CONFIG_PM
static int dsim_runtime_suspend(struct device *dev)
{
	struct dsim_device *dsim = dev_get_drvdata(dev);
	const struct decon_device *decon = dsim_get_decon(dsim);

	DPU_ATRACE_BEGIN(__func__);

	mutex_lock(&dsim->state_lock);

	dsim_debug(dsim, "state: %d\n", dsim->state);
	if (dsim->state == DSIM_STATE_HSCLKEN)
		_dsim_enter_ulps_locked(dsim);

	dsim->suspend_state = dsim->state;
	mutex_unlock(&dsim->state_lock);
	if (decon)
		DPU_EVENT_LOG(DPU_EVT_DSIM_RUNTIME_SUSPEND, decon->id, dsim);
	DPU_ATRACE_END(__func__);

	return 0;
}

static int dsim_runtime_resume(struct device *dev)
{
	struct dsim_device *dsim = dev_get_drvdata(dev);
	const struct decon_device *decon = dsim_get_decon(dsim);
	int ret = 0;

	DPU_ATRACE_BEGIN(__func__);

	mutex_lock(&dsim->state_lock);
	dsim_debug(dsim, "state: %d\n", dsim->state);

	if (dsim->state == DSIM_STATE_BYPASS)
		ret = -EPERM;
	else if (dsim->state == DSIM_STATE_ULPS)
		_dsim_exit_ulps_locked(dsim);

	dsim->suspend_state = dsim->state;
	mutex_unlock(&dsim->state_lock);

	if (decon)
		DPU_EVENT_LOG(DPU_EVT_DSIM_RUNTIME_RESUME, decon->id, dsim);
	DPU_ATRACE_END(__func__);

	return ret;
}

static int dsim_suspend(struct device *dev)
{
	struct dsim_device *dsim = dev_get_drvdata(dev);
	const struct decon_device *decon = dsim_get_decon(dsim);

	mutex_lock(&dsim->state_lock);
	dsim->suspend_state = dsim->state;

	if (dsim->state == DSIM_STATE_HSCLKEN) {
		_dsim_enter_ulps_locked(dsim);
		dev->power.must_resume = true;
	}

	dsim_debug(dsim, "-\n");

	mutex_unlock(&dsim->state_lock);

	if (decon)
		DPU_EVENT_LOG(DPU_EVT_DSIM_SUSPEND, decon->id, dsim);

	return 0;
}

static int dsim_resume(struct device *dev)
{
	struct dsim_device *dsim = dev_get_drvdata(dev);
	const struct decon_device *decon = dsim_get_decon(dsim);

	mutex_lock(&dsim->state_lock);
	if (dsim->suspend_state == DSIM_STATE_HSCLKEN)
		_dsim_exit_ulps_locked(dsim);

	dsim_debug(dsim, "-\n");

	mutex_unlock(&dsim->state_lock);

	if (decon)
		DPU_EVENT_LOG(DPU_EVT_DSIM_RESUME, decon->id, dsim);

	return 0;
}
#endif

static const struct dev_pm_ops dsim_pm_ops = {
	SET_RUNTIME_PM_OPS(dsim_runtime_suspend, dsim_runtime_resume, NULL)
	SET_LATE_SYSTEM_SLEEP_PM_OPS(dsim_suspend, dsim_resume)
};

struct platform_driver dsim_driver = {
	.probe = dsim_probe,
	.remove = dsim_remove,
	.driver = {
		   .name = "exynos-dsim",
		   .owner = THIS_MODULE,
		   .of_match_table = dsim_of_match,
		   .pm = &dsim_pm_ops,
	},
};

MODULE_SOFTDEP("pre: phy-exynos-mipi");
MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
MODULE_DESCRIPTION("Samsung SoC MIPI DSI Master");
MODULE_LICENSE("GPL v2");