summaryrefslogtreecommitdiffstats
path: root/drivers/video/tegra/dc/hdcp/dphdcp.c
blob: 260a200180f02d1d4fdc6b992f76b25685af8433 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
/*
 * dphdcp.c: dp hdcp driver.
 *
 * Copyright (c) 2015-2020, NVIDIA CORPORATION, All rights reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/sched.h>
#include <linux/miscdevice.h>
#include <linux/workqueue.h>
#include <linux/atomic.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/tsec.h>
#include <soc/tegra/kfuse.h>
#include <soc/tegra/fuse.h>

#include "dc.h"
#include "dphdcp.h"
#include "dp.h"
#include "dpaux.h"
#include "edid.h"
#include "sor.h"
#include "sor_regs.h"
#include "dpaux_regs.h"
#include "tsec_drv.h"
#include "tsec/tsec_methods.h"
#include "nvhdcp_hdcp22_methods.h"
#include "tsec/tsec.h"

#if (defined(CONFIG_TRUSTY))
#include <linux/trusty/trusty_ipc.h>
#endif

static DECLARE_WAIT_QUEUE_HEAD(wq_worker);

/* Bcaps register bits */
#define BCAPS_REPEATER			(1 << 1)
#define BCAPS_HDCP_CAPABLE		(1 << 0)

/* Bstatus register bits */
#define BSTATUS_REAUTH_REQ		(1 << 3)
#define BSTATUS_LINK_INTEG_FAIL		(1 << 2)
#define BSTATUS_R0_PRIME_SET		(1 << 1)
#define BSTATUS_READY			(1 << 0)

/* Binfo register bits */
#define BINFO_MAX_DEVS_EXCEEDED		(1 << 7)
#define BINFO_MAX_CASCADE_EXCEEDED	(1 << 11)

/* for hdcp 2.2 */
#define HDCP22_PROTOCOL			1
#define HDCP1X_PROTOCOL			0
#define HDCP_DEBUG			1
#define HDCP_READY			1
#define HDCP_REAUTH			2
#define HDCP_READY_SET			(1 << 0)
#define HDCP_HPRIME_AVAIL		(1 << 1)
#define HDCP_PAIRING_AVAIL		(1 << 2)
#define HDCP_REAUTH_MASK		(1 << 3)
#define HDCP_LINK_INTEG_FAIL		(1 << 4)

#define HDCP_TA_CMD_CTRL		0
#define HDCP_TA_CMD_AKSV		1
#define HDCP_TA_CMD_ENC			2
#define HDCP_TA_CMD_REP			3
#define HDCP_TA_CMD_BKSV		4

#define PKT_SIZE			256
#define HDCP_AUTH_CMD			0x5

#define HDCP_TA_CTRL_ENABLE		1
#define HDCP_TA_CTRL_DISABLE		0

#define HDCP_CMD_OFFSET			1
#define HDCP_CMD_BYTE_OFFSET		8


#define MAX_AUX_SIZE			15

#define SIZE_ONE_BYTE			1
#define SIZE_TWO_BYTES			2
#define SIZE_FIVE_BYTES			5
#define SIZE_EIGHT_BYTES		8

#define KEY_CTRL_RETRIES		101
#define HDCP_CTRL_RETRIES		13
#define SRAM_CLR_RETRIES		6
#define RX_VALIDATE_RETRIES		3
#define VPRIME_RETRIES			3
#define KSV_RETRIES			10
#define MAX_BYTES_READ			15
#define REPEATER_READY_RETRY		51

#define HDCP_KEY_LOAD			0x100
#define KFUSE_MASK			0x10

#define HDCP11_SRM_PATH			"vendor/etc/hdcpsrm/hdcp1x.srm"

#define CP_IRQ_OFFSET			(1 << 2)
#define CP_IRQ_RESET			0x4

/* logging */
#ifdef VERBOSE_DEBUG
#define dphdcp_vdbg(...)	\
		pr_debug("dphdcp: " __VA_ARGS__)
#else
#define dphdcp_vdbg(...)		\
({						\
	if (0)					\
		pr_debug("dphdcp: " __VA_ARGS__); \
	0;					\
})
#endif
#define dphdcp_debug(...)	\
		pr_debug("dphdcp: " __VA_ARGS__)
#define	dphdcp_err(...)	\
		pr_err("dphdcp: Error: " __VA_ARGS__)
#define dphdcp_info(...)	\
		pr_info("dphdcp: " __VA_ARGS__)

#define HDCP_PORT_NAME	"com.nvidia.tos.13f616f9-8572-4a6f-a1f104aa9b05f9ff"

static bool repeater_flag;
static bool vprime_check_done;
static struct tegra_dphdcp **dphdcp_head;

static int tegra_dphdcp_read(struct tegra_dc_dp_data *dp, u32 cmd,
	u8 *data_ptr, u32 size, u32 *aux_status)
{
	u32 status = 0;
	u32 cursize = 0;
	int ret = 0;
	struct tegra_dc_dpaux_data *dpaux = NULL;

	if (!dp || !data_ptr || !aux_status) {
		dphdcp_err("Null params sent\n");
		return -EINVAL;
	}

	if (dp->dc->out->type == TEGRA_DC_OUT_FAKE_DP)
		return -EIO;

	dpaux = dp->dpaux;
	cursize = size;
	mutex_lock(&dpaux->lock);
	tegra_dpaux_get(dp->dpaux);
	ret = tegra_dc_dpaux_read_chunk_locked(dpaux, DPAUX_DP_AUXCTL_CMD_AUXRD,
		cmd, data_ptr, &cursize, &status);
	tegra_dpaux_put(dp->dpaux);
	mutex_unlock(&dpaux->lock);
	if (ret)
		dev_err(&dp->dc->ndev->dev,
			"dp: Failed to read data. CMD 0x%x, Status 0x%x\n",
			cmd, status);
	*aux_status = status;
	return ret;
}

static int tegra_dphdcp_write(struct tegra_dc_dp_data *dp, u32 cmd,
	u8 *data, u32 size)
{
	u32 status = 0;
	u32 cursize = 0;
	int ret;
	struct tegra_dc_dpaux_data *dpaux = NULL;

	if (!dp || !data) {
		dphdcp_err("Null params sent\n");
		return -EINVAL;
	}

	if (dp->dc->out->type == TEGRA_DC_OUT_FAKE_DP)
		return -EIO;

	dpaux = dp->dpaux;
	cursize = size;
	mutex_lock(&dpaux->lock);
	tegra_dpaux_get(dp->dpaux);
	ret = tegra_dc_dpaux_write_chunk_locked(dpaux,
			DPAUX_DP_AUXCTL_CMD_AUXWR, cmd, data,
			&cursize, &status);
	tegra_dpaux_put(dp->dpaux);
	mutex_unlock(&dpaux->lock);
	if (ret)
		dev_err(&dp->dc->ndev->dev,
			"dp: Failed to write data. CMD 0x%x, Status 0x%x\n",
			cmd, status);
	return ret;
}

/* read 5 bytes of data */
static int tegra_dphdcp_read40(struct tegra_dc_dp_data *dp, u32 cmd,
			u64 *data)
{
	u8 buf[SIZE_FIVE_BYTES];
	int i;
	u64 n;
	int e;
	u32 status;

	if (!dp || !data) {
		dphdcp_err("Null params sent\n");
		return -EINVAL;
	}

	e = tegra_dphdcp_read(dp, cmd, buf, sizeof(buf), &status);
	if (e)
		return e;

	/* assign the value read from aux to data */
	for (i = 0, n = 0; i < 5; i++) {
		n <<= 8;
		n |= buf[4 - i];
	}
	if (data)
		*data = n;
	return 0;
}

/* read 2 bytes of data */
static int tegra_dphdcp_read16(struct tegra_dc_dp_data *dp, u32 cmd,
				u64 *data)
{
	u8 buf[SIZE_TWO_BYTES];
	int e;
	u32 status;

	if (!dp || !data) {
		dphdcp_err("Null params sent\n");
		return -EINVAL;
	}

	e = tegra_dphdcp_read(dp, cmd, buf, sizeof(buf), &status);
	if (e)
		return e;

	if (data)
		*data = buf[0] | (u16)buf[1] << 8;
	return 0;
}

/* write 8 bytes of data */
static int tegra_dphdcp_write64(struct tegra_dc_dp_data *dp, u32 reg,
	u64 *data)
{
	char buf[SIZE_EIGHT_BYTES];

	if (!dp || !data) {
		dphdcp_err("Null params sent\n");
		return -EINVAL;
	}

	memcpy(buf, (char *)data, sizeof(buf));
	return tegra_dphdcp_write(dp, reg, buf, sizeof(buf));
}

/* write 1 byte of data */
static int tegra_dphdcp_write8(struct tegra_dc_dp_data *dp, u32 reg,
	u8 data)
{
	char buf[SIZE_ONE_BYTE];
	u8 cur_data;

	if (!dp) {
		dphdcp_err("Null params sent\n");
		return -EINVAL;
	}

	cur_data = data;

	memcpy(buf, (char *)&cur_data, sizeof(buf));
	return tegra_dphdcp_write(dp, reg, buf, sizeof(buf));
}

/* write 5 bytes of data */
static int tegra_dphdcp_write40(struct tegra_dc_dp_data *dp, u32 reg,
	u64 *data)
{
	char buf[SIZE_FIVE_BYTES];

	if (!dp || !data) {
		dphdcp_err("Null params sent\n");
		return -EINVAL;
	}

	memcpy(buf, data, sizeof(buf));
	return tegra_dphdcp_write(dp, reg, buf, sizeof(buf));
}

/*
 * wait for bits in mask to be set to value in NV_SOR_KEY_CTRL
 * waits upto 100 ms
 */
static int wait_key_ctrl(struct tegra_dc_sor_data *sor, u32 mask, u32 value)
{
	int retries = KEY_CTRL_RETRIES;
	u32 ctrl;

	if (!sor) {
		dphdcp_err("Null params sent\n");
		return -EINVAL;
	}

	do {
		usleep_range(1, 2);
		ctrl = tegra_sor_readl_ext(sor, NV_SOR_KEY_CTRL);
		if (((ctrl ^ value) & mask) == 0)
			break;
	} while (--retries);
	if (!retries) {
		dphdcp_err("key ctrl read timeout (mask=0x%x)\n", mask);
		return -EIO;
	}
	return 0;
}

/* set or clear RUN_YES */
static void hdcp_ctrl_run(struct tegra_dc_sor_data *sor, bool v)
{
	u32 ctrl;

	if (!sor) {
		dphdcp_err("Null params sent\n");
		return;
	}

	if (v) {
		ctrl = tegra_sor_readl_ext(sor, NV_SOR_DP_HDCP_CTRL);
		ctrl |= HDCP_RUN_YES;
	} else {
		ctrl = 0;
	}

	tegra_sor_writel_ext(sor, NV_SOR_DP_HDCP_CTRL, ctrl);
}

/*
 * wait for any bits in mask to be set in NV_SOR_DP_HDCP_CTRL
 * sleeps up to 120 ms
 */
static int wait_hdcp_ctrl(struct tegra_dc_sor_data *sor, u32 mask, u32 *v)
{
	int retries = HDCP_CTRL_RETRIES;
	u32 ctrl;

	if (!sor) {
		dphdcp_err("Null params sent\n");
		return -EINVAL;
	}

	do {
		ctrl = tegra_sor_readl_ext(sor, NV_SOR_DP_HDCP_CTRL);
		if ((ctrl & mask)) {
			if (v)
				*v = ctrl;
			break;
		}
		if (retries > 1)
			usleep_range(10, 15);
	} while (--retries);
	if (!retries) {
		dphdcp_err("ctrl read timeout (mask=0x%x)\n", mask);
		return -EIO;
	}
	return 0;
}

/*
 * check if the KSV values returned are valid,
 * i.e a combination of 20 ones and 20 zeroes
 */
static int verify_ksv(u64 k)
{
	unsigned i;
	/* count set bits, must be exactly 20 set to be valid */
	for (i = 0; k; i++)
		k ^= k & -k;

	return  (i != 20) ? -EINVAL : 0;
}

/* 64-bit link encryption session random number */
static inline u64 get_an(struct tegra_dc_sor_data *sor)
{
	u64 r;

	if (!sor) {
		dphdcp_err("Null params sent!\n");
		return -EINVAL;
	}

	r = (u64)tegra_sor_readl_ext(sor, NV_SOR_DP_HDCP_AN_MSB) << 32;
	r |= tegra_sor_readl_ext(sor, NV_SOR_DP_HDCP_AN_LSB);
	return r;
}

/* 40-bit transmitter's key selection vector */
static inline u64 get_aksv(struct tegra_dc_sor_data *sor)
{
	u64 r;

	if (!sor) {
		dphdcp_err("Null params sent!\n");
		return -EINVAL;
	}

	r = (u64)tegra_sor_readl_ext(sor, NV_SOR_DP_HDCP_AKSV_MSB) << 32;
	r |= tegra_sor_readl_ext(sor, NV_SOR_DP_HDCP_AKSV_LSB);
	return r;
}

/* 40-bit receiver's key selection vector */
static inline void set_bksv(struct tegra_dc_sor_data *sor, u64 b_ksv,
							bool repeater)
{
	if (sor) {
		if (repeater)
			b_ksv |= (u64)REPEATER << 32;
		tegra_sor_writel_ext(sor, NV_SOR_DP_HDCP_BKSV_LSB, (u32)b_ksv);
		tegra_sor_writel_ext(sor, NV_SOR_DP_HDCP_BKSV_MSB, b_ksv >> 32);
	}
}

static int get_bcaps(struct tegra_dc_dp_data *dp, u8 *b_caps)
{
	u32 status;

	if (!dp || !b_caps) {
		dphdcp_err("Null params sent!\n");
		return -EINVAL;
	}

	return tegra_dphdcp_read(dp, NV_DPCD_HDCP_BCAPS_OFFSET,
						b_caps, 1, &status);
}

static int get_bstatus(struct tegra_dc_dp_data *dp, u8 *bstatus)
{
	u32 status;

	if (!dp || !bstatus) {
		dphdcp_err("Null params sent!\n");
		return -EINVAL;
	}

	return tegra_dphdcp_read(dp, NV_DPCD_HDCP_BSTATUS_OFFSET,
						bstatus, 1, &status);
}

static int get_irq_status(struct tegra_dc_dp_data *dp, u8 *irq_status)
{
	u32 status;

	if (!dp || !irq_status) {
		dphdcp_err("Null params sent!\n");
		return -EINVAL;
	}

	return tegra_dphdcp_read(dp, NV_DPCD_DEVICE_SERVICE_IRQ_VECTOR,
				irq_status, 1, &status);
}

static inline bool dphdcp_is_plugged(struct tegra_dphdcp *dphdcp)
{
	rmb();
	if (dphdcp)
		return dphdcp->plugged;
	return false;
}

static inline bool dphdcp_set_plugged(struct tegra_dphdcp *dphdcp,
						bool plugged)
{
	if (dphdcp) {
		dphdcp->plugged = plugged;
		wmb();
	}
	return plugged;
}

static int load_kfuse(struct tegra_dc_dp_data *dp)
{
	u32 ctrl;
	u32 tmp;
	int retries;
	int e;
	int i;
	unsigned buf[KFUSE_DATA_SZ/4];
	struct tegra_dc_sor_data *sor;

	if (!dp) {
		dphdcp_err("Null params sent!\n");
		return -EINVAL;
	}

	sor = dp->sor;

	memset(buf, 0, sizeof(buf));

	/* load kfuse */
	dphdcp_vdbg("loading kfuse\n");
	/* copy load kfuse into buffer - only needed for early Tegra parts */
	e = tegra_kfuse_read(buf, sizeof(buf));
	if (e) {
		dphdcp_err("Kfuse read failure\n");
		return e;
	}

	/* write the kfuse to the DP SRAM */
	tegra_sor_writel_ext(sor, NV_SOR_KEY_CTRL, 1);

	/* issue a reload */
	ctrl = tegra_sor_readl_ext(sor, NV_SOR_KEY_CTRL);
	tegra_sor_writel_ext(sor, NV_SOR_KEY_CTRL, ctrl | PKEY_RELOAD_TRIGGER
					| LOCAL_KEYS);
	e = wait_key_ctrl(sor, PKEY_LOADED, PKEY_LOADED);
	if (e) {
		dphdcp_err("key reload timeout\n");
		return e;
	}

	tegra_sor_writel_ext(sor, NV_SOR_KEY_SKEY_INDEX, 0);

	/* wait for SRAM to be cleared */
	retries = SRAM_CLR_RETRIES;
	do {
		tmp = tegra_sor_readl_ext(sor, NV_SOR_KEY_DEBUG0);
		if ((tmp & 1) == 0)
			break;
		if (retries > 1)
			mdelay(1);
	} while (--retries);
	if (!retries) {
		dphdcp_err("key SRAM clear timeout\n");
		return -EIO;
	}

	for (i = 0; i < KFUSE_DATA_SZ / 4; i += 4) {

		/* load 128-bits*/
		tegra_sor_writel_ext(sor, NV_SOR_KEY_HDCP_KEY_0, buf[i]);
		tegra_sor_writel_ext(sor, NV_SOR_KEY_HDCP_KEY_1, buf[i+1]);
		tegra_sor_writel_ext(sor, NV_SOR_KEY_HDCP_KEY_2, buf[i+2]);
		tegra_sor_writel_ext(sor, NV_SOR_KEY_HDCP_KEY_3, buf[i+3]);

		/* trigger LOAD_HDCP_KEY */
		tegra_sor_writel_ext(sor,
			NV_SOR_KEY_HDCP_KEY_TRIG, HDCP_KEY_LOAD);
		tmp = LOCAL_KEYS | WRITE16;
		if (i)
			tmp |= AUTOINC;
		tegra_sor_writel_ext(sor, NV_SOR_KEY_CTRL, tmp);

		/* wait for WRITE16 to complete */
		e = wait_key_ctrl(sor, KFUSE_MASK, 0); /* WRITE16 */
		if (e) {
			dphdcp_err("key write timeout\n");
			return -EIO;
		}
	}
	return 0;
}

/* check the 16 bit link integrity value */
static inline u64 get_transmitter_ro_prime(struct tegra_dc_dp_data *dp)
{
	struct tegra_dc_sor_data *sor;

	if (!dp) {
		dphdcp_err("Null params sent!\n");
		return -EINVAL;
	}

	sor = dp->sor;
	return tegra_sor_readl_ext(sor, NV_SOR_DP_HDCP_RI);
}

/* R0' prime value generated from the receiver */
static inline int get_receiver_ro_prime(struct tegra_dc_dp_data *dp, u64 *r)
{
	if (!dp || !r) {
		dphdcp_err("Null params sent!\n");
		return -EINVAL;
	}
	return tegra_dphdcp_read16(dp, NV_DPCD_HDCP_RPRIME_OFFSET, r);
}

static int validate_rx(struct tegra_dphdcp *dphdcp)
{
	int retries = RX_VALIDATE_RETRIES;
	u64 rx = 0, tx = 0;
	int e;
	struct tegra_dc_dp_data *dp;

	if (!dphdcp) {
		dphdcp_err("Null params sent!\n");
		return -EINVAL;
	}

	dp = dphdcp->dp;

	/* try 3 times for possible link errors */
	do {
		tx = get_transmitter_ro_prime(dp);
		e = get_receiver_ro_prime(dp, &rx);
	} while (--retries && rx != tx);
	dphdcp_vdbg("rx=0x%016llx tx=0x%016llx\n", rx, tx);

	if (rx != tx)
		return -EINVAL;
	return 0;
}

/* get V' 160-bit SHA-1 hash from repeater */
static int get_vprime(struct tegra_dc_dp_data *dp, u8 *v_prime)
{
	int e, i;
	u32 status;

	if (!dp || !v_prime) {
		dphdcp_err("Null params sent!\n");
		return -EINVAL;
	}

	for (i = 0; i < 20; i += 4) {
		e = tegra_dphdcp_read(dp,
			NV_DPCP_HDCP_SHA_H0_OFFSET+i, v_prime+i, 4, &status);
		if (e) {
			dphdcp_err("Error reading V'\n");
			return e;
		}
	}
	return 0;
}

static int get_ksvfifo(struct tegra_dc_dp_data *dp,
			unsigned num_bksv_list, u64 *ksv_list)
{
	u8 *buf = NULL;
	u8 *orig_buf = NULL;
	int e;
	unsigned int dp_retries = KSV_RETRIES;
	u32 status;
	size_t buf_len = num_bksv_list * SIZE_FIVE_BYTES;


	if (!ksv_list || !dp || num_bksv_list > TEGRA_NVHDCP_MAX_DEVS)
		return -EINVAL;

	if (num_bksv_list == 0)
		return 0;

	buf = kmalloc(buf_len, GFP_KERNEL);
	if (IS_ERR_OR_NULL(buf))
		return -ENOMEM;

	orig_buf = buf;
	while (buf_len > MAX_BYTES_READ) {
aux_read:
		e = tegra_dphdcp_read(dp,
		NV_DPCD_HDCP_KSV_FIFO_OFFSET, buf, MAX_BYTES_READ, &status);
		if (e) {
			dphdcp_err("Error reading KSV\n");
			kfree(orig_buf);
			return e;
		}
		if ((status & DPAUX_DP_AUXSTAT_REPLYTYPE_I2CDEFER) ||
		(status & DPAUX_DP_AUXSTAT_REPLYTYPE_DEFER)) {
			if (--dp_retries)
				goto aux_read;
		}
		buf_len -= MAX_BYTES_READ;
		buf += MAX_BYTES_READ;
	}

	if (buf_len)
		e = tegra_dphdcp_read(dp,
		NV_DPCD_HDCP_KSV_FIFO_OFFSET, buf, buf_len, &status);
	if (e) {
		dphdcp_err("Error reading KSV\n");
		kfree(orig_buf);
		return e;
	}
	memcpy(ksv_list, orig_buf, num_bksv_list*5);
	kfree(orig_buf);
	return 0;
}

/* validate srm signature */
static int get_srm_signature(struct hdcp_context_t *hdcp_context,
			char *nonce, uint64_t *pkt, void *ta_ctx)
{
	int err = 0;

	if (!hdcp_context || !nonce || !pkt || !ta_ctx) {
		dphdcp_err("Null params sent!\n");
		return err;
	}
	/* generate nonce in the ucode */
	err = tsec_hdcp_generate_nonce(hdcp_context, nonce);
	if (err) {
		dphdcp_err("Error generating nonce!\n");
		return err;
	}
	/* pass the nonce to hdcp TA and get the signature back */
	memcpy(pkt, nonce, HDCP_NONCE_SIZE);
	*(pkt + HDCP_NONCE_SIZE) = HDCP_1x;
	err = te_launch_trusted_oper(pkt, PKT_SIZE, HDCP_CMD_GEN_CMAC, ta_ctx);
	if (err)
		dphdcp_err("te launch operation failed with error %d\n", err);
	return err;
}

/* SRM revocation check for receiver */
static int srm_revocation_check(struct tegra_dphdcp *dphdcp)
{
	struct hdcp_context_t *hdcp_context =
		kmalloc(sizeof(struct hdcp_context_t), GFP_KERNEL);
	int e = 0;
	unsigned char nonce[HDCP_NONCE_SIZE];

	uint64_t *pkt = kzalloc(PKT_SIZE, GFP_KERNEL);

	if (!pkt || !hdcp_context)
		goto exit;

	e = tsec_hdcp_context_creation(hdcp_context, DISPLAY_TYPE_DP,
			dphdcp->dp->sor->ctrl_num);
	if (e) {
		dphdcp_err("hdcp context create/init failed\n");
		goto exit;
	}

	e =  tsec_hdcp_create_session(hdcp_context, DISPLAY_TYPE_DP,
				dphdcp->dp->sor->ctrl_num);
	if (e) {
		dphdcp_err("error in session creation\n");
		goto exit;
	}
	e = get_srm_signature(hdcp_context, nonce, pkt, dphdcp->ta_ctx);
	if (e) {
		dphdcp_err("Error getting srm signature!\n");
		goto exit;
	}
	e = tsec_hdcp_revocation_check(hdcp_context,
			(unsigned char *)(pkt + HDCP_CMAC_OFFSET),
			*((unsigned int *)(pkt + HDCP_TSEC_ADDR_OFFSET)),
			TEGRA_NVHDCP_PORT_DP, HDCP_1x);

	if (e)
		dphdcp_err("hdcp revocation check failed with err: %x\n", e);
exit:
	tsec_hdcp_free_context(hdcp_context);
	kfree(hdcp_context);
	kfree(pkt);
	return e;
}

/* vprime verification for repeater */
static int tsec_hdcp_dp_verify_vprime(struct tegra_dphdcp *dphdcp)
{
	int i;
	u8 *p;
	u8 buf[RCVR_ID_LIST_SIZE];
	unsigned char nonce[HDCP_NONCE_SIZE];
	struct hdcp_verify_vprime_param verify_vprime_param;
	int e = 0;
	uint64_t *pkt = NULL;
	struct hdcp_context_t *hdcp_context =
		kmalloc(sizeof(struct hdcp_context_t), GFP_KERNEL);

	e = tsec_hdcp_context_creation(hdcp_context, DISPLAY_TYPE_DP,
			dphdcp->dp->sor->ctrl_num);
	if (e) {
		dphdcp_err("hdcp context create/init failed\n");
		goto exit;
	}
	pkt = kzalloc(PKT_SIZE, GFP_KERNEL);

	if (!pkt || !hdcp_context)
		goto exit;
	e = get_srm_signature(hdcp_context, nonce, pkt, dphdcp->ta_ctx);
	if (e) {
		dphdcp_err("Error getting srm signature!\n");
		goto exit;
	}

	memset(&verify_vprime_param, 0x0,
		sizeof(struct hdcp_verify_vprime_param));

	/* convert 64 bit values to 40 bit */
	p = buf;
	for (i = 0; i < dphdcp->num_bksv_list; i++) {
		p[0] = (u8)(dphdcp->bksv_list[i] & 0xff);
		p[1] = (u8)((dphdcp->bksv_list[i]>>8) & 0xff);
		p[2] = (u8)((dphdcp->bksv_list[i]>>16) & 0xff);
		p[3] = (u8)((dphdcp->bksv_list[i]>>24) & 0xff);
		p[4] = (u8)((dphdcp->bksv_list[i]>>32) & 0xff);
		p += 5;
	}

	memcpy((void *)verify_vprime_param.vprime, dphdcp->v_prime,
			HDCP_SIZE_VPRIME_1X_8);
	verify_vprime_param.port = TEGRA_NVHDCP_PORT_DP; /* hdcp 1.x */
	verify_vprime_param.bstatus = dphdcp->binfo;
	e = tsec_hdcp1x_verify_vprime(verify_vprime_param, hdcp_context,
		buf, dphdcp->num_bksv_list, pkt);

exit:
	tsec_hdcp_free_context(hdcp_context);
	kfree(pkt);
	kfree(hdcp_context);
	return e;
}

static int get_repeater_info(struct tegra_dphdcp *dphdcp)
{
	int e = 0;
	unsigned int retries;
	int vcheck_tries = VPRIME_RETRIES;
	u8 bstatus;
	u64 binfo;
	u8 irq;
	int err = 0;
	struct tegra_dc_dp_data *dp = dphdcp->dp;

	dphdcp_vdbg("repeater found:fetching repeater info\n");
	/* wait up to 5 seconds for READY on repeater */
	retries = REPEATER_READY_RETRY;
	do {
		mutex_lock(&dphdcp->lock);
		e = dphdcp_is_plugged(dphdcp);
		mutex_unlock(&dphdcp->lock);
		if (!e) {
			dphdcp_err("disconnect while waiting for repeater\n");
			return -EIO;
		}
		/* wait till receiver computes V' */
		e = get_bstatus(dp, &bstatus);
		if (!e && (bstatus & BSTATUS_READY)) {
			dphdcp_vdbg("Bstatus READY from repeater\n");
			break;
		}
		if (retries > 1)
			msleep(100);
	} while (--retries);
	if (!retries) {
		dphdcp_err("repeater Bstatus read timeout\n");
		return -ETIMEDOUT;
	}
	/* READY is set so the CP_IRQ interrupt should go high */
	e = get_irq_status(dp, &irq);
	if (e) {
		dphdcp_err("irq register read failure!\n");
		return e;
	}
	if (irq & CP_IRQ_OFFSET)
		dphdcp_vdbg("CP_IRQ interrupt set high\n");

	/* verify V' thrice to check for link failures */
	do {
		memset(dphdcp->bksv_list, 0, sizeof(dphdcp->bksv_list));
		memset(dphdcp->v_prime, 0, sizeof(dphdcp->v_prime));
		/* read Binfo register */
		e = tegra_dphdcp_read16(dp, NV_DPCD_HDCP_BINFO_OFFSET,
				&binfo);
		if (e) {
			dphdcp_err("Binfo read failure!\n");
			return e;
		}
		msleep(100);

		/* clear the irq register, this will be needed to find out
		 * if a spurious interrupt is generated. The DEVICE_SERVICE_IRQ
		 * register is clearable read only and can be cleared by writing
		 * 1 to the respective bit
		 */
		e = tegra_dphdcp_write8(dp, NV_DPCD_DEVICE_SERVICE_IRQ_VECTOR,
								CP_IRQ_RESET);
		/* wait for the CP_IRQ bit to be cleared */
		msleep(100);

		e = get_irq_status(dp, &irq);
		if (e) {
			dphdcp_err("irq register read failure!\n");
			return e;
		}
		dphdcp_vdbg("read irq after clearing: %x\n", irq);

		if (binfo & BINFO_MAX_DEVS_EXCEEDED) {
			dphdcp_err("repeater:max devices (0x%016llx)\n", binfo);
			return -EINVAL;
		}

		if (binfo & BINFO_MAX_CASCADE_EXCEEDED) {
			dphdcp_err("repeater:max cascade (0x%16llx)\n", binfo);
			return -EINVAL;
		}
		dphdcp->binfo = binfo;
		dphdcp->num_bksv_list = binfo & 0x7f;
		dphdcp_vdbg("Binfo 0x%16llx (devices: %d)\n",
				binfo, dphdcp->num_bksv_list);

		/* do not read KSV FIFO when device count is zero */
		if (dphdcp->num_bksv_list != 0)	 {
			e = get_ksvfifo(dp, dphdcp->num_bksv_list,
						dphdcp->bksv_list);
			if (e) {
				dphdcp_err("KSVFIFO read (err %d)\n", e);
				return e;
			}
		}
		/* verify V' three times to check for link failures */
		e = get_vprime(dp, dphdcp->v_prime);
		if (e)
			dphdcp_err("repeater Vprime read failure!\n");
		err = tsec_hdcp_dp_verify_vprime(dphdcp);
		if (err)
			dphdcp_err("vprime verification failed\n");

		/* read CP_IRQ interrupt to check for spurious interrupts.
		 * This needs to be done only when we are authenticating
		 * on the DP engine. On the HDMI engine, we will not take
		 * any action for a spurious interrupt
		 */
		if (!repeater_flag) {
			e = get_irq_status(dp, &irq);
			if (e) {
				dphdcp_err("irq register read failure!\n");
				return e;
			}
			get_bstatus(dp, &bstatus);
			/* For a spurious CP_IRQ interrupt, the CP_IRQ bit
			 * will be high and the bstatus register bits will be
			 * de-asserted. Check for both these conditions
			 * to ensure if the interrupt generated is a
			 * spurious one
			 */
			if ((irq & CP_IRQ_OFFSET) && (bstatus == 0)) {
				dphdcp_vdbg("Spurious interrupt set\n");
				return -EINVAL;
			}
		}
	} while (--vcheck_tries && err);
	if (err)
		return -EINVAL;
	vprime_check_done = true;
	return 0;
}

static void dphdcp_downstream_worker(struct work_struct *work)
{
	int e = 0;
	u8 b_caps = 0;
	u8 bstatus = 0;
	u32 tmp = 0;
	u32 res = 0;

	struct tegra_dphdcp *dphdcp =
		container_of(to_delayed_work(work), struct tegra_dphdcp, work);
	struct tegra_dc_dp_data *dp = dphdcp->dp;
	struct tegra_dc *dc = dp->dc;
	struct tegra_dc_sor_data *sor = dp->sor;
	int hdcp_ta_ret; /* track returns from TA */
	uint32_t ta_cmd = HDCP_AUTH_CMD;
	bool enc = false;

	uint64_t *pkt = kmalloc(PKT_SIZE, GFP_KERNEL);

	/* If memory unavailable */
	if (!pkt) {
		dphdcp_err("Memory allocation failed\n");
		e = -ENOMEM;
		goto failure;
	}
	dphdcp_vdbg("%s():started thread %s\n", __func__, dphdcp->name);
	tegra_dc_io_start(dc);
	mutex_lock(&dphdcp->lock);
	if (dphdcp->state == STATE_OFF) {
		dphdcp_err("dphdcp failure - giving up\n");
		goto err;
	}
	dphdcp->state = STATE_UNAUTHENTICATED;

	/* check plug state to terminate early in case flush_workqueue() */
	if (!dphdcp_is_plugged(dphdcp)) {
		dphdcp_err("worker started while unplugged!\n");
		goto lost_dp;
	}
	dphdcp_vdbg("%s():hpd=%d\n", __func__, dphdcp->plugged);

	dphdcp->a_ksv = 0;
	dphdcp->b_ksv = 0;
	dphdcp->a_n = 0;
	mutex_unlock(&dphdcp->lock);

	/* read bcaps from receiver */
	e = get_bcaps(dp, &b_caps);
	mutex_lock(&dphdcp->lock);
	if (e) {
		dphdcp_err("Error reading bcaps\n");
		goto failure;
	}

	dphdcp_vdbg("read Bcaps = 0x%02x\n", b_caps);
	 /* check if receiver is hdcp capable */
	if (b_caps & BCAPS_HDCP_CAPABLE)
		dphdcp_vdbg("receiver is hdcp capable\n");
	else {
		dphdcp_err("receiver is not hdcp capable\n");
		goto failure;
	}
	dphdcp->ta_ctx = NULL;
	e = te_open_trusted_session(HDCP_PORT_NAME, &dphdcp->ta_ctx);
	if (e) {
		dphdcp_err("open session failed\n");
		goto failure;
	}
repeater_auth:
	if (tegra_dc_is_nvdisplay()) {
		/* if session successfully opened, launch operations */
		/* repeater flag in Bskv must be configured before
		 * loading fuses
		 */
		*pkt = HDCP_TA_CMD_REP;
		*(pkt + 1*HDCP_CMD_OFFSET) = TEGRA_NVHDCP_PORT_DP;
		*(pkt + 2*HDCP_CMD_OFFSET) = 0;
		*(pkt + 3*HDCP_CMD_OFFSET) = b_caps & BCAPS_REPEATER;
		*(pkt + 4*HDCP_CMD_OFFSET) = repeater_flag;
		*(pkt + 5*HDCP_CMD_OFFSET) = dphdcp->dp->sor->ctrl_num;
		e = te_launch_trusted_oper(pkt, PKT_SIZE, ta_cmd,
				dphdcp->ta_ctx);
		if (e) {
			dphdcp_err("te_launch_op failed with error %d\n", e);
			goto failure;
		} else {
			dphdcp_vdbg("Loading kfuse\n");
			e = load_kfuse(dp);
			if (e) {
				dphdcp_err("te_launch_op failed with error %d\n", e);
				goto failure;
			} else {
				dphdcp_vdbg("Loading kfuse\n");
				e = load_kfuse(dp);
				if (e) {
					dphdcp_err("kfuse could not be loaded\n");
					goto failure;
				}
			}

			usleep_range(20000, 25000);
			*pkt = HDCP_TA_CMD_CTRL;
			*(pkt + 1*HDCP_CMD_OFFSET) = TEGRA_NVHDCP_PORT_DP;
			*(pkt + 2*HDCP_CMD_OFFSET) = HDCP_TA_CTRL_ENABLE;
			*(pkt + 3*HDCP_CMD_OFFSET) = repeater_flag;
			*(pkt + 4*HDCP_CMD_OFFSET) = dphdcp->dp->sor->ctrl_num;
			e = te_launch_trusted_oper(pkt, PKT_SIZE, ta_cmd,
					dphdcp->ta_ctx);
			if (e) {
				dphdcp_err("te_launch_op failed with error %d\n", e);
				goto failure;
			} else {
				dphdcp_vdbg("wait AN_VALID ...\n");
				hdcp_ta_ret = *pkt;
				dphdcp_vdbg("An returned %x\n", e);
				if (hdcp_ta_ret) {
					dphdcp_err("An key generation timeout\n");
					goto failure;
				}
				/* check SROM return */
				hdcp_ta_ret = *(pkt + HDCP_CMD_BYTE_OFFSET);
				if (hdcp_ta_ret) {
					dphdcp_err("SROM error\n");
					goto failure;
				}
			}

			msleep(25);
			*pkt = HDCP_TA_CMD_AKSV;
			*(pkt + 1*HDCP_CMD_OFFSET) = TEGRA_NVHDCP_PORT_DP;
			*(pkt + 2*HDCP_CMD_OFFSET) = repeater_flag;
			*(pkt + 3*HDCP_CMD_OFFSET) = dphdcp->dp->sor->ctrl_num;
			e = te_launch_trusted_oper(pkt, PKT_SIZE, ta_cmd,
					dphdcp->ta_ctx);
			if (e) {
				dphdcp_err("te_launch_op failed with error %d\n", e);
				goto failure;

			} else {
				hdcp_ta_ret = (u64)*pkt;
				dphdcp->a_ksv = (u64)*(pkt + 1*HDCP_CMD_BYTE_OFFSET);
				dphdcp->a_n = (u64)*(pkt + 2*HDCP_CMD_BYTE_OFFSET);
				dphdcp_vdbg("Aksv is 0x%016llx\n", dphdcp->a_ksv);
				dphdcp_vdbg("An is 0x%016llx\n", dphdcp->a_n);
				/* check if verification of Aksv failed */
			if (hdcp_ta_ret) {
				dphdcp_err("Aksv verify failure\n");
				goto disable;
			}
		} }
	} else {
		set_bksv(sor, 0, (b_caps & BCAPS_REPEATER));
		e = load_kfuse(dp);
		if (e) {
			dphdcp_err("error loading kfuse\n");
			goto failure;
		}

		usleep_range(20000, 25000);
		hdcp_ctrl_run(sor, 1);

		dphdcp_vdbg("waiting for An_valid\n");

		/* wait for hardware to generate HDCP values */
		e = wait_hdcp_ctrl(sor, AN_VALID | SROM_ERR, &res);
		if (e) {
			dphdcp_err("An key generation timeout\n");
			goto failure;
		}
		if (res & SROM_ERR) {
			dphdcp_err("SROM error\n");
			goto failure;
		}

		msleep(25);

		dphdcp->a_ksv = get_aksv(sor);
		dphdcp->a_n = get_an(sor);

		dphdcp_vdbg("aksv is 0x%016llx\n", dphdcp->a_ksv);
		dphdcp_vdbg("an is 0x%016llx\n", dphdcp->a_n);

		if (verify_ksv(dphdcp->a_ksv)) {
			dphdcp_err("Aksv verify failure! (0x%016llx)\n",
					dphdcp->a_ksv);
			goto disable;
		}
	}
	mutex_unlock(&dphdcp->lock);

	/* write An to receiver */
	e = tegra_dphdcp_write64(dp, NV_DPCD_HDCP_AN_OFFSET, &dphdcp->a_n);
	if (e) {
		dphdcp_err("An write failure\n");
		mutex_lock(&dphdcp->lock);
		goto failure;
	}
	dphdcp_vdbg("wrote An = 0x%016llx\n", dphdcp->a_n);

	/* write Aksv to receiver */
	e = tegra_dphdcp_write40(dp, NV_DPCD_HDCP_AKSV_OFFSET,
						&dphdcp->a_ksv);
	if (e) {
		dphdcp_err("Aksv write failure\n");
		mutex_lock(&dphdcp->lock);
		goto failure;
	}
	dphdcp_vdbg("wrote Aksv = 0x%010llx\n", dphdcp->a_ksv);

	mutex_lock(&dphdcp->lock);
	/* handle if connection lost in the middle of authentication */
	if (!dphdcp_is_plugged(dphdcp))
		goto lost_dp;

	mutex_unlock(&dphdcp->lock);

	/* get bksv from receiver */
	e = tegra_dphdcp_read40(dp, NV_DPCD_HDCP_BKSV_OFFSET,
					&dphdcp->b_ksv);
	mutex_lock(&dphdcp->lock);
	if (e) {
		dphdcp_err("Bksv read failure\n");
		goto failure;
	}
	dphdcp_vdbg("read Bksv from device: 0x%016llx\n", dphdcp->b_ksv);

	if (tegra_dc_is_nvdisplay()) {
		*pkt = HDCP_TA_CMD_BKSV;
		*(pkt + 1*HDCP_CMD_OFFSET) = TEGRA_NVHDCP_PORT_DP;
		*(pkt + 2*HDCP_CMD_OFFSET) = dphdcp->b_ksv;
		*(pkt + 3*HDCP_CMD_OFFSET) = b_caps & BCAPS_REPEATER;
		*(pkt + 4*HDCP_CMD_OFFSET) = repeater_flag;
		*(pkt + 5*HDCP_CMD_OFFSET) = dphdcp->dp->sor->ctrl_num;
		e = te_launch_trusted_oper(pkt, PKT_SIZE, ta_cmd, dphdcp->ta_ctx);
		if (e) {
			dphdcp_err("te launch operation failed with error: %d\n", e);
			goto failure;
		} else {
			/* check if Bksv verification was successful */
			hdcp_ta_ret = (int)*pkt;
			if (hdcp_ta_ret) {
				dphdcp_err("Bksv verify failure\n");
				goto failure;
			} else {
				dphdcp_vdbg("loaded Bksv into controller\n");
				/* check if R0 read was successful */
				hdcp_ta_ret = (int)*(pkt);
				if (hdcp_ta_ret) {
					dphdcp_err("R0 read failure\n");
					goto failure;
				}
			}
		}
	} else {
		/*
		 * verify the bksv to be if it has a correct combination of
		 * 1's and 0's
		 */
		if (verify_ksv(dphdcp->b_ksv)) {
			dphdcp_err("Bksv verify failure! (0x%016llx)\n",
					dphdcp->b_ksv);
			goto failure;
		}

		set_bksv(sor, dphdcp->b_ksv, (b_caps & BCAPS_REPEATER));
		dphdcp_vdbg("Loaded Bksv into controller\n");

		/* check if the computations of Km, Ks, M0 and R0 are over */
		e = wait_hdcp_ctrl(sor, R0_VALID, NULL);
		if (e) {
			dphdcp_err("R0 read failure\n");
			goto failure;
		}

		dphdcp_vdbg("R0 valid\n");
	}
	mutex_unlock(&dphdcp->lock);

	msleep(100); /* cannot read R0' within 100 ms of writing AKSV */

	/*
	 * after part 1 of authentication protocol, check for
	 * link integrity
	 * TODO: add support for both single and multi stream mode
	 */
	if (!repeater_flag) {
		e = validate_rx(dphdcp);
		if (e) {
			dphdcp_err("could not validate receiver\n");
			mutex_lock(&dphdcp->lock);
			goto failure;
		}
		if (tegra_dc_is_nvdisplay()) {
			*pkt = HDCP_TA_CMD_ENC;
			*(pkt + 1*HDCP_CMD_OFFSET) = TEGRA_NVHDCP_PORT_DP;
			*(pkt + 2*HDCP_CMD_OFFSET) = b_caps;
			*(pkt + 3*HDCP_CMD_OFFSET) = dphdcp->dp->sor->ctrl_num;
			e = te_launch_trusted_oper(pkt, PKT_SIZE/4, ta_cmd,
					dphdcp->ta_ctx);
			if (e) {
				dphdcp_err("launch oper failed with error: %d\n", e);
				goto failure;
			}
			enc = true;
		} else {
			tmp = tegra_sor_readl_ext(sor, NV_SOR_DP_HDCP_CTRL);
			tmp |= CRYPT_ENABLED;
			tegra_sor_writel_ext(sor, NV_SOR_DP_HDCP_CTRL, tmp);
		}
		dphdcp_vdbg("CRYPT enabled\n");
	}
	msleep(100);
	e = get_bstatus(dp, &bstatus);
	if (!e && (bstatus & BSTATUS_LINK_INTEG_FAIL)) {
		dphdcp_err("link integrity failure\n");
		mutex_lock(&dphdcp->lock);
		goto failure;
	}
	/* revocation check for receiver. For repeater, is it
	 * handled in verify V'
	 */
	if (!(b_caps & BCAPS_REPEATER)) {
		e = srm_revocation_check(dphdcp);
		if (e) {
			dphdcp_err("SRM revocation check failed\n");
			goto failure;
		}
	}
	/*
	 * part 2 of authentication protocol, if receiver is
	 * a repeater
	 */
	if ((b_caps & BCAPS_REPEATER) && !vprime_check_done) {
		e = get_repeater_info(dphdcp);
		if (e) {
			dphdcp_err("get repeater info failed\n");
			/* some latency before we transition to the
			 * HDMI engine
			 */
			msleep(100);
			repeater_flag = true;
			mutex_lock(&dphdcp->lock);
			goto failure;
		}
		/* continue last part of authentication as
		 * a DP receiver, ie. second stage of
		 * authentication will not be performed
		 */
		repeater_flag = false;
		mutex_lock(&dphdcp->lock);
		if (tegra_dc_is_nvdisplay()) {
			*pkt = HDCP_TA_CMD_CTRL;
			*(pkt + 1*HDCP_CMD_OFFSET) = TEGRA_NVHDCP_PORT_DP;
			*(pkt + 2*HDCP_CMD_OFFSET) = HDCP_TA_CTRL_DISABLE;
			*(pkt + 3*HDCP_CMD_OFFSET) = repeater_flag;
			*(pkt + 4*HDCP_CMD_OFFSET) = dphdcp->dp->sor->ctrl_num;
			e = te_launch_trusted_oper(pkt, PKT_SIZE, ta_cmd, dphdcp->ta_ctx);
			if (e) {
				dphdcp_err("te_launch_oper failed with err: %d\n", e);
				goto failure;
			}
		} else {
			tmp = tegra_sor_readl_ext(sor, NV_SOR_DP_HDCP_CTRL);
			tmp |= CRYPT_ENABLED;
			tegra_sor_writel_ext(sor, NV_SOR_DP_HDCP_CTRL, tmp);
		}
		goto repeater_auth;
	}

	mutex_lock(&dphdcp->lock);
	dphdcp->state = STATE_LINK_VERIFY;
	dphdcp_info("link verified!\n");

	while (1) {
		if (!dphdcp_is_plugged(dphdcp))
			goto lost_dp;

		if (dphdcp->state != STATE_LINK_VERIFY)
			goto failure;

		mutex_unlock(&dphdcp->lock);
		/* check for link integrity failure */
		e = get_bstatus(dp, &bstatus);
		if (!e && (bstatus & BSTATUS_LINK_INTEG_FAIL)) {
			dphdcp_err("link integrity failure\n");
			mutex_lock(&dphdcp->lock);
			goto failure;
		}
		tegra_dc_io_end(dc);
		wait_event_interruptible_timeout(wq_worker,
				!dphdcp_is_plugged(dphdcp), msecs_to_jiffies(200));
		tegra_dc_io_start(dc);
		mutex_lock(&dphdcp->lock);

	}

failure:
	dphdcp->fail_count++;
	if (dphdcp->fail_count > dphdcp->max_retries)
		dphdcp_err("dphdcp failure- too many failures, giving up\n");
	else {
		dphdcp_err("dphdcp failure- renegotiating in 1 second\n");
		if (!dphdcp_is_plugged(dphdcp))
			goto lost_dp;

		queue_delayed_work(dphdcp->downstream_wq, &dphdcp->work,
						msecs_to_jiffies(1000));
	}

	/* Failed because of lack of memory */
	if (e == -ENOMEM) {
		kfree(pkt);
		return;
	}
lost_dp:
	dphdcp_info("lost dp connection\n");
	dphdcp->state = STATE_UNAUTHENTICATED;
	if (tegra_dc_is_nvdisplay()) {
		if (pkt) {
			*pkt = HDCP_TA_CMD_CTRL;
			*(pkt + HDCP_CMD_OFFSET) = TEGRA_NVHDCP_PORT_DP;
			*(pkt + 2*HDCP_CMD_OFFSET) = HDCP_TA_CTRL_DISABLE;
			*(pkt + 3*HDCP_CMD_OFFSET) = repeater_flag;
			*(pkt + 4*HDCP_CMD_OFFSET) = dphdcp->dp->sor->ctrl_num;
		}
		/* a launch operation makes sense only if a valid context exists
		 * already
		 */
		if (dphdcp->ta_ctx) {
			e = te_launch_trusted_oper(pkt, PKT_SIZE, ta_cmd,
					dphdcp->ta_ctx);
			if (e) {
				dphdcp_err("te_launch_oper failed with error:"
						"%d\n", e);
				goto failure;
			}
		}
	} else {
		hdcp_ctrl_run(sor, 0);
	}

err:
	mutex_unlock(&dphdcp->lock);
	kfree(pkt);
	if (dphdcp->ta_ctx) {
		te_close_trusted_session(dphdcp->ta_ctx);
		dphdcp->ta_ctx = NULL;
	}
	tegra_dc_io_end(dc);
	return;
disable:
	dphdcp->state = STATE_OFF;
	kfree(pkt);
	if (dphdcp->ta_ctx) {
		te_close_trusted_session(dphdcp->ta_ctx);
		dphdcp->ta_ctx = NULL;
	}
	dphdcp_set_plugged(dphdcp, false);
	mutex_unlock(&dphdcp->lock);
	tegra_dc_io_end(dc);
}

#ifdef DPHDCP22
/* HDCP 2.2 over display port */

/* write N bytes of data over AUX channel */
static int tegra_dphdcp_write_n(struct tegra_dc_dp_data *dp, u32 reg,
						u64 *data, u8 size)
{
	u8 *buf = NULL;
	u8 *orig_buf = NULL;
	int e;

	buf = kmalloc(size, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	orig_buf = buf;
	memcpy(buf, data, size);
	if (size <= MAX_AUX_SIZE) {
		e = tegra_dphdcp_write(dp, reg, buf, size);
		if (e) {
			dphdcp_err("Error writing over AUX\n");
			goto error;
		}
	} else {
		while (size > MAX_AUX_SIZE) {
			e = tegra_dphdcp_write(dp, reg, buf, MAX_AUX_SIZE);
			if (e) {
				dphdcp_err("Error writing over AUX\n");
				goto error;
			}
			size -= MAX_AUX_SIZE;
			buf += MAX_AUX_SIZE;
		}
		if (size) {
			e = tegra_dphdcp_write(dp, reg, buf, size);
			if (e) {
				dphdcp_err("Error writing over AUX\n");
				goto error;
			}
		}
	}

error:
	kfree(orig_buf);
	return e;
}

/* read N bytes of data over AUX channel */
static int tegra_dphdcp_read_n(struct tegra_dc_dp_data *dp, u32 cmd,
						u64 *data, int size)
{
	u8 *buf;
	int e;
	u8 *orig_buf;
	u32 status;

	buf = kmalloc(size, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	orig_buf = buf;
	if (size <= MAX_AUX_SIZE) {
		e = tegra_dphdcp_read(dp, cmd, buf, size, &status);
		if (e) {
			dphdcp_err("Error reading over AUX\n");
			goto error;
		}
	} else {
		while (size > MAX_AUX_SIZE) {
			e = tegra_dphdcp_read(dp, cmd, buf,
					MAX_AUX_SIZE, &status);
			if (e) {
				dphdcp_err("Error reading over AUX\n");
				goto error;
			}
			size -= MAX_AUX_SIZE;
			buf += MAX_AUX_SIZE;
		}
		if (size) {
			e = tegra_dphdcp_write(dp, cmd, buf, MAX_AUX_SIZE);
			if (e) {
				dphdcp_err("Error reading over AUX\n");
				goto error;
			}
		}
	}
	/* copy the content to data */
	memcpy(data, orig_buf, size);

error:
	kfree(orig_buf);
	return e;
}

static int get_rxstatus(struct tegra_dc_dp_data *dp, u8 *rxstatus)
{
	u32 status;

	return tegra_dphdcp_read(dp, NV_DPCD_HDCP_RXSTATUS,
					rxstatus, 1, &status);
}

static int dphdcp_ake_init_rtx_send(struct tegra_dc_dp_data *dp, u64 *buf)
{
	/* write 8 bytes of rtx */
	return tegra_dphdcp_write_n(dp, NV_DPCD_HDCP_RTX_OFFSET, buf, 8);
}

static int dphdcp_ake_init_txcaps_send(struct tegra_dc_dp_data *dp, u64 *buf)
{
	/* write 3 bytes of txcaps */
	return tegra_dphdcp_write_n(dp, NV_DPCD_HDCP_TXCAPS_OFFSET, buf, 3);
}

static int dphdcp_ake_cert_recv_rx(struct tegra_dc_dp_data *dp, u8 *buf)
{
	/* read 522 bytes of receiver certificate */
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_CERT_RX_OFFSET,
						(u64 *)buf, 522);
}

static int dphdcp_ake_cert_recv_rrx(struct tegra_dc_dp_data *dp, u64 *buf)
{
	/* write 8 bytes of pseudo random number */
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_CERT_RRX_OFFSET,
							buf, 8);
}

static int dphdcp_ake_cert_recv_rxcaps(struct tegra_dc_dp_data *dp, u16 *buf)
{
	/* write 3 bytes of receiver capability */
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_CERT_RXCAPS_OFFSET,
							(u64 *)buf, 3);
}

static int dphdcp_ake_no_stored_km_send(struct tegra_dc_dp_data *dp, u64 *buf)
{
	return tegra_dphdcp_write_n(dp, NV_DPCD_HDCP_EKM_NOSTORED, buf, 128);
}

static int dphdcp_ake_hprime_receive(struct tegra_dc_dp_data *dp, u32 *buf)
{
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_HPRIME,
					(u64 *)buf, 32);
}

static int dphdcp_lc_init_send(struct tegra_dc_dp_data *dp, u64 *buf)
{
	return tegra_dphdcp_write_n(dp, NV_DPCD_HDCP_RN, buf, 8);
}

static int dphdcp_ake_pairing_info_receive(struct tegra_dc_dp_data *dp,
							u64 *buf)
{
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_LPRIME,
						buf, 16);
}

static int dphdcp_lc_lprime_receive(struct tegra_dc_dp_data *dp, u64 *buf)
{
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_LPRIME,
						buf, 32);
}

static int dphdcp_ske_eks_send(struct tegra_dc_dp_data *dp, u64 *buf)
{
	return tegra_dphdcp_write_n(dp, NV_DPCD_HDCP_EKS, buf, 16);
}

static int dphdcp_ske_riv_send(struct tegra_dc_dp_data *dp, u64 *buf)
{
	return tegra_dphdcp_write_n(dp, NV_DPCD_HDCP_RIV, buf, 8);
}

static int dphdcp_rxinfo_recv(struct tegra_dc_dp_data *dp, u16 *buf)
{
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_RXINFO, (u64 *)buf, 2);
}

static int dphdcp_seqnum_recv(struct tegra_dc_dp_data *dp, u64 *buf)
{
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_SEQNUM_V, buf, 3);
}

static int dphdcp_vprime_recv(struct tegra_dc_dp_data *dp, u16 *buf)
{
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_VPRIME, (u64 *)buf, 16);
}

static int dphdcp_recvrid_list_recv(struct tegra_dc_dp_data *dp, u64 *buf)
{
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_RX_ID_LIST, buf, 635);
}

static int dphdcp_rptr_ack_send(struct tegra_dc_dp_data *dp, u64 *buf)
{
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_V, buf, 16);
}

static int dphdcp_rptr_seqnum_send(struct tegra_dc_dp_data *dp, u8 *buf)
{
	return tegra_dphdcp_write_n(dp, NV_DPCD_HDCP_SEQ_NUM_M,
						(u64 *)buf, 3);
}

static int dphdcp_rptr_k_send(struct tegra_dc_dp_data *dp, u16 *buf)
{
	return tegra_dphdcp_write_n(dp, NV_DPCD_HDCP_K,
					(u64 *)&buf, 2);
}

static int dphdcp_rptr_strmid_type_send(struct tegra_dc_dp_data *dp, u16 *buf)
{
	/* max streams: 1 */
	return tegra_dphdcp_write_n(dp, NV_DPCD_HDCP_STRMID_TYPE,
					(u64 *)buf, 2);
}

static int dphdcp_rptr_stream_ready_recv(struct tegra_dc_dp_data *dp, u8 *buf)
{
	return tegra_dphdcp_read_n(dp, NV_DPCD_HDCP_MPRIME, (u64 *)buf, 32);
}

/* poll for status until timeout */
static int dphdcp_poll(struct tegra_dc_dp_data *dp, int timeout, int status)
{
	int e;
	s64 start_time;
	s64 end_time;
	struct timespec tm;
	u8 val;
	u32 aux_stat;

	ktime_get_ts(&tm);
	start_time = timespec_to_ns(&tm);

	while (1) {
		ktime_get_ts(&tm);
		end_time = timespec_to_ns(&tm);
		if ((end_time - start_time)/1000 >= timeout*1000)
			return -ETIMEDOUT;
		e = tegra_dphdcp_read(dp,
		NV_DPCD_HDCP_RXSTATUS, &val, 1, &aux_stat);
		if (e) {
			dphdcp_err("dphdcp_poll_ready failed\n");
			goto exit;
		}
		if (status == HDCP_READY) {
			if (val)
				break;
		} else if (status == HDCP_REAUTH) {
			if (cpu_to_be16(val) & HDCP_REAUTH_MASK)
				break;
		}
	}
	e = 0;
exit:
	return e;
}

static int dphdcp_poll_ready(struct tegra_dc_dp_data *dp,
					int timeout)
{
	int e;

	e = dphdcp_poll(dp, timeout, HDCP_READY);
	return e;
}

static int tsec_hdcp_authentication(struct tegra_dc_dp_data *dp,
				struct hdcp_context_t *hdcp_context)
{
	int err = 0;
	u8 version = 0x02;
	u16 caps = 0;
	u16 txcaps = 0x0;
	u64 txval;
	u32 *buf;

	err = tsec_hdcp_readcaps(hdcp_context);
	if (err)
		goto exit;
	err = tsec_hdcp_init(hdcp_context);
	if (err)
		goto exit;
	/* rtx populated in hdcp create session */
	err = tsec_hdcp_create_session(hdcp_context, DISPLAY_TYPE_DP,
					dphdcp->dp->sor->ctrl_num);
	if (err)
		goto exit;
	err = tsec_hdcp_exchange_info(hdcp_context,
		HDCP_EXCHANGE_INFO_GET_TMTR_INFO, &version, &caps);
	if (err)
		goto exit;
	hdcp_context->msg.txcaps_version = version;
	hdcp_context->msg.txcaps_capmask = txcaps;
	/* initiate authentication with 64 bit pseudo random # */
	err = dphdcp_ake_init_rtx_send(dp, &hdcp_context->msg.rtx);
	if (err)
		goto exit;
	txval = (version << 16) | txcaps;
	/* write the txcaps register */
	err = dphdcp_ake_init_txcaps_send(dp, &txval);
	if (err)
		goto exit;

	/* wait for 100 ms before reading the RX */
	err = dphdcp_poll_ready(dp, 100);
	if (err)
		goto exit;

	/* process upon CP_IRQ interrupt */
	/* read the certificate from the rx */
	err = dphdcp_ake_cert_recv_rx(dp, hdcp_context->msg.cert_rx);
	if (err)
		goto exit;

	err = dphdcp_ake_cert_recv_rrx(dp, &hdcp_context->msg.rrx);
	if (err)
		goto exit;

	err = dphdcp_ake_cert_recv_rxcaps(dp,
		&hdcp_context->msg.rxcaps_capmask);
	if (err)
		goto exit;

	/* verify received certificate */
	err = tsec_hdcp_verify_cert(hdcp_context);
	if (err)
		goto exit;

	err = tsec_hdcp_update_rrx(hdcp_context);
	if (err)
		goto exit;

	err = tsec_hdcp_generate_ekm(hdcp_context);
	if (err)
		goto exit;

	err = dphdcp_ake_no_stored_km_send(dp, (u64 *)hdcp_context->msg.ekm);
	if (err)
		goto exit;

	err = tsec_hdcp_exchange_info(hdcp_context,
		HDCP_EXCHANGE_INFO_SET_RCVR_INFO,
		&hdcp_context->msg.rxcaps_version,
		&hdcp_context->msg.rxcaps_capmask);

	if (err)
		goto exit;

	/* device in revocation list ? */
	err = tsec_hdcp_revocation_check(hdcp_context);
	if (err)
		goto exit;
	/* H' should be ready within 1 sec */
	err = dphdcp_poll_ready(dp, 1000);
	if (err)
		goto exit;
	buf = (u32 *)hdcp_context->msg.hprime;
	err = dphdcp_ake_hprime_receive(dp, buf);
	if (err)
		goto exit;

	err = tsec_hdcp_verify_hprime(hdcp_context);
	if (err)
		goto exit;
	/* wait for AKE pairing message to be ready */
	err = dphdcp_poll_ready(dp, 200);
	if (err)
		goto exit;

	err = dphdcp_ake_pairing_info_receive(dp,
		(u64 *)hdcp_context->msg.ekhkm);
	if (err)
		goto exit;

	err = tsec_hdcp_encrypt_pairing_info(hdcp_context);
	if (err)
		goto exit;

	err = tsec_hdcp_encrypt_pairing_info(hdcp_context);
	if (err)
		goto exit;

	err = tsec_hdcp_generate_lc_init(hdcp_context);
	if (err)
		goto exit;

	err = dphdcp_lc_init_send(dp, &hdcp_context->msg.rn);
	if (err)
		goto exit;

	err = dphdcp_poll_ready(dp, 7);
	if (err)
		goto exit;

	err = dphdcp_lc_lprime_receive(dp, (u64 *)hdcp_context->msg.lprime);
	if (err)
		goto exit;

	err = tsec_hdcp_verify_lprime(hdcp_context);
	if (err)
		goto exit;

	err = tsec_hdcp_ske_init(hdcp_context);
	if (err)
		goto exit;

	err = dphdcp_ske_eks_send(dp, (u64 *)hdcp_context->msg.eks);
	if (err)
		goto exit;

	err = dphdcp_ske_riv_send(dp, (u64 *)hdcp_context->msg.riv);
	if (err)
		goto exit;

	/* check if the receiver is a repeater */
	if (hdcp_context->msg.rxcaps_capmask & HDCP_22_REPEATER) {
		dp->dphdcp->repeater = 1;
		err = dphdcp_poll_ready(dp, 3000);
		if (err)
			goto exit;

		/* read receiver id list */
		err = dphdcp_rxinfo_recv(dp, &hdcp_context->msg.rxinfo);
		if (err)
			goto exit;

		err = dphdcp_seqnum_recv(dp, (u64 *)hdcp_context->msg.seq_num);
		if (err)
			goto exit;

		err = dphdcp_vprime_recv(dp, &hdcp_context->msg.rxinfo);
		if (err)
			goto exit;

		err = dphdcp_recvrid_list_recv(dp,
			(u64 *)&hdcp_context->msg.rxinfo);
		if (err)
			goto exit;

		err = tsec_hdcp_verify_vprime(hdcp_context);
		if (err)
			goto exit;

		/* send ack for repeater auth */
		err = dphdcp_rptr_ack_send(dp, (u64 *)hdcp_context->msg.v);
		if (err)
			goto exit;

		err = tsec_hdcp_rptr_stream_manage(hdcp_context);
		if (err)
			goto exit;
		/* One stream */
		hdcp_context->msg.k = 0x0100;
		/* STREAM_ID and Type are 0 */
		hdcp_context->msg.streamid_type[0] = 0x0000;
		/* stream management information */
		err = dphdcp_rptr_seqnum_send(dp,
			hdcp_context->msg.seq_num_m);
		if (err)
			goto exit;

		err = dphdcp_rptr_k_send(dp,
			&hdcp_context->msg.k);
		if (err)
			goto exit;

		err = dphdcp_rptr_strmid_type_send(dp,
			hdcp_context->msg.streamid_type);
		if (err)
			goto exit;

		/* repeater auth stream ready information */
		err = dphdcp_rptr_stream_ready_recv(dp,
				hdcp_context->msg.mprime);
		if (err)
			goto exit;

	}
	dphdcp_info("HDCP authentication successful\n");
exit:
	if (err)
		dphdcp_err("HDCP authentication failed with err %d\n", err);
	return err;
}

static void dphdcp2_downstream_worker(struct work_struct *work)
{
	struct tegra_dphdcp *dphdcp =
		container_of(to_delayed_work(work),
		 struct tegra_dphdcp, work);
	struct tegra_dc_dp_data *dp = dphdcp->dp;
	struct hdcp_context_t hdcp_context;
	struct tegra_dc *dc = dp->dc;
	int e;
	u8 rxstatus;

	e = tsec_hdcp_create_context(&hdcp_context);
	if (e)
		goto err;

	dphdcp_vdbg("%s():started thread %s\n", __func__, dphdcp->name);
	tegra_dc_io_start(dc);

	mutex_lock(&dphdcp->lock);
	if (dphdcp->state == STATE_OFF) {
		dphdcp_err("dphdcp failure: giving up\n");
		goto err;
	}
	dphdcp->state = STATE_UNAUTHENTICATED;

	/* check plug state to terminate early in case of flush_workqueue */
	if (!dphdcp_is_plugged(dphdcp)) {
		dphdcp_err("worker started in unplugged state\n");
		goto lost_dp;
	}
	dphdcp_vdbg("%s():hpd=%d\n", __func__, dphdcp->plugged);
	mutex_unlock(&dphdcp->lock);

	if (tsec_hdcp_authentication(dp, &hdcp_context)) {
		mutex_lock(&dphdcp->lock);
		goto failure;
	}

	mutex_lock(&dphdcp->lock);
	dphdcp->state = STATE_LINK_VERIFY;
	mutex_unlock(&dphdcp->lock);

	e = tsec_hdcp_session_ctrl(&hdcp_context,
			HDCP_SESSION_CTRL_FLAG_ACTIVATE);
	if (e) {
		dphdcp_err("tsec hdcp session ctrl failed\n");
		mutex_lock(&dphdcp->lock);
		goto failure;
	}
	dphdcp_info("HDCP 2.2 CRYPT enabled\n");
	mutex_lock(&dphdcp->lock);
	while (1) {
		if (!dphdcp_is_plugged(dphdcp))
			goto lost_dp;

		if (dphdcp->state != STATE_LINK_VERIFY)
			goto failure;

		mutex_unlock(&dphdcp->lock);

		/* link integrity check */
		e = get_rxstatus(dp, &rxstatus);
		if (!e && (rxstatus & HDCP_LINK_INTEG_FAIL)) {
			dphdcp_err("link integrity failure\n");
			mutex_lock(&dphdcp->lock);
			goto failure;
		}
		tegra_dc_io_end(dc);
		wait_event_interruptible_timeout(wq_worker,
		!dphdcp_is_plugged(dphdcp), msecs_to_jiffies(200));
		tegra_dc_io_start(dc);
		mutex_lock(&dphdcp->lock);
	}

failure:
	dphdcp->fail_count++;
	if (dphdcp->fail_count > dphdcp->max_retries) {
		dphdcp_err("dphdcp failure - too many failures, giving up\n");
	} else {
		dphdcp_err("dphdcp failure - renegotiating in 1 sec\n");
		if (!dphdcp_is_plugged(dphdcp))
			goto lost_dp;

		queue_delayed_work(dphdcp->downstream_wq, &dphdcp->work,
						msecs_to_jiffies(1000));
	}

lost_dp:
	dphdcp->state = STATE_UNAUTHENTICATED;

err:
	mutex_unlock(&dphdcp->lock);
	tegra_dc_io_end(dc);
	e = tsec_hdcp_free_context(&hdcp_context);
}
#endif

static int tegra_dphdcp_on(struct tegra_dphdcp *dphdcp)
{
	u8 hdcp2version = 0;
	int e;
	u32 status;
	struct tegra_dc_dp_data *dp = dphdcp->dp;

	dphdcp->state = STATE_UNAUTHENTICATED;
	if (dphdcp_is_plugged(dphdcp) &&
		atomic_read(&dphdcp->policy) !=
		TEGRA_DC_HDCP_POLICY_ALWAYS_OFF) {
		dphdcp->fail_count = 0;
		e = tegra_dphdcp_read(dp, HDCP_HDCP2_VERSION,
				&hdcp2version, 1, &status);
		if (e)
			return -EIO;

		dphdcp_vdbg("read back version:%x\n", hdcp2version);
		if (hdcp2version & HDCP_HDCP2_VERSION_HDCP22_YES) {
#ifdef DPHDCP22
			INIT_DELAYED_WORK(&dphdcp->work,
					dphdcp2_downstream_worker);
#endif
			dphdcp->hdcp22 = HDCP22_PROTOCOL;
		} else {
			INIT_DELAYED_WORK(&dphdcp->work,
					dphdcp_downstream_worker);
			dphdcp->hdcp22 = HDCP1X_PROTOCOL;
		}

		queue_delayed_work(dphdcp->downstream_wq, &dphdcp->work,
						msecs_to_jiffies(100));
	}

	return 0;
}

static int tegra_dphdcp_off(struct tegra_dphdcp *dphdcp)
{
	mutex_lock(&dphdcp->lock);
	dphdcp->state = STATE_OFF;
	dphdcp_set_plugged(dphdcp, false);
	mutex_unlock(&dphdcp->lock);
	wake_up_interruptible(&wq_worker);
	cancel_delayed_work_sync(&dphdcp->work);
	return 0;
}

static int get_dphdcp_state(struct tegra_dphdcp *dphdcp,
			struct tegra_nvhdcp_packet *pkt)
{
	int i;

	mutex_lock(&dphdcp->lock);
	if (dphdcp->state != STATE_LINK_VERIFY) {
		memset(pkt, 0, sizeof(struct tegra_nvhdcp_packet));
		pkt->packet_results = TEGRA_NVHDCP_RESULT_LINK_FAILED;
	} else {
		pkt->num_bksv_list = dphdcp->num_bksv_list;
		for (i = 0; i < pkt->num_bksv_list; i++)
			pkt->bksv_list[i] = dphdcp->bksv_list[i];
		pkt->binfo = dphdcp->binfo;
		pkt->b_ksv = dphdcp->b_ksv;
		memcpy(pkt->v_prime, dphdcp->v_prime, sizeof(dphdcp->v_prime));
		pkt->packet_results = TEGRA_NVHDCP_RESULT_SUCCESS;
		pkt->hdcp22 = dphdcp->hdcp22;
		pkt->port = TEGRA_NVHDCP_PORT_DP;
	}
	pkt->sor = dphdcp->dp->sor->ctrl_num;
	mutex_unlock(&dphdcp->lock);
	return 0;
}

int tegra_dphdcp_set_policy(struct tegra_dphdcp *dphdcp, int pol)
{
	if (pol == TEGRA_DC_HDCP_POLICY_ALWAYS_ON) {
		dphdcp_info("using \"always on\" policy.\n");
		if (atomic_xchg(&dphdcp->policy, pol) != pol) {
			/* policy changed, start working */
			tegra_dphdcp_on(dphdcp);
		}
	} else if (pol == TEGRA_DC_HDCP_POLICY_ALWAYS_OFF) {
		dphdcp_info("using \"always off\" policy.\n");
		if (atomic_xchg(&dphdcp->policy, pol) != pol) {
			/* policy changed, stop working */
			tegra_dphdcp_off(dphdcp);
		}
	} else {
		/* unsupported policy */
		return -EINVAL;
	}

	return 0;
}

static int tegra_dphdcp_renegotiate(struct tegra_dphdcp *dphdcp)
{
	mutex_lock(&dphdcp->lock);
	dphdcp->state = STATE_RENEGOTIATE;
	mutex_unlock(&dphdcp->lock);
	tegra_dphdcp_on(dphdcp);
	return 0;
}

void tegra_dphdcp_set_plug(struct tegra_dphdcp *dphdcp, bool hpd)
{
	if (tegra_dc_is_t19x()) {
		uint32_t ft_info;
		/* enable HDCP only if board has SFK */
		tegra_fuse_readl(FUSE_OPT_FT_REV_0, &ft_info);
		/* only fuses with revision id >= 0x5 have SFK */
		if (ft_info < FUSE_START_SFK) {
			dphdcp_err("Device does not have SFK!");
			return;
		}
	}
	/* ensure all previous values are reset on hotplug */
	vprime_check_done = false;
	repeater_flag = false;
	dphdcp_debug("DP hotplug detected (hpd = %d)\n", hpd);
	if (hpd) {
		dphdcp_set_plugged(dphdcp, true);
		tegra_dphdcp_on(dphdcp);
	} else {
		tegra_dphdcp_off(dphdcp);
	}
}

static long dphdcp_dev_ioctl(struct file *filp,
			unsigned int cmd, unsigned long arg)
{
	struct tegra_dphdcp *dphdcp;
	struct tegra_nvhdcp_packet *pkt;
	struct tegra_dc_dp_data *dp;
	int e = -ENOTTY;

	if (!filp)
		return -EINVAL;

	dphdcp = filp->private_data;
	dp = dphdcp->dp;

	switch (cmd) {
	case TEGRAIO_NVHDCP_ON:
		mutex_lock(&dphdcp->lock);
		dphdcp_set_plugged(dphdcp, dp->enabled);
		mutex_unlock(&dphdcp->lock);
		return tegra_dphdcp_on(dphdcp);

	case TEGRAIO_NVHDCP_OFF:
		return tegra_dphdcp_off(dphdcp);

	case TEGRAIO_NVHDCP_SET_POLICY:
		mutex_lock(&dphdcp->lock);
		dphdcp_set_plugged(dphdcp, dp->enabled);
		mutex_unlock(&dphdcp->lock);
		return tegra_dphdcp_set_policy(dphdcp, arg);

	case TEGRAIO_NVHDCP_RENEGOTIATE:
		mutex_lock(&dphdcp->lock);
		dphdcp_set_plugged(dphdcp, dp->enabled);
		mutex_unlock(&dphdcp->lock);
		e = tegra_dphdcp_renegotiate(dphdcp);
		break;

	case TEGRAIO_NVHDCP_HDCP_STATE:
		pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
		if (!pkt) {
			kfree(pkt);
			return -ENOMEM;
		}
		e = get_dphdcp_state(dphdcp, pkt);
		if (copy_to_user((void __user *)arg, pkt, sizeof(*pkt))) {
			kfree(pkt);
			return -EFAULT;
		}
		kfree(pkt);
		return e;

	}
	return e;
}

static int dphdcp_dev_open(struct inode *inode, struct file *filp)
{
	struct miscdevice *miscdev = filp->private_data;
	struct tegra_dphdcp *dphdcp =
		container_of(miscdev, struct tegra_dphdcp, miscdev);
	filp->private_data = dphdcp;
	return 0;
}

static int dphdcp_dev_release(struct inode *inode, struct file *filp)
{
	filp->private_data = NULL;
	return 0;
}

static const struct file_operations dphdcp_fops = {
	.owner			= THIS_MODULE,
	.llseek			= no_llseek,
	.unlocked_ioctl = dphdcp_dev_ioctl,
	.open			= dphdcp_dev_open,
	.release        = dphdcp_dev_release,
#ifdef CONFIG_COMPAT
	.compat_ioctl	= dphdcp_dev_ioctl,
#endif
};
/* we only support one AP right now, so should only call this once. */
struct tegra_dphdcp *tegra_dphdcp_create(struct tegra_dc_dp_data *dp,
			int id, int bus)
{
	struct tegra_dphdcp *dphdcp;
	int e;
	int num_heads;

	num_heads = tegra_dc_get_numof_dispheads();

	if (id >= num_heads) {
		dphdcp_err("head id greater than what's available!");
		return ERR_PTR(-EMFILE);
	}

	/* ensure memory allocated once */
	if (!dphdcp_head) {
		dphdcp_head =
		kzalloc(sizeof(void *) * num_heads, GFP_KERNEL);
		if (!dphdcp_head)
			return ERR_PTR(-ENOMEM);
	}

	dphdcp = dphdcp_head[id];
	/* do not allow multiple node creation */
	if (dphdcp)
		return ERR_PTR(-EMFILE);

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

	dphdcp->id = id;
	snprintf(dphdcp->name, sizeof(dphdcp->name), "nvhdcp%u", id);
	dphdcp->dp = dp;
	mutex_init(&dphdcp->lock);

	strlcpy(dphdcp->info.type, dphdcp->name, sizeof(dphdcp->info.type));
	dphdcp->bus = bus;
	dphdcp->fail_count = 0;
	dphdcp->max_retries = HDCP_MAX_RETRIES;
	dphdcp->hpd = 0;
	atomic_set(&dphdcp->policy, dp->dc->pdata->default_out->hdcp_policy);

	dphdcp->state = STATE_UNAUTHENTICATED;

	dphdcp->downstream_wq = create_singlethread_workqueue(dphdcp->name);

	dphdcp->miscdev.minor = MISC_DYNAMIC_MINOR;
	dphdcp->miscdev.name = dphdcp->name;
	dphdcp->miscdev.fops = &dphdcp_fops;
	/* register miscellaneous device */
	e = misc_register(&dphdcp->miscdev);
	if (e) {
		dphdcp_err("cannot register\n");
		goto free_workqueue;
	}

	dphdcp_head[id] = dphdcp;
	dphdcp_vdbg("%s(): created misc device %s\n", __func__, dphdcp->name);

	return dphdcp;

free_workqueue:
	destroy_workqueue(dphdcp->downstream_wq);
	return ERR_PTR(e);
}

void tegra_dphdcp_destroy(struct tegra_dphdcp *dphdcp)
{
	misc_deregister(&dphdcp->miscdev);
	tegra_dphdcp_off(dphdcp);
	destroy_workqueue(dphdcp->downstream_wq);
	dphdcp_head[dphdcp->id] = NULL;
	kfree(dphdcp);
}

#ifdef CONFIG_TEGRA_DEBUG_DP_HDCP
/* show current maximum number of retries for HDCP DP authentication */
static int tegra_dp_max_retries_dbg_show(struct seq_file *m, void *unused)
{
	struct tegra_dphdcp *dphdcp = m->private;

	if (WARN_ON(!dphdcp))
		return -EINVAL;

	seq_printf(m, "hdcp max_retries value: %d\n", dphdcp->max_retries);

	return 0;
}

/* show current hotplug state */
static int tegra_dp_hotplug_dbg_show(struct seq_file *m, void *unused)
{
	struct tegra_dphdcp *dphdcp = m->private;

	if (WARN_ON(!dphdcp))
		return -EINVAL;

	seq_printf(m, "hotplug value set to: %d\n", dphdcp->hpd);

	return 0;
}

/*
 * sw control for hdcp max retries.
 * 5 is the normal number of max retries.
 * 1 is the minimum number of retries.
 * 5 is the maximum number of retries.
 * sw should keep the number of retries to 5 until unless a change is required
 */
static ssize_t tegra_dp_max_retries_dbg_write(struct file *file,
						const char __user *addr,
						size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data;
	struct tegra_dphdcp *dphdcp = m->private;
	u8 new_max_retries;
	int ret;

	if (WARN_ON(!dphdcp))
		return -EINVAL;

	ret = kstrtou8_from_user(addr, len, 6, &new_max_retries);
	if (ret < 0)
		return ret;

	if (new_max_retries >= HDCP_MIN_RETRIES &&
		new_max_retries <= HDCP_MAX_RETRIES)
		dphdcp->max_retries = new_max_retries;

	return len;
}

/*
 * sw control for hotplug on and off
 * 1: turn hotplug on
 * 0: turn hotplug off
 */
static ssize_t tegra_dp_hotplug_dbg_write(struct file *file,
					const char __user *addr,
					size_t len, loff_t *pos)
{
	struct seq_file *m = file->private_data;
	struct tegra_dphdcp *dphdcp = m->private;
	u8 new_hpd;
	int ret;

	if (WARN_ON(!dphdcp))
		return -EINVAL;

	ret = kstrtou8_from_user(addr, len, 6, &new_hpd);
	if (ret < 0)
		return ret;

	if (new_hpd > 0) {
		/* start downstream_worker */
		dphdcp_set_plugged(dphdcp, true);
		tegra_dphdcp_on(dphdcp);
		dphdcp->hpd = new_hpd;
	}
	return len;
}

static int tegra_dp_max_retries_dbg_open(struct inode *inode,
		struct file *file)
{
	if (!inode || !file)
		return -EINVAL;

	return single_open(file, tegra_dp_max_retries_dbg_show,
			inode->i_private);
}

static int tegra_dp_hotplug_dbg_open(struct inode *inode,
		struct file *file)
{
	if (!inode || !file)
		return -EINVAL;

	return single_open(file, tegra_dp_hotplug_dbg_show,
			inode->i_private);
}

static const struct file_operations tegra_dp_max_retries_dbg_ops = {
	.open = tegra_dp_max_retries_dbg_open,
	.read = seq_read,
	.write = tegra_dp_max_retries_dbg_write,
	.llseek = seq_lseek,
	.release = single_release,
};

static const struct file_operations tegra_dp_hotplug_dbg_ops = {
	.open = tegra_dp_hotplug_dbg_open,
	.read = seq_read,
	.write = tegra_dp_hotplug_dbg_write,
	.llseek = seq_lseek,
	.release = single_release,
};

void tegra_dphdcp_debugfs_init(struct tegra_dphdcp *dphdcp)
{
	struct dentry *dir, *ret;

	if (!dphdcp)
		goto fail;

	dir = debugfs_create_dir("tegra_dphdcp",  NULL);

	ret = debugfs_create_file("max_retries", 0444, dir,
				dphdcp, &tegra_dp_max_retries_dbg_ops);
	if (IS_ERR_OR_NULL(ret))
		goto fail;

	ret = debugfs_create_file("hotplug", 0444, dir,
				dphdcp, &tegra_dp_hotplug_dbg_ops);
	if (IS_ERR_OR_NULL(ret))
		goto fail;

fail:
	debugfs_remove_recursive(dir);
}
#else
void tegra_dphdcp_debugfs_init(struct tegra_dphdcp *dphdcp)
{
}
#endif