aboutsummaryrefslogtreecommitdiffstats
path: root/arch/mips/include/asm/octeon/octeon-feature.h
blob: 04fac684069c9e1f4a983d44908b935cae156fbd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/***********************license start***************
 * Author: Cavium Networks
 *
 * Contact: support@caviumnetworks.com
 * This file is part of the OCTEON SDK
 *
 * Copyright (c) 2003-2008 Cavium Networks
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, Version 2, as
 * published by the Free Software Foundation.
 *
 * This file is distributed in the hope that it will be useful, but
 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
 * NONINFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this file; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 * or visit http://www.gnu.org/licenses/.
 *
 * This file may also be available under a different license from Cavium.
 * Contact Cavium Networks for more information
 ***********************license end**************************************/

/*
 * File defining checks for different Octeon features.
 */

#ifndef __OCTEON_FEATURE_H__
#define __OCTEON_FEATURE_H__

enum octeon_feature {
	/*
	 * Octeon models in the CN5XXX family and higher support
	 * atomic add instructions to memory (saa/saad).
	 */
	OCTEON_FEATURE_SAAD,
	/* Does this Octeon support the ZIP offload engine? */
	OCTEON_FEATURE_ZIP,
	/* Does this Octeon support crypto acceleration using COP2? */
	OCTEON_FEATURE_CRYPTO,
	/* Does this Octeon support PCI express? */
	OCTEON_FEATURE_PCIE,
	/* Some Octeon models support internal memory for storing
	 * cryptographic keys */
	OCTEON_FEATURE_KEY_MEMORY,
	/* Octeon has a LED controller for banks of external LEDs */
	OCTEON_FEATURE_LED_CONTROLLER,
	/* Octeon has a trace buffer */
	OCTEON_FEATURE_TRA,
	/* Octeon has a management port */
	OCTEON_FEATURE_MGMT_PORT,
	/* Octeon has a raid unit */
	OCTEON_FEATURE_RAID,
	/* Octeon has a builtin USB */
	OCTEON_FEATURE_USB,
};

static inline int cvmx_fuse_read(int fuse);

/**
 * Determine if the current Octeon supports a specific feature. These
 * checks have been optimized to be fairly quick, but they should still
 * be kept out of fast path code.
 *
 * @feature: Feature to check for. This should always be a constant so the
 *                compiler can remove the switch statement through optimization.
 *
 * Returns Non zero if the feature exists. Zero if the feature does not
 *         exist.
 */
static inline int octeon_has_feature(enum octeon_feature feature)
{
	switch (feature) {
	case OCTEON_FEATURE_SAAD:
		return !OCTEON_IS_MODEL(OCTEON_CN3XXX);

	case OCTEON_FEATURE_ZIP:
		if (OCTEON_IS_MODEL(OCTEON_CN30XX)
		    || OCTEON_IS_MODEL(OCTEON_CN50XX)
		    || OCTEON_IS_MODEL(OCTEON_CN52XX))
			return 0;
		else if (OCTEON_IS_MODEL(OCTEON_CN38XX_PASS1))
			return 1;
		else
			return !cvmx_fuse_read(121);

	case OCTEON_FEATURE_CRYPTO:
		return !cvmx_fuse_read(90);

	case OCTEON_FEATURE_PCIE:
		return OCTEON_IS_MODEL(OCTEON_CN56XX)
			|| OCTEON_IS_MODEL(OCTEON_CN52XX);

	case OCTEON_FEATURE_KEY_MEMORY:
	case OCTEON_FEATURE_LED_CONTROLLER:
		return OCTEON_IS_MODEL(OCTEON_CN38XX)
			|| OCTEON_IS_MODEL(OCTEON_CN58XX)
			|| OCTEON_IS_MODEL(OCTEON_CN56XX);
	case OCTEON_FEATURE_TRA:
		return !(OCTEON_IS_MODEL(OCTEON_CN30XX)
			 || OCTEON_IS_MODEL(OCTEON_CN50XX));
	case OCTEON_FEATURE_MGMT_PORT:
		return OCTEON_IS_MODEL(OCTEON_CN56XX)
			|| OCTEON_IS_MODEL(OCTEON_CN52XX);
	case OCTEON_FEATURE_RAID:
		return OCTEON_IS_MODEL(OCTEON_CN56XX)
			|| OCTEON_IS_MODEL(OCTEON_CN52XX);
	case OCTEON_FEATURE_USB:
		return !(OCTEON_IS_MODEL(OCTEON_CN38XX)
			 || OCTEON_IS_MODEL(OCTEON_CN58XX));
	}
	return 0;
}

#endif /* __OCTEON_FEATURE_H__ */
ef='#n588'>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
/*
 * Copyright (c) 2015-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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/delay.h>
#include <linux/io.h>
#include <linux/mailbox_client.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/phy/phy.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/pinmux.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
#include <linux/workqueue.h>
#include <soc/tegra/fuse.h>
#include <soc/tegra/chip-id.h>
#include <linux/clk.h>
#include <linux/reset.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <soc/tegra/xusb.h>
#include <linux/tegra_prod.h>
#include <dt-bindings/pinctrl/pinctrl-tegra-padctl.h>
#include <linux/version.h>

#undef VERBOSE_DEBUG
#ifdef TRACE
#undef TRACE
#endif
#ifdef VERBOSE_DEBUG
#define TRACE(dev, fmt, args...)					\
	dev_dbg(dev, "%s(%d) " fmt "\n", __func__, __LINE__, ## args)
#else
#define TRACE(dev, fmt, args...)					\
	do {								\
		if (0)							\
			dev_dbg(dev, "%s(%d) " fmt "\n",		\
				__func__, __LINE__, ## args);		\
	} while (0)
#endif

#include "core.h"
#include "pinctrl-utils.h"

#define TEGRA_USB3_PHYS		(3)
#define TEGRA_UTMI_PHYS		(3)
#define TEGRA_HSIC_PHYS		(1)
#define TEGRA_CDP_PHYS		(3)

/* FUSE USB_CALIB registers */
/* FUSE_USB_CALIB_0 */
#define HS_CURR_LEVEL_PADX_SHIFT(x)		((x) ? (11 + (x - 1) * 6) : 0)
#define HS_CURR_LEVEL_PAD_MASK			(0x3f)
/* TODO: HS_TERM_RANGE_ADJ has bits overlap, check with hardware team */
#define HS_TERM_RANGE_ADJ_SHIFT			(7)
#define HS_TERM_RANGE_ADJ_MASK			(0xf)
#define HS_SQUELCH_SHIFT			(29)
#define HS_SQUELCH_MASK				(0x7)

/* FUSE_USB_CALIB_EXT_0 */
#define RPD_CTRL_SHIFT				(0)
#define RPD_CTRL_MASK				(0x1f)

/* Data contact detection timeout */
#define TDCD_TIMEOUT_MS				400

/* XUSB PADCTL registers */
#define XUSB_PADCTL_USB2_PAD_MUX		(0x4)
#define   PORT_HSIC				(0)
#define   PORT_XUSB				(1)

#define XUSB_PADCTL_USB2_PORT_CAP		(0x8)
#define XUSB_PADCTL_SS_PORT_CAP			(0xc)
#define   PORTX_CAP_SHIFT(x)			((x) * 4)
#define   PORT_CAP_MASK				(0x3)
#define     PORT_CAP_DISABLED			(0x0)
#define     PORT_CAP_HOST			(0x1)
#define     PORT_CAP_DEVICE			(0x2)
#define     PORT_CAP_OTG			(0x3)
#define   PORT_REVERSE_ID(x)			(1 << ((x) * 4 + 3))

#define XUSB_PADCTL_USB2_OC_MAP			(0x10)
#define XUSB_PADCTL_SS_OC_MAP			(0x14)
#define	  PORTX_OC_PIN_SHIFT(x)			((x) * 4)
#define	  PORT_OC_PIN_MASK			(0xf)
#define	    OC_PIN_DETECTION_DISABLED		(0xf)
#define	    OC_PIN_DETECTED(x)			(x)
#define	    OC_PIN_DETECTED_VBUS_PAD(x)		((x) + 4)

#define XUSB_PADCTL_VBUS_OC_MAP			(0x18)
#define	  VBUS_OC_MAP_SHIFT(x)			((x) * 5 + 1)
#define	  VBUS_OC_MAP_MASK			(0xf)
#define	    VBUS_OC_DETECTION_DISABLED		(0xf)
#define	    VBUS_OC_DETECTED(x)			(x)
#define	    VBUS_OC_DETECTED_VBUS_PAD(x)	((x) + 4)
#define	  VBUS_ENABLE(x)			(1 << (x) * 5)

#define	XUSB_PADCTL_OC_DET			(0x1c)
#define	  SET_OC_DETECTED(x)			(1 << (x))
#define	  OC_DETECTED(x)			(1 << (8 + (x)))
#define	  OC_DETECTED_VBUS_PAD(x)		(1 << (12 + (x)))
#define	  OC_DETECTED_VBUS_PAD_MASK		(0xf << 12)
#define	  OC_DETECTED_INT_EN			(1 << (20 + (x)))
#define	  OC_DETECTED_INT_EN_VBUS_PAD(x)	(1 << (24 + (x)))

#define XUSB_PADCTL_ELPG_PROGRAM		(0x20)
#define   USB2_PORT_WAKE_INTERRUPT_ENABLE(x)	(1 << (x))
#define   USB2_PORT_WAKEUP_EVENT(x)		(1 << ((x) + 7))
#define   SS_PORT_WAKE_INTERRUPT_ENABLE(x)	(1 << ((x) + 14))
#define   SS_PORT_WAKEUP_EVENT(x)		(1 << ((x) + 21))
#define   USB2_HSIC_PORT_WAKE_INTERRUPT_ENABLE(x)	(1 << ((x) + 28))
#define   USB2_HSIC_PORT_WAKEUP_EVENT(x)	(1 << ((x) + 30))
#define   ALL_WAKE_EVENTS						\
	(USB2_PORT_WAKEUP_EVENT(0) | USB2_PORT_WAKEUP_EVENT(1) |	\
	USB2_PORT_WAKEUP_EVENT(2) | SS_PORT_WAKEUP_EVENT(0) |		\
	SS_PORT_WAKEUP_EVENT(1) | SS_PORT_WAKEUP_EVENT(2) |		\
	USB2_HSIC_PORT_WAKEUP_EVENT(0))

#define XUSB_PADCTL_ELPG_PROGRAM_1		(0x24)
#define   SSPX_ELPG_CLAMP_EN(x)			(1 << (0 + (x) * 3))
#define   SSPX_ELPG_CLAMP_EN_EARLY(x)		(1 << (1 + (x) * 3))
#define   SSPX_ELPG_VCORE_DOWN(x)		(1 << (2 + (x) * 3))

#define USB2_BATTERY_CHRG_OTGPADX_CTL0(x)	(0x80 + (x) * 0x40)
#define   PD_CHG				(1 << 0)
#define   VDCD_DET_FILTER_EN			(1 << 4)
#define   VDAT_DET				(1 << 5)
#define   VDAT_DET_FILTER_EN			(1 << 8)
#define   OP_SINK_EN				(1 << 9)
#define   OP_SRC_EN				(1 << 10)
#define   ON_SINK_EN				(1 << 11)
#define   ON_SRC_EN				(1 << 12)
#define   OP_I_SRC_EN				(1 << 13)
#define   ZIP_FILTER_EN				(1 << 21)
#define   ZIN_FILTER_EN				(1 << 25)
#define   DCD_DETECTED				(1 << 26)
#define   SRP_DETECT_EN				(1 << 28)
#define   SRP_DETECTED				(1 << 29)
#define   SRP_INTR_EN				(1 << 30)
#define   GENERATE_SRP				(1 << 31)

#define USB2_BATTERY_CHRG_OTGPADX_CTL1(x)	(0x84 + (x) * 0x40)
#define   DIV_DET_EN				(1 << 4)
#define   PD_VREG				(1 << 6)
#define   VREG_LEV(x)				(((x) & 0x3) << 7)
#define   VREG_DIR(x)				(((x) & 0x3) << 11)
#define   VREG_DIR_IN				VREG_DIR(1)
#define   VREG_DIR_OUT				VREG_DIR(2)
#define   USBOP_RPD_OVRD			(1 << 16)
#define   USBOP_RPD_OVRD_VAL			(1 << 17)
#define   USBOP_RPU_OVRD			(1 << 18)
#define   USBOP_RPU_OVRD_VAL			(1 << 19)
#define   USBON_RPD_OVRD			(1 << 20)
#define   USBON_RPD_OVRD_VAL			(1 << 21)
#define   USBON_RPU_OVRD			(1 << 22)
#define   USBON_RPU_OVRD_VAL			(1 << 23)

#define XUSB_PADCTL_USB2_OTG_PADX_CTL0(x)	(0x88 + (x) * 0x40)
#define   HS_CURR_LEVEL(x)			((x) & 0x3f)
#define   TERM_SEL				(1 << 25)
#define   USB2_OTG_PD				(1 << 26)
#define   USB2_OTG_PD2				(1 << 27)
#define   USB2_OTG_PD2_OVRD_EN			(1 << 28)
#define   USB2_OTG_PD_ZI			(1 << 29)

#define XUSB_PADCTL_USB2_OTG_PADX_CTL1(x)	(0x8c + (x) * 0x40)
#define   USB2_OTG_PD_DR			(1 << 2)
#define   TERM_RANGE_ADJ(x)			(((x) & 0xf) << 3)
#define   RPD_CTRL(x)				(((x) & 0x1f) << 26)

#define XUSB_PADCTL_USB2_BATTERY_CHRG_TDCD_DBNC_TIMER_0	(0x280)
#define   TDCD_DBNC(x)				(((x) & 0x7ff) << 0)

#define XUSB_PADCTL_USB2_BIAS_PAD_CTL0		(0x284)
#define   BIAS_PAD_PD				(1 << 11)
#define   HS_SQUELCH_LEVEL(x)			(((x) & 0x7) << 0)

#define XUSB_PADCTL_USB2_BIAS_PAD_CTL1		(0x288)
#define   USB2_TRK_START_TIMER(x)		(((x) & 0x7f) << 12)
#define   USB2_TRK_DONE_RESET_TIMER(x)		(((x) & 0x7f) << 19)
#define   USB2_PD_TRK				(1 << 26)

#define XUSB_PADCTL_HSIC_PADX_CTL0(x)		(0x300 + (x) * 0x20)
#define   HSIC_PD_TX_DATA0			(1 << 1)
#define   HSIC_PD_TX_STROBE			(1 << 3)
#define   HSIC_PD_RX_DATA0			(1 << 4)
#define   HSIC_PD_RX_STROBE			(1 << 6)
#define   HSIC_PD_ZI_DATA0			(1 << 7)
#define   HSIC_PD_ZI_STROBE			(1 << 9)
#define   HSIC_RPD_DATA0			(1 << 13)
#define   HSIC_RPD_STROBE			(1 << 15)
#define   HSIC_RPU_DATA0			(1 << 16)
#define   HSIC_RPU_STROBE			(1 << 18)

#define XUSB_PADCTL_HSIC_PAD_TRK_CTL0		(0x340)
#define   HSIC_TRK_START_TIMER(x)		(((x) & 0x7f) << 5)
#define   HSIC_TRK_DONE_RESET_TIMER(x)		(((x) & 0x7f) << 12)
#define   HSIC_PD_TRK				(1 << 19)

#define USB2_VBUS_ID				(0x360)
#define   OTG_VBUS_SESS_VLD			(1 << 0)
#define   OTG_VBUS_SESS_VLD_ST_CHNG		(1 << 1)
#define   OTG_VBUS_SESS_VLD_CHNG_INTR_EN	(1 << 2)
#define   VBUS_VALID				(1 << 3)
#define   VBUS_VALID_ST_CHNG			(1 << 4)
#define   VBUS_VALID_CHNG_INTR_EN		(1 << 5)
#define   IDDIG					(1 << 6)
#define   IDDIG_A				(1 << 7)
#define   IDDIG_B				(1 << 8)
#define   IDDIG_C				(1 << 9)
#define   RID_MASK				(0xf << 6)
#define   IDDIG_ST_CHNG				(1 << 10)
#define   IDDIG_CHNG_INTR_EN			(1 << 11)
#define   VBUS_OVERRIDE				(1 << 14)
#define   ID_OVERRIDE_SHIFT			18
#define   ID_OVERRIDE_MASK			0xf
#define   ID_OVERRIDE(x)			(((x) & 0xf) << 18)
#define   ID_OVERRIDE_FLOATING			ID_OVERRIDE(8)
#define   ID_OVERRIDE_GROUNDED			ID_OVERRIDE(0)
#define   VBUS_WAKEUP				(1 << 22)
#define   VBUS_WAKEUP_ST_CHNG			(1 << 23)
#define   VBUS_WAKEUP_CHNG_INTR_EN		(1 << 24)

/* XUSB AO registers */
#define XUSB_AO_USB_DEBOUNCE_DEL		(0x4)
#define   UHSIC_LINE_DEB_CNT(x)			(((x) & 0xf) << 4)
#define   UTMIP_LINE_DEB_CNT(x)			((x) & 0xf)

#define XUSB_AO_UTMIP_TRIGGERS(x)		(0x40 + (x) * 4)
#define   CLR_WALK_PTR				(1 << 0)
#define   CAP_CFG				(1 << 1)
#define   CLR_WAKE_ALARM			(1 << 3)

#define XUSB_AO_UHSIC_TRIGGERS(x)		(0x60 + (x) * 4)
#define   HSIC_CLR_WALK_PTR			(1 << 0)
#define   HSIC_CLR_WAKE_ALARM			(1 << 3)
#define   HSIC_CAP_CFG				(1 << 4)

#define XUSB_AO_UTMIP_SAVED_STATE(x)		(0x70 + (x) * 4)
#define   SPEED(x)				((x) & 0x3)
#define     UTMI_HS				SPEED(0)
#define     UTMI_FS				SPEED(1)
#define     UTMI_LS				SPEED(2)
#define     UTMI_RST				SPEED(3)

#define XUSB_AO_UHSIC_SAVED_STATE(x)		(0x90 + (x) * 4)
#define   MODE(x)				((x) & 0x1)
#define   MODE_HS				MODE(1)
#define   MODE_RST				MODE(0)

#define XUSB_AO_UTMIP_SLEEPWALK_CFG(x)		(0xd0 + (x) * 4)
#define XUSB_AO_UHSIC_SLEEPWALK_CFG(x)		(0xf0 + (x) * 4)
#define   FAKE_USBOP_VAL			(1 << 0)
#define   FAKE_USBON_VAL			(1 << 1)
#define   FAKE_USBOP_EN				(1 << 2)
#define   FAKE_USBON_EN				(1 << 3)
#define   FAKE_STROBE_VAL			(1 << 0)
#define   FAKE_DATA_VAL				(1 << 1)
#define   FAKE_STROBE_EN			(1 << 2)
#define   FAKE_DATA_EN				(1 << 3)
#define   WAKE_WALK_EN				(1 << 14)
#define   MASTER_ENABLE				(1 << 15)
#define   LINEVAL_WALK_EN			(1 << 16)
#define   WAKE_VAL(x)				(((x) & 0xf) << 17)
#define     WAKE_VAL_NONE			WAKE_VAL(12)
#define     WAKE_VAL_ANY			WAKE_VAL(15)
#define     WAKE_VAL_DS10			WAKE_VAL(2)
#define   LINE_WAKEUP_EN			(1 << 21)
#define   MASTER_CFG_SEL			(1 << 22)

#define XUSB_AO_UTMIP_SLEEPWALK(x)		(0x100 + (x) * 4)
/* phase A */
#define   USBOP_RPD_A				(1 << 0)
#define   USBON_RPD_A				(1 << 1)
#define   AP_A					(1 << 4)
#define   AN_A					(1 << 5)
#define   HIGHZ_A				(1 << 6)
/* phase B */
#define   USBOP_RPD_B				(1 << 8)
#define   USBON_RPD_B				(1 << 9)
#define   AP_B					(1 << 12)
#define   AN_B					(1 << 13)
#define   HIGHZ_B				(1 << 14)
/* phase C */
#define   USBOP_RPD_C				(1 << 16)
#define   USBON_RPD_C				(1 << 17)
#define   AP_C					(1 << 20)
#define   AN_C					(1 << 21)
#define   HIGHZ_C				(1 << 22)
/* phase D */
#define   USBOP_RPD_D				(1 << 24)
#define   USBON_RPD_D				(1 << 25)
#define   AP_D					(1 << 28)
#define   AN_D					(1 << 29)
#define   HIGHZ_D				(1 << 30)

#define XUSB_AO_UHSIC_SLEEPWALK(x)		(0x120 + (x) * 4)
/* phase A */
#define   RPD_STROBE_A				(1 << 0)
#define   RPD_DATA0_A				(1 << 1)
#define   RPU_STROBE_A				(1 << 2)
#define   RPU_DATA0_A				(1 << 3)
/* phase B */
#define   RPD_STROBE_B				(1 << 8)
#define   RPD_DATA0_B				(1 << 9)
#define   RPU_STROBE_B				(1 << 10)
#define   RPU_DATA0_B				(1 << 11)
/* phase C */
#define   RPD_STROBE_C				(1 << 16)
#define   RPD_DATA0_C				(1 << 17)
#define   RPU_STROBE_C				(1 << 18)
#define   RPU_DATA0_C				(1 << 19)
/* phase D */
#define   RPD_STROBE_D				(1 << 24)
#define   RPD_DATA0_D				(1 << 25)
#define   RPU_STROBE_D				(1 << 26)
#define   RPU_DATA0_D				(1 << 27)

#define XUSB_AO_UTMIP_PAD_CFG(x)		(0x130 + (x) * 4)
#define   FSLS_USE_XUSB_AO			(1 << 3)
#define   TRK_CTRL_USE_XUSB_AO			(1 << 4)
#define   RPD_CTRL_USE_XUSB_AO			(1 << 5)
#define   RPU_USE_XUSB_AO			(1 << 6)
#define   VREG_USE_XUSB_AO			(1 << 7)
#define   USBOP_VAL_PD				(1 << 8)
#define   USBON_VAL_PD				(1 << 9)
#define   E_DPD_OVRD_EN				(1 << 10)
#define   E_DPD_OVRD_VAL			(1 << 11)

#define XUSB_AO_UHSIC_PAD_CFG(x)		(0x150 + (x) * 4)
#define   STROBE_VAL_PD				(1 << 0)
#define   DATA0_VAL_PD				(1 << 1)
#define   USE_XUSB_AO				(1 << 4)

enum tegra186_function {
	TEGRA186_FUNC_HSIC = 0,
	TEGRA186_FUNC_XUSB,
};

struct tegra_padctl_function {
	const char *name;
	const char * const *groups;
	unsigned int num_groups;
};

struct tegra_padctl_group {
	const unsigned int *funcs;
	unsigned int num_funcs;
};

struct tegra_padctl_soc {
	const struct pinctrl_pin_desc *pins;
	unsigned int num_pins;

	const struct tegra_padctl_function *functions;
	unsigned int num_functions;

	const struct tegra_padctl_pad *pads;
	unsigned int num_pads;

	unsigned int hsic_port_offset;

	const char * const *supply_names;
	unsigned int num_supplies;

	unsigned int num_oc_pins;
};

struct tegra_padctl_pad {
	const char *name;

	unsigned int offset;
	unsigned int shift;
	unsigned int mask;
	unsigned int iddq;

	const unsigned int *funcs;
	unsigned int num_funcs;
};

struct tegra_xusb_fuse_calibration {
	u32 hs_curr_level[TEGRA_UTMI_PHYS];
	u32 hs_squelch;
	u32 hs_term_range_adj;
	u32 rpd_ctrl;
};

enum xusb_port_cap {
	CAP_DISABLED = TEGRA_PADCTL_PORT_DISABLED,
	HOST_ONLY = TEGRA_PADCTL_PORT_HOST_ONLY,
	DEVICE_ONLY = TEGRA_PADCTL_PORT_DEVICE_ONLY,
	OTG = TEGRA_PADCTL_PORT_OTG_CAP,
};

struct tegra_xusb_usb3_port {
	enum xusb_port_cap port_cap;
	int oc_pin;
};

struct tegra_xusb_utmi_port {
	enum xusb_port_cap port_cap;
	int hs_curr_level_offset; /* deal with platform design deviation */
	bool poweron;
	int oc_pin;
};

struct tegra_xusb_hsic_port {
	bool pretend_connected;
};

struct padctl_context {
	u32 vbus_id;
	u32 usb2_pad_mux;
	u32 usb2_port_cap;
	u32 ss_port_cap;
};

struct tegra_padctl {
	struct device *dev;
	void __iomem *padctl_regs;
	void __iomem *ao_regs;

	struct reset_control *padctl_rst;

	struct clk *xusb_clk; /* xusb main clock */
	struct clk *utmipll; /* utmi pads */
	struct clk *usb2_trk_clk; /* utmi tracking circuit clock */
	struct clk *hsic_trk_clk; /* hsic tracking circuit clock */

	struct mutex lock;

	const struct tegra_padctl_soc *soc;
	struct tegra_xusb_fuse_calibration calib;
	struct tegra_prod *prod_list;
	struct pinctrl_dev *pinctrl;
	struct pinctrl_desc desc;

	struct phy_provider *provider;
	struct phy *usb3_phys[TEGRA_USB3_PHYS];
	struct phy *utmi_phys[TEGRA_UTMI_PHYS];
	struct phy *hsic_phys[TEGRA_HSIC_PHYS];
	struct phy *cdp_phys[TEGRA_CDP_PHYS];
	struct tegra_xusb_hsic_port hsic_ports[TEGRA_HSIC_PHYS];
	struct tegra_xusb_utmi_port utmi_ports[TEGRA_UTMI_PHYS];
	int utmi_otg_port_base_1; /* one based utmi port number */
	struct tegra_xusb_usb3_port usb3_ports[TEGRA_USB3_PHYS];
	int usb3_otg_port_base_1; /* one based usb3 port number */

	struct work_struct mbox_req_work;
	struct tegra_xusb_mbox_msg mbox_req;
	struct mbox_client mbox_client;
	struct mbox_chan *mbox_chan;

	bool host_mode_phy_disabled; /* set true if mailbox is not available */
	unsigned int bias_pad_enable;
	/* TODO: should move to host controller driver? */
	struct regulator *vbus[TEGRA_UTMI_PHYS];
	struct regulator *vddio_hsic;

	bool otg_vbus_alwayson;

	struct regulator_bulk_data *supplies;
	struct padctl_context padctl_context;

	bool cdp_used;

	struct pinctrl *oc_pinctrl;
	/*
	 * array of pinctrl_state (of number num_oc_pins)
	 * for different OC states
	 */
	struct pinctrl_state **oc_tristate_enable;
	struct pinctrl_state **oc_passthrough_enable;
	struct pinctrl_state **oc_disable;
};

#ifdef VERBOSE_DEBUG
#define ao_writel(_padctl, _value, _offset)				\
{									\
	unsigned long v = _value, o = _offset;				\
	pr_debug("%s ao_writel %s(@0x%lx) with 0x%lx\n", __func__,	\
		#_offset, o, v);					\
	writel(v, _padctl->ao_regs + o);				\
}

#define ao_readl(_padctl, _offset)					\
({									\
	unsigned long v, o = _offset;					\
	v = readl(_padctl->ao_regs + o);				\
	pr_debug("%s ao_readl %s(@0x%lx) = 0x%lx\n", __func__,		\
		#_offset, o, v);					\
	v;								\
})
#else
static inline void ao_writel(struct tegra_padctl *padctl, u32 value,
				 unsigned long offset)
{
	writel(value, padctl->ao_regs + offset);
}

static inline u32 ao_readl(struct tegra_padctl *padctl,
			       unsigned long offset)
{
	return readl(padctl->ao_regs + offset);
}
#endif

#ifdef VERBOSE_DEBUG
#define padctl_writel(_padctl, _value, _offset)				\
{									\
	unsigned long v = _value, o = _offset;				\
	pr_debug("%s padctl_write %s(@0x%lx) with 0x%lx\n", __func__,	\
		#_offset, o, v);					\
	writel(v, _padctl->padctl_regs + o);				\
}

#define padctl_readl(_padctl, _offset)					\
({									\
	unsigned long v, o = _offset;					\
	v = readl(_padctl->padctl_regs + o);				\
	pr_debug("%s padctl_read %s(@0x%lx) = 0x%lx\n", __func__,	\
		#_offset, o, v);					\
	v;								\
})
#else
static inline void padctl_writel(struct tegra_padctl *padctl, u32 value,
				 unsigned long offset)
{
	writel(value, padctl->padctl_regs + offset);
}

static inline u32 padctl_readl(struct tegra_padctl *padctl,
			       unsigned long offset)
{
	return readl(padctl->padctl_regs + offset);
}
#endif

static int tegra186_padctl_regulators_init(struct tegra_padctl *padctl)
{
	struct device *dev = padctl->dev;
	size_t size;
	int err;
	int i;

	size = padctl->soc->num_supplies * sizeof(struct regulator_bulk_data);
	padctl->supplies = devm_kzalloc(dev, size, GFP_ATOMIC);
	if (!padctl->supplies) {
		dev_err(dev, "failed to alloc memory for regulators\n");
		return -ENOMEM;
	}

	for (i = 0; i < padctl->soc->num_supplies; i++)
		padctl->supplies[i].supply = padctl->soc->supply_names[i];

	err = devm_regulator_bulk_get(dev, padctl->soc->num_supplies,
					padctl->supplies);
	if (err) {
		dev_err(dev, "failed to request regulators %d\n", err);
		return err;
	}

	return 0;
}

static inline
struct tegra_padctl *mbox_work_to_padctl(struct work_struct *work)
{
	return container_of(work, struct tegra_padctl, mbox_req_work);
}

#define PIN_OTG_0	0
#define PIN_OTG_1	1
#define PIN_OTG_2	2
#define PIN_HSIC_0	3
#define PIN_USB3_0	4
#define PIN_USB3_1	5
#define PIN_USB3_2	6
#define PIN_CDP_0	7
#define PIN_CDP_1	8
#define PIN_CDP_2	9

static inline bool pad_is_otg(unsigned int pad)
{
	return pad >= PIN_OTG_0 && pad <= PIN_OTG_2;
}

static inline bool pad_is_hsic(unsigned int pad)
{
	return pad == PIN_HSIC_0;
}

static inline bool pad_is_usb3(unsigned int pad)
{
	return pad >= PIN_USB3_0 && pad <= PIN_USB3_2;
}

static inline bool pad_is_cdp(unsigned int pad)
{
	return pad >= PIN_CDP_0 && pad <= PIN_CDP_2;
}

static int tegra_padctl_get_groups_count(struct pinctrl_dev *pinctrl)
{
	struct tegra_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);

	TRACE(padctl->dev, "num_pins %u", padctl->soc->num_pins);
	return padctl->soc->num_pins;
}

static const char *tegra_padctl_get_group_name(struct pinctrl_dev *pinctrl,
						    unsigned int group)
{
	struct tegra_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
	struct device *dev = padctl->dev;

	TRACE(dev, "group %u name %s", group, padctl->soc->pins[group].name);
	return padctl->soc->pins[group].name;
}

static int tegra_padctl_get_group_pins(struct pinctrl_dev *pinctrl,
				       unsigned group,
				       const unsigned **pins,
				       unsigned *num_pins)
{
	struct tegra_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
	struct device *d = padctl->dev;

	*pins = &padctl->soc->pins[group].number;
	*num_pins = 1; /* one pin per group */

	TRACE(d, "group %u num_pins %u pins[0] %u", group, *num_pins, *pins[0]);

	return 0;
}

enum tegra_xusb_padctl_param {
	TEGRA_PADCTL_PORT_CAP,
	TEGRA_PADCTL_HSIC_PRETEND_CONNECTED,
	TEGRA_PADCTL_UTMI_HS_CURR_LEVEL_OFFSET,
	TEGRA_PADCTL_OC_PIN,
};

static const struct tegra_padctl_property {
	const char *name;
	enum tegra_xusb_padctl_param param;
} properties[] = {
	{"nvidia,port-cap", TEGRA_PADCTL_PORT_CAP},
	{"nvidia,pretend-connected", TEGRA_PADCTL_HSIC_PRETEND_CONNECTED},
	{"nvidia,hs_curr_level_offset", TEGRA_PADCTL_UTMI_HS_CURR_LEVEL_OFFSET},
	{"nvidia,oc-pin", TEGRA_PADCTL_OC_PIN},
};

#define TEGRA_XUSB_PADCTL_PACK(param, value) ((param) << 16 | (value & 0xffff))
#define TEGRA_XUSB_PADCTL_UNPACK_PARAM(config) ((config) >> 16)
#define TEGRA_XUSB_PADCTL_UNPACK_VALUE(config) ((config) & 0xffff)

static int tegra186_padctl_parse_subnode(struct tegra_padctl *padctl,
					   struct device_node *np,
					   struct pinctrl_map **maps,
					   unsigned int *reserved_maps,
					   unsigned int *num_maps)
{
	unsigned int i, reserve = 0, num_configs = 0;
	unsigned long config, *configs = NULL;
	const char *function, *group;
	struct property *prop;
	int err = 0;
	u32 value;

	err = of_property_read_string(np, "nvidia,function", &function);
	if (err < 0) {
		if (err != -EINVAL)
			goto out;

		function = NULL;
	}

	for (i = 0; i < ARRAY_SIZE(properties); i++) {
		err = of_property_read_u32(np, properties[i].name, &value);
		if (err < 0) {
			if (err == -EINVAL)
				continue;

			goto out;
		}

		config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, value);

		err = pinctrl_utils_add_config(padctl->pinctrl, &configs,
					       &num_configs, config);
		if (err < 0)
			goto out;
	}

	if (function)
		reserve++;

	if (num_configs)
		reserve++;

	err = of_property_count_strings(np, "nvidia,lanes");
	if (err < 0)
		goto out;

	reserve *= err;

	err = pinctrl_utils_reserve_map(padctl->pinctrl, maps, reserved_maps,
					num_maps, reserve);
	if (err < 0)
		goto out;

	of_property_for_each_string(np, "nvidia,lanes", prop, group) {
		if (function) {
			err = pinctrl_utils_add_map_mux(padctl->pinctrl, maps,
					reserved_maps, num_maps, group,
					function);
			if (err < 0)
				goto out;
		}

		if (num_configs) {
			err = pinctrl_utils_add_map_configs(padctl->pinctrl,
					maps, reserved_maps, num_maps, group,
					configs, num_configs,
					PIN_MAP_TYPE_CONFIGS_GROUP);
			if (err < 0)
				goto out;
		}
	}

	err = 0;

out:
	kfree(configs);
	return err;
}

static int tegra_padctl_dt_node_to_map(struct pinctrl_dev *pinctrl,
				       struct device_node *parent,
				       struct pinctrl_map **maps,
				       unsigned int *num_maps)
{
	struct tegra_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
	unsigned int reserved_maps = 0;
	struct device_node *np;
	int err;

	*num_maps = 0;
	*maps = NULL;

	for_each_child_of_node(parent, np) {
		/* If node status is disabled then ignore the node */
		if (!of_device_is_available(np))
			continue;

		err = tegra186_padctl_parse_subnode(padctl, np, maps,
						    &reserved_maps,
						    num_maps);
		if (err < 0) {
			pr_info("%s %d err %d\n", __func__, __LINE__, err);
			return err;
		}
	}

	return 0;
}

static const struct pinctrl_ops tegra_xusb_padctl_pinctrl_ops = {
	.get_groups_count = tegra_padctl_get_groups_count,
	.get_group_name = tegra_padctl_get_group_name,
	.get_group_pins = tegra_padctl_get_group_pins,
	.dt_node_to_map = tegra_padctl_dt_node_to_map,
/* API changed after 4.6.0-rc1 */
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
	.dt_free_map = pinctrl_utils_dt_free_map,
#else
	.dt_free_map = pinctrl_utils_free_map,
#endif
};

static int tegra186_padctl_get_functions_count(struct pinctrl_dev *pinctrl)
{
	struct tegra_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);

	TRACE(padctl->dev, "num_functions %u", padctl->soc->num_functions);
	return padctl->soc->num_functions;
}

static const char *
tegra186_padctl_get_function_name(struct pinctrl_dev *pinctrl,
				    unsigned int function)
{
	struct tegra_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);

	TRACE(padctl->dev, "function %u name %s", function,
					padctl->soc->functions[function].name);

	return padctl->soc->functions[function].name;
}

static int tegra186_padctl_get_function_groups(struct pinctrl_dev *pinctrl,
					       unsigned int function,
					       const char * const **groups,
					       unsigned * const num_groups)
{
	struct tegra_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);

	*num_groups = padctl->soc->functions[function].num_groups;
	*groups = padctl->soc->functions[function].groups;

	TRACE(padctl->dev, "function %u *num_groups %u groups %s",
				function, *num_groups, *groups[0]);
	return 0;
}

static int tegra186_padctl_pinmux_set(struct pinctrl_dev *pinctrl,
				      unsigned int function,
				      unsigned int group)
{
	struct tegra_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
	const struct tegra_padctl_pad *pad;
	unsigned int i;
	u32 value;

	pad = &padctl->soc->pads[group];

	TRACE(padctl->dev, "group %u (%s) function %u num_funcs %d",
			group, pad->name, function, pad->num_funcs);

	for (i = 0; i < pad->num_funcs; i++) {
		TRACE(padctl->dev, "lane->funcs[%d] %d\n", i, pad->funcs[i]);
		if (pad->funcs[i] == function)
			break;
	}

	if (i >= pad->num_funcs)
		return -EINVAL;

	TRACE(padctl->dev, "group %s set to function %s",
			pad->name, padctl->soc->functions[function].name);

	if (pad_is_otg(group)) {
		value = padctl_readl(padctl, pad->offset);
		value &= ~(pad->mask << pad->shift);
		value |= (PORT_XUSB << pad->shift);
		padctl_writel(padctl, value, pad->offset);
	} else if (pad_is_hsic(group)) {
		value = padctl_readl(padctl, pad->offset);
		value &= ~(pad->mask << pad->shift);
		value |= (PORT_HSIC << pad->shift);
		padctl_writel(padctl, value, pad->offset);
	} else if (pad_is_cdp(group)) {
		if (function != TEGRA186_FUNC_XUSB)
			dev_warn(padctl->dev, "group %s isn't for xusb!",
				 pad->name);
	} else
		return -EINVAL;

	return 0;
}

static const struct pinmux_ops tegra186_padctl_pinmux_ops = {
	.get_functions_count = tegra186_padctl_get_functions_count,
	.get_function_name = tegra186_padctl_get_function_name,
	.get_function_groups = tegra186_padctl_get_function_groups,
	.set_mux = tegra186_padctl_pinmux_set,
};

static int tegra_padctl_pinconf_group_get(struct pinctrl_dev *pinctrl,
					  unsigned int group,
					  unsigned long *config)
{
	struct tegra_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
	enum tegra_xusb_padctl_param param;
	int value = 0;

	param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(*config);

	TRACE(padctl->dev, "group %u param 0x%x\n", group, param);

	switch (param) {

	default:
		dev_err(padctl->dev, "invalid configuration parameter: %04x\n",
			param);
		return -ENOTSUPP;
	}

	*config = TEGRA_XUSB_PADCTL_PACK(param, value);
	return 0;
}

static int tegra_padctl_pinconf_group_set(struct pinctrl_dev *pinctrl,
					       unsigned int group,
					       unsigned long *configs,
					       unsigned num_configs)
{
	struct tegra_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
	struct device *dev = padctl->dev;
	enum tegra_xusb_padctl_param param;
	unsigned long value;
	int port;
	s16 offset;
	int i;

	for (i = 0; i < num_configs; i++) {
		param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(configs[i]);
		value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(configs[i]);

		TRACE(dev, "group %u config 0x%lx param 0x%x value 0x%lx",
			group, configs[i], param, value);

		switch (param) {
		case TEGRA_PADCTL_PORT_CAP:
			if (value > TEGRA_PADCTL_PORT_OTG_CAP) {
				dev_err(dev, "Invalid port-cap: %lu\n", value);
				return -EINVAL;
			}
			if (pad_is_usb3(group)) {
				int port = group - PIN_USB3_0;

				padctl->usb3_ports[port].port_cap = value;
				TRACE(dev, "USB3 port %d cap %lu",
				      port, value);
				if (value == OTG) {
					if (padctl->usb3_otg_port_base_1)
						dev_warn(dev, "enabling OTG on multiple USB3 ports\n");


					dev_info(dev, "using USB3 port %d for otg\n",
						 port);
					padctl->usb3_otg_port_base_1 = port + 1;
				}
			} else if (pad_is_otg(group)) {
				int port = group - PIN_OTG_0;

				padctl->utmi_ports[port].port_cap = value;
				TRACE(dev, "UTMI port %d cap %lu",
				      port, value);
				if (value == OTG) {
					if (padctl->utmi_otg_port_base_1)
						dev_warn(dev, "enabling OTG on multiple UTMI ports\n");

					dev_info(dev, "using UTMI port %d for otg\n",
						 port);

					padctl->utmi_otg_port_base_1 = port + 1;
				}
			} else {
				dev_err(dev, "port-cap not applicable for pin %d\n",
					group);
				return -EINVAL;
			}

			break;

		case TEGRA_PADCTL_HSIC_PRETEND_CONNECTED:
			if (!pad_is_hsic(group)) {
				dev_err(dev, "pretend-connected is not applicable for pin %d\n",
					group);
				return -EINVAL;
			}

			port = group - PIN_HSIC_0;
			padctl->hsic_ports[port].pretend_connected = value;
			TRACE(dev, "HSIC port %d pretend-connected %ld",
			      port, value);
			break;

		case TEGRA_PADCTL_UTMI_HS_CURR_LEVEL_OFFSET:
			if (!pad_is_otg(group)) {
				dev_err(dev, "hs_curr_level_offset is not applicable for pin %d\n",
					group);
				return -EINVAL;
			}

			port = group - PIN_OTG_0;
			offset = TEGRA_XUSB_PADCTL_UNPACK_VALUE(configs[i]);

			padctl->utmi_ports[port].hs_curr_level_offset = offset;
			TRACE(dev, "UTMI port %d hs_curr_level_offset %d",
			      port, offset);
			break;
		case TEGRA_PADCTL_OC_PIN:
			if (pad_is_usb3(group)) {
				int port = group - PIN_USB3_0;

				if (value >= padctl->soc->num_oc_pins) {
					dev_err(dev, "Invalid OC pin: %lu\n",
						value);
					return -EINVAL;
				}
				TRACE(dev, "USB3 port %d OC pin %lu",
				      port, value);
				padctl->usb3_ports[port].oc_pin = (int) value;
			} else if (pad_is_otg(group)) {
				int port = group - PIN_OTG_0;

				if (value >= padctl->soc->num_oc_pins) {
					dev_err(dev, "Invalid OC pin: %lu\n",
						value);
					return -EINVAL;
				}
				TRACE(dev, "USB2 port %d OC pin %lu",
				      port, value);

				padctl->utmi_ports[port].oc_pin = (int) value;
			}

			break;
		default:
			dev_err(dev, "invalid configuration parameter: %04x\n",
				param);
			return -ENOTSUPP;
		}
	}
	return 0;
}

#ifdef CONFIG_DEBUG_FS
static const char *strip_prefix(const char *s)
{
	const char *comma = strchr(s, ',');

	if (!comma)
		return s;

	return comma + 1;
}

static void
tegra_padctl_pinconf_group_dbg_show(struct pinctrl_dev *pinctrl,
				    struct seq_file *s,
				    unsigned int group)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(properties); i++) {
		unsigned long config, value;
		int err;

		config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, 0);

		err = tegra_padctl_pinconf_group_get(pinctrl, group,
							  &config);
		if (err < 0)
			continue;

		value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);

		seq_printf(s, "\n\t%s=%lu\n", strip_prefix(properties[i].name),
			   value);
	}
}

static void
tegra_padctl_pinconf_config_dbg_show(struct pinctrl_dev *pinctrl,
				     struct seq_file *s,
				     unsigned long config)
{
	enum tegra_xusb_padctl_param param;
	const char *name = "unknown";
	unsigned long value;
	unsigned int i;

	param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(config);
	value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);

	for (i = 0; i < ARRAY_SIZE(properties); i++) {
		if (properties[i].param == param) {
			name = properties[i].name;
			break;
		}
	}

	seq_printf(s, "%s=%lu", strip_prefix(name), value);
}
#endif

static const struct pinconf_ops tegra_padctl_pinconf_ops = {
	.pin_config_group_get = tegra_padctl_pinconf_group_get,
	.pin_config_group_set = tegra_padctl_pinconf_group_set,
#ifdef CONFIG_DEBUG_FS
	.pin_config_group_dbg_show = tegra_padctl_pinconf_group_dbg_show,
	.pin_config_config_dbg_show = tegra_padctl_pinconf_config_dbg_show,
#endif
};

static int usb3_phy_to_port(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	unsigned int i;

	for (i = 0; i < TEGRA_USB3_PHYS; i++) {
		if (phy == padctl->usb3_phys[i])
			return i;
	}
	WARN_ON(1);

	return -EINVAL;
}

static int tegra186_usb3_phy_power_on(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port = usb3_phy_to_port(phy);
	int pin;
	u32 reg;

	if (port < 0)
		return port;

	pin = padctl->usb3_ports[port].oc_pin;
	mutex_lock(&padctl->lock);

	dev_dbg(padctl->dev, "power on USB3 port %d\n", port);

	reg = padctl_readl(padctl, XUSB_PADCTL_SS_PORT_CAP);
	reg &= ~(PORT_CAP_MASK << PORTX_CAP_SHIFT(port));
	if (padctl->usb3_ports[port].port_cap == CAP_DISABLED)
		reg |= (PORT_CAP_DISABLED << PORTX_CAP_SHIFT(port));
	else if (padctl->usb3_ports[port].port_cap == DEVICE_ONLY)
		reg |= (PORT_CAP_DEVICE << PORTX_CAP_SHIFT(port));
	else if (padctl->usb3_ports[port].port_cap == HOST_ONLY)
		reg |= (PORT_CAP_HOST << PORTX_CAP_SHIFT(port));
	else if (padctl->usb3_ports[port].port_cap == OTG)
		reg |= (PORT_CAP_OTG << PORTX_CAP_SHIFT(port));
	padctl_writel(padctl, reg, XUSB_PADCTL_SS_PORT_CAP);

	/* setting SS OC map */
	if (pin >= 0) {
		reg = padctl_readl(padctl, XUSB_PADCTL_SS_OC_MAP);
		reg &= ~(PORT_OC_PIN_MASK << PORTX_OC_PIN_SHIFT(port));
		reg |= (OC_PIN_DETECTED_VBUS_PAD(pin) & PORT_OC_PIN_MASK) <<
			PORTX_OC_PIN_SHIFT(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_SS_OC_MAP);
	}

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
	reg &= ~SSPX_ELPG_VCORE_DOWN(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM_1);

	usleep_range(100, 200);

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
	reg &= ~SSPX_ELPG_CLAMP_EN_EARLY(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM_1);

	usleep_range(100, 200);

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
	reg &= ~SSPX_ELPG_CLAMP_EN(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM_1);

	mutex_unlock(&padctl->lock);
	return 0;
}

static int tegra186_usb3_phy_power_off(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	struct device *dev = padctl->dev;
	int port = usb3_phy_to_port(phy);
	u32 reg;

	if (port < 0)
		return port;

	mutex_lock(&padctl->lock);

	dev_dbg(dev, "power off USB3 port %d\n", port);

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
	reg |= SSPX_ELPG_CLAMP_EN_EARLY(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM_1);

	usleep_range(100, 200);

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
	reg |= SSPX_ELPG_CLAMP_EN(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM_1);

	usleep_range(250, 350);

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
	reg |= SSPX_ELPG_VCORE_DOWN(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM_1);

	mutex_unlock(&padctl->lock);

	return 0;
}

static int tegra186_usb3_phy_enable_wakelogic(struct tegra_padctl *padctl,
					      int port)
{
	struct device *dev = padctl->dev;
	u32 reg;

	dev_dbg(dev, "enable wakelogic USB3 port %d\n", port);

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
	reg |= SSPX_ELPG_CLAMP_EN_EARLY(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM_1);

	usleep_range(100, 200);

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
	reg |= SSPX_ELPG_CLAMP_EN(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM_1);

	usleep_range(250, 350);

	return 0;
}

static int tegra186_usb3_phy_disable_wakelogic(struct tegra_padctl *padctl,
					      int port)
{
	struct device *dev = padctl->dev;
	u32 reg;

	dev_dbg(dev, "disable wakelogic USB3 port %d\n", port);

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
	reg &= ~SSPX_ELPG_CLAMP_EN_EARLY(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM_1);

	usleep_range(100, 200);

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
	reg &= ~SSPX_ELPG_CLAMP_EN(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM_1);

	return 0;
}

static int tegra186_usb3_phy_init(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port = usb3_phy_to_port(phy);

	if (port < 0)
		return port;

	mutex_lock(&padctl->lock);

	dev_dbg(padctl->dev, "phy init USB3 port %d\n", port);

	mutex_unlock(&padctl->lock);

	return 0;
}

static int tegra186_usb3_phy_exit(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	struct device *dev = padctl->dev;
	int port = usb3_phy_to_port(phy);

	if (port < 0)
		return port;

	mutex_lock(&padctl->lock);

	dev_dbg(dev, "phy exit USB3 port %d\n", port);

	mutex_unlock(&padctl->lock);

	return 0;
}

static const struct phy_ops usb3_phy_ops = {
	.init = tegra186_usb3_phy_init,
	.exit = tegra186_usb3_phy_exit,
	.power_on = tegra186_usb3_phy_power_on,
	.power_off = tegra186_usb3_phy_power_off,
	.owner = THIS_MODULE,
};
static inline bool is_usb3_phy(struct phy *phy)
{
	return phy->ops == &usb3_phy_ops;
}

static int utmi_phy_to_port(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	unsigned int i;

	for (i = 0; i < TEGRA_UTMI_PHYS; i++) {
		if (phy == padctl->utmi_phys[i])
			return i;
	}
	WARN_ON(1);

	return -EINVAL;
}

static int cdp_phy_to_port(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	unsigned int i;

	for (i = 0; i < TEGRA_CDP_PHYS; i++) {
		if (phy == padctl->cdp_phys[i])
			return i;
	}
	WARN_ON(1);

	return -EINVAL;
}

static int tegra186_utmi_phy_enable_sleepwalk(struct tegra_padctl *padctl,
				       int port, enum usb_device_speed speed)
{
	u32 reg;

	dev_dbg(padctl->dev, "enable sleepwalk UTMI port %d speed %d\n",
		port, speed);

	/* ensure sleepwalk logic is disabled */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));
	reg &= ~MASTER_ENABLE;
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));

	/* ensure sleepwalk logics are in low power mode */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));
	reg |= MASTER_CFG_SEL;
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));

	/* set debounce time */
	reg = ao_readl(padctl, XUSB_AO_USB_DEBOUNCE_DEL);
	reg &= ~UTMIP_LINE_DEB_CNT(~0);
	reg |= UTMIP_LINE_DEB_CNT(1);
	ao_writel(padctl, reg, XUSB_AO_USB_DEBOUNCE_DEL);

	/* ensure fake events of sleepwalk logic are desiabled */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));
	reg &= ~(FAKE_USBOP_VAL | FAKE_USBON_VAL |
		FAKE_USBOP_EN | FAKE_USBON_EN);
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));

	/* ensure wake events of sleepwalk logic are not latched */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));
	reg &= ~LINE_WAKEUP_EN;
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));

	/* disable wake event triggers of sleepwalk logic */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));
	reg &= ~WAKE_VAL(~0);
	reg |= WAKE_VAL_NONE;
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));

	/* power down the line state detectors of the pad */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_PAD_CFG(port));
	reg |= (USBOP_VAL_PD | USBON_VAL_PD);
	ao_writel(padctl, reg, XUSB_AO_UTMIP_PAD_CFG(port));

	/* save state per speed */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SAVED_STATE(port));
	reg &= ~SPEED(~0);
	if (speed == USB_SPEED_HIGH)
		reg |= UTMI_HS;
	else if (speed == USB_SPEED_FULL)
		reg |= UTMI_FS;
	else if (speed == USB_SPEED_LOW)
		reg |= UTMI_LS;
	else
		reg |= UTMI_RST;
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SAVED_STATE(port));

	/* enable the trigger of the sleepwalk logic */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));
	reg |= LINEVAL_WALK_EN;
	reg &= ~WAKE_WALK_EN;
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));

	/* reset the walk pointer and clear the alarm of the sleepwalk logic,
	 * as well as capture the configuration of the USB2.0 pad
	 */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_TRIGGERS(port));
	reg |= (CLR_WALK_PTR | CLR_WAKE_ALARM | CAP_CFG);
	ao_writel(padctl, reg, XUSB_AO_UTMIP_TRIGGERS(port));

	/* setup the pull-ups and pull-downs of the signals during the four
	 * stages of sleepwalk.
	 * if device is connected, program sleepwalk logic to maintain a J and
	 * keep driving K upon seeing remote wake.
	 */
	reg = (USBOP_RPD_A | USBOP_RPD_B | USBOP_RPD_C | USBOP_RPD_D);
	reg |= (USBON_RPD_A | USBON_RPD_B | USBON_RPD_C | USBON_RPD_D);
	if (speed == USB_SPEED_UNKNOWN) {
		reg |= (HIGHZ_A | HIGHZ_B | HIGHZ_C | HIGHZ_D);
	} else if ((speed == USB_SPEED_HIGH) || (speed == USB_SPEED_FULL)) {
		/* J state: D+/D- = high/low, K state: D+/D- = low/high */
		reg |= HIGHZ_A;
		reg |= (AP_A);
		reg |= (AN_B | AN_C | AN_D);
	} else if (speed == USB_SPEED_LOW) {
		/* J state: D+/D- = low/high, K state: D+/D- = high/low */
		reg |= HIGHZ_A;
		reg |= AN_A;
		reg |= (AP_B | AP_C | AP_D);
	}
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK(port));

	/* power up the line state detectors of the pad */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_PAD_CFG(port));
	reg &= ~(USBOP_VAL_PD | USBON_VAL_PD);
	ao_writel(padctl, reg, XUSB_AO_UTMIP_PAD_CFG(port));

	usleep_range(150, 200);

	/* switch the electric control of the USB2.0 pad to XUSB_AO */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_PAD_CFG(port));
	reg |= (FSLS_USE_XUSB_AO | TRK_CTRL_USE_XUSB_AO |
		RPD_CTRL_USE_XUSB_AO | RPU_USE_XUSB_AO | VREG_USE_XUSB_AO);
	ao_writel(padctl, reg, XUSB_AO_UTMIP_PAD_CFG(port));

	/* set the wake signaling trigger events */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));
	reg &= ~WAKE_VAL(~0);
	reg |= WAKE_VAL_ANY;
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));

	/* enable the wake detection */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));
	reg |= (MASTER_ENABLE | LINE_WAKEUP_EN);
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));

	return 0;
}

static int tegra186_utmi_phy_disable_sleepwalk(struct tegra_padctl *padctl,
					       int port)
{
	u32 reg;

	dev_dbg(padctl->dev, "disable sleepwalk UTMI port %d\n",  port);

	/* disable the wake detection */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));
	reg &= ~(MASTER_ENABLE | LINE_WAKEUP_EN);
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));

	/* switch the electric control of the USB2.0 pad to XUSB vcore logic */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_PAD_CFG(port));
	reg &= ~(FSLS_USE_XUSB_AO | TRK_CTRL_USE_XUSB_AO |
		RPD_CTRL_USE_XUSB_AO | RPU_USE_XUSB_AO | VREG_USE_XUSB_AO);
	ao_writel(padctl, reg, XUSB_AO_UTMIP_PAD_CFG(port));

	/* disable wake event triggers of sleepwalk logic */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));
	reg &= ~WAKE_VAL(~0);
	reg |= WAKE_VAL_NONE;
	ao_writel(padctl, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(port));

	/* power down the line state detectors of the port */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_PAD_CFG(port));
	reg |= (USBOP_VAL_PD | USBON_VAL_PD);
	ao_writel(padctl, reg, XUSB_AO_UTMIP_PAD_CFG(port));

	/* clear alarm of the sleepwalk logic */
	reg = ao_readl(padctl, XUSB_AO_UTMIP_TRIGGERS(port));
	reg |= CLR_WAKE_ALARM;
	ao_writel(padctl, reg, XUSB_AO_UTMIP_TRIGGERS(port));

	return 0;
}

static void tegra186_utmi_bias_pad_power_on(struct tegra_padctl *padctl)
{
	u32 reg;

	if (padctl->bias_pad_enable++ > 0)
		return;

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
	reg &= ~USB2_TRK_START_TIMER(~0);
	reg |= USB2_TRK_START_TIMER(0x1e);
	reg &= ~USB2_TRK_DONE_RESET_TIMER(~0);
	reg |= USB2_TRK_DONE_RESET_TIMER(0xa);
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL0);
	reg &= ~BIAS_PAD_PD;
	reg &= ~HS_SQUELCH_LEVEL(~0);
	reg |= HS_SQUELCH_LEVEL(padctl->calib.hs_squelch);
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_BIAS_PAD_CTL0);

	udelay(1);

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
	reg &= ~USB2_PD_TRK;
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
}

static void tegra186_utmi_bias_pad_power_off(struct tegra_padctl *padctl)
{
	u32 reg;

	if (WARN_ON(padctl->bias_pad_enable == 0))
		return;

	if (--padctl->bias_pad_enable > 0)
		return;

	if (!padctl->cdp_used) {
		/* only turn BIAS pad off when host CDP isn't enabled */
		reg = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL0);
		reg |= BIAS_PAD_PD;
		padctl_writel(padctl, reg, XUSB_PADCTL_USB2_BIAS_PAD_CTL0);
	}

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
	reg |= USB2_PD_TRK;
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
}

void tegra18x_phy_xusb_utmi_pad_power_on(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	if (padctl->utmi_ports[port].poweron)
		return;

	tegra186_utmi_bias_pad_power_on(padctl);

	udelay(2);

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));
	reg &= ~USB2_OTG_PD;
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL1(port));
	reg &= ~USB2_OTG_PD_DR;
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OTG_PADX_CTL1(port));

	padctl->utmi_ports[port].poweron = true;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_pad_power_on);

void tegra18x_phy_xusb_utmi_pad_power_down(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	if (!padctl->utmi_ports[port].poweron)
		return;

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));
	reg |= USB2_OTG_PD;
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL1(port));
	reg |= USB2_OTG_PD_DR;
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OTG_PADX_CTL1(port));

	udelay(2);

	tegra186_utmi_bias_pad_power_off(padctl);
	padctl->utmi_ports[port].poweron = false;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_pad_power_down);

#define oc_debug(u) \
		dev_dbg(u->dev, "%s(%d):OC_DET %#x, VBUS_OC_MAP %#x, "\
			"USB2_OC_MAP %#x, SS_OC_MAP %#x\n",\
			__func__, __LINE__,\
			padctl_readl(u, XUSB_PADCTL_OC_DET), \
			padctl_readl(u, XUSB_PADCTL_VBUS_OC_MAP), \
			padctl_readl(u, XUSB_PADCTL_USB2_OC_MAP), \
			padctl_readl(u, XUSB_PADCTL_SS_OC_MAP));

/* should only be called with a UTMI phy and with padctl->lock held */
static void tegra186_enable_vbus_oc(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port, pin;
	u32 reg;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	if (!padctl->oc_pinctrl) {
		dev_dbg(padctl->dev, "%s no OC pinctrl device\n", __func__);
		return;
	}

	if (port < 0) {
		dev_warn(padctl->dev, "%s wrong port %d\n", __func__, port);
		return;
	}

	pin = padctl->utmi_ports[port].oc_pin;
	if (pin < 0) {
		dev_dbg(padctl->dev, "%s no OC support for port %d\n", __func__,
			port);
		return;
	}

	dev_dbg(padctl->dev, "enable VBUS/OC on UTMI port %d, pin %d\n", port,
		pin);

	/* initialize OC: step 7 in PG p.1272 */
	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OC_MAP);
	reg &= ~(PORT_OC_PIN_MASK << PORTX_OC_PIN_SHIFT(port));
	reg |= OC_PIN_DETECTION_DISABLED << PORTX_OC_PIN_SHIFT(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OC_MAP);

	/* need to disable VBUS_ENABLEx_OC_MAP before enabling VBUS */
	reg = padctl_readl(padctl, XUSB_PADCTL_VBUS_OC_MAP);
	reg &= ~(VBUS_OC_MAP_MASK << VBUS_OC_MAP_SHIFT(pin));
	reg |= VBUS_OC_DETECTION_DISABLED << VBUS_OC_MAP_SHIFT(pin);
	padctl_writel(padctl, reg, XUSB_PADCTL_VBUS_OC_MAP);

	/* WAR: disable UTMIPLL power down, not needed for current clk
	 * framework */

	/* clear false OC_DETECTED VBUS_PADx */
	reg = padctl_readl(padctl, XUSB_PADCTL_OC_DET);
	reg &= ~OC_DETECTED_VBUS_PAD_MASK;
	reg |= OC_DETECTED_VBUS_PAD(pin);
	padctl_writel(padctl, reg, XUSB_PADCTL_OC_DET);

	udelay(100);

	/* WAR: enable UTMIPLL power down, not needed for current clk
	 * framework */

	/* Enable VBUS */
	reg = padctl_readl(padctl, XUSB_PADCTL_VBUS_OC_MAP);
	reg |= VBUS_ENABLE(pin);
	padctl_writel(padctl, reg, XUSB_PADCTL_VBUS_OC_MAP);

	/* vbus has been supplied to device. A finite time (>10ms) for OC
	 * detection pin to be pulled-up */
	msleep(20);

	/* check and clear if there is any stray OC */
	reg = padctl_readl(padctl, XUSB_PADCTL_OC_DET);
	if (reg & OC_DETECTED_VBUS_PAD(pin)) {
		/* clear stray OC */
		dev_dbg(padctl->dev,
			 "clear stray OC on port %d pin %d, OC_DET=%#x\n",
			 port, pin, reg);

		reg = padctl_readl(padctl, XUSB_PADCTL_VBUS_OC_MAP);
		reg &= ~VBUS_ENABLE(pin);

		reg = padctl_readl(padctl, XUSB_PADCTL_OC_DET);
		reg &= ~OC_DETECTED_VBUS_PAD_MASK;
		reg |= OC_DETECTED_VBUS_PAD(pin);
		padctl_writel(padctl, reg, XUSB_PADCTL_OC_DET);

		/* Enable VBUS back after clearing stray OC */
		reg = padctl_readl(padctl, XUSB_PADCTL_VBUS_OC_MAP);
		reg |= VBUS_ENABLE(pin);
		padctl_writel(padctl, reg, XUSB_PADCTL_VBUS_OC_MAP);
	}

	/* change the OC_MAP source and enable OC interrupt */
	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OC_MAP);
	reg &= ~(PORT_OC_PIN_MASK << PORTX_OC_PIN_SHIFT(port));
	reg |= (OC_PIN_DETECTED_VBUS_PAD(pin) & PORT_OC_PIN_MASK) <<
		PORTX_OC_PIN_SHIFT(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OC_MAP);

	reg = padctl_readl(padctl, XUSB_PADCTL_OC_DET);
	reg &= ~OC_DETECTED_VBUS_PAD_MASK;
	reg |= OC_DETECTED_INT_EN_VBUS_PAD(pin);
	padctl_writel(padctl, reg, XUSB_PADCTL_OC_DET);

	reg = padctl_readl(padctl, XUSB_PADCTL_VBUS_OC_MAP);
	reg &= ~(VBUS_OC_MAP_MASK << VBUS_OC_MAP_SHIFT(pin));
	reg |= (VBUS_OC_DETECTED_VBUS_PAD(pin) & VBUS_OC_MAP_MASK) <<
		VBUS_OC_MAP_SHIFT(pin);
	padctl_writel(padctl, reg, XUSB_PADCTL_VBUS_OC_MAP);

	oc_debug(padctl);
}

/* should only be called with a UTMI phy and with padctl->lock held */
static void tegra186_disable_vbus_oc(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port, pin;
	u32 reg;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	if (!padctl->oc_pinctrl || port < 0)
		return;

	pin = padctl->utmi_ports[port].oc_pin;
	if (pin < 0)
		return;

	dev_dbg(padctl->dev, "disable VBUS/OC on UTMI port %d, pin %d\n",
		port, pin);

	/* disable VBUS PAD interrupt for this port */
	reg = padctl_readl(padctl, XUSB_PADCTL_OC_DET);
	reg &= ~OC_DETECTED_INT_EN_VBUS_PAD(pin);
	padctl_writel(padctl, reg, XUSB_PADCTL_OC_DET);

	/* clear VBUS OC MAP, disable VBUS. Skip doing so if it's OTG port and
	 * OTG vbus always on is set. */
	reg = padctl_readl(padctl, XUSB_PADCTL_VBUS_OC_MAP);
	reg &= ~(VBUS_OC_MAP_MASK << VBUS_OC_MAP_SHIFT(pin));
	reg |= VBUS_OC_DETECTION_DISABLED << VBUS_OC_MAP_SHIFT(pin);
	reg &= ~VBUS_ENABLE(pin);
	padctl_writel(padctl, reg, XUSB_PADCTL_VBUS_OC_MAP);
}

static int tegra186_utmi_phy_power_on(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	struct device *dev = padctl->dev;
	int port = utmi_phy_to_port(phy);
	char prod_name[] = "prod_c_utmiX";
	int err;
	u32 reg;

	if (port < 0)
		return port;

	dev_dbg(dev, "power on UTMI port %d\n",  port);

	sprintf(prod_name, "prod_c_utmi%d", port);
	err = tegra_prod_set_by_name(&padctl->padctl_regs, prod_name,
				     padctl->prod_list);
	if (err) {
		dev_info(dev, "failed to apply prod for utmi pad%d (%d)\n",
			port, err);
	}

	sprintf(prod_name, "prod_c_bias");
	err = tegra_prod_set_by_name(&padctl->padctl_regs, prod_name,
				     padctl->prod_list);
	if (err)
		dev_info(dev, "failed to apply prod for bias pad (%d)\n", err);

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_PORT_CAP);
	reg &= ~(PORT_CAP_MASK << PORTX_CAP_SHIFT(port));
	if (padctl->utmi_ports[port].port_cap == CAP_DISABLED)
		reg |= (PORT_CAP_DISABLED << PORTX_CAP_SHIFT(port));
	else if (padctl->utmi_ports[port].port_cap == DEVICE_ONLY)
		reg |= (PORT_CAP_DEVICE << PORTX_CAP_SHIFT(port));
	else if (padctl->utmi_ports[port].port_cap == HOST_ONLY)
		reg |= (PORT_CAP_HOST << PORTX_CAP_SHIFT(port));
	else if (padctl->utmi_ports[port].port_cap == OTG)
		reg |= (PORT_CAP_OTG << PORTX_CAP_SHIFT(port));
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_PORT_CAP);

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));
	reg &= ~USB2_OTG_PD_ZI;
	reg |= TERM_SEL;
	reg &= ~HS_CURR_LEVEL(~0);
	if (padctl->utmi_ports[port].hs_curr_level_offset) {
		int hs_current_level;

		dev_dbg(dev, "UTMI port %d apply hs_curr_level_offset %d\n",
			port, padctl->utmi_ports[port].hs_curr_level_offset);

		hs_current_level = (int) padctl->calib.hs_curr_level[port] +
			padctl->utmi_ports[port].hs_curr_level_offset;

		if (hs_current_level < 0)
			hs_current_level = 0;
		if (hs_current_level > 0x3f)
			hs_current_level = 0x3f;

		reg |= HS_CURR_LEVEL(hs_current_level);
	} else
		reg |= HS_CURR_LEVEL(padctl->calib.hs_curr_level[port]);
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL1(port));
	reg &= ~TERM_RANGE_ADJ(~0);
	reg |= TERM_RANGE_ADJ(padctl->calib.hs_term_range_adj);
	reg &= ~RPD_CTRL(~0);
	reg |= RPD_CTRL(padctl->calib.rpd_ctrl);
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OTG_PADX_CTL1(port));

	/* enable VBUS OC support only on non-OTG port */
	if (port != padctl->utmi_otg_port_base_1 - 1) {
		mutex_lock(&padctl->lock);
		tegra186_enable_vbus_oc(phy);
		mutex_unlock(&padctl->lock);
	}

	return 0;
}

static int tegra186_utmi_phy_power_off(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port = utmi_phy_to_port(phy);

	if (port < 0)
		return port;

	dev_dbg(padctl->dev, "power off UTMI port %d\n", port);

	return 0;
}

int tegra18x_utmi_vbus_enable(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port = utmi_phy_to_port(phy);
	int rc;

	if (port < 0)
		return port;

	dev_dbg(padctl->dev, "enable vbus-%d\n", port);

	mutex_lock(&padctl->lock);

	/* only enable regulator when OC is disabled for host only ports */
	/* OC is disabled when either oc_pinctrl is NULL or oc_pin is not
	 * defined (-1)
	 */
	if (padctl->vbus[port] && (!padctl->oc_pinctrl ||
			padctl->utmi_ports[port].oc_pin < 0) &&
			padctl->utmi_ports[port].port_cap == HOST_ONLY) {
		rc = regulator_enable(padctl->vbus[port]);
		if (rc) {
			dev_err(padctl->dev, "enable port %d vbus failed %d\n",
				port, rc);
			mutex_unlock(&padctl->lock);
			return rc;
		}
	}

	mutex_unlock(&padctl->lock);

	return 0;
}
EXPORT_SYMBOL_GPL(tegra18x_utmi_vbus_enable);

static int tegra186_utmi_phy_init(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port = utmi_phy_to_port(phy);

	if (port < 0)
		return port;

	mutex_lock(&padctl->lock);

	dev_dbg(padctl->dev, "phy init UTMI port %d\n",  port);

	mutex_unlock(&padctl->lock);

	return 0;
}

static int tegra186_utmi_phy_exit(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port = utmi_phy_to_port(phy);
	int rc;

	if (port < 0)
		return port;

	dev_dbg(padctl->dev, "phy exit UTMI port %d\n",  port);

	mutex_lock(&padctl->lock);

	if (padctl->vbus[port] &&
			padctl->utmi_ports[port].port_cap == HOST_ONLY) {
		rc = regulator_disable(padctl->vbus[port]);
		if (rc) {
			dev_err(padctl->dev, "disable port %d vbus failed %d\n",
				port, rc);
			mutex_unlock(&padctl->lock);
			return rc;
		}
	}

	mutex_unlock(&padctl->lock);

	return 0;
}

static const struct phy_ops utmi_phy_ops = {
	.init = tegra186_utmi_phy_init,
	.exit = tegra186_utmi_phy_exit,
	.power_on = tegra186_utmi_phy_power_on,
	.power_off = tegra186_utmi_phy_power_off,
	.owner = THIS_MODULE,
};

static int tegra186_cdp_phy_set_cdp(struct phy *phy, bool enable)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port = cdp_phy_to_port(phy);
	u32 reg;

	dev_info(padctl->dev, "%sable UTMI port %d Tegra CDP\n",
		 enable ? "en" : "dis", port);
	if (enable) {
		reg = padctl_readl(padctl,
				   USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
		reg &= ~PD_CHG;
		padctl_writel(padctl, reg,
			      USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

		reg = padctl_readl(padctl,
				   XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));
		reg |= (USB2_OTG_PD2 | USB2_OTG_PD2_OVRD_EN);
		padctl_writel(padctl, reg,
			      XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));

		reg = padctl_readl(padctl,
				   USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
		reg |= ON_SRC_EN;
		padctl_writel(padctl, reg,
			      USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	} else {
		reg = padctl_readl(padctl,
				   USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
		reg |= PD_CHG;
		padctl_writel(padctl, reg,
			      USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

		reg = padctl_readl(padctl,
				   XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));
		reg &= ~(USB2_OTG_PD2 | USB2_OTG_PD2_OVRD_EN);
		padctl_writel(padctl, reg,
			      XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));

		reg = padctl_readl(padctl,
				   USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
		reg &= ~ON_SRC_EN;
		padctl_writel(padctl, reg,
			      USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	}

	return 0;
}

static int tegra186_cdp_phy_power_on(struct phy *phy)
{
	return tegra186_cdp_phy_set_cdp(phy, true);
}

static int tegra186_cdp_phy_power_off(struct phy *phy)
{
	return tegra186_cdp_phy_set_cdp(phy, false);
}

static const struct phy_ops cdp_phy_ops = {
	.power_on = tegra186_cdp_phy_power_on,
	.power_off = tegra186_cdp_phy_power_off,
	.owner = THIS_MODULE,
};

static inline bool is_utmi_phy(struct phy *phy)
{
	return phy->ops == &utmi_phy_ops;
}

static int hsic_phy_to_port(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	unsigned int i;

	for (i = 0; i < TEGRA_HSIC_PHYS; i++) {
		if (phy == padctl->hsic_phys[i])
			return i;
	}
	WARN_ON(1);

	return -EINVAL;
}

enum hsic_pad_pupd {
	PUPD_DISABLE = 0,
	PUPD_IDLE,
	PUPD_RESET
};

static int tegra186_hsic_phy_pupd_set(struct tegra_padctl *padctl, int pad,
				      enum hsic_pad_pupd pupd)
{
	struct device *dev = padctl->dev;
	u32 reg;

	if (pad >= 1) {
		dev_err(dev, "%s invalid HSIC pad number %u\n", __func__, pad);
		return -EINVAL;
	}

	dev_dbg(dev, "%s pad %u pupd %d\n", __func__, pad, pupd);

	reg = padctl_readl(padctl, XUSB_PADCTL_HSIC_PADX_CTL0(pad));
	reg &= ~(HSIC_RPD_DATA0 | HSIC_RPU_DATA0);
	reg &= ~(HSIC_RPU_STROBE | HSIC_RPD_STROBE);
	if (pupd == PUPD_IDLE) {
		reg |= (HSIC_RPD_DATA0 | HSIC_RPU_STROBE);
	} else if (pupd == PUPD_RESET) {
		reg |= (HSIC_RPD_DATA0 | HSIC_RPD_STROBE);
	} else if (pupd != PUPD_DISABLE) {
		dev_err(dev, "%s invalid pupd %d\n", __func__, pupd);
		return -EINVAL;
	}
	padctl_writel(padctl, reg, XUSB_PADCTL_HSIC_PADX_CTL0(pad));

	return 0;
}

static ssize_t hsic_power_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct tegra_padctl *padctl = platform_get_drvdata(pdev);
	int pad = 0;
	u32 reg;
	int on;

	reg = padctl_readl(padctl, XUSB_PADCTL_HSIC_PADX_CTL0(pad));

	if (reg & (HSIC_RPD_DATA0 | HSIC_RPD_STROBE))
		on = 0; /* bus in reset */
	else
		on = 1;

	return sprintf(buf, "%d\n", on);
}

static ssize_t hsic_power_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t n)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct tegra_padctl *padctl = platform_get_drvdata(pdev);
	struct tegra_xusb_mbox_msg msg;
	unsigned int on;
	int port;
	int rc;

	if (kstrtouint(buf, 10, &on))
		return -EINVAL;

	if (padctl->host_mode_phy_disabled) {
		dev_err(dev, "doesn't support HSIC PHY because mailbox is not available\n");
		return -EINVAL;
	}

	if (on)
		msg.cmd = MBOX_CMD_AIRPLANE_MODE_DISABLED;
	else
		msg.cmd = MBOX_CMD_AIRPLANE_MODE_ENABLED;

	port = padctl->soc->hsic_port_offset;
	msg.data = BIT(port + 1);
	rc = mbox_send_message(padctl->mbox_chan, &msg);
	if (rc < 0)
		dev_err(dev, "failed to send message to firmware %d\n", rc);

	if (on)
		rc = tegra186_hsic_phy_pupd_set(padctl, 0, PUPD_IDLE);
	else
		rc = tegra186_hsic_phy_pupd_set(padctl, 0, PUPD_RESET);

	return n;
}
static DEVICE_ATTR(hsic_power, S_IRUGO | S_IWUSR,
		   hsic_power_show, hsic_power_store);

static ssize_t otg_vbus_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct tegra_padctl *padctl = platform_get_drvdata(pdev);
	int port = padctl->utmi_otg_port_base_1 - 1;

	if (!padctl->utmi_otg_port_base_1)
		return sprintf(buf, "No UTMI OTG port\n");

	return sprintf(buf, "OTG port %d vbus always-on: %s\n",
			port, padctl->otg_vbus_alwayson ? "yes" : "no");
}

static ssize_t otg_vbus_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t n)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct tegra_padctl *padctl = platform_get_drvdata(pdev);
	int port = padctl->utmi_otg_port_base_1 - 1;
	unsigned int on;
	int err = 0;

	if (kstrtouint(buf, 10, &on))
		return -EINVAL;

	if (!padctl->utmi_otg_port_base_1) {
		dev_err(dev, "No UTMI OTG port\n");
		return -EINVAL;
	}

	if (on && !padctl->otg_vbus_alwayson) {
		err = tegra18x_phy_xusb_utmi_vbus_power_on(
				padctl->utmi_phys[port]);
		if (!err)
			padctl->otg_vbus_alwayson = true;
	} else if (!on && padctl->otg_vbus_alwayson) {
		/* pre-set this to make vbus power off really work */
		padctl->otg_vbus_alwayson = false;
		err = tegra18x_phy_xusb_utmi_vbus_power_off(
				padctl->utmi_phys[port]);
		if (!err)
			padctl->otg_vbus_alwayson = false;
		else
			padctl->otg_vbus_alwayson = true;
	}

	if (err)
		dev_err(dev, "failed to %s OTG port %d vbus always-on: %d\n",
				on ? "enable" : "disable", port, err);

	return n;
}

static DEVICE_ATTR(otg_vbus, S_IRUGO | S_IWUSR, otg_vbus_show, otg_vbus_store);

static struct attribute *padctl_attrs[] = {
	&dev_attr_hsic_power.attr,
	&dev_attr_otg_vbus.attr,
	NULL,
};
static struct attribute_group padctl_attr_group = {
	.attrs = padctl_attrs,
};

static int tegra186_hsic_phy_pretend_connected(struct tegra_padctl *padctl,
					       int port)
{
	struct device *dev = padctl->dev;
	struct tegra_xusb_mbox_msg msg;
	int rc;

	if (!padctl->hsic_ports[port].pretend_connected)
		return 0; /* pretend-connected is not enabled */

	msg.cmd = MBOX_CMD_HSIC_PRETEND_CONNECT;
	msg.data = BIT(padctl->soc->hsic_port_offset + port + 1);
	rc = mbox_send_message(padctl->mbox_chan, &msg);
	if (rc < 0)
		dev_err(dev, "failed to send message to firmware %d\n", rc);

	return rc;
}

static int tegra186_hsic_phy_enable_sleepwalk(struct tegra_padctl *padctl,
					      int port)
{
	u32 reg;

	dev_dbg(padctl->dev, "enable sleepwalk HSIC port %d\n",  port);

	/* ensure sleepwalk logic is disabled */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));
	reg &= ~MASTER_ENABLE;
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));

	/* ensure sleepwalk logics are in low power mode */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));
	reg |= MASTER_CFG_SEL;
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));

	/* set debounce time */
	reg = ao_readl(padctl, XUSB_AO_USB_DEBOUNCE_DEL);
	reg &= ~UHSIC_LINE_DEB_CNT(~0);
	reg |= UHSIC_LINE_DEB_CNT(1);
	ao_writel(padctl, reg, XUSB_AO_USB_DEBOUNCE_DEL);

	/* ensure fake events of sleepwalk logic are desiabled */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));
	reg &= ~(FAKE_STROBE_VAL | FAKE_DATA_VAL |
		FAKE_STROBE_EN | FAKE_DATA_EN);
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));

	/* ensure wake events of sleepwalk logic are not latched */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));
	reg &= ~LINE_WAKEUP_EN;
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));

	/* disable wake event triggers of sleepwalk logic */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));
	reg &= ~WAKE_VAL(~0);
	reg |= WAKE_VAL_NONE;
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));

	/* power down the line state detectors of the port */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_PAD_CFG(port));
	reg |= (STROBE_VAL_PD | DATA0_VAL_PD);
	ao_writel(padctl, reg, XUSB_AO_UHSIC_PAD_CFG(port));

	/* save state, HSIC always comes up as HS */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SAVED_STATE(port));
	reg &= ~MODE(~0);
	reg |= MODE_HS;
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SAVED_STATE(port));

	/* enable the trigger of the sleepwalk logic */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));
	reg |= (WAKE_WALK_EN | LINEVAL_WALK_EN);
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));

	/* reset the walk pointer and clear the alarm of the sleepwalk logic,
	 * as well as capture the configuration of the USB2.0 port
	 */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_TRIGGERS(port));
	reg |= (HSIC_CLR_WALK_PTR | HSIC_CLR_WAKE_ALARM | HSIC_CAP_CFG);
	ao_writel(padctl, reg, XUSB_AO_UHSIC_TRIGGERS(port));

	/* setup the pull-ups and pull-downs of the signals during the four
	 * stages of sleepwalk.
	 * maintain a HSIC IDLE and keep driving HSIC RESUME upon remote wake
	 */
	reg = (RPD_DATA0_A | RPU_DATA0_B | RPU_DATA0_C | RPU_DATA0_D);
	reg |= (RPU_STROBE_A | RPD_STROBE_B | RPD_STROBE_C | RPD_STROBE_D);
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK(port));

	/* power up the line state detectors of the port */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_PAD_CFG(port));
	reg &= ~(DATA0_VAL_PD | STROBE_VAL_PD);
	ao_writel(padctl, reg, XUSB_AO_UHSIC_PAD_CFG(port));

	usleep_range(150, 200);

	/* switch the electric control of the USB2.0 pad to XUSB_AO */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_PAD_CFG(port));
	reg |= USE_XUSB_AO;
	ao_writel(padctl, reg, XUSB_AO_UHSIC_PAD_CFG(port));

	/* set the wake signaling trigger events */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));
	reg &= ~WAKE_VAL(~0);
	reg |= WAKE_VAL_DS10;
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));

	/* enable the wake detection */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));
	reg |= (MASTER_ENABLE | LINE_WAKEUP_EN);
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));

	return 0;
}

static int tegra186_hsic_phy_disable_sleepwalk(struct tegra_padctl *padctl,
					       int port)
{
	u32 reg;

	dev_dbg(padctl->dev, "disable sleepwalk HSIC port %d\n",  port);

	/* disable the wake detection */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));
	reg &= ~(MASTER_ENABLE | LINE_WAKEUP_EN);
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));

	/* switch the electric control of the USB2.0 pad to XUSB vcore logic */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_PAD_CFG(port));
	reg &= ~USE_XUSB_AO;
	ao_writel(padctl, reg, XUSB_AO_UHSIC_PAD_CFG(port));

	/* disable wake event triggers of sleepwalk logic */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));
	reg &= ~WAKE_VAL(~0);
	reg |= WAKE_VAL_NONE;
	ao_writel(padctl, reg, XUSB_AO_UHSIC_SLEEPWALK_CFG(port));

	/* power down the line state detectors of the port */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_PAD_CFG(port));
	reg |= (STROBE_VAL_PD | DATA0_VAL_PD);
	ao_writel(padctl, reg, XUSB_AO_UHSIC_PAD_CFG(port));

	/* clear alarm of the sleepwalk logic */
	reg = ao_readl(padctl, XUSB_AO_UHSIC_TRIGGERS(port));
	reg |= HSIC_CLR_WAKE_ALARM;
	ao_writel(padctl, reg, XUSB_AO_UHSIC_TRIGGERS(port));

	return 0;
}

static int tegra186_hsic_phy_power_on(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	struct device *dev = padctl->dev;
	int port = hsic_phy_to_port(phy);
	char prod_name[] = "prod_c_hsicX";
	int rc;
	u32 reg;

	dev_dbg(dev, "power on HSIC port %d\n", port);
	if (port < 0)
		return port;

	sprintf(prod_name, "prod_c_hsic%d", port);
	rc = tegra_prod_set_by_name(&padctl->padctl_regs, prod_name,
				    padctl->prod_list);
	if (rc) {
		dev_info(dev, "failed to apply prod for hsic pad%d (%d)\n",
			 port, rc);
	}

	rc = regulator_enable(padctl->vddio_hsic);
	if (rc) {
		dev_err(dev, "enable hsic %d power failed %d\n",
			port, rc);
		return rc;
	}

	rc = clk_prepare_enable(padctl->hsic_trk_clk);
	if (rc) {
		dev_err(dev, "failed to enable HSIC tracking clock %d\n",
			rc);
	}

	reg = padctl_readl(padctl, XUSB_PADCTL_HSIC_PAD_TRK_CTL0);
	reg &= ~HSIC_TRK_START_TIMER(~0);
	reg |= HSIC_TRK_START_TIMER(0x1e);
	reg &= ~HSIC_TRK_DONE_RESET_TIMER(~0);
	reg |= HSIC_TRK_DONE_RESET_TIMER(0xa);
	padctl_writel(padctl, reg, XUSB_PADCTL_HSIC_PAD_TRK_CTL0);

	reg = padctl_readl(padctl, XUSB_PADCTL_HSIC_PADX_CTL0(port));
	reg &= ~(HSIC_PD_TX_DATA0 | HSIC_PD_TX_STROBE |
		HSIC_PD_RX_DATA0 | HSIC_PD_RX_STROBE |
		HSIC_PD_ZI_DATA0 | HSIC_PD_ZI_STROBE);
	padctl_writel(padctl, reg, XUSB_PADCTL_HSIC_PADX_CTL0(port));

	udelay(1);

	reg = padctl_readl(padctl, XUSB_PADCTL_HSIC_PAD_TRK_CTL0);
	reg &= ~HSIC_PD_TRK;
	padctl_writel(padctl, reg, XUSB_PADCTL_HSIC_PAD_TRK_CTL0);

	usleep_range(50, 60);

	clk_disable_unprepare(padctl->hsic_trk_clk);

	return 0;
}

static int tegra186_hsic_phy_power_off(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port = hsic_phy_to_port(phy);
	int rc;
	u32 reg;

	dev_dbg(padctl->dev, "power off HSIC port %d\n", port);
	if (port < 0)
		return port;

	rc = regulator_disable(padctl->vddio_hsic);
	if (rc) {
		dev_err(padctl->dev, "disable hsic %d power failed %d\n",
			port, rc);
	}
	reg = padctl_readl(padctl, XUSB_PADCTL_HSIC_PADX_CTL0(port));
	reg |= (HSIC_PD_TX_DATA0 | HSIC_PD_TX_STROBE |
		HSIC_PD_RX_DATA0 | HSIC_PD_RX_STROBE |
		HSIC_PD_ZI_DATA0 | HSIC_PD_ZI_STROBE);
	padctl_writel(padctl, reg, XUSB_PADCTL_HSIC_PADX_CTL0(port));

	reg = padctl_readl(padctl, XUSB_PADCTL_HSIC_PAD_TRK_CTL0);
	reg |= HSIC_PD_TRK;
	padctl_writel(padctl, reg, XUSB_PADCTL_HSIC_PAD_TRK_CTL0);

	return 0;
}

static int tegra186_hsic_phy_init(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port = hsic_phy_to_port(phy);

	mutex_lock(&padctl->lock);

	dev_dbg(padctl->dev, "phy init HSIC port %d\n", port);

	mutex_unlock(&padctl->lock);

	return 0;
}

static int tegra186_hsic_phy_exit(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port = hsic_phy_to_port(phy);

	mutex_lock(&padctl->lock);

	dev_dbg(padctl->dev, "phy exit HSIC port %d\n", port);

	mutex_unlock(&padctl->lock);

	return 0;
}

static const struct phy_ops hsic_phy_ops = {
	.init = tegra186_hsic_phy_init,
	.exit = tegra186_hsic_phy_exit,
	.power_on = tegra186_hsic_phy_power_on,
	.power_off = tegra186_hsic_phy_power_off,
	.owner = THIS_MODULE,
};

static inline bool is_hsic_phy(struct phy *phy)
{
	return phy->ops == &hsic_phy_ops;
}

static void tegra_xusb_phy_mbox_work(struct work_struct *work)
{
	struct tegra_padctl *padctl = mbox_work_to_padctl(work);
	struct tegra_xusb_mbox_msg *msg = &padctl->mbox_req;
	struct tegra_xusb_mbox_msg resp;
	u32 ports;
	int ret = 0;

	dev_dbg(padctl->dev, "mailbox command %d\n", msg->cmd);
	resp.cmd = 0;
	switch (msg->cmd) {
	case MBOX_CMD_START_HSIC_IDLE:
	case MBOX_CMD_STOP_HSIC_IDLE:
		ports = msg->data >> (padctl->soc->hsic_port_offset + 1);
		resp.data = msg->data;
		resp.cmd = MBOX_CMD_ACK;
		if (msg->cmd == MBOX_CMD_START_HSIC_IDLE)
			tegra186_hsic_phy_pupd_set(padctl, 0, PUPD_IDLE);
		else
			tegra186_hsic_phy_pupd_set(padctl, 0, PUPD_DISABLE);
		break;
	default:
		break;
	}

	if (resp.cmd) {
		ret = mbox_send_message(padctl->mbox_chan, &resp);
		if (ret < 0)
			dev_err(padctl->dev, "mbox_send_message failed\n");
	}
}

static bool is_phy_mbox_message(u32 cmd)
{
	switch (cmd) {
	case MBOX_CMD_START_HSIC_IDLE:
	case MBOX_CMD_STOP_HSIC_IDLE:
		return true;
	default:
		return false;
	}
}

static void tegra_xusb_phy_mbox_rx(struct mbox_client *cl, void *data)
{
	struct tegra_padctl *padctl = dev_get_drvdata(cl->dev);
	struct tegra_xusb_mbox_msg *msg = data;

	if (is_phy_mbox_message(msg->cmd)) {
		padctl->mbox_req = *msg;
		schedule_work(&padctl->mbox_req_work);
	}
}

static struct phy *tegra186_padctl_xlate(struct device *dev,
					 struct of_phandle_args *args)
{
	struct tegra_padctl *padctl = dev_get_drvdata(dev);
	unsigned int index = args->args[0];
	unsigned int phy_index;
	struct phy *phy = NULL;

	if (args->args_count <= 0)
		return ERR_PTR(-EINVAL);

	dev_dbg(dev, "%s index %d\n", __func__, index);

	if ((index >= TEGRA_PADCTL_PHY_USB3_BASE) &&
		(index < TEGRA_PADCTL_PHY_USB3_BASE + 16)) {

		phy_index = index - TEGRA_PADCTL_PHY_USB3_BASE;
		if (phy_index < TEGRA_USB3_PHYS)
			phy = padctl->usb3_phys[phy_index];

	} else if ((index >= TEGRA_PADCTL_PHY_UTMI_BASE) &&
		(index < TEGRA_PADCTL_PHY_UTMI_BASE + 16)) {

		phy_index = index - TEGRA_PADCTL_PHY_UTMI_BASE;
		if (phy_index < TEGRA_UTMI_PHYS)
			phy = padctl->utmi_phys[phy_index];

	} else if ((index >= TEGRA_PADCTL_PHY_HSIC_BASE) &&
		(index < TEGRA_PADCTL_PHY_HSIC_BASE + 16)) {

		phy_index = index - TEGRA_PADCTL_PHY_HSIC_BASE;
		if (phy_index < TEGRA_HSIC_PHYS)
			phy = padctl->hsic_phys[phy_index];

	} else if ((index >= TEGRA_PADCTL_PHY_CDP_BASE) &&
		(index < TEGRA_PADCTL_PHY_CDP_BASE + 16)) {

		phy_index = index -  TEGRA_PADCTL_PHY_CDP_BASE;
		if (phy_index < TEGRA_CDP_PHYS)
			phy = padctl->cdp_phys[phy_index];
		padctl->cdp_used = true;
	}

	return (phy) ? phy : ERR_PTR(-EINVAL);
}

static const struct pinctrl_pin_desc tegra186_pins[] = {
	PINCTRL_PIN(PIN_OTG_0,  "otg-0"),
	PINCTRL_PIN(PIN_OTG_1,  "otg-1"),
	PINCTRL_PIN(PIN_OTG_2,  "otg-2"),
	PINCTRL_PIN(PIN_HSIC_0, "hsic-0"),
	PINCTRL_PIN(PIN_USB3_0, "usb3-0"),
	PINCTRL_PIN(PIN_USB3_1, "usb3-1"),
	PINCTRL_PIN(PIN_USB3_2, "usb3-2"),
	PINCTRL_PIN(PIN_CDP_0,  "cdp-0"),
	PINCTRL_PIN(PIN_CDP_1,  "cdp-1"),
	PINCTRL_PIN(PIN_CDP_2,  "cdp-2"),
};


static const char * const tegra186_hsic_groups[] = {
	"hsic-0",
};

static const char * const tegra186_xusb_groups[] = {
	"otg-0",
	"otg-1",
	"otg-2",
	"cdp-0",
	"cdp-1",
	"cdp-2",
};

#define TEGRA186_FUNCTION(_name)					\
	{								\
		.name = #_name,						\
		.num_groups = ARRAY_SIZE(tegra186_##_name##_groups),	\
		.groups = tegra186_##_name##_groups,			\
	}

static struct tegra_padctl_function tegra186_functions[] = {
	TEGRA186_FUNCTION(hsic),
	TEGRA186_FUNCTION(xusb),
};

static const unsigned int tegra186_otg_functions[] = {
	TEGRA186_FUNC_XUSB
};

static const unsigned int tegra186_hsic_functions[] = {
	TEGRA186_FUNC_HSIC,
};

#define TEGRA186_PAD(_name, _offset, _shift, _mask, _funcs)	\
	{								\
		.name = _name,						\
		.offset = _offset,					\
		.shift = _shift,					\
		.mask = _mask,						\
		.num_funcs = ARRAY_SIZE(tegra186_##_funcs##_functions),	\
		.funcs = tegra186_##_funcs##_functions,			\
	}

static const struct tegra_padctl_pad tegra186_pads[] = {
	TEGRA186_PAD("otg-0",  0x004,  0, 0x3, otg),
	TEGRA186_PAD("otg-1",  0x004,  2, 0x3, otg),
	TEGRA186_PAD("otg-2",  0x004,  4, 0x3, otg),
	TEGRA186_PAD("hsic-0", 0x004, 20, 0x1, hsic),

};

static const char * const tegra186_supply_names[] = {
	"avdd_usb",		/* 3.3V, vddp_usb */
	"vclamp_usb",		/* 1.8V, vclamp_usb_init */
	"avdd_pll_erefeut",	/* 1.8V, pll_utmip_avdd */
};

static const struct tegra_padctl_soc tegra186_soc = {
	.num_pins = ARRAY_SIZE(tegra186_pins),
	.pins = tegra186_pins,
	.num_functions = ARRAY_SIZE(tegra186_functions),
	.functions = tegra186_functions,
	.num_pads = ARRAY_SIZE(tegra186_pads),
	.pads = tegra186_pads,
	.hsic_port_offset = 6,
	.supply_names = tegra186_supply_names,
	.num_supplies = ARRAY_SIZE(tegra186_supply_names),
	.num_oc_pins = 2,
};

static const struct of_device_id tegra_padctl_of_match[] = {
	{.compatible = "nvidia,tegra186-xusb-padctl", .data = &tegra186_soc},
	{ }
};
MODULE_DEVICE_TABLE(of, tegra_padctl_of_match);

static int tegra_xusb_read_fuse_calibration(struct tegra_padctl *padctl)
{
	unsigned int i;
	u32 reg;

	tegra_fuse_readl(FUSE_SKU_USB_CALIB_0, &reg);
	dev_info(padctl->dev, "FUSE_SKU_USB_CALIB_0 0x%x\n", reg);

	for (i = 0; i < TEGRA_UTMI_PHYS; i++) {
		padctl->calib.hs_curr_level[i] =
			(reg >> HS_CURR_LEVEL_PADX_SHIFT(i)) &
			HS_CURR_LEVEL_PAD_MASK;
	}
	padctl->calib.hs_squelch = (reg >> HS_SQUELCH_SHIFT) & HS_SQUELCH_MASK;
	padctl->calib.hs_term_range_adj = (reg >> HS_TERM_RANGE_ADJ_SHIFT) &
					HS_TERM_RANGE_ADJ_MASK;

	tegra_fuse_readl(FUSE_USB_CALIB_EXT_0, &reg);
	dev_info(padctl->dev, "FUSE_USB_CALIB_EXT_0 0x%x\n", reg);

	padctl->calib.rpd_ctrl = (reg >> RPD_CTRL_SHIFT) & RPD_CTRL_MASK;

	return 0;
}

static int tegra_xusb_select_vbus_en_state(struct tegra_padctl *padctl,
			int pin, bool tristate)
{
	int err;

	if (tristate)
		err = pinctrl_select_state(
				padctl->oc_pinctrl,
				padctl->oc_tristate_enable[pin]);
	else
		err = pinctrl_select_state(
				padctl->oc_pinctrl,
				padctl->oc_passthrough_enable[pin]);

	if (err < 0) {
		dev_err(padctl->dev,
			"setting pin %d OC state failed: %d\n",
			pin, err);
	}
	return err;
}

static int tegra_xusb_setup_usb(struct tegra_padctl *padctl)
{
	struct phy *phy;
	unsigned int i;

	for (i = 0; i < TEGRA_USB3_PHYS; i++) {
		if (padctl->usb3_ports[i].port_cap == CAP_DISABLED)
			continue;
		if (padctl->host_mode_phy_disabled &&
			(padctl->usb3_ports[i].port_cap == HOST_ONLY))
			continue; /* no mailbox support */

		phy = devm_phy_create(padctl->dev, NULL, &usb3_phy_ops);
		if (IS_ERR(phy))
			return PTR_ERR(phy);

		padctl->usb3_phys[i] = phy;
		phy_set_drvdata(phy, padctl);
	}

	for (i = 0; i < TEGRA_UTMI_PHYS; i++) {
		char reg_name[sizeof("vbus-N")];

		if (padctl->host_mode_phy_disabled &&
			(padctl->utmi_ports[i].port_cap == HOST_ONLY))
			continue; /* no mailbox support */

		sprintf(reg_name, "vbus-%d", i);
		padctl->vbus[i] = devm_regulator_get_optional(padctl->dev,
							    reg_name);
		if (IS_ERR(padctl->vbus[i])) {
			if (PTR_ERR(padctl->vbus[i]) == -EPROBE_DEFER)
				return -EPROBE_DEFER;

			padctl->vbus[i] = NULL;
		}

		phy = devm_phy_create(padctl->dev, NULL, &utmi_phy_ops);
		if (IS_ERR(phy))
			return PTR_ERR(phy);

		padctl->utmi_phys[i] = phy;
		phy_set_drvdata(phy, padctl);
	}

	if (padctl->host_mode_phy_disabled)
		goto skip_hsic; /* no mailbox support */

	padctl->vddio_hsic = devm_regulator_get(padctl->dev, "vddio-hsic");
	if (IS_ERR(padctl->vddio_hsic))
		return PTR_ERR(padctl->vddio_hsic);

	for (i = 0; i < TEGRA_HSIC_PHYS; i++) {
		phy = devm_phy_create(padctl->dev, NULL, &hsic_phy_ops);
		if (IS_ERR(phy))
			return PTR_ERR(phy);

		padctl->hsic_phys[i] = phy;
		phy_set_drvdata(phy, padctl);
	}

	for (i = 0; i < TEGRA_CDP_PHYS; i++) {
		phy = devm_phy_create(padctl->dev, NULL, &cdp_phy_ops);
		if (IS_ERR(phy))
			return PTR_ERR(phy);

		padctl->cdp_phys[i] = phy;
		phy_set_drvdata(phy, padctl);
	}

skip_hsic:
	return 0;
}

static int tegra_xusb_setup_oc(struct tegra_padctl *padctl)
{
	int i;
	bool oc_enabled = false;

	/* check oc_pin properties from USB3 and UTMI phy */
	for (i = 0; i < TEGRA_USB3_PHYS; i++) {
		if (padctl->usb3_ports[i].oc_pin >= 0) {
			oc_enabled = true;
			break;
		}
	}
	for (i = 0; i < TEGRA_UTMI_PHYS; i++) {
		if (padctl->utmi_ports[i].oc_pin >= 0) {
			oc_enabled = true;
			break;
		}
	}
	if (!oc_enabled) {
		dev_dbg(padctl->dev, "No OC pin defined for USB3/UTMI phys\n");
		return -EINVAL;
	}

	/* getting pinctrl for controlling OC pins */
	padctl->oc_pinctrl = devm_pinctrl_get(padctl->dev);
	if (IS_ERR_OR_NULL(padctl->oc_pinctrl)) {
		dev_info(padctl->dev, "Missing OC pinctrl device: %ld\n",
			 PTR_ERR(padctl->oc_pinctrl));
		return PTR_ERR(padctl->oc_pinctrl);
	}

	/* OC enable state */
	padctl->oc_tristate_enable = devm_kcalloc(padctl->dev,
			padctl->soc->num_oc_pins,
			sizeof(struct pinctrl_state *), GFP_KERNEL);
	if (!padctl->oc_tristate_enable)
		return -ENOMEM;
	for (i = 0; i < padctl->soc->num_oc_pins; i++) {
		char state_name[sizeof("vbus_enX_sfio_tristate")];

		sprintf(state_name, "vbus_en%d_sfio_tristate", i);
		padctl->oc_tristate_enable[i] = pinctrl_lookup_state(
				padctl->oc_pinctrl, state_name);
		if (IS_ERR(padctl->oc_tristate_enable[i])) {
			dev_info(padctl->dev,
				 "Missing OC pin %d pinctrl state %s: %ld\n",
				 i, state_name,
				 PTR_ERR(padctl->oc_tristate_enable[i]));
			return PTR_ERR(padctl->oc_tristate_enable[i]);
		}
	}

	/* OC enable passthrough state */
	padctl->oc_passthrough_enable = devm_kcalloc(padctl->dev,
			padctl->soc->num_oc_pins,
			sizeof(struct pinctrl_state *), GFP_KERNEL);
	if (!padctl->oc_passthrough_enable)
		return -ENOMEM;
	for (i = 0; i < padctl->soc->num_oc_pins; i++) {
		char state_name[sizeof("vbus_enX_sfio_passthrough")];

		sprintf(state_name, "vbus_en%d_sfio_passthrough", i);
		padctl->oc_passthrough_enable[i] = pinctrl_lookup_state(
				padctl->oc_pinctrl, state_name);
		if (IS_ERR(padctl->oc_passthrough_enable[i])) {
			dev_info(padctl->dev,
				 "Missing OC pin %d pinctrl state %s: %ld\n",
				 i, state_name,
				 PTR_ERR(padctl->oc_passthrough_enable[i]));
			return PTR_ERR(padctl->oc_passthrough_enable[i]);
		}
	}

	/* OC disable state */
	padctl->oc_disable = devm_kcalloc(padctl->dev,
			padctl->soc->num_oc_pins,
			sizeof(struct pinctrl_state *), GFP_KERNEL);
	if (!padctl->oc_disable)
		return -ENOMEM;
	for (i = 0; i < padctl->soc->num_oc_pins; i++) {
		char state_name[sizeof("vbus_enX_default")];

		sprintf(state_name, "vbus_en%d_default", i);
		padctl->oc_disable[i] = pinctrl_lookup_state(
				padctl->oc_pinctrl, state_name);
		if (IS_ERR(padctl->oc_disable[i])) {
			dev_info(padctl->dev,
				 "Missing OC pin %d pinctrl state %s: %ld\n",
				 i, state_name,
				 PTR_ERR(padctl->oc_disable[i]));
			return PTR_ERR(padctl->oc_disable[i]);
		}
	}

	return 0;
}

#ifdef DEBUG
#define reg_dump(_dev, _base, _reg) \
	dev_dbg(_dev, "%s @%x = 0x%x\n", #_reg, _reg, ioread32(_base + _reg))
#else
#define reg_dump(_dev, _base, _reg)	do {} while (0)
#endif

/* initializations to be done at cold boot and SC7 exit */
static void tegra186_padctl_init(struct tegra_padctl *padctl)
{
	int i;
	u32 reg;

	for (i = 0; i < TEGRA_UTMI_PHYS; i++) {
		reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL1(i));
		reg |= PD_VREG;
		padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL1(i));

		if (padctl->utmi_ports[i].port_cap == CAP_DISABLED) {
			reg = ao_readl(padctl, XUSB_AO_UTMIP_PAD_CFG(i));
			reg |= (E_DPD_OVRD_EN | E_DPD_OVRD_VAL);
			ao_writel(padctl, reg, XUSB_AO_UTMIP_PAD_CFG(i));
		}
	}
}

static void tegra186_padctl_save(struct tegra_padctl *padctl)
{
	padctl->padctl_context.vbus_id = padctl_readl(padctl, USB2_VBUS_ID);
	padctl->padctl_context.usb2_pad_mux =
				padctl_readl(padctl, XUSB_PADCTL_USB2_PAD_MUX);
	padctl->padctl_context.usb2_port_cap =
				padctl_readl(padctl, XUSB_PADCTL_USB2_PORT_CAP);
	padctl->padctl_context.ss_port_cap =
				padctl_readl(padctl, XUSB_PADCTL_SS_PORT_CAP);
}

static void tegra186_padctl_restore(struct tegra_padctl *padctl)
{
	padctl_writel(padctl, padctl->padctl_context.usb2_pad_mux,
		      XUSB_PADCTL_USB2_PAD_MUX);
	padctl_writel(padctl, padctl->padctl_context.usb2_port_cap,
		      XUSB_PADCTL_USB2_PORT_CAP);
	padctl_writel(padctl, padctl->padctl_context.ss_port_cap,
		      XUSB_PADCTL_SS_PORT_CAP);
	padctl_writel(padctl, padctl->padctl_context.vbus_id, USB2_VBUS_ID);
}

static int tegra186_padctl_suspend(struct device *dev)
{
	struct tegra_padctl *padctl = dev_get_drvdata(dev);

	dev_dbg(dev, "%s\n", __func__);

	tegra186_padctl_save(padctl);

	return 0;
}

static int tegra186_padctl_resume(struct device *dev)
{
	struct tegra_padctl *padctl = dev_get_drvdata(dev);

	dev_dbg(dev, "%s\n", __func__);

	tegra186_padctl_init(padctl);
	tegra186_padctl_restore(padctl);

	return 0;
}

static const struct dev_pm_ops tegra186_padctl_pm_ops = {
	.suspend_noirq = tegra186_padctl_suspend,
	.resume_noirq = tegra186_padctl_resume,
};

static int tegra186_padctl_probe(struct platform_device *pdev)
{
	struct tegra_padctl *padctl;
	const struct of_device_id *match;
	struct device *dev = &pdev->dev;
	struct resource *res;
	int err;
	int i;

	padctl = devm_kzalloc(dev, sizeof(*padctl), GFP_KERNEL);
	if (!padctl)
		return -ENOMEM;

	platform_set_drvdata(pdev, padctl);
	mutex_init(&padctl->lock);
	padctl->dev = dev;

	match = of_match_node(tegra_padctl_of_match, pdev->dev.of_node);
	if (!match)
		return -ENODEV;
	padctl->soc = match->data;

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "padctl");
	padctl->padctl_regs = devm_ioremap_resource(dev, res);
	if (IS_ERR(padctl->padctl_regs))
		return PTR_ERR(padctl->padctl_regs);
	dev_info(dev, "padctl mmio start %pa end %pa\n",
		 &res->start, &res->end);

	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ao");
	padctl->ao_regs = devm_ioremap_resource(dev, res);
	if (IS_ERR(padctl->ao_regs))
		return PTR_ERR(padctl->ao_regs);
	dev_info(dev, "ao mmio start %pa end %pa\n", &res->start, &res->end);

	padctl->prod_list = devm_tegra_prod_get(&pdev->dev);
	if (IS_ERR(padctl->prod_list)) {
		dev_warn(&pdev->dev, "Prod-settings not available\n");
		padctl->prod_list = NULL;
	}

	if (tegra_platform_is_silicon()) {
		err = tegra_xusb_read_fuse_calibration(padctl);
		if (err < 0)
			return err;
	}

	/* overcurrent disabled by default */
	for (i = 0; i < TEGRA_USB3_PHYS; i++)
		padctl->usb3_ports[i].oc_pin = -1;
	for (i = 0; i < TEGRA_UTMI_PHYS; i++)
		padctl->utmi_ports[i].oc_pin = -1;

	padctl->padctl_rst = devm_reset_control_get(dev, "padctl_rst");
	if (IS_ERR(padctl->padctl_rst)) {
		dev_err(padctl->dev, "failed to get padctl reset\n");
		return PTR_ERR(padctl->padctl_rst);
	}

	padctl->xusb_clk = devm_clk_get(dev, "xusb_clk");
	if (IS_ERR(padctl->xusb_clk)) {
		dev_err(dev, "failed to get xusb_clk clock\n");
		return PTR_ERR(padctl->xusb_clk);
	}

	padctl->utmipll = devm_clk_get(dev, "utmipll");
	if (IS_ERR(padctl->utmipll)) {
		dev_err(dev, "failed to get utmipll clock\n");
		return PTR_ERR(padctl->utmipll);
	}

	padctl->usb2_trk_clk = devm_clk_get(dev, "usb2_trk");
	if (IS_ERR(padctl->usb2_trk_clk)) {
		dev_err(dev, "failed to get usb2_trk clock\n");
		return PTR_ERR(padctl->usb2_trk_clk);
	}

	padctl->hsic_trk_clk = devm_clk_get(dev, "hsic_trk");
	if (IS_ERR(padctl->hsic_trk_clk)) {
		dev_err(dev, "failed to get hsic_trk clock\n");
		return PTR_ERR(padctl->hsic_trk_clk);
	}

	err = tegra186_padctl_regulators_init(padctl);
	if (err < 0)
		return err;

	err = regulator_bulk_enable(padctl->soc->num_supplies,
				    padctl->supplies);
	if (err) {
		dev_err(dev, "failed to enable regulators %d\n", err);
		return err;
	}

	err = clk_prepare_enable(padctl->xusb_clk);
	if (err) {
		dev_err(dev, "failed to enable xusb_clk %d\n", err);
		goto disable_regulators;
	}

	err = clk_prepare_enable(padctl->utmipll);
	if (err) {
		dev_err(dev, "failed to enable UTMIPLL %d\n", err);
		goto disable_xusb_clk;
	}

	err = clk_prepare_enable(padctl->usb2_trk_clk);
	if (err) {
		dev_err(dev, "failed to enable USB2 tracking clock %d\n",
			err);
		goto disable_utmipll;
	}

	err = reset_control_deassert(padctl->padctl_rst);
	if (err) {
		dev_err(dev, "failed to deassert padctl_rst %d\n", err);
		goto disable_usb2_trk;
	}

	memset(&padctl->desc, 0, sizeof(padctl->desc));
	padctl->desc.name = dev_name(dev);
	padctl->desc.pins = padctl->soc->pins;
	padctl->desc.npins = padctl->soc->num_pins;
	padctl->desc.pctlops = &tegra_xusb_padctl_pinctrl_ops;
	padctl->desc.pmxops = &tegra186_padctl_pinmux_ops;
	padctl->desc.confops = &tegra_padctl_pinconf_ops;
	padctl->desc.owner = THIS_MODULE;

	padctl->pinctrl = pinctrl_register(&padctl->desc, &pdev->dev, padctl);
	if (!padctl->pinctrl) {
		dev_err(&pdev->dev, "failed to register pinctrl\n");
		err = -ENODEV;
		goto assert_padctl_rst;
	}

	INIT_WORK(&padctl->mbox_req_work, tegra_xusb_phy_mbox_work);
	padctl->mbox_client.dev = dev;
	padctl->mbox_client.tx_block = true;
	padctl->mbox_client.tx_tout = 0;
	padctl->mbox_client.rx_callback = tegra_xusb_phy_mbox_rx;
	padctl->mbox_chan = mbox_request_channel(&padctl->mbox_client, 0);
	if (IS_ERR(padctl->mbox_chan)) {
		err = PTR_ERR(padctl->mbox_chan);
		if (err == -EPROBE_DEFER) {
			dev_info(&pdev->dev, "mailbox is not ready yet\n");
			goto unregister;
		} else {
			dev_warn(&pdev->dev,
				 "failed to get mailbox, USB Host PHY support disabled\n");
			padctl->host_mode_phy_disabled = true;
		}
	}

	err = tegra_xusb_setup_usb(padctl);
	if (err)
		goto free_mailbox;

	tegra186_padctl_init(padctl);

	err = tegra_xusb_setup_oc(padctl);
	if (err)
		padctl->oc_pinctrl = NULL;
	else
		dev_info(&pdev->dev, "VBUS over-current detection enabled\n");

	/* when oc_pinctrl is not NULL, over-current detection is enabled
	 * for at least one port */
	if (padctl->oc_pinctrl)
		/* switch VBUS pin states to enable OC */
		for (i = 0; i < TEGRA_UTMI_PHYS; i++) {
			int ocpin = padctl->utmi_ports[i].oc_pin;
			bool isotg =
				(padctl->utmi_ports[i].port_cap == OTG);
			if (ocpin >= 0) {
				/* this OC pin is in use, enable the pin
				 * as SFIO input pin for OC detection,
				 * for OTG port, the default state is
				 * device mode and VBUS off.
				 */
				err = tegra_xusb_select_vbus_en_state(
						padctl, ocpin, !isotg);
				if (err < 0)
					goto restore_oc_pin;
			}
		}

	padctl->provider = devm_of_phy_provider_register(dev,
					tegra186_padctl_xlate);
	if (IS_ERR(padctl->provider)) {
		err = PTR_ERR(padctl->provider);
		dev_err(&pdev->dev, "failed to register PHYs: %d\n", err);
		goto restore_oc_pin;
	}

	err = sysfs_create_group(&pdev->dev.kobj, &padctl_attr_group);
	if (err) {
		dev_err(&pdev->dev, "cannot create sysfs group: %d\n", err);
		goto restore_oc_pin;
	}

	return 0;

restore_oc_pin:
	if (padctl->oc_pinctrl)
		for (i--; i >= 0; i--) {
			err = pinctrl_select_state(padctl->oc_pinctrl,
						     padctl->oc_disable[i]);
			if (err < 0)
				dev_err(dev,
					"set pin %d OC disable failed: %d\n",
					i, err);
		}


free_mailbox:
	if (!IS_ERR(padctl->mbox_chan)) {
		cancel_work_sync(&padctl->mbox_req_work);
		mbox_free_channel(padctl->mbox_chan);
	}
unregister:
	pinctrl_unregister(padctl->pinctrl);
assert_padctl_rst:
	reset_control_assert(padctl->padctl_rst);
disable_usb2_trk:
	clk_disable_unprepare(padctl->usb2_trk_clk);
disable_utmipll:
	clk_disable_unprepare(padctl->utmipll);
disable_xusb_clk:
	clk_disable_unprepare(padctl->xusb_clk);
disable_regulators:
	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);

	return err;
}

static int tegra186_padctl_remove(struct platform_device *pdev)
{
	struct tegra_padctl *padctl = platform_get_drvdata(pdev);
	int i;
	int err;

	/* switch all VBUS_ENx pins back to default state */
	if (padctl->oc_pinctrl)
		for (i = 0; i < padctl->soc->num_oc_pins; i++) {
			err = pinctrl_select_state(padctl->oc_pinctrl,
					     padctl->oc_disable[i]);
			if (err < 0)
				dev_err(&pdev->dev,
					"set pin %d OC disable failed: %d\n",
					i, err);
		}

	sysfs_remove_group(&pdev->dev.kobj, &padctl_attr_group);

	if (!IS_ERR(padctl->mbox_chan)) {
		cancel_work_sync(&padctl->mbox_req_work);
		mbox_free_channel(padctl->mbox_chan);
	}

	pinctrl_unregister(padctl->pinctrl);

	reset_control_assert(padctl->padctl_rst);
	clk_disable_unprepare(padctl->usb2_trk_clk);
	clk_disable_unprepare(padctl->utmipll);
	clk_disable_unprepare(padctl->xusb_clk);

	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
	padctl->cdp_used = false;

	return 0;
}

static struct platform_driver tegra186_padctl_driver = {
	.driver = {
		.name = "tegra186-padctl",
		.of_match_table = tegra_padctl_of_match,
		.pm = &tegra186_padctl_pm_ops,
	},
	.probe = tegra186_padctl_probe,
	.remove = tegra186_padctl_remove,
};
module_platform_driver(tegra186_padctl_driver);

/* Tegra Generic PHY Extensions */
int tegra18x_phy_xusb_enable_sleepwalk(struct phy *phy,
				    enum usb_device_speed speed)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port;

	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_utmi_phy_enable_sleepwalk(padctl, port, speed);
	} else if (is_hsic_phy(phy)) {
		port = hsic_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_hsic_phy_enable_sleepwalk(padctl, port);
	} else if (is_usb3_phy(phy)) {
		port = usb3_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_usb3_phy_enable_wakelogic(padctl, port);
	} else
		return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_enable_sleepwalk);

int tegra18x_phy_xusb_disable_sleepwalk(struct phy *phy)
{
	struct tegra_padctl *padctl = phy_get_drvdata(phy);
	int port;

	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_utmi_phy_disable_sleepwalk(padctl, port);
	} else if (is_hsic_phy(phy)) {
		port = hsic_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_hsic_phy_disable_sleepwalk(padctl, port);
	} else if (is_usb3_phy(phy)) {
		port = usb3_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_usb3_phy_disable_wakelogic(padctl, port);
	} else
		return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_disable_sleepwalk);

static int tegra186_padctl_vbus_override(struct tegra_padctl *padctl,
					 bool on)
{
	u32 reg;

	reg = padctl_readl(padctl, USB2_VBUS_ID);
	if (on)
		reg |= VBUS_OVERRIDE;
	else
		reg &= ~VBUS_OVERRIDE;
	padctl_writel(padctl, reg, USB2_VBUS_ID);

	return 0;
}

int tegra18x_phy_xusb_set_vbus_override(struct phy *phy)
{
	struct tegra_padctl *padctl;

	if (!phy)
		return 0;

	padctl = phy_get_drvdata(phy);

	return tegra186_padctl_vbus_override(padctl, true);
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_set_vbus_override);

int tegra18x_phy_xusb_clear_vbus_override(struct phy *phy)
{
	struct tegra_padctl *padctl;

	if (!phy)
		return 0;

	padctl = phy_get_drvdata(phy);

	return tegra186_padctl_vbus_override(padctl, false);
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_clear_vbus_override);

static int tegra186_padctl_id_override(struct tegra_padctl *padctl,
					 bool grounded)
{
	u32 reg;

	reg = padctl_readl(padctl, USB2_VBUS_ID);
	if (grounded) {
		reg &= ~ID_OVERRIDE(~0);
		reg |= ID_OVERRIDE_GROUNDED;
	} else {
		reg &= ~ID_OVERRIDE(~0);
		reg |= ID_OVERRIDE_FLOATING;
	}
	padctl_writel(padctl, reg, USB2_VBUS_ID);

	return 0;
}

int tegra18x_phy_xusb_set_id_override(struct phy *phy)
{
	struct tegra_padctl *padctl;

	if (!phy)
		return 0;

	padctl = phy_get_drvdata(phy);

	return tegra186_padctl_id_override(padctl, true);
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_set_id_override);

int tegra18x_phy_xusb_clear_id_override(struct phy *phy)
{
	struct tegra_padctl *padctl;

	if (!phy)
		return 0;

	padctl = phy_get_drvdata(phy);

	return tegra186_padctl_id_override(padctl, false);
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_clear_id_override);

static enum tegra_xusb_vbus_rid tegra_phy_xusb_parse_rid(u32 rid_value)
{
	rid_value &= RID_MASK;

	if (rid_value == IDDIG)
		return VBUS_ID_RID_FLOAT;
	else if (rid_value == IDDIG_A)
		return VBUS_ID_RID_A;
	else if (rid_value == IDDIG_B)
		return VBUS_ID_RID_B;
	else if (rid_value == IDDIG_C)
		return VBUS_ID_RID_C;
	else if (rid_value == 0)
		return VBUS_ID_RID_GND;

	return VBUS_ID_RID_UNDEFINED;
}

bool tegra18x_phy_xusb_has_otg_cap(struct phy *phy)
{
	struct tegra_padctl *padctl;

	if (!phy)
		return false;

	padctl = phy_get_drvdata(phy);
	if (is_utmi_phy(phy)) {
		if ((padctl->utmi_otg_port_base_1) &&
		    padctl->utmi_phys[padctl->utmi_otg_port_base_1 - 1] == phy)
			return true;
	} else if (is_usb3_phy(phy)) {
		if ((padctl->usb3_otg_port_base_1) &&
		    padctl->usb3_phys[padctl->usb3_otg_port_base_1 - 1] == phy)
			return true;
	}

	return false;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_has_otg_cap);

static int tegra186_usb3_phy_set_wake(struct tegra_padctl *padctl,
				      int port, bool enable)
{
	u32 reg;

	mutex_lock(&padctl->lock);
	if (enable) {
		dev_dbg(padctl->dev, "enable USB3 port %d wake\n", port);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg |= SS_PORT_WAKEUP_EVENT(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);

		usleep_range(10, 20);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg |= SS_PORT_WAKE_INTERRUPT_ENABLE(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);
	} else {
		dev_dbg(padctl->dev, "disable USB3 port %d wake\n", port);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg &= ~SS_PORT_WAKE_INTERRUPT_ENABLE(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);

		usleep_range(10, 20);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg |= SS_PORT_WAKEUP_EVENT(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);
	}
	mutex_unlock(&padctl->lock);

	return 0;
}

static int tegra186_utmi_phy_set_wake(struct tegra_padctl *padctl,
				      int port, bool enable)
{
	u32 reg;

	mutex_lock(&padctl->lock);
	if (enable) {
		dev_dbg(padctl->dev, "enable UTMI port %d wake\n", port);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg |= USB2_PORT_WAKEUP_EVENT(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);

		usleep_range(10, 20);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg |= USB2_PORT_WAKE_INTERRUPT_ENABLE(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);

	} else {
		dev_dbg(padctl->dev, "disable UTMI port %d wake\n", port);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg &= ~USB2_PORT_WAKE_INTERRUPT_ENABLE(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);

		usleep_range(10, 20);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg |= USB2_PORT_WAKEUP_EVENT(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);
	}
	mutex_unlock(&padctl->lock);

	return 0;
}

static int tegra186_hsic_phy_set_wake(struct tegra_padctl *padctl,
				      int port, bool enable)
{
	u32 reg;

	mutex_lock(&padctl->lock);
	if (enable) {
		dev_dbg(padctl->dev, "enable HSIC port %d wake\n", port);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg |= USB2_HSIC_PORT_WAKEUP_EVENT(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);

		usleep_range(10, 20);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg |= USB2_HSIC_PORT_WAKE_INTERRUPT_ENABLE(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);

	} else {
		dev_dbg(padctl->dev, "disable HSIC port %d wake\n", port);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg &= ~USB2_HSIC_PORT_WAKE_INTERRUPT_ENABLE(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);

		usleep_range(10, 20);

		reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
		reg &= ~ALL_WAKE_EVENTS;
		reg |= USB2_HSIC_PORT_WAKEUP_EVENT(port);
		padctl_writel(padctl, reg, XUSB_PADCTL_ELPG_PROGRAM);
	}
	mutex_unlock(&padctl->lock);

	return 0;
}

int tegra18x_phy_xusb_enable_wake(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;

	if (!phy)
		return 0;

	padctl = phy_get_drvdata(phy);

	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_utmi_phy_set_wake(padctl, port, true);
	} else if (is_hsic_phy(phy)) {
		port = hsic_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_hsic_phy_set_wake(padctl, port, true);
	} else if (is_usb3_phy(phy)) {
		port = usb3_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_usb3_phy_set_wake(padctl, port, true);
	} else
		return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_enable_wake);

int tegra18x_phy_xusb_disable_wake(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;

	if (!phy)
		return 0;

	padctl = phy_get_drvdata(phy);

	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_utmi_phy_set_wake(padctl, port, false);
	} else if (is_hsic_phy(phy)) {
		port = hsic_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_hsic_phy_set_wake(padctl, port, false);
	} else if (is_usb3_phy(phy)) {
		port = usb3_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_usb3_phy_set_wake(padctl, port, false);
	}
		return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_disable_wake);


static int tegra186_usb3_phy_remote_wake_detected(struct tegra_padctl *padctl,
						  int port)
{
	u32 reg;

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
	if ((reg & SS_PORT_WAKE_INTERRUPT_ENABLE(port)) &&
			(reg & SS_PORT_WAKEUP_EVENT(port)))
		return true;
	else
		return false;
}

static int tegra186_utmi_phy_remote_wake_detected(struct tegra_padctl *padctl,
						  int port)
{
	u32 reg;

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
	if ((reg & USB2_PORT_WAKE_INTERRUPT_ENABLE(port)) &&
			(reg & USB2_PORT_WAKEUP_EVENT(port)))
		return true;
	else
		return false;
}

static int tegra186_hsic_phy_remote_wake_detected(struct tegra_padctl *padctl,
						  int port)
{
	u32 reg;

	reg = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
	if ((reg & USB2_HSIC_PORT_WAKE_INTERRUPT_ENABLE(port)) &&
			(reg & USB2_HSIC_PORT_WAKEUP_EVENT(port)))
		return true;
	else
		return false;
}

int tegra18x_phy_xusb_remote_wake_detected(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;

	if (!phy)
		return 0;

	padctl = phy_get_drvdata(phy);

	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_utmi_phy_remote_wake_detected(padctl, port);
	} else if (is_hsic_phy(phy)) {
		port = hsic_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_hsic_phy_remote_wake_detected(padctl, port);
	} else if (is_usb3_phy(phy)) {
		port = usb3_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_usb3_phy_remote_wake_detected(padctl, port);
	}
		return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_remote_wake_detected);


int tegra18x_phy_xusb_pretend_connected(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;

	if (!phy)
		return 0;

	padctl = phy_get_drvdata(phy);

	/* applicable to HSIC only */
	if (is_hsic_phy(phy)) {
		port = hsic_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_hsic_phy_pretend_connected(padctl, port);
	}

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_pretend_connected);

void tegra18x_phy_xusb_set_dcd_debounce_time(struct phy *phy, u32 val)
{
	struct tegra_padctl *padctl;
	u32 reg;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);

	reg = padctl_readl(padctl,
			XUSB_PADCTL_USB2_BATTERY_CHRG_TDCD_DBNC_TIMER_0);
	reg &= ~TDCD_DBNC(~0);
	reg |= TDCD_DBNC(val);
	padctl_writel(padctl, reg,
			XUSB_PADCTL_USB2_BATTERY_CHRG_TDCD_DBNC_TIMER_0);
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_set_dcd_debounce_time);

void tegra18x_phy_xusb_utmi_pad_charger_detect_on(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	/* power up necessary stuff */
	tegra18x_phy_xusb_utmi_pad_power_on(phy);

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));
	reg &= ~USB2_OTG_PD_ZI;
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));
	reg |= (USB2_OTG_PD2 | USB2_OTG_PD2_OVRD_EN);
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));

	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg &= ~PD_CHG;
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

	/* Set DP/DN Pull up/down to zero by default */
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));
	reg &= ~(USBOP_RPD_OVRD_VAL | USBOP_RPU_OVRD_VAL |
		 USBON_RPD_OVRD_VAL | USBON_RPU_OVRD_VAL);
	reg |= (USBOP_RPD_OVRD | USBOP_RPU_OVRD |
		USBON_RPD_OVRD | USBON_RPU_OVRD);
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));

	/* Disable DP/DN as src/sink */
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg &= ~(OP_SRC_EN | ON_SINK_EN |
		 ON_SRC_EN | OP_SINK_EN);
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_pad_charger_detect_on);

void tegra18x_phy_xusb_utmi_pad_charger_detect_off(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));
	reg &= ~(USBOP_RPD_OVRD | USBOP_RPU_OVRD |
		 USBON_RPD_OVRD | USBON_RPU_OVRD);
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));

	/* power down necessary stuff */
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg |= PD_CHG;
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));
	reg &= ~(USB2_OTG_PD2 | USB2_OTG_PD2_OVRD_EN);
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));

	tegra18x_phy_xusb_utmi_pad_power_down(phy);
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_pad_charger_detect_off);

void tegra18x_phy_xusb_utmi_pad_enable_detect_filters(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg |= (VDCD_DET_FILTER_EN | VDAT_DET_FILTER_EN |
		ZIP_FILTER_EN | ZIN_FILTER_EN);
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_pad_enable_detect_filters);

void tegra18x_phy_xusb_utmi_pad_disable_detect_filters(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg &= ~(VDCD_DET_FILTER_EN | VDAT_DET_FILTER_EN |
		 ZIP_FILTER_EN | ZIN_FILTER_EN);
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_pad_disable_detect_filters);

void tegra18x_phy_xusb_utmi_pad_set_protection_level(struct phy *phy, int level,
						  enum tegra_vbus_dir dir)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));
	if (level < 0) {
		/* disable pad protection */
		reg |= PD_VREG;
		reg &= ~VREG_LEV(~0);
		reg &= ~VREG_DIR(~0);
	} else {
		reg &= ~PD_VREG;

		reg &= ~VREG_DIR(~0);
		if (padctl->utmi_ports[port].port_cap == HOST_ONLY ||
				dir == TEGRA_VBUS_SOURCE)
			reg |= VREG_DIR_OUT;
		else if (padctl->utmi_ports[port].port_cap == DEVICE_ONLY ||
				dir == TEGRA_VBUS_SINK)
			reg |= VREG_DIR_IN;
		reg &= ~VREG_LEV(~0);
		reg |= VREG_LEV(level);
	}
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_pad_set_protection_level);

bool tegra18x_phy_xusb_utmi_pad_dcd(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;
	int dcd_timeout_ms = 0;
	bool ret = false;

	if (!phy)
		return false;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	/* data contact detection */
	/* Turn on IDP_SRC */
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg |= OP_I_SRC_EN;
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

	/* Turn on D- pull-down resistor */
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));
	reg |= USBON_RPD_OVRD_VAL;
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));

	/* Wait for TDCD_DBNC */
	usleep_range(10000, 120000);

	while (dcd_timeout_ms < TDCD_TIMEOUT_MS) {
		reg = padctl_readl(padctl,
				   USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
		if (reg & DCD_DETECTED) {
			dev_dbg(padctl->dev, "USB2 port %d DCD successful\n",
				port);
			ret = true;
			break;
		}
		usleep_range(20000, 22000);
		dcd_timeout_ms += 22;
	}

	if (!ret)
		dev_info(padctl->dev, "%s: DCD timeout %d ms\n", __func__,
			 dcd_timeout_ms);

	/* Turn off IP_SRC, clear DCD DETECTED*/
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg &= ~OP_I_SRC_EN;
	reg |= DCD_DETECTED;
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

	/* Turn off D- pull-down resistor */
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));
	reg &= ~USBON_RPD_OVRD_VAL;
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));

	return ret;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_pad_dcd);

u32 tegra18x_phy_xusb_noncompliant_div_detect(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;

	if (!phy)
		return 0;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));
	reg |= DIV_DET_EN;
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));

	udelay(10);

	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));
	reg &= ~DIV_DET_EN;
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL1(port));

	return reg;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_noncompliant_div_detect);

bool tegra18x_phy_xusb_utmi_pad_primary_charger_detect(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;
	bool ret;

	if (!phy)
		return false;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	/* Source D+ to D- */
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg |= OP_SRC_EN | ON_SINK_EN;
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

	/* Wait for TVDPSRC_ON */
	msleep(40);

	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	ret = !!(reg & VDAT_DET);

	/* Turn off OP_SRC, ON_SINK, clear VDAT, ZIN status change */
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg &= ~(OP_SRC_EN | ON_SINK_EN);
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

	return ret;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_pad_primary_charger_detect);

bool tegra18x_phy_xusb_utmi_pad_secondary_charger_detect(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	u32 reg;
	bool ret;

	if (!phy)
		return false;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	/* Source D- to D+ */
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg |= ON_SRC_EN | OP_SINK_EN;
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

	/* Wait for TVDPSRC_ON */
	msleep(40);

	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	ret = !(reg & VDAT_DET);

	/* Turn off ON_SRC, OP_SINK, clear VDAT, ZIP status change */
	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	reg &= ~(ON_SRC_EN | OP_SINK_EN);
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

	return ret;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_pad_secondary_charger_detect);

/*
 * This function will fource vbus on whatever under
 * over-current SFIO or regulator GPIO control,
 * and also without caring about regulator refcnt.
 */
int tegra18x_phy_xusb_utmi_vbus_power_on(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	int rc = 0;
	int status;

	if (!phy)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	mutex_lock(&padctl->lock);
	if (padctl->oc_pinctrl && padctl->utmi_ports[port].oc_pin >= 0) {
		tegra_xusb_select_vbus_en_state(padctl,
			padctl->utmi_ports[port].oc_pin, true);
		tegra186_enable_vbus_oc(padctl->utmi_phys[port]);
	} else {
		status = regulator_is_enabled(padctl->vbus[port]);
		if (padctl->vbus[port] && !status) {
			rc = regulator_enable(padctl->vbus[port]);
			if (rc)
				dev_err(padctl->dev, "enable port %d vbus failed %d\n",
					port, rc);
		}
		dev_dbg(padctl->dev, "%s: port %d regulator status: %d->%d\n",
			 __func__, port, status,
			 regulator_is_enabled(padctl->vbus[port]));
	}
	mutex_unlock(&padctl->lock);
	return rc;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_vbus_power_on);

/*
 * This function will fource vbus off whatever under
 * over-current SFIO or regulator GPIO control,
 * and also without caring about regulator refcnt;
 * the only exception is for 'otg vbus always on' case.
 */
int tegra18x_phy_xusb_utmi_vbus_power_off(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	int rc = 0;
	int status;

	if (!phy)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);
	port = utmi_phy_to_port(phy);

	if (port == padctl->utmi_otg_port_base_1 - 1
			&& padctl->otg_vbus_alwayson) {
		dev_dbg(padctl->dev, "%s: port %d vbus cannot off due to alwayson\n",
			 __func__, port);
		return -EINVAL;
	}

	mutex_lock(&padctl->lock);
	if (padctl->oc_pinctrl && padctl->utmi_ports[port].oc_pin >= 0) {
		tegra_xusb_select_vbus_en_state(padctl,
			padctl->utmi_ports[port].oc_pin, false);
		tegra186_disable_vbus_oc(padctl->utmi_phys[port]);
	} else {
		status = regulator_is_enabled(padctl->vbus[port]);
		if (padctl->vbus[port] && status) {
			rc = regulator_disable(padctl->vbus[port]);
			if (rc)
				dev_err(padctl->dev, "disable port %d vbus failed %d\n",
					port, rc);
		}
		dev_dbg(padctl->dev, "%s: port %d regulator status: %d->%d\n",
			 __func__, port, status,
			 regulator_is_enabled(padctl->vbus[port]));
	}
	mutex_unlock(&padctl->lock);
	return rc;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_utmi_vbus_power_off);

int tegra18x_phy_xusb_overcurrent_detected(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;
	bool detected = false;
	u32 reg;
	int pin;

	if (!phy)
		return 0;

	padctl = phy_get_drvdata(phy);
	if (!is_utmi_phy(phy))
		return -EINVAL;

	port = utmi_phy_to_port(phy);
	if (port < 0)
		return -EINVAL;

	pin = padctl->utmi_ports[port].oc_pin;
	if (pin < 0)
		return -EINVAL;

	reg = padctl_readl(padctl, XUSB_PADCTL_OC_DET);

	detected = !!(reg & OC_DETECTED_VBUS_PAD(pin));
	if (detected) {
		reg &= ~OC_DETECTED_VBUS_PAD_MASK;
		reg &= ~OC_DETECTED_INT_EN_VBUS_PAD(pin);
		padctl_writel(padctl, reg, XUSB_PADCTL_OC_DET);
	}

	return detected;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_overcurrent_detected);

void tegra18x_phy_xusb_handle_overcurrent(struct phy *phy)
{
	struct tegra_padctl *padctl;
	u32 reg;
	unsigned i;
	int pin;

	if (!phy)
		return;

	padctl = phy_get_drvdata(phy);
	if (!is_utmi_phy(phy))
		return;

	oc_debug(padctl);
	mutex_lock(&padctl->lock);
	reg = padctl_readl(padctl, XUSB_PADCTL_OC_DET);

	for (i = 0; i < TEGRA_UTMI_PHYS; i++) {
		pin = padctl->utmi_ports[i].oc_pin;
		if (pin < 0)
			continue;

		if (reg & OC_DETECTED_VBUS_PAD(pin)) {
			dev_info(padctl->dev, "%s: clear port %d pin %d OC\n",
				 __func__, i, pin);
			tegra186_enable_vbus_oc(padctl->utmi_phys[i]);
		}
	}
	mutex_unlock(&padctl->lock);
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_handle_overcurrent);

static int tegra186_usb3_phy_reverse_id(struct tegra_padctl *padctl,
					int port, bool enable)
{
	u32 reg;

	mutex_lock(&padctl->lock);
	reg = padctl_readl(padctl, XUSB_PADCTL_SS_PORT_CAP);
	if (enable)
		reg |= PORT_REVERSE_ID(port);
	else
		reg &= ~PORT_REVERSE_ID(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_SS_PORT_CAP);
	mutex_unlock(&padctl->lock);

	return 0;
}

static int tegra186_utmi_phy_reverse_id(struct tegra_padctl *padctl,
					int port, bool enable)
{
	u32 reg;

	mutex_lock(&padctl->lock);
	reg = padctl_readl(padctl, XUSB_PADCTL_USB2_PORT_CAP);
	if (enable)
		reg |= PORT_REVERSE_ID(port);
	else
		reg &= ~PORT_REVERSE_ID(port);
	padctl_writel(padctl, reg, XUSB_PADCTL_USB2_PORT_CAP);
	mutex_unlock(&padctl->lock);

	return 0;
}

int tegra18x_phy_xusb_set_reverse_id(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;

	if (!phy)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);

	/* applicable to SS/UTMI only */
	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_utmi_phy_reverse_id(padctl, port, true);
	} else if (is_usb3_phy(phy)) {
		port = usb3_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_usb3_phy_reverse_id(padctl, port, true);
	}

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_set_reverse_id);

int tegra18x_phy_xusb_clear_reverse_id(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;

	if (!phy)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);

	/* applicable to SS/UTMI only */
	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_utmi_phy_reverse_id(padctl, port, false);
	} else if (is_usb3_phy(phy)) {
		port = usb3_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_usb3_phy_reverse_id(padctl, port, false);
	}

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_clear_reverse_id);

int tegra18x_phy_xusb_generate_srp(struct phy *phy)
{
	struct tegra_padctl *padctl;
	u32 reg;
	int port;

	if (!phy)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);

	/* applicable only to UTMI */
	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		mutex_lock(&padctl->lock);
		reg = padctl_readl(padctl,
				   USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
		reg |= GENERATE_SRP;
		padctl_writel(padctl, reg,
			      USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
		mutex_unlock(&padctl->lock);

		return 0;
	}

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_generate_srp);

static int tegra186_utmi_phy_srp_detect(struct tegra_padctl *padctl,
					int port, bool enable)
{
	u32 reg;

	reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
	if (enable)
		reg |= SRP_DETECT_EN | SRP_INTR_EN;
	else
		reg &= ~(SRP_DETECT_EN | SRP_INTR_EN);
	padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL0(port));

	return 0;
}

int tegra18x_phy_xusb_enable_srp_detect(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;

	if (!phy)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);

	/* applicable only to UTMI */
	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_utmi_phy_srp_detect(padctl, port, true);
	}

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_enable_srp_detect);

int tegra18x_phy_xusb_disable_srp_detect(struct phy *phy)
{
	struct tegra_padctl *padctl;
	int port;

	if (!phy)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);

	/* applicable only to UTMI */
	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return -EINVAL;
		return tegra186_utmi_phy_srp_detect(padctl, port, false);
	}

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_disable_srp_detect);

bool tegra18x_phy_xusb_srp_detected(struct phy *phy)
{
	struct tegra_padctl *padctl;
	u32 reg;
	int port;

	if (!phy)
		return false;

	padctl = phy_get_drvdata(phy);

	/* applicable only to UTMI */
	if (is_utmi_phy(phy)) {
		port = utmi_phy_to_port(phy);
		if (port < 0)
			return false;

		reg = padctl_readl(padctl,
				   USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
		dev_dbg(padctl->dev, "USB2_BATTERY_CHRG_OTGPADX_CTL0:%#x\n",
			reg);
		if (reg & SRP_DETECTED) {
			padctl_writel(padctl, reg,
					USB2_BATTERY_CHRG_OTGPADX_CTL0(port));
			return true;
		}
	}

	return false;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_srp_detected);

int tegra18x_phy_xusb_enable_otg_int(struct phy *phy)
{
	struct tegra_padctl *padctl;
	u32 reg;

	if (!phy)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);

	mutex_lock(&padctl->lock);
	reg = padctl_readl(padctl, USB2_VBUS_ID);
	reg |= VBUS_VALID_CHNG_INTR_EN | OTG_VBUS_SESS_VLD_CHNG_INTR_EN |
		IDDIG_CHNG_INTR_EN | VBUS_WAKEUP_CHNG_INTR_EN;
	padctl_writel(padctl, reg, USB2_VBUS_ID);
	mutex_unlock(&padctl->lock);

	return 0;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_enable_otg_int);

int tegra18x_phy_xusb_disable_otg_int(struct phy *phy)
{
	struct tegra_padctl *padctl;
	u32 reg;

	if (!phy)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);

	mutex_lock(&padctl->lock);
	reg = padctl_readl(padctl, USB2_VBUS_ID);
	reg &= ~(VBUS_VALID_CHNG_INTR_EN | OTG_VBUS_SESS_VLD_CHNG_INTR_EN |
		 IDDIG_CHNG_INTR_EN | VBUS_WAKEUP_CHNG_INTR_EN);
	padctl_writel(padctl, reg, USB2_VBUS_ID);
	mutex_unlock(&padctl->lock);

	return 0;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_disable_otg_int);

int tegra18x_phy_xusb_ack_otg_int(struct phy *phy)
{
	struct tegra_padctl *padctl;
	u32 reg;

	if (!phy)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);

	reg = padctl_readl(padctl, USB2_VBUS_ID);
	padctl_writel(padctl, reg, USB2_VBUS_ID);

	return 0;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_ack_otg_int);

int tegra18x_phy_xusb_get_otg_vbus_id(struct phy *phy,
		struct tegra_xusb_otg_vbus_id *info)
{
	struct tegra_padctl *padctl;
	u32 reg;

	if (!phy || !info)
		return -EINVAL;

	padctl = phy_get_drvdata(phy);

	reg = padctl_readl(padctl, USB2_VBUS_ID);

	info->iddig_chg = !!(reg & IDDIG_ST_CHNG);
	info->iddig = tegra_phy_xusb_parse_rid(reg);
	dev_dbg(padctl->dev, "%s: iddig_chg=%d, iddig=%d\n",
		__func__, info->iddig_chg, info->iddig);

	info->vbus_sess_vld_chg = !!(reg & OTG_VBUS_SESS_VLD_ST_CHNG);
	info->vbus_sess_vld = !!(reg & OTG_VBUS_SESS_VLD);
	dev_dbg(padctl->dev, "%s: vbus_sess_vld_chg=%d, vbus_sess_vld=%d\n",
		__func__, info->vbus_sess_vld_chg, info->vbus_sess_vld);

	info->vbus_vld_chg = !!(reg & VBUS_VALID_ST_CHNG);
	info->vbus_vld = !!(reg & VBUS_VALID);
	dev_dbg(padctl->dev, "%s: vbus_vld_chg=%d, vbus_vld=%d\n",
		__func__, info->vbus_vld_chg, info->vbus_vld);

	info->vbus_wakeup_chg = !!(reg & VBUS_WAKEUP_ST_CHNG);
	info->vbus_wakeup = !!(reg & VBUS_WAKEUP);
	dev_dbg(padctl->dev, "%s: vbus_wakeup_chg=%d, vbus_wakeup=%d\n",
		__func__, info->vbus_wakeup_chg, info->vbus_wakeup);

	info->vbus_override = !!(reg & VBUS_OVERRIDE);
	info->id_override = (reg >> ID_OVERRIDE_SHIFT) & ID_OVERRIDE_MASK;
	dev_dbg(padctl->dev, "%s: vbus_override=%d, id_override=%d\n",
		__func__, info->vbus_override, info->id_override);

	return 0;
}
EXPORT_SYMBOL_GPL(tegra18x_phy_xusb_get_otg_vbus_id);

MODULE_AUTHOR("JC Kuo <jckuo@nvidia.com>");
MODULE_DESCRIPTION("Tegra 186 XUSB PADCTL driver");
MODULE_LICENSE("GPL v2");