1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
|
/*
* Copyright (C) 2012 Samsung Electronics
*
* Author: Sangmi Park <sm0327.park@samsung.com>
* Kyungrok Min <gyoungrok.min@samsung.com>
* Date: 2:06 PM, 1st June,2012
*
* Based on drivers/video/sii9234.c[Google AOSP(Galaxy Nexus/Tuna)]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/err.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/gpio.h>
#include <linux/mutex.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/power_supply.h>
#include <linux/battery/sec_charger.h>
#include <linux/switch.h>
#if defined(CONFIG_MUIC_NOTIFIER)
#include <linux/muic/muic.h>
#include <linux/muic/muic_notifier.h>
#endif /* CONFIG_MUIC_NOTIFIER */
#include <linux/input.h>
#include <video/edid.h>
#include "../../edid.h"
#include "sii8240_rcp.h"
#include "sii8240_driver.h"
#include "sii8240_platform.h"
#include "sii8240_timing.h"
#include <linux/power_supply.h>
#include <linux/regulator/consumer.h>
#ifdef CONFIG_OF
#include <linux/of_device.h>
#include <linux/of_gpio.h>
static struct device *sii8240_mhldev;
static struct sii8240_data *g_sii8240;
#endif
#define CONFIG_MHL_SWING_LEVEL
static bool check_vbus_present(void);
static int mhl_write_byte_reg(struct i2c_client *client, u32 offset, u8 value)
{
int ret = i2c_smbus_write_byte_data(client, offset, value);
if (ret < 0)
pr_err("sii8240: %s():%d offset:0x%X ret:%d\n",
__func__, __LINE__, offset, ret);
return ret;
}
static int mhl_read_byte_reg(struct i2c_client *client, unsigned int offset,
u8 *value)
{
int ret;
if (!value)
return -EINVAL;
ret = i2c_smbus_read_byte_data(client, offset);
if (ret < 0) {
pr_err("sii8240: %s():%d offset:0x%X ret:%d\n",
__func__, __LINE__, offset, ret);
return ret;
}
*value = ret & 0x000000FF;
return 0;
}
static int mhl_write_block_reg(struct i2c_client *client, unsigned int offset,
u8 len, u8 *values)
{
int ret;
if (!values)
return -EINVAL;
ret = i2c_smbus_write_i2c_block_data(client, offset, len, values);
if (ret < 0)
pr_err("sii8240: %s():%d offset:0x%X ret:%d\n",
__func__, __LINE__, offset, ret);
return ret;
}
static int mhl_read_block_reg(struct i2c_client *client, unsigned int offset,
u8 len, u8 *values)
{
int ret;
if (!values)
return -EINVAL;
ret = i2c_smbus_read_i2c_block_data(client, offset, len, values);
if (ret < 0)
pr_err("sii8240: %s():%d offset:0x%X ret:%d\n",
__func__, __LINE__, offset, ret);
return ret;
}
static int mhl_modify_reg(struct i2c_client *i2c_client, u8 offset,
u8 mask, u8 data)
{
u8 rd;
int ret;
ret = mhl_read_byte_reg(i2c_client, offset, &rd);
if (ret < 0) {
pr_err("sii8240: %s():%d offset:0x%X ret:%d\n",
__func__, __LINE__, offset, ret);
return ret;
}
rd &= ~mask;
rd |= (data & mask);
ret = mhl_write_byte_reg(i2c_client, offset, rd);
if (ret < 0)
pr_err("sii8240: %s():%d offset:0x%X ret:%d\n",
__func__, __LINE__, offset, ret);
return ret;
}
/* NOTE: Registers are set and cleared either by 1 or 0.
* Functions calling these mhl_set_reg and mhl_clear_reg should
* take care of these register-specific details and adjust the mask
*/
static int mhl_clear_reg(struct i2c_client *client, unsigned int offset,
u8 mask)
{
int ret;
u8 value;
ret = mhl_read_byte_reg(client, offset, &value);
if (ret < 0) {
pr_err("sii8240: %s():%d offset:0x%X ret:%d\n",
__func__, __LINE__, offset, ret);
return ret;
}
value &= ~mask;
ret = mhl_write_byte_reg(client, offset, value);
if (ret < 0)
pr_err("sii8240: %s():%d offset:0x%X ret:%d\n",
__func__, __LINE__, offset, ret);
return ret;
}
static int mhl_set_reg(struct i2c_client *client, unsigned int offset,
u8 mask)
{
int ret;
u8 value;
ret = mhl_read_byte_reg(client, offset, &value);
if (ret < 0) {
pr_err("sii8240: %s():%d offset:0x%X ret:%d\n",
__func__, __LINE__, offset, ret);
return ret;
}
value |= mask;
ret = mhl_write_byte_reg(client, offset, value);
if (ret < 0)
pr_err("sii8240: %s():%d offset:0x%X ret:%d\n",
__func__, __LINE__, offset, ret);
return ret;
}
#ifdef SII8240_CHECK_MONITOR
static inline u32 exynos_smc(u32 cmd, u32 arg1, u32 arg2, u32 arg3)
{
register u32 reg0 __asm__("r0") = cmd;
register u32 reg1 __asm__("r1") = arg1;
register u32 reg2 __asm__("r2") = arg2;
register u32 reg3 __asm__("r3") = arg3;
__asm__ volatile (
#ifdef MC_ARCH_EXTENSION_SEC
/* This pseudo op is supported and required from
* binutils 2.21 on */
".arch_extension sec\n"
#endif
"smc 0\n"
: "+r"(reg0), "+r"(reg1), "+r"(reg2), "+r"(reg3)
);
return reg1;
}
static ssize_t sii8240_hdcp_status(struct class *dev,
struct class_attribute *attr, char *buf)
{
int size;
struct sii8240_data *sii8240 = g_sii8240;
size = snprintf(buf, 10,"%d", sii8240->monitor_cmd);
return size;
}
static CLASS_ATTR(hdcp_status, 0444, sii8240_hdcp_status, NULL);
static void sii8240_link_monitor_timer(unsigned long data)
{
struct sii8240_data *sii8240 = (struct sii8240_data *) data;
schedule_work(&sii8240->mhl_link_monitor_work);
}
static void sii8240_link_monitor_work(struct work_struct *work)
{
int ret;
unsigned char rd_data = 0, hdcp_query = 0, status = 0;
struct sii8240_data *sii8240 = container_of(work, struct sii8240_data,
mhl_link_monitor_work);
struct i2c_client *tpi = sii8240->pdata->tpi_client;
if (sii8240->state < STATE_MHL_DISCOVERY_SUCCESS) {
sii8240->monitor_cmd |= 0x08;
sii8240->monitor_cmd &= ~(0x01);
del_timer_sync(&sii8240->mhl_timer);
ret = exynos_smc(MC_FC_HDCP_VALUE, 0, sii8240->monitor_cmd, 0);
if (ret != MC_FC_RET_OK)
pr_err("exynos_smc fail ret = %d\n", ret);
pr_info("%s() monitor_cmd = %d, mhl status = %d\n",
__func__, sii8240->monitor_cmd, sii8240->state);
return;
}
ret = mhl_read_byte_reg(tpi, TPI_HDCP_QUERY_DATA_REG, &hdcp_query);
if (ret < 0) {
pr_err("%s() mhl has been shut-down\n", __func__);
return;
}
rd_data = hdcp_query & HDCP_REPEATER_MASK;
if (rd_data) {
status = hdcp_query & EXTENDED_LINK_PROTECTION_MASK;
pr_debug("%s() repeater extended = %d\n", __func__, status);
} else {
status = hdcp_query & LOCAL_LINK_PROTECTION_MASK;
pr_debug("%s() local = %d\n", __func__, status);
}
if (status == 0) {
sii8240->monitor_cmd |= 0x04;
pr_info("%s() monitor_cmd = %d\n",
__func__, sii8240->monitor_cmd);
} else
sii8240->monitor_cmd |= 0x02;
ret = exynos_smc(MC_FC_HDCP_VALUE, 0, sii8240->monitor_cmd, 0);
if (ret != MC_FC_RET_OK)
pr_err("exynos_smc fail ret = %d\n", ret);
mod_timer(&sii8240->mhl_timer, jiffies + msecs_to_jiffies(2000));
}
#endif
static int set_mute_mode(struct sii8240_data *sii8240, bool mute)
{
int ret;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
pr_info("set_mute_mode : %d\n", mute);
if (mute)
ret = mhl_modify_reg(tpi, 0x1A, AV_MUTE_MASK, AV_MUTE_MASK);
else
ret = mhl_modify_reg(tpi, 0x1A, AV_MUTE_MASK, AV_MUTE_NORMAL);
return ret;
}
static int sii8240_send_avi_infoframe(struct sii8240_data *sii8240)
{
int ret = 0;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
pr_debug("sii8240: send avi infoframe %d\n", sii8240->hdmi_sink);
if (sii8240->hdmi_sink) {
ret = mhl_write_block_reg(tpi, 0x0C, SIZE_AVI_INFOFRAME,
sii8240->output_avi_data);
}
return ret;
}
#ifdef SFEATURE_HDCP_SUPPORT
static int sii8240_hdcp_on(struct sii8240_data *sii8240, bool hdcp_on)
{
int ret = 0;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
if (hdcp_on) {
pr_info("sii8240:HDCP On\n");
ret = mhl_modify_reg(tpi, HDCP_CTRL,
BIT_TPI_HDCP_CONTROL_DATA_DOUBLE_RI_CHECK_MASK |
BIT_TPI_HDCP_CONTROL_DATA_COPP_PROTLEVEL_MASK,
BIT_TPI_HDCP_CONTROL_DATA_DOUBLE_RI_CHECK_ENABLE |
BIT_TPI_HDCP_CONTROL_DATA_COPP_PROTLEVEL_MAX);
if (ret < 0)
pr_warn("unable to set HDCP_CTRL register\n");
#ifdef SII8240_CHECK_MONITOR
sii8240->monitor_cmd = 0x01;
#endif
} else {
pr_info("sii8240:HDCP Off\n");
ret = mhl_write_byte_reg(tpi, HDCP_CTRL, 0x00);
if (ret < 0)
pr_warn("unable to reset HDCP_CTRL register to 0x00\n");
#ifdef SII8240_CHECK_MONITOR
sii8240->monitor_cmd |= 0x08;
sii8240->monitor_cmd &= ~(0x01);
del_timer_sync(&sii8240->mhl_timer);
cancel_work_sync(&sii8240->mhl_link_monitor_work);
#endif
}
#ifdef SII8240_CHECK_MONITOR
ret = exynos_smc(MC_FC_HDCP_VALUE, 0, sii8240->monitor_cmd, 0);
if (ret != MC_FC_RET_OK)
pr_err("exynos_smc fail ret = %d\n", ret);
pr_info("monitor_cmd %d\n", sii8240->monitor_cmd);
#endif
return ret;
}
static int sii8240_tmds_active_hdcp(struct sii8240_data *sii8240)
{
int ret = 0;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
ret = set_mute_mode(sii8240, true);
if (ret < 0)
pr_info("sii8240: set_mute_mode fail %d\n", __LINE__);
mhl_modify_reg(tpi, 0x1A,
TMDS_OUTPUT_CONTROL_MASK, TMDS_OUTPUT_CONTROL_POWER_DOWN);
mhl_modify_reg(tpi, 0x1A,
TMDS_OUTPUT_CONTROL_MASK, TMDS_OUTPUT_CONTROL_ACTIVE);
ret = sii8240_send_avi_infoframe(sii8240);
if (ret < 0)
pr_info("sii8240: send AVIF fail %d\n", __LINE__);
return ret;
}
#endif
static int tmds_control(struct sii8240_data *sii8240, bool tmds_on)
{
int ret = 0;
#ifdef SFEATURE_HDCP_SUPPORT
u8 value, value2;
#endif
struct i2c_client *tpi = sii8240->pdata->tpi_client;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
pr_info("sii8240: TMDS ==> %d\n", tmds_on);
ret = mhl_modify_reg(hdmi, 0x81, 0x3F, 0x3C);
ret = mhl_modify_reg(hdmi, 0x87, 0x07, 0x03);
switch (tmds_on) {
case true:
#ifdef SFEATURE_HDCP_SUPPORT
if (sii8240->hdcp_support) {
ret = mhl_read_byte_reg(tpi, 0x1A, &value);
if (TMDS_OUTPUT_CONTROL_POWER_DOWN & value) {
pr_info("sii8240: TMDS is power_down\n");
sii8240_tmds_active_hdcp(sii8240);
} else {
ret = mhl_read_byte_reg(tpi, 0x29, &value2);
if (LINK_STATUS_NORMAL !=
(LINK_STATUS_MASK & value2))
ret = sii8240_tmds_active_hdcp(sii8240);
else if (AV_MUTE_MUTED & value)
ret = set_mute_mode(sii8240, false);
}
} else {
pr_info("hdcp is forced disable\n");
mhl_modify_reg(tpi, 0x1A,
AV_MUTE_MASK|TMDS_OUTPUT_CONTROL_MASK,
AV_MUTE_NORMAL|TMDS_OUTPUT_CONTROL_ACTIVE);
ret = sii8240_send_avi_infoframe(sii8240);
if (ret < 0)
pr_info("send AVIF fail%d\n", __LINE__);
}
#else
mhl_modify_reg(tpi, 0x1A,
AV_MUTE_MASK|TMDS_OUTPUT_CONTROL_MASK,
AV_MUTE_NORMAL|TMDS_OUTPUT_CONTROL_ACTIVE);
ret = sii8240_send_avi_infoframe(sii8240);
if (ret < 0)
pr_info("sii8240: send AVIF fail %d\n", __LINE__);
#endif
break;
case false:
#ifdef SFEATURE_HDCP_SUPPORT
if (sii8240->hdcp_support)
sii8240_hdcp_on(sii8240, false);
#endif
sii8240->regs.intr_masks.intr_tpi_mask_value = 0x0;
ret = mhl_write_byte_reg(tpi, 0x3C, 0x0);
pr_info("sii8240: TMDS_OUTPUT_CONTROL_POWER_DOWN |AV_MUTE_NORMAL\n");
ret = mhl_modify_reg(tpi, 0x1A,
TMDS_OUTPUT_CONTROL_MASK | AV_MUTE_MASK,
TMDS_OUTPUT_CONTROL_POWER_DOWN | AV_MUTE_MASK);
if (ret < 0) {
pr_info("sii8240: [ERROR] %s():%d failed !\n",
__func__, __LINE__);
return ret;
}
break;
default:
pr_warn("sii8240: default option @tmds _control\n");
break;
}
return ret;
}
static int set_hdmi_mode(struct sii8240_data *sii8240, bool hdmi_mode)
{
int ret = 0;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
ret = mhl_modify_reg(hdmi, 0xA1,
BIT_REG_RX_HDMI_CTRL0_hdmi_mode_overwrite_MASK,
BIT_REG_RX_HDMI_CTRL0_hdmi_mode_overwrite_SW_CTRL);
if (hdmi_mode) {
ret = mhl_modify_reg(hdmi, 0xA1,
BIT_REG_RX_HDMI_CTRL0_hdmi_mode_sw_value_MASK,
BIT_REG_RX_HDMI_CTRL0_hdmi_mode_sw_value_HDMI);
ret = mhl_modify_reg(tpi, 0x1A, TMDS_OUTPUT_MODE_MASK,
TMDS_OUTPUT_MODE_HDMI);
ret = mhl_write_byte_reg(hdmi, 0x90, 0xF5);
ret = mhl_write_byte_reg(hdmi, 0x91, 0x06);
ret = mhl_modify_reg(hdmi, 0xA3,
BIT_RX_HDMI_CTRL2_USE_AV_MUTE_SUPPORT_MASK,
BIT_RX_HDMI_CTRL2_USE_AV_MUTE_SUPPORT_ENABLE);
} else{
ret = mhl_modify_reg(hdmi, 0xA1,
BIT_REG_RX_HDMI_CTRL0_hdmi_mode_sw_value_MASK,
BIT_REG_RX_HDMI_CTRL0_hdmi_mode_sw_value_DVI);
ret = mhl_modify_reg(tpi, 0x1A, TMDS_OUTPUT_MODE_MASK,
TMDS_OUTPUT_MODE_DVI);
ret = mhl_write_byte_reg(hdmi, 0x90, 0xFF);
ret = mhl_write_byte_reg(hdmi, 0x91, 0xFF);
ret = mhl_modify_reg(hdmi, 0xA3,
BIT_RX_HDMI_CTRL2_USE_AV_MUTE_SUPPORT_MASK,
BIT_RX_HDMI_CTRL2_USE_AV_MUTE_SUPPORT_DISABLE);
}
sii8240->hdmi_mode = hdmi_mode;
pr_info("sii8240: %s():HDMI mode = %d\n", __func__, hdmi_mode);
return ret;
}
static int sii8240_fifo_clear(struct sii8240_data *sii8240)
{
struct i2c_client *tmds = sii8240->pdata->tmds_client;
u8 data;
int ret;
pr_info("%s\n", __func__);
ret = mhl_read_byte_reg(tmds, 0xf2, &data);
if (ret < 0) {
pr_info("sii8240:%d failed!\n", __LINE__);
return ret;
}
ret = mhl_set_reg(tmds,
TPI_DISABLE_REG, SW_TPI_EN_MASK);
if (ret < 0) {
pr_info("sii8240:%d failed!\n", __LINE__);
return ret;
}
if (0x20 & data) {
ret = mhl_write_byte_reg(tmds,
0xF2, data & ~0x20);
if (ret < 0) {
pr_info("%d failed!\n", __LINE__);
return ret;
}
ret = mhl_modify_reg(tmds, 0xF3, 0x0F, 0x09);
if (ret < 0) {
pr_info("%d failed!\n", __LINE__);
return ret;
}
}
ret = mhl_clear_reg(tmds,
TPI_DISABLE_REG, SW_TPI_EN_MASK);
if (ret < 0) {
pr_info("sii8240:%d failed!\n", __LINE__);
return ret;
}
return ret;
}
#ifdef SFEATURE_HDCP_SUPPORT
static int sii8240_hdcp_key_check(struct sii8240_data *sii8240)
{
u8 aksv[AKSV_SIZE];
int ret, i, cnt;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
memset(aksv, 0x00, AKSV_SIZE);
cnt = 0;
ret = mhl_read_block_reg(tpi, HDCP_KEY, AKSV_SIZE, aksv);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
return ret;
}
pr_info("sii8240: AKSV :0x%x, 0x%x, 0x%x, 0x%x, 0x%x",
aksv[0], aksv[1], aksv[2], aksv[3], aksv[4]);
for (i = 0; i < AKSV_SIZE; i++) {
while (aksv[i] != 0x00) {
if (aksv[i] & 0x01)
cnt++;
aksv[i] >>= 1;
}
}
if (cnt != NUM_OF_ONES_IN_KSV) {
pr_cont(" -> Illegal AKSV !\n");
ret = -EINVAL;
} else
pr_cont(" -> ok\n");
return ret;
}
static int sii8240_hdcp_control(struct sii8240_data *sii8240, u8 hdcp_reg)
{
int ret = 0;
u8 rd_data = 0, hdcp_query;
u8 bksv[AKSV_SIZE] = {0,};
struct i2c_client *tpi = sii8240->pdata->tpi_client;
struct i2c_client *tmds = sii8240->pdata->tmds_client;
ret = mhl_read_byte_reg(tpi, TPI_HDCP_QUERY_DATA_REG, &hdcp_query);
if (ret < 0) {
pr_info("sii8240: HDCP query request fail\n");
return ret;
}
/*HDCP link status changed.
*Indicates a status change event in the LinkStatus (0x29) value.*/
if (hdcp_reg & BIT_TPI_INTR_ST0_HDCP_SECURITY_CHANGE_EVENT) {
rd_data = hdcp_query & LINK_STATUS_MASK;
switch (rd_data) {
case LINK_STATUS_NORMAL:
pr_info("sii8240: %s():%d LINK_STATUS_NORMAL !!!\n",
__func__, __LINE__);
break;
case LINK_STATUS_LINK_LOST:
pr_info("sii8240: %s():%d LINK_STATUS_LINK_LOST !!!\n",
__func__, __LINE__);
ret = set_mute_mode(sii8240, false);
if (ret < 0)
pr_info("[ERROR]sii8240: set_mute_mode on fail\n");
ret = sii8240_hdcp_on(sii8240, false);
if (ret < 0)
pr_info("[ERROR]sii8240: tmds_control on fail\n");
ret = sii8240_tmds_active_hdcp(sii8240);
break;
case LINK_STATUS_RENEGOTIATION_REQ:
pr_info("sii8240: %s():%d LINK_STATUS_RENEGOTIATION_REQ !!!\n",
__func__, __LINE__);
sii8240_fifo_clear(sii8240);
ret = sii8240_hdcp_on(sii8240, false);
if (ret < 0)
pr_info("[ERROR]sii8240: sii8240_hdcp_on off fail\n");
msleep(500);
ret = sii8240_tmds_active_hdcp(sii8240);
if (ret < 0)
pr_info("[ERROR]sii8240: set_mute_mode on fail\n");
break;
case LINK_STATUS_LINK_SUSPENDED:
pr_info("sii8240: %s():%d LINK_STATUS_LINK_SUSPENDED !!!\n",
__func__, __LINE__);
ret = sii8240_hdcp_on(sii8240, false);
if (ret < 0)
pr_info("[ERROR]sii8240: hdcp off fail\n");
break;
}
}
/*HDCP Authentication status changed.
*Indicates either that the previous authentication request has completed
*or that an Ri mismatch has caused authentication to fail.*/
if (hdcp_reg & BIT_TPI_INTR_ST0_HDCP_AUTH_STATUS_CHANGE_EVENT) {
rd_data = hdcp_query &
(EXTENDED_LINK_PROTECTION_MASK |
LOCAL_LINK_PROTECTION_MASK);
switch (rd_data) {
case (EXTENDED_LINK_PROTECTION_NONE |
LOCAL_LINK_PROTECTION_NONE):
pr_info("EXTENDED_LINK_PROTECTION_NONE\n");
ret = sii8240_hdcp_on(sii8240, false);
if (ret < 0)
pr_info("[ERROR]sii8240: sii8240_hdcp_on fail\n");
ret = sii8240_tmds_active_hdcp(sii8240);
if (ret < 0)
pr_info("[ERROR]sii8240: sii8240_tmds_active_hdcp fail\n");
break;
case LOCAL_LINK_PROTECTION_SECURE:
pr_info("sii8240: %s():%d LOCAL_LINK_PROTECTION_SECURE\n",
__func__, __LINE__);
if (!(HDCP_REPEATER_MASK & hdcp_query)) {
ret = set_mute_mode(sii8240, false);
if (ret < 0)
pr_info("[ERROR]sii8240: tmds_control off fail\n");
#ifdef SII8240_CHECK_MONITOR
if ((sii8240->monitor_cmd & 0x01) == 0x01) {
/*start checking link status*/
sii8240_link_monitor_timer
((unsigned long) sii8240);
pr_info("monitor start\n");
} else
pr_err("[ERROR] : %s monitor state : %d\n",
__func__, sii8240->monitor_cmd);
#endif
}
break;
case (EXTENDED_LINK_PROTECTION_SECURE |
LOCAL_LINK_PROTECTION_SECURE):
pr_info("EXTENDED_LINK_PROTECTION_SECURE\n");
if (HDCP_REPEATER_MASK & hdcp_query) {
ret = set_mute_mode(sii8240, false);
#ifdef SII8240_CHECK_MONITOR
if ((sii8240->monitor_cmd & 0x01) == 0x01) {
/*start checking link status*/
sii8240_link_monitor_timer
((unsigned long)sii8240);
pr_info("monitor start\n");
} else
pr_err("[ERROR] : %s monitor state : %d\n",
__func__, sii8240->monitor_cmd);
#endif
}
break;
default:
pr_info("sii8240: %s():%d default !!!\n",
__func__, __LINE__);
ret = sii8240_hdcp_on(sii8240, false);
if (ret < 0)
pr_info("sii8240: sii8240_hdcp_on fail\n");
ret = sii8240_tmds_active_hdcp(sii8240);
if (ret < 0)
pr_info("sii8240: tmds_control off fail\n");
break;
}
}
/*Read BKSV Done.
*Various bits in 0x29 contain information acquired from BCAPs.*/
if (hdcp_reg & BIT_TPI_INTR_ST0_BKSV_DONE) {
pr_info("sii8240: %s():%d BIT_TPI_INTR_ST0_BKSV_DONE\n",
__func__, __LINE__);
set_hdmi_mode(sii8240, sii8240->hdmi_sink);
/*HDCP on*/
if (hdcp_query & PROTECTION_TYPE_MASK) {
ret = sii8240_hdcp_on(sii8240, true);
if (ret < 0)
pr_info("sii8240: %s():sii8240_hdcp_on is fail, %d\n",
__func__, ret);
}
}
if (hdcp_reg & BIT_TPI_INTR_ST0_BKSV_ERR) {
pr_info("sii8240: %s():bksv err\n", __func__);
ret = mhl_read_block_reg(tmds, 0x10, AKSV_SIZE, bksv);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
return ret;
}
pr_info("sii8240: BKSV :0x%x, 0x%x, 0x%x, 0x%x, 0x%x",
bksv[0], bksv[1], bksv[2], bksv[3], bksv[4]);
ret = sii8240_hdcp_on(sii8240, false);
if (ret < 0)
pr_warn("sii8240: %s() ii8240_hdcp_on is fail, %d\n",
__func__, ret);
ret = sii8240_tmds_active_hdcp(sii8240);
if (ret < 0)
pr_info("sii8240: tmds_control off fail\n");
}
return 0;
}
#endif
static int sii8240_read_edid_block(struct sii8240_data *sii8240, u8 edid_ext)
{
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
int ret;
int i = 0;
u8 data;
u16 temp = edid_ext << 7;
ret = mhl_set_reg(hdmi, EDID_STATUS_HW_ASSIST_REG, HW_EDID_DONE);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(hdmi, EDID_CTRL_REG,
EDID_MODE_EN | EDID_FIFO_ADDR_AUTO);
if (ret < 0)
return ret;
if (!edid_ext) /* Block-0 */
ret = mhl_write_byte_reg(hdmi, EDID_PAGE_HW_ASSIST_REG,
HW_READ_EDID_BLOCK_0);
else /* other EDID-extension blocks */
ret = mhl_write_byte_reg(hdmi, EDID_BLOCK_ADDR_HW_ASSIST_REG,
(1<<(edid_ext-1)));
if (ret < 0)
return ret;
/* TODO: use some completion mechanism or wait_for_completion_timeout
* APIs instead of this loop */
do {
ret = mhl_read_byte_reg(hdmi, EDID_STATUS_HW_ASSIST_REG,
&data);
if (ret < 0)
return ret;
if (data & HW_EDID_DONE) {
ret = mhl_write_byte_reg(hdmi,
EDID_STATUS_HW_ASSIST_REG,
HW_EDID_DONE);
if (ret < 0) {
pr_info("sii8240: edid done failed!\n");
return ret;
}
break;
}
if (data & HW_EDID_ERROR) {
ret = mhl_write_byte_reg(hdmi,
EDID_STATUS_HW_ASSIST_REG,
HW_EDID_ERROR);
if (ret < 0) {
pr_info("sii8240: edid read error!\n");
return ret;
}
sii8240_fifo_clear(sii8240);
ret = mhl_write_byte_reg(hdmi, EDID_PAGE_HW_ASSIST_REG,
HW_READ_EDID_BLOCK_0);
if (ret < 0) {
pr_info("sii8240: edid block 0 failed !\n");
return ret;
}
}
usleep_range(1000, 2000);
i++;
} while (i < 100);
if (i == 100) {
pr_info("sii8240: EDID READ timeout\n");
return -ETIMEDOUT;
}
/* CHECK: 0,(1<<7),0,(1<<7) values being used in reference driver */
mhl_write_byte_reg(hdmi, EDID_FIFO_ADDR_REG, (temp & 0xFF));
/* TODO 1: We can optimize this loop using loop unrolling techniques */
/* TODO 2: In one of the reference driver, block read is being used;
need to investiage on this */
/* SMBus allows at most 32 bytes, so read by 32 bytes 4 times. */
for (i = 0; i < EDID_LENGTH/I2C_SMBUS_BLOCK_MAX; i++) {
ret = mhl_read_block_reg(hdmi, EDID_FIFO_RD_DATA_REG,
I2C_SMBUS_BLOCK_MAX,
(&sii8240->edid[i*I2C_SMBUS_BLOCK_MAX +
edid_ext*EDID_LENGTH]));
if (ret < 0)
return ret;
}
return ret;
}
static void sii8240_check_hdmi_mode(struct sii8240_data *sii8240, int exts)
{
u8 datablock;
u8 *edid;
int i, ieee_reg;
/* 3 = VSD */
datablock = 3;
i = 0x4;
edid = sii8240->edid + (exts * EDID_LENGTH);
for (; i < edid[2]; i += ((edid[i] & 0x1f) + 1)) {
/* Find vendor specific block */
if ((edid[i] >> 5) == datablock) {
ieee_reg = edid[i + 1] | (edid[i + 2] << 8) |
edid[i + 3] << 16;
if (ieee_reg == 0x000c03) {
pr_info("sii8240: hdmi_sink\n");
sii8240->hdmi_sink = true;
}
break;
}
}
}
static int sii8240_read_edid(struct sii8240_data *sii8240)
{
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
int ret;
int i;
u8 edid_exts;
sii8240->hdmi_sink = false;
memset(sii8240->edid, 0, sizeof(sii8240->edid));
/* Read EDID block-0 */
ret = sii8240_read_edid_block(sii8240, 0);
if (ret < 0)
goto err_exit;
edid_exts = sii8240->edid[0x7e]; /* no. of edid extensions */
/* boundary check for edid_exist: especially when mhl urgents
for disconnection, edid_exts get wrong value, in this case
reset edid_exts=1(default) to avoid kernel panic
by exceding sii8240->edid[0x7e] array boundary */
if (edid_exts >= (EDID_MAX_LENGTH/EDID_LENGTH)) {
pr_err("sii8240: edid_exts = %d is wrong\n", edid_exts);
sii8240->edid[0x7e] = -EINVAL;
edid_exts = 0;
}
if (!edid_exts) {
ret = mhl_write_byte_reg(hdmi, EDID_BLOCK_ADDR_HW_ASSIST_REG,
(1<<0));
if (ret < 0)
goto err_exit;
goto edid_populated;
}
for (i = 1; i <= edid_exts; i++) {
ret = sii8240_read_edid_block(sii8240, i);
if (ret < 0)
goto err_exit;
/* check hdmi mode if CEA BLOCK(ext edid[0] == 0x02) */
if (sii8240->edid[i * EDID_LENGTH] == 0x02)
sii8240_check_hdmi_mode(sii8240, i);
}
edid_populated:
print_hex_dump(KERN_INFO, "EDID= ", DUMP_PREFIX_OFFSET, 16, 1,
sii8240->edid, EDID_LENGTH * (1 + edid_exts), false);
ret = mhl_write_byte_reg(hdmi, EDID_FIFO_ADDR_REG, 0);
if (ret < 0)
goto err_exit;
/* Block operations can handle only 32 bytes at a time */
for (i = 0; i < (edid_exts+1)*(EDID_LENGTH/I2C_SMBUS_BLOCK_MAX); i++) {
ret = mhl_write_block_reg(hdmi, EDID_FIFO_WR_DATA_REG,
I2C_SMBUS_BLOCK_MAX,
&sii8240->edid[i*I2C_SMBUS_BLOCK_MAX]);
if (ret < 0) {
pr_err("sii8240: edid write block error\n");
break;
}
}
ret = mhl_write_byte_reg(hdmi, EDID_CTRL_REG, EDID_PRIME_VALID |
EDID_FIFO_ADDR_AUTO | EDID_MODE_EN);
if (ret < 0)
goto err_exit;
ret = mhl_set_reg(sii8240->pdata->disc_client, POWER_CTRL_REG, PCLK_EN);
if (ret < 0)
goto err_exit;
pr_info("sii8240: edid read and stored successfully\n");
exynos_get_edid(sii8240->edid, (1 + edid_exts)
* EDID_LENGTH,
sii8240->regs.peer_devcap[MHL_DEVCAP_VID_LINK_MODE]);
return ret;
err_exit:
sii8240->hdmi_sink = false;
return ret;
}
static int mhl_hpd_control_low(struct sii8240_data *sii8240)
{
int ret;
struct i2c_client *tmds = sii8240->pdata->tmds_client;
ret = mhl_modify_reg(tmds, UPSTRM_HPD_CTRL_REG,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_MASK |
BIT_HPD_CTRL_HPD_OUT_OVR_EN_MASK,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_OFF |
BIT_HPD_CTRL_HPD_OUT_OVR_EN_ON);
if (ret < 0)
pr_info("[ERROR]sii8240: %s():%d failed !\n",
__func__, __LINE__);
sii8240->hpd_status = false;
return ret;
}
static int force_usb_id_switch_open(struct sii8240_data *sii8240)
{
int ret = 0;
struct i2c_client *disc = sii8240->pdata->disc_client;
ret = mhl_modify_reg(disc, 0x10, 0x1, 0x0);
if (ret < 0)
pr_info("[ERROR]sii8240: %s():%d !\n",
__func__, __LINE__);
ret = mhl_modify_reg(disc, 0x15,
BIT_DC6_USB_OVERRIDE_MASK, BIT_DC6_USB_OVERRIDE_ON);
if (ret < 0)
pr_info("[ERROR]sii8240: %s():%d !\n",
__func__, __LINE__);
ret = mhl_write_byte_reg(disc, 0x12, BIT_DC3_COMM_IMME_ON |
BIT_DC3_FORCE_MHL_OFF |
BIT_DC3_DISC_SIMODE_OFF |
BIT_DC3_FORCE_USB_OFF |
BIT_DC3_USB_EN_OFF |
BIT_DC3_DLYTRG_SEL_064ms);
if (ret < 0)
pr_warn("[ERROR]sii8240: %s():%d failed !\n",
__func__, __LINE__);
ret = mhl_hpd_control_low(sii8240);
if (ret < 0)
pr_info("[ERROR]sii8240: %s():%d failed !\n",
__func__, __LINE__);
return ret;
}
static int release_usb_id_switch_open(struct sii8240_data *sii8240)
{
int ret = 0;
struct i2c_client *disc = sii8240->pdata->disc_client;
msleep(50);
ret = mhl_modify_reg(disc, 0x15, 0x40, 0x0);
if (ret < 0) {
pr_info("[ERROR]sii8240: %s():%d failed !\n",
__func__, __LINE__);
return ret;
}
/*enable discovery*/
ret = mhl_modify_reg(disc, 0x10, 0x1, 0x1);
if (ret < 0) {
pr_info("[ERROR]sii8240: %s():%d failed !\n",
__func__, __LINE__);
return ret;
}
return ret;
}
static int sii8240_set_interrupt(struct sii8240_data *sii8240)
{
int ret;
struct i2c_client *tmds = sii8240->pdata->tmds_client;
struct i2c_client *disc = sii8240->pdata->disc_client;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
struct i2c_client *cbus = sii8240->pdata->cbus_client;
ret = mhl_write_byte_reg(tmds, 0x75,
sii8240->regs.intr_masks.intr1_mask_value);
if (ret < 0) {
pr_info("[ERROR]sii8240:failed !\n");
return ret;
}
ret = mhl_write_byte_reg(tmds, 0x76,
sii8240->regs.intr_masks.intr2_mask_value);
if (ret < 0) {
pr_info("[ERROR]sii8240:failed !\n");
return ret;
}
ret = mhl_write_byte_reg(tmds, 0x77,
sii8240->regs.intr_masks.intr3_mask_value);
if (ret < 0) {
pr_info("[ERROR]sii8240:failed !\n");
return ret;
}
ret = mhl_write_byte_reg(disc, DISC_INTR_ENABLE_REG,
sii8240->regs.intr_masks.intr4_mask_value);
if (ret < 0) {
pr_info("[ERROR]sii8240:failed !\n");
return ret;
}
ret = mhl_write_byte_reg(tmds, 0x78,
sii8240->regs.intr_masks.intr5_mask_value);
if (ret < 0) {
pr_info("[ERROR]sii8240:failed !\n");
return ret;
}
ret = mhl_write_byte_reg(cbus, CBUS_MSC_INTR_ENABLE_REG,
sii8240->regs.intr_masks.intr_cbus0_mask_value);
if (ret < 0) {
pr_info("[ERROR]sii8240:sii8240:failed !\n");
return ret;
}
ret = mhl_write_byte_reg(cbus, CBUS_MSC_ERROR_INTR_ENABLE_REG,
sii8240->regs.intr_masks.intr_cbus1_mask_value);
if (ret < 0) {
pr_info("[ERROR]sii8240:sii8240:failed !\n");
return ret;
}
ret = mhl_write_byte_reg(tmds, 0x7D,
sii8240->regs.intr_masks.intr7_mask_value);
if (ret < 0) {
pr_info("[ERROR]sii8240:sii8240:failed !\n");
return ret;
}
ret = mhl_write_byte_reg(tmds, 0x7E,
sii8240->regs.intr_masks.intr8_mask_value);
if (ret < 0) {
pr_info("[ERROR]sii8240:sii8240:failed !\n");
return ret;
}
ret = mhl_write_byte_reg(tpi, 0x3C,
sii8240->regs.intr_masks.intr_tpi_mask_value);
if (ret < 0) {
pr_info("[ERROR]sii8240:sii8240:failed !\n");
return ret;
}
return ret;
}
/* Functions for Switching Power States, Some boards use these
* functions,hence adding it here */
static int switch_to_d3(struct sii8240_data *sii8240)
{
int ret;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
struct i2c_client *disc = sii8240->pdata->disc_client;
struct i2c_client *tmds = sii8240->pdata->tmds_client;
struct i2c_client *cbus = sii8240->pdata->cbus_client;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
memset(&sii8240->regs.intr_masks, 0, sizeof(sii8240->regs.intr_masks));
sii8240->regs.intr_masks.intr4_mask_value = BIT_INTR4_RGND_DETECTION;
sii8240->regs.link_mode = MHL_STATUS_CLK_NORMAL;
ret = sii8240_set_interrupt(sii8240);
if (ret < 0)
return ret;
ret = mhl_hpd_control_low(sii8240);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(hdmi, MHLTX_TERM_CTRL_REG, 0xD0);
if (ret < 0)
return ret;
/*Clear all interrupt*/
ret = mhl_write_byte_reg(disc, 0x21, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tmds, 0x71, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tmds, 0x72, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tmds, 0x73, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tmds, 0x74, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tmds, 0x7B, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tmds, 0x7C, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tmds, 0xE0, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, 0x8C, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, 0x8E, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, 0x92, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, 0x94, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, 0x96, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, 0x98, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, 0x9A, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, 0x9C, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tpi, 0x3D, 0xFF);
if (ret < 0)
return ret;
msleep(50);
ret = mhl_modify_reg(disc, DISC_CTRL1_REG, 1<<0x0, 0x1);
if (ret < 0)
return ret;
ret = mhl_modify_reg(disc, 0x01, 1<<0x0, 0x0);
if (ret < 0)
return ret;
sii8240->state = STATE_MHL_READY_RGND_DETECT;
pr_info("sii8240: D3: Power saving mode\n");
if (!sii8240->irq_enabled) {
enable_irq(sii8240->irq);
sii8240->irq_enabled = true;
pr_info("sii8240: interrupt enabled\n");
}
return 0;
}
/* toggle hpd line low for 100ms */
static void sii8240_toggle_hpd(struct i2c_client *client)
{
mhl_set_reg(client, UPSTRM_HPD_CTRL_REG, HPD_OVERRIDE_EN);
mhl_clear_reg(client, UPSTRM_HPD_CTRL_REG, HPD_OUT_OVERRIDE_VAL);
msleep(100);
mhl_set_reg(client, UPSTRM_HPD_CTRL_REG, HPD_OUT_OVERRIDE_VAL);
mhl_clear_reg(client, UPSTRM_HPD_CTRL_REG, HPD_OVERRIDE_EN);
}
static int sii8240_host_devcap_init(struct sii8240_data *sii8240)
{
int ret;
struct i2c_client *cbus = sii8240->pdata->cbus_client;
u8 devcap[] = { DEV_STATE, DEV_MHL_VERSION, DEV_CAT_SOURCE_NO_PWR,
DEV_ADOPTER_ID_H, DEV_ADOPTER_ID_L, DEV_VID_LINK_MODE,
DEV_AUDIO_LINK_MODE, DEV_VIDEO_TYPE, DEV_LOGICAL_DEV,
DEV_BANDWIDTH, DEV_FEATURE_FLAG, DEV_DEVICE_ID_H,
DEV_DEVICE_ID_L, DEV_SCRATCHPAD_SIZE,
DEV_INT_STATUS_SIZE, DEV_RESERVED };
for (ret = 0; ret < DEVCAP_COUNT_MAX; ret++)
sii8240->regs.host_devcap[ret] = devcap[ret];
ret = mhl_write_block_reg(cbus, MHL_DEVCAP_DEVSTATE,
DEVCAP_COUNT_MAX, devcap);
if (ret < 0)
return ret;
return 0;
}
/* Do we really need to do this? */
static int sii8240_cbus_init(struct sii8240_data *sii8240)
{
int ret;
struct i2c_client *cbus = sii8240->pdata->cbus_client;
ret = mhl_write_byte_reg(cbus, CBUS_MHL_INTR_REG_0_MASK, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, CBUS_MHL_INTR_REG_1_MASK, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, CBUS_MHL_INTR_REG_2_MASK, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, CBUS_MHL_INTR_REG_3_MASK, 0xFF);
return ret;
}
static int sii8240_init_regs(struct sii8240_data *sii8240)
{
int ret = 0;
struct i2c_client *disc = sii8240->pdata->disc_client;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
struct i2c_client *tmds = sii8240->pdata->tmds_client;
struct i2c_client *cbus = sii8240->pdata->cbus_client;
pr_debug("sii8240_init_regs\n");
memset(sii8240->regs.peer_devcap, 0x0,
sizeof(sii8240->regs.peer_devcap));
ret = mhl_modify_reg(disc, INT_CTRL_REG, 0x06, 0x00);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n", __func__, __LINE__);
return ret;
}
ret = mhl_write_byte_reg(disc, POWER_CTRL_REG, POWER_TO_D0);
if (ret < 0)
return ret;
if (sii8240->state == STATE_MHL_CONNECTED) {
ret = mhl_modify_reg(disc, DISC_CTRL1_REG, (1<<1), 0x00);
if (ret < 0)
return ret;
}
ret = mhl_write_byte_reg(hdmi, TMDS_CLK_EN_REG, 0x01);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(hdmi, TMDS_CH_EN_REG, 0x11);
if (ret < 0)
return ret;
ret = mhl_clear_reg(tmds, TPI_DISABLE_REG, SW_TPI_EN_MASK);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tpi, TPI_HDCP_CTRL_REG, 0x00);
if (ret < 0)
return ret;
ret = mhl_set_reg(tpi, TPI_AV_MUTE_REG, AV_MUTE_MASK);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tpi, 0xBB, 0x76);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tpi, 0x3D, 0xFF);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(hdmi, 0xA4, 0x00);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(hdmi, 0x80,
BIT_MHLTX_CTL1_TX_TERM_MODE_100DIFF |
BIT_MHLTX_CTL1_DISC_OVRIDE_ON);
if (ret < 0)
return ret;
ret = mhl_modify_reg(hdmi, 0x82, BIT_MHLTX_CTL3_DAMPING_SEL_MASK,
BIT_MHLTX_CTL3_DAMPING_SEL_OFF);
if (ret < 0)
return ret;
ret = mhl_modify_reg(hdmi, MHLTX_CTL4_REG, BIT_CLK_SWING_CTL_MASK |
BIT_DATA_SWING_CTL_MASK, sii8240->pdata->swing_level);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(cbus, 0xA7, 0x1C);
if (ret < 0)
return ret;
if (sii8240->state == STATE_MHL_CONNECTED) {
ret = mhl_write_byte_reg(hdmi, MHLTX_TERM_CTRL_REG, 0x10);
if (ret < 0)
return ret;
ret = mhl_modify_reg(hdmi,
MHLTX_CTL4_REG, BIT_CLK_SWING_CTL_MASK |
BIT_DATA_SWING_CTL_MASK, sii8240->pdata->swing_level);
if (ret < 0)
return ret;
pr_info("sii8240: INIT REGS swing_level: 0x%02X\n",
sii8240->pdata->swing_level);
}
ret = mhl_write_byte_reg(hdmi, 0x87, 0x0A);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tmds, 0x85, 0x02);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(hdmi, 0x00, 0x00);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(hdmi, 0x4C, 0xD0);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(disc, 0x11, 0xA5);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(disc, DISC_CTRL6_REG, 0x11);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(disc, DISC_CTRL9_REG,
CBUS_LOW_TO_DISCONNECT |
WAKE_DRVFLT | DISC_PULSE_PROCEED);
if (ret < 0)
return ret;
if (sii8240->state == STATE_MHL_CONNECTED) {
ret = mhl_write_byte_reg(disc, DISC_CTRL1_REG, 0x27);
if (ret < 0)
return ret;
} else {
ret = mhl_write_byte_reg(disc, DISC_CTRL1_REG, 0x26);
if (ret < 0)
return ret;
}
ret = mhl_write_byte_reg(disc, DISC_CTRL3_REG, 0x86);
if (ret < 0)
return ret;
/*This code is for disable RGND open interrupt */
/*disable intr when cbus open*/
/*ret = mhl_write_byte_reg(disc, 0x13, 0xAC);
*if (ret < 0)
* return ret;
*/
ret = mhl_write_byte_reg(hdmi, TPI_PACKET_FILTER_REG,
DROP_GCP_PKT | DROP_AVI_PKT |
DROP_MPEG_PKT | DROP_SPIF_PKT |
DROP_CEA_CP_PKT | DROP_CEA_GAMUT_PKT);
if (ret < 0)
return ret;
if (sii8240->state == STATE_DISCONNECTED) {
ret = mhl_modify_reg(disc, POWER_CTRL_REG,
BIT_DPD_PDIDCK_MASK, BIT_DPD_PDIDCK_POWER_DOWN);
if (ret < 0)
return ret;
}
if (sii8240->state == STATE_MHL_CONNECTED) {
ret = mhl_modify_reg(disc, POWER_CTRL_REG,
BIT_DPD_PDIDCK_MASK, BIT_DPD_PDIDCK_MASK);
if (ret < 0)
return ret;
}
/* In reference driver,HPD_OVERRIDE is enabled and HPD is set to low.
* Here, override of hpd is disabled(hence, hpd will propagate from
* downstream to upstream, no manual intervention).
*/
ret = mhl_modify_reg(tmds, UPSTRM_HPD_CTRL_REG, (1<<6), 0x0);
if (ret < 0) {
pr_err("sii8240: %s():%d Fail to set register\n",
__func__, __LINE__);
return ret;
}
ret = mhl_hpd_control_low(sii8240);
if (sii8240->state == STATE_MHL_CONNECTED) {
ret = mhl_modify_reg(hdmi, 0xAC,
BIT_HDMI_CLR_BUFFER_RX_HDMI_VSI_CLR_W_AVI_EN_MASK |
BIT_HDMI_CLR_BUFFER_RX_HDMI_VSI_CLR_EN_MASK,
BIT_HDMI_CLR_BUFFER_RX_HDMI_VSI_CLR_W_AVI_EN_CLEAR |
BIT_HDMI_CLR_BUFFER_RX_HDMI_VSI_CLR_EN_CLEAR);
if (ret < 0)
return ret;
}
ret = mhl_write_byte_reg(disc, 0x00, 0x84);
if (ret < 0)
return ret;
ret = mhl_write_byte_reg(tmds, DCTL_REG, TRANSCODE_OFF |
TCLK_PHASE_INVERTED);
if (ret < 0)
return ret;
#ifdef SFEATURE_UNSTABLE_SOURCE_WA
ret = mhl_modify_reg(tmds, 0x80, BIT_TMDS_CCTRL_CKDT_EN,
BIT_TMDS_CCTRL_CKDT_EN);
if (ret < 0)
return ret;
#endif
ret = sii8240_cbus_init(sii8240);
ret = sii8240_host_devcap_init(sii8240);
if (ret < 0)
return ret;
#ifdef SFEATURE_HDCP_SUPPORT
ret = sii8240_hdcp_key_check(sii8240);
if (ret < 0)
pr_info("sii8240_hdcp_key_check failed !\n");
#endif/*SFEATURE_HDCP_SUPPORT*/
return ret;
}
/* Must call with sii8240->lock held */
static int sii8240_msc_req_locked(struct sii8240_data *sii8240, u8 req_type,
u8 offset, u8 first_data, u8 second_data)
{
int ret = 1;
struct i2c_client *cbus = sii8240->pdata->cbus_client;
bool write_offset;
bool write_first_data;
bool write_second_data;
mutex_unlock(&sii8240->lock);
mutex_lock(&sii8240->msc_lock);
write_offset = req_type & (START_READ_DEVCAP |
START_WRITE_STAT_SET_INT | START_WRITE_BURST);
write_first_data = req_type &
(START_WRITE_STAT_SET_INT | START_MSC_MSG);
write_second_data = req_type & START_MSC_MSG;
pr_debug("SEND:offset = 0x%x\n", offset);
init_completion(&sii8240->cbus_complete);
if (write_offset)
mhl_write_byte_reg(cbus, MSC_CMD_OR_OFFSET_REG, offset);
if (write_first_data)
mhl_write_byte_reg(cbus, MSC_SEND_DATA1_REG, first_data);
if (write_second_data)
mhl_write_byte_reg(cbus, MSC_SEND_DATA2_REG, second_data);
mhl_write_byte_reg(cbus, CBUS_MSC_CMD_START_REG, req_type);
if (!completion_done(&sii8240->cbus_complete))
ret = wait_for_completion_timeout(&sii8240->cbus_complete,
msecs_to_jiffies(1800));
mutex_unlock(&sii8240->msc_lock);
mutex_lock(&sii8240->lock);
return ret ? 0 : -EIO;
}
/* Must call with sii8240->lock held */
static int sii8240_devcap_read_locked(struct sii8240_data *sii8240, u8 offset)
{
int ret;
u8 val;
struct i2c_client *cbus = sii8240->pdata->cbus_client;
if (offset > 0xf)
return -EINVAL;
ret = sii8240_msc_req_locked(sii8240, START_READ_DEVCAP, offset, 0, 0);
if (ret < 0)
pr_info("msc req locked error\n");
ret = mhl_read_byte_reg(cbus, MSC_RCVD_DATA1_REG, &val);
if (ret < 0) {
pr_info("msc rcvd data reg read failing\n");
return ret;
}
return val;
}
static int sii8240_queue_devcap_read_locked(struct sii8240_data *sii8240,
u8 offset)
{
struct cbus_data *cbus_cmd;
int ret = 0;
cbus_cmd = kzalloc(sizeof(struct cbus_data), GFP_KERNEL);
if (!cbus_cmd) {
pr_err("sii8240: failed to allocate msc data\n");
return -ENOMEM;
}
cbus_cmd->cmd = READ_DEVCAP;
cbus_cmd->offset = offset;
cbus_cmd->use_completion = true;
init_completion(&cbus_cmd->complete);
list_add_tail(&cbus_cmd->list, &sii8240->cbus_data_list);
queue_work(sii8240->cbus_cmd_wqs, &sii8240->cbus_work);
mutex_unlock(&sii8240->lock);
ret = wait_for_completion_timeout(&cbus_cmd->complete,
msecs_to_jiffies(2500));
mutex_lock(&sii8240->lock);
if (ret == 0)
pr_warn("sii8240: read devcap:0x%X time out !!\n", offset);
else
kfree(cbus_cmd);
return ret;
}
static int sii8240_queue_cbus_cmd_locked(struct sii8240_data *sii8240,
u8 command, u8 offset, u8 data)
{
struct cbus_data *cbus_cmd;
cbus_cmd = kzalloc(sizeof(struct cbus_data), GFP_KERNEL);
if (!cbus_cmd) {
pr_err("sii8240: failed to allocate msc data\n");
return -ENOMEM;
}
cbus_cmd->cmd = command;
cbus_cmd->offset = offset;
cbus_cmd->data = data; /*modified for 3.1.1.13*/
cbus_cmd->use_completion = false;
list_add_tail(&cbus_cmd->list, &sii8240->cbus_data_list);
queue_work(sii8240->cbus_cmd_wqs, &sii8240->cbus_work);
return 0;
}
bool is_key_supported(struct sii8240_data *sii8240, int keyindex)
{
u8 log_dev = DEV_LOGICAL_DEV;
if (sii8240_rcp_keymap[keyindex].key_code != KEY_UNKNOWN &&
sii8240_rcp_keymap[keyindex].key_code != KEY_RESERVED &&
(sii8240_rcp_keymap[keyindex].log_dev_type & log_dev))
return true;
else
return false;
}
static int sii8240_register_input_device(struct sii8240_data *sii8240)
{
struct input_dev *input;
int ret;
u8 i;
input = input_allocate_device();
if (!input) {
pr_err("sii8240: failed to allocate input device\n");
return -ENOMEM;
}
set_bit(EV_KEY, input->evbit);
for (i = 0; i < SII8240_RCP_NUM_KEYS; i++)
sii8240->keycode[i] = sii8240_rcp_keymap[i].key_code;
input->keycode = sii8240->keycode;
input->keycodemax = SII8240_RCP_NUM_KEYS;
input->keycodesize = sizeof(sii8240->keycode[0]);
for (i = 0; i < SII8240_RCP_NUM_KEYS; i++) {
if (is_key_supported(sii8240, i))
set_bit(sii8240->keycode[i], input->keybit);
}
input->name = "sii8240_rcp";
input->id.bustype = BUS_I2C;
input_set_drvdata(input, sii8240);
pr_debug("sii8240: registering input device\n");
ret = input_register_device(input);
if (ret < 0) {
pr_err("sii8240: failed to register input device\n");
input_free_device(input);
return ret;
}
mutex_lock(&sii8240->input_lock);
sii8240->input_dev = input;
mutex_unlock(&sii8240->input_lock);
return 0;
}
#if 0 /*unused*/
static void sii8240_unregister_input_device(struct sii8240_data *sii8240)
{
mutex_lock(&sii8240->input_lock);
if (sii8240->input_dev) {
pr_debug("sii8240: unregistering input device\n");
input_unregister_device(sii8240->input_dev);
sii8240->input_dev = NULL;
}
mutex_unlock(&sii8240->input_lock);
}
#endif
static void rcp_key_report(struct sii8240_data *sii8240, u16 key)
{
pr_info("sii8240: report rcp key: %d\n", key);
mutex_lock(&sii8240->input_lock);
if (sii8240->input_dev) {
input_report_key(sii8240->input_dev, key, 1);
input_report_key(sii8240->input_dev, key, 0);
input_sync(sii8240->input_dev);
}
mutex_unlock(&sii8240->input_lock);
}
static void cbus_process_rcp_key(struct sii8240_data *sii8240, u8 key)
{
if (key == 0x7E) {
pr_info("sii8240: MHL switch event sent :1\n");
switch_set_state(&sii8240->mhl_event_switch, 1);
}
if (key < SII8240_RCP_NUM_KEYS) {
if (is_key_supported(sii8240, key)) {
/* Report the key */
rcp_key_report(sii8240, sii8240->keycode[key]);
/* Send the RCP ack */
sii8240_msc_req_locked(sii8240, START_MSC_MSG, 0, MSG_RCPK, key);
} else {
/* Send a RCPE(RCP Error Message) to Peer followed by RCPK with
* old key-code so that initiator(TV) can recognize
* failed key code */
sii8240_msc_req_locked(sii8240, START_MSC_MSG,
0, MSG_RCPE, RCPE_KEY_INVALID);
}
} else {
/* Input key value is release key
* Send the RCP ack */
sii8240_msc_req_locked(sii8240, START_MSC_MSG, 0, MSG_RCPK, key);
}
}
static void cbus_process_rap_key(struct sii8240_data *sii8240, u8 key)
{
u8 err = RAPK_NO_ERROR;
switch (key) {
case RAP_POLL:
/* no action, just sent to elicit an ACK */
break;
case RAP_CONTENT_ON:
/*TODO:A source shall not enable its TMDS unless it has received
* SET_HPD,sees active RxSense(RSEN) and sees PATH_EN{Sink} = 1
*/
tmds_control(sii8240, true);
break;
case RAP_CONTENT_OFF:
/*TODO: With MHL 1.2 Specs,For a Source, CONTENT_OFF does not
* necessarily means that TMDS output is disabled */
tmds_control(sii8240, false);
break;
default:
pr_debug("sii8240: unrecognized RAP code %u\n", key);
err = RAPK_UNRECOGNIZED;
}
sii8240_msc_req_locked(sii8240, START_MSC_MSG, 0, MSG_RAPK, err);
}
static void sii8240_power_down(struct sii8240_data *sii8240)
{
pr_info("sii8240: %s\n", __func__);
if (sii8240->irq_enabled) {
disable_irq_nosync(sii8240->irq);
sii8240->irq_enabled = false;
pr_info("sii8240: interrupt disabled\n");
}
sii8240->state = STATE_DISCONNECTED;
sii8240->pdata->hw_onoff(0);
}
static int cbus_handle_write_state(struct sii8240_data *sii8240,
u8 offset, u8 data)
{
int ret = -1;
ret = sii8240_msc_req_locked
(sii8240, START_WRITE_STAT_SET_INT, offset, data, 0);
if (ret < 0) {
pr_info("sii8240: msc_req_locked error : 0x%X\n", offset);
return ret;
}
if (offset == CBUS_MHL_STATUS_OFFSET_0 &&
data == MHL_STATUS_DCAP_READY) {
/* notify the peer by updating the status register too */
sii8240_msc_req_locked(sii8240, START_WRITE_STAT_SET_INT,
CBUS_MHL_INTR_REG_0,
MHL_INT_DCAP_CHG, 0);
}
return ret;
}
static bool check_vbus_present(void)
{
bool ret = true;
union power_supply_propval value;
psy_do_property("sec-charger", get, POWER_SUPPLY_PROP_ONLINE, value);
pr_info("sec-charger : %d\n", value.intval);
if (value.intval == POWER_SUPPLY_TYPE_BATTERY
/*|| value.intval == POWER_SUPPLY_TYPE_WPC*/)
ret = false;
pr_info("VBUS : %s in %s\n", ret ? "IN" : "OUT", __func__);
return ret;
}
static void sii8240_setup_charging(struct sii8240_data *sii8240)
{
u8 devType, pow, plim, value;
u8 *peer_devcap = sii8240->regs.peer_devcap;
if ((peer_devcap[MHL_DEVCAP_MHL_VERSION] & 0xF0) >= 0x20) {
value = peer_devcap[MHL_DEVCAP_DEV_CAT];
devType = MHL_FLD_GET(value, 3, 0);
pow = MHL_FLD_GET(value, 4, 4);
plim = MHL_FLD_GET(value, 6, 5);
pr_info("%s: type=%d, pow=%d, plim=%d\n",
__func__, devType, pow, plim);
/* PLIM1 and PLIM0 together indicate the minimum VBUS power
* [PLIM1, PLIM0]
* {0,0} = Device delivers at least 500mA
* {0,1} = Device delivers at least 900mA
* {1,0} = Device delivers at least 1.5A
* {1,1} = Reserved*/
/*When set, device can output power across VBUS to the device
* connected through the cable,
* When clear, device cannot output power
* across VBUS to the device
* connected through the cable*/
if (pow == 1) {
/*Powered dongle*/
if (sii8240->pdata->sii8240_muic_cb && !(sii8240->muic_smartdock))
sii8240->pdata->sii8240_muic_cb(false, plim);
}
} else if ((peer_devcap[MHL_DEVCAP_MHL_VERSION] & 0xF0) == 0x10) {
u16 adopter_id = peer_devcap[MHL_DEVCAP_ADOPTER_ID_L] |
peer_devcap[MHL_DEVCAP_ADOPTER_ID_H] << 8;
pr_info("sii8240:%s adopter id:%d, reserved:%d\n", __func__,
adopter_id, peer_devcap[MHL_DEVCAP_RESERVED]);
if (adopter_id == 321 && peer_devcap[MHL_DEVCAP_RESERVED] == 2 &&
!(sii8240->muic_smartdock))
sii8240->pdata->sii8240_muic_cb(false, 0x1);
} else {
pr_err("sii8240:%s MHL version error - 0x%X\n", __func__,
peer_devcap[MHL_DEVCAP_MHL_VERSION]);
}
return;
}
static void sii8240_msc_event(struct work_struct *work)
{
int ret = -1;
struct cbus_data *data, *next;
struct sii8240_data *sii8240 = container_of(work, struct sii8240_data,
cbus_work);
mutex_lock(&sii8240->cbus_lock);
mutex_lock(&sii8240->lock);
list_for_each_entry_safe(data, next, &sii8240->cbus_data_list, list) {
if (sii8240->cbus_abort) {
pr_warn("sii8240: abort received. so wait 2secs\n");
sii8240->cbus_abort = false;
msleep(2000);
}
if (sii8240->state != STATE_DISCONNECTED) {
switch (data->cmd) {
case MSC_MSG:
switch (data->offset) {
case MSG_RCP:
pr_debug("sii8240: RCP KEY CODE:%d\n",
data->data);
cbus_process_rcp_key(sii8240,
data->data);
break;
case MSG_RAP:
pr_debug("sii8240: RAP Arrived\n");
cbus_process_rap_key(sii8240,
data->data);
break;
case MSG_RCPK:
pr_debug("sii8240: RCPK Arrived\n");
break;
case MSG_RCPE:
pr_debug("sii8240: RCPE Arrived\n");
break;
case MSG_RAPK:
pr_debug("sii8240: RAPK Arrived\n");
break;
default:
pr_debug("sii8240: MAC error\n");
break;
}
break;
case READ_DEVCAP:
pr_debug("sii8240: READ_DEVCAP : 0x%X\n",
data->offset);
ret = sii8240_devcap_read_locked(sii8240,
data->offset);
if (ret < 0) {
pr_err("error offset%d\n",
data->offset);
break;
}
sii8240->regs.peer_devcap[data->offset] = ret;
if (data->use_completion)
complete(&data->complete);
if (data->offset == MHL_DEVCAP_DEV_CAT)
sii8240_setup_charging(sii8240);
ret = 0;
break;
case SET_INT:
pr_info("sii8240: msc_event: SET_INT\n");
ret = sii8240_msc_req_locked(sii8240,
START_WRITE_STAT_SET_INT,
data->offset, data->data, 0);
if (ret < 0)
pr_err("sii8240: error set_int req\n");
break;
case WRITE_STAT:
pr_info("sii8240: msc_event: WRITE_STAT\n");
ret = cbus_handle_write_state(sii8240,
data->offset, data->data);
if (ret < 0)
pr_err("sii8240: error write_stat\n");
break;
case WRITE_BURST:
/* TODO: */
break;
case GET_STATE:
case GET_VENDOR_ID:
case SET_HPD:
case CLR_HPD:
case GET_MSC_ERR_CODE:
case GET_SC3_ERR_CODE:
case GET_SC1_ERR_CODE:
case GET_DDC_ERR_CODE:
ret = sii8240_msc_req_locked(sii8240,
START_MISC_CMD, data->offset,
data->data, 0);
if (ret < 0)
pr_err("sii8240: offset%d,data=%d\n",
data->offset, data->data);
break;
default:
pr_info("sii8240: invalid msc command\n");
break;
}
}
list_del(&data->list);
if (!data->use_completion)
kfree(data);
}
mutex_unlock(&sii8240->lock);
mutex_unlock(&sii8240->cbus_lock);
}
static int sii8240_get_avi_info(struct sii8240_data *sii8240)
{
int ret = 0;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
mhl_modify_reg(hdmi, US_HDMI_INFO_PKT_CTRL,
BIT_RX_HDMI_CTRL2_VSI_MON_SEL_MASK,
BIT_RX_HDMI_CTRL2_VSI_MON_SEL_AVI_INFOFRAME);
ret = mhl_read_block_reg(hdmi,
0xB8, INFO_BUFFER, sii8240->aviInfoFrame);
print_hex_dump(KERN_ERR, "sii8240: avi_info = ",
DUMP_PREFIX_NONE, 16, 1,
sii8240->aviInfoFrame, INFO_BUFFER, false);
return ret;
}
#ifdef MHL_2X_3D
static int sii8240_get_vsi_info(struct sii8240_data *sii8240)
{
int ret;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
ret = mhl_modify_reg(hdmi,
0xA3, BIT_RX_HDMI_CTRL2_VSI_MON_SEL_MASK,
BIT_RX_HDMI_CTRL2_VSI_MON_SEL_VS_INFOFRAME);
ret = mhl_write_block_reg(hdmi, 0xB8,
INFO_BUFFER,
sii8240->vendorSpecificInfoFrame);
return ret;
}
/*Video Format Data in 3D support *
* 0x00: burst id_h *
* 0x01: burst id_l *
* 0x02: check_sum *
* 0x03: total_entry *
* 0x04: sequence index *
* 0x05: number of entry *
* 0x06: vdi_0_h *
* 0x07: vdi_0_l *
* 0x08: vdi_1_h *
* 0x09: vdi_1_l *
* 0x0A: vdi_2_h *
* 0x0B: vdi_2_l *
* 0x0C: vdi_3_h *
* 0x0D: vdi_3_l *
* 0x0E: vdi_4_h *
* 0x0F: vdi_4_l */
static int sii8240_check_3D_vic(struct sii8240_data *sii8240,
u8 tot_ent, u8 num_ent, u8 *data)
{
struct i2c_client *tmds = sii8240->pdata->tmds_client;
int ret = 0;
if ((sii8240->vic_data.tot_ent - (sii8240->vic_data.num_ent + num_ent))
>= 0) {
pr_info("num_ent : %d\n", num_ent);
memcpy(&sii8240->vic_data.vdi[sii8240->vic_data.num_ent],
data, (2*num_ent));
sii8240->vic_data.num_ent += num_ent;
}
if (sii8240->vic_data.num_ent == sii8240->vic_data.tot_ent) {
if ((sii8240->hpd_status == true) &&
(sii8240->dtd_data.num_ent == sii8240->dtd_data.tot_ent)) {
/*make HPD be high*/
ret = mhl_modify_reg(tmds, UPSTRM_HPD_CTRL_REG,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_MASK|
BIT_HPD_CTRL_HPD_OUT_OVR_EN_MASK,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_ON|
BIT_HPD_CTRL_HPD_OUT_OVR_EN_ON);
}
pr_info("HPD high\n");
}
return ret;
}
static int sii8240_check_3D_dtd(struct sii8240_data *sii8240,
u8 tot_ent, u8 num_ent, u8 *data)
{
struct i2c_client *tmds = sii8240->pdata->tmds_client;
int ret = 0;
if ((sii8240->dtd_data.tot_ent -
(sii8240->dtd_data.num_ent + num_ent)) >= 0) {
memcpy(&sii8240->dtd_data.vdi[sii8240->dtd_data.num_ent],
data, (2*num_ent));
sii8240->dtd_data.num_ent += num_ent;
}
if (sii8240->dtd_data.num_ent == sii8240->dtd_data.tot_ent) {
if ((sii8240->hpd_status == true) &&
(sii8240->vic_data.num_ent == sii8240->vic_data.tot_ent)) {
/*make HPD be high*/
ret = mhl_modify_reg(tmds, UPSTRM_HPD_CTRL_REG,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_MASK|
BIT_HPD_CTRL_HPD_OUT_OVR_EN_MASK,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_ON|
BIT_HPD_CTRL_HPD_OUT_OVR_EN_ON);
}
pr_info("HPD high\n");
}
return ret;
}
#endif
static int sii8240_get_write_burst(struct sii8240_data *sii8240)
{
struct i2c_client *cbus = sii8240->pdata->cbus_client;
int ret = 0;
u8 scratchpad[16];
#ifdef MHL_2X_3D
u16 burst_id = 0;
#endif
memset(scratchpad, 0x00, 16);
ret = mhl_read_block_reg(cbus, CBUS_MHL_SCRPAD_REG_0, 16, scratchpad);
#ifdef MHL_2X_3D
burst_id = scratchpad[B_ID_H];
burst_id <<= 8;
burst_id |= scratchpad[B_ID_L];
switch (burst_id) {
case MHL_3D_VIC_CODE:
pr_info("vid code\n");
/*Initiation of first scratchpad*/
if (scratchpad[SEQ] == 0x01) {
sii8240->vic_data.tot_ent = scratchpad[TOT_ENT];
sii8240->vic_data.num_ent = 0;
}
sii8240_check_3D_vic(sii8240, scratchpad[TOT_ENT],
scratchpad[NUM_ENT], &scratchpad[VDI_0_H]);
break;
case MHL_3D_DTD_CODE:
pr_info("dtd code\n");
if (scratchpad[SEQ] == 0x01) {
sii8240->dtd_data.tot_ent = scratchpad[TOT_ENT];
sii8240->dtd_data.num_ent = 0;
}
sii8240_check_3D_dtd(sii8240, scratchpad[TOT_ENT],
scratchpad[NUM_ENT], &scratchpad[VDI_0_H]);
break;
default:
break;
}
#endif
return ret;
}
static int sii8240_check_avi_info_checksum(struct sii8240_data *sii8240,
u8 *data)
{
u8 checksum = 0;
u8 i = 0;
u8 infoframedata[INFO_BUFFER];
memcpy(infoframedata, data, INFO_BUFFER);
for (i = 0; i < INFO_BUFFER; i++)
checksum += infoframedata[i];
checksum = 0x100 - checksum;
return checksum;
}
static void sii8240_set_avi_info_checksum_data(struct sii8240_data *sii8240)
{
u8 checksum = 0;
u8 i;
/*these are set by the hardware*/
checksum = 0x82 + 0x02 + 0x0D;
for (i = 1; i < SIZE_AVI_INFOFRAME; i++)
checksum += sii8240->output_avi_data[i];
checksum = 0x100 - checksum;
sii8240->output_avi_data[0] = checksum;
}
static int sii8240_check_avi_info(struct sii8240_data *sii8240)
{
int ret = 0, i;
for (i = 0; i < INFO_BUFFER; i++) {
if (sii8240->current_aviInfoFrame[i] !=
sii8240->aviInfoFrame[i])
ret = 1;
}
return ret;
}
static void sii8240_set_colorspace
(struct sii8240_data *sii8240, bool pack_pixel)
{
int ret = 0;
u8 colorspace = 0;
u8 input_range = 0;
u8 data;
struct i2c_client *tmds = sii8240->pdata->tmds_client;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
if (pack_pixel)
pr_debug("sii8240: %s(): pack_pixel\n", __func__);
else
pr_debug("sii8240: %s(): none pack_pixel\n", __func__);
if (pack_pixel)
/*Video stream is encoded in PackedPixel mode*/
data = BIT_VID_MODE_m1080p_ENABLE;
else
data = BIT_VID_MODE_m1080p_DISABLE;
ret = mhl_modify_reg(tmds, 0x4A, BIT_VID_MODE_m1080p_MASK, data);
if (pack_pixel)
/*The incoming HDMI pixel clock is multiplied
by 2 for PackedPixel mode*/
data = BIT_MHLTX_CTL4_MHL_CLK_RATIO_2X;
else
data = BIT_MHLTX_CTL4_MHL_CLK_RATIO_3X;
ret = mhl_modify_reg(hdmi, 0x83,
BIT_MHLTX_CTL4_MHL_CLK_RATIO_MASK, data);
if (pack_pixel)
/*the outgoing MHL link clock is
configured as PackedPixel clock*/
data = 0x60;
else
data = 0xA0;
ret = mhl_write_byte_reg(hdmi, 0x85, data);
/*[INFO_DATA1] 0x00 : nodata
0x20 : YCbCr 4:2:2
0x40 : YCbCr 4:4:4
0x60 ; Future*/
colorspace = (sii8240->current_aviInfoFrame
[INFO_CHECKSUM + INFO_DATA1] & 0x60);
pr_info("sii8240: colorspace : 0x%X :", colorspace);
switch (colorspace) {
case 0x00:
pr_cont("RGB :");
colorspace = 0x00;
/*[INFO_DATA3] 0x00 : Default
0x04 : Limited Range
0x08 : Full Range
0x0C : Reserved*/
input_range = (sii8240->current_aviInfoFrame
[INFO_CHECKSUM + INFO_DATA3] & 0x0C);
break;
case 0x20:
pr_cont("YCBCR 422 :");
colorspace = 0x02;
input_range = (sii8240->current_aviInfoFrame
[INFO_CHECKSUM + INFO_DATA5] & 0xC0);
break;
case 0x40:
pr_cont("YCBCR 444 :");
input_range = (sii8240->current_aviInfoFrame
[INFO_CHECKSUM + INFO_DATA5] & 0xC0);
colorspace = 0x01;
break;
case 0x60:
pr_cont("FUTURE :");
colorspace = 0x00;
input_range = (sii8240->current_aviInfoFrame
[INFO_CHECKSUM + INFO_DATA5] & 0xC0);
break;
}
/*Input range checking*/
switch (input_range) {
case 0x00:
pr_cont("input_range AUTO\n");
input_range = 0x00;
break;
case 0x04:
pr_cont("input_range Limited\n");
input_range = 0x08;
break;
case 0x08:
pr_cont("input_range FULL\n");
input_range = 0x04;
break;
case 0x0C:
pr_cont("input_range Reserved\n");
input_range = 0x00;
break;
default:
pr_cont("input_range default\n");
input_range = 0x00;
break;
}
if (!sii8240->hdmi_sink) {
colorspace = 0x00;
input_range = BIT_TPI_OUTPUT_QUAN_RANGE_LIMITED;
}
/*The output video encoding is forced to YCbCr 4:2:2
by setting the input video format and
output video format at TPI:0x09 and TPI:0x0A.*/
data = colorspace|input_range;
ret = mhl_modify_reg(tpi, 0x09,
BIT_TPI_INPUT_FORMAT_MASK |
BIT_TPI_INPUT_QUAN_RANGE_MASK, data);
if (pack_pixel)
colorspace = 0x02;
else if (input_range != 0x04)
input_range = 0x08;
data = colorspace|input_range;
ret = mhl_modify_reg(tpi, 0x0A,
BIT_TPI_OUTPUT_FORMAT_MASK |
BIT_TPI_OUTPUT_QUAN_RANGE_MASK, data);
return;
}
static int sii8240_set_mhl_timing(struct sii8240_data *sii8240)
{
/*set packed pixcel information*/
memset(sii8240->support_mhl_timing.avi_infoframe,
0x00, HDMI_VFRMT_MAX);
sii8240->support_mhl_timing.
avi_infoframe[HDMI_PATTERN_GEN] = 1;
sii8240->support_mhl_timing.
avi_infoframe[HDMI_VFRMT_1920x1080p60_16_9] = 1;
sii8240->support_mhl_timing.
avi_infoframe[HDMI_VFRMT_1920x1080p50_16_9] = 1;
#ifdef MHL_2X_3D
sii8240->support_mhl_timing.
avi_infoframe[HDMI_VFRMT_1280x720p50_16_9] = 1;
sii8240->support_mhl_timing.
avi_infoframe[HDMI_VFRMT_1280x720p60_16_9] = 1;
sii8240->support_mhl_timing.
avi_infoframe[HDMI_VFRMT_1920x1080p24_16_9] = 1;
sii8240->support_mhl_timing.
avi_infoframe[HDMI_VFRMT_1920x1080p30_16_9] = 1;
#endif
sii8240->support_mhl_timing.
avi_infoframe[HDMI_VFRMT_640x480p60_4_3] = 1;
return 1;
}
static bool sii8240_get_mhl_timing(struct sii8240_data *sii8240, u8 vic)
{
bool ret = false;
/*get packed pixcel information*/
if (sii8240->support_mhl_timing.avi_infoframe[vic] == 1)
ret = true;
switch (vic) {
case HDMI_VFRMT_1280x720p50_16_9:
case HDMI_VFRMT_1280x720p60_16_9:
case HDMI_VFRMT_1920x1080p24_16_9:
case HDMI_VFRMT_1920x1080p30_16_9:
sii8240->input_3d_format = NON_FRAME_PACKING_3D;
ret = false;
break;
default:
break;
}
return ret;
}
static int sii8240_packed_pixel_avi_info(struct sii8240_data *sii8240)
{
int ret = 0;
bool patternGen = false;
u8 vic;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
ret = sii8240_check_avi_info_checksum(sii8240, sii8240->aviInfoFrame);
if (ret) {
pr_err("sii8240: avi info checksum is fail\n");
set_hdmi_mode(sii8240, false);
return ret;
}
/*AVI information was changed newly and checksum is also no problems*/
set_hdmi_mode(sii8240, true);
/* VIC = video format Identification Code
16 : 1920x1080p 60Hz
31 : 1920x1080p 50HZ
0 : no VIC format in CEA document */
vic = sii8240->aviInfoFrame[INFO_VIC];
patternGen = sii8240_get_mhl_timing(sii8240, vic);
if (patternGen) {
pr_info("sii8240: over 75MHz\n");
sii8240->regs.link_mode &= ~MHL_STATUS_CLK_MODE_NORMAL;
sii8240->regs.link_mode |= MHL_STATUS_CLK_MODE_PACKED_PIXEL;
} else {
sii8240->regs.link_mode &= ~MHL_STATUS_CLK_MODE_PACKED_PIXEL;
sii8240->regs.link_mode |= MHL_STATUS_CLK_MODE_NORMAL;
}
pr_info("vic value = %d, linkmode = 0x%x\n",
vic, sii8240->regs.link_mode);
sii8240_msc_req_locked(sii8240, START_WRITE_STAT_SET_INT,
CBUS_MHL_STATUS_OFFSET_1,
sii8240->regs.link_mode, 0);
/*Copy avi information to current variable*/
memcpy(&sii8240->current_aviInfoFrame,
&sii8240->aviInfoFrame, INFO_BUFFER);
memcpy(&sii8240->output_avi_data[0],
&sii8240->aviInfoFrame[INFO_CHECKSUM], SIZE_AVI_INFOFRAME);
if (patternGen) {
/*Because of the 16bits carried in PackedPixel mode,
if the incoming video encoding is RGB of YCbCr 4:4:4,
the video must be converted to YCbCr 4:2:2*/
pr_info("sii8240: convert Color space\n");
sii8240->output_avi_data[INFO_DATA1] &= ~0x60;
sii8240->output_avi_data[INFO_DATA1] |= 0x20;
}
sii8240_set_avi_info_checksum_data(sii8240);
/*HDMI input clock is under 75MHz*/
if (vic <= HDMI_VFRMT_720x480p60_16_9 && vic != 0) {
pr_err("sii8240: under 75MHz\n");
mhl_write_byte_reg(hdmi, 0x4C, 0xD0);
}
/*HDMI input clock is over 75MHz*/
if (patternGen)
mhl_write_byte_reg(hdmi, 0x4C, 0xD0);
sii8240_set_colorspace(sii8240, patternGen);
return ret;
}
static int sii8240_bypass_avi_info(struct sii8240_data *sii8240)
{
int ret;
/*struct i2c_client *tpi = sii8240->pdata->tpi_client; */
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
pr_info("sii8240_bypass_avi_info\n");
ret = sii8240_check_avi_info_checksum(sii8240, sii8240->aviInfoFrame);
if (ret) {
pr_err("sii8240: avi info checksum is fail\n");
set_hdmi_mode(sii8240, false);
return ret;
}
/*AVI information was changed newly and checksum is also no problems*/
sii8240->regs.link_mode &= ~MHL_STATUS_CLK_MODE_PACKED_PIXEL;
sii8240->regs.link_mode |= MHL_STATUS_CLK_MODE_NORMAL;
sii8240_msc_req_locked(sii8240, START_WRITE_STAT_SET_INT,
CBUS_MHL_STATUS_OFFSET_1,
sii8240->regs.link_mode, 0);
ret = mhl_write_byte_reg(hdmi, 0x4C, 0xD0);
/*bypass avi info*/
memcpy(&sii8240->current_aviInfoFrame,
&sii8240->aviInfoFrame, INFO_BUFFER);
memcpy(&sii8240->output_avi_data[0],
&sii8240->aviInfoFrame[INFO_CHECKSUM], SIZE_AVI_INFOFRAME);
sii8240_set_avi_info_checksum_data(sii8240);
sii8240_set_colorspace(sii8240, false);
return ret;
}
static void sii8240_avi_control_thread(struct work_struct *work)
{
struct sii8240_data *sii8240 = container_of(work, struct sii8240_data,
avi_control_work);
struct i2c_client *tmds = sii8240->pdata->tmds_client;
#ifdef SFEATURE_HDCP_SUPPORT
struct i2c_client *tpi = sii8240->pdata->tpi_client;
#endif
int ret = 0;
#ifdef MHL_2X_3D
struct cbus_data *cbus_cmd;
bool feature_3d = false;
#endif
mutex_lock(&sii8240->lock);
if (sii8240->avi_work == true) {
sii8240->avi_work = false;
switch (sii8240->avi_cmd) {
case HPD_HIGH_EVENT:
/*We will read minimum devcap information*/
sii8240_queue_devcap_read_locked(sii8240,
MHL_DEVCAP_MHL_VERSION);
sii8240_queue_devcap_read_locked(sii8240,
MHL_DEVCAP_VID_LINK_MODE);
if (sii8240->cbus_ready) {
pr_info("sii8240: DCAP_READY and read devcap\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_RESERVED, 0) < 0)
pr_info("sii8240: MHL_RESERVED read fail\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_ADOPTER_ID_H, 0) < 0)
pr_info("sii8240: ADOPTER_ID_H read fail\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_ADOPTER_ID_L, 0) < 0)
pr_info("sii8240: ADOPTER_ID_L read fail\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_DEV_CAT, 0) < 0)
pr_info("sii8240: DEVCAP_DEV_CAT read fail\n");
}
pr_info("sii8240: HPD high - MHL ver=0x%x, linkmode = 0x%x\n",
sii8240->regs.peer_devcap[MHL_DEVCAP_MHL_VERSION],
sii8240->regs.peer_devcap[MHL_DEVCAP_VID_LINK_MODE]);
ret = sii8240_read_edid(sii8240);
if (ret < 0) {
pr_info("sii8240: edid read failed\n");
goto exit;
}
if (((sii8240->regs.peer_devcap[MHL_DEVCAP_MHL_VERSION] & 0xF0) >= 0x20)
&& (sii8240->regs.peer_devcap[MHL_DEVCAP_VID_LINK_MODE] &
(MHL_DEV_VID_LINK_SUPP_PPIXEL |
MHL_DEV_VID_LINK_SUPPYCBCR422)) &&
sii8240->hdmi_sink == true) {
pr_info("CEA_NEW_AVI MHL RX Ver.2.x\n");
#ifdef MHL_2X_3D
feature_3d = true;
/*Request 3D interrupt to sink device.
* To do msc command*/
cbus_cmd = kzalloc(sizeof(struct cbus_data),
GFP_KERNEL);
if (!cbus_cmd) {
pr_err("sii8240: failed to allocate cbus data\n");
goto exit;
}
cbus_cmd->cmd = SET_INT;
cbus_cmd->offset = CBUS_MHL_INTR_REG_0;
cbus_cmd->data = MHL_INT_3D_REQ;
list_add_tail(&cbus_cmd->list,
&sii8240->cbus_data_list);
queue_work(sii8240->cbus_cmd_wqs, &sii8240->cbus_work);
#endif
} else {
/* TODO: This case MHL 1.0*/
pr_info("CEA_NEW_AVI MHL RX Ver.1.x, Pixel 60\n");
}
if (sii8240->hdmi_sink) {
sii8240->regs.intr_masks.intr7_mask_value =
BIT_INTR7_CEA_NO_AVI|BIT_INTR7_CEA_NO_VSI;
mhl_write_byte_reg(tmds, 0x7D,
sii8240->regs.intr_masks.intr7_mask_value);
sii8240->regs.intr_masks.intr8_mask_value =
BIT_INTR8_CEA_NEW_AVI|BIT_INTR8_CEA_NEW_VSI;
ret = mhl_write_byte_reg(tmds, 0x7E,
sii8240->regs.intr_masks.intr8_mask_value);
if (ret < 0)
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
sii8240->regs.intr_masks.intr5_mask_value =
BIT_INTR5_CKDT_CHANGE | BIT_INTR5_SCDT_CHANGE;
ret = mhl_write_byte_reg(tmds, 0x78,
sii8240->regs.intr_masks.intr5_mask_value);
if (ret < 0)
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
}
/*make HPD be high*/
ret = mhl_modify_reg(tmds, UPSTRM_HPD_CTRL_REG,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_MASK |
BIT_HPD_CTRL_HPD_OUT_OVR_EN_MASK,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_ON |
BIT_HPD_CTRL_HPD_OUT_OVR_EN_ON);
if (ret < 0)
pr_info("sii8240: %s():%d failed\n",
__func__, __LINE__);
if (sii8240->hdmi_sink == false) {
#ifdef SFEATURE_HDCP_SUPPORT
if (sii8240->hdcp_support) {
sii8240->regs.intr_masks.intr_tpi_mask_value =
BIT_TPI_INTR_ST0_HDCP_AUTH_STATUS_CHANGE_EVENT |
BIT_TPI_INTR_ST0_HDCP_VPRIME_VALUE_READY_EVENT |
BIT_TPI_INTR_ST0_HDCP_SECURITY_CHANGE_EVENT |
BIT_TPI_INTR_ST0_BKSV_DONE |
BIT_TPI_INTR_ST0_BKSV_ERR;
ret = mhl_write_byte_reg(tpi, 0x3C,
sii8240->regs.intr_masks.intr_tpi_mask_value);
}
#endif
ret = set_hdmi_mode(sii8240, false);
if (ret < 0) {
pr_info("sii8240: %s():%d failed\n",
__func__, __LINE__);
goto exit;
}
ret = tmds_control(sii8240, true);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
goto exit;
}
}
break;
case CEA_NEW_AVI:
pr_info("sii8240: %s():%d CEA_NEW_AVI MHL RX Ver.2.x\n",
__func__, __LINE__);
#ifdef SFEATURE_HDCP_SUPPORT
if (sii8240->hdcp_support) {
sii8240->regs.intr_masks.intr_tpi_mask_value =
BIT_TPI_INTR_ST0_HDCP_AUTH_STATUS_CHANGE_EVENT |
BIT_TPI_INTR_ST0_HDCP_VPRIME_VALUE_READY_EVENT |
BIT_TPI_INTR_ST0_HDCP_SECURITY_CHANGE_EVENT |
BIT_TPI_INTR_ST0_BKSV_DONE |
BIT_TPI_INTR_ST0_BKSV_ERR;
ret = mhl_write_byte_reg(tpi, 0x3C,
sii8240->regs.intr_masks.intr_tpi_mask_value);
}
#endif
/*hdmi sink but avi => hdmi mode*/
ret = set_hdmi_mode(sii8240, true);
if (ret < 0) {
pr_debug("sii8240: %s():%d failed!\n",
__func__, __LINE__);
goto exit;
}
if (((sii8240->regs.peer_devcap[MHL_DEVCAP_MHL_VERSION] & 0xF0)
>= 0x20) &&
(sii8240->regs.peer_devcap[MHL_DEVCAP_VID_LINK_MODE] &
MHL_DEV_VID_LINK_SUPP_PPIXEL) &&
(sii8240->regs.peer_devcap[MHL_DEVCAP_VID_LINK_MODE] &
MHL_DEV_VID_LINK_SUPPYCBCR422)) {
ret = sii8240_packed_pixel_avi_info(sii8240);
} else
ret = sii8240_bypass_avi_info(sii8240);
ret = tmds_control(sii8240, true);
break;
default:
pr_info("default cmd\n");
break;
}
sii8240->avi_work = false;
}
sii8240->avi_cmd = AVI_CMD_NONE;
exit:
mutex_unlock(&sii8240->lock);
}
static void sii8240_detection_restart(struct work_struct *work)
{
struct sii8240_data *sii8240 = container_of(work, struct sii8240_data,
redetect_work);
mutex_lock(&sii8240->lock);
pr_info("sii8240: detection restarted\n");
if (sii8240->muic_state == MHL_DETTACHED) {
pr_info("sii8240 : already powered off\n");
goto disconnection_exit;
}
sii8240->state = STATE_DISCONNECTED;
sii8240->cbus_ready = 0;
sii8240->pdata->hw_reset();
if (sii8240_init_regs(sii8240) < 0) {
pr_info("sii8240: redetection failed\n");
goto err_exit;
}
if (sii8240_set_mhl_timing(sii8240) < 0) {
pr_info("sii8240: set mhl timing failed\n");
goto err_exit;
}
if (switch_to_d3(sii8240) < 0)
pr_info("sii8240: switch to d3 error\n");
mutex_unlock(&sii8240->lock);
return;
err_exit:
queue_work(sii8240->cbus_cmd_wqs, &sii8240->redetect_work);
disconnection_exit:
mutex_unlock(&sii8240->lock);
}
static int sii8240_detection_callback(struct notifier_block *this,
unsigned long event, void *data)
{
int ret;
struct sii8240_data *sii8240 = container_of(this, struct sii8240_data,
mhl_nb);
muic_attached_dev_t attached_dev = *(muic_attached_dev_t *)data;
int handled = MHL_CON_UNHANDLED;
sii8240->muic_smartdock = false;
pr_info("sii8240: muic event: %ld, att_dev: %d\n", event, attached_dev);
switch (attached_dev) {
case ATTACHED_DEV_SMARTDOCK_TA_MUIC:
case ATTACHED_DEV_SMARTDOCK_USB_MUIC:
sii8240->muic_smartdock = true;
case ATTACHED_DEV_MHL_MUIC:
break;
default:
return MHL_CON_UNHANDLED;
}
if (event == sii8240->muic_state) {
pr_info("sii8240: Same muic event, Ignored!\n");
return MHL_CON_UNHANDLED;
}
sii8240->muic_state = event;
if (event) {
pr_info("sii8240:detection started\n");
wake_lock(&sii8240->mhl_wake_lock);
sii8240->cbus_ready = 0;
} else {
pr_info("sii8240:disconnection\n");
/* Charging stop when MHL detach */
if (sii8240->pdata->sii8240_muic_cb && !(sii8240->muic_smartdock))
sii8240->pdata->sii8240_muic_cb(false, -1);
wake_unlock(&sii8240->mhl_wake_lock);
mutex_lock(&sii8240->lock);
goto power_down;
}
mutex_lock(&sii8240->lock);
/* Reset flags,state of mhl */
sii8240->state = STATE_DISCONNECTED;
sii8240->rgnd = RGND_UNKNOWN;
sii8240->cbus_abort = false;
/* Set the board configuration so the SiI8240 has access to the
* external connector
*/
sii8240->pdata->hw_onoff(1);
sii8240->pdata->hw_reset();
ret = sii8240_init_regs(sii8240);
if (ret < 0) {
pr_err("sii8240: sii8240_init_regs error\n");
goto unhandled;
}
sii8240->hdmi_sink = false;
sii8240_set_mhl_timing(sii8240);
ret = switch_to_d3(sii8240);
if (ret < 0) {
pr_err("sii8240: switch_to_d3 error!\n");
goto unhandled;
}
mutex_unlock(&sii8240->lock);
pr_info("sii8240: detection_callback return !\n");
return MHL_CON_HANDLED;
unhandled:
if (sii8240->state == STATE_DISCONNECTED)
pr_cont(" (timeout)");
else if (sii8240->state == STATE_CBUS_UNSTABLE)
pr_cont(" (cbus unstable)");
else if (sii8240->state == STATE_NON_MHL_DETECTED)
pr_cont(" (Non-MHL device(USB?))");
pr_cont("\n");
power_down:
sii8240_power_down(sii8240);
mutex_unlock(&sii8240->lock);
if (sii8240->mhl_event_switch.state == 1) {
pr_info("sii8240: MHL switch event sent :0\n");
switch_set_state(&sii8240->mhl_event_switch, 0);
}
return handled;
}
static int sii8240_discovery_irq_handler(struct sii8240_data *sii8240, u8 intr)
{
u8 rgnd;
int ret = 0;
struct i2c_client *disc = sii8240->pdata->disc_client;
pr_info("sii8240_discovery_irq_handler : 0x%X\n", intr);
if (intr & RGND_RDY_INT) {
ret = mhl_read_byte_reg(disc, DISC_RGND_REG, &rgnd);
if (ret < 0) {
pr_err("sii8240: RGND read error:%d\n", ret);
return ret;
}
switch (rgnd & RGND_INTP_MASK) {
case RGND_INTP_OPEN:
pr_info("sii8240: RGND Open\n");
sii8240->rgnd = RGND_OPEN;
sii8240->state = STATE_MHL_USB_CONNECTED;
break;
case RGND_INTP_1K:
pr_info("sii8240: RGND 1K\n");
sii8240->rgnd = RGND_1K;
sii8240->state = STATE_MHL_CONNECTED;
break;
case RGND_INTP_2K:
pr_info("sii8240: RGND 2K\n");
sii8240->rgnd = RGND_2K;
break;
case RGND_INTP_SHORT:
pr_info("sii8240: RGND Short\n");
break;
};
} else if (intr & CBUS_UNSTABLE_INT) {
pr_info("sii8240: CBUS unstable\n");
sii8240->state = STATE_CBUS_UNSTABLE;
}
return ret;
}
static int sii8240_msc_irq_handler(struct sii8240_data *sii8240, u8 intr)
{
int ret = 0;
u8 hpd;
u8 temp;
u8 cbus_intr[4], cbus_status[4];
struct i2c_client *cbus = sii8240->pdata->cbus_client;
struct i2c_client *tmds = sii8240->pdata->tmds_client;
struct cbus_data *data;
ret = mhl_read_block_reg(cbus, CBUS_MHL_STATUS_OFFSET_0, 4,
cbus_status);
if (ret < 0)
goto err_exit;
ret = mhl_read_block_reg(cbus, CBUS_MHL_INTR_REG_0, 4, cbus_intr);
if (ret < 0)
goto err_exit;
if (intr & MSC_CMD_DONE)
complete(&sii8240->cbus_complete);
if (intr & MSC_HPD_RCVD) {
pr_info("sii8240: Downstream HPD Changed\n");
ret = mhl_read_byte_reg(cbus, CBUS_CONN_STATUS_REG, &hpd);
if (ret < 0)
goto err_exit;
ret = mhl_read_byte_reg(tmds, UPSTRM_HPD_CTRL_REG, &temp);
if (ret < 0)
goto err_exit;
if (DOWNSTREAM_HPD_MASK & hpd) {
pr_info("sii8240: HPD HIGH , Overriden: 0x%X\n", temp);
if (temp & HPD_OVERRIDE_EN) {
/* TODO:If HPD is overriden,//enable HPD_OUT bit
in upstream register */
sii8240->hpd_status = true;
sii8240->avi_cmd = HPD_HIGH_EVENT;
sii8240->avi_work = true;
queue_work(sii8240->avi_cmd_wqs,
&sii8240->avi_control_work);
/*Initiate sii8240's information variables*/
memset(&sii8240->current_aviInfoFrame,
0x00, INFO_BUFFER);
memset(&sii8240->output_avi_data,
0x00, SIZE_AVI_INFOFRAME);
memset(&sii8240->vendorSpecificInfoFrame,
0x00, INFO_BUFFER);
}
} else {
pr_info("sii8240: HPD LOW , Overriden: 0x%X\n", temp);
if (temp & HPD_OVERRIDE_EN) {
/* TODO:If HPD is overriden,clear HPD_OUT bit
upstream register */
sii8240->hpd_status = false;
sii8240->cbus_ready = 0;
mhl_modify_reg(tmds, UPSTRM_HPD_CTRL_REG,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_MASK|
BIT_HPD_CTRL_HPD_OUT_OVR_EN_MASK,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_OFF|
BIT_HPD_CTRL_HPD_OUT_OVR_EN_ON);
/*Disable interrupt*/
sii8240->regs.intr_masks.intr7_mask_value = 0;
sii8240->regs.intr_masks.intr8_mask_value = 0;
mhl_write_byte_reg(tmds, 0x7D,
sii8240->regs.intr_masks.intr7_mask_value);
mhl_write_byte_reg(tmds, 0x7E,
sii8240->regs.intr_masks.intr8_mask_value);
set_mute_mode(sii8240, true);
tmds_control(sii8240, false);
}
if (sii8240->mhl_event_switch.state == 1) {
pr_info("sii8240: MHL switch event sent :0\n");
switch_set_state(&sii8240->mhl_event_switch,
0);
}
}
}
if (intr & MSC_WRITE_STAT_RCVD) {
bool path_en_changed = false;
pr_info("sii8240: WRITE_STAT received\n");
sii8240->cbus_ready = cbus_status[0] & MHL_STATUS_DCAP_READY;
if (sii8240->cbus_ready)
pr_info("sii8240: DCAP_READY intr\n");
if (!(sii8240->regs.link_mode & MHL_STATUS_PATH_ENABLED) &&
(MHL_STATUS_PATH_ENABLED & cbus_status[1])) {
/* PATH_EN{SOURCE} = 0 and PATH_EN{SINK}= 1 */
sii8240->regs.link_mode |= MHL_STATUS_PATH_ENABLED;
path_en_changed = true;
} else if ((sii8240->regs.link_mode & MHL_STATUS_PATH_ENABLED)
&& !(MHL_STATUS_PATH_ENABLED & cbus_status[1])) {
/* PATH_EN{SOURCE} = 1 and PATH_EN{SINK}= 0 */
sii8240->regs.link_mode &= ~MHL_STATUS_PATH_ENABLED;
path_en_changed = true;
ret = tmds_control(sii8240, false);
if (ret < 0)
return ret;
}
if (path_en_changed)
sii8240_queue_cbus_cmd_locked(sii8240, WRITE_STAT,
CBUS_MHL_STATUS_OFFSET_1,
sii8240->regs.link_mode);
}
if (intr & MSC_MSG_RCVD) {
pr_debug("sii8240: MSC_MSG received\n");
data = kzalloc(sizeof(struct cbus_data), GFP_KERNEL);
if (!data) {
pr_err("sii8240: failed to allocate cbus data\n");
ret = -ENOMEM;
goto err_exit;
}
data->cmd = MSC_MSG;
mhl_read_byte_reg(cbus, MSC_MSG_RCVD_DATA1_REG, &data->offset);
mhl_read_byte_reg(cbus, MSC_MSG_RCVD_DATA2_REG, &data->data);
list_add_tail(&data->list, &sii8240->cbus_data_list);
queue_work(sii8240->cbus_cmd_wqs, &sii8240->cbus_work);
}
if (intr & MSC_WRITE_BURST_RCVD)
pr_debug("sii8240: WRITE_BURST received\n");
if (intr & MSC_SET_INT_RCVD) {
pr_info("sii8240: SET_INT received\n");
if (cbus_intr[0] & MHL_INT_DCAP_CHG) {
pr_info("sii8240: device capability changed\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_MHL_VERSION, 0) < 0)
pr_info("sii8240: MHL_VERSION read fail\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_RESERVED, 0) < 0)
pr_info("sii8240: MHL_RESERVED read fail\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_ADOPTER_ID_H, 0) < 0)
pr_info("sii8240: ADOPTER_ID_H read fail\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_ADOPTER_ID_L, 0) < 0)
pr_info("sii8240: ADOPTER_ID_L read fail\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_DEV_CAT, 0) < 0)
pr_info("sii8240: DEVCAP_DEV_CAT read fail\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_FEATURE_FLAG, 0) < 0)
pr_info("sii8240: FEATURE_FLAG read fail\n");
if (sii8240_queue_cbus_cmd_locked(sii8240, READ_DEVCAP,
MHL_DEVCAP_VID_LINK_MODE, 0) < 0)
pr_info("sii8240: VID_LINK_MODE read fail\n");
}
if (cbus_intr[0] & MHL_INT_DSCR_CHG) {
pr_debug("sii8240: scratchpad register change done\n");
sii8240_get_write_burst(sii8240);
}
if (cbus_intr[0] & MHL_INT_REQ_WRT) {
pr_info("sii8240: request-to-write received\n");
data = kzalloc(sizeof(struct cbus_data), GFP_KERNEL);
if (!data) {
pr_err("sii8240: failed to allocate cbus data\n");
ret = -ENOMEM;
goto err_exit;
}
data->cmd = SET_INT;
data->offset = CBUS_MHL_INTR_REG_0;
data->data = MHL_INT_GRT_WRT;
list_add_tail(&data->list, &sii8240->cbus_data_list);
queue_work(sii8240->cbus_cmd_wqs, &sii8240->cbus_work);
}
if (cbus_intr[0] & MHL_INT_GRT_WRT)
pr_debug("sii8240: grant-to-write received\n");
if (cbus_intr[1] & MHL_INT_EDID_CHG) {
pr_info("sii8240: MHL_INT_EDID_CHG\n");
sii8240_toggle_hpd(cbus);
/*Initiate sii8240's information variables*/
sii8240->hpd_status = false;
memset(&sii8240->current_aviInfoFrame,
0x00, INFO_BUFFER);
memset(&sii8240->output_avi_data,
0x00, SIZE_AVI_INFOFRAME);
memset(&sii8240->vendorSpecificInfoFrame,
0x00, INFO_BUFFER);
/*Disable interrupt*/
sii8240->regs.intr_masks.intr7_mask_value = 0;
sii8240->regs.intr_masks.intr8_mask_value = 0;
mhl_write_byte_reg(tmds, 0x7D,
sii8240->regs.intr_masks.intr7_mask_value);
mhl_write_byte_reg(tmds, 0x7E,
sii8240->regs.intr_masks.intr8_mask_value);
set_mute_mode(sii8240, true);
tmds_control(sii8240, false);
/*After HPD toggle, Need to setup again*/
sii8240->hpd_status = true;
sii8240->avi_cmd = HPD_HIGH_EVENT;
sii8240->avi_work = true;
queue_work(sii8240->avi_cmd_wqs,
&sii8240->avi_control_work);
/*Wake up avi control thread*/
}
}
if (intr & MSC_MSG_NACK_RCVD)
pr_debug(KERN_ERR "NACK received\n");
err_exit:
return ret;
}
/*Need to check*/
static int sii8240_msc_error_irq_handler(struct sii8240_data *sii8240, u8 intr)
{
int ret = 0;
u8 error;
struct i2c_client *cbus = sii8240->pdata->cbus_client;
if (intr == 0)
return ret;
if (intr & SEND_ERROR_INT) {
ret = mhl_read_byte_reg(cbus, MSC_SEND_ERROR_REG, &error);
if (ret < 0)
goto err_exit;
if (error & CMD_ABORT) {
pr_warn("sii8240: [sender] Command Aborted\n");
sii8240->cbus_abort = true;
}
if (error & CMD_UNDEF_OPCODE)
pr_warn("sii8240: [sender] Undefined Opcode\n");
if (error & CMD_TIMEOUT)
pr_warn("sii8240: [sender] Timeout\n");
if (error & CMD_RCVD_PROT_ERR)
pr_warn("sii8240: [sender] Protocol Error\n");
if (error & CMD_MAX_FAIL)
pr_warn("sii8240:[sender]Failed MAX retries\n");
}
if (intr & RECD_ERROR_INT) {
ret = mhl_read_byte_reg(cbus, MSC_RECVD_ERROR_REG, &error);
if (ret < 0)
goto err_exit;
if (error & CMD_ABORT) {
pr_warn("sii8240: [receiver] Command Aborted\n");
sii8240->cbus_abort = true;
}
if (error & CMD_UNDEF_OPCODE)
pr_warn("sii8240: [receiver] Undefined Opcode\n");
if (error & CMD_TIMEOUT)
pr_warn("sii8240: [reciever] Timeout\n");
if (error & CMD_RCVD_PROT_ERR)
pr_warn("sii8240: [reciever] Protocol Error\n");
if (error & CMD_MAX_FAIL)
pr_warn("sii8240:[reciever]Command Failed After MAX\n");
}
if (intr & RECD_DDC_ABORT_INT) {
/* TODO: We used to terminate connection after
* DDC_ABORT had been received.Need more details on how to
* handle this interrupt with MHL 1.2 specs. */
pr_warn("sii8240: DDC_ABORT received\n");
/*queue_work(sii8240->cbus_cmd_wqs, &sii8240->redetect_work);*/
}
err_exit:
return ret;
}
static int sii8240_rx_connected(struct sii8240_data *sii8240,
u8 int1, u8 cbus_con_st)
{
int ret = 0;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
struct i2c_client *disc = sii8240->pdata->disc_client;
struct cbus_data *cbus_cmd;
sii8240->cbus_connected = (cbus_con_st & 0x01);
if ((int1 & BIT_INTR4_MHL_EST) && (sii8240->cbus_connected)) {
sii8240->state = STATE_MHL_DISCOVERY_SUCCESS;
ret = mhl_hpd_control_low(sii8240);
if (ret < 0)
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
/*Must be set to 1.*/
ret = mhl_write_byte_reg(hdmi, MHLTX_TERM_CTRL_REG,
BIT_MHLTX_CTL1_DISC_OVRIDE_ON);
/*Enable discovery*/
pr_info("sii8240:Enable discovery\n");
ret = mhl_modify_reg(disc, 0x10, 0x01, 0x01);
memset(&sii8240->regs.intr_masks, 0,
sizeof(sii8240->regs.intr_masks));
sii8240->regs.intr_masks.intr1_mask_value =
BIT_INTR1_HPD_CHG;
sii8240->regs.intr_masks.intr4_mask_value =
(BIT_INTR4_CBUS_LKOUT | BIT_INTR4_CBUS_DISCONNECT);
sii8240->regs.intr_masks.intr_cbus0_mask_value =
(BIT_CBUS_MSC_MT_DONE |
BIT_CBUS_HPD_RCVD |
BIT_CBUS_MSC_MR_WRITE_STAT |
BIT_CBUS_MSC_MR_MSC_MSG |
BIT_CBUS_MSC_MR_WRITE_BURST |
BIT_CBUS_MSC_MR_SET_INT |
BIT_CBUS_MSC_MT_DONE_NACK);
sii8240->regs.intr_masks.intr_cbus1_mask_value =
(BIT_CBUS_DDC_ABRT | BIT_CBUS_CEC_ABRT | BIT_CBUS_CMD_ABORT);
ret = sii8240_set_interrupt(sii8240);
/*CBUS command*/
cbus_cmd = kzalloc(sizeof(struct cbus_data), GFP_KERNEL);
if (!cbus_cmd) {
pr_err("sii8240: failed to allocate cbus data\n");
return -ENOMEM;
}
cbus_cmd->cmd = WRITE_STAT;
cbus_cmd->offset = CBUS_MHL_STATUS_OFFSET_0;
cbus_cmd->data = MHL_STATUS_DCAP_READY;
list_add_tail(&cbus_cmd->list, &sii8240->cbus_data_list);
pr_debug("sii8240: schedule_work: cbus_work\n");
queue_work(sii8240->cbus_cmd_wqs, &sii8240->cbus_work);
} else if (int1 & BIT_INTR4_NON_MHL_EST) {
pr_info("sii8240: discovery failed\n");
sii8240->state = STATE_MHL_DISCOVERY_FAIL;
} else if (sii8240->cbus_connected) {
sii8240->state = STATE_MHL_DISCOVERY_ON;
memset(&sii8240->regs.intr_masks, 0,
sizeof(sii8240->regs.intr_masks));
sii8240->regs.intr_masks.intr4_mask_value =
(BIT_INTR4_MHL_EST|BIT_INTR4_NON_MHL_EST);
sii8240->regs.intr_masks.intr_cbus1_mask_value =
(BIT_CBUS_DDC_ABRT | BIT_CBUS_CEC_ABRT |
BIT_CBUS_CMD_ABORT);
ret = sii8240_set_interrupt(sii8240);
}
return ret;
}
static int sii8240_cbus_irq(struct sii8240_data *sii8240)
{
struct i2c_client *cbus = sii8240->pdata->cbus_client;
int ret;
u8 cbus_con_st = 0, msc_intr = 0,
msc_intr_en = 0, msc_err_intr = 0, msc_err_en = 0;
ret = mhl_read_byte_reg(cbus, CBUS_CONN_STATUS_REG, &cbus_con_st);
if (ret < 0) {
pr_info("sii8240: %s, %d\n", __func__, __LINE__);
return ret;
}
ret = mhl_read_byte_reg(cbus, CBUS_MSC_INTR_REG, &msc_intr);
if (ret < 0) {
pr_info("sii8240: %s, %d\n", __func__, __LINE__);
return ret;
}
ret = mhl_read_byte_reg(cbus, CBUS_MSC_INTR_ENABLE_REG, &msc_intr_en);
if (ret < 0) {
pr_info("sii8240: %s, %d\n", __func__, __LINE__);
return ret;
}
ret = mhl_read_byte_reg(cbus, CBUS_MSC_ERROR_INTR_REG, &msc_err_intr);
if (ret < 0) {
pr_info("sii8240: %s, %d\n", __func__, __LINE__);
return ret;
}
ret = mhl_read_byte_reg(cbus, CBUS_MSC_ERROR_INTR_ENABLE_REG,
&msc_err_en);
if (ret < 0) {
pr_info("sii8240: %s, %d\n", __func__, __LINE__);
return ret;
}
if (msc_err_intr)
mhl_write_byte_reg(cbus, CBUS_MSC_ERROR_INTR_REG, msc_err_intr);
if (msc_intr)
mhl_write_byte_reg(cbus, CBUS_MSC_INTR_REG, msc_intr);
/*Handling of Cbus error*/
ret = sii8240_msc_error_irq_handler(sii8240, msc_err_intr);
if (ret < 0)
pr_info("[ERROR]sii8240: %s():%d failed !\n",
__func__, __LINE__);
ret = sii8240_msc_irq_handler(sii8240, msc_intr);
if (ret < 0)
pr_info("[ERROR]sii8240: %s():%d failed !\n",
__func__, __LINE__);
return ret;
}
#ifdef SFEATURE_UNSTABLE_SOURCE_WA
static void sii8240_avi_check_work(struct work_struct *work)
{
struct sii8240_data *sii8240 = container_of(work, struct sii8240_data,
avi_check_work);
struct i2c_client *tpi = sii8240->pdata->tpi_client;
int ret = 0;
u8 checksum = 0;
pr_info("Sii8240: AVIF getting problem??\n");
ret = sii8240_get_avi_info(sii8240);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
return;
}
if ((sii8240->aviInfoFrame[0] != 0x82) ||
(sii8240->aviInfoFrame[1] != 0x02) ||
(sii8240->aviInfoFrame[2] != 0x0D)) {
pr_info("Sii8240: NO AVIF.:%d\n", __LINE__);
ret = mhl_modify_reg(tpi, 0x1A,
TMDS_OUTPUT_CONTROL_MASK,
TMDS_OUTPUT_CONTROL_ACTIVE);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
return;
}
ret = mhl_modify_reg(tpi, 0x1A,
TMDS_OUTPUT_CONTROL_MASK,
TMDS_OUTPUT_CONTROL_POWER_DOWN);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
return;
}
return;
}
if (sii8240_check_avi_info(sii8240)) {
pr_info("sii8240: AVI change! %d\n", __LINE__);
checksum = sii8240_check_avi_info_checksum
(sii8240, sii8240->aviInfoFrame);
if (checksum) {
pr_info("sii8240: %s():%d checksum error!\n",
__func__, __LINE__);
return;
}
memcpy(&sii8240->current_aviInfoFrame,
&sii8240->aviInfoFrame, INFO_BUFFER);
pr_info("Sii8240: avi change and tmds off\n");
ret = tmds_control(sii8240, false);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
return;
}
sii8240->avi_cmd = CEA_NEW_AVI;
sii8240->avi_work = true;
queue_work(sii8240->avi_cmd_wqs,
&sii8240->avi_control_work);
} else
pr_info("sii8240: %s():%d NO AVI change!!\n",
__func__, __LINE__);
}
static void sii8240_avif_check_callback(unsigned long data)
{
struct sii8240_data *sii8240 = (struct sii8240_data *) data;
if (((sii8240->aviInfoFrame[0] == 0x82) &&
(sii8240->aviInfoFrame[1] == 0x02) &&
(sii8240->aviInfoFrame[2] == 0x0D)) ||
(sii8240->hdmi_sink == false)) {
pr_info("Sii8240: %s() return AVIF callback\n", __func__);
} else
schedule_work(&sii8240->avi_check_work);
}
static int sii8240_check_ckdt_change(struct sii8240_data *sii8240)
{
int ret = 0;
struct i2c_client *tmds = sii8240->pdata->tmds_client;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
if (sii8240->r281 == 0) {
pr_info("Sii8240: %s():%d (r281 == 0)\n", __func__, __LINE__);
ret = mhl_read_byte_reg(hdmi, 0x81, &sii8240->r281);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
return ret;
}
}
pr_info("Sii8240: %s():%d r281 data: %02x\n",
__func__, __LINE__, sii8240->r281);
ret = mhl_write_byte_reg(hdmi, 0x81, (sii8240->r281 & (~0x3F)));
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n", __func__, __LINE__);
return ret;
}
if (sii8240->r281 & 0x3F) {
/* there is default value...*/
pr_info("Sii8240: there is default value.\n");
} else {
sii8240->r281 = 0xFF;
pr_info("Sii8240: there is non default value.\n");
}
ret = mhl_modify_reg(tmds, 0x80, BIT_TMDS_CCTRL_TMDS_OE,
BIT_TMDS_CCTRL_TMDS_OE);
if (ret < 0)
pr_info("sii8240: %s():%d failed !\n", __func__, __LINE__);
return ret;
}
static int sii8240_check_scdt_reg(struct sii8240_data *sii8240)
{
int ret = 0;
struct i2c_client *tmds = sii8240->pdata->tmds_client;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
if (sii8240->r281 != 0) {
pr_info("sii8240:%s r281 = 0x%02x\n", __func__, sii8240->r281);
ret = mhl_modify_reg(tmds, 0x80, BIT_TMDS_CCTRL_TMDS_OE, 0x0);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
return ret;
}
if (sii8240->r281 == 0xFF)
sii8240->r281 = 0;
ret = mhl_write_byte_reg(hdmi, 0x81, sii8240->r281);
if (ret < 0) {
pr_info("sii8240: %s():%d failed !\n",
__func__, __LINE__);
return ret;
}
sii8240->r281 = 0;
}
mod_timer(&sii8240->avi_check_timer, jiffies + msecs_to_jiffies(200));
return ret;
}
#endif
static int sii8240_audio_video_intr_control(struct sii8240_data *sii8240)
{
int ret = 0;
u8 upstatus, ceainfo;
u8 checksum = 0;
#ifdef SFEATURE_HDCP_SUPPORT
u8 value;
struct i2c_client *tpi = sii8240->pdata->tpi_client;
#endif
struct i2c_client *tmds = sii8240->pdata->tmds_client;
#ifdef SFEATURE_HDCP_SUPPORT
if (sii8240->hdcp_support) {
mhl_read_byte_reg(tpi, HDCP_INTR, &value);
mhl_write_byte_reg(tpi, HDCP_INTR, value);
if (value && sii8240->hpd_status)
sii8240_hdcp_control(sii8240, value);
}
#endif
mhl_read_byte_reg(tmds, US_INTR, &upstatus);
mhl_read_byte_reg(tmds, 0x7C, &ceainfo);
pr_debug("ssi8240: ceainfo: 0x%X, upstatus:0x%X\n",
ceainfo, upstatus);
if (upstatus)
mhl_write_byte_reg(tmds, US_INTR, upstatus);
if (ceainfo)
mhl_write_byte_reg(tmds, 0x7C, ceainfo);
if ((sii8240->regs.intr_masks.intr7_mask_value & BIT_INTR7_CEA_NO_AVI)
&& (upstatus & BIT_INTR7_CEA_NO_AVI))
pr_info("sii8240: NO_AVI_INFORMATION\n");
if (upstatus & BIT_INTR7_CEA_NO_VSI) {
pr_info("sii8240: BIT_INTR7_CEA_NO_VSI\n");
#ifdef MHL_2X_3D
sii8240->input_3d_format = NON_3D_VIDEO;
mhl_modify_reg(tmds, 0x51, BIT_VID_OVRRD_3DCONV_EN_MASK,
BIT_VID_OVRRD_3DCONV_EN_NORMAL);
#endif
}
if (upstatus & BIT_INTR7_CP_NEW_CP)
pr_info("sii8240: BIT_INTR7_CP_NEW_CP\n");
if (upstatus & BIT_INTR7_CP_SET_MUTE) {
pr_info("sii8240: BIT_INTR7_CP_SET_MUTE\n");
ret = set_mute_mode(sii8240, true);
if (ret < 0)
pr_info("sii8240: get avi info error\n");
}
if (upstatus & BIT_INTR7_CP_CLR_MUTE) {
pr_info("sii8240: BIT_INTR7_CP_CLR_MUTE\n");
ret = set_mute_mode(sii8240, false);
if (ret < 0)
pr_info("sii8240: get avi info error\n");
}
#ifdef MHL_2X_3D
/*VSI 3D*/
if ((ceainfo & BIT_INTR8_CEA_NEW_VSI) &&
(sii8240->hpd_status == true)) {
sii8240_get_vsi_info(sii8240);
ret = sii8240_check_avi_info_checksum(sii8240,
sii8240->vendorSpecificInfoFrame);
if (ret) {
pr_err("vendor specific checksum failing\n");
return 0;
}
#ifdef SII_DEBUG
print_hex_dump(KERN_ERR, "vsi = ",
DUMP_PREFIX_NONE, 16, 1,
sii8240->vendorSpecificInfoFrame, 16, false);
#endif
if ((sii8240->vendorSpecificInfoFrame[7] & 0xE0) == 0x40) {
if ((sii8240->vendorSpecificInfoFrame[8]
& 0xF0) == 0x00) {
/*frame packing 3D*/
pr_info("sii8240: frame packing 3D\n");
sii8240->input_3d_format = FRAME_PACKING_3D;
ret = mhl_modify_reg(tmds, 0x51,
BIT_VID_OVRRD_3DCONV_EN_MASK,
BIT_VID_OVRRD_3DCONV_EN_FRAME_PACK);
} else {
/*non frame packing 3D*/
pr_info("sii8240:non frame packing 3D\n");
sii8240->input_3d_format = NON_FRAME_PACKING_3D;
ret = mhl_modify_reg(tmds, 0x51,
BIT_VID_OVRRD_3DCONV_EN_MASK,
BIT_VID_OVRRD_3DCONV_EN_NORMAL);
}
}
}
#endif
/*AVI InfoFrame*/
if ((ceainfo & BIT_INTR8_CEA_NEW_AVI) &&
(sii8240->hpd_status == true)) {
ret = sii8240_get_avi_info(sii8240);
if (ret < 0) {
pr_info("sii8240 : get avi info error\n");
return ret;
}
if (sii8240_check_avi_info(sii8240)) {
pr_info("sii8240: %s():%d AVI change!\n",
__func__, __LINE__);
checksum = sii8240_check_avi_info_checksum
(sii8240, sii8240->aviInfoFrame);
if (checksum) {
pr_info("sii8240: %s():%d checksum error\n",
__func__, __LINE__);
return -EINVAL;
}
memcpy(&sii8240->current_aviInfoFrame,
&sii8240->aviInfoFrame, INFO_BUFFER);
tmds_control(sii8240, false);
sii8240->avi_cmd = CEA_NEW_AVI;
sii8240->avi_work = true;
queue_work(sii8240->avi_cmd_wqs,
&sii8240->avi_control_work);
} else
pr_info("sii8240: no AVIF\n");
}
return 0;
}
static irqreturn_t sii8240_irq_thread(int irq, void *data)
{
struct sii8240_data *sii8240 = data;
struct i2c_client *disc = sii8240->pdata->disc_client;
struct i2c_client *cbus = sii8240->pdata->cbus_client;
struct i2c_client *tmds = sii8240->pdata->tmds_client;
struct i2c_client *hdmi = sii8240->pdata->hdmi_client;
u8 intr1 = 0, intr1_en, int1_status, int2_status, cbus_con_st;
u8 value;
u8 cbus1, cbus2, cbus3, cbus4;
int ret = 0;
bool clock_stable = false;
mutex_lock(&sii8240->lock);
if (sii8240->state == STATE_DISCONNECTED) {
pr_info("sii8240: mhl is disconnected, so return irq\n");
mutex_unlock(&sii8240->lock);
return IRQ_HANDLED;
}
ret = mhl_read_byte_reg(disc, DISC_INTR_REG, &intr1);
if (ret < 0) {
pr_err("[ERROR] %s() DISC_INTR_REG\n", __func__);
goto err_exit2;
}
ret = mhl_read_byte_reg(disc, DISC_INTR_ENABLE_REG, &intr1_en);
if (ret < 0) {
pr_err("[ERROR] %s() DISC_INTR_ENABLE_REG\n", __func__);
goto err_exit2;
}
ret = mhl_read_byte_reg(cbus, CBUS_CONN_STATUS_REG, &cbus_con_st);
if (ret < 0) {
pr_err("[ERROR] %s() CBUS_CONN_STATUS_REG\n", __func__);
goto err_exit2;
}
ret = mhl_read_byte_reg(tmds, 0x71, &int1_status);
if (ret < 0) {
pr_err("[ERROR] %s() mhl_read_byte_reg : 0x71\n", __func__);
goto err_exit2;
}
ret = mhl_read_byte_reg(tmds, 0x74, &int2_status);
if (ret < 0) {
pr_err("[ERROR] %s() mhl_read_byte_reg : 0x74\n", __func__);
goto err_exit2;
}
/*Cbus link interrupt*/
ret = mhl_read_byte_reg(cbus, 0x20, &cbus1);
if (ret < 0)
goto err_exit2;
ret = mhl_read_byte_reg(cbus, 0x21, &cbus2);
if (ret < 0)
goto err_exit2;
ret = mhl_read_byte_reg(cbus, 0x22, &cbus3);
if (ret < 0)
goto err_exit2;
ret = mhl_read_byte_reg(cbus, 0x23, &cbus4);
if (ret < 0)
goto err_exit2;
pr_debug("sii8240: state:%d disc:0x%X/0x%X, cbus_con:0x%X ",
sii8240->state, intr1, intr1_en, cbus_con_st);
pr_debug(" tmds:0x%X,0x%X cbus:0x%X,0x%X,0x%X,0x%X\n",
int1_status, int2_status, cbus1, cbus2, cbus3, cbus4);
switch (sii8240->state) {
case STATE_MHL_READY_RGND_DETECT:
/* interrupts related to discovery & connection */
if (intr1) {
ret = sii8240_discovery_irq_handler(sii8240, intr1);
if (ret < 0) {
pr_err("[ERROR] %s() sii8240_discovery_irq_handler\n",
__func__);
goto err_exit;
} }
if (sii8240->state == STATE_MHL_CONNECTED) {
pr_info("sii8240: mhl connected\n");
msleep(30);
if (sii8240->pdata->sii8240_muic_cb && !(sii8240->muic_smartdock))
sii8240->pdata->sii8240_muic_cb(true, 0x3);
ret = sii8240_init_regs(sii8240);
if (ret < 0) {
pr_err("[ERROR] %s() sii8240_init_regs\n",
__func__);
goto err_exit;
}
memset(&sii8240->regs.intr_masks,
0x0, sizeof(sii8240->regs.intr_masks));
sii8240->regs.intr_masks.intr4_mask_value =
(BIT_INTR4_MHL_EST|BIT_INTR4_NON_MHL_EST);
ret = sii8240_set_interrupt(sii8240);
if (ret < 0) {
pr_err("[ERROR] %s() sii8240_set_interrupt\n",
__func__);
goto err_exit;
}
} else if (intr1 & (RGND_RDY_INT|CBUS_UNSTABLE_INT)) {
pr_info("sii8240: rgnd 1k not detected\n");
if (sii8240->irq_enabled) {
disable_irq_nosync(sii8240->irq);
sii8240->irq_enabled = false;
pr_info("sii8240: interrupt disabled\n");
}
/* If there is VBUS, charging start */
if (check_vbus_present()) {
if (sii8240->pdata->sii8240_muic_cb && !(sii8240->muic_smartdock))
sii8240->pdata->sii8240_muic_cb(false, 0x3);
}
queue_work(sii8240->cbus_cmd_wqs,
&sii8240->redetect_work);
}
break;
case STATE_MHL_CONNECTED:
ret = sii8240_rx_connected(sii8240, intr1, cbus_con_st);
if (ret < 0) {
pr_err("[ERROR] %s() sii8240_rx_connected\n", __func__);
goto err_exit;
}
if (sii8240->state == STATE_MHL_DISCOVERY_FAIL) {
/*changing of status information*/
if (sii8240->irq_enabled) {
disable_irq_nosync(sii8240->irq);
sii8240->irq_enabled = false;
pr_info("sii8240: interrupt disabled\n");
}
/* CTS 3.3.5.2 */
/* OTG should turn off when discovery fail */
if (sii8240->pdata->charging_type == POWER_SUPPLY_TYPE_OTG) {
if (sii8240->pdata->sii8240_muic_cb && !(sii8240->muic_smartdock))
sii8240->pdata->sii8240_muic_cb(false, -1);
}
queue_work(sii8240->cbus_cmd_wqs,
&sii8240->redetect_work);
}
break;
case STATE_MHL_DISCOVERY_SUCCESS:
/*checking of cbus locking out*/
if (intr1 & BIT_INTR4_CBUS_LKOUT) {
force_usb_id_switch_open(sii8240);
release_usb_id_switch_open(sii8240);
break;
}
/*checking of cbus disconnection*/
if (intr1 & BIT_INTR4_CBUS_DISCONNECT) {
if (sii8240->pdata->charging_type == POWER_SUPPLY_TYPE_OTG) {
if (sii8240->pdata->sii8240_muic_cb && !(sii8240->muic_smartdock))
sii8240->pdata->sii8240_muic_cb(false, -1);
}
mhl_write_byte_reg(hdmi, 0x80, 0xD0);
pr_info("sii8240: CBUS DISCONNECTED !!\n");
if (sii8240->irq_enabled) {
disable_irq_nosync(sii8240->irq);
sii8240->irq_enabled = false;
pr_info("sii8240: interrupt disabled\n");
}
queue_work(sii8240->cbus_cmd_wqs,
&sii8240->redetect_work);
break;
}
if (int2_status == 0x00) {
sii8240->regs.intr_masks.intr1_mask_value =
(BIT_INTR1_HPD_CHG);
mhl_write_byte_reg(tmds, 0x75,
sii8240->regs.intr_masks.intr1_mask_value);
}
/*checking HPD event high/low*/
if (int1_status & BIT_INTR1_HPD_CHG) {
ret = mhl_read_byte_reg
(tmds, MHL_TX_SYSSTAT_REG, &value);
if (ret < 0) {
pr_err("[ERROR] sii8240: %s():%d failed\n",
__func__, __LINE__);
goto err_exit;
}
memset(&sii8240->current_aviInfoFrame,
0x00, INFO_BUFFER);
memset(&sii8240->output_avi_data,
0x00, SIZE_AVI_INFOFRAME);
#ifdef MHL_2X_3D
memset(&sii8240->vendorSpecificInfoFrame,
0x00, INFO_BUFFER);
sii8240->input_3d_format = NON_3D_VIDEO;
ret = mhl_modify_reg(tmds, 0x51,
BIT_VID_OVRRD_3DCONV_EN_MASK,
BIT_VID_OVRRD_3DCONV_EN_NORMAL);
#endif
if (value & 0x02) {
pr_info("HPD event high\n");
if (sii8240->hpd_status == false) {
sii8240->hpd_status = true;
sii8240->avi_cmd = HPD_HIGH_EVENT;
sii8240->avi_work = true;
queue_work(sii8240->avi_cmd_wqs,
&sii8240->avi_control_work);
}
} else {
pr_info("Sii8240:HPD event low\n");
if (sii8240->hpd_status) {
sii8240->hpd_status = false;
tmds_control(sii8240, false);
ret = mhl_modify_reg(tmds,
UPSTRM_HPD_CTRL_REG,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_MASK |
BIT_HPD_CTRL_HPD_OUT_OVR_EN_MASK,
BIT_HPD_CTRL_HPD_OUT_OVR_VAL_OFF |
BIT_HPD_CTRL_HPD_OUT_OVR_EN_ON);
sii8240->regs.intr_masks.intr5_mask_value
= 0x00;
ret = mhl_write_byte_reg
(tmds, 0x78, 0x00);
ret = mhl_modify_reg(hdmi, 0xA1,
BIT_REG_RX_HDMI_CTRL0_hdmi_mode_overwrite_MASK,
BIT_REG_RX_HDMI_CTRL0_hdmi_mode_overwrite_HW_CTRL);
}
}
}
/*checking of MHL link*/
/*Upstream TMDS interrupt register*/
if (int2_status & BIT_INTR5_CKDT_CHANGE) {
pr_info("sii8240: CKDT: Upstream TMDS interrupt register\n");
#ifdef SFEATURE_UNSTABLE_SOURCE_WA
if (sii8240->hdmi_sink)
sii8240_check_ckdt_change(sii8240);
#endif
mhl_read_byte_reg(hdmi, 0xA0, &value);
if (value & BIT_TMDS_CSTAT_P3_PDO_MASK)
pr_info("sii8240: CKDT: stable\n");
else
pr_info("sii8240: CKDT: not stable\n");
clock_stable = true;
}
if (int2_status & BIT_INTR5_SCDT_CHANGE) {
mhl_read_byte_reg(hdmi, 0xA0, &value);
if (BIT_TMDS_CSTAT_P3_SCDT & value) {
pr_info("sii8240: BIT_TMDS_CSTAT_P3_SCDT\n");
memset(&sii8240->aviInfoFrame,
0x00, INFO_BUFFER);
#ifdef SFEATURE_UNSTABLE_SOURCE_WA
if (sii8240->hdmi_sink)
sii8240_check_scdt_reg(sii8240);
#endif
} else
pr_info("sii8240: disable_encryption\n");
}
if (int2_status & BIT_INTR5_RPWR5V_CHG) {
pr_info("sii8240: BIT_INTR5_RPWR5V_CHG\n");
mhl_read_byte_reg(tmds, 0x81, &value);
if (value & BIT_TMDS_CSTAT_RPWR5V_STATUS)
pr_info("sii8240: TMDS_CSTAT_RPWR5V_STATUS\n");
}
ret = sii8240_cbus_irq(sii8240);
if (ret < 0) {
pr_err("sii8240_cbus_irq failed\n");
goto err_exit;
}
ret = sii8240_audio_video_intr_control(sii8240);
if (ret < 0) {
pr_err("audio_video_intr_control failed\n");
goto err_exit;
}
break;
default:
break;
}
err_exit:
/* Clear interrupts */
ret = mhl_write_byte_reg(disc, DISC_INTR_REG, intr1);
if (ret < 0) {
pr_err("DISC_INTR_REG write failed\n");
goto err_exit2;
}
ret = mhl_write_byte_reg(tmds, 0x71, int1_status);
if (ret < 0) {
pr_err("0x71 write failed\n");
goto err_exit2;
}
ret = mhl_write_byte_reg(tmds, 0x74, int2_status);
if (ret < 0) {
pr_err("0x74 write failed\n");
goto err_exit2;
}
ret = mhl_write_byte_reg(cbus, 0x20, cbus1);
if (ret < 0) {
pr_err("0x20 write failed\n");
goto err_exit2;
}
ret = mhl_write_byte_reg(cbus, 0x21, cbus2);
if (ret < 0) {
pr_err("0x21 write failed\n");
goto err_exit2;
}
ret = mhl_write_byte_reg(cbus, 0x22, cbus3);
if (ret < 0) {
pr_err("0x22 write failed\n");
goto err_exit2;
}
ret = mhl_write_byte_reg(cbus, 0x23, cbus4);
if (ret < 0) {
pr_err("0x23 write failed\n");
goto err_exit2;
}
err_exit2:
mutex_unlock(&sii8240->lock);
wake_up(&sii8240->wq);
pr_debug("sii8240: IRQ_HANDLED\n");
return IRQ_HANDLED;
}
#if defined(CONFIG_SS_FACTORY) || defined(CONFIG_MHL_SWING_LEVEL)
static struct sii8240_data *sii8240_factory;
#endif
#ifdef CONFIG_MHL_SWING_LEVEL
static ssize_t sii8240_swing_test_show(struct class *dev,
struct class_attribute *attr, char *buf)
{
u8 clk = (sii8240_factory->pdata->swing_level >> 3) & 0x07;
u8 data = sii8240_factory->pdata->swing_level & 0x07;
return sprintf(buf, "SII8240,0x%X,0x%X,0x%X\n", clk, data,
sii8240_factory->pdata->swing_level);
}
static ssize_t sii8240_swing_test_store(struct class *dev,
struct class_attribute *attr,
const char *buf, size_t size)
{
if (buf[0] >= '0' && buf[0] <= '7' &&
buf[1] >= '0' && buf[1] <= '7')
sii8240_factory->pdata->swing_level = ((buf[0] - '0') << 3) |
(buf[1] - '0');
else
sii8240_factory->pdata->swing_level = 0x2D; /*Clk=5 and Data=5*/
return size;
}
static CLASS_ATTR(swing, 0664,
sii8240_swing_test_show, sii8240_swing_test_store);
extern int hdmi_forced_resolution;
static ssize_t sii8240_timing_test_show(struct class *dev,
struct class_attribute *attr, char *buf)
{
return sprintf(buf, "%d\n", hdmi_forced_resolution);
}
static ssize_t sii8240_timing_test_store(struct class *dev,
struct class_attribute *attr,
const char *buf, size_t size)
{
int timing, ret;
ret = kstrtouint(buf, 10, &timing);
if (ret < 0)
return size;
if (timing >= 0 && timing <= 44)
hdmi_forced_resolution = timing;
else
hdmi_forced_resolution = -1;
return size;
}
static CLASS_ATTR(timing, 0664,
sii8240_timing_test_show, sii8240_timing_test_store);
#endif
/* for factory test process */
#ifdef CONFIG_SS_FACTORY
#define SII_ID 0x82
static ssize_t sii8240_test_show(struct class *class,
struct class_attribute *attr,
char *buf)
{
struct i2c_client *tmds = sii8240_factory->pdata->tmds_client;
int size;
u8 sii_id = 0;
sii8240_factory->pdata->hw_onoff(1);
sii8240_factory->pdata->hw_reset();
mhl_read_byte_reg(tmds, 0x03, &sii_id);
pr_info("sii8240: check mhl : %X\n", sii_id);
sii8240_factory->pdata->hw_onoff(0);
size = sprintf(buf, "%d\n", sii_id == SII_ID ? 1 : 0);
return size;
}
static CLASS_ATTR(test_result, 0664,
sii8240_test_show, NULL);
#endif
#ifdef CONFIG_MUIC_NOTIFIER
static void sii8240_notifier_delay_work(struct work_struct *work)
{
struct sii8240_data *sii8240 = container_of(work, struct sii8240_data,
notifier_register_work.work);
muic_notifier_register(&sii8240->mhl_nb,
sii8240_detection_callback, MUIC_NOTIFY_DEV_MHL);
}
#endif /* CONFIG_MUIC_NOTIFIER */
static int __devinit sii8240_tmds_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int ret;
struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
struct sii8240_data *sii8240;
struct class *sec_mhl;
if (strcmp(client->adapter->name, "exynos5250-i2c")) {
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
return -EIO;
/* going to use block read/write, so check for this too */
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
return -EIO;
}
sii8240 = kzalloc(sizeof(struct sii8240_data), GFP_KERNEL);
if (!sii8240) {
dev_err(&client->dev, "failed to allocate driver data\n");
return -ENOMEM;
}
sii8240->pdata = client->dev.platform_data;
if (!sii8240->pdata) {
dev_err(&client->dev, "failed to find platform data\n");
ret = -EINVAL;
goto err_exit0;
}
sii8240->pdata->tmds_client = client;
i2c_set_clientdata(client, sii8240);
mutex_init(&sii8240->lock);
mutex_init(&sii8240->cbus_lock);
mutex_init(&sii8240->msc_lock);
mutex_init(&sii8240->input_lock);
wake_lock_init(&sii8240->mhl_wake_lock,
WAKE_LOCK_SUSPEND, "mhl_wake_lock");
init_waitqueue_head(&sii8240->wq);
sii8240->cbus_cmd_wqs = create_workqueue("sii8240-cmdwq");
if (sii8240->cbus_cmd_wqs == NULL)
ret = -ENXIO;
sii8240->avi_cmd_wqs = create_workqueue("sii8240-aviwq");
if (sii8240->avi_cmd_wqs == NULL)
ret = -ENXIO;
/*workqueue for CBUS*/
INIT_WORK(&sii8240->cbus_work, sii8240_msc_event);
INIT_LIST_HEAD(&sii8240->cbus_data_list);
INIT_WORK(&sii8240->redetect_work, sii8240_detection_restart);
INIT_WORK(&sii8240->avi_control_work, sii8240_avi_control_thread);
#ifdef CONFIG_MUIC_NOTIFIER
INIT_DELAYED_WORK(&sii8240->notifier_register_work, sii8240_notifier_delay_work);
#endif
#ifdef SII8240_CHECK_MONITOR
INIT_WORK(&sii8240->mhl_link_monitor_work, sii8240_link_monitor_work);
setup_timer(&sii8240->mhl_timer, sii8240_link_monitor_timer,
(unsigned long)sii8240);
#endif
sii8240->irq = client->irq;
ret = request_threaded_irq(client->irq, NULL, sii8240_irq_thread,
IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
"sii8240", sii8240);
if (ret < 0) {
dev_err(&client->dev, "[MHL]request irq Q failing\n");
goto err_exit1;
}
disable_irq(client->irq);
sii8240->irq_enabled = false;
sii8240->hdcp_support = true;
/*MHL sysfs entry*/
sec_mhl = class_create(THIS_MODULE, "mhl");
if (IS_ERR(&sec_mhl))
pr_info("failed to create class sec_mhl");
sii8240_mhldev = &client->dev;
g_sii8240 = sii8240;
#ifdef CONFIG_SS_FACTORY
sii8240_factory = sii8240;
ret = class_create_file(sec_mhl, &class_attr_test_result);
if (ret) {
dev_err(&client->dev, "failed to dev_attr_test_result\n");
goto err_exit2;
}
#endif
#ifdef CONFIG_MHL_SWING_LEVEL
if (sii8240_factory->pdata->swing_level == 0)
sii8240_factory->pdata->swing_level = 0x2D;
ret = class_create_file(sec_mhl, &class_attr_swing);
if (ret)
pr_err("failed to create swing sysfs file\n");
ret = class_create_file(sec_mhl, &class_attr_timing);
if (ret)
pr_err("failed to create timing sysfs file\n");
#endif
#ifdef SII8240_CHECK_MONITOR
ret = class_create_file(sec_mhl, &class_attr_hdcp_status);
if (ret)
pr_err("failed to create hdcp_status sysfs file\n");
#endif
ret = sii8240_register_input_device(sii8240);
if (ret) {
dev_err(&client->dev, "failed to register input device\n");
goto err_exit3;
}
#ifdef SFEATURE_UNSTABLE_SOURCE_WA
INIT_WORK(&sii8240->avi_check_work, sii8240_avi_check_work);
setup_timer(&sii8240->avi_check_timer, sii8240_avif_check_callback,
(unsigned long)sii8240);
#endif
/* mhl_event_switch is for home theater*/
sii8240->mhl_event_switch.name = "mhl_event_switch";
switch_dev_register(&sii8240->mhl_event_switch);
pr_info("***MHL probe is successful\n");
return 0;
err_exit3:
class_remove_file(sec_mhl, &class_attr_test_result);
err_exit2:
free_irq(client->irq,sii8240);
err_exit1:
mutex_destroy(&sii8240->lock);
mutex_destroy(&sii8240->cbus_lock);
mutex_destroy(&sii8240->msc_lock);
mutex_destroy(&sii8240->input_lock);
destroy_workqueue(sii8240->cbus_cmd_wqs);
destroy_workqueue(sii8240->avi_cmd_wqs);
err_exit0:
kfree(sii8240);
return ret;
}
static int __devinit sii8240_hdmi_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct sii8240_platform_data *pdata = client->dev.platform_data;
pdata->hdmi_client = client;
return 0;
}
static int __devinit sii8240_disc_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct sii8240_platform_data *pdata = client->dev.platform_data;
pdata->disc_client = client;
return 0;
}
static int __devinit sii8240_tpi_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct sii8240_platform_data *pdata = client->dev.platform_data;
pdata->tpi_client = client;
return 0;
}
static int __devinit sii8240_cbus_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct sii8240_platform_data *pdata = client->dev.platform_data;
pdata->cbus_client = client;
return 0;
}
#ifdef CONFIG_OF
static int __devinit of_sii8240_probe_dt(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
const struct i2c_device_id *id_table = client->driver->id_table;
struct sii8240_platform_data *pdata = NULL;
u32 client_id = -1;
if (!client->dev.of_node) {
dev_err(&client->dev, "sii8240: Client node not-found\n");
return -1;
}
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
return -EIO;
/* going to use block read/write, so check for this too */
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
return -EIO;
if (of_property_read_u32(client->dev.of_node,
"sii8240,client_id", &client_id))
dev_err(&client->dev, "Wrong Client_id# %d", client_id);
if (0 == client_id) {
pdata = platform_init_data(client);
client->dev.platform_data = pdata;
sii8240_tmds_i2c_probe(client, id_table);
} else if (g_sii8240) {
client->dev.platform_data = g_sii8240->pdata;
if (1 == client_id)
sii8240_hdmi_i2c_probe(client, id_table);
else if (2 == client_id)
sii8240_disc_i2c_probe(client, id_table);
else if (3 == client_id)
sii8240_tpi_i2c_probe(client, id_table);
else if (4 == client_id)
sii8240_cbus_i2c_probe(client, id_table);
}
#ifdef CONFIG_MUIC_NOTIFIER
queue_delayed_work(system_nrt_wq,
&g_sii8240->notifier_register_work, msecs_to_jiffies(20000));
#endif /* CONFIG_MUIC_NOTIFIER */
return 0;
}
#endif
static int __devexit sii8240_tmds_remove(struct i2c_client *client)
{
return 0;
}
static int __devexit sii8240_hdmi_remove(struct i2c_client *client)
{
return 0;
}
static int __devexit sii8240_disc_remove(struct i2c_client *client)
{
return 0;
}
static int __devexit sii8240_tpi_remove(struct i2c_client *client)
{
return 0;
}
static int __devexit sii8240_cbus_remove(struct i2c_client *client)
{
return 0;
}
static const struct i2c_device_id sii8240_tmds_id[] = {
{"sii8240_tmds", 0},
{}
};
static const struct i2c_device_id sii8240_hdmi_id[] = {
{"sii8240_hdmi", 0},
{}
};
static const struct i2c_device_id sii8240_disc_id[] = {
{"sii8240_disc", 0},
{}
};
static const struct i2c_device_id sii8240_tpi_id[] = {
{"sii8240_tpi", 0},
{}
};
static const struct i2c_device_id sii8240_cbus_id[] = {
{"sii8240_cbus", 0},
{}
};
MODULE_DEVICE_TABLE(i2c, sii8240_tmds_id);
MODULE_DEVICE_TABLE(i2c, sii8240_hdmi_id);
MODULE_DEVICE_TABLE(i2c, sii8240_disc_id);
MODULE_DEVICE_TABLE(i2c, sii8240_tpi_id);
MODULE_DEVICE_TABLE(i2c, sii8240_cbus_id);
#ifdef CONFIG_OF
static struct of_device_id sii8240_dt_ids[] = {
{ .compatible = "sii8240,tmds",},
{ .compatible = "sii8240,hdmi",},
{ .compatible = "sii8240,disc",},
{ .compatible = "sii8240,tpi",},
{ .compatible = "sii8240,cbus",},
{}
};
MODULE_DEVICE_TABLE(of, sii8240_dt_ids);
static const struct i2c_device_id sii8240_id[] = {
{"sii8240_mhlv2", 0},
{}
};
MODULE_DEVICE_TABLE(i2c, sii8240_id);
static struct i2c_driver sii8240_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "sii8240_mhlv2dt",
.of_match_table = of_match_ptr(sii8240_dt_ids),
},
.id_table = sii8240_id,
.probe = of_sii8240_probe_dt,
};
#else
static struct i2c_driver sii8240_tmds_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "sii8240_tmds",
},
.id_table = sii8240_tmds_id,
.probe = sii8240_tmds_i2c_probe,
.remove = __devexit_p(sii8240_tmds_remove),
};
static struct i2c_driver sii8240_hdmi_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "sii8240_hdmi",
},
.id_table = sii8240_hdmi_id,
.probe = sii8240_hdmi_i2c_probe,
.remove = __devexit_p(sii8240_hdmi_remove),
};
static struct i2c_driver sii8240_disc_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "sii8240_disc",
},
.id_table = sii8240_disc_id,
.probe = sii8240_disc_i2c_probe,
.remove = __devexit_p(sii8240_disc_remove),
};
static struct i2c_driver sii8240_tpi_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "sii8240_tpi",
},
.id_table = sii8240_tpi_id,
.probe = sii8240_tpi_i2c_probe,
.remove = __devexit_p(sii8240_tpi_remove),
};
static struct i2c_driver sii8240_cbus_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "sii8240_cbus",
},
.id_table = sii8240_cbus_id,
.probe = sii8240_cbus_i2c_probe,
.remove = __devexit_p(sii8240_cbus_remove),
};
#endif
static int __init sii8240_init(void)
{
int ret;
pr_info("[MHL] sii8240_init was called");
#ifdef CONFIG_OF
ret = i2c_add_driver(&sii8240_i2c_driver);
if (ret < 0) {
pr_err("[ERROR] sii8240: mhl_v2 i2c driver init failed");
return ret;
}
#else
ret = i2c_add_driver(&sii8240_tmds_i2c_driver);
if (ret < 0) {
pr_err("sii8240: tmds i2c driver init failed");
return ret;
}
ret = i2c_add_driver(&sii8240_hdmi_i2c_driver);
if (ret < 0) {
pr_err("sii8240: hdmi i2c driver init failed");
goto err_hdmi;
}
ret = i2c_add_driver(&sii8240_disc_i2c_driver);
if (ret < 0) {
pr_err("sii8240: disc i2c driver init failed");
goto err_disc;
}
ret = i2c_add_driver(&sii8240_tpi_i2c_driver);
if (ret < 0) {
pr_err("sii8240: tpi i2c driver init failed");
goto err_tpi;
}
ret = i2c_add_driver(&sii8240_cbus_i2c_driver);
if (ret < 0) {
pr_err("sii8240: cbus i2c driver init failed");
goto err_cbus;
}
#endif
return 0;
#ifndef CONFIG_OF
err_cbus:
i2c_del_driver(&sii8240_tpi_i2c_driver);
err_tpi:
i2c_del_driver(&sii8240_disc_i2c_driver);
err_disc:
i2c_del_driver(&sii8240_hdmi_i2c_driver);
err_hdmi:
i2c_del_driver(&sii8240_tmds_i2c_driver);
#endif
#ifdef CONFIG_OF
i2c_del_driver(&sii8240_i2c_driver);
#endif
return ret;
}
static void __exit sii8240_exit(void)
{
#ifdef CONFIG_OF
i2c_del_driver(&sii8240_i2c_driver);
#else
i2c_del_driver(&sii8240_cbus_i2c_driver);
i2c_del_driver(&sii8240_tpi_i2c_driver);
i2c_del_driver(&sii8240_disc_i2c_driver);
i2c_del_driver(&sii8240_hdmi_i2c_driver);
i2c_del_driver(&sii8240_tmds_i2c_driver);
#endif
}
module_init(sii8240_init);
module_exit(sii8240_exit);
MODULE_DESCRIPTION("Silicon Image MHL-8240 Transmitter driver");
MODULE_AUTHOR("Sangmi Park <sm0327.park@samsung.com>");
MODULE_LICENSE("GPL");
|