summaryrefslogtreecommitdiff
path: root/drivers/video/fbdev/mxc/imx_dcss.c
blob: b065956335f39b220eefb7fae41b79353e118d4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
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
/*
 * Copyright 2017 NXP
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/cache.h>
#include <asm/cacheflush.h>
#include <asm/uaccess.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/irqdesc.h>
#include <linux/fb.h>
#include <linux/freezer.h>
#include <linux/kfifo.h>
#include <linux/kthread.h>
#include <linux/log2.h>
#include <linux/regulator/consumer.h>
#include <linux/scatterlist.h>
#include <linux/videodev2.h>
#include <video/mxc_edid.h>
#include <linux/workqueue.h>

#include "mxc_dispdrv.h"
#include "imx_dcss_table.h"

/* sub engines address start offset */
#define HDR_CHAN1_START		0x00000
#define HDR_CHAN2_START		0x04000
#define HDR_CHAN3_START		0x08000
#define HDR_OUT_START		0x0C000
#define DOLBY_VISION_START	0x10000
#define DOLBY_GRAPHIC_START	0x11000
#define DOLBY_OUTPUT_START	0x12000
#define DEC400D_CHAN1_START	0x15000
#define DTRC_CHAN2_START	0x16000
#define DTRC_CHAN3_START	0x17000
#define DPR_CHAN1_START		0x18000
#define DPR_CHAN2_START		0x19000
#define DPR_CHAN3_START		0x1A000
#define SUBSAM_START		0x1B000
#define SCALER_CHAN1_START	0x1C000
#define SCALER_CHAN2_START	0x1C400
#define SCALER_CHAN3_START	0x1C800
#define DTG_START		0x20000
#define WR_SCL_START		0x21000
#define RD_SRC_START		0x22000
#define CTX_LD_START		0x23000
#define LUT_LD_START		0x24000
#define IRQ_STEER_START		0x2D000
#define LPCG_START		0x2E000
#define BLK_CTRL_START		0x2F000

#define DCSS_MODE_SCALE_EN	(1 << 0)
#define DCSS_MODE_CSC_EN	(1 << 1)
#define DCSS_MODE_RESOLVE_EN	(1 << 2)
#define DCSS_MODE_DECOMPRESS_EN	(1 << 3)
#define DCSS_MODE_HDR10_EN	(1 << 4)
#define DCSS_MODE_DOLBY_EN	(1 << 5)

/* define several clocks rate */
#define DISP_AXI_RATE		800000000
#define DISP_APB_RATE		133000000
#define DISP_RTRAM_RATE		400000000
#define DISP_PIXEL_RATE		100000000

#define TILE_TYPE_LINEAR	0x0
#define TILE_TYPE_GPU_STANDARD	0x1
#define TILE_TYPE_GPU_SUPER	0x2
#define TILE_TYPE_VPU_2PYUV420	0x3
#define TILE_TYPE_VPU_2PVP9	0x4

#define PIXEL_STORE_NONCOMPRESS	0x1
#define PIXEL_STORE_COMPRESS	0x2

#define CSC_MODE_BYPASS		0x0
#define CSC_MODE_YUV2RGB	0x1
#define CSC_MODE_RGB2YUV	0x2

#define HSYNC_ACTIVE_HIGH	0x1
#define VSYNC_ACTIVE_HIGH	0x1
#define DE_ACTIVE_HIGH		0x1

/* pixel format macros */
#define PIX_FMT_A8R8G8B8	0x1
#define PIX_FMT_A2R10G10B10	0x2
#define PIX_FMT_1PYUV422	0x3
#define PIX_FMT_2PYUV420_8	0x4
#define PIX_FMT_2PYUV420_10	0x5
#define PIX_FMT_2PVP9_8		0x6
#define PIX_FMT_2PVP9_10	0x7
#define PIX_FMT_AYUV444		0x8

/* more formats fourcc define */
#define V4L2_PIX_FMT_A2R10G10B10 v4l2_fourcc('B', 'A', '3', '0') /* 32 ARGB-2-10-10-10 */

#define MAX_WIDTH		4096
#define MAX_HEIGHT		4096

#define RED			0
#define GREEN			1
#define BLUE			2
#define TRANSP			3

#define CTXLD_TYPE_DB		0x1
#define CTXLD_TYPE_SB		0x2

#define DCSS_REGS_SIZE		0x30000
#define DCSS_CFIFO_SIZE		0x100000			/* power of 2 */
#define DCSS_IRQS_NUM		32

/* define registers offset */
#define CTXLD_CTRL_STATUS	0x0
#define CTXLD_CTRL_STATUS_SET	0x4
#define CTXLD_CTRL_STATUS_CLR	0x8
#define CTXLD_CTRL_STATUS_TOG	0xC

#define CTXLD_DB_BASE_ADDR	0x10
#define CTXLD_DB_COUNT		0x14
#define CTXLD_SB_BASE_ADDR	0x18
#define CTXLD_SB_COUNT		0x1C

#define TC_LINE1_INT		0x50
#define TC_INTERRUPT_STATUS	0x5C
#define TC_INTERRUPT_CONTROL	0x60
#define TC_INTERRUPT_MASK	0x68

/* define dcss state */
#define DCSS_STATE_RESET	0x0
#define DCSS_STATE_RUNNING	0x1
#define DCSS_STATE_STOP		0x2

/* io memory blocks number */
#define IORESOURCE_MEM_NUM	0x2

#define USE_CTXLD		0x1

#define NAME_LEN		32

/* TODO: DCSS IRQs indexes, more added later */
#define IRQ_DPR_CH1		3
#define IRQ_DPR_CH2		4
#define IRQ_DPR_CH3		5
#define IRQ_CTX_LD		6
#define IRQ_TC_LINE1		8
#define IRQ_DEC400D_CH1		15
#define IRQ_DTRC_CH2		16
#define IRQ_DTRC_CH3		17

/* ctxld irqs status */
#define RD_ERR			(1 << 16)
#define DB_COMP			(1 << 17)
#define SB_HP_COMP		(1 << 18)
#define SB_LP_COMP		(1 << 19)
#define AHB_ERR			(1 << 22)

/* ctxld irqs mask */
#define RD_ERR_EN		(1 << 2)
#define DB_COMP_EN		(1 << 3)
#define SB_HP_COMP_EN		(1 << 4)
#define SB_LP_COMP_EN		(1 << 5)
#define AHB_ERR_EN		(1 << 8)

/* channels */
#define DCSS_CHAN_MAIN		0
#define DCSS_CHAN_SECONDARY	1
#define DCSS_CHAN_THIRD		2
/* all channels are disabled */
#define DCSS_CHAN_NULL		3

/**
 * kfifo_to_end_len - returns the size from 'out' to buffer end
 * this is a kfifo extend interface as required
 */
#define kfifo_to_end_len(fifo) (			\
{							\
	unsigned int ptr;				\
							\
	if (!(fifo)->kfifo.in)				\
		ptr = 0;				\
	else						\
		ptr = ((((fifo)->kfifo.in - 1) & (fifo)->kfifo.mask) + 1);	\
							\
	kfifo_size(fifo) - ptr;				\
}							\
)

/* TODO: */
struct coordinate {
	uint32_t x;
	uint32_t y;
};

struct rectangle {
	uint32_t ulc_x;
	uint32_t ulc_y;
	uint32_t lrc_x;
	uint32_t lrc_y;
};

struct pix_fmt_info {
	uint32_t fourcc;
	uint32_t bpp;
	bool is_yuv;
};

struct dcss_pixmap {
	uint32_t channel_id;
	uint32_t width;
	uint32_t height;
	uint32_t bits_per_pixel;
	uint32_t pitch;
	struct rectangle crop;		/* active area */
	uint32_t format;
	uint32_t tile_type;		/* see TILE_TYPE_* macros */
	uint32_t pixel_store;		/* see PIXEL_STORE_* macros */
	uint32_t flags;
	dma_addr_t paddr;
};

/* Display state format in DRAM used by CTX_LD */
struct ctxld_unit {
	uint32_t reg_value;
	uint32_t reg_offset;
};

/* ctxld buffer */
struct cbuffer{
	void *sb_addr;
	void *db_addr;
	uint32_t sb_len;	/* buffer length in elements */
	uint32_t db_len;
	uint32_t sb_data_len;	/* data length in elements   */
	uint32_t db_data_len;
	uint32_t esize;		/* size per element */
};

struct vsync_info {
	wait_queue_head_t vwait;
	unsigned long vcount;
};

struct ctxld_commit {
	struct list_head list;
	struct kref refcount;
	struct work_struct work;
	void *data;
	uint32_t sb_data_len;
	uint32_t sb_hp_data_len;
	uint32_t db_data_len;
	uint32_t sb_trig_pos;
	uint32_t db_trig_pos;
};

struct ctxld_fifo {
	uint32_t size;
	void *vaddr;
	dma_addr_t dma_handle;
	struct list_head ctxld_list;	/* manage context loader */
	DECLARE_KFIFO_PTR(fifo, struct ctxld_unit);
	struct scatterlist sgl[1];
	uint32_t sgl_num;
	struct workqueue_struct *ctxld_wq;
	/* synchronization in two points:
	 * a. simutanous fifo commits
	 * b. queue waiting for cfifo flush
	 */
	wait_queue_head_t cqueue;
	struct completion complete;
};

/* channel info: 3 channels in DCSS */
struct dcss_channel_info {
	uint32_t channel_id;
	uint32_t channel_en;		/* channel 1 enable by default */
	struct platform_device *pdev;
	struct fb_info *fb_info;
	struct dcss_pixmap input;
	struct rectangle ch_pos;	/* display position in dtg for one channel */
	struct cbuffer cb;
	uint32_t hdr10_in_addr;
	uint32_t decomp_addr;
	uint32_t dpr_addr;
	uint32_t scaler_addr;
	int blank;			/* see FB_BLANK_* macros */
	uint32_t csc_mode;		/* see CSC_MODE_* macros */
	bool dpr_scaler_en;		/* record dpr and scaler enabled or not */
	unsigned long update_stamp;	/* default is ~0x0UL */

	void *dev_data;			/* pointer to dcss_info */
};

struct dcss_channels {
	struct dcss_channel_info chan_info[3];
	uint32_t hdr10_out_addr;
	uint32_t subsam_addr;
	uint32_t dtg_addr;
	uint32_t wrscl_addr;
	uint32_t rdsrc_addr;
	uint32_t ctxld_addr;
	uint32_t lutld_addr;
	uint32_t hdmi_phy_addr;
	uint32_t irq_steer_addr;
	uint32_t lpcg_addr;
	uint32_t blk_ctrl_addr;
};

/* display info: output to monitor */
struct dcss_info {
	struct platform_device *pdev;
	void __iomem *base;
	void __iomem *blkctl_base;
	spinlock_t llock;		/* list lock: for ctxld_list */
	int irqs[DCSS_IRQS_NUM];
	uint32_t irqs_num;
	uint32_t dcss_state;		/* see DCSS_STATE_* macros */
	struct clk *clk_axi;
	struct clk *clk_apb;
	struct clk *clk_rtram;
	struct clk *clk_dtrc;
	struct clk *clk_pix;
	struct regulator *power;
	struct ctxld_fifo cfifo;
	struct task_struct *handler;
	struct dcss_pixmap *output;
	struct dcss_channels chans;	/* maximum 3 channels
					 * TODO: better change to layer
					 */
	const struct fb_videomode *dft_disp_mode;	/* Default display mode */
	uint32_t tile_type;		/* see TILE_TYPE_* macros */
	uint32_t pixel_store;		/* see PIXEL_STORE_* macros */
	uint32_t csc_mode;		/* see CSC_MODE_* macros */
	uint32_t mode_flags;		/* see DCSS_MODE_* macros */
	uint32_t sync_flags;		/* see FB_SYNC_* macros   */
	uint32_t hsync_pol;
	uint32_t vsync_pol;
	uint32_t de_pol;
	char disp_dev[NAME_LEN];
	struct mxc_dispdrv_handle *dispdrv;
	struct vsync_info vinfo;

	atomic_t flush;
};

const struct fb_videomode imx_cea_mode[100] = {
	/* #1: 640x480p@59.94/60Hz 4:3 */
	[1] = {
		NULL, 60, 640, 480, 39722, 48, 16, 33, 10, 96, 2, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_4_3, 0,
	},
	/* #2: 720x480p@59.94/60Hz 4:3 */
	[2] = {
		NULL, 60, 720, 480, 37037, 60, 16, 30, 9, 62, 6, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_4_3, 0,
	},
	/* #3: 720x480p@59.94/60Hz 16:9 */
	[3] = {
		NULL, 60, 720, 480, 37037, 60, 16, 30, 9, 62, 6, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #4: 1280x720p@59.94/60Hz 16:9 */
	[4] = {
		NULL, 60, 1280, 720, 13468, 220, 110, 20, 5, 40, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0
	},
	/* #5: 1920x1080i@59.94/60Hz 16:9 */
	[5] = {
		NULL, 60, 1920, 1080, 13763, 148, 88, 15, 2, 44, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_INTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #6: 720(1440)x480iH@59.94/60Hz 4:3 */
	[6] = {
		NULL, 60, 1440, 480, 18554/*37108*/, 114, 38, 15, 4, 124, 3, 0,
		FB_VMODE_INTERLACED | FB_VMODE_ASPECT_4_3, 0,
	},
	/* #7: 720(1440)x480iH@59.94/60Hz 16:9 */
	[7] = {
		NULL, 60, 1440, 480, 18554/*37108*/, 114, 38, 15, 4, 124, 3, 0,
		FB_VMODE_INTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #8: 720(1440)x240pH@59.94/60Hz 4:3 */
	[8] = {
		NULL, 60, 1440, 240, 37108, 114, 38, 15, 4, 124, 3, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_4_3, 0,
	},
	/* #9: 720(1440)x240pH@59.94/60Hz 16:9 */
	[9] = {
		NULL, 60, 1440, 240, 37108, 114, 38, 15, 4, 124, 3, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #14: 1440x480p@59.94/60Hz 4:3 */
	[14] = {
		NULL, 60, 1440, 480, 18500, 120, 32, 30, 9, 124, 6, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_4_3, 0,
	},
	/* #15: 1440x480p@59.94/60Hz 16:9 */
	[15] = {
		NULL, 60, 1440, 480, 18500, 120, 32, 30, 9, 124, 6, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #16: 1920x1080p@60Hz 16:9 */
	[16] = {
		NULL, 60, 1920, 1080, 6734, 148, 88, 36, 4, 44, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #17: 720x576pH@50Hz 4:3 */
	[17] = {
		NULL, 50, 720, 576, 37037, 68, 12, 39, 5, 64, 5, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_4_3, 0,
	},
	/* #18: 720x576pH@50Hz 16:9 */
	[18] = {
		NULL, 50, 720, 576, 37037, 68, 12, 39, 5, 64, 5, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #19: 1280x720p@50Hz */
	[19] = {
		NULL, 50, 1280, 720, 13468, 220, 440, 20, 5, 40, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #20: 1920x1080i@50Hz */
	[20] = {
		NULL, 50, 1920, 1080, 13480, 148, 528, 15, 5, 528, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_INTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #23: 720(1440)x288pH@50Hz 4:3 */
	[23] = {
		NULL, 50, 1440, 288, 37037, 138, 24, 19, 2, 126, 3, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_4_3, 0,
	},
	/* #24: 720(1440)x288pH@50Hz 16:9 */
	[24] = {
		NULL, 50, 1440, 288, 37037, 138, 24, 19, 2, 126, 3, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #29: 720(1440)x576pH@50Hz 4:3 */
	[29] = {
		NULL, 50, 1440, 576, 18518, 136, 24, 39, 5, 128, 5, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_4_3, 0,
	},
	/* #30: 720(1440)x576pH@50Hz 16:9 */
	[30] = {
		NULL, 50, 1440, 576, 18518, 136, 24, 39, 5, 128, 5, 0,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #31: 1920x1080p@50Hz */
	[31] = {
		NULL, 50, 1920, 1080, 6734, 148, 528, 36, 4, 44, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #32: 1920x1080p@23.98/24Hz */
	[32] = {
		NULL, 24, 1920, 1080, 13468, 148, 638, 36, 4, 44, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #33: 1920x1080p@25Hz */
	[33] = {
		NULL, 25, 1920, 1080, 13468, 148, 528, 36, 4, 44, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #34: 1920x1080p@30Hz */
	[34] = {
		NULL, 30, 1920, 1080, 13468, 148, 88, 36, 4, 44, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0,
	},
	/* #41: 1280x720p@100Hz 16:9 */
	[41] = {
		NULL, 100, 1280, 720, 6734, 220, 440, 20, 5, 40, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0
	},
	/* #47: 1280x720p@119.88/120Hz 16:9 */
	[47] = {
		NULL, 120, 1280, 720, 6734, 220, 110, 20, 5, 40, 5,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0
	},
	/* #95: 3840x2160p@30Hz 16:9 */
	[95] = {
		NULL, 30, 3840, 2160, 3367, 296, 176, 72, 8, 88, 10,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0
	},
	/* #97: 3840x2160p@60Hz 16:9 */
	[97] = {
		NULL, 30, 3840, 2160, 1684, 296, 176, 72, 8, 88, 10,
		FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
		FB_VMODE_NONINTERLACED | FB_VMODE_ASPECT_16_9, 0
	},
};

static const struct of_device_id dcss_dt_ids[] ={
	{ .compatible = "fsl,imx8mq-dcss", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, dcss_dt_ids);

static void dcss_free_fbmem(struct fb_info *fbi);
static int dcss_open(struct fb_info *fbi, int user);
static int dcss_check_var(struct fb_var_screeninfo *var,
			  struct fb_info *fbi);
static int dcss_set_par(struct fb_info *fbi);
static int dcss_setcolreg(unsigned regno, unsigned red, unsigned green,
		unsigned blue, unsigned transp, struct fb_info *info);
static int dcss_blank(int blank, struct fb_info *fbi);
static int dcss_pan_display(struct fb_var_screeninfo *var,
			    struct fb_info *fbi);
static int dcss_ioctl(struct fb_info *fbi, unsigned int cmd,
		      unsigned long arg);
static int vcount_compare(unsigned long vcount,
			  struct vsync_info *vinfo);
static int dcss_wait_for_vsync(unsigned long crtc,
			       struct dcss_info *info);
static void flush_cfifo(struct ctxld_fifo *cfifo,
			struct work_struct *work);

static struct fb_ops dcss_ops = {
	.owner = THIS_MODULE,
        .fb_open        = dcss_open,
	.fb_check_var	= dcss_check_var,
	.fb_set_par	= dcss_set_par,
	.fb_setcolreg	= dcss_setcolreg,
	.fb_blank	= dcss_blank,
	.fb_pan_display	= dcss_pan_display,
	.fb_ioctl	= dcss_ioctl,
	.fb_fillrect	= cfb_fillrect,
	.fb_copyarea	= cfb_copyarea,
	.fb_imageblit	= cfb_imageblit,
};

/* TODO: more added later */
static const struct pix_fmt_info formats[] = {
	{
		.fourcc = V4L2_PIX_FMT_ARGB32,
		.bpp = 32,
		.is_yuv = false,
	}, {
		.fourcc = V4L2_PIX_FMT_A2R10G10B10,
		.bpp = 32,
		.is_yuv = false,
	}, {
		.fourcc = V4L2_PIX_FMT_YUYV,
		.bpp = 16,
		.is_yuv = true,
	}, {
		.fourcc = V4L2_PIX_FMT_YVYU,
		.bpp = 16,
		.is_yuv = true,
	}, {
		.fourcc = V4L2_PIX_FMT_UYVY,
		.bpp = 16,
		.is_yuv = true,
	}, {
		.fourcc = V4L2_PIX_FMT_VYUY,
		.bpp = 16,
		.is_yuv = true,
	}, {
		.fourcc = V4L2_PIX_FMT_YUV32,
		.bpp = 32,
		.is_yuv = true,
	}, {
		.fourcc = V4L2_PIX_FMT_NV12,
		.bpp = 8,
		.is_yuv = true,
	},
};

static const struct fb_bitfield def_a8r8g8b8[] = {
	[RED] = {
		.offset = 16,
		.length = 8,
		.msb_right = 0,
	},
	[GREEN] = {
		.offset = 8,
		.length = 8,
		.msb_right = 0,
	},
	[BLUE] = {
		.offset = 0,
		.length = 8,
		.msb_right = 0,
	},
	[TRANSP] = {
		.offset = 24,
		.length = 8,
		.msb_right = 0,
	}
};

static const struct fb_bitfield def_a2r10g10b10[] = {
	[RED] = {
		.offset = 20,
		.length = 10,
	},
	[GREEN] = {
		.offset = 10,
		.length = 10,
	},
	[BLUE] = {
		.offset = 0,
		.length = 10,
	},
	[TRANSP] = {
		.offset = 30,
		.length = 2,
	}
};

static const struct pix_fmt_info *get_fmt_info(uint32_t fourcc)
{
	uint32_t i;

	for (i = 0; i < ARRAY_SIZE(formats); i++) {
		if (formats[i].fourcc == fourcc)
			return &formats[i];
	}

	return NULL;
}

static int fmt_is_yuv(uint32_t fourcc)
{
        switch (fourcc) {
        case V4L2_PIX_FMT_ARGB32:
        case V4L2_PIX_FMT_A2R10G10B10:
                return 0;
        case V4L2_PIX_FMT_YUYV:
        case V4L2_PIX_FMT_YVYU:
        case V4L2_PIX_FMT_UYVY:
        case V4L2_PIX_FMT_VYUY:
        case V4L2_PIX_FMT_YUV32:
                return 1;
        case V4L2_PIX_FMT_NV12:
                return 2;
        default:
                return -EINVAL;
        }
}

/* TODO: writel ? */
#define fill_unit(uint, offset, value) {		\
	unit->reg_value  = value;			\
	unit->reg_offset = offset;			\
}

static void fill_sb(struct cbuffer *cb,
		    uint32_t offset,
		    uint32_t value)
{
	struct ctxld_unit *unit = NULL;

	BUG_ON(!cb);

	if (unlikely(cb->sb_data_len == cb->sb_len)) {
		/* sb is full */
		BUG_ON(1);
	}

	unit = (struct ctxld_unit *)(cb->sb_addr + cb->sb_data_len * cb->esize);

	fill_unit(unit, offset, value);
	cb->sb_data_len++;
}

static void __maybe_unused fill_db(struct cbuffer *cb,
				   uint32_t offset,
				   uint32_t value)
{
	struct ctxld_unit *unit = NULL;

	BUG_ON(!cb);

	if (unlikely(cb->db_data_len == cb->db_len)) {
		/* db is full */
		BUG_ON(1);
	}

	unit = (struct ctxld_unit *)(cb->db_addr + cb->db_data_len * cb->esize);

	fill_unit(unit, offset, value);
	cb->db_data_len++;
}

static void ctxld_fifo_info_print(struct ctxld_fifo *cfifo)
{
	pr_debug("%s: print kfifo info: **********\n", __func__);
	pr_debug("in = 0x%x, out = 0x%x, mask = 0x%x\n",
		 cfifo->fifo.kfifo.in,
		 cfifo->fifo.kfifo.out,
		 cfifo->fifo.kfifo.mask);
}

static int ctxld_fifo_alloc(struct device *dev,
			    struct ctxld_fifo *cfifo,
			    uint32_t fifo_size)
{
	if (!cfifo || fifo_size < 2)
		return -EINVAL;

	fifo_size = roundup_pow_of_two(fifo_size);
	cfifo->vaddr = dma_alloc_coherent(dev, fifo_size,
					  &cfifo->dma_handle,
					  GFP_DMA | GFP_KERNEL);
	if (!cfifo->vaddr) {
		dev_err(dev, "allocate ctxld fifo failed\n");
		return -ENOMEM;
	}

	cfifo->size = fifo_size;
	kfifo_init(&cfifo->fifo, cfifo->vaddr, fifo_size);

	/* TODO: sgl num can be changed if required */
	cfifo->sgl_num = 1;

	INIT_LIST_HEAD(&cfifo->ctxld_list);
	init_waitqueue_head(&cfifo->cqueue);
	init_completion(&cfifo->complete);

	return 0;
}

static void ctxld_fifo_free(struct device *dev,
			    struct ctxld_fifo *cfifo)
{
	if (!cfifo)
		return;

	/* TODO: wait fifo flush empty */

	kfifo_reset(&cfifo->fifo);

	dma_free_coherent(dev, cfifo->size,
			  cfifo->vaddr,
			  cfifo->dma_handle);

	cfifo->size  = 0;
	cfifo->vaddr = NULL;
	cfifo->dma_handle = 0;

	memset(cfifo->sgl, 0x0, sizeof(*cfifo->sgl));
	cfifo->sgl_num = 0;
}

static int dcss_clks_get(struct dcss_info *info)
{
	int ret = 0;
	struct platform_device *pdev = info->pdev;

	info->clk_axi = devm_clk_get(&pdev->dev, "axi");
	if (IS_ERR(info->clk_axi)) {
		ret = PTR_ERR(info->clk_axi);
		goto out;
	}

	info->clk_apb = devm_clk_get(&pdev->dev, "apb");
	if (IS_ERR(info->clk_apb)) {
		ret = PTR_ERR(info->clk_apb);
		goto put_axi;
	}

	info->clk_rtram = devm_clk_get(&pdev->dev, "rtram");
	if (IS_ERR(info->clk_rtram)) {
		ret = PTR_ERR(info->clk_rtram);
		goto put_apb;
	}

	info->clk_dtrc = devm_clk_get(&pdev->dev, "dtrc");
	if (IS_ERR(info->clk_dtrc)) {
		ret = PTR_ERR(info->clk_dtrc);
		goto put_rtram;
	}

	info->clk_pix = devm_clk_get(&pdev->dev, "pix");
	if (IS_ERR(info->clk_pix)) {
		ret = PTR_ERR(info->clk_pix);
		goto put_dtrc;
	}

	goto out;

put_dtrc:
	devm_clk_put(&pdev->dev, info->clk_dtrc);
put_rtram:
	devm_clk_put(&pdev->dev, info->clk_rtram);
put_apb:
	devm_clk_put(&pdev->dev, info->clk_apb);
put_axi:
	devm_clk_put(&pdev->dev, info->clk_axi);
out:
	return ret;
}

#if 0
static void dcss_clks_put(struct dcss_info *info)
{
	struct platform_device *pdev = info->pdev;

	devm_clk_put(&pdev->dev, info->clk_axi);
	devm_clk_put(&pdev->dev, info->clk_apb);
	devm_clk_put(&pdev->dev, info->clk_rtram);
	devm_clk_put(&pdev->dev, info->clk_dtrc);
	devm_clk_put(&pdev->dev, info->clk_pix);
}
#endif

static void fb_var_to_pixmap(struct dcss_pixmap *pixmap,
			     struct fb_var_screeninfo *var)
{
	BUG_ON(!pixmap || !var);

	pixmap->width  = var->xres;
	pixmap->height = var->yres;
	pixmap->bits_per_pixel = var->bits_per_pixel;
	pixmap->pitch  = var->width * (var->bits_per_pixel >> 3);
	pixmap->crop.ulc_x = 0;
	pixmap->crop.ulc_y = 0;
	pixmap->crop.lrc_x = var->xres;
	pixmap->crop.lrc_y = var->yres;
	pixmap->format = var->grayscale;

	/* TODO possible passed through 'reserved' ? */
	pixmap->tile_type = TILE_TYPE_LINEAR;
	pixmap->pixel_store = PIXEL_STORE_NONCOMPRESS;
}

static int fill_one_chan_info(uint32_t chan_id,
			      struct dcss_channel_info *info)
{
	void *dsb;	/* mem to store ctxld units */
	struct cbuffer *cb;
	struct platform_device *pdev;

	BUG_ON(!info || IS_ERR(info));
	pdev = info->pdev;

	if (chan_id > 2) {
		dev_err(&pdev->dev, "incorrect channel number: %d\n", chan_id);
		return -EINVAL;
	}

	info->channel_id = chan_id;
	info->channel_en = (chan_id == 0) ? 1 : 0;
	info->blank = FB_BLANK_NORMAL;
	info->update_stamp = ~0x0UL;

	switch (chan_id) {
	case 0:
		info->hdr10_in_addr = HDR_CHAN1_START;
		info->decomp_addr = DEC400D_CHAN1_START;
		info->dpr_addr = DPR_CHAN1_START;
		info->scaler_addr = SCALER_CHAN1_START;
		break;
	case 1:
		info->hdr10_in_addr = HDR_CHAN2_START;
		info->decomp_addr = DTRC_CHAN2_START;
		info->dpr_addr = DPR_CHAN2_START;
		info->scaler_addr = SCALER_CHAN2_START;
		break;
	case 2:
		info->hdr10_in_addr = HDR_CHAN3_START;
		info->decomp_addr = DTRC_CHAN3_START;
		info->dpr_addr = DPR_CHAN3_START;
		info->scaler_addr = SCALER_CHAN3_START;
		break;
	default:
		return -EINVAL;
	}

	/* dsb is used to hold double & single buffer units */
	dsb = devm_kzalloc(&pdev->dev, DCSS_REGS_SIZE << 1, GFP_KERNEL);
	if (!dsb)
		return -ENOMEM;

	/* init cbuffer struct */
	cb = &info->cb;
	cb->esize = sizeof(struct ctxld_unit);

	cb->sb_addr = dsb;
	cb->sb_len  = DCSS_REGS_SIZE / cb->esize;
	cb->sb_data_len = 0x0;

	cb->db_addr = dsb + DCSS_REGS_SIZE;
	cb->db_len  = DCSS_REGS_SIZE / cb->esize;
	cb->db_data_len = 0x0;

	return 0;
}

/* allocate fb info for one channel */
static int alloc_one_fbinfo(struct dcss_channel_info *cinfo)
{
	struct platform_device *pdev;

	BUG_ON(!cinfo || IS_ERR(cinfo));

	pdev = cinfo->pdev;
	if (!pdev)
		return -ENODEV;

	cinfo->fb_info = framebuffer_alloc(0, &pdev->dev);
	if (!cinfo->fb_info) {
		dev_err(&pdev->dev, "failed to alloc fb info for channel %d\n",
			cinfo->channel_id);
		return -ENOMEM;
	}

	cinfo->fb_info->par = cinfo;
	INIT_LIST_HEAD(&cinfo->fb_info->modelist);

	return 0;
}

static struct fb_info *get_one_fbinfo(uint32_t ch_id,
				      struct dcss_channels *chans)
{
	struct dcss_channel_info *cinfo;

	if (ch_id > 2)
		return NULL;

	cinfo = &chans->chan_info[ch_id];

	return cinfo->fb_info;
}

static int init_chan_pixmap(struct dcss_channel_info *cinfo)
{
	struct dcss_pixmap *pixmap;
	struct fb_info *fbi;
	struct fb_var_screeninfo *var;

	BUG_ON(!cinfo || IS_ERR(cinfo));

	pixmap = &cinfo->input;
	fbi = cinfo->fb_info;
	var = &fbi->var;

	fb_var_to_pixmap(pixmap, var);

	return 0;
}

static int init_ch_pos(struct dcss_channel_info *cinfo)
{
	struct rectangle *pos;
	struct fb_info *fbi;
	struct fb_var_screeninfo *var;

	/* TODO: init ch_pos with var temporarily */
	pos = &cinfo->ch_pos;
	fbi = cinfo->fb_info;
	var = &fbi->var;

	pos->ulc_x = var->left_margin + var->hsync_len - 1;
	pos->ulc_y = var->upper_margin + var->lower_margin +
		     var->vsync_len - 1;
	pos->lrc_x = var->xres + var->left_margin +
		     var->hsync_len - 1;
	pos->lrc_y = var->yres + var->upper_margin +
		     var->lower_margin + var->vsync_len - 1;

	return 0;
}

static int dcss_init_chans(struct dcss_info *info)
{
	struct dcss_channels *dcss_chans = &info->chans;

	/* init sharable info between chans */
	dcss_chans->hdr10_out_addr = HDR_OUT_START;
	dcss_chans->subsam_addr = SUBSAM_START;
	dcss_chans->dtg_addr   = DTG_START;
	dcss_chans->wrscl_addr = WR_SCL_START;
	dcss_chans->rdsrc_addr = RD_SRC_START;
	dcss_chans->ctxld_addr = CTX_LD_START;
	dcss_chans->lutld_addr = LUT_LD_START;
	dcss_chans->hdmi_phy_addr  = 0;
	dcss_chans->irq_steer_addr = IRQ_STEER_START;
	dcss_chans->lpcg_addr = LPCG_START;
	dcss_chans->blk_ctrl_addr  = BLK_CTRL_START;

	return 0;
}

static int dcss_init_fbinfo(struct fb_info *fbi)
{
	int ret;
	uint32_t luma_size;
	struct dcss_channel_info *cinfo = fbi->par;
	struct dcss_info *info = cinfo->dev_data;
	struct fb_fix_screeninfo *fix = &fbi->fix;
	struct fb_var_screeninfo *var = &fbi->var;
	struct fb_modelist *modelist;

	fbi->fbops = &dcss_ops;

	/* init fix screeninfo */
	fix->type = FB_TYPE_PACKED_PIXELS;
	fix->ypanstep  = 1;
	fix->ywrapstep = 1;
	fix->visual = FB_VISUAL_TRUECOLOR;
	fix->accel = FB_ACCEL_NONE;

	ret = fb_add_videomode(info->dft_disp_mode, &fbi->modelist);
	if (ret)
		return ret;

	/* init var screeninfo */
	modelist = list_first_entry(&fbi->modelist,
			struct fb_modelist, list);
	fb_videomode_to_var(var, &modelist->mode);
	var->nonstd = 0;
	var->activate = FB_ACTIVATE_NOW;
	var->vmode = FB_VMODE_NONINTERLACED;
	/* default format */
	if (cinfo->channel_id == DCSS_CHAN_MAIN)
		/* main channel is for graphic */
		var->grayscale = V4L2_PIX_FMT_ARGB32;
	else
		/* other channels are for video */
		var->grayscale = V4L2_PIX_FMT_NV12;

	/* Allocate memory buffer: Maybe need alignment */
	fix->smem_len = (fix->line_length * var->yres_virtual > SZ_32M) ?
			 fix->line_length * var->yres_virtual : SZ_32M;
	fbi->screen_base = dma_alloc_writecombine(fbi->device, fix->smem_len,
						  (dma_addr_t *)&fix->smem_start,
						  GFP_DMA | GFP_KERNEL);
	if (!fbi->screen_base) {
		dev_err(fbi->device, "Unable to alloc fb memory\n");
		fix->smem_len = 0;
		fix->smem_start = 0;
		return -ENOMEM;
	}
	dev_dbg(fbi->device, "%s: smem_start = 0x%lx, screen_base = 0x%p\n",
			      __func__, fix->smem_start, fbi->screen_base);

	fbi->screen_size = fix->smem_len;

	fbi->pseudo_palette = devm_kzalloc(fbi->device,
					   sizeof(u32) * 16, GFP_KERNEL);
	if (!fbi->pseudo_palette) {
		dev_err(fbi->device, "alloc pseudo_palette failed\n");
		dcss_free_fbmem(fbi);
		return -ENOMEM;
	}

	/* clear screen content to black */
	switch (var->grayscale) {
	case V4L2_PIX_FMT_ARGB32:
		memset((void*)fbi->screen_base, 0x0, fix->smem_len);
		break;
	case V4L2_PIX_FMT_NV12:
		/* set luma: 0x0 */
		luma_size = var->xres * var->yres;
		memset((void*)fbi->screen_base, 0x0, luma_size);
		/* set chroma: 0x80 */
		memset((void*)fbi->screen_base + luma_size, 0x80, luma_size);
		break;
	default:
		return -EINVAL;
	}

	if (dcss_check_var(var, fbi)) {
		devm_kfree(fbi->device, fbi->pseudo_palette);
		dcss_free_fbmem(fbi);
		return -EINVAL;
	}

	return 0;
}

static void dcss_free_fbmem(struct fb_info *fbi)
{
	struct fb_fix_screeninfo *fix = &fbi->fix;

	dma_free_writecombine(fbi->device, fix->smem_len,
			      fbi->screen_base,
			      (dma_addr_t)fix->smem_start);

	fbi->screen_base = NULL;
	fix->smem_start  = 0;
	fix->smem_len    = 0;
}

static int dcss_clks_enable(struct dcss_info *info)
{
	int ret = 0;
	struct platform_device *pdev = info->pdev;

	/* TODO: Add return value check */
	ret = clk_prepare_enable(info->clk_axi);
	if (ret) {
		dev_err(&pdev->dev, "enable axi clock failed\n");
		return ret;
	}

	ret = clk_prepare_enable(info->clk_apb);
	if (ret) {
		dev_err(&pdev->dev, "enable apb clock failed\n");
		goto disable_axi;
	}

	ret = clk_prepare_enable(info->clk_rtram);
	if (ret) {
		dev_err(&pdev->dev, "enable rtram clock failed\n");
		goto disable_apb;
	}

	ret = clk_prepare_enable(info->clk_dtrc);
	if (ret) {
		dev_err(&pdev->dev, "enable dtrc clock failed\n");
		goto disable_rtram;
	}

	ret = clk_prepare_enable(info->clk_pix);
	if (ret) {
		dev_err(&pdev->dev, "enable pix clock failed\n");
		goto disable_dtrc;
	}

	goto out;

disable_dtrc:
	clk_disable_unprepare(info->clk_dtrc);
disable_rtram:
	clk_disable_unprepare(info->clk_rtram);
disable_apb:
	clk_disable_unprepare(info->clk_apb);
disable_axi:
	clk_disable_unprepare(info->clk_axi);
out:
	return ret;
}

#if 0
static void dcss_clks_disable(struct dcss_info *info)
{
	clk_disable_unprepare(info->clk_axi);
	clk_disable_unprepare(info->clk_apb);
	clk_disable_unprepare(info->clk_rtram);
	clk_disable_unprepare(info->clk_dtrc);
	clk_disable_unprepare(info->clk_pix);
}
#endif

static int dcss_clks_rate_set(struct dcss_info *info)
{
	int ret;
	uint32_t pix_clk_rate;
	struct platform_device *pdev  = info->pdev;

	/* TODO: axi, abp, rtrm clock rate are set by uboot already */
	ret = clk_set_rate(info->clk_axi, DISP_AXI_RATE);
	if (ret) {
		dev_err(&pdev->dev, "set axi clock rate failed\n");
		return ret;
	}

	ret = clk_set_rate(info->clk_apb, DISP_APB_RATE);
	if (ret) {
		dev_err(&pdev->dev, "set apb clock rate failed\n");
		return ret;
	}

	ret = clk_set_rate(info->clk_rtram, DISP_RTRAM_RATE);
	if (ret) {
		dev_err(&pdev->dev, "set rtram clock rate failed\n");
		return ret;
	}

	pix_clk_rate = PICOS2KHZ(info->dft_disp_mode->pixclock) * 1000U;
	dev_dbg(&pdev->dev, "%s: pix clock rate = %u\n", __func__, pix_clk_rate);

	ret = clk_set_rate(info->clk_pix, pix_clk_rate);
	if (ret) {
		dev_err(&pdev->dev, "set pixel clock rate failed, ret = %d\n", ret);
		return ret;
	}

	return 0;
}

static int dcss_dec400d_config(struct dcss_info *info,
			       bool decompress, bool resolve)
{
	struct dcss_channel_info *chan_info;
	struct cbuffer *cb;

	if (resolve == true)
		return -EINVAL;

	/* dec400d always in channel 1 */
	chan_info = &info->chans.chan_info[0];
	cb = &chan_info->cb;

	if (decompress == true) {
		/* TODO: configure decompress */
		;
	} else {
		/* TODO: configure bypass */
		;
	}

	return 0;
}

static int dcss_dtrc_config(uint32_t dtrc_ch,
			    struct dcss_info *info,
			    bool decompress,
			    bool resolve)
{
	struct platform_device *pdev = info->pdev;
	struct dcss_channel_info *chan_info;
	struct cbuffer *cb;

	if (dtrc_ch != 1 && dtrc_ch != 2) {
		dev_err(&pdev->dev, "invalid dtrc channel number\n");
		return -EINVAL;
	}

	chan_info = &info->chans.chan_info[dtrc_ch];
	cb = &chan_info->cb;

	if (!decompress && !resolve) {
#if USE_CTXLD
		/* Bypass DTRC */
		fill_sb(cb, chan_info->decomp_addr + 0xc8, 0x2);
#else
		writel(0x2, info->base + chan_info->decomp_addr + 0xc8);
#endif
	} else {
		/* TODO: decompress & resolve config */
		;
	}

	return 0;
}

static int dcss_decomp_config(uint32_t decomp_ch, struct dcss_info *info)
{
	bool need_decomp, need_resolve;
	struct platform_device *pdev = info->pdev;
	struct dcss_channel_info *chan_info;
	struct dcss_pixmap *input;

	if (decomp_ch > 2) {
		dev_err(&pdev->dev, "invalid decompression channel number\n");
		return -EINVAL;
	}

	chan_info = &info->chans.chan_info[decomp_ch];
	input = &chan_info->input;

	switch (input->pixel_store) {
	case PIXEL_STORE_NONCOMPRESS:
		need_decomp = false;
		break;
	case PIXEL_STORE_COMPRESS:
		need_decomp = true;
		break;
	default:
		dev_err(&pdev->dev, "invalid pixel store type\n");
		return -EINVAL;
	}

	switch (input->tile_type) {
	case TILE_TYPE_LINEAR:
	case TILE_TYPE_GPU_STANDARD:
	case TILE_TYPE_GPU_SUPER:
		need_resolve = false;
		break;
	case TILE_TYPE_VPU_2PYUV420:
	case TILE_TYPE_VPU_2PVP9:
		need_resolve = true;
		break;
	default:
		dev_err(&pdev->dev, "invalid buffer tile type\n");
		return -EINVAL;
	}

	switch (decomp_ch) {
	case 0:		/* DEC400D */
		dcss_dec400d_config(info, need_decomp, need_resolve);
		break;
	case 1:		/* DTRC1   */
	case 2:		/* DTRC2   */
		dcss_dtrc_config(decomp_ch, info,
				 need_decomp, need_resolve);
		break;
	default:
		dev_err(&pdev->dev, "invalid ch num = %d\n", decomp_ch);
		break;
	}

	return 0;
}

/* for both luma and chroma
 */
static int dpr_pix_x_calc(u32 pix_size,
			  u32 width,
			  u32 tile_type)
{
	unsigned int num_pix_x_in_64byte;
	unsigned int pix_x_div_64byte_mod;
	unsigned int pix_x_offset;

	if (pix_size > 2)
		return -EINVAL;

	/* 1st calculation step */
	switch (tile_type) {
	case TILE_TYPE_LINEAR:
		/* Divisable by 64 bytes */
		num_pix_x_in_64byte = 64 / (1 << pix_size);
		break;
	/* 4x4 tile or super tile */
	case TILE_TYPE_GPU_STANDARD:
	case TILE_TYPE_GPU_SUPER:
		BUG_ON(!pix_size);
		num_pix_x_in_64byte = 64 / (4 * (1 << pix_size));
		break;
	/* 8bpp YUV420 8x8 tile */
	case TILE_TYPE_VPU_2PYUV420:
		BUG_ON(pix_size);
		num_pix_x_in_64byte = 64 / (8 * (1 << pix_size));
		break;
	/* 8bpp or 10bpp VP9 4x4 tile */
	case TILE_TYPE_VPU_2PVP9:
		BUG_ON(pix_size == 2);
		num_pix_x_in_64byte = 64 / (4 * (1 << pix_size));
		break;
	default:
		return -EINVAL;
	}

	/* 2nd calculation step */
	pix_x_div_64byte_mod = width % num_pix_x_in_64byte;
	pix_x_offset = !pix_x_div_64byte_mod ? 0 :
				(num_pix_x_in_64byte - pix_x_div_64byte_mod);

	return width + pix_x_offset;
}

/* Divisable by 4 or 8 */
static int dpr_pix_y_calc(u32 rtr_lines,
			  u32 height,
			  u32 tile_type)
{
	unsigned int num_rows_buf;
	unsigned int pix_y_mod = 0;
	unsigned int pix_y_offset = 0;

	if (rtr_lines != 0 && rtr_lines != 1)
		return -EINVAL;

	switch (tile_type) {
	case TILE_TYPE_LINEAR:
		num_rows_buf = rtr_lines ? 4 : 8;
		break;
	/* 4x4 tile or super tile */
	case TILE_TYPE_GPU_STANDARD:
	case TILE_TYPE_GPU_SUPER:
		num_rows_buf = 4;
		break;
	/* 8bpp YUV420 8x8 tile */
	case TILE_TYPE_VPU_2PYUV420:
		num_rows_buf = 8;
		break;
	/* 8bpp or 10bpp VP9 4x4 tile */
	case TILE_TYPE_VPU_2PVP9:
		num_rows_buf = 4;
		break;
	default:
		return -EINVAL;
	}
	pix_y_mod = height % num_rows_buf;
	pix_y_offset = !pix_y_mod ? 0 : (num_rows_buf - pix_y_mod);

	return height + pix_y_offset;
}

static int dcss_dpr_config(uint32_t dpr_ch, struct dcss_info *info)
{
	uint32_t pitch, pix_size;
	uint32_t num_pix_x, num_pix_y;
	bool need_resolve = false;
	struct platform_device *pdev = info->pdev;
	struct dcss_channels *chans = &info->chans;
	struct dcss_channel_info *chan_info;
	struct fb_info *fbi;
	struct fb_fix_screeninfo *fix;
	struct fb_var_screeninfo *var;
	struct dcss_pixmap *input;
	struct cbuffer *cb;

	if (dpr_ch > 2) {
		dev_err(&pdev->dev, "invalid dpr channel number\n");
		return -EINVAL;
	}

	chan_info = &chans->chan_info[dpr_ch];
	fbi = chan_info->fb_info;
	fix = &fbi->fix;
	var = &fbi->var;
	input = &chan_info->input;

	if (dpr_ch == 0) {
		switch (input->tile_type) {
		case TILE_TYPE_LINEAR:
			need_resolve = false;
			break;
		case TILE_TYPE_GPU_STANDARD:
		case TILE_TYPE_GPU_SUPER:
			need_resolve = true;
			break;
		default:
			return -EINVAL;
		}
	}
	/* For channel 2,3, 'Tile Resolve' will be done by DTRC */

#if !USE_CTXLD
	writel(fix->smem_start, info->base + chan_info->dpr_addr + 0xc0);
	writel(0x2, info->base + chan_info->dpr_addr + 0x90);
	writel(var->xres, info->base + chan_info->dpr_addr + 0xa0);
	writel(var->yres, info->base + chan_info->dpr_addr + 0xb0);

	/* TODO: second plane config for YUV2P formats */
	writel(fix->smem_start + var->xres * var->yres,
	       info->base + chan_info->dpr_addr + 0x110);
	writel(var->xres, info->base + chan_info->dpr_addr + 0xf0);
	writel(var->yres, info->base + chan_info->dpr_addr + 0x100);

	/* TODO: calculate pitch for different formats */
	pitch = (var->xres * (var->bits_per_pixel >> 3)) << 16;
	writel(pitch, info->base + chan_info->dpr_addr + 0x70);

	if (!need_resolve) {
		/* Bypass resolve */
		writel(0xe4203, info->base + chan_info->dpr_addr + 0x50);
	} else {
		/* configure resolve */
		;
	}

	writel(0x38, info->base + chan_info->dpr_addr + 0x200);
	writel(0x4, info->base + chan_info->dpr_addr + 0x0);

	/* Trigger DPR on */
	writel(0x4, info->base + chan_info->dpr_addr + 0x0);
	writel(0x5, info->base + chan_info->dpr_addr + 0x0);
#else
	cb = &chan_info->cb;

	fill_sb(cb, chan_info->dpr_addr + 0xc0, fix->smem_start);
	fill_sb(cb, chan_info->dpr_addr + 0x90, 0x2);

	pix_size = ilog2(input->bits_per_pixel >> 3);

	num_pix_x = dpr_pix_x_calc(pix_size, input->width, input->tile_type);
	BUG_ON(num_pix_x < 0);
	fill_sb(cb, chan_info->dpr_addr + 0xa0, num_pix_x);

	switch (fmt_is_yuv(input->format)) {
	case 0:         /* RGB */
		num_pix_y = dpr_pix_y_calc(1, input->height, input->tile_type);
		BUG_ON(num_pix_y < 0);
		fill_sb(cb, chan_info->dpr_addr + 0xb0, num_pix_y);

		if (!need_resolve)
			/* Bypass resolve */
			fill_sb(cb, chan_info->dpr_addr + 0x50, 0xe4203);
		else {
			/* TODO: configure resolve */
			;
		}
		pitch = var->xres * (var->bits_per_pixel >> 3);
		break;
	case 1:         /* TODO: YUV 1P */
		return -EINVAL;
	case 2:         /* YUV 2P */
		/* Two planes YUV format */
		num_pix_y = dpr_pix_y_calc(0, input->height, input->tile_type);
		BUG_ON(num_pix_y < 0);
		fill_sb(cb, chan_info->dpr_addr + 0xb0, num_pix_y);

		fill_sb(cb, chan_info->dpr_addr + 0x50, 0xc1);
		fill_sb(cb, chan_info->dpr_addr + 0xe0, 0x2);

		/* TODO: VPU always has 16bytes alignment in width */
		pitch = ALIGN(var->xres * (var->bits_per_pixel >> 3), 16);
		fill_sb(cb, chan_info->dpr_addr + 0x110,
			fix->smem_start + pitch * var->yres);
		fill_sb(cb, chan_info->dpr_addr + 0xf0, num_pix_x);

		/* TODO: Require alignment handling:
		 * value must be evenly divisible by
		 * the number of rows programmed in
		 * MODE_CTRL0: RTR_4LINE_BUF_EN.
		 * UV height is 1/2 height of Luma.
		 */
		num_pix_y = dpr_pix_y_calc(0, input->height >> 1, input->tile_type);
		BUG_ON(num_pix_y < 0);
		fill_sb(cb, chan_info->dpr_addr + 0x100, num_pix_y);
		break;
	default:
		return -EINVAL;
	}

	/* TODO: calculate pitch for different formats */
	/* config pitch */
	fill_sb(cb, chan_info->dpr_addr + 0x70, pitch << 16);

	fill_sb(cb, chan_info->dpr_addr + 0x200, 0x38);

	/* Trigger DPR on */
	fill_sb(cb, chan_info->dpr_addr + 0x0, 0x5);
#endif

	return 0;
}

static int dcss_scaler_config(uint32_t scaler_ch, struct dcss_info *info)
{
	struct platform_device *pdev = info->pdev;
	struct dcss_channels *chans = &info->chans;
	struct dcss_channel_info *chan_info;
	struct fb_info *fbi;
	struct fb_var_screeninfo *var;
	struct dcss_pixmap *input;
	struct cbuffer *cb;
	int scale_v_luma_inc, scale_h_luma_inc;
	uint32_t align_width, align_height;
	const struct fb_videomode *dmode = info->dft_disp_mode;

	if (scaler_ch > 2) {
		dev_err(&pdev->dev, "invalid scaler channel number\n");
		return -EINVAL;
	}

	chan_info = &chans->chan_info[scaler_ch];
	fbi = chan_info->fb_info;
	var = &fbi->var;
	input = &chan_info->input;
#if !USE_CTXLD
	writel(0x0, info->base + chan_info->scaler_addr + 0x8);
	writel(0x0, info->base + chan_info->scaler_addr + 0xc);
	writel(0x2, info->base + chan_info->scaler_addr + 0x10);	/* src format */
	writel(0x2, info->base + chan_info->scaler_addr + 0x14);	/* dst format */

	writel((var->xres - 1) | (var->yres - 1) << 16,
		info->base + chan_info->scaler_addr + 0x18);	/* src resolution */
	writel((var->xres - 1) | (var->yres - 1) << 16,
		info->base + chan_info->scaler_addr + 0x1c);
	writel((var->xres - 1) | (var->yres - 1) << 16,
		info->base + chan_info->scaler_addr + 0x20);	/* dst resolution */
	writel((var->xres - 1) | (var->yres - 1) << 16,
		info->base + chan_info->scaler_addr + 0x24);
	writel(0x0, info->base + chan_info->scaler_addr + 0x28);
	writel(0x0, info->base + chan_info->scaler_addr + 0x2c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x30);
	writel(0x0, info->base + chan_info->scaler_addr + 0x34);
	writel(0x0, info->base + chan_info->scaler_addr + 0x38);
	writel(0x0, info->base + chan_info->scaler_addr + 0x3c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x40);
	writel(0x0, info->base + chan_info->scaler_addr + 0x44);

	/* scale ratio: ###.#_####_####_#### */
	writel(0x0, info->base + chan_info->scaler_addr + 0x48);
	writel(0x2000, info->base + chan_info->scaler_addr + 0x4c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x50);
	writel(0x2000, info->base + chan_info->scaler_addr + 0x54);
	writel(0x0, info->base + chan_info->scaler_addr + 0x58);
	writel(0x2000, info->base + chan_info->scaler_addr + 0x5c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x60);
	writel(0x2000, info->base + chan_info->scaler_addr + 0x64);

	writel(0x0, info->base + chan_info->scaler_addr + 0x80);

	writel(0x40000, info->base + chan_info->scaler_addr + 0xc0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x100);
	writel(0x0, info->base + chan_info->scaler_addr + 0x84);
	writel(0x0, info->base + chan_info->scaler_addr + 0xc4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x104);
	writel(0x0, info->base + chan_info->scaler_addr + 0x88);
	writel(0x0, info->base + chan_info->scaler_addr + 0xc8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x108);
	writel(0x0, info->base + chan_info->scaler_addr + 0x8c);
	writel(0x0, info->base + chan_info->scaler_addr + 0xcc);
	writel(0x0, info->base + chan_info->scaler_addr + 0x10c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x90);
	writel(0x0, info->base + chan_info->scaler_addr + 0xd0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x110);
	writel(0x0, info->base + chan_info->scaler_addr + 0x94);
	writel(0x0, info->base + chan_info->scaler_addr + 0xd4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x114);
	writel(0x0, info->base + chan_info->scaler_addr + 0x98);
	writel(0x0, info->base + chan_info->scaler_addr + 0xd8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x118);
	writel(0x0, info->base + chan_info->scaler_addr + 0x9c);
	writel(0x0, info->base + chan_info->scaler_addr + 0xdc);
	writel(0x0, info->base + chan_info->scaler_addr + 0x11c);
	writel(0x0, info->base + chan_info->scaler_addr + 0xa0);
	writel(0x0, info->base + chan_info->scaler_addr + 0xe0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x120);
	writel(0x0, info->base + chan_info->scaler_addr + 0xa4);
	writel(0x0, info->base + chan_info->scaler_addr + 0xe4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x124);
	writel(0x0, info->base + chan_info->scaler_addr + 0xa8);
	writel(0x0, info->base + chan_info->scaler_addr + 0xe8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x128);
	writel(0x0, info->base + chan_info->scaler_addr + 0xac);
	writel(0x0, info->base + chan_info->scaler_addr + 0xec);
	writel(0x0, info->base + chan_info->scaler_addr + 0x12c);
	writel(0x0, info->base + chan_info->scaler_addr + 0xb0);
	writel(0x0, info->base + chan_info->scaler_addr + 0xf0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x130);
	writel(0x0, info->base + chan_info->scaler_addr + 0xb4);
	writel(0x0, info->base + chan_info->scaler_addr + 0xf4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x134);
	writel(0x0, info->base + chan_info->scaler_addr + 0xb8);
	writel(0x0, info->base + chan_info->scaler_addr + 0xf8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x138);
	writel(0x0, info->base + chan_info->scaler_addr + 0xbc);
	writel(0x0, info->base + chan_info->scaler_addr + 0xfc);
	writel(0x0, info->base + chan_info->scaler_addr + 0x13c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x140);

	writel(0x40000, info->base + chan_info->scaler_addr + 0x180);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1c0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x144);
	writel(0x0, info->base + chan_info->scaler_addr + 0x184);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1c4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x148);
	writel(0x0, info->base + chan_info->scaler_addr + 0x188);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1c8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x14c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x18c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1cc);
	writel(0x0, info->base + chan_info->scaler_addr + 0x150);
	writel(0x0, info->base + chan_info->scaler_addr + 0x190);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1d0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x154);
	writel(0x0, info->base + chan_info->scaler_addr + 0x194);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1d4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x158);
	writel(0x0, info->base + chan_info->scaler_addr + 0x198);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1d8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x15c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x19c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1dc);
	writel(0x0, info->base + chan_info->scaler_addr + 0x160);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1a0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1e0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x164);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1a4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1e4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x168);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1a8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1e8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x16c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1ac);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1ec);
	writel(0x0, info->base + chan_info->scaler_addr + 0x170);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1b0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1f0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x174);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1b4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1f4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x178);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1b8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1f8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x17c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1bc);
	writel(0x0, info->base + chan_info->scaler_addr + 0x1fc);

	writel(0x0, info->base + chan_info->scaler_addr + 0x300);
	writel(0x0, info->base + chan_info->scaler_addr + 0x340);
	writel(0x0, info->base + chan_info->scaler_addr + 0x380);
	writel(0x0, info->base + chan_info->scaler_addr + 0x304);
	writel(0x0, info->base + chan_info->scaler_addr + 0x344);
	writel(0x0, info->base + chan_info->scaler_addr + 0x384);
	writel(0x0, info->base + chan_info->scaler_addr + 0x308);
	writel(0x0, info->base + chan_info->scaler_addr + 0x348);
	writel(0x0, info->base + chan_info->scaler_addr + 0x388);
	writel(0x0, info->base + chan_info->scaler_addr + 0x30c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x34c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x38c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x310);
	writel(0x0, info->base + chan_info->scaler_addr + 0x350);
	writel(0x0, info->base + chan_info->scaler_addr + 0x390);
	writel(0x0, info->base + chan_info->scaler_addr + 0x314);
	writel(0x0, info->base + chan_info->scaler_addr + 0x354);
	writel(0x0, info->base + chan_info->scaler_addr + 0x394);
	writel(0x0, info->base + chan_info->scaler_addr + 0x318);
	writel(0x0, info->base + chan_info->scaler_addr + 0x358);
	writel(0x0, info->base + chan_info->scaler_addr + 0x398);
	writel(0x0, info->base + chan_info->scaler_addr + 0x31c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x35c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x39c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x320);
	writel(0x0, info->base + chan_info->scaler_addr + 0x360);
	writel(0x0, info->base + chan_info->scaler_addr + 0x3a0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x324);
	writel(0x0, info->base + chan_info->scaler_addr + 0x364);
	writel(0x0, info->base + chan_info->scaler_addr + 0x3a4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x328);
	writel(0x0, info->base + chan_info->scaler_addr + 0x368);
	writel(0x0, info->base + chan_info->scaler_addr + 0x3a8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x32c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x36c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x3ac);
	writel(0x0, info->base + chan_info->scaler_addr + 0x330);
	writel(0x0, info->base + chan_info->scaler_addr + 0x370);
	writel(0x0, info->base + chan_info->scaler_addr + 0x3b0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x334);
	writel(0x0, info->base + chan_info->scaler_addr + 0x374);
	writel(0x0, info->base + chan_info->scaler_addr + 0x3b4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x338);
	writel(0x0, info->base + chan_info->scaler_addr + 0x378);
	writel(0x0, info->base + chan_info->scaler_addr + 0x3b8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x33c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x37c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x3bc);

	writel(0x0, info->base + chan_info->scaler_addr + 0x200);
	writel(0x0, info->base + chan_info->scaler_addr + 0x240);
	writel(0x0, info->base + chan_info->scaler_addr + 0x280);
	writel(0x0, info->base + chan_info->scaler_addr + 0x204);
	writel(0x0, info->base + chan_info->scaler_addr + 0x244);
	writel(0x0, info->base + chan_info->scaler_addr + 0x284);
	writel(0x0, info->base + chan_info->scaler_addr + 0x208);
	writel(0x0, info->base + chan_info->scaler_addr + 0x248);
	writel(0x0, info->base + chan_info->scaler_addr + 0x288);
	writel(0x0, info->base + chan_info->scaler_addr + 0x20c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x24c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x28c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x210);
	writel(0x0, info->base + chan_info->scaler_addr + 0x250);
	writel(0x0, info->base + chan_info->scaler_addr + 0x290);
	writel(0x0, info->base + chan_info->scaler_addr + 0x214);
	writel(0x0, info->base + chan_info->scaler_addr + 0x254);
	writel(0x0, info->base + chan_info->scaler_addr + 0x294);
	writel(0x0, info->base + chan_info->scaler_addr + 0x218);
	writel(0x0, info->base + chan_info->scaler_addr + 0x258);
	writel(0x0, info->base + chan_info->scaler_addr + 0x298);
	writel(0x0, info->base + chan_info->scaler_addr + 0x21c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x25c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x29c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x220);
	writel(0x0, info->base + chan_info->scaler_addr + 0x260);
	writel(0x0, info->base + chan_info->scaler_addr + 0x2a0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x224);
	writel(0x0, info->base + chan_info->scaler_addr + 0x264);
	writel(0x0, info->base + chan_info->scaler_addr + 0x2a4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x228);
	writel(0x0, info->base + chan_info->scaler_addr + 0x268);
	writel(0x0, info->base + chan_info->scaler_addr + 0x2a8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x22c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x26c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x2ac);
	writel(0x0, info->base + chan_info->scaler_addr + 0x230);
	writel(0x0, info->base + chan_info->scaler_addr + 0x270);
	writel(0x0, info->base + chan_info->scaler_addr + 0x2b0);
	writel(0x0, info->base + chan_info->scaler_addr + 0x234);
	writel(0x0, info->base + chan_info->scaler_addr + 0x274);
	writel(0x0, info->base + chan_info->scaler_addr + 0x2b4);
	writel(0x0, info->base + chan_info->scaler_addr + 0x238);
	writel(0x0, info->base + chan_info->scaler_addr + 0x278);
	writel(0x0, info->base + chan_info->scaler_addr + 0x2b8);
	writel(0x0, info->base + chan_info->scaler_addr + 0x23c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x27c);
	writel(0x0, info->base + chan_info->scaler_addr + 0x2bc);

	/* Trigger Scaler on */
	writel(0x11, info->base + chan_info->scaler_addr + 0x0);
#else
	cb = &chan_info->cb;

	switch (fmt_is_yuv(input->format)) {
	case 0:         /* ARGB8888 */
		fill_sb(cb, chan_info->scaler_addr + 0x8, 0x0);
		/* Scaler Input Format */
		fill_sb(cb, chan_info->scaler_addr + 0x10, 0x2);
		break;
	case 1:         /* TODO: YUV422 or YUV444 */
		break;
	case 2:         /* YUV420 */
		fill_sb(cb, chan_info->scaler_addr + 0x8, 0x3);
		/* Scaler Input Format */
		fill_sb(cb, chan_info->scaler_addr + 0x10, 0x0);
		break;
	default:
		return -EINVAL;
	}

	/* Chroma and Luma bit depth */
	fill_sb(cb, chan_info->scaler_addr + 0xc, 0x0);

	/* Scaler Output Format
	 * TODO: set dst fmt always to RGB888/YUV444
	 */
	fill_sb(cb, chan_info->scaler_addr + 0x14, 0x2);

	/* Scaler Input Luma Resolution
	 * Alighment Workaround for YUV420:
	 * 'width' divisable by 16, 'height' divisable by 8.
	 */

	if (fmt_is_yuv(input->format) == 2) {
		align_width  = round_down(input->width, 16);
		align_height = round_down(input->height, 8);
	} else {
		align_width  = input->width;
		align_height = input->height;
	}

	fill_sb(cb, chan_info->scaler_addr + 0x18,
		(align_height - 1) << 16 | (align_width - 1));

	/* Scaler Input Chroma Resolution */
	switch (fmt_is_yuv(input->format)) {
	case 0:         /* ARGB8888 */
		fill_sb(cb, chan_info->scaler_addr + 0x1c,
			(align_height - 1) << 16 | (align_width - 1));
		break;
	case 1:         /* TODO: YUV422 or YUV444 */
		break;
	case 2:         /* YUV420 */
		fill_sb(cb, chan_info->scaler_addr + 0x1c,
			((align_height >> 1) - 1) << 16 |
			((align_width >> 1) - 1));
		break;
	default:
		return -EINVAL;
	}

	/* Scaler Output Luma Resolution
	 * TODO: It should be scaled result value.
	 */
	fill_sb(cb, chan_info->scaler_addr + 0x20,
		(dmode->yres - 1) << 16 | (dmode->xres - 1));

	/* Scaler Output Chroma Resolution
	 * TODO: It should be scaled result value.
	 */
	fill_sb(cb, chan_info->scaler_addr + 0x24,
		(dmode->yres - 1) << 16 | (dmode->xres - 1));

	fill_sb(cb, chan_info->scaler_addr + 0x28, 0x0);
	fill_sb(cb, chan_info->scaler_addr + 0x2c, 0x0);
	fill_sb(cb, chan_info->scaler_addr + 0x30, 0x0);
	fill_sb(cb, chan_info->scaler_addr + 0x34, 0x0);
	fill_sb(cb, chan_info->scaler_addr + 0x38, 0x0);
	fill_sb(cb, chan_info->scaler_addr + 0x3c, 0x0);
	fill_sb(cb, chan_info->scaler_addr + 0x40, 0x0);
	fill_sb(cb, chan_info->scaler_addr + 0x44, 0x0);

	/* scale ratio: ###.#_####_####_#### */
	/* vertical ratio */
	scale_v_luma_inc = ((align_height << 13) + (dmode->yres >> 1)) / dmode->yres;
	/* horizontal ratio */
	scale_h_luma_inc = ((align_width << 13) + (dmode->xres >> 1)) / dmode->xres;

	fill_sb(cb, chan_info->scaler_addr + 0x48, 0x0);
	fill_sb(cb, chan_info->scaler_addr + 0x4c, scale_v_luma_inc);
	fill_sb(cb, chan_info->scaler_addr + 0x50, 0x0);
	fill_sb(cb, chan_info->scaler_addr + 0x54, scale_h_luma_inc);

	switch (fmt_is_yuv(input->format)) {
	case 0:         /* ARGB8888 */
		/* Scale Vertical Chroma Start */
		fill_sb(cb, chan_info->scaler_addr + 0x58, 0x0);

		/* Scale Vertical Chroma Increment */
		fill_sb(cb, chan_info->scaler_addr + 0x5c, scale_v_luma_inc);

		/* Scale Horizontal Chroma Increment */
		fill_sb(cb, chan_info->scaler_addr + 0x64, scale_h_luma_inc);
		break;
	case 1:         /* TODO: YUV422 or YUV444 */
		break;
	case 2:         /* YUV420 */
		/* Scale Vertical Chroma Start */
		fill_sb(cb, chan_info->scaler_addr + 0x58, 0x01fff800);

		/* Scale Vertical Chroma Increment */
		fill_sb(cb, chan_info->scaler_addr + 0x5c, scale_v_luma_inc >> 1);

		/* Scale Horizontal Chroma Increment */
		fill_sb(cb, chan_info->scaler_addr + 0x64, scale_h_luma_inc >> 1);
		break;
	default:
		return -EINVAL;
	}

	/* Scale Horizontal Chroma Start */
	fill_sb(cb, chan_info->scaler_addr + 0x60, 0x0);

	/* Trigger SCALER on */
	fill_sb(cb, chan_info->scaler_addr + 0x0, 0x11);
#endif
	return 0;
}

static int dcss_dtg_start(struct dcss_info *info)
{
	uint32_t dtg_lrc_x, dtg_lrc_y;
	uint32_t dis_ulc_x, dis_ulc_y;
	uint32_t dis_lrc_x, dis_lrc_y;
	struct dcss_channels *chans = &info->chans;
	struct dcss_channel_info *chan_info;
	const struct fb_videomode *dmode;
	struct cbuffer *cb;

	chan_info = &chans->chan_info[0];
	dmode = info->dft_disp_mode;
	cb = &chan_info->cb;

	/* Display Timing Config */
	dtg_lrc_x = dmode->xres + dmode->left_margin +
		    dmode->right_margin + dmode->hsync_len - 1;
	dtg_lrc_y = dmode->yres + dmode->upper_margin +
		    dmode->lower_margin + dmode->vsync_len - 1;
	writel(dtg_lrc_y << 16 | dtg_lrc_x, info->base + chans->dtg_addr + 0x4);

	/* global output timing */
	dis_ulc_x = dmode->left_margin + dmode->hsync_len - 1;
	dis_ulc_y = dmode->upper_margin + dmode->lower_margin +
		    dmode->vsync_len - 1;
	writel(dis_ulc_y << 16 | dis_ulc_x, info->base + chans->dtg_addr + 0x8);

	dis_lrc_x = dmode->xres + dmode->left_margin +
		    dmode->hsync_len - 1;
	dis_lrc_y = dmode->yres + dmode->upper_margin +
		    dmode->lower_margin + dmode->vsync_len - 1;
	writel(dis_lrc_y << 16 | dis_lrc_x, info->base + chans->dtg_addr + 0xc);

	/* config db and sb loading position of ctxld */
	writel(0xb000a, info->base + chans->dtg_addr + 0x28);

	/* config background color for graph layer: black */
	writel(0x0, info->base + chans->dtg_addr + 0x2c);

	/* config background color for video layer: black */
	writel(0x00080200, info->base + chans->dtg_addr + 0x30);

	/* Trigger DTG on */
	writel(0xff00518e, info->base + chans->dtg_addr + 0x0);

	info->dcss_state = DCSS_STATE_RUNNING;

	return 0;
}

static void dtg_channel_timing_config(int blank,
				struct dcss_channel_info *cinfo)
{
	struct cbuffer *cb;
	uint32_t ch_ulc_reg, ch_lrc_reg;
	struct fb_info *fbi = cinfo->fb_info;
	struct rectangle *pos = &cinfo->ch_pos;
	struct platform_device *pdev = cinfo->pdev;
	struct dcss_info *info = cinfo->dev_data;
	struct dcss_channels *chans = &info->chans;

	switch (fbi->node) {
	case 0:
		ch_ulc_reg = 0x10;
		ch_lrc_reg = 0x14;
		break;
	case 1:
		ch_ulc_reg = 0x18;
		ch_lrc_reg = 0x1c;
		break;
	case 2:
		ch_ulc_reg = 0x20;
		ch_lrc_reg = 0x24;
		break;
	default:
		dev_err(&pdev->dev, "%s: invalid channel number %d\n",
			__func__, fbi->node);
		return;
	}

	cb = &cinfo->cb;

	switch (blank) {
	case FB_BLANK_UNBLANK:
		/* set display window for one channel */
		fill_sb(cb, chans->dtg_addr + ch_ulc_reg,
			pos->ulc_y << 16 | pos->ulc_x);
		fill_sb(cb, chans->dtg_addr + ch_lrc_reg,
			pos->lrc_y << 16 | pos->lrc_x);
		break;
	case FB_BLANK_NORMAL:
	case FB_BLANK_VSYNC_SUSPEND:
	case FB_BLANK_HSYNC_SUSPEND:
	case FB_BLANK_POWERDOWN:
		fill_sb(cb, chans->dtg_addr + ch_ulc_reg, 0x0);
		fill_sb(cb, chans->dtg_addr + ch_lrc_reg, 0x0);
		break;
	default:
		return;
	}
}

static void dtg_global_timing_config(struct dcss_info *info)
{
	struct cbuffer *cb;
	uint32_t dtg_lrc_x, dtg_lrc_y;
	uint32_t dis_ulc_x, dis_ulc_y;
	uint32_t dis_lrc_x, dis_lrc_y;
	struct dcss_channels *chans = &info->chans;
	struct dcss_channel_info *cmain;
	const struct fb_videomode *dmode = info->dft_disp_mode;

	/* only main channel can change dtg timings */
	cmain = &chans->chan_info[DCSS_CHAN_MAIN];
	cb = &cmain->cb;

	/* Display Timing config */
	dtg_lrc_x = dmode->xres + dmode->left_margin +
		    dmode->right_margin + dmode->hsync_len - 1;
	dtg_lrc_y = dmode->yres + dmode->upper_margin +
		    dmode->lower_margin + dmode->vsync_len - 1;
	fill_sb(cb, chans->dtg_addr + 0x4, dtg_lrc_y << 16 | dtg_lrc_x);

	/* Active Region Timing config*/
	dis_ulc_x = dmode->left_margin  + dmode->hsync_len - 1;
	dis_ulc_y = dmode->upper_margin + dmode->lower_margin +
		    dmode->vsync_len - 1;
	fill_sb(cb, chans->dtg_addr + 0x8, dis_ulc_y << 16 | dis_ulc_x);

	dis_lrc_x = dmode->xres + dmode->left_margin +
		    dmode->hsync_len - 1;
	dis_lrc_y = dmode->yres + dmode->upper_margin +
		    dmode->lower_margin + dmode->vsync_len - 1;
	fill_sb(cb, chans->dtg_addr + 0xc, dis_lrc_y << 16 | dis_lrc_x);
}

static int dcss_dtg_config(uint32_t ch_id, struct dcss_info *info)
{
	struct platform_device *pdev = info->pdev;
	struct dcss_channels *chans = &info->chans;
	struct dcss_channel_info *cinfo;

	if (ch_id > 2) {
		dev_err(&pdev->dev, "invalid channel id\n");
		return -EINVAL;
	}

	cinfo = &chans->chan_info[ch_id];

	if (ch_id == DCSS_CHAN_MAIN)
		dtg_global_timing_config(info);

	/* TODO: Channel Timing Config */
	dtg_channel_timing_config(FB_BLANK_UNBLANK, cinfo);

	return 0;
}

static int dcss_subsam_config(struct dcss_info *info)
{
	uint32_t hsync_pol, vsync_pol, de_pol;
	uint32_t disp_lrc_x, disp_lrc_y;
	uint32_t hsync_start, hsync_end;
	uint32_t vsync_start, vsync_end;
	uint32_t de_ulc_x, de_ulc_y;
	uint32_t de_lrc_x, de_lrc_y;
	struct dcss_channels *chans = &info->chans;
	struct dcss_channel_info *chan_info;
	struct cbuffer *cb;
	const struct fb_videomode *dmode;

	/* using channel 0 by default */
	chan_info = &chans->chan_info[0];
	cb = &chan_info->cb;
	dmode = info->dft_disp_mode;

	/* TODO: for 1080p only */
	hsync_pol = 1;
	vsync_pol = 1;
	de_pol = 1;

#if USE_CTXLD
	/* 3 tap fir filters coefficients */
	fill_sb(cb, chans->subsam_addr + 0x70, 0x41614161);
	fill_sb(cb, chans->subsam_addr + 0x80, 0x03ff0000);
	fill_sb(cb, chans->subsam_addr + 0x90, 0x03ff0000);
#else
	writel(0x41614161, info->base + chans->subsam_addr + 0x70);
	writel(0x03ff0000, info->base + chans->subsam_addr + 0x80);
	writel(0x03ff0000, info->base + chans->subsam_addr + 0x90);
#endif

	/* Timing Config */
	disp_lrc_x = dmode->xres + dmode->left_margin +
		     dmode->right_margin + dmode->hsync_len - 1;
	disp_lrc_y = dmode->yres + dmode->upper_margin +
		     dmode->lower_margin + dmode->vsync_len - 1;
#if USE_CTXLD
	fill_sb(cb, chans->subsam_addr + 0x10,
		disp_lrc_y << 16 | disp_lrc_x);
#else
	writel(disp_lrc_y << 16 | disp_lrc_x,
	       info->base + chans->subsam_addr + 0x10);
#endif

	/* horizontal sync will be asserted when
	 * horizontal count == START
	 */
	hsync_start = dmode->xres + dmode->left_margin +
		      dmode->right_margin + dmode->hsync_len - 1;
	hsync_end   = dmode->hsync_len - 1;
#if USE_CTXLD
	fill_sb(cb, chans->subsam_addr + 0x20,
		(hsync_pol << 31) | hsync_end << 16 | hsync_start);
#else
	writel((hsync_pol << 31) | hsync_end << 16 | hsync_start,
	       info->base + chans->subsam_addr + 0x20);
#endif

	vsync_start = dmode->lower_margin - 1;
	vsync_end   = dmode->lower_margin + dmode->vsync_len - 1;
#if USE_CTXLD
	fill_sb(cb, chans->subsam_addr + 0x30,
		(vsync_pol << 31) | vsync_end << 16 | vsync_start);
#else
	writel((vsync_pol << 31) | vsync_end << 16 | vsync_start,
	       info->base + chans->subsam_addr + 0x30);
#endif

	de_ulc_x = dmode->left_margin + dmode->hsync_len - 1;
	de_ulc_y = dmode->upper_margin + dmode->lower_margin +
		   dmode->vsync_len;
#if USE_CTXLD
	fill_sb(cb, chans->subsam_addr + 0x40,
		(de_pol << 31) | de_ulc_y << 16 | de_ulc_x);
#else
	writel((de_pol << 31) | de_ulc_y << 16 | de_ulc_x,
	       info->base + chans->subsam_addr + 0x40);
#endif

	de_lrc_x = dmode->xres + dmode->left_margin +
		   dmode->hsync_len - 1;
	de_lrc_y = dmode->yres + dmode->upper_margin +
		   dmode->lower_margin + dmode->vsync_len - 1;
#if USE_CTXLD
	fill_sb(cb, chans->subsam_addr + 0x50,
		de_lrc_y << 16 | de_lrc_x);

	/* Trigger Subsam on */
	fill_sb(cb, chans->subsam_addr + 0x0, 0x1);
#else
	writel(de_lrc_y << 16 | de_lrc_x,
	       info->base + chans->subsam_addr + 0x50);
	writel(0x1, info->base + chans->subsam_addr + 0x0);
#endif

	return 0;
}

static void ctxld_irq_unmask(uint32_t irq_en, struct dcss_info *info)
{
	struct dcss_channels *chans = &info->chans;

	writel(irq_en, info->base + chans->ctxld_addr + CTXLD_CTRL_STATUS_SET);
}

static void __maybe_unused dtg_irq_mask(unsigned long hwirq,
					struct dcss_info *info)
{
	unsigned long irq_mask = 0;
	struct dcss_channels *chans = &info->chans;

	irq_mask = readl(info->base + chans->dtg_addr + TC_INTERRUPT_MASK);
	writel(~(1 << (hwirq - 8)) & irq_mask,
	       info->base + chans->dtg_addr + TC_INTERRUPT_MASK);
}

static void dtg_irq_unmask(unsigned long hwirq,
			   struct dcss_info *info)
{
	unsigned long irq_mask = 0;
	struct dcss_channels *chans = &info->chans;

	irq_mask = readl(info->base + chans->dtg_addr + TC_INTERRUPT_MASK);

	writel(1 << (hwirq - 8) | irq_mask,
	       info->base + chans->dtg_addr + TC_INTERRUPT_MASK);
}

static void dtg_irq_clear(unsigned long hwirq,
			  struct dcss_info *info)
{
	unsigned long irq_status = 0;
	struct dcss_channels *chans = &info->chans;

	irq_status = readl(info->base + chans->dtg_addr + TC_INTERRUPT_STATUS);
	BUG_ON(!(irq_status & 1 << (hwirq - 8)));

	/* write 1 to clear irq */
	writel(1 << (hwirq - 8),
	       info->base + chans->dtg_addr + TC_INTERRUPT_CONTROL);
}

static void dcss_ctxld_config(struct work_struct *work)
{
	int ret;
	uint32_t dsb_len, nsgl, esize, offset;
	struct dcss_info *info;
	struct platform_device *pdev;
	struct dcss_channels *chans;
	struct ctxld_commit *cc;
	struct ctxld_fifo *cfifo;

	cc = container_of(work, struct ctxld_commit, work);
	info = (struct dcss_info *)cc->data;
	pdev = info->pdev;
	chans = &info->chans;
	cfifo = &info->cfifo;
	dsb_len = cc->sb_data_len + cc->db_data_len;
	esize = kfifo_esize(&cfifo->fifo);

	/* NOOP cc */
	if (!cc->sb_data_len && !cc->db_data_len)
		goto free_cc;

	sg_init_table(cfifo->sgl, cfifo->sgl_num);
	nsgl = kfifo_dma_out_prepare(&cfifo->fifo, cfifo->sgl,
				     cfifo->sgl_num, dsb_len);
	BUG_ON(!nsgl);

	if (nsgl == 1) {
		if (cfifo->sgl[0].length != dsb_len * esize)
			BUG_ON(1);
	}

	offset = cfifo->fifo.kfifo.out & cfifo->fifo.kfifo.mask;

	/* configure sb buffer */
	if (cc->sb_data_len) {
		/* cfifo first store sb and than store db */
		writel(cfifo->dma_handle + offset * esize,
		       info->base + chans->ctxld_addr + CTXLD_SB_BASE_ADDR);
		writel(cc->sb_hp_data_len |
		       ((cc->sb_data_len - cc->sb_hp_data_len) << 16),
		       info->base + chans->ctxld_addr + CTXLD_SB_COUNT);
	}

	/* configure db buffer */
	if (cc->db_data_len) {
		writel(cfifo->dma_handle + (offset + cc->sb_data_len) * esize,
		       info->base + chans->ctxld_addr + CTXLD_DB_BASE_ADDR);
		writel(cc->db_data_len,
		       info->base + chans->ctxld_addr + CTXLD_DB_COUNT);
	}

	/* enable ctx_ld */
	writel(0x1, info->base + chans->ctxld_addr + CTXLD_CTRL_STATUS_SET);

	/* wait finish */
	reinit_completion(&cfifo->complete);
	ret = wait_for_completion_timeout(&cfifo->complete, HZ);
	if (!ret)	/* timeout */
		dev_err(&pdev->dev, "wait ctxld finish timeout\n");

	ctxld_fifo_info_print(cfifo);
	kfifo_dma_out_finish(&cfifo->fifo,
		(cc->sb_data_len + cc->db_data_len) * esize);
	ctxld_fifo_info_print(cfifo);

free_cc:
	kfree(cc);

	dev_dbg(&pdev->dev, "finish ctxld config\n");
}

static void copy_data_to_cfifo(struct ctxld_fifo *cfifo,
			       struct cbuffer *cb,
			       struct ctxld_commit *cc)
{
	struct ctxld_unit *unit;
	uint32_t count;

	unit = (struct ctxld_unit *)cb->sb_addr;

	if (cb->sb_data_len) {
		count = kfifo_in(&cfifo->fifo, cb->sb_addr, cb->sb_data_len);
		if (count != cb->sb_data_len) {
			/* TODO: this case should be completely ignored */
			pr_err("write sb data mismatch\n");
			count = kfifo_out(&cfifo->fifo, cb->sb_addr, count);
			WARN_ON(1);
		}
		cc->sb_hp_data_len += count;
		cc->sb_data_len += count;
	}

	if (cb->db_data_len) {
		count = kfifo_in(&cfifo->fifo, cb->db_addr, cb->db_data_len);
		if (count != cb->db_data_len) {
			/* TODO: this case should be completely ignored */
			pr_err("write db data mismatch\n");
			count = kfifo_out(&cfifo->fifo, cb->db_addr, count);
			WARN_ON(1);
		}
		cc->db_data_len += count;
	}
}

static struct ctxld_commit *alloc_cc(struct dcss_info *info)
{
	struct ctxld_commit *cc;

	cc = kzalloc(sizeof(*cc), GFP_KERNEL);
	if (!cc)
		return ERR_PTR(-ENOMEM);

	INIT_LIST_HEAD(&cc->list);
	INIT_WORK(&cc->work, dcss_ctxld_config);
	kref_init(&cc->refcount);
	cc->data = info;

	return cc;
}

static struct ctxld_commit *obtain_cc(int ch_id, struct dcss_info *info)
{
	int ret;
	unsigned long irqflags;
	struct dcss_channel_info *cinfo;
	struct ctxld_commit *cc = NULL;
	struct platform_device *pdev = info->pdev;
	struct dcss_channels *chans = &info->chans;
	struct ctxld_fifo *cfifo = &info->cfifo;
	struct vsync_info *vinfo = &info->vinfo;

	cinfo = &chans->chan_info[ch_id];

	/* wait for next frame window */
	ret = wait_event_interruptible_timeout(vinfo->vwait,
			vcount_compare(cinfo->update_stamp, vinfo),
			HZ);
	if (!ret) {
		dev_err(&pdev->dev, "wait next frame timeout\n");
		return ERR_PTR(-EBUSY);
	}

	spin_lock_irqsave(&vinfo->vwait.lock, irqflags);

	cinfo->update_stamp = vinfo->vcount;
	if (!list_empty(&cfifo->ctxld_list)) {
		cc = list_first_entry(&cfifo->ctxld_list,
				      struct ctxld_commit,
				      list);
		kref_get(&cc->refcount);
	}

	spin_unlock_irqrestore(&vinfo->vwait.lock, irqflags);

	if (!cc) {
		cc = alloc_cc(info);
		if (IS_ERR(cc))
			return cc;

		spin_lock_irqsave(&vinfo->vwait.lock, irqflags);

		if (list_empty(&cfifo->ctxld_list))
			list_add_tail(&cfifo->ctxld_list, &cc->list);
		else {
			kfree(cc);
			cc = list_first_entry(&cfifo->ctxld_list,
					      struct ctxld_commit,
					      list);
		}
		kref_get(&cc->refcount);

		spin_unlock_irqrestore(&vinfo->vwait.lock, irqflags);
	}

	return cc;
}

static void release_cc(struct kref *kref)
{
	unsigned long irqflags;
	struct ctxld_commit *cc;
	struct dcss_info *info;
	struct vsync_info *vinfo;
	struct ctxld_fifo *cfifo;

	cc = container_of(kref, struct ctxld_commit, refcount);
	info  = (struct dcss_info *)cc->data;
	vinfo = &info->vinfo;
	cfifo = &info->cfifo;

	spin_lock_irqsave(&vinfo->vwait.lock, irqflags);

	list_del(&cc->list);
	flush_cfifo(cfifo, &cc->work);

	spin_unlock_irqrestore(&vinfo->vwait.lock, irqflags);
}

/**
 * Only be called when 'vwait.lock' is hold
 */
static void release_cc_locked(struct kref *kref)
{
	struct ctxld_commit *cc;
	struct dcss_info *info;
	struct ctxld_fifo *cfifo;

	cc = container_of(kref, struct ctxld_commit, refcount);
	info  = (struct dcss_info *)cc->data;
	cfifo = &info->cfifo;

	list_del(&cc->list);
	flush_cfifo(cfifo, &cc->work);
}

static void flush_cfifo(struct ctxld_fifo *cfifo,
			struct work_struct *work)
{
	int ret;

	ret = queue_work(cfifo->ctxld_wq, work);

	WARN(!ret, "work has already been queued\n");
}

static int defer_flush_cfifo(struct ctxld_fifo *cfifo)
{
	int i;
	struct dcss_info *info;
	struct dcss_channels *chans;
	struct dcss_channel_info *cinfo;

	info = container_of(cfifo, struct dcss_info, cfifo);
	chans = &info->chans;

	for (i = 0; i < 3; i++) {
		cinfo = &chans->chan_info[i];
		cinfo->update_stamp = info->vinfo.vcount;
	}

	return 0;
}

static int finish_cfifo(struct ctxld_fifo *cfifo)
{
	int ret;
	struct dcss_info *info;

	info = container_of(cfifo, struct dcss_info, cfifo);

	ret = dcss_wait_for_vsync(0, info);
	if (ret)
		return ret;

	flush_workqueue(cfifo->ctxld_wq);

	return 0;
}

static int commit_cfifo(uint32_t channel,
			struct dcss_info *info,
			struct ctxld_commit *cc)
{
	uint32_t commit_size;
	struct dcss_channels *chans;
	struct dcss_channel_info *chan_info;
	struct ctxld_fifo *cfifo;
	struct cbuffer *cb;

	cfifo = &info->cfifo;
	chans = &info->chans;
	chan_info = &chans->chan_info[channel];
	cb = &chan_info->cb;
	commit_size = cb->sb_data_len + cb->db_data_len;

	spin_lock(&cfifo->cqueue.lock);

	if (unlikely(atomic_read(&info->flush) == 1)) {
		/* cancel this commit and restart it later */
		kref_put(&cc->refcount, release_cc);

		wait_event_interruptible_exclusive_locked(cfifo->cqueue,
						atomic_read(&info->flush));

		spin_unlock(&cfifo->cqueue.lock);
		return -ERESTARTSYS;
	} else {
		if (unlikely(waitqueue_active(&cfifo->cqueue)))
			wake_up_locked(&cfifo->cqueue);
	}

	if (unlikely(commit_size > kfifo_to_end_len(&cfifo->fifo))) {
		atomic_set(&info->flush, 1);
		spin_unlock(&cfifo->cqueue.lock);

		/* cancel this commit and restart it later */
		kref_put(&cc->refcount, release_cc);

		/* Wait fifo flush empty to avoid fifo wrap */
		finish_cfifo(cfifo);

		spin_lock(&cfifo->cqueue.lock);

		atomic_set(&info->flush, 0);
		kfifo_reset(&cfifo->fifo);
		if (waitqueue_active(&cfifo->cqueue))
			wake_up_locked(&cfifo->cqueue);

		spin_unlock(&cfifo->cqueue.lock);

		return -ERESTART;
	}

	copy_data_to_cfifo(cfifo, cb, cc);

	ctxld_fifo_info_print(cfifo);

	/* empty sb and db buffer */
	cb->db_data_len = 0;
	cb->sb_data_len = 0;

	spin_unlock(&cfifo->cqueue.lock);

	return 0;
}

static int dcss_open(struct fb_info *fbi, int user)
{
	int fb_node = fbi->node;
	struct dcss_channel_info *cinfo = fbi->par;
	struct dcss_info *info = cinfo->dev_data;
	struct dcss_channels *chans = &info->chans;
	struct dcss_channel_info *chan_info;
	struct cbuffer *cb;

	if (fb_node < 0 || fb_node > 2)
		BUG_ON(1);

	chan_info = &chans->chan_info[fb_node];
	cb = &chan_info->cb;

	if (fb_node == 0)
		return 0;

	return 0;
}

static int dcss_check_var(struct fb_var_screeninfo *var,
			  struct fb_info *fbi)
{
	uint32_t fb_size;
	uint32_t scale_ratio_mode_x, scale_ratio_mode_y;
	uint32_t scale_ratio_x, scale_ratio_y;
	struct dcss_channel_info *cinfo = fbi->par;
	struct dcss_info *info = cinfo->dev_data;
	struct platform_device *pdev = info->pdev;
	const struct fb_bitfield *rgb = NULL;
	const struct pix_fmt_info *format = NULL;
	struct fb_fix_screeninfo *fix = &fbi->fix;
	const struct fb_videomode *dmode = info->dft_disp_mode;

	if (var->xres > MAX_WIDTH || var->yres > MAX_HEIGHT) {
		dev_err(&pdev->dev, "unsupport display resolution\n");
		return -EINVAL;
	}

	if (var->xres_virtual > var->xres) {
		dev_err(&pdev->dev, "stride not supported\n");
		return -EINVAL;
	}

	if (var->xres_virtual < var->xres)
		var->xres_virtual = var->xres;
	if (var->yres_virtual < var->yres)
		var->yres_virtual = var->yres;

	switch (var->grayscale) {
	case 0:		/* TODO: color */
	case 1:		/* grayscale */
		return -EINVAL;
	default:	/* fourcc */
		format = get_fmt_info(var->grayscale);
		if (!format) {
			dev_err(&pdev->dev, "unsupport pixel format\n");
			return -EINVAL;
		}
		var->bits_per_pixel = format->bpp;
	}

	/* Add alignment check for scaler */
	switch (fmt_is_yuv(var->grayscale)) {
	case 0:         /* ARGB8888 */
	case 2:         /* YUV420 */
		if (ALIGN(var->xres, 4) != var->xres ||
		    ALIGN(var->yres, 4) != var->yres) {
			dev_err(&pdev->dev, "width or height is not aligned\n");
			return -EINVAL;
		}
		break;
	default:
		return -EINVAL;
	}

	/* Add scale ratio check:
	 * Maximum scale down ratio is 1/7;
	 * Maximum scale up   ratio is 8;
	 */
	if (dmode->xres > var->xres) {
		/* upscaling */
		scale_ratio_mode_x = dmode->xres % var->xres;
		scale_ratio_mode_y = dmode->yres % var->yres;
		scale_ratio_x = (dmode->xres - scale_ratio_mode_x) / var->xres;
		scale_ratio_y = (dmode->yres - scale_ratio_mode_y) / var->yres;
		if (scale_ratio_x >= 8) {
			if ((scale_ratio_x == 8 && scale_ratio_mode_x > 0) ||
			    (scale_ratio_x > 8)) {
				dev_err(&pdev->dev, "unsupport scaling ration for width\n");
				return -EINVAL;
			}
		}

		if (scale_ratio_y >= 8) {
			if ((scale_ratio_y == 8 && scale_ratio_mode_y > 0) ||
			    (scale_ratio_y > 8)) {
				dev_err(&pdev->dev, "unsupport scaling ration for height\n");
				return -EINVAL;
			}
		}
	} else {
		/* downscaling */
		scale_ratio_mode_x = var->xres % dmode->xres;
		scale_ratio_mode_y = var->yres % dmode->yres;
		scale_ratio_x = (var->xres - scale_ratio_mode_x) / dmode->xres;
		scale_ratio_y = (var->yres - scale_ratio_mode_y) / dmode->yres;
		if (scale_ratio_x >= 7) {
			if ((scale_ratio_x == 7 && scale_ratio_mode_x > 0) ||
			    (scale_ratio_x > 7)) {
				dev_err(&pdev->dev, "unsupport scaling ration for width\n");
				return -EINVAL;
			}
		}

		if (scale_ratio_y >= 7) {
			if ((scale_ratio_y == 7 && scale_ratio_mode_y > 0) ||
			    (scale_ratio_y > 7)) {
				dev_err(&pdev->dev, "unsupport scaling ration for height\n");
				return -EINVAL;
			}
		}
	}

	fix->line_length = var->xres * (var->bits_per_pixel >> 3);
	fb_size = var->yres_virtual * fix->line_length;

	if (fb_size > fix->smem_len) {
		dev_err(&pdev->dev, "exceeds fb size limit!\n");
		return -ENOMEM;
	}

	if (format && !format->is_yuv) {
		switch (format->fourcc) {
		case V4L2_PIX_FMT_ARGB32:
			rgb = def_a8r8g8b8;
			break;
		case V4L2_PIX_FMT_A2R10G10B10:
			rgb = def_a2r10g10b10;
			break;
		default:
			dev_err(&pdev->dev, "unsupport pixel format\n");
			return -EINVAL;
		}

		var->red    = rgb[RED];
		var->green  = rgb[GREEN];
		var->blue   = rgb[BLUE];
		var->transp = rgb[TRANSP];
	} else {
		/* TODO: YUV format */
		;
	}

	return 0;
}

static int config_channel_pipe(struct dcss_channel_info *cinfo)
{
	int ret = 0;
	int fb_node;
	struct fb_info *fbi = cinfo->fb_info;
	struct dcss_info *info = cinfo->dev_data;
	struct platform_device *pdev = info->pdev;

	fb_node = fbi->node;

	dev_dbg(&cinfo->pdev->dev, "begin config pipe %d\n", fb_node);

	/* configure all the sub modules on one channel:
	 * 1. DEC400D/DTRC
	 * 2. DPR
	 * 3. SCALER
	 * 4. HDR10_INPUT
	 */
	ret = dcss_decomp_config(fb_node, info);
	if (ret) {
		dev_err(&pdev->dev, "decomp config failed\n");
		goto out;
	}

	ret = dcss_dpr_config(fb_node, info);
	if (ret) {
		dev_err(&pdev->dev, "dpr config failed\n");
		goto out;
	}

	ret = dcss_scaler_config(fb_node, info);
	if (ret) {
		dev_err(&pdev->dev, "scaler config failed\n");
		goto out;
	}

out:
	return ret;
}

static int dcss_set_par(struct fb_info *fbi)
{
	int ret = 0;
	int fb_node = fbi->node;
	struct dcss_channel_info *cinfo = fbi->par;
	struct dcss_info *info = cinfo->dev_data;
	struct cbuffer *cb = &cinfo->cb;
	struct ctxld_commit *cc;

	if (fb_node < 0 || fb_node > 2)
		BUG_ON(1);

	/* TODO: add save/recovery when config failed */
	fb_var_to_pixmap(&cinfo->input, &fbi->var);

	ret = config_channel_pipe(cinfo);
	if (ret)
		goto fail;

	ret = dcss_dtg_config(fb_node, info);
	if (ret)
		goto fail;

#if USE_CTXLD
restart:
	cc = obtain_cc(fb_node, info);
	if (IS_ERR(cc)) {
		ret = PTR_ERR(cc);
		goto fail;
	}

	ret = commit_cfifo(fb_node, info, cc);
	if (ret == -ERESTART)
		goto restart;

	kref_put(&cc->refcount, release_cc);
#endif

	goto out;

fail:
	/* drop any ctxld_uint already
	 * been written to sb or db
	 */
	cb->sb_data_len = 0;
	cb->db_data_len = 0;
out:
	return ret;
}

static int dcss_setcolreg(unsigned regno, unsigned red, unsigned green,
		unsigned blue, unsigned transp, struct fb_info *info)
{
	return 0;
}

static int dcss_channel_blank(int blank,
		struct dcss_channel_info *cinfo)
{
	uint32_t dtg_ctrl;
	struct dcss_info *info = cinfo->dev_data;
	struct dcss_channels *chans = &info->chans;
	struct cbuffer *cb = &cinfo->cb;

	dtg_ctrl = readl(info->base + chans->dtg_addr + 0x0);

	switch (blank) {
	case FB_BLANK_UNBLANK:
		/* set global alpha */
		if (cinfo->channel_id == DCSS_CHAN_MAIN)
			dtg_ctrl |= (0xff << 24);
		else
			dtg_ctrl &= ~(0xff << 24);
		break;
	case FB_BLANK_NORMAL:
	case FB_BLANK_VSYNC_SUSPEND:
	case FB_BLANK_HSYNC_SUSPEND:
	case FB_BLANK_POWERDOWN:
		/* clear global alpha */
		if (cinfo->channel_id == DCSS_CHAN_MAIN)
			dtg_ctrl &= ~(0xff << 24);
		else
			dtg_ctrl |= (0xff << 24);
		break;
	default:
		return -EINVAL;
	}

	fill_sb(cb, chans->dtg_addr + 0x0, dtg_ctrl);

	return 0;
}

static int dcss_blank(int blank, struct fb_info *fbi)
{
	int ret = 0;
	int fb_node = fbi->node;
	struct dcss_channel_info *cinfo = fbi->par;
	struct dcss_info *info = cinfo->dev_data;
	struct cbuffer *cb;
	struct ctxld_commit *cc;

	cb = &cinfo->cb;

	dtg_channel_timing_config(blank, cinfo);
	dcss_channel_blank(blank, cinfo);

#if USE_CTXLD
restart:
	cc = obtain_cc(fb_node, info);
	if (IS_ERR(cc)) {
		ret = PTR_ERR(cc);
		goto fail;
	}

	ret = commit_cfifo(fb_node, info, cc);
	if (ret == -ERESTART)
		goto restart;

	kref_put(&cc->refcount, release_cc);
#endif

	cinfo->blank = blank;

	goto out;

fail:
	/* drop any ctxld_uint already
	 * been written to sb or db
	 */
	cb->sb_data_len = 0;
	cb->db_data_len = 0;

out:
	return ret;
}

static int dcss_pan_display(struct fb_var_screeninfo *var,
			    struct fb_info *fbi)
{
	int ret = 0;
	int fb_node = fbi->node;
	uint32_t offset, pitch, luma_addr, chroma_addr = 0;
	struct dcss_channel_info *cinfo = fbi->par;
	struct dcss_info *info = cinfo->dev_data;
	struct platform_device *pdev = info->pdev;
	struct cbuffer *cb = &cinfo->cb;
	struct dcss_pixmap *input = &cinfo->input;
	struct ctxld_commit *cc;

	/* TODO: change framebuffer memory start address */
	luma_addr = var->reserved[0] ? var->reserved[0] :
				       fbi->fix.smem_start;

	/* change display offset in framebuffer */
	if (var->xoffset > 0) {
		dev_dbg(&pdev->dev, "x panning not supported\n");
		return -EINVAL;
	}

	if ((var->yoffset + var->yres > var->yres_virtual)) {
		dev_err(&pdev->dev, "y panning exceeds\n");
		return -EINVAL;
	}

	offset = fbi->fix.line_length * var->yoffset;

	fill_sb(cb, cinfo->dpr_addr + 0xc0, luma_addr + offset);

	/* Two planes YUV format */
	if (fmt_is_yuv(input->format) == 2) {
		pitch = ALIGN(var->xres * (var->bits_per_pixel >> 3), 16);
		chroma_addr = luma_addr + pitch * var->yres;
		fill_sb(cb,
			cinfo->dpr_addr + 0x110,
			chroma_addr + (offset >> 1));
	}

#if USE_CTXLD
restart:
	cc = obtain_cc(fb_node, info);
	if (IS_ERR(cc)) {
		ret = PTR_ERR(cc);
		goto fail;
	}

	ret = commit_cfifo(fb_node, info, cc);
	if (ret == -ERESTART)
		goto restart;

	kref_put(&cc->refcount, release_cc);
#endif

	goto out;

fail:
	/* drop any ctxld_uint already
	 * been written to sb or db
	 */
	cb->sb_data_len = 0;
	cb->db_data_len = 0;

out:
	return ret;
}

static int vcount_compare(unsigned long vcount,
			  struct vsync_info *vinfo)
{
	int ret = 0;
	unsigned long irqflags;

	spin_lock_irqsave(&vinfo->vwait.lock, irqflags);

	ret = (vcount != vinfo->vcount) ? 1 : 0;

	spin_unlock_irqrestore(&vinfo->vwait.lock, irqflags);

	return ret;
}

static int dcss_wait_for_vsync(unsigned long crtc,
			       struct dcss_info *info)
{
	int ret = 0;
	unsigned long irqflags, vcount;
	struct platform_device *pdev = info->pdev;

	spin_lock_irqsave(&info->vinfo.vwait.lock, irqflags);
	vcount = info->vinfo.vcount;
	spin_unlock_irqrestore(&info->vinfo.vwait.lock, irqflags);

	ret = wait_event_interruptible_timeout(info->vinfo.vwait,
					vcount_compare(vcount, &info->vinfo),
					HZ);
	if (!ret) {
		dev_err(&pdev->dev, "wait vsync active timeout\n");
		return -EBUSY;
	}

	return 0;
}

static int dcss_ioctl(struct fb_info *fbi, unsigned int cmd,
		      unsigned long arg)
{
	int ret = 0;
	unsigned long crtc;
	void __user *argp = (void __user *)arg;
	struct dcss_channel_info *cinfo = fbi->par;
	struct dcss_info *info = cinfo->dev_data;
	struct platform_device *pdev = cinfo->pdev;

	switch (cmd) {
	case FBIO_WAITFORVSYNC:
		if (copy_from_user(&crtc, argp, sizeof(unsigned long)))
			return -EFAULT;

		ret = dcss_wait_for_vsync(crtc, info);
		break;
	default:
		dev_err(&pdev->dev, "invalid ioctl command: 0x%x\n", cmd);
		ret = -EINVAL;
		break;
	}

	return ret;
}

static void ctxld_irq_clear(struct dcss_info *info)
{
	uint32_t irq_status;
	struct dcss_channels *chans  = &info->chans;
	struct platform_device *pdev = info->pdev;

	irq_status = readl(info->base + chans->ctxld_addr + CTXLD_CTRL_STATUS);
	dev_dbg(&pdev->dev, "ctxld irq_status before = 0x%x\n", irq_status);

	if (irq_status & RD_ERR)
		writel(RD_ERR, info->base + chans->ctxld_addr + CTXLD_CTRL_STATUS_CLR);

	if (irq_status & DB_COMP)
		writel(DB_COMP, info->base + chans->ctxld_addr + CTXLD_CTRL_STATUS_CLR);

	if (irq_status & SB_HP_COMP)
		writel(SB_HP_COMP,
		       info->base + chans->ctxld_addr + CTXLD_CTRL_STATUS_CLR);

	if (irq_status & SB_LP_COMP)
		writel(SB_LP_COMP,
		       info->base + chans->ctxld_addr + CTXLD_CTRL_STATUS_CLR);

	if (irq_status & AHB_ERR)
		writel(AHB_ERR,
		       info->base + chans->ctxld_addr + CTXLD_CTRL_STATUS_CLR);

	irq_status = readl(info->base + chans->ctxld_addr + CTXLD_CTRL_STATUS);
}

static irqreturn_t dcss_irq_handler(int irq, void *dev_id)
{
	int ret;
	struct irq_desc *desc;
	uint32_t irq_status;
	unsigned long irqflags;
	struct dcss_info *info = (struct dcss_info *)dev_id;
	struct dcss_channels *chans = &info->chans;
	struct dcss_channel_info *chan;
	struct ctxld_fifo *cfifo;
	struct ctxld_commit *cc;

	cfifo = &info->cfifo;
	desc = irq_to_desc(irq);

	switch (desc->irq_data.hwirq) {
	case IRQ_DPR_CH1:
		chan = &chans->chan_info[0];
		irq_status = readl(info->base + chan->dpr_addr + 0x40);
		writel(irq_status, info->base + chan->dpr_addr + 0x40);
		break;
	case IRQ_DPR_CH2:
		break;
	case IRQ_DPR_CH3:
		break;
	case IRQ_CTX_LD:
		ctxld_irq_clear(info);
		complete(&cfifo->complete);
		break;
	case IRQ_TC_LINE1:
		dtg_irq_clear(IRQ_TC_LINE1, info);

		spin_lock_irqsave(&info->vinfo.vwait.lock, irqflags);

		/* unblock new commits */
		info->vinfo.vcount++;

		if (!list_empty(&cfifo->ctxld_list)) {
			cc = list_first_entry(&cfifo->ctxld_list,
					      struct ctxld_commit,
					      list);

			ret = kref_put(&cc->refcount, release_cc_locked);

			/* 'cc' can not be released */
			if (!ret)
				defer_flush_cfifo(cfifo);
		}

		spin_unlock_irqrestore(&info->vinfo.vwait.lock, irqflags);

		wake_up_all(&info->vinfo.vwait);
		break;
	case IRQ_DEC400D_CH1:
	case IRQ_DTRC_CH2:
	case IRQ_DTRC_CH3:
		break;
	}

	return IRQ_HANDLED;
}

static int dcss_interrupts_init(struct dcss_info *info)
{
	int i, ret = 0;
	struct irq_desc *desc;
	struct dcss_channels *chans = &info->chans;
	struct platform_device *pdev = info->pdev;

	for (i = 0; i < DCSS_IRQS_NUM; i++) {
		info->irqs[i] = platform_get_irq(pdev, i);
		if (info->irqs[i] < 0)
			break;

		desc = irq_to_desc(info->irqs[i]);
		switch (desc->irq_data.hwirq) {
		case 6:         /* CTX_LD */
			ctxld_irq_unmask(SB_HP_COMP_EN, info);
			break;
		case 8:		/* dtg_programmable_1: for vsync */
			/* TODO: (0, 0) or (last, last)? */
			writel(0x0, info->base + chans->dtg_addr + TC_LINE1_INT);
			dtg_irq_unmask(IRQ_TC_LINE1, info);
			break;
		default:	/* TODO: add support later */
			continue;
		}

		ret = devm_request_irq(&pdev->dev, info->irqs[i],
				dcss_irq_handler, 0,
				dev_name(&pdev->dev), info);
		if (ret) {
			dev_err(&pdev->dev, "request_irq (%d) failed with error %d\n",
				info->irqs[i], ret);
			return ret;
		}
	}

	if (i == 0)
		return -ENXIO;

	info->irqs_num = i + 1;

	return 0;
}

static void __iomem *dev_iomem_init(struct platform_device *pdev,
				    unsigned int res_idx)
{
	struct resource *res;

	if (res_idx > IORESOURCE_MEM_NUM)
		return NULL;

	res = platform_get_resource(pdev, IORESOURCE_MEM, res_idx);
	if (!res)
		return ERR_PTR(-ENODEV);

	return devm_ioremap_resource(&pdev->dev, res);
}

static int dcss_dispdrv_init(struct platform_device *pdev,
			     struct fb_info *fbi)
{
	struct dcss_channel_info *cinfo = fbi->par;
	struct dcss_info *info = cinfo->dev_data;
	struct mxc_dispdrv_setting setting;
	char disp_dev[NAME_LEN];

	memset(&setting, 0x0, sizeof(setting));
	setting.fbi = fbi;
	memcpy(disp_dev, info->disp_dev, strlen(info->disp_dev));
	disp_dev[strlen(info->disp_dev)] = '\0';

	info->dispdrv = mxc_dispdrv_gethandle(disp_dev, &setting);
	if (IS_ERR(info->dispdrv)) {
		dev_info(&pdev->dev, "no encoder driver exists\n");
		return -EPROBE_DEFER;
	}

	dev_info(&pdev->dev, "%s encoder registered success\n", disp_dev);

	return 0;
}

static int dcss_register_one_ch(uint32_t ch_id,
				struct dcss_info *info)
{
	int ret = 0;
	struct dcss_channels *chans;
	struct dcss_channel_info *cinfo;

	BUG_ON(ch_id > 2);

	chans = &info->chans;
	cinfo = &chans->chan_info[ch_id];

	cinfo->pdev = info->pdev;
	cinfo->dev_data = (void *)info;

	ret = fill_one_chan_info(ch_id, cinfo);
	if (ret) {
		dev_err(&info->pdev->dev, "register channel %d failed\n", ch_id);
		return ret;
	}

	return ret;
}

static int dcss_register_one_fb(struct dcss_channel_info *cinfo)
{
	int ret = 0;
	struct fb_info *fbi;

	ret = alloc_one_fbinfo(cinfo);
	if (ret) {
		dev_err(&cinfo->pdev->dev,
			"register fb %d failed\n", cinfo->channel_id);
		goto out;
	}

	fbi = cinfo->fb_info;
	ret = dcss_init_fbinfo(fbi);
	if (ret)
		goto out;

	init_chan_pixmap(cinfo);
	init_ch_pos(cinfo);

	if (cinfo->channel_id == 0) {
		ret = dcss_dispdrv_init(cinfo->pdev, fbi);
		if (ret == -EPROBE_DEFER) {
			dev_info(&cinfo->pdev->dev,
				 "Defer fb probe for encoder unready\n");
			goto out;
		}
	}

	ret = register_framebuffer(fbi);
	if (ret) {
		dev_err(&cinfo->pdev->dev, "failed to register fb%d\n",
			cinfo->channel_id);
		goto out;
        }

out:
	return ret;
}

static int read_dcss_properties(struct dcss_info *info)
{
	int ret = 0;
	uint32_t disp_mode;
	const char *disp_dev;
	struct platform_device *pdev = info->pdev;
	struct device_node *np = pdev->dev.of_node;

	/* read disp-mode */
	ret = of_property_read_u32(np, "disp-mode", &disp_mode);
	if (ret < 0) {
		dev_err(&pdev->dev, "invalid disp-mode provided in dtb\n");
		return -EINVAL;
	}

	info->dft_disp_mode = &imx_cea_mode[disp_mode];
	if (!info->dft_disp_mode->xres)
		return -EINVAL;

	/* read disp-dev */
	ret = of_property_read_string(np, "disp-dev", &disp_dev);
	if (!ret) {
		memcpy(info->disp_dev, disp_dev, strlen(disp_dev));
		dev_info(&pdev->dev, "%s: disp_dev = %s\n", __func__,
			 info->disp_dev);
	}

	return 0;
}

static int dcss_info_init(struct dcss_info *info)
{
	int ret = 0;
	struct platform_device *pdev = info->pdev;

	spin_lock_init(&info->llock);

	info->dcss_state = DCSS_STATE_RESET;

	ret = read_dcss_properties(info);
	if (ret)
		return -ENODEV;

	ret = dcss_init_chans(info);

	info->base = dev_iomem_init(pdev, 0);
	if (IS_ERR(info->base)) {
		ret = PTR_ERR(info->base);
		goto out;
	}

	info->blkctl_base = dev_iomem_init(pdev, 1);
	if (IS_ERR(info->blkctl_base)) {
		ret = PTR_ERR(info->blkctl_base);
		goto out;
	}

	ret = dcss_clks_get(info);
	if (ret)
		goto out;

	ret = dcss_clks_rate_set(info);
	if (ret)
		goto out;

	ret = dcss_clks_enable(info);
	if (ret)
		goto out;

	/* alloc ctxld fifo */
	ret = ctxld_fifo_alloc(&pdev->dev, &info->cfifo, DCSS_CFIFO_SIZE);
	if (ret) {
		dev_err(&pdev->dev, "ctxld fifo alloc failed\n");
		goto out;
	}

	info->cfifo.ctxld_wq = alloc_ordered_workqueue("ctxld-wq", WQ_FREEZABLE);
	if (!info->cfifo.ctxld_wq) {
		dev_err(&pdev->dev, "allocate ctxld wq failed\n");
		ret = -EINVAL;
		goto free_cfifo;
	}

	platform_set_drvdata(pdev, info);
	init_waitqueue_head(&info->vinfo.vwait);
	info->vinfo.vcount = 0;

	goto out;

free_cfifo:
	ctxld_fifo_free(&pdev->dev, &info->cfifo);
out:
	return ret;
}

static int dcss_enable_encoder(struct dcss_info *info)
{
	int ret = 0;
	struct fb_info *main_fbinfo;
	struct platform_device *pdev;

	if (!info->dispdrv)
		goto out;

	pdev = info->pdev;
	main_fbinfo = get_one_fbinfo(0, &info->chans);

	if (info->dispdrv->drv->setup) {
		ret = info->dispdrv->drv->setup(info->dispdrv, main_fbinfo);
		if (ret < 0) {
			dev_err(&pdev->dev, "setup encoder failed: %d\n", ret);
			goto out;
		}
	}

	if (info->dispdrv->drv->enable) {
		ret = info->dispdrv->drv->enable(info->dispdrv, main_fbinfo);
		if (ret < 0) {
			dev_err(&pdev->dev, "enable encoder failed: %d\n", ret);
			goto out;
		}
	}

out:
	return ret;
}

static void dcss_fix_data_config(struct dcss_info *info)
{
	int i, esize;

	esize = sizeof(struct data_unit);

	/* SCALER COEFFS config */
	for (i = 0; i < sizeof(scaler_coeffs_ch0) / esize; i++)
		writel(scaler_coeffs_ch0[i].data,
		       info->base + scaler_coeffs_ch0[i].addr);

	for (i = 0; i < sizeof(scaler_coeffs_ch1) / esize; i++)
		writel(scaler_coeffs_ch1[i].data,
		       info->base + scaler_coeffs_ch1[i].addr);

	/* HDR10 PIPE1 config */
	for (i = 0; i < sizeof(hdr10_pipe1_lut_a0) / esize; i++)
		writel(hdr10_pipe1_lut_a0[i].data,
		       info->base + hdr10_pipe1_lut_a0[i].addr);

	for (i = 0; i < sizeof(hdr10_pipe1_lut_a1) / esize; i++)
		writel(hdr10_pipe1_lut_a1[i].data,
		       info->base + hdr10_pipe1_lut_a1[i].addr);

	for (i = 0; i < sizeof(hdr10_pipe1_lut_a2) / esize; i++)
		writel(hdr10_pipe1_lut_a2[i].data,
		       info->base + hdr10_pipe1_lut_a2[i].addr);

	for (i = 0; i < sizeof(hdr10_pipe1_csca) / esize; i++)
		writel(hdr10_pipe1_csca[i].data,
		       info->base + hdr10_pipe1_csca[i].addr);

	for (i = 0; i < sizeof(hdr10_pipe1_cscb) / esize; i++)
		writel(hdr10_pipe1_cscb[i].data,
		       info->base + hdr10_pipe1_cscb[i].addr);

	/* HDR10 PIPE2 config */
	for (i = 0; i < sizeof(hdr10_pipe2_lut_a0) / esize; i++)
		writel(hdr10_pipe2_lut_a0[i].data,
		       info->base + hdr10_pipe2_lut_a0[i].addr);

	for (i = 0; i < sizeof(hdr10_pipe2_lut_a1) / esize; i++)
		writel(hdr10_pipe2_lut_a1[i].data,
		       info->base + hdr10_pipe2_lut_a1[i].addr);

	for (i = 0; i < sizeof(hdr10_pipe2_lut_a2) / esize; i++)
		writel(hdr10_pipe2_lut_a2[i].data,
		       info->base + hdr10_pipe2_lut_a2[i].addr);

	for (i = 0; i < sizeof(hdr10_pipe2_csca) / esize; i++)
		writel(hdr10_pipe2_csca[i].data,
		       info->base + hdr10_pipe2_csca[i].addr);

	for (i = 0; i < sizeof(hdr10_pipe2_cscb) / esize; i++)
		writel(hdr10_pipe2_cscb[i].data,
		       info->base + hdr10_pipe2_cscb[i].addr);

	/* HDR10 OPIPE config */
	for (i = 0; i < sizeof(hdr10_opipe_a0) / esize; i++)
		writel(hdr10_opipe_a0[i].data,
		       info->base + hdr10_opipe_a0[i].addr);

	for (i = 0; i < sizeof(hdr10_opipe_a1) / esize; i++)
		writel(hdr10_opipe_a1[i].data,
		       info->base + hdr10_opipe_a1[i].addr);

	for (i = 0; i < sizeof(hdr10_opipe_a2) / esize; i++)
		writel(hdr10_opipe_a2[i].data,
		       info->base + hdr10_opipe_a2[i].addr);

	for (i = 0; i < sizeof(hdr10_opipe_csco) / esize; i++)
		writel(hdr10_opipe_csco[i].data,
		       info->base + hdr10_opipe_csco[i].addr);
}

static int dcss_probe(struct platform_device *pdev)
{
	int ret = 0;
	struct dcss_info *info;
	struct fb_info *m_fbinfo;

	info = devm_kzalloc(&pdev->dev, sizeof(struct dcss_info), GFP_KERNEL);
	if (!info)
		return -ENOMEM;
	info->pdev = pdev;

	ret = dcss_info_init(info);
	if (ret)
		goto kfree_info;

	/* TODO: reset DCSS to make it clean */

	/* Clocks select: before dcss de-resets */
	if (!strcmp(info->disp_dev, "hdmi_disp"))
		/* HDMI */
		writel(0x0, info->blkctl_base + 0x10);
	else
		/* MIPI DSI */
		writel(0x101, info->blkctl_base + 0x10);

	/* Pull DCSS out of resets */
	writel(0xffffffff, info->blkctl_base + 0x0);

	/* TODO: config fixed data for DCSS */
	dcss_fix_data_config(info);

	dcss_interrupts_init(info);

	ret = dcss_dtg_start(info);
	if (ret)
		goto kfree_info;

	/* register channel 0: graphic */
	ret = dcss_register_one_ch(0, info);
	if (ret)
		goto kfree_info;

	/* register fb 0 */
	ret = dcss_register_one_fb(&info->chans.chan_info[0]);
	if (ret)
		goto unregister_ch0;

	/* enable encoder if exists */
	dcss_enable_encoder(info);

	ret = dcss_subsam_config(info);
	if (ret)
		goto unregister_fb0;

	/* register channel 1: video */
	ret = dcss_register_one_ch(1, info);
	if (ret)
		goto unregister_fb0;

	/* register fb 1 */
	ret = dcss_register_one_fb(&info->chans.chan_info[1]);
	if (ret)
		goto unregister_ch1;

	/* unblank fb0 */
	m_fbinfo = get_one_fbinfo(0, &info->chans);
	dcss_blank(FB_BLANK_UNBLANK, m_fbinfo);

	/* init fb1 */
	dcss_set_par(get_one_fbinfo(1, &info->chans));

	goto out;

unregister_ch1:
	/* TODO: add later */
	;
unregister_fb0:
	framebuffer_release(get_one_fbinfo(0, &info->chans));
unregister_ch0:
	/* TODO: add later */
	;
kfree_info:
	devm_kfree(&pdev->dev, info);
out:
	return ret;
}

static int dcss_remove(struct platform_device *pdev)
{
	return 0;
}

static void dcss_shutdown(struct platform_device *pdev)
{
}

static struct platform_driver dcss_driver = {
	.probe  = dcss_probe,
	.remove = dcss_remove,
	.shutdown = dcss_shutdown,
	.driver = {
		.name = "dcss_fb",
		.of_match_table = dcss_dt_ids,
	},
};

module_platform_driver(dcss_driver);

MODULE_DESCRIPTION("NXP DCSS framebuffer driver");
MODULE_LICENSE("GPL");