aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ata/pata_it8213.c
Commit message (Expand)AuthorAge
* pata_it8213: Cable detectAlan Cox2007-04-28
* libata: add missing CONFIG_PM in LLDsTejun Heo2007-03-02
* libata: add another IRQ calls (libata drivers)Akira Iguchi2007-02-09
* libata: convert to iomapTejun Heo2007-02-09
* libata: update libata LLDs to use devresTejun Heo2007-02-09
* [PATCH] pata_it8213: Add new driver for the IT8213 cardAlan2007-02-09
/a> 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663
/*
 * drivers/video/tegra/dc/dc.c
 *
 * Copyright (C) 2010 Google, Inc.
 * Author: Erik Gilling <konkers@android.com>
 *
 * Copyright (c) 2010-2016, NVIDIA CORPORATION, All rights reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/pm.h>
#include <linux/tegra-pm.h>
#include <linux/pm_runtime.h>
#include <linux/clk.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/workqueue.h>
#include <linux/ktime.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/backlight.h>
#include <linux/gpio.h>
#include <linux/nvhost.h>
#include <linux/clk/tegra.h>
#include <video/tegrafb.h>
#include <drm/drm_fixed.h>
#ifdef CONFIG_SWITCH
#include <linux/switch.h>
#endif
#include <linux/of_irq.h>
#include <linux/of_address.h>
#include <linux/tegra_pm_domains.h>
#include <linux/uaccess.h>
#include <linux/ote_protocol.h>
#include <linux/tegra-timer.h>

#define CREATE_TRACE_POINTS
#include <trace/events/display.h>
EXPORT_TRACEPOINT_SYMBOL(display_writel);
EXPORT_TRACEPOINT_SYMBOL(display_readl);

#include <mach/dc.h>
#include <mach/fb.h>
#include <linux/nvhost.h>
#include <linux/nvhost_ioctl.h>

#include <linux/platform/tegra/latency_allowance.h>
#include <linux/platform/tegra/mc.h>
#include <soc/tegra/tegra_bpmp.h>

#include "dc_reg.h"
#include "dc_config.h"
#include "dc_priv.h"
#include "dc_shared_isr.h"
#include "dev.h"
#include "nvhost_sync.h"
#include "nvsd.h"
#include "nvsd2.h"
#include "dpaux.h"
#include "nvsr.h"

#ifdef CONFIG_ADF_TEGRA
#include "tegra_adf.h"
#endif

#include "edid.h"

#ifdef CONFIG_TEGRA_DC_FAKE_PANEL_SUPPORT
#include "fake_panel.h"
#include "null_or.h"
#endif /*CONFIG_TEGRA_DC_FAKE_PANEL_SUPPORT*/


/* HACK! This needs to come from DT */
#include "../../../../arch/arm/mach-tegra/iomap.h"

#define TEGRA_CRC_LATCHED_DELAY		34

#define DC_COM_PIN_OUTPUT_POLARITY1_INIT_VAL	0x01000000
#define DC_COM_PIN_OUTPUT_POLARITY3_INIT_VAL	0x0

#define MAX_VRR_V_FRONT_PORCH			0x1000

#ifndef CONFIG_TEGRA_NVDISPLAY
static struct of_device_id tegra_disa_pd[] = {
	{ .compatible = "nvidia,tegra186-disa-pd", },
	{ .compatible = "nvidia,tegra210-disa-pd", },
	{ .compatible = "nvidia,tegra132-disa-pd", },
	{},
};

static struct of_device_id tegra_disb_pd[] = {
	{ .compatible = "nvidia,tegra186-disb-pd", },
	{ .compatible = "nvidia,tegra210-disb-pd", },
	{ .compatible = "nvidia,tegra132-disb-pd", },
	{},
};
#endif

struct fb_videomode tegra_dc_vga_mode = {
	.refresh = 60,
	.xres = 640,
	.yres = 480,
	.pixclock = KHZ2PICOS(25200),
	.hsync_len = 96,	/* h_sync_width */
	.vsync_len = 2,		/* v_sync_width */
	.left_margin = 48,	/* h_back_porch */
	.upper_margin = 33,	/* v_back_porch */
	.right_margin = 16,	/* h_front_porch */
	.lower_margin = 10,	/* v_front_porch */
	.vmode = 0,
	.sync = 0,
};

/* needs to be big enough to be index by largest supported out->type */
static struct tegra_dc_mode override_disp_mode[TEGRA_DC_OUT_NULL + 1];

static void _tegra_dc_controller_disable(struct tegra_dc *dc);
static void tegra_dc_disable_irq_ops(struct tegra_dc *dc, bool from_irq);
static void tegra_dc_sor_instance(struct tegra_dc *dc, int out_type);
static int _tegra_dc_config_frame_end_intr(struct tegra_dc *dc, bool enable);

static int tegra_dc_set_out(struct tegra_dc *dc, struct tegra_dc_out *out);
#ifdef PM
static int tegra_dc_suspend(struct platform_device *ndev, pm_message_t state);
static int tegra_dc_resume(struct platform_device *ndev);
#endif

static struct tegra_dc *tegra_dcs[TEGRA_MAX_DC];

#ifdef CONFIG_TEGRA_NVDISPLAY
static struct tegra_dc_win	tegra_dc_windows[DC_N_WINDOWS];
#endif


static DEFINE_MUTEX(tegra_dc_lock);
#ifdef CONFIG_ARCH_TEGRA_2x_SOC
static DEFINE_MUTEX(shared_lock);
#endif

static struct device_dma_parameters tegra_dc_dma_parameters = {
	.max_segment_size = UINT_MAX,
};

static const struct {
	bool h;
	bool v;
} can_filter[] = {
	/* Window A has no filtering */
	{ false, false },
	/* Window B has both H and V filtering */
	{ true,  true  },
	/* Window C has only H filtering */
	{ false, true  },
};

#ifdef CONFIG_TEGRA_DC_CMU
static struct tegra_dc_cmu default_cmu = {
	/* lut1 maps sRGB to linear space. */
	{
		0,    1,    2,    4,    5,    6,    7,    9,
		10,   11,   12,   14,   15,   16,   18,   20,
		21,   23,   25,   27,   29,   31,   33,   35,
		37,   40,   42,   45,   48,   50,   53,   56,
		59,   62,   66,   69,   72,   76,   79,   83,
		87,   91,   95,   99,   103,  107,  112,  116,
		121,  126,  131,  136,  141,  146,  151,  156,
		162,  168,  173,  179,  185,  191,  197,  204,
		210,  216,  223,  230,  237,  244,  251,  258,
		265,  273,  280,  288,  296,  304,  312,  320,
		329,  337,  346,  354,  363,  372,  381,  390,
		400,  409,  419,  428,  438,  448,  458,  469,
		479,  490,  500,  511,  522,  533,  544,  555,
		567,  578,  590,  602,  614,  626,  639,  651,
		664,  676,  689,  702,  715,  728,  742,  755,
		769,  783,  797,  811,  825,  840,  854,  869,
		884,  899,  914,  929,  945,  960,  976,  992,
		1008, 1024, 1041, 1057, 1074, 1091, 1108, 1125,
		1142, 1159, 1177, 1195, 1213, 1231, 1249, 1267,
		1286, 1304, 1323, 1342, 1361, 1381, 1400, 1420,
		1440, 1459, 1480, 1500, 1520, 1541, 1562, 1582,
		1603, 1625, 1646, 1668, 1689, 1711, 1733, 1755,
		1778, 1800, 1823, 1846, 1869, 1892, 1916, 1939,
		1963, 1987, 2011, 2035, 2059, 2084, 2109, 2133,
		2159, 2184, 2209, 2235, 2260, 2286, 2312, 2339,
		2365, 2392, 2419, 2446, 2473, 2500, 2527, 2555,
		2583, 2611, 2639, 2668, 2696, 2725, 2754, 2783,
		2812, 2841, 2871, 2901, 2931, 2961, 2991, 3022,
		3052, 3083, 3114, 3146, 3177, 3209, 3240, 3272,
		3304, 3337, 3369, 3402, 3435, 3468, 3501, 3535,
		3568, 3602, 3636, 3670, 3705, 3739, 3774, 3809,
		3844, 3879, 3915, 3950, 3986, 4022, 4059, 4095,
	},
	/* csc */
	{
		0x100, 0x0,   0x0,
		0x0,   0x100, 0x0,
		0x0,   0x0,   0x100,
	},
	/* lut2 maps linear space to sRGB*/
	{
		0,    1,    2,    2,    3,    4,    5,    6,
		6,    7,    8,    9,    10,   10,   11,   12,
		13,   13,   14,   15,   15,   16,   16,   17,
		18,   18,   19,   19,   20,   20,   21,   21,
		22,   22,   23,   23,   23,   24,   24,   25,
		25,   25,   26,   26,   27,   27,   27,   28,
		28,   29,   29,   29,   30,   30,   30,   31,
		31,   31,   32,   32,   32,   33,   33,   33,
		34,   34,   34,   34,   35,   35,   35,   36,
		36,   36,   37,   37,   37,   37,   38,   38,
		38,   38,   39,   39,   39,   40,   40,   40,
		40,   41,   41,   41,   41,   42,   42,   42,
		42,   43,   43,   43,   43,   43,   44,   44,
		44,   44,   45,   45,   45,   45,   46,   46,
		46,   46,   46,   47,   47,   47,   47,   48,
		48,   48,   48,   48,   49,   49,   49,   49,
		49,   50,   50,   50,   50,   50,   51,   51,
		51,   51,   51,   52,   52,   52,   52,   52,
		53,   53,   53,   53,   53,   54,   54,   54,
		54,   54,   55,   55,   55,   55,   55,   55,
		56,   56,   56,   56,   56,   57,   57,   57,
		57,   57,   57,   58,   58,   58,   58,   58,
		58,   59,   59,   59,   59,   59,   59,   60,
		60,   60,   60,   60,   60,   61,   61,   61,
		61,   61,   61,   62,   62,   62,   62,   62,
		62,   63,   63,   63,   63,   63,   63,   64,
		64,   64,   64,   64,   64,   64,   65,   65,
		65,   65,   65,   65,   66,   66,   66,   66,
		66,   66,   66,   67,   67,   67,   67,   67,
		67,   67,   68,   68,   68,   68,   68,   68,
		68,   69,   69,   69,   69,   69,   69,   69,
		70,   70,   70,   70,   70,   70,   70,   71,
		71,   71,   71,   71,   71,   71,   72,   72,
		72,   72,   72,   72,   72,   72,   73,   73,
		73,   73,   73,   73,   73,   74,   74,   74,
		74,   74,   74,   74,   74,   75,   75,   75,
		75,   75,   75,   75,   75,   76,   76,   76,
		76,   76,   76,   76,   77,   77,   77,   77,
		77,   77,   77,   77,   78,   78,   78,   78,
		78,   78,   78,   78,   78,   79,   79,   79,
		79,   79,   79,   79,   79,   80,   80,   80,
		80,   80,   80,   80,   80,   81,   81,   81,
		81,   81,   81,   81,   81,   81,   82,   82,
		82,   82,   82,   82,   82,   82,   83,   83,
		83,   83,   83,   83,   83,   83,   83,   84,
		84,   84,   84,   84,   84,   84,   84,   84,
		85,   85,   85,   85,   85,   85,   85,   85,
		85,   86,   86,   86,   86,   86,   86,   86,
		86,   86,   87,   87,   87,   87,   87,   87,
		87,   87,   87,   88,   88,   88,   88,   88,
		88,   88,   88,   88,   88,   89,   89,   89,
		89,   89,   89,   89,   89,   89,   90,   90,
		90,   90,   90,   90,   90,   90,   90,   90,
		91,   91,   91,   91,   91,   91,   91,   91,
		91,   91,   92,   92,   92,   92,   92,   92,
		92,   92,   92,   92,   93,   93,   93,   93,
		93,   93,   93,   93,   93,   93,   94,   94,
		94,   94,   94,   94,   94,   94,   94,   94,
		95,   95,   95,   95,   95,   95,   95,   95,
		95,   95,   96,   96,   96,   96,   96,   96,
		96,   96,   96,   96,   96,   97,   97,   97,
		97,   97,   97,   97,   97,   97,   97,   98,
		98,   98,   98,   98,   98,   98,   98,   98,
		98,   98,   99,   99,   99,   99,   99,   99,
		99,   100,  101,  101,  102,  103,  103,  104,
		105,  105,  106,  107,  107,  108,  109,  109,
		110,  111,  111,  112,  113,  113,  114,  115,
		115,  116,  116,  117,  118,  118,  119,  119,
		120,  120,  121,  122,  122,  123,  123,  124,
		124,  125,  126,  126,  127,  127,  128,  128,
		129,  129,  130,  130,  131,  131,  132,  132,
		133,  133,  134,  134,  135,  135,  136,  136,
		137,  137,  138,  138,  139,  139,  140,  140,
		141,  141,  142,  142,  143,  143,  144,  144,
		145,  145,  145,  146,  146,  147,  147,  148,
		148,  149,  149,  150,  150,  150,  151,  151,
		152,  152,  153,  153,  153,  154,  154,  155,
		155,  156,  156,  156,  157,  157,  158,  158,
		158,  159,  159,  160,  160,  160,  161,  161,
		162,  162,  162,  163,  163,  164,  164,  164,
		165,  165,  166,  166,  166,  167,  167,  167,
		168,  168,  169,  169,  169,  170,  170,  170,
		171,  171,  172,  172,  172,  173,  173,  173,
		174,  174,  174,  175,  175,  176,  176,  176,
		177,  177,  177,  178,  178,  178,  179,  179,
		179,  180,  180,  180,  181,  181,  182,  182,
		182,  183,  183,  183,  184,  184,  184,  185,
		185,  185,  186,  186,  186,  187,  187,  187,
		188,  188,  188,  189,  189,  189,  189,  190,
		190,  190,  191,  191,  191,  192,  192,  192,
		193,  193,  193,  194,  194,  194,  195,  195,
		195,  196,  196,  196,  196,  197,  197,  197,
		198,  198,  198,  199,  199,  199,  200,  200,
		200,  200,  201,  201,  201,  202,  202,  202,
		202,  203,  203,  203,  204,  204,  204,  205,
		205,  205,  205,  206,  206,  206,  207,  207,
		207,  207,  208,  208,  208,  209,  209,  209,
		209,  210,  210,  210,  211,  211,  211,  211,
		212,  212,  212,  213,  213,  213,  213,  214,
		214,  214,  214,  215,  215,  215,  216,  216,
		216,  216,  217,  217,  217,  217,  218,  218,
		218,  219,  219,  219,  219,  220,  220,  220,
		220,  221,  221,  221,  221,  222,  222,  222,
		223,  223,  223,  223,  224,  224,  224,  224,
		225,  225,  225,  225,  226,  226,  226,  226,
		227,  227,  227,  227,  228,  228,  228,  228,
		229,  229,  229,  229,  230,  230,  230,  230,
		231,  231,  231,  231,  232,  232,  232,  232,
		233,  233,  233,  233,  234,  234,  234,  234,
		235,  235,  235,  235,  236,  236,  236,  236,
		237,  237,  237,  237,  238,  238,  238,  238,
		239,  239,  239,  239,  240,  240,  240,  240,
		240,  241,  241,  241,  241,  242,  242,  242,
		242,  243,  243,  243,  243,  244,  244,  244,
		244,  244,  245,  245,  245,  245,  246,  246,
		246,  246,  247,  247,  247,  247,  247,  248,
		248,  248,  248,  249,  249,  249,  249,  249,
		250,  250,  250,  250,  251,  251,  251,  251,
		251,  252,  252,  252,  252,  253,  253,  253,
		253,  253,  254,  254,  254,  254,  255,  255,
	},
};

static struct tegra_dc_cmu default_limited_cmu = {
	/* lut1 maps sRGB to linear space. */
	{
		0,    1,    2,    4,    5,    6,    7,    9,
		10,   11,   12,   14,   15,   16,   18,   20,
		21,   23,   25,   27,   29,   31,   33,   35,
		37,   40,   42,   45,   48,   50,   53,   56,
		59,   62,   66,   69,   72,   76,   79,   83,
		87,   91,   95,   99,   103,  107,  112,  116,
		121,  126,  131,  136,  141,  146,  151,  156,
		162,  168,  173,  179,  185,  191,  197,  204,
		210,  216,  223,  230,  237,  244,  251,  258,
		265,  273,  280,  288,  296,  304,  312,  320,
		329,  337,  346,  354,  363,  372,  381,  390,
		400,  409,  419,  428,  438,  448,  458,  469,
		479,  490,  500,  511,  522,  533,  544,  555,
		567,  578,  590,  602,  614,  626,  639,  651,
		664,  676,  689,  702,  715,  728,  742,  755,
		769,  783,  797,  811,  825,  840,  854,  869,
		884,  899,  914,  929,  945,  960,  976,  992,
		1008, 1024, 1041, 1057, 1074, 1091, 1108, 1125,
		1142, 1159, 1177, 1195, 1213, 1231, 1249, 1267,
		1286, 1304, 1323, 1342, 1361, 1381, 1400, 1420,
		1440, 1459, 1480, 1500, 1520, 1541, 1562, 1582,
		1603, 1625, 1646, 1668, 1689, 1711, 1733, 1755,
		1778, 1800, 1823, 1846, 1869, 1892, 1916, 1939,
		1963, 1987, 2011, 2035, 2059, 2084, 2109, 2133,
		2159, 2184, 2209, 2235, 2260, 2286, 2312, 2339,
		2365, 2392, 2419, 2446, 2473, 2500, 2527, 2555,
		2583, 2611, 2639, 2668, 2696, 2725, 2754, 2783,
		2812, 2841, 2871, 2901, 2931, 2961, 2991, 3022,
		3052, 3083, 3114, 3146, 3177, 3209, 3240, 3272,
		3304, 3337, 3369, 3402, 3435, 3468, 3501, 3535,
		3568, 3602, 3636, 3670, 3705, 3739, 3774, 3809,
		3844, 3879, 3915, 3950, 3986, 4022, 4059, 4095,
	},
	/* csc */
	{
		0x100, 0x000, 0x000,
		0x000, 0x100, 0x000,
		0x000, 0x000, 0x100,
	},
	/*
	 * lut2 maps linear space back to sRGB, where
	 * the output range is [16...235] (limited).
	 */
	{
		16,  17,  18,  18,  19,  19,  20,  21,
		21,  22,  23,  24,  25,  25,  25,  26,
		27,  27,  28,  28,  29,  30,  30,  31,
		31,  31,  31,  32,  32,  33,  33,  34,
		34,  35,  35,  36,  36,  37,  37,  37,
		37,  37,  38,  38,  38,  39,  39,  39,
		40,  40,  41,  41,  41,  42,  42,  42,
		43,  43,  43,  43,  43,  43,  44,  44,
		44,  44,  45,  45,  45,  46,  46,  46,
		47,  47,  47,  47,  48,  48,  48,  49,
		49,  49,  49,  49,  49,  49,  49,  50,
		50,  50,  50,  51,  51,  51,  51,  52,
		52,  52,  52,  53,  53,  53,  53,  54,
		54,  54,  54,  55,  55,  55,  55,  55,
		56,  56,  56,  56,  56,  56,  56,  56,
		56,  57,  57,  57,  57,  57,  58,  58,
		58,  58,  58,  59,  59,  59,  59,  59,
		60,  60,  60,  60,  60,  61,  61,  61,
		61,  61,  62,  62,  62,  62,  62,  62,
		62,  62,  62,  62,  63,  63,  63,  63,
		63,  63,  64,  64,  64,  64,  64,  64,
		65,  65,  65,  65,  65,  66,  66,  66,
		66,  66,  66,  67,  67,  67,  67,  67,
		67,  68,  68,  68,  68,  68,  68,  68,
		68,  68,  68,  68,  68,  69,  69,  69,
		69,  69,  69,  69,  70,  70,  70,  70,
		70,  70,  71,  71,  71,  71,  71,  71,
		72,  72,  72,  72,  72,  72,  72,  73,
		73,  73,  73,  73,  73,  73,  74,  74,
		74,  74,  74,  74,  74,  74,  74,  74,
		74,  74,  74,  74,  75,  75,  75,  75,
		75,  75,  75,  76,  76,  76,  76,  76,
		76,  76,  77,  77,  77,  77,  77,  77,
		77,  78,  78,  78,  78,  78,  78,  78,
		78,  79,  79,  79,  79,  79,  79,  79,
		80,  80,  80,  80,  80,  80,  80,  80,
		80,  80,  80,  80,  80,  80,  80,  80,
		81,  81,  81,  81,  81,  81,  81,  81,
		82,  82,  82,  82,  82,  82,  82,  82,
		83,  83,  83,  83,  83,  83,  83,  83,
		84,  84,  84,  84,  84,  84,  84,  84,
		84,  85,  85,  85,  85,  85,  85,  85,
		85,  86,  86,  86,  86,  86,  86,  86,
		86,  86,  86,  86,  86,  86,  86,  86,
		86,  86,  87,  87,  87,  87,  87,  87,
		87,  87,  87,  88,  88,  88,  88,  88,
		88,  88,  88,  88,  89,  89,  89,  89,
		89,  89,  89,  89,  89,  90,  90,  90,
		90,  90,  90,  90,  90,  90,  91,  91,
		91,  91,  91,  91,  91,  91,  91,  91,
		92,  92,  92,  92,  92,  92,  92,  92,
		92,  92,  92,  92,  92,  92,  92,  92,
		92,  92,  92,  93,  93,  93,  93,  93,
		93,  93,  93,  93,  94,  94,  94,  94,
		94,  94,  94,  94,  94,  94,  95,  95,
		95,  95,  95,  95,  95,  95,  95,  95,
		96,  96,  96,  96,  96,  96,  96,  96,
		96,  96,  97,  97,  97,  97,  97,  97,
		97,  97,  97,  97,  97,  98,  98,  98,
		98,  98,  98,  98,  98,  98,  98,  98,
		98,  98,  98,  98,  98,  98,  98,  98,
		98,  98,  99,  99,  99,  99,  99,  99,
		99,  99,  99,  99, 100, 100, 100, 100,
		100, 100, 100, 100, 100, 100, 100, 101,
		101, 102, 103, 103, 104, 104, 105, 105,
		106, 107, 107, 108, 109, 109, 110, 110,
		110, 111, 111, 112, 113, 113, 114, 115,
		115, 116, 116, 116, 117, 117, 118, 118,
		119, 120, 120, 121, 121, 122, 122, 122,
		122, 123, 124, 124, 125, 125, 126, 126,
		127, 127, 128, 128, 129, 129, 129, 129,
		130, 130, 131, 131, 132, 132, 133, 133,
		134, 134, 135, 135, 135, 135, 136, 136,
		137, 137, 138, 138, 139, 139, 140, 140,
		141, 141, 141, 141, 141, 142, 142, 143,
		143, 144, 144, 144, 145, 145, 146, 146,
		147, 147, 147, 147, 147, 148, 148, 149,
		149, 149, 150, 150, 151, 151, 151, 152,
		152, 153, 153, 153, 153, 153, 154, 154,
		154, 155, 155, 156, 156, 156, 157, 157,
		158, 158, 158, 159, 159, 159, 159, 159,
		160, 160, 160, 161, 161, 162, 162, 162,
		163, 163, 163, 164, 164, 165, 165, 165,
		165, 165, 165, 166, 166, 166, 167, 167,
		167, 168, 168, 169, 169, 169, 170, 170,
		170, 171, 171, 171, 171, 171, 171, 172,
		172, 172, 173, 173, 173, 174, 174, 174,
		175, 175, 175, 176, 176, 176, 177, 177,
		177, 177, 177, 177, 178, 178, 178, 179,
		179, 179, 180, 180, 180, 181, 181, 181,
		181, 182, 182, 182, 183, 183, 183, 183,
		183, 183, 184, 184, 184, 185, 185, 185,
		185, 186, 186, 186, 187, 187, 187, 188,
		188, 188, 188, 189, 189, 189, 189, 189,
		189, 190, 190, 190, 190, 191, 191, 191,
		192, 192, 192, 193, 193, 193, 193, 194,
		194, 194, 195, 195, 195, 195, 195, 195,
		195, 196, 196, 196, 196, 197, 197, 197,
		197, 198, 198, 198, 199, 199, 199, 199,
		200, 200, 200, 201, 201, 201, 201, 202,
		202, 202, 202, 202, 202, 202, 203, 203,
		203, 203, 204, 204, 204, 204, 205, 205,
		205, 205, 206, 206, 206, 207, 207, 207,
		207, 208, 208, 208, 208, 208, 208, 208,
		208, 209, 209, 209, 209, 210, 210, 210,
		210, 211, 211, 211, 211, 212, 212, 212,
		212, 213, 213, 213, 213, 214, 214, 214,
		214, 214, 214, 214, 214, 215, 215, 215,
		215, 216, 216, 216, 216, 217, 217, 217,
		217, 218, 218, 218, 218, 219, 219, 219,
		219, 220, 220, 220, 220, 220, 220, 220,
		220, 221, 221, 221, 221, 221, 222, 222,
		222, 222, 223, 223, 223, 223, 224, 224,
		224, 224, 225, 225, 225, 225, 225, 226,
		226, 226, 226, 226, 226, 226, 226, 227,
		227, 227, 227, 227, 228, 228, 228, 228,
		229, 229, 229, 229, 230, 230, 230, 230,
		230, 231, 231, 231, 231, 232, 232, 232,
		232, 232, 232, 232, 232, 232, 233, 233,
		233, 233, 233, 234, 234, 234, 234, 235
	},
};
#elif defined(CONFIG_TEGRA_DC_CMU_V2)
static struct tegra_dc_cmu default_cmu = {
	{},
};
static struct tegra_dc_cmu default_limited_cmu = {
	{},
};
#endif

#define DSC_MAX_RC_BUF_THRESH_REGS	4
static int dsc_rc_buf_thresh_regs[DSC_MAX_RC_BUF_THRESH_REGS] = {
	DC_COM_DSC_RC_BUF_THRESH_0,
	DC_COM_DSC_RC_BUF_THRESH_1,
	DC_COM_DSC_RC_BUF_THRESH_2,
	DC_COM_DSC_RC_BUF_THRESH_3,
};

/*
 * Always set the first two values to 0. This is to ensure that RC threshold
 * values are programmed in the correct registers.
 */
static int dsc_rc_buf_thresh[] = {
	0, 0, 14, 28, 42, 56, 70, 84, 98, 105, 112, 119, 121,
	123, 125, 126,
};

#define DSC_MAX_RC_RANGE_CFG_REGS	8
static int dsc_rc_range_config[DSC_MAX_RC_RANGE_CFG_REGS] = {
	DC_COM_DSC_RC_RANGE_CFG_0,
	DC_COM_DSC_RC_RANGE_CFG_1,
	DC_COM_DSC_RC_RANGE_CFG_2,
	DC_COM_DSC_RC_RANGE_CFG_3,
	DC_COM_DSC_RC_RANGE_CFG_4,
	DC_COM_DSC_RC_RANGE_CFG_5,
	DC_COM_DSC_RC_RANGE_CFG_6,
	DC_COM_DSC_RC_RANGE_CFG_7,
};

static int dsc_rc_ranges_8bpp_8bpc[16][3] = {
	{0, 4, 2},
	{0, 4, 0},
	{1, 5, 0},
	{1, 6, -2},
	{3, 7, -4},
	{3, 7, -6},
	{3, 7, -8},
	{3, 8, -8},
	{3, 9, -8},
	{3, 10, -10},
	{5, 11, -10},
	{5, 12, -12},
	{5, 13, -12},
	{7, 13, -12},
	{13, 15, -12},
	{0, 0, 0},
};
void tegra_dc_clk_enable(struct tegra_dc *dc)
{
	tegra_disp_clk_prepare_enable(dc->clk);
#ifdef CONFIG_TEGRA_CORE_DVFS
	tegra_dvfs_set_rate(dc->clk, dc->mode.pclk);
#endif
}

void tegra_dc_clk_disable(struct tegra_dc *dc)
{
	tegra_disp_clk_disable_unprepare(dc->clk);
#ifdef CONFIG_TEGRA_CORE_DVFS
	tegra_dvfs_set_rate(dc->clk, 0);
#endif
}

static void tegra_dc_sor_instance(struct tegra_dc *dc, int out_type)
{
#if defined(CONFIG_ARCH_TEGRA_18x_SOC)
	/* check the dc_or_node to set the instance */
	if (!strcmp(dc_or_node_names[dc->ndev->id], "/host1x/sor"))
		dc->sor_instance = 0;
	else if (!strcmp(dc_or_node_names[dc->ndev->id], "/host1x/sor1"))
		dc->sor_instance = 1;
	else if (out_type == TEGRA_DC_OUT_FAKE_DP)
		/* Assign default instance to fake DP if
		SOR instance is not available */
		dc->sor_instance = 0;
	else
		dc->sor_instance = -1;
#else
	dc->sor_instance = dc->ndev->id;

	if (out_type == TEGRA_DC_OUT_HDMI)
		dc->sor_instance = 1;

#endif
}

void tegra_dc_get(struct tegra_dc *dc)
{
	int enable_count = atomic_inc_return(&dc->enable_count);

	BUG_ON(enable_count  < 1);
	if (enable_count == 1) {
		tegra_dc_io_start(dc);

		/* extra reference to dc clk */
		tegra_disp_clk_prepare_enable(dc->clk);
	}
}
EXPORT_SYMBOL(tegra_dc_get);

void tegra_dc_put(struct tegra_dc *dc)
{
	if (WARN_ONCE(atomic_read(&dc->enable_count) == 0,
		"unbalanced clock calls"))
		return;
	if (atomic_dec_return(&dc->enable_count) == 0) {
		/* balance extra dc clk reference */
		tegra_disp_clk_disable_unprepare(dc->clk);

		tegra_dc_io_end(dc);
	}
}
EXPORT_SYMBOL(tegra_dc_put);

unsigned tegra_dc_out_flags_from_dev(struct device *dev)
{
	struct platform_device *ndev = NULL;
	struct tegra_dc *dc = NULL;

	if (dev)
		ndev = to_platform_device(dev);
	if (ndev)
		dc = platform_get_drvdata(ndev);
	if (dc)
		return dc->out->flags;
	else
		return 0;
}
EXPORT_SYMBOL(tegra_dc_out_flags_from_dev);

bool tegra_dc_initialized(struct device *dev)
{
	struct platform_device *ndev = NULL;
	struct tegra_dc *dc = NULL;

	if (dev)
		ndev = to_platform_device(dev);
	if (ndev)
		dc = platform_get_drvdata(ndev);
	if (dc)
		return dc->initialized;
	else
		return false;
}
EXPORT_SYMBOL(tegra_dc_initialized);

void tegra_dc_hold_dc_out(struct tegra_dc *dc)
{
	if (1 == atomic_inc_return(&dc->holding)) {
		tegra_dc_get(dc);
		if (dc->out_ops && dc->out_ops->hold)
			dc->out_ops->hold(dc);
	}
}

void tegra_dc_release_dc_out(struct tegra_dc *dc)
{
	if (0 == atomic_dec_return(&dc->holding)) {
		if (dc->out_ops && dc->out_ops->release)
			dc->out_ops->release(dc);
		tegra_dc_put(dc);
	}
}

#define DUMP_REG(a) do {			\
	snprintf(buff, sizeof(buff), "%-32s\t%03x\t%08lx\n",  \
		 #a, a, tegra_dc_readl(dc, a));		      \
	print(data, buff);				      \
	} while (0)

#ifndef CONFIG_TEGRA_NVDISPLAY
void reg_dump(struct tegra_dc *dc, void *data,
		       void (* print)(void *data, const char *str))
{
	int i;
	char buff[256];
	const char winname[] = "ABCDHT";
	/* for above, see also: DC_CMD_DISPLAY_WINDOW_HEADER and DC_N_WINDOWS */
	unsigned long cmd_state;

	/* If gated, quietly return. */
	if (!tegra_powergate_is_powered(dc->powergate_id))
		return;

	mutex_lock(&dc->lock);
	tegra_dc_get(dc);
	cmd_state = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
	tegra_dc_writel(dc, WRITE_MUX_ACTIVE | READ_MUX_ACTIVE,
		DC_CMD_STATE_ACCESS);

	DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
	DUMP_REG(DC_CMD_DISPLAY_COMMAND);
	DUMP_REG(DC_CMD_SIGNAL_RAISE);
	DUMP_REG(DC_CMD_INT_STATUS);
	DUMP_REG(DC_CMD_INT_MASK);
	DUMP_REG(DC_CMD_INT_ENABLE);
	DUMP_REG(DC_CMD_INT_TYPE);
	DUMP_REG(DC_CMD_INT_POLARITY);
	DUMP_REG(DC_CMD_SIGNAL_RAISE1);
	DUMP_REG(DC_CMD_SIGNAL_RAISE2);
	DUMP_REG(DC_CMD_SIGNAL_RAISE3);
	DUMP_REG(DC_CMD_STATE_ACCESS);
	DUMP_REG(DC_CMD_STATE_CONTROL);
	DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
	DUMP_REG(DC_CMD_REG_ACT_CONTROL);

	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
	DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
	DUMP_REG(DC_DISP_MEM_HIGH_PRIORITY);
	DUMP_REG(DC_DISP_MEM_HIGH_PRIORITY_TIMER);
	DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
	DUMP_REG(DC_DISP_REF_TO_SYNC);
	DUMP_REG(DC_DISP_SYNC_WIDTH);
	DUMP_REG(DC_DISP_BACK_PORCH);
	DUMP_REG(DC_DISP_DISP_ACTIVE);
	DUMP_REG(DC_DISP_FRONT_PORCH);
	DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
	DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
	DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
	DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
	DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
	DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
	DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
	DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
	DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
	DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
	DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
	DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
	DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
	DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
	DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
	DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
	DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
	DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
	DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
	DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
	DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
	DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
	DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
	DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
	DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
	DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
	DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
	DUMP_REG(DC_DISP_M0_CONTROL);
	DUMP_REG(DC_DISP_M1_CONTROL);
	DUMP_REG(DC_DISP_DI_CONTROL);
	DUMP_REG(DC_DISP_PP_CONTROL);
	DUMP_REG(DC_DISP_PP_SELECT_A);
	DUMP_REG(DC_DISP_PP_SELECT_B);
	DUMP_REG(DC_DISP_PP_SELECT_C);
	DUMP_REG(DC_DISP_PP_SELECT_D);
	DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
	DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
	DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
	DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
	DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
	DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
	DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
#if !defined(CONFIG_TEGRA_DC_BLENDER_GEN2)
	DUMP_REG(DC_DISP_BORDER_COLOR);
#endif
	DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
	DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
	DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
	DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
	DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
	DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
	DUMP_REG(DC_DISP_CURSOR_START_ADDR);
	DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
#if defined(CONFIG_ARCH_TEGRA_12x_SOC) || defined(CONFIG_ARCH_TEGRA_21x_SOC)
	DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI);
	DUMP_REG(DC_DISP_CURSOR_START_ADDR_HI_NS);
#endif
	DUMP_REG(DC_DISP_CURSOR_POSITION);
	DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
	DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
	DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
	DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
	DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
	DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
	DUMP_REG(DC_DISP_MCCIF_DISPLAY0C_HYST);
#if defined(CONFIG_ARCH_TEGRA_2x_SOC) || defined(CONFIG_ARCH_TEGRA_3x_SOC)
	DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
#endif
	DUMP_REG(DC_DISP_DAC_CRT_CTRL);
	DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
#if defined(CONFIG_TEGRA_DC_INTERLACE)
	DUMP_REG(DC_DISP_INTERLACE_CONTROL);
	DUMP_REG(DC_DISP_INTERLACE_FIELD2_REF_TO_SYNC);
	DUMP_REG(DC_DISP_INTERLACE_FIELD2_SYNC_WIDTH);
	DUMP_REG(DC_DISP_INTERLACE_FIELD2_BACK_PORCH);
	DUMP_REG(DC_DISP_INTERLACE_FIELD2_FRONT_PORCH);
	DUMP_REG(DC_DISP_INTERLACE_FIELD2_DISP_ACTIVE);
#endif

	DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
	DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE2);
	DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY2);
	DUMP_REG(DC_COM_PIN_OUTPUT_DATA2);
	DUMP_REG(DC_COM_PIN_INPUT_ENABLE2);
	DUMP_REG(DC_COM_PIN_OUTPUT_SELECT5);
	DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
	DUMP_REG(DC_DISP_M1_CONTROL);
	DUMP_REG(DC_COM_PM1_CONTROL);
	DUMP_REG(DC_COM_PM1_DUTY_CYCLE);
	DUMP_REG(DC_DISP_SD_CONTROL);

#ifdef CONFIG_TEGRA_DC_CMU
	DUMP_REG(DC_COM_CMU_CSC_KRR);
	DUMP_REG(DC_COM_CMU_CSC_KGR);
	DUMP_REG(DC_COM_CMU_CSC_KBR);
	DUMP_REG(DC_COM_CMU_CSC_KRG);
	DUMP_REG(DC_COM_CMU_CSC_KGG);
	DUMP_REG(DC_COM_CMU_CSC_KBG);
	DUMP_REG(DC_COM_CMU_CSC_KRB);
	DUMP_REG(DC_COM_CMU_CSC_KGB);
	DUMP_REG(DC_COM_CMU_CSC_KBB);
#endif

	for_each_set_bit(i, &dc->valid_windows, DC_N_WINDOWS) {
		print(data, "\n");
		snprintf(buff, sizeof(buff), "WINDOW %c:\n", winname[i]);
		print(data, buff);

		tegra_dc_writel(dc, WINDOW_A_SELECT << i,
				DC_CMD_DISPLAY_WINDOW_HEADER);
		DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
		DUMP_REG(DC_WIN_WIN_OPTIONS);
		DUMP_REG(DC_WIN_BYTE_SWAP);
		DUMP_REG(DC_WIN_BUFFER_CONTROL);
		DUMP_REG(DC_WIN_COLOR_DEPTH);
		DUMP_REG(DC_WIN_POSITION);
		DUMP_REG(DC_WIN_SIZE);
		DUMP_REG(DC_WIN_PRESCALED_SIZE);
		DUMP_REG(DC_WIN_H_INITIAL_DDA);
		DUMP_REG(DC_WIN_V_INITIAL_DDA);
		DUMP_REG(DC_WIN_DDA_INCREMENT);
		DUMP_REG(DC_WIN_LINE_STRIDE);
#if defined(CONFIG_ARCH_TEGRA_2x_SOC) || defined(CONFIG_ARCH_TEGRA_3x_SOC)
		DUMP_REG(DC_WIN_BUF_STRIDE);
		DUMP_REG(DC_WIN_UV_BUF_STRIDE);
#endif
		DUMP_REG(DC_WIN_BLEND_NOKEY);
		DUMP_REG(DC_WIN_BLEND_1WIN);
		DUMP_REG(DC_WIN_BLEND_2WIN_X);
		DUMP_REG(DC_WIN_BLEND_2WIN_Y);
		DUMP_REG(DC_WIN_BLEND_3WIN_XY);
		DUMP_REG(DC_WIN_GLOBAL_ALPHA);
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_14x_SOC)
		DUMP_REG(DC_WINBUF_BLEND_LAYER_CONTROL);
#endif
		DUMP_REG(DC_WINBUF_START_ADDR);
		DUMP_REG(DC_WINBUF_START_ADDR_U);
		DUMP_REG(DC_WINBUF_START_ADDR_V);
		DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
		DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_14x_SOC)
		DUMP_REG(DC_WINBUF_START_ADDR_HI);
		DUMP_REG(DC_WINBUF_START_ADDR_HI_U);
		DUMP_REG(DC_WINBUF_START_ADDR_HI_V);
		DUMP_REG(DC_WINBUF_START_ADDR_FIELD2);
		DUMP_REG(DC_WINBUF_START_ADDR_FIELD2_U);
		DUMP_REG(DC_WINBUF_START_ADDR_FIELD2_V);
		DUMP_REG(DC_WINBUF_START_ADDR_FIELD2_HI);
		DUMP_REG(DC_WINBUF_START_ADDR_FIELD2_HI_U);
		DUMP_REG(DC_WINBUF_START_ADDR_FIELD2_HI_V);
		DUMP_REG(DC_WINBUF_ADDR_H_OFFSET_FIELD2);
		DUMP_REG(DC_WINBUF_ADDR_V_OFFSET_FIELD2);
#endif
		DUMP_REG(DC_WINBUF_UFLOW_STATUS);
		DUMP_REG(DC_WIN_CSC_YOF);
		DUMP_REG(DC_WIN_CSC_KYRGB);
		DUMP_REG(DC_WIN_CSC_KUR);
		DUMP_REG(DC_WIN_CSC_KVR);
		DUMP_REG(DC_WIN_CSC_KUG);
		DUMP_REG(DC_WIN_CSC_KVG);
		DUMP_REG(DC_WIN_CSC_KUB);
		DUMP_REG(DC_WIN_CSC_KVB);

#if defined(CONFIG_ARCH_TEGRA_21x_SOC)
		DUMP_REG(DC_WINBUF_CDE_CONTROL);
		DUMP_REG(DC_WINBUF_CDE_COMPTAG_BASE_0);
		DUMP_REG(DC_WINBUF_CDE_COMPTAG_BASEHI_0);
		DUMP_REG(DC_WINBUF_CDE_ZBC_COLOR_0);
		DUMP_REG(DC_WINBUF_CDE_SURFACE_OFFSET_0);
		DUMP_REG(DC_WINBUF_CDE_CTB_ENTRY_0);
		DUMP_REG(DC_WINBUF_CDE_CG_SW_OVR);
		DUMP_REG(DC_WINBUF_CDE_PM_CONTROL);
		DUMP_REG(DC_WINBUF_CDE_PM_COUNTER);
#endif
	}

	tegra_dc_writel(dc, cmd_state, DC_CMD_STATE_ACCESS);
	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
}
#endif	/* !CONFIG_TEGRA_NVDISPLAY */

#undef DUMP_REG

#ifdef DEBUG
static void dump_regs_print(void *data, const char *str)
{
	struct tegra_dc *dc = data;
	dev_dbg(&dc->ndev->dev, "%s", str);
}

void dump_regs(struct tegra_dc *dc)
{
	reg_dump(dc, dc, dump_regs_print);
}
#else /* !DEBUG */

void dump_regs(struct tegra_dc *dc) {}

#endif /* DEBUG */

#ifdef CONFIG_DEBUG_FS

static void dbg_regs_print(void *data, const char *str)
{
	struct seq_file *s = data;

	seq_printf(s, "%s", str);
}

#undef DUMP_REG

static int dbg_dc_show(struct seq_file *s, void *unused)
{
	struct tegra_dc *dc = s->private;

	reg_dump(dc, s, dbg_regs_print);

	return 0;
}


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

static const struct file_operations regs_fops = {
	.open		= dbg_dc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int dbg_dc_mode_show(struct seq_file *s, void *unused)
{
	struct tegra_dc *dc = s->private;
	struct tegra_dc_mode *m;

	mutex_lock(&dc->lock);
	m = &dc->mode;
	seq_printf(s,
		"pclk: %d\n"
		"h_ref_to_sync: %d\n"
		"v_ref_to_sync: %d\n"
		"h_sync_width: %d\n"
		"v_sync_width: %d\n"
		"h_back_porch: %d\n"
		"v_back_porch: %d\n"
		"h_active: %d\n"
		"v_active: %d\n"
		"h_front_porch: %d\n"
		"v_front_porch: %d\n"
		"flags: 0x%x\n"
		"stereo_mode: %d\n"
		"avi_m: 0x%x\n"
		"vmode: 0x%x\n",
		m->pclk, m->h_ref_to_sync, m->v_ref_to_sync,
		m->h_sync_width, m->v_sync_width,
		m->h_back_porch, m->v_back_porch,
		m->h_active, m->v_active,
		m->h_front_porch, m->v_front_porch,
		m->flags, m->stereo_mode, m->avi_m, m->vmode);
	mutex_unlock(&dc->lock);
	return 0;
}

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

static const struct file_operations mode_fops = {
	.open		= dbg_dc_mode_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int dbg_dc_stats_show(struct seq_file *s, void *unused)
{
	struct tegra_dc *dc = s->private;

	mutex_lock(&dc->lock);
	seq_printf(s,
		"underflows: %llu\n"
		"underflows_a: %llu\n"
		"underflows_b: %llu\n"
		"underflows_c: %llu\n",
		dc->stats.underflows,
		dc->stats.underflows_a,
		dc->stats.underflows_b,
		dc->stats.underflows_c);
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC)
	seq_printf(s,
		"underflows_d: %llu\n"
		"underflows_h: %llu\n"
		"underflows_t: %llu\n",
		dc->stats.underflows_d,
		dc->stats.underflows_h,
		dc->stats.underflows_t);
#endif
#if defined(CONFIG_TEGRA_NVDISPLAY)
	seq_printf(s,
		"underflow_frames: %llu\n",
		dc->stats.underflow_frames);
#endif
	mutex_unlock(&dc->lock);

	return 0;
}

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

static int dbg_dc_event_inject_show(struct seq_file *s, void *unused)
{
	return 0;
}

static ssize_t dbg_dc_event_inject_write(struct file *file,
	const char __user *addr, size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data; /* single_open() initialized */
	struct tegra_dc *dc = m ? m->private : NULL;
	long event;
	int ret;

	if (!dc)
		return -EINVAL;

	ret = kstrtol_from_user(addr, len, 10, &event);
	if (ret < 0)
		return ret;

	/*
	 * ADF has two seperate events for hotplug connect and disconnect.
	 * We map event 0x0, and 0x1 for them accordingly.  For DC_EXT,
	 * both events map to HOTPLUG.
	 */
#ifdef CONFIG_ADF_TEGRA
	if (event == 0x0)
		tegra_adf_process_hotplug_connected(dc->adf, NULL);
	else if (event == 0x1)
		tegra_adf_process_hotplug_disconnected(dc->adf);
	else if (event == 0x2)
		tegra_adf_process_bandwidth_renegotiate(dc->adf, 0);
	else {
		dev_err(&dc->ndev->dev, "Unknown event 0x%lx\n", event);
		return -EINVAL; /* unknown event number */
	}
#endif
#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	if (event == 0x0 || event == 0x1) /* TEGRA_DC_EXT_EVENT_HOTPLUG */
		tegra_dc_ext_process_hotplug(dc->ndev->id);
	else if (event == 0x2) /* TEGRA_DC_EXT_EVENT_BANDWIDTH_DEC */
		tegra_dc_ext_process_bandwidth_renegotiate(
				dc->ndev->id, NULL);
	else {
		dev_err(&dc->ndev->dev, "Unknown event 0x%lx\n", event);
		return -EINVAL; /* unknown event number */
	}
#endif
	return len;
}

/* Update the strings as dc.h get updated for new output types*/
static const char * const dc_outtype_strings[] = {
	"TEGRA_DC_OUT_RGB",
	"TEGRA_DC_OUT_HDMI",
	"TEGRA_DC_OUT_DSI",
	"TEGRA_DC_OUT_DP",
	"TEGRA_DC_OUT_LVDS",
	"TEGRA_DC_OUT_NVSR_DP",
	"TEGRA_DC_OUT_FAKE_DP",
	"TEGRA_DC_OUT_FAKE_DSIA",
	"TEGRA_DC_OUT_FAKE_DSIB",
	"TEGRA_DC_OUT_FAKE_DSI_GANGED",
	"TEGRA_DC_OUT_NULL",
	"TEGRA_DC_OUT_UNKNOWN"
};

static int dbg_dc_outtype_show(struct seq_file *s, void *unused)
{
	struct tegra_dc *dc = s->private;

	mutex_lock(&dc->lock);
	seq_puts(s, "\n");
	seq_printf(s,
		"\tDC OUTPUT: \t%s (%d)\n",
		dc_outtype_strings[dc->out->type], dc->out->type);
	seq_puts(s, "\n");
	mutex_unlock(&dc->lock);
	return 0;
}

/* Add specific variable related to each output type.
 * Save and reuse on changing the output type
 */
#if defined(CONFIG_TEGRA_DC_FAKE_PANEL_SUPPORT)
struct tegra_dc_out_info {
	struct tegra_dc_out_ops *out_ops;
	void *out_data;
	struct tegra_dc_out out;
	struct tegra_dc_mode mode;
	int fblistindex;
	struct tegra_edid *edid;
};

static struct tegra_dc_out_info dbg_dc_out_info[TEGRA_DC_OUT_MAX]
						[TEGRA_DC_OUT_MAX];
/* array for saving the out_type for each head */
static int  boot_out_type[] = {-1, -1, -1};

static int is_invalid_dc_out(struct tegra_dc *dc, long dc_outtype)
{
	if ((dc_outtype != boot_out_type[dc->ndev->id]) &&
		(dc_outtype != TEGRA_DC_OUT_FAKE_DP) &&
		(dc_outtype != TEGRA_DC_OUT_FAKE_DSIA) &&
		(dc_outtype != TEGRA_DC_OUT_FAKE_DSIB) &&
		(dc_outtype != TEGRA_DC_OUT_FAKE_DSI_GANGED) &&
		(dc_outtype != TEGRA_DC_OUT_NULL)) {
		dev_err(&dc->ndev->dev,
			"Request 0x%lx is unsupported target out_type\n",
			 dc_outtype);
		dev_err(&dc->ndev->dev,
			"boot_out_type[%d] is 0x%x\n",
			 dc->ndev->id, boot_out_type[dc->ndev->id]);
		return -EINVAL;
	}

	return 0;
}

static int is_valid_dsi_out(struct tegra_dc *dc, long dc_outtype)
{
	if (((dc_outtype >= TEGRA_DC_OUT_FAKE_DSIA) &&
		(dc_outtype <= TEGRA_DC_OUT_FAKE_DSI_GANGED)) ||
		(dc_outtype == TEGRA_DC_OUT_DSI))
			return 1;

	return 0;
}


static int is_valid_fake_support(struct tegra_dc *dc, long dc_outtype)
{
	if ((dc_outtype == TEGRA_DC_OUT_FAKE_DP) ||
		(dc_outtype == TEGRA_DC_OUT_FAKE_DSIA) ||
		(dc_outtype == TEGRA_DC_OUT_FAKE_DSIB) ||
		(dc_outtype == TEGRA_DC_OUT_FAKE_DSI_GANGED) ||
		(dc_outtype == TEGRA_DC_OUT_NULL))
		return 1;

	return 0;
}

static int set_avdd(struct tegra_dc *dc, long cur_out, long new_out)
{
#ifdef CONFIG_ARCH_TEGRA_21x_SOC
	/* T210 macro_clk is failing SOR access
	 * if avdd_lcd is not enabled
	 */
	bool is_enable = false;
	struct tegra_dc_out *dc_out =
		&dbg_dc_out_info[boot_out_type[dc->ndev->id]][new_out].out;

	/* cur is fake and new is fake - skip */
	if (is_valid_fake_support(dc, cur_out) &&
		is_valid_fake_support(dc, new_out))
		return 0;

	/* cur is valid and new is fake - enable */
	if (!is_valid_fake_support(dc, cur_out) &&
		is_valid_fake_support(dc, new_out))
		is_enable = true;

	if (is_enable) {
		if (dc_out && dc_out->enable)
			dc_out->enable(&dc->ndev->dev);
	} else {
		if (dc_out && dc_out->disable)
			dc_out->disable(&dc->ndev->dev);
	}
#endif
	return 0;
}
static ssize_t dbg_dc_out_type_set(struct file *file,
	const char __user *addr, size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data; /* single_open() initialized */
	struct tegra_dc *dc = m ? m->private : NULL;
	long cur_dc_out;
	long out_type;
	int ret = 0;
	bool  allocate = false;

	if (!dc)
		return -EINVAL;

	ret = kstrtol_from_user(addr, len, 10, &out_type);
	if (ret < 0)
		return ret;

	if (!dc->pdata->default_out)
		return -EINVAL;

	/* check out type is out of range then skip */
	if (out_type < TEGRA_DC_OUT_RGB ||
		out_type >= TEGRA_DC_OUT_MAX) {
		dev_err(&dc->ndev->dev, "Unknown out_type 0x%lx\n", out_type);
		return -EINVAL;
	}

	WARN_ON((sizeof(boot_out_type) / sizeof(int)) != TEGRA_MAX_DC);

	if (boot_out_type[dc->ndev->id] == -1)
		boot_out_type[dc->ndev->id] = dc->pdata->default_out->type;

	cur_dc_out = dc->pdata->default_out->type;

	/* Nothing to do if new outtype is same as old
	 * Allow to switch between booted out type and fake panel out
	 */
	if ((cur_dc_out == out_type) || is_invalid_dc_out(dc, out_type))
		return -EINVAL;

	/* disable the dc and output controllers */
	if (dc->enabled)
		tegra_dc_disable(dc);

	/* If output is already created - save it */
	if (dc->out_data) {
		dbg_dc_out_info[cur_dc_out][out_type].out_data = dc->out_data;
		dbg_dc_out_info[cur_dc_out][out_type].out_ops  = dc->out_ops;
		memcpy(&dbg_dc_out_info[cur_dc_out][out_type].out, dc->out,
					sizeof(struct tegra_dc_out));
		dbg_dc_out_info[cur_dc_out][out_type].mode = dc->mode;
		dbg_dc_out_info[cur_dc_out][out_type].edid = dc->edid;

		if (is_valid_dsi_out(dc, cur_dc_out) &&
			dbg_dc_out_info[cur_dc_out][out_type].out_data)
			tegra_dc_destroy_dsi_resources(dc, cur_dc_out);

		if (!is_valid_fake_support(dc, cur_dc_out))
			dbg_dc_out_info[cur_dc_out][out_type].fblistindex =
						tegra_fb_update_modelist(dc, 0);

		set_avdd(dc, cur_dc_out, out_type);
	}

	/* If output already created - reuse it */
	if (dbg_dc_out_info[out_type][cur_dc_out].out_data) {
		mutex_lock(&dc->lp_lock);
		mutex_lock(&dc->lock);

		/* Change the out type */
		dc->pdata->default_out->type = out_type;
		dc->out_ops = dbg_dc_out_info[out_type][cur_dc_out].out_ops;
		dc->out_data = dbg_dc_out_info[out_type][cur_dc_out].out_data;
		memcpy(dc->out, &dbg_dc_out_info[out_type][cur_dc_out].out,
						sizeof(struct tegra_dc_out));
		dc->mode = dbg_dc_out_info[out_type][cur_dc_out].mode;
		dc->edid = dbg_dc_out_info[out_type][cur_dc_out].edid;

		/* Re-init the resources that are destroyed for dsi */
		if (is_valid_dsi_out(dc, out_type))
			ret = tegra_dc_reinit_dsi_resources(dc, out_type);

		if (!is_valid_fake_support(dc, out_type))
			tegra_fb_update_modelist(dc,
					dbg_dc_out_info[out_type]
					[cur_dc_out].fblistindex);

		mutex_unlock(&dc->lock);
		mutex_unlock(&dc->lp_lock);

		if (ret) {
			dev_err(&dc->ndev->dev, "Failed to reinit!!!\n");
			return -EINVAL;
		}

	} else {
		/* Change the out type */
		dc->pdata->default_out->type = out_type;

		/* create new - now restricted to fake_dp only */
		if (out_type == TEGRA_DC_OUT_FAKE_DP) {

			/* set to default bpp */
			if (!dc->pdata->default_out->depth)
				dc->pdata->default_out->depth = 24;

			/* DP and Fake_DP use same data
			*  Reuse DP data for fake_DP */
			if (cur_dc_out != TEGRA_DC_OUT_DP) {
				allocate = true;
			}
		} else if ((out_type >= TEGRA_DC_OUT_FAKE_DSIA) &&
				(out_type <= TEGRA_DC_OUT_FAKE_DSI_GANGED)) {
			/* DSI and fake DSI use same data
			 * create new if not created yet
			 */
			if (!dc->pdata->default_out->depth)
				dc->pdata->default_out->depth = 18;

			allocate = true;
			tegra_dc_init_fakedsi_panel(dc, out_type);

		} else if (out_type == TEGRA_DC_OUT_NULL) {
			if (!dbg_dc_out_info[TEGRA_DC_OUT_NULL]
						[cur_dc_out].out_data) {
				allocate = true;
				tegra_dc_init_null_or(dc);
			}
		} else {
			/* set  back to existing one */
			dc->pdata->default_out->type = cur_dc_out;
			dev_err(&dc->ndev->dev, "Unknown type is asked\n");
			goto by_pass;
		}

		if (allocate) {
			ret = tegra_dc_set_out(dc, dc->pdata->default_out);
				if (ret < 0) {
					dev_err(&dc->ndev->dev,
					"Failed to initialize DC out ops\n");
					return -EINVAL;
				}
		}

		dbg_dc_out_info[out_type][cur_dc_out].out_ops = dc->out_ops;
		dbg_dc_out_info[out_type][cur_dc_out].out_data = dc->out_data;
		memcpy(&dbg_dc_out_info[out_type][cur_dc_out].out, dc->out,
						sizeof(struct tegra_dc_out));
		dbg_dc_out_info[out_type][cur_dc_out].mode = dc->mode;
		dbg_dc_out_info[out_type][cur_dc_out].edid = dc->edid;

	}

by_pass:
	/*enable the dc and output controllers */
	if (!dc->enabled)
		tegra_dc_enable(dc);

	return len;
}
#else
static ssize_t dbg_dc_out_type_set(struct file *file,
	const char __user *addr, size_t len, loff_t *pos)
{
	return -EINVAL;
}
#endif /*CONFIG_TEGRA_DC_FAKE_PANEL_SUPPORT*/

static const struct file_operations stats_fops = {
	.open		= dbg_dc_stats_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

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

static const struct file_operations event_inject_fops = {
	.open		= dbg_dc_event_inject_open,
	.read		= seq_read,
	.write		= dbg_dc_event_inject_write,
	.llseek		= seq_lseek,
	.release	= single_release,
};

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

static const struct file_operations outtype_fops = {
	.open		= dbg_dc_outtype_open,
	.read		= seq_read,
	.write		= dbg_dc_out_type_set,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int dbg_edid_show(struct seq_file *s, void *unused)
{
	struct tegra_dc *dc = s->private;
	struct tegra_edid *edid = dc->edid;
	struct tegra_dc_edid *data;
	u8 *buf;
	int i;

	if (WARN_ON(!dc || !dc->out || !dc->edid))
		return -EINVAL;

	data = tegra_edid_get_data(edid);
	if (!data) {
		seq_puts(s, "No EDID\n");
		return 0;
	}

	buf = data->buf;

	for (i = 0; i < data->len; i++) {
#ifdef DEBUG
		if (i % 16 == 0)
			seq_printf(s, "edid[%03x] =", i);
#endif

		seq_printf(s, " %02x", buf[i]);

		if (i % 16 == 15)
			seq_puts(s, "\n");

	}

	tegra_edid_put_data(data);

	return 0;
}

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

static ssize_t dbg_edid_write(struct file *file,
const char __user *addr, size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data;
	struct tegra_dc *dc = m ? m->private : NULL;
	int ret;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	dc->vedid = false;

	kfree(dc->vedid_data);
	dc->vedid_data = NULL;

	if (len < 128) /* invalid edid, turn off vedid */
		return 1;

	dc->vedid_data = kmalloc(sizeof(char) * len, GFP_KERNEL);
	if (!dc->vedid_data) {
		dev_err(&dc->ndev->dev, "no memory for edid\n");
		return 0; /* dc->vedid is false */
	}

	ret = copy_from_user(dc->vedid_data, addr, len);
	if (ret < 0) {
		dev_err(&dc->ndev->dev, "error copying edid\n");
		kfree(dc->vedid_data);
		dc->vedid_data = NULL;
		return ret; /* dc->vedid is false */
	}

	dc->vedid = true;

	return len;
}

static const struct file_operations edid_fops = {
	.open		= dbg_edid_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.write		= dbg_edid_write,
	.release	= single_release,
};

static int dbg_hotplug_show(struct seq_file *s, void *unused)
{
	struct tegra_dc *dc = s->private;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	rmb();
	seq_put_decimal_ll(s, '\0', dc->out->hotplug_state);
	seq_putc(s, '\n');
	return 0;
}

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

static ssize_t dbg_hotplug_write(struct file *file, const char __user *addr,
	size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data; /* single_open() initialized */
	struct tegra_dc *dc = m ? m->private : NULL;
	int ret;
	long new_state;
	int hotplug_state;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	ret = kstrtol_from_user(addr, len, 10, &new_state);
	if (ret < 0)
		return ret;

	mutex_lock(&dc->lock);
	rmb();
	hotplug_state = dc->out->hotplug_state;
	if (hotplug_state == 0 && new_state != 0
			&& dc->hotplug_supported) {
		/* was 0, now -1 or 1.
		 * we are overriding the hpd GPIO, so ignore the interrupt. */
		int gpio_irq = gpio_to_irq(dc->out->hotplug_gpio);

		disable_irq(gpio_irq);
	} else if (hotplug_state != 0 && new_state == 0
			&& dc->hotplug_supported) {
		/* was -1 or 1, and now 0
		 * restore the interrupt for hpd GPIO. */
		int gpio_irq = gpio_to_irq(dc->out->hotplug_gpio);

		enable_irq(gpio_irq);
	}

	dc->out->hotplug_state = new_state;
	wmb();

	/* retrigger the hotplug */
	if (dc->out_ops->detect)
		dc->out_ops->detect(dc);
	mutex_unlock(&dc->lock);

	return len;
}

static const struct file_operations dbg_hotplug_fops = {
	.open		= dbg_hotplug_open,
	.read		= seq_read,
	.write		= dbg_hotplug_write,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int dbg_vrr_enable_show(struct seq_file *m, void *unused)
{
	struct tegra_vrr *vrr = m->private;

	if (!vrr) return -EINVAL;

	seq_printf(m, "vrr enable state: %d\n", vrr->enable);

	return 0;
}

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

static const struct file_operations dbg_vrr_enable_ops = {
	.open = dbg_vrr_enable_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

static int dbg_vrr_dcb_show(struct seq_file *m, void *unused)
{
	struct tegra_vrr *vrr = m->private;

	if (!vrr)
		return -EINVAL;

	seq_printf(m, "vrr dc balance: %d\n", vrr->dcb);

	return 0;
}

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

static const struct file_operations dbg_vrr_dcb_ops = {
	.open = dbg_vrr_dcb_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

static int dbg_vrr_db_tolerance_show(struct seq_file *m, void *unused)
{
	struct tegra_vrr *vrr = m->private;

	if (!vrr)
		return -EINVAL;

	seq_printf(m, "vrr db tolerance: %d\n", vrr->db_tolerance);

	return 0;
}

static ssize_t dbg_vrr_db_tolerance_write(struct file *file,
		const char __user *addr, size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data;
	struct tegra_vrr *vrr = m->private;
	long   new_value;
	int    ret;

	if (!vrr)
		return -EINVAL;

	ret = kstrtol_from_user(addr, len, 10, &new_value);
	if (ret < 0)
		return ret;

	vrr->db_tolerance = new_value;

	return len;
}

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

static const struct file_operations dbg_vrr_db_tolerance_ops = {
	.open = dbg_vrr_db_tolerance_open,
	.read = seq_read,
	.write = dbg_vrr_db_tolerance_write,
	.llseek = seq_lseek,
	.release = single_release,
};

static int dbg_vrr_frame_avg_pct_show(struct seq_file *m, void *unused)
{
	struct tegra_vrr *vrr = m->private;

	if (!vrr)
		return -EINVAL;

	seq_printf(m, "vrr frame average percent: %d\n", vrr->frame_avg_pct);

	return 0;
}

static ssize_t dbg_vrr_frame_avg_pct_write(struct file *file,
		const char __user *addr, size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data;
	struct tegra_vrr *vrr = m->private;
	long   new_pct;
	int    ret;

	if (!vrr)
		return -EINVAL;

	ret = kstrtol_from_user(addr, len, 10, &new_pct);
	if (ret < 0)
		return ret;

	vrr->frame_avg_pct = new_pct;

	return len;
}

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

static const struct file_operations dbg_vrr_frame_avg_pct_ops = {
	.open = dbg_vrr_frame_avg_pct_open,
	.read = seq_read,
	.write = dbg_vrr_frame_avg_pct_write,
	.llseek = seq_lseek,
	.release = single_release,
};

static int dbg_vrr_fluct_avg_pct_show(struct seq_file *m, void *unused)
{
	struct tegra_vrr *vrr = m->private;

	if (!vrr)
		return -EINVAL;

	seq_printf(m, "vrr fluct average percent: %d\n", vrr->fluct_avg_pct);

	return 0;
}

static ssize_t dbg_vrr_fluct_avg_pct_write(struct file *file,
		const char __user *addr, size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data;
	struct tegra_vrr *vrr = m->private;
	long   new_pct;
	int    ret;

	if (!vrr)
		return -EINVAL;

	ret = kstrtol_from_user(addr, len, 10, &new_pct);
	if (ret < 0)
		return ret;

	vrr->fluct_avg_pct = new_pct;

	return len;
}

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

static const struct file_operations dbg_vrr_fluct_avg_pct_ops = {
	.open = dbg_vrr_fluct_avg_pct_open,
	.read = seq_read,
	.write = dbg_vrr_fluct_avg_pct_write,
	.llseek = seq_lseek,
	.release = single_release,
};

static int dbg_tegrahw_type_show(struct seq_file *m, void *unused)
{
	struct tegra_dc *dc = m->private;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	/* All platforms other than real silicon are taken
		as simulation */
	seq_printf(m,
		"real_silicon: %d\n",
		tegra_platform_is_silicon());

	return 0;
}

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

static const struct file_operations dbg_tegrahw_type_ops = {
	.open = dbg_tegrahw_type_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

static ssize_t dbg_background_write(struct file *file,
		const char __user *addr, size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data;
	struct tegra_dc *dc = m->private;
	unsigned long background;
	u32 old_state;

	if (!dc)
		return -EINVAL;

	if (kstrtoul_from_user(addr, len, 0, &background) < 0)
		return -EINVAL;

	if (!dc->enabled)
		return -EBUSY;

	tegra_dc_get(dc);
	mutex_lock(&dc->lock);
	old_state = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
	/* write active version */
	tegra_dc_writel(dc, WRITE_MUX_ACTIVE | READ_MUX_ACTIVE,
			DC_CMD_STATE_ACCESS);
	tegra_dc_readl(dc, DC_CMD_STATE_ACCESS); /* flush */
	tegra_dc_writel(dc, background, DC_DISP_BLEND_BACKGROUND_COLOR);
	/* write assembly version */
	tegra_dc_writel(dc, WRITE_MUX_ASSEMBLY | READ_MUX_ASSEMBLY,
			DC_CMD_STATE_ACCESS);
	tegra_dc_readl(dc, DC_CMD_STATE_ACCESS); /* flush */
	tegra_dc_writel(dc, background, DC_DISP_BLEND_BACKGROUND_COLOR);
	/* cycle the values through assemby -> arm -> active */
	tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
	tegra_dc_readl(dc, DC_CMD_STATE_CONTROL); /* flush */
	tegra_dc_writel(dc, NC_HOST_TRIG | GENERAL_ACT_REQ,
			DC_CMD_STATE_CONTROL);
	tegra_dc_writel(dc, old_state, DC_CMD_STATE_ACCESS);
	tegra_dc_readl(dc, DC_CMD_STATE_ACCESS); /* flush */
	mutex_unlock(&dc->lock);
	tegra_dc_put(dc);

	return len;
}

static int dbg_background_show(struct seq_file *m, void *unused)
{
	struct tegra_dc *dc = m->private;
	u32 old_state;
	u32 background;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	if (!dc->enabled)
		return -EBUSY;

	tegra_dc_get(dc);
	mutex_lock(&dc->lock);
	old_state = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
	tegra_dc_writel(dc, WRITE_MUX_ACTIVE | READ_MUX_ACTIVE,
			DC_CMD_STATE_ACCESS);
	background = tegra_dc_readl(dc, DC_DISP_BLEND_BACKGROUND_COLOR);
	tegra_dc_writel(dc, old_state, DC_CMD_STATE_ACCESS);
	mutex_unlock(&dc->lock);
	tegra_dc_put(dc);

	seq_printf(m, "%#x\n", (unsigned)background);

	return 0;
}

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

static const struct file_operations dbg_background_ops = {
	.open = dbg_background_open,
	.write = dbg_background_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

/* toggly the enable/disable for any windows with 1 bit set */
static ssize_t dbg_window_toggle_write(struct file *file,
		const char __user *addr, size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data;
	struct tegra_dc *dc = m->private;
	unsigned long windows;
	int i;
	u32 status;
	int retries;

	if (!dc)
		return -EINVAL;

	if (kstrtoul_from_user(addr, len, 0, &windows) < 0)
		return -EINVAL;

	if (!dc->enabled)
		return 0;

	mutex_lock(&dc->lock);
	tegra_dc_get(dc);

	/* limit the request only to valid windows */
	windows &= dc->valid_windows;
	for_each_set_bit(i, &windows, DC_N_WINDOWS) {
		u32 val;
		/* select the assembly registers for window i */
		tegra_dc_writel(dc, WRITE_MUX_ASSEMBLY | READ_MUX_ASSEMBLY,
				DC_CMD_STATE_ACCESS);
		tegra_dc_writel(dc, WINDOW_A_SELECT << i,
				DC_CMD_DISPLAY_WINDOW_HEADER);

		/* toggle the enable bit */
		val = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
		val ^= WIN_ENABLE;
		dev_dbg(&dc->ndev->dev, "%s window #%d\n",
			(val & WIN_ENABLE) ? "enabling" : "disabling", i);
		tegra_dc_writel(dc, val, DC_WIN_WIN_OPTIONS);

		/* post the update */
		tegra_dc_writel(dc, WIN_A_UPDATE << i, DC_CMD_STATE_CONTROL);
		retries = 8;
		do {
			status = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
			retries--;
		} while (retries && (status & (WIN_A_UPDATE << i)));
		tegra_dc_writel(dc, WIN_A_ACT_REQ << i, DC_CMD_STATE_CONTROL);

	}
	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);

	return len;
}

/* reading shows the enabled windows */
static int dbg_window_toggle_show(struct seq_file *m, void *unused)
{
	struct tegra_dc *dc = m->private;
	int i;
	unsigned long windows;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	mutex_lock(&dc->lock);
	tegra_dc_get(dc);
	/* limit the request only to valid windows */
	windows = 0;
	for_each_set_bit(i, &dc->valid_windows, DC_N_WINDOWS) {
		u32 val;

		/* select the active registers for window i */
		tegra_dc_writel(dc, WRITE_MUX_ACTIVE | READ_MUX_ACTIVE,
				DC_CMD_STATE_ACCESS);
		tegra_dc_writel(dc, WINDOW_A_SELECT << i,
				DC_CMD_DISPLAY_WINDOW_HEADER);

		/* add i to a bitmap if WIN_ENABLE is set */
		val = tegra_dc_readl(dc, DC_WIN_WIN_OPTIONS);
		if (val & WIN_ENABLE)
			set_bit(i, &windows);

	}
	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);

	seq_printf(m, "%#lx %#lx\n", dc->valid_windows, windows);

	return 0;
}

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

static const struct file_operations dbg_window_toggle_ops = {
	.open = dbg_window_toggle_open,
	.write = dbg_window_toggle_write,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

static int dbg_dc_cmu_lut1_show(struct seq_file *s, void *unused)
{
	struct tegra_dc *dc = s->private;
	u32 val;
	int i;

	mutex_lock(&dc->lock);
	tegra_dc_get(dc);

	/* Disable CMU while reading LUTs */
	val = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
	tegra_dc_writel(dc, val & ~CMU_ENABLE, DC_DISP_DISP_COLOR_CONTROL);
	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
	_tegra_dc_wait_for_frame_end(dc,
		div_s64(dc->frametime_ns, 1000000ll) * 2);

	for (i = 0; i < 256; i++) {
		tegra_dc_writel(dc, LUT1_READ_EN | LUT1_READ_ADDR(i),
			DC_COM_CMU_LUT1_READ);

		seq_printf(s, "%lu\n",
			LUT1_READ_DATA(tegra_dc_readl(dc, DC_COM_CMU_LUT1)));
	}
	tegra_dc_writel(dc, 0, DC_COM_CMU_LUT1_READ);

	tegra_dc_writel(dc, val, DC_DISP_DISP_COLOR_CONTROL);
	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);

	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
	return 0;
}

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

static const struct file_operations cmu_lut1_fops = {
	.open		= dbg_dc_cmu_lut1_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int dbg_dc_cmu_lut2_show(struct seq_file *s, void *unused)
{
	struct tegra_dc *dc = s->private;
	u32 val;
	int i;

	mutex_lock(&dc->lock);
	tegra_dc_get(dc);

	/* Disable CMU while reading LUTs */
	val = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
	tegra_dc_writel(dc, val & ~CMU_ENABLE, DC_DISP_DISP_COLOR_CONTROL);
	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
	_tegra_dc_wait_for_frame_end(dc,
		div_s64(dc->frametime_ns, 1000000ll) * 2);

	for (i = 0; i < 960; i++) {
		tegra_dc_writel(dc, LUT2_READ_EN | LUT2_READ_ADDR(i),
			DC_COM_CMU_LUT2_READ);

		seq_printf(s, "%lu\n",
			LUT2_READ_DATA(tegra_dc_readl(dc, DC_COM_CMU_LUT2)));
	}
	tegra_dc_writel(dc, 0, DC_COM_CMU_LUT2_READ);

	tegra_dc_writel(dc, val, DC_DISP_DISP_COLOR_CONTROL);
	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);

	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
	return 0;
}

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

static const struct file_operations cmu_lut2_fops = {
	.open		= dbg_dc_cmu_lut2_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int dbg_measure_refresh_show(struct seq_file *m, void *unused)
{
	struct tegra_dc *dc = m->private;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	seq_puts(m, "Write capture time in seconds to this node.\n");
	seq_puts(m, "Results will show up in dmesg.\n");

	return 0;
}

static ssize_t dbg_measure_refresh_write(struct file *file,
		const char __user *addr, size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data;
	struct tegra_dc *dc = m->private;
	s32 seconds;
	u32 fe_count;
	int ret;
	fixed20_12 refresh_rate;
	fixed20_12 seconds_fixed;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	ret = kstrtoint_from_user(addr, len, 10, &seconds);
	if (ret < 0 || seconds < 1) {
		dev_info(&dc->ndev->dev,
				"specify integer number of seconds greater than 0\n");
		return -EINVAL;
	}

	dev_info(&dc->ndev->dev, "measuring for %d seconds\n", seconds);

	mutex_lock(&dc->lock);
	_tegra_dc_config_frame_end_intr(dc, true);
	dc->dbg_fe_count = 0;
	mutex_unlock(&dc->lock);

	msleep(1000 * seconds);

	mutex_lock(&dc->lock);
	_tegra_dc_config_frame_end_intr(dc, false);
	fe_count = dc->dbg_fe_count;
	mutex_unlock(&dc->lock);

	refresh_rate.full = dfixed_const(fe_count);
	seconds_fixed.full = dfixed_const(seconds);
	refresh_rate.full = dfixed_div(refresh_rate, seconds_fixed);

	/* Print fixed point 20.12 in decimal, truncating the 12-bit fractional
	   part to 2 decimal points */
	dev_info(&dc->ndev->dev, "refresh rate: %d.%dHz\n",
		dfixed_trunc(refresh_rate),
		dfixed_frac(refresh_rate) * 100 / 4096);

	return len;
}

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

static const struct file_operations dbg_measure_refresh_ops = {
	.open = dbg_measure_refresh_open,
	.read = seq_read,
	.write = dbg_measure_refresh_write,
	.llseek = seq_lseek,
	.release = single_release,
};

static void tegra_dc_remove_debugfs(struct tegra_dc *dc)
{
	if (dc->debugdir)
		debugfs_remove_recursive(dc->debugdir);
	dc->debugdir = NULL;
}

#ifdef CONFIG_TEGRA_NVDISPLAY
/*
 * ihub_win_num specifies the window number. A value of -1 should be used if
 * the property you want to read isn't window specific.
 */
static int ihub_win_num;

static int dbg_ihub_win_num_show(struct seq_file *s, void *unused)
{
	struct tegra_dc *dc = s->private;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	seq_put_decimal_ll(s, '\0', ihub_win_num);
	seq_putc(s, '\n');
	return 0;
}

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

static ssize_t dbg_ihub_win_num_write(struct file *file,
	const char __user *addr, size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data; /* single_open() initialized */
	struct tegra_dc *dc = m ? m->private : NULL;
	int ret;
	long new_win_num;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	ret = kstrtol_from_user(addr, len, 10, &new_win_num);
	if (ret < 0)
		return ret;

	mutex_lock(&dc->lock);
	ihub_win_num = new_win_num;
	mutex_unlock(&dc->lock);

	return len;
}

static const struct file_operations dbg_ihub_win_num_ops = {
	.open = dbg_ihub_win_num_open,
	.read = seq_read,
	.write = dbg_ihub_win_num_write,
	.llseek = seq_lseek,
	.release = single_release,
};

static int dbg_ihub_mempool_size_show(struct seq_file *s, void *unused)
{
	struct tegra_dc *dc = s->private;

	if (WARN_ON(!dc || !dc->out))
		return -EINVAL;

	seq_put_decimal_ll(s, '\0', tegra_nvdisp_ihub_read(dc, -1, 0));
	seq_putc(s, '\n');
	return 0;
}

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

static ssize_t dbg_ihub_mempool_size_write(struct file *file,
	const char __user *addr, size_t len, loff_t *pos)
{
	return -EINVAL; /* read-only property */
}

static const struct file_operations dbg_ihub_mempool_size_ops = {
	.open = dbg_ihub_mempool_size_open,
	.read = seq_read,
	.write = dbg_ihub_mempool_size_write,
	.llseek = seq_lseek,
	.release = single_release,
};

#endif

static void tegra_dc_create_debugfs(struct tegra_dc *dc)
{
	struct dentry *retval, *vrrdir;
#ifdef CONFIG_TEGRA_NVDISPLAY
	struct dentry *ihubdir;
#endif
	char   devname[50];

	snprintf(devname, sizeof(devname), "tegradc.%d", dc->ctrl_num);
	dc->debugdir = debugfs_create_dir(devname, NULL);
	if (!dc->debugdir)
		goto remove_out;

	retval = debugfs_create_file("regs", S_IRUGO, dc->debugdir, dc,
		&regs_fops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("mode", S_IRUGO, dc->debugdir, dc,
		&mode_fops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("stats", S_IRUGO, dc->debugdir, dc,
		&stats_fops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("event_inject", S_IRUGO, dc->debugdir, dc,
		&event_inject_fops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("out_type", S_IRUGO, dc->debugdir, dc,
		&outtype_fops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("edid", S_IRUGO, dc->debugdir, dc,
		&edid_fops);
	if (!retval)
		goto remove_out;

	if (dc->out_ops->detect) {
		/* only create the file if hotplug is supported */
		retval = debugfs_create_file("hotplug", S_IRUGO, dc->debugdir,
			dc, &dbg_hotplug_fops);
		if (!retval)
			goto remove_out;
	}

	vrrdir = debugfs_create_dir("vrr",  dc->debugdir);
	if (!vrrdir)
		goto remove_out;

	retval = debugfs_create_file("enable", S_IRUGO, vrrdir,
				dc->out->vrr, &dbg_vrr_enable_ops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("dcb", S_IRUGO, vrrdir,
				dc->out->vrr, &dbg_vrr_dcb_ops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("frame_avg_pct", S_IRUGO, vrrdir,
				dc->out->vrr, &dbg_vrr_frame_avg_pct_ops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("fluct_avg_pct", S_IRUGO, vrrdir,
				dc->out->vrr, &dbg_vrr_fluct_avg_pct_ops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("tegrahw_type", S_IRUGO, dc->debugdir,
				dc, &dbg_tegrahw_type_ops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("background", S_IRUGO, dc->debugdir,
				dc, &dbg_background_ops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("window_toggle", S_IRUGO, dc->debugdir,
				dc, &dbg_window_toggle_ops);
	if (!retval)
		goto remove_out;

#ifdef CONFIG_TEGRA_NVDISPLAY
	ihubdir = debugfs_create_dir("ihub", dc->debugdir);
	if (!ihubdir)
		goto remove_out;

	retval = debugfs_create_file("win_num", S_IRUGO, ihubdir,
				dc, &dbg_ihub_win_num_ops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("mempool_size", S_IRUGO, ihubdir,
				dc, &dbg_ihub_mempool_size_ops);
	if (!retval)
		goto remove_out;
#endif

	retval = debugfs_create_file("cmu_lut1", S_IRUGO, dc->debugdir, dc,
		&cmu_lut1_fops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("cmu_lut2", S_IRUGO, dc->debugdir, dc,
		&cmu_lut2_fops);
	if (!retval)
		goto remove_out;

	retval = debugfs_create_file("measure_refresh", S_IRUGO, dc->debugdir,
				dc, &dbg_measure_refresh_ops);
	if (!retval)
		goto remove_out;

	return;
remove_out:
	dev_err(&dc->ndev->dev, "could not create debugfs\n");
	tegra_dc_remove_debugfs(dc);
}

#else /* !CONFIG_DEBUGFS */
static inline void tegra_dc_create_debugfs(struct tegra_dc *dc) { };
static inline void tegra_dc_remove_debugfs(struct tegra_dc *dc) { };
#endif /* CONFIG_DEBUGFS */

s32 tegra_dc_calc_v_front_porch(struct tegra_dc_mode *mode,
				int desired_fps)
{
	int vfp = 0;

	if (desired_fps > 0) {
		int line = mode->h_sync_width + mode->h_back_porch +
			mode->h_active + mode->h_front_porch;
		int lines_per_frame = mode->pclk / line / desired_fps;
		vfp = lines_per_frame - mode->v_sync_width -
			mode->v_active - mode->v_back_porch;
	}

	return vfp;
}

static void tegra_dc_setup_vrr(struct tegra_dc *dc)
{
	int lines_per_frame_max, lines_per_frame_min;

	struct tegra_dc_mode *m;
	struct tegra_vrr *vrr  = dc->out->vrr;

	if (!vrr) return;

	m = &dc->out->modes[dc->out->n_modes-1];
	vrr->v_front_porch = m->v_front_porch;
	vrr->v_back_porch = m->v_back_porch;
	vrr->pclk = m->pclk;

	if (vrr->vrr_min_fps > 0)
		vrr->v_front_porch_max = tegra_dc_calc_v_front_porch(m,
				vrr->vrr_min_fps);

	vrr->vrr_max_fps =
		(s32)div_s64(NSEC_PER_SEC, dc->frametime_ns);

	vrr->v_front_porch_min = m->v_front_porch;

	vrr->line_width = m->h_sync_width + m->h_back_porch +
			m->h_active + m->h_front_porch;
	vrr->lines_per_frame_common = m->v_sync_width +
			m->v_back_porch + m->v_active;
	lines_per_frame_max = vrr->lines_per_frame_common +
			vrr->v_front_porch_max;
	lines_per_frame_min = vrr->lines_per_frame_common +
			vrr->v_front_porch_min;

	if (lines_per_frame_max < 2*lines_per_frame_min) {
		pr_err("max fps is less than 2 times min fps.\n");
		return;
	}

	vrr->frame_len_max = vrr->line_width * lines_per_frame_max /
					(m->pclk / 1000000);
	vrr->frame_len_min = vrr->line_width * lines_per_frame_min /
					(m->pclk / 1000000);
	vrr->vfp_extend = vrr->v_front_porch_max;
	vrr->vfp_shrink = vrr->v_front_porch_min;

	vrr->frame_type = 0;
	vrr->frame_delta_us = 0;

	vrr->max_adj_pct = 50;
	vrr->max_flip_pct = 20;
	vrr->max_dcb = 20000;
	vrr->max_inc_pct = 5;

	vrr->dcb = 0;
	vrr->frame_avg_pct = 75;
	vrr->fluct_avg_pct = 75;
	vrr->db_tolerance = 5000;
}

unsigned long tegra_dc_poll_register(struct tegra_dc *dc, u32 reg, u32 mask,
		u32 exp_val, u32 poll_interval_us, u32 timeout_ms)
{
	unsigned long timeout_jf = jiffies + msecs_to_jiffies(timeout_ms);
	u32 reg_val = 0;

	if (tegra_platform_is_linsim() || tegra_platform_is_vdk())
		return 0;

	do {
		usleep_range(poll_interval_us, poll_interval_us << 1);
/*		usleep_range(1000, 1500);*/
		reg_val = tegra_dc_readl(dc, reg);
	} while (((reg_val & mask) != exp_val) &&
		time_after(timeout_jf, jiffies));

	if ((reg_val & mask) == exp_val)
		return 0;       /* success */
	dev_err(&dc->ndev->dev,
		"dc_poll_register 0x%x: timeout\n", reg);
	return jiffies - timeout_jf + 1;
}


void tegra_dc_enable_general_act(struct tegra_dc *dc)
{
	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);

	if (tegra_dc_poll_register(dc, DC_CMD_STATE_CONTROL,
		GENERAL_ACT_REQ, 0, 1,
		TEGRA_DC_POLL_TIMEOUT_MS))
		dev_err(&dc->ndev->dev,
			"dc timeout waiting for DC to stop\n");
}


static int tegra_dc_set_next(struct tegra_dc *dc)
{
	int i;
	int ret = -EBUSY;

	mutex_lock(&tegra_dc_lock);

	for (i = 0; i < TEGRA_MAX_DC; i++) {
		if (tegra_dcs[i] == NULL) {
			tegra_dcs[i] = dc;
			ret = i;
			break;
		}
	}

	mutex_unlock(&tegra_dc_lock);

	return ret;
}

static int tegra_dc_set_idx(struct tegra_dc *dc, int index)
{
	int ret = 0;

	mutex_lock(&tegra_dc_lock);
	if (index >= TEGRA_MAX_DC) {
		ret = -EINVAL;
		goto out;
	}

	if (dc != NULL && tegra_dcs[index] != NULL) {
		ret = -EBUSY;
		goto out;
	}

	tegra_dcs[index] = dc;

out:
	mutex_unlock(&tegra_dc_lock);

	return ret;
}

/*
 * If index == -1, set dc at next available index. This is to be called only
 * when registering dc in DT case. For non DT case & when removing the device
 * (dc == NULL), index should be accordingly.
 */
static int tegra_dc_set(struct tegra_dc *dc, int index)
{
	if ((index == -1) && (dc != NULL)) /* DT register case */
		return tegra_dc_set_next(dc);
	else /* non DT, unregister case */
		return tegra_dc_set_idx(dc, index);
}

unsigned int tegra_dc_has_multiple_dc(void)
{
	unsigned int idx;
	unsigned int cnt = 0;
	struct tegra_dc *dc;

	mutex_lock(&tegra_dc_lock);
	for (idx = 0; idx < TEGRA_MAX_DC; idx++)
		cnt += ((dc = tegra_dcs[idx]) != NULL && dc->enabled) ? 1 : 0;
	mutex_unlock(&tegra_dc_lock);

	return (cnt > 1);
}

/* get the stride size of a window.
 * return: stride size in bytes for window win. or 0 if unavailble. */
int tegra_dc_get_stride(struct tegra_dc *dc, unsigned win)
{
	u32 stride;

	if (!dc->enabled)
		return 0;
	BUG_ON(win > DC_N_WINDOWS);
	mutex_lock(&dc->lock);
	tegra_dc_get(dc);
#ifdef CONFIG_TEGRA_NVDISPLAY
	stride = tegra_nvdisp_get_linestride(dc, win);
#else
	tegra_dc_writel(dc, WINDOW_A_SELECT << win,
		DC_CMD_DISPLAY_WINDOW_HEADER);

	stride = tegra_dc_readl(dc, DC_WIN_LINE_STRIDE);
#endif
	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
	return GET_LINE_STRIDE(stride);
}
EXPORT_SYMBOL(tegra_dc_get_stride);

struct tegra_dc *tegra_dc_get_dc(unsigned idx)
{
	if (idx < TEGRA_MAX_DC)
		return tegra_dcs[idx];
	else
		return NULL;
}
EXPORT_SYMBOL(tegra_dc_get_dc);

struct tegra_dc_win *tegra_dc_get_window(struct tegra_dc *dc, unsigned win)
{
	if (win >= DC_N_WINDOWS || !test_bit(win, &dc->valid_windows))
		return NULL;

#ifdef CONFIG_TEGRA_NVDISPLAY
	return &tegra_dc_windows[win];
#else
	return &dc->windows[win];
#endif
}
EXPORT_SYMBOL(tegra_dc_get_window);

bool tegra_dc_get_connected(struct tegra_dc *dc)
{
	return dc->connected;
}
EXPORT_SYMBOL(tegra_dc_get_connected);

bool tegra_dc_hpd(struct tegra_dc *dc)
{
	int hpd = false;
	int hotplug_state;

	if (WARN_ON(!dc || !dc->out))
		return false;

	rmb();
	hotplug_state = dc->out->hotplug_state;

	if (hotplug_state != TEGRA_HPD_STATE_NORMAL) {
		if (hotplug_state == TEGRA_HPD_STATE_FORCE_ASSERT)
			return true;
		if (hotplug_state == TEGRA_HPD_STATE_FORCE_DEASSERT)
			return false;
	}

	if (!dc->hotplug_supported)
		return true;

	if (dc->out_ops && dc->out_ops->hpd_state)
		hpd = dc->out_ops->hpd_state(dc);

	if (dc->out->hotplug_report)
		dc->out->hotplug_report(hpd);

	return hpd;
}
EXPORT_SYMBOL(tegra_dc_hpd);

#ifndef CONFIG_TEGRA_NVDISPLAY
static void tegra_dc_set_scaling_filter(struct tegra_dc *dc)
{
	unsigned i;
	unsigned v0 = 128;
	unsigned v1 = 0;

	/* linear horizontal and vertical filters */
	for (i = 0; i < 16; i++) {
		tegra_dc_writel(dc, (v1 << 16) | (v0 << 8),
				DC_WIN_H_FILTER_P(i));

		tegra_dc_writel(dc, v0,
				DC_WIN_V_FILTER_P(i));
		v0 -= 8;
		v1 += 8;
	}
}
#endif

static int _tegra_dc_config_frame_end_intr(struct tegra_dc *dc, bool enable)
{
	tegra_dc_get(dc);
	if (enable) {
		atomic_inc(&dc->frame_end_ref);
		tegra_dc_unmask_interrupt(dc, FRAME_END_INT);
	} else if (!atomic_dec_return(&dc->frame_end_ref))
		tegra_dc_mask_interrupt(dc, FRAME_END_INT);
	tegra_dc_put(dc);

	return 0;
}

#if defined(CONFIG_TEGRA_DC_CMU) || defined(CONFIG_TEGRA_DC_CMU_V2)
static struct tegra_dc_cmu *tegra_dc_get_cmu(struct tegra_dc *dc)
{
	if (dc->out->type == TEGRA_DC_OUT_FAKE_DP ||
		dc->out->type == TEGRA_DC_OUT_FAKE_DSIA ||
		dc->out->type == TEGRA_DC_OUT_FAKE_DSIB ||
		dc->out->type == TEGRA_DC_OUT_FAKE_DSI_GANGED ||
		dc->out->type == TEGRA_DC_OUT_NULL) {
#if defined(CONFIG_TEGRA_NVDISPLAY)
		tegra_nvdisp_get_default_cmu(&default_cmu);
#endif
		return &default_cmu;
	}
	if (dc->pdata->cmu && !dc->pdata->default_clr_space)
		return dc->pdata->cmu;
	else if (dc->pdata->cmu_adbRGB && dc->pdata->default_clr_space)
		return dc->pdata->cmu_adbRGB;
	else if (dc->out->type == TEGRA_DC_OUT_HDMI) {
#if defined(CONFIG_TEGRA_NVDISPLAY)
		tegra_nvdisp_get_default_cmu(&default_limited_cmu);
#endif
		return &default_limited_cmu;
	} else {
#if defined(CONFIG_TEGRA_NVDISPLAY)
		tegra_nvdisp_get_default_cmu(&default_cmu);
#endif
		return &default_cmu;
	}
}

void tegra_dc_cmu_enable(struct tegra_dc *dc, bool cmu_enable)
{
	dc->cmu_enabled = cmu_enable;
#if defined(CONFIG_TEGRA_NVDISPLAY)
	dc->pdata->cmu_enable = cmu_enable;
	tegra_dc_cache_cmu(dc, tegra_dc_get_cmu(dc));
	tegra_nvdisp_update_cmu(dc, &dc->cmu);
#else
	tegra_dc_update_cmu(dc, tegra_dc_get_cmu(dc));
#endif
}
EXPORT_SYMBOL(tegra_dc_cmu_enable);
#else
#define tegra_dc_cmu_enable(dc, cmu_enable)
#endif

#ifdef CONFIG_TEGRA_DC_CMU
static void tegra_dc_cache_cmu(struct tegra_dc *dc,
				struct tegra_dc_cmu *src_cmu)
{
	if (&dc->cmu != src_cmu) /* ignore if it would require memmove() */
		memcpy(&dc->cmu, src_cmu, sizeof(*src_cmu));
	dc->cmu_dirty = true;
}

static void tegra_dc_set_cmu(struct tegra_dc *dc, struct tegra_dc_cmu *cmu)
{
	u32 val;
	u32 i;

	for (i = 0; i < 256; i++) {
		val = LUT1_ADDR(i) | LUT1_DATA(cmu->lut1[i]);
		tegra_dc_writel(dc, val, DC_COM_CMU_LUT1);
	}

	tegra_dc_writel(dc, cmu->csc.krr, DC_COM_CMU_CSC_KRR);
	tegra_dc_writel(dc, cmu->csc.kgr, DC_COM_CMU_CSC_KGR);
	tegra_dc_writel(dc, cmu->csc.kbr, DC_COM_CMU_CSC_KBR);
	tegra_dc_writel(dc, cmu->csc.krg, DC_COM_CMU_CSC_KRG);
	tegra_dc_writel(dc, cmu->csc.kgg, DC_COM_CMU_CSC_KGG);
	tegra_dc_writel(dc, cmu->csc.kbg, DC_COM_CMU_CSC_KBG);
	tegra_dc_writel(dc, cmu->csc.krb, DC_COM_CMU_CSC_KRB);
	tegra_dc_writel(dc, cmu->csc.kgb, DC_COM_CMU_CSC_KGB);
	tegra_dc_writel(dc, cmu->csc.kbb, DC_COM_CMU_CSC_KBB);

	for (i = 0; i < 960; i++) {
		val = LUT2_ADDR(i) | LUT1_DATA(cmu->lut2[i]);
		tegra_dc_writel(dc, val, DC_COM_CMU_LUT2);
	}

	dc->cmu_dirty = false;
}

static void _tegra_dc_update_cmu(struct tegra_dc *dc, struct tegra_dc_cmu *cmu)
{
	u32 val;

	if (!dc->cmu_enabled)
		return;

	tegra_dc_cache_cmu(dc, cmu);

	if (dc->cmu_dirty) {
		/* Disable CMU to avoid programming it while it is in use */
		val = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
		if (val & CMU_ENABLE) {
			val &= ~CMU_ENABLE;
			tegra_dc_writel(dc, val,
					DC_DISP_DISP_COLOR_CONTROL);
			val = GENERAL_ACT_REQ;
			tegra_dc_writel(dc, val, DC_CMD_STATE_CONTROL);
			/*TODO: Sync up with vsync */
			mdelay(20);
		}
		dev_dbg(&dc->ndev->dev, "updating CMU cmu_dirty=%d\n",
			dc->cmu_dirty);

		tegra_dc_set_cmu(dc, &dc->cmu);
	}
}

void _tegra_dc_cmu_enable(struct tegra_dc *dc, bool cmu_enable)
{
	dc->cmu_enabled = cmu_enable;
	_tegra_dc_update_cmu(dc, tegra_dc_get_cmu(dc));
	tegra_dc_set_color_control(dc);
	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
}
EXPORT_SYMBOL(_tegra_dc_cmu_enable);

int tegra_dc_update_cmu(struct tegra_dc *dc, struct tegra_dc_cmu *cmu)
{
	mutex_lock(&dc->lock);
	if (!dc->enabled) {
		mutex_unlock(&dc->lock);
		return 0;
	}

	tegra_dc_get(dc);

	_tegra_dc_update_cmu(dc, cmu);
	tegra_dc_set_color_control(dc);
	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);

	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);

	return 0;
}
EXPORT_SYMBOL(tegra_dc_update_cmu);

static int _tegra_dc_update_cmu_aligned(struct tegra_dc *dc,
				struct tegra_dc_cmu *cmu,
				bool force)
{
	memcpy(&dc->cmu_shadow, cmu, sizeof(dc->cmu));
	dc->cmu_shadow_dirty = true;
	dc->cmu_shadow_force_update = dc->cmu_shadow_force_update || force;
	_tegra_dc_config_frame_end_intr(dc, true);

	return 0;
}

int tegra_dc_update_cmu_aligned(struct tegra_dc *dc, struct tegra_dc_cmu *cmu)
{
	int ret;

	mutex_lock(&dc->lock);
	if (!dc->enabled) {
		mutex_unlock(&dc->lock);
		return 0;
	}
	ret = _tegra_dc_update_cmu_aligned(dc, cmu, false);
	mutex_unlock(&dc->lock);

	return ret;
}

EXPORT_SYMBOL(tegra_dc_update_cmu_aligned);
#else
#define tegra_dc_cache_cmu(dc, src_cmu)
#define tegra_dc_set_cmu(dc, cmu)
#define tegra_dc_update_cmu(dc, cmu)
#define _tegra_dc_enable_cmu(dc, cmu)
#define tegra_dc_update_cmu_aligned(dc, cmu)
#endif

int tegra_dc_set_hdr(struct tegra_dc *dc, struct tegra_dc_hdr *hdr,
						bool cache_dirty)
{
	int ret;

	mutex_lock(&dc->lock);

	if (!dc->enabled) {
		mutex_unlock(&dc->lock);
		return 0;
	}
	trace_hdr_data_update(dc, hdr);
	if (cache_dirty) {
		dc->hdr.eotf = hdr->eotf;
		dc->hdr.static_metadata_id = hdr->static_metadata_id;
		memcpy(dc->hdr.static_metadata, hdr->static_metadata,
					sizeof(dc->hdr.static_metadata));
	} else if (dc->hdr.enabled == hdr->enabled) {
		mutex_unlock(&dc->lock);
		return 0;
	}
	dc->hdr.enabled = hdr->enabled;
	dc->hdr_cache_dirty = true;
	if (!dc->hdr.enabled)
		memset(&dc->hdr, 0, sizeof(dc->hdr));
	ret = _tegra_dc_config_frame_end_intr(dc, true);

	mutex_unlock(&dc->lock);

	return ret;
}
EXPORT_SYMBOL(tegra_dc_set_hdr);

/* disable_irq() blocks until handler completes, calling this function while
 * holding dc->lock can deadlock. */
static inline void disable_dc_irq(const struct tegra_dc *dc)
{
	disable_irq(dc->irq);
}

u32 tegra_dc_get_syncpt_id(struct tegra_dc *dc, int i)
{
	struct tegra_dc_win *win = tegra_dc_get_window(dc, i);
	BUG_ON(!win);
	return win->syncpt.id;
}
EXPORT_SYMBOL(tegra_dc_get_syncpt_id);

static u32 tegra_dc_incr_syncpt_max_locked(struct tegra_dc *dc, int i)
{
	u32 max;
	struct tegra_dc_win *win = tegra_dc_get_window(dc, i);

	BUG_ON(!win);
	max = nvhost_syncpt_incr_max_ext(dc->ndev,
		win->syncpt.id, ((dc->enabled) ? 1 : 0));
	win->syncpt.max = max;

	return max;
}

u32 tegra_dc_incr_syncpt_max(struct tegra_dc *dc, int i)
{
	u32 max;

	mutex_lock(&dc->lock);
	tegra_dc_get(dc);
	max = tegra_dc_incr_syncpt_max_locked(dc, i);
	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);

	return max;
}

void tegra_dc_incr_syncpt_min(struct tegra_dc *dc, int i, u32 val)
{
	struct tegra_dc_win *win = tegra_dc_get_window(dc, i);

	BUG_ON(!win);
	mutex_lock(&dc->lock);

	tegra_dc_get(dc);
	while (win->syncpt.min < val) {
		win->syncpt.min++;
		nvhost_syncpt_cpu_incr_ext(dc->ndev, win->syncpt.id);
		}
	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
}

struct sync_fence *tegra_dc_create_fence(struct tegra_dc *dc, int i, u32 val)
{
	struct nvhost_ctrl_sync_fence_info syncpt;
	u32 id = tegra_dc_get_syncpt_id(dc, i);

	syncpt.id = id;
	syncpt.thresh = val;
	return nvhost_sync_create_fence(
			to_platform_device(dc->ndev->dev.parent),
			&syncpt, 1, dev_name(&dc->ndev->dev));
}

void
tegra_dc_config_pwm(struct tegra_dc *dc, struct tegra_dc_pwm_params *cfg)
{
	unsigned int ctrl;
	unsigned long out_sel;
	unsigned long cmd_state;

	mutex_lock(&dc->lock);
	if (!dc->enabled) {
		mutex_unlock(&dc->lock);
		return;
	}

	tegra_dc_get(dc);

	ctrl = ((cfg->period << PM_PERIOD_SHIFT) |
		(cfg->clk_div << PM_CLK_DIVIDER_SHIFT) |
		cfg->clk_select);

	/* The new value should be effected immediately */
	cmd_state = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
	tegra_dc_writel(dc, (cmd_state | (1 << 2)), DC_CMD_STATE_ACCESS);

	switch (cfg->which_pwm) {
	case TEGRA_PWM_PM0:
		/* Select the LM0 on PM0 */
		out_sel = tegra_dc_readl(dc, DC_COM_PIN_OUTPUT_SELECT5);
		out_sel &= ~(7 << 0);
		out_sel |= (3 << 0);
		tegra_dc_writel(dc, out_sel, DC_COM_PIN_OUTPUT_SELECT5);
		tegra_dc_writel(dc, ctrl, DC_COM_PM0_CONTROL);
		tegra_dc_writel(dc, cfg->duty_cycle, DC_COM_PM0_DUTY_CYCLE);
		break;
	case TEGRA_PWM_PM1:
		/* Select the LM1 on PM1 */
		out_sel = tegra_dc_readl(dc, DC_COM_PIN_OUTPUT_SELECT5);
		out_sel &= ~(7 << 4);
		out_sel |= (3 << 4);
		tegra_dc_writel(dc, out_sel, DC_COM_PIN_OUTPUT_SELECT5);
		tegra_dc_writel(dc, ctrl, DC_COM_PM1_CONTROL);
		tegra_dc_writel(dc, cfg->duty_cycle, DC_COM_PM1_DUTY_CYCLE);
		break;
	default:
		dev_err(&dc->ndev->dev, "Error: Need which_pwm\n");
		break;
	}
	tegra_dc_writel(dc, cmd_state, DC_CMD_STATE_ACCESS);
	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
}
EXPORT_SYMBOL(tegra_dc_config_pwm);

void tegra_dc_set_out_pin_polars(struct tegra_dc *dc,
				const struct tegra_dc_out_pin *pins,
				const unsigned int n_pins)
{
	unsigned int i;

	int name;
	int pol;

	u32 pol1, pol3;

	u32 set1, unset1;
	u32 set3, unset3;

	set1 = set3 = unset1 = unset3 = 0;

	for (i = 0; i < n_pins; i++) {
		name = (pins + i)->name;
		pol  = (pins + i)->pol;

		/* set polarity by name */
		switch (name) {
		case TEGRA_DC_OUT_PIN_DATA_ENABLE:
			if (pol == TEGRA_DC_OUT_PIN_POL_LOW)
				set3 |= LSPI_OUTPUT_POLARITY_LOW;
			else
				unset3 |= LSPI_OUTPUT_POLARITY_LOW;
			break;
		case TEGRA_DC_OUT_PIN_H_SYNC:
			if (pol == TEGRA_DC_OUT_PIN_POL_LOW)
				set1 |= LHS_OUTPUT_POLARITY_LOW;
			else
				unset1 |= LHS_OUTPUT_POLARITY_LOW;
			break;
		case TEGRA_DC_OUT_PIN_V_SYNC:
			if (pol == TEGRA_DC_OUT_PIN_POL_LOW)
				set1 |= LVS_OUTPUT_POLARITY_LOW;
			else
				unset1 |= LVS_OUTPUT_POLARITY_LOW;
			break;
		case TEGRA_DC_OUT_PIN_PIXEL_CLOCK:
			if (pol == TEGRA_DC_OUT_PIN_POL_LOW)
				set1 |= LSC0_OUTPUT_POLARITY_LOW;
			else
				unset1 |= LSC0_OUTPUT_POLARITY_LOW;
			break;
		default:
			printk("Invalid argument in function %s\n",
			       __FUNCTION__);
			break;
		}
	}

	pol1 = DC_COM_PIN_OUTPUT_POLARITY1_INIT_VAL;
	pol3 = DC_COM_PIN_OUTPUT_POLARITY3_INIT_VAL;

	pol1 |= set1;
	pol1 &= ~unset1;

	pol3 |= set3;
	pol3 &= ~unset3;

	tegra_dc_writel(dc, pol1, DC_COM_PIN_OUTPUT_POLARITY1);
	tegra_dc_writel(dc, pol3, DC_COM_PIN_OUTPUT_POLARITY3);
}

static struct tegra_dc_mode *tegra_dc_get_override_mode(struct tegra_dc *dc)
{
	unsigned long refresh;

	if (dc->out->type == TEGRA_DC_OUT_HDMI &&
			tegra_is_bl_display_initialized(dc->ndev->id)) {

		/* For seamless HDMI, read mode parameters from bootloader
		 * set DC configuration
		 */
		u32 val = 0;
		struct tegra_dc_mode *mode = &override_disp_mode[dc->out->type];
#ifdef CONFIG_TEGRA_NVDISPLAY
		struct clk *parent_clk = tegra_disp_clk_get(&dc->ndev->dev,
				dc->out->parent_clk ? : "plld2");
#else
		struct clk *parent_clk = clk_get_sys(NULL,
				dc->out->parent_clk ? : "pll_d2");
#endif
		memset(mode, 0, sizeof(struct tegra_dc_mode));
		mode->pclk = clk_get_rate(parent_clk);
		mode->rated_pclk = 0;

		tegra_dc_get(dc);
		val = tegra_dc_readl(dc, DC_DISP_REF_TO_SYNC);
		mode->h_ref_to_sync = val & 0xffff;
		mode->v_ref_to_sync = (val >> 16) & 0xffff;

		val = tegra_dc_readl(dc, DC_DISP_SYNC_WIDTH);
		mode->h_sync_width = val & 0xffff;
		mode->v_sync_width = (val >> 16) & 0xffff;

		val = tegra_dc_readl(dc, DC_DISP_BACK_PORCH);
		mode->h_back_porch = val & 0xffff;
		mode->v_back_porch = (val >> 16) & 0xffff;

		val = tegra_dc_readl(dc, DC_DISP_FRONT_PORCH);
		mode->h_front_porch = val & 0xffff;
		mode->v_front_porch = (val >> 16) & 0xffff;

		val = tegra_dc_readl(dc, DC_DISP_DISP_ACTIVE);
		mode->h_active = val & 0xffff;
		mode->v_active = (val >> 16) & 0xffff;

		/* Check the freq setup by the BL, 59.94 or 60Hz
		 * If 59.94, vmode needs to be FB_VMODE_1000DIV1001
		 * for seamless
		 */
		refresh = tegra_dc_calc_refresh(mode);
		if (refresh % 1000)
			mode->vmode |= FB_VMODE_1000DIV1001;

#ifdef CONFIG_TEGRA_DC_CMU
		/*
		 * Implicit contract between BL and us. If CMU is enabled,
		 * assume limited range. This sort of works because we know
		 * BL doesn't support YUV
		 */
		val = tegra_dc_readl(dc, DC_DISP_DISP_COLOR_CONTROL);
		if (val & CMU_ENABLE)
			mode->vmode |= FB_VMODE_LIMITED_RANGE;
#endif

		tegra_dc_put(dc);
	}

	if (dc->out->type == TEGRA_DC_OUT_RGB  ||
		dc->out->type == TEGRA_DC_OUT_HDMI ||
		dc->out->type == TEGRA_DC_OUT_DP ||
		dc->out->type == TEGRA_DC_OUT_DSI  ||
		dc->out->type == TEGRA_DC_OUT_NULL)
		return override_disp_mode[dc->out->type].pclk ?
			&override_disp_mode[dc->out->type] : NULL;
	else
		return NULL;
}

static int tegra_dc_set_out(struct tegra_dc *dc, struct tegra_dc_out *out)
{
	struct tegra_dc_mode *mode;
	int err = 0;

	dc->out = out;

	if (dc->out->type == TEGRA_DC_OUT_HDMI &&
			tegra_is_bl_display_initialized(dc->ndev->id)) {
		/*
		 * Bootloader enables clk and host1x in seamless
		 * usecase. Below extra reference accounts for it
		 */
		tegra_dc_get(dc);
	}
/*
 * This config enables seamless feature only for
 * android usecase as a WAR for improper DSI initialization
 * in bootloader for L4T usecase.
 * Bug 200122858
 */
#ifdef CONFIG_ANDROID
	/*
	 * Seamless supporting panels can work in seamless mode
	 * only if BL initializes DC/DSI. If not, panel should
	 * go with complete initialization.
	 */
	if (dc->out->type == TEGRA_DC_OUT_DSI &&
			!tegra_is_bl_display_initialized(dc->ndev->id)) {
		dc->initialized = false;
	} else if (dc->out->type == TEGRA_DC_OUT_DSI &&
			tegra_is_bl_display_initialized(dc->ndev->id)) {
		/*
		 * In case of dsi->csi loopback support, force re-initialize
		 * all DSI controllers. So, set dc->initialized to false.
		 */
		if (dc->out->dsi->dsi_csi_loopback)
			dc->initialized = false;
		else
			dc->initialized = true;
	}
#endif
	mode = tegra_dc_get_override_mode(dc);

	if (mode) {
		tegra_dc_set_mode(dc, mode);

		/*
		 * Bootloader should and should only pass disp_params if
		 * it has initialized display controller.  Whenever we see
		 * override modes, we should skip things cause display resets.
		 */
		dev_info(&dc->ndev->dev, "Bootloader disp_param detected. "
				"Detected mode: %dx%d (on %dx%dmm) pclk=%d\n",
				dc->mode.h_active, dc->mode.v_active,
				dc->out->h_size, dc->out->v_size,
				dc->mode.pclk);
		dc->initialized = true;
	} else if (out->n_modes > 0) {
		/* For VRR panels, default mode is first in the list,
		 * and native panel mode is the last.
		 * Initialization must occur using the native panel mode. */
		if (dc->out->vrr) {
			tegra_dc_set_mode(dc,
				&dc->out->modes[dc->out->n_modes-1]);
			tegra_dc_setup_vrr(dc);
		} else
			tegra_dc_set_mode(dc, &dc->out->modes[0]);
	}
	tegra_dc_sor_instance(dc, out->type);

	switch (out->type) {
	case TEGRA_DC_OUT_RGB:
		dc->out_ops = &tegra_dc_rgb_ops;
		break;

	case TEGRA_DC_OUT_HDMI:
#if	defined(CONFIG_TEGRA_HDMI2_0)
		dc->out_ops = &tegra_dc_hdmi2_0_ops;
#elif defined(CONFIG_TEGRA_HDMI)
		dc->out_ops = &tegra_dc_hdmi_ops;
#endif
#ifdef CONFIG_ARCH_TEGRA_21x_SOC
		if (tegra_bonded_out_dev(BOND_OUT_SOR1)) {
			dev_info(&dc->ndev->dev,
				"SOR1 instance is bonded out\n");
			dc->out_ops = NULL;
			err = -ENODEV;
		}
#endif
		break;

	case TEGRA_DC_OUT_DSI:
	case TEGRA_DC_OUT_FAKE_DSIA:
	case TEGRA_DC_OUT_FAKE_DSIB:
	case TEGRA_DC_OUT_FAKE_DSI_GANGED:
		dc->out_ops = &tegra_dc_dsi_ops;
#ifdef CONFIG_ARCH_TEGRA_21x_SOC
		if (tegra_bonded_out_dev(BOND_OUT_DSI) ||
			tegra_bonded_out_dev(BOND_OUT_DSIB)) {
			dev_info(&dc->ndev->dev,
				"DSI instance is bonded out\n");
			dc->out_ops = NULL;
			err = -ENODEV;
		}
#endif
		break;

#ifdef CONFIG_TEGRA_DP
	case TEGRA_DC_OUT_FAKE_DP:
	case TEGRA_DC_OUT_DP:
		dc->out_ops = &tegra_dc_dp_ops;
		break;
#ifdef CONFIG_TEGRA_NVSR
	case TEGRA_DC_OUT_NVSR_DP:
		dc->out_ops = &tegra_dc_nvsr_ops;
		break;
#endif
#endif
#ifdef CONFIG_TEGRA_LVDS
	case TEGRA_DC_OUT_LVDS:
		dc->out_ops = &tegra_dc_lvds_ops;
		break;
#endif
#ifdef CONFIG_TEGRA_DC_FAKE_PANEL_SUPPORT
	case TEGRA_DC_OUT_NULL:
		dc->out_ops = &tegra_dc_null_ops;
		break;
#endif /*CONFIG_TEGRA_DC_FAKE_PANEL_SUPPORT*/

	default:
		dc->out_ops = NULL;
		break;
	}

#ifdef CONFIG_TEGRA_DC_CMU
	tegra_dc_cache_cmu(dc, tegra_dc_get_cmu(dc));
#endif

	if (dc->out_ops && dc->out_ops->init) {
		err = dc->out_ops->init(dc);
		if (err < 0) {
			dc->out = NULL;
			dc->out_ops = NULL;
			dev_err(&dc->ndev->dev,
				"Error: out->type:%d out_ops->init() failed\n",
				out->type);
			return err;
		}
	}

	return err;
}

int tegra_dc_get_head(const struct tegra_dc *dc)
{
	if (dc)
		return dc->ctrl_num;
	return -EINVAL;
}

/* returns on error: -EINVAL
 * on success: TEGRA_DC_OUT_RGB, TEGRA_DC_OUT_HDMI, ... */
int tegra_dc_get_out(const struct tegra_dc *dc)
{
	if (dc && dc->out)
		return dc->out->type;
	return -EINVAL;
}

bool tegra_dc_is_ext_dp_panel(const struct tegra_dc *dc)
{
	if (dc && dc->out)
		return dc->out->is_ext_dp_panel;
	return false;
}

unsigned tegra_dc_get_out_height(const struct tegra_dc *dc)
{
	unsigned height = 0;

	if (dc->out) {
		if (dc->out->height)
			height = dc->out->height;
		else if (dc->out->h_size && dc->out->v_size)
			height = dc->out->v_size;
	}

	return height;
}
EXPORT_SYMBOL(tegra_dc_get_out_height);

unsigned tegra_dc_get_out_width(const struct tegra_dc *dc)
{
	unsigned width = 0;

	if (dc->out) {
		if (dc->out->width)
			width = dc->out->width;
		else if (dc->out->h_size && dc->out->v_size)
			width = dc->out->h_size;
	}

	return width;
}
EXPORT_SYMBOL(tegra_dc_get_out_width);

unsigned tegra_dc_get_out_max_pixclock(const struct tegra_dc *dc)
{
	if (dc && dc->out)
		return dc->out->max_pixclock;
	else
		return 0;
}
EXPORT_SYMBOL(tegra_dc_get_out_max_pixclock);

void tegra_dc_enable_crc(struct tegra_dc *dc)
{
	u32 val;

	mutex_lock(&dc->lock);
	tegra_dc_get(dc);

	val = CRC_ALWAYS_ENABLE | CRC_INPUT_DATA_ACTIVE_DATA |
		CRC_ENABLE_ENABLE;
	tegra_dc_writel(dc, val, DC_COM_CRC_CONTROL);
	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);

	/* Register a client of frame_end interrupt */
	tegra_dc_config_frame_end_intr(dc, true);
}

void tegra_dc_disable_crc(struct tegra_dc *dc)
{
	/* Unregister a client of frame_end interrupt */
	tegra_dc_config_frame_end_intr(dc, false);

	mutex_lock(&dc->lock);
	tegra_dc_get(dc);
	tegra_dc_writel(dc, 0x0, DC_COM_CRC_CONTROL);
	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);

	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
}

u32 tegra_dc_read_checksum_latched(struct tegra_dc *dc)
{
	int crc = 0;

	if (!dc) {
		pr_err("Failed to get dc: NULL parameter.\n");
		goto crc_error;
	}

	/* If gated quitely return */
	if (!tegra_dc_is_powered(dc))
		return 0;

	reinit_completion(&dc->crc_complete);
	if (dc->crc_pending &&
	    wait_for_completion_interruptible(&dc->crc_complete)) {
		pr_err("CRC read interrupted.\n");
		goto crc_error;
	}

	mutex_lock(&dc->lock);
	tegra_dc_get(dc);
	crc = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM_LATCHED);
	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
crc_error:
	return crc;
}
EXPORT_SYMBOL(tegra_dc_read_checksum_latched);

bool tegra_dc_windows_are_dirty(struct tegra_dc *dc, u32 win_act_req_mask)
{
	u32 val;

	if (tegra_platform_is_linsim() || tegra_platform_is_vdk())
		return false;

	val = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
	if (val & (win_act_req_mask))
		return true;

	return false;
}

static inline void __maybe_unused
enable_dc_irq(const struct tegra_dc *dc)
{
	if (tegra_platform_is_fpga())
		/* Always disable DC interrupts on FPGA. */
		disable_irq(dc->irq);
	else
		enable_irq(dc->irq);
}

/* assumes dc->lock is already taken. */
static void _tegra_dc_vsync_enable(struct tegra_dc *dc)
{
	int vsync_irq;

	if (test_bit(V_BLANK_USER, &dc->vblank_ref_count))
		return; /* already set, nothing needs to be done */
	if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
		vsync_irq = MSF_INT;
	else
		vsync_irq = V_BLANK_INT;
	tegra_dc_hold_dc_out(dc);
	set_bit(V_BLANK_USER, &dc->vblank_ref_count);
	tegra_dc_unmask_interrupt(dc, vsync_irq);
}

int tegra_dc_vsync_enable(struct tegra_dc *dc)
{
	mutex_lock(&dc->lock);
	if (dc->enabled) {
		_tegra_dc_vsync_enable(dc);
		mutex_unlock(&dc->lock);
		return 0;
	}
	mutex_unlock(&dc->lock);
	return 1;
}

/* assumes dc->lock is already taken. */
static void _tegra_dc_vsync_disable(struct tegra_dc *dc)
{
	int vsync_irq;

	if (!test_bit(V_BLANK_USER, &dc->vblank_ref_count))
		return; /* already clear, nothing needs to be done */
	if (dc->out->type == TEGRA_DC_OUT_DSI)
		vsync_irq = MSF_INT;
	else
		vsync_irq = V_BLANK_INT;
	clear_bit(V_BLANK_USER, &dc->vblank_ref_count);
	if (!dc->vblank_ref_count)
		tegra_dc_mask_interrupt(dc, vsync_irq);
	tegra_dc_release_dc_out(dc);
}

void tegra_dc_vsync_disable(struct tegra_dc *dc)
{
	mutex_lock(&dc->lock);
	_tegra_dc_vsync_disable(dc);
	mutex_unlock(&dc->lock);
}

bool tegra_dc_has_vsync(struct tegra_dc *dc)
{
	return true;
}

/* assumes dc->lock is already taken. */
static void _tegra_dc_user_vsync_enable(struct tegra_dc *dc, bool enable)
{
	if (enable) {
		dc->out->user_needs_vblank++;
		init_completion(&dc->out->user_vblank_comp);
		_tegra_dc_vsync_enable(dc);
	} else {
		_tegra_dc_vsync_disable(dc);
		if (dc->out->user_needs_vblank > 0)
			dc->out->user_needs_vblank--;
	}
}

int tegra_dc_wait_for_vsync(struct tegra_dc *dc)
{
	unsigned long timeout_ms;
	unsigned long refresh; /* in 1000th Hz */
	int ret;

	mutex_lock(&dc->lp_lock);
	mutex_lock(&dc->lock);
	if (!dc->enabled) {
		ret = -ENOTTY;
		goto out;
	}
	refresh = tegra_dc_calc_refresh(&dc->mode);
	/* time out if waiting took more than 2 frames */
	timeout_ms = DIV_ROUND_UP(2 * 1000000, refresh);
	_tegra_dc_user_vsync_enable(dc, true);
	mutex_unlock(&dc->lock);
	ret = wait_for_completion_interruptible_timeout(
		&dc->out->user_vblank_comp, msecs_to_jiffies(timeout_ms));
	mutex_lock(&dc->lock);
	_tegra_dc_user_vsync_enable(dc, false);
out:
	mutex_unlock(&dc->lock);
	mutex_unlock(&dc->lp_lock);
	return ret;
}

int _tegra_dc_wait_for_frame_end(struct tegra_dc *dc,
	u32 timeout_ms)
{
	int ret;

	reinit_completion(&dc->frame_end_complete);

	tegra_dc_get(dc);

	tegra_dc_flush_interrupt(dc, FRAME_END_INT);
	/* unmask frame end interrupt */
	_tegra_dc_config_frame_end_intr(dc, true);

	ret = wait_for_completion_interruptible_timeout(
			&dc->frame_end_complete,
			msecs_to_jiffies(timeout_ms));

	_tegra_dc_config_frame_end_intr(dc, false);

	tegra_dc_put(dc);

	return ret;
}

#if defined(CONFIG_TEGRA_NVSD) || defined(CONFIG_TEGRA_NVDISPLAY)
static void tegra_dc_prism_update_backlight(struct tegra_dc *dc)
{
	/* Do the actual brightness update outside of the mutex dc->lock */
	if (dc->out->sd_settings && !dc->out->sd_settings->bl_device &&
		dc->out->sd_settings->bl_device_name) {
		char *bl_device_name =
			dc->out->sd_settings->bl_device_name;
		dc->out->sd_settings->bl_device =
			get_backlight_device_by_name(bl_device_name);
	}

	if (dc->out->sd_settings && dc->out->sd_settings->bl_device) {
		struct backlight_device *bl = dc->out->sd_settings->bl_device;
		backlight_update_status(bl);
	}
}
#endif

void tegra_dc_set_act_vfp(struct tegra_dc *dc, int vfp)
{
	WARN_ON(!mutex_is_locked(&dc->lock));
	WARN_ON(vfp < dc->mode.v_ref_to_sync + 1);
	/* It's very unlikely that active vfp will need to
	 * be changed outside of vrr context */
	WARN_ON(!dc->out->vrr || !dc->out->vrr->capability);

	tegra_dc_writel(dc, WRITE_MUX_ACTIVE | READ_MUX_ACTIVE,
			DC_CMD_STATE_ACCESS);
	tegra_dc_writel(dc, dc->mode.h_front_porch |
			(vfp << 16), DC_DISP_FRONT_PORCH);
	tegra_dc_writel(dc, WRITE_MUX_ASSEMBLY | READ_MUX_ASSEMBLY,
			DC_CMD_STATE_ACCESS);
}

static void tegra_dc_vrr_extend_vfp(struct tegra_dc *dc)
{
	struct tegra_vrr *vrr  = dc->out->vrr;

	if (!vrr || !vrr->capability)
		return;

	if (!vrr->enable)
		return;

	tegra_dc_set_act_vfp(dc, MAX_VRR_V_FRONT_PORCH);
}

int tegra_dc_get_v_count(struct tegra_dc *dc)
{
	u32     value;

	value = tegra_dc_readl(dc, DC_DISP_DISPLAY_DBG_TIMING);
	return (value & DBG_V_COUNT_MASK) >> DBG_V_COUNT_SHIFT;
}

static void tegra_dc_vrr_get_ts(struct tegra_dc *dc)
{
	struct timespec time_now;
	struct tegra_vrr *vrr  = dc->out->vrr;

	if (!vrr || !vrr->capability ||
		(!vrr->enable && !vrr->lastenable))
		return;

	getnstimeofday(&time_now);
	vrr->fe_time_us = (s64)time_now.tv_sec * 1000000 +
				time_now.tv_nsec / 1000;
	vrr->v_count = tegra_dc_get_v_count(dc);
}

static void tegra_dc_vrr_sec(struct tegra_dc *dc)
{
	struct tegra_vrr *vrr  = dc->out->vrr;

	if (!vrr || !vrr->capability)
		return;

	if (!vrr->enable && !vrr->fe_intr_req)
		return;

#ifdef CONFIG_TEGRA_NVDISPLAY
	cancel_delayed_work_sync(&dc->vrr_work);
#endif

	/* Decrement frame end interrupt refcount previously
	   requested by secure library */
	if (vrr->fe_intr_req) {
		_tegra_dc_config_frame_end_intr(dc, false);
		vrr->fe_intr_req = 0;
	}

#if defined(CONFIG_TRUSTED_LITTLE_KERNEL) || defined(CONFIG_OTE_TRUSTY)
	if (te_is_secos_dev_enabled())
		te_vrr_sec();
#endif

	/* Increment frame end interrupt refcount requested
	   by secure library */
	if (vrr->fe_intr_req)
		_tegra_dc_config_frame_end_intr(dc, true);

#ifdef CONFIG_TEGRA_NVDISPLAY
	if (vrr->insert_frame)
		schedule_delayed_work(&dc->vrr_work,
			msecs_to_jiffies(vrr->insert_frame/1000));
#endif
}

static void tegra_dc_vblank(struct work_struct *work)
{
	struct tegra_dc *dc = container_of(work, struct tegra_dc, vblank_work);
#if defined(CONFIG_TEGRA_NVSD) || defined(CONFIG_TEGRA_NVDISPLAY)
	bool nvsd_updated = false;
#endif
	mutex_lock(&dc->lock);

	if (!dc->enabled) {
		mutex_unlock(&dc->lock);
		return;
	}

	tegra_dc_get(dc);

	/* Clear the V_BLANK_FLIP bit of vblank ref-count if update is clean. */
	if (!tegra_dc_windows_are_dirty(dc, WIN_ALL_ACT_REQ))
		clear_bit(V_BLANK_FLIP, &dc->vblank_ref_count);

#ifdef CONFIG_TEGRA_NVDISPLAY
	/*
	 * COMMON channel state is promoted on the very next loadv boundary for
	 * whichever HEAD set COMMON_ACT_REQ. Clear the COMMON channel pending
	 * flag if this condition has been met.
	 */
	tegra_nvdisp_handle_common_channel_promotion(dc);

	if (dc->out->sd_settings) {
		if (dc->out->sd_settings->enable) {
			if ((dc->out->sd_settings->update_sd) ||
					(dc->out->sd_settings->phase_in_steps)) {
				tegra_dc_mask_interrupt(dc, SMARTDIM_INT);
				nvsd_updated = tegra_sd_update_brightness(dc);
				dc->out->sd_settings->update_sd = false;
				tegra_dc_unmask_interrupt(dc, SMARTDIM_INT);
			}
		}
	}
#endif
#ifdef CONFIG_TEGRA_NVSD
	/* Update the SD brightness */
	if (dc->out->sd_settings && !dc->out->sd_settings->use_vpulse2) {
		nvsd_updated = nvsd_update_brightness(dc);
		/* Ref-count vblank if nvsd is on-going. Otherwise, clean the
		 * V_BLANK_NVSD bit of vblank ref-count. */
		if (nvsd_updated) {
			set_bit(V_BLANK_NVSD, &dc->vblank_ref_count);
			tegra_dc_unmask_interrupt(dc, V_BLANK_INT);
		} else {
			clear_bit(V_BLANK_NVSD, &dc->vblank_ref_count);
		}
	}

	/* Mask vblank interrupt if ref-count is zero. */
	if (!dc->vblank_ref_count)
		tegra_dc_mask_interrupt(dc, V_BLANK_INT);
#endif /* CONFIG_TEGRA_NVSD */

	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);

#if defined(CONFIG_TEGRA_NVSD) || defined(CONFIG_TEGRA_NVDISPLAY)
	/* Do the actual brightness update outside of the mutex dc->lock */
	if (nvsd_updated)
		tegra_dc_prism_update_backlight(dc);
#endif
}

#define CSC_UPDATE_IF_CHANGED(entry, ENTRY) do { \
		if (cmu_active->csc.entry != cmu_shadow->csc.entry || \
			dc->cmu_shadow_force_update) { \
			cmu_active->csc.entry = cmu_shadow->csc.entry; \
			tegra_dc_writel(dc, \
				cmu_active->csc.entry, \
				DC_COM_CMU_CSC_##ENTRY); \
		} \
	} while (0)

static void _tegra_dc_handle_hdr(struct tegra_dc *dc)
{
	mutex_lock(&dc->lock);
	if (!dc->enabled) {
		mutex_unlock(&dc->lock);
		return;
	}

	tegra_dc_get(dc);

	if (dc->out_ops->set_hdr)
		dc->out_ops->set_hdr(dc);

	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);

	return;
}

static void tegra_dc_frame_end(struct work_struct *work)
{
	struct tegra_dc *dc = container_of(work,
		struct tegra_dc, frame_end_work);
#ifdef CONFIG_TEGRA_DC_CMU
	u32 val;
	u32 i;

	mutex_lock(&dc->lock);

	if (!dc->enabled) {
		mutex_unlock(&dc->lock);
		return;
	}

	tegra_dc_get(dc);

	if (dc->cmu_shadow_dirty) {
		struct tegra_dc_cmu *cmu_active = &dc->cmu;
		struct tegra_dc_cmu *cmu_shadow = &dc->cmu_shadow;

		for (i = 0; i < 256; i++) {
			if (cmu_active->lut1[i] != cmu_shadow->lut1[i] ||
				dc->cmu_shadow_force_update) {
				cmu_active->lut1[i] = cmu_shadow->lut1[i];
				val = LUT1_ADDR(i) |
					LUT1_DATA(cmu_shadow->lut1[i]);
				tegra_dc_writel(dc, val, DC_COM_CMU_LUT1);
			}
		}

		CSC_UPDATE_IF_CHANGED(krr, KRR);
		CSC_UPDATE_IF_CHANGED(kgr, KGR);
		CSC_UPDATE_IF_CHANGED(kbr, KBR);
		CSC_UPDATE_IF_CHANGED(krg, KRG);
		CSC_UPDATE_IF_CHANGED(kgg, KGG);
		CSC_UPDATE_IF_CHANGED(kbg, KBG);
		CSC_UPDATE_IF_CHANGED(krb, KRB);
		CSC_UPDATE_IF_CHANGED(kgb, KGB);
		CSC_UPDATE_IF_CHANGED(kbb, KBB);

		for (i = 0; i < 960; i++)
			if (cmu_active->lut2[i] != cmu_shadow->lut2[i] ||
				dc->cmu_shadow_force_update) {
				cmu_active->lut2[i] = cmu_shadow->lut2[i];
				val = LUT2_ADDR(i) |
					LUT2_DATA(cmu_active->lut2[i]);
				tegra_dc_writel(dc, val, DC_COM_CMU_LUT2);
			}

		dc->cmu_shadow_dirty = false;
		dc->cmu_shadow_force_update = false;
		_tegra_dc_config_frame_end_intr(dc, false);
	}

	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
#endif
	if (dc->hdr_cache_dirty) {
		_tegra_dc_handle_hdr(dc);
		_tegra_dc_config_frame_end_intr(dc, false);
		dc->hdr_cache_dirty = false;
	}
	return;
}

static void tegra_dc_one_shot_worker(struct work_struct *work)
{
	struct tegra_dc *dc = container_of(
		to_delayed_work(work), struct tegra_dc, one_shot_work);
	mutex_lock(&dc->lock);

	/* memory client has gone idle */
	tegra_dc_clear_bandwidth(dc);

	if (dc->out_ops && dc->out_ops->idle) {
		tegra_dc_io_start(dc);
		dc->out_ops->idle(dc);
		tegra_dc_io_end(dc);
	}

	mutex_unlock(&dc->lock);
}

#if !defined(CONFIG_TEGRA_NVDISPLAY)
/* return an arbitrarily large number if count overflow occurs.
 * make it a nice base-10 number to show up in stats output */
static u64 tegra_dc_underflow_count(struct tegra_dc *dc, unsigned reg)
{
	unsigned count = tegra_dc_readl(dc, reg);

	tegra_dc_writel(dc, 0, reg);
	return ((count & 0x80000000) == 0) ? count : 10000000000ll;
}
#endif

static void tegra_dc_underflow_handler(struct tegra_dc *dc)
{
#if !defined(CONFIG_TEGRA_NVDISPLAY)

	const u32 masks[] = {
		WIN_A_UF_INT,
		WIN_B_UF_INT,
		WIN_C_UF_INT,
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC)
		WIN_D_UF_INT,
		HC_UF_INT,
		WIN_T_UF_INT,
#endif
	};
	int i;

	dc->stats.underflows++;
	if (dc->underflow_mask & WIN_A_UF_INT)
		dc->stats.underflows_a += tegra_dc_underflow_count(dc,
			DC_WINBUF_AD_UFLOW_STATUS);
	if (dc->underflow_mask & WIN_B_UF_INT)
		dc->stats.underflows_b += tegra_dc_underflow_count(dc,
			DC_WINBUF_BD_UFLOW_STATUS);
	if (dc->underflow_mask & WIN_C_UF_INT)
		dc->stats.underflows_c += tegra_dc_underflow_count(dc,
			DC_WINBUF_CD_UFLOW_STATUS);
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC)
	if (dc->underflow_mask & HC_UF_INT)
		dc->stats.underflows_h += tegra_dc_underflow_count(dc,
			DC_WINBUF_HD_UFLOW_STATUS);
	if (dc->underflow_mask & WIN_D_UF_INT)
		dc->stats.underflows_d += tegra_dc_underflow_count(dc,
			DC_WINBUF_DD_UFLOW_STATUS);
	if (dc->underflow_mask & WIN_T_UF_INT)
		dc->stats.underflows_t += tegra_dc_underflow_count(dc,
			DC_WINBUF_TD_UFLOW_STATUS);
#endif

	/* Check for any underflow reset conditions */
	for_each_set_bit(i, &dc->valid_windows, DC_N_WINDOWS) {
		struct tegra_dc_win *win = tegra_dc_get_window(dc, i);
		if (WARN_ONCE(i >= ARRAY_SIZE(masks),
			"underflow stats unsupported"))
			break; /* bail if the table above is missing entries */
		if (!masks[i])
			continue; /* skip empty entries */

		if (dc->underflow_mask & masks[i]) {
			win->underflows++;

#ifdef CONFIG_ARCH_TEGRA_2x_SOC
			if (i < 3 && win->underflows > 4) {
				schedule_work(&dc->reset_work);
				/* reset counter */
				win->underflows = 0;
				trace_display_reset(dc);
			}
#endif
#ifdef CONFIG_ARCH_TEGRA_3x_SOC
			if (i < 3 && win->underflows > 4) {
				trace_display_reset(dc);
				tegra_dc_writel(dc, UF_LINE_FLUSH,
						DC_DISP_DISP_MISC_CONTROL);
				tegra_dc_writel(dc, GENERAL_ACT_REQ,
						DC_CMD_STATE_CONTROL);

				tegra_dc_writel(dc, 0,
						DC_DISP_DISP_MISC_CONTROL);
				tegra_dc_writel(dc, GENERAL_ACT_REQ,
						DC_CMD_STATE_CONTROL);
			}
#endif
		} else {
			win->underflows = 0;
		}
	}

#else
	tegra_nvdisp_underflow_handler(dc);
#endif /* CONFIG_TEGRA_NVDISPLAY */

	/* Clear the underflow mask now that we've checked it. */
	tegra_dc_writel(dc, dc->underflow_mask, DC_CMD_INT_STATUS);
	dc->underflow_mask = 0;
	tegra_dc_unmask_interrupt(dc, ALL_UF_INT());
	trace_underflow(dc);
}

#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && !defined(CONFIG_ARCH_TEGRA_3x_SOC)
static void tegra_dc_vpulse2(struct work_struct *work)
{
	struct tegra_dc *dc = container_of(work, struct tegra_dc, vpulse2_work);
#ifdef CONFIG_TEGRA_NVSD
	bool nvsd_updated = false;
#endif

	mutex_lock(&dc->lock);

	if (!dc->enabled) {
		mutex_unlock(&dc->lock);
		return;
	}

	tegra_dc_get(dc);

	/* Clear the V_PULSE2_FLIP if no update */
	if (!tegra_dc_windows_are_dirty(dc, WIN_ALL_ACT_REQ))
		clear_bit(V_PULSE2_FLIP, &dc->vpulse2_ref_count);

#ifdef CONFIG_TEGRA_NVSD
	/* Update the SD brightness */
	if (dc->out->sd_settings && dc->out->sd_settings->use_vpulse2) {
		nvsd_updated = nvsd_update_brightness(dc);

		if (nvsd_updated) {
			set_bit(V_PULSE2_NVSD, &dc->vpulse2_ref_count);
			tegra_dc_unmask_interrupt(dc, V_PULSE2_INT);
		} else {
			clear_bit(V_PULSE2_NVSD, &dc->vpulse2_ref_count);
		}
	}

	/* Mask vpulse2 interrupt if ref-count is zero. */
	if (!dc->vpulse2_ref_count)
		tegra_dc_mask_interrupt(dc, V_PULSE2_INT);
#endif /* CONFIG_TEGRA_NVSD */

	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);

#ifdef CONFIG_TEGRA_NVSD
	/* Do the actual brightness update outside of the mutex dc->lock */
	if (nvsd_updated)
		tegra_dc_prism_update_backlight(dc);
#endif
}
#endif

static void tegra_dc_process_vblank(struct tegra_dc *dc, ktime_t timestamp)
{
	/* pending user vblank, so wakeup */
	if (dc->out->user_needs_vblank) {
		dc->out->user_needs_vblank = false;
		complete(&dc->out->user_vblank_comp);
	}
	if (test_bit(V_BLANK_USER, &dc->vblank_ref_count)) {
#ifdef CONFIG_ADF_TEGRA
		tegra_adf_process_vblank(dc->adf, timestamp);
#endif
#ifdef CONFIG_TEGRA_DC_EXTENSIONS
		tegra_dc_ext_process_vblank(dc->ndev->id, timestamp);
#endif
	}
}

int tegra_dc_config_frame_end_intr(struct tegra_dc *dc, bool enable)
{
	int ret;

	mutex_lock(&dc->lock);
	ret = _tegra_dc_config_frame_end_intr(dc, enable);
	mutex_unlock(&dc->lock);

	return ret;
}

static void tegra_dc_one_shot_irq(struct tegra_dc *dc, unsigned long status,
		ktime_t timestamp)
{
	if (status & MSF_INT)
		tegra_dc_process_vblank(dc, timestamp);

	if (status & V_BLANK_INT) {
		/* Sync up windows. */
		tegra_dc_trigger_windows(dc);

		/* Schedule any additional bottom-half vblank actvities. */
		queue_work(system_freezable_wq, &dc->vblank_work);
	}

	if (status & FRAME_END_INT) {
		/* Mark the frame_end as complete. */
		dc->crc_pending = false;
		if (!completion_done(&dc->frame_end_complete))
			complete(&dc->frame_end_complete);
		if (!completion_done(&dc->crc_complete))
			complete(&dc->crc_complete);

		if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
			tegra_dc_put(dc);

		queue_work(system_freezable_wq, &dc->frame_end_work);
	}

#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && !defined(CONFIG_ARCH_TEGRA_3x_SOC)
	if (status & V_PULSE2_INT)
		queue_work(system_freezable_wq, &dc->vpulse2_work);
#endif
}

static void tegra_dc_continuous_irq(struct tegra_dc *dc, unsigned long status,
		ktime_t timestamp)
{
	/* Schedule any additional bottom-half vblank actvities. */
	if (status & V_BLANK_INT) {
#ifdef CONFIG_TEGRA_NVDISPLAY
		if (status & SMARTDIM_INT) {
			if (dc->out->sd_settings)
				dc->out->sd_settings->update_sd = true;
		}
#endif
		queue_work(system_freezable_wq, &dc->vblank_work);
	}
	if (status & (V_BLANK_INT | MSF_INT)) {
		if (dc->out->user_needs_vblank) {
			dc->out->user_needs_vblank = false;
			complete(&dc->out->user_vblank_comp);
		}
		tegra_dc_process_vblank(dc, timestamp);
	}

	if (status & FRAME_END_INT) {
		struct timespec tm;
		ktime_get_ts(&tm);
		dc->frame_end_timestamp = timespec_to_ns(&tm);
		wake_up(&dc->timestamp_wq);

		if (!tegra_dc_windows_are_dirty(dc, WIN_ALL_ACT_REQ)) {
			if (dc->out->type == TEGRA_DC_OUT_DSI) {
				tegra_dc_vrr_get_ts(dc);
				tegra_dc_vrr_sec(dc);
			} else
				tegra_dc_vrr_extend_vfp(dc);
		}

		/* Mark the frame_end as complete. */
		if (!completion_done(&dc->frame_end_complete))
			complete(&dc->frame_end_complete);
		if (!completion_done(&dc->crc_complete))
			complete(&dc->crc_complete);

		tegra_dc_trigger_windows(dc);

		queue_work(system_freezable_wq, &dc->frame_end_work);
	}

#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && !defined(CONFIG_ARCH_TEGRA_3x_SOC)
	if (status & V_PULSE2_INT)
		queue_work(system_freezable_wq, &dc->vpulse2_work);
#endif
}

/* XXX: Not sure if we limit look ahead to 1 frame */
bool tegra_dc_is_within_n_vsync(struct tegra_dc *dc, s64 ts)
{
	BUG_ON(!dc->frametime_ns);
	return ((ts - dc->frame_end_timestamp) < dc->frametime_ns);
}

bool tegra_dc_does_vsync_separate(struct tegra_dc *dc, s64 new_ts, s64 old_ts)
{
	BUG_ON(!dc->frametime_ns);
	return (((new_ts - old_ts) > dc->frametime_ns)
		|| (div_s64((new_ts - dc->frame_end_timestamp), dc->frametime_ns)
			!= div_s64((old_ts - dc->frame_end_timestamp),
				dc->frametime_ns)));
}

static irqreturn_t tegra_dc_irq(int irq, void *ptr)
{
	ktime_t timestamp = ktime_get();
	struct tegra_dc *dc = ptr;
	unsigned long status;
	unsigned long underflow_mask;
	u32 val;
	int need_disable = 0;

#ifndef CONFIG_TEGRA_NVDISPLAY
	if (tegra_platform_is_fpga())
		return IRQ_NONE;
#endif
	mutex_lock(&dc->lock);
	if (!tegra_dc_is_powered(dc)) {
		mutex_unlock(&dc->lock);
		return IRQ_HANDLED;
	}

	tegra_dc_get(dc);

	if (!dc->enabled || !nvhost_module_powered_ext(dc->ndev)) {
		dev_dbg(&dc->ndev->dev, "IRQ when DC not powered!\n");
		status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
		tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
		tegra_dc_put(dc);
		mutex_unlock(&dc->lock);
		return IRQ_HANDLED;
	}

	/* clear all status flags except underflow, save those for the worker */
	status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
	tegra_dc_writel(dc, status & ~ALL_UF_INT(), DC_CMD_INT_STATUS);
	val = tegra_dc_readl(dc, DC_CMD_INT_MASK);
	tegra_dc_writel(dc, val & ~ALL_UF_INT(), DC_CMD_INT_MASK);

	/*
	 * Overlays can get thier internal state corrupted during and underflow
	 * condition.  The only way to fix this state is to reset the DC.
	 * if we get 4 consecutive frames with underflows, assume we're
	 * hosed and reset.
	 */
	underflow_mask = status & ALL_UF_INT();

	/* Check underflow */
	if (underflow_mask) {
		dc->underflow_mask |= underflow_mask;
		schedule_delayed_work(&dc->underflow_work,
			msecs_to_jiffies(1));
	}

	if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE ||
		dc->out->flags & TEGRA_DC_OUT_NVSR_MODE)
		tegra_dc_one_shot_irq(dc, status, timestamp);
	else
		tegra_dc_continuous_irq(dc, status, timestamp);

	if (dc->nvsr)
		tegra_dc_nvsr_irq(dc->nvsr, status);

	/* update video mode if it has changed since the last frame */
	if (status & (FRAME_END_INT | V_BLANK_INT))
		if (tegra_dc_update_mode(dc))
			need_disable = 1; /* force display off on error */

	if (status & FRAME_END_INT) {
		dc->dbg_fe_count++;
		if (dc->disp_active_dirty) {
			tegra_dc_writel(dc, dc->mode.h_active |
				(dc->mode.v_active << 16), DC_DISP_DISP_ACTIVE);
			tegra_dc_writel(dc,
				GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);

			dc->disp_active_dirty = false;
		}
	}

	if (status & V_BLANK_INT)
		trace_display_vblank(dc->ctrl_num,
			tegra_dc_readl(dc, DC_COM_RG_DPCA) >> 16);

	tegra_dc_put(dc);

#ifdef TEGRA_DC_USR_SHARED_IRQ
	/* user shared display ISR call-back */
	if (dc->isr_usr_cb)
		dc->isr_usr_cb(dc->ctrl_num, status, dc->isr_usr_pdt);
#endif /* TEGRA_DC_USR_SHARED_IRQ */

	mutex_unlock(&dc->lock);

	if (need_disable)
		tegra_dc_disable_irq_ops(dc, true);

	return IRQ_HANDLED;
}

void tegra_dc_set_color_control(struct tegra_dc *dc)
{
	u32 color_control;

	switch (dc->out->depth) {
	case 3:
		color_control = BASE_COLOR_SIZE111;
		break;

	case 6:
		color_control = BASE_COLOR_SIZE222;
		break;

	case 8:
		color_control = BASE_COLOR_SIZE332;
		break;

	case 9:
		color_control = BASE_COLOR_SIZE333;
		break;

	case 12:
		color_control = BASE_COLOR_SIZE444;
		break;

	case 15:
		color_control = BASE_COLOR_SIZE555;
		break;

	case 16:
		color_control = BASE_COLOR_SIZE565;
		break;

	case 18:
		color_control = BASE_COLOR_SIZE666;
		break;

	default:
		color_control = BASE_COLOR_SIZE888;
		break;
	}

	switch (dc->out->dither) {
	case TEGRA_DC_UNDEFINED_DITHER:
	case TEGRA_DC_DISABLE_DITHER:
		color_control |= DITHER_CONTROL_DISABLE;
		break;
	case TEGRA_DC_ORDERED_DITHER:
		color_control |= DITHER_CONTROL_ORDERED;
		break;
#ifdef CONFIG_TEGRA_DC_TEMPORAL_DITHER
	case TEGRA_DC_TEMPORAL_DITHER:
		color_control |= DITHER_CONTROL_TEMPORAL;
		break;
#else
	case TEGRA_DC_ERRDIFF_DITHER:
		/* The line buffer for error-diffusion dither is limited
		 * to 1280 pixels per line. This limits the maximum
		 * horizontal active area size to 1280 pixels when error
		 * diffusion is enabled.
		 */
		BUG_ON(dc->mode.h_active > 1280);
		color_control |= DITHER_CONTROL_ERRDIFF;
		break;
#endif
	default:
		dev_err(&dc->ndev->dev, "Error: Unsupported dithering mode\n");
	}

#ifdef CONFIG_TEGRA_DC_CMU
	if (dc->cmu_enabled)
		color_control |= CMU_ENABLE;
#endif

	tegra_dc_writel(dc, color_control, DC_DISP_DISP_COLOR_CONTROL);
}


/*
 * Due to the limitations in DSC architecture, program DSC block with predefined
 * values.
*/
void tegra_dc_dsc_init(struct tegra_dc *dc)
{
	struct tegra_dc_mode *mode = &dc->mode;
	u32 val;
	u32 slice_width, slice_height, chunk_size, hblank;
	u32 min_rate_buf_size, num_xtra_mux_bits, hrdelay;
	u32 initial_offset, final_offset;
	u32 initial_xmit_delay, initial_dec_delay;
	u32 initial_scale_value, final_scale;
	u32 scale_dec_interval, scale_inc_interval;
	u32 groups_per_line, total_groups, first_line_bpg_offset;
	u32 nfl_bpg_offset, slice_bpg_offset;
	u32 rc_model_size = DSC_DEF_RC_MODEL_SIZE;
	u32 delay_in_slice, output_delay, wrap_output_delay;
	u8 i, j;
	u8 bpp;
#if defined(CONFIG_ARCH_TEGRA_21x_SOC)
	u32 check_flatness;
#endif

	/* Link compression is only supported for DSI panels */
	if ((dc->out->type != TEGRA_DC_OUT_DSI) || !dc->out->dsc_en) {
		dev_info(&dc->ndev->dev,
			"Link compression not supported by the panel\n");
		return;
	}

	dev_info(&dc->ndev->dev, "Configuring DSC\n");
	/*
	 * Slice height and width are in pixel. When the whole picture is one
	 * slice, slice height and width should be equal to picture height or
	 * width.
	*/
	bpp = dc->out->dsc_bpp;
	slice_height = dc->out->slice_height;
	slice_width = (mode->h_active / dc->out->num_of_slices);
	val = DSC_VALID_SLICE_HEIGHT(slice_height) |
		DSC_VALID_SLICE_WIDTH(slice_width);
	tegra_dc_writel(dc, val, DSC_COM_DSC_SLICE_INFO);

#if defined(CONFIG_ARCH_TEGRA_21x_SOC)
	/*
	 * Use RC overflow solution 2. Program overflow threshold values and
	 * enable flatness checking.
	 */
	check_flatness = ((3 - (slice_width % 3)) != 2);
	val = DC_DISP_SPARE0_VALID_OVERFLOW_THRES(DC_DISP_DEF_OVERFLOW_THRES) |
		DC_DISP_SPARE0_RC_SOLUTION_MODE(DC_SPARE0_RC_SOLUTION_2) |
		(check_flatness << 1) | 0x1;
	tegra_dc_writel(dc, val, DC_DISP_DISPLAY_SPARE0);
#endif
	/*
	 * Calculate chunk size based on slice width. Enable block prediction
	 * and set compressed bpp rate.
	 */
	chunk_size = DIV_ROUND_UP((slice_width * bpp), 8);
	val = DSC_VALID_BITS_PER_PIXEL(bpp << 4) |
		DSC_VALID_CHUNK_SIZE(chunk_size);
	if (dc->out->en_block_pred)
		val |= DSC_BLOCK_PRED_ENABLE;
	tegra_dc_writel(dc, val, DSC_COM_DSC_COMMON_CTRL);

	/* Set output delay */
	initial_xmit_delay = (4096 / bpp);
	if (slice_height == mode->v_active)
		initial_xmit_delay = 475;
	delay_in_slice = DIV_ROUND_UP(DSC_ENC_FIFO_SIZE * 8 * 3, bpp) +
		slice_width + initial_xmit_delay + DSC_START_PIXEL_POS;
	hblank = mode->h_sync_width + mode->h_front_porch + mode->h_back_porch;
	output_delay = ((delay_in_slice / slice_width) *
		(mode->h_active + hblank)) + (delay_in_slice % slice_width);
	wrap_output_delay = output_delay + 20;
	val = DSC_VALID_OUTPUT_DELAY(output_delay);
	val |= DSC_VALID_WRAP_OUTPUT_DELAY(wrap_output_delay);
	tegra_dc_writel(dc, val, DC_COM_DSC_DELAY);

	/* Set RC flatness info and bpg offset for first line of slice */
	first_line_bpg_offset = (bpp == 8) ? DSC_DEF_8BPP_FIRST_LINE_BPG_OFFS :
		DSC_DEF_12BPP_FIRST_LINE_BPG_OFFS;
	val = DSC_VALID_FLATNESS_MAX_QP(12) | DSC_VALID_FLATNESS_MIN_QP(3) |
		DSC_VALID_FIRST_LINE_BPG_OFFS(first_line_bpg_offset);
	tegra_dc_writel(dc, val, DC_COM_DSC_RC_FLATNESS_INFO);


	/* Set RC model offset values to be used at slice start and end */
	initial_offset = (bpp == 8) ? DSC_DEF_8BPP_INITIAL_OFFSET :
		DSC_DEF_12BPP_INITIAL_OFFSET;
	num_xtra_mux_bits = 198 + ((chunk_size * slice_height * 8 - 246) % 48);
	final_offset = rc_model_size - (initial_xmit_delay * bpp) +
		num_xtra_mux_bits;
	val = DSC_VALID_INITIAL_OFFSET(initial_offset) |
		DSC_VALID_FINAL_OFFSET(final_offset);
	tegra_dc_writel(dc, val, DC_COM_DSC_RC_OFFSET_INFO);

	/*
	 * DSC_SLICE_BPG_OFFSET:Bpg offset used to enforce slice bit constraint
	 * DSC_NFL_BPG_OFFSET:Non-first line bpg offset to use
	 */
	nfl_bpg_offset = DIV_ROUND_UP((first_line_bpg_offset << 11),
		(slice_height - 1));
	slice_bpg_offset = (rc_model_size - initial_offset +
		num_xtra_mux_bits) * (1 << 11);
	groups_per_line = slice_width / 3;
	total_groups = slice_height * groups_per_line;
	slice_bpg_offset = DIV_ROUND_UP(slice_bpg_offset, total_groups);
	val = DSC_VALID_SLICE_BPG_OFFSET(slice_bpg_offset) |
		DSC_VALID_NFL_BPG_OFFSET(nfl_bpg_offset);
	tegra_dc_writel(dc, val, DC_COM_DSC_RC_BPGOFF_INFO);

	/*
	 * INITIAL_DEC_DELAY:Num of pixels to delay the VLD on the decoder
	 * INITIAL_XMIT_DELAY:Num of pixels to delay the initial transmission
	 */
	min_rate_buf_size = rc_model_size - initial_offset +
		(initial_xmit_delay * bpp) +
		(groups_per_line * first_line_bpg_offset);
	hrdelay = DIV_ROUND_UP(min_rate_buf_size, bpp);
	initial_dec_delay = hrdelay - initial_xmit_delay;
	val = DSC_VALID_INITIAL_XMIT_DELAY(initial_xmit_delay) |
		DSC_VALID_INITIAL_DEC_DELAY(initial_dec_delay);
	tegra_dc_writel(dc, val, DSC_COM_DSC_RC_RELAY_INFO);

	/*
	 * SCALE_DECR_INTERVAL:Decrement scale factor every scale_decr_interval
	 * groups.
	 * INITIAL_SCALE_VALUE:Initial value for scale factor
	 * SCALE_INCR_INTERVAL:Increment scale factor every scale_incr_interval
	 * groups.
	 */
	initial_scale_value = (8 * rc_model_size) / (rc_model_size -
		initial_offset);
	scale_dec_interval = groups_per_line / (initial_scale_value - 8);
	val = DSC_VALID_SCALE_DECR_INTERVAL(scale_dec_interval) |
		DSC_VALID_INITIAL_SCALE_VALUE(initial_scale_value);
	tegra_dc_writel(dc, val, DC_COM_DSC_RC_SCALE_INFO);

	final_scale = (8 * rc_model_size) / (rc_model_size - final_offset);
	scale_inc_interval = (2048 * final_offset) /
		((final_scale - 9) * (slice_bpg_offset + nfl_bpg_offset));
	val = DSC_VALID_SCALE_INCR_INTERVAL(scale_inc_interval);
	tegra_dc_writel(dc, val, DC_COM_DSC_RC_SCALE_INFO_2);

	/* Set the RC parameters */
	val = DSC_VALID_RC_TGT_OFFSET_LO(3) | DSC_VALID_RC_TGT_OFFSET_HI(3) |
		DSC_VALID_RC_EDGE_FACTOR(6) |
		DSC_VALID_RC_QUANT_INCR_LIMIT1(11) |
		DSC_VALID_RC_QUANT_INCR_LIMIT0(11);
	tegra_dc_writel(dc, val, DC_COM_DSC_RC_PARAM_SET);

	for (i = 0, j = 0; j < DSC_MAX_RC_BUF_THRESH_REGS; j++) {
		val = DSC_VALID_RC_BUF_THRESH_0(dsc_rc_buf_thresh[i++]);
		val |= DSC_VALID_RC_BUF_THRESH_1(dsc_rc_buf_thresh[i++]);
		val |= DSC_VALID_RC_BUF_THRESH_2(dsc_rc_buf_thresh[i++]);
		val |= DSC_VALID_RC_BUF_THRESH_3(dsc_rc_buf_thresh[i++]);

		if (dsc_rc_buf_thresh_regs[j] == DC_COM_DSC_RC_BUF_THRESH_0)
			val |= DSC_VALID_RC_MODEL_SIZE(rc_model_size);
		tegra_dc_writel(dc, val, dsc_rc_buf_thresh_regs[j]);
	}

	for (i = 0, j = 0; j < DSC_MAX_RC_RANGE_CFG_REGS; j++) {
		val = DSC_VALID_RC_RANGE_PARAM_LO(
			SET_RC_RANGE_MIN_QP(dsc_rc_ranges_8bpp_8bpc[i][0]) |
			SET_RC_RANGE_MAX_QP(dsc_rc_ranges_8bpp_8bpc[i][1]) |
			SET_RC_RANGE_BPG_OFFSET(dsc_rc_ranges_8bpp_8bpc[i][2]));
		i++;
		val |= DSC_VALID_RC_RANGE_PARAM_HI(
			SET_RC_RANGE_MIN_QP(dsc_rc_ranges_8bpp_8bpc[i][0]) |
			SET_RC_RANGE_MAX_QP(dsc_rc_ranges_8bpp_8bpc[i][1]) |
			SET_RC_RANGE_BPG_OFFSET(dsc_rc_ranges_8bpp_8bpc[i][2]));
		i++;
		tegra_dc_writel(dc, val, dsc_rc_range_config[j]);
	}

	val = tegra_dc_readl(dc, DC_COM_DSC_UNIT_SET);
	val &= ~DSC_LINEBUF_DEPTH_8_BIT;
	val |= DSC_VALID_SLICE_NUM_MINUS1_IN_LINE(dc->out->num_of_slices - 1);
	val |= DSC_CHECK_FLATNESS2;
	val |= DSC_FLATNESS_FIX_EN;
	tegra_dc_writel(dc, val, DC_COM_DSC_UNIT_SET);

	dev_info(&dc->ndev->dev, "DSC configured\n");
}

void tegra_dc_en_dis_dsc(struct tegra_dc *dc, bool enable)
{
	u32 val;
	bool is_enabled = false, set_reg = false;

	if ((dc->out->type != TEGRA_DC_OUT_DSI) || !dc->out->dsc_en)
		return;

	val = tegra_dc_readl(dc, DC_COM_DSC_TOP_CTL);
	if (val & DSC_ENABLE)
		is_enabled = true;

	if (enable && !is_enabled) {
		val |= DSC_ENABLE;
		set_reg = true;
	} else if (!enable && is_enabled) {
		val &= ~DSC_ENABLE;
		set_reg = true;
	}

	if (set_reg) {
		dev_info(&dc->ndev->dev, "Link compression %s\n",
			enable ? "enabled" : "disabled");
		val &= ~DSC_AUTO_RESET;
		tegra_dc_writel(dc, val, DC_COM_DSC_TOP_CTL);
	}
}

#ifndef CONFIG_TEGRA_NVDISPLAY
static void tegra_dc_init_vpulse2_int(struct tegra_dc *dc)
{
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && !defined(CONFIG_ARCH_TEGRA_3x_SOC)
	u32 start, end;
	unsigned long val;

	val = V_PULSE2_H_POSITION(0) | V_PULSE2_LAST(0x1);
	tegra_dc_writel(dc, val, DC_DISP_V_PULSE2_CONTROL);

	start = dc->mode.v_ref_to_sync + dc->mode.v_sync_width +
		dc->mode.v_back_porch +	dc->mode.v_active;
	end = start + 1;
	val = V_PULSE2_START_A(start) + V_PULSE2_END_A(end);
	tegra_dc_writel(dc, val, DC_DISP_V_PULSE2_POSITION_A);

	val = tegra_dc_readl(dc, DC_CMD_INT_ENABLE);
	val |= V_PULSE2_INT;
	tegra_dc_writel(dc, val , DC_CMD_INT_ENABLE);

	tegra_dc_mask_interrupt(dc, V_PULSE2_INT);
	val = tegra_dc_readl(dc, DC_DISP_DISP_SIGNAL_OPTIONS0);
	val |= V_PULSE_2_ENABLE;
	tegra_dc_writel(dc, val, DC_DISP_DISP_SIGNAL_OPTIONS0);
#endif
}

static int tegra_dc_init(struct tegra_dc *dc)
{
	int i;
	int int_enable;
	u32 val;

	tegra_dc_io_start(dc);
	tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
	tegra_dc_writel(dc, 0x00000100 | dc->vblank_syncpt,
			DC_CMD_CONT_SYNCPT_VSYNC);

	tegra_dc_writel(dc, 0x00004700, DC_CMD_INT_TYPE);
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC)
	tegra_dc_writel(dc, WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT |
		WIN_T_UF_INT | WIN_D_UF_INT | HC_UF_INT |
		WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT,
		DC_CMD_INT_POLARITY);
#else
	tegra_dc_writel(dc, WIN_A_OF_INT | WIN_B_OF_INT | WIN_C_OF_INT |
		WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT,
		DC_CMD_INT_POLARITY);
#endif
	tegra_dc_writel(dc, 0x00202020, DC_DISP_MEM_HIGH_PRIORITY);
	tegra_dc_writel(dc, 0x00010101, DC_DISP_MEM_HIGH_PRIORITY_TIMER);
#ifdef CONFIG_ARCH_TEGRA_3x_SOC
	tegra_dc_writel(dc, 0x00000000, DC_DISP_DISP_MISC_CONTROL);
#endif
	/* enable interrupts for vblank, frame_end and underflows */
	int_enable = (FRAME_END_INT | V_BLANK_INT | ALL_UF_INT());
	/* for panels with one-shot mode enable tearing effect interrupt */
	if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
		int_enable |= MSF_INT;

	tegra_dc_writel(dc, int_enable, DC_CMD_INT_ENABLE);
	tegra_dc_writel(dc, ALL_UF_INT(), DC_CMD_INT_MASK);
	tegra_dc_init_vpulse2_int(dc);

	tegra_dc_writel(dc, WRITE_MUX_ASSEMBLY | READ_MUX_ASSEMBLY,
		DC_CMD_STATE_ACCESS);

#if !defined(CONFIG_TEGRA_DC_BLENDER_GEN2)
	tegra_dc_writel(dc, 0x00000000, DC_DISP_BORDER_COLOR);
#else
	tegra_dc_writel(dc, 0x00000000, DC_DISP_BLEND_BACKGROUND_COLOR);
#endif

#ifdef CONFIG_TEGRA_DC_CMU
	if (dc->is_cmu_set_bl)
		_tegra_dc_update_cmu_aligned(dc, &dc->cmu, true);
	else
		_tegra_dc_update_cmu(dc, &dc->cmu);
	dc->is_cmu_set_bl = false;
#endif
	tegra_dc_set_color_control(dc);
	for_each_set_bit(i, &dc->valid_windows, DC_N_WINDOWS) {
		struct tegra_dc_win *win = tegra_dc_get_window(dc, i);
		tegra_dc_writel(dc, WINDOW_A_SELECT << i,
				DC_CMD_DISPLAY_WINDOW_HEADER);
		tegra_dc_set_csc(dc, &win->csc);
		tegra_dc_set_lut(dc, win);
		tegra_dc_set_scaling_filter(dc);
	}

#ifdef CONFIG_TEGRA_DC_WIN_H
	/* Window H is set to window mode by default for t14x. */
	tegra_dc_writel(dc, WINH_CURS_SELECT(1),
			DC_DISP_BLEND_CURSOR_CONTROL);
#endif

	for_each_set_bit(i, &dc->valid_windows, DC_N_WINDOWS) {
		struct tegra_dc_win *win = tegra_dc_get_window(dc, i);

		BUG_ON(!win);

		/* refuse to operate on invalid syncpts */
		if (WARN_ON(win->syncpt.id == NVSYNCPT_INVALID))
			continue;

		if (!nvhost_syncpt_read_ext_check(dc->ndev, win->syncpt.id, &val))
			win->syncpt.min = win->syncpt.max = val;
	}

	dc->crc_pending = false;

	trace_display_mode(dc, &dc->mode);

	if (dc->mode.pclk) {
		if (!dc->initialized) {
			if (tegra_dc_program_mode(dc, &dc->mode)) {
				tegra_dc_io_end(dc);
				dev_warn(&dc->ndev->dev,
					"%s: tegra_dc_program_mode failed\n",
					__func__);
				return -EINVAL;
			}
		} else {
			dev_info(&dc->ndev->dev, "DC initialized, "
					"skipping tegra_dc_program_mode.\n");
		}
	}

	/* Initialize SD AFTER the modeset.
	   nvsd_init handles the sd_settings = NULL case. */
	nvsd_init(dc, dc->out->sd_settings);

	tegra_dc_io_end(dc);

	return 0;
}

static bool _tegra_dc_controller_enable(struct tegra_dc *dc)
{
	int failed_init = 0;
	int i;

#if !defined(CONFIG_ARCH_TEGRA_21x_SOC) && !defined(CONFIG_ARCH_TEGRA_18x_SOC)
	struct device_node *np_dpaux;
#endif

	if (WARN_ON(!dc || !dc->out || !dc->out_ops))
		return false;

	tegra_dc_unpowergate_locked(dc);

	if (dc->out->enable)
		dc->out->enable(&dc->ndev->dev);

	tegra_dc_setup_clk(dc, dc->clk);

	/* dc clk always on for continuous mode */
	if (!(dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE))
		tegra_dc_clk_enable(dc);
	else
#ifdef CONFIG_TEGRA_CORE_DVFS
		tegra_dvfs_set_rate(dc->clk, dc->mode.pclk);
#else
		;
#endif

	tegra_dc_get(dc);

	tegra_dc_power_on(dc);

	/* do not accept interrupts during initialization */
	tegra_dc_writel(dc, 0, DC_CMD_INT_MASK);

	enable_dc_irq(dc);

	failed_init = tegra_dc_init(dc);
	if (failed_init) {
		tegra_dc_writel(dc, 0, DC_CMD_INT_MASK);
		disable_irq_nosync(dc->irq);
		tegra_dc_clear_bandwidth(dc);
		if (dc->out && dc->out->disable)
			dc->out->disable(&dc->ndev->dev);
		tegra_dc_put(dc);
		if (!(dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE))
			tegra_dc_clk_disable(dc);
		else
#ifdef CONFIG_TEGRA_CORE_DVFS
			tegra_dvfs_set_rate(dc->clk, 0);
#else
			;
#endif
		dev_warn(&dc->ndev->dev,
			"%s: tegra_dc_init failed\n", __func__);
		return false;
	}

#if !defined(CONFIG_ARCH_TEGRA_21x_SOC) && !defined(CONFIG_ARCH_TEGRA_18x_SOC)
	if (dc->out->type != TEGRA_DC_OUT_DP) {
		int sor_num = tegra_dc_which_sor(dc);
		np_dpaux = of_find_node_by_path(
				sor_num ? DPAUX1_NODE : DPAUX_NODE);
		if (np_dpaux || !dc->ndev->dev.of_node)
			tegra_dpaux_pad_power(dc,
			sor_num ? TEGRA_DPAUX_INSTANCE_1 :
			TEGRA_DPAUX_INSTANCE_0, false);
		of_node_put(np_dpaux);
	}
#endif

	if (dc->out_ops && dc->out_ops->enable)
		dc->out_ops->enable(dc);

	/* force a full blending update */
	for (i = 0; i < DC_N_WINDOWS; i++)
		dc->blend.z[i] = -1;

#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	tegra_dc_ext_enable(dc->ext);
#endif

	/* initialize cursor to defaults, as driver depends on HW state */
	tegra_dc_writel(dc, 0, DC_DISP_CURSOR_START_ADDR);
	tegra_dc_writel(dc, 0, DC_DISP_CURSOR_START_ADDR_NS);
#if defined(CONFIG_ARCH_TEGRA_12x_SOC) || defined(CONFIG_ARCH_TEGRA_21x_SOC)
	tegra_dc_writel(dc, 0, DC_DISP_CURSOR_START_ADDR_HI);
	tegra_dc_writel(dc, 0, DC_DISP_CURSOR_START_ADDR_HI_NS);
#endif
	tegra_dc_writel(dc, 0, DC_DISP_CURSOR_POSITION);
	tegra_dc_writel(dc, 0, DC_DISP_CURSOR_POSITION_NS);
	tegra_dc_writel(dc, 0xffffff, DC_DISP_CURSOR_FOREGROUND); /* white */
	tegra_dc_writel(dc, 0x000000, DC_DISP_CURSOR_BACKGROUND); /* black */
	tegra_dc_writel(dc, 0, DC_DISP_BLEND_CURSOR_CONTROL);

	trace_display_enable(dc);

#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
!defined(CONFIG_ARCH_TEGRA_11x_SOC) && \
!defined(CONFIG_ARCH_TEGRA_14x_SOC)
	tegra_dc_writel(dc, CURSOR_UPDATE, DC_CMD_STATE_CONTROL);
	tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
#endif
	tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);

	tegra_dc_dsc_init(dc);

	if (dc->out->postpoweron)
		dc->out->postpoweron(&dc->ndev->dev);

	if (dc->out_ops && dc->out_ops->postpoweron)
		dc->out_ops->postpoweron(dc);

	tegra_log_resume_time();

	tegra_dc_put(dc);

	tegra_disp_clk_prepare_enable(dc->emc_la_clk);

	return true;
}
#endif

#ifdef CONFIG_ARCH_TEGRA_2x_SOC
static bool _tegra_dc_controller_reset_enable(struct tegra_dc *dc)
{
	bool ret = true;

	if (WARN_ON(!dc || !dc->out || !dc->out_ops))
		return false;

	if (dc->out->enable)
		dc->out->enable(&dc->ndev->dev);

	tegra_dc_setup_clk(dc, dc->clk);
	tegra_dc_clk_enable(dc);

	if (dc->ndev->id == 0 && tegra_dcs[1] != NULL) {
		mutex_lock(&tegra_dcs[1]->lock);
		disable_irq_nosync(tegra_dcs[1]->irq);
	} else if (dc->ndev->id == 1 && tegra_dcs[0] != NULL) {
		mutex_lock(&tegra_dcs[0]->lock);
		disable_irq_nosync(tegra_dcs[0]->irq);
	}

	msleep(5);
	tegra_periph_reset_assert(dc->clk);
	msleep(2);
	if (tegra_platform_is_silicon()) {
		tegra_periph_reset_deassert(dc->clk);
		msleep(1);
	}

	if (dc->ndev->id == 0 && tegra_dcs[1] != NULL) {
		enable_dc_irq(tegra_dcs[1]);
		mutex_unlock(&tegra_dcs[1]->lock);
	} else if (dc->ndev->id == 1 && tegra_dcs[0] != NULL) {
		enable_dc_irq(tegra_dcs[0]);
		mutex_unlock(&tegra_dcs[0]->lock);
	}

	enable_dc_irq(dc);

	if (tegra_dc_init(dc)) {
		dev_err(&dc->ndev->dev, "cannot initialize\n");
		ret = false;
	}

	if (dc->out_ops && dc->out_ops->enable)
		dc->out_ops->enable(dc);

	if (dc->out->postpoweron)
		dc->out->postpoweron(&dc->ndev->dev);

	/* force a full blending update */
	dc->blend.z[0] = -1;

#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	tegra_dc_ext_enable(dc->ext);
#endif

	if (!ret) {
		dev_err(&dc->ndev->dev, "initialization failed,disabling");
		_tegra_dc_controller_disable(dc);
	}

	trace_display_reset(dc);
	return ret;
}
#endif

static int _tegra_dc_set_default_videomode(struct tegra_dc *dc)
{
	if (dc->mode.pclk == 0) {
		switch (dc->out->type) {
		case TEGRA_DC_OUT_HDMI:
			/* No fallback mode. If no mode info available
			 * from bootloader or device tree,
			 * mode will be set by userspace during unblank.
			 */
			break;
		case TEGRA_DC_OUT_DP:
#ifdef CONFIG_TEGRA_NVDISPLAY
			break;
#endif
		case TEGRA_DC_OUT_NVSR_DP:
		case TEGRA_DC_OUT_FAKE_DP:
		case TEGRA_DC_OUT_NULL:
			return tegra_dc_set_fb_mode(dc, &tegra_dc_vga_mode, 0);

		/* Do nothing for other outputs for now */
		case TEGRA_DC_OUT_RGB:

		case TEGRA_DC_OUT_DSI:

		default:
			return false;
		}
	}

	return false;
}

int tegra_dc_set_default_videomode(struct tegra_dc *dc)
{
	return _tegra_dc_set_default_videomode(dc);
}

static bool _tegra_dc_enable(struct tegra_dc *dc)
{
	if (dc->mode.pclk == 0)
		return false;

	if (!dc->out)
		return false;

	if (dc->enabled)
		return true;

	dc->shutdown = false;

	if ((dc->out->type == TEGRA_DC_OUT_HDMI ||
		dc->out->type == TEGRA_DC_OUT_DP) &&
		!tegra_dc_hpd(dc))
		return false;

	pm_runtime_get_sync(&dc->ndev->dev);

#ifdef CONFIG_TEGRA_NVDISPLAY
	if (tegra_nvdisp_head_enable(dc)) {
#else
	if (!_tegra_dc_controller_enable(dc)) {
#endif
		pm_runtime_put_sync(&dc->ndev->dev);
		return false;
	}

	return true;
}

void tegra_dc_enable(struct tegra_dc *dc)
{
	if (WARN_ON(!dc || !dc->out || !dc->out_ops))
		return;

	mutex_lock(&dc->lock);

	if (!dc->enabled)
		dc->enabled = _tegra_dc_enable(dc);

	mutex_unlock(&dc->lock);
	trace_display_mode(dc, &dc->mode);
}

static void tegra_dc_flush_syncpts_window(struct tegra_dc *dc, unsigned win)
{
	struct tegra_dc_win *w = tegra_dc_get_window(dc, win);
	u32 max;

	/* refuse to operate on invalid syncpts */
	if (WARN_ON(w->syncpt.id == NVSYNCPT_INVALID))
		return;

	/* flush any pending syncpt waits */
	max = tegra_dc_incr_syncpt_max_locked(dc, win);
	while (w->syncpt.min < w->syncpt.max) {
		trace_display_syncpt_flush(dc, w->syncpt.id,
			w->syncpt.min, w->syncpt.max);
		w->syncpt.min++;
		nvhost_syncpt_cpu_incr_ext(dc->ndev, w->syncpt.id);
	}
}

void tegra_dc_disable_window(struct tegra_dc *dc, unsigned win)
{
	struct tegra_dc_win *w = tegra_dc_get_window(dc, win);

	/* reset window bandwidth */
	w->bandwidth = 0;
	w->new_bandwidth = 0;

	/* disable windows */
	w->flags &= ~TEGRA_WIN_FLAG_ENABLED;

	/* flush pending syncpts */
	tegra_dc_flush_syncpts_window(dc, win);
}

static void _tegra_dc_controller_disable(struct tegra_dc *dc)
{
	unsigned i;

	tegra_dc_get(dc);

	if (atomic_read(&dc->holding)) {
		/* Force release all refs but the last one */
		atomic_set(&dc->holding, 1);
		tegra_dc_release_dc_out(dc);
	}

	if (dc->out && dc->out->prepoweroff)
		dc->out->prepoweroff();

	if (dc->out_ops && dc->out_ops->vrr_enable &&
		dc->out->vrr && dc->out->vrr->capability) {
		dc->out_ops->vrr_enable(dc, 0);
		/* TODO: Fix properly. Bug 1644102. */
		tegra_dc_set_act_vfp(dc, dc->mode.v_front_porch);
	}

	if (dc->out_ops && dc->out_ops->disable)
		dc->out_ops->disable(dc);

	if (tegra_powergate_is_powered(dc->powergate_id))
		tegra_dc_writel(dc, 0, DC_CMD_INT_MASK);

	disable_irq_nosync(dc->irq);

	tegra_dc_clear_bandwidth(dc);

	if (dc->out && dc->out->disable)
		dc->out->disable(&dc->ndev->dev);

	for_each_set_bit(i, &dc->valid_windows, DC_N_WINDOWS) {
		tegra_dc_disable_window(dc, i);
	}
	trace_display_disable(dc);

	if (dc->out_ops && dc->out_ops->postpoweroff)
		dc->out_ops->postpoweroff(dc);

#ifdef CONFIG_TEGRA_NVDISPLAY
	/* clear the windows ownership from head*/
	tegra_nvdisp_head_disable(dc);
#endif

	/* clean up tegra_dc_vsync_enable() */
	while (dc->out->user_needs_vblank > 0)
		_tegra_dc_user_vsync_enable(dc, false);

	if (test_bit(V_BLANK_USER, &dc->vblank_ref_count)) {
		tegra_dc_release_dc_out(dc);
		clear_bit(V_BLANK_USER, &dc->vblank_ref_count);
	}

	tegra_dc_put(dc);

#ifndef CONFIG_TEGRA_NVDISPLAY
	/* disable always on dc clk in continuous mode */
	if (!(dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE))
		tegra_dc_clk_disable(dc);
	else
#ifdef CONFIG_TEGRA_CORE_DVFS
		tegra_dvfs_set_rate(dc->clk, 0);
#else
		;
#endif

	tegra_disp_clk_disable_unprepare(dc->emc_la_clk);
#endif
}

void tegra_dc_stats_enable(struct tegra_dc *dc, bool enable)
{
#if 0 /* underflow interrupt is already enabled by dc reset worker */
	u32 val;
	if (dc->enabled)  {
		val = tegra_dc_readl(dc, DC_CMD_INT_ENABLE);
		if (enable)
			val |= (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT);
		else
			val &= ~(WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT);
		tegra_dc_writel(dc, val, DC_CMD_INT_ENABLE);
	}
#endif
}

bool tegra_dc_stats_get(struct tegra_dc *dc)
{
#if 0 /* right now it is always enabled */
	u32 val;
	bool res;

	if (dc->enabled)  {
		val = tegra_dc_readl(dc, DC_CMD_INT_ENABLE);
		res = !!(val & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT));
	} else {
		res = false;
	}

	return res;
#endif
	return true;
}

/* blank selected windows by disabling them */
void tegra_dc_blank(struct tegra_dc *dc, unsigned windows)
{
	struct tegra_dc_win *dcwins[DC_N_WINDOWS];
	struct tegra_dc_win blank_win;
	unsigned i;
	unsigned long int blank_windows;
	int nr_win = 0;

	/* YUV420 10bpc variables */
	int yuv_flag = dc->mode.vmode & FB_VMODE_YUV_MASK;
	bool yuv_420_10b_path = false;
	int fb_win_idx = -1;
	int fb_win_pos = -1;

	if (dc->yuv_bypass && yuv_flag == (FB_VMODE_Y420 | FB_VMODE_Y30))
		yuv_420_10b_path = true;

#ifdef CONFIG_TEGRA_NVDISPLAY
	if (dc->shutdown)
		yuv_420_10b_path = false;
#endif

	if (yuv_420_10b_path) {
		u32 active_width = dc->mode.h_active;
		u32 active_height = dc->mode.v_active;

		blank_win = *tegra_fb_get_blank_win(dc->fb);

		/*
		 * 420 10bpc blank frame statically
		 * created for this pixel format
		 */
		blank_win.h.full = dfixed_const(1);
		blank_win.w.full = dfixed_const(active_width);
		blank_win.fmt = TEGRA_WIN_FMT_B8G8R8A8;
		blank_win.out_w = active_width;
		blank_win.out_h = active_height;

		dcwins[0] = &blank_win;
		fb_win_idx = dcwins[0]->idx;
		nr_win++;
	}

	blank_windows = windows & dc->valid_windows;

	if (!blank_windows)
		return;

	for_each_set_bit(i, &blank_windows, DC_N_WINDOWS) {
		dcwins[nr_win] = tegra_dc_get_window(dc, i);
		if (!dcwins[nr_win])
			continue;
		/*
		 * Prevent disabling the YUV410 10bpc window in case
		 * it is also in blank_windows, additionally, prevent
		 * adding it to the list twice.
		 */
		if (fb_win_idx == dcwins[nr_win]->idx) {
			fb_win_pos = i;
			continue;
		}
		dcwins[nr_win++]->flags &= ~TEGRA_WIN_FLAG_ENABLED;
	}

#ifdef CONFIG_TEGRA_NVDISPLAY
	if (dc->shutdown) {
		if ((dc->out->type == TEGRA_DC_OUT_HDMI) ||
			(dc->out->type == TEGRA_DC_OUT_DP))
			if (dc->out_ops && dc->out_ops->shutdown_interface)
				dc->out_ops->shutdown_interface(dc);
	}
#endif

	/* Skip update for linsim */
	if (!tegra_platform_is_linsim() && !tegra_platform_is_vdk()) {
		tegra_dc_update_windows(dcwins, nr_win, NULL, true);
		tegra_dc_sync_windows(dcwins, nr_win);
	}

	tegra_dc_program_bandwidth(dc, true);

	/*
	 * Disable, reset bandwidth and advance pending syncpoints
	 * of all windows. In case the statically created 420 10bpc
	 * is also present in blank_windows, only advance syncpoints.
	 */
	for_each_set_bit(i, &blank_windows, DC_N_WINDOWS) {
		if (fb_win_pos == i) {
			tegra_dc_flush_syncpts_window(dc, i);
			continue;
		}
		tegra_dc_disable_window(dc, i);
	}
}

int tegra_dc_restore(struct tegra_dc *dc)
{
#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	return tegra_dc_ext_restore(dc->ext);
#else
	return 0;
#endif
}

static void _tegra_dc_disable(struct tegra_dc *dc)
{
#ifdef CONFIG_TEGRA_DC_CMU
	/* power down resets the registers, setting to true
	 * causes CMU to be restored in tegra_dc_init(). */
	dc->cmu_dirty = true;
#endif
	tegra_dc_get(dc);
	_tegra_dc_controller_disable(dc);
	tegra_dc_put(dc);

	tegra_dc_powergate_locked(dc);

	pm_runtime_put(&dc->ndev->dev);

	tegra_log_suspend_entry_time();
}

void tegra_dc_disable(struct tegra_dc *dc)
{
	dc->shutdown = true;
	tegra_dc_disable_irq_ops(dc, false);
}

static void tegra_dc_disable_irq_ops(struct tegra_dc *dc, bool from_irq)
{
	if (WARN_ON(!dc || !dc->out || !dc->out_ops))
		return;

#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	if (!tegra_dc_ext_disable(dc->ext))
		tegra_dc_blank(dc, BLANK_ALL);
#else
	tegra_dc_blank(dc, BLANK_ALL);
#endif

	if (dc->cursor.enabled)
		tegra_dc_cursor_suspend(dc);

	/* it's important that new underflow work isn't scheduled before the
	 * lock is acquired. */
	cancel_delayed_work_sync(&dc->underflow_work);


	if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE) {
		mutex_lock(&dc->one_shot_lock);
		cancel_delayed_work_sync(&dc->one_shot_work);
	}

	mutex_lock(&dc->lp_lock);
	mutex_lock(&dc->lock);

	if (dc->enabled) {
		dc->enabled = false;
		dc->blanked = false;

		if (!dc->suspended)
			_tegra_dc_disable(dc);
	}

#ifdef CONFIG_SWITCH
	if (dc->switchdev_registered)
		switch_set_state(&dc->modeset_switch, 0);
#endif
	mutex_unlock(&dc->lock);
	mutex_unlock(&dc->lp_lock);
	if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
		mutex_unlock(&dc->one_shot_lock);
	if (!from_irq)
		synchronize_irq(dc->irq);
	trace_display_mode(dc, &dc->mode);

	/* disable pending clks due to uncompleted frames */
	while (tegra_platform_is_silicon() && atomic_read(&dc->enable_count))
		tegra_dc_put(dc);
}

#ifdef CONFIG_ARCH_TEGRA_2x_SOC
static void tegra_dc_reset_worker(struct work_struct *work)
{
	struct tegra_dc *dc =
		container_of(work, struct tegra_dc, reset_work);

	unsigned long val = 0;

	mutex_lock(&shared_lock);

	dev_warn(&dc->ndev->dev,
		"overlay stuck in underflow state.  resetting.\n");

#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	tegra_dc_ext_disable(dc->ext);
#endif

	mutex_lock(&dc->lock);

	if (dc->enabled == false)
		goto unlock;

	dc->enabled = false;

	/*
	 * off host read bus
	 */
	val = tegra_dc_readl(dc, DC_CMD_CONT_SYNCPT_VSYNC);
	val &= ~(0x00000100);
	tegra_dc_writel(dc, val, DC_CMD_CONT_SYNCPT_VSYNC);

	/*
	 * set DC to STOP mode
	 */
	tegra_dc_writel(dc, DISP_CTRL_MODE_STOP, DC_CMD_DISPLAY_COMMAND);

	msleep(10);

	_tegra_dc_controller_disable(dc);

	/* _tegra_dc_controller_reset_enable deasserts reset */
	_tegra_dc_controller_reset_enable(dc);

	dc->enabled = true;

	/* reopen host read bus */
	val = tegra_dc_readl(dc, DC_CMD_CONT_SYNCPT_VSYNC);
	val &= ~(0x00000100);
	val |= 0x100;
	tegra_dc_writel(dc, val, DC_CMD_CONT_SYNCPT_VSYNC);

unlock:
	mutex_unlock(&dc->lock);
	mutex_unlock(&shared_lock);
	trace_display_reset(dc);
}
#endif

static void tegra_dc_underflow_worker(struct work_struct *work)
{
	struct tegra_dc *dc = container_of(
		to_delayed_work(work), struct tegra_dc, underflow_work);

	mutex_lock(&dc->lock);
	tegra_dc_get(dc);

	if (dc->enabled)
		tegra_dc_underflow_handler(dc);

	tegra_dc_put(dc);
	mutex_unlock(&dc->lock);
}

static void (*flip_callback)(void);
static spinlock_t flip_callback_lock;
static bool init_tegra_dc_flip_callback_called;

static int __init init_tegra_dc_flip_callback(void)
{
	spin_lock_init(&flip_callback_lock);
	init_tegra_dc_flip_callback_called = true;
	return 0;
}

pure_initcall(init_tegra_dc_flip_callback);

int tegra_dc_set_flip_callback(void (*callback)(void))
{
	WARN_ON(!init_tegra_dc_flip_callback_called);

	spin_lock(&flip_callback_lock);
	flip_callback = callback;
	spin_unlock(&flip_callback_lock);

	return 0;
}
EXPORT_SYMBOL(tegra_dc_set_flip_callback);

int tegra_dc_unset_flip_callback(void)
{
	spin_lock(&flip_callback_lock);
	flip_callback = NULL;
	spin_unlock(&flip_callback_lock);

	return 0;
}
EXPORT_SYMBOL(tegra_dc_unset_flip_callback);

void tegra_dc_call_flip_callback(void)
{
	spin_lock(&flip_callback_lock);
	if (flip_callback)
		flip_callback();
	spin_unlock(&flip_callback_lock);
}
EXPORT_SYMBOL(tegra_dc_call_flip_callback);

#ifdef CONFIG_SWITCH
static ssize_t switch_modeset_print_mode(struct switch_dev *sdev, char *buf)
{
	struct tegra_dc *dc =
		container_of(sdev, struct tegra_dc, modeset_switch);

	if (!sdev->state)
		return sprintf(buf, "offline\n");

	return sprintf(buf, "%dx%d\n", dc->mode.h_active, dc->mode.v_active);
}
#endif

/* enables pads and clocks to perform DDC/I2C */
int tegra_dc_ddc_enable(struct tegra_dc *dc, bool enabled)
{
	int ret = -ENOSYS;
	if (dc->out_ops) {
		if (enabled && dc->out_ops->ddc_enable)
			ret = dc->out_ops->ddc_enable(dc);
		else if (!enabled && dc->out_ops->ddc_disable)
			ret = dc->out_ops->ddc_disable(dc);
	}
	return ret;
}

#if IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)
int tegra_dc_slgc_disp0(struct notifier_block *nb,
	unsigned long unused0, void *unused1)
{
#ifdef CONFIG_ARCH_TEGRA_21x_SOC
	struct tegra_dc *dc = container_of(nb, struct tegra_dc, slgc_notifier);
	u32 val;

	tegra_dc_get(dc);

	val = tegra_dc_readl(dc, DC_COM_DSC_TOP_CTL);
	val |= DSC_SLCG_OVERRIDE;
	tegra_dc_writel(dc, val, DC_COM_DSC_TOP_CTL); /* set */
	/* flush the previous write */
	(void)tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
	val &= ~DSC_SLCG_OVERRIDE;
	tegra_dc_writel(dc, val, DC_COM_DSC_TOP_CTL); /* restore */

	tegra_dc_put(dc);
#endif
	return NOTIFY_OK;
}
#endif

int tegra_dc_update_winmask(struct tegra_dc *dc, unsigned long winmask)
{
	struct tegra_dc *dc_other;
	struct tegra_dc_win *win;
	int i, j, ret = 0;

#ifndef CONFIG_TEGRA_NVDISPLAY
	return -EINVAL;
#endif /* CONFIG_TEGRA_NVDISPLAY */

	/* check that dc is not NULL and do range check */
	if (!dc || (winmask >= (1 << DC_N_WINDOWS)))
		return -EINVAL;

	mutex_lock(&dc->lock);
	if ((!dc->ndev) || (dc->enabled)) {
		ret = -EINVAL;
		goto exit;
	}

	/* check requested=enabled windows NOT owned by other dcs */
	for_each_set_bit(i, &winmask, DC_N_WINDOWS) {
		j = dc->ndev->id;
		win = tegra_dc_get_window(dc, i);
		/* is window already owned by this dc? */
		if (win && win->dc && (win->dc == dc))
			continue;
		/* is window already owned by other dc? */
		for (j = 0; j < TEGRA_MAX_DC; j++) {
			dc_other = tegra_dc_get_dc(j);
			if (!dc_other)
				continue;
			if (!dc_other->pdata) {
				ret = -EINVAL;
				goto exit;
			}
			/* found valid dc, does it own window=i? */
			if ((dc_other->pdata->win_mask >> i) & 0x1) {
				dev_err(&dc->ndev->dev,
					"win[%d] already on fb%d\n", i, j);
				ret = -EINVAL;
				goto exit;
			}
		}
	}

	/* attach window happens on device enable call and
	 * detach window happens on device disable call
	 */

	dc->pdata->win_mask = winmask;
	dc->valid_windows = winmask;
	/* cleanup the valid window bits */
	if (!winmask) {
		/* disable the fb win_index */
		tegra_fb_set_win_index(dc, winmask);
		dc->pdata->fb->win = -1;
	}

exit:
	mutex_unlock(&dc->lock);
	return ret;
}

struct clk *tegra_disp_of_clk_get_by_name(struct device_node *np,
						const char *name)
{
#ifdef CONFIG_TEGRA_NVDISPLAY
	if (!tegra_bpmp_running())
		return of_clk_get_by_name(np, "clk32k_in");
#endif
	return of_clk_get_by_name(np, name);
}

struct clk *tegra_disp_clk_get(struct device *dev, const char *id)
{
#ifdef CONFIG_TEGRA_NVDISPLAY
	struct clk *disp_clk;

	if (!tegra_bpmp_running()) {
		return of_clk_get_by_name(dev->of_node, "clk32k_in");
	} else {
		disp_clk = devm_clk_get(dev, id);
		if (IS_ERR_OR_NULL(disp_clk))
			pr_err("Failed to get %s clk\n", id);
		return disp_clk;
	}

#elif defined(CONFIG_ARCH_TEGRA_210_SOC)
	return devm_clk_get(dev, id);
#else
	return clk_get(dev, id);
#endif
}

void tegra_disp_clk_put(struct device *dev, struct clk *clk)
{
#ifdef CONFIG_TEGRA_NVDISPLAY
	if (tegra_platform_is_silicon() && tegra_bpmp_running())
		devm_clk_put(dev, clk);
#else
	return clk_put(clk);
#endif
}

static int tegra_dc_probe(struct platform_device *ndev)
{
	struct tegra_dc *dc;
	struct tegra_dc_mode *mode;
	struct tegra_dc_platform_data *dt_pdata = NULL;
	struct clk *clk;
#ifndef CONFIG_TEGRA_ISOMGR
	struct clk *emc_clk;
#elif !defined(CONFIG_TEGRA_NVDISPLAY)
	int isomgr_client_id = -1;
#endif
	struct clk *emc_la_clk;
	struct device_node *np = ndev->dev.of_node;
	struct resource *res;
	struct resource dt_res;
	struct resource *base_res;
	struct resource *fb_mem = NULL;
	char clk_name[16];
	int ret = 0;
	void __iomem *base;
	int irq;
	int i;
#if IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS) && !IS_ENABLED(CONFIG_TEGRA_NVDISPLAY)
	int partition_id_disa, partition_id_disb;
#endif

#ifdef CONFIG_ARCH_TEGRA_21x_SOC
	if (tegra_platform_is_linsim()) {
		dev_info(&ndev->dev, "DC instances are not present on linsim\n");
		return -ENODEV;
	}
#endif

	if (!np && !ndev->dev.platform_data) {
		dev_err(&ndev->dev, "no platform data\n");
		return -ENOENT;
	}

	/* Specify parameters for the maximum physical segment size. */
	ndev->dev.dma_parms = &tegra_dc_dma_parameters;

	dc = kzalloc(sizeof(struct tegra_dc), GFP_KERNEL);
	if (!dc) {
		dev_err(&ndev->dev, "can't allocate memory for tegra_dc\n");
		return -ENOMEM;
	}

	if (np) {
#ifdef CONFIG_OF
		irq = of_irq_to_resource(np, 0, NULL);
		if (!irq)
			goto err_free;
#endif

		ret = of_address_to_resource(np, 0, &dt_res);
		if (ret)
			goto err_free;

		ndev->id = tegra_dc_set(dc, -1);
		if (ndev->id < 0) {
			dev_err(&ndev->dev, "can't add dc\n");
			goto err_free;
		}

		dev_info(&ndev->dev, "Display dc.%08x registered with id=%d\n",
				(unsigned int)dt_res.start, ndev->id);
		res = &dt_res;

		dt_pdata = of_dc_parse_platform_data(ndev);
		if (IS_ERR_OR_NULL(dt_pdata)) {
			if (dt_pdata)
				ret = PTR_ERR(dt_pdata);
			goto err_free;
		}

#ifdef CONFIG_TEGRA_NVDISPLAY
		dc->ctrl_num = dt_pdata->ctrl_num;
#else
		if (dt_res.start == TEGRA_DISPLAY_BASE)
			dc->ctrl_num = 0;
		else if (dt_res.start == TEGRA_DISPLAY2_BASE)
			dc->ctrl_num = 1;
		else
			goto err_free;
#endif

	} else {

		dc->ctrl_num = ndev->id;

		irq = platform_get_irq_byname(ndev, "irq");
		if (irq <= 0) {
			dev_err(&ndev->dev, "no irq\n");
			ret = -ENOENT;
			goto err_free;
		}

		res = platform_get_resource_byname(ndev,
			IORESOURCE_MEM, "regs");
		if (!res) {
			dev_err(&ndev->dev, "no mem resource\n");
			ret = -ENOENT;
			goto err_free;
		}

		if (tegra_dc_set(dc, ndev->id) < 0) {
			dev_err(&ndev->dev, "can't add dc\n");
			goto err_free;
		}

	}

	base_res = request_mem_region(res->start, resource_size(res),
		ndev->name);
	if (!base_res) {
		dev_err(&ndev->dev, "request_mem_region failed\n");
		ret = -EBUSY;
		goto err_free;
	}

	base = ioremap(res->start, resource_size(res));
	if (!base) {
		dev_err(&ndev->dev, "registers can't be mapped\n");
		ret = -EBUSY;
		goto err_release_resource_reg;
	}

#ifndef CONFIG_TEGRA_NVDISPLAY
	for (i = 0; i < DC_N_WINDOWS; i++)
		dc->windows[i].syncpt.id = NVSYNCPT_INVALID;

	if (TEGRA_DISPLAY_BASE == res->start) {
		dc->vblank_syncpt = NVSYNCPT_VBLANK0;
		dc->windows[0].syncpt.id =
			nvhost_get_syncpt_client_managed(ndev, "disp0_a");
		dc->windows[1].syncpt.id =
			nvhost_get_syncpt_client_managed(ndev, "disp0_b");
		dc->windows[2].syncpt.id =
			nvhost_get_syncpt_client_managed(ndev, "disp0_c");
		dc->valid_windows = 0x07;
#if defined(CONFIG_ARCH_TEGRA_14x_SOC)
		dc->windows[3].syncpt.id =
			nvhost_get_syncpt_client_managed(ndev, "disp0_d");
		dc->windows[4].syncpt.id =
			nvhost_get_syncpt_client_managed(ndev, "disp0_h");
		dc->valid_windows |= 0x18;
#elif !defined(CONFIG_ARCH_TEGRA_2x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_3x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_11x_SOC)
		dc->windows[3].syncpt.id =
			nvhost_get_syncpt_client_managed(ndev, "disp0_d");
		dc->valid_windows |= 0x08;
#endif
#if IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)
		partition_id_disa = tegra_pd_get_powergate_id(tegra_disa_pd);
		if (partition_id_disa < 0)
			return -EINVAL;

		dc->powergate_id = partition_id_disa;
#ifdef CONFIG_TEGRA_ISOMGR
		isomgr_client_id = TEGRA_ISO_CLIENT_DISP_0;
#endif
		dc->slgc_notifier.notifier_call = tegra_dc_slgc_disp0;
		slcg_register_notifier(dc->powergate_id,
			&dc->slgc_notifier);
#endif
	} else if (TEGRA_DISPLAY2_BASE == res->start) {
		dc->vblank_syncpt = NVSYNCPT_VBLANK1;
		dc->windows[0].syncpt.id =
			nvhost_get_syncpt_client_managed(ndev, "disp1_a");
		dc->windows[1].syncpt.id =
			nvhost_get_syncpt_client_managed(ndev, "disp1_b");
		dc->windows[2].syncpt.id =
			nvhost_get_syncpt_client_managed(ndev, "disp1_c");
		dc->valid_windows = 0x07;
#ifdef CONFIG_ARCH_TEGRA_14x_SOC
		dc->windows[4].syncpt.id =
			nvhost_get_syncpt_client_managed(ndev, "disp1_h");
		dc->valid_windows |= 0x10;
#endif
#if IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)
		partition_id_disb = tegra_pd_get_powergate_id(tegra_disb_pd);
		if (partition_id_disb < 0)
			return -EINVAL;

		dc->powergate_id = partition_id_disb;
#endif
#ifdef CONFIG_TEGRA_ISOMGR
		isomgr_client_id = TEGRA_ISO_CLIENT_DISP_1;
#endif
	} else {
		dev_err(&ndev->dev,
			"Unknown base address %llx: unable to assign syncpt\n",
			(u64)res->start);
	}
#endif	/* !CONFIG_TEGRA_NVDISPLAY */

	if (np) {
		struct resource of_fb_res;
#ifdef CONFIG_TEGRA_NVDISPLAY
		tegra_get_fb_resource(&of_fb_res);
#else
		if (dc->ctrl_num == 0)
			tegra_get_fb_resource(&of_fb_res);
		else /* dc->ctrl_num == 1*/
			tegra_get_fb2_resource(&of_fb_res);
#endif

		fb_mem = kzalloc(sizeof(struct resource), GFP_KERNEL);
		if (fb_mem == NULL) {
			ret = -ENOMEM;
			goto err_iounmap_reg;
		}
		fb_mem->name = "fbmem";
		fb_mem->flags = IORESOURCE_MEM;
		fb_mem->start = (resource_size_t)of_fb_res.start;
		fb_mem->end = (resource_size_t)of_fb_res.end;
	} else {
		fb_mem = platform_get_resource_byname(ndev,
			IORESOURCE_MEM, "fbmem");
		if (fb_mem == NULL) {
			ret = -ENOMEM;
			goto err_iounmap_reg;
		}
	}

#ifdef CONFIG_TEGRA_NVDISPLAY
	snprintf(clk_name, sizeof(clk_name), "nvdisplay_p%u", dc->ctrl_num);
#elif !defined(CONFIG_ARCH_TEGRA_21x_SOC)
	memset(clk_name, 0, sizeof(clk_name));
#else
	snprintf(clk_name, sizeof(clk_name), "disp%u", dc->ctrl_num + 1);

#endif
	clk = tegra_disp_clk_get(&ndev->dev, clk_name);
	if (IS_ERR_OR_NULL(clk)) {
		dev_err(&ndev->dev, "can't get clock: %s\n", clk_name);
		ret = -ENOENT;
		goto err_iounmap_reg;
	}

	dc->clk = clk;
	dc->shift_clk_div.mul = dc->shift_clk_div.div = 1;
	/* Initialize one shot work delay, it will be assigned by dsi
	 * according to refresh rate later. */
	dc->one_shot_delay_ms = 40;

	dc->base_res = base_res;
	dc->base = base;
	dc->irq = irq;
	dc->ndev = ndev;
	dc->fb_mem = fb_mem;

	if (!np)
		dc->pdata = ndev->dev.platform_data;
	else
		dc->pdata = dt_pdata;

	dc->bw_kbps = 0;

#ifdef CONFIG_TEGRA_NVDISPLAY
	/* dc variables need to initialized before nvdisp init */
	ret = tegra_nvdisp_init(dc);
	if (ret)
		goto err_iounmap_reg;
#endif

	mutex_init(&dc->lock);
	mutex_init(&dc->one_shot_lock);
	mutex_init(&dc->lp_lock);
	init_completion(&dc->frame_end_complete);
	init_completion(&dc->crc_complete);
	init_waitqueue_head(&dc->wq);
	init_waitqueue_head(&dc->timestamp_wq);
#ifdef CONFIG_ARCH_TEGRA_2x_SOC
	INIT_WORK(&dc->reset_work, tegra_dc_reset_worker);
#endif
	INIT_WORK(&dc->vblank_work, tegra_dc_vblank);
	dc->vblank_ref_count = 0;
	INIT_WORK(&dc->frame_end_work, tegra_dc_frame_end);
#if !defined(CONFIG_ARCH_TEGRA_2x_SOC) && !defined(CONFIG_ARCH_TEGRA_3x_SOC)
	INIT_WORK(&dc->vpulse2_work, tegra_dc_vpulse2);
#endif
	dc->vpulse2_ref_count = 0;
	INIT_DELAYED_WORK(&dc->underflow_work, tegra_dc_underflow_worker);
	INIT_DELAYED_WORK(&dc->one_shot_work, tegra_dc_one_shot_worker);
#ifdef CONFIG_TEGRA_NVDISPLAY
	INIT_DELAYED_WORK(&dc->vrr_work, tegra_nvdisp_vrr_work);
#endif
	tegra_dc_init_lut_defaults(&dc->fb_lut);

	dc->n_windows = DC_N_WINDOWS;
	for (i = 0; i < DC_N_WINDOWS; i++) {
		struct tegra_dc_win *tmp_win = &dc->tmp_wins[i];
#ifdef CONFIG_TEGRA_NVDISPLAY
		struct tegra_dc_win *win = &tegra_dc_windows[i];
#else
		struct tegra_dc_win *win = &dc->windows[i];
		win->dc = dc;
#endif
		if (!test_bit(i, &dc->valid_windows))
			win->flags |= TEGRA_WIN_FLAG_INVALID;
		else {
		win->idx = i;
		tmp_win->idx = i;
		tmp_win->dc = dc;
#if defined(CONFIG_TEGRA_CSC)
		tegra_dc_init_csc_defaults(&win->csc);
#endif
		tegra_dc_init_lut_defaults(&win->lut);
		}
	}

#if defined(CONFIG_TEGRA_CSC_V2)
	if (dc->pdata->cmu)
		dc->default_csc = dc->pdata->cmu->panel_csc;
	else
		tegra_nvdisp_init_csc_defaults(&dc->default_csc);
#endif

	platform_set_drvdata(ndev, dc);

#ifdef CONFIG_SWITCH
	dc->modeset_switch.name = dev_name(&ndev->dev);
	dc->modeset_switch.state = 0;
	dc->modeset_switch.print_state = switch_modeset_print_mode;
	ret = switch_dev_register(&dc->modeset_switch);
	if (ret < 0) {
		dev_err(&ndev->dev,
			"failed to register switch driver ret(%d)\n", ret);
		dc->switchdev_registered = false;
	} else
		dc->switchdev_registered = true;
#endif

	tegra_dc_feature_register(dc);

	if (dc->pdata->default_out) {
		if (dc->pdata->default_out->hotplug_init)
			dc->pdata->default_out->hotplug_init(&dc->ndev->dev);
		ret = tegra_dc_set_out(dc, dc->pdata->default_out);
		if (ret < 0) {
			dev_err(&dc->ndev->dev, "failed to initialize DC out ops\n");
			goto err_put_clk;
		}
	} else {
		dev_err(&ndev->dev,
			"No default output specified.  Leaving output disabled.\n");
	}
	dc->mode_dirty = false; /* ignore changes tegra_dc_set_out has done */
#ifdef CONFIG_TEGRA_NVDISPLAY
	nvdisp_register_backlight_notifier(dc);
#endif

	/* For HDMI|DP, hotplug always supported
	 * For eDP, hotplug is never supported
	 * Else GPIO# determines if hotplug supported
	 */
	if (dc->out->type == TEGRA_DC_OUT_HDMI)
		dc->hotplug_supported = true;
	else if (dc->out->type == TEGRA_DC_OUT_DP)
		dc->hotplug_supported = tegra_dc_is_ext_dp_panel(dc);
	else
		dc->hotplug_supported = dc->out->hotplug_gpio >= 0;

	if ((dc->pdata->flags & TEGRA_DC_FLAG_ENABLED) &&
			dc->out && dc->out->type == TEGRA_DC_OUT_LVDS) {
		struct fb_monspecs specs;
		struct tegra_dc_lvds_data *lvds = tegra_dc_get_outdata(dc);
		if (!tegra_edid_get_monspecs(lvds->edid, &specs))
			tegra_dc_set_fb_mode(dc, specs.modedb, false);
	}

#ifndef CONFIG_TEGRA_ISOMGR
		/*
		 * The emc is a shared clock, it will be set based on
		 * the requirements for each user on the bus.
		 */
		emc_clk = tegra_disp_clk_get(&ndev->dev, "emc");
		if (IS_ERR_OR_NULL(emc_clk)) {
#ifdef CONFIG_ARCH_TEGRA_21x_SOC
			dev_info(&ndev->dev, "can't get emc clock\n");
			emc_clk = NULL;
#else
			dev_err(&ndev->dev, "can't get emc clock\n");
			ret = -ENOENT;
			goto err_put_clk;
#endif
		}
		dc->emc_clk = emc_clk;
#endif
		/*
		 * The emc_la clock is being added to set the floor value
		 * for emc depending on the LA calculaions for each window
		 */
#ifdef CONFIG_TEGRA_NVDISPLAY
		emc_la_clk = tegra_disp_clk_get(&ndev->dev, "emc_latency");
#else
		emc_la_clk = tegra_disp_clk_get(&ndev->dev, "emc.la");
#endif
		if (IS_ERR_OR_NULL(emc_la_clk)) {
#ifdef CONFIG_ARCH_TEGRA_21x_SOC
			dev_info(&ndev->dev, "can't get emc.la clock\n");
			emc_la_clk = NULL;
#else
			dev_err(&ndev->dev, "can't get emc.la clock\n");
			ret = -ENOENT;
			goto err_put_clk;
#endif
		}
		dc->emc_la_clk = emc_la_clk;
		clk_set_rate(dc->emc_la_clk, 0);

#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	dc->ext = tegra_dc_ext_register(ndev, dc);
	if (IS_ERR_OR_NULL(dc->ext)) {
		dev_warn(&ndev->dev, "Failed to enable Tegra DC extensions.\n");
		dc->ext = NULL;
	}
#endif

	/* interrupt handler must be registered before tegra_fb_register() */
	if (request_threaded_irq(irq, NULL, tegra_dc_irq, IRQF_ONESHOT,
			dev_name(&ndev->dev), dc)) {
		dev_err(&ndev->dev, "request_irq %d failed\n", irq);
		ret = -EBUSY;
		goto err_disable_dc;
	}
	disable_dc_irq(dc);

	tegra_pd_add_device(&ndev->dev);
	pm_runtime_use_autosuspend(&ndev->dev);
	pm_runtime_set_autosuspend_delay(&ndev->dev, 100);
	pm_runtime_enable(&ndev->dev);

#if defined(CONFIG_TEGRA_DC_CMU) || defined(CONFIG_TEGRA_DC_CMU_V2)
	/* if bootloader leaves this head enabled, then skip CMU programming. */
	dc->is_cmu_set_bl = (dc->pdata->flags & TEGRA_DC_FLAG_ENABLED) != 0;
	dc->cmu_enabled = dc->pdata->cmu_enable;
#endif

#if !defined(CONFIG_TEGRA_NVDISPLAY) && defined(CONFIG_TEGRA_ISOMGR)
	if (isomgr_client_id == -1) {
		dc->isomgr_handle = NULL;
	} else {
		dc->isomgr_handle = tegra_isomgr_register(isomgr_client_id,
			tegra_dc_calc_min_bandwidth(dc),
			tegra_dc_bandwidth_renegotiate, dc);
		if (IS_ERR(dc->isomgr_handle)) {
			dev_err(&dc->ndev->dev,
				"could not register isomgr. err=%ld\n",
				PTR_ERR(dc->isomgr_handle));
			ret = -ENOENT;
			goto err_put_clk;
		}
		dc->reserved_bw = tegra_dc_calc_min_bandwidth(dc);
		/*
		 * Use maximum value so we can try to reserve as much as
		 * needed until we are told by isomgr to backoff.
		 */
		dc->available_bw = UINT_MAX;
	}
#endif

	tegra_dc_create_debugfs(dc);

	dev_info(&ndev->dev, "probed\n");

	if (dc->pdata->fb) {
		if (dc->enabled && dc->pdata->fb->bits_per_pixel == -1) {
			unsigned long fmt;
			tegra_dc_writel(dc,
					WINDOW_A_SELECT << dc->pdata->fb->win,
					DC_CMD_DISPLAY_WINDOW_HEADER);

			fmt = tegra_dc_readl(dc, DC_WIN_COLOR_DEPTH);
			dc->pdata->fb->bits_per_pixel =
				tegra_dc_fmt_bpp(fmt);
		}

		mode = tegra_dc_get_override_mode(dc);
		if (mode) {
			dc->pdata->fb->xres = mode->h_active;
			dc->pdata->fb->yres = mode->v_active;
		}

#ifdef CONFIG_ADF_TEGRA
		tegra_dc_io_start(dc);
		dc->adf = tegra_adf_init(ndev, dc, dc->pdata->fb, fb_mem);
		tegra_dc_io_end(dc);

		if (IS_ERR(dc->adf)) {
			tegra_dc_io_start(dc);
			dc->fb = tegra_fb_register(ndev, dc, dc->pdata->fb,
				fb_mem);
			tegra_dc_io_end(dc);
			if (IS_ERR_OR_NULL(dc->fb)) {
				dc->fb = NULL;
				dev_err(&ndev->dev, "failed to register fb\n");
				goto err_remove_debugfs;
			}
		}
#endif
#ifdef CONFIG_TEGRA_DC_EXTENSIONS
		tegra_dc_get(dc);
#ifdef CONFIG_TEGRA_NVDISPLAY
		dc->fb = tegra_nvdisp_fb_register(ndev, dc, dc->pdata->fb,
			fb_mem);
#else
		dc->fb = tegra_fb_register(ndev, dc, dc->pdata->fb, fb_mem,
			NULL);
#endif
		tegra_dc_put(dc);
		if (IS_ERR_OR_NULL(dc->fb)) {
			dc->fb = NULL;
			dev_err(&ndev->dev, "failed to register fb\n");
			goto err_remove_debugfs;
		}
#endif
	}

	if (dc->pdata->flags & TEGRA_DC_FLAG_ENABLED) {
		/* WAR: BL is putting DC in bad state for EDP configuration */
		if (!(tegra_platform_is_linsim() || tegra_platform_is_vdk()) &&
			(dc->out->type == TEGRA_DC_OUT_DP ||
				dc->out->type == TEGRA_DC_OUT_NVSR_DP)) {
			tegra_disp_clk_prepare_enable(dc->clk);
			tegra_periph_reset_assert(dc->clk);
			udelay(10);
			tegra_periph_reset_deassert(dc->clk);
			udelay(10);
			tegra_disp_clk_disable_unprepare(dc->clk);
		}

		if (dc->out_ops && dc->out_ops->hotplug_init)
			dc->out_ops->hotplug_init(dc);

		_tegra_dc_set_default_videomode(dc);
		dc->enabled = _tegra_dc_enable(dc);

#if !defined(CONFIG_ARCH_TEGRA_11x_SOC) && \
	!defined(CONFIG_ARCH_TEGRA_14x_SOC) && \
	!defined(CONFIG_TEGRA_NVDISPLAY) && \
	IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)
		/* BL or PG init will keep DISA unpowergated after booting.
		 * Adding an extra powergate to balance the refcount
		 * since _tegra_dc_enable() increases the refcount.
		 */
		if (!tegra_platform_is_fpga())
			if (dc->powergate_id == TEGRA_POWERGATE_DISA)
				tegra_dc_powergate_locked(dc);
#endif
	}

	if (dc->out_ops) {
		if (dc->out_ops->detect)
			dc->connected = dc->out_ops->detect(dc);
		else
			dc->connected = true;
	} else
		dc->connected = false;

	/* Powergate display module when it's unconnected. */
	/* detect() function, if presetns, responsible for the powergate */
	if (!tegra_dc_get_connected(dc) &&
			!(dc->out_ops && dc->out_ops->detect))
		tegra_dc_powergate_locked(dc);

	tegra_dc_create_sysfs(&dc->ndev->dev);

	/*
	 * Overriding the display mode only applies for modes set up during
	 * boot. It should not apply for e.g. HDMI hotplug.
	 */
	dc->initialized = false;
#ifdef CONFIG_TEGRA_NVDISPLAY
	if (dc->out->sd_settings) {
		if (dc->out->sd_settings->enable) {
			mutex_lock(&dc->lock);
			tegra_dc_unmask_interrupt(dc, SMARTDIM_INT);
			tegra_sd_stop(dc);
			tegra_sd_init(dc);
			mutex_unlock(&dc->lock);
		}
	}
#endif
	/*
	 * Initialize vedid state. This is placed here
	 * to allow persistence across sw HDMI hotplugs.
	 */
	dc->vedid = false;
	dc->vedid_data = NULL;

	return 0;

err_remove_debugfs:
	tegra_dc_remove_debugfs(dc);
	free_irq(irq, dc);
err_disable_dc:
#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	if (dc->ext) {
		tegra_dc_ext_disable(dc->ext);
		tegra_dc_ext_unregister(dc->ext);
	}
#endif
	mutex_lock(&dc->lock);
	if (dc->enabled)
		_tegra_dc_disable(dc);
	dc->enabled = false;
	mutex_unlock(&dc->lock);
#if !defined(CONFIG_TEGRA_NVDISPLAY) && defined(CONFIG_TEGRA_ISOMGR)
	tegra_isomgr_unregister(dc->isomgr_handle);
#elif !defined(CONFIG_TEGRA_ISOMGR)
	tegra_disp_clk_put(&ndev->dev, emc_clk);
#endif
	tegra_disp_clk_put(&ndev->dev, dc->emc_la_clk);
err_put_clk:
#ifdef CONFIG_SWITCH
	if (dc->switchdev_registered)
		switch_dev_unregister(&dc->modeset_switch);
#endif
	tegra_disp_clk_put(&ndev->dev, clk);
err_iounmap_reg:
	iounmap(base);
	if (fb_mem) {
		if (!np)
			release_resource(fb_mem);
		else
			kfree(fb_mem);
	}
err_release_resource_reg:
	release_resource(base_res);
err_free:
	kfree(dc);
	tegra_dc_set(NULL, ndev->id);

	return ret;
}

static int tegra_dc_remove(struct platform_device *ndev)
{
	struct tegra_dc *dc = platform_get_drvdata(ndev);
	struct device_node *np = ndev->dev.of_node;

	tegra_dc_remove_sysfs(&dc->ndev->dev);
	tegra_dc_remove_debugfs(dc);

	if (dc->fb) {
		tegra_fb_unregister(dc->fb);
		if (dc->fb_mem) {
			if (!np)
				release_resource(dc->fb_mem);
			else
				kfree(dc->fb_mem);
		}
	}

#ifdef CONFIG_ADF_TEGRA
	if (dc->adf)
		tegra_adf_unregister(dc->adf);
#endif
#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	if (dc->ext) {
		tegra_dc_ext_disable(dc->ext);
		tegra_dc_ext_unregister(dc->ext);
	}
#endif

	if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE) {
		mutex_lock(&dc->one_shot_lock);
		cancel_delayed_work_sync(&dc->one_shot_work);
	}
	mutex_lock(&dc->lock);
	if (dc->enabled)
		_tegra_dc_disable(dc);
	dc->enabled = false;
	mutex_unlock(&dc->lock);
	if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
		mutex_unlock(&dc->one_shot_lock);
	synchronize_irq(dc->irq); /* wait for IRQ handlers to finish */

#ifdef CONFIG_SWITCH
	if (dc->switchdev_registered)
		switch_dev_unregister(&dc->modeset_switch);
#endif
	free_irq(dc->irq, dc);
#if defined(CONFIG_TEGRA_NVDISPLAY) && defined(CONFIG_TEGRA_ISOMGR)
	if (!tegra_platform_is_vdk())
		tegra_nvdisp_isomgr_unregister();
#elif defined(CONFIG_TEGRA_ISOMGR)
	if (dc->isomgr_handle) {
		tegra_isomgr_unregister(dc->isomgr_handle);
		dc->isomgr_handle = NULL;
	}
#else
	tegra_disp_clk_put(&ndev->dev, dc->emc_clk);
#endif
	tegra_disp_clk_put(&ndev->dev, dc->emc_la_clk);

	tegra_disp_clk_put(&ndev->dev, dc->clk);
	iounmap(dc->base);
	if (dc->fb_mem)
		release_resource(dc->base_res);
	kfree(dc);
	tegra_dc_set(NULL, ndev->id);

	return 0;
}

#ifdef CONFIG_PM
static int tegra_dc_suspend(struct platform_device *ndev, pm_message_t state)
{
	struct tegra_dc *dc = platform_get_drvdata(ndev);
	int ret = 0;

	trace_display_suspend(dc);
	dev_info(&ndev->dev, "suspend\n");

#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	tegra_dc_ext_disable(dc->ext);
#endif

	tegra_dc_cursor_suspend(dc);

	if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE) {
		mutex_lock(&dc->one_shot_lock);
		cancel_delayed_work_sync(&dc->one_shot_work);
	}
	mutex_lock(&dc->lock);
	ret = tegra_dc_io_start(dc);

	if (dc->out_ops && dc->out_ops->suspend)
		dc->out_ops->suspend(dc);

	if (dc->enabled) {
		_tegra_dc_disable(dc);

		dc->suspended = true;
	}

	if (dc->out && dc->out->postsuspend) {
		dc->out->postsuspend();
		/* avoid resume event due to voltage falling on interfaces that
		 * support hotplug wake. And only do this if a panel is
		 * connected, if we are already disconnected, then no phantom
		 * hotplug can occur by disabling the voltage.
		 */
		if ((dc->out->flags & TEGRA_DC_OUT_HOTPLUG_WAKE_LP0)
			&& tegra_dc_get_connected(dc))
			msleep(100);
	}

	if (!ret)
		tegra_dc_io_end(dc);

	mutex_unlock(&dc->lock);
	if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
		mutex_unlock(&dc->one_shot_lock);
	synchronize_irq(dc->irq); /* wait for IRQ handlers to finish */

	return 0;
}

static int tegra_dc_resume(struct platform_device *ndev)
{
	struct tegra_dc *dc = platform_get_drvdata(ndev);

	trace_display_resume(dc);
	dev_info(&ndev->dev, "resume\n");

	mutex_lock(&dc->lock);
	dc->suspended = false;

	/* To pan the fb on resume */
	tegra_fb_pan_display_reset(dc->fb);

	if (dc->enabled) {
		dc->enabled = false;
		_tegra_dc_set_default_videomode(dc);
		dc->enabled = _tegra_dc_enable(dc);
	}

	if (dc->out && dc->out->hotplug_init)
		dc->out->hotplug_init(&ndev->dev);

	if (dc->out_ops && dc->out_ops->resume)
		dc->out_ops->resume(dc);

	mutex_unlock(&dc->lock);
	tegra_dc_cursor_resume(dc);

	return 0;
}

#endif /* CONFIG_PM */

static void tegra_dc_shutdown(struct platform_device *ndev)
{
	struct tegra_dc *dc = platform_get_drvdata(ndev);

	if (WARN_ON(!dc || !dc->out || !dc->out_ops))
		return;

	if (!dc->enabled)
		return;

	kfree(dc->vedid_data);
	dc->vedid_data = NULL;
	dc->vedid = false;


	/* Let dc clients know about shutdown event before calling disable */
	if (dc->out_ops && dc->out_ops->shutdown)
		dc->out_ops->shutdown(dc);

	tegra_dc_disable(dc);
}

static int suspend_set(const char *val, struct kernel_param *kp)
{
	if (!strcmp(val, "dump"))
		dump_regs(tegra_dcs[0]);
#ifdef CONFIG_PM
	else if (!strcmp(val, "suspend"))
		tegra_dc_suspend(tegra_dcs[0]->ndev, PMSG_SUSPEND);
	else if (!strcmp(val, "resume"))
		tegra_dc_resume(tegra_dcs[0]->ndev);
#endif

	return 0;
}

static int suspend_get(char *buffer, struct kernel_param *kp)
{
	return 0;
}

static int suspend;

module_param_call(suspend, suspend_set, suspend_get, &suspend, 0644);


#ifdef CONFIG_OF
static struct of_device_id tegra_display_of_match[] = {
	{.compatible = "nvidia,tegra114-dc", },
	{.compatible = "nvidia,tegra124-dc", },
	{.compatible = "nvidia,tegra210-dc", },
	{.compatible = "nvidia,tegra186-dc", },
	{ },
};
#endif

static struct platform_driver tegra_dc_driver = {
	.driver = {
		.name = "tegradc",
		.owner = THIS_MODULE,
#ifdef CONFIG_OF
		.of_match_table =
			of_match_ptr(tegra_display_of_match),
#endif
		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
	},
	.probe = tegra_dc_probe,
	.remove = tegra_dc_remove,
#ifdef CONFIG_PM
	.suspend = tegra_dc_suspend,
	.resume = tegra_dc_resume,
#endif
	.shutdown = tegra_dc_shutdown,
};

#ifndef MODULE
static int __init parse_disp_params(char *options, struct tegra_dc_mode *mode)
{
	int i, params[11];
	char *p;

	memset(params, 0, ARRAY_SIZE(params));
	for (i = 0; i < ARRAY_SIZE(params); i++) {
		if ((p = strsep(&options, ",")) != NULL) {
			if (*p)
				params[i] = simple_strtoul(p, &p, 10);
		} else
			return -EINVAL;
	}

	if ((mode->pclk = params[0]) == 0)
		return -EINVAL;

	mode->h_active      = params[1];
	mode->v_active      = params[2];
	mode->h_ref_to_sync = params[3];
	mode->v_ref_to_sync = params[4];
	mode->h_sync_width  = params[5];
	mode->v_sync_width  = params[6];
	mode->h_back_porch  = params[7];
	mode->v_back_porch  = params[8];
	mode->h_front_porch = params[9];
	mode->v_front_porch = params[10];

	return 0;
}

static int __init tegra_dc_mode_override(char *str)
{
	char *p = str, *options;

	if (!p || !*p)
		return -EINVAL;

	p = strstr(str, "hdmi:");
	if (p) {
		p += 5;
		options = strsep(&p, ";");
		if (parse_disp_params(options, &override_disp_mode[TEGRA_DC_OUT_HDMI]))
			return -EINVAL;
	}

	p = strstr(str, "rgb:");
	if (p) {
		p += 4;
		options = strsep(&p, ";");
		if (parse_disp_params(options, &override_disp_mode[TEGRA_DC_OUT_RGB]))
			return -EINVAL;
	}

	p = strstr(str, "dsi:");
	if (p) {
		p += 4;
		options = strsep(&p, ";");
		if (parse_disp_params(options, &override_disp_mode[TEGRA_DC_OUT_DSI]))
			return -EINVAL;
	}

	p = strstr(str, "null:");
	if (p) {
		p += 5;
		options = strsep(&p, ";");
		if (parse_disp_params(options,
				&override_disp_mode[TEGRA_DC_OUT_NULL]))
			return -EINVAL;
	}

	return 0;
}

__setup("disp_params=", tegra_dc_mode_override);
#endif


#ifdef TEGRA_DC_USR_SHARED_IRQ

static struct tegra_dc  *tegra_dc_hwidx2dc(int dcid)
{
	struct tegra_dc  *dc;
	int              i;

	for (i = 0; i < TEGRA_MAX_DC; i++) {
		dc = tegra_dc_get_dc(i);
		if (dc && (dcid == dc->ctrl_num))
			return dc;
	}

	return NULL;
}


/*
 * get number of Tegra display heads
 * o inputs: none
 * o outputs:
 *  - return: number of Tegra display heads
 */
int  tegra_dc_get_numof_dispheads(void)
{
	return TEGRA_MAX_DC;
}
EXPORT_SYMBOL(tegra_dc_get_numof_dispheads);


/*
 * get Tegra display head status
 * o inputs:
 *  - dcid: display head HW index (0 to TEGRA_MAX_DC-1)
 *  - pSts: pointer to the head status structure to be returned
 * o outputs:
 *  - return: error number
 *   . 0: registration successful without an error
 *   . !0: registration failed with an error
 *  - *pSts: head status
 * o notes:
 */
int  tegra_dc_get_disphead_sts(int dcid, struct tegra_dc_head_status *pSts)
{
	struct tegra_dc  *dc = tegra_dc_hwidx2dc(dcid);

	if (dc) {
		pSts->magic = TEGRA_DC_HEAD_STATUS_MAGIC1;
		pSts->irqnum = dc->irq;
		pSts->init = dc->initialized ? 1 : 0;
		pSts->connected = dc->connected ? 1 : 0;
		pSts->active = dc->enabled ? 1 : 0;
		return 0;
	} else {
		return -ENODEV;
	}
}
EXPORT_SYMBOL(tegra_dc_get_disphead_sts);


/*
 * to register the Tegra display ISR user call-back routine
 * o inputs:
 *  - dcid: display head HW index (0 to TEGRA_MAX_DC-1)
 *  - usr_isr_cb: function pointer to the user call-back routine
 *  - usr_isr_pdt: user call-back private data
 * o outputs:
 *  - return: error code
 *   . 0: registration successful without an error
 *   . !0: registration failed with an error
 * o notes: will overwrite the old CB always
 */
int  tegra_dc_register_isr_usr_cb(int dcid,
	int (*usr_isr_cb)(int dcid, unsigned long irq_sts, void *usr_isr_pdt),
	void *usr_isr_pdt)
{
	struct tegra_dc  *dc = tegra_dc_hwidx2dc(dcid);

	/* register usr ISR */
	if (dc && usr_isr_cb) {
		if (dc->isr_usr_cb) {
			dev_warn(&dc->ndev->dev,
				"%s DC%d: overwriting ISR USR CB:%p PDT:%p\n",
				 __func__, dcid,
				 dc->isr_usr_cb, dc->isr_usr_pdt);
		}
		mutex_lock(&dc->lock);
		/* always replace the old ISR */
		dc->isr_usr_cb  = usr_isr_cb;
		dc->isr_usr_pdt = usr_isr_pdt;
		mutex_unlock(&dc->lock);
		dev_info(&dc->ndev->dev,
			"DC%d: ISR USR CB:%p PDT:%p registered\n",
			dcid, usr_isr_cb, usr_isr_pdt);
		return 0;
	} else {
		return dc ? -EINVAL : -ENODEV;
	}
}
EXPORT_SYMBOL(tegra_dc_register_isr_usr_cb);


/*
 * to unregister the Tegra display ISR user call-back routine
 * o inputs:
 *  - dcid: display head HW index (0 to TEGRA_MAX_DC-1)
 *  - usr_isr_cb: registered user call-back. ignored.
 *  - usr_isr_pdt: registered user call-back private data. ignored.
 * o outputs:
 *  - return: error code
 *   . 0: unregistration successful
 *   . !0: unregistration failed with an error
 * o notes: will unregister the current CB always
 */
int  tegra_dc_unregister_isr_usr_cb(int dcid,
	int (*usr_isr_cb)(int dcid, unsigned long irq_sts, void *usr_isr_pdt),
	void *usr_isr_pdt)
{
	struct tegra_dc  *dc = tegra_dc_hwidx2dc(dcid);

	/* unregister USR ISR CB */
	if (dc) {
		mutex_lock(&dc->lock);
		dc->isr_usr_cb = NULL;
		dc->isr_usr_pdt = NULL;
		mutex_unlock(&dc->lock);
		dev_info(&dc->ndev->dev,
			"DC%d: USR ISR CB unregistered\n", dcid);
		return 0;
	} else {
		return -ENODEV;
	}
}
EXPORT_SYMBOL(tegra_dc_unregister_isr_usr_cb);

#endif /* TEGRA_DC_USR_SHARED_IRQ */


static int __init tegra_dc_module_init(void)
{
#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	int ret = tegra_dc_ext_module_init();
	if (ret)
		return ret;
#endif
	return platform_driver_register(&tegra_dc_driver);
}

static void __exit tegra_dc_module_exit(void)
{
	platform_driver_unregister(&tegra_dc_driver);
#ifdef CONFIG_TEGRA_DC_EXTENSIONS
	tegra_dc_ext_module_exit();
#endif
}

module_exit(tegra_dc_module_exit);
module_init(tegra_dc_module_init);