hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2020 Rockchip Electronics Co., Ltd.
 * Author: Andy Yan <andy.yan@rock-chips.com>
 */
#include <drm/drm.h>
#include <drm/drm_atomic.h>
#include <drm/drm_atomic_uapi.h>
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_debugfs.h>
#include <drm/drm_flip_work.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_gem_framebuffer_helper.h>
#include <drm/drm_plane_helper.h>
#include <drm/drm_probe_helper.h>
#include <drm/drm_self_refresh_helper.h>
 
#include <drm/drm_writeback.h>
#ifdef CONFIG_DRM_ANALOGIX_DP
#include <drm/bridge/analogix_dp.h>
#endif
#include <dt-bindings/soc/rockchip-system-status.h>
 
#include <linux/debugfs.h>
#include <linux/fixp-arith.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/clk/clk-conf.h>
#include <linux/iopoll.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_graph.h>
#include <linux/pm_runtime.h>
#include <linux/component.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <linux/mfd/syscon.h>
#include <linux/delay.h>
#include <linux/swab.h>
#include <linux/sort.h>
#include <linux/rockchip/cpu.h>
#include <linux/workqueue.h>
#include <linux/types.h>
#include <soc/rockchip/rockchip_dmc.h>
#include <soc/rockchip/rockchip-system-status.h>
#include <uapi/linux/videodev2.h>
 
#include "../drm_crtc_internal.h"
#include "../drm_internal.h"
 
#include "rockchip_drm_drv.h"
#include "rockchip_drm_gem.h"
#include "rockchip_drm_fb.h"
#include "rockchip_drm_vop.h"
#include "rockchip_vop_reg.h"
#include "rockchip_post_csc.h"
 
#define _REG_SET(vop2, name, off, reg, mask, v, relaxed) \
       vop2_mask_write(vop2, off + reg.offset, mask, reg.shift, v, reg.write_mask, relaxed)
 
#define REG_SET(x, name, off, reg, v, relaxed) \
       _REG_SET(x, name, off, reg, reg.mask, v, relaxed)
#define REG_SET_MASK(x, name, off, reg, mask, v, relaxed) \
       _REG_SET(x, name, off, reg, reg.mask & mask, v, relaxed)
 
#define REG_GET(vop2, reg) ((vop2_readl(vop2, reg.offset) >> reg.shift) & reg.mask)
 
#define VOP_CLUSTER_SET(x, win, name, v) \
   do { \
       if (win->regs->cluster) \
           REG_SET(x, name, 0, win->regs->cluster->name, v, true); \
   } while (0)
 
#define VOP_AFBC_SET(x, win, name, v) \
   do { \
       if (win->regs->afbc) \
           REG_SET(x, name, win->offset, win->regs->afbc->name, v, true); \
   } while (0)
 
#define VOP_WIN_SET(x, win, name, v) \
       REG_SET(x, name, win->offset, VOP_WIN_NAME(win, name), v, true)
 
#define VOP_SCL_SET(x, win, name, v) \
       REG_SET(x, name, win->offset, win->regs->scl->name, v, true)
 
#define VOP_CTRL_SET(x, name, v) \
       REG_SET(x, name, 0, (x)->data->ctrl->name, v, false)
 
#define VOP_CTRL_GET(x, name) vop2_read_reg(x, 0, &(x)->data->ctrl->name)
 
#define VOP_INTR_GET(vop2, name) \
       vop2_read_reg(vop2, 0, &vop2->data->ctrl->name)
 
#define VOP_INTR_SET(vop2, intr, name, v) \
       REG_SET(vop2, name, 0, intr->name, v, false)
 
#define VOP_MODULE_SET(vop2, module, name, v) \
       REG_SET(vop2, name, 0, module->regs->name, v, false)
 
#define VOP_INTR_SET_MASK(vop2, intr, name, mask, v) \
       REG_SET_MASK(vop2, name, 0, intr->name, mask, v, false)
 
#define VOP_INTR_SET_TYPE(vop2, intr, name, type, v) \
   do { \
       int i, reg = 0, mask = 0; \
       for (i = 0; i < intr->nintrs; i++) { \
           if (intr->intrs[i] & type) { \
               reg |= (v) << i; \
               mask |= 1 << i; \
           } \
       } \
       VOP_INTR_SET_MASK(vop2, intr, name, mask, reg); \
   } while (0)
 
#define VOP_INTR_GET_TYPE(vop2, intr, name, type) \
       vop2_get_intr_type(vop2, intr, &intr->name, type)
 
#define VOP_MODULE_GET(x, module, name) \
       vop2_read_reg(x, 0, &module->regs->name)
 
#define VOP_WIN_GET(vop2, win, name) \
       vop2_read_reg(vop2, win->offset, &VOP_WIN_NAME(win, name))
 
#define VOP_WIN_GET_REG_BAK(vop2, win, name) \
           vop2_read_reg_bak(vop2, win->offset, &VOP_WIN_NAME(win, name))
 
#define VOP_WIN_NAME(win, name) \
       (vop2_get_win_regs(win, &win->regs->name)->name)
 
#define VOP_WIN_TO_INDEX(vop2_win) \
   ((vop2_win) - (vop2_win)->vop2->win)
 
#define VOP_GRF_SET(vop2, grf, reg, v) \
   do { \
       if (vop2->data->grf) { \
           vop2_grf_writel(vop2->grf, vop2->data->grf->reg, v); \
       } \
   } while (0)
 
#define to_vop2_win(x) container_of(x, struct vop2_win, base)
#define to_vop2_plane_state(x) container_of(x, struct vop2_plane_state, base)
#define to_wb_state(x) container_of(x, struct vop2_wb_connector_state, base)
#define output_if_is_hdmi(x)        (x & (VOP_OUTPUT_IF_HDMI0 | VOP_OUTPUT_IF_HDMI1))
#define output_if_is_dp(x)        (x & (VOP_OUTPUT_IF_DP0 | VOP_OUTPUT_IF_DP1))
#define output_if_is_edp(x)        (x & (VOP_OUTPUT_IF_eDP0 | VOP_OUTPUT_IF_eDP1))
#define output_if_is_mipi(x)        (x & (VOP_OUTPUT_IF_MIPI0 | VOP_OUTPUT_IF_MIPI1))
#define output_if_is_lvds(x)        (x & (VOP_OUTPUT_IF_LVDS0 | VOP_OUTPUT_IF_LVDS1))
#define output_if_is_dpi(x)        (x & (VOP_OUTPUT_IF_BT656 | VOP_OUTPUT_IF_BT1120 | \
                         VOP_OUTPUT_IF_RGB))
 
/*
 * max two jobs a time, one is running(writing back),
 * another one will run in next frame.
 */
#define VOP2_WB_JOB_MAX      2
#define VOP2_SYS_AXI_BUS_NUM 2
 
#define VOP2_MAX_VP_OUTPUT_WIDTH    4096
/* KHZ */
#define VOP2_MAX_DCLK_RATE        600000
/* KHZ */
#define VOP2_COMMON_ACLK_RATE        500000
 
enum vop2_data_format {
   VOP2_FMT_ARGB8888 = 0,
   VOP2_FMT_RGB888,
   VOP2_FMT_RGB565,
   VOP2_FMT_XRGB101010,
   VOP2_FMT_YUV420SP,
   VOP2_FMT_YUV422SP,
   VOP2_FMT_YUV444SP,
   VOP2_FMT_YUYV422 = 8,
   VOP2_FMT_YUYV420,
   VOP2_FMT_VYUY422,
   VOP2_FMT_VYUY420,
   VOP2_FMT_YUV420SP_TILE_8x4 = 0x10,
   VOP2_FMT_YUV420SP_TILE_16x2,
   VOP2_FMT_YUV422SP_TILE_8x4,
   VOP2_FMT_YUV422SP_TILE_16x2,
   VOP2_FMT_YUV420SP_10,
   VOP2_FMT_YUV422SP_10,
   VOP2_FMT_YUV444SP_10,
};
 
enum vop2_afbc_format {
   VOP2_AFBC_FMT_RGB565,
   VOP2_AFBC_FMT_ARGB2101010 = 2,
   VOP2_AFBC_FMT_YUV420_10BIT,
   VOP2_AFBC_FMT_RGB888,
   VOP2_AFBC_FMT_ARGB8888,
   VOP2_AFBC_FMT_YUV420 = 9,
   VOP2_AFBC_FMT_YUV422 = 0xb,
   VOP2_AFBC_FMT_YUV422_10BIT = 0xe,
   VOP2_AFBC_FMT_INVALID = -1,
};
 
enum vop2_tiled_format {
   VOP2_TILED_8X8_FMT_YUV420SP = 0xc,
   VOP2_TILED_8X8_FMT_YUV422SP,
   VOP2_TILED_8X8_FMT_YUV444SP,
   VOP2_TILED_8X8_FMT_YUV400SP,
   VOP2_TILED_8X8_FMT_YUV420SP_10 = 0x1c,
   VOP2_TILED_8X8_FMT_YUV422SP_10,
   VOP2_TILED_8X8_FMT_YUV444SP_10,
   VOP2_TILED_8X8_FMT_YUV400SP_10,
   VOP2_TILED_FMT_INVALID = -1,
};
 
enum vop3_tiled_format {
   VOP3_TILED_4X4_FMT_YUV420SP = 0xc,
   VOP3_TILED_4X4_FMT_YUV422SP,
   VOP3_TILED_4X4_FMT_YUV444SP,
   VOP3_TILED_4X4_FMT_YUV400SP,
   VOP3_TILED_4X4_FMT_YUV420SP_10 = 0x1c,
   VOP3_TILED_4X4_FMT_YUV422SP_10,
   VOP3_TILED_4X4_FMT_YUV444SP_10,
   VOP3_TILED_4X4_FMT_YUV400SP_10,
 
   VOP3_TILED_8X8_FMT_YUV420SP = 0x2c,
   VOP3_TILED_8X8_FMT_YUV422SP,
   VOP3_TILED_8X8_FMT_YUV444SP,
   VOP3_TILED_8X8_FMT_YUV400SP,
   VOP3_TILED_8X8_FMT_YUV420SP_10 = 0x3c,
   VOP3_TILED_8X8_FMT_YUV422SP_10,
   VOP3_TILED_8X8_FMT_YUV444SP_10,
   VOP3_TILED_8X8_FMT_YUV400SP_10,
 
   VOP3_TILED_FMT_INVALID = -1,
};
 
enum vop2_hdr_lut_mode {
   VOP2_HDR_LUT_MODE_AXI,
   VOP2_HDR_LUT_MODE_AHB,
};
 
enum vop2_pending {
   VOP_PENDING_FB_UNREF,
};
 
enum vop2_layer_phy_id {
   ROCKCHIP_VOP2_CLUSTER0 = 0,
   ROCKCHIP_VOP2_CLUSTER1,
   ROCKCHIP_VOP2_ESMART0,
   ROCKCHIP_VOP2_ESMART1,
   ROCKCHIP_VOP2_SMART0,
   ROCKCHIP_VOP2_SMART1,
   ROCKCHIP_VOP2_CLUSTER2,
   ROCKCHIP_VOP2_CLUSTER3,
   ROCKCHIP_VOP2_ESMART2,
   ROCKCHIP_VOP2_ESMART3,
   ROCKCHIP_VOP2_PHY_ID_INVALID = -1,
};
 
struct vop2_power_domain {
   struct vop2_power_domain *parent;
   struct vop2 *vop2;
   /*
    * @lock: protect power up/down procedure.
    * power on take effect immediately,
    * power down take effect by vsync.
    * we must check power_domain_status register
    * to make sure the power domain is down before
    * send a power on request.
    *
    */
   spinlock_t lock;
   unsigned int ref_count;
   bool on;
   /* @vp_mask: Bit mask of video port of the power domain's
    * module attached to.
    * For example: PD_CLUSTER0 belongs to module Cluster0, it's
    * bitmask is the VP which Cluster0 attached to. PD_ESMART is
    * shared between Esmart1/2/3, it's bitmask will be all the VP
    * which Esmart1/2/3 attached to.
    * This is used to check if we can power off a PD by vsync.
    */
   uint8_t vp_mask;
 
   const struct vop2_power_domain_data *data;
   struct list_head list;
   struct delayed_work power_off_work;
};
 
struct vop2_zpos {
   struct drm_plane *plane;
   int win_phys_id;
   int zpos;
};
 
union vop2_alpha_ctrl {
   uint32_t val;
   struct {
       /* [0:1] */
       uint32_t color_mode:1;
       uint32_t alpha_mode:1;
       /* [2:3] */
       uint32_t blend_mode:2;
       uint32_t alpha_cal_mode:1;
       /* [5:7] */
       uint32_t factor_mode:3;
       /* [8:9] */
       uint32_t alpha_en:1;
       uint32_t src_dst_swap:1;
       uint32_t reserved:6;
       /* [16:23] */
       uint32_t glb_alpha:8;
   } bits;
};
 
union vop2_bg_alpha_ctrl {
   uint32_t val;
   struct {
       /* [0:1] */
       uint32_t alpha_en:1;
       uint32_t alpha_mode:1;
       /* [2:3] */
       uint32_t alpha_pre_mul:1;
       uint32_t alpha_sat_mode:1;
       /* [4:7] */
       uint32_t reserved:4;
       /* [8:15] */
       uint32_t glb_alpha:8;
   } bits;
};
 
struct vop2_alpha {
   union vop2_alpha_ctrl src_color_ctrl;
   union vop2_alpha_ctrl dst_color_ctrl;
   union vop2_alpha_ctrl src_alpha_ctrl;
   union vop2_alpha_ctrl dst_alpha_ctrl;
};
 
struct vop2_alpha_config {
   bool src_premulti_en;
   bool dst_premulti_en;
   bool src_pixel_alpha_en;
   bool dst_pixel_alpha_en;
   u16 src_glb_alpha_value;
   u16 dst_glb_alpha_value;
};
 
struct vop2_plane_state {
   struct drm_plane_state base;
   int format;
   int zpos;
   struct drm_rect src;
   struct drm_rect dest;
   dma_addr_t yrgb_mst;
   dma_addr_t uv_mst;
   bool afbc_en;
   bool hdr_in;
   bool hdr2sdr_en;
   bool r2y_en;
   bool y2r_en;
   uint32_t csc_mode;
   uint8_t xmirror_en;
   uint8_t ymirror_en;
   uint8_t rotate_90_en;
   uint8_t rotate_270_en;
   uint8_t afbc_half_block_en;
   uint8_t tiled_en;
   int eotf;
   int color_space;
   int global_alpha;
   int blend_mode;
   uint64_t color_key;
   unsigned long offset;
   int pdaf_data_type;
   bool async_commit;
   struct vop_dump_list *planlist;
};
 
struct vop2_win {
   const char *name;
   struct vop2 *vop2;
   struct vop2_win *parent;
   struct drm_plane base;
 
   /*
    * This is for cluster window
    *
    * A cluster window can split as two windows:
    * a main window and a sub window.
    */
   bool two_win_mode;
 
   /**
    * ---------------------------
    * |          |              |
    * | Left     |  Right       |
    * |          |              |
    * | Cluster0 |  Cluster1    |
    * ---------------------------
    */
 
   /*
    * @splice_mode_right: As right part of the screen in splice mode.
    */
   bool splice_mode_right;
 
   /**
    * @splice_win: splice win which used to splice for a plane
    * hdisplay > 4096
    */
   struct vop2_win *splice_win;
   struct vop2_win *left_win;
 
   uint8_t splice_win_id;
 
   struct vop2_power_domain *pd;
 
   /**
    * @phys_id: physical id for cluster0/1, esmart0/1, smart0/1
    * Will be used as a identification for some register
    * configuration such as OVL_LAYER_SEL/OVL_PORT_SEL.
    */
   uint8_t phys_id;
 
   /**
    * @win_id: graphic window id, a cluster maybe split into two
    * graphics windows.
    */
   uint8_t win_id;
   /**
    * @area_id: multi display region id in a graphic window, they
    * share the same win_id.
    */
   uint8_t area_id;
   /**
    * @plane_id: unique plane id.
    */
   uint8_t plane_id;
   /**
    * @layer_id: id of the layer which the window attached to
    */
   uint8_t layer_id;
   const uint8_t *layer_sel_id;
   /**
    * @vp_mask: Bitmask of video_port0/1/2 this win attached to,
    * one win can only attach to one vp at the one time.
    */
   uint8_t vp_mask;
   /**
    * @old_vp_mask: Bitmask of video_port0/1/2 this win attached of last commit,
    * this is used for trackng the change of VOP2_PORT_SEL register.
    */
   uint8_t old_vp_mask;
   uint8_t zpos;
   uint32_t offset;
   uint8_t axi_id;
   uint8_t axi_yrgb_id;
   uint8_t axi_uv_id;
   uint8_t scale_engine_num;
   uint8_t possible_crtcs;
   enum drm_plane_type type;
   unsigned int max_upscale_factor;
   unsigned int max_downscale_factor;
   unsigned int supported_rotations;
   const uint8_t *dly;
   /*
    * vertical/horizontal scale up/down filter mode
    */
   uint8_t hsu_filter_mode;
   uint8_t hsd_filter_mode;
   uint8_t vsu_filter_mode;
   uint8_t vsd_filter_mode;
   uint8_t hsd_pre_filter_mode;
   uint8_t vsd_pre_filter_mode;
 
   const struct vop2_win_regs *regs;
   const uint64_t *format_modifiers;
   const uint32_t *formats;
   uint32_t nformats;
   uint64_t feature;
   struct drm_property *feature_prop;
   struct drm_property *input_width_prop;
   struct drm_property *input_height_prop;
   struct drm_property *output_width_prop;
   struct drm_property *output_height_prop;
   struct drm_property *color_key_prop;
   struct drm_property *scale_prop;
   struct drm_property *name_prop;
};
 
struct vop2_cluster {
   bool splice_mode;
   struct vop2_win *main;
   struct vop2_win *sub;
};
 
struct vop2_layer {
   uint8_t id;
   /*
    * @win_phys_id: window id of the layer selected.
    * Every layer must make sure to select different
    * windows of others.
    */
   uint8_t win_phys_id;
   const struct vop2_layer_regs *regs;
};
 
struct vop2_wb_job {
 
   bool pending;
   /**
    * @fs_vsync_cnt: frame start vysnc counter,
    * used to get the write back complete event;
    */
   uint32_t fs_vsync_cnt;
};
 
struct vop2_wb {
   uint8_t vp_id;
   struct drm_writeback_connector conn;
   const struct vop2_wb_regs *regs;
   struct vop2_wb_job jobs[VOP2_WB_JOB_MAX];
   uint8_t job_index;
 
   /**
    * @job_lock:
    *
    * spinlock to protect the job between vop2_wb_commit and vop2_wb_handler in isr.
    */
   spinlock_t job_lock;
 
};
 
struct vop2_dsc {
   uint8_t id;
   uint8_t max_slice_num;
   uint8_t max_linebuf_depth;    /* used to generate the bitstream */
   uint8_t min_bits_per_pixel;    /* bit num after encoder compress */
   bool enabled;
   char attach_vp_id;
   const struct vop2_dsc_regs *regs;
   struct vop2_power_domain *pd;
};
 
enum vop2_wb_format {
   VOP2_WB_ARGB8888,
   VOP2_WB_BGR888,
   VOP2_WB_RGB565,
   VOP2_WB_YUV420SP = 4,
   VOP2_WB_INVALID = -1,
};
 
struct vop2_wb_connector_state {
   struct drm_connector_state base;
   dma_addr_t yrgb_addr;
   dma_addr_t uv_addr;
   enum vop2_wb_format format;
   uint16_t scale_x_factor;
   uint8_t scale_x_en;
   uint8_t scale_y_en;
   uint8_t vp_id;
};
 
struct vop2_video_port {
   struct rockchip_crtc rockchip_crtc;
   struct rockchip_mcu_timing mcu_timing;
   struct vop2 *vop2;
   struct reset_control *dclk_rst;
   struct clk *dclk;
   struct clk *dclk_parent;
   uint8_t id;
   bool layer_sel_update;
   bool xmirror_en;
   bool need_reset_p2i_flag;
   atomic_t post_buf_empty_flag;
   const struct vop2_video_port_regs *regs;
 
   struct completion dsp_hold_completion;
   struct completion line_flag_completion;
 
   /* protected by dev->event_lock */
   struct drm_pending_vblank_event *event;
 
   struct drm_flip_work fb_unref_work;
   unsigned long pending;
 
   /**
    * @hdr_in: Indicate we have a hdr plane input.
    *
    */
   bool hdr_in;
   /**
    * @hdr_out: Indicate the screen want a hdr output
    * from video port.
    *
    */
   bool hdr_out;
   /*
    * @sdr2hdr_en: All the ui plane need to do sdr2hdr for a hdr_out enabled vp.
    *
    */
   bool sdr2hdr_en;
   /**
    * @skip_vsync: skip on vsync when port_mux changed on this vp.
    * a win move from one VP to another need wait one vsync until
    * port_mut take effect before this win can be enabled.
    *
    */
   bool skip_vsync;
 
   /**
    * @bg_ovl_dly: The timing delay from background layer
    * to overlay module.
    */
   u8 bg_ovl_dly;
 
   /**
    * @hdr_en: Set when has a hdr video input.
    */
   int hdr_en;
 
   /**
    * -----------------
    * |      |       |
    * | Left | Right |
    * |      |       |
    * | VP0  |  VP1  |
    * -----------------
    * @splice_mode_right: As right part of the screen in splice mode.
    */
   bool splice_mode_right;
 
   /**
    * @hdr10_at_splice_mode: enable hdr10 at splice mode on rk3588.
    */
   bool hdr10_at_splice_mode;
   /**
    * @left_vp: VP as left part of the screen in splice mode.
    */
   struct vop2_video_port *left_vp;
 
   /**
    * @win_mask: Bitmask of wins attached to the video port;
    */
   uint32_t win_mask;
   /**
    * @enabled_win_mask: Bitmask of enabled wins attached to the video port;
    */
   uint32_t enabled_win_mask;
 
   /**
    * @nr_layers: active layers attached to the video port;
    */
   uint8_t nr_layers;
 
   int cursor_win_id;
   /**
    * @output_if: output connector attached to the video port,
    * this flag is maintained in vop driver, updated in crtc_atomic_enable,
    * cleared in crtc_atomic_disable;
    */
   u32 output_if;
 
   /**
    * @active_tv_state: TV connector related states
    */
   struct drm_tv_connector_state active_tv_state;
 
   /**
    * @lut: store legacy gamma look up table
    */
   u32 *lut;
 
   /**
    * @gamma_lut_len: gamma look up table size
    */
   u32 gamma_lut_len;
 
   /**
    * @gamma_lut_active: gamma states
    */
   bool gamma_lut_active;
 
   /**
    * @lut_dma_rid: lut dma id
    */
   u16 lut_dma_rid;
 
   /**
    * @gamma_lut: atomic gamma look up table
    */
   struct drm_color_lut *gamma_lut;
 
   /**
    * @cubic_lut_len: cubic look up table size
    */
   u32 cubic_lut_len;
 
   /**
    * @cubic_lut_gem_obj: gem obj to store cubic lut
    */
   struct rockchip_gem_object *cubic_lut_gem_obj;
 
   /**
    * @hdr_lut_gem_obj: gem obj to store hdr lut
    */
   struct rockchip_gem_object *hdr_lut_gem_obj;
 
   /**
    * @cubic_lut: cubic look up table
    */
   struct drm_color_lut *cubic_lut;
 
   /**
    * @loader_protect: loader logo protect state
    */
   bool loader_protect;
 
   /**
    * @plane_mask: show the plane attach to this vp,
    * it maybe init at dts file or uboot driver
    */
   uint32_t plane_mask;
 
   /**
    * @plane_mask_prop: plane mask interaction with userspace
    */
   struct drm_property *plane_mask_prop;
   /**
    * @feature_prop: crtc feature interaction with userspace
    */
   struct drm_property *feature_prop;
 
   /**
    * @variable_refresh_rate_prop: crtc variable refresh rate interaction with userspace
    */
   struct drm_property *variable_refresh_rate_prop;
 
   /**
    * @max_refresh_rate_prop: crtc max refresh rate interaction with userspace
    */
   struct drm_property *max_refresh_rate_prop;
 
   /**
    * @min_refresh_rate_prop: crtc min refresh rate interaction with userspace
    */
   struct drm_property *min_refresh_rate_prop;
 
   /**
    * @hdr_ext_data_prop: hdr extend data interaction with userspace
    */
   struct drm_property *hdr_ext_data_prop;
 
   int hdrvivid_mode;
 
   /**
    * @acm_lut_data_prop: acm lut data interaction with userspace
    */
   struct drm_property *acm_lut_data_prop;
   /**
    * @post_csc_data_prop: post csc data interaction with userspace
    */
   struct drm_property *post_csc_data_prop;
   /**
    * @output_width_prop: vp max output width prop
    */
   struct drm_property *output_width_prop;
   /**
    * @output_dclk_prop: vp max output dclk prop
    */
   struct drm_property *output_dclk_prop;
 
   /**
    * @primary_plane_phy_id: vp primary plane phy id, the primary plane
    * will be used to show uboot logo and kernel logo
    */
   enum vop2_layer_phy_id primary_plane_phy_id;
 
   struct post_acm acm_info;
   struct post_csc csc_info;
 
   /**
    * @refresh_rate_change: indicate whether refresh rate change
    */
   bool refresh_rate_change;
};
 
struct vop2_extend_pll {
   struct list_head list;
   struct clk *clk;
   char clk_name[32];
   u32 vp_mask;
};
 
struct vop2 {
   u32 version;
   struct device *dev;
   struct drm_device *drm_dev;
   struct vop2_dsc dscs[ROCKCHIP_MAX_CRTC];
   struct vop2_video_port vps[ROCKCHIP_MAX_CRTC];
   struct vop2_wb wb;
   struct dentry *debugfs;
   struct drm_info_list *debugfs_files;
   struct drm_prop_enum_list *plane_name_list;
   bool is_iommu_enabled;
   bool is_iommu_needed;
   bool is_enabled;
   bool support_multi_area;
   bool disable_afbc_win;
 
   /* no move win from one vp to another */
   bool disable_win_move;
   /*
    * Usually we increase old fb refcount at
    * atomic_flush and decrease it when next
    * vsync come, this can make user the fb
    * not been releasced before vop finish use
    * it.
    *
    * But vop decrease fb refcount by a thread
    * vop2_unref_fb_work, which may run a little
    * slow sometimes, so when userspace do a rmfb,
    *
    * see drm_mode_rmfb,
    * it will find the fb refcount is still > 1,
    * than goto a fallback to init drm_mode_rmfb_work_fn,
    * this will cost a long time(>10 ms maybe) and block
    * rmfb work. Some userspace don't have with this(such as vo).
    *
    * Don't reference framebuffer refcount by
    * drm_framebuffer_get as some userspace want
    * rmfb as soon as possible(nvr vo). And the userspace
    * should make sure release fb after it receive the vsync.
    */
   bool skip_ref_fb;
 
   bool loader_protect;
 
   bool aclk_rate_reset;
   unsigned long aclk_rate;
 
   const struct vop2_data *data;
   /* Number of win that registered as plane,
    * maybe less than the total number of hardware
    * win.
    */
   uint32_t registered_num_wins;
   uint8_t used_mixers;
   uint8_t esmart_lb_mode;
   /**
    * @active_vp_mask: Bitmask of active video ports;
    */
   uint8_t active_vp_mask;
   uint16_t port_mux_cfg;
 
   uint32_t *regsbak;
   struct resource *res;
   void __iomem *regs;
   struct regmap *grf;
   struct regmap *sys_grf;
   struct regmap *vo0_grf;
   struct regmap *vo1_grf;
   struct regmap *sys_pmu;
 
   /* physical map length of vop2 register */
   uint32_t len;
 
   void __iomem *lut_regs;
   void __iomem *acm_regs;
   /* one time only one process allowed to config the register */
   spinlock_t reg_lock;
   /* lock vop2 irq reg */
   spinlock_t irq_lock;
   /* protects crtc enable/disable */
   struct mutex vop2_lock;
 
   int irq;
 
   /*
    * Some globle resource are shared between all
    * the vidoe ports(crtcs), so we need a ref counter here.
    */
   unsigned int enable_count;
   struct clk *hclk;
   struct clk *aclk;
   struct clk *pclk;
   struct reset_control *ahb_rst;
   struct reset_control *axi_rst;
 
   /* list_head of extend clk */
   struct list_head extend_clk_list_head;
   /* list_head of internal clk */
   struct list_head clk_list_head;
   struct list_head pd_list_head;
   struct work_struct post_buf_empty_work;
   struct workqueue_struct *workqueue;
 
   struct vop2_layer layers[ROCKCHIP_MAX_LAYER];
   /* must put at the end of the struct */
   struct vop2_win win[];
};
 
struct vop2_clk {
   struct vop2 *vop2;
   struct list_head list;
   unsigned long rate;
   struct clk_hw hw;
   struct clk_divider div;
   int div_val;
   u8 parent_index;
};
 
#define to_vop2_clk(_hw) container_of(_hw, struct vop2_clk, hw)
 
/*
 * bus-format types.
 */
struct drm_bus_format_enum_list {
   int type;
   const char *name;
};
 
static const struct drm_bus_format_enum_list drm_bus_format_enum_list[] = {
   { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
   { MEDIA_BUS_FMT_RGB565_1X16, "RGB565_1X16" },
   { MEDIA_BUS_FMT_RGB666_1X18, "RGB666_1X18" },
   { MEDIA_BUS_FMT_RGB666_1X24_CPADHI, "RGB666_1X24_CPADHI" },
   { MEDIA_BUS_FMT_RGB666_1X7X3_SPWG, "RGB666_1X7X3_SPWG" },
   { MEDIA_BUS_FMT_YUV8_1X24, "YUV8_1X24" },
   { MEDIA_BUS_FMT_UYYVYY8_0_5X24, "UYYVYY8_0_5X24" },
   { MEDIA_BUS_FMT_YUV10_1X30, "YUV10_1X30" },
   { MEDIA_BUS_FMT_UYYVYY10_0_5X30, "UYYVYY10_0_5X30" },
   { MEDIA_BUS_FMT_RGB888_3X8, "RGB888_3X8" },
   { MEDIA_BUS_FMT_RGB888_DUMMY_4X8, "RGB888_DUMMY_4X8" },
   { MEDIA_BUS_FMT_RGB888_1X24, "RGB888_1X24" },
   { MEDIA_BUS_FMT_RGB888_1X7X4_SPWG, "RGB888_1X7X4_SPWG" },
   { MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA, "RGB888_1X7X4_JEIDA" },
   { MEDIA_BUS_FMT_UYVY8_2X8, "UYVY8_2X8" },
   { MEDIA_BUS_FMT_YUYV8_1X16, "YUYV8_1X16" },
   { MEDIA_BUS_FMT_UYVY8_1X16, "UYVY8_1X16" },
   { MEDIA_BUS_FMT_RGB101010_1X30, "RGB101010_1X30" },
   { MEDIA_BUS_FMT_YUYV10_1X20, "YUYV10_1X20" },
};
 
static DRM_ENUM_NAME_FN(drm_get_bus_format_name, drm_bus_format_enum_list)
 
static inline struct vop2_video_port *to_vop2_video_port(struct drm_crtc *crtc)
{
   struct rockchip_crtc *rockchip_crtc;
 
   rockchip_crtc = container_of(crtc, struct rockchip_crtc, crtc);
 
   return container_of(rockchip_crtc, struct vop2_video_port, rockchip_crtc);
}
 
static void vop2_lock(struct vop2 *vop2)
{
   mutex_lock(&vop2->vop2_lock);
   rockchip_dmcfreq_lock();
}
 
static void vop2_unlock(struct vop2 *vop2)
{
   rockchip_dmcfreq_unlock();
   mutex_unlock(&vop2->vop2_lock);
}
 
static inline void vop2_grf_writel(struct regmap *regmap, struct vop_reg reg, u32 v)
{
   u32 val = 0;
 
   if (IS_ERR_OR_NULL(regmap))
       return;
 
   if (reg.mask) {
       val = (v << reg.shift) | (reg.mask << (reg.shift + 16));
       regmap_write(regmap, reg.offset, val);
   }
}
 
static inline uint32_t vop2_grf_readl(struct regmap *regmap, const struct vop_reg *reg)
{
   uint32_t v;
 
   regmap_read(regmap, reg->offset, &v);
 
   return v;
}
 
static inline void vop2_writel(struct vop2 *vop2, uint32_t offset, uint32_t v)
{
   writel(v, vop2->regs + offset);
   vop2->regsbak[offset >> 2] = v;
}
 
static inline uint32_t vop2_readl(struct vop2 *vop2, uint32_t offset)
{
   return readl(vop2->regs + offset);
}
 
static inline uint32_t vop2_read_reg(struct vop2 *vop2, uint32_t base,
                    const struct vop_reg *reg)
{
   return (vop2_readl(vop2, base + reg->offset) >> reg->shift) & reg->mask;
}
 
static inline uint32_t vop2_read_reg_bak(struct vop2 *vop2, uint32_t base,
                    const struct vop_reg *reg)
{
   return (vop2->regsbak[(base + reg->offset) >> 2] >> reg->shift) & reg->mask;
}
 
static inline uint32_t vop2_read_grf_reg(struct regmap *regmap, const struct vop_reg *reg)
{
   return (vop2_grf_readl(regmap, reg) >> reg->shift) & reg->mask;
}
 
static inline void vop2_write_reg_uncached(struct vop2 *vop2, const struct vop_reg *reg, uint32_t v)
{
   uint32_t offset = reg->offset;
   uint32_t cached_val = vop2->regsbak[offset >> 2];
 
   v = (cached_val & ~(reg->mask << reg->shift)) | ((v & reg->mask) << reg->shift);
   writel(v, vop2->regs + offset);
}
 
static inline void vop2_mask_write(struct vop2 *vop2, uint32_t offset,
                  uint32_t mask, uint32_t shift, uint32_t v,
                  bool write_mask, bool relaxed)
{
   uint32_t cached_val;
 
   if (!mask)
       return;
 
   if (write_mask) {
       v = ((v & mask) << shift) | (mask << (shift + 16));
   } else {
       cached_val = vop2->regsbak[offset >> 2];
 
       v = (cached_val & ~(mask << shift)) | ((v & mask) << shift);
       vop2->regsbak[offset >> 2] = v;
   }
 
   if (relaxed)
       writel_relaxed(v, vop2->regs + offset);
   else
       writel(v, vop2->regs + offset);
}
 
static inline u32 vop2_line_to_time(struct drm_display_mode *mode, int line)
{
   u64 val = 1000000000ULL * mode->crtc_htotal * line;
 
   do_div(val, mode->crtc_clock);
   do_div(val, 1000000);
 
   return val; /* us */
}
 
static inline bool vop2_plane_active(struct drm_plane_state *pstate)
{
   if (!pstate || !pstate->fb)
       return false;
   else
       return true;
}
 
static inline bool is_vop3(struct vop2 *vop2)
{
   if (vop2->version == VOP_VERSION_RK3568 || vop2->version == VOP_VERSION_RK3588)
       return false;
   else
       return true;
}
 
static bool vop2_soc_is_rk3566(void)
{
   return soc_is_rk3566();
}
 
static bool vop2_is_mirror_win(struct vop2_win *win)
{
   return soc_is_rk3566() && (win->feature & WIN_FEATURE_MIRROR);
}
 
static uint64_t vop2_soc_id_fixup(uint64_t soc_id)
{
   switch (soc_id) {
   case 0x3566:
       if (rockchip_get_cpu_version())
           return 0x3566A;
       else
           return 0x3566;
   case 0x3568:
       if (rockchip_get_cpu_version())
           return 0x3568A;
       else
           return 0x3568;
   default:
       return soc_id;
   }
}
 
static void vop2_crtc_standby(struct drm_crtc *crtc, bool standby)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
 
   if (standby) {
       VOP_MODULE_SET(vop2, vp, standby, 1);
       mdelay(20);
   } else {
       VOP_MODULE_SET(vop2, vp, standby, 0);
   }
}
 
static inline const struct vop2_win_regs *vop2_get_win_regs(struct vop2_win *win,
                               const struct vop_reg *reg)
{
   if (!reg->mask && win->parent)
       return win->parent->regs;
 
   return win->regs;
}
 
static inline uint32_t vop2_get_intr_type(struct vop2 *vop2, const struct vop_intr *intr,
                     const struct vop_reg *reg, int type)
{
   uint32_t val, i;
   uint32_t ret = 0;
 
   val = vop2_read_reg(vop2, 0, reg);
 
   for (i = 0; i < intr->nintrs; i++) {
       if ((type & intr->intrs[i]) && (val & 1 << i))
           ret |= intr->intrs[i];
   }
 
   return ret;
}
 
/*
 * phys_id is used to identify a main window(Cluster Win/Smart Win, not
 * include the sub win of a cluster or the multi area) that can do
 * overlay in main overlay stage.
 */
static struct vop2_win *vop2_find_win_by_phys_id(struct vop2 *vop2, uint8_t phys_id)
{
   struct vop2_win *win;
   int i;
 
   for (i = 0; i < vop2->registered_num_wins; i++) {
       win = &vop2->win[i];
       if (win->phys_id == phys_id)
           return win;
   }
 
   return NULL;
}
 
static struct vop2_power_domain *vop2_find_pd_by_id(struct vop2 *vop2, uint8_t id)
{
   struct vop2_power_domain *pd, *n;
 
   list_for_each_entry_safe(pd, n, &vop2->pd_list_head, list) {
       if (pd->data->id == id)
           return pd;
   }
 
   return NULL;
}
 
static const struct vop2_connector_if_data *vop2_find_connector_if_data(struct vop2 *vop2, int id)
{
   const struct vop2_connector_if_data *if_data;
   int i;
 
   for (i = 0; i < vop2->data->nr_conns; i++) {
       if_data = &vop2->data->conn[i];
       if (if_data->id == id)
           return if_data;
   }
 
   return NULL;
}
 
static struct drm_crtc *vop2_find_crtc_by_plane_mask(struct vop2 *vop2, uint8_t phys_id)
{
   struct vop2_video_port *vp;
   int i;
 
   for (i = 0; i < vop2->data->nr_vps; i++) {
       vp = &vop2->vps[i];
       if (vp->plane_mask & BIT(phys_id))
           return &vp->rockchip_crtc.crtc;
   }
 
   return NULL;
}
 
static int vop2_clk_reset(struct reset_control *rstc)
{
   int ret;
 
   if (!rstc)
       return 0;
 
   ret = reset_control_assert(rstc);
   if (ret < 0)
       DRM_WARN("failed to assert reset\n");
   udelay(10);
   ret = reset_control_deassert(rstc);
   if (ret < 0)
       DRM_WARN("failed to deassert reset\n");
 
   return ret;
}
 
static void vop2_load_hdr2sdr_table(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_hdr_table *table = vp_data->hdr_table;
   const struct vop2_video_port_regs *regs = vp->regs;
   uint32_t hdr2sdr_eetf_oetf_yn[33];
   int i;
 
   for (i = 0; i < 33; i++)
       hdr2sdr_eetf_oetf_yn[i] = table->hdr2sdr_eetf_yn[i] +
               (table->hdr2sdr_bt1886oetf_yn[i] << 16);
 
   for (i = 0; i < 33; i++)
       vop2_writel(vop2, regs->hdr2sdr_eetf_oetf_y0_offset + i * 4,
               hdr2sdr_eetf_oetf_yn[i]);
 
   for (i = 0; i < 9; i++)
       vop2_writel(vop2, regs->hdr2sdr_sat_y0_offset + i * 4,
               table->hdr2sdr_sat_yn[i]);
}
 
static void vop2_load_sdr2hdr_table(struct vop2_video_port *vp, int sdr2hdr_tf)
{
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_hdr_table *table = vp_data->hdr_table;
   const struct vop2_video_port_regs *regs = vp->regs;
   uint32_t sdr2hdr_eotf_oetf_yn[65];
   uint32_t sdr2hdr_oetf_dx_dxpow[64];
   int i;
 
   for (i = 0; i < 65; i++) {
       if (sdr2hdr_tf == SDR2HDR_FOR_BT2020)
           sdr2hdr_eotf_oetf_yn[i] =
               table->sdr2hdr_bt1886eotf_yn_for_bt2020[i] +
               (table->sdr2hdr_st2084oetf_yn_for_bt2020[i] << 18);
       else if (sdr2hdr_tf == SDR2HDR_FOR_HDR)
           sdr2hdr_eotf_oetf_yn[i] =
               table->sdr2hdr_bt1886eotf_yn_for_hdr[i] +
               (table->sdr2hdr_st2084oetf_yn_for_hdr[i] << 18);
       else if (sdr2hdr_tf == SDR2HDR_FOR_HLG_HDR)
           sdr2hdr_eotf_oetf_yn[i] =
               table->sdr2hdr_bt1886eotf_yn_for_hlg_hdr[i] +
               (table->sdr2hdr_st2084oetf_yn_for_hlg_hdr[i] << 18);
   }
 
   for (i = 0; i < 65; i++)
       vop2_writel(vop2, regs->sdr2hdr_eotf_oetf_y0_offset + i * 4,
               sdr2hdr_eotf_oetf_yn[i]);
 
   for (i = 0; i < 64; i++) {
       sdr2hdr_oetf_dx_dxpow[i] = table->sdr2hdr_st2084oetf_dxn[i] +
               (table->sdr2hdr_st2084oetf_dxn_pow2[i] << 16);
       vop2_writel(vop2, regs->sdr2hdr_oetf_dx_pow1_offset + i * 4,
               sdr2hdr_oetf_dx_dxpow[i]);
   }
 
   for (i = 0; i < 63; i++)
       vop2_writel(vop2, regs->sdr2hdr_oetf_xn1_offset + i * 4,
               table->sdr2hdr_st2084oetf_xn[i]);
}
 
static bool vop2_fs_irq_is_pending(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
 
   return VOP_INTR_GET_TYPE(vop2, intr, status, FS_FIELD_INTR);
}
 
static uint32_t vop2_read_vcnt(struct vop2_video_port *vp)
{
   uint32_t offset =  RK3568_SYS_STATUS0 + (vp->id << 2);
   uint32_t vcnt0, vcnt1;
   int i = 0;
 
   for (i = 0; i < 10; i++) {
       vcnt0 = vop2_readl(vp->vop2, offset) >> 16;
       vcnt1 = vop2_readl(vp->vop2, offset) >> 16;
 
       if ((vcnt1 - vcnt0) <= 1)
           break;
   }
 
   if (i == 10) {
       DRM_DEV_ERROR(vp->vop2->dev, "read VP%d vcnt error: %d %d\n", vp->id, vcnt0, vcnt1);
       vcnt1 = vop2_readl(vp->vop2, offset) >> 16;
   }
 
   return vcnt1;
}
 
static void vop2_wait_for_irq_handler(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   bool pending;
   int ret;
 
   /*
    * Spin until frame start interrupt status bit goes low, which means
    * that interrupt handler was invoked and cleared it. The timeout of
    * 10 msecs is really too long, but it is just a safety measure if
    * something goes really wrong. The wait will only happen in the very
    * unlikely case of a vblank happening exactly at the same time and
    * shouldn't exceed microseconds range.
    */
   ret = readx_poll_timeout_atomic(vop2_fs_irq_is_pending, vp, pending,
                   !pending, 0, 10 * 1000);
   if (ret)
       DRM_DEV_ERROR(vop2->dev, "VOP vblank IRQ stuck for 10 ms\n");
 
   synchronize_irq(vop2->irq);
}
 
static bool vop2_vp_done_bit_status(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   u32 done_bits = vop2_readl(vop2, RK3568_REG_CFG_DONE) & BIT(vp->id);
 
   /*
    * When done bit is 0, indicate current frame is take effect.
    */
   return done_bits == 0 ? true : false;
}
 
static void vop2_wait_for_fs_by_done_bit_status(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   bool done_bit;
   int ret;
 
   ret = readx_poll_timeout_atomic(vop2_vp_done_bit_status, vp, done_bit,
                   done_bit, 0, 50 * 1000);
   if (ret)
       DRM_DEV_ERROR(vop2->dev, "wait vp%d done bit status timeout, vcnt: %d\n",
                 vp->id, vop2_read_vcnt(vp));
}
 
static uint16_t vop2_read_port_mux(struct vop2 *vop2)
{
   return vop2_readl(vop2, RK3568_OVL_PORT_SEL) & 0xffff;
}
 
static void vop2_wait_for_port_mux_done(struct vop2 *vop2)
{
   uint16_t port_mux_cfg;
   int ret;
 
   /*
    * Spin until the previous port_mux figuration
    * is done.
    */
   ret = readx_poll_timeout_atomic(vop2_read_port_mux, vop2, port_mux_cfg,
                   port_mux_cfg == vop2->port_mux_cfg, 0, 50 * 1000);
   if (ret)
       DRM_DEV_ERROR(vop2->dev, "wait port_mux done timeout: 0x%x--0x%x\n",
                 port_mux_cfg, vop2->port_mux_cfg);
}
 
static u32 vop2_read_layer_cfg(struct vop2 *vop2)
{
   return vop2_readl(vop2, RK3568_OVL_LAYER_SEL);
}
 
static void vop2_wait_for_layer_cfg_done(struct vop2 *vop2, u32 cfg)
{
   u32 atv_layer_cfg;
   int ret;
 
   /*
    * Spin until the previous layer configuration is done.
    */
   ret = readx_poll_timeout_atomic(vop2_read_layer_cfg, vop2, atv_layer_cfg,
                   atv_layer_cfg == cfg, 0, 50 * 1000);
   if (ret)
       DRM_DEV_ERROR(vop2->dev, "wait layer cfg done timeout: 0x%x--0x%x\n",
                 atv_layer_cfg, cfg);
}
 
static int32_t vop2_pending_done_bits(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   struct drm_display_mode *adjusted_mode;
   struct vop2_video_port *done_vp;
   uint32_t done_bits, done_bits_bak;
   uint32_t vp_id;
   uint32_t vcnt;
 
   done_bits = vop2_readl(vop2, RK3568_REG_CFG_DONE) & 0x7;
   done_bits_bak = done_bits;
 
   /* no done bit, so no need to wait config done take effect */
   if (done_bits == 0)
       return 0;
 
   vp_id = ffs(done_bits) - 1;
   /* done bit is same with current vp config done, so no need to wait */
   if (hweight32(done_bits) == 1 && vp_id == vp->id)
       return 0;
 
   /* have the other one different vp, wait for config done take effect */
   if (hweight32(done_bits) == 1 ||
       (hweight32(done_bits) == 2 && (done_bits & BIT(vp->id)))) {
       /* two done bit, clear current vp done bit and find the other done bit vp */
       if (done_bits & BIT(vp->id))
           done_bits &= ~BIT(vp->id);
       vp_id = ffs(done_bits) - 1;
       done_vp = &vop2->vps[vp_id];
       adjusted_mode = &done_vp->rockchip_crtc.crtc.state->adjusted_mode;
       vcnt = vop2_read_vcnt(done_vp);
       if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
           vcnt >>= 1;
       /* if close to the last 1/8 frame, wait to next frame */
       if (vcnt > (adjusted_mode->crtc_vtotal * 7 >> 3)) {
           vop2_wait_for_fs_by_done_bit_status(done_vp);
           done_bits = 0;
       }
   } else { /* exist the other two vp done bit */
       struct drm_display_mode *first_mode, *second_mode;
       struct vop2_video_port *first_done_vp, *second_done_vp, *wait_vp;
       uint32_t first_vp_id, second_vp_id;
       uint32_t first_vp_vcnt, second_vp_vcnt;
       uint32_t first_vp_left_vcnt, second_vp_left_vcnt;
       uint32_t first_vp_left_time, second_vp_left_time;
       uint32_t first_vp_safe_time, second_vp_safe_time;
       unsigned int vrefresh;
 
       first_vp_id = ffs(done_bits) - 1;
       first_done_vp = &vop2->vps[first_vp_id];
       first_mode = &first_done_vp->rockchip_crtc.crtc.state->adjusted_mode;
       /* set last 1/8 frame time as safe section */
       vrefresh = drm_mode_vrefresh(first_mode);
       if (!vrefresh) {
           WARN(1, "%s first vp:%d vrefresh is zero\n", __func__, first_vp_id);
           vrefresh = 60;
       }
       first_vp_safe_time = (1000000 / vrefresh) >> 3;
 
       done_bits &= ~BIT(first_vp_id);
       second_vp_id = ffs(done_bits) - 1;
       second_done_vp = &vop2->vps[second_vp_id];
       second_mode = &second_done_vp->rockchip_crtc.crtc.state->adjusted_mode;
       /* set last 1/8 frame time as safe section */
       vrefresh = drm_mode_vrefresh(second_mode);
       if (!vrefresh) {
           WARN(1, "%s second vp:%d vrefresh is zero\n", __func__, second_vp_id);
           vrefresh = 60;
       }
       second_vp_safe_time = (1000000 / vrefresh) >> 3;
 
       first_vp_vcnt = vop2_read_vcnt(first_done_vp);
       if (first_mode->flags & DRM_MODE_FLAG_INTERLACE)
           first_vp_vcnt >>= 1;
       second_vp_vcnt = vop2_read_vcnt(second_done_vp);
       if (second_mode->flags & DRM_MODE_FLAG_INTERLACE)
           second_vp_vcnt >>= 1;
 
       first_vp_left_vcnt = first_mode->crtc_vtotal - first_vp_vcnt;
       second_vp_left_vcnt = second_mode->crtc_vtotal - second_vp_vcnt;
       first_vp_left_time = vop2_line_to_time(first_mode, first_vp_left_vcnt);
       second_vp_left_time = vop2_line_to_time(second_mode, second_vp_left_vcnt);
 
       /* if the two vp both at safe section, no need to wait */
       if (first_vp_left_time > first_vp_safe_time &&
           second_vp_left_time > second_vp_safe_time)
           return done_bits_bak;
 
       if (first_vp_left_time > second_vp_left_time) {
           if ((first_vp_left_time - second_vp_left_time) > first_vp_safe_time)
               wait_vp = second_done_vp;
           else
               wait_vp = first_done_vp;
       } else {
           if ((second_vp_left_time - first_vp_left_time) > second_vp_safe_time)
               wait_vp = first_done_vp;
           else
               wait_vp = second_done_vp;
       }
 
       vop2_wait_for_fs_by_done_bit_status(wait_vp);
 
       done_bits = vop2_readl(vop2, RK3568_REG_CFG_DONE) & 0x7;
   }
   return done_bits;
}
 
static inline void rk3588_vop2_dsc_cfg_done(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2_dsc *dsc = &vop2->dscs[vcstate->dsc_id];
 
   if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE) {
       dsc = &vop2->dscs[0];
       if (vcstate->dsc_enable)
           VOP_MODULE_SET(vop2, dsc, dsc_cfg_done, 1);
       dsc = &vop2->dscs[1];
       if (vcstate->dsc_enable)
           VOP_MODULE_SET(vop2, dsc, dsc_cfg_done, 1);
   } else {
       if (vcstate->dsc_enable)
           VOP_MODULE_SET(vop2, dsc, dsc_cfg_done, 1);
   }
}
 
static inline void rk3568_vop2_cfg_done(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   uint32_t done_bits;
   uint32_t val;
   u32 old_layer_sel_val, cfg_layer_sel_val;
   struct vop2_layer *layer = &vop2->layers[0];
   u32 layer_sel_offset = layer->regs->layer_sel.offset;
 
   /*
    * This is a workaround, the config done bits of VP0,
    * VP1, VP2 on RK3568 stands on the first three bits
    * on REG_CFG_DONE register without mask bit.
    * If two or three config done events happens one after
    * another in a very shot time, the flowing config done
    * write may override the previous config done bit before
    * it take effect:
    * 1: config done 0x8001 for VP0
    * 2: config done 0x8002 for VP1
    *
    * 0x8002 may override 0x8001 before it take effect.
    *
    * So we do a read | write here.
    *
    */
   done_bits = vop2_pending_done_bits(vp);
   val = RK3568_VOP2_GLB_CFG_DONE_EN | BIT(vp->id) | done_bits;
   old_layer_sel_val = vop2_readl(vop2, layer_sel_offset);
   cfg_layer_sel_val = vop2->regsbak[layer_sel_offset >> 2];
   /**
    * This is rather low probability for miss some done bit.
    */
   val |= vop2_readl(vop2, RK3568_REG_CFG_DONE) & 0x7;
 
   rockchip_drm_dbg(vop2->dev, VOP_DEBUG_CFG_DONE, "cfg_done: 0x%x\n", val);
 
   vop2_writel(vop2, 0, val);
 
   /**
    * Make sure the layer sel is take effect when it's updated.
    */
   if (old_layer_sel_val != cfg_layer_sel_val) {
       vp->layer_sel_update = true;
       vop2_wait_for_fs_by_done_bit_status(vp);
       DRM_DEV_DEBUG(vop2->dev, "vp%d need to wait fs as old layer_sel val[0x%x] != new val[0x%x]\n",
                 vp->id, old_layer_sel_val, cfg_layer_sel_val);
   }
}
 
static inline void rk3588_vop2_cfg_done(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   const struct vop2_video_port_data *vp_data = &vp->vop2->data->vp[vp->id];
   struct vop2 *vop2 = vp->vop2;
   uint32_t val;
 
   val = RK3568_VOP2_GLB_CFG_DONE_EN | BIT(vp->id) | (BIT(vp->id) << 16);
   if (vcstate->splice_mode)
       val |= BIT(vp_data->splice_vp_id) | (BIT(vp_data->splice_vp_id) << 16);
 
   rockchip_drm_dbg(vop2->dev, VOP_DEBUG_CFG_DONE, "cfg_done: 0x%x\n", val);
 
   vop2_writel(vop2, 0, val);
}
 
static inline void vop2_wb_cfg_done(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   uint32_t val = RK3568_VOP2_WB_CFG_DONE | (RK3568_VOP2_WB_CFG_DONE << 16) |
              RK3568_VOP2_GLB_CFG_DONE_EN;
   uint32_t done_bits;
   unsigned long flags;
 
   if (vop2->version == VOP_VERSION_RK3568) {
       spin_lock_irqsave(&vop2->irq_lock, flags);
       done_bits = vop2_pending_done_bits(vp);
       val |= done_bits;
       vop2_writel(vop2, 0, val);
       spin_unlock_irqrestore(&vop2->irq_lock, flags);
   } else {
       vop2_writel(vop2, 0, val);
   }
 
}
 
static inline void vop2_cfg_done(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
 
   if (vop2->version == VOP_VERSION_RK3568)
       return rk3568_vop2_cfg_done(crtc);
   else
       return rk3588_vop2_cfg_done(crtc);
}
 
/*
 * A PD can power off by vsync when it's module attached to
 * a activated VP.
 */
static uint32_t vop2_power_domain_can_off_by_vsync(struct vop2_power_domain *pd)
{
   struct vop2 *vop2 = pd->vop2;
 
   if (vop2->active_vp_mask & pd->vp_mask)
       return true;
   else
       return false;
}
 
/*
 * Read VOP internal power domain on/off status.
 * We should query BISR_STS register in PMU for
 * power up/down status when memory repair is enabled.
 * Return value: 1 for power on, 0 for power off;
 */
static uint32_t vop2_power_domain_status(struct vop2_power_domain *pd)
{
   struct vop2 *vop2 = pd->vop2;
 
   if (vop2_read_grf_reg(vop2->sys_pmu, &pd->data->regs->bisr_en_status))
       return vop2_read_grf_reg(vop2->sys_pmu, &pd->data->regs->pmu_status);
   else
       return vop2_read_reg(vop2, 0, &pd->data->regs->status) ? 0 : 1;
}
 
static void vop2_wait_power_domain_off(struct vop2_power_domain *pd)
{
   struct vop2 *vop2 = pd->vop2;
   int val;
   int ret;
 
   ret = readx_poll_timeout_atomic(vop2_power_domain_status, pd, val, !val, 0, 50 * 1000);
 
   if (ret)
       DRM_DEV_ERROR(vop2->dev, "wait pd%d off timeout power_ctrl: 0x%x\n",
                 ffs(pd->data->id) - 1, vop2_readl(vop2, 0x34));
}
 
static void vop2_wait_power_domain_on(struct vop2_power_domain *pd)
{
   struct vop2 *vop2 = pd->vop2;
   int val;
   int ret;
 
   ret = readx_poll_timeout_atomic(vop2_power_domain_status, pd, val, val, 0, 50 * 1000);
   if (ret)
       DRM_DEV_ERROR(vop2->dev, "wait pd%d on timeout power_ctrl: 0x%x\n",
                 ffs(pd->data->id) - 1, vop2_readl(vop2, 0x34));
}
 
/*
 * Power domain on take effect immediately
 */
static void vop2_power_domain_on(struct vop2_power_domain *pd)
{
   struct vop2 *vop2 = pd->vop2;
 
   if (!pd->on) {
       dev_dbg(vop2->dev, "pd%d on\n", ffs(pd->data->id) - 1);
       vop2_wait_power_domain_off(pd);
       VOP_MODULE_SET(vop2, pd->data, pd, 0);
       vop2_wait_power_domain_on(pd);
       pd->on = true;
   }
}
 
/*
 * Power domain off take effect by vsync.
 */
static void vop2_power_domain_off(struct vop2_power_domain *pd)
{
   struct vop2 *vop2 = pd->vop2;
 
   dev_dbg(vop2->dev, "pd%d off\n", ffs(pd->data->id) - 1);
   pd->on = false;
   VOP_MODULE_SET(vop2, pd->data, pd, 1);
}
 
static void vop2_power_domain_get(struct vop2_power_domain *pd)
{
   if (pd->parent)
       vop2_power_domain_get(pd->parent);
 
   spin_lock(&pd->lock);
   if (pd->ref_count == 0) {
       if (pd->vop2->data->delayed_pd)
           cancel_delayed_work(&pd->power_off_work);
       vop2_power_domain_on(pd);
   }
   pd->ref_count++;
   spin_unlock(&pd->lock);
}
 
static void vop2_power_domain_put(struct vop2_power_domain *pd)
{
   spin_lock(&pd->lock);
 
   /*
    * For a nested power domain(PD_Cluster0 is the parent of PD_CLuster1/2/3)
    * the parent power domain must be enabled before child power domain
    * is on.
    *
    * So we may met this condition: Cluster0 is not on a activated VP,
    * but PD_Cluster0 must enabled as one of the child PD_CLUSTER1/2/3 is enabled.
    * when all child PD is disabled, we want disable the parent
    * PD(PD_CLUSTER0), but as module CLUSTER0 is not attcthed on a activated VP,
    * the turn off operation(which is take effect by vsync) will never take effect.
    * so we will see a "wait pd0 off timeout" log when we turn on PD_CLUSTER0 next time.
    *
    * So we have a check here
    */
   if (--pd->ref_count == 0 && vop2_power_domain_can_off_by_vsync(pd)) {
       if (pd->vop2->data->delayed_pd)
           schedule_delayed_work(&pd->power_off_work, msecs_to_jiffies(2500));
       else
           vop2_power_domain_off(pd);
   }
 
   spin_unlock(&pd->lock);
   if (pd->parent)
       vop2_power_domain_put(pd->parent);
}
 
/*
 * Called if the pd ref_count reach 0 after 2.5
 * seconds.
 */
static void vop2_power_domain_off_work(struct work_struct *work)
{
   struct vop2_power_domain *pd;
 
   pd = container_of(to_delayed_work(work), struct vop2_power_domain, power_off_work);
 
   spin_lock(&pd->lock);
   if (pd->ref_count == 0)
       vop2_power_domain_off(pd);
   spin_unlock(&pd->lock);
}
 
static void vop2_win_enable(struct vop2_win *win)
{
   /*
    * a win such as cursor update by async:
    * first frame enable win pd, enable win, return without wait vsync
    * second frame come, but the first frame may still not enabled
    * in this case, the win pd is turn on by fist frame, so we don't
    * need get pd again.
    *
    * another case:
    * first frame: disable win, disable pd, return without wait vsync
    * second frame come very soon, the previous win disable may still not
    * take effect, but the pd is disable in progress, we should do pd_get
    * at this situation.
    *
    * check the backup register for previous enable operation.
    */
   if (!VOP_WIN_GET_REG_BAK(win->vop2, win, enable)) {
       if (win->pd) {
           if (win->pd->data->id == VOP2_PD_ESMART)
               return;
 
           vop2_power_domain_get(win->pd);
           win->pd->vp_mask |= win->vp_mask;
       }
   }
}
 
static void vop2_win_multi_area_disable(struct vop2_win *parent)
{
   struct vop2 *vop2 = parent->vop2;
   struct vop2_win *area;
   int i;
 
   for (i = 0; i < vop2->registered_num_wins; i++) {
       area = &vop2->win[i];
       if (area->parent == parent)
           VOP_WIN_SET(vop2, area, enable, 0);
   }
}
 
static void vop2_win_disable(struct vop2_win *win, bool skip_splice_win)
{
   struct vop2 *vop2 = win->vop2;
 
   /* Disable the right splice win */
   if (win->splice_win && !skip_splice_win) {
       vop2_win_disable(win->splice_win, false);
       win->splice_win = NULL;
   }
 
   if (VOP_WIN_GET(vop2, win, enable) || VOP_WIN_GET_REG_BAK(vop2, win, enable)) {
       VOP_WIN_SET(vop2, win, enable, 0);
       if (win->feature & WIN_FEATURE_CLUSTER_MAIN) {
           struct vop2_win *sub_win;
           int i = 0;
 
           for (i = 0; i < vop2->registered_num_wins; i++) {
               sub_win = &vop2->win[i];
 
               if ((sub_win->phys_id == win->phys_id) &&
                   (sub_win->feature & WIN_FEATURE_CLUSTER_SUB))
                   VOP_WIN_SET(vop2, sub_win, enable, 0);
           }
 
           VOP_CLUSTER_SET(vop2, win, enable, 0);
       }
 
       /*
        * disable all other multi area win if we want disable area0 here
        */
       if (!win->parent && (win->feature & WIN_FEATURE_MULTI_AREA))
           vop2_win_multi_area_disable(win);
 
       if (win->pd) {
 
           /*
            * Don't dynamic turn on/off PD_ESMART.
            * (1) There is a design issue for PD_EMSART when attached
            *     on VP1/2/3, we found it will trigger POST_BUF_EMPTY irq at vp0
            *     in splice mode.
            * (2) PD_ESMART will be closed at esmart layers attathed on VPs
            *     config done + FS, but different VP FS time is different, this
            *     maybe lead to PD_ESMART closed at wrong time and display error.
            * (3) PD_ESMART power up maybe have 4 us delay, this will lead to POST_BUF_EMPTY.
            */
           if (win->pd->data->id == VOP2_PD_ESMART)
               return;
 
           vop2_power_domain_put(win->pd);
           win->pd->vp_mask &= ~win->vp_mask;
       }
   }
 
   if (win->left_win && win->splice_mode_right) {
       win->left_win = NULL;
       win->splice_mode_right = false;
   }
}
 
static inline void vop2_write_lut(struct vop2 *vop2, uint32_t offset, uint32_t v)
{
   writel(v, vop2->lut_regs + offset);
}
 
static inline uint32_t vop2_read_lut(struct vop2 *vop2, uint32_t offset)
{
   return readl(vop2->lut_regs + offset);
}
 
static bool is_linear_10bit_yuv(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_NV15:
   case DRM_FORMAT_NV20:
   case DRM_FORMAT_NV30:
       return true;
   default:
       return false;
   }
}
 
static enum vop2_data_format vop2_convert_format(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_XRGB2101010:
   case DRM_FORMAT_ARGB2101010:
   case DRM_FORMAT_XBGR2101010:
   case DRM_FORMAT_ABGR2101010:
       return VOP2_FMT_XRGB101010;
   case DRM_FORMAT_XRGB8888:
   case DRM_FORMAT_ARGB8888:
   case DRM_FORMAT_XBGR8888:
   case DRM_FORMAT_ABGR8888:
       return VOP2_FMT_ARGB8888;
   case DRM_FORMAT_RGB888:
   case DRM_FORMAT_BGR888:
       return VOP2_FMT_RGB888;
   case DRM_FORMAT_RGB565:
   case DRM_FORMAT_BGR565:
       return VOP2_FMT_RGB565;
   case DRM_FORMAT_NV12:
   case DRM_FORMAT_NV21:
   case DRM_FORMAT_YUV420_8BIT:
       return VOP2_FMT_YUV420SP;
   case DRM_FORMAT_NV15:
   case DRM_FORMAT_YUV420_10BIT:
       return VOP2_FMT_YUV420SP_10;
   case DRM_FORMAT_NV16:
   case DRM_FORMAT_NV61:
       return VOP2_FMT_YUV422SP;
   case DRM_FORMAT_NV20:
   case DRM_FORMAT_Y210:
       return VOP2_FMT_YUV422SP_10;
   case DRM_FORMAT_NV24:
   case DRM_FORMAT_NV42:
       return VOP2_FMT_YUV444SP;
   case DRM_FORMAT_NV30:
       return VOP2_FMT_YUV444SP_10;
   case DRM_FORMAT_YUYV:
   case DRM_FORMAT_YVYU:
       return VOP2_FMT_VYUY422;
   case DRM_FORMAT_VYUY:
   case DRM_FORMAT_UYVY:
       return VOP2_FMT_YUYV422;
   default:
       DRM_ERROR("unsupported format[%08x]\n", format);
       return -EINVAL;
   }
}
 
static enum vop2_afbc_format vop2_convert_afbc_format(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_XRGB2101010:
   case DRM_FORMAT_ARGB2101010:
   case DRM_FORMAT_XBGR2101010:
   case DRM_FORMAT_ABGR2101010:
       return VOP2_AFBC_FMT_ARGB2101010;
   case DRM_FORMAT_XRGB8888:
   case DRM_FORMAT_ARGB8888:
   case DRM_FORMAT_XBGR8888:
   case DRM_FORMAT_ABGR8888:
       return VOP2_AFBC_FMT_ARGB8888;
   case DRM_FORMAT_RGB888:
   case DRM_FORMAT_BGR888:
       return VOP2_AFBC_FMT_RGB888;
   case DRM_FORMAT_RGB565:
   case DRM_FORMAT_BGR565:
       return VOP2_AFBC_FMT_RGB565;
   case DRM_FORMAT_YUV420_8BIT:
       return VOP2_AFBC_FMT_YUV420;
   case DRM_FORMAT_YUV420_10BIT:
       return VOP2_AFBC_FMT_YUV420_10BIT;
   case DRM_FORMAT_YVYU:
   case DRM_FORMAT_YUYV:
   case DRM_FORMAT_VYUY:
   case DRM_FORMAT_UYVY:
       return VOP2_AFBC_FMT_YUV422;
   case DRM_FORMAT_Y210:
       return VOP2_AFBC_FMT_YUV422_10BIT;
 
       /* either of the below should not be reachable */
   default:
       DRM_WARN_ONCE("unsupported AFBC format[%08x]\n", format);
       return VOP2_AFBC_FMT_INVALID;
   }
 
   return VOP2_AFBC_FMT_INVALID;
}
 
static enum vop2_tiled_format vop2_convert_tiled_format(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_NV12:
   case DRM_FORMAT_NV21:
       return VOP2_TILED_8X8_FMT_YUV420SP;
   case DRM_FORMAT_NV16:
   case DRM_FORMAT_NV61:
       return VOP2_TILED_8X8_FMT_YUV422SP;
   case DRM_FORMAT_NV24:
   case DRM_FORMAT_NV42:
       return VOP2_TILED_8X8_FMT_YUV444SP;
   case DRM_FORMAT_NV15:
       return VOP2_TILED_8X8_FMT_YUV420SP_10;
   case DRM_FORMAT_NV20:
       return VOP2_TILED_8X8_FMT_YUV422SP_10;
   case DRM_FORMAT_NV30:
       return VOP2_TILED_8X8_FMT_YUV444SP_10;
   default:
       DRM_WARN_ONCE("unsupported tiled format[%08x]\n", format);
       return VOP2_TILED_FMT_INVALID;
   }
 
   return VOP2_TILED_FMT_INVALID;
}
 
static enum vop3_tiled_format vop3_convert_tiled_format(uint32_t format, uint32_t tile_mode)
{
   switch (format) {
   case DRM_FORMAT_NV12:
   case DRM_FORMAT_NV21:
       return tile_mode == ROCKCHIP_TILED_BLOCK_SIZE_8x8 ?
               VOP3_TILED_8X8_FMT_YUV420SP : VOP3_TILED_4X4_FMT_YUV420SP;
   case DRM_FORMAT_NV16:
   case DRM_FORMAT_NV61:
       return tile_mode == ROCKCHIP_TILED_BLOCK_SIZE_8x8 ?
               VOP3_TILED_8X8_FMT_YUV422SP : VOP3_TILED_4X4_FMT_YUV422SP;
   case DRM_FORMAT_NV24:
   case DRM_FORMAT_NV42:
       return tile_mode == ROCKCHIP_TILED_BLOCK_SIZE_8x8 ?
               VOP3_TILED_8X8_FMT_YUV444SP : VOP3_TILED_4X4_FMT_YUV444SP;
   case DRM_FORMAT_NV15:
       return tile_mode == ROCKCHIP_TILED_BLOCK_SIZE_8x8 ?
               VOP3_TILED_8X8_FMT_YUV420SP_10 : VOP3_TILED_4X4_FMT_YUV420SP_10;
   case DRM_FORMAT_NV20:
       return tile_mode == ROCKCHIP_TILED_BLOCK_SIZE_8x8 ?
               VOP3_TILED_8X8_FMT_YUV422SP_10 : VOP3_TILED_4X4_FMT_YUV422SP_10;
   case DRM_FORMAT_NV30:
       return tile_mode == ROCKCHIP_TILED_BLOCK_SIZE_8x8 ?
               VOP3_TILED_8X8_FMT_YUV444SP_10 : VOP3_TILED_4X4_FMT_YUV444SP_10;
   default:
       DRM_WARN_ONCE("unsupported tiled format[%08x]\n", format);
       return VOP3_TILED_FMT_INVALID;
   }
 
   return VOP3_TILED_FMT_INVALID;
}
 
static enum vop2_wb_format vop2_convert_wb_format(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_ARGB8888:
       return VOP2_WB_ARGB8888;
   case DRM_FORMAT_BGR888:
       return VOP2_WB_BGR888;
   case DRM_FORMAT_RGB565:
       return VOP2_WB_RGB565;
   case DRM_FORMAT_NV12:
       return VOP2_WB_YUV420SP;
   default:
       DRM_ERROR("unsupported wb format[%08x]\n", format);
       return VOP2_WB_INVALID;
   }
}
 
static void vop2_set_system_status(struct vop2 *vop2)
{
   if (hweight8(vop2->active_vp_mask) > 1)
       rockchip_set_system_status(SYS_STATUS_DUALVIEW);
   else
       rockchip_clear_system_status(SYS_STATUS_DUALVIEW);
}
 
static bool vop2_win_rb_swap(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_XBGR2101010:
   case DRM_FORMAT_ABGR2101010:
   case DRM_FORMAT_XBGR8888:
   case DRM_FORMAT_ABGR8888:
   case DRM_FORMAT_BGR888:
   case DRM_FORMAT_BGR565:
       return true;
   default:
       return false;
   }
}
 
static bool vop2_afbc_rb_swap(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_NV24:
   case DRM_FORMAT_NV30:
       return true;
   default:
       return false;
   }
}
 
static bool vop2_afbc_uv_swap(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_NV12:
   case DRM_FORMAT_NV16:
   case DRM_FORMAT_YUYV:
   case DRM_FORMAT_Y210:
   case DRM_FORMAT_YUV420_8BIT:
   case DRM_FORMAT_YUV420_10BIT:
       return true;
   default:
       return false;
   }
}
 
static bool vop2_win_uv_swap(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_NV12:
   case DRM_FORMAT_NV16:
   case DRM_FORMAT_NV24:
   case DRM_FORMAT_NV15:
   case DRM_FORMAT_NV20:
   case DRM_FORMAT_NV30:
   case DRM_FORMAT_YUYV:
   case DRM_FORMAT_UYVY:
       return true;
   default:
       return false;
   }
}
 
static bool vop2_win_dither_up(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_BGR565:
   case DRM_FORMAT_RGB565:
       return true;
   default:
       return false;
   }
}
 
static bool vop2_output_uv_swap(uint32_t bus_format, uint32_t output_mode)
{
   /*
    * FIXME:
    *
    * There is no media type for YUV444 output,
    * so when out_mode is AAAA or P888, assume output is YUV444 on
    * yuv format.
    *
    * From H/W testing, YUV444 mode need a rb swap.
    */
   if (bus_format == MEDIA_BUS_FMT_YVYU8_1X16 ||
       bus_format == MEDIA_BUS_FMT_VYUY8_1X16 ||
       bus_format == MEDIA_BUS_FMT_YVYU8_2X8 ||
       bus_format == MEDIA_BUS_FMT_VYUY8_2X8 ||
       ((bus_format == MEDIA_BUS_FMT_YUV8_1X24 ||
         bus_format == MEDIA_BUS_FMT_YUV10_1X30) &&
        (output_mode == ROCKCHIP_OUT_MODE_AAAA ||
         output_mode == ROCKCHIP_OUT_MODE_P888)))
       return true;
   else
       return false;
}
 
static bool vop3_output_rb_swap(uint32_t bus_format, uint32_t output_mode)
{
   /*
    * The default component order of serial rgb3x8 formats
    * is BGR. So it is needed to enable RB swap.
    */
   if (bus_format == MEDIA_BUS_FMT_RGB888_3X8 ||
       bus_format == MEDIA_BUS_FMT_RGB888_DUMMY_4X8)
       return true;
   else
       return false;
}
 
static bool vop2_output_yc_swap(uint32_t bus_format)
{
   switch (bus_format) {
   case MEDIA_BUS_FMT_YUYV8_1X16:
   case MEDIA_BUS_FMT_YVYU8_1X16:
   case MEDIA_BUS_FMT_YUYV8_2X8:
   case MEDIA_BUS_FMT_YVYU8_2X8:
       return true;
   default:
       return false;
   }
}
 
static bool is_yuv_output(uint32_t bus_format)
{
   switch (bus_format) {
   case MEDIA_BUS_FMT_YUV8_1X24:
   case MEDIA_BUS_FMT_YUV10_1X30:
   case MEDIA_BUS_FMT_YUYV10_1X20:
   case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
   case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
   case MEDIA_BUS_FMT_YUYV8_2X8:
   case MEDIA_BUS_FMT_YVYU8_2X8:
   case MEDIA_BUS_FMT_UYVY8_2X8:
   case MEDIA_BUS_FMT_VYUY8_2X8:
   case MEDIA_BUS_FMT_YUYV8_1X16:
   case MEDIA_BUS_FMT_YVYU8_1X16:
   case MEDIA_BUS_FMT_UYVY8_1X16:
   case MEDIA_BUS_FMT_VYUY8_1X16:
       return true;
   default:
       return false;
   }
}
 
static bool is_alpha_support(uint32_t format)
{
   switch (format) {
   case DRM_FORMAT_ARGB8888:
   case DRM_FORMAT_ABGR8888:
       return true;
   default:
       return false;
   }
}
 
static inline bool rockchip_afbc(struct drm_plane *plane, u64 modifier)
{
   int i;
 
   if (modifier == DRM_FORMAT_MOD_LINEAR)
       return false;
 
   if (!drm_is_afbc(modifier))
       return false;
 
   for (i = 0 ; i < plane->modifier_count; i++)
       if (plane->modifiers[i] == modifier)
           break;
 
   return (i < plane->modifier_count) ? true : false;
}
 
static inline bool rockchip_tiled(struct drm_plane *plane, u64 modifier)
{
   int i;
 
   if (modifier == DRM_FORMAT_MOD_LINEAR)
       return false;
 
   if (!IS_ROCKCHIP_TILED_MOD(modifier))
       return false;
 
   for (i = 0 ; i < plane->modifier_count; i++)
       if (plane->modifiers[i] == modifier)
           break;
 
   return (i < plane->modifier_count) ? true : false;
}
 
static bool rockchip_vop2_mod_supported(struct drm_plane *plane, u32 format, u64 modifier)
{
   if (modifier == DRM_FORMAT_MOD_INVALID)
       return false;
 
   if (modifier == DRM_FORMAT_MOD_LINEAR)
       return true;
 
   if (!rockchip_afbc(plane, modifier) && !rockchip_tiled(plane, modifier)) {
       DRM_ERROR("Unsupported format modifier 0x%llx\n", modifier);
 
       return false;
   }
 
   return vop2_convert_afbc_format(format) >= 0 ||
          vop2_convert_tiled_format(format) >= 0 ||
          vop3_convert_tiled_format(format, 0) >= 0;
}
 
static inline bool vop2_multi_area_sub_window(struct vop2_win *win)
{
   return (win->parent && (win->feature & WIN_FEATURE_MULTI_AREA));
}
 
static inline bool vop2_cluster_window(struct vop2_win *win)
{
   return  (win->feature & (WIN_FEATURE_CLUSTER_MAIN | WIN_FEATURE_CLUSTER_SUB));
}
 
static inline bool vop2_cluster_sub_window(struct vop2_win *win)
{
   return (win->feature & WIN_FEATURE_CLUSTER_SUB);
}
 
static inline bool vop2_has_feature(struct vop2 *vop2, uint64_t feature)
{
   return (vop2->data->feature & feature);
}
 
/*
 * 0: Full mode, 16 lines for one tail
 * 1: half block mode
 */
static int vop2_afbc_half_block_enable(struct vop2_plane_state *vpstate)
{
   if (vpstate->rotate_270_en || vpstate->rotate_90_en)
       return 0;
   else
       return 1;
}
 
/*
 * @xoffset: the src x offset of the right win in splice mode, other wise it
 * must be zero.
 */
static uint32_t vop2_afbc_transform_offset(struct vop2_plane_state *vpstate, int xoffset)
{
   struct drm_rect *src = &vpstate->src;
   struct drm_framebuffer *fb = vpstate->base.fb;
   uint32_t bpp = rockchip_drm_get_bpp(fb->format);
   uint32_t vir_width = (fb->pitches[0] << 3) / (bpp ? bpp : 1);
   uint32_t width = drm_rect_width(src) >> 16;
   uint32_t height = drm_rect_height(src) >> 16;
   uint32_t act_xoffset = src->x1 >> 16;
   uint32_t act_yoffset = src->y1 >> 16;
   uint32_t align16_crop = 0;
   uint32_t align64_crop = 0;
   uint32_t height_tmp = 0;
   uint32_t transform_tmp = 0;
   uint8_t transform_xoffset = 0;
   uint8_t transform_yoffset = 0;
   uint8_t top_crop = 0;
   uint8_t top_crop_line_num = 0;
   uint8_t bottom_crop_line_num = 0;
 
   act_xoffset += xoffset;
   /* 16 pixel align */
   if (height & 0xf)
       align16_crop = 16 - (height & 0xf);
 
   height_tmp = height + align16_crop;
 
   /* 64 pixel align */
   if (height_tmp & 0x3f)
       align64_crop = 64 - (height_tmp & 0x3f);
 
   top_crop_line_num = top_crop << 2;
   if (top_crop == 0)
       bottom_crop_line_num = align16_crop + align64_crop;
   else if (top_crop == 1)
       bottom_crop_line_num = align16_crop + align64_crop + 12;
   else if (top_crop == 2)
       bottom_crop_line_num = align16_crop + align64_crop + 8;
 
   if (vpstate->xmirror_en) {
       if (vpstate->ymirror_en) {
           if (vpstate->afbc_half_block_en) {
               transform_tmp = act_xoffset + width;
               transform_xoffset = 16 - (transform_tmp & 0xf);
               transform_tmp = bottom_crop_line_num - act_yoffset;
               transform_yoffset = transform_tmp & 0x7;
           } else { //FULL MODEL
               transform_tmp = act_xoffset + width;
               transform_xoffset = 16 - (transform_tmp & 0xf);
               transform_tmp = bottom_crop_line_num - act_yoffset;
               transform_yoffset = (transform_tmp & 0xf);
           }
       } else if (vpstate->rotate_90_en) {
           transform_tmp = bottom_crop_line_num - act_yoffset;
           transform_xoffset = transform_tmp & 0xf;
           transform_tmp = vir_width - width - act_xoffset;
           transform_yoffset = transform_tmp & 0xf;
       } else if (vpstate->rotate_270_en) {
           transform_tmp = top_crop_line_num + act_yoffset;
           transform_xoffset = transform_tmp & 0xf;
           transform_tmp = act_xoffset;
           transform_yoffset = transform_tmp & 0xf;
 
       } else { //xmir
           if (vpstate->afbc_half_block_en) {
               transform_tmp = act_xoffset + width;
               transform_xoffset = 16 - (transform_tmp & 0xf);
               transform_tmp = top_crop_line_num + act_yoffset;
               transform_yoffset = transform_tmp & 0x7;
           } else {
               transform_tmp = act_xoffset + width;
               transform_xoffset = 16 - (transform_tmp & 0xf);
               transform_tmp = top_crop_line_num + act_yoffset;
               transform_yoffset = transform_tmp & 0xf;
           }
       }
   } else if (vpstate->ymirror_en) {
       if (vpstate->afbc_half_block_en) {
           transform_tmp = act_xoffset;
           transform_xoffset = transform_tmp & 0xf;
           transform_tmp = bottom_crop_line_num - act_yoffset;
           transform_yoffset = transform_tmp & 0x7;
       } else { //full_mode
           transform_tmp = act_xoffset;
           transform_xoffset = transform_tmp & 0xf;
           transform_tmp = bottom_crop_line_num - act_yoffset;
           transform_yoffset = transform_tmp & 0xf;
       }
   } else if (vpstate->rotate_90_en) {
       transform_tmp = bottom_crop_line_num - act_yoffset;
       transform_xoffset = transform_tmp & 0xf;
       transform_tmp = act_xoffset;
       transform_yoffset = transform_tmp & 0xf;
   } else if (vpstate->rotate_270_en) {
       transform_tmp = top_crop_line_num + act_yoffset;
       transform_xoffset = transform_tmp & 0xf;
       transform_tmp = vir_width - width - act_xoffset;
       transform_yoffset = transform_tmp & 0xf;
   } else { //normal
       if (vpstate->afbc_half_block_en) {
           transform_tmp = act_xoffset;
           transform_xoffset = transform_tmp & 0xf;
           transform_tmp = top_crop_line_num + act_yoffset;
           transform_yoffset = transform_tmp & 0x7;
       } else { //full_mode
           transform_tmp = act_xoffset;
           transform_xoffset = transform_tmp & 0xf;
           transform_tmp = top_crop_line_num + act_yoffset;
           transform_yoffset = transform_tmp & 0xf;
       }
   }
 
   return (transform_xoffset & 0xf) | ((transform_yoffset & 0xf) << 16);
}
 
static uint32_t vop2_tile_transform_offset(struct vop2_plane_state *vpstate, uint8_t tiled_en)
{
   struct drm_rect *src = &vpstate->src;
   uint32_t act_xoffset = src->x1 >> 16;
   uint32_t act_yoffset = src->y1 >> 16;
   uint8_t transform_xoffset = 0;
   uint8_t transform_yoffset = 0;
   uint32_t tile_size = 1;
 
   if (tiled_en == 0)
       return 0;
 
   tile_size = tiled_en == ROCKCHIP_TILED_BLOCK_SIZE_8x8 ? 8 : 4;
   transform_xoffset = act_xoffset & (tile_size - 1);
   transform_yoffset = act_yoffset & (tile_size - 1);
 
   return (transform_xoffset & 0xf) | ((transform_yoffset & 0xf) << 16);
}
 
/*
 * A Cluster window has 2048 x 16 line buffer, which can
 * works at 2048 x 16(Full) or 4096 x 8 (Half) mode.
 * for Cluster_lb_mode register:
 * 0: half mode, for plane input width range 2048 ~ 4096
 * 1: half mode, for cluster work at 2 * 2048 plane mode
 * 2: half mode, for rotate_90/270 mode
 *
 */
static int vop2_get_cluster_lb_mode(struct vop2_win *win, struct vop2_plane_state *vpstate)
{
   if (vpstate->rotate_270_en || vpstate->rotate_90_en)
       return 2;
   else if (win->feature & WIN_FEATURE_CLUSTER_SUB)
       return 1;
   else
       return 0;
}
 
/*
 * bli_sd_factor = (src - 1) / (dst - 1) << 12;
 * avg_sd_factor:
 * bli_su_factor:
 * bic_su_factor:
 * = (src - 1) / (dst - 1) << 16;
 *
 * ygt2 enable: dst get one line from two line of the src
 * ygt4 enable: dst get one line from four line of the src.
 *
 */
#define VOP2_BILI_SCL_DN(src, dst)    (((src - 1) << 12) / (dst - 1))
#define VOP2_COMMON_SCL(src, dst)    (((src - 1) << 16) / (dst - 1))
 
#define VOP2_BILI_SCL_FAC_CHECK(src, dst, fac)     \
               (fac * (dst - 1) >> 12 < (src - 1))
#define VOP2_COMMON_SCL_FAC_CHECK(src, dst, fac) \
               (fac * (dst - 1) >> 16 < (src - 1))
#define VOP3_COMMON_HOR_SCL_FAC_CHECK(src, dst, fac) \
                   (fac * (dst - 1) >> 16 < (src - 1))
 
static uint16_t vop2_scale_factor(enum scale_mode mode,
                 int32_t filter_mode,
                 uint32_t src, uint32_t dst)
{
   uint32_t fac = 0;
   int i = 0;
 
   if (mode == SCALE_NONE)
       return 0;
 
   /*
    * A workaround to avoid zero div.
    */
   if ((dst == 1) || (src == 1)) {
       dst = dst + 1;
       src = src + 1;
   }
 
   if ((mode == SCALE_DOWN) && (filter_mode == VOP2_SCALE_DOWN_BIL)) {
       fac = VOP2_BILI_SCL_DN(src, dst);
       for (i = 0; i < 100; i++) {
           if (VOP2_BILI_SCL_FAC_CHECK(src, dst, fac))
               break;
           fac -= 1;
           DRM_DEBUG("down fac cali: src:%d, dst:%d, fac:0x%x\n", src, dst, fac);
       }
   } else {
       fac = VOP2_COMMON_SCL(src, dst);
       for (i = 0; i < 100; i++) {
           if (VOP2_COMMON_SCL_FAC_CHECK(src, dst, fac))
               break;
           fac -= 1;
           DRM_DEBUG("up fac cali:  src:%d, dst:%d, fac:0x%x\n", src, dst, fac);
       }
   }
 
   return fac;
}
 
static bool vop3_scale_up_fac_check(uint32_t src, uint32_t dst, uint32_t fac, bool is_hor)
{
   if (is_hor)
       return VOP3_COMMON_HOR_SCL_FAC_CHECK(src, dst, fac);
   return VOP2_COMMON_SCL_FAC_CHECK(src, dst, fac);
}
 
static uint16_t vop3_scale_factor(enum scale_mode mode,
                 uint32_t src, uint32_t dst, bool is_hor)
{
   uint32_t fac = 0;
   int i = 0;
 
   if (mode == SCALE_NONE)
       return 0;
 
   /*
    * A workaround to avoid zero div.
    */
   if ((dst == 1) || (src == 1)) {
       dst = dst + 1;
       src = src + 1;
   }
 
   if (mode == SCALE_DOWN) {
       fac = VOP2_BILI_SCL_DN(src, dst);
       for (i = 0; i < 100; i++) {
           if (VOP2_BILI_SCL_FAC_CHECK(src, dst, fac))
               break;
           fac -= 1;
           DRM_DEBUG("down fac cali: src:%d, dst:%d, fac:0x%x\n", src, dst, fac);
       }
   } else {
       fac = VOP2_COMMON_SCL(src, dst);
       for (i = 0; i < 100; i++) {
           if (vop3_scale_up_fac_check(src, dst, fac, is_hor))
               break;
           fac -= 1;
           DRM_DEBUG("up fac cali:  src:%d, dst:%d, fac:0x%x\n", src, dst, fac);
       }
   }
 
   return fac;
}
 
static void vop2_setup_scale(struct vop2 *vop2, struct vop2_win *win,
                uint32_t src_w, uint32_t src_h, uint32_t dst_w,
                uint32_t dst_h, struct drm_plane_state *pstate)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_win_data *win_data = &vop2_data->win[win->win_id];
   struct vop2_plane_state *vpstate = to_vop2_plane_state(pstate);
   struct drm_framebuffer *fb = pstate->fb;
   uint32_t pixel_format = fb->format->format;
   const struct drm_format_info *info = drm_format_info(pixel_format);
   uint8_t hsub = info->hsub;
   uint8_t vsub = info->vsub;
   uint16_t cbcr_src_w = src_w / hsub;
   uint16_t cbcr_src_h = src_h / vsub;
   uint16_t yrgb_hor_scl_mode, yrgb_ver_scl_mode;
   uint16_t cbcr_hor_scl_mode, cbcr_ver_scl_mode;
   uint16_t hscl_filter_mode, vscl_filter_mode;
   uint8_t xgt2 = 0, xgt4 = 0;
   uint8_t ygt2 = 0, ygt4 = 0;
   uint32_t val;
 
   if (is_vop3(vop2)) {
       if (src_w >= (4 * dst_w)) {
           xgt4 = 1;
           src_w >>= 2;
       } else if (src_w >= (2 * dst_w)) {
           xgt2 = 1;
           src_w >>= 1;
       }
   }
 
   /**
    * The rk3528 is processed as 2 pixel/cycle,
    * so ygt2/ygt4 needs to be triggered in advance to improve performance
    * when src_w is bigger than 1920.
    * dst_h / src_h is at [1, 0.65)     ygt2=0; ygt4=0;
    * dst_h / src_h is at [0.65, 0.35)  ygt2=1; ygt4=0;
    * dst_h / src_h is at [0.35, 0)     ygt2=0; ygt4=1;
    */
   if (vop2->version == VOP_VERSION_RK3528 && src_w > 1920) {
       if (src_h >= (100 * dst_h / 35)) {
           ygt4 = 1;
           src_h >>= 2;
       } else if ((src_h >= 100 * dst_h / 65) && (src_h < 100 * dst_h / 35)) {
           ygt2 = 1;
           src_h >>= 1;
       }
   } else {
       if (src_h >= (4 * dst_h)) {
           ygt4 = 1;
           src_h >>= 2;
       } else if (src_h >= (2 * dst_h)) {
           ygt2 = 1;
           src_h >>= 1;
       }
   }
 
   yrgb_hor_scl_mode = scl_get_scl_mode(src_w, dst_w);
   yrgb_ver_scl_mode = scl_get_scl_mode(src_h, dst_h);
 
   if (yrgb_hor_scl_mode == SCALE_UP)
       hscl_filter_mode = win_data->hsu_filter_mode;
   else
       hscl_filter_mode = win_data->hsd_filter_mode;
 
   if (yrgb_ver_scl_mode == SCALE_UP)
       vscl_filter_mode = win_data->vsu_filter_mode;
   else
       vscl_filter_mode = win_data->vsd_filter_mode;
 
   /*
    * RK3568 VOP Esmart/Smart dsp_w should be even pixel
    * at scale down mode
    */
   if (!(win->feature & WIN_FEATURE_AFBDC) && !is_vop3(vop2)) {
       if ((yrgb_hor_scl_mode == SCALE_DOWN) && (dst_w & 0x1)) {
           dev_dbg(vop2->dev, "%s dst_w[%d] should align as 2 pixel\n", win->name, dst_w);
           dst_w += 1;
       }
   }
 
   if (is_vop3(vop2)) {
       bool xgt_en = false, xavg_en = false;
 
       val = vop3_scale_factor(yrgb_hor_scl_mode, src_w, dst_w, true);
       VOP_SCL_SET(vop2, win, scale_yrgb_x, val);
       val = vop3_scale_factor(yrgb_ver_scl_mode, src_h, dst_h, false);
       VOP_SCL_SET(vop2, win, scale_yrgb_y, val);
 
       if (win_data->hsd_pre_filter_mode == VOP3_PRE_SCALE_DOWN_AVG)
           xavg_en = xgt2 || xgt4;
       else
           xgt_en = xgt2 || xgt4;
 
       VOP_SCL_SET(vop2, win, xgt_en, xgt_en);
       VOP_SCL_SET(vop2, win, xavg_en, xavg_en);
       VOP_SCL_SET(vop2, win, xgt_mode, xgt2 ? 0 : 1);
   } else {
       val = vop2_scale_factor(yrgb_hor_scl_mode, hscl_filter_mode, src_w, dst_w);
       VOP_SCL_SET(vop2, win, scale_yrgb_x, val);
       val = vop2_scale_factor(yrgb_ver_scl_mode, vscl_filter_mode, src_h, dst_h);
       VOP_SCL_SET(vop2, win, scale_yrgb_y, val);
   }
 
   /* vop2 and linear mode only can support gt */
   if (!is_vop3(vop2) ||
       (!vpstate->afbc_en && !vpstate->tiled_en) ||
       win_data->vsd_pre_filter_mode == VOP3_PRE_SCALE_DOWN_GT) {
       VOP_SCL_SET(vop2, win, vsd_yrgb_gt4, ygt4);
       VOP_SCL_SET(vop2, win, vsd_yrgb_gt2, ygt2);
       VOP_SCL_SET(vop2, win, vsd_avg4, 0);
       VOP_SCL_SET(vop2, win, vsd_avg2, 0);
   } else {
       VOP_SCL_SET(vop2, win, vsd_yrgb_gt4, 0);
       VOP_SCL_SET(vop2, win, vsd_yrgb_gt2, 0);
       VOP_SCL_SET(vop2, win, vsd_avg4, ygt4);
       VOP_SCL_SET(vop2, win, vsd_avg2, ygt2);
   }
 
   VOP_SCL_SET(vop2, win, yrgb_hor_scl_mode, yrgb_hor_scl_mode);
   VOP_SCL_SET(vop2, win, yrgb_ver_scl_mode, yrgb_ver_scl_mode);
 
   VOP_SCL_SET(vop2, win, yrgb_hscl_filter_mode, hscl_filter_mode);
   VOP_SCL_SET(vop2, win, yrgb_vscl_filter_mode, vscl_filter_mode);
 
   if (info->is_yuv) {
       ygt4 = ygt2 = 0;
 
       if (!is_vop3(vop2) ||
           (!vpstate->afbc_en && !vpstate->tiled_en) ||
           win_data->vsd_pre_filter_mode == VOP3_PRE_SCALE_DOWN_GT) {
           if (vop2->version == VOP_VERSION_RK3528 && src_w > 1920) {
               if (cbcr_src_h >= (100 * dst_h / 35))
                   ygt4 = 1;
               else if ((cbcr_src_h >= 100 * dst_h / 65) && (cbcr_src_h < 100 * dst_h / 35))
                   ygt2 = 1;
           } else {
               if (cbcr_src_h >= (4 * dst_h))
                   ygt4 = 1;
               else if (cbcr_src_h >= (2 * dst_h))
                   ygt2 = 1;
           }
 
           if (ygt4)
               cbcr_src_h >>= 2;
           else if (ygt2)
               cbcr_src_h >>= 1;
       }
       VOP_SCL_SET(vop2, win, vsd_cbcr_gt4, ygt4);
       VOP_SCL_SET(vop2, win, vsd_cbcr_gt2, ygt2);
 
       if (!is_vop3(vop2)) {
           cbcr_hor_scl_mode = scl_get_scl_mode(cbcr_src_w, dst_w);
           cbcr_ver_scl_mode = scl_get_scl_mode(cbcr_src_h, dst_h);
 
           val = vop2_scale_factor(cbcr_hor_scl_mode, hscl_filter_mode,
                       cbcr_src_w, dst_w);
           VOP_SCL_SET(vop2, win, scale_cbcr_x, val);
           val = vop2_scale_factor(cbcr_ver_scl_mode, vscl_filter_mode,
                       cbcr_src_h, dst_h);
           VOP_SCL_SET(vop2, win, scale_cbcr_y, val);
 
           VOP_SCL_SET(vop2, win, cbcr_hor_scl_mode, cbcr_hor_scl_mode);
           VOP_SCL_SET(vop2, win, cbcr_ver_scl_mode, cbcr_ver_scl_mode);
           VOP_SCL_SET(vop2, win, cbcr_hscl_filter_mode, hscl_filter_mode);
           VOP_SCL_SET(vop2, win, cbcr_vscl_filter_mode, vscl_filter_mode);
       }
   }
}
 
static int vop2_convert_csc_mode(int csc_mode, int bit_depth)
{
   switch (csc_mode) {
   case V4L2_COLORSPACE_SMPTE170M:
   case V4L2_COLORSPACE_470_SYSTEM_M:
   case V4L2_COLORSPACE_470_SYSTEM_BG:
       return CSC_BT601L;
   case V4L2_COLORSPACE_REC709:
   case V4L2_COLORSPACE_SMPTE240M:
   case V4L2_COLORSPACE_DEFAULT:
       if (bit_depth == CSC_13BIT_DEPTH)
           return CSC_BT709L_13BIT;
       else
           return CSC_BT709L;
   case V4L2_COLORSPACE_JPEG:
       return CSC_BT601F;
   case V4L2_COLORSPACE_BT2020:
       if (bit_depth == CSC_13BIT_DEPTH)
           return CSC_BT2020L_13BIT;
       else
           return CSC_BT2020;
   case V4L2_COLORSPACE_BT709F:
       if (bit_depth == CSC_10BIT_DEPTH) {
           DRM_WARN("Unsupported bt709f at 10bit csc depth, use bt601f instead\n");
           return CSC_BT601F;
       } else {
           return CSC_BT709F_13BIT;
       }
   case V4L2_COLORSPACE_BT2020F:
       if (bit_depth == CSC_10BIT_DEPTH) {
           DRM_WARN("Unsupported bt2020f at 10bit csc depth, use bt601f instead\n");
           return CSC_BT601F;
       } else {
           return CSC_BT2020F_13BIT;
       }
   default:
       return CSC_BT709L;
   }
}
 
static bool vop2_is_allwin_disabled(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   unsigned long win_mask = vp->win_mask;
   struct vop2_win *win;
   int phys_id;
 
   for_each_set_bit(phys_id, &win_mask, ROCKCHIP_MAX_LAYER) {
       win = vop2_find_win_by_phys_id(vop2, phys_id);
       if (VOP_WIN_GET(vop2, win, enable) != 0)
           return false;
   }
 
   return true;
}
 
static void vop2_disable_all_planes_for_crtc(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct vop2_win *win;
   unsigned long win_mask = vp->win_mask;
   int phys_id, ret;
   bool active, need_wait_win_disabled = false;
 
   for_each_set_bit(phys_id, &win_mask, ROCKCHIP_MAX_LAYER) {
       win = vop2_find_win_by_phys_id(vop2, phys_id);
       need_wait_win_disabled |= VOP_WIN_GET(vop2, win, enable);
       vop2_win_disable(win, false);
   }
 
   if (need_wait_win_disabled) {
       vop2_cfg_done(crtc);
       ret = readx_poll_timeout_atomic(vop2_is_allwin_disabled, crtc,
                       active, active, 0, 500 * 1000);
       if (ret)
           DRM_DEV_ERROR(vop2->dev, "wait win close timeout\n");
   }
}
 
/*
 * colorspace path:
 *      Input        Win csc                     Output
 * 1. YUV(2020)  --> Y2R->2020To709->R2Y   --> YUV_OUTPUT(601/709)
 *    RGB        --> R2Y                  __/
 *
 * 2. YUV(2020)  --> bypasss               --> YUV_OUTPUT(2020)
 *    RGB        --> 709To2020->R2Y       __/
 *
 * 3. YUV(2020)  --> Y2R->2020To709        --> RGB_OUTPUT(709)
 *    RGB        --> R2Y                  __/
 *
 * 4. YUV(601/709)-> Y2R->709To2020->R2Y   --> YUV_OUTPUT(2020)
 *    RGB        --> 709To2020->R2Y       __/
 *
 * 5. YUV(601/709)-> bypass                --> YUV_OUTPUT(709)
 *    RGB        --> R2Y                  __/
 *
 * 6. YUV(601/709)-> bypass                --> YUV_OUTPUT(601)
 *    RGB        --> R2Y(601)             __/
 *
 * 7. YUV        --> Y2R(709)              --> RGB_OUTPUT(709)
 *    RGB        --> bypass               __/
 *
 * 8. RGB        --> 709To2020->R2Y        --> YUV_OUTPUT(2020)
 *
 * 9. RGB        --> R2Y(709)              --> YUV_OUTPUT(709)
 *
 * 10. RGB       --> R2Y(601)              --> YUV_OUTPUT(601)
 *
 * 11. RGB       --> bypass                --> RGB_OUTPUT(709)
 */
 
static void vop2_setup_csc_mode(struct vop2_video_port *vp,
               struct vop2_plane_state *vpstate)
{
   struct drm_plane_state *pstate = &vpstate->base;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(vp->rockchip_crtc.crtc.state);
   int is_input_yuv = pstate->fb->format->is_yuv;
   int is_output_yuv = vcstate->yuv_overlay;
   int input_csc = vpstate->color_space;
   int output_csc = vcstate->color_space;
   struct vop2_win *win = to_vop2_win(pstate->plane);
   int csc_y2r_bit_depth = CSC_10BIT_DEPTH;
 
   if (win->feature & WIN_FEATURE_Y2R_13BIT_DEPTH)
       csc_y2r_bit_depth = CSC_13BIT_DEPTH;
 
   vpstate->y2r_en = 0;
   vpstate->r2y_en = 0;
   vpstate->csc_mode = 0;
 
   if (is_vop3(vp->vop2)) {
       if (vpstate->hdr_in) {
           if (is_input_yuv) {
               vpstate->y2r_en = 1;
               vpstate->csc_mode = vop2_convert_csc_mode(input_csc,
                                     CSC_13BIT_DEPTH);
           }
           return;
       } else if (vp->sdr2hdr_en) {
           if (is_input_yuv) {
               vpstate->y2r_en = 1;
               vpstate->csc_mode = vop2_convert_csc_mode(input_csc,
                                     csc_y2r_bit_depth);
           }
           return;
       }
   } else {
       /* hdr2sdr and sdr2hdr will do csc itself */
       if (vpstate->hdr2sdr_en) {
           /*
            * This is hdr2sdr enabled plane
            * If it's RGB layer do hdr2sdr, we need to do r2y before send to hdr2sdr,
            * because hdr2sdr only support yuv input.
            */
           if (!is_input_yuv) {
               vpstate->r2y_en = 1;
               vpstate->csc_mode = vop2_convert_csc_mode(output_csc,
                                     CSC_10BIT_DEPTH);
           }
           return;
       } else if (!vpstate->hdr_in && vp->sdr2hdr_en) {
           /*
            * This is sdr2hdr enabled plane
            * If it's YUV layer do sdr2hdr, we need to do y2r before send to sdr2hdr,
            * because sdr2hdr only support rgb input.
            */
           if (is_input_yuv) {
               vpstate->y2r_en = 1;
               vpstate->csc_mode = vop2_convert_csc_mode(input_csc,
                                     csc_y2r_bit_depth);
           }
           return;
       }
   }
 
   if (is_input_yuv && !is_output_yuv) {
       vpstate->y2r_en = 1;
       vpstate->csc_mode = vop2_convert_csc_mode(input_csc, csc_y2r_bit_depth);
   } else if (!is_input_yuv && is_output_yuv) {
       vpstate->r2y_en = 1;
       vpstate->csc_mode = vop2_convert_csc_mode(output_csc, CSC_10BIT_DEPTH);
   }
}
 
static void vop2_axi_irqs_enable(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop_intr *intr;
   uint32_t irqs = BUS_ERROR_INTR;
   uint32_t i;
 
   for (i = 0; i < vop2_data->nr_axi_intr; i++) {
       intr = &vop2_data->axi_intr[i];
       VOP_INTR_SET_TYPE(vop2, intr, clear, irqs, 1);
       VOP_INTR_SET_TYPE(vop2, intr, enable, irqs, 1);
   }
}
 
static uint32_t vop2_read_and_clear_axi_irqs(struct vop2 *vop2, int index)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop_intr *intr = &vop2_data->axi_intr[index];
   uint32_t irqs = BUS_ERROR_INTR;
   uint32_t val;
 
   val = VOP_INTR_GET_TYPE(vop2, intr, status, irqs);
   if (val)
       VOP_INTR_SET_TYPE(vop2, intr, clear, val, 1);
 
   return val;
}
 
static void vop2_dsp_hold_valid_irq_enable(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
 
   unsigned long flags;
 
   if (WARN_ON(!vop2->is_enabled))
       return;
 
   spin_lock_irqsave(&vop2->irq_lock, flags);
 
   VOP_INTR_SET_TYPE(vop2, intr, clear, DSP_HOLD_VALID_INTR, 1);
   VOP_INTR_SET_TYPE(vop2, intr, enable, DSP_HOLD_VALID_INTR, 1);
 
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
}
 
static void vop2_dsp_hold_valid_irq_disable(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
   unsigned long flags;
 
   if (WARN_ON(!vop2->is_enabled))
       return;
 
   spin_lock_irqsave(&vop2->irq_lock, flags);
 
   VOP_INTR_SET_TYPE(vop2, intr, enable, DSP_HOLD_VALID_INTR, 0);
 
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
}
 
static void vop2_debug_irq_enable(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
   uint32_t irqs = POST_BUF_EMPTY_INTR;
 
   VOP_INTR_SET_TYPE(vop2, intr, clear, irqs, 1);
   VOP_INTR_SET_TYPE(vop2, intr, enable, irqs, 1);
}
 
/*
 * (1) each frame starts at the start of the Vsync pulse which is signaled by
 *     the "FRAME_SYNC" interrupt.
 * (2) the active data region of each frame ends at dsp_vact_end
 * (3) we should program this same number (dsp_vact_end) into dsp_line_frag_num,
 *      to get "LINE_FLAG" interrupt at the end of the active on screen data.
 *
 * VOP_INTR_CTRL0.dsp_line_frag_num = VOP_DSP_VACT_ST_END.dsp_vact_end
 * Interrupts
 * LINE_FLAG -------------------------------+
 * FRAME_SYNC ----+                         |
 *                |                         |
 *                v                         v
 *                | Vsync | Vbp |  Vactive  | Vfp |
 *                        ^     ^           ^     ^
 *                        |     |           |     |
 *                        |     |           |     |
 * dsp_vs_end ------------+     |           |     |   VOP_DSP_VTOTAL_VS_END
 * dsp_vact_start --------------+           |     |   VOP_DSP_VACT_ST_END
 * dsp_vact_end ----------------------------+     |   VOP_DSP_VACT_ST_END
 * dsp_total -------------------------------------+   VOP_DSP_VTOTAL_VS_END
 */
 
static int vop2_core_clks_enable(struct vop2 *vop2)
{
   int ret;
 
   ret = clk_enable(vop2->hclk);
   if (ret < 0)
       return ret;
 
   ret = clk_enable(vop2->aclk);
   if (ret < 0)
       goto err_disable_hclk;
 
   ret = clk_enable(vop2->pclk);
   if (ret < 0)
       goto err_disable_aclk;
 
   return 0;
 
err_disable_aclk:
   clk_disable(vop2->aclk);
err_disable_hclk:
   clk_disable(vop2->hclk);
   return ret;
}
 
static void vop2_core_clks_disable(struct vop2 *vop2)
{
   clk_disable(vop2->pclk);
   clk_disable(vop2->aclk);
   clk_disable(vop2->hclk);
}
 
static void vop2_wb_connector_reset(struct drm_connector *connector)
{
   struct vop2_wb_connector_state *wb_state;
 
   if (connector->state) {
       __drm_atomic_helper_connector_destroy_state(connector->state);
       kfree(connector->state);
       connector->state = NULL;
   }
 
   wb_state = kzalloc(sizeof(*wb_state), GFP_KERNEL);
   if (wb_state)
       __drm_atomic_helper_connector_reset(connector, &wb_state->base);
}
 
static enum drm_connector_status
vop2_wb_connector_detect(struct drm_connector *connector, bool force)
{
   return connector_status_connected;
}
 
static void vop2_wb_connector_destroy(struct drm_connector *connector)
{
   drm_connector_cleanup(connector);
}
 
static struct drm_connector_state *
vop2_wb_connector_duplicate_state(struct drm_connector *connector)
{
   struct vop2_wb_connector_state *wb_state;
 
   if (WARN_ON(!connector->state))
       return NULL;
 
   wb_state = kzalloc(sizeof(*wb_state), GFP_KERNEL);
   if (!wb_state)
       return NULL;
 
   __drm_atomic_helper_connector_duplicate_state(connector, &wb_state->base);
 
   return &wb_state->base;
}
 
static const struct drm_connector_funcs vop2_wb_connector_funcs = {
   .reset = vop2_wb_connector_reset,
   .detect = vop2_wb_connector_detect,
   .fill_modes = drm_helper_probe_single_connector_modes,
   .destroy = vop2_wb_connector_destroy,
   .atomic_duplicate_state = vop2_wb_connector_duplicate_state,
   .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
};
 
static int vop2_wb_connector_get_modes(struct drm_connector *connector)
{
   struct drm_display_mode *mode;
   int i;
 
   for (i = 0; i < 2; i++) {
       mode = drm_mode_create(connector->dev);
       if (!mode)
           break;
 
       mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
       mode->clock = 148500 >> i;
       mode->hdisplay = 1920 >> i;
       mode->hsync_start = 1930 >> i;
       mode->hsync_end = 1940 >> i;
       mode->htotal = 1990 >> i;
       mode->vdisplay = 1080 >> i;
       mode->vsync_start = 1090 >> i;
       mode->vsync_end = 1100 >> i;
       mode->vtotal = 1110 >> i;
       mode->flags = 0;
 
       drm_mode_set_name(mode);
       drm_mode_probed_add(connector, mode);
   }
   return i;
}
 
static enum drm_mode_status
vop2_wb_connector_mode_valid(struct drm_connector *connector,
                  struct drm_display_mode *mode)
{
 
   struct drm_writeback_connector *wb_conn;
   struct vop2_wb *wb;
   struct vop2 *vop2;
   int w, h;
 
   wb_conn = container_of(connector, struct drm_writeback_connector, base);
   wb = container_of(wb_conn, struct vop2_wb, conn);
   vop2 = container_of(wb, struct vop2, wb);
   w = mode->hdisplay;
   h = mode->vdisplay;
 
 
   if (w > vop2->data->wb->max_output.width)
       return MODE_BAD_HVALUE;
 
   if (h > vop2->data->wb->max_output.height)
       return MODE_BAD_VVALUE;
 
   return MODE_OK;
}
 
static inline bool
vop2_wb_connector_changed_only(struct drm_crtc_state *cstate, struct drm_connector *conn)
{
   struct drm_crtc_state *old_state;
   u32 changed_connectors;
 
   old_state = drm_atomic_get_old_crtc_state(cstate->state, cstate->crtc);
   changed_connectors = cstate->connector_mask ^ old_state->connector_mask;
 
   return BIT(drm_connector_index(conn)) == changed_connectors;
}
 
static int vop2_wb_encoder_atomic_check(struct drm_encoder *encoder,
                  struct drm_crtc_state *cstate,
                  struct drm_connector_state *conn_state)
{
   struct vop2_wb_connector_state *wb_state = to_wb_state(conn_state);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(cstate);
   struct vop2_video_port *vp = to_vop2_video_port(cstate->crtc);
   struct drm_framebuffer *fb;
   struct drm_gem_object *obj, *uv_obj;
   struct rockchip_gem_object *rk_obj, *rk_uv_obj;
 
   /*
    * No need for a full modested when the only connector changed is the
    * writeback connector.
    */
   if (cstate->connectors_changed &&
       vop2_wb_connector_changed_only(cstate, conn_state->connector)) {
       cstate->connectors_changed = false;
       DRM_DEBUG("VP%d force change connectors_changed to false when only wb changed\n", vp->id);
   }
   if (!conn_state->writeback_job || !conn_state->writeback_job->fb)
       return 0;
 
   fb = conn_state->writeback_job->fb;
   DRM_DEV_DEBUG(vp->vop2->dev, "%d x % d\n", fb->width, fb->height);
 
   if (!fb->format->is_yuv && is_yuv_output(vcstate->bus_format)) {
       DRM_ERROR("YUV2RGB is not supported by writeback\n");
       return -EINVAL;
   }
 
   if ((fb->width > cstate->mode.hdisplay) ||
       ((fb->height < cstate->mode.vdisplay) &&
       (fb->height != (cstate->mode.vdisplay >> 1)))) {
       DRM_DEBUG_KMS("Invalid framebuffer size %ux%u, Only support x scale down and 1/2 y scale down\n",
               fb->width, fb->height);
       return -EINVAL;
   }
 
   wb_state->scale_x_factor = vop2_scale_factor(SCALE_DOWN, VOP2_SCALE_DOWN_BIL,
                             cstate->mode.hdisplay, fb->width);
   wb_state->scale_x_en = (fb->width < cstate->mode.hdisplay) ? 1 : 0;
   wb_state->scale_y_en = (fb->height < cstate->mode.vdisplay) ? 1 : 0;
 
   wb_state->format = vop2_convert_wb_format(fb->format->format);
   if (wb_state->format < 0) {
       struct drm_format_name_buf format_name;
 
       DRM_DEBUG_KMS("Invalid pixel format %s\n",
                 drm_get_format_name(fb->format->format,
                         &format_name));
       return -EINVAL;
   }
 
   wb_state->vp_id = vp->id;
   obj = fb->obj[0];
   rk_obj = to_rockchip_obj(obj);
   wb_state->yrgb_addr = rk_obj->dma_addr + fb->offsets[0];
 
   if (fb->format->is_yuv) {
       uv_obj = fb->obj[1];
       rk_uv_obj = to_rockchip_obj(uv_obj);
 
       wb_state->uv_addr = rk_uv_obj->dma_addr + fb->offsets[1];
   }
 
   return 0;
}
 
static const struct drm_encoder_helper_funcs vop2_wb_encoder_helper_funcs = {
   .atomic_check = vop2_wb_encoder_atomic_check,
};
 
static const struct drm_connector_helper_funcs vop2_wb_connector_helper_funcs = {
   .get_modes = vop2_wb_connector_get_modes,
   .mode_valid = vop2_wb_connector_mode_valid,
};
 
 
static int vop2_wb_connector_init(struct vop2 *vop2, int nr_crtcs)
{
   const struct vop2_data *vop2_data = vop2->data;
   int ret;
 
   vop2->wb.regs = vop2_data->wb->regs;
   vop2->wb.conn.encoder.possible_crtcs = (1 << nr_crtcs) - 1;
   spin_lock_init(&vop2->wb.job_lock);
   drm_connector_helper_add(&vop2->wb.conn.base, &vop2_wb_connector_helper_funcs);
 
   ret = drm_writeback_connector_init(vop2->drm_dev, &vop2->wb.conn,
                      &vop2_wb_connector_funcs,
                      &vop2_wb_encoder_helper_funcs,
                      vop2_data->wb->formats,
                      vop2_data->wb->nformats);
   if (ret)
       DRM_DEV_ERROR(vop2->dev, "writeback connector init failed\n");
   return ret;
}
 
static void vop2_wb_connector_destory(struct vop2 *vop2)
{
   drm_encoder_cleanup(&vop2->wb.conn.encoder);
   drm_connector_cleanup(&vop2->wb.conn.base);
}
 
static void vop2_wb_irqs_enable(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop_intr *intr = &vop2_data->axi_intr[0];
   uint32_t irqs = WB_UV_FIFO_FULL_INTR | WB_YRGB_FIFO_FULL_INTR;
 
   VOP_INTR_SET_TYPE(vop2, intr, clear, irqs, 1);
   VOP_INTR_SET_TYPE(vop2, intr, enable, irqs, 1);
}
 
static uint32_t vop2_read_and_clear_wb_irqs(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop_intr *intr = &vop2_data->axi_intr[0];
   uint32_t irqs = WB_UV_FIFO_FULL_INTR | WB_YRGB_FIFO_FULL_INTR;
   uint32_t val;
 
   val = VOP_INTR_GET_TYPE(vop2, intr, status, irqs);
   if (val)
       VOP_INTR_SET_TYPE(vop2, intr, clear, val, 1);
 
 
   return val;
}
 
static void vop2_wb_commit(struct drm_crtc *crtc)
{
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct vop2_wb *wb = &vop2->wb;
   struct drm_writeback_connector *wb_conn = &wb->conn;
   struct drm_connector_state *conn_state = wb_conn->base.state;
   struct vop2_wb_connector_state *wb_state;
   unsigned long flags;
   uint32_t fifo_throd;
   uint8_t r2y;
 
   if (!conn_state)
       return;
   wb_state = to_wb_state(conn_state);
 
   if (wb_state->vp_id != vp->id)
       return;
 
   if (conn_state->writeback_job && conn_state->writeback_job->fb) {
       struct drm_framebuffer *fb = conn_state->writeback_job->fb;
 
       rockchip_drm_dbg(vop2->dev, VOP_DEBUG_WB,
                "Enable wb %ux%u fmt: %u pitches: %d addr: %pad\n",
                fb->width, fb->height, wb_state->format,
                fb->pitches[0], &wb_state->yrgb_addr);
 
       drm_writeback_queue_job(wb_conn, conn_state);
       conn_state->writeback_job = NULL;
 
       spin_lock_irqsave(&wb->job_lock, flags);
       wb->jobs[wb->job_index].pending = true;
       wb->job_index++;
       if (wb->job_index >= VOP2_WB_JOB_MAX)
           wb->job_index = 0;
       spin_unlock_irqrestore(&wb->job_lock, flags);
 
       fifo_throd = fb->pitches[0] >> 4;
       if (fifo_throd >= vop2->data->wb->fifo_depth)
           fifo_throd = vop2->data->wb->fifo_depth;
       r2y = !vcstate->yuv_overlay && fb->format->is_yuv;
 
       /*
        * the vp_id register config done immediately
        */
       VOP_MODULE_SET(vop2, wb, vp_id, wb_state->vp_id);
       VOP_MODULE_SET(vop2, wb, format, wb_state->format);
       VOP_MODULE_SET(vop2, wb, yrgb_mst, wb_state->yrgb_addr);
       VOP_MODULE_SET(vop2, wb, uv_mst, wb_state->uv_addr);
       VOP_MODULE_SET(vop2, wb, fifo_throd, fifo_throd);
       VOP_MODULE_SET(vop2, wb, scale_x_factor, wb_state->scale_x_factor);
       VOP_MODULE_SET(vop2, wb, scale_x_en, wb_state->scale_x_en);
       VOP_MODULE_SET(vop2, wb, scale_y_en, wb_state->scale_y_en);
       VOP_MODULE_SET(vop2, wb, r2y_en, r2y);
       VOP_MODULE_SET(vop2, wb, enable, 1);
       vop2_wb_irqs_enable(vop2);
       VOP_CTRL_SET(vop2, wb_dma_finish_and_en, 1);
   }
}
 
static void rk3568_crtc_load_lut(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   int dle = 0, i = 0;
   u8 vp_enable_gamma_nr = 0;
 
   for (i = 0; i < vop2->data->nr_vps; i++) {
       struct vop2_video_port *_vp = &vop2->vps[i];
 
       if (VOP_MODULE_GET(vop2, _vp, dsp_lut_en))
           vp_enable_gamma_nr++;
   }
 
   if (vop2->data->nr_gammas &&
       vp_enable_gamma_nr >= vop2->data->nr_gammas &&
       VOP_MODULE_GET(vop2, vp, dsp_lut_en) == 0) {
       DRM_INFO("only support %d gamma\n", vop2->data->nr_gammas);
 
       return;
   }
 
   spin_lock(&vop2->reg_lock);
   VOP_MODULE_SET(vop2, vp, dsp_lut_en, 0);
   vop2_cfg_done(crtc);
   spin_unlock(&vop2->reg_lock);
 
#define CTRL_GET(name) VOP_MODULE_GET(vop2, vp, name)
   readx_poll_timeout(CTRL_GET, dsp_lut_en, dle, !dle, 5, 33333);
 
   VOP_CTRL_SET(vop2, gamma_port_sel, vp->id);
   for (i = 0; i < vp->gamma_lut_len; i++)
       vop2_write_lut(vop2, i << 2, vp->lut[i]);
 
   spin_lock(&vop2->reg_lock);
 
   VOP_MODULE_SET(vop2, vp, dsp_lut_en, 1);
   vop2_write_reg_uncached(vop2, &vp->regs->gamma_update_en, 1);
   vp->gamma_lut_active = true;
 
   spin_unlock(&vop2->reg_lock);
#undef CTRL_GET
}
 
static void rk3588_crtc_load_lut(struct drm_crtc *crtc, u32 *lut)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   int i = 0;
 
   spin_lock(&vop2->reg_lock);
 
   VOP_CTRL_SET(vop2, gamma_port_sel, vp->id);
   for (i = 0; i < vp->gamma_lut_len; i++)
       vop2_write_lut(vop2, i << 2, lut[i]);
 
   VOP_MODULE_SET(vop2, vp, dsp_lut_en, 1);
   vop2_write_reg_uncached(vop2, &vp->regs->gamma_update_en, 1);
   vp->gamma_lut_active = true;
 
   spin_unlock(&vop2->reg_lock);
}
 
static void vop2_crtc_load_lut(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
 
   if (!vop2->is_enabled || !vp->lut || !vop2->lut_regs)
       return;
 
   if (WARN_ON(!drm_modeset_is_locked(&crtc->mutex)))
       return;
 
   if (vop2->version == VOP_VERSION_RK3568) {
       rk3568_crtc_load_lut(crtc);
   } else {
       struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
       const struct vop2_video_port_data *vp_data = &vop2->data->vp[vp->id];
       struct vop2_video_port *splice_vp = &vop2->vps[vp_data->splice_vp_id];
 
       rk3588_crtc_load_lut(&vp->rockchip_crtc.crtc, vp->lut);
       if (vcstate->splice_mode)
           rk3588_crtc_load_lut(&splice_vp->rockchip_crtc.crtc, vp->lut);
   }
}
 
static void rockchip_vop2_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red,
                       u16 green, u16 blue, int regno)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   u32 lut_len = vp->gamma_lut_len;
   u32 r, g, b;
 
   if (regno >= lut_len || !vp->lut)
       return;
 
   r = red * (lut_len - 1) / 0xffff;
   g = green * (lut_len - 1) / 0xffff;
   b = blue * (lut_len - 1) / 0xffff;
   vp->lut[regno] = b * lut_len * lut_len + g * lut_len + r;
}
 
static void rockchip_vop2_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red,
                      u16 *green, u16 *blue, int regno)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   u32 lut_len = vp->gamma_lut_len;
   u32 r, g, b;
 
   if (regno >= lut_len || !vp->lut)
       return;
 
   b = (vp->lut[regno] / lut_len / lut_len) & (lut_len - 1);
   g = (vp->lut[regno] / lut_len) & (lut_len - 1);
   r = vp->lut[regno] & (lut_len - 1);
   *red = r * 0xffff / (lut_len - 1);
   *green = g * 0xffff / (lut_len - 1);
   *blue = b * 0xffff / (lut_len - 1);
}
 
static int vop2_crtc_legacy_gamma_set(struct drm_crtc *crtc, u16 *red,
                     u16 *green, u16 *blue, uint32_t size,
                     struct drm_modeset_acquire_ctx *ctx)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   int i;
 
   if (!vp->lut)
       return -EINVAL;
 
   if (size > vp->gamma_lut_len) {
       DRM_ERROR("gamma size[%d] out of video port%d gamma lut len[%d]\n",
             size, vp->id, vp->gamma_lut_len);
       return -ENOMEM;
   }
   for (i = 0; i < size; i++)
       rockchip_vop2_crtc_fb_gamma_set(crtc, red[i], green[i],
                       blue[i], i);
   vop2_crtc_load_lut(crtc);
   vop2_cfg_done(crtc);
   /*
    * maybe appear the following case:
    * -> set gamma
    * -> config done
    * -> atomic commit
    *  --> update win format
    *  --> update win address
    *  ---> here maybe meet vop hardware frame start, and triggle some config take affect.
    *  ---> as only some config take affect, this maybe lead to iommu pagefault.
    *  --> update win size
    *  --> update win other parameters
    * -> config done
    *
    * so we add vop2_wait_for_fs_by_done_bit_status() to make sure the first config done take
    * effect and then to do next frame config.
    */
   if (VOP_MODULE_GET(vop2, vp, standby) == 0)
       vop2_wait_for_fs_by_done_bit_status(vp);
 
   return 0;
}
 
static int vop2_crtc_atomic_gamma_set(struct drm_crtc *crtc,
                     struct drm_crtc_state *old_state)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct drm_color_lut *lut = vp->gamma_lut;
   unsigned int i;
 
   for (i = 0; i < vp->gamma_lut_len; i++)
       rockchip_vop2_crtc_fb_gamma_set(crtc, lut[i].red, lut[i].green,
                       lut[i].blue, i);
   vop2_crtc_load_lut(crtc);
 
   return 0;
}
 
static int vop2_crtc_atomic_cubic_lut_set(struct drm_crtc *crtc,
                     struct drm_crtc_state *old_state)
{
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct rockchip_drm_private *private = crtc->dev->dev_private;
   struct drm_color_lut *lut = vp->cubic_lut;
   struct vop2 *vop2 = vp->vop2;
   u32 *cubic_lut_kvaddr;
   dma_addr_t cubic_lut_mst;
   unsigned int i;
 
   if (!vp->cubic_lut_len) {
       DRM_ERROR("Video Port%d unsupported 3D lut\n", vp->id);
       return -ENODEV;
   }
 
   if (!private->cubic_lut[vp->id].enable) {
       if (!vp->cubic_lut_gem_obj) {
           size_t size = (vp->cubic_lut_len + 1) / 2 * 16;
 
           vp->cubic_lut_gem_obj = rockchip_gem_create_object(crtc->dev, size, true, 0);
           if (IS_ERR(vp->cubic_lut_gem_obj))
               return -ENOMEM;
       }
 
       cubic_lut_kvaddr = (u32 *)vp->cubic_lut_gem_obj->kvaddr;
       cubic_lut_mst = vp->cubic_lut_gem_obj->dma_addr;
   } else {
       cubic_lut_kvaddr = private->cubic_lut[vp->id].offset + private->cubic_lut_kvaddr;
       cubic_lut_mst = private->cubic_lut[vp->id].offset + private->cubic_lut_dma_addr;
   }
 
   for (i = 0; i < vp->cubic_lut_len / 2; i++) {
       *cubic_lut_kvaddr++ = (lut[2 * i].red & 0xfff) +
                   ((lut[2 * i].green & 0xfff) << 12) +
                   ((lut[2 * i].blue & 0xff) << 24);
       *cubic_lut_kvaddr++ = ((lut[2 * i].blue & 0xf00) >> 8) +
                   ((lut[2 * i + 1].red & 0xfff) << 4) +
                   ((lut[2 * i + 1].green & 0xfff) << 16) +
                   ((lut[2 * i + 1].blue & 0xf) << 28);
       *cubic_lut_kvaddr++ = (lut[2 * i + 1].blue & 0xff0) >> 4;
       *cubic_lut_kvaddr++ = 0;
   }
 
   if (vp->cubic_lut_len % 2) {
       *cubic_lut_kvaddr++ = (lut[2 * i].red & 0xfff) +
                   ((lut[2 * i].green & 0xfff) << 12) +
                   ((lut[2 * i].blue & 0xff) << 24);
       *cubic_lut_kvaddr++ = (lut[2 * i].blue & 0xf00) >> 8;
       *cubic_lut_kvaddr++ = 0;
       *cubic_lut_kvaddr = 0;
   }
 
   VOP_MODULE_SET(vop2, vp, lut_dma_rid, vp->lut_dma_rid);
   VOP_MODULE_SET(vop2, vp, cubic_lut_mst, cubic_lut_mst);
   VOP_MODULE_SET(vop2, vp, cubic_lut_update_en, 1);
   VOP_MODULE_SET(vop2, vp, cubic_lut_en, 1);
   VOP_CTRL_SET(vop2, lut_dma_en, 1);
 
   if (vcstate->splice_mode) {
       const struct vop2_video_port_data *vp_data = &vop2->data->vp[vp->id];
       struct vop2_video_port *splice_vp = &vop2->vps[vp_data->splice_vp_id];
 
       VOP_MODULE_SET(vop2, splice_vp, cubic_lut_mst, cubic_lut_mst);
       VOP_MODULE_SET(vop2, splice_vp, cubic_lut_update_en, 1);
       VOP_MODULE_SET(vop2, splice_vp, cubic_lut_en, 1);
   }
 
   return 0;
}
 
static void vop2_attach_cubic_lut_prop(struct drm_crtc *crtc, unsigned int cubic_lut_size)
{
   struct rockchip_drm_private *private = crtc->dev->dev_private;
 
   drm_object_attach_property(&crtc->base, private->cubic_lut_prop, 0);
   drm_object_attach_property(&crtc->base, private->cubic_lut_size_prop, cubic_lut_size);
}
 
static void vop2_cubic_lut_init(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data;
   struct vop2_video_port *vp;
   struct drm_crtc *crtc;
   int i;
 
   for (i = 0; i < vop2_data->nr_vps; i++) {
       vp = &vop2->vps[i];
       crtc = &vp->rockchip_crtc.crtc;
       if (!crtc->dev)
           continue;
       vp_data = &vop2_data->vp[vp->id];
       vp->cubic_lut_len = vp_data->cubic_lut_len;
 
       if (vp->cubic_lut_len)
           vop2_attach_cubic_lut_prop(crtc, vp->cubic_lut_len);
   }
}
 
static int vop2_core_clks_prepare_enable(struct vop2 *vop2)
{
   int ret;
 
   ret = clk_prepare_enable(vop2->hclk);
   if (ret < 0) {
       dev_err(vop2->dev, "failed to enable hclk - %d\n", ret);
       return ret;
   }
 
   ret = clk_prepare_enable(vop2->aclk);
   if (ret < 0) {
       dev_err(vop2->dev, "failed to enable aclk - %d\n", ret);
       goto err;
   }
 
   ret = clk_prepare_enable(vop2->pclk);
   if (ret < 0) {
       dev_err(vop2->dev, "failed to enable pclk - %d\n", ret);
       goto err1;
   }
 
   return 0;
err1:
   clk_disable_unprepare(vop2->aclk);
err:
   clk_disable_unprepare(vop2->hclk);
 
   return ret;
}
 
/*
 * VOP2 architecture
 *
 +----------+   +-------------+
 |  Cluster |   | Sel 1 from 6
 |  window0 |   |    Layer0   |              +---------------+    +-------------+    +-----------+
 +----------+   +-------------+              |N from 6 layers|    |             |    | 1 from 3  |
 +----------+   +-------------+              |   Overlay0    |    | Video Port0 |    |    RGB    |
 |  Cluster |   | Sel 1 from 6|              |               |    |             |    +-----------+
 |  window1 |   |    Layer1   |              +---------------+    +-------------+
 +----------+   +-------------+                                                      +-----------+
 +----------+   +-------------+                               +-->                   | 1 from 3  |
 |  Esmart  |   | Sel 1 from 6|              +---------------+    +-------------+    |   LVDS    |
 |  window0 |   |   Layer2    |              |N from 6 Layers     |             |    +-----------+
 +----------+   +-------------+              |   Overlay1    +    | Video Port1 | +--->
 +----------+   +-------------+   -------->  |               |    |             |    +-----------+
 |  Esmart  |   | Sel 1 from 6|   -------->  +---------------+    +-------------+    | 1 from 3  |
 |  Window1 |   |   Layer3    |                               +-->                   |   MIPI    |
 +----------+   +-------------+                                                      +-----------+
 +----------+   +-------------+              +---------------+    +-------------+
 |  Smart   |   | Sel 1 from 6|              |N from 6 Layers|    |             |    +-----------+
 |  Window0 |   |    Layer4   |              |   Overlay2    |    | Video Port2 |    | 1 from 3  |
 +----------+   +-------------+              |               |    |             |    |   HDMI    |
 +----------+   +-------------+              +---------------+    +-------------+    +-----------+
 |  Smart   |   | Sel 1 from 6|                                                      +-----------+
 |  Window1 |   |    Layer5   |                                                      |  1 from 3 |
 +----------+   +-------------+                                                      |    eDP    |
 *                                                                                   +-----------+
 */
static void vop3_layer_map_initial(struct vop2 *vop2, uint32_t current_vp_id)
{
   uint16_t vp_id;
   struct drm_plane *plane = NULL;
 
   drm_for_each_plane(plane, vop2->drm_dev) {
       struct vop2_win *win = to_vop2_win(plane);
 
       vp_id = VOP_CTRL_GET(vop2, win_vp_id[win->phys_id]);
       win->vp_mask = BIT(vp_id);
       win->old_vp_mask = win->vp_mask;
       vop2->vps[vp_id].win_mask |= BIT(win->phys_id);
   }
}
 
static void vop2_layer_map_initial(struct vop2 *vop2, uint32_t current_vp_id)
{
   struct vop2_layer *layer;
   struct vop2_video_port *vp;
   struct vop2_win *win;
   unsigned long win_mask;
   uint32_t used_layers = 0;
   uint16_t port_mux_cfg = 0;
   uint16_t port_mux;
   uint16_t vp_id;
   uint8_t nr_layers;
   int phys_id;
   int i, j;
 
   if (is_vop3(vop2)) {
       vop3_layer_map_initial(vop2, current_vp_id);
       return;
   }
 
   for (i = 0; i < vop2->data->nr_vps; i++) {
       vp_id = i;
       j = 0;
       vp = &vop2->vps[vp_id];
       vp->win_mask = vp->plane_mask;
       nr_layers = hweight32(vp->win_mask);
       win_mask = vp->win_mask;
       for_each_set_bit(phys_id, &win_mask, ROCKCHIP_MAX_LAYER) {
           layer = &vop2->layers[used_layers + j];
           win = vop2_find_win_by_phys_id(vop2, phys_id);
           VOP_CTRL_SET(vop2, win_vp_id[phys_id], vp_id);
           VOP_MODULE_SET(vop2, layer, layer_sel, win->layer_sel_id[vp_id]);
           win->vp_mask = BIT(i);
           win->old_vp_mask = win->vp_mask;
           layer->win_phys_id = win->phys_id;
           win->layer_id = layer->id;
           j++;
           DRM_DEV_DEBUG(vop2->dev, "layer%d select %s for vp%d phys_id: %d\n",
                     layer->id, win->name, vp_id, phys_id);
       }
       used_layers += nr_layers;
   }
 
   /*
    * The last Video Port(VP2 for RK3568, VP3 for RK3588) is fixed
    * at the last level of the all the mixers by hardware design,
    * so we just need to handle (nr_vps - 1) vps here.
    */
   used_layers = 0;
   for (i = 0; i < vop2->data->nr_vps - 1; i++) {
       vp = &vop2->vps[i];
       used_layers += hweight32(vp->win_mask);
       if (used_layers == 0)
           port_mux = 8;
       else
           port_mux = used_layers - 1;
       port_mux_cfg |= port_mux << (vp->id * 4);
   }
 
   /* the last VP is fixed */
   if (vop2->data->nr_vps >= 1)
       port_mux_cfg |= 7 << (4 * (vop2->data->nr_vps - 1));
   vop2->port_mux_cfg = port_mux_cfg;
   VOP_CTRL_SET(vop2, ovl_port_mux_cfg, port_mux_cfg);
 
}
 
static void rk3588_vop2_regsbak(struct vop2 *vop2)
{
   uint32_t *base = vop2->regs;
   int i;
 
   /*
    * No need to backup DSC/GAMMA_LUT/BPP_LUT/MMU
    */
   for (i = 0; i < (0x2000 >> 2); i++)
       vop2->regsbak[i] = base[i];
}
 
static void vop2_initial(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   uint32_t current_vp_id = vp->id;
   struct vop2_wb *wb = &vop2->wb;
   int ret;
 
   if (vop2->enable_count == 0) {
       ret = pm_runtime_get_sync(vop2->dev);
       if (ret < 0) {
           DRM_DEV_ERROR(vop2->dev, "failed to get pm runtime: %d\n", ret);
           return;
       }
 
       ret = vop2_core_clks_prepare_enable(vop2);
       if (ret) {
           pm_runtime_put_sync(vop2->dev);
           return;
       }
 
       if (vop2_soc_is_rk3566())
           VOP_CTRL_SET(vop2, otp_en, 1);
 
       /*
        * rk3588 don't support access mmio by memcpy
        */
       if (vop2->version == VOP_VERSION_RK3588)
           rk3588_vop2_regsbak(vop2);
       else
           memcpy(vop2->regsbak, vop2->regs, vop2->len);
 
       VOP_MODULE_SET(vop2, wb, axi_yrgb_id, 0xd);
       VOP_MODULE_SET(vop2, wb, axi_uv_id, 0xe);
       vop2_wb_cfg_done(vp);
 
       if (is_vop3(vop2)) {
           VOP_CTRL_SET(vop2, dsp_vs_t_sel, 0);
           VOP_CTRL_SET(vop2, esmart_lb_mode, vop2->esmart_lb_mode);
       }
 
       /*
        * This is unused and error init value for rk3528/rk3562 vp1, if less of this config,
        * vp1 can't display normally.
        */
       if (vop2->version == VOP_VERSION_RK3528 || vop2->version == VOP_VERSION_RK3562)
           vop2_mask_write(vop2, 0x700, 0x3, 4, 0, 0, true);
 
       VOP_CTRL_SET(vop2, cfg_done_en, 1);
       /*
        * Disable auto gating, this is a workaround to
        * avoid display image shift when a window enabled.
        */
       VOP_CTRL_SET(vop2, auto_gating_en, 0);
 
       VOP_CTRL_SET(vop2, aclk_pre_auto_gating_en, 0);
 
       /*
        * Register OVERLAY_LAYER_SEL and OVERLAY_PORT_SEL should take effect immediately,
        * than windows configuration(CLUSTER/ESMART/SMART) can take effect according the
        * video port mux configuration as we wished.
        */
       VOP_CTRL_SET(vop2, ovl_port_mux_cfg_done_imd, 1);
       /*
        * Let SYS_DSP_INFACE_EN/SYS_DSP_INFACE_CTRL/SYS_DSP_INFACE_POL take effect
        * immediately.
        */
       VOP_CTRL_SET(vop2, if_ctrl_cfg_done_imd, 1);
 
       /* Close dynamic turn on/off rk3588 PD_ESMART and keep esmart pd on when enable */
       if (vop2->version == VOP_VERSION_RK3588) {
           struct vop2_power_domain *esmart_pd = vop2_find_pd_by_id(vop2, VOP2_PD_ESMART);
 
           if (vop2_power_domain_status(esmart_pd))
               esmart_pd->on = true;
           else
               vop2_power_domain_on(esmart_pd);
       }
       vop2_layer_map_initial(vop2, current_vp_id);
       vop2_axi_irqs_enable(vop2);
       vop2->is_enabled = true;
   }
 
   vop2_debug_irq_enable(crtc);
 
   vop2->enable_count++;
 
   ret = clk_prepare_enable(vp->dclk);
   if (ret < 0)
       DRM_DEV_ERROR(vop2->dev, "failed to enable dclk for video port%d - %d\n",
                 vp->id, ret);
}
 
/*
 * The internal PD of VOP2 on rk3588 take effect immediately
 * for power up and take effect by vsync for power down.
 *
 * And the PD_CLUSTER0 is a parent PD of PD_CLUSTER1/2/3,
 * we may have this use case:
 * Cluster0 is attached to VP0 for HDMI output,
 * Cluster1 is attached to VP1 for MIPI DSI,
 
 * When we enable Cluster1 on VP1, we should enable PD_CLUSTER0 as
 * it is the parent PD, event though HDMI is plugout, VP1 is disabled,
 * the PD of Cluster0 should keep power on.
 
 * When system go to suspend:
 * (1) Power down PD of Cluster1 before VP1 standby(the power down is take
 *     effect by vsync)
 * (2) Power down PD of Cluster0
 *
 * But we have problem at step (2), Cluster0 is attached to VP0. but VP0
 * is in standby mode, as it is never used or hdmi plugout. So there is
 * no vsync, the power down will never take effect.
 
 * According to IC designer: We must power down all internal PD of VOP
 * before we power down the global PD_VOP.
 
 * So we get this workaround:
 * If we found a VP is in standby mode when we want power down a PD is
 * attached to it, we release the VP from standby mode, than it will
 * run a default timing and generate vsync. Than we can power down the
 * PD by this vsync. After all this is done, we standby the VP at last.
 */
static void vop2_power_domain_off_by_disabled_vp(struct vop2_power_domain *pd)
{
   struct vop2_video_port *vp = NULL;
   struct vop2 *vop2 = pd->vop2;
   struct vop2_win *win;
   struct drm_crtc *crtc;
   uint32_t vp_id;
   uint8_t phys_id;
   int ret;
 
   if (pd->data->id == VOP2_PD_CLUSTER0 || pd->data->id == VOP2_PD_CLUSTER1 ||
       pd->data->id == VOP2_PD_CLUSTER2 || pd->data->id == VOP2_PD_CLUSTER3 ||
       pd->data->id == VOP2_PD_ESMART) {
       phys_id = ffs(pd->data->module_id_mask) - 1;
       win = vop2_find_win_by_phys_id(vop2, phys_id);
       vp_id = ffs(win->vp_mask) - 1;
       vp = &vop2->vps[vp_id];
   } else {
       DRM_DEV_ERROR(vop2->dev, "unexpected power on pd%d\n", ffs(pd->data->id) - 1);
   }
 
   if (vp) {
       ret = clk_prepare_enable(vp->dclk);
       if (ret < 0)
           DRM_DEV_ERROR(vop2->dev, "failed to enable dclk for video port%d - %d\n",
                     vp->id, ret);
       crtc = &vp->rockchip_crtc.crtc;
       VOP_MODULE_SET(vop2, vp, standby, 0);
       vop2_power_domain_off(pd);
       vop2_cfg_done(crtc);
       vop2_wait_power_domain_off(pd);
 
       reinit_completion(&vp->dsp_hold_completion);
       vop2_dsp_hold_valid_irq_enable(crtc);
       VOP_MODULE_SET(vop2, vp, standby, 1);
       ret = wait_for_completion_timeout(&vp->dsp_hold_completion, msecs_to_jiffies(50));
       if (!ret)
           DRM_DEV_INFO(vop2->dev, "wait for vp%d dsp_hold timeout\n", vp->id);
 
       vop2_dsp_hold_valid_irq_disable(crtc);
       clk_disable_unprepare(vp->dclk);
   }
}
 
static void vop2_power_off_all_pd(struct vop2 *vop2)
{
   struct vop2_power_domain *pd, *n;
 
   list_for_each_entry_safe_reverse(pd, n, &vop2->pd_list_head, list) {
       if (vop2_power_domain_status(pd))
           vop2_power_domain_off_by_disabled_vp(pd);
       pd->on = false;
       pd->vp_mask = 0;
   }
}
 
static void vop2_disable(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
 
   clk_disable_unprepare(vp->dclk);
 
   if (--vop2->enable_count > 0)
       return;
 
   if (vop2->is_iommu_enabled) {
       /*
        * vop2 standby complete, so iommu detach is safe.
        */
       VOP_CTRL_SET(vop2, dma_stop, 1);
       rockchip_drm_dma_detach_device(vop2->drm_dev, vop2->dev);
       vop2->is_iommu_enabled = false;
   }
   if (vop2->version == VOP_VERSION_RK3588)
       vop2_power_off_all_pd(vop2);
 
   vop2->is_enabled = false;
   pm_runtime_put_sync(vop2->dev);
 
   clk_disable_unprepare(vop2->pclk);
   clk_disable_unprepare(vop2->aclk);
   clk_disable_unprepare(vop2->hclk);
}
 
static void vop2_crtc_disable_dsc(struct vop2 *vop2, u8 dsc_id)
{
   struct vop2_dsc *dsc = &vop2->dscs[dsc_id];
 
   VOP_MODULE_SET(vop2, dsc, dsc_mer, 1);
   VOP_MODULE_SET(vop2, dsc, dsc_interface_mode, 0);
   VOP_MODULE_SET(vop2, dsc, dsc_en, 0);
   VOP_MODULE_SET(vop2, dsc, rst_deassert, 0);
}
 
static struct vop2_clk *vop2_clk_get(struct vop2 *vop2, const char *name)
{
   struct vop2_clk *clk, *n;
 
   if (!name)
       return NULL;
 
   list_for_each_entry_safe(clk, n, &vop2->clk_list_head, list) {
       if (!strcmp(clk_hw_get_name(&clk->hw), name))
           return clk;
   }
 
   return NULL;
}
 
static void vop2_clk_set_parent(struct clk *clk, struct clk *parent)
{
   int ret = 0;
 
   if (parent)
       ret = clk_set_parent(clk, parent);
   if (ret < 0)
       DRM_WARN("failed to set %s as parent for %s\n",
            __clk_get_name(parent), __clk_get_name(clk));
}
 
static int vop2_extend_clk_init(struct vop2 *vop2)
{
   const char * const extend_clk_name[] = {
       "hdmi0_phy_pll", "hdmi1_phy_pll"};
   struct drm_device *drm_dev = vop2->drm_dev;
   struct clk *clk;
   struct vop2_extend_pll *extend_pll;
   int i;
 
   INIT_LIST_HEAD(&vop2->extend_clk_list_head);
 
   if (vop2->version != VOP_VERSION_RK3588)
       return 0;
 
   for (i = 0; i < ARRAY_SIZE(extend_clk_name); i++) {
       clk = devm_clk_get_optional(drm_dev->dev, extend_clk_name[i]);
       if (IS_ERR(clk)) {
           dev_warn(drm_dev->dev, "failed to get %s: %ld\n",
                extend_clk_name[i], PTR_ERR(clk));
           continue;
       }
 
       if (!clk)
           continue;
 
       extend_pll = devm_kzalloc(drm_dev->dev, sizeof(*extend_pll), GFP_KERNEL);
       if (!extend_pll)
           return -ENOMEM;
 
       extend_pll->clk = clk;
       extend_pll->vp_mask = 0;
       strncpy(extend_pll->clk_name, extend_clk_name[i], sizeof(extend_pll->clk_name));
       list_add_tail(&extend_pll->list, &vop2->extend_clk_list_head);
   }
 
   return 0;
}
 
static struct vop2_extend_pll *vop2_extend_clk_find_by_name(struct vop2 *vop2, char *clk_name)
{
   struct vop2_extend_pll *extend_pll;
 
   list_for_each_entry(extend_pll, &vop2->extend_clk_list_head, list) {
       if (!strcmp(extend_pll->clk_name, clk_name))
           return extend_pll;
   }
 
   return NULL;
}
 
static int vop2_extend_clk_switch_pll(struct vop2 *vop2, struct vop2_extend_pll *src,
                     struct vop2_extend_pll *dst)
{
   struct vop2_clk *dclk;
   u32 vp_mask;
   int i = 0;
   char clk_name[32];
 
   if (!src->vp_mask)
       return -EINVAL;
 
   if (dst->vp_mask)
       return -EBUSY;
 
   vp_mask = src->vp_mask;
 
   while (vp_mask) {
       if ((BIT(i) & src->vp_mask)) {
           snprintf(clk_name, sizeof(clk_name), "dclk%d", i);
           dclk = vop2_clk_get(vop2, clk_name);
           clk_set_rate(dst->clk, dclk->rate);
           vop2_clk_set_parent(vop2->vps[i].dclk, dst->clk);
           src->vp_mask &= ~BIT(i);
           dst->vp_mask |= BIT(i);
       }
       i++;
       vp_mask  = vp_mask >> 1;
   }
 
   return 0;
}
 
static inline int vop2_extend_clk_get_vp_id(struct vop2_extend_pll *ext_pll)
{
   return ffs(ext_pll->vp_mask) - 1;
}
 
/*
 * Here are 2 hdmi phy pll can use for video port dclk. The strategies of how to use hdmi phy pll
 * as follow:
 *
 * 1. hdmi phy pll can be used for video port0/1/2 when output format under 4K@60Hz;
 *
 * 2. When a video port connect both hdmi0 and hdmi1(may also connect other output interface),
 *    it must hold the hdmi0 and hdmi1 phy pll, and other video port can't use it. if request dclk
 *    is under 4K@60Hz, set the video port dlk parent as hdmi0 phy pll.if hdmi0 or hdmi1 phy pll
 *    is used by other video port, report a error.
 *
 * 3. When a video port(A) connect hdmi0(may also connect other output interface but not hdmi1),
 *    it must hold the hdmi0 phy pll, and other video port can't use it. If both hdmi0 and hdmi1
 *    phy pll is used by other video port, report a error. If hdmi0 phy pll is used by another
 *    video port(B) and hdmi1 phy pll is free, set hdmi1 phy pll as video port(B) dclk parent and
 *    video port(A) hold hdmi0 phy pll. If hdmi0 phy pll is free, video port(A) hold hdmi0 pll.If
 *    video port(A) hold hdmi0 phy pll and request dclk is under 4k@60Hz, set hdmi0 phy pll as
 *    video port(A) dclk parent.
 *
 * 4. When a video port(A) connect hdmi1(may also connect other output interface but not hdmi0),
 *    it must hold the hdmi1 phy pll, and other video port can't use it. If both hdmi0 and hdmi1
 *    phy pll is used by other video port, report a error. If hdmi1 phy pll is used by another
 *    video port(B) and hdmi0 phy pll is free, set hdmi0 phy pll as video port(B) dclk parent and
 *    video port(A) hold hdmi1 phy pll. If hdmi1 phy pll is free, video port(A) hold hdmi1 pll. If
 *    video port(A) hold hdmi1 phy pll and request dclk is under 4k@60Hz, set hdmi1 phy pll as
 *    video port(A) dclk parent.
 *
 * 5. When a video port connect dp(0, 1, or both, may also connect other output type but not hdmi0
 *    and hdmi1). If the request dclk is higher than 4K@60Hz or video port id is 2, do nothing.
 *    Otherwise get a free hdmi phy pll as video port dclk parent. If no free hdmi phy pll can be
 *    get, report a error.
 */
 
static int vop2_clk_set_parent_extend(struct vop2_video_port *vp,
                     struct rockchip_crtc_state *vcstate, bool enable)
{
   struct vop2 *vop2 = vp->vop2;
   struct vop2_extend_pll *hdmi0_phy_pll, *hdmi1_phy_pll;
   struct drm_crtc *crtc = &vp->rockchip_crtc.crtc;
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
 
   hdmi0_phy_pll = vop2_extend_clk_find_by_name(vop2, "hdmi0_phy_pll");
   hdmi1_phy_pll = vop2_extend_clk_find_by_name(vop2, "hdmi1_phy_pll");
 
   if (hdmi0_phy_pll)
       clk_get_rate(hdmi0_phy_pll->clk);
   if (hdmi1_phy_pll)
       clk_get_rate(hdmi1_phy_pll->clk);
 
   if ((!hdmi0_phy_pll && !hdmi1_phy_pll) ||
       ((vcstate->output_if & VOP_OUTPUT_IF_HDMI0) && !hdmi0_phy_pll) ||
       ((vcstate->output_if & VOP_OUTPUT_IF_HDMI1) && !hdmi1_phy_pll))
       return 0;
 
   if (enable) {
       if ((vcstate->output_if & VOP_OUTPUT_IF_HDMI0) &&
           (vcstate->output_if & VOP_OUTPUT_IF_HDMI1)) {
           if (hdmi0_phy_pll->vp_mask) {
               DRM_ERROR("hdmi0 phy pll is used by vp%d\n",
                     vop2_extend_clk_get_vp_id(hdmi0_phy_pll));
               return -EBUSY;
           }
 
           if (hdmi1_phy_pll->vp_mask) {
               DRM_ERROR("hdmi1 phy pll is used by vp%d\n",
                     vop2_extend_clk_get_vp_id(hdmi1_phy_pll));
               return -EBUSY;
           }
 
           if (adjusted_mode->crtc_clock > VOP2_MAX_DCLK_RATE)
               vop2_clk_set_parent(vp->dclk, vp->dclk_parent);
           else
               vop2_clk_set_parent(vp->dclk, hdmi0_phy_pll->clk);
 
           hdmi0_phy_pll->vp_mask |= BIT(vp->id);
           hdmi1_phy_pll->vp_mask |= BIT(vp->id);
       } else if ((vcstate->output_if & VOP_OUTPUT_IF_HDMI0) &&
              !(vcstate->output_if & VOP_OUTPUT_IF_HDMI1)) {
           if (hdmi0_phy_pll->vp_mask) {
               if (hdmi1_phy_pll) {
                   if (hdmi1_phy_pll->vp_mask) {
                       DRM_ERROR("hdmi0: phy pll is used by vp%d:vp%d\n",
                             vop2_extend_clk_get_vp_id(hdmi0_phy_pll),
                             vop2_extend_clk_get_vp_id(hdmi1_phy_pll));
                       return -EBUSY;
                   }
 
                   vop2_extend_clk_switch_pll(vop2, hdmi0_phy_pll,
                                  hdmi1_phy_pll);
               } else {
                   DRM_ERROR("hdmi0: phy pll is used by vp%d\n",
                         vop2_extend_clk_get_vp_id(hdmi0_phy_pll));
                   return -EBUSY;
               }
           }
 
           if (adjusted_mode->crtc_clock > VOP2_MAX_DCLK_RATE)
               vop2_clk_set_parent(vp->dclk, vp->dclk_parent);
           else
               vop2_clk_set_parent(vp->dclk, hdmi0_phy_pll->clk);
 
           hdmi0_phy_pll->vp_mask |= BIT(vp->id);
       } else if (!(vcstate->output_if & VOP_OUTPUT_IF_HDMI0) &&
              (vcstate->output_if & VOP_OUTPUT_IF_HDMI1)) {
           if (hdmi1_phy_pll->vp_mask) {
               if (hdmi0_phy_pll) {
                   if (hdmi0_phy_pll->vp_mask) {
                       DRM_ERROR("hdmi1: phy pll is used by vp%d:vp%d\n",
                             vop2_extend_clk_get_vp_id(hdmi0_phy_pll),
                             vop2_extend_clk_get_vp_id(hdmi1_phy_pll));
                       return -EBUSY;
                   }
 
                   vop2_extend_clk_switch_pll(vop2, hdmi1_phy_pll,
                                  hdmi0_phy_pll);
               } else {
                   DRM_ERROR("hdmi1: phy pll is used by vp%d\n",
                         vop2_extend_clk_get_vp_id(hdmi1_phy_pll));
                   return -EBUSY;
               }
           }
 
           if (adjusted_mode->crtc_clock > VOP2_MAX_DCLK_RATE)
               vop2_clk_set_parent(vp->dclk, vp->dclk_parent);
           else
               vop2_clk_set_parent(vp->dclk, hdmi1_phy_pll->clk);
 
           hdmi1_phy_pll->vp_mask |= BIT(vp->id);
       } else if (output_if_is_dp(vcstate->output_if)) {
           if (vp->id == 2) {
               vop2_clk_set_parent(vp->dclk, vp->dclk_parent);
               return 0;
           }
 
           if (hdmi0_phy_pll && !hdmi0_phy_pll->vp_mask) {
               vop2_clk_set_parent(vp->dclk, hdmi0_phy_pll->clk);
               hdmi0_phy_pll->vp_mask |= BIT(vp->id);
           } else if (hdmi1_phy_pll && !hdmi1_phy_pll->vp_mask) {
               vop2_clk_set_parent(vp->dclk, hdmi1_phy_pll->clk);
               hdmi1_phy_pll->vp_mask |= BIT(vp->id);
           } else {
               vop2_clk_set_parent(vp->dclk, vp->dclk_parent);
               DRM_INFO("No free hdmi phy pll for DP, use default parent\n");
           }
       }
   } else {
       if (hdmi0_phy_pll && (BIT(vp->id) & hdmi0_phy_pll->vp_mask))
           hdmi0_phy_pll->vp_mask &= ~BIT(vp->id);
 
       if (hdmi1_phy_pll && (BIT(vp->id) & hdmi1_phy_pll->vp_mask))
           hdmi1_phy_pll->vp_mask &= ~BIT(vp->id);
   }
 
   return 0;
}
 
static void vop2_crtc_atomic_enter_psr(struct drm_crtc *crtc, struct drm_crtc_state *old_state)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct vop2_win *win;
   unsigned long win_mask = vp->enabled_win_mask;
   int phys_id;
 
   for_each_set_bit(phys_id, &win_mask, ROCKCHIP_MAX_LAYER) {
       win = vop2_find_win_by_phys_id(vop2, phys_id);
       VOP_WIN_SET(vop2, win, enable, 0);
 
       if (win->feature & WIN_FEATURE_CLUSTER_MAIN)
           VOP_CLUSTER_SET(vop2, win, enable, 0);
   }
 
   vop2_cfg_done(crtc);
   vop2_wait_for_fs_by_done_bit_status(vp);
   drm_crtc_vblank_off(crtc);
   if (hweight8(vop2->active_vp_mask) == 1) {
       u32 adjust_aclk_rate = 0;
       u32 htotal = (VOP_MODULE_GET(vop2, vp, htotal_pw) >> 16) & 0xffff;
       u32 pre_scan_dly = VOP_MODULE_GET(vop2, vp, pre_scan_htiming);
       u32 pre_scan_hblank = pre_scan_dly & 0x1fff;
       u32 pre_scan_hactive = (pre_scan_dly >> 16) & 0x1fff;
       u32 dclk_rate = crtc->state->adjusted_mode.crtc_clock / 1000;
       /**
        * (pre_scan_hblank + pre_scan_hactive) x aclk_margin / adjust_aclk_rate = hotal / dclk_rate
        * aclk_margin = 1.2, so
        * adjust_aclk_rate = (pre_scan_hblank + pre_scan_hactive) x 1.2 * aclk_margin / htotal
        */
 
       adjust_aclk_rate = (pre_scan_hblank + pre_scan_hactive) * dclk_rate * 12 / 10 / htotal;
 
       vop2->aclk_rate = clk_get_rate(vop2->aclk);
       clk_set_rate(vop2->aclk, adjust_aclk_rate * 1000000L);
       vop2->aclk_rate_reset = true;
   }
}
 
static void vop2_crtc_atomic_exit_psr(struct drm_crtc *crtc, struct drm_crtc_state *old_state)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   u32 phys_id;
   struct vop2_win *win;
   unsigned long enabled_win_mask = vp->enabled_win_mask;
 
   drm_crtc_vblank_on(crtc);
   if (vop2->aclk_rate_reset)
       clk_set_rate(vop2->aclk, vop2->aclk_rate);
   vop2->aclk_rate_reset = false;
 
   for_each_set_bit(phys_id, &enabled_win_mask, ROCKCHIP_MAX_LAYER) {
       win = vop2_find_win_by_phys_id(vop2, phys_id);
       VOP_WIN_SET(vop2, win, enable, 1);
       if (win->feature & WIN_FEATURE_CLUSTER_MAIN)
           VOP_CLUSTER_SET(vop2, win, enable, 1);
   }
 
   vop2_cfg_done(crtc);
   vop2_wait_for_fs_by_done_bit_status(vp);
}
 
static void vop2_crtc_atomic_disable(struct drm_crtc *crtc,
                    struct drm_crtc_state *old_state)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_video_port_data *vp_data = &vop2->data->vp[vp->id];
   struct vop2_video_port *splice_vp = &vop2->vps[vp_data->splice_vp_id];
   bool dual_channel = !!(vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE);
   int ret;
 
   WARN_ON(vp->event);
 
   if (crtc->state->self_refresh_active) {
       vop2_crtc_atomic_enter_psr(crtc, old_state);
       goto out;
   }
 
   vop2_lock(vop2);
   DRM_DEV_INFO(vop2->dev, "Crtc atomic disable vp%d\n", vp->id);
   VOP_MODULE_SET(vop2, vp, almost_full_or_en, 0);
   VOP_MODULE_SET(vop2, vp, line_flag_or_en, 0);
   drm_crtc_vblank_off(crtc);
   if (vop2->dscs[vcstate->dsc_id].enabled &&
       vop2->dscs[vcstate->dsc_id].attach_vp_id == vp->id &&
       vop2->data->nr_dscs) {
       if (dual_channel) {
           vop2_crtc_disable_dsc(vop2, 0);
           vop2_crtc_disable_dsc(vop2, 1);
       } else {
           vop2_crtc_disable_dsc(vop2, vcstate->dsc_id);
       }
   }
 
   if (vp->cubic_lut) {
       VOP_MODULE_SET(vop2, vp, cubic_lut_update_en, 0);
       VOP_MODULE_SET(vop2, vp, cubic_lut_en, 0);
   }
 
   if (vp_data->feature & VOP_FEATURE_VIVID_HDR)
       VOP_MODULE_SET(vop2, vp, hdr_lut_update_en, 0);
   vop2_disable_all_planes_for_crtc(crtc);
 
   if (vop2->dscs[vcstate->dsc_id].enabled &&
       vop2->dscs[vcstate->dsc_id].attach_vp_id == vp->id &&
       vop2->data->nr_dscs && vop2->dscs[vcstate->dsc_id].pd) {
       if (dual_channel) {
           vop2_power_domain_put(vop2->dscs[0].pd);
           vop2_power_domain_put(vop2->dscs[1].pd);
           vop2->dscs[0].pd->vp_mask = 0;
           vop2->dscs[1].pd->vp_mask = 0;
           vop2->dscs[0].attach_vp_id = -1;
           vop2->dscs[1].attach_vp_id = -1;
       } else {
           vop2_power_domain_put(vop2->dscs[vcstate->dsc_id].pd);
           vop2->dscs[vcstate->dsc_id].pd->vp_mask = 0;
           vop2->dscs[vcstate->dsc_id].attach_vp_id = -1;
       }
       vop2->dscs[vcstate->dsc_id].enabled = false;
       vcstate->dsc_enable = false;
   }
 
   if (vp->output_if & VOP_OUTPUT_IF_eDP0)
       VOP_GRF_SET(vop2, grf, grf_edp0_en, 0);
 
   if (vp->output_if & VOP_OUTPUT_IF_eDP1) {
       VOP_GRF_SET(vop2, grf, grf_edp1_en, 0);
       if (dual_channel)
           VOP_CTRL_SET(vop2, edp_dual_en, 0);
   }
 
   if (vp->output_if & VOP_OUTPUT_IF_HDMI0) {
       VOP_GRF_SET(vop2, grf, grf_hdmi0_dsc_en, 0);
       VOP_GRF_SET(vop2, grf, grf_hdmi0_en, 0);
   }
 
   if (vp->output_if & VOP_OUTPUT_IF_HDMI1) {
       VOP_GRF_SET(vop2, grf, grf_hdmi1_dsc_en, 0);
       VOP_GRF_SET(vop2, grf, grf_hdmi1_en, 0);
       if (dual_channel)
           VOP_CTRL_SET(vop2, hdmi_dual_en, 0);
   }
 
   if ((vcstate->output_if & VOP_OUTPUT_IF_DP1) && dual_channel)
       VOP_CTRL_SET(vop2, dp_dual_en, 0);
 
   if ((vcstate->output_if & VOP_OUTPUT_IF_MIPI1) && dual_channel)
       VOP_CTRL_SET(vop2, mipi_dual_en, 0);
 
   VOP_MODULE_SET(vop2, vp, dual_channel_en, 0);
   VOP_MODULE_SET(vop2, vp, dual_channel_swap, 0);
 
   vp->output_if = 0;
 
   vop2_clk_set_parent_extend(vp, vcstate, false);
   /*
    * Vop standby will take effect at end of current frame,
    * if dsp hold valid irq happen, it means standby complete.
    *
    * we must wait standby complete when we want to disable aclk,
    * if not, memory bus maybe dead.
    */
   reinit_completion(&vp->dsp_hold_completion);
   vop2_dsp_hold_valid_irq_enable(crtc);
 
   spin_lock(&vop2->reg_lock);
 
   VOP_MODULE_SET(vop2, vp, splice_en, 0);
 
   VOP_MODULE_SET(vop2, vp, standby, 1);
 
   spin_unlock(&vop2->reg_lock);
 
   ret = wait_for_completion_timeout(&vp->dsp_hold_completion, msecs_to_jiffies(50));
   if (!ret)
       DRM_DEV_INFO(vop2->dev, "wait for vp%d dsp_hold timeout\n", vp->id);
 
   vop2_dsp_hold_valid_irq_disable(crtc);
 
   vop2_disable(crtc);
 
   vop2->active_vp_mask &= ~BIT(vp->id);
   if (vcstate->splice_mode)
       vop2->active_vp_mask &= ~BIT(splice_vp->id);
   vcstate->splice_mode = false;
   vcstate->output_flags = 0;
   vp->splice_mode_right = false;
   vp->loader_protect = false;
   splice_vp->splice_mode_right = false;
   memset(&vp->active_tv_state, 0, sizeof(vp->active_tv_state));
   vop2_unlock(vop2);
 
   vop2_set_system_status(vop2);
 
out:
   if (crtc->state->event && !crtc->state->active) {
       spin_lock_irq(&crtc->dev->event_lock);
       drm_crtc_send_vblank_event(crtc, crtc->state->event);
       spin_unlock_irq(&crtc->dev->event_lock);
 
       crtc->state->event = NULL;
   }
}
 
static int vop2_cluster_two_win_mode_check(struct drm_plane_state *pstate)
{
   struct drm_atomic_state *state = pstate->state;
   struct drm_plane *plane = pstate->plane;
   struct vop2_win *win = to_vop2_win(plane);
   struct vop2 *vop2 = win->vop2;
   struct vop2_win *main_win = vop2_find_win_by_phys_id(vop2, win->phys_id);
   struct drm_plane_state *main_pstate;
   int actual_w = drm_rect_width(&pstate->src) >> 16;
   int xoffset;
 
   if (pstate->fb->modifier == DRM_FORMAT_MOD_LINEAR)
       xoffset = 0;
   else
       xoffset = pstate->src.x1 >> 16;
 
   if ((actual_w + xoffset % 16) > 2048) {
       DRM_ERROR("%s act_w(%d) + xoffset(%d) / 16  << 2048 in two win mode\n",
                 win->name, actual_w, xoffset);
       return -EINVAL;
   }
 
   main_pstate = drm_atomic_get_new_plane_state(state, &main_win->base);
 
   if (pstate->fb->modifier != main_pstate->fb->modifier) {
       DRM_ERROR("%s(fb->modifier: 0x%llx) must use same data layout as %s(fb->modifier: 0x%llx)\n",
               win->name, pstate->fb->modifier, main_win->name, main_pstate->fb->modifier);
       return -EINVAL;
   }
 
   if (main_pstate->fb->modifier == DRM_FORMAT_MOD_LINEAR)
       xoffset = 0;
   else
       xoffset = main_pstate->src.x1 >> 16;
   actual_w = drm_rect_width(&main_pstate->src) >> 16;
 
   if ((actual_w + xoffset % 16) > 2048) {
       DRM_ERROR("%s act_w(%d) + xoffset(%d) / 16  << 2048 in two win mode\n",
                 main_win->name, actual_w, xoffset);
       return -EINVAL;
   }
 
   return 0;
}
 
static int vop2_cluter_splice_scale_check(struct vop2_win *win, struct drm_plane_state *pstate,
                     u16 hdisplay)
{
   struct drm_rect src = drm_plane_state_src(pstate);
   struct drm_rect dst = drm_plane_state_dest(pstate);
   u16 half_hdisplay = hdisplay >> 1;
 
   /* scale up is ok */
   if ((drm_rect_width(&src) >> 16) <= drm_rect_width(&dst))
       return 0;
 
   if ((drm_rect_width(&src) >> 16) <= VOP2_MAX_VP_OUTPUT_WIDTH)
       return 0;
   /*
    * Cluster scale down limitation in splice mode:
    * If scale down, must display at horizontal center
    */
   if ((dst.x1 < half_hdisplay) && (dst.x2 > half_hdisplay)) {
       if ((dst.x2 + dst.x1) != hdisplay) {
           DRM_ERROR("%s src_w: %d dst_w %d dst(%d %d) must scale down at center in splice mode\n",
                 win->name, drm_rect_width(&src) >> 16,
                 drm_rect_width(&dst), dst.x1, dst.x2);
           return -EINVAL;
       }
 
       if (drm_rect_calc_hscale(&src, &dst, 1, FRAC_16_16(6, 5)) < 0) {
           DRM_ERROR("%s %d --> %d scale down factor should < 1.2 in splice mode\n",
                 win->name, drm_rect_width(&src) >> 16, drm_rect_width(&dst));
           return -EINVAL;
       }
   }
 
   return 0;
}
 
static int vop2_plane_splice_check(struct drm_plane *plane, struct drm_plane_state *pstate,
                  struct drm_display_mode *mode)
{
   struct vop2_win *win = to_vop2_win(plane);
   int ret = 0;
 
   if (!(win->feature & WIN_FEATURE_SPLICE_LEFT)) {
       DRM_ERROR("%s can't be left win in splice mode\n", win->name);
       return -EINVAL;
   }
 
   if (win->feature & WIN_FEATURE_CLUSTER_SUB) {
       DRM_ERROR("%s can't use two win mode in splice mode\n", win->name);
       return -EINVAL;
   }
 
   if ((pstate->rotation & DRM_MODE_ROTATE_270) ||
       (pstate->rotation & DRM_MODE_ROTATE_90) ||
       (pstate->rotation & DRM_MODE_REFLECT_X)) {
       DRM_ERROR("%s can't rotate 270/90 and xmirror in splice mode\n", win->name);
       return -EINVAL;
   }
 
   /* check for cluster splice scale down */
   if (win->feature & WIN_FEATURE_CLUSTER_MAIN)
       ret = vop2_cluter_splice_scale_check(win, pstate, mode->hdisplay);
 
   return ret;
}
 
/*
 * 1. NV12/NV16/YUYV xoffset must aligned as 2 pixel;
 * 2. NV12/NV15 yoffset must aligned as 2 pixel;
 * 3. NV30 xoffset must aligned as 4 pixel;
 * 4. NV15/NV20 xoffset must aligend as 8 pixel at rk3568/rk3588/rk3528/rk3562,
 *    others must aligned as 4 pixel;
 */
static int vop2_linear_yuv_format_check(struct drm_plane *plane, struct drm_plane_state *state)
{
   struct vop2_plane_state *vpstate = to_vop2_plane_state(state);
   struct drm_crtc *crtc = state->crtc;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2_win *win = to_vop2_win(plane);
   struct drm_framebuffer *fb = state->fb;
   struct drm_rect *src = &vpstate->src;
   u32 val = 0;
 
   if (vpstate->afbc_en || vpstate->tiled_en || !fb->format->is_yuv)
       return 0;
 
   switch (fb->format->format) {
   case DRM_FORMAT_NV12:
   case DRM_FORMAT_NV21:
       val = src->x1 >> 16;
       if (val % 2) {
           src->x1 = ALIGN(val, 2) << 16;
           DRM_WARN("VP%d %s src x offset[%d] must aligned as 2 pixel at NV12 fmt, and adjust to: %d\n", vp->id, win->name, val, src->x1 >> 16);
       }
       val = src->y1 >> 16;
       if (val % 2) {
           src->y1 = ALIGN(val, 2) << 16;
           DRM_WARN("VP%d %s src y offset[%d] must aligned as 2 pixel at NV12 fmt, and adjust to: %d\n", vp->id, win->name, val, src->y1 >> 16);
       }
       break;
   case DRM_FORMAT_NV15:
       val = src->y1 >> 16;
       if (val % 2) {
           src->y1 = ALIGN(val, 2) << 16;
           DRM_WARN("VP%d %s src y offset[%d] must aligned as 2 pixel at NV15 fmt, and adjust to: %d\n", vp->id, win->name, val, src->y1 >> 16);
       }
       if (vp->vop2->version == VOP_VERSION_RK3568 ||
           vp->vop2->version == VOP_VERSION_RK3588 ||
           vp->vop2->version == VOP_VERSION_RK3528 ||
           vp->vop2->version == VOP_VERSION_RK3562) {
           val = src->x1 >> 16;
           if (val % 8) {
               src->x1 = ALIGN(val, 8) << 16;
               DRM_WARN("VP%d %s src x offset[%d] must aligned as 8 pixel at NV15 fmt, and adjust to: %d\n", vp->id, win->name, val, src->x1 >> 16);
           }
       } else {
           val = src->x1 >> 16;
           if (val % 4) {
               src->x1 = ALIGN(val, 4) << 16;
               DRM_WARN("VP%d %s src x offset[%d] must aligned as 4 pixel at NV15 fmt, and adjust to: %d\n", vp->id, win->name, val, src->x1 >> 16);
           }
       }
       break;
   case DRM_FORMAT_NV16:
   case DRM_FORMAT_NV61:
   case DRM_FORMAT_YUYV:
   case DRM_FORMAT_YVYU:
   case DRM_FORMAT_VYUY:
   case DRM_FORMAT_UYVY:
       val = src->x1 >> 16;
       if (val % 2) {
           src->x1 = ALIGN(val, 2) << 16;
           DRM_WARN("VP%d %s src x offset[%d] must aligned as 2 pixel at YUYV fmt, and adjust to: %d\n", vp->id, win->name, val, src->x1 >> 16);
       }
       break;
   case DRM_FORMAT_NV20:
       if (vp->vop2->version == VOP_VERSION_RK3568 ||
           vp->vop2->version == VOP_VERSION_RK3588 ||
           vp->vop2->version == VOP_VERSION_RK3528 ||
           vp->vop2->version == VOP_VERSION_RK3562) {
           val = src->x1 >> 16;
           if (val % 8) {
               src->x1 = ALIGN(val, 8) << 16;
               DRM_WARN("VP%d %s src x offset[%d] must aligned as 8 pixel at NV20 fmt, and adjust to: %d\n", vp->id, win->name, val, src->x1 >> 16);
           }
       } else {
           val = src->x1 >> 16;
           if (val % 4) {
               src->x1 = ALIGN(val, 4) << 16;
               DRM_WARN("VP%d %s src x offset[%d] must aligned as 4 pixel at NV20 fmt, and adjust to: %d\n", vp->id, win->name, val, src->x1 >> 16);
           }
       }
       break;
   case DRM_FORMAT_NV30:
       val = src->x1 >> 16;
       if (val % 4) {
           src->x1 = ALIGN(val, 4) << 16;
           DRM_WARN("VP%d %s src x offset[%d] must aligned as 4 pixel at NV30 fmt, and adjust to: %d\n", vp->id, win->name, val, src->x1 >> 16);
       }
       break;
   default:
       return 0;
   }
 
   return 0;
}
 
static int vop2_plane_atomic_check(struct drm_plane *plane, struct drm_plane_state *state)
{
   struct vop2_plane_state *vpstate = to_vop2_plane_state(state);
   struct vop2_win *win = to_vop2_win(plane);
   struct vop2_win *splice_win;
   struct vop2 *vop2 = win->vop2;
   struct drm_framebuffer *fb = state->fb;
   struct drm_display_mode *mode;
   struct drm_crtc *crtc = state->crtc;
   struct drm_crtc_state *cstate;
   struct rockchip_crtc_state *vcstate;
   struct vop2_video_port *vp;
   const struct vop2_data *vop2_data;
   struct drm_rect *dest = &vpstate->dest;
   struct drm_rect *src = &vpstate->src;
   struct drm_gem_object *obj, *uv_obj;
   struct rockchip_gem_object *rk_obj, *rk_uv_obj;
   int min_scale = win->regs->scl ? FRAC_16_16(1, 8) : DRM_PLANE_HELPER_NO_SCALING;
   int max_scale = win->regs->scl ? FRAC_16_16(8, 1) : DRM_PLANE_HELPER_NO_SCALING;
   uint32_t tile_size = 1;
   int max_input_w;
   int max_input_h;
   unsigned long offset;
   dma_addr_t dma_addr;
   int ret;
 
   crtc = crtc ? crtc : plane->state->crtc;
   if (!crtc || !fb) {
       plane->state->visible = false;
       return 0;
   }
 
   vp = to_vop2_video_port(crtc);
   vop2_data = vp->vop2->data;
 
   cstate = drm_atomic_get_existing_crtc_state(state->state, crtc);
   if (WARN_ON(!cstate))
       return -EINVAL;
 
   mode = &cstate->mode;
   vcstate = to_rockchip_crtc_state(cstate);
 
   max_input_w = vop2_data->max_input.width;
   max_input_h = vop2_data->max_input.height;
 
   if (vop2_has_feature(win->vop2, VOP_FEATURE_SPLICE)) {
       if (mode->hdisplay > VOP2_MAX_VP_OUTPUT_WIDTH) {
           vcstate->splice_mode = true;
           ret = vop2_plane_splice_check(plane, state, mode);
           if (ret < 0)
               return ret;
           splice_win = vop2_find_win_by_phys_id(vop2, win->splice_win_id);
           splice_win->splice_mode_right = true;
           splice_win->left_win = win;
           win->splice_win = splice_win;
           max_input_w <<= 1;
       }
   }
 
   vpstate->xmirror_en = (state->rotation & DRM_MODE_REFLECT_X) ? 1 : 0;
   vpstate->ymirror_en = (state->rotation & DRM_MODE_REFLECT_Y) ? 1 : 0;
   vpstate->rotate_270_en = (state->rotation & DRM_MODE_ROTATE_270) ? 1 : 0;
   vpstate->rotate_90_en = (state->rotation & DRM_MODE_ROTATE_90) ? 1 : 0;
 
   if (vpstate->rotate_270_en && vpstate->rotate_90_en) {
       DRM_ERROR("Can't rotate 90 and 270 at the same time\n");
       return -EINVAL;
   }
 
   ret = drm_atomic_helper_check_plane_state(state, cstate,
                         min_scale, max_scale,
                         true, true);
   if (ret)
       return ret;
 
   if (!state->visible) {
       DRM_ERROR("%s is invisible(src: pos[%d, %d] rect[%d x %d] dst: pos[%d, %d] rect[%d x %d]\n",
             plane->name, state->src_x >> 16, state->src_y >> 16, state->src_w >> 16,
             state->src_h >> 16, state->crtc_x, state->crtc_y, state->crtc_w,
             state->crtc_h);
       return 0;
   }
 
   src->x1 = state->src.x1;
   src->y1 = state->src.y1;
   src->x2 = state->src.x2;
   src->y2 = state->src.y2;
   dest->x1 = state->dst.x1;
   dest->y1 = state->dst.y1;
   dest->x2 = state->dst.x2;
   dest->y2 = state->dst.y2;
 
   vpstate->zpos = state->zpos;
   vpstate->global_alpha = state->alpha >> 8;
   vpstate->blend_mode = state->pixel_blend_mode;
   vpstate->format = vop2_convert_format(fb->format->format);
   if (vpstate->format < 0)
       return vpstate->format;
 
   if (drm_rect_width(src) >> 16 < 4 || drm_rect_height(src) >> 16 < 4 ||
       drm_rect_width(dest) < 4 || drm_rect_width(dest) < 4) {
       DRM_ERROR("Invalid size: %dx%d->%dx%d, min size is 4x4\n",
             drm_rect_width(src) >> 16, drm_rect_height(src) >> 16,
             drm_rect_width(dest), drm_rect_height(dest));
       state->visible = false;
       return 0;
   }
 
   if (drm_rect_width(src) >> 16 > max_input_w ||
       drm_rect_height(src) >> 16 > max_input_h) {
       DRM_ERROR("Invalid source: %dx%d. max input: %dx%d\n",
             drm_rect_width(src) >> 16,
             drm_rect_height(src) >> 16,
             max_input_w,
             max_input_h);
       return -EINVAL;
   }
 
   if (rockchip_afbc(plane, fb->modifier))
       vpstate->afbc_en = true;
   else
       vpstate->afbc_en = false;
 
   vpstate->tiled_en = rockchip_tiled(plane, fb->modifier) ?
               fb->modifier & ROCKCHIP_TILED_BLOCK_SIZE_MASK : 0;
   if (vpstate->tiled_en && vpstate->afbc_en) {
       DRM_ERROR("%s afbc and tiled format can't be enabled at same time(modifier: 0x%llx)\n",
             win->name, fb->modifier);
       return -EINVAL;
   }
   if (vpstate->tiled_en)
       tile_size = vpstate->tiled_en == ROCKCHIP_TILED_BLOCK_SIZE_8x8 ? 8 : 4;
 
   /*
    * This is special feature at rk356x, the cluster layer only can support
    * afbc format and can't support linear format;
    */
   if (vp->vop2->version == VOP_VERSION_RK3568) {
       if (vop2_cluster_window(win) && !vpstate->afbc_en) {
           DRM_ERROR("Unsupported linear format at %s\n", win->name);
           return -EINVAL;
       }
   }
 
   if (vp->vop2->version > VOP_VERSION_RK3568) {
       if (vop2_cluster_window(win) && !vpstate->afbc_en && fb->format->is_yuv && !is_vop3(vop2)) {
           DRM_ERROR("Unsupported linear yuv format at %s\n", win->name);
           return -EINVAL;
       }
 
       if (vop2_cluster_window(win) && !vpstate->afbc_en &&
           (win->supported_rotations & state->rotation)) {
           DRM_ERROR("Unsupported linear rotation(%d) format at %s\n",
                 state->rotation, win->name);
           return -EINVAL;
       }
   }
 
   if (win->feature & WIN_FEATURE_CLUSTER_SUB) {
       ret = vop2_cluster_two_win_mode_check(state);
       if (ret < 0)
           return ret;
   }
 
   if (vop2_linear_yuv_format_check(plane, state))
       return -EINVAL;
 
   if (fb->format->char_per_block[0] == 0)
       offset = ALIGN_DOWN(src->x1 >> 16, tile_size) * fb->format->cpp[0] * tile_size;
   else
       offset = drm_format_info_min_pitch(fb->format, 0, ALIGN_DOWN(src->x1 >> 16, tile_size)) * tile_size;
   vpstate->offset = offset + fb->offsets[0];
 
   /*
    * AFBC HDR_PTR must set to the zero offset of the framebuffer.
    */
   if (vpstate->afbc_en)
       offset = 0;
   else if (vpstate->ymirror_en)
       offset += ((src->y2 >> 16) - 1) * fb->pitches[0];
   else
       offset += ALIGN_DOWN(src->y1 >> 16, tile_size) * fb->pitches[0];
 
   obj = fb->obj[0];
   rk_obj = to_rockchip_obj(obj);
 
   vpstate->yrgb_mst = rk_obj->dma_addr + offset + fb->offsets[0];
   if (fb->format->is_yuv && fb->format->num_planes > 1) {
       int hsub = fb->format->hsub;
       int vsub = fb->format->vsub;
 
       if (fb->format->char_per_block[0] == 0)
           offset = ALIGN_DOWN(src->x1 >> 16, tile_size) * fb->format->cpp[1] / hsub * tile_size;
       else
           offset = drm_format_info_min_pitch(fb->format, 1, ALIGN_DOWN(src->x1 >> 16, tile_size)) * tile_size / hsub;
 
       if (vpstate->tiled_en)
           offset /= vsub;
       offset += ALIGN_DOWN(src->y1 >> 16, tile_size) * fb->pitches[1] / vsub;
 
       uv_obj = fb->obj[1];
       rk_uv_obj = to_rockchip_obj(uv_obj);
 
       if (vpstate->ymirror_en && !vpstate->afbc_en)
           offset += fb->pitches[1] * ((state->src_h >> 16) - 2)  / vsub;
       dma_addr = rk_uv_obj->dma_addr + offset + fb->offsets[1];
       vpstate->uv_mst = dma_addr;
       /* tile 4x4 m0 format, y and uv is packed together */
       if (vpstate->tiled_en == ROCKCHIP_TILED_BLOCK_SIZE_4x4_MODE0)
           vpstate->yrgb_mst += offset;
   }
 
   return 0;
}
 
static void vop2_plane_atomic_disable(struct drm_plane *plane, struct drm_plane_state *old_state)
{
   struct vop2_win *win = to_vop2_win(plane);
   struct vop2 *vop2 = win->vop2;
   struct drm_crtc *crtc;
   struct vop2_video_port *vp;
 
#if defined(CONFIG_ROCKCHIP_DRM_DEBUG)
   struct vop2_plane_state *vpstate = to_vop2_plane_state(plane->state);
#endif
 
   rockchip_drm_dbg(vop2->dev, VOP_DEBUG_PLANE, "%s disable %s\n",
            win->name, current->comm);
 
   if (!old_state->crtc)
       return;
 
   spin_lock(&vop2->reg_lock);
 
   crtc = old_state->crtc;
   vp = to_vop2_video_port(crtc);
 
   vop2_win_disable(win, false);
   vp->enabled_win_mask &= ~BIT(win->phys_id);
   if (win->splice_win) {
       vop2_win_disable(win->splice_win, false);
       vp->enabled_win_mask &= ~BIT(win->splice_win->phys_id);
   }
 
#if defined(CONFIG_ROCKCHIP_DRM_DEBUG)
   kfree(vpstate->planlist);
   vpstate->planlist = NULL;
#endif
 
   spin_unlock(&vop2->reg_lock);
}
 
/*
 * The color key is 10 bit, so all format should
 * convert to 10 bit here.
 */
static void vop2_plane_setup_color_key(struct drm_plane *plane)
{
   struct drm_plane_state *pstate = plane->state;
   struct vop2_plane_state *vpstate = to_vop2_plane_state(pstate);
   struct drm_framebuffer *fb = pstate->fb;
   struct vop2_win *win = to_vop2_win(plane);
   struct vop2 *vop2 = win->vop2;
   uint32_t color_key_en = 0;
   uint32_t color_key;
   uint32_t r = 0;
   uint32_t g = 0;
   uint32_t b = 0;
 
   if (!(vpstate->color_key & VOP_COLOR_KEY_MASK) || fb->format->is_yuv) {
       VOP_WIN_SET(vop2, win, color_key_en, 0);
       return;
   }
 
   switch (fb->format->format) {
   case DRM_FORMAT_RGB565:
   case DRM_FORMAT_BGR565:
       r = (vpstate->color_key & 0xf800) >> 11;
       g = (vpstate->color_key & 0x7e0) >> 5;
       b = (vpstate->color_key & 0x1f);
       r <<= 5;
       g <<= 4;
       b <<= 5;
       color_key_en = 1;
       break;
   case DRM_FORMAT_XRGB8888:
   case DRM_FORMAT_ARGB8888:
   case DRM_FORMAT_XBGR8888:
   case DRM_FORMAT_ABGR8888:
   case DRM_FORMAT_RGB888:
   case DRM_FORMAT_BGR888:
       r = (vpstate->color_key & 0xff0000) >> 16;
       g = (vpstate->color_key & 0xff00) >> 8;
       b = (vpstate->color_key & 0xff);
       r <<= 2;
       g <<= 2;
       b <<= 2;
       color_key_en = 1;
       break;
   }
 
   color_key = (r << 20) | (g << 10) | b;
   VOP_WIN_SET(vop2, win, color_key_en, color_key_en);
   VOP_WIN_SET(vop2, win, color_key, color_key);
}
 
static void vop2_calc_drm_rect_for_splice(struct vop2_plane_state *vpstate,
                     struct drm_rect *left_src, struct drm_rect *left_dst,
                     struct drm_rect *right_src, struct drm_rect *right_dst)
{
   struct drm_crtc *crtc = vpstate->base.crtc;
   struct drm_display_mode *mode = &crtc->state->adjusted_mode;
   struct drm_rect *dst = &vpstate->dest;
   struct drm_rect *src = &vpstate->src;
   u16 half_hdisplay = mode->crtc_hdisplay >> 1;
   int hscale = drm_rect_calc_hscale(src, dst, 0, INT_MAX);
   int dst_w = drm_rect_width(dst);
   int src_w = drm_rect_width(src) >> 16;
   int left_src_w, left_dst_w, right_dst_w;
   struct drm_plane_state *pstate = &vpstate->base;
   struct drm_framebuffer *fb = pstate->fb;
 
   left_dst_w = min_t(u16, half_hdisplay, dst->x2) - dst->x1;
   if (left_dst_w < 0)
       left_dst_w = 0;
   right_dst_w = dst_w - left_dst_w;
 
   if (!right_dst_w)
       left_src_w = src_w;
   else
       left_src_w = (left_dst_w * hscale) >> 16;
 
   /*
    * Make sure the yrgb/uv mst of right win are byte aligned
    * with full pixel.
    */
   if (right_dst_w) {
       if (fb->format->format == DRM_FORMAT_NV15)
           left_src_w &= ~0x7;
       else if (fb->format->format == DRM_FORMAT_NV12)
           left_src_w &= ~0x1;
   }
   left_src->x1 = src->x1;
   left_src->x2 = src->x1 + (left_src_w << 16);
   left_dst->x1 = dst->x1;
   left_dst->x2 = dst->x1 + left_dst_w;
   right_src->x1 = left_src->x2;
   right_src->x2 = src->x2;
   right_dst->x1 = dst->x1 + left_dst_w - half_hdisplay;
   if (right_dst->x1 < 0)
       right_dst->x1 = 0;
 
   right_dst->x2 = right_dst->x1 + right_dst_w;
 
   left_src->y1 = src->y1;
   left_src->y2 = src->y2;
   left_dst->y1 = dst->y1;
   left_dst->y2 = dst->y2;
   right_src->y1 = src->y1;
   right_src->y2 = src->y2;
   right_dst->y1 = dst->y1;
   right_dst->y2 = dst->y2;
}
 
static void rk3588_vop2_win_cfg_axi(struct vop2_win *win)
{
   struct vop2 *vop2 = win->vop2;
 
   /*
    * No need to set multi area sub windows as it
    * share the same axi bus and read_id with main window.
    */
   if (vop2_multi_area_sub_window(win))
       return;
   /*
    * No need to set Cluster sub windows axi_id as it
    * share the same axi bus with main window.
    */
   if (!vop2_cluster_sub_window(win))
       VOP_WIN_SET(vop2, win, axi_id, win->axi_id);
   VOP_WIN_SET(vop2, win, axi_yrgb_id, win->axi_yrgb_id);
   VOP_WIN_SET(vop2, win, axi_uv_id, win->axi_uv_id);
}
 
static const char *modifier_to_string(uint64_t modifier)
{
   switch (modifier) {
   case DRM_FORMAT_MOD_ROCKCHIP_TILED(ROCKCHIP_TILED_BLOCK_SIZE_8x8):
       return "[TILE_8x8]";
   case DRM_FORMAT_MOD_ROCKCHIP_TILED(ROCKCHIP_TILED_BLOCK_SIZE_4x4_MODE0):
       return "[TILE_4x4_M0]";
   case DRM_FORMAT_MOD_ROCKCHIP_TILED(ROCKCHIP_TILED_BLOCK_SIZE_4x4_MODE1):
       return "[TILE_4x4_M1]";
   default:
       return drm_is_afbc(modifier) ? "[AFBC]" : "";
   }
}
 
static void vop2_win_atomic_update(struct vop2_win *win, struct drm_rect *src, struct drm_rect *dst,
                  struct drm_plane_state *pstate)
{
   struct drm_crtc *crtc = pstate->crtc;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2_plane_state *vpstate = to_vop2_plane_state(pstate);
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
   struct vop2 *vop2 = win->vop2;
   struct drm_framebuffer *fb = pstate->fb;
   struct drm_rect *left_src = &vpstate->src;
   uint32_t bpp = rockchip_drm_get_bpp(fb->format);
   uint32_t actual_w, actual_h, dsp_w, dsp_h;
   uint32_t dsp_stx, dsp_sty;
   uint32_t act_info, dsp_info, dsp_st;
   uint32_t format, check_size;
   uint32_t afbc_format;
   uint32_t rb_swap;
   uint32_t uv_swap;
   uint32_t afbc_half_block_en;
   uint32_t afbc_tile_num;
   uint32_t lb_mode;
   uint32_t stride, uv_stride = 0;
   uint32_t transform_offset;
   /* offset of the right window in splice mode */
   uint32_t splice_pixel_offset = 0;
   uint32_t splice_yrgb_offset = 0;
   uint32_t splice_uv_offset = 0;
   uint32_t afbc_xoffset;
   uint32_t hsub;
   dma_addr_t yrgb_mst;
   dma_addr_t uv_mst;
 
   struct drm_format_name_buf format_name;
   bool dither_up;
   bool tile_4x4_m0 = vpstate->tiled_en == ROCKCHIP_TILED_BLOCK_SIZE_4x4_MODE0 ? true : false;
 
   actual_w = drm_rect_width(src) >> 16;
   actual_h = drm_rect_height(src) >> 16;
 
   if (!actual_w || !actual_h) {
       vop2_win_disable(win, true);
       return;
   }
 
   dsp_w = drm_rect_width(dst);
   /*
    * This win is for the right part of the plane,
    * we need calculate the fb offset for it.
    */
   if (win->splice_mode_right) {
       splice_pixel_offset = (src->x1 - left_src->x1) >> 16;
       splice_yrgb_offset = drm_format_info_min_pitch(fb->format, 0, splice_pixel_offset);
       if (fb->format->is_yuv && fb->format->num_planes > 1) {
           hsub = fb->format->hsub;
           splice_uv_offset = drm_format_info_min_pitch(fb->format, 1, splice_pixel_offset / hsub);
       }
   }
 
   if (dst->x1 + dsp_w > adjusted_mode->crtc_hdisplay) {
       DRM_ERROR("vp%d %s dest->x1[%d] + dsp_w[%d] exceed mode hdisplay[%d]\n",
             vp->id, win->name, dst->x1, dsp_w, adjusted_mode->crtc_hdisplay);
       dsp_w = adjusted_mode->crtc_hdisplay - dst->x1;
       if (dsp_w < 4)
           dsp_w = 4;
       actual_w = dsp_w * actual_w / drm_rect_width(dst);
   }
   dsp_h = drm_rect_height(dst);
   check_size = adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE ? adjusted_mode->vdisplay : adjusted_mode->crtc_vdisplay;
   if (dst->y1 + dsp_h > check_size) {
       DRM_ERROR("vp%d %s dest->y1[%d] + dsp_h[%d] exceed mode vdisplay[%d]\n",
             vp->id, win->name, dst->y1, dsp_h, adjusted_mode->crtc_vdisplay);
       dsp_h = adjusted_mode->crtc_vdisplay - dst->y1;
       if (dsp_h < 4)
           dsp_h = 4;
       actual_h = dsp_h * actual_h / drm_rect_height(dst);
   }
 
   /*
    * Workaround only for rk3568 vop
    */
   if (vop2->version == VOP_VERSION_RK3568) {
       /*
        * This is workaround solution for IC design:
        * esmart can't support scale down when actual_w % 16 == 1;
        * esmart can't support scale down when dsp_w % 2 == 1;
        * esmart actual_w should align as 4 pixel when is linear 10 bit yuv format;
        *
        * cluster actual_w should align as 4 pixel when enable afbc;
        */
       if (!vop2_cluster_window(win)) {
           if (actual_w > dsp_w && (actual_w & 0xf) == 1) {
               DRM_WARN("vp%d %s act_w[%d] MODE 16 == 1 at scale down mode\n", vp->id, win->name, actual_w);
               actual_w -= 1;
           }
           if (actual_w > dsp_w && (dsp_w & 0x1) == 1) {
               DRM_WARN("vp%d %s dsp_w[%d] MODE 2 == 1 at scale down mode\n", vp->id, win->name, dsp_w);
               dsp_w -= 1;
           }
       }
 
       if (vop2_cluster_window(win) && actual_w % 4) {
           DRM_WARN("vp%d %s actual_w[%d] should align as 4 pixel when enable afbc\n",
                vp->id, win->name, actual_w);
           actual_w = ALIGN_DOWN(actual_w, 4);
       }
   }
 
   if (is_linear_10bit_yuv(fb->format->format) && actual_w & 0x3) {
       DRM_WARN("vp%d %s actual_w[%d] should align as 4 pixel when is linear 10 bit yuv format\n", vp->id, win->name, actual_w);
       actual_w = ALIGN_DOWN(actual_w, 4);
   }
 
   act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff);
   dsp_info = (dsp_h - 1) << 16 | ((dsp_w - 1) & 0xffff);
   stride = DIV_ROUND_UP(fb->pitches[0], 4);
   dsp_stx = dst->x1;
   dsp_sty = dst->y1;
   dsp_st = dsp_sty << 16 | (dsp_stx & 0xffff);
 
   if (vpstate->tiled_en) {
       if (is_vop3(vop2))
           format = vop3_convert_tiled_format(fb->format->format, vpstate->tiled_en);
       else
           format = vop2_convert_tiled_format(fb->format->format);
   } else {
       format = vop2_convert_format(fb->format->format);
   }
 
   vop2_setup_csc_mode(vp, vpstate);
 
   afbc_half_block_en = vop2_afbc_half_block_enable(vpstate);
 
   vop2_win_enable(win);
   spin_lock(&vop2->reg_lock);
   rockchip_drm_dbg(vop2->dev, VOP_DEBUG_PLANE,
            "vp%d update %s[%dx%d->%dx%d@(%d, %d)] fmt[%.4s%s] addr[%pad] by %s\n",
            vp->id, win->name, actual_w, actual_h, dsp_w, dsp_h,
            dsp_stx, dsp_sty,
            drm_get_format_name(fb->format->format, &format_name),
            modifier_to_string(fb->modifier), &vpstate->yrgb_mst, current->comm);
 
   if (vop2->version != VOP_VERSION_RK3568)
       rk3588_vop2_win_cfg_axi(win);
 
   if (!win->parent && !vop2_cluster_window(win) && is_vop3(vop2))
       VOP_WIN_SET(vop2, win, scale_engine_num, win->scale_engine_num);
 
   if (vpstate->afbc_en) {
       /* the afbc superblock is 16 x 16 */
       afbc_format = vop2_convert_afbc_format(fb->format->format);
       /* Enable color transform for YTR */
       if (fb->modifier & AFBC_FORMAT_MOD_YTR)
           afbc_format |= (1 << 4);
       afbc_tile_num = ALIGN(actual_w, 16) >> 4;
 
       /* The right win should have a src  offset in splice mode */
       afbc_xoffset = (src->x1 >> 16);
       /* AFBC pic_vir_width is count by pixel, this is different
        * with WIN_VIR_STRIDE.
        */
       if (!bpp) {
           WARN(1, "bpp is zero\n");
           bpp = 1;
       }
       stride = (fb->pitches[0] << 3) / bpp;
       if ((stride & 0x3f) &&
           (vpstate->xmirror_en || vpstate->rotate_90_en || vpstate->rotate_270_en))
           DRM_ERROR("vp%d %s stride[%d] must align as 64 pixel when enable xmirror/rotate_90/rotate_270[0x%x]\n",
                 vp->id, win->name, stride, pstate->rotation);
 
       rb_swap = vop2_afbc_rb_swap(fb->format->format);
       uv_swap = vop2_afbc_uv_swap(fb->format->format);
       vpstate->afbc_half_block_en = afbc_half_block_en;
 
       transform_offset = vop2_afbc_transform_offset(vpstate, splice_pixel_offset);
       VOP_CLUSTER_SET(vop2, win, afbc_enable, 1);
       VOP_AFBC_SET(vop2, win, format, afbc_format);
       VOP_AFBC_SET(vop2, win, rb_swap, rb_swap);
       VOP_AFBC_SET(vop2, win, uv_swap, uv_swap);
 
       if (vop2->version == VOP_VERSION_RK3568)
           VOP_AFBC_SET(vop2, win, auto_gating_en, 0);
       else
           VOP_AFBC_SET(vop2, win, auto_gating_en, 1);
       VOP_AFBC_SET(vop2, win, block_split_en, 0);
       VOP_AFBC_SET(vop2, win, hdr_ptr, vpstate->yrgb_mst);
       VOP_AFBC_SET(vop2, win, pic_size, act_info);
       VOP_AFBC_SET(vop2, win, transform_offset, transform_offset);
       VOP_AFBC_SET(vop2, win, pic_offset, (afbc_xoffset | src->y1));
       VOP_AFBC_SET(vop2, win, dsp_offset, (dst->x1 | (dst->y1 << 16)));
       VOP_AFBC_SET(vop2, win, pic_vir_width, stride);
       VOP_AFBC_SET(vop2, win, tile_num, afbc_tile_num);
       VOP_AFBC_SET(vop2, win, xmirror, vpstate->xmirror_en);
       VOP_AFBC_SET(vop2, win, ymirror, vpstate->ymirror_en);
       VOP_AFBC_SET(vop2, win, rotate_270, vpstate->rotate_270_en);
       VOP_AFBC_SET(vop2, win, rotate_90, vpstate->rotate_90_en);
   } else {
       VOP_CLUSTER_SET(vop2, win, afbc_enable, 0);
       transform_offset = vop2_tile_transform_offset(vpstate, vpstate->tiled_en);
       VOP_AFBC_SET(vop2, win, transform_offset, transform_offset);
       VOP_WIN_SET(vop2, win, ymirror, vpstate->ymirror_en);
       VOP_WIN_SET(vop2, win, xmirror, vpstate->xmirror_en);
   }
 
   if (vpstate->rotate_90_en || vpstate->rotate_270_en) {
       act_info = swahw32(act_info);
       actual_w = drm_rect_height(src) >> 16;
       actual_h = drm_rect_width(src) >> 16;
   }
 
   yrgb_mst = vpstate->yrgb_mst + splice_yrgb_offset;
   uv_mst = vpstate->uv_mst + splice_uv_offset;
   /* rk3588 should set half_blocK_en to 1 in line and tile mode */
   VOP_AFBC_SET(vop2, win, half_block_en, afbc_half_block_en);
 
   VOP_WIN_SET(vop2, win, format, format);
   VOP_WIN_SET(vop2, win, yrgb_mst, yrgb_mst);
 
   rb_swap = vop2_win_rb_swap(fb->format->format);
   uv_swap = vop2_win_uv_swap(fb->format->format);
   if (vpstate->tiled_en) {
       uv_swap = 1;
       if (vpstate->tiled_en == ROCKCHIP_TILED_BLOCK_SIZE_8x8)
           stride <<= 3;
       else
           stride <<= 2;
   }
   VOP_WIN_SET(vop2, win, rb_swap, rb_swap);
   VOP_WIN_SET(vop2, win, uv_swap, uv_swap);
 
   if (fb->format->is_yuv) {
       uv_stride = DIV_ROUND_UP(fb->pitches[1], 4);
       if (vpstate->tiled_en) {
           int vsub = fb->format->vsub;
 
           if (vpstate->tiled_en == ROCKCHIP_TILED_BLOCK_SIZE_8x8)
               uv_stride = uv_stride * 8 / vsub;
           else
               uv_stride = uv_stride * 4 / vsub;
           VOP_WIN_SET(vop2, win, tile_mode, tile_4x4_m0);
       }
 
       VOP_WIN_SET(vop2, win, uv_vir, uv_stride);
       VOP_WIN_SET(vop2, win, uv_mst, uv_mst);
   }
 
   /* tile 4x4 m0 format, y and uv is packed together */
   if (tile_4x4_m0)
       VOP_WIN_SET(vop2, win, yrgb_vir, stride + uv_stride);
   else
       VOP_WIN_SET(vop2, win, yrgb_vir, stride);
 
   vop2_setup_scale(vop2, win, actual_w, actual_h, dsp_w, dsp_h, pstate);
   vop2_plane_setup_color_key(&win->base);
   VOP_WIN_SET(vop2, win, act_info, act_info);
   VOP_WIN_SET(vop2, win, dsp_info, dsp_info);
   VOP_WIN_SET(vop2, win, dsp_st, dsp_st);
 
   VOP_WIN_SET(vop2, win, y2r_en, vpstate->y2r_en);
   VOP_WIN_SET(vop2, win, r2y_en, vpstate->r2y_en);
   VOP_WIN_SET(vop2, win, csc_mode, vpstate->csc_mode);
 
   if (win->feature & WIN_FEATURE_Y2R_13BIT_DEPTH && !vop2_cluster_window(win))
       VOP_WIN_SET(vop2, win, csc_13bit_en, !!(vpstate->csc_mode & CSC_BT709L_13BIT));
 
   dither_up = vop2_win_dither_up(fb->format->format);
   VOP_WIN_SET(vop2, win, dither_up, dither_up);
 
   VOP_WIN_SET(vop2, win, enable, 1);
   vp->enabled_win_mask |= BIT(win->phys_id);
   if (vop2_cluster_window(win)) {
       lb_mode = vop2_get_cluster_lb_mode(win, vpstate);
       VOP_CLUSTER_SET(vop2, win, lb_mode, lb_mode);
       VOP_CLUSTER_SET(vop2, win, scl_lb_mode, lb_mode == 1 ? 3 : 0);
       VOP_CLUSTER_SET(vop2, win, enable, 1);
       VOP_CLUSTER_SET(vop2, win, frm_reset_en, 1);
   }
   spin_unlock(&vop2->reg_lock);
}
 
static void vop2_plane_atomic_update(struct drm_plane *plane, struct drm_plane_state *old_state)
{
   struct drm_plane_state *pstate = plane->state;
   struct drm_crtc *crtc = pstate->crtc;
   struct vop2_win *win = to_vop2_win(plane);
   struct vop2_win *splice_win;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2_plane_state *vpstate = to_vop2_plane_state(pstate);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct drm_framebuffer *fb = pstate->fb;
   struct drm_format_name_buf format_name;
   struct vop2 *vop2 = win->vop2;
   struct drm_rect wsrc;
   struct drm_rect wdst;
   /* right part in splice mode */
   struct drm_rect right_wsrc;
   struct drm_rect right_wdst;
 
#if defined(CONFIG_ROCKCHIP_DRM_DEBUG)
   struct drm_rect *psrc = &vpstate->src;
   bool AFBC_flag = false;
   struct vop_dump_list *planlist;
   unsigned long num_pages;
   struct page **pages;
   struct drm_gem_object *obj;
   struct rockchip_gem_object *rk_obj;
 
   num_pages = 0;
   pages = NULL;
   obj = fb->obj[0];
   rk_obj = to_rockchip_obj(obj);
   if (rk_obj) {
       num_pages = rk_obj->num_pages;
       pages = rk_obj->pages;
   }
   if (rockchip_afbc(plane, fb->modifier))
       AFBC_flag = true;
   else
       AFBC_flag = false;
#endif
 
   /*
    * can't update plane when vop2 is disabled.
    */
   if (WARN_ON(!crtc))
       return;
 
   if (WARN_ON(!vop2->is_enabled))
       return;
 
   if (!pstate->visible) {
       vop2_plane_atomic_disable(plane, old_state);
       return;
   }
 
   /*
    * This means this window is moved from another vp
    * so the VOP2_PORT_SEL register is changed and
    * take effect by vop2_wait_for_port_mux_done
    * in this commit. so we can continue configure
    * the window and report vsync
    */
   if (win->old_vp_mask != win->vp_mask) {
       win->old_vp_mask = win->vp_mask;
       if (!is_vop3(vop2))
           vp->skip_vsync = false;
   }
 
   if (vcstate->splice_mode) {
       DRM_DEV_DEBUG(vop2->dev, "vp%d update %s[%dx%d->%dx%d@(%d,%d)] fmt[%.4s%s] addr[%pad]\n",
                 vp->id, win->name, drm_rect_width(&vpstate->src) >> 16,
                 drm_rect_height(&vpstate->src) >> 16,
                 drm_rect_width(&vpstate->dest), drm_rect_height(&vpstate->dest),
                 vpstate->dest.x1, vpstate->dest.y1,
                 drm_get_format_name(fb->format->format, &format_name),
                 modifier_to_string(fb->modifier), &vpstate->yrgb_mst);
 
       vop2_calc_drm_rect_for_splice(vpstate, &wsrc, &wdst, &right_wsrc, &right_wdst);
       splice_win = win->splice_win;
       vop2_win_atomic_update(splice_win, &right_wsrc, &right_wdst, pstate);
   } else {
       memcpy(&wsrc, &vpstate->src, sizeof(struct drm_rect));
       memcpy(&wdst, &vpstate->dest, sizeof(struct drm_rect));
   }
 
   vop2_win_atomic_update(win, &wsrc, &wdst, pstate);
 
   vop2->is_iommu_needed = true;
#if defined(CONFIG_ROCKCHIP_DRM_DEBUG)
   kfree(vpstate->planlist);
   vpstate->planlist = NULL;
 
   planlist = kmalloc(sizeof(*planlist), GFP_KERNEL);
   if (planlist) {
       planlist->dump_info.AFBC_flag = AFBC_flag;
       planlist->dump_info.area_id = win->area_id;
       planlist->dump_info.win_id = win->win_id;
       planlist->dump_info.yuv_format = fb->format->is_yuv;
       planlist->dump_info.num_pages = num_pages;
       planlist->dump_info.pages = pages;
       planlist->dump_info.offset = vpstate->offset;
       planlist->dump_info.pitches = fb->pitches[0];
       planlist->dump_info.height = drm_rect_height(psrc) >> 16;
       planlist->dump_info.format = fb->format;
       list_add_tail(&planlist->entry, &vp->rockchip_crtc.vop_dump_list_head);
       vpstate->planlist = planlist;
   } else {
       DRM_ERROR("can't alloc a node of planlist %p\n", planlist);
       return;
   }
   if (vp->rockchip_crtc.vop_dump_status == DUMP_KEEP ||
       vp->rockchip_crtc.vop_dump_times > 0) {
       rockchip_drm_dump_plane_buffer(&planlist->dump_info, vp->rockchip_crtc.frame_count);
       vp->rockchip_crtc.vop_dump_times--;
   }
#endif
}
 
static const struct drm_plane_helper_funcs vop2_plane_helper_funcs = {
   .atomic_check = vop2_plane_atomic_check,
   .atomic_update = vop2_plane_atomic_update,
   .atomic_disable = vop2_plane_atomic_disable,
};
 
/**
 * rockchip_atomic_helper_update_plane copy from drm_atomic_helper_update_plane
 * be designed to support async commit at ioctl DRM_IOCTL_MODE_SETPLANE.
 * @plane: plane object to update
 * @crtc: owning CRTC of owning plane
 * @fb: framebuffer to flip onto plane
 * @crtc_x: x offset of primary plane on crtc
 * @crtc_y: y offset of primary plane on crtc
 * @crtc_w: width of primary plane rectangle on crtc
 * @crtc_h: height of primary plane rectangle on crtc
 * @src_x: x offset of @fb for panning
 * @src_y: y offset of @fb for panning
 * @src_w: width of source rectangle in @fb
 * @src_h: height of source rectangle in @fb
 * @ctx: lock acquire context
 *
 * Provides a default plane update handler using the atomic driver interface.
 *
 * RETURNS:
 * Zero on success, error code on failure
 */
static int __maybe_unused
rockchip_atomic_helper_update_plane(struct drm_plane *plane,
                   struct drm_crtc *crtc,
                   struct drm_framebuffer *fb,
                   int crtc_x, int crtc_y,
                   unsigned int crtc_w, unsigned int crtc_h,
                   uint32_t src_x, uint32_t src_y,
                   uint32_t src_w, uint32_t src_h,
                   struct drm_modeset_acquire_ctx *ctx)
{
   struct drm_atomic_state *state;
   struct drm_plane_state *pstate;
   struct vop2_plane_state *vpstate;
   int ret = 0;
 
   state = drm_atomic_state_alloc(plane->dev);
   if (!state)
       return -ENOMEM;
 
   state->acquire_ctx = ctx;
   pstate = drm_atomic_get_plane_state(state, plane);
   if (IS_ERR(pstate)) {
       ret = PTR_ERR(pstate);
       goto fail;
   }
 
   vpstate = to_vop2_plane_state(pstate);
 
   ret = drm_atomic_set_crtc_for_plane(pstate, crtc);
   if (ret != 0)
       goto fail;
   drm_atomic_set_fb_for_plane(pstate, fb);
   pstate->crtc_x = crtc_x;
   pstate->crtc_y = crtc_y;
   pstate->crtc_w = crtc_w;
   pstate->crtc_h = crtc_h;
   pstate->src_x = src_x;
   pstate->src_y = src_y;
   pstate->src_w = src_w;
   pstate->src_h = src_h;
 
   if (plane == crtc->cursor || vpstate->async_commit)
       state->legacy_cursor_update = true;
 
   ret = drm_atomic_commit(state);
fail:
   drm_atomic_state_put(state);
   return ret;
}
 
/**
 * drm_atomic_helper_disable_plane copy from drm_atomic_helper_disable_plane
 * be designed to support async commit at ioctl DRM_IOCTL_MODE_SETPLANE.
 *
 * @plane: plane to disable
 * @ctx: lock acquire context
 *
 * Provides a default plane disable handler using the atomic driver interface.
 *
 * RETURNS:
 * Zero on success, error code on failure
 */
static int __maybe_unused
rockchip_atomic_helper_disable_plane(struct drm_plane *plane,
                    struct drm_modeset_acquire_ctx *ctx)
{
   struct drm_atomic_state *state;
   struct drm_plane_state *pstate;
   struct vop2_plane_state *vpstate;
   int ret = 0;
 
   state = drm_atomic_state_alloc(plane->dev);
   if (!state)
       return -ENOMEM;
 
   state->acquire_ctx = ctx;
   pstate = drm_atomic_get_plane_state(state, plane);
   if (IS_ERR(pstate)) {
       ret = PTR_ERR(pstate);
       goto fail;
   }
   vpstate = to_vop2_plane_state(pstate);
 
   if ((pstate->crtc && pstate->crtc->cursor == plane) ||
       vpstate->async_commit)
       pstate->state->legacy_cursor_update = true;
 
   ret = __drm_atomic_helper_disable_plane(plane, pstate);
   if (ret != 0)
       goto fail;
 
   ret = drm_atomic_commit(state);
fail:
   drm_atomic_state_put(state);
   return ret;
}
 
static void vop2_plane_destroy(struct drm_plane *plane)
{
   drm_plane_cleanup(plane);
}
 
static void vop2_atomic_plane_reset(struct drm_plane *plane)
{
   struct vop2_plane_state *vpstate = to_vop2_plane_state(plane->state);
   struct vop2_win *win = to_vop2_win(plane);
 
   if (plane->state && plane->state->fb)
       __drm_atomic_helper_plane_destroy_state(plane->state);
   kfree(vpstate);
   vpstate = kzalloc(sizeof(*vpstate), GFP_KERNEL);
   if (!vpstate)
       return;
 
   __drm_atomic_helper_plane_reset(plane, &vpstate->base);
   vpstate->base.zpos = win->zpos;
}
 
static struct drm_plane_state *vop2_atomic_plane_duplicate_state(struct drm_plane *plane)
{
   struct vop2_plane_state *old_vpstate;
   struct vop2_plane_state *vpstate;
 
   if (WARN_ON(!plane->state))
       return NULL;
 
   old_vpstate = to_vop2_plane_state(plane->state);
   vpstate = kmemdup(old_vpstate, sizeof(*vpstate), GFP_KERNEL);
   if (!vpstate)
       return NULL;
 
   vpstate->hdr_in = 0;
   vpstate->hdr2sdr_en = 0;
 
   __drm_atomic_helper_plane_duplicate_state(plane, &vpstate->base);
 
   return &vpstate->base;
}
 
static void vop2_atomic_plane_destroy_state(struct drm_plane *plane,
                       struct drm_plane_state *state)
{
   struct vop2_plane_state *vpstate = to_vop2_plane_state(state);
 
   __drm_atomic_helper_plane_destroy_state(state);
 
   kfree(vpstate);
}
 
static int vop2_atomic_plane_set_property(struct drm_plane *plane,
                     struct drm_plane_state *state,
                     struct drm_property *property,
                     uint64_t val)
{
   struct rockchip_drm_private *private = plane->dev->dev_private;
   struct vop2_plane_state *vpstate = to_vop2_plane_state(state);
   struct vop2_win *win = to_vop2_win(plane);
 
   if (property == private->eotf_prop) {
       vpstate->eotf = val;
       return 0;
   }
 
   if (property == private->color_space_prop) {
       vpstate->color_space = val;
       return 0;
   }
 
   if (property == private->async_commit_prop) {
       vpstate->async_commit = val;
       return 0;
   }
 
   if (property == win->color_key_prop) {
       vpstate->color_key = val;
       return 0;
   }
 
   DRM_ERROR("failed to set vop2 plane property id:%d, name:%s\n",
         property->base.id, property->name);
 
   return -EINVAL;
}
 
static int vop2_atomic_plane_get_property(struct drm_plane *plane,
                     const struct drm_plane_state *state,
                     struct drm_property *property,
                     uint64_t *val)
{
   struct rockchip_drm_private *private = plane->dev->dev_private;
   struct vop2_plane_state *vpstate = to_vop2_plane_state(state);
   struct vop2_win *win = to_vop2_win(plane);
 
   if (property == private->eotf_prop) {
       *val = vpstate->eotf;
       return 0;
   }
 
   if (property == private->color_space_prop) {
       *val = vpstate->color_space;
       return 0;
   }
 
   if (property == private->async_commit_prop) {
       *val = vpstate->async_commit;
       return 0;
   }
 
   if (property == private->share_id_prop) {
       int i;
       struct drm_mode_object *obj = &plane->base;
 
       for (i = 0; i < obj->properties->count; i++) {
           if (obj->properties->properties[i] == property) {
               *val = obj->properties->values[i];
               return 0;
           }
       }
   }
 
   if (property == win->color_key_prop) {
       *val = vpstate->color_key;
       return 0;
   }
 
   DRM_ERROR("failed to get vop2 plane property id:%d, name:%s\n",
         property->base.id, property->name);
 
   return -EINVAL;
}
 
static const struct drm_plane_funcs vop2_plane_funcs = {
   .update_plane    = rockchip_atomic_helper_update_plane,
   .disable_plane    = rockchip_atomic_helper_disable_plane,
   .destroy = vop2_plane_destroy,
   .reset = vop2_atomic_plane_reset,
   .atomic_duplicate_state = vop2_atomic_plane_duplicate_state,
   .atomic_destroy_state = vop2_atomic_plane_destroy_state,
   .atomic_set_property = vop2_atomic_plane_set_property,
   .atomic_get_property = vop2_atomic_plane_get_property,
   .format_mod_supported = rockchip_vop2_mod_supported,
};
 
static int vop2_crtc_enable_vblank(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
   unsigned long flags;
 
   if (WARN_ON(!vop2->is_enabled))
       return -EPERM;
 
   spin_lock_irqsave(&vop2->irq_lock, flags);
 
   VOP_INTR_SET_TYPE(vop2, intr, clear, FS_FIELD_INTR, 1);
   VOP_INTR_SET_TYPE(vop2, intr, enable, FS_FIELD_INTR, 1);
 
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
 
   return 0;
}
 
static void vop2_crtc_disable_vblank(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
   unsigned long flags;
 
   if (WARN_ON(!vop2->is_enabled))
       return;
 
   spin_lock_irqsave(&vop2->irq_lock, flags);
 
   VOP_INTR_SET_TYPE(vop2, intr, enable, FS_FIELD_INTR, 0);
 
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
}
 
static void vop2_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
                       struct drm_file *file_priv)
{
   struct drm_device *drm = crtc->dev;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct drm_pending_vblank_event *e;
   unsigned long flags;
 
   spin_lock_irqsave(&drm->event_lock, flags);
   e = vp->event;
   if (e && e->base.file_priv == file_priv) {
       vp->event = NULL;
 
       //e->base.destroy(&e->base);//todo
       file_priv->event_space += sizeof(e->event);
   }
   spin_unlock_irqrestore(&drm->event_lock, flags);
}
 
static bool vop2_crtc_line_flag_irq_is_enabled(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
   uint32_t line_flag_irq;
   unsigned long flags;
 
   spin_lock_irqsave(&vop2->irq_lock, flags);
   line_flag_irq = VOP_INTR_GET_TYPE(vop2, intr, enable, LINE_FLAG_INTR);
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
 
   return !!line_flag_irq;
}
 
static void vop2_crtc_line_flag_irq_enable(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
   unsigned long flags;
 
   if (!vop2->is_enabled)
       return;
 
   spin_lock_irqsave(&vop2->irq_lock, flags);
   VOP_INTR_SET_TYPE(vop2, intr, clear, LINE_FLAG_INTR, 1);
   VOP_INTR_SET_TYPE(vop2, intr, enable, LINE_FLAG_INTR, 1);
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
}
 
static void vop2_crtc_line_flag_irq_disable(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
   unsigned long flags;
 
   if (!vop2->is_enabled)
       return;
 
   spin_lock_irqsave(&vop2->irq_lock, flags);
   VOP_INTR_SET_TYPE(vop2, intr, enable, LINE_FLAG_INTR, 0);
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
}
 
static void vop3_mcu_mode_setup(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
 
   VOP_MODULE_SET(vop2, vp, mcu_type, 1);
   VOP_MODULE_SET(vop2, vp, mcu_hold_mode, 1);
   VOP_MODULE_SET(vop2, vp, mcu_pix_total, vp->mcu_timing.mcu_pix_total);
   VOP_MODULE_SET(vop2, vp, mcu_cs_pst, vp->mcu_timing.mcu_cs_pst);
   VOP_MODULE_SET(vop2, vp, mcu_cs_pend, vp->mcu_timing.mcu_cs_pend);
   VOP_MODULE_SET(vop2, vp, mcu_rw_pst, vp->mcu_timing.mcu_rw_pst);
   VOP_MODULE_SET(vop2, vp, mcu_rw_pend, vp->mcu_timing.mcu_rw_pend);
}
 
static void vop3_mcu_bypass_mode_setup(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
 
   VOP_MODULE_SET(vop2, vp, mcu_type, 1);
   VOP_MODULE_SET(vop2, vp, mcu_hold_mode, 1);
   VOP_MODULE_SET(vop2, vp, mcu_pix_total, 53);
   VOP_MODULE_SET(vop2, vp, mcu_cs_pst, 6);
   VOP_MODULE_SET(vop2, vp, mcu_cs_pend, 48);
   VOP_MODULE_SET(vop2, vp, mcu_rw_pst, 12);
   VOP_MODULE_SET(vop2, vp, mcu_rw_pend, 30);
}
 
static u32 vop3_mode_done(struct vop2_video_port *vp)
{
   return VOP_MODULE_GET(vp->vop2, vp, out_mode);
}
 
static void vop3_set_out_mode(struct drm_crtc *crtc, u32 out_mode)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   int ret;
   u32 val;
 
   VOP_MODULE_SET(vop2, vp, out_mode, out_mode);
   vop2_cfg_done(crtc);
   ret = readx_poll_timeout(vop3_mode_done, vp, val, val == out_mode,
                1000, 500 * 1000);
   if (ret)
       dev_err(vop2->dev, "wait mode 0x%x timeout\n", out_mode);
}
 
static void vop3_crtc_send_mcu_cmd(struct drm_crtc *crtc, u32 type, u32 value)
{
   struct drm_crtc_state *crtc_state;
   struct drm_display_mode *adjusted_mode;
   struct vop2_video_port *vp;
   struct vop2 *vop2;
 
   if (!crtc)
       return;
 
   crtc_state = crtc->state;
   adjusted_mode = &crtc_state->adjusted_mode;
   vp = to_vop2_video_port(crtc);
   vop2 = vp->vop2;
 
   /*
    * 1.set mcu bypass mode timing.
    * 2.set dclk rate to 150M.
    */
   if ((type == MCU_SETBYPASS) && value) {
       vop3_mcu_bypass_mode_setup(crtc);
       clk_set_rate(vp->dclk, 150000000);
   }
 
   mutex_lock(&vop2->vop2_lock);
   if (vop2 && vop2->is_enabled) {
       switch (type) {
       case MCU_WRCMD:
           VOP_MODULE_SET(vop2, vp, mcu_rs, 0);
           VOP_MODULE_SET(vop2, vp, mcu_rw_bypass_port, value);
           VOP_MODULE_SET(vop2, vp, mcu_rs, 1);
           break;
       case MCU_WRDATA:
           VOP_MODULE_SET(vop2, vp, mcu_rs, 1);
           VOP_MODULE_SET(vop2, vp, mcu_rw_bypass_port, value);
           break;
       case MCU_SETBYPASS:
           VOP_MODULE_SET(vop2, vp, mcu_bypass, value ? 1 : 0);
           break;
       default:
           break;
       }
   }
   mutex_unlock(&vop2->vop2_lock);
 
   /*
    * 1.restore mcu data mode timing.
    * 2.restore dclk rate to crtc_clock.
    */
   if ((type == MCU_SETBYPASS) && !value) {
       vop3_mcu_mode_setup(crtc);
       clk_set_rate(vp->dclk, adjusted_mode->crtc_clock * 1000);
   }
}
 
static int vop2_crtc_wait_vact_end(struct drm_crtc *crtc, unsigned int mstimeout)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   unsigned long jiffies_left;
   int ret = 0;
 
   if (!vop2->is_enabled)
       return -ENODEV;
 
   mutex_lock(&vop2->vop2_lock);
 
   if (vop2_crtc_line_flag_irq_is_enabled(vp)) {
       ret = -EBUSY;
       goto out;
   }
 
   reinit_completion(&vp->line_flag_completion);
   vop2_crtc_line_flag_irq_enable(vp);
   jiffies_left = wait_for_completion_timeout(&vp->line_flag_completion,
                          msecs_to_jiffies(mstimeout));
   vop2_crtc_line_flag_irq_disable(vp);
 
   if (jiffies_left == 0) {
       DRM_DEV_ERROR(vop2->dev, "timeout waiting for lineflag IRQ\n");
       ret = -ETIMEDOUT;
       goto out;
   }
 
out:
   mutex_unlock(&vop2->vop2_lock);
   return ret;
}
 
static int vop2_crtc_enable_line_flag_event(struct drm_crtc *crtc, uint32_t line)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
   unsigned long flags;
 
   if (WARN_ON(!vop2->is_enabled))
       return -EPERM;
 
   spin_lock_irqsave(&vop2->irq_lock, flags);
 
   VOP_INTR_SET(vop2, intr, line_flag_num[1], line);
 
   VOP_INTR_SET_TYPE(vop2, intr, clear, LINE_FLAG1_INTR, 1);
   VOP_INTR_SET_TYPE(vop2, intr, enable, LINE_FLAG1_INTR, 1);
 
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
 
   return 0;
}
 
static void vop2_crtc_disable_line_flag_event(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
   unsigned long flags;
 
   if (WARN_ON(!vop2->is_enabled))
       return;
 
   spin_lock_irqsave(&vop2->irq_lock, flags);
 
   VOP_INTR_SET_TYPE(vop2, intr, enable, LINE_FLAG1_INTR, 0);
 
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
}
 
static int vop2_crtc_get_inital_acm_info(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct post_acm *acm = &vp->acm_info;
   s16 *lut_y;
   s16 *lut_h;
   s16 *lut_s;
   u32 value;
   int i;
 
   value = readl(vop2->acm_regs + RK3528_ACM_CTRL);
   acm->acm_enable = value & 0x1;
   value = readl(vop2->acm_regs + RK3528_ACM_DELTA_RANGE);
   acm->y_gain = value & 0x3ff;
   acm->h_gain = (value >> 10) & 0x3ff;
   acm->s_gain = (value >> 20) & 0x3ff;
 
   lut_y = &acm->gain_lut_hy[0];
   lut_h = &acm->gain_lut_hy[ACM_GAIN_LUT_HY_LENGTH];
   lut_s = &acm->gain_lut_hy[ACM_GAIN_LUT_HY_LENGTH * 2];
   for (i = 0; i < ACM_GAIN_LUT_HY_LENGTH; i++) {
       value = readl(vop2->acm_regs + RK3528_ACM_YHS_DEL_HY_SEG0 + (i << 2));
       lut_y[i] = value & 0xff;
       lut_h[i] = (value >> 8) & 0xff;
       lut_s[i] = (value >> 16) & 0xff;
   }
 
   lut_y = &acm->gain_lut_hs[0];
   lut_h = &acm->gain_lut_hs[ACM_GAIN_LUT_HS_LENGTH];
   lut_s = &acm->gain_lut_hs[ACM_GAIN_LUT_HS_LENGTH * 2];
   for (i = 0; i < ACM_GAIN_LUT_HS_LENGTH; i++) {
       value = readl(vop2->acm_regs + RK3528_ACM_YHS_DEL_HS_SEG0 + (i << 2));
       lut_y[i] = value & 0xff;
       lut_h[i] = (value >> 8) & 0xff;
       lut_s[i] = (value >> 16) & 0xff;
   }
 
   lut_y = &acm->delta_lut_h[0];
   lut_h = &acm->delta_lut_h[ACM_DELTA_LUT_H_LENGTH];
   lut_s = &acm->delta_lut_h[ACM_DELTA_LUT_H_LENGTH * 2];
   for (i = 0; i < ACM_DELTA_LUT_H_LENGTH; i++) {
       value = readl(vop2->acm_regs + RK3528_ACM_YHS_DEL_HGAIN_SEG0 + (i << 2));
       lut_y[i] = value & 0x3ff;
       lut_h[i] = (value >> 12) & 0xff;
       lut_s[i] = (value >> 20) & 0x3ff;
   }
 
   return 0;
}
 
static int vop2_crtc_loader_protect(struct drm_crtc *crtc, bool on, void *data)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2 *vop2 = vp->vop2;
   struct rockchip_drm_private *private = crtc->dev->dev_private;
   const struct vop2_video_port_data *vp_data = &vop2->data->vp[vp->id];
   struct vop2_video_port *splice_vp = &vop2->vps[vp_data->splice_vp_id];
   struct drm_crtc_state *crtc_state;
   struct drm_display_mode *mode;
   struct vop2_win *win, *splice_win;
   struct vop2_extend_pll *ext_pll;
   struct clk *parent_clk;
   const char *clk_name;
 
   if (on == vp->loader_protect)
       return 0;
 
   if (on) {
       vp->loader_protect = true;
       vop2->active_vp_mask |= BIT(vp->id);
       vop2_set_system_status(vop2);
       vop2_initial(crtc);
       if (crtc->primary) {
           win = to_vop2_win(crtc->primary);
           if (VOP_WIN_GET(vop2, win, enable)) {
               if (win->pd) {
                   win->pd->ref_count++;
                   win->pd->vp_mask |= BIT(vp->id);
               }
 
               vp->enabled_win_mask |= BIT(win->phys_id);
               crtc_state = drm_atomic_get_crtc_state(crtc->state->state, crtc);
               mode = &crtc_state->adjusted_mode;
               if (mode->hdisplay > VOP2_MAX_VP_OUTPUT_WIDTH)    {
                   vcstate->splice_mode = true;
                   splice_win = vop2_find_win_by_phys_id(vop2,
                                         win->splice_win_id);
                   splice_win->splice_mode_right = true;
                   splice_win->left_win = win;
                   win->splice_win = splice_win;
                   splice_vp->win_mask |=  BIT(splice_win->phys_id);
                   splice_win->vp_mask = BIT(splice_vp->id);
                   vop2->active_vp_mask |= BIT(splice_vp->id);
                   vp->enabled_win_mask |= BIT(splice_win->phys_id);
 
                   if (splice_win->pd &&
                       VOP_WIN_GET(vop2, splice_win, enable)) {
                       splice_win->pd->ref_count++;
                       splice_win->pd->vp_mask |= BIT(splice_vp->id);
                   }
               }
           }
       }
       parent_clk = clk_get_parent(vp->dclk);
       clk_name = __clk_get_name(parent_clk);
       if (!strcmp(clk_name, "clk_hdmiphy_pixel0")) {
           ext_pll = vop2_extend_clk_find_by_name(vop2, "hdmi0_phy_pll");
           if (ext_pll)
               ext_pll->vp_mask |= BIT(vp->id);
       } else if (!strcmp(clk_name, "clk_hdmiphy_pixel1")) {
           ext_pll = vop2_extend_clk_find_by_name(vop2, "hdmi1_phy_pll");
           if (ext_pll)
               ext_pll->vp_mask |= BIT(vp->id);
       }
       drm_crtc_vblank_on(crtc);
       if (is_vop3(vop2)) {
           if (vp_data->feature & (VOP_FEATURE_POST_ACM))
               vop2_crtc_get_inital_acm_info(crtc);
           if (data && (vp_data->feature & VOP_FEATURE_POST_CSC))
               memcpy(&vp->csc_info, data, sizeof(struct post_csc));
       }
       if (private->cubic_lut[vp->id].enable) {
           dma_addr_t cubic_lut_mst;
           struct loader_cubic_lut *cubic_lut = &private->cubic_lut[vp->id];
 
           cubic_lut_mst = cubic_lut->offset + private->cubic_lut_dma_addr;
           VOP_MODULE_SET(vop2, vp, cubic_lut_mst, cubic_lut_mst);
       }
   } else {
       vop2_crtc_atomic_disable(crtc, NULL);
   }
 
   return 0;
}
 
#define DEBUG_PRINT(args...) \
       do { \
           if (s) \
               seq_printf(s, args); \
           else \
               pr_err(args); \
       } while (0)
 
static int vop2_plane_info_dump(struct seq_file *s, struct drm_plane *plane)
{
   struct vop2_win *win = to_vop2_win(plane);
   struct drm_plane_state *pstate = plane->state;
   struct vop2_plane_state *vpstate = to_vop2_plane_state(pstate);
   struct drm_rect *src, *dest;
   struct drm_framebuffer *fb = pstate->fb;
   struct drm_format_name_buf format_name;
   struct drm_gem_object *obj;
   struct rockchip_gem_object *rk_obj;
   dma_addr_t fb_addr;
 
   int i;
 
   DEBUG_PRINT("    %s: %s\n", win->name, pstate->crtc ? "ACTIVE" : "DISABLED");
   if (!fb)
       return 0;
 
   src = &vpstate->src;
   dest = &vpstate->dest;
 
   DEBUG_PRINT("\twin_id: %d\n", win->win_id);
 
   drm_get_format_name(fb->format->format, &format_name);
   DEBUG_PRINT("\tformat: %s%s%s[%d] color_space[%d] glb_alpha[0x%x]\n",
           format_name.str,
           modifier_to_string(fb->modifier),
           vpstate->eotf ? " HDR" : " SDR", vpstate->eotf,
           vpstate->color_space, vpstate->global_alpha);
   DEBUG_PRINT("\trotate: xmirror: %d ymirror: %d rotate_90: %d rotate_270: %d\n",
           vpstate->xmirror_en, vpstate->ymirror_en, vpstate->rotate_90_en,
           vpstate->rotate_270_en);
   DEBUG_PRINT("\tcsc: y2r[%d] r2y[%d] csc mode[%d]\n",
           vpstate->y2r_en, vpstate->r2y_en,
           vpstate->csc_mode);
   DEBUG_PRINT("\tzpos: %d\n", vpstate->zpos);
   DEBUG_PRINT("\tsrc: pos[%d, %d] rect[%d x %d]\n", src->x1 >> 16,
           src->y1 >> 16, drm_rect_width(src) >> 16,
           drm_rect_height(src) >> 16);
   DEBUG_PRINT("\tdst: pos[%d, %d] rect[%d x %d]\n", dest->x1, dest->y1,
           drm_rect_width(dest), drm_rect_height(dest));
 
   for (i = 0; i < fb->format->num_planes; i++) {
       obj = fb->obj[0];
       rk_obj = to_rockchip_obj(obj);
       fb_addr = rk_obj->dma_addr + fb->offsets[0];
 
       DEBUG_PRINT("\tbuf[%d]: addr: %pad pitch: %d offset: %d\n",
               i, &fb_addr, fb->pitches[i], fb->offsets[i]);
   }
 
   return 0;
}
 
static void vop2_dump_connector_on_crtc(struct drm_crtc *crtc, struct seq_file *s)
{
   struct drm_connector_list_iter conn_iter;
   struct drm_connector *connector;
 
   drm_connector_list_iter_begin(crtc->dev, &conn_iter);
   drm_for_each_connector_iter(connector, &conn_iter) {
       if (crtc->state->connector_mask & drm_connector_mask(connector))
           DEBUG_PRINT("    Connector: %s\n", connector->name);
 
   }
   drm_connector_list_iter_end(&conn_iter);
}
 
static int vop2_crtc_debugfs_dump(struct drm_crtc *crtc, struct seq_file *s)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct drm_crtc_state *crtc_state = crtc->state;
   struct drm_display_mode *mode = &crtc->state->adjusted_mode;
   struct rockchip_crtc_state *state = to_rockchip_crtc_state(crtc->state);
   bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
   struct drm_plane *plane;
 
   DEBUG_PRINT("Video Port%d: %s\n", vp->id, crtc_state->active ? "ACTIVE" : "DISABLED");
 
   if (!crtc_state->active)
       return 0;
 
   vop2_dump_connector_on_crtc(crtc, s);
   DEBUG_PRINT("\tbus_format[%x]: %s\n", state->bus_format,
           drm_get_bus_format_name(state->bus_format));
   DEBUG_PRINT("\toverlay_mode[%d] output_mode[%x]",
           state->yuv_overlay, state->output_mode);
   DEBUG_PRINT(" color_space[%d], eotf:%d\n",
           state->color_space, state->eotf);
   DEBUG_PRINT("    Display mode: %dx%d%s%d\n",
           mode->hdisplay, mode->vdisplay, interlaced ? "i" : "p",
           drm_mode_vrefresh(mode));
   DEBUG_PRINT("\tclk[%d] real_clk[%d] type[%x] flag[%x]\n",
           mode->clock, mode->crtc_clock, mode->type, mode->flags);
   DEBUG_PRINT("\tH: %d %d %d %d\n", mode->hdisplay, mode->hsync_start,
           mode->hsync_end, mode->htotal);
   DEBUG_PRINT("\tV: %d %d %d %d\n", mode->vdisplay, mode->vsync_start,
           mode->vsync_end, mode->vtotal);
 
   drm_atomic_crtc_for_each_plane(plane, crtc) {
       vop2_plane_info_dump(s, plane);
   }
 
   return 0;
}
 
static void vop2_crtc_regs_dump(struct drm_crtc *crtc, struct seq_file *s)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   struct drm_crtc_state *cstate = crtc->state;
   const struct vop_dump_regs *regs = vop2->data->dump_regs;
   uint32_t buf[68];
   uint32_t len = ARRAY_SIZE(buf);
   unsigned int n, i, j;
   resource_size_t offset_addr;
   uint32_t base;
   struct drm_crtc *first_active_crtc = NULL;
 
   if (!cstate->active)
       return;
 
   /* only need to dump once at first active crtc for vop2 */
   for (i = 0; i < vop2_data->nr_vps; i++) {
       if (vop2->vps[i].rockchip_crtc.crtc.state->active) {
           first_active_crtc = &vop2->vps[i].rockchip_crtc.crtc;
           break;
       }
   }
   if (first_active_crtc != crtc)
       return;
 
   n = vop2->data->dump_regs_size;
   for (i = 0; i < n; i++) {
       base = regs[i].offset;
       offset_addr = vop2->res->start + base;
       DEBUG_PRINT("\n%s:\n", regs[i].name);
       for (j = 0; j < len;) {
           DEBUG_PRINT("%08x:  %08x %08x %08x %08x\n", (u32)offset_addr + j * 4,
                   vop2_readl(vop2, base + (4 * j)),
                   vop2_readl(vop2, base + (4 * (j + 1))),
                   vop2_readl(vop2, base + (4 * (j + 2))),
                   vop2_readl(vop2, base + (4 * (j + 3))));
           j += 4;
       }
   }
}
 
static void vop2_crtc_active_regs_dump(struct drm_crtc *crtc, struct seq_file *s)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   struct drm_crtc_state *cstate = crtc->state;
   const struct vop_dump_regs *regs = vop2->data->dump_regs;
   uint32_t buf[68];
   uint32_t len = ARRAY_SIZE(buf);
   unsigned int n, i, j;
   resource_size_t offset_addr;
   uint32_t base;
   struct drm_crtc *first_active_crtc = NULL;
 
   if (!cstate->active)
       return;
 
   /* only need to dump once at first active crtc for vop2 */
   for (i = 0; i < vop2_data->nr_vps; i++) {
       if (vop2->vps[i].rockchip_crtc.crtc.state->active) {
           first_active_crtc = &vop2->vps[i].rockchip_crtc.crtc;
           break;
       }
   }
   if (first_active_crtc != crtc)
       return;
 
   n = vop2->data->dump_regs_size;
   for (i = 0; i < n; i++) {
       if (regs[i].state.mask &&
           REG_GET(vop2, regs[i].state) != regs[i].enable_state)
           continue;
       base = regs[i].offset;
       offset_addr = vop2->res->start + base;
       DEBUG_PRINT("\n%s:\n", regs[i].name);
       for (j = 0; j < len;) {
           DEBUG_PRINT("%08x:  %08x %08x %08x %08x\n", (u32)offset_addr + j * 4,
                   vop2_readl(vop2, base + (4 * j)),
                   vop2_readl(vop2, base + (4 * (j + 1))),
                   vop2_readl(vop2, base + (4 * (j + 2))),
                   vop2_readl(vop2, base + (4 * (j + 3))));
           j += 4;
       }
   }
}
 
static int vop2_gamma_show(struct seq_file *s, void *data)
{
   struct drm_info_node *node = s->private;
   struct vop2 *vop2 = node->info_ent->data;
   int i, j;
 
   for (i = 0; i < vop2->data->nr_vps; i++) {
       struct vop2_video_port *vp = &vop2->vps[i];
 
       if (!vp->lut || !vp->gamma_lut_active ||
           !vop2->lut_regs || !vp->rockchip_crtc.crtc.state->enable) {
           DEBUG_PRINT("Video port%d gamma disabled\n", vp->id);
           continue;
       }
       DEBUG_PRINT("Video port%d gamma:\n", vp->id);
       for (j = 0; j < vp->gamma_lut_len; j++) {
           if (j % 8 == 0)
               DEBUG_PRINT("\n");
           DEBUG_PRINT("0x%08x ", vp->lut[j]);
       }
       DEBUG_PRINT("\n");
   }
 
   return 0;
}
 
static int vop2_cubic_lut_show(struct seq_file *s, void *data)
{
   struct drm_info_node *node = s->private;
   struct vop2 *vop2 = node->info_ent->data;
   struct rockchip_drm_private *private = vop2->drm_dev->dev_private;
   int i, j;
 
   for (i = 0; i < vop2->data->nr_vps; i++) {
       struct vop2_video_port *vp = &vop2->vps[i];
 
       if ((!vp->cubic_lut_gem_obj && !private->cubic_lut[vp->id].enable) ||
           !vp->cubic_lut || !vp->rockchip_crtc.crtc.state->enable) {
           DEBUG_PRINT("Video port%d cubic lut disabled\n", vp->id);
           continue;
       }
       DEBUG_PRINT("Video port%d cubic lut:\n", vp->id);
       for (j = 0; j < vp->cubic_lut_len; j++) {
           DEBUG_PRINT("%04d: 0x%04x 0x%04x 0x%04x\n", j,
                   vp->cubic_lut[j].red,
                   vp->cubic_lut[j].green,
                   vp->cubic_lut[j].blue);
       }
       DEBUG_PRINT("\n");
   }
 
   return 0;
}
 
#undef DEBUG_PRINT
 
static struct drm_info_list vop2_debugfs_files[] = {
   { "gamma_lut", vop2_gamma_show, 0, NULL },
   { "cubic_lut", vop2_cubic_lut_show, 0, NULL },
};
 
static int vop2_crtc_debugfs_init(struct drm_minor *minor, struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   int ret, i;
   char name[12];
 
   snprintf(name, sizeof(name), "video_port%d", vp->id);
   vop2->debugfs = debugfs_create_dir(name, minor->debugfs_root);
   if (!vop2->debugfs)
       return -ENOMEM;
 
   vop2->debugfs_files = kmemdup(vop2_debugfs_files, sizeof(vop2_debugfs_files),
                     GFP_KERNEL);
   if (!vop2->debugfs_files) {
       ret = -ENOMEM;
       goto remove;
   }
#if defined(CONFIG_ROCKCHIP_DRM_DEBUG)
   rockchip_drm_add_dump_buffer(crtc, vop2->debugfs);
   rockchip_drm_debugfs_add_color_bar(crtc, vop2->debugfs);
#endif
   for (i = 0; i < ARRAY_SIZE(vop2_debugfs_files); i++)
       vop2->debugfs_files[i].data = vop2;
 
   drm_debugfs_create_files(vop2->debugfs_files,
                ARRAY_SIZE(vop2_debugfs_files),
                vop2->debugfs,
                minor);
   return 0;
remove:
   debugfs_remove(vop2->debugfs);
   vop2->debugfs = NULL;
   return ret;
}
 
static enum drm_mode_status
vop2_crtc_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode)
{
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   int request_clock = mode->clock;
   int clock;
   unsigned long aclk_rate;
   uint8_t active_vp_mask = vop2->active_vp_mask;
 
   /*
    * For RK3588, VP0 and VP1 will be both used in splice mode. All display
    * modes of the right VP should be set as invalid when vop2 is working in
    * splice mode.
    */
   if (vp->splice_mode_right)
       return MODE_BAD;
 
   if ((active_vp_mask & BIT(ROCKCHIP_VOP_VP1)) && !vcstate->splice_mode &&
       mode->hdisplay > VOP2_MAX_VP_OUTPUT_WIDTH) {
       DRM_DEV_DEBUG(vop2->dev, "can not support resolution %dx%d, vp1 is busy\n",
                 mode->hdisplay, mode->vdisplay);
       return MODE_BAD;
   }
 
   if (mode->hdisplay > vp_data->max_output.width)
       return MODE_BAD_HVALUE;
 
   if (mode->flags & DRM_MODE_FLAG_DBLCLK || vcstate->output_if & VOP_OUTPUT_IF_BT656)
       request_clock *= 2;
 
   aclk_rate = clk_get_rate(vop2->aclk) / 1000;
 
   if (request_clock > VOP2_MAX_DCLK_RATE && aclk_rate <= VOP2_COMMON_ACLK_RATE)
       return MODE_BAD;
 
   if ((request_clock <= VOP2_MAX_DCLK_RATE) &&
       (vop2_extend_clk_find_by_name(vop2, "hdmi0_phy_pll") ||
        vop2_extend_clk_find_by_name(vop2, "hdmi1_phy_pll"))) {
       clock = request_clock;
   } else {
       if (request_clock > VOP2_MAX_DCLK_RATE)
           request_clock = request_clock >> 2;
       clock = clk_round_rate(vp->dclk, request_clock * 1000) / 1000;
   }
 
   /*
    * Hdmi or DisplayPort request a Accurate clock.
    */
   if (vcstate->output_type == DRM_MODE_CONNECTOR_HDMIA ||
       vcstate->output_type == DRM_MODE_CONNECTOR_DisplayPort)
       if (clock != request_clock)
           return MODE_CLOCK_RANGE;
 
   return MODE_OK;
}
 
struct vop2_bandwidth {
   size_t bandwidth;
   int y1;
   int y2;
};
 
static int vop2_bandwidth_cmp(const void *a, const void *b)
{
   struct vop2_bandwidth *pa = (struct vop2_bandwidth *)a;
   struct vop2_bandwidth *pb = (struct vop2_bandwidth *)b;
 
   return pa->y1 - pb->y2;
}
 
static size_t vop2_plane_line_bandwidth(struct drm_plane_state *pstate)
{
   struct vop2_plane_state *vpstate = to_vop2_plane_state(pstate);
   struct drm_framebuffer *fb = pstate->fb;
   struct drm_rect *dst = &vpstate->dest;
   struct drm_rect *src = &vpstate->src;
   int bpp = rockchip_drm_get_bpp(fb->format);
   int src_width = drm_rect_width(src) >> 16;
   int src_height = drm_rect_height(src) >> 16;
   int dst_width = drm_rect_width(dst);
   int dst_height = drm_rect_height(dst);
   int vskiplines = scl_get_vskiplines(src_height, dst_height);
   size_t bandwidth;
 
   if (src_width <= 0 || src_height <= 0 || dst_width <= 0 ||
       dst_height <= 0)
       return 0;
 
   bandwidth = src_width * bpp / 8;
 
   bandwidth = bandwidth * src_width / dst_width;
   bandwidth = bandwidth * src_height / dst_height;
   if (vskiplines == 2 && vpstate->afbc_en == 0)
       bandwidth /= 2;
   else if (vskiplines == 4 && vpstate->afbc_en == 0)
       bandwidth /= 4;
 
   return bandwidth;
}
 
static u64 vop2_calc_max_bandwidth(struct vop2_bandwidth *bw, int start,
                  int count, int y2)
{
   u64 max_bandwidth = 0;
   int i;
 
   for (i = start; i < count; i++) {
       u64 bandwidth = 0;
 
       if (bw[i].y1 > y2)
           continue;
       bandwidth = bw[i].bandwidth;
       bandwidth += vop2_calc_max_bandwidth(bw, i + 1, count,
                           min(bw[i].y2, y2));
 
       if (bandwidth > max_bandwidth)
           max_bandwidth = bandwidth;
   }
 
   return max_bandwidth;
}
 
static size_t vop2_crtc_bandwidth(struct drm_crtc *crtc,
                 struct drm_crtc_state *crtc_state,
                 struct dmcfreq_vop_info *vop_bw_info)
{
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
   uint16_t htotal = adjusted_mode->crtc_htotal;
   uint16_t vdisplay = adjusted_mode->crtc_vdisplay;
   int clock = adjusted_mode->crtc_clock;
   struct drm_atomic_state *state = crtc_state->state;
   struct vop2_plane_state *vpstate;
   struct drm_plane_state *pstate;
   struct vop2_bandwidth *pbandwidth;
   struct drm_plane *plane;
   u64 line_bw_mbyte = 0;
   int8_t cnt = 0, plane_num = 0;
   int i = 0;
#if defined(CONFIG_ROCKCHIP_DRM_DEBUG)
   struct vop_dump_list *pos, *n;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
#endif
 
   if (!htotal || !vdisplay)
       return 0;
 
#if defined(CONFIG_ROCKCHIP_DRM_DEBUG)
   if (!vp->rockchip_crtc.vop_dump_list_init_flag) {
       INIT_LIST_HEAD(&vp->rockchip_crtc.vop_dump_list_head);
       vp->rockchip_crtc.vop_dump_list_init_flag = true;
   }
   list_for_each_entry_safe(pos, n, &vp->rockchip_crtc.vop_dump_list_head, entry) {
       list_del(&pos->entry);
   }
   if (vp->rockchip_crtc.vop_dump_status == DUMP_KEEP ||
       vp->rockchip_crtc.vop_dump_times > 0) {
       vp->rockchip_crtc.frame_count++;
   }
#endif
 
   for_each_new_plane_in_state(state, plane, pstate, i) {
       if (pstate->crtc == crtc)
           plane_num++;
   }
 
   vop_bw_info->plane_num += plane_num;
   pbandwidth = kmalloc_array(plane_num, sizeof(*pbandwidth),
                  GFP_KERNEL);
   if (!pbandwidth)
       return -ENOMEM;
 
   for_each_new_plane_in_state(state, plane, pstate, i) {
       int act_w, act_h, bpp, afbc_fac;
       int fps = drm_mode_vrefresh(adjusted_mode);
 
       if (!pstate || pstate->crtc != crtc || !pstate->fb)
           continue;
 
       /* This is an empirical value, if it's afbc format, the frame buffer size div 2 */
       afbc_fac = rockchip_afbc(plane, pstate->fb->modifier) ? 2 : 1;
 
       vpstate = to_vop2_plane_state(pstate);
       pbandwidth[cnt].y1 = vpstate->dest.y1;
       pbandwidth[cnt].y2 = vpstate->dest.y2;
       pbandwidth[cnt++].bandwidth = vop2_plane_line_bandwidth(pstate) / afbc_fac;
 
       act_w = drm_rect_width(&pstate->src) >> 16;
       act_h = drm_rect_height(&pstate->src) >> 16;
       if (pstate->fb->format->is_yuv && (act_w >= 3840 || act_h >= 3840))
           vop_bw_info->plane_num_4k++;
 
       bpp = rockchip_drm_get_bpp(pstate->fb->format);
 
       vop_bw_info->frame_bw_mbyte += act_w * act_h / 1000 * bpp / 8 * fps / 1000 / afbc_fac;
   }
 
   sort(pbandwidth, cnt, sizeof(pbandwidth[0]), vop2_bandwidth_cmp, NULL);
 
   line_bw_mbyte = vop2_calc_max_bandwidth(pbandwidth, 0, cnt, vdisplay);
   kfree(pbandwidth);
   /*
    * line_bandwidth(MB/s)
    *    = line_bandwidth / line_time
    *    = line_bandwidth(Byte) * clock(KHZ) / 1000 / htotal
    */
   line_bw_mbyte *= clock;
   do_div(line_bw_mbyte, htotal * 1000);
   vop_bw_info->line_bw_mbyte = line_bw_mbyte;
 
   return 0;
}
 
static void vop2_crtc_close(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
 
   if (!crtc)
       return;
 
   mutex_lock(&vop2->vop2_lock);
   if (!vop2->is_enabled) {
       mutex_unlock(&vop2->vop2_lock);
       return;
   }
 
   vop2_disable_all_planes_for_crtc(crtc);
   mutex_unlock(&vop2->vop2_lock);
}
 
static void vop2_crtc_te_handler(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
 
   if (!crtc || !crtc->state->active)
       return;
 
   VOP_MODULE_SET(vop2, vp, edpi_wms_fs, 1);
}
 
static int vop2_crtc_set_color_bar(struct drm_crtc *crtc, enum rockchip_color_bar_mode mode)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   int ret = 0;
 
   if (!crtc->state->active) {
       DRM_INFO("Video port%d disabled\n", vp->id);
       return -EINVAL;
   }
 
   switch (mode) {
   case ROCKCHIP_COLOR_BAR_OFF:
       DRM_INFO("disable color bar in VP%d\n", vp->id);
       VOP_MODULE_SET(vop2, vp, color_bar_en, 0);
       vop2_cfg_done(crtc);
       break;
   case ROCKCHIP_COLOR_BAR_HORIZONTAL:
       DRM_INFO("enable horizontal color bar in VP%d\n", vp->id);
       VOP_MODULE_SET(vop2, vp, color_bar_mode, 0);
       VOP_MODULE_SET(vop2, vp, color_bar_en, 1);
       vop2_cfg_done(crtc);
       break;
   case ROCKCHIP_COLOR_BAR_VERTICAL:
       DRM_INFO("enable vertical color bar in VP%d\n", vp->id);
       VOP_MODULE_SET(vop2, vp, color_bar_mode, 1);
       VOP_MODULE_SET(vop2, vp, color_bar_en, 1);
       vop2_cfg_done(crtc);
       break;
   default:
       DRM_INFO("Unsupported color bar mode\n");
       ret = -EINVAL;
       break;
   }
 
   return ret;
}
 
static const struct rockchip_crtc_funcs private_crtc_funcs = {
   .loader_protect = vop2_crtc_loader_protect,
   .cancel_pending_vblank = vop2_crtc_cancel_pending_vblank,
   .debugfs_init = vop2_crtc_debugfs_init,
   .debugfs_dump = vop2_crtc_debugfs_dump,
   .regs_dump = vop2_crtc_regs_dump,
   .active_regs_dump = vop2_crtc_active_regs_dump,
   .bandwidth = vop2_crtc_bandwidth,
   .crtc_close = vop2_crtc_close,
   .te_handler = vop2_crtc_te_handler,
   .crtc_send_mcu_cmd = vop3_crtc_send_mcu_cmd,
   .wait_vact_end = vop2_crtc_wait_vact_end,
   .crtc_standby = vop2_crtc_standby,
   .crtc_set_color_bar = vop2_crtc_set_color_bar,
};
 
static bool vop2_crtc_mode_fixup(struct drm_crtc *crtc,
                const struct drm_display_mode *mode,
                struct drm_display_mode *adj_mode)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct drm_connector *connector;
   struct drm_connector_list_iter conn_iter;
   struct drm_crtc_state *new_crtc_state = container_of(mode, struct drm_crtc_state, mode);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(new_crtc_state);
 
   /*
    * For RK3568 and RK3588, the hactive of video timing must
    * be 4-pixel aligned.
    */
   if (vop2->version == VOP_VERSION_RK3568 || vop2->version == VOP_VERSION_RK3588) {
       if (adj_mode->hdisplay % 4) {
           u16 old_hdisplay = adj_mode->hdisplay;
           u16 align;
 
           align = 4 - (adj_mode->hdisplay % 4);
           adj_mode->hdisplay += align;
           adj_mode->hsync_start += align;
           adj_mode->hsync_end += align;
           adj_mode->htotal += align;
 
           DRM_WARN("VP%d: hactive need to be aligned with 4-pixel, %d -> %d\n",
                vp->id, old_hdisplay, adj_mode->hdisplay);
       }
   }
 
   drm_mode_set_crtcinfo(adj_mode, CRTC_INTERLACE_HALVE_V | CRTC_STEREO_DOUBLE);
 
   if (mode->flags & DRM_MODE_FLAG_DBLCLK || vcstate->output_if & VOP_OUTPUT_IF_BT656)
       adj_mode->crtc_clock *= 2;
 
   if (vp->mcu_timing.mcu_pix_total)
       adj_mode->crtc_clock *= rockchip_drm_get_cycles_per_pixel(vcstate->bus_format) *
                   (vp->mcu_timing.mcu_pix_total + 1);
 
   drm_connector_list_iter_begin(crtc->dev, &conn_iter);
   drm_for_each_connector_iter(connector, &conn_iter) {
       if ((new_crtc_state->connector_mask & drm_connector_mask(connector)) &&
           ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
            (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA))) {
           drm_connector_list_iter_end(&conn_iter);
           return true;
       }
   }
   drm_connector_list_iter_end(&conn_iter);
 
   if (adj_mode->crtc_clock <= VOP2_MAX_DCLK_RATE)
       adj_mode->crtc_clock = DIV_ROUND_UP(clk_round_rate(vp->dclk,
                           adj_mode->crtc_clock * 1000), 1000);
   return true;
}
 
static void vop2_dither_setup(struct rockchip_crtc_state *vcstate, struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   bool pre_dither_down_en = false;
 
   switch (vcstate->bus_format) {
   case MEDIA_BUS_FMT_RGB565_1X16:
       VOP_MODULE_SET(vop2, vp, dither_down_en, 1);
       VOP_MODULE_SET(vop2, vp, dither_down_mode, RGB888_TO_RGB565);
       pre_dither_down_en = true;
       break;
   case MEDIA_BUS_FMT_RGB666_1X18:
   case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
   case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
       VOP_MODULE_SET(vop2, vp, dither_down_en, 1);
       VOP_MODULE_SET(vop2, vp, dither_down_mode, RGB888_TO_RGB666);
       pre_dither_down_en = true;
       break;
   case MEDIA_BUS_FMT_YUYV8_1X16:
   case MEDIA_BUS_FMT_YUV8_1X24:
   case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
       VOP_MODULE_SET(vop2, vp, dither_down_en, 0);
       pre_dither_down_en = true;
       break;
   case MEDIA_BUS_FMT_YUYV10_1X20:
   case MEDIA_BUS_FMT_YUV10_1X30:
   case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
   case MEDIA_BUS_FMT_RGB101010_1X30:
       VOP_MODULE_SET(vop2, vp, dither_down_en, 0);
       pre_dither_down_en = false;
       break;
   case MEDIA_BUS_FMT_RGB888_3X8:
   case MEDIA_BUS_FMT_RGB888_DUMMY_4X8:
   case MEDIA_BUS_FMT_RGB888_1X24:
   case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
   case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
   default:
       VOP_MODULE_SET(vop2, vp, dither_down_en, 0);
       pre_dither_down_en = true;
       break;
   }
 
   if (is_yuv_output(vcstate->bus_format))
       pre_dither_down_en = false;
 
   VOP_MODULE_SET(vop2, vp, pre_dither_down_en, pre_dither_down_en);
   VOP_MODULE_SET(vop2, vp, dither_down_sel, DITHER_DOWN_ALLEGRO);
}
 
static void vop2_post_config(struct drm_crtc *crtc)
{
   struct rockchip_crtc_state *vcstate =
           to_rockchip_crtc_state(crtc->state);
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   struct drm_display_mode *mode = &crtc->state->adjusted_mode;
   u16 vtotal = mode->crtc_vtotal;
   u16 hdisplay = mode->crtc_hdisplay;
   u16 hact_st = mode->crtc_htotal - mode->crtc_hsync_start;
   u16 vdisplay = mode->crtc_vdisplay;
   u16 vact_st = mode->crtc_vtotal - mode->crtc_vsync_start;
   u16 hsize = hdisplay * (vcstate->left_margin + vcstate->right_margin) / 200;
   u16 vsize = vdisplay * (vcstate->top_margin + vcstate->bottom_margin) / 200;
   u16 hact_end, vact_end;
   u32 val;
 
   vsize = rounddown(vsize, 2);
   hsize = rounddown(hsize, 2);
   hact_st += hdisplay * (100 - vcstate->left_margin) / 200;
   hact_end = hact_st + hsize;
   val = hact_st << 16;
   val |= hact_end;
   VOP_MODULE_SET(vop2, vp, hpost_st_end, val);
   vact_st += vdisplay * (100 - vcstate->top_margin) / 200;
   vact_end = vact_st + vsize;
   val = vact_st << 16;
   val |= vact_end;
   VOP_MODULE_SET(vop2, vp, vpost_st_end, val);
   val = scl_cal_scale2(vdisplay, vsize) << 16;
   val |= scl_cal_scale2(hdisplay, hsize);
   VOP_MODULE_SET(vop2, vp, post_scl_factor, val);
 
#define POST_HORIZONTAL_SCALEDOWN_EN(x)        ((x) << 0)
#define POST_VERTICAL_SCALEDOWN_EN(x)        ((x) << 1)
   VOP_MODULE_SET(vop2, vp, post_scl_ctrl,
              POST_HORIZONTAL_SCALEDOWN_EN(hdisplay != hsize) |
              POST_VERTICAL_SCALEDOWN_EN(vdisplay != vsize));
   if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
       u16 vact_st_f1 = vtotal + vact_st + 1;
       u16 vact_end_f1 = vact_st_f1 + vsize;
 
       val = vact_st_f1 << 16 | vact_end_f1;
       VOP_MODULE_SET(vop2, vp, vpost_st_end_f1, val);
   }
 
   /*
    * BCSH[R2Y] -> POST Linebuffer[post scale] -> the background R2Y will be deal by post_dsp_out_r2y
    *
    * POST Linebuffer[post scale] -> ACM[R2Y] -> the background R2Y will be deal by ACM[R2Y]
    */
   if (vp_data->feature & VOP_FEATURE_POST_ACM)
       VOP_MODULE_SET(vop2, vp, post_dsp_out_r2y, vcstate->yuv_overlay);
   else
       VOP_MODULE_SET(vop2, vp, post_dsp_out_r2y, is_yuv_output(vcstate->bus_format));
}
 
/*
 * if adjusted mode update, return true, else return false
 */
static bool vop2_crtc_mode_update(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
   u16 hsync_len = adjusted_mode->crtc_hsync_end -
               adjusted_mode->crtc_hsync_start;
   u16 hdisplay = adjusted_mode->crtc_hdisplay;
   u16 htotal = adjusted_mode->crtc_htotal;
   u16 hact_st = adjusted_mode->crtc_htotal -
               adjusted_mode->crtc_hsync_start;
   u16 hact_end = hact_st + hdisplay;
   u16 vdisplay = adjusted_mode->crtc_vdisplay;
   u16 vtotal = adjusted_mode->crtc_vtotal;
   u16 vsync_len = adjusted_mode->crtc_vsync_end -
               adjusted_mode->crtc_vsync_start;
   u16 vact_st = adjusted_mode->crtc_vtotal -
               adjusted_mode->crtc_vsync_start;
   u16 vact_end = vact_st + vdisplay;
   u32 htotal_sync = htotal << 16 | hsync_len;
   u32 hactive_st_end = hact_st << 16 | hact_end;
   u32 vactive_st_end = vact_st << 16 | vact_end;
   u32 crtc_clock = adjusted_mode->crtc_clock * 100;
 
   if (htotal_sync != VOP_MODULE_GET(vop2, vp, htotal_pw) ||
       hactive_st_end != VOP_MODULE_GET(vop2, vp, hact_st_end) ||
       vtotal != VOP_MODULE_GET(vop2, vp, dsp_vtotal) ||
       vsync_len != VOP_MODULE_GET(vop2, vp, dsp_vs_end) ||
       vactive_st_end != VOP_MODULE_GET(vop2, vp, vact_st_end) ||
       crtc_clock != clk_get_rate(vp->dclk))
       return true;
 
   return false;
}
 
static int vop2_cru_set_rate(struct vop2_clk *if_pixclk, struct vop2_clk *if_dclk)
{
   int ret = 0;
 
   if (if_pixclk) {
       ret =  clk_set_rate(if_pixclk->hw.clk, if_pixclk->rate);
       if (ret < 0) {
           DRM_DEV_ERROR(if_pixclk->vop2->dev, "set %s to %ld failed: %d\n",
                     clk_hw_get_name(&if_pixclk->hw), if_pixclk->rate, ret);
           return ret;
       }
   }
 
   if (if_dclk) {
       ret = clk_set_rate(if_dclk->hw.clk, if_dclk->rate);
       if (ret < 0)
           DRM_DEV_ERROR(if_dclk->vop2->dev, "set %s to %ld failed %d\n",
                     clk_hw_get_name(&if_dclk->hw), if_dclk->rate, ret);
   }
 
   return ret;
}
 
static int vop2_set_dsc_clk(struct drm_crtc *crtc, u8 dsc_id)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_dsc_data *dsc_data = &vop2_data->dsc[dsc_id];
   struct vop2_clk *dsc_txp_clk, *dsc_pxl_clk, *dsc_cds_clk, *dsc_txp_clk_parent;
   char clk_name[32];
   int ret = 0;
 
   /* set clk parent */
   snprintf(clk_name, sizeof(clk_name), "dclk%d", vp->id);
   dsc_txp_clk = vop2_clk_get(vop2, dsc_data->dsc_txp_clk_src_name);
   dsc_txp_clk_parent = vop2_clk_get(vop2, clk_name);
   if (!dsc_txp_clk || !dsc_txp_clk_parent) {
       DRM_DEV_ERROR(vop2->dev, "failed to get dsc clk\n");
       return -ENODEV;
   }
   ret = clk_set_parent(dsc_txp_clk->hw.clk, dsc_txp_clk_parent->hw.clk);
   if (ret < 0) {
       DRM_DEV_ERROR(vop2->dev, "failed to set parent(%s) for %s: %d\n",
                 __clk_get_name(dsc_txp_clk_parent->hw.clk),
                 __clk_get_name(dsc_txp_clk->hw.clk), ret);
       return ret;
   }
 
   /* set dsc txp clk rate */
   clk_set_rate(dsc_txp_clk->hw.clk, vcstate->dsc_txp_clk_rate);
 
   /* set dsc pxl clk rate */
   dsc_pxl_clk = vop2_clk_get(vop2, dsc_data->dsc_pxl_clk_name);
   if (!dsc_pxl_clk) {
       DRM_DEV_ERROR(vop2->dev, "failed to get dsc_pxl_clk\n");
       return -ENODEV;
   }
   clk_set_rate(dsc_pxl_clk->hw.clk, vcstate->dsc_pxl_clk_rate);
 
   /* set dsc cds clk rate */
   dsc_cds_clk = vop2_clk_get(vop2, dsc_data->dsc_cds_clk_name);
   if (!dsc_cds_clk) {
       DRM_DEV_ERROR(vop2->dev, "failed to get dsc_cds_clk\n");
       return -ENODEV;
   }
   clk_set_rate(dsc_cds_clk->hw.clk, vcstate->dsc_cds_clk_rate);
 
   return 0;
}
 
static int vop2_calc_if_clk(struct drm_crtc *crtc, const struct vop2_connector_if_data *if_data,
               struct vop2_clk *if_pixclk, struct vop2_clk *if_dclk, int conn_id)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   u64 v_pixclk = adjusted_mode->crtc_clock * 1000LL; /* video timing pixclk */
   unsigned long dclk_core_rate, dclk_out_rate = 0;
   /*conn_dclk = conn_pixclk or conn_dclk = conn_pixclk / 2 */
   u64 hdmi_edp_pixclk, hdmi_edp_dclk, mipi_pixclk;
   char dclk_core_div_shift = 2;
   char K = 1;
   char clk_name[32];
   struct vop2_clk *dclk_core, *dclk_out, *dclk;
   int ret;
   bool dsc_txp_clk_is_biggest = false;
   u8 dsc_id = conn_id & (VOP_OUTPUT_IF_MIPI0 | VOP_OUTPUT_IF_HDMI0) ? 0 : 1;
 
   dclk_core_div_shift = if_data->post_proc_div_shift;
   dclk_core_rate = v_pixclk >> dclk_core_div_shift;
 
   if (!if_dclk && (output_if_is_hdmi(conn_id) || output_if_is_edp(conn_id)))
       return -EINVAL;
   if ((vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE) &&
       (vcstate->output_mode == ROCKCHIP_OUT_MODE_YUV420)) {
       DRM_DEV_ERROR(vop2->dev, "Dual channel and YUV420 can't work together\n");
       return -EINVAL;
   }
 
   if ((vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE) ||
       (vcstate->output_mode == ROCKCHIP_OUT_MODE_YUV420))
       K = 2;
 
   if (output_if_is_hdmi(conn_id)) {
       if (vcstate->dsc_enable) {
           hdmi_edp_pixclk = vcstate->dsc_cds_clk_rate << 1;
           hdmi_edp_dclk = vcstate->dsc_cds_clk_rate;
       } else {
           hdmi_edp_pixclk = (dclk_core_rate << 1) / K;
           hdmi_edp_dclk = dclk_core_rate / K;
       }
 
       if_pixclk->rate = hdmi_edp_pixclk;
       if_dclk->rate = hdmi_edp_dclk;
   } else if (output_if_is_edp(conn_id)) {
       hdmi_edp_pixclk = v_pixclk;
       do_div(hdmi_edp_pixclk, K);
       hdmi_edp_dclk = hdmi_edp_pixclk;
 
       if_pixclk->rate = hdmi_edp_pixclk;
       if_dclk->rate = hdmi_edp_dclk;
   } else if (output_if_is_dp(conn_id)) {
       dclk_out_rate = v_pixclk >> 2;
       dclk_out_rate = dclk_out_rate / K;
       if_pixclk->rate = dclk_out_rate;
   } else if (output_if_is_mipi(conn_id)) {
       if (vcstate->dsc_enable)
           /* dsc output is 96bit, dsi input is 192 bit */
           mipi_pixclk = vcstate->dsc_cds_clk_rate >> 1;
       else
           mipi_pixclk = dclk_core_rate / K;
 
       dclk_out_rate = dclk_core_rate / K;
       if_pixclk->rate = mipi_pixclk;
   } else if (output_if_is_dpi(conn_id)) {
       if_pixclk->rate = v_pixclk;
   }
 
   /*
    * RGB/eDP/HDMI: if_pixclk >= dclk_core
    * DP: dp_pixclk = dclk_out <= dclk_core
    * DSI: mipi_pixclk <= dclk_out <= dclk_core
    *
    */
   snprintf(clk_name, sizeof(clk_name), "dclk_core%d", vp->id);
   dclk_core = vop2_clk_get(vop2, clk_name);
 
   snprintf(clk_name, sizeof(clk_name), "dclk_out%d", vp->id);
   dclk_out = vop2_clk_get(vop2, clk_name);
 
   /*
    * HDMI use 1:1 dclk for rgb/yuv444, 1:2 for yuv420 when
    * pixclk <= 600
    * We want use HDMI PHY clk as dclk source for DP/HDMI.
    * The max freq of HDMI PHY CLK is 600 MHZ.
    * When used for HDMI, the input freq and v_pixclk must
    * keep 1:1 for rgb/yuv444, 1:2 for yuv420
    */
   if (output_if_is_hdmi(conn_id) || output_if_is_dp(conn_id) || output_if_is_mipi(conn_id)) {
       snprintf(clk_name, sizeof(clk_name), "dclk%d", vp->id);
       dclk = vop2_clk_get(vop2, clk_name);
       if (v_pixclk <= (VOP2_MAX_DCLK_RATE * 1000)) {
           if (vcstate->output_mode == ROCKCHIP_OUT_MODE_YUV420 ||
               (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE))
               v_pixclk = v_pixclk >> 1;
       } else {
           v_pixclk = v_pixclk >> 2;
       }
       clk_set_rate(dclk->hw.clk, v_pixclk);
   }
 
   if (vcstate->dsc_enable) {
       if ((vcstate->dsc_txp_clk_rate >= dclk_core_rate) &&
           (vcstate->dsc_txp_clk_rate >= if_pixclk->rate)) {
           dsc_txp_clk_is_biggest = true;
           if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE) {
               vop2_set_dsc_clk(crtc, 0);
               vop2_set_dsc_clk(crtc, 1);
           } else {
               vop2_set_dsc_clk(crtc, dsc_id);
           }
       }
   }
 
   if (dclk_core_rate > if_pixclk->rate) {
       clk_set_rate(dclk_core->hw.clk, dclk_core_rate);
       if (output_if_is_mipi(conn_id))
           clk_set_rate(dclk_out->hw.clk, dclk_out_rate);
       ret = vop2_cru_set_rate(if_pixclk, if_dclk);
   } else {
       if (output_if_is_mipi(conn_id))
           clk_set_rate(dclk_out->hw.clk, dclk_out_rate);
       ret = vop2_cru_set_rate(if_pixclk, if_dclk);
       clk_set_rate(dclk_core->hw.clk, dclk_core_rate);
   }
 
   if (!dsc_txp_clk_is_biggest && vcstate->dsc_enable) {
       if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE) {
           vop2_set_dsc_clk(crtc, 0);
           vop2_set_dsc_clk(crtc, 1);
       } else {
           vop2_set_dsc_clk(crtc, dsc_id);
       }
   }
 
   return ret;
}
 
static int vop2_calc_dsc_clk(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2 *vop2 = vp->vop2;
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
   u64 v_pixclk = adjusted_mode->crtc_clock * 1000LL; /* video timing pixclk */
   u8 k = 1;
 
   if (!vop2->data->nr_dscs) {
       DRM_WARN("Unsupported DSC\n");
 
       return 0;
   }
 
   if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE)
       k = 2;
 
   vcstate->dsc_txp_clk_rate = v_pixclk;
   do_div(vcstate->dsc_txp_clk_rate, (vcstate->dsc_pixel_num * k));
 
   vcstate->dsc_pxl_clk_rate = v_pixclk;
   do_div(vcstate->dsc_pxl_clk_rate, (vcstate->dsc_slice_num * k));
 
   /* dsc_cds = crtc_clock / (cds_dat_width / bits_per_pixel)
    * cds_dat_width = 96;
    * bits_per_pixel = [8-12];
    * As cds clk is div from txp clk and only support 1/2/4 div,
    * so when txp_clk is equal to v_pixclk, we set dsc_cds = crtc_clock / 4,
    * otherwise dsc_cds = crtc_clock / 8;
    */
   vcstate->dsc_cds_clk_rate = v_pixclk / (vcstate->dsc_txp_clk_rate == v_pixclk ? 4 : 8);
 
   return 0;
}
 
static int vop2_calc_cru_cfg(struct drm_crtc *crtc, int conn_id,
                struct vop2_clk **if_pixclk, struct vop2_clk **if_dclk)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_connector_if_data *if_data;
   struct vop2_clk *if_clk_src, *if_clk_parent;
   char clk_name[32];
   int ret;
 
   if (vop2->version != VOP_VERSION_RK3588)
       return 0;
 
   if_data = vop2_find_connector_if_data(vop2, conn_id);
   if_clk_src = vop2_clk_get(vop2, if_data->clk_src_name);
   snprintf(clk_name, sizeof(clk_name), "%s%d", if_data->clk_parent_name, vp->id);
   if_clk_parent = vop2_clk_get(vop2, clk_name);
   *if_pixclk = vop2_clk_get(vop2, if_data->pixclk_name);
   *if_dclk = vop2_clk_get(vop2, if_data->dclk_name);
   if (!(*if_pixclk) || !if_clk_parent) {
       DRM_DEV_ERROR(vop2->dev, "failed to get connector interface clk\n");
       return -ENODEV;
   }
 
   ret = clk_set_parent(if_clk_src->hw.clk, if_clk_parent->hw.clk);
   if (ret < 0) {
       DRM_DEV_ERROR(vop2->dev, "failed to set parent(%s) for %s: %d\n",
                 __clk_get_name(if_clk_parent->hw.clk),
                 __clk_get_name(if_clk_src->hw.clk), ret);
       return ret;
   }
 
   /* HDMI and eDP use independent if_pixclk and if_dclk, and others if_pixclk = if_dclk */
   if (output_if_is_hdmi(conn_id) || output_if_is_edp(conn_id))
       ret = vop2_calc_if_clk(crtc, if_data, *if_pixclk, *if_dclk, conn_id);
   else
       ret = vop2_calc_if_clk(crtc, if_data, *if_pixclk, NULL, conn_id);
 
   return ret;
}
 
static void vop2_crtc_load_pps(struct drm_crtc *crtc, u8 dsc_id)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
 
   struct drm_dsc_picture_parameter_set *pps = &vcstate->pps;
   struct drm_dsc_picture_parameter_set config_pps;
   int i = 0;
   u32 *pps_val = (u32 *)&config_pps;
   u32 offset;
   struct vop2_dsc *dsc;
 
   dsc = &vop2->dscs[dsc_id];
   offset = dsc->regs->dsc_pps0_3.offset;
 
   memcpy(&config_pps, pps, sizeof(config_pps));
 
   if ((config_pps.pps_3 & 0xf) > dsc->max_linebuf_depth) {
       config_pps.pps_3 &= 0xf0;
       config_pps.pps_3 |= dsc->max_linebuf_depth;
       DRM_WARN("DSC%d max_linebuf_depth is: %d, current set value is: %d\n",
            dsc_id, dsc->max_linebuf_depth, config_pps.pps_3 & 0xf);
   }
 
   for (i = 0; i < DSC_NUM_BUF_RANGES; i++) {
       config_pps.rc_range_parameters[i] =
           (pps->rc_range_parameters[i] >> 3 & 0x1f) |
           ((pps->rc_range_parameters[i] >> 14 & 0x3) << 5) |
           ((pps->rc_range_parameters[i] >> 0 & 0x7) << 7) |
           ((pps->rc_range_parameters[i] >> 8 & 0x3f) << 10);
   }
 
   for (i = 0; i < ROCKCHIP_DSC_PPS_SIZE_BYTE / 4; i++)
       vop2_writel(vop2, offset + i * 4, *pps_val++);
}
 
static void vop2_crtc_enable_dsc(struct drm_crtc *crtc, struct drm_crtc_state *old_state, u8 dsc_id)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
   struct rockchip_dsc_sink_cap *dsc_sink_cap = &vcstate->dsc_sink_cap;
   u16 hsync_len = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
   u16 hdisplay = adjusted_mode->crtc_hdisplay;
   u16 htotal = adjusted_mode->crtc_htotal;
   u16 hact_st = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_start;
   u16 vdisplay = adjusted_mode->crtc_vdisplay;
   u16 vtotal = adjusted_mode->crtc_vtotal;
   u16 vsync_len = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
   u16 vact_st = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_start;
   u16 vact_end = vact_st + vdisplay;
   u8 dsc_interface_mode = 0;
   struct vop2_dsc *dsc;
   struct vop2_clk *dsc_cds_clk, *dsc_pxl_clk, *dsc_txp_clk;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_dsc_data *dsc_data = &vop2_data->dsc[dsc_id];
   bool mipi_ds_mode = false;
   uint32_t *reg_base = vop2->regs;
   u32 offset = 0;
 
   if (!vop2->data->nr_dscs) {
       DRM_WARN("Unsupported DSC\n");
 
       return;
   }
 
   if (vcstate->dsc_slice_num > dsc_data->max_slice_num)
       DRM_ERROR("DSC%d supported max slice is: %d, current is: %d\n",
             dsc_data->id, dsc_data->max_slice_num, vcstate->dsc_slice_num);
 
   dsc = &vop2->dscs[dsc_id];
   if (dsc->pd) {
       dsc->pd->vp_mask = BIT(vp->id);
       vop2_power_domain_get(dsc->pd);
   }
 
   VOP_MODULE_SET(vop2, dsc, scan_timing_para_imd_en, 1);
   VOP_MODULE_SET(vop2, dsc, dsc_port_sel, vp->id);
   if (vcstate->output_if & (VOP_OUTPUT_IF_HDMI0 | VOP_OUTPUT_IF_HDMI1)) {
       dsc_interface_mode = VOP_DSC_IF_HDMI;
   } else {
       mipi_ds_mode = !!(vcstate->output_flags & ROCKCHIP_OUTPUT_MIPI_DS_MODE);
       if (mipi_ds_mode)
           dsc_interface_mode = VOP_DSC_IF_MIPI_DS_MODE;
       else
           dsc_interface_mode = VOP_DSC_IF_MIPI_VIDEO_MODE;
   }
 
   if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE)
       VOP_MODULE_SET(vop2, dsc, dsc_man_mode, 0);
   else
       VOP_MODULE_SET(vop2, dsc, dsc_man_mode, 1);
   dsc_cds_clk = vop2_clk_get(vop2, dsc_data->dsc_cds_clk_name);
   dsc_pxl_clk = vop2_clk_get(vop2, dsc_data->dsc_pxl_clk_name);
   dsc_txp_clk = vop2_clk_get(vop2, dsc_data->dsc_txp_clk_name);
 
   VOP_MODULE_SET(vop2, dsc, dsc_interface_mode, dsc_interface_mode);
   VOP_MODULE_SET(vop2, dsc, dsc_pixel_num, vcstate->dsc_pixel_num >> 1);
   VOP_MODULE_SET(vop2, dsc, dsc_txp_clk_div, dsc_txp_clk->div_val);
   VOP_MODULE_SET(vop2, dsc, dsc_pxl_clk_div, dsc_pxl_clk->div_val);
   VOP_MODULE_SET(vop2, dsc, dsc_cds_clk_div, dsc_cds_clk->div_val);
   VOP_MODULE_SET(vop2, dsc, dsc_scan_en, !mipi_ds_mode);
   VOP_MODULE_SET(vop2, dsc, dsc_halt_en, mipi_ds_mode);
 
   if (!mipi_ds_mode) {
       u16 dsc_hsync, dsc_htotal, dsc_hact_st, dsc_hact_end;
       u32 target_bpp = dsc_sink_cap->target_bits_per_pixel_x16;
       u64 dsc_cds_rate = vcstate->dsc_cds_clk_rate;
       u32 v_pixclk_mhz = adjusted_mode->crtc_clock / 1000; /* video timing pixclk */
       u32 dly_num, dsc_cds_rate_mhz, val = 0;
       struct vop2_clk *dclk_core;
       char clk_name[32];
       int k = 1;
 
       if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE)
           k = 2;
 
       snprintf(clk_name, sizeof(clk_name), "dclk_core%d", vp->id);
       dclk_core = vop2_clk_get(vop2, clk_name);
 
       if (target_bpp >> 4 < dsc->min_bits_per_pixel)
           DRM_ERROR("Unsupported bpp less than: %d\n", dsc->min_bits_per_pixel);
 
       /*
        * dly_num = delay_line_num * T(one-line) / T (dsc_cds)
        * T (one-line) = 1/v_pixclk_mhz * htotal = htotal/v_pixclk_mhz
        * T (dsc_cds) = 1 / dsc_cds_rate_mhz
        *
        * HDMI:
        * delay_line_num: according the pps initial_xmit_delay to adjust vop dsc delay
        *                 delay_line_num = 4 - BPP / 8
        *                                = (64 - target_bpp / 8) / 16
        * dly_num = htotal * dsc_cds_rate_mhz / v_pixclk_mhz * (64 - target_bpp / 8) / 16;
        *
        * MIPI DSI[4320 and 9216 is buffer size for DSC]:
        * DSC0:delay_line_num = 4320 * 8 / slince_num / chunk_size;
        *    delay_line_num = delay_line_num > 5 ? 5 : delay_line_num;
        * DSC1:delay_line_num = 9216 * 2 / slince_num / chunk_size;
        *    delay_line_num = delay_line_num > 5 ? 5 : delay_line_num;
        * dly_num = htotal * dsc_cds_rate_mhz / v_pixclk_mhz * delay_line_num
        */
       do_div(dsc_cds_rate, 1000000); /* hz to Mhz */
       dsc_cds_rate_mhz = dsc_cds_rate;
       dsc_hsync = hsync_len / 2;
       if (dsc_interface_mode == VOP_DSC_IF_HDMI) {
           dly_num = htotal * dsc_cds_rate_mhz / v_pixclk_mhz * (64 - target_bpp / 8) / 16;
       } else {
           int dsc_buf_size  = dsc->id == 0 ? 4320 * 8 : 9216 * 2;
           int delay_line_num = dsc_buf_size / vcstate->dsc_slice_num / be16_to_cpu(vcstate->pps.chunk_size);
 
           delay_line_num = delay_line_num > 5 ? 5 : delay_line_num;
           dly_num = htotal * dsc_cds_rate_mhz / v_pixclk_mhz * delay_line_num;
 
           /* The dsc mipi video mode dsc_hsync minimum size is 8 pixels */
           if (dsc_hsync < 8)
               dsc_hsync = 8;
       }
       VOP_MODULE_SET(vop2, dsc, dsc_init_dly_mode, 0);
       VOP_MODULE_SET(vop2, dsc, dsc_init_dly_num, dly_num);
       /*
        * htotal / dclk_core = dsc_htotal /cds_clk
        *
        * dclk_core = DCLK / (1 << dclk_core->div_val)
        * cds_clk = txp_clk / (1 << dsc_cds_clk->div_val)
        * txp_clk = DCLK / (1 << dsc_txp_clk->div_val)
        *
        * dsc_htotal = htotal * (1 << dclk_core->div_val) /
               ((1 << dsc_txp_clk->div_val) * (1 << dsc_cds_clk->div_val))
       */
       dsc_htotal = htotal * (1 << dclk_core->div_val) /
               ((1 << dsc_txp_clk->div_val) * (1 << dsc_cds_clk->div_val));
       val = dsc_htotal << 16 | dsc_hsync;
       VOP_MODULE_SET(vop2, dsc, dsc_htotal_pw, val);
 
       dsc_hact_st = hact_st / 2;
       dsc_hact_end = (hdisplay / k * target_bpp >> 4) / 24 + dsc_hact_st;
       val = dsc_hact_end << 16 | dsc_hact_st;
       VOP_MODULE_SET(vop2, dsc, dsc_hact_st_end, val);
 
       VOP_MODULE_SET(vop2, dsc, dsc_vtotal, vtotal);
       VOP_MODULE_SET(vop2, dsc, dsc_vs_end, vsync_len);
       VOP_MODULE_SET(vop2, dsc, dsc_vact_st_end, vact_end << 16 | vact_st);
   }
 
   VOP_MODULE_SET(vop2, dsc, rst_deassert, 1);
   udelay(10);
   /* read current dsc core register and backup to regsbak */
   offset = dsc->regs->dsc_en.offset;
   vop2->regsbak[offset >> 2] = reg_base[offset >> 2];
 
   VOP_MODULE_SET(vop2, dsc, dsc_en, 1);
   vop2_crtc_load_pps(crtc, dsc_id);
 
   VOP_MODULE_SET(vop2, dsc, dsc_rbit, 1);
   VOP_MODULE_SET(vop2, dsc, dsc_rbyt, 0);
   VOP_MODULE_SET(vop2, dsc, dsc_flal, 1);
   VOP_MODULE_SET(vop2, dsc, dsc_mer, 1);
   VOP_MODULE_SET(vop2, dsc, dsc_epb, 0);
   VOP_MODULE_SET(vop2, dsc, dsc_epl, 1);
   VOP_MODULE_SET(vop2, dsc, dsc_nslc, ilog2(vcstate->dsc_slice_num));
   VOP_MODULE_SET(vop2, dsc, dsc_sbo, 1);
   VOP_MODULE_SET(vop2, dsc, dsc_ifep, dsc_sink_cap->version_minor == 2 ? 1 : 0);
   VOP_MODULE_SET(vop2, dsc, dsc_pps_upd, 1);
 
   DRM_DEV_INFO(vop2->dev, "DSC%d: txp:%lld div:%d, pxl:%lld div:%d, dsc:%lld div:%d\n",
            dsc->id,
            vcstate->dsc_txp_clk_rate, dsc_txp_clk->div_val,
            vcstate->dsc_pxl_clk_rate, dsc_pxl_clk->div_val,
            vcstate->dsc_cds_clk_rate, dsc_cds_clk->div_val);
 
   dsc->attach_vp_id = vp->id;
   dsc->enabled = true;
}
 
static inline bool vop2_mark_as_left_panel(struct rockchip_crtc_state *vcstate, u32 output_if)
{
   return vcstate->output_if_left_panel & output_if;
}
 
static void vop2_setup_dual_channel_if(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2 *vop2 = vp->vop2;
 
   if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_ODD_EVEN_MODE) {
       VOP_CTRL_SET(vop2, lvds_dual_en, 1);
       VOP_CTRL_SET(vop2, lvds_dual_mode, 0);
       if (vcstate->output_flags & ROCKCHIP_OUTPUT_DATA_SWAP)
           VOP_CTRL_SET(vop2, lvds_dual_channel_swap, 1);
       return;
   }
 
   VOP_MODULE_SET(vop2, vp, dual_channel_en, 1);
   if (vcstate->output_flags & ROCKCHIP_OUTPUT_DATA_SWAP)
       VOP_MODULE_SET(vop2, vp, dual_channel_swap, 1);
 
   if (vcstate->output_if & VOP_OUTPUT_IF_DP1 &&
       !vop2_mark_as_left_panel(vcstate, VOP_OUTPUT_IF_DP1))
       VOP_CTRL_SET(vop2, dp_dual_en, 1);
   else if (vcstate->output_if & VOP_OUTPUT_IF_eDP1 &&
        !vop2_mark_as_left_panel(vcstate, VOP_OUTPUT_IF_eDP1))
       VOP_CTRL_SET(vop2, edp_dual_en, 1);
   else if (vcstate->output_if & VOP_OUTPUT_IF_HDMI1 &&
        !vop2_mark_as_left_panel(vcstate, VOP_OUTPUT_IF_HDMI1))
       VOP_CTRL_SET(vop2, hdmi_dual_en, 1);
   else if (vcstate->output_if & VOP_OUTPUT_IF_MIPI1 &&
        !vop2_mark_as_left_panel(vcstate, VOP_OUTPUT_IF_MIPI1))
       VOP_CTRL_SET(vop2, mipi_dual_en, 1);
   else if (vcstate->output_if & VOP_OUTPUT_IF_LVDS1) {
       VOP_CTRL_SET(vop2, lvds_dual_en, 1);
       VOP_CTRL_SET(vop2, lvds_dual_mode, 1);
   }
}
 
/*
 * MIPI port mux on rk3588:
 * 0: Video Port2
 * 1: Video Port3
 * 3: Video Port 1(MIPI1 only)
 */
static int vop2_get_mipi_port_mux(struct vop2 *vop2, int vp_id)
{
   if (vop2->version == VOP_VERSION_RK3588) {
       if (vp_id == 1)
           return 3;
       else if (vp_id == 3)
           return 1;
       else
           return 0;
   } else {
       return vp_id;
   }
}
 
static u32 vop2_get_hdmi_pol(struct vop2 *vop2, u32 flags)
{
   u32 val;
 
   if (vop2->version == VOP_VERSION_RK3588) {
       val = (flags & DRM_MODE_FLAG_NHSYNC) ? BIT(HSYNC_POSITIVE) : 0;
       val |= (flags & DRM_MODE_FLAG_NVSYNC) ? BIT(VSYNC_POSITIVE) : 0;
   } else {
       val = (flags & DRM_MODE_FLAG_NHSYNC) ? 0 : BIT(HSYNC_POSITIVE);
       val |= (flags & DRM_MODE_FLAG_NVSYNC) ? 0 : BIT(VSYNC_POSITIVE);
   }
 
   return val;
}
 
static void vop2_post_color_swap(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   u32 output_if = vcstate->output_if;
   u32 data_swap = 0;
 
   if (vop2_output_uv_swap(vcstate->bus_format, vcstate->output_mode) ||
       vop3_output_rb_swap(vcstate->bus_format, vcstate->output_mode))
       data_swap = DSP_RB_SWAP;
 
   if (vop2->version == VOP_VERSION_RK3588 &&
       (output_if_is_hdmi(output_if) || output_if_is_dp(output_if)) &&
       (vcstate->bus_format == MEDIA_BUS_FMT_YUV8_1X24 ||
        vcstate->bus_format == MEDIA_BUS_FMT_YUV10_1X30))
       data_swap |= DSP_RG_SWAP;
 
   VOP_MODULE_SET(vop2, vp, dsp_data_swap, data_swap);
}
 
/*
 * For vop3 video port0, if hdr_vivid is not enable, the pipe delay time as follow:
 * win_dly + config_win_dly + layer_mix_dly + sdr2hdr_dly + * hdr_mix_dly = config_bg_dly
 *
 * if hdr_vivid is enable, the hdr layer's pipe delay time as follow:
 * win_dly + config_win_dly +hdrvivid_dly + hdr_mix_dly = config_bg_dly
 *
 * If hdrvivid and sdr2hdr bot enable, the time arrivr hdr_mix should be the same:
 * win_dly + config_win_dly0 + hdrvivid_dly = win_dly + config_win_dly1 + laer_mix_dly +
 * sdr2hdr_dly
 *
 * For vop3 video port1, the pipe delay time as follow:
 * win_dly + config_win_dly + layer_mix_dly = config_bg_dly
 *
 * Here, win_dly, layer_mix_dly, sdr2hdr_dly, hdr_mix_dly, hdrvivid_dly is the hardware
 * delay cycles. Config_win_dly and config_bg_dly is the register value that we can config.
 * Different hdr vivid mode have different hdrvivid_dly. For sdr2hdr_dly, only sde2hdr
 * enable, it will delay, otherwise, the sdr2hdr_dly is 0.
 *
 * For default, the config_win_dly will be 0, it just user to make the pipe to arrive
 * hdr_mix at the same time.
 */
static void vop3_setup_pipe_dly(struct vop2_video_port *vp, const struct vop2_zpos *vop2_zpos)
{
   struct vop2 *vop2 = vp->vop2;
   struct drm_crtc *crtc = &vp->rockchip_crtc.crtc;
   const struct vop2_zpos *zpos;
   struct drm_plane *plane;
   struct vop2_plane_state *vpstate;
   struct vop2_win *win;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
   u16 hsync_len = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
   u16 hdisplay = adjusted_mode->crtc_hdisplay;
   int bg_dly = 0x0;
   int dly = 0x0;
   int hdr_win_dly;
   int sdr_win_dly;
   int sdr2hdr_dly;
   int pre_scan_dly;
   int i;
 
   /**
    * config bg dly, select the max delay num of hdrvivid and sdr2hdr module
    * as the increase value of bg delay num. If hdrvivid and sdr2hdr is not
    * work, the default bg_dly is 0x10. and the default win delay num is 0.
    */
   if ((vp->hdr_en || vp->sdr2hdr_en) &&
       (vp->hdrvivid_mode >= 0 && vp->hdrvivid_mode <= SDR2HLG)) {
       /* set sdr2hdr_dly to 0 if sdr2hdr is disable */
       sdr2hdr_dly = vp->sdr2hdr_en ? vp_data->sdr2hdr_dly : 0;
 
       /* set the max delay pipe's config_win_dly as 0 */
       if (vp_data->hdrvivid_dly[vp->hdrvivid_mode] >=
           sdr2hdr_dly + vp_data->layer_mix_dly) {
           bg_dly = vp_data->win_dly + vp_data->hdrvivid_dly[vp->hdrvivid_mode] +
                vp_data->hdr_mix_dly;
           hdr_win_dly = 0;
           sdr_win_dly = vp_data->hdrvivid_dly[vp->hdrvivid_mode] -
                     vp_data->layer_mix_dly - sdr2hdr_dly;
       } else {
           bg_dly = vp_data->win_dly + vp_data->layer_mix_dly + sdr2hdr_dly +
                vp_data->hdr_mix_dly;
           hdr_win_dly = sdr2hdr_dly + vp_data->layer_mix_dly -
                     vp_data->hdrvivid_dly[vp->hdrvivid_mode];
           sdr_win_dly = 0;
       }
   } else {
       bg_dly = vp_data->win_dly + vp_data->layer_mix_dly + vp_data->hdr_mix_dly;
       sdr_win_dly = 0;
   }
 
   pre_scan_dly = bg_dly + (hdisplay >> 1) - 1;
   pre_scan_dly = (pre_scan_dly << 16) | hsync_len;
   VOP_MODULE_SET(vop2, vp, bg_dly, bg_dly);
   VOP_MODULE_SET(vop2, vp, pre_scan_htiming, pre_scan_dly);
 
   /**
    * config win dly
    */
   if (!vop2_zpos)
       return;
 
   for (i = 0; i < vp->nr_layers; i++) {
       zpos = &vop2_zpos[i];
       win = vop2_find_win_by_phys_id(vop2, zpos->win_phys_id);
       plane = &win->base;
       vpstate = to_vop2_plane_state(plane->state);
 
       if ((vp->hdr_en || vp->sdr2hdr_en) &&
           (vp->hdrvivid_mode >= 0 && vp->hdrvivid_mode <= SDR2HLG)) {
           dly = vpstate->hdr_in ? hdr_win_dly : sdr_win_dly;
       }
       if (vop2_cluster_window(win))
           dly |= dly << 8;
 
       VOP_CTRL_SET(vop2, win_dly[win->phys_id], dly);
   }
}
 
static void vop2_crtc_atomic_enable(struct drm_crtc *crtc, struct drm_crtc_state *old_state)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2_video_port *splice_vp;
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_intr *intr = vp_data->intr;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
   struct rockchip_dsc_sink_cap *dsc_sink_cap = &vcstate->dsc_sink_cap;
   u16 hsync_len = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
   u16 hdisplay = adjusted_mode->crtc_hdisplay;
   u16 htotal = adjusted_mode->crtc_htotal;
   u16 hact_st = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_start;
   u16 hact_end = hact_st + hdisplay;
   u16 vdisplay = adjusted_mode->crtc_vdisplay;
   u16 vtotal = adjusted_mode->crtc_vtotal;
   u16 vsync_len = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
   u16 vact_st = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_start;
   u16 vact_end = vact_st + vdisplay;
   bool interlaced = !!(adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE);
   bool dclk_inv, yc_swap = false;
   int act_end;
   uint32_t val;
   char clk_name[32];
   struct vop2_clk *if_pixclk = NULL;
   struct vop2_clk *if_dclk = NULL;
   struct vop2_clk *dclk, *dclk_out, *dclk_core;
   int splice_en = 0;
   int port_mux;
   int ret;
 
   if (old_state && old_state->self_refresh_active) {
       vop2_crtc_atomic_exit_psr(crtc, old_state);
 
       return;
   }
 
   vop2->active_vp_mask |= BIT(vp->id);
   vop2_set_system_status(vop2);
 
   vop2_lock(vop2);
   DRM_DEV_INFO(vop2->dev, "Update mode to %dx%d%s%d, type: %d(if:%x, flag:0x%x) for vp%d dclk: %d\n",
            hdisplay, adjusted_mode->vdisplay, interlaced ? "i" : "p",
            drm_mode_vrefresh(adjusted_mode),
            vcstate->output_type, vcstate->output_if, vcstate->output_flags,
            vp->id, adjusted_mode->crtc_clock * 1000);
 
   if (adjusted_mode->hdisplay > VOP2_MAX_VP_OUTPUT_WIDTH) {
       vcstate->splice_mode = true;
       splice_vp = &vop2->vps[vp_data->splice_vp_id];
       splice_vp->splice_mode_right = true;
       splice_vp->left_vp = vp;
       splice_en = 1;
       vop2->active_vp_mask |= BIT(splice_vp->id);
   }
 
   if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CONNECTOR_SPLIT_MODE)
       vcstate->output_flags |= ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE;
 
   if (vcstate->dsc_enable) {
       int k = 1;
 
       if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE)
           k = 2;
 
       vcstate->dsc_id = vcstate->output_if & (VOP_OUTPUT_IF_MIPI0 | VOP_OUTPUT_IF_HDMI0) ? 0 : 1;
       vcstate->dsc_slice_num = hdisplay / dsc_sink_cap->slice_width / k;
       vcstate->dsc_pixel_num = vcstate->dsc_slice_num > 4 ? 4 : vcstate->dsc_slice_num;
 
       vop2_calc_dsc_clk(crtc);
       DRM_DEV_INFO(vop2->dev, "Enable DSC%d slice:%dx%d, slice num:%d\n",
                vcstate->dsc_id, dsc_sink_cap->slice_width,
                dsc_sink_cap->slice_height, vcstate->dsc_slice_num);
   }
 
   vop2_initial(crtc);
   vcstate->vdisplay = vdisplay;
   vcstate->mode_update = vop2_crtc_mode_update(crtc);
   if (vcstate->mode_update)
       vop2_disable_all_planes_for_crtc(crtc);
 
   dclk_inv = (vcstate->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE) ? 1 : 0;
   val = (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC) ? 0 : BIT(HSYNC_POSITIVE);
   val |= (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC) ? 0 : BIT(VSYNC_POSITIVE);
 
   vp->output_if = vcstate->output_if;
 
   if (vcstate->output_if & VOP_OUTPUT_IF_RGB) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_RGB, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
 
       VOP_CTRL_SET(vop2, rgb_en, 1);
       VOP_CTRL_SET(vop2, rgb_mux, vp_data->id);
       VOP_CTRL_SET(vop2, rgb_pin_pol, val);
       VOP_GRF_SET(vop2, sys_grf, grf_dclk_inv, dclk_inv);
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_BT1120) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_RGB, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
 
       if (vop2->version == VOP_VERSION_RK3588) {
           VOP_CTRL_SET(vop2, bt1120_en, 3);
       } else {
           VOP_CTRL_SET(vop2, rgb_en, 1);
           VOP_CTRL_SET(vop2, bt1120_en, 1);
       }
       VOP_CTRL_SET(vop2, rgb_mux, vp_data->id);
       VOP_GRF_SET(vop2, sys_grf, grf_bt1120_clk_inv, !dclk_inv);
       VOP_CTRL_SET(vop2, bt1120_dclk_pol, !dclk_inv);
       yc_swap = vop2_output_yc_swap(vcstate->bus_format);
       VOP_CTRL_SET(vop2, bt1120_yc_swap, yc_swap);
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_BT656) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_RGB, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
 
       if (vop2->version == VOP_VERSION_RK3588) {
           VOP_CTRL_SET(vop2, bt656_en, 1);
       } else {
           VOP_CTRL_SET(vop2, rgb_en, 1);
           VOP_CTRL_SET(vop2, bt656_en, 1);
       }
       VOP_CTRL_SET(vop2, rgb_mux, vp_data->id);
       VOP_GRF_SET(vop2, sys_grf, grf_bt656_clk_inv, !dclk_inv);
       VOP_CTRL_SET(vop2, bt656_dclk_pol, !dclk_inv);
       yc_swap = vop2_output_yc_swap(vcstate->bus_format);
       VOP_CTRL_SET(vop2, bt656_yc_swap, yc_swap);
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_LVDS0) {
       VOP_CTRL_SET(vop2, lvds0_en, 1);
       VOP_CTRL_SET(vop2, lvds0_mux, vp_data->id);
       VOP_CTRL_SET(vop2, lvds_pin_pol, val);
       VOP_CTRL_SET(vop2, lvds_dclk_pol, dclk_inv);
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_LVDS1) {
       VOP_CTRL_SET(vop2, lvds1_en, 1);
       VOP_CTRL_SET(vop2, lvds1_mux, vp_data->id);
       VOP_CTRL_SET(vop2, lvds_pin_pol, val);
       VOP_CTRL_SET(vop2, lvds_dclk_pol, dclk_inv);
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_MIPI0) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_MIPI0, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
       if (if_pixclk)
           VOP_CTRL_SET(vop2, mipi0_pixclk_div, if_pixclk->div_val);
 
       if (vcstate->output_flags & ROCKCHIP_OUTPUT_MIPI_DS_MODE)
           VOP_CTRL_SET(vop2, mipi0_ds_mode, 1);
 
       port_mux = vop2_get_mipi_port_mux(vop2, vp_data->id);
       VOP_CTRL_SET(vop2, mipi0_en, 1);
       VOP_CTRL_SET(vop2, mipi0_mux, port_mux);
       VOP_CTRL_SET(vop2, mipi_pin_pol, val);
       VOP_CTRL_SET(vop2, mipi_dclk_pol, dclk_inv);
       if (vcstate->hold_mode) {
           VOP_MODULE_SET(vop2, vp, edpi_te_en, !vcstate->soft_te);
           VOP_MODULE_SET(vop2, vp, edpi_wms_hold_en, 1);
       }
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_MIPI1) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_MIPI1, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
       if (if_pixclk)
           VOP_CTRL_SET(vop2, mipi1_pixclk_div, if_pixclk->div_val);
 
       if (vcstate->output_flags & ROCKCHIP_OUTPUT_MIPI_DS_MODE)
           VOP_CTRL_SET(vop2, mipi1_ds_mode, 1);
 
       port_mux = vop2_get_mipi_port_mux(vop2, vp_data->id);
 
       VOP_CTRL_SET(vop2, mipi1_en, 1);
       VOP_CTRL_SET(vop2, mipi1_mux, port_mux);
       VOP_CTRL_SET(vop2, mipi_pin_pol, val);
       VOP_CTRL_SET(vop2, mipi_dclk_pol, dclk_inv);
       if (vcstate->hold_mode) {
           VOP_MODULE_SET(vop2, vp, edpi_te_en, !vcstate->soft_te);
           VOP_MODULE_SET(vop2, vp, edpi_wms_hold_en, 1);
       }
   }
 
   if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE ||
       vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_ODD_EVEN_MODE)
       vop2_setup_dual_channel_if(crtc);
 
   if (vcstate->output_if & VOP_OUTPUT_IF_eDP0) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_eDP0, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
       if (if_pixclk && if_dclk) {
           VOP_CTRL_SET(vop2, edp0_pixclk_div, if_pixclk->div_val);
           VOP_CTRL_SET(vop2, edp0_dclk_div, if_dclk->div_val);
       }
 
       VOP_CTRL_SET(vop2, edp0_en, 1);
       VOP_CTRL_SET(vop2, edp0_mux, vp_data->id);
       VOP_CTRL_SET(vop2, edp_pin_pol, val);
       VOP_CTRL_SET(vop2, edp_dclk_pol, dclk_inv);
       VOP_GRF_SET(vop2, grf, grf_edp0_en, 1);
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_eDP1) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_eDP1, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
       if (if_pixclk && if_dclk) {
           VOP_CTRL_SET(vop2, edp1_pixclk_div, if_pixclk->div_val);
           VOP_CTRL_SET(vop2, edp1_dclk_div, if_dclk->div_val);
       }
 
       VOP_CTRL_SET(vop2, edp1_en, 1);
       VOP_CTRL_SET(vop2, edp1_mux, vp_data->id);
       VOP_CTRL_SET(vop2, edp_pin_pol, val);
       VOP_CTRL_SET(vop2, edp_dclk_pol, dclk_inv);
       VOP_GRF_SET(vop2, grf, grf_edp1_en, 1);
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_DP0) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_DP0, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
       VOP_CTRL_SET(vop2, dp0_en, 1);
       VOP_CTRL_SET(vop2, dp0_mux, vp_data->id);
       VOP_CTRL_SET(vop2, dp0_dclk_pol, 0);
       VOP_CTRL_SET(vop2, dp0_pin_pol, val);
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_DP1) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_DP0, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
 
       VOP_CTRL_SET(vop2, dp1_en, 1);
       VOP_CTRL_SET(vop2, dp1_mux, vp_data->id);
       VOP_CTRL_SET(vop2, dp1_dclk_pol, 0);
       VOP_CTRL_SET(vop2, dp1_pin_pol, val);
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_HDMI0) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_HDMI0, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
       if (if_pixclk && if_dclk) {
           VOP_CTRL_SET(vop2, hdmi0_pixclk_div, if_pixclk->div_val);
           VOP_CTRL_SET(vop2, hdmi0_dclk_div, if_dclk->div_val);
       }
 
       if (vcstate->dsc_enable)
           VOP_GRF_SET(vop2, grf, grf_hdmi0_dsc_en, 1);
 
       val = vop2_get_hdmi_pol(vop2, adjusted_mode->flags);
       VOP_GRF_SET(vop2, grf, grf_hdmi0_en, 1);
       VOP_GRF_SET(vop2, vo1_grf, grf_hdmi0_pin_pol, val);
 
       VOP_CTRL_SET(vop2, hdmi0_en, 1);
       VOP_CTRL_SET(vop2, hdmi0_mux, vp_data->id);
       VOP_CTRL_SET(vop2, hdmi_pin_pol, val);
       VOP_CTRL_SET(vop2, hdmi_dclk_pol, 1);
   }
 
   if (vcstate->output_if & VOP_OUTPUT_IF_HDMI1) {
       ret = vop2_calc_cru_cfg(crtc, VOP_OUTPUT_IF_HDMI1, &if_pixclk, &if_dclk);
       if (ret < 0)
           goto out;
 
       if (if_pixclk && if_dclk) {
           VOP_CTRL_SET(vop2, hdmi1_pixclk_div, if_pixclk->div_val);
           VOP_CTRL_SET(vop2, hdmi1_dclk_div, if_dclk->div_val);
       }
 
       if (vcstate->dsc_enable)
           VOP_GRF_SET(vop2, grf, grf_hdmi1_dsc_en, 1);
 
       val = vop2_get_hdmi_pol(vop2, adjusted_mode->flags);
       VOP_GRF_SET(vop2, grf, grf_hdmi1_en, 1);
       VOP_GRF_SET(vop2, vo1_grf, grf_hdmi1_pin_pol, val);
 
       VOP_CTRL_SET(vop2, hdmi1_en, 1);
       VOP_CTRL_SET(vop2, hdmi1_mux, vp_data->id);
       VOP_CTRL_SET(vop2, hdmi_pin_pol, val);
       VOP_CTRL_SET(vop2, hdmi_dclk_pol, 1);
   }
 
   VOP_MODULE_SET(vop2, vp, splice_en, splice_en);
 
   VOP_MODULE_SET(vop2, vp, htotal_pw, (htotal << 16) | hsync_len);
   val = hact_st << 16;
   val |= hact_end;
   VOP_MODULE_SET(vop2, vp, hact_st_end, val);
 
   val = vact_st << 16;
   val |= vact_end;
   VOP_MODULE_SET(vop2, vp, vact_st_end, val);
 
   if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
       u16 vact_st_f1 = vtotal + vact_st + 1;
       u16 vact_end_f1 = vact_st_f1 + vdisplay;
 
       val = vact_st_f1 << 16 | vact_end_f1;
       VOP_MODULE_SET(vop2, vp, vact_st_end_f1, val);
 
       val = vtotal << 16 | (vtotal + vsync_len);
       VOP_MODULE_SET(vop2, vp, vs_st_end_f1, val);
       VOP_MODULE_SET(vop2, vp, dsp_interlace, 1);
       VOP_MODULE_SET(vop2, vp, dsp_filed_pol, 1);
       VOP_MODULE_SET(vop2, vp, p2i_en, 1);
       vtotal += vtotal + 1;
       act_end = vact_end_f1;
   } else {
       VOP_MODULE_SET(vop2, vp, dsp_interlace, 0);
       VOP_MODULE_SET(vop2, vp, dsp_filed_pol, 0);
       VOP_MODULE_SET(vop2, vp, p2i_en, 0);
       act_end = vact_end;
   }
 
   if (vp->xmirror_en)
       VOP_MODULE_SET(vop2, vp, dsp_x_mir_en, 1);
 
   VOP_INTR_SET(vop2, intr, line_flag_num[0], act_end);
   VOP_INTR_SET(vop2, intr, line_flag_num[1], act_end);
 
   VOP_MODULE_SET(vop2, vp, dsp_vtotal, vtotal);
   VOP_MODULE_SET(vop2, vp, dsp_vs_end, vsync_len);
   /**
    * when display interface support vrr, config vtotal valid immediately
    */
   if (vcstate->max_refresh_rate && vcstate->min_refresh_rate)
       VOP_MODULE_SET(vop2, vp, sw_dsp_vtotal_imd, 1);
 
   if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK ||
       vcstate->output_if & VOP_OUTPUT_IF_BT656)
       VOP_MODULE_SET(vop2, vp, core_dclk_div, 1);
   else
       VOP_MODULE_SET(vop2, vp, core_dclk_div, 0);
 
   if (vcstate->output_mode == ROCKCHIP_OUT_MODE_YUV420) {
       VOP_MODULE_SET(vop2, vp, dclk_div2, 1);
       VOP_MODULE_SET(vop2, vp, dclk_div2_phase_lock, 1);
   } else {
       VOP_MODULE_SET(vop2, vp, dclk_div2, 0);
       VOP_MODULE_SET(vop2, vp, dclk_div2_phase_lock, 0);
   }
 
   snprintf(clk_name, sizeof(clk_name), "dclk_out%d", vp->id);
   dclk_out = vop2_clk_get(vop2, clk_name);
   snprintf(clk_name, sizeof(clk_name), "dclk_core%d", vp->id);
   dclk_core = vop2_clk_get(vop2, clk_name);
   if (dclk_out && dclk_core) {
       DRM_DEV_INFO(vop2->dev, "%s div: %d %s div: %d\n",
                __clk_get_name(dclk_out->hw.clk), dclk_out->div_val,
                __clk_get_name(dclk_core->hw.clk), dclk_core->div_val);
       VOP_MODULE_SET(vop2, vp, dclk_src_sel, 0);
       VOP_MODULE_SET(vop2, vp, dclk_out_div, dclk_out->div_val);
       VOP_MODULE_SET(vop2, vp, dclk_core_div, dclk_core->div_val);
   }
 
   snprintf(clk_name, sizeof(clk_name), "dclk%d", vp->id);
   dclk = vop2_clk_get(vop2, clk_name);
   if (dclk) {
       /*
        * use HDMI_PHY_PLL as dclk source under 4K@60 if it is available,
        * otherwise use system cru as dclk source.
        */
       ret = vop2_clk_set_parent_extend(vp, vcstate, true);
       if (ret < 0)
           goto out;
 
       clk_set_rate(vp->dclk, dclk->rate);
       DRM_DEV_INFO(vop2->dev, "set %s to %ld, get %ld\n",
                 __clk_get_name(vp->dclk), dclk->rate, clk_get_rate(vp->dclk));
   } else {
       /*
        * For RK3528, the path of CVBS output is like:
        * VOP BT656 ENCODER -> CVBS BT656 DECODER -> CVBS ENCODER -> CVBS VDAC
        * The vop2 dclk should be four times crtc_clock for CVBS sampling clock needs.
        */
       if (vop2->version == VOP_VERSION_RK3528 && vcstate->output_if & VOP_OUTPUT_IF_BT656)
           clk_set_rate(vp->dclk, 4 * adjusted_mode->crtc_clock * 1000);
       else
           clk_set_rate(vp->dclk, adjusted_mode->crtc_clock * 1000);
   }
 
   if (vp_data->feature & VOP_FEATURE_OVERSCAN)
       vop2_post_config(crtc);
 
   VOP_MODULE_SET(vop2, vp, almost_full_or_en, 1);
   VOP_MODULE_SET(vop2, vp, line_flag_or_en, 1);
   if (vcstate->dsc_enable) {
       if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE) {
           vop2_crtc_enable_dsc(crtc, old_state, 0);
           vop2_crtc_enable_dsc(crtc, old_state, 1);
       } else {
           vop2_crtc_enable_dsc(crtc, old_state, vcstate->dsc_id);
       }
   }
   /* For RK3588, the reset value of background is 0xa0080200,
    * which will enable background and output a grey image. But
    * the reset value is just valid in first frame and disable
    * in follow frames. If the panel backlight is valid before
    * follow frames. The screen may flick a grey image. To avoid
    * this phenomenon appear, setting black background after
    * reset vop
    */
   if (vop2->version == VOP_VERSION_RK3588)
       VOP_MODULE_SET(vop2, vp, dsp_background, 0x80000000);
   if (is_vop3(vop2))
       vop3_setup_pipe_dly(vp, NULL);
 
   vop2_cfg_done(crtc);
 
   /*
    * when clear standby bits, it will take effect immediately,
    * This means the vp will start scan out immediately with
    * the timing it been configured before.
    * So we must make sure release standby after the display
    * timing is correctly configured.
    * This is important when switch resolution, such as
    * 4K-->720P:
    * if we release standby before 720P timing is configured,
    * the VP will start scan out immediately with 4K timing,
    * when we switch dclk to 74.25MHZ, VP timing is still 4K,
    * so VP scan out with 4K timing at 74.25MHZ dclk, this is
    * very slow, than this will trigger vblank timeout.
    *
    */
   VOP_MODULE_SET(vop2, vp, standby, 0);
 
   if (vp->mcu_timing.mcu_pix_total) {
       vop3_set_out_mode(crtc, vcstate->output_mode);
       vop3_mcu_mode_setup(crtc);
   }
 
   if (!vp->loader_protect)
       vop2_clk_reset(vp->dclk_rst);
   if (vcstate->dsc_enable)
       rk3588_vop2_dsc_cfg_done(crtc);
   drm_crtc_vblank_on(crtc);
   /*
    * restore the lut table.
    */
   if (vp->gamma_lut_active) {
       vop2_crtc_load_lut(crtc);
       vop2_cfg_done(crtc);
       vop2_wait_for_fs_by_done_bit_status(vp);
   }
out:
   vop2_unlock(vop2);
}
 
static int vop2_zpos_cmp(const void *a, const void *b)
{
   struct vop2_zpos *pa = (struct vop2_zpos *)a;
   struct vop2_zpos *pb = (struct vop2_zpos *)b;
 
   if (pa->zpos != pb->zpos)
       return pa->zpos - pb->zpos;
   else
       return pa->plane->base.id - pb->plane->base.id;
}
 
static int vop2_crtc_atomic_check(struct drm_crtc *crtc,
                 struct drm_crtc_state *crtc_state)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2_video_port *splice_vp;
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct rockchip_crtc_state *new_vcstate = to_rockchip_crtc_state(crtc_state);
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
 
   if (vop2_has_feature(vop2, VOP_FEATURE_SPLICE)) {
       if (adjusted_mode->hdisplay > VOP2_MAX_VP_OUTPUT_WIDTH) {
           vcstate->splice_mode = true;
           splice_vp = &vop2->vps[vp_data->splice_vp_id];
           splice_vp->splice_mode_right = true;
           splice_vp->left_vp = vp;
       }
   }
 
   if ((vcstate->request_refresh_rate != new_vcstate->request_refresh_rate) ||
       crtc_state->active_changed || crtc_state->mode_changed)
       vp->refresh_rate_change = true;
   else
       vp->refresh_rate_change = false;
 
   return 0;
}
 
static void vop3_disable_dynamic_hdr(struct vop2_video_port *vp, uint8_t win_phys_id)
{
   struct vop2 *vop2 = vp->vop2;
   struct vop2_win *win = vop2_find_win_by_phys_id(vop2, win_phys_id);
   struct drm_plane *plane = &win->base;
   struct drm_plane_state *pstate = plane->state;
   struct vop2_plane_state *vpstate = to_vop2_plane_state(pstate);
 
   VOP_MODULE_SET(vop2, vp, hdr10_en, 0);
   VOP_MODULE_SET(vop2, vp, hdr_vivid_en, 0);
   VOP_MODULE_SET(vop2, vp, hdr_vivid_bypass_en, 0);
   VOP_MODULE_SET(vop2, vp, hdr_lut_update_en, 0);
   VOP_MODULE_SET(vop2, vp, sdr2hdr_en, 0);
   VOP_MODULE_SET(vop2, vp, sdr2hdr_path_en, 0);
   VOP_MODULE_SET(vop2, vp, sdr2hdr_auto_gating_en, 1);
 
   vp->hdr_en = false;
   vp->hdr_in = false;
   vp->hdr_out = false;
   vp->sdr2hdr_en = false;
   vpstate->hdr_in = false;
   vpstate->hdr2sdr_en = false;
}
 
static void vop3_setup_hdrvivid(struct vop2_video_port *vp, uint8_t win_phys_id)
{
   struct vop2 *vop2 = vp->vop2;
   struct vop2_win *win = vop2_find_win_by_phys_id(vop2, win_phys_id);
   struct drm_plane *plane = &win->base;
   struct drm_plane_state *pstate = plane->state;
   struct vop2_plane_state *vpstate = to_vop2_plane_state(pstate);
   struct drm_crtc_state *cstate = vp->rockchip_crtc.crtc.state;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(cstate);
   unsigned long win_mask = vp->win_mask;
   int phys_id;
   struct hdrvivid_regs *hdrvivid_data;
   struct hdr_extend *hdr_data;
   struct rockchip_gem_object *lut_gem_obj;
   bool have_sdr_layer = false;
   uint32_t hdr_mode;
   int i;
   u32 *tone_lut_kvaddr;
   dma_addr_t tone_lut_mst;
 
   vp->hdr_en = false;
   vp->hdr_in = false;
   vp->hdr_out = false;
   vp->sdr2hdr_en = false;
   vpstate->hdr_in = false;
   vpstate->hdr2sdr_en = false;
 
   hdr_data = (struct hdr_extend *)vcstate->hdr_ext_data->data;
   hdrvivid_data = &hdr_data->hdrvivid_data;
 
   hdr_mode = hdrvivid_data->hdr_mode;
 
   if (hdr_mode > SDR2HLG && hdr_mode != SDR2HDR10_USERSPACE &&
       hdr_mode != SDR2HLG_USERSPACE) {
       DRM_ERROR("Invalid HDR mode:%d, beyond the mode range\n", hdr_mode);
       return;
   }
 
   /* adjust userspace hdr mode value to kernel value */
   if (hdr_mode == SDR2HDR10_USERSPACE)
       hdr_mode = SDR2HDR10;
   if (hdr_mode == SDR2HLG_USERSPACE)
       hdr_mode = SDR2HLG;
 
   if (hdr_mode <= HDR102SDR && vpstate->eotf != HDMI_EOTF_SMPTE_ST2084 && vpstate->eotf != HDMI_EOTF_BT_2100_HLG) {
       DRM_ERROR("Invalid HDR mode:%d, mismatch plane eotf:%d\n", hdr_mode,
             vpstate->eotf);
       return;
   }
 
   vp->hdrvivid_mode = hdr_mode;
   vcstate->yuv_overlay = false;
 
   if (hdr_mode <= HDR102SDR) {
       vp->hdr_en = true;
       vp->hdr_in = true;
       vpstate->hdr_in = true;
   } else {
       vp->sdr2hdr_en = true;
   }
 
   /*
    * To confirm whether need to enable sdr2hdr.
    */
   for_each_set_bit(phys_id, &win_mask, ROCKCHIP_MAX_LAYER) {
       win = vop2_find_win_by_phys_id(vop2, phys_id);
       plane = &win->base;
       pstate = plane->state;
       vpstate = to_vop2_plane_state(pstate);
 
       /* skip inactive plane */
       if (!vop2_plane_active(pstate))
           continue;
 
       if (vpstate->eotf != HDMI_EOTF_SMPTE_ST2084 &&
           vpstate->eotf != HDMI_EOTF_BT_2100_HLG) {
           have_sdr_layer = true;
           break;
       }
   }
 
   if (hdr_mode == PQHDR2SDR_WITH_DYNAMIC || hdr_mode == HLG2SDR_WITH_DYNAMIC ||
       hdr_mode == HLG2SDR_WITHOUT_DYNAMIC || hdr_mode == HDR102SDR) {
       vpstate->hdr2sdr_en = true;
   } else {
       vp->hdr_out = true;
       if (have_sdr_layer)
           vp->sdr2hdr_en = true;
   }
 
   /**
    * Config hdr ctrl registers
    */
   vop2_writel(vop2, RK3528_SDR2HDR_CTRL, hdrvivid_data->sdr2hdr_ctrl);
   vop2_writel(vop2, RK3528_HDRVIVID_CTRL, hdrvivid_data->hdrvivid_ctrl);
 
   VOP_MODULE_SET(vop2, vp, hdr10_en, vp->hdr_en);
   if (vp->hdr_en) {
       VOP_MODULE_SET(vop2, vp, hdr_vivid_en, (hdr_mode == HDR_BYPASS) ? 0 : 1);
       VOP_MODULE_SET(vop2, vp, hdr_vivid_path_mode,
                  (hdr_mode == HDR102SDR) ? PQHDR2SDR_WITH_DYNAMIC : hdr_mode);
       VOP_MODULE_SET(vop2, vp, hdr_vivid_bypass_en, (hdr_mode == HDR_BYPASS) ? 1 : 0);
   } else {
       VOP_MODULE_SET(vop2, vp, hdr_vivid_en, 0);
   }
   VOP_MODULE_SET(vop2, vp, sdr2hdr_en, vp->sdr2hdr_en);
   VOP_MODULE_SET(vop2, vp, sdr2hdr_path_en, vp->sdr2hdr_en);
   VOP_MODULE_SET(vop2, vp, sdr2hdr_auto_gating_en, vp->sdr2hdr_en ? 0 : 1);
 
   vop2_writel(vop2, RK3528_SDR_CFG_COE0, hdrvivid_data->sdr2hdr_coe0);
   vop2_writel(vop2, RK3528_SDR_CFG_COE1, hdrvivid_data->sdr2hdr_coe1);
   vop2_writel(vop2, RK3528_SDR_CSC_COE00_01, hdrvivid_data->sdr2hdr_csc_coe00_01);
   vop2_writel(vop2, RK3528_SDR_CSC_COE02_10, hdrvivid_data->sdr2hdr_csc_coe02_10);
   vop2_writel(vop2, RK3528_SDR_CSC_COE11_12, hdrvivid_data->sdr2hdr_csc_coe11_12);
   vop2_writel(vop2, RK3528_SDR_CSC_COE20_21, hdrvivid_data->sdr2hdr_csc_coe20_21);
   vop2_writel(vop2, RK3528_SDR_CSC_COE22, hdrvivid_data->sdr2hdr_csc_coe22);
 
   vop2_writel(vop2, RK3528_HDR_PQ_GAMMA, hdrvivid_data->hdr_pq_gamma);
   vop2_writel(vop2, RK3528_HLG_RFIX_SCALEFAC, hdrvivid_data->hlg_rfix_scalefac);
   vop2_writel(vop2, RK3528_HLG_MAXLUMA, hdrvivid_data->hlg_maxluma);
   vop2_writel(vop2, RK3528_HLG_R_TM_LIN2NON, hdrvivid_data->hlg_r_tm_lin2non);
 
   vop2_writel(vop2, RK3528_HDR_CSC_COE00_01, hdrvivid_data->hdr_csc_coe00_01);
   vop2_writel(vop2, RK3528_HDR_CSC_COE02_10, hdrvivid_data->hdr_csc_coe02_10);
   vop2_writel(vop2, RK3528_HDR_CSC_COE11_12, hdrvivid_data->hdr_csc_coe11_12);
   vop2_writel(vop2, RK3528_HDR_CSC_COE20_21, hdrvivid_data->hdr_csc_coe20_21);
   vop2_writel(vop2, RK3528_HDR_CSC_COE22, hdrvivid_data->hdr_csc_coe22);
 
   if (!vp->hdr_lut_gem_obj) {
       lut_gem_obj = rockchip_gem_create_object(vop2->drm_dev,
           RK_HDRVIVID_TONE_SCA_AXI_TAB_LENGTH * 4, true, 0);
       if (IS_ERR(lut_gem_obj)) {
           DRM_ERROR("create hdr lut obj failed\n");
           return;
       }
       vp->hdr_lut_gem_obj = lut_gem_obj;
   }
 
   tone_lut_kvaddr = (u32 *)vp->hdr_lut_gem_obj->kvaddr;
   tone_lut_mst = vp->hdr_lut_gem_obj->dma_addr;
 
   for (i = 0; i < RK_HDRVIVID_TONE_SCA_AXI_TAB_LENGTH; i++)
       *tone_lut_kvaddr++ =  hdrvivid_data->tone_sca_axi_tab[i];
 
   VOP_MODULE_SET(vop2, vp, lut_dma_rid, vp->lut_dma_rid - vp->id);
   VOP_MODULE_SET(vop2, vp, hdr_lut_mode, 1);
   VOP_MODULE_SET(vop2, vp, hdr_lut_mst, tone_lut_mst);
   VOP_MODULE_SET(vop2, vp, hdr_lut_update_en, 1);
   VOP_CTRL_SET(vop2, lut_dma_en, 1);
 
   for (i = 0; i < RK_HDRVIVID_GAMMA_CURVE_LENGTH; i++)
       vop2_writel(vop2, RK3528_HDRGAMMA_CURVE + i * 4, hdrvivid_data->hdrgamma_curve[i]);
 
   for (i = 0; i < RK_HDRVIVID_GAMMA_MDFVALUE_LENGTH; i++)
       vop2_writel(vop2, RK3528_HDRGAMMA_MDFVALUE + i * 4,
               hdrvivid_data->hdrgamma_mdfvalue[i]);
 
   for (i = 0; i < RK_SDR2HDR_INVGAMMA_CURVE_LENGTH; i++)
       vop2_writel(vop2, RK3528_SDRINVGAMMA_CURVE + i * 4,
               hdrvivid_data->sdrinvgamma_curve[i]);
 
   for (i = 0; i < RK_SDR2HDR_INVGAMMA_S_IDX_LENGTH; i++)
       vop2_writel(vop2, RK3528_SDRINVGAMMA_STARTIDX + i * 4,
               hdrvivid_data->sdrinvgamma_startidx[i]);
 
   for (i = 0; i < RK_SDR2HDR_INVGAMMA_C_IDX_LENGTH; i++)
       vop2_writel(vop2, RK3528_SDRINVGAMMA_CHANGEIDX + i * 4,
               hdrvivid_data->sdrinvgamma_changeidx[i]);
 
   for (i = 0; i < RK_SDR2HDR_SMGAIN_LENGTH; i++)
       vop2_writel(vop2, RK3528_SDR_SMGAIN + i * 4, hdrvivid_data->sdr_smgain[i]);
}
 
static void vop3_setup_dynamic_hdr(struct vop2_video_port *vp, uint8_t win_phys_id)
{
   struct drm_crtc_state *cstate = vp->rockchip_crtc.crtc.state;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(cstate);
   struct hdr_extend *hdr_data;
   uint32_t hdr_format;
 
   /* If hdr extend data is null, exit hdr mode */
   if (!vcstate->hdr_ext_data) {
       vop3_disable_dynamic_hdr(vp, win_phys_id);
       return;
   }
 
   hdr_data = (struct hdr_extend *)vcstate->hdr_ext_data->data;
   hdr_format = hdr_data->hdr_type;
 
   switch (hdr_format) {
   case HDR_NONE:
   case HDR_HDR10:
   case HDR_HLGSTATIC:
   case HDR_HDRVIVID:
       /*
        * hdr module support hdr10, hlg, vividhdr
        * sdr2hdr module support hdrnone for sdr2hdr
        */
       vop3_setup_hdrvivid(vp, win_phys_id);
       break;
   default:
       DRM_DEBUG("unsupprot hdr format:%u\n", hdr_format);
       break;
   }
}
 
static void vop2_setup_hdr10(struct vop2_video_port *vp, uint8_t win_phys_id)
{
   struct vop2 *vop2 = vp->vop2;
   struct vop2_win *win = vop2_find_win_by_phys_id(vop2, win_phys_id);
   struct drm_plane *plane = &win->base;
   struct drm_plane_state *pstate;
   struct drm_crtc_state *cstate = vp->rockchip_crtc.crtc.state;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   const struct vop_hdr_table *hdr_table = vp_data->hdr_table;
   struct rockchip_crtc_state *vcstate;
   struct vop2_plane_state *vpstate;
   uint32_t lut_mode = VOP2_HDR_LUT_MODE_AHB;
   uint32_t sdr2hdr_r2r_mode = 0;
   bool hdr_en = 0;
   bool hdr2sdr_en = 0;
   bool sdr2hdr_en = 0;
   bool sdr2hdr_tf = 0;
   bool hdr2sdr_tf_update = 1;
   bool sdr2hdr_tf_update = 0; /* default sdr2hdr curve is 1000 nit */
   unsigned long win_mask = vp->win_mask;
   int phys_id;
   bool have_sdr_layer = false;
 
   /*
    * Check whether this video port support hdr or not
    */
   if (!hdr_table)
       return;
 
   /*
    * right vp share the same crtc/plane state in splice mode
    */
   if (vp->splice_mode_right) {
       vcstate = to_rockchip_crtc_state(vp->left_vp->rockchip_crtc.crtc.state);
       pstate = win->left_win->base.state;
   } else {
       vcstate = to_rockchip_crtc_state(cstate);
       pstate = plane->state;
   }
 
   vpstate = to_vop2_plane_state(pstate);
 
   /*
    * HDR video plane input
    */
   if (vpstate->eotf == HDMI_EOTF_SMPTE_ST2084)
       hdr_en = 1;
 
   vp->hdr_en = hdr_en;
   vp->hdr_in = hdr_en;
   vp->hdr_out = (vcstate->eotf == HDMI_EOTF_SMPTE_ST2084) ? true : false;
 
   /*
    * only laryer0 support hdr2sdr
    * if we have more than one active win attached to the video port,
    * the other attached win must for ui, and should do sdr2hdr.
    *
    */
   if (vp->hdr_in && !vp->hdr_out)
       hdr2sdr_en = 1;
   vpstate->hdr_in = hdr_en;
   vpstate->hdr2sdr_en = hdr2sdr_en;
 
   /*
    * To confirm whether need to enable sdr2hdr.
    */
   for_each_set_bit(phys_id, &win_mask, ROCKCHIP_MAX_LAYER) {
       win = vop2_find_win_by_phys_id(vop2, phys_id);
       if (vp->splice_mode_right) {
           if (win->left_win)
               pstate = win->left_win->base.state;
           else
               pstate = NULL; /* this win is not activated */
       } else {
           pstate = win->base.state;
       }
 
       vpstate = pstate ? to_vop2_plane_state(pstate) : NULL;
 
       if (!vop2_plane_active(pstate))
           continue;
 
       if (vpstate->eotf != HDMI_EOTF_SMPTE_ST2084) {
           have_sdr_layer = true;
           break;
       }
   }
 
   if (have_sdr_layer && vp->hdr_out) {
       sdr2hdr_en = 1;
       sdr2hdr_r2r_mode = BT709_TO_BT2020;
       sdr2hdr_tf = SDR2HDR_FOR_HDR;
   }
   vp->sdr2hdr_en = sdr2hdr_en;
 
   VOP_MODULE_SET(vop2, vp, hdr10_en, hdr_en);
 
   if (hdr2sdr_en || sdr2hdr_en) {
       /*
        * HDR2SDR and SDR2HDR must overlay in yuv color space
        */
       vcstate->yuv_overlay = false;
       VOP_MODULE_SET(vop2, vp, hdr_lut_mode, lut_mode);
   }
 
   if (hdr2sdr_en) {
       if (hdr2sdr_tf_update)
           vop2_load_hdr2sdr_table(vp);
       VOP_MODULE_SET(vop2, vp, hdr2sdr_src_min, hdr_table->hdr2sdr_src_range_min);
       VOP_MODULE_SET(vop2, vp, hdr2sdr_src_max, hdr_table->hdr2sdr_src_range_max);
       VOP_MODULE_SET(vop2, vp, hdr2sdr_normfaceetf, hdr_table->hdr2sdr_normfaceetf);
       VOP_MODULE_SET(vop2, vp, hdr2sdr_dst_min, hdr_table->hdr2sdr_dst_range_min);
       VOP_MODULE_SET(vop2, vp, hdr2sdr_dst_max, hdr_table->hdr2sdr_dst_range_max);
       VOP_MODULE_SET(vop2, vp, hdr2sdr_normfacgamma, hdr_table->hdr2sdr_normfacgamma);
   }
   VOP_MODULE_SET(vop2, vp, hdr2sdr_en, hdr2sdr_en);
   VOP_MODULE_SET(vop2, vp, hdr2sdr_bypass_en, !hdr2sdr_en);
 
   if (sdr2hdr_en) {
       if (sdr2hdr_tf_update)
           vop2_load_sdr2hdr_table(vp, sdr2hdr_tf);
       VOP_MODULE_SET(vop2, vp, sdr2hdr_r2r_mode, sdr2hdr_r2r_mode);
   }
   VOP_MODULE_SET(vop2, vp, sdr2hdr_path_en, sdr2hdr_en);
   VOP_MODULE_SET(vop2, vp, sdr2hdr_oetf_en, sdr2hdr_en);
   VOP_MODULE_SET(vop2, vp, sdr2hdr_eotf_en, sdr2hdr_en);
   VOP_MODULE_SET(vop2, vp, sdr2hdr_r2r_en, sdr2hdr_en);
   VOP_MODULE_SET(vop2, vp, sdr2hdr_bypass_en, !sdr2hdr_en);
}
 
static void vop2_parse_alpha(struct vop2_alpha_config *alpha_config,
                struct vop2_alpha *alpha)
{
   int src_glb_alpha_en = (alpha_config->src_glb_alpha_value == 0xff) ? 0 : 1;
   int dst_glb_alpha_en = (alpha_config->dst_glb_alpha_value == 0xff) ? 0 : 1;
   int src_color_mode = alpha_config->src_premulti_en ? ALPHA_SRC_PRE_MUL : ALPHA_SRC_NO_PRE_MUL;
   int dst_color_mode = alpha_config->dst_premulti_en ? ALPHA_SRC_PRE_MUL : ALPHA_SRC_NO_PRE_MUL;
 
   alpha->src_color_ctrl.val = 0;
   alpha->dst_color_ctrl.val = 0;
   alpha->src_alpha_ctrl.val = 0;
   alpha->dst_alpha_ctrl.val = 0;
 
   if (!alpha_config->src_pixel_alpha_en)
       alpha->src_color_ctrl.bits.blend_mode = ALPHA_GLOBAL;
   else if (alpha_config->src_pixel_alpha_en && !src_glb_alpha_en)
       alpha->src_color_ctrl.bits.blend_mode = ALPHA_PER_PIX;
   else
       alpha->src_color_ctrl.bits.blend_mode = ALPHA_PER_PIX_GLOBAL;
 
   alpha->src_color_ctrl.bits.alpha_en = 1;
 
   if (alpha->src_color_ctrl.bits.blend_mode == ALPHA_GLOBAL) {
       alpha->src_color_ctrl.bits.color_mode = src_color_mode;
       alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_SRC_GLOBAL;
   } else if (alpha->src_color_ctrl.bits.blend_mode == ALPHA_PER_PIX) {
       alpha->src_color_ctrl.bits.color_mode = src_color_mode;
       alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_ONE;
   } else {
       alpha->src_color_ctrl.bits.color_mode = ALPHA_SRC_PRE_MUL;
       alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_SRC_GLOBAL;
   }
   alpha->src_color_ctrl.bits.glb_alpha = alpha_config->src_glb_alpha_value;
   alpha->src_color_ctrl.bits.alpha_mode = ALPHA_STRAIGHT;
   alpha->src_color_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION;
 
   alpha->dst_color_ctrl.bits.alpha_mode = ALPHA_STRAIGHT;
   alpha->dst_color_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION;
   alpha->dst_color_ctrl.bits.blend_mode = ALPHA_GLOBAL;
   alpha->dst_color_ctrl.bits.glb_alpha = alpha_config->dst_glb_alpha_value;
   alpha->dst_color_ctrl.bits.color_mode = dst_color_mode;
   alpha->dst_color_ctrl.bits.factor_mode = ALPHA_SRC_INVERSE;
 
   alpha->src_alpha_ctrl.bits.alpha_mode = ALPHA_STRAIGHT;
   alpha->src_alpha_ctrl.bits.blend_mode = alpha->src_color_ctrl.bits.blend_mode;
   alpha->src_alpha_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION;
   alpha->src_alpha_ctrl.bits.factor_mode = ALPHA_ONE;
 
   alpha->dst_alpha_ctrl.bits.alpha_mode = ALPHA_STRAIGHT;
   if (alpha_config->dst_pixel_alpha_en && !dst_glb_alpha_en)
       alpha->dst_alpha_ctrl.bits.blend_mode = ALPHA_PER_PIX;
   else
       alpha->dst_alpha_ctrl.bits.blend_mode = ALPHA_PER_PIX_GLOBAL;
   alpha->dst_alpha_ctrl.bits.alpha_cal_mode = ALPHA_NO_SATURATION;
   alpha->dst_alpha_ctrl.bits.factor_mode = ALPHA_SRC_INVERSE;
}
 
static int vop2_find_start_mixer_id_for_vp(struct vop2 *vop2, uint8_t port_id)
{
   struct vop2_video_port *vp;
   int used_layer = 0;
   int i;
 
   for (i = 0; i < port_id; i++) {
       vp = &vop2->vps[i];
       used_layer += hweight32(vp->win_mask);
   }
 
   return used_layer;
}
 
/*
 * src: top layer
 * dst: bottom layer.
 * Cluster mixer default use win1 as top layer
 */
static void vop2_setup_cluster_alpha(struct vop2 *vop2, struct vop2_cluster *cluster)
{
   uint32_t src_color_ctrl_offset = cluster->main->regs->cluster->src_color_ctrl.offset;
   uint32_t dst_color_ctrl_offset = cluster->main->regs->cluster->dst_color_ctrl.offset;
   uint32_t src_alpha_ctrl_offset = cluster->main->regs->cluster->src_alpha_ctrl.offset;
   uint32_t dst_alpha_ctrl_offset = cluster->main->regs->cluster->dst_alpha_ctrl.offset;
   struct drm_framebuffer *fb;
   struct vop2_alpha_config alpha_config;
   struct vop2_alpha alpha;
   struct vop2_win *main_win = cluster->main;
   struct vop2_win *sub_win = cluster->sub;
   struct drm_plane *plane;
   struct vop2_plane_state *main_vpstate;
   struct vop2_plane_state *sub_vpstate;
   struct vop2_plane_state *top_win_vpstate;
   struct vop2_plane_state *bottom_win_vpstate;
   bool src_pixel_alpha_en = false;
   u16 src_glb_alpha_val = 0xff, dst_glb_alpha_val = 0xff;
   bool premulti_en = false;
   bool swap = false;
 
   if (!sub_win) {
       /* At one win mode, win0 is dst/bottom win, and win1 is a all zero src/top win */
 
       /*
        * right cluster share the same plane state in splice mode
        */
       if (cluster->splice_mode)
           plane = &main_win->left_win->base;
       else
           plane = &main_win->base;
 
       top_win_vpstate = NULL;
       bottom_win_vpstate = to_vop2_plane_state(plane->state);
       src_glb_alpha_val = 0;
       dst_glb_alpha_val = bottom_win_vpstate->global_alpha;
   } else {
       plane = &sub_win->base;
       sub_vpstate = to_vop2_plane_state(plane->state);
       plane = &main_win->base;
       main_vpstate = to_vop2_plane_state(plane->state);
       if (main_vpstate->zpos > sub_vpstate->zpos) {
           swap = 1;
           top_win_vpstate = main_vpstate;
           bottom_win_vpstate = sub_vpstate;
       } else {
           swap = 0;
           top_win_vpstate = sub_vpstate;
           bottom_win_vpstate = main_vpstate;
       }
       src_glb_alpha_val = top_win_vpstate->global_alpha;
       dst_glb_alpha_val = bottom_win_vpstate->global_alpha;
   }
 
   if (top_win_vpstate) {
       fb = top_win_vpstate->base.fb;
       if (!fb)
           return;
       if (top_win_vpstate->base.pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
           premulti_en = true;
       else
           premulti_en = false;
       src_pixel_alpha_en = is_alpha_support(fb->format->format);
   }
   fb = bottom_win_vpstate->base.fb;
   if (!fb)
       return;
   alpha_config.src_premulti_en = premulti_en;
   alpha_config.dst_premulti_en = false;
   alpha_config.src_pixel_alpha_en = src_pixel_alpha_en;
   alpha_config.dst_pixel_alpha_en = true; /* alpha value need transfer to next mix */
   alpha_config.src_glb_alpha_value = src_glb_alpha_val;
   alpha_config.dst_glb_alpha_value = dst_glb_alpha_val;
   vop2_parse_alpha(&alpha_config, &alpha);
 
   alpha.src_color_ctrl.bits.src_dst_swap = swap;
   vop2_writel(vop2, src_color_ctrl_offset, alpha.src_color_ctrl.val);
   vop2_writel(vop2, dst_color_ctrl_offset, alpha.dst_color_ctrl.val);
   vop2_writel(vop2, src_alpha_ctrl_offset, alpha.src_alpha_ctrl.val);
   vop2_writel(vop2, dst_alpha_ctrl_offset, alpha.dst_alpha_ctrl.val);
}
 
static void vop2_setup_alpha(struct vop2_video_port *vp,
                const struct vop2_zpos *vop2_zpos)
{
   struct vop2 *vop2 = vp->vop2;
   uint32_t src_color_ctrl_offset = vop2->data->ctrl->src_color_ctrl.offset;
   uint32_t dst_color_ctrl_offset = vop2->data->ctrl->dst_color_ctrl.offset;
   uint32_t src_alpha_ctrl_offset = vop2->data->ctrl->src_alpha_ctrl.offset;
   uint32_t dst_alpha_ctrl_offset = vop2->data->ctrl->dst_alpha_ctrl.offset;
   unsigned long win_mask = vp->win_mask;
   const struct vop2_zpos *zpos;
   struct vop2_plane_state *vpstate;
   struct vop2_alpha_config alpha_config;
   struct vop2_alpha alpha;
   struct vop2_win *win;
   struct drm_plane_state *pstate;
   struct drm_framebuffer *fb;
   int pixel_alpha_en;
   int premulti_en = 1;
   int mixer_id;
   int phys_id;
   uint32_t offset;
   int i;
   bool bottom_layer_alpha_en = false;
   u32 dst_global_alpha = 0xff;
 
   for_each_set_bit(phys_id, &win_mask, ROCKCHIP_MAX_LAYER) {
       win = vop2_find_win_by_phys_id(vop2, phys_id);
       if (win->splice_mode_right)
           pstate = win->left_win->base.state;
       else
           pstate = win->base.state;
 
       vpstate = to_vop2_plane_state(pstate);
 
       if (!vop2_plane_active(pstate))
           continue;
 
       if (vpstate->zpos == 0 && vpstate->global_alpha != 0xff &&
           !vop2_cluster_window(win)) {
           /*
            * If bottom layer have global alpha effect [except cluster layer,
            * because cluster have deal with bottom layer global alpha value
            * at cluster mix], bottom layer mix need deal with global alpha.
            */
           bottom_layer_alpha_en = true;
           dst_global_alpha = vpstate->global_alpha;
           if (pstate->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
               premulti_en = 1;
           else
               premulti_en = 0;
 
           break;
       }
   }
 
   mixer_id = vop2_find_start_mixer_id_for_vp(vop2, vp->id);
 
   if (vop2->version == VOP_VERSION_RK3588 &&
       vp->hdr10_at_splice_mode && vp->id == 0)
       mixer_id++;/* fixed path for rk3588: layer1 -> hdr10_1 */
 
   alpha_config.dst_pixel_alpha_en = true; /* alpha value need transfer to next mix */
   for (i = 1; i < vp->nr_layers; i++) {
       zpos = &vop2_zpos[i];
       win = vop2_find_win_by_phys_id(vop2, zpos->win_phys_id);
       if (win->splice_mode_right)
           pstate = win->left_win->base.state;
       else
           pstate = win->base.state;
 
       vpstate = to_vop2_plane_state(pstate);
       fb = pstate->fb;
       if (pstate->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
           premulti_en = 1;
       else
           premulti_en = 0;
       pixel_alpha_en = is_alpha_support(fb->format->format);
 
       alpha_config.src_premulti_en = premulti_en;
       if (bottom_layer_alpha_en && i == 1) {/* Cd = Cs + (1 - As) * Cd * Agd */
           alpha_config.dst_premulti_en = false;
           alpha_config.src_pixel_alpha_en = pixel_alpha_en;
           alpha_config.src_glb_alpha_value =  vpstate->global_alpha;
           alpha_config.dst_glb_alpha_value = dst_global_alpha;
       } else if (vop2_cluster_window(win)) {/* Mix output data only have pixel alpha */
           alpha_config.dst_premulti_en = true;
           alpha_config.src_pixel_alpha_en = true;
           alpha_config.src_glb_alpha_value = 0xff;
           alpha_config.dst_glb_alpha_value = 0xff;
       } else {/* Cd = Cs + (1 - As) * Cd */
           alpha_config.dst_premulti_en = true;
           alpha_config.src_pixel_alpha_en = pixel_alpha_en;
           alpha_config.src_glb_alpha_value =  vpstate->global_alpha;
           alpha_config.dst_glb_alpha_value = 0xff;
       }
       vop2_parse_alpha(&alpha_config, &alpha);
 
       offset = (mixer_id + i - 1) * 0x10;
       vop2_writel(vop2, src_color_ctrl_offset + offset, alpha.src_color_ctrl.val);
       vop2_writel(vop2, dst_color_ctrl_offset + offset, alpha.dst_color_ctrl.val);
       vop2_writel(vop2, src_alpha_ctrl_offset + offset, alpha.src_alpha_ctrl.val);
       vop2_writel(vop2, dst_alpha_ctrl_offset + offset, alpha.dst_alpha_ctrl.val);
   }
 
   if (bottom_layer_alpha_en || vp->hdr_en) {
       /* Transfer pixel alpha to hdr mix */
       alpha_config.src_premulti_en = premulti_en;
       alpha_config.dst_premulti_en = true;
       alpha_config.src_pixel_alpha_en = true;
       alpha_config.src_glb_alpha_value = 0xff;
       alpha_config.dst_glb_alpha_value = 0xff;
       vop2_parse_alpha(&alpha_config, &alpha);
 
       VOP_MODULE_SET(vop2, vp, hdr_src_color_ctrl,
                  alpha.src_color_ctrl.val);
       VOP_MODULE_SET(vop2, vp, hdr_dst_color_ctrl,
                  alpha.dst_color_ctrl.val);
       VOP_MODULE_SET(vop2, vp, hdr_src_alpha_ctrl,
                  alpha.src_alpha_ctrl.val);
       VOP_MODULE_SET(vop2, vp, hdr_dst_alpha_ctrl,
                  alpha.dst_alpha_ctrl.val);
   } else {
       VOP_MODULE_SET(vop2, vp, hdr_src_color_ctrl, 0);
   }
 
   /* Transfer pixel alpha value to next mix */
   alpha_config.src_premulti_en = true;
   alpha_config.dst_premulti_en = true;
   alpha_config.src_pixel_alpha_en = false;
   alpha_config.src_glb_alpha_value = 0xff;
   alpha_config.dst_glb_alpha_value = 0xff;
   vop2_parse_alpha(&alpha_config, &alpha);
 
   for (; i < hweight32(vp->win_mask); i++) {
       offset = (mixer_id + i - 1) * 0x10;
 
       vop2_writel(vop2, src_color_ctrl_offset + offset, alpha.src_alpha_ctrl.val);
       vop2_writel(vop2, dst_color_ctrl_offset + offset, alpha.dst_color_ctrl.val);
       vop2_writel(vop2, src_alpha_ctrl_offset + offset, alpha.src_alpha_ctrl.val);
       vop2_writel(vop2, dst_alpha_ctrl_offset + offset, alpha.dst_alpha_ctrl.val);
   }
}
 
static void vop3_setup_alpha(struct vop2_video_port *vp,
                const struct vop2_zpos *vop2_zpos)
{
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_video_port_data *vp_data = &vop2->data->vp[vp->id];
   const struct vop3_ovl_regs *ovl_regs = vop2->data->vp[vp->id].ovl_regs;
   uint32_t src_color_ctrl_offset = ovl_regs->layer_mix_regs->src_color_ctrl.offset;
   uint32_t dst_color_ctrl_offset = ovl_regs->layer_mix_regs->dst_color_ctrl.offset;
   uint32_t src_alpha_ctrl_offset = ovl_regs->layer_mix_regs->src_alpha_ctrl.offset;
   uint32_t dst_alpha_ctrl_offset = ovl_regs->layer_mix_regs->dst_alpha_ctrl.offset;
   unsigned long win_mask = vp->win_mask;
   const struct vop2_zpos *zpos;
   struct vop2_plane_state *vpstate;
   struct vop2_alpha_config alpha_config;
   union vop2_bg_alpha_ctrl bg_alpha_ctrl;
   struct vop2_alpha alpha;
   struct vop2_win *win;
   struct drm_plane_state *pstate;
   struct drm_framebuffer *fb;
   int pixel_alpha_en;
   int premulti_en = 1;
   int phys_id;
   uint32_t offset;
   int i;
   bool bottom_layer_alpha_en = false;
   u32 dst_global_alpha = 0xff;
 
   for_each_set_bit(phys_id, &win_mask, ROCKCHIP_MAX_LAYER) {
       win = vop2_find_win_by_phys_id(vop2, phys_id);
       pstate = win->base.state;
       vpstate = to_vop2_plane_state(pstate);
 
       if (!vop2_plane_active(pstate))
           continue;
 
       if (vpstate->zpos == 0 && vpstate->global_alpha != 0xff &&
           !vop2_cluster_window(win)) {
           /*
            * If bottom layer have global alpha effect [except cluster layer,
            * because cluster have deal with bottom layer global alpha value
            * at cluster mix], bottom layer mix need deal with global alpha.
            */
           bottom_layer_alpha_en = true;
           dst_global_alpha = vpstate->global_alpha;
           if (pstate->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
               premulti_en = 1;
           else
               premulti_en = 0;
 
           break;
       }
   }
 
   alpha_config.dst_pixel_alpha_en = true; /* alpha value need transfer to next mix */
   for (i = 1; i < vp->nr_layers; i++) {
       zpos = &vop2_zpos[i];
       win = vop2_find_win_by_phys_id(vop2, zpos->win_phys_id);
       pstate = win->base.state;
       vpstate = to_vop2_plane_state(pstate);
       fb = pstate->fb;
       if (pstate->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
           premulti_en = 1;
       else
           premulti_en = 0;
       pixel_alpha_en = is_alpha_support(fb->format->format);
 
       alpha_config.src_premulti_en = premulti_en;
       if (bottom_layer_alpha_en && i == 1) {/* Cd = Cs + (1 - As) * Cd * Agd */
           alpha_config.dst_premulti_en = false;
           alpha_config.src_pixel_alpha_en = pixel_alpha_en;
           alpha_config.src_glb_alpha_value =  vpstate->global_alpha;
           alpha_config.dst_glb_alpha_value = dst_global_alpha;
       } else if (vop2_cluster_window(win)) {/* Mix output data only have pixel alpha */
           alpha_config.dst_premulti_en = true;
           alpha_config.src_pixel_alpha_en = true;
           alpha_config.src_glb_alpha_value = 0xff;
           alpha_config.dst_glb_alpha_value = 0xff;
       } else {/* Cd = Cs + (1 - As) * Cd */
           alpha_config.dst_premulti_en = true;
           alpha_config.src_pixel_alpha_en = pixel_alpha_en;
           alpha_config.src_glb_alpha_value =  vpstate->global_alpha;
           alpha_config.dst_glb_alpha_value = 0xff;
       }
       vop2_parse_alpha(&alpha_config, &alpha);
 
       offset = (i - 1) * 0x10;
       vop2_writel(vop2, src_color_ctrl_offset + offset, alpha.src_color_ctrl.val);
       vop2_writel(vop2, dst_color_ctrl_offset + offset, alpha.dst_color_ctrl.val);
       vop2_writel(vop2, src_alpha_ctrl_offset + offset, alpha.src_alpha_ctrl.val);
       vop2_writel(vop2, dst_alpha_ctrl_offset + offset, alpha.dst_alpha_ctrl.val);
   }
 
   /* Transfer pixel alpha value to next mix */
   alpha_config.src_premulti_en = true;
   alpha_config.dst_premulti_en = true;
   alpha_config.src_pixel_alpha_en = false;
   alpha_config.src_glb_alpha_value = 0xff;
   alpha_config.dst_glb_alpha_value = 0xff;
   vop2_parse_alpha(&alpha_config, &alpha);
 
   for (; i < vop2->data->nr_layers; i++) {
       offset = (i - 1) * 0x10;
 
       vop2_writel(vop2, src_color_ctrl_offset + offset, alpha.src_color_ctrl.val);
       vop2_writel(vop2, dst_color_ctrl_offset + offset, alpha.dst_color_ctrl.val);
       vop2_writel(vop2, src_alpha_ctrl_offset + offset, alpha.src_alpha_ctrl.val);
       vop2_writel(vop2, dst_alpha_ctrl_offset + offset, alpha.dst_alpha_ctrl.val);
   }
 
   if (vp_data->feature & (VOP_FEATURE_HDR10 | VOP_FEATURE_VIVID_HDR)) {
       src_color_ctrl_offset = ovl_regs->hdr_mix_regs->src_color_ctrl.offset;
       dst_color_ctrl_offset = ovl_regs->hdr_mix_regs->dst_color_ctrl.offset;
       src_alpha_ctrl_offset = ovl_regs->hdr_mix_regs->src_alpha_ctrl.offset;
       dst_alpha_ctrl_offset = ovl_regs->hdr_mix_regs->dst_alpha_ctrl.offset;
 
       if (bottom_layer_alpha_en || vp->hdr_en) {
           /* Transfer pixel alpha to hdr mix */
           alpha_config.src_premulti_en = premulti_en;
           alpha_config.dst_premulti_en = true;
           alpha_config.src_pixel_alpha_en = true;
           alpha_config.src_glb_alpha_value = 0xff;
           alpha_config.dst_glb_alpha_value = 0xff;
           vop2_parse_alpha(&alpha_config, &alpha);
 
           vop2_writel(vop2, src_color_ctrl_offset, alpha.src_color_ctrl.val);
           vop2_writel(vop2, dst_color_ctrl_offset, alpha.dst_color_ctrl.val);
           vop2_writel(vop2, src_alpha_ctrl_offset, alpha.src_alpha_ctrl.val);
           vop2_writel(vop2, dst_alpha_ctrl_offset, alpha.dst_alpha_ctrl.val);
       } else {
           vop2_writel(vop2, src_color_ctrl_offset, 0);
           vop2_writel(vop2, dst_color_ctrl_offset, 0);
           vop2_writel(vop2, src_alpha_ctrl_offset, 0);
           vop2_writel(vop2, dst_alpha_ctrl_offset, 0);
       }
   }
 
   bg_alpha_ctrl.bits.alpha_en = 0;
   VOP_MODULE_SET(vop2, vp, bg_mix_ctrl, bg_alpha_ctrl.val);
}
 
static u32 vop2_layer_cfg_update(struct vop2_layer *layer, u32 old_layer_cfg, u8 win_layer_id)
{
   const struct vop_reg *reg = &layer->regs->layer_sel;
   u32 mask = reg->mask;
   u32 shift = reg->shift;
 
   return (old_layer_cfg & ~(mask << shift)) | ((win_layer_id & mask) << shift);
}
 
static u16 vop2_calc_bg_ovl_and_port_mux(struct vop2_video_port *vp)
{
   struct vop2_video_port *prev_vp;
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   u16 port_mux_cfg = 0;
   u8 port_mux;
   u8 used_layers = 0;
   int i;
 
   for (i = 0; i < vop2_data->nr_vps - 1; i++) {
       prev_vp = &vop2->vps[i];
       used_layers += hweight32(prev_vp->win_mask);
       if (vop2->version == VOP_VERSION_RK3588) {
           if (vop2->vps[0].hdr10_at_splice_mode && i == 0)
               used_layers += 1;
           if (vop2->vps[0].hdr10_at_splice_mode && i == 1)
               used_layers -= 1;
       }
       /*
        * when a window move from vp0 to vp1, or vp0 to vp2,
        * it should flow these steps:
        * (1) first commit, disable this windows on VP0,
        *     keep the win_mask of VP0.
        * (2) second commit, set this window to VP1, clear
        *     the corresponding win_mask on VP0, and set the
        *     corresponding win_mask on VP1.
        *  This means we only know the decrease of the windows
        *  number of VP0 until VP1 take it, so the port_mux of
        *  VP0 should change at VP1's commit.
        */
       if (used_layers == 0)
           port_mux = 8;
       else
           port_mux = used_layers - 1;
 
       port_mux_cfg |= port_mux << (prev_vp->id * 4);
 
       if (port_mux > vop2_data->nr_mixers)
           prev_vp->bg_ovl_dly = 0;
       else
           prev_vp->bg_ovl_dly = (vop2_data->nr_mixers - port_mux) << 1;
   }
 
   port_mux_cfg |= 7 << (4 * (vop2->data->nr_vps - 1));
 
   return port_mux_cfg;
}
 
static void vop2_setup_port_mux(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   u16 port_mux_cfg;
 
   port_mux_cfg = vop2_calc_bg_ovl_and_port_mux(vp);
   spin_lock(&vop2->reg_lock);
   if (vop2->port_mux_cfg != port_mux_cfg) {
       VOP_CTRL_SET(vop2, ovl_port_mux_cfg, port_mux_cfg);
       vp->skip_vsync = true;
       vop2_cfg_done(&vp->rockchip_crtc.crtc);
       vop2->port_mux_cfg = port_mux_cfg;
       vop2_wait_for_port_mux_done(vop2);
   }
   spin_unlock(&vop2->reg_lock);
}
 
static void vop2_setup_layer_mixer_for_vp(struct vop2_video_port *vp,
                     const struct vop2_zpos *vop2_zpos)
{
   struct vop2_video_port *prev_vp;
   struct vop2 *vop2 = vp->vop2;
   struct vop2_layer *layer = &vop2->layers[0];
   u8 port_id = vp->id;
   const struct vop2_zpos *zpos;
   struct vop2_win *win;
   u8 used_layers = 0;
   u8 layer_id, win_phys_id;
   u32 layer_cfg_reg_offset = layer->regs->layer_sel.offset;
   u8 nr_layers = vp->nr_layers;
   u32 old_layer_cfg = 0;
   u32 new_layer_cfg = 0;
   u32 atv_layer_cfg;
   int i;
 
   /*
    * Win and layer must map one by one, if a win is selected
    * by two layers, unexpected error may happen.
    * So when we attach a new win to a layer, we also move the
    * old win of the layer to the layer where the new win comes from.
    *
    */
   for (i = 0; i < port_id; i++) {
       prev_vp = &vop2->vps[i];
       used_layers += hweight32(prev_vp->win_mask);
   }
 
   old_layer_cfg = vop2->regsbak[layer_cfg_reg_offset >> 2];
   new_layer_cfg = old_layer_cfg;
 
   if (vp->hdr10_at_splice_mode)
       nr_layers *= 2;
 
   for (i = 0; i < nr_layers; i++) {
       layer = &vop2->layers[used_layers + i];
       zpos = &vop2_zpos[i];
       win = vop2_find_win_by_phys_id(vop2, zpos->win_phys_id);
       layer_id = win->layer_id;
       win_phys_id = layer->win_phys_id;
       VOP_CTRL_SET(vop2, win_vp_id[win->phys_id], port_id);
       new_layer_cfg = vop2_layer_cfg_update(layer, new_layer_cfg, win->layer_sel_id[vp->id]);
       win->layer_id = layer->id;
       layer->win_phys_id = win->phys_id;
       layer = &vop2->layers[layer_id];
       win = vop2_find_win_by_phys_id(vop2, win_phys_id);
       new_layer_cfg = vop2_layer_cfg_update(layer, new_layer_cfg, win->layer_sel_id[vp->id]);
       win->layer_id = layer_id;
       layer->win_phys_id = win_phys_id;
   }
 
   atv_layer_cfg = vop2_read_layer_cfg(vop2);
   if (new_layer_cfg != old_layer_cfg &&
       atv_layer_cfg != old_layer_cfg &&
       !vp->splice_mode_right) {
       dev_dbg(vop2->dev, "wait old_layer_sel: 0x%x\n", old_layer_cfg);
       vop2_wait_for_layer_cfg_done(vop2, old_layer_cfg);
   }
   vop2_writel(vop2, RK3568_OVL_LAYER_SEL, new_layer_cfg);
   if (new_layer_cfg != old_layer_cfg)
       VOP_CTRL_SET(vop2, ovl_cfg_done_port, vp->id);
   VOP_CTRL_SET(vop2, ovl_port_mux_cfg_done_imd, 0);
}
 
static void vop3_setup_layer_sel_for_vp(struct vop2_video_port *vp,
                   const struct vop2_zpos *vop2_zpos)
{
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_zpos *zpos;
   struct vop2_win *win;
   u32 layer_sel = 0;
   u8 port_id = vp->id;
   u8 layer_sel_id;
   u8 layer_sel_none = 0xff;
   int i;
 
   for (i = 0; i < vop2->data->nr_layers; i++) {
       layer_sel_id = layer_sel_none;
       if (i < vp->nr_layers) {
           zpos = &vop2_zpos[i];
           win = vop2_find_win_by_phys_id(vop2, zpos->win_phys_id);
           if (win->old_vp_mask != win->vp_mask && VOP_WIN_GET(vop2, win, enable))
               DRM_ERROR("must wait %s disabled and change vp_mask[0x%x->0x%x]\n",
                     win->name, win->old_vp_mask, win->vp_mask);
           VOP_CTRL_SET(vop2, win_vp_id[win->phys_id], port_id);
           layer_sel_id = win->layer_sel_id[vp->id];
       }
       layer_sel |= layer_sel_id << i * 4;
   }
   VOP_MODULE_SET(vop2, vp, layer_sel, layer_sel);
}
 
/*
 * HDR window is fixed(not move in the overlay path with port_mux change)
 * and is the most slow window. And the bg is the fast. So other windows
 * and bg need to add delay number to keep align with the most slow window.
 * The delay number list in the trm is a relative value for port_mux set at
 * last level.
 */
static void vop2_setup_dly_for_vp(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   struct vop2_video_port *left_vp = vp->left_vp;
   struct drm_crtc *crtc = &vp->rockchip_crtc.crtc;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
   u16 hsync_len = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
   u16 hdisplay = adjusted_mode->crtc_hdisplay;
   u32 bg_dly = vp_data->pre_scan_max_dly[0];
   u32 pre_scan_dly;
 
   if (vp_data->hdr_table)  {
       if (vp->hdr_in) {
           if (vp->hdr_out)
               bg_dly = vp_data->pre_scan_max_dly[2];
       } else {
           if (vp->hdr_out)
               bg_dly = vp_data->pre_scan_max_dly[1];
           else
               bg_dly = vp_data->pre_scan_max_dly[3];
       }
   }
 
   if (!vp->hdr_in ||
       (vop2->version == VOP_VERSION_RK3588 && vp->hdr_out))
       bg_dly -= vp->bg_ovl_dly;
 
   /*
    * right vp share the same crtc state in splice mode
    */
   if (vp->splice_mode_right) {
       vcstate = to_rockchip_crtc_state(left_vp->rockchip_crtc.crtc.state);
       adjusted_mode = &left_vp->rockchip_crtc.crtc.state->adjusted_mode;
       hsync_len = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
       hdisplay = adjusted_mode->crtc_hdisplay;
   }
 
   if (vcstate->splice_mode)
       pre_scan_dly = bg_dly + (hdisplay >> 2) - 1;
   else
       pre_scan_dly = bg_dly + (hdisplay >> 1) - 1;
 
   if (vop2->version == VOP_VERSION_RK3588 && hsync_len < 8)
       hsync_len = 8;
 
   pre_scan_dly = (pre_scan_dly << 16) | hsync_len;
 
   VOP_MODULE_SET(vop2, vp, bg_dly, bg_dly);
   VOP_MODULE_SET(vop2, vp, pre_scan_htiming, pre_scan_dly);
}
 
static void vop2_setup_dly_for_window(struct vop2_video_port *vp, const struct vop2_zpos *vop2_zpos)
{
   struct vop2 *vop2 = vp->vop2;
   struct vop2_plane_state *vpstate;
   const struct vop2_zpos *zpos;
   struct drm_plane *plane;
   struct vop2_win *win;
   uint32_t dly;
   int i = 0;
 
   for (i = 0; i < vp->nr_layers; i++) {
       zpos = &vop2_zpos[i];
       win = vop2_find_win_by_phys_id(vop2, zpos->win_phys_id);
       /*
        * right vp share the same plane state in splice mode
        */
       if (vp->splice_mode_right) {
           plane = &win->left_win->base;
           vpstate = to_vop2_plane_state(plane->state);
       } else {
           plane = &win->base;
           vpstate = to_vop2_plane_state(plane->state);
       }
 
       if (vp->hdr_in && !vp->hdr_out && !vpstate->hdr_in) {
           dly = win->dly[VOP2_DLY_MODE_HISO_S];
           dly += vp->bg_ovl_dly;
       } else if (vp->hdr_in && vp->hdr_out && vpstate->hdr_in) {
           dly = win->dly[VOP2_DLY_MODE_HIHO_H];
           dly -= vp->bg_ovl_dly;
       } else {
           dly = win->dly[VOP2_DLY_MODE_DEFAULT];
       }
       if (vop2_cluster_window(win))
           dly |= dly << 8;
 
       VOP_CTRL_SET(vop2, win_dly[win->phys_id], dly);
   }
}
 
static void rk3588_vop2_setup_hdr10_splice_layer_mixer(struct drm_crtc *crtc,
                              struct vop2_zpos *vop2_zpos,
                              struct vop2_zpos *vop2_zpos_splice)
{
   int zpos_id, i;
   struct vop2_zpos *vop2_zpos_splice_hdr;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
 
   vop2_zpos_splice_hdr = kmalloc_array(vop2->data->win_size, sizeof(*vop2_zpos),
                    GFP_KERNEL);
   if (!vop2_zpos_splice_hdr)
       goto out;
 
   zpos_id = 0;
   vop2_zpos_splice_hdr[zpos_id].zpos = zpos_id;
   vop2_zpos_splice_hdr[zpos_id].win_phys_id = vop2_zpos[0].win_phys_id;
   vop2_zpos_splice_hdr[zpos_id].plane = vop2_zpos[0].plane;
 
   zpos_id++;
   vop2_zpos_splice_hdr[zpos_id].zpos = zpos_id;
   vop2_zpos_splice_hdr[zpos_id].win_phys_id = vop2_zpos_splice[0].win_phys_id;
   vop2_zpos_splice_hdr[zpos_id].plane = vop2_zpos_splice[0].plane;
 
   for (i = 1; i < vp->nr_layers; i++) {
       zpos_id++;
       vop2_zpos_splice_hdr[zpos_id].zpos = zpos_id;
       vop2_zpos_splice_hdr[zpos_id].win_phys_id = vop2_zpos[i].win_phys_id;
       vop2_zpos_splice_hdr[zpos_id].plane = vop2_zpos[i].plane;
   }
 
   for (i = 1; i < vp->nr_layers; i++) {
       zpos_id++;
       vop2_zpos_splice_hdr[zpos_id].zpos = zpos_id;
       vop2_zpos_splice_hdr[zpos_id].win_phys_id = vop2_zpos_splice[i].win_phys_id;
       vop2_zpos_splice_hdr[zpos_id].plane = vop2_zpos_splice[i].plane;
   }
   vop2_setup_layer_mixer_for_vp(vp, vop2_zpos_splice_hdr);
 
out:
   kfree(vop2_zpos_splice_hdr);
}
 
static void vop2_crtc_update_vrr(struct drm_crtc *crtc)
{
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct drm_display_mode *adjust_mode = &crtc->state->adjusted_mode;
 
   unsigned int vrefresh;
   unsigned int new_vtotal, vfp, new_vfp;
 
   if (!vp->refresh_rate_change)
       return;
 
   if (!vcstate->min_refresh_rate || !vcstate->max_refresh_rate)
       return;
 
   if (vcstate->request_refresh_rate < vcstate->min_refresh_rate ||
       vcstate->request_refresh_rate > vcstate->max_refresh_rate) {
       DRM_ERROR("invalid rate:%d\n", vcstate->request_refresh_rate);
       return;
   }
 
   vrefresh = drm_mode_vrefresh(adjust_mode);
 
   /* calculate new vfp for new refresh rate */
   new_vtotal = adjust_mode->vtotal * vrefresh / vcstate->request_refresh_rate;
   vfp = adjust_mode->vsync_start -  adjust_mode->vdisplay;
   new_vfp = vfp + new_vtotal - adjust_mode->vtotal;
 
   /* config vop2 vtotal register */
   VOP_MODULE_SET(vop2, vp, dsp_vtotal, new_vtotal);
 
   /* config dsc vtotal register */
   if (vcstate->dsc_enable) {
       struct vop2_dsc *dsc;
 
       dsc = &vop2->dscs[vcstate->dsc_id];
       VOP_MODULE_SET(vop2, dsc, dsc_vtotal, new_vtotal);
 
       if (vcstate->output_flags & ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE) {
           dsc = &vop2->dscs[vcstate->dsc_id ? 0 : 1];
           VOP_MODULE_SET(vop2, dsc, dsc_vtotal, new_vtotal);
       }
   }
 
   /* config all connectors attach to this crtc */
   rockchip_connector_update_vfp_for_vrr(crtc, adjust_mode, new_vfp);
}
 
static void vop2_crtc_atomic_begin(struct drm_crtc *crtc, struct drm_crtc_state *old_crtc_state)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_video_port_data *vp_data = &vop2->data->vp[vp->id];
   struct vop2_video_port *splice_vp = &vop2->vps[vp_data->splice_vp_id];
   struct drm_plane *plane;
   struct vop2_plane_state *vpstate;
   struct vop2_zpos *vop2_zpos;
   struct vop2_zpos *vop2_zpos_splice;
   struct vop2_cluster cluster;
   uint8_t nr_layers = 0;
   uint8_t splice_nr_layers = 0;
   bool hdr10_in = false;
   bool hdr10_at_splice_mode = false;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
 
   vcstate->yuv_overlay = is_yuv_output(vcstate->bus_format);
   vop2_zpos = kmalloc_array(vop2->data->win_size, sizeof(*vop2_zpos), GFP_KERNEL);
   if (!vop2_zpos)
       return;
   if (vcstate->splice_mode) {
       vop2_zpos_splice = kmalloc_array(vop2->data->win_size, sizeof(*vop2_zpos),
                        GFP_KERNEL);
       if (!vop2_zpos_splice)
           goto out;
   }
 
   if (vop2->version == VOP_VERSION_RK3588)
       vop2_crtc_update_vrr(crtc);
 
   /* Process cluster sub windows overlay. */
   drm_atomic_crtc_for_each_plane(plane, crtc) {
       struct vop2_win *win = to_vop2_win(plane);
       struct vop2_win *main_win;
 
       win->two_win_mode = false;
       if (!(win->feature & WIN_FEATURE_CLUSTER_SUB))
           continue;
       if (vcstate->splice_mode)
           DRM_ERROR("vp%d %s not supported two win mode at splice mode\n",
                 vp->id, win->name);
       main_win = vop2_find_win_by_phys_id(vop2, win->phys_id);
       cluster.main = main_win;
       cluster.sub = win;
       cluster.splice_mode = false;
       win->two_win_mode = true;
       main_win->two_win_mode = true;
       vop2_setup_cluster_alpha(vop2, &cluster);
       if (abs(main_win->base.state->zpos - win->base.state->zpos) != 1)
           DRM_ERROR("vp%d Cluster%d win0[zpos:%d] must next to win1[zpos:%d]\n",
                 vp->id, cluster.main->phys_id,
                 main_win->base.state->zpos, win->base.state->zpos);
   }
 
   drm_atomic_crtc_for_each_plane(plane, crtc) {
       struct vop2_win *win = to_vop2_win(plane);
       struct vop2_win *splice_win;
       struct vop2_video_port *old_vp;
       uint8_t old_vp_id;
 
       /*
        * Sub win of a cluster will be handled by pre overlay module automatically
        * win in multi area share the same overlay zorder with it's parent.
        */
       if ((win->feature & WIN_FEATURE_CLUSTER_SUB) || win->parent)
           continue;
       old_vp_id = ffs(win->vp_mask);
       old_vp_id = (old_vp_id == 0) ? 0 : old_vp_id - 1;
       old_vp = &vop2->vps[old_vp_id];
       old_vp->win_mask &= ~BIT(win->phys_id);
       vp->win_mask |=  BIT(win->phys_id);
       win->vp_mask = BIT(vp->id);
       vpstate = to_vop2_plane_state(plane->state);
       vop2_zpos[nr_layers].win_phys_id = win->phys_id;
       vop2_zpos[nr_layers].zpos = vpstate->zpos;
       vop2_zpos[nr_layers].plane = plane;
 
       rockchip_drm_dbg(vop2->dev, VOP_DEBUG_OVERLAY, "%s active zpos:%d for vp%d from vp%d\n",
                win->name, vpstate->zpos, vp->id, old_vp->id);
       /* left and right win may have different number */
       if (vcstate->splice_mode) {
           splice_win = vop2_find_win_by_phys_id(vop2, win->splice_win_id);
           splice_win->splice_mode_right = true;
           splice_win->left_win = win;
           win->splice_win = splice_win;
 
           old_vp_id = ffs(splice_win->vp_mask);
           old_vp_id = (old_vp_id == 0) ? 0 : old_vp_id - 1;
           old_vp = &vop2->vps[old_vp_id];
           old_vp->win_mask &= ~BIT(splice_win->phys_id);
           splice_vp->win_mask |=  BIT(splice_win->phys_id);
           splice_win->vp_mask = BIT(splice_vp->id);
           hdr10_in |= vpstate->eotf == HDMI_EOTF_SMPTE_ST2084 ? true : false;
           vop2_zpos_splice[splice_nr_layers].win_phys_id = splice_win->phys_id;
           vop2_zpos_splice[splice_nr_layers].zpos = vpstate->zpos;
           vop2_zpos_splice[splice_nr_layers].plane = &splice_win->base;
           splice_nr_layers++;
           DRM_DEV_DEBUG(vop2->dev, "%s active zpos:%d for vp%d from vp%d\n",
                splice_win->name, vpstate->zpos, splice_vp->id, old_vp->id);
       }
       nr_layers++;
   }
 
   if (vcstate->splice_mode) {
       if (hdr10_in)
           hdr10_at_splice_mode = true;
 
       splice_vp->hdr10_at_splice_mode = hdr10_at_splice_mode;
   }
   vp->hdr10_at_splice_mode = hdr10_at_splice_mode;
 
   rockchip_drm_dbg(vop2->dev, VOP_DEBUG_OVERLAY, "vp%d: %d windows, active layers %d\n",
            vp->id, hweight32(vp->win_mask), nr_layers);
   if (nr_layers) {
       vp->nr_layers = nr_layers;
 
       sort(vop2_zpos, nr_layers, sizeof(vop2_zpos[0]), vop2_zpos_cmp, NULL);
 
       if (!vp->hdr10_at_splice_mode) {
           if (is_vop3(vop2)) {
               vop3_setup_layer_sel_for_vp(vp, vop2_zpos);
           } else {
               vop2_setup_port_mux(vp);
               vop2_setup_layer_mixer_for_vp(vp, vop2_zpos);
           }
       }
 
       if (is_vop3(vop2)) {
           if (vp_data->feature & VOP_FEATURE_VIVID_HDR)
               vop3_setup_dynamic_hdr(vp, vop2_zpos[0].win_phys_id);
           vop3_setup_alpha(vp, vop2_zpos);
           vop3_setup_pipe_dly(vp, vop2_zpos);
       } else {
           vop2_setup_hdr10(vp, vop2_zpos[0].win_phys_id);
           vop2_setup_alpha(vp, vop2_zpos);
           vop2_setup_dly_for_vp(vp);
           vop2_setup_dly_for_window(vp, vop2_zpos);
       }
 
       if (vcstate->splice_mode) {/* Fixme for VOP3 8K */
           splice_vp->nr_layers = splice_nr_layers;
 
           sort(vop2_zpos_splice, splice_nr_layers, sizeof(vop2_zpos_splice[0]),
                vop2_zpos_cmp, NULL);
 
           vop2_setup_port_mux(splice_vp);
           if (!vp->hdr10_at_splice_mode)
               vop2_setup_layer_mixer_for_vp(splice_vp, vop2_zpos_splice);
           vop2_setup_hdr10(splice_vp, vop2_zpos_splice[0].win_phys_id);
           vop2_setup_alpha(splice_vp, vop2_zpos_splice);
           vop2_setup_dly_for_vp(splice_vp);
           vop2_setup_dly_for_window(splice_vp, vop2_zpos_splice);
 
           if (vop2->version == VOP_VERSION_RK3588 &&
               vp->hdr10_at_splice_mode)
               rk3588_vop2_setup_hdr10_splice_layer_mixer(crtc, vop2_zpos, vop2_zpos_splice);
       }
   } else {
       if (!is_vop3(vop2)) {
           vop2_calc_bg_ovl_and_port_mux(vp);
           vop2_setup_dly_for_vp(vp);
           if (vcstate->splice_mode)
               vop2_setup_dly_for_vp(splice_vp);
       } else {
           vop3_setup_pipe_dly(vp, NULL);
       }
   }
 
   /* The pre alpha overlay of Cluster still need process in one win mode. */
   drm_atomic_crtc_for_each_plane(plane, crtc) {
       struct vop2_win *win = to_vop2_win(plane);
       struct vop2_win *splice_win;
 
       if (!(win->feature & WIN_FEATURE_CLUSTER_MAIN))
           continue;
       if (win->two_win_mode)
           continue;
       cluster.main = win;
       cluster.sub = NULL;
       cluster.splice_mode = false;
       vop2_setup_cluster_alpha(vop2, &cluster);
       if (vcstate->splice_mode) {
           splice_win = win->splice_win;
           cluster.main = splice_win;
           cluster.splice_mode = true;
           vop2_setup_cluster_alpha(vop2, &cluster);
       }
   }
 
   if (vcstate->splice_mode)
       kfree(vop2_zpos_splice);
out:
   kfree(vop2_zpos);
}
 
static void vop2_bcsh_reg_update(struct rockchip_crtc_state *vcstate,
                struct vop2_video_port *vp,
                struct rockchip_bcsh_state *bcsh_state)
{
   struct vop2 *vop2 = vp->vop2;
 
   VOP_MODULE_SET(vop2, vp, bcsh_r2y_en, vcstate->post_r2y_en);
   VOP_MODULE_SET(vop2, vp, bcsh_y2r_en, vcstate->post_y2r_en);
   VOP_MODULE_SET(vop2, vp, bcsh_r2y_csc_mode, vcstate->post_csc_mode);
   VOP_MODULE_SET(vop2, vp, bcsh_y2r_csc_mode, vcstate->post_csc_mode);
   if (!vcstate->bcsh_en) {
       VOP_MODULE_SET(vop2, vp, bcsh_en, vcstate->bcsh_en);
       return;
   }
 
   VOP_MODULE_SET(vop2, vp, bcsh_brightness, bcsh_state->brightness);
   VOP_MODULE_SET(vop2, vp, bcsh_contrast, bcsh_state->contrast);
   VOP_MODULE_SET(vop2, vp, bcsh_sat_con,
              bcsh_state->saturation * bcsh_state->contrast / 0x100);
   VOP_MODULE_SET(vop2, vp, bcsh_sin_hue, bcsh_state->sin_hue);
   VOP_MODULE_SET(vop2, vp, bcsh_cos_hue, bcsh_state->cos_hue);
   VOP_MODULE_SET(vop2, vp, bcsh_out_mode, BCSH_OUT_MODE_NORMAL_VIDEO);
   VOP_MODULE_SET(vop2, vp, bcsh_en, vcstate->bcsh_en);
}
 
static void vop2_tv_config_update(struct drm_crtc *crtc,
                 struct drm_crtc_state *old_crtc_state)
{
   struct rockchip_crtc_state *vcstate =
           to_rockchip_crtc_state(crtc->state);
   struct rockchip_crtc_state *old_vcstate =
           to_rockchip_crtc_state(old_crtc_state);
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   int brightness, contrast, saturation, hue, sin_hue, cos_hue;
   struct rockchip_bcsh_state bcsh_state;
 
   if (!vcstate->tv_state)
       return;
 
   /* post BCSH CSC */
   vcstate->post_r2y_en = 0;
   vcstate->post_y2r_en = 0;
   vcstate->bcsh_en = 0;
   if (vcstate->tv_state->brightness != 50 ||
       vcstate->tv_state->contrast != 50 ||
       vcstate->tv_state->saturation != 50 || vcstate->tv_state->hue != 50)
       vcstate->bcsh_en = 1;
   /*
    * The BCSH only need to config once except one of the following
    * condition changed:
    *   1. tv_state: include brightness,contrast,saturation and hue;
    *   2. yuv_overlay: it is related to BCSH r2y module;
    *   4. bcsh_en: control the BCSH module enable or disable state;
    *   5. bus_format: it is related to BCSH y2r module;
    */
   if (!memcmp(vcstate->tv_state, &vp->active_tv_state, sizeof(*vcstate->tv_state)) &&
       vcstate->yuv_overlay == old_vcstate->yuv_overlay &&
       vcstate->bcsh_en == old_vcstate->bcsh_en &&
       vcstate->bus_format == old_vcstate->bus_format)
       return;
 
   memcpy(&vp->active_tv_state, vcstate->tv_state, sizeof(*vcstate->tv_state));
   if (vcstate->bcsh_en) {
       if (!vcstate->yuv_overlay)
           vcstate->post_r2y_en = 1;
       if (!is_yuv_output(vcstate->bus_format))
           vcstate->post_y2r_en = 1;
   } else {
       if (!vcstate->yuv_overlay && is_yuv_output(vcstate->bus_format))
           vcstate->post_r2y_en = 1;
       if (vcstate->yuv_overlay && !is_yuv_output(vcstate->bus_format))
           vcstate->post_y2r_en = 1;
   }
 
   vcstate->post_csc_mode = vop2_convert_csc_mode(vcstate->color_space, CSC_10BIT_DEPTH);
 
   if (vp_data->feature & VOP_FEATURE_OUTPUT_10BIT)
       brightness = interpolate(0, -128, 100, 127,
                    vcstate->tv_state->brightness);
   else
       brightness = interpolate(0, -32, 100, 31,
                    vcstate->tv_state->brightness);
   contrast = interpolate(0, 0, 100, 511, vcstate->tv_state->contrast);
   saturation = interpolate(0, 0, 100, 511, vcstate->tv_state->saturation);
   hue = interpolate(0, -30, 100, 30, vcstate->tv_state->hue);
 
   /*
    *  a:[-30~0]:
    *    sin_hue = 0x100 - sin(a)*256;
    *    cos_hue = cos(a)*256;
    *  a:[0~30]
    *    sin_hue = sin(a)*256;
    *    cos_hue = cos(a)*256;
    */
   sin_hue = fixp_sin32(hue) >> 23;
   cos_hue = fixp_cos32(hue) >> 23;
 
   bcsh_state.brightness = brightness;
   bcsh_state.contrast = contrast;
   bcsh_state.saturation = saturation;
   bcsh_state.sin_hue = sin_hue;
   bcsh_state.cos_hue = cos_hue;
 
   vop2_bcsh_reg_update(vcstate, vp, &bcsh_state);
   if (vcstate->splice_mode) {
       const struct vop2_video_port_data *vp_data = &vop2->data->vp[vp->id];
       struct vop2_video_port *splice_vp = &vop2->vps[vp_data->splice_vp_id];
 
       vop2_bcsh_reg_update(vcstate, splice_vp, &bcsh_state);
   }
}
 
static void vop3_post_csc_config(struct drm_crtc *crtc, struct post_acm *acm, struct post_csc *csc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2 *vop2 = vp->vop2;
   struct post_csc_coef csc_coef;
   bool acm_enable;
   bool is_input_yuv = false;
   bool is_output_yuv = false;
   bool post_r2y_en = false;
   bool post_csc_en = false;
   int range_type;
 
   if (!acm)
       acm_enable = false;
   else
       acm_enable = acm->acm_enable;
 
   if (acm_enable) {
       if (!vcstate->yuv_overlay)
           post_r2y_en = true;
 
       /* do y2r in csc module */
       if (!is_yuv_output(vcstate->bus_format))
           post_csc_en = true;
   } else {
       if (!vcstate->yuv_overlay && is_yuv_output(vcstate->bus_format))
           post_r2y_en = true;
 
       /* do y2r in csc module */
       if (vcstate->yuv_overlay && !is_yuv_output(vcstate->bus_format))
           post_csc_en = true;
   }
 
   if (csc && csc->csc_enable)
       post_csc_en = true;
 
   if (vcstate->yuv_overlay || post_r2y_en)
       is_input_yuv = true;
 
   if (is_yuv_output(vcstate->bus_format))
       is_output_yuv = true;
 
   vcstate->post_csc_mode = vop2_convert_csc_mode(vcstate->color_space, CSC_13BIT_DEPTH);
 
   if (post_csc_en) {
       rockchip_calc_post_csc(csc, &csc_coef, vcstate->post_csc_mode, is_input_yuv,
                      is_output_yuv);
 
       VOP_MODULE_SET(vop2, vp, csc_coe00, csc_coef.csc_coef00);
       VOP_MODULE_SET(vop2, vp, csc_coe01, csc_coef.csc_coef01);
       VOP_MODULE_SET(vop2, vp, csc_coe02, csc_coef.csc_coef02);
       VOP_MODULE_SET(vop2, vp, csc_coe10, csc_coef.csc_coef10);
       VOP_MODULE_SET(vop2, vp, csc_coe11, csc_coef.csc_coef11);
       VOP_MODULE_SET(vop2, vp, csc_coe12, csc_coef.csc_coef12);
       VOP_MODULE_SET(vop2, vp, csc_coe20, csc_coef.csc_coef20);
       VOP_MODULE_SET(vop2, vp, csc_coe21, csc_coef.csc_coef21);
       VOP_MODULE_SET(vop2, vp, csc_coe22, csc_coef.csc_coef22);
       VOP_MODULE_SET(vop2, vp, csc_offset0, csc_coef.csc_dc0);
       VOP_MODULE_SET(vop2, vp, csc_offset1, csc_coef.csc_dc1);
       VOP_MODULE_SET(vop2, vp, csc_offset2, csc_coef.csc_dc2);
 
       range_type = csc_coef.range_type ? 0 : 1;
       range_type <<= is_input_yuv ? 0 : 1;
       VOP_MODULE_SET(vop2, vp, csc_mode, range_type);
   }
 
   VOP_MODULE_SET(vop2, vp, acm_r2y_en, post_r2y_en ? 1 : 0);
   VOP_MODULE_SET(vop2, vp, csc_en, post_csc_en ? 1 : 0);
   VOP_MODULE_SET(vop2, vp, acm_r2y_mode, vcstate->post_csc_mode);
}
 
static void vop3_post_acm_config(struct drm_crtc *crtc, struct post_acm *acm)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
   s16 *lut_y;
   s16 *lut_h;
   s16 *lut_s;
   u32 value;
   int i;
 
   writel(0, vop2->acm_regs + RK3528_ACM_CTRL);
   VOP_MODULE_SET(vop2, vp, acm_bypass_en, 0);
 
   if (!acm || !acm->acm_enable)
       return;
 
   /*
    * If acm update parameters, it need disable acm in the first frame,
    * then update parameters and enable acm in second frame.
    */
   vop2_cfg_done(crtc);
   readx_poll_timeout(readl, vop2->acm_regs + RK3528_ACM_CTRL, value, !value, 200, 50000);
 
   value = RK3528_ACM_ENABLE + ((adjusted_mode->hdisplay & 0xfff) << 8) +
       ((adjusted_mode->vdisplay & 0xfff) << 20);
   writel(value, vop2->acm_regs + RK3528_ACM_CTRL);
 
 
   writel(1, vop2->acm_regs + RK3528_ACM_FETCH_START);
 
   value = (acm->y_gain & 0x3ff) + ((acm->h_gain << 10) & 0xffc00) +
       ((acm->s_gain << 20) & 0x3ff00000);
   writel(value, vop2->acm_regs + RK3528_ACM_DELTA_RANGE);
 
   lut_y = &acm->gain_lut_hy[0];
   lut_h = &acm->gain_lut_hy[ACM_GAIN_LUT_HY_LENGTH];
   lut_s = &acm->gain_lut_hy[ACM_GAIN_LUT_HY_LENGTH * 2];
   for (i = 0; i < ACM_GAIN_LUT_HY_LENGTH; i++) {
       value = (lut_y[i] & 0xff) + ((lut_h[i] << 8) & 0xff00) +
           ((lut_s[i] << 16) & 0xff0000);
       writel(value, vop2->acm_regs + RK3528_ACM_YHS_DEL_HY_SEG0 + (i << 2));
   }
 
   lut_y = &acm->gain_lut_hs[0];
   lut_h = &acm->gain_lut_hs[ACM_GAIN_LUT_HS_LENGTH];
   lut_s = &acm->gain_lut_hs[ACM_GAIN_LUT_HS_LENGTH * 2];
   for (i = 0; i < ACM_GAIN_LUT_HS_LENGTH; i++) {
       value = (lut_y[i] & 0xff) + ((lut_h[i] << 8) & 0xff00) +
           ((lut_s[i] << 16) & 0xff0000);
       writel(value, vop2->acm_regs + RK3528_ACM_YHS_DEL_HS_SEG0 + (i << 2));
   }
 
   lut_y = &acm->delta_lut_h[0];
   lut_h = &acm->delta_lut_h[ACM_DELTA_LUT_H_LENGTH];
   lut_s = &acm->delta_lut_h[ACM_DELTA_LUT_H_LENGTH * 2];
   for (i = 0; i < ACM_DELTA_LUT_H_LENGTH; i++) {
       value = (lut_y[i] & 0x3ff) + ((lut_h[i] << 12) & 0xff000) +
           ((lut_s[i] << 20) & 0x3ff00000);
       writel(value, vop2->acm_regs + RK3528_ACM_YHS_DEL_HGAIN_SEG0 + (i << 2));
   }
 
   writel(1, vop2->acm_regs + RK3528_ACM_FETCH_DONE);
}
 
static void vop3_post_config(struct drm_crtc *crtc)
{
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct post_acm *acm;
   struct post_csc *csc;
 
   csc = vcstate->post_csc_data ? (struct post_csc *)vcstate->post_csc_data->data : NULL;
   if (csc && memcmp(&vp->csc_info, csc, sizeof(struct post_csc)))
       memcpy(&vp->csc_info, csc, sizeof(struct post_csc));
   vop3_post_csc_config(crtc, &vp->acm_info, &vp->csc_info);
 
   acm = vcstate->acm_lut_data ? (struct post_acm *)vcstate->acm_lut_data->data : NULL;
 
   if (acm && memcmp(&vp->acm_info, acm, sizeof(struct post_acm))) {
       memcpy(&vp->acm_info, acm, sizeof(struct post_acm));
       vop3_post_acm_config(crtc, &vp->acm_info);
   } else if (crtc->state->active_changed) {
       vop3_post_acm_config(crtc, &vp->acm_info);
   }
}
 
static void vop2_cfg_update(struct drm_crtc *crtc,
               struct drm_crtc_state *old_crtc_state)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct vop2 *vop2 = vp->vop2;
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   struct vop2_video_port *splice_vp = &vop2->vps[vp_data->splice_vp_id];
   uint32_t val;
   uint32_t r, g, b;
   uint8_t out_mode;
 
   spin_lock(&vop2->reg_lock);
 
   if ((vcstate->output_mode == ROCKCHIP_OUT_MODE_AAAA &&
        !(vp_data->feature & VOP_FEATURE_OUTPUT_10BIT)) ||
       vcstate->output_if & VOP_OUTPUT_IF_BT656)
       out_mode = ROCKCHIP_OUT_MODE_P888;
   else
       out_mode = vcstate->output_mode;
   VOP_MODULE_SET(vop2, vp, out_mode, out_mode);
 
   vop2_post_color_swap(crtc);
 
   vop2_dither_setup(vcstate, crtc);
   if (vcstate->splice_mode)
       vop2_dither_setup(vcstate, &splice_vp->rockchip_crtc.crtc);
 
   VOP_MODULE_SET(vop2, vp, overlay_mode, vcstate->yuv_overlay);
 
   /*
    * userspace specified background.
    */
   if (vcstate->background) {
       r = (vcstate->background & 0xff0000) >> 16;
       g = (vcstate->background & 0xff00) >> 8;
       b = (vcstate->background & 0xff);
       r <<= 2;
       g <<= 2;
       b <<= 2;
       val = (r << 20) | (g << 10) | b;
   } else {
       if (vcstate->yuv_overlay)
           val = 0x20010200;
       else
           val = 0;
   }
 
   VOP_MODULE_SET(vop2, vp, dsp_background, val);
   if (vcstate->splice_mode) {
       VOP_MODULE_SET(vop2, splice_vp, overlay_mode, vcstate->yuv_overlay);
       VOP_MODULE_SET(vop2, splice_vp, dsp_background, val);
   }
 
   vop2_tv_config_update(crtc, old_crtc_state);
 
   if (vp_data->feature & VOP_FEATURE_OVERSCAN)
       vop2_post_config(crtc);
 
   spin_unlock(&vop2->reg_lock);
 
   if (vp_data->feature & (VOP_FEATURE_POST_ACM | VOP_FEATURE_POST_CSC))
       vop3_post_config(crtc);
}
 
static void vop2_sleep_scan_line_time(struct vop2_video_port *vp, int scan_line)
{
   struct vop2 *vop2 = vp->vop2;
   struct drm_display_mode *mode = &vp->rockchip_crtc.crtc.state->adjusted_mode;
 
   if (scan_line <= 0)
       return;
 
   if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS) &&
       (!IS_ENABLED(CONFIG_NO_GKI) || (hrtimer_resolution != LOW_RES_NSEC))) {
       u16 htotal = VOP_MODULE_GET(vop2, vp, htotal_pw) >> 16;
       u32 linedur_ns = div_u64((u64) htotal * 1000000, mode->crtc_clock);
       u64 sleep_time = linedur_ns * scan_line;
 
       sleep_time = div_u64((sleep_time + 1000), 1000);
       if (sleep_time > 200)
           usleep_range(sleep_time, sleep_time);
   }
}
 
/*
 * return scan timing from FS to the assigned wait line
 */
static void vop2_wait_for_scan_timing_max_to_assigned_line(struct vop2_video_port *vp,
                              u32 current_line,
                              u32 wait_line)
 
{
   struct vop2 *vop2 = vp->vop2;
   u32 vcnt;
   int ret;
   u16 vtotal = VOP_MODULE_GET(vop2, vp, dsp_vtotal);
   int delta_line = vtotal - current_line;
 
   vop2_sleep_scan_line_time(vp, delta_line);
   if (vop2_read_vcnt(vp) < wait_line)
       return;
 
   ret = readx_poll_timeout_atomic(vop2_read_vcnt, vp, vcnt, vcnt < wait_line, 0, 50 * 1000);
   if (ret)
       DRM_DEV_ERROR(vop2->dev, "wait scan timing from FS to the assigned wait line: %d, vcnt:%d, ret:%d\n",
                 wait_line, vcnt, ret);
}
 
/*
 * return scan timing from the assigned wait line
 */
static void vop2_wait_for_scan_timing_from_the_assigned_line(struct vop2_video_port *vp,
                                u32 current_line,
                                u32 wait_line)
{
   struct vop2 *vop2 = vp->vop2;
   u32 vcnt;
   int ret;
   int delta_line = wait_line - current_line;
 
   vop2_sleep_scan_line_time(vp, delta_line);
   if (vop2_read_vcnt(vp) > wait_line)
       return;
 
   ret = readx_poll_timeout_atomic(vop2_read_vcnt, vp, vcnt, vcnt > wait_line, 0, 50 * 1000);
   if (ret)
       DRM_DEV_ERROR(vop2->dev, "wait scan timing from the assigned wait line: %d, vcnt:%d, ret:%d\n",
                 wait_line, vcnt, ret);
}
 
static void vop2_crtc_atomic_flush(struct drm_crtc *crtc, struct drm_crtc_state *old_cstate)
{
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
   struct drm_atomic_state *old_state = old_cstate->state;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct drm_plane_state *old_pstate;
   struct drm_plane *plane;
   unsigned long flags;
   int i, ret;
   struct vop2_wb *wb = &vop2->wb;
   struct drm_writeback_connector *wb_conn = &wb->conn;
   struct drm_connector_state *conn_state = wb_conn->base.state;
 
   if (conn_state && conn_state->writeback_job && conn_state->writeback_job->fb) {
       u16 vtotal = VOP_MODULE_GET(vop2, vp, dsp_vtotal);
       u32 current_line = vop2_read_vcnt(vp);
 
       if (current_line > vtotal * 7 >> 3)
           vop2_wait_for_scan_timing_max_to_assigned_line(vp, current_line, vtotal * 7 >> 3);
 
       current_line = vop2_read_vcnt(vp);
       if (current_line < vtotal >> 3)
           vop2_wait_for_scan_timing_from_the_assigned_line(vp, current_line, vtotal >> 3);
   }
 
   vop2_cfg_update(crtc, old_cstate);
 
   if (!vop2->is_iommu_enabled && vop2->is_iommu_needed) {
       if (vcstate->mode_update)
           VOP_CTRL_SET(vop2, dma_stop, 1);
 
       ret = rockchip_drm_dma_attach_device(vop2->drm_dev, vop2->dev);
       if (ret) {
           vop2->is_iommu_enabled = false;
           vop2_disable_all_planes_for_crtc(crtc);
           DRM_DEV_ERROR(vop2->dev, "vp%d failed to attach dma mapping, %d\n", vp->id, ret);
       } else {
           vop2->is_iommu_enabled = true;
           VOP_CTRL_SET(vop2, dma_stop, 0);
       }
   }
 
 
   if (crtc->state->color_mgmt_changed || crtc->state->active_changed) {
       if (crtc->state->gamma_lut || vp->gamma_lut) {
           if (crtc->state->gamma_lut)
               vp->gamma_lut = crtc->state->gamma_lut->data;
           vop2_crtc_atomic_gamma_set(crtc, crtc->state);
       }
       if (vcstate->cubic_lut_data || vp->cubic_lut) {
           if (vcstate->cubic_lut_data)
               vp->cubic_lut = vcstate->cubic_lut_data->data;
           vop2_crtc_atomic_cubic_lut_set(crtc, crtc->state);
       }
   } else {
       VOP_MODULE_SET(vop2, vp, cubic_lut_update_en, 0);
   }
 
   if (vcstate->line_flag)
       vop2_crtc_enable_line_flag_event(crtc, vcstate->line_flag);
   else
       vop2_crtc_disable_line_flag_event(crtc);
 
   spin_lock_irqsave(&vop2->irq_lock, flags);
   vop2_wb_commit(crtc);
   vop2_cfg_done(crtc);
 
   if (vp->mcu_timing.mcu_pix_total)
       VOP_MODULE_SET(vop2, vp, mcu_hold_mode, 0);
 
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
 
   /*
    * There is a (rather unlikely) possibility that a vblank interrupt
    * fired before we set the cfg_done bit. To avoid spuriously
    * signalling flip completion we need to wait for it to finish.
    */
   vop2_wait_for_irq_handler(crtc);
 
   /**
    * move here is to make sure current fs call function is complete,
    * so when layer_sel_update is true, we can skip current vblank correctly.
    */
   vp->layer_sel_update = false;
 
   spin_lock_irq(&crtc->dev->event_lock);
   if (crtc->state->event) {
       WARN_ON(drm_crtc_vblank_get(crtc) != 0);
       WARN_ON(vp->event);
 
       vp->event = crtc->state->event;
       crtc->state->event = NULL;
   }
   spin_unlock_irq(&crtc->dev->event_lock);
 
   for_each_old_plane_in_state(old_state, plane, old_pstate, i) {
       if (!old_pstate->fb)
           continue;
 
       if (old_pstate->fb == plane->state->fb)
           continue;
       if (!vop2->skip_ref_fb)
           drm_framebuffer_get(old_pstate->fb);
       WARN_ON(drm_crtc_vblank_get(crtc) != 0);
       drm_flip_work_queue(&vp->fb_unref_work, old_pstate->fb);
       set_bit(VOP_PENDING_FB_UNREF, &vp->pending);
   }
}
 
static const struct drm_crtc_helper_funcs vop2_crtc_helper_funcs = {
   .mode_valid = vop2_crtc_mode_valid,
   .mode_fixup = vop2_crtc_mode_fixup,
   .atomic_check = vop2_crtc_atomic_check,
   .atomic_begin = vop2_crtc_atomic_begin,
   .atomic_flush = vop2_crtc_atomic_flush,
   .atomic_enable = vop2_crtc_atomic_enable,
   .atomic_disable = vop2_crtc_atomic_disable,
};
 
static void vop2_crtc_destroy(struct drm_crtc *crtc)
{
   drm_crtc_cleanup(crtc);
}
 
static void vop2_crtc_reset(struct drm_crtc *crtc)
{
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state);
 
   if (crtc->state) {
       __drm_atomic_helper_crtc_destroy_state(crtc->state);
       kfree(vcstate);
   }
 
   vcstate = kzalloc(sizeof(*vcstate), GFP_KERNEL);
   if (!vcstate)
       return;
   crtc->state = &vcstate->base;
   crtc->state->crtc = crtc;
 
   vcstate->left_margin = 100;
   vcstate->right_margin = 100;
   vcstate->top_margin = 100;
   vcstate->bottom_margin = 100;
   vcstate->background = 0;
}
 
static struct drm_crtc_state *vop2_crtc_duplicate_state(struct drm_crtc *crtc)
{
   struct rockchip_crtc_state *vcstate, *old_vcstate;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
 
   if (WARN_ON(!crtc->state))
       return NULL;
 
   old_vcstate = to_rockchip_crtc_state(crtc->state);
   vcstate = kmemdup(old_vcstate, sizeof(*old_vcstate), GFP_KERNEL);
   if (!vcstate)
       return NULL;
 
   vcstate->vp_id = vp->id;
   if (vcstate->hdr_ext_data)
       drm_property_blob_get(vcstate->hdr_ext_data);
   if (vcstate->acm_lut_data)
       drm_property_blob_get(vcstate->acm_lut_data);
   if (vcstate->post_csc_data)
       drm_property_blob_get(vcstate->post_csc_data);
   if (vcstate->cubic_lut_data)
       drm_property_blob_get(vcstate->cubic_lut_data);
 
   __drm_atomic_helper_crtc_duplicate_state(crtc, &vcstate->base);
   return &vcstate->base;
}
 
static void vop2_crtc_destroy_state(struct drm_crtc *crtc,
                   struct drm_crtc_state *state)
{
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(state);
 
   __drm_atomic_helper_crtc_destroy_state(&vcstate->base);
   drm_property_blob_put(vcstate->hdr_ext_data);
   drm_property_blob_put(vcstate->acm_lut_data);
   drm_property_blob_put(vcstate->post_csc_data);
   drm_property_blob_put(vcstate->cubic_lut_data);
   kfree(vcstate);
}
 
#ifdef CONFIG_DRM_ANALOGIX_DP
static struct drm_connector *vop2_get_edp_connector(struct vop2 *vop2)
{
   struct drm_connector *connector;
   struct drm_connector_list_iter conn_iter;
 
   drm_connector_list_iter_begin(vop2->drm_dev, &conn_iter);
   drm_for_each_connector_iter(connector, &conn_iter) {
       if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
           drm_connector_list_iter_end(&conn_iter);
           return connector;
       }
   }
   drm_connector_list_iter_end(&conn_iter);
 
   return NULL;
}
 
static int vop2_crtc_set_crc_source(struct drm_crtc *crtc,
                   const char *source_name)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
   struct drm_connector *connector;
   int ret;
 
   connector = vop2_get_edp_connector(vop2);
   if (!connector)
       return -EINVAL;
 
   if (source_name && strcmp(source_name, "auto") == 0)
       ret = analogix_dp_start_crc(connector);
   else if (!source_name)
       ret = analogix_dp_stop_crc(connector);
   else
       ret = -EINVAL;
 
   return ret;
}
 
static int vop2_crtc_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
                      size_t *values_cnt)
{
   if (source_name && strcmp(source_name, "auto") != 0)
       return -EINVAL;
 
   *values_cnt = 3;
   return 0;
}
 
#else
static int vop2_crtc_set_crc_source(struct drm_crtc *crtc,
                   const char *source_name)
{
   return -ENODEV;
}
 
static int
vop2_crtc_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
               size_t *values_cnt)
{
   return -ENODEV;
}
#endif
 
static int vop2_crtc_atomic_get_property(struct drm_crtc *crtc,
                    const struct drm_crtc_state *state,
                    struct drm_property *property,
                    uint64_t *val)
{
   struct drm_device *drm_dev = crtc->dev;
   struct rockchip_drm_private *private = drm_dev->dev_private;
   struct drm_mode_config *mode_config = &drm_dev->mode_config;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(state);
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct vop2 *vop2 = vp->vop2;
 
   if (property == mode_config->tv_left_margin_property) {
       *val = vcstate->left_margin;
       return 0;
   }
 
   if (property == mode_config->tv_right_margin_property) {
       *val = vcstate->right_margin;
       return 0;
   }
 
   if (property == mode_config->tv_top_margin_property) {
       *val = vcstate->top_margin;
       return 0;
   }
 
   if (property == mode_config->tv_bottom_margin_property) {
       *val = vcstate->bottom_margin;
       return 0;
   }
 
   if (property == private->aclk_prop) {
       /* KHZ, keep align with mode->clock */
       *val = clk_get_rate(vop2->aclk) / 1000;
       return 0;
   }
 
   if (property == private->bg_prop) {
       *val = vcstate->background;
       return 0;
   }
 
   if (property == private->line_flag_prop) {
       *val = vcstate->line_flag;
       return 0;
   }
 
   if (property == vp->variable_refresh_rate_prop) {
       *val = vcstate->request_refresh_rate;
       return 0;
   }
 
   if (property == vp->max_refresh_rate_prop) {
       *val = vcstate->max_refresh_rate;
       return 0;
   }
 
   if (property == vp->min_refresh_rate_prop) {
       *val = vcstate->min_refresh_rate;
       return 0;
   }
 
   if (property == vp->hdr_ext_data_prop) {
       *val = vcstate->hdr_ext_data ? vcstate->hdr_ext_data->base.id : 0;
       return 0;
   }
 
   if (property == vp->acm_lut_data_prop) {
       *val = vcstate->acm_lut_data ? vcstate->acm_lut_data->base.id : 0;
       return 0;
   }
 
   if (property == vp->post_csc_data_prop) {
       *val = vcstate->post_csc_data ? vcstate->post_csc_data->base.id : 0;
       return 0;
   }
 
   if (property == private->cubic_lut_prop) {
       *val = (vcstate->cubic_lut_data) ? vcstate->cubic_lut_data->base.id : 0;
       return 0;
   }
 
   DRM_ERROR("failed to get vop2 crtc property: %s\n", property->name);
 
   return -EINVAL;
}
 
/* copied from drm_atomic.c */
static int
vop2_atomic_replace_property_blob_from_id(struct drm_device *dev,
                    struct drm_property_blob **blob,
                    uint64_t blob_id,
                    ssize_t expected_size,
                    ssize_t expected_elem_size,
                    bool *replaced)
{
   struct drm_property_blob *new_blob = NULL;
 
   if (blob_id != 0) {
       new_blob = drm_property_lookup_blob(dev, blob_id);
       if (new_blob == NULL)
           return -EINVAL;
 
       if (expected_size > 0 &&
           new_blob->length != expected_size) {
           drm_property_blob_put(new_blob);
           return -EINVAL;
       }
       if (expected_elem_size > 0 &&
           new_blob->length % expected_elem_size != 0) {
           drm_property_blob_put(new_blob);
           return -EINVAL;
       }
   }
 
   *replaced |= drm_property_replace_blob(blob, new_blob);
   drm_property_blob_put(new_blob);
 
   return 0;
}
 
static int vop2_crtc_atomic_set_property(struct drm_crtc *crtc,
                    struct drm_crtc_state *state,
                    struct drm_property *property,
                    uint64_t val)
{
   struct drm_device *drm_dev = crtc->dev;
   struct rockchip_drm_private *private = drm_dev->dev_private;
   struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(state);
   struct drm_mode_config *mode_config = &drm_dev->mode_config;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   bool replaced = false;
   int ret;
 
   if (property == mode_config->tv_left_margin_property) {
       vcstate->left_margin = val;
       return 0;
   }
 
   if (property == mode_config->tv_right_margin_property) {
       vcstate->right_margin = val;
       return 0;
   }
 
   if (property == mode_config->tv_top_margin_property) {
       vcstate->top_margin = val;
       return 0;
   }
 
   if (property == mode_config->tv_bottom_margin_property) {
       vcstate->bottom_margin = val;
       return 0;
   }
 
 
   if (property == private->bg_prop) {
       vcstate->background = val;
       return 0;
   }
 
   if (property == private->line_flag_prop) {
       vcstate->line_flag = val;
       return 0;
   }
 
   if (property == vp->variable_refresh_rate_prop) {
       vcstate->request_refresh_rate = val;
       return 0;
   }
 
   if (property == vp->max_refresh_rate_prop) {
       vcstate->max_refresh_rate = val;
       return 0;
   }
 
   if (property == vp->min_refresh_rate_prop) {
       vcstate->min_refresh_rate = val;
       return 0;
   }
 
   if (property == vp->hdr_ext_data_prop) {
       ret = vop2_atomic_replace_property_blob_from_id(drm_dev,
                               &vcstate->hdr_ext_data,
                               val,
                               -1, -1,
                               &replaced);
       return ret;
   }
 
   if (property == vp->acm_lut_data_prop) {
       ret = vop2_atomic_replace_property_blob_from_id(drm_dev,
                               &vcstate->acm_lut_data,
                               val,
                               sizeof(struct post_acm), -1,
                               &replaced);
       return ret;
   }
 
   if (property == vp->post_csc_data_prop) {
       ret = vop2_atomic_replace_property_blob_from_id(drm_dev,
                               &vcstate->post_csc_data,
                               val,
                               sizeof(struct post_csc), -1,
                               &replaced);
       return ret;
   }
 
   if (property == private->cubic_lut_prop) {
       ret = vop2_atomic_replace_property_blob_from_id(drm_dev,
                               &vcstate->cubic_lut_data,
                               val,
                               -1, sizeof(struct drm_color_lut),
                               &replaced);
       state->color_mgmt_changed |= replaced;
       return ret;
   }
 
   DRM_ERROR("failed to set vop2 crtc property %s\n", property->name);
 
   return -EINVAL;
}
 
static const struct drm_crtc_funcs vop2_crtc_funcs = {
   .gamma_set = vop2_crtc_legacy_gamma_set,
   .set_config = drm_atomic_helper_set_config,
   .page_flip = drm_atomic_helper_page_flip,
   .destroy = vop2_crtc_destroy,
   .reset = vop2_crtc_reset,
   .atomic_get_property = vop2_crtc_atomic_get_property,
   .atomic_set_property = vop2_crtc_atomic_set_property,
   .atomic_duplicate_state = vop2_crtc_duplicate_state,
   .atomic_destroy_state = vop2_crtc_destroy_state,
   .enable_vblank = vop2_crtc_enable_vblank,
   .disable_vblank = vop2_crtc_disable_vblank,
   .set_crc_source = vop2_crtc_set_crc_source,
   .verify_crc_source = vop2_crtc_verify_crc_source,
};
 
static void vop2_fb_unref_worker(struct drm_flip_work *work, void *val)
{
   struct vop2_video_port *vp = container_of(work, struct vop2_video_port, fb_unref_work);
   struct drm_framebuffer *fb = val;
 
   drm_crtc_vblank_put(&vp->rockchip_crtc.crtc);
   if (!vp->vop2->skip_ref_fb)
       drm_framebuffer_put(fb);
}
 
static void vop2_handle_vblank(struct vop2 *vop2, struct drm_crtc *crtc)
{
   struct drm_device *drm = vop2->drm_dev;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   unsigned long flags;
 
   spin_lock_irqsave(&drm->event_lock, flags);
   if (vp->event) {
       drm_crtc_send_vblank_event(crtc, vp->event);
       drm_crtc_vblank_put(crtc);
       vp->event = NULL;
   }
   spin_unlock_irqrestore(&drm->event_lock, flags);
 
   if (test_and_clear_bit(VOP_PENDING_FB_UNREF, &vp->pending))
       drm_flip_work_commit(&vp->fb_unref_work, system_unbound_wq);
}
 
static void vop2_handle_vcnt(struct drm_crtc *crtc)
{
   struct drm_device *dev = crtc->dev;
   struct rockchip_drm_private *priv = dev->dev_private;
   struct rockchip_drm_vcnt *vcnt;
   struct drm_pending_vblank_event *e;
   struct timespec64 now;
   unsigned long irqflags;
   int pipe;
 
   now = ktime_to_timespec64(ktime_get());
 
   spin_lock_irqsave(&dev->event_lock, irqflags);
   pipe = drm_crtc_index(crtc);
   vcnt = &priv->vcnt[pipe];
   vcnt->sequence++;
   if (vcnt->event) {
       e = vcnt->event;
       e->event.vbl.tv_sec = now.tv_sec;
       e->event.vbl.tv_usec = now.tv_nsec / NSEC_PER_USEC;
       e->event.vbl.sequence = vcnt->sequence;
       drm_send_event_locked(dev, &e->base);
       vcnt->event = NULL;
   }
   spin_unlock_irqrestore(&dev->event_lock, irqflags);
}
 
static u32 vop2_read_and_clear_active_vp_irqs(struct vop2 *vop2, int vp_id)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data;
   const struct vop_intr *intr;
   int val;
 
   vp_data = &vop2_data->vp[vp_id];
   intr = vp_data->intr;
   val = VOP_INTR_GET_TYPE(vop2, intr, status, INTR_MASK);
   if (val)
       VOP_INTR_SET_TYPE(vop2, intr, clear, val, 1);
   return val;
}
 
static void vop2_wb_disable(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   struct vop2_wb *wb = &vop2->wb;
 
   VOP_MODULE_SET(vop2, wb, enable, 0);
   VOP_CTRL_SET(vop2, wb_dma_finish_and_en, 0);
   vop2_wb_cfg_done(vp);
}
 
static void vop2_wb_handler(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   struct vop2_wb *wb = &vop2->wb;
   struct vop2_wb_job *job;
   unsigned long flags;
   uint8_t wb_en;
   uint8_t wb_vp_id;
   uint8_t i;
 
   wb_en = vop2_readl(vop2, RK3568_WB_CTRL) & 0x01;
   wb_vp_id = (vop2_readl(vop2, RK3568_LUT_PORT_SEL) >> 8) & 0x3;
   if (wb_vp_id != vp->id)
       return;
   /*
    * The write back should work in one shot mode,
    * stop when write back complete in next vsync.
    */
   if (wb_en)
       vop2_wb_disable(vp);
 
   spin_lock_irqsave(&wb->job_lock, flags);
   for (i = 0; i < VOP2_WB_JOB_MAX; i++) {
       job = &wb->jobs[i];
       if (job->pending) {
           job->fs_vsync_cnt++;
 
           if (job->fs_vsync_cnt == 2) {
               job->pending = false;
               job->fs_vsync_cnt = 0;
               drm_writeback_signal_completion(&vop2->wb.conn, 0);
           }
       }
   }
   spin_unlock_irqrestore(&wb->job_lock, flags);
}
 
static void vop2_dsc_isr(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   struct vop2_dsc *dsc;
   const struct dsc_error_info *dsc_error_ecw = vop2_data->dsc_error_ecw;
   const struct dsc_error_info *dsc_error_buffer_flow = vop2_data->dsc_error_buffer_flow;
   u32 dsc_error_status = 0, dsc_ecw = 0;
   int i = 0, j = 0;
 
   for (i = 0; i < vop2_data->nr_dscs; i++) {
       dsc = &vop2->dscs[i];
 
       if (!dsc->enabled)
           continue;
 
       dsc_error_status = VOP_MODULE_GET(vop2, dsc, dsc_error_status);
       if (!dsc_error_status)
           continue;
       dsc_ecw = VOP_MODULE_GET(vop2, dsc, dsc_ecw);
 
       for (j = 0; j < vop2_data->nr_dsc_ecw; j++) {
           if (dsc_ecw == dsc_error_ecw[j].dsc_error_val) {
               DRM_ERROR("dsc%d %s\n", dsc->id, dsc_error_ecw[j].dsc_error_info);
               break;
           }
       }
 
       if (dsc_ecw == 0x0120ffff) {
           u32 offset = dsc->regs->dsc_status.offset;
 
           for (j = 0; j < vop2_data->nr_dsc_buffer_flow; j++)
               DRM_ERROR("dsc%d %s:0x%x\n", dsc->id, dsc_error_buffer_flow[j].dsc_error_info,
                     vop2_readl(vop2, offset + (j << 2)));
       }
   }
}
 
static irqreturn_t vop2_isr(int irq, void *data)
{
   struct vop2 *vop2 = data;
   struct drm_crtc *crtc;
   struct vop2_video_port *vp;
   const struct vop2_data *vop2_data = vop2->data;
   size_t vp_max = min_t(size_t, vop2_data->nr_vps, ROCKCHIP_MAX_CRTC);
   size_t axi_max = min_t(size_t, vop2_data->nr_axi_intr, VOP2_SYS_AXI_BUS_NUM);
   uint32_t vp_irqs[ROCKCHIP_MAX_CRTC];
   uint32_t axi_irqs[VOP2_SYS_AXI_BUS_NUM];
   uint32_t active_irqs;
   uint32_t wb_irqs;
   unsigned long flags;
   int ret = IRQ_NONE;
   int i;
 
#define ERROR_HANDLER(x) \
   do { \
       if (active_irqs & x##_INTR) {\
           if (x##_INTR == POST_BUF_EMPTY_INTR) \
               DRM_DEV_ERROR_RATELIMITED(vop2->dev, #x " irq err at vp%d\n", vp->id); \
           else \
               DRM_DEV_ERROR_RATELIMITED(vop2->dev, #x " irq err\n"); \
           active_irqs &= ~x##_INTR; \
           ret = IRQ_HANDLED; \
       } \
   } while (0)
 
   /*
    * The irq is shared with the iommu. If the runtime-pm state of the
    * vop2-device is disabled the irq has to be targeted at the iommu.
    */
   if (!pm_runtime_get_if_in_use(vop2->dev))
       return IRQ_NONE;
 
   if (vop2_core_clks_enable(vop2)) {
       DRM_DEV_ERROR(vop2->dev, "couldn't enable clocks\n");
       goto out;
   }
 
   /*
    * interrupt register has interrupt status, enable and clear bits, we
    * must hold irq_lock to avoid a race with enable/disable_vblank().
    */
   spin_lock_irqsave(&vop2->irq_lock, flags);
   for (i = 0; i < vp_max; i++)
       vp_irqs[i] = vop2_read_and_clear_active_vp_irqs(vop2, i);
   for (i = 0; i < axi_max; i++)
       axi_irqs[i] = vop2_read_and_clear_axi_irqs(vop2, i);
   wb_irqs = vop2_read_and_clear_wb_irqs(vop2);
   spin_unlock_irqrestore(&vop2->irq_lock, flags);
 
   for (i = 0; i < vp_max; i++) {
       vp = &vop2->vps[i];
       crtc = &vp->rockchip_crtc.crtc;
       active_irqs = vp_irqs[i];
       if (active_irqs & DSP_HOLD_VALID_INTR) {
           complete(&vp->dsp_hold_completion);
           active_irqs &= ~DSP_HOLD_VALID_INTR;
           ret = IRQ_HANDLED;
       }
 
       if (active_irqs & LINE_FLAG_INTR) {
           complete(&vp->line_flag_completion);
           active_irqs &= ~LINE_FLAG_INTR;
           ret = IRQ_HANDLED;
       }
 
       if (active_irqs & LINE_FLAG1_INTR) {
           vop2_handle_vcnt(crtc);
           active_irqs &= ~LINE_FLAG1_INTR;
           ret = IRQ_HANDLED;
       }
 
       if (vop2->version == VOP_VERSION_RK3528 && vp->id == 1) {
           if (active_irqs & POST_BUF_EMPTY_INTR)
               atomic_inc(&vp->post_buf_empty_flag);
 
           if (active_irqs & FS_FIELD_INTR &&
               (atomic_read(&vp->post_buf_empty_flag) > 0 ||
                vp->need_reset_p2i_flag == true))
               queue_work(vop2->workqueue, &vop2->post_buf_empty_work);
       }
 
       if (active_irqs & FS_FIELD_INTR) {
           rockchip_drm_dbg(vop2->dev, VOP_DEBUG_VSYNC, "vsync_vp%d\n", vp->id);
           vop2_wb_handler(vp);
           if (likely(!vp->skip_vsync) || (vp->layer_sel_update == false)) {
               drm_crtc_handle_vblank(crtc);
               vop2_handle_vblank(vop2, crtc);
           }
           active_irqs &= ~FS_FIELD_INTR;
           ret = IRQ_HANDLED;
       }
 
       ERROR_HANDLER(POST_BUF_EMPTY);
 
       /* Unhandled irqs are spurious. */
       if (active_irqs)
           DRM_ERROR("Unknown video_port%d IRQs: %02x\n", i, active_irqs);
   }
 
   if (wb_irqs) {
       active_irqs = wb_irqs;
       ERROR_HANDLER(WB_UV_FIFO_FULL);
       ERROR_HANDLER(WB_YRGB_FIFO_FULL);
   }
 
   for (i = 0; i < axi_max; i++) {
       active_irqs = axi_irqs[i];
 
       ERROR_HANDLER(BUS_ERROR);
 
       /* Unhandled irqs are spurious. */
       if (active_irqs)
           DRM_ERROR("Unknown axi_bus%d IRQs: %02x\n", i, active_irqs);
   }
 
   if (vop2->data->nr_dscs)
       vop2_dsc_isr(vop2);
 
   vop2_core_clks_disable(vop2);
out:
   pm_runtime_put(vop2->dev);
   return ret;
}
 
static int vop2_plane_create_name_property(struct vop2 *vop2, struct vop2_win *win)
{
   struct drm_prop_enum_list *props = vop2->plane_name_list;
   struct drm_property *prop;
   uint64_t bits = BIT_ULL(win->plane_id);
 
   prop = drm_property_create_bitmask(vop2->drm_dev,
                      DRM_MODE_PROP_IMMUTABLE, "NAME",
                      props, vop2->registered_num_wins,
                      bits);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create Name prop for %s failed\n", win->name);
       return -ENOMEM;
   }
   win->name_prop = prop;
   drm_object_attach_property(&win->base.base, win->name_prop, bits);
 
   return 0;
}
 
static int vop2_plane_create_feature_property(struct vop2 *vop2, struct vop2_win *win)
{
   uint64_t feature = 0;
   struct drm_property *prop;
 
   static const struct drm_prop_enum_list props[] = {
       { ROCKCHIP_DRM_PLANE_FEATURE_SCALE, "scale" },
       { ROCKCHIP_DRM_PLANE_FEATURE_AFBDC, "afbdc" },
   };
 
   if ((win->max_upscale_factor != 1) || (win->max_downscale_factor != 1))
       feature |= BIT(ROCKCHIP_DRM_PLANE_FEATURE_SCALE);
   if (win->feature & WIN_FEATURE_AFBDC)
       feature |= BIT(ROCKCHIP_DRM_PLANE_FEATURE_AFBDC);
 
   prop = drm_property_create_bitmask(vop2->drm_dev,
                      DRM_MODE_PROP_IMMUTABLE, "FEATURE",
                      props, ARRAY_SIZE(props),
                      feature);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create feature prop for %s failed\n", win->name);
       return -ENOMEM;
   }
 
   win->feature_prop = prop;
 
   drm_object_attach_property(&win->base.base, win->feature_prop, feature);
 
   return 0;
}
 
static bool vop3_ignore_plane(struct vop2 *vop2, struct vop2_win *win)
{
   if (!is_vop3(vop2))
       return false;
 
   if (vop2->esmart_lb_mode == VOP3_ESMART_8K_MODE &&
       win->phys_id != ROCKCHIP_VOP2_ESMART0)
       return true;
   else if (vop2->esmart_lb_mode == VOP3_ESMART_4K_4K_MODE &&
        (win->phys_id == ROCKCHIP_VOP2_ESMART1 || win->phys_id == ROCKCHIP_VOP2_ESMART3))
       return true;
   else if (vop2->esmart_lb_mode == VOP3_ESMART_4K_2K_2K_MODE &&
        win->phys_id == ROCKCHIP_VOP2_ESMART1)
       return true;
   else
       return false;
}
 
static u32 vop3_esmart_linebuffer_size(struct vop2 *vop2, struct vop2_win *win)
{
   if (!is_vop3(vop2) || vop2_cluster_window(win))
       return vop2->data->max_output.width;
 
   if (vop2->esmart_lb_mode == VOP3_ESMART_2K_2K_2K_2K_MODE ||
       (vop2->esmart_lb_mode == VOP3_ESMART_4K_2K_2K_MODE && win->phys_id != ROCKCHIP_VOP2_ESMART0))
       return vop2->data->max_output.width / 2;
   else
       return vop2->data->max_output.width;
}
 
static void vop3_init_esmart_scale_engine(struct vop2 *vop2)
{
   u8 scale_engine_num = 0;
   struct drm_plane *plane = NULL;
 
   drm_for_each_plane(plane, vop2->drm_dev) {
       struct vop2_win *win = to_vop2_win(plane);
 
       if (win->parent || vop2_cluster_window(win))
           continue;
 
       win->scale_engine_num = scale_engine_num++;
   }
}
 
static int vop2_plane_init(struct vop2 *vop2, struct vop2_win *win, unsigned long possible_crtcs)
{
   struct rockchip_drm_private *private = vop2->drm_dev->dev_private;
   unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) | BIT(DRM_MODE_BLEND_PREMULTI) |
                 BIT(DRM_MODE_BLEND_COVERAGE);
   unsigned int max_width, max_height;
   int ret;
 
   /*
    * Some userspace software don't want use afbc plane
    */
   if (win->feature & WIN_FEATURE_AFBDC) {
       if (vop2->disable_afbc_win)
           return -EACCES;
   }
 
   /*
    * Some userspace software don't want cluster sub plane
    */
   if (!vop2->support_multi_area) {
       if (win->feature & WIN_FEATURE_CLUSTER_SUB)
           return -EACCES;
   }
 
   /* ignore some plane register according vop3 esmart lb mode */
   if (vop3_ignore_plane(vop2, win))
       return -EACCES;
 
   ret = drm_universal_plane_init(vop2->drm_dev, &win->base, possible_crtcs,
                      &vop2_plane_funcs, win->formats, win->nformats,
                      win->format_modifiers, win->type, win->name);
   if (ret) {
       DRM_DEV_ERROR(vop2->dev, "failed to initialize plane %d\n", ret);
       return ret;
   }
 
   drm_plane_helper_add(&win->base, &vop2_plane_helper_funcs);
 
   drm_object_attach_property(&win->base.base, private->eotf_prop, 0);
   drm_object_attach_property(&win->base.base, private->color_space_prop, 0);
   drm_object_attach_property(&win->base.base, private->async_commit_prop, 0);
 
   if (win->feature & (WIN_FEATURE_CLUSTER_SUB | WIN_FEATURE_CLUSTER_MAIN))
       drm_object_attach_property(&win->base.base, private->share_id_prop, win->plane_id);
 
   if (win->parent)
       drm_object_attach_property(&win->base.base, private->share_id_prop,
                      win->parent->base.base.id);
   else
       drm_object_attach_property(&win->base.base, private->share_id_prop,
                      win->base.base.id);
   if (win->supported_rotations)
       drm_plane_create_rotation_property(&win->base, DRM_MODE_ROTATE_0,
                          DRM_MODE_ROTATE_0 | win->supported_rotations);
   drm_plane_create_alpha_property(&win->base);
   drm_plane_create_blend_mode_property(&win->base, blend_caps);
   drm_plane_create_zpos_property(&win->base, win->win_id, 0, vop2->registered_num_wins - 1);
   vop2_plane_create_name_property(vop2, win);
   vop2_plane_create_feature_property(vop2, win);
   max_width = vop2->data->max_input.width;
   max_height = vop2->data->max_input.height;
   if (win->feature & WIN_FEATURE_CLUSTER_SUB)
       max_width >>= 1;
   win->input_width_prop = drm_property_create_range(vop2->drm_dev, DRM_MODE_PROP_IMMUTABLE,
                             "INPUT_WIDTH", 0, max_width);
   win->input_height_prop = drm_property_create_range(vop2->drm_dev, DRM_MODE_PROP_IMMUTABLE,
                              "INPUT_HEIGHT", 0, max_height);
   max_width = vop3_esmart_linebuffer_size(vop2, win);
   max_height = vop2->data->max_output.height;
   if (win->feature & WIN_FEATURE_CLUSTER_SUB)
       max_width >>= 1;
   win->output_width_prop = drm_property_create_range(vop2->drm_dev, DRM_MODE_PROP_IMMUTABLE,
                              "OUTPUT_WIDTH", 0, max_width);
   win->output_height_prop = drm_property_create_range(vop2->drm_dev, DRM_MODE_PROP_IMMUTABLE,
                               "OUTPUT_HEIGHT", 0, max_height);
   win->scale_prop = drm_property_create_range(vop2->drm_dev, DRM_MODE_PROP_IMMUTABLE,
                           "SCALE_RATE", win->max_downscale_factor,
                           win->max_upscale_factor);
   /*
    * Support 24 bit(RGB888) or 16 bit(rgb565) color key.
    * Bit 31 is used as a flag to disable (0) or enable
    * color keying (1).
    */
   win->color_key_prop = drm_property_create_range(vop2->drm_dev, 0, "colorkey", 0,
                           0x80ffffff);
 
   if (!win->input_width_prop || !win->input_height_prop ||
       !win->output_width_prop || !win->output_height_prop ||
       !win->scale_prop || !win->color_key_prop) {
       DRM_ERROR("failed to create property\n");
       return -ENOMEM;
   }
 
   drm_object_attach_property(&win->base.base, win->input_width_prop, 0);
   drm_object_attach_property(&win->base.base, win->input_height_prop, 0);
   drm_object_attach_property(&win->base.base, win->output_width_prop, 0);
   drm_object_attach_property(&win->base.base, win->output_height_prop, 0);
   drm_object_attach_property(&win->base.base, win->scale_prop, 0);
   drm_object_attach_property(&win->base.base, win->color_key_prop, 0);
 
   return 0;
}
 
static struct drm_plane *vop2_cursor_plane_init(struct vop2_video_port *vp)
{
   struct vop2 *vop2 = vp->vop2;
   struct drm_plane *cursor = NULL;
   struct vop2_win *win;
   unsigned long possible_crtcs = 0;
 
   win = vop2_find_win_by_phys_id(vop2, vp->cursor_win_id);
   if (win) {
       if (vop2->disable_win_move) {
           const struct vop2_data *vop2_data = vop2->data;
           struct drm_crtc *crtc = vop2_find_crtc_by_plane_mask(vop2, win->phys_id);
 
           if (crtc)
               possible_crtcs = drm_crtc_mask(crtc);
           else
               possible_crtcs = (1 << vop2_data->nr_vps) - 1;
       }
 
       if (win->possible_crtcs)
           possible_crtcs = win->possible_crtcs;
       win->type = DRM_PLANE_TYPE_CURSOR;
       win->zpos = vop2->registered_num_wins - 1;
       if (!vop2_plane_init(vop2, win, possible_crtcs))
           cursor = &win->base;
   }
 
   return cursor;
}
 
static int vop2_gamma_init(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_video_port_data *vp_data;
   struct vop2_video_port *vp;
   struct device *dev = vop2->dev;
   u16 *r_base, *g_base, *b_base;
   struct drm_crtc *crtc;
   int i = 0, j = 0;
   u32 lut_len = 0;
 
   if (!vop2->lut_regs)
       return 0;
 
   for (i = 0; i < vop2_data->nr_vps; i++) {
       vp = &vop2->vps[i];
       crtc = &vp->rockchip_crtc.crtc;
       if (!crtc->dev)
           continue;
       vp_data = &vop2_data->vp[vp->id];
       lut_len = vp_data->gamma_lut_len;
       if (!lut_len)
           continue;
       vp->gamma_lut_len = vp_data->gamma_lut_len;
       vp->lut_dma_rid = vp_data->lut_dma_rid;
       vp->lut = devm_kmalloc_array(dev, lut_len, sizeof(*vp->lut),
                        GFP_KERNEL);
       if (!vp->lut)
           return -ENOMEM;
 
       for (j = 0; j < lut_len; j++) {
           u32 b = j * lut_len * lut_len;
           u32 g = j * lut_len;
           u32 r = j;
 
           vp->lut[j] = r | g | b;
       }
 
       drm_mode_crtc_set_gamma_size(crtc, lut_len);
       drm_crtc_enable_color_mgmt(crtc, 0, false, lut_len);
       r_base = crtc->gamma_store;
       g_base = r_base + crtc->gamma_size;
       b_base = g_base + crtc->gamma_size;
       for (j = 0; j < lut_len; j++) {
           rockchip_vop2_crtc_fb_gamma_get(crtc, &r_base[j],
                           &g_base[j],
                           &b_base[j], j);
       }
   }
 
   return 0;
}
 
static int vop2_crtc_create_plane_mask_property(struct vop2 *vop2,
                       struct drm_crtc *crtc,
                       uint32_t plane_mask)
{
   struct drm_property *prop;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
 
   static const struct drm_prop_enum_list props[] = {
       { ROCKCHIP_VOP2_CLUSTER0, "Cluster0" },
       { ROCKCHIP_VOP2_CLUSTER1, "Cluster1" },
       { ROCKCHIP_VOP2_ESMART0, "Esmart0" },
       { ROCKCHIP_VOP2_ESMART1, "Esmart1" },
       { ROCKCHIP_VOP2_SMART0, "Smart0" },
       { ROCKCHIP_VOP2_SMART1, "Smart1" },
       { ROCKCHIP_VOP2_CLUSTER2, "Cluster2" },
       { ROCKCHIP_VOP2_CLUSTER3, "Cluster3" },
       { ROCKCHIP_VOP2_ESMART2, "Esmart2" },
       { ROCKCHIP_VOP2_ESMART3, "Esmart3" },
   };
 
   prop = drm_property_create_bitmask(vop2->drm_dev,
                      DRM_MODE_PROP_IMMUTABLE, "PLANE_MASK",
                      props, ARRAY_SIZE(props),
                      0xffffffff);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create plane_mask prop for vp%d failed\n", vp->id);
       return -ENOMEM;
   }
 
   vp->plane_mask_prop = prop;
   drm_object_attach_property(&crtc->base, vp->plane_mask_prop, plane_mask);
 
   return 0;
}
 
static int vop2_crtc_create_feature_property(struct vop2 *vop2, struct drm_crtc *crtc)
{
   const struct vop2_data *vop2_data = vop2->data;
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id];
   struct drm_property *prop;
   u64 feature = 0;
 
   static const struct drm_prop_enum_list props[] = {
       { ROCKCHIP_DRM_CRTC_FEATURE_ALPHA_SCALE, "ALPHA_SCALE" },
       { ROCKCHIP_DRM_CRTC_FEATURE_HDR10, "HDR10" },
       { ROCKCHIP_DRM_CRTC_FEATURE_NEXT_HDR, "NEXT_HDR" },
       { ROCKCHIP_DRM_CRTC_FEATURE_VIVID_HDR, "VIVID_HDR" },
   };
 
   if (vp_data->feature & VOP_FEATURE_ALPHA_SCALE)
       feature |= BIT(ROCKCHIP_DRM_CRTC_FEATURE_ALPHA_SCALE);
   if (vp_data->feature & VOP_FEATURE_HDR10)
       feature |= BIT(ROCKCHIP_DRM_CRTC_FEATURE_HDR10);
   if (vp_data->feature & VOP_FEATURE_NEXT_HDR)
       feature |= BIT(ROCKCHIP_DRM_CRTC_FEATURE_NEXT_HDR);
   if (vp_data->feature & VOP_FEATURE_VIVID_HDR)
       feature |= BIT(ROCKCHIP_DRM_CRTC_FEATURE_VIVID_HDR);
 
   prop = drm_property_create_bitmask(vop2->drm_dev,
                      DRM_MODE_PROP_IMMUTABLE, "FEATURE",
                      props, ARRAY_SIZE(props),
                      0xffffffff);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create FEATURE prop for vp%d failed\n", vp->id);
       return -ENOMEM;
   }
 
   vp->feature_prop = prop;
   drm_object_attach_property(&crtc->base, vp->feature_prop, feature);
 
   prop = drm_property_create_range(vop2->drm_dev, DRM_MODE_PROP_IMMUTABLE, "OUTPUT_WIDTH",
                    0, vop2->data->vp[vp->id].max_output.width);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create OUTPUT_WIDTH prop for vp%d failed\n", vp->id);
       return -ENOMEM;
   }
   vp->output_width_prop = prop;
   drm_object_attach_property(&crtc->base, vp->output_width_prop, 0);
 
   prop = drm_property_create_range(vop2->drm_dev, DRM_MODE_PROP_IMMUTABLE, "OUTPUT_DCLK",
                    0, rockchip_drm_get_dclk_by_width(vop2->data->vp[vp->id].max_output.width) * 1000);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create OUTPUT_DCLK prop for vp%d failed\n", vp->id);
       return -ENOMEM;
   }
   vp->output_dclk_prop = prop;
   drm_object_attach_property(&crtc->base, vp->output_dclk_prop, 0);
 
   return 0;
}
 
static int vop2_crtc_create_vrr_property(struct vop2 *vop2, struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct drm_property *prop;
 
   prop = drm_property_create_range(vop2->drm_dev, 0, "variable refresh rate", 0, 144);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create vrr prop for vp%d failed\n", vp->id);
       return -ENOMEM;
   }
   vp->variable_refresh_rate_prop = prop;
   drm_object_attach_property(&crtc->base, vp->variable_refresh_rate_prop, 0);
 
   prop = drm_property_create_range(vop2->drm_dev, 0, "max refresh rate", 0, 144);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create vrr prop for vp%d failed\n", vp->id);
       return -ENOMEM;
   }
   vp->max_refresh_rate_prop = prop;
   drm_object_attach_property(&crtc->base, vp->max_refresh_rate_prop, 0);
 
   prop = drm_property_create_range(vop2->drm_dev, 0, "min refresh rate", 0, 144);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create vrr prop for vp%d failed\n", vp->id);
       return -ENOMEM;
   }
   vp->min_refresh_rate_prop = prop;
   drm_object_attach_property(&crtc->base, vp->min_refresh_rate_prop, 0);
 
   return 0;
}
 
static int vop2_crtc_create_hdr_property(struct vop2 *vop2, struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct drm_property *prop;
 
   prop = drm_property_create(vop2->drm_dev, DRM_MODE_PROP_BLOB, "HDR_EXT_DATA", 0);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create hdr ext data prop for vp%d failed\n", vp->id);
       return -ENOMEM;
   }
   vp->hdr_ext_data_prop = prop;
   drm_object_attach_property(&crtc->base, vp->hdr_ext_data_prop, 0);
 
   return 0;
}
 
static int vop2_crtc_create_post_acm_property(struct vop2 *vop2, struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct drm_property *prop;
 
   prop = drm_property_create(vop2->drm_dev, DRM_MODE_PROP_BLOB, "ACM_LUT_DATA", 0);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create acm lut data prop for vp%d failed\n", vp->id);
       return -ENOMEM;
   }
   vp->acm_lut_data_prop = prop;
   drm_object_attach_property(&crtc->base, vp->acm_lut_data_prop, 0);
 
   return 0;
}
 
static int vop2_crtc_create_post_csc_property(struct vop2 *vop2, struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
   struct drm_property *prop;
 
   prop = drm_property_create(vop2->drm_dev, DRM_MODE_PROP_BLOB, "POST_CSC_DATA", 0);
   if (!prop) {
       DRM_DEV_ERROR(vop2->dev, "create post csc data prop for vp%d failed\n", vp->id);
       return -ENOMEM;
   }
   vp->post_csc_data_prop = prop;
   drm_object_attach_property(&crtc->base, vp->post_csc_data_prop, 0);
 
   return 0;
}
#define RK3566_MIRROR_PLANE_MASK (BIT(ROCKCHIP_VOP2_CLUSTER1) | BIT(ROCKCHIP_VOP2_ESMART1) | \
                 BIT(ROCKCHIP_VOP2_SMART1))
 
/*
 * Returns:
 * Registered crtc number on success, negative error code on failure.
 */
static int vop2_create_crtc(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   struct drm_device *drm_dev = vop2->drm_dev;
   struct device *dev = vop2->dev;
   struct drm_plane *primary;
   struct drm_plane *cursor = NULL;
   struct drm_crtc *crtc;
   struct device_node *port;
   struct vop2_win *win = NULL;
   struct vop2_video_port *vp;
   const struct vop2_video_port_data *vp_data;
   uint32_t possible_crtcs;
   uint64_t soc_id;
   uint32_t registered_num_crtcs = 0;
   uint32_t plane_mask = 0;
   char clk_name[16];
   int i = 0, j = 0, k = 0;
   int ret = 0;
   bool be_used_for_primary_plane = false;
   bool find_primary_plane = false;
   bool bootloader_initialized = false;
   struct rockchip_drm_private *private = drm_dev->dev_private;
 
   /* all planes can attach to any crtc */
   possible_crtcs = (1 << vop2_data->nr_vps) - 1;
 
   /*
    * We set plane_mask from dts or bootloader
    * if all the plane_mask are zero, that means
    * the bootloader don't initialized the vop, or
    * something is wrong, the kernel will try to
    * initial all the vp.
    */
   for (i = 0; i < vop2_data->nr_vps; i++) {
       vp = &vop2->vps[i];
       if (vp->plane_mask) {
           bootloader_initialized = true;
           break;
       }
   }
 
   /*
    * Create primary plane for eache crtc first, since we need
    * to pass them to drm_crtc_init_with_planes, which sets the
    * "possible_crtcs" to the newly initialized crtc.
    */
   for (i = 0; i < vop2_data->nr_vps; i++) {
       vp_data = &vop2_data->vp[i];
       vp = &vop2->vps[i];
       vp->vop2 = vop2;
       vp->id = vp_data->id;
       vp->regs = vp_data->regs;
       vp->cursor_win_id = -1;
       primary = NULL;
       cursor = NULL;
 
       if (vop2->disable_win_move)
           possible_crtcs = BIT(registered_num_crtcs);
 
       /*
        * we assume a vp with a zere plane_mask(set from dts or bootloader)
        * as unused.
        */
       if (!vp->plane_mask && bootloader_initialized)
           continue;
 
       if (vop2_soc_is_rk3566())
           soc_id = vp_data->soc_id[1];
       else
           soc_id = vp_data->soc_id[0];
 
       snprintf(clk_name, sizeof(clk_name), "dclk_vp%d", vp->id);
       vp->dclk_rst = devm_reset_control_get_optional(vop2->dev, clk_name);
       if (IS_ERR(vp->dclk_rst)) {
           DRM_DEV_ERROR(vop2->dev, "failed to get dclk reset\n");
           return PTR_ERR(vp->dclk_rst);
       }
 
       vp->dclk = devm_clk_get(vop2->dev, clk_name);
       if (IS_ERR(vp->dclk)) {
           DRM_DEV_ERROR(vop2->dev, "failed to get %s\n", clk_name);
           return PTR_ERR(vp->dclk);
       }
 
       snprintf(clk_name, sizeof(clk_name), "dclk_src_vp%d", vp->id);
       vp->dclk_parent = devm_clk_get_optional(vop2->dev, clk_name);
       if (IS_ERR(vp->dclk)) {
           DRM_DEV_ERROR(vop2->dev, "failed to get %s\n", clk_name);
           return PTR_ERR(vp->dclk);
       }
 
       crtc = &vp->rockchip_crtc.crtc;
 
       port = of_graph_get_port_by_id(dev->of_node, i);
       if (!port) {
           DRM_DEV_ERROR(vop2->dev, "no port node found for video_port%d\n", i);
           return -ENOENT;
       }
       crtc->port = port;
       of_property_read_u32(port, "cursor-win-id", &vp->cursor_win_id);
 
       plane_mask = vp->plane_mask;
       if (vop2_soc_is_rk3566()) {
           if ((vp->plane_mask & RK3566_MIRROR_PLANE_MASK) &&
               (vp->plane_mask & ~RK3566_MIRROR_PLANE_MASK)) {
               plane_mask &= ~RK3566_MIRROR_PLANE_MASK;
           }
       }
 
       if (vp->primary_plane_phy_id >= 0) {
           win = vop2_find_win_by_phys_id(vop2, vp->primary_plane_phy_id);
           if (win) {
               find_primary_plane = true;
               win->type = DRM_PLANE_TYPE_PRIMARY;
           }
       } else {
           j = 0;
           while (j < vop2->registered_num_wins) {
               be_used_for_primary_plane = false;
               win = &vop2->win[j];
               j++;
 
               if (win->parent || (win->feature & WIN_FEATURE_CLUSTER_SUB))
                   continue;
 
               if (win->type != DRM_PLANE_TYPE_PRIMARY)
                   continue;
 
               for (k = 0; k < vop2_data->nr_vps; k++) {
                   if (win->phys_id == vop2->vps[k].primary_plane_phy_id) {
                       be_used_for_primary_plane = true;
                       break;
                   }
               }
 
               if (be_used_for_primary_plane)
                   continue;
 
               find_primary_plane = true;
               break;
           }
 
           if (find_primary_plane)
               vp->primary_plane_phy_id = win->phys_id;
       }
 
       if (!find_primary_plane) {
           DRM_DEV_ERROR(vop2->dev, "No primary plane find for video_port%d\n", i);
           break;
       } else {
           /* give lowest zpos for primary plane */
           win->zpos = registered_num_crtcs;
           if (win->possible_crtcs)
               possible_crtcs = win->possible_crtcs;
           if (vop2_plane_init(vop2, win, possible_crtcs)) {
               DRM_DEV_ERROR(vop2->dev, "failed to init primary plane\n");
               break;
           }
           primary = &win->base;
       }
 
       /* some times we want a cursor window for some vp */
       if (vp->cursor_win_id < 0) {
           bool be_used_for_cursor_plane = false;
 
           j = 0;
           while (j < vop2->registered_num_wins) {
               win = &vop2->win[j++];
 
               if (win->parent || (win->feature & WIN_FEATURE_CLUSTER_SUB))
                   continue;
 
               if (win->type != DRM_PLANE_TYPE_CURSOR)
                   continue;
 
               for (k = 0; k < vop2_data->nr_vps; k++) {
                   if (vop2->vps[k].cursor_win_id == win->phys_id)
                       be_used_for_cursor_plane = true;
               }
               if (be_used_for_cursor_plane)
                   continue;
               vp->cursor_win_id = win->phys_id;
           }
       }
 
       if (vp->cursor_win_id >= 0) {
           cursor = vop2_cursor_plane_init(vp);
           if (!cursor)
               DRM_WARN("failed to init cursor plane for vp%d\n", vp->id);
           else
               DRM_DEV_INFO(vop2->dev, "%s as cursor plane for vp%d\n",
                        cursor->name, vp->id);
       }
 
       ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor, &vop2_crtc_funcs,
                       "video_port%d", vp->id);
       if (ret) {
           DRM_DEV_ERROR(vop2->dev, "crtc init for video_port%d failed\n", i);
           return ret;
       }
 
       drm_crtc_helper_add(crtc, &vop2_crtc_helper_funcs);
 
       drm_flip_work_init(&vp->fb_unref_work, "fb_unref", vop2_fb_unref_worker);
 
       init_completion(&vp->dsp_hold_completion);
       init_completion(&vp->line_flag_completion);
       rockchip_register_crtc_funcs(crtc, &private_crtc_funcs);
       soc_id = vop2_soc_id_fixup(soc_id);
       drm_object_attach_property(&crtc->base, private->soc_id_prop, soc_id);
       drm_object_attach_property(&crtc->base, private->port_id_prop, vp->id);
       drm_object_attach_property(&crtc->base, private->aclk_prop, 0);
       drm_object_attach_property(&crtc->base, private->bg_prop, 0);
       drm_object_attach_property(&crtc->base, private->line_flag_prop, 0);
       if (vp_data->feature & VOP_FEATURE_OVERSCAN) {
           drm_object_attach_property(&crtc->base,
                          drm_dev->mode_config.tv_left_margin_property, 100);
           drm_object_attach_property(&crtc->base,
                          drm_dev->mode_config.tv_right_margin_property, 100);
           drm_object_attach_property(&crtc->base,
                          drm_dev->mode_config.tv_top_margin_property, 100);
           drm_object_attach_property(&crtc->base,
                          drm_dev->mode_config.tv_bottom_margin_property, 100);
       }
       if (plane_mask)
           vop2_crtc_create_plane_mask_property(vop2, crtc, plane_mask);
       vop2_crtc_create_feature_property(vop2, crtc);
       vop2_crtc_create_vrr_property(vop2, crtc);
 
       ret = drm_self_refresh_helper_init(crtc);
       if (ret)
           DRM_DEV_DEBUG_KMS(vop2->dev,
                     "Failed to init %s with SR helpers %d, ignoring\n",
                     crtc->name, ret);
 
       if (vp_data->feature & VOP_FEATURE_VIVID_HDR)
           vop2_crtc_create_hdr_property(vop2, crtc);
       if (vp_data->feature & VOP_FEATURE_POST_ACM)
           vop2_crtc_create_post_acm_property(vop2, crtc);
       if (vp_data->feature & VOP_FEATURE_POST_CSC)
           vop2_crtc_create_post_csc_property(vop2, crtc);
 
       registered_num_crtcs++;
   }
 
   /*
    * change the unused primary window to overlay window
    */
   for (j = 0; j < vop2->registered_num_wins; j++) {
       win = &vop2->win[j];
       be_used_for_primary_plane = false;
 
       for (k = 0; k < vop2_data->nr_vps; k++) {
           if (vop2->vps[k].primary_plane_phy_id == win->phys_id) {
               be_used_for_primary_plane = true;
               break;
           }
       }
 
       if (win->type == DRM_PLANE_TYPE_PRIMARY &&
           !be_used_for_primary_plane)
           win->type = DRM_PLANE_TYPE_OVERLAY;
   }
 
   /*
    * create overlay planes of the leftover overlay win
    * Create drm_planes for overlay windows with possible_crtcs restricted
    */
   for (j = 0; j < vop2->registered_num_wins; j++) {
       win = &vop2->win[j];
 
       if (win->type != DRM_PLANE_TYPE_OVERLAY)
           continue;
       /*
        * Only dual display on rk3568(which need two crtcs) need mirror win
        */
       if (registered_num_crtcs < 2 && vop2_is_mirror_win(win))
           continue;
       /*
        * zpos of overlay plane is higher than primary
        * and lower than cursor
        */
       win->zpos = registered_num_crtcs + j;
 
       if (vop2->disable_win_move) {
           crtc = vop2_find_crtc_by_plane_mask(vop2, win->phys_id);
           if (crtc)
               possible_crtcs = drm_crtc_mask(crtc);
           else
               possible_crtcs = (1 << vop2_data->nr_vps) - 1;
       }
       if (win->possible_crtcs)
           possible_crtcs = win->possible_crtcs;
 
       ret = vop2_plane_init(vop2, win, possible_crtcs);
       if (ret)
           DRM_WARN("failed to init overlay plane %s\n", win->name);
   }
 
   if (is_vop3(vop2))
       vop3_init_esmart_scale_engine(vop2);
 
   return registered_num_crtcs;
}
 
static void vop2_destroy_crtc(struct drm_crtc *crtc)
{
   struct vop2_video_port *vp = to_vop2_video_port(crtc);
 
   drm_self_refresh_helper_cleanup(crtc);
   if (vp->hdr_lut_gem_obj)
       rockchip_gem_free_object(&vp->hdr_lut_gem_obj->base);
 
   of_node_put(crtc->port);
 
   /*
    * Destroy CRTC after vop2_plane_destroy() since vop2_disable_plane()
    * references the CRTC.
    */
   drm_crtc_cleanup(crtc);
   drm_flip_work_cleanup(&vp->fb_unref_work);
}
 
static int vop2_pd_data_init(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_power_domain_data *pd_data;
   struct vop2_power_domain *pd;
   int i;
 
   INIT_LIST_HEAD(&vop2->pd_list_head);
 
   for (i = 0; i < vop2_data->nr_pds; i++) {
       pd_data = &vop2_data->pd[i];
       pd = devm_kzalloc(vop2->dev, sizeof(*pd), GFP_KERNEL);
       if (!pd)
           return -ENOMEM;
       pd->vop2 = vop2;
       pd->data = pd_data;
       pd->vp_mask = 0;
       spin_lock_init(&pd->lock);
       list_add_tail(&pd->list, &vop2->pd_list_head);
       INIT_DELAYED_WORK(&pd->power_off_work, vop2_power_domain_off_work);
       if (pd_data->parent_id) {
           pd->parent = vop2_find_pd_by_id(vop2, pd_data->parent_id);
           if (!pd->parent) {
               DRM_DEV_ERROR(vop2->dev, "no parent pd find for pd%d\n", pd->data->id);
               return -EINVAL;
           }
       }
   }
 
   return 0;
}
 
static void vop2_dsc_data_init(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_dsc_data *dsc_data;
   struct vop2_dsc *dsc;
   int i;
 
   for (i = 0; i < vop2_data->nr_dscs; i++) {
       dsc = &vop2->dscs[i];
       dsc_data = &vop2_data->dsc[i];
       dsc->id = dsc_data->id;
       dsc->max_slice_num = dsc_data->max_slice_num;
       dsc->max_linebuf_depth = dsc_data->max_linebuf_depth;
       dsc->min_bits_per_pixel = dsc_data->min_bits_per_pixel;
       dsc->regs = dsc_data->regs;
       dsc->attach_vp_id = -1;
       if (dsc_data->pd_id)
           dsc->pd = vop2_find_pd_by_id(vop2, dsc_data->pd_id);
   }
}
 
static int vop2_win_init(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_layer_data *layer_data;
   struct drm_prop_enum_list *plane_name_list;
   struct vop2_win *win;
   struct vop2_layer *layer;
   char name[DRM_PROP_NAME_LEN];
   unsigned int num_wins = 0;
   uint8_t plane_id = 0;
   unsigned int i, j;
 
   for (i = 0; i < vop2_data->win_size; i++) {
       const struct vop2_win_data *win_data = &vop2_data->win[i];
 
       win = &vop2->win[num_wins];
       win->name = win_data->name;
       win->regs = win_data->regs;
       win->offset = win_data->base;
       win->type = win_data->type;
       win->formats = win_data->formats;
       win->nformats = win_data->nformats;
       win->format_modifiers = win_data->format_modifiers;
       win->supported_rotations = win_data->supported_rotations;
       win->max_upscale_factor = win_data->max_upscale_factor;
       win->max_downscale_factor = win_data->max_downscale_factor;
       win->hsu_filter_mode = win_data->hsu_filter_mode;
       win->hsd_filter_mode = win_data->hsd_filter_mode;
       win->vsu_filter_mode = win_data->vsu_filter_mode;
       win->vsd_filter_mode = win_data->vsd_filter_mode;
       win->hsd_pre_filter_mode = win_data->hsd_pre_filter_mode;
       win->vsd_pre_filter_mode = win_data->vsd_pre_filter_mode;
       win->dly = win_data->dly;
       win->feature = win_data->feature;
       win->phys_id = win_data->phys_id;
       win->splice_win_id = win_data->splice_win_id;
       win->layer_sel_id = win_data->layer_sel_id;
       win->win_id = i;
       win->plane_id = plane_id++;
       win->area_id = 0;
       win->zpos = i;
       win->vop2 = vop2;
       win->axi_id = win_data->axi_id;
       win->axi_yrgb_id = win_data->axi_yrgb_id;
       win->axi_uv_id = win_data->axi_uv_id;
       win->possible_crtcs = win_data->possible_crtcs;
 
       if (win_data->pd_id)
           win->pd = vop2_find_pd_by_id(vop2, win_data->pd_id);
 
       num_wins++;
 
       if (!vop2->support_multi_area)
           continue;
 
       for (j = 0; j < win_data->area_size; j++) {
           struct vop2_win *area = &vop2->win[num_wins];
           const struct vop2_win_regs *regs = win_data->area[j];
 
           area->parent = win;
           area->offset = win->offset;
           area->regs = regs;
           area->type = DRM_PLANE_TYPE_OVERLAY;
           area->formats = win->formats;
           area->feature = win->feature;
           area->nformats = win->nformats;
           area->format_modifiers = win->format_modifiers;
           area->max_upscale_factor = win_data->max_upscale_factor;
           area->max_downscale_factor = win_data->max_downscale_factor;
           area->supported_rotations = win_data->supported_rotations;
           area->hsu_filter_mode = win_data->hsu_filter_mode;
           area->hsd_filter_mode = win_data->hsd_filter_mode;
           area->vsu_filter_mode = win_data->vsu_filter_mode;
           area->vsd_filter_mode = win_data->vsd_filter_mode;
           area->hsd_pre_filter_mode = win_data->hsd_pre_filter_mode;
           area->vsd_pre_filter_mode = win_data->vsd_pre_filter_mode;
           area->possible_crtcs = win->possible_crtcs;
 
           area->vop2 = vop2;
           area->win_id = i;
           area->phys_id = win->phys_id;
           area->area_id = j + 1;
           area->plane_id = plane_id++;
           snprintf(name, min(sizeof(name), strlen(win->name)), "%s", win->name);
           snprintf(name, sizeof(name), "%s%d", name, area->area_id);
           area->name = devm_kstrdup(vop2->dev, name, GFP_KERNEL);
           num_wins++;
       }
   }
 
   vop2->registered_num_wins = num_wins;
 
   if (!is_vop3(vop2)) {
       for (i = 0; i < vop2_data->nr_layers; i++) {
           layer = &vop2->layers[i];
           layer_data = &vop2_data->layer[i];
           layer->id = layer_data->id;
           layer->regs = layer_data->regs;
       }
   }
 
   plane_name_list = devm_kzalloc(vop2->dev,
                      vop2->registered_num_wins * sizeof(*plane_name_list),
                      GFP_KERNEL);
   if (!plane_name_list) {
       DRM_DEV_ERROR(vop2->dev, "failed to alloc memory for plane_name_list\n");
       return -ENOMEM;
   }
 
   for (i = 0; i < vop2->registered_num_wins; i++) {
       win = &vop2->win[i];
       plane_name_list[i].type = win->plane_id;
       plane_name_list[i].name = win->name;
   }
 
   vop2->plane_name_list = plane_name_list;
 
   return 0;
}
 
#include "rockchip_vop2_clk.c"
static void post_buf_empty_work_event(struct work_struct *work)
{
   struct vop2 *vop2 = container_of(work, struct vop2, post_buf_empty_work);
   struct rockchip_drm_private *private = vop2->drm_dev->dev_private;
   struct vop2_video_port *vp = &vop2->vps[1];
 
   /*
    * For RK3528, VP1 only supports NTSC and PAL mode(both interlace). If
    * POST_BUF_EMPTY_INTR comes, it is needed to reset the p2i_en bit, in
    * order to update the line parity flag, which ensures the correct order
    * of odd and even lines.
    */
   if (vop2->version == VOP_VERSION_RK3528) {
       if (atomic_read(&vp->post_buf_empty_flag) > 0) {
           atomic_set(&vp->post_buf_empty_flag, 0);
 
           mutex_lock(&private->ovl_lock);
           vop2_wait_for_fs_by_done_bit_status(vp);
           VOP_MODULE_SET(vop2, vp, p2i_en, 0);
           vop2_cfg_done(&vp->rockchip_crtc.crtc);
           vop2_wait_for_fs_by_done_bit_status(vp);
           mutex_unlock(&private->ovl_lock);
 
           vp->need_reset_p2i_flag = true;
       } else if (vp->need_reset_p2i_flag == true) {
           mutex_lock(&private->ovl_lock);
           vop2_wait_for_fs_by_done_bit_status(vp);
           VOP_MODULE_SET(vop2, vp, p2i_en, 1);
           vop2_cfg_done(&vp->rockchip_crtc.crtc);
           vop2_wait_for_fs_by_done_bit_status(vp);
           mutex_unlock(&private->ovl_lock);
 
           vp->need_reset_p2i_flag = false;
       }
   }
}
 
static bool vop2_plane_mask_check(struct vop2 *vop2)
{
   const struct vop2_data *vop2_data = vop2->data;
   u32 plane_mask = 0;
   int i;
 
   /*
    * For RK3568 and RK3588, all windows need to be assigned to
    * one of all vps, and two of vps can not share the same window.
    */
   if (vop2->version != VOP_VERSION_RK3568 && vop2->version != VOP_VERSION_RK3588)
       return true;
 
   for (i = 0; i < vop2_data->nr_vps; i++) {
       if (plane_mask & vop2->vps[i].plane_mask) {
           DRM_WARN("the same window can't be assigned to two vp\n");
           return false;
       }
       plane_mask |= vop2->vps[i].plane_mask;
   }
 
   if (hweight32(plane_mask) != vop2_data->nr_layers ||
       plane_mask != vop2_data->plane_mask_base) {
       DRM_WARN("all windows should be assigned, full plane mask: 0x%x, current plane mask: 0x%x\n",
            vop2_data->plane_mask_base, plane_mask);
       return false;
   }
 
   return true;
}
 
static uint32_t vop2_vp_plane_mask_to_bitmap(const struct vop2_vp_plane_mask *vp_plane_mask)
{
   int layer_phy_id = 0;
   int plane_mask = 0;
   int i;
 
   for (i = 0; i < vp_plane_mask->attached_layers_nr; i++) {
       layer_phy_id = vp_plane_mask->attached_layers[i];
       plane_mask |= BIT(layer_phy_id);
   }
 
   return plane_mask;
}
 
static bool vop2_get_vp_of_status(struct device_node *vp_node)
{
   struct device_node *vp_sub_node;
   struct device_node *remote_node;
   bool vp_enable = false;
 
   for_each_child_of_node(vp_node, vp_sub_node) {
       remote_node = of_graph_get_remote_endpoint(vp_sub_node);
       vp_enable |= of_device_is_available(remote_node);
   }
 
   return vp_enable;
}
 
static void vop2_plane_mask_assign(struct vop2 *vop2, struct device_node *vop_out_node)
{
   const struct vop2_data *vop2_data = vop2->data;
   const struct vop2_vp_plane_mask *plane_mask;
   struct device_node *child;
   int active_vp_num = 0;
   int vp_id;
   int i = 0;
 
   for_each_child_of_node(vop_out_node, child) {
       if (vop2_get_vp_of_status(child))
           active_vp_num++;
   }
 
   if (vop2_soc_is_rk3566() && active_vp_num > 2)
       DRM_WARN("RK3566 only support 2 vps\n");
   plane_mask = vop2_data->plane_mask;
   plane_mask += (active_vp_num - 1) * ROCKCHIP_MAX_CRTC;
 
   for_each_child_of_node(vop_out_node, child) {
       of_property_read_u32(child, "reg", &vp_id);
       if (vop2_get_vp_of_status(child)) {
           vop2->vps[vp_id].plane_mask = vop2_vp_plane_mask_to_bitmap(&plane_mask[i]);
           vop2->vps[vp_id].primary_plane_phy_id = plane_mask[i].primary_plane_id;
           i++;
       } else {
           vop2->vps[vp_id].plane_mask = 0;
           vop2->vps[vp_id].primary_plane_phy_id = ROCKCHIP_VOP2_PHY_ID_INVALID;
       }
   }
}
 
static int vop2_bind(struct device *dev, struct device *master, void *data)
{
   struct platform_device *pdev = to_platform_device(dev);
   const struct vop2_data *vop2_data;
   struct drm_device *drm_dev = data;
   struct vop2 *vop2;
   struct resource *res;
   size_t alloc_size;
   int ret, i;
   int num_wins = 0;
   int registered_num_crtcs;
   struct device_node *vop_out_node;
   struct device_node *mcu_timing_node;
 
   vop2_data = of_device_get_match_data(dev);
   if (!vop2_data)
       return -ENODEV;
 
   for (i = 0; i < vop2_data->win_size; i++) {
       const struct vop2_win_data *win_data = &vop2_data->win[i];
 
       num_wins += win_data->area_size + 1;
   }
 
   /* Allocate vop2 struct and its vop2_win array */
   alloc_size = sizeof(*vop2) + sizeof(*vop2->win) * num_wins;
   vop2 = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
   if (!vop2)
       return -ENOMEM;
 
   vop2->dev = dev;
   vop2->data = vop2_data;
   vop2->drm_dev = drm_dev;
   vop2->version = vop2_data->version;
 
   dev_set_drvdata(dev, vop2);
 
   vop2->support_multi_area = of_property_read_bool(dev->of_node, "support-multi-area");
   vop2->disable_afbc_win = of_property_read_bool(dev->of_node, "disable-afbc-win");
   vop2->disable_win_move = of_property_read_bool(dev->of_node, "disable-win-move");
   vop2->skip_ref_fb = of_property_read_bool(dev->of_node, "skip-ref-fb");
 
   ret = vop2_pd_data_init(vop2);
   if (ret)
       return ret;
   /*
    * esmart lb mode default config at vop2_reg.c vop2_data.esmart_lb_mode,
    * you can rewrite at dts vop node:
    *
    * VOP3_ESMART_8K_MODE = 0,
    * VOP3_ESMART_4K_4K_MODE = 1,
    * VOP3_ESMART_4K_2K_2K_MODE = 2,
    * VOP3_ESMART_2K_2K_2K_2K_MODE = 3,
    *
    * &vop {
    *     esmart_lb_mode = /bits/ 8 <2>;
    * };
    */
   ret = of_property_read_u8(dev->of_node, "esmart_lb_mode", &vop2->esmart_lb_mode);
   if (ret < 0)
       vop2->esmart_lb_mode = vop2->data->esmart_lb_mode;
 
   ret = vop2_win_init(vop2);
   if (ret)
       return ret;
 
   res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
   if (!res) {
       DRM_DEV_ERROR(vop2->dev, "failed to get vop2 register byname\n");
       return -EINVAL;
   }
   vop2->res = res;
   vop2->regs = devm_ioremap_resource(dev, res);
   if (IS_ERR(vop2->regs))
       return PTR_ERR(vop2->regs);
   vop2->len = resource_size(res);
 
   vop2->regsbak = devm_kzalloc(dev, vop2->len, GFP_KERNEL);
   if (!vop2->regsbak)
       return -ENOMEM;
 
   res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gamma_lut");
   if (res) {
       vop2->lut_regs = devm_ioremap_resource(dev, res);
       if (IS_ERR(vop2->lut_regs))
           return PTR_ERR(vop2->lut_regs);
   }
 
   res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "acm_regs");
   if (res) {
       vop2->acm_regs = devm_ioremap_resource(dev, res);
       if (IS_ERR(vop2->acm_regs))
           return PTR_ERR(vop2->acm_regs);
   }
 
   vop2->sys_grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
   vop2->grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,vop-grf");
   vop2->vo1_grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,vo1-grf");
   vop2->sys_pmu = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,pmu");
 
   vop2->hclk = devm_clk_get(vop2->dev, "hclk_vop");
   if (IS_ERR(vop2->hclk)) {
       DRM_DEV_ERROR(vop2->dev, "failed to get hclk source\n");
       return PTR_ERR(vop2->hclk);
   }
   vop2->aclk = devm_clk_get(vop2->dev, "aclk_vop");
   if (IS_ERR(vop2->aclk)) {
       DRM_DEV_ERROR(vop2->dev, "failed to get aclk source\n");
       return PTR_ERR(vop2->aclk);
   }
 
   vop2->pclk = devm_clk_get_optional(vop2->dev, "pclk_vop");
   if (IS_ERR(vop2->pclk)) {
       DRM_DEV_ERROR(vop2->dev, "failed to get pclk source\n");
       return PTR_ERR(vop2->pclk);
   }
 
   vop2->ahb_rst = devm_reset_control_get_optional(vop2->dev, "ahb");
   if (IS_ERR(vop2->ahb_rst)) {
       DRM_DEV_ERROR(vop2->dev, "failed to get ahb reset\n");
       return PTR_ERR(vop2->ahb_rst);
   }
 
   vop2->axi_rst = devm_reset_control_get_optional(vop2->dev, "axi");
   if (IS_ERR(vop2->axi_rst)) {
       DRM_DEV_ERROR(vop2->dev, "failed to get axi reset\n");
       return PTR_ERR(vop2->axi_rst);
   }
 
   vop2->irq = platform_get_irq(pdev, 0);
   if (vop2->irq < 0) {
       DRM_DEV_ERROR(dev, "cannot find irq for vop2\n");
       return vop2->irq;
   }
 
   vop_out_node = of_get_child_by_name(dev->of_node, "ports");
   if (vop_out_node) {
       struct device_node *child;
 
       for_each_child_of_node(vop_out_node, child) {
           u32 plane_mask = 0;
           u32 primary_plane_phy_id = 0;
           u32 vp_id = 0;
           u32 val = 0;
 
           of_property_read_u32(child, "rockchip,plane-mask", &plane_mask);
           of_property_read_u32(child, "rockchip,primary-plane", &primary_plane_phy_id);
           of_property_read_u32(child, "reg", &vp_id);
 
           vop2->vps[vp_id].plane_mask = plane_mask;
           if (plane_mask)
               vop2->vps[vp_id].primary_plane_phy_id = primary_plane_phy_id;
           else
               vop2->vps[vp_id].primary_plane_phy_id = ROCKCHIP_VOP2_PHY_ID_INVALID;
 
           vop2->vps[vp_id].xmirror_en = of_property_read_bool(child, "xmirror-enable");
 
           ret = of_clk_set_defaults(child, false);
           if (ret) {
               DRM_DEV_ERROR(dev, "Failed to set clock defaults %d\n", ret);
               return ret;
           }
 
           mcu_timing_node = of_get_child_by_name(child, "mcu-timing");
           if (mcu_timing_node) {
               if (!of_property_read_u32(mcu_timing_node, "mcu-pix-total", &val))
                   vop2->vps[vp_id].mcu_timing.mcu_pix_total = val;
               if (!of_property_read_u32(mcu_timing_node, "mcu-cs-pst", &val))
                   vop2->vps[vp_id].mcu_timing.mcu_cs_pst = val;
               if (!of_property_read_u32(mcu_timing_node, "mcu-cs-pend", &val))
                   vop2->vps[vp_id].mcu_timing.mcu_cs_pend = val;
               if (!of_property_read_u32(mcu_timing_node, "mcu-rw-pst", &val))
                   vop2->vps[vp_id].mcu_timing.mcu_rw_pst = val;
               if (!of_property_read_u32(mcu_timing_node, "mcu-rw-pend", &val))
                   vop2->vps[vp_id].mcu_timing.mcu_rw_pend = val;
               if (!of_property_read_u32(mcu_timing_node, "mcu-hold-mode", &val))
                   vop2->vps[vp_id].mcu_timing.mcu_hold_mode = val;
           }
       }
 
       if (!vop2_plane_mask_check(vop2)) {
           DRM_WARN("use default plane mask\n");
           vop2_plane_mask_assign(vop2, vop_out_node);
       }
 
       for (i = 0; i < vop2->data->nr_vps; i++) {
           DRM_DEV_INFO(dev, "vp%d assign plane mask: 0x%x, primary plane phy id: %d\n",
                    i, vop2->vps[i].plane_mask,
                    vop2->vps[i].primary_plane_phy_id);
       }
   }
 
   vop2_extend_clk_init(vop2);
   spin_lock_init(&vop2->reg_lock);
   spin_lock_init(&vop2->irq_lock);
   mutex_init(&vop2->vop2_lock);
 
   if (vop2->version == VOP_VERSION_RK3528) {
       atomic_set(&vop2->vps[1].post_buf_empty_flag, 0);
       vop2->workqueue = create_workqueue("post_buf_empty_wq");
       INIT_WORK(&vop2->post_buf_empty_work, post_buf_empty_work_event);
   }
 
   ret = devm_request_irq(dev, vop2->irq, vop2_isr, IRQF_SHARED, dev_name(dev), vop2);
   if (ret)
       return ret;
 
   vop2_dsc_data_init(vop2);
 
   registered_num_crtcs = vop2_create_crtc(vop2);
   if (registered_num_crtcs <= 0)
       return -ENODEV;
 
   ret = vop2_gamma_init(vop2);
   if (ret)
       return ret;
   vop2_clk_init(vop2);
   vop2_cubic_lut_init(vop2);
   vop2_wb_connector_init(vop2, registered_num_crtcs);
   pm_runtime_enable(&pdev->dev);
 
   return 0;
}
 
static void vop2_unbind(struct device *dev, struct device *master, void *data)
{
   struct vop2 *vop2 = dev_get_drvdata(dev);
   struct drm_device *drm_dev = vop2->drm_dev;
   struct list_head *plane_list = &drm_dev->mode_config.plane_list;
   struct list_head *crtc_list = &drm_dev->mode_config.crtc_list;
   struct drm_crtc *crtc, *tmpc;
   struct drm_plane *plane, *tmpp;
 
   pm_runtime_disable(dev);
 
   list_for_each_entry_safe(plane, tmpp, plane_list, head)
       drm_plane_cleanup(plane);
 
   list_for_each_entry_safe(crtc, tmpc, crtc_list, head)
       vop2_destroy_crtc(crtc);
 
   vop2_wb_connector_destory(vop2);
}
 
const struct component_ops vop2_component_ops = {
   .bind = vop2_bind,
   .unbind = vop2_unbind,
};
EXPORT_SYMBOL_GPL(vop2_component_ops);