vip.c
98.9 KB
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
/*
* TI VIP capture driver
*
* Copyright (C) 2013 - 2014 Texas Instruments, Inc.
* David Griego, <dagriego@biglakesoftware.com>
* Dale Farnsworth, <dale@farnsworth.org>
* Nikhil Devshatwar, <nikhil.nd@ti.com>
* Benoit Parrot, <bparrot@ti.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/ioctl.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/mfd/syscon.h>
#include <linux/regmap.h>
#include <linux/pinctrl/consumer.h>
#include <linux/of_device.h>
#include <linux/of_graph.h>
#include <media/v4l2-of.h>
#include <media/v4l2-async.h>
#include "vip.h"
#define VIP_MODULE_NAME "vip"
#define VIP_INPUT_NAME "Vin0"
static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "debug level (0-8)");
/*
* Minimum and maximum frame sizes
*/
#define MIN_W 128
#define MIN_H 128
#define MAX_W 2048
#define MAX_H 1536
/*
* Required alignments
*/
#define S_ALIGN 0 /* multiple of 1 */
#define H_ALIGN 1 /* multiple of 2 */
#define W_ALIGN 1 /* multiple of 2 */
#define L_ALIGN 7 /* multiple of 128, line stride, 16 bytes */
/*
* Need a descriptor entry for each of up to 15 outputs,
* and up to 2 control transfers.
*/
#define VIP_DESC_LIST_SIZE (17 * sizeof(struct vpdma_dtd))
#define vip_dbg(level, dev, fmt, arg...) \
v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ##arg)
#define vip_err(dev, fmt, arg...) \
v4l2_err(&dev->v4l2_dev, fmt, ##arg)
#define vip_info(dev, fmt, arg...) \
v4l2_info(&dev->v4l2_dev, fmt, ##arg)
#define CTRL_CORE_SMA_SW_1 0x534
/*
* The srce_info structure contains per-srce data.
*/
struct vip_srce_info {
u8 base_channel; /* the VPDMA channel nummber */
u8 vb_index; /* input frame f, f-1, f-2 index */
u8 vb_part; /* identifies section of co-planar formats */
};
#define VIP_VPDMA_FIFO_SIZE 2
#define VIP_DROPQ_SIZE 3
/*
* Define indices into the srce_info tables
*/
#define VIP_SRCE_MULT_PORT 0
#define VIP_SRCE_MULT_ANC 1
#define VIP_SRCE_LUMA 2
#define VIP_SRCE_CHROMA 3
#define VIP_SRCE_RGB 4
static struct vip_srce_info srce_info[5] = {
[VIP_SRCE_MULT_PORT] = {
.base_channel = VIP1_CHAN_NUM_MULT_PORT_A_SRC0,
.vb_index = 0,
.vb_part = VIP_CHROMA,
},
[VIP_SRCE_MULT_ANC] = {
.base_channel = VIP1_CHAN_NUM_MULT_ANC_A_SRC0,
.vb_index = 0,
.vb_part = VIP_LUMA,
},
[VIP_SRCE_LUMA] = {
.base_channel = VIP1_CHAN_NUM_PORT_B_LUMA,
.vb_index = 1,
.vb_part = VIP_LUMA,
},
[VIP_SRCE_CHROMA] = {
.base_channel = VIP1_CHAN_NUM_PORT_B_CHROMA,
.vb_index = 1,
.vb_part = VIP_CHROMA,
},
[VIP_SRCE_RGB] = {
.base_channel = VIP1_CHAN_NUM_PORT_A_RGB,
.vb_part = VIP_LUMA,
},
};
static struct vip_fmt vip_formats[VIP_MAX_ACTIVE_FMT] = {
{
.fourcc = V4L2_PIX_FMT_NV12,
.code = MEDIA_BUS_FMT_UYVY8_2X8,
.colorspace = V4L2_COLORSPACE_SMPTE170M,
.coplanar = 1,
.vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y420],
&vpdma_yuv_fmts[VPDMA_DATA_FMT_C420],
},
},
{
.fourcc = V4L2_PIX_FMT_UYVY,
.code = MEDIA_BUS_FMT_UYVY8_2X8,
.colorspace = V4L2_COLORSPACE_SMPTE170M,
.coplanar = 0,
.vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CBY422],
},
},
{
.fourcc = V4L2_PIX_FMT_YUYV,
.code = MEDIA_BUS_FMT_UYVY8_2X8,
.colorspace = V4L2_COLORSPACE_SMPTE170M,
.coplanar = 0,
.vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YCB422],
},
},
{
.fourcc = V4L2_PIX_FMT_VYUY,
.code = MEDIA_BUS_FMT_UYVY8_2X8,
.colorspace = V4L2_COLORSPACE_SMPTE170M,
.coplanar = 0,
.vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CRY422],
},
},
{
.fourcc = V4L2_PIX_FMT_YVYU,
.code = MEDIA_BUS_FMT_UYVY8_2X8,
.colorspace = V4L2_COLORSPACE_SMPTE170M,
.coplanar = 0,
.vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YCR422],
},
},
{
.fourcc = V4L2_PIX_FMT_RGB24,
.code = MEDIA_BUS_FMT_UYVY8_2X8,
.colorspace = V4L2_COLORSPACE_SRGB,
.coplanar = 0,
.vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
},
},
{
.fourcc = V4L2_PIX_FMT_RGB32,
.code = MEDIA_BUS_FMT_UYVY8_2X8,
.colorspace = V4L2_COLORSPACE_SRGB,
.coplanar = 0,
.vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
},
},
{
.fourcc = V4L2_PIX_FMT_BGR24,
.code = MEDIA_BUS_FMT_UYVY8_2X8,
.colorspace = V4L2_COLORSPACE_SRGB,
.coplanar = 0,
.vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_BGR24],
},
},
{
.fourcc = V4L2_PIX_FMT_BGR32,
.code = MEDIA_BUS_FMT_UYVY8_2X8,
.colorspace = V4L2_COLORSPACE_SRGB,
.coplanar = 0,
.vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ABGR32],
},
},
{
.fourcc = V4L2_PIX_FMT_RGB24,
.code = MEDIA_BUS_FMT_RGB888_1X24,
.colorspace = V4L2_COLORSPACE_SRGB,
.coplanar = 0,
.vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
},
},
{
.fourcc = V4L2_PIX_FMT_RGB32,
.code = MEDIA_BUS_FMT_ARGB8888_1X32,
.colorspace = V4L2_COLORSPACE_SRGB,
.coplanar = 0,
.vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
},
},
{
.fourcc = V4L2_PIX_FMT_SBGGR8,
.code = MEDIA_BUS_FMT_SBGGR8_1X8,
.colorspace = V4L2_COLORSPACE_SMPTE170M,
.coplanar = 0,
.vpdma_fmt = { &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8],
},
},
{
.fourcc = V4L2_PIX_FMT_SGBRG8,
.code = MEDIA_BUS_FMT_SGBRG8_1X8,
.colorspace = V4L2_COLORSPACE_SMPTE170M,
.coplanar = 0,
.vpdma_fmt = { &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8],
},
},
{
.fourcc = V4L2_PIX_FMT_SGRBG8,
.code = MEDIA_BUS_FMT_SGRBG8_1X8,
.colorspace = V4L2_COLORSPACE_SMPTE170M,
.coplanar = 0,
.vpdma_fmt = { &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8],
},
},
{
.fourcc = V4L2_PIX_FMT_SRGGB8,
.code = MEDIA_BUS_FMT_SRGGB8_1X8,
.colorspace = V4L2_COLORSPACE_SMPTE170M,
.coplanar = 0,
.vpdma_fmt = { &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8],
},
},
};
/* Print Four-character-code (FOURCC) */
static char *fourcc_to_str(u32 fmt)
{
static char code[5];
code[0] = (unsigned char)(fmt & 0xff);
code[1] = (unsigned char)((fmt >> 8) & 0xff);
code[2] = (unsigned char)((fmt >> 16) & 0xff);
code[3] = (unsigned char)((fmt >> 24) & 0xff);
code[4] = '\0';
return code;
}
/*
* Find our format description corresponding to the passed v4l2_format
*/
static struct vip_fmt *find_port_format_by_pix(struct vip_port *port,
u32 pixelformat)
{
struct vip_fmt *fmt;
unsigned int k;
for (k = 0; k < port->num_active_fmt; k++) {
fmt = port->active_fmt[k];
if (fmt->fourcc == pixelformat)
return fmt;
}
return NULL;
}
static struct vip_fmt *find_port_format_by_code(struct vip_port *port,
u32 code)
{
struct vip_fmt *fmt;
unsigned int k;
for (k = 0; k < port->num_active_fmt; k++) {
fmt = port->active_fmt[k];
if (fmt->code == code)
return fmt;
}
return NULL;
}
inline struct vip_port *notifier_to_vip_port(struct v4l2_async_notifier *n)
{
return container_of(n, struct vip_port, notifier);
}
static bool __maybe_unused vip_is_fmt_yuv(u32 fourcc)
{
if (fourcc == V4L2_PIX_FMT_NV12 ||
fourcc == V4L2_PIX_FMT_UYVY ||
fourcc == V4L2_PIX_FMT_YUYV ||
fourcc == V4L2_PIX_FMT_VYUY ||
fourcc == V4L2_PIX_FMT_YVYU)
return true;
return false;
}
static bool vip_is_fmt_rgb(u32 fourcc)
{
if (fourcc == V4L2_PIX_FMT_RGB24 ||
fourcc == V4L2_PIX_FMT_BGR24 ||
fourcc == V4L2_PIX_FMT_RGB32 ||
fourcc == V4L2_PIX_FMT_BGR32)
return true;
return false;
}
static bool __maybe_unused vip_is_mbuscode_yuv(u32 code)
{
return ((code & 0xFF00) == 0x2000);
}
static bool vip_is_mbuscode_rgb(u32 code)
{
return ((code & 0xFF00) == 0x1000);
}
static enum v4l2_colorspace vip_fourcc_to_colorspace(u32 fourcc)
{
if (vip_is_fmt_rgb(fourcc))
return V4L2_COLORSPACE_SRGB;
return V4L2_COLORSPACE_SMPTE170M;
}
static enum v4l2_colorspace vip_code_to_colorspace(u32 code)
{
if (vip_is_mbuscode_rgb(code))
return V4L2_COLORSPACE_SRGB;
return V4L2_COLORSPACE_SMPTE170M;
}
static enum vip_csc_state vip_csc_direction(u32 src_code, u32 dst_fourcc)
{
if (vip_is_mbuscode_yuv(src_code) && vip_is_fmt_rgb(dst_fourcc))
return VIP_CSC_Y2R;
else if (vip_is_mbuscode_rgb(src_code) && vip_is_fmt_yuv(dst_fourcc))
return VIP_CSC_R2Y;
else
return VIP_CSC_NA;
}
/*
* port flag bits
*/
#define FLAG_FRAME_1D BIT(0)
#define FLAG_EVEN_LINE_SKIP BIT(1)
#define FLAG_ODD_LINE_SKIP BIT(2)
#define FLAG_MODE_TILED BIT(3)
#define FLAG_INTERLACED BIT(4)
#define FLAG_MULTIPLEXED BIT(5)
#define FLAG_MULT_PORT BIT(6)
#define FLAG_MULT_ANC BIT(7)
/*
* Function prototype declarations
*/
static int alloc_port(struct vip_dev *, int);
static void free_port(struct vip_port *);
static int vip_setup_parser(struct vip_port *port);
static int vip_setup_scaler(struct vip_stream *stream);
static void vip_enable_parser(struct vip_port *port, bool on);
static void vip_reset_parser(struct vip_port *port, bool on);
static void vip_parser_stop_imm(struct vip_port *port, bool on);
static void stop_dma(struct vip_stream *stream, bool clear_list);
static int vip_load_vpdma_list_fifo(struct vip_stream *stream);
static inline bool is_scaler_available(struct vip_port *port);
static inline bool allocate_scaler(struct vip_port *port);
static inline void free_scaler(struct vip_port *port);
static bool is_csc_available(struct vip_port *port);
static bool allocate_csc(struct vip_port *port,
enum vip_csc_state csc_direction);
static void free_csc(struct vip_port *port);
#define reg_read(dev, offset) ioread32(dev->base + offset)
#define reg_write(dev, offset, val) iowrite32(val, dev->base + offset)
/*
* Insert a masked field into a 32-bit field
*/
static void insert_field(u32 *valp, u32 field, u32 mask, int shift)
{
u32 val = *valp;
val &= ~(mask << shift);
val |= (field & mask) << shift;
*valp = val;
}
/*
* DMA address/data block for the shadow registers
*/
struct vip_mmr_adb {
struct vpdma_adb_hdr sc_hdr0;
u32 sc_regs0[7];
u32 sc_pad0[1];
struct vpdma_adb_hdr sc_hdr8;
u32 sc_regs8[6];
u32 sc_pad8[2];
struct vpdma_adb_hdr sc_hdr17;
u32 sc_regs17[9];
u32 sc_pad17[3];
struct vpdma_adb_hdr csc_hdr;
u32 csc_regs[6];
u32 csc_pad[2];
};
#define GET_OFFSET_TOP(port, obj, reg) \
((obj)->res->start - port->dev->res->start + reg)
#define VIP_SET_MMR_ADB_HDR(port, hdr, regs, offset_a) \
VPDMA_SET_MMR_ADB_HDR(port->mmr_adb, vip_mmr_adb, hdr, regs, offset_a)
/*
* Set the headers for all of the address/data block structures.
*/
static void init_adb_hdrs(struct vip_port *port)
{
VIP_SET_MMR_ADB_HDR(port, sc_hdr0, sc_regs0,
GET_OFFSET_TOP(port, port->dev->sc, CFG_SC0));
VIP_SET_MMR_ADB_HDR(port, sc_hdr8, sc_regs8,
GET_OFFSET_TOP(port, port->dev->sc, CFG_SC8));
VIP_SET_MMR_ADB_HDR(port, sc_hdr17, sc_regs17,
GET_OFFSET_TOP(port, port->dev->sc, CFG_SC17));
VIP_SET_MMR_ADB_HDR(port, csc_hdr, csc_regs,
GET_OFFSET_TOP(port, port->dev->csc, CSC_CSC00));
};
/*
* These represent the module resets bit for slice 1
* Upon detecting slice2 we simply left shift by 1
*/
#define VIP_DP_RST BIT(16)
#define VIP_PARSER_RST BIT(18)
#define VIP_CSC_RST BIT(20)
#define VIP_SC_RST BIT(22)
#define VIP_DS0_RST BIT(25)
#define VIP_DS1_RST BIT(27)
static void vip_module_reset(struct vip_dev *dev, uint32_t module, bool on)
{
u32 val = 0;
val = reg_read(dev, VIP_CLK_RESET);
if (dev->slice_id == VIP_SLICE2)
module <<= 1;
if (on)
val |= module;
else
val &= ~module;
reg_write(dev, VIP_CLK_RESET, val);
}
/*
* Enable or disable the VIP clocks
*/
static void vip_set_clock_enable(struct vip_dev *dev, bool on)
{
u32 val = 0;
val = reg_read(dev, VIP_CLK_ENABLE);
if (on) {
val |= VIP_VPDMA_CLK_ENABLE;
if (dev->slice_id == VIP_SLICE1)
val |= VIP_VIP1_DATA_PATH_CLK_ENABLE;
else
val |= VIP_VIP2_DATA_PATH_CLK_ENABLE;
} else {
if (dev->slice_id == VIP_SLICE1)
val &= ~VIP_VIP1_DATA_PATH_CLK_ENABLE;
else
val &= ~VIP_VIP2_DATA_PATH_CLK_ENABLE;
/* Both VIP are disabled then shutdown VPDMA also */
if (!(val & (VIP_VIP1_DATA_PATH_CLK_ENABLE |
VIP_VIP2_DATA_PATH_CLK_ENABLE)))
val = 0;
}
reg_write(dev, VIP_CLK_ENABLE, val);
}
/* This helper function is used to enable the clock early on to
* enable vpdma firmware loading before the slice device are created
*/
static void vip_shared_set_clock_enable(struct vip_shared *shared, bool on)
{
u32 val = 0;
if (on)
val = VIP_VIP1_DATA_PATH_CLK_ENABLE | VIP_VPDMA_CLK_ENABLE;
reg_write(shared, VIP_CLK_ENABLE, val);
}
static void vip_top_reset(struct vip_dev *dev)
{
u32 val = 0;
val = reg_read(dev, VIP_CLK_RESET);
if (dev->slice_id == VIP_SLICE1)
insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
VIP_VIP1_DATA_PATH_RESET_SHIFT);
else
insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
VIP_VIP2_DATA_PATH_RESET_SHIFT);
reg_write(dev, VIP_CLK_RESET, val);
usleep_range(200, 250);
val = reg_read(dev, VIP_CLK_RESET);
if (dev->slice_id == VIP_SLICE1)
insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
VIP_VIP1_DATA_PATH_RESET_SHIFT);
else
insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
VIP_VIP2_DATA_PATH_RESET_SHIFT);
reg_write(dev, VIP_CLK_RESET, val);
}
static void vip_top_vpdma_reset(struct vip_shared *shared)
{
u32 val;
val = reg_read(shared, VIP_CLK_RESET);
insert_field(&val, 1, VIP_VPDMA_CLK_RESET_MASK,
VIP_VPDMA_CLK_RESET_SHIFT);
reg_write(shared, VIP_CLK_RESET, val);
usleep_range(200, 250);
val = reg_read(shared, VIP_CLK_RESET);
insert_field(&val, 0, VIP_VPDMA_CLK_RESET_MASK,
VIP_VPDMA_CLK_RESET_SHIFT);
reg_write(shared, VIP_CLK_RESET, val);
}
static void vip_set_pclk_invert(struct vip_port *port)
{
u32 offset;
/*
* When the VIP parser is configured to so that the pixel clock
* is to be sampled at falling edge, the pixel clock needs to be
* inverted before it is given to the VIP module. This is done
* by setting a bit in the CTRL_CORE_SMA_SW1 register.
*/
if (port->dev->instance_id == VIP_INSTANCE1) {
offset = 0 + 2 * port->port_id + port->dev->slice_id;
} else if (port->dev->instance_id == VIP_INSTANCE2) {
offset = 4 + 2 * port->port_id + port->dev->slice_id;
} else if (port->dev->instance_id == VIP_INSTANCE3) {
offset = 10 - port->dev->slice_id;
} else {
vip_err(port->dev, "%s: VIP instance id out of range...\n",
__func__);
return;
}
if (port->dev->syscon_pol)
regmap_update_bits(port->dev->syscon_pol,
port->dev->syscon_pol_offset,
1 << offset, 1 << offset);
}
#define VIP_PARSER_PORT(p) (VIP_PARSER_PORTA_0 + (p * 0x8U))
#define VIP_PARSER_EXTRA_PORT(p) (VIP_PARSER_PORTA_1 + (p * 0x8U))
#define VIP_PARSER_CROP_H_PORT(p) (VIP_PARSER_PORTA_EXTRA4 + (p * 0x10U))
#define VIP_PARSER_CROP_V_PORT(p) (VIP_PARSER_PORTA_EXTRA5 + (p * 0x10U))
#define VIP_PARSER_STOP_IMM_PORT(p) (VIP_PARSER_PORTA_EXTRA6 + (p * 0x4U))
static void vip_set_data_interface(struct vip_port *port,
enum data_interface_modes mode)
{
u32 val = 0;
insert_field(&val, mode, VIP_DATA_INTERFACE_MODE_MASK,
VIP_DATA_INTERFACE_MODE_SHFT);
reg_write(port->dev->parser, VIP_PARSER_MAIN_CFG, val);
}
static void vip_set_slice_path(struct vip_dev *dev,
enum data_path_select data_path, u32 path_val)
{
u32 val = 0;
int data_path_reg;
vip_dbg(3, dev, "%s:\n", __func__);
data_path_reg = VIP_VIP1_DATA_PATH_SELECT + 4 * dev->slice_id;
switch (data_path) {
case ALL_FIELDS_DATA_SELECT:
val |= path_val;
break;
case VIP_CSC_SRC_DATA_SELECT:
insert_field(&val, path_val, VIP_CSC_SRC_SELECT_MASK,
VIP_CSC_SRC_SELECT_SHFT);
break;
case VIP_SC_SRC_DATA_SELECT:
insert_field(&val, path_val, VIP_SC_SRC_SELECT_MASK,
VIP_SC_SRC_SELECT_SHFT);
break;
case VIP_RGB_SRC_DATA_SELECT:
val |= (path_val) ? VIP_RGB_SRC_SELECT : 0;
break;
case VIP_RGB_OUT_LO_DATA_SELECT:
val |= (path_val) ? VIP_RGB_OUT_LO_SRC_SELECT : 0;
break;
case VIP_RGB_OUT_HI_DATA_SELECT:
val |= (path_val) ? VIP_RGB_OUT_HI_SRC_SELECT : 0;
break;
case VIP_CHR_DS_1_SRC_DATA_SELECT:
insert_field(&val, path_val, VIP_DS1_SRC_SELECT_MASK,
VIP_DS1_SRC_SELECT_SHFT);
break;
case VIP_CHR_DS_2_SRC_DATA_SELECT:
insert_field(&val, path_val, VIP_DS2_SRC_SELECT_MASK,
VIP_DS2_SRC_SELECT_SHFT);
break;
case VIP_MULTI_CHANNEL_DATA_SELECT:
val |= (path_val) ? VIP_MULTI_CHANNEL_SELECT : 0;
break;
case VIP_CHR_DS_1_DATA_BYPASS:
val |= (path_val) ? VIP_DS1_BYPASS : 0;
break;
case VIP_CHR_DS_2_DATA_BYPASS:
val |= (path_val) ? VIP_DS2_BYPASS : 0;
break;
default:
vip_err(dev, "%s: data_path 0x%x is not valid\n",
__func__, data_path);
return;
}
insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
VIP_DATAPATH_SELECT_SHFT);
reg_write(dev, data_path_reg, val);
vip_dbg(3, dev, "%s: DATA_PATH_SELECT(%08X): %08X\n", __func__,
data_path_reg, reg_read(dev, data_path_reg));
}
/*
* Return the vip_stream structure for a given struct file
*/
static inline struct vip_stream *file2stream(struct file *file)
{
return video_drvdata(file);
}
/*
* Append a destination descriptor to the current descriptor list,
* setting up dma to the given srce.
*/
static int add_out_dtd(struct vip_stream *stream, int srce_type)
{
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
struct vip_srce_info *sinfo = &srce_info[srce_type];
struct v4l2_rect *c_rect = &port->c_rect;
struct vip_fmt *fmt = port->fmt;
int channel, plane = 0;
int max_width, max_height;
dma_addr_t dma_addr;
u32 flags;
u32 width = stream->width;
channel = sinfo->base_channel;
switch (srce_type) {
case VIP_SRCE_MULT_PORT:
case VIP_SRCE_MULT_ANC:
if (port->port_id == VIP_PORTB)
channel += VIP_CHAN_MULT_PORTB_OFFSET;
channel += stream->stream_id;
flags = 0;
break;
case VIP_SRCE_CHROMA:
plane = 1;
case VIP_SRCE_LUMA:
if (port->port_id == VIP_PORTB) {
if (port->scaler && !port->fmt->coplanar)
/*
* In this case Port A Chroma channel
* is used to carry Port B scaled YUV422
*/
channel += 1;
else
channel += VIP_CHAN_YUV_PORTB_OFFSET;
}
flags = port->flags;
break;
case VIP_SRCE_RGB:
if ((port->port_id == VIP_PORTB) ||
((port->port_id == VIP_PORTA) &&
(port->csc == VIP_CSC_NA) &&
vip_is_fmt_rgb(port->fmt->fourcc)))
/*
* RGB sensor only connect to Y_LO
* channel i.e. port B channel.
*/
channel += VIP_CHAN_RGB_PORTB_OFFSET;
flags = port->flags;
break;
default:
vip_err(dev, "%s: srce_type 0x%x is not valid\n",
__func__, srce_type);
return -1;
}
if (dev->slice_id == VIP_SLICE2)
channel += VIP_CHAN_VIP2_OFFSET;
/* This is just for initialization purposes.
* The actual dma_addr will be configured in vpdma_update_dma_addr
*/
dma_addr = 0;
if (port->fmt->vpdma_fmt[0] == &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8]) {
/*
* Special case since we are faking a YUV422 16bit format
* to have the vpdma perform the needed byte swap
* we need to adjust the pixel width accordingly
* otherwise the parser will attempt to collect more pixels
* then available and the vpdma transfer will exceed the
* allocated frame buffer.
*/
width >>= 1;
vip_dbg(1, dev, "%s: 8 bit raw detected, adjusting width to %d\n",
__func__, width);
}
/*
* Use VPDMA_MAX_SIZE1 or VPDMA_MAX_SIZE2 register for slice0/1
*/
if (dev->slice_id == VIP_SLICE1) {
vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE1,
width, stream->height);
max_width = MAX_OUT_WIDTH_REG1;
max_height = MAX_OUT_HEIGHT_REG1;
} else {
vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE2,
width, stream->height);
max_width = MAX_OUT_WIDTH_REG2;
max_height = MAX_OUT_HEIGHT_REG2;
}
/*
* Mark this channel to be cleared while cleaning up resources
* This will make sure that an abort descriptor for this channel
* would be submitted to VPDMA causing any ongoing transaction to be
* aborted and cleanup the VPDMA FSM for this channel
*/
stream->vpdma_channels[channel] = 1;
vpdma_rawchan_add_out_dtd(&stream->desc_list, c_rect->width, c_rect,
fmt->vpdma_fmt[plane], dma_addr,
max_width, max_height, channel, flags);
return 0;
}
/*
* add_stream_dtds - prepares and starts DMA for pending transfers
*/
static void add_stream_dtds(struct vip_stream *stream)
{
struct vip_port *port = stream->port;
int srce_type;
if (port->flags & FLAG_MULT_PORT)
srce_type = VIP_SRCE_MULT_PORT;
else if (port->flags & FLAG_MULT_ANC)
srce_type = VIP_SRCE_MULT_ANC;
else if (vip_is_fmt_rgb(port->fmt->fourcc))
srce_type = VIP_SRCE_RGB;
else
srce_type = VIP_SRCE_LUMA;
add_out_dtd(stream, srce_type);
if (srce_type == VIP_SRCE_LUMA && port->fmt->coplanar)
add_out_dtd(stream, VIP_SRCE_CHROMA);
}
#define PARSER_IRQ_MASK (VIP_PORTA_OUTPUT_FIFO_YUV | \
VIP_PORTB_OUTPUT_FIFO_YUV)
static void enable_irqs(struct vip_dev *dev, int irq_num, int list_num)
{
struct vip_parser_data *parser = dev->parser;
u32 reg_addr = VIP_INT0_ENABLE0_SET +
VIP_INTC_INTX_OFFSET * irq_num;
u32 irq_val = (1 << (list_num * 2)) |
(VIP_VIP1_PARSER_INT << (irq_num * 1));
/* Enable Parser Interrupt */
reg_write(parser, VIP_PARSER_FIQ_MASK, ~PARSER_IRQ_MASK);
reg_write(dev->shared, reg_addr, irq_val);
vpdma_enable_list_complete_irq(dev->shared->vpdma,
irq_num, list_num, true);
}
static void disable_irqs(struct vip_dev *dev, int irq_num, int list_num)
{
struct vip_parser_data *parser = dev->parser;
u32 reg_addr = VIP_INT0_ENABLE0_CLR +
VIP_INTC_INTX_OFFSET * irq_num;
u32 irq_val = (1 << (list_num * 2)) |
(VIP_VIP1_PARSER_INT << (irq_num * 1));
/* Disable all Parser Interrupt */
reg_write(parser, VIP_PARSER_FIQ_MASK, 0xffffffff);
reg_write(dev->shared, reg_addr, irq_val);
vpdma_enable_list_complete_irq(dev->shared->vpdma,
irq_num, list_num, false);
}
static void clear_irqs(struct vip_dev *dev, int irq_num, int list_num)
{
struct vip_parser_data *parser = dev->parser;
u32 reg_addr = VIP_INT0_STATUS0_CLR +
VIP_INTC_INTX_OFFSET * irq_num;
u32 irq_val = (1 << (list_num * 2)) |
(VIP_VIP1_PARSER_INT << (irq_num * 1));
/* Clear all Parser Interrupt */
reg_write(parser, VIP_PARSER_FIQ_CLR, 0xffffffff);
reg_write(parser, VIP_PARSER_FIQ_CLR, 0x0);
reg_write(dev->shared, reg_addr, irq_val);
vpdma_clear_list_stat(dev->shared->vpdma, irq_num, dev->slice_id);
}
static void populate_desc_list(struct vip_stream *stream)
{
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
unsigned int list_length;
stream->desc_next = stream->desc_list.buf.addr;
add_stream_dtds(stream);
list_length = stream->desc_next - stream->desc_list.buf.addr;
vpdma_map_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
}
/*
* start_dma - adds descriptors to the dma list and submits them.
* Should be called after a new vb is queued and on a vpdma list
* completion interrupt.
*/
static void start_dma(struct vip_stream *stream, struct vip_buffer *buf)
{
struct vip_dev *dev = stream->port->dev;
struct vpdma_data *vpdma = dev->shared->vpdma;
int list_num = stream->list_num;
dma_addr_t dma_addr;
int drop_data;
if (vpdma_list_busy(vpdma, list_num)) {
vip_err(dev, "vpdma list busy, cannot post\n");
return; /* nothing to do */
}
if (buf) {
dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
drop_data = 0;
vip_dbg(4, dev, "start_dma: vb2 buf idx:%d, dma_addr:0x%08x\n",
buf->vb.vb2_buf.index, dma_addr);
} else {
dma_addr = 0;
drop_data = 1;
vip_dbg(4, dev, "start_dma: dropped\n");
}
vpdma_update_dma_addr(dev->shared->vpdma, &stream->desc_list,
dma_addr, stream->write_desc, drop_data, 0);
if (stream->port->fmt->coplanar) {
dma_addr += stream->width * stream->height;
vpdma_update_dma_addr(dev->shared->vpdma, &stream->desc_list,
dma_addr, stream->write_desc + 1,
drop_data, 1);
}
vpdma_submit_descs(dev->shared->vpdma,
&stream->desc_list, stream->list_num);
}
static void vip_schedule_next_buffer(struct vip_stream *stream)
{
struct vip_dev *dev = stream->port->dev;
struct vip_buffer *buf;
unsigned long flags;
spin_lock_irqsave(&dev->slock, flags);
if (list_empty(&stream->vidq)) {
vip_dbg(4, dev, "Dropping frame\n");
if (list_empty(&stream->dropq)) {
vip_err(dev, "No dropq buffer left!");
spin_unlock_irqrestore(&dev->slock, flags);
return;
}
buf = list_entry(stream->dropq.next,
struct vip_buffer, list);
buf->drop = true;
list_move_tail(&buf->list, &stream->post_bufs);
buf = NULL;
} else {
buf = list_entry(stream->vidq.next,
struct vip_buffer, list);
buf->drop = false;
list_move_tail(&buf->list, &stream->post_bufs);
vip_dbg(4, dev, "added next buffer\n");
}
spin_unlock_irqrestore(&dev->slock, flags);
start_dma(stream, buf);
}
static void vip_process_buffer_complete(struct vip_stream *stream)
{
struct vip_dev *dev = stream->port->dev;
struct vip_buffer *buf;
struct vb2_v4l2_buffer *vb = NULL;
unsigned long flags, fld;
buf = list_first_entry(&stream->post_bufs, struct vip_buffer, list);
if (stream->port->flags & FLAG_INTERLACED) {
vpdma_unmap_desc_buf(dev->shared->vpdma,
&stream->desc_list.buf);
fld = dtd_get_field(stream->write_desc);
stream->field = fld ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
vpdma_map_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
}
if (buf) {
vip_dbg(4, dev, "vip buffer complete 0x%x, 0x%x\n",
(unsigned int)buf, buf->drop);
vb = &buf->vb;
vb->field = stream->field;
vb->sequence = stream->sequence;
v4l2_get_timestamp(&vb->timestamp);
if (buf->drop) {
spin_lock_irqsave(&dev->slock, flags);
list_move_tail(&buf->list, &stream->dropq);
spin_unlock_irqrestore(&dev->slock, flags);
} else {
spin_lock_irqsave(&dev->slock, flags);
list_del(&buf->list);
spin_unlock_irqrestore(&dev->slock, flags);
vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
}
} else {
vip_err(dev, "%s: buf is null!!!\n", __func__);
return;
}
stream->sequence++;
}
static int vip_reset_vpdma(struct vip_stream *stream)
{
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
struct vip_buffer *buf;
unsigned long flags;
stop_dma(stream, false);
spin_lock_irqsave(&dev->slock, flags);
/* requeue all active buffers in the opposite order */
while (!list_empty(&stream->post_bufs)) {
buf = list_last_entry(&stream->post_bufs,
struct vip_buffer, list);
list_del(&buf->list);
if (buf->drop == 1) {
list_add_tail(&buf->list, &stream->dropq);
vip_dbg(4, dev, "requeueing drop buffer on dropq\n");
} else {
list_add(&buf->list, &stream->vidq);
vip_dbg(4, dev, "requeueing vb2 buf idx:%d on vidq\n",
buf->vb.vb2_buf.index);
}
}
spin_unlock_irqrestore(&dev->slock, flags);
/* Make sure the desc_list is unmapped */
vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
return 0;
}
static void vip_overflow_recovery_work(struct work_struct *work)
{
struct vip_stream *stream = container_of(work, struct vip_stream,
recovery_work);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
vip_err(dev, "%s: Port %c\n", __func__,
port->port_id == VIP_PORTA ? 'A' : 'B');
disable_irqs(dev, dev->slice_id, stream->list_num);
clear_irqs(dev, dev->slice_id, stream->list_num);
/* 1. Set VIP_XTRA6_PORT_A[31:16] YUV_SRCNUM_STOP_IMMEDIATELY */
/* 2. Set VIP_XTRA6_PORT_A[15:0] ANC_SRCNUM_STOP_IMMEDIATELY */
vip_parser_stop_imm(port, 1);
/* 3. Clear VIP_PORT_A[8] ENABLE */
/*
* 4. Set VIP_PORT_A[7] CLR_ASYNC_FIFO_RD
* Set VIP_PORT_A[6] CLR_ASYNC_FIFO_WR
*/
vip_enable_parser(port, false);
/* 5. Set VIP_PORT_A[23] SW_RESET */
vip_reset_parser(port, 1);
/*
* 6. Reset other VIP modules
* For each module used downstream of VIP_PARSER, write 1 to the
* bit location of the VIP_CLKC_RST register which is connected
* to VIP_PARSER
*/
vip_module_reset(dev, VIP_DP_RST, true);
usleep_range(200, 250);
/*
* 7. Abort VPDMA channels
* Write to list attribute to stop list 0
* Write to list address register location of abort list
* Write to list attribute register list 0 and size of abort list
*/
vip_reset_vpdma(stream);
/* 8. Clear VIP_PORT_A[23] SW_RESET */
vip_reset_parser(port, 0);
/*
* 9. Un-reset other VIP modules
* For each module used downstream of VIP_PARSER, write 0 to
* the bit location of the VIP_CLKC_RST register which is
* connected to VIP_PARSER
*/
vip_module_reset(dev, VIP_DP_RST, false);
/* 10. (Delay) */
/* 11. SC coeff downloaded (if VIP_SCALER is being used) */
vip_setup_scaler(stream);
/* 12. (Delay) */
/* the above are not needed here yet */
populate_desc_list(stream);
stream->num_recovery++;
if (stream->num_recovery < 5) {
/* Reload the vpdma */
vip_load_vpdma_list_fifo(stream);
enable_irqs(dev, dev->slice_id, stream->list_num);
vip_schedule_next_buffer(stream);
/* 13. Clear VIP_XTRA6_PORT_A[31:16] YUV_SRCNUM_STOP_IMM */
/* 14. Clear VIP_XTRA6_PORT_A[15:0] ANC_SRCNUM_STOP_IMM */
vip_parser_stop_imm(port, 0);
/* 15. Set VIP_PORT_A[8] ENABLE */
/*
* 16. Clear VIP_PORT_A[7] CLR_ASYNC_FIFO_RD
* Clear VIP_PORT_A[6] CLR_ASYNC_FIFO_WR
*/
vip_enable_parser(port, true);
} else {
vip_err(dev, "%s: num_recovery limit exceeded leaving disabled\n",
__func__);
}
}
static void handle_parser_irqs(struct vip_dev *dev)
{
struct vip_parser_data *parser = dev->parser;
struct vip_port *porta = dev->ports[VIP_PORTA];
struct vip_port *portb = dev->ports[VIP_PORTB];
struct vip_stream *stream = NULL;
u32 irq_stat = reg_read(parser, VIP_PARSER_FIQ_STATUS);
int i;
vip_dbg(3, dev, "%s: FIQ_STATUS: 0x%08x\n", __func__, irq_stat);
/* Clear all Parser Interrupt */
reg_write(parser, VIP_PARSER_FIQ_CLR, irq_stat);
reg_write(parser, VIP_PARSER_FIQ_CLR, 0x0);
if (irq_stat & VIP_PORTA_VDET)
vip_dbg(3, dev, "VIP_PORTA_VDET\n");
if (irq_stat & VIP_PORTB_VDET)
vip_dbg(3, dev, "VIP_PORTB_VDET\n");
if (irq_stat & VIP_PORTA_ASYNC_FIFO_OF)
vip_err(dev, "VIP_PORTA_ASYNC_FIFO_OF\n");
if (irq_stat & VIP_PORTB_ASYNC_FIFO_OF)
vip_err(dev, "VIP_PORTB_ASYNC_FIFO_OF\n");
if (irq_stat & VIP_PORTA_OUTPUT_FIFO_YUV)
vip_err(dev, "VIP_PORTA_OUTPUT_FIFO_YUV\n");
if (irq_stat & VIP_PORTA_OUTPUT_FIFO_ANC)
vip_err(dev, "VIP_PORTA_OUTPUT_FIFO_ANC\n");
if (irq_stat & VIP_PORTB_OUTPUT_FIFO_YUV)
vip_err(dev, "VIP_PORTB_OUTPUT_FIFO_YUV\n");
if (irq_stat & VIP_PORTB_OUTPUT_FIFO_ANC)
vip_err(dev, "VIP_PORTB_OUTPUT_FIFO_ANC\n");
if (irq_stat & VIP_PORTA_CONN)
vip_dbg(3, dev, "VIP_PORTA_CONN\n");
if (irq_stat & VIP_PORTA_DISCONN)
vip_dbg(3, dev, "VIP_PORTA_DISCONN\n");
if (irq_stat & VIP_PORTB_CONN)
vip_dbg(3, dev, "VIP_PORTB_CONN\n");
if (irq_stat & VIP_PORTB_DISCONN)
vip_dbg(3, dev, "VIP_PORTB_DISCONN\n");
if (irq_stat & VIP_PORTA_SRC0_SIZE)
vip_dbg(3, dev, "VIP_PORTA_SRC0_SIZE\n");
if (irq_stat & VIP_PORTB_SRC0_SIZE)
vip_dbg(3, dev, "VIP_PORTB_SRC0_SIZE\n");
if (irq_stat & VIP_PORTA_YUV_PROTO_VIOLATION)
vip_dbg(3, dev, "VIP_PORTA_YUV_PROTO_VIOLATION\n");
if (irq_stat & VIP_PORTA_ANC_PROTO_VIOLATION)
vip_dbg(3, dev, "VIP_PORTA_ANC_PROTO_VIOLATION\n");
if (irq_stat & VIP_PORTB_YUV_PROTO_VIOLATION)
vip_dbg(3, dev, "VIP_PORTB_YUV_PROTO_VIOLATION\n");
if (irq_stat & VIP_PORTB_ANC_PROTO_VIOLATION)
vip_dbg(3, dev, "VIP_PORTB_ANC_PROTO_VIOLATION\n");
if (irq_stat & VIP_PORTA_CFG_DISABLE_COMPLETE)
vip_dbg(3, dev, "VIP_PORTA_CFG_DISABLE_COMPLETE\n");
if (irq_stat & VIP_PORTB_CFG_DISABLE_COMPLETE)
vip_dbg(3, dev, "VIP_PORTB_CFG_DISABLE_COMPLETE\n");
if (irq_stat & (VIP_PORTA_ASYNC_FIFO_OF |
VIP_PORTA_OUTPUT_FIFO_YUV |
VIP_PORTA_OUTPUT_FIFO_ANC)) {
for (i = 0; i < VIP_CAP_STREAMS_PER_PORT; i++) {
if (porta->cap_streams[i] &&
porta->cap_streams[i]->port->port_id ==
porta->port_id) {
stream = porta->cap_streams[i];
break;
}
}
if (stream) {
disable_irqs(dev, dev->slice_id,
stream->list_num);
schedule_work(&stream->recovery_work);
return;
}
}
if (irq_stat & (VIP_PORTB_ASYNC_FIFO_OF |
VIP_PORTB_OUTPUT_FIFO_YUV |
VIP_PORTB_OUTPUT_FIFO_ANC)) {
for (i = 0; i < VIP_CAP_STREAMS_PER_PORT; i++) {
if (portb->cap_streams[i] &&
portb->cap_streams[i]->port->port_id ==
portb->port_id) {
stream = portb->cap_streams[i];
break;
}
}
if (stream) {
disable_irqs(dev, dev->slice_id,
stream->list_num);
schedule_work(&stream->recovery_work);
return;
}
}
}
static irqreturn_t vip_irq(int irq_vip, void *data)
{
struct vip_dev *dev = (struct vip_dev *)data;
struct vpdma_data *vpdma = dev->shared->vpdma;
struct vip_stream *stream;
int list_num;
int irq_num = dev->slice_id;
u32 irqst, irqst_saved, reg_addr;
if (!dev->shared)
return IRQ_HANDLED;
reg_addr = VIP_INT0_STATUS0 +
VIP_INTC_INTX_OFFSET * irq_num;
irqst_saved = reg_read(dev->shared, reg_addr);
irqst = irqst_saved;
vip_dbg(8, dev, "IRQ %d VIP_INT%d_STATUS0 0x%x\n",
irq_vip, irq_num, irqst);
if (irqst) {
if (irqst & (VIP_VIP1_PARSER_INT << (irq_num * 1))) {
irqst &= ~(VIP_VIP1_PARSER_INT << (irq_num * 1));
handle_parser_irqs(dev);
}
for (list_num = 0; irqst && (list_num < 8); list_num++) {
/* Check for LIST_COMPLETE IRQ */
if (!(irqst & (1 << list_num * 2)))
continue;
vip_dbg(8, dev, "IRQ %d: handling LIST%d_COMPLETE\n",
irq_num, list_num);
stream = vpdma_hwlist_get_priv(vpdma, list_num);
if (!stream || stream->list_num != list_num) {
vip_err(dev, "IRQ occurred for unused list");
continue;
}
vpdma_clear_list_stat(vpdma, irq_num, list_num);
vip_process_buffer_complete(stream);
vip_schedule_next_buffer(stream);
irqst &= ~((1 << list_num * 2));
}
}
/* Acknowledge that we are done with all interrupts */
reg_write(dev->shared, VIP_INTC_E0I, 1 << irq_num);
/* Clear handled events from status register */
reg_addr = VIP_INT0_STATUS0_CLR +
VIP_INTC_INTX_OFFSET * irq_num;
reg_write(dev->shared, reg_addr, irqst_saved);
return IRQ_HANDLED;
}
/*
* video ioctls
*/
static int vip_querycap(struct file *file, void *priv,
struct v4l2_capability *cap)
{
strlcpy(cap->driver, VIP_MODULE_NAME, sizeof(cap->driver));
strlcpy(cap->card, VIP_MODULE_NAME, sizeof(cap->card));
snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
VIP_MODULE_NAME);
cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_READWRITE;
cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
return 0;
}
static int vip_enuminput(struct file *file, void *priv,
struct v4l2_input *inp)
{
struct vip_stream *stream = file2stream(file);
if (inp->index)
return -EINVAL;
inp->type = V4L2_INPUT_TYPE_CAMERA;
inp->std = stream->vfd->tvnorms;
sprintf(inp->name, "camera %u", stream->vfd->num);
return 0;
}
static int vip_g_input(struct file *file, void *priv, unsigned int *i)
{
*i = 0;
return 0;
}
static int vip_s_input(struct file *file, void *priv, unsigned int i)
{
if (i != 0)
return -EINVAL;
return 0;
}
static int vip_querystd(struct file *file, void *fh, v4l2_std_id *std)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
*std = stream->vfd->tvnorms;
v4l2_subdev_call(port->subdev, video, querystd, std);
vip_dbg(1, dev, "querystd: 0x%lx\n", (unsigned long)*std);
return 0;
}
static int vip_g_std(struct file *file, void *fh, v4l2_std_id *std)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
*std = stream->vfd->tvnorms;
v4l2_subdev_call(port->subdev, video, g_std_output, std);
vip_dbg(1, dev, "g_std: 0x%lx\n", (unsigned long)*std);
return 0;
}
static int vip_s_std(struct file *file, void *fh, v4l2_std_id std)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
vip_dbg(1, dev, "s_std: 0x%lx\n", (unsigned long)std);
if (!(std & stream->vfd->tvnorms)) {
vip_dbg(1, dev, "s_std after check: 0x%lx\n",
(unsigned long)std);
return -EINVAL;
}
v4l2_subdev_call(port->subdev, video, s_std_output, std);
return 0;
}
static int vip_enum_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *f)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
struct vip_fmt *fmt;
vip_dbg(3, dev, "enum_fmt index:%d\n", f->index);
if (f->index >= port->num_active_fmt)
return -EINVAL;
fmt = port->active_fmt[f->index];
f->pixelformat = fmt->fourcc;
f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
vip_dbg(3, dev, "enum_fmt fourcc:%s\n", fourcc_to_str(f->pixelformat));
return 0;
}
static int vip_enum_framesizes(struct file *file, void *priv,
struct v4l2_frmsizeenum *f)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
struct vip_fmt *fmt;
struct v4l2_subdev_frame_size_enum fse;
int ret;
fmt = find_port_format_by_pix(port, f->pixel_format);
if (!fmt)
return -EINVAL;
fse.index = f->index;
fse.pad = 0;
fse.code = fmt->code;
ret = v4l2_subdev_call(port->subdev, pad, enum_frame_size, NULL, &fse);
if (ret)
return -EINVAL;
vip_dbg(1, dev, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
__func__, fse.index, fse.code, fse.min_width, fse.max_width,
fse.min_height, fse.max_height);
f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
f->discrete.width = fse.max_width;
f->discrete.height = fse.max_height;
return 0;
}
static int vip_enum_frameintervals(struct file *file, void *priv,
struct v4l2_frmivalenum *f)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
struct vip_fmt *fmt;
struct v4l2_subdev_frame_interval_enum fie = {
.index = f->index,
.width = f->width,
.height = f->height,
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
};
int ret;
fmt = find_port_format_by_pix(port, f->pixel_format);
if (!fmt)
return -EINVAL;
fie.code = fmt->code;
ret = v4l2_subdev_call(port->subdev, pad, enum_frame_interval,
NULL, &fie);
if (ret)
return ret;
f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
f->discrete = fie.interval;
return 0;
}
static int vip_g_parm(struct file *file, void *priv,
struct v4l2_streamparm *parm)
{
if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL;
parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
parm->parm.capture.timeperframe.numerator = 1;
parm->parm.capture.timeperframe.denominator = 30;
parm->parm.capture.readbuffers = 4;
return 0;
}
static int vip_s_parm(struct file *file, void *priv,
struct v4l2_streamparm *parm)
{
if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL;
parm->parm.capture.timeperframe.numerator = 1;
parm->parm.capture.timeperframe.denominator = 30;
parm->parm.capture.readbuffers = 4;
return 0;
}
static int vip_calc_format_size(struct vip_port *port,
struct vip_fmt *fmt,
struct v4l2_format *f)
{
struct vip_dev *dev = port->dev;
enum v4l2_field *field;
if (!fmt) {
vip_dbg(2, dev,
"no vip_fmt format provided!\n");
return -EINVAL;
}
field = &f->fmt.pix.field;
if (*field == V4L2_FIELD_ANY)
*field = V4L2_FIELD_NONE;
else if (V4L2_FIELD_NONE != *field && V4L2_FIELD_ALTERNATE != *field)
return -EINVAL;
v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W, W_ALIGN,
&f->fmt.pix.height, MIN_H, MAX_H, H_ALIGN,
S_ALIGN);
f->fmt.pix.bytesperline = f->fmt.pix.width *
(fmt->vpdma_fmt[0]->depth >> 3);
f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
VPDMA_STRIDE_ALIGN);
f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.width *
(fmt->vpdma_fmt[0]->depth +
(fmt->coplanar ? fmt->vpdma_fmt[1]->depth : 0)) >> 3;
f->fmt.pix.colorspace = fmt->colorspace;
f->fmt.pix.priv = 0;
vip_dbg(3, dev, "calc_format_size: fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
fourcc_to_str(f->fmt.pix.pixelformat),
f->fmt.pix.width, f->fmt.pix.height,
f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
return 0;
}
static inline bool vip_is_size_dma_aligned(u32 bpp, u32 width)
{
return ((width * bpp) == ALIGN(width * bpp, VPDMA_STRIDE_ALIGN));
}
static int vip_try_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
struct v4l2_subdev_frame_size_enum fse;
struct vip_fmt *fmt;
u32 best_width, best_height, largest_width, largest_height;
int ret, found;
enum vip_csc_state csc_direction;
vip_dbg(3, dev, "try_fmt fourcc:%s size: %dx%d\n",
fourcc_to_str(f->fmt.pix.pixelformat),
f->fmt.pix.width, f->fmt.pix.height);
fmt = find_port_format_by_pix(port, f->fmt.pix.pixelformat);
if (!fmt) {
vip_dbg(2, dev,
"Fourcc format (0x%08x) not found.\n",
f->fmt.pix.pixelformat);
/* Just get the first one enumerated */
fmt = port->active_fmt[0];
f->fmt.pix.pixelformat = fmt->fourcc;
}
csc_direction = vip_csc_direction(fmt->code, fmt->fourcc);
if (csc_direction != VIP_CSC_NA) {
if (!is_csc_available(port)) {
vip_dbg(2, dev,
"CSC not available for Fourcc format (0x%08x).\n",
f->fmt.pix.pixelformat);
/* Just get the first one enumerated */
fmt = port->active_fmt[0];
f->fmt.pix.pixelformat = fmt->fourcc;
/* re-evaluate the csc_direction here */
csc_direction = vip_csc_direction(fmt->code,
fmt->fourcc);
} else {
vip_dbg(3, dev, "CSC active on Port %c: going %s\n",
port->port_id == VIP_PORTA ? 'A' : 'B',
(csc_direction == VIP_CSC_Y2R) ? "Y2R" : "R2Y");
}
}
/*
* Given that sensors might support multiple mbus code we need
* to use the one that matches the requested pixel format
*/
port->try_mbus_framefmt = port->mbus_framefmt;
port->try_mbus_framefmt.code = fmt->code;
/* check for/find a valid width/height */
ret = 0;
found = false;
best_width = 0;
best_height = 0;
largest_width = 0;
largest_height = 0;
fse.pad = 0;
fse.code = fmt->code;
fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
for (fse.index = 0; ; fse.index++) {
u32 bpp = fmt->vpdma_fmt[0]->depth >> 3;
ret = v4l2_subdev_call(port->subdev, pad,
enum_frame_size, NULL, &fse);
if (ret)
break;
vip_dbg(3, dev, "try_fmt loop:%d fourcc:%s size: %dx%d\n",
fse.index, fourcc_to_str(f->fmt.pix.pixelformat),
fse.max_width, fse.max_height);
if (!vip_is_size_dma_aligned(bpp, fse.max_width))
continue;
if ((fse.max_width >= largest_width) &&
(fse.max_height >= largest_height)) {
vip_dbg(3, dev, "try_fmt loop:%d found new larger: %dx%d\n",
fse.index, fse.max_width, fse.max_height);
largest_width = fse.max_width;
largest_height = fse.max_height;
}
if ((fse.max_width >= f->fmt.pix.width) &&
(fse.max_height >= f->fmt.pix.height)) {
vip_dbg(3, dev, "try_fmt loop:%d found at least larger: %dx%d\n",
fse.index, fse.max_width, fse.max_height);
if (!best_width ||
((abs(best_width - f->fmt.pix.width) >=
abs(fse.max_width - f->fmt.pix.width)) &&
(abs(best_height - f->fmt.pix.height) >=
abs(fse.max_height - f->fmt.pix.height)))) {
best_width = fse.max_width;
best_height = fse.max_height;
vip_dbg(3, dev, "try_fmt loop:%d found new best: %dx%d\n",
fse.index, fse.max_width,
fse.max_height);
}
}
if ((f->fmt.pix.width == fse.max_width) &&
(f->fmt.pix.height == fse.max_height)) {
found = true;
vip_dbg(3, dev, "try_fmt loop:%d found direct match: %dx%d\n",
fse.index, fse.max_width,
fse.max_height);
break;
}
if ((f->fmt.pix.width >= fse.min_width) &&
(f->fmt.pix.width <= fse.max_width) &&
(f->fmt.pix.height >= fse.min_height) &&
(f->fmt.pix.height <= fse.max_height)) {
found = true;
vip_dbg(3, dev, "try_fmt loop:%d found direct range match: %dx%d\n",
fse.index, fse.max_width,
fse.max_height);
break;
}
}
if (found) {
port->try_mbus_framefmt.width = f->fmt.pix.width;
port->try_mbus_framefmt.height = f->fmt.pix.height;
/* No need to check for scaling */
goto calc_size;
} else if (f->fmt.pix.width > largest_width) {
port->try_mbus_framefmt.width = largest_width;
port->try_mbus_framefmt.height = largest_height;
} else if (best_width) {
port->try_mbus_framefmt.width = best_width;
port->try_mbus_framefmt.height = best_height;
} else {
/* use existing values as default */
}
vip_dbg(3, dev, "try_fmt best subdev size: %dx%d\n",
port->try_mbus_framefmt.width,
port->try_mbus_framefmt.height);
if (is_scaler_available(port) &&
csc_direction != VIP_CSC_Y2R &&
f->fmt.pix.height <= port->try_mbus_framefmt.height &&
port->try_mbus_framefmt.height <= SC_MAX_PIXEL_HEIGHT &&
port->try_mbus_framefmt.width <= SC_MAX_PIXEL_WIDTH) {
/*
* Scaler is only accessible if the dst colorspace is YUV.
* As the input to the scaler must be in YUV mode only.
*
* Scaling up is allowed only horizontally.
*/
unsigned int hratio, vratio, width_align, height_align;
u32 bpp = fmt->vpdma_fmt[0]->depth >> 3;
vip_dbg(3, dev, "Scaler active on Port %c: requesting %dx%d\n",
port->port_id == VIP_PORTA ? 'A' : 'B',
f->fmt.pix.width, f->fmt.pix.height);
/* Just make sure everything is properly aligned */
width_align = ALIGN(f->fmt.pix.width * bpp, VPDMA_STRIDE_ALIGN);
width_align /= bpp;
height_align = ALIGN(f->fmt.pix.height, 2);
f->fmt.pix.width = width_align;
f->fmt.pix.height = height_align;
hratio = f->fmt.pix.width * 1000 /
port->try_mbus_framefmt.width;
vratio = f->fmt.pix.height * 1000 /
port->try_mbus_framefmt.height;
if (hratio < 125) {
f->fmt.pix.width = port->try_mbus_framefmt.width / 8;
vip_dbg(3, dev, "Horizontal scaling ratio out of range adjusting -> %d\n",
f->fmt.pix.width);
}
if (vratio < 188) {
f->fmt.pix.height = port->try_mbus_framefmt.height / 4;
vip_dbg(3, dev, "Vertical scaling ratio out of range adjusting -> %d\n",
f->fmt.pix.height);
}
vip_dbg(3, dev, "Scaler: got %dx%d\n",
f->fmt.pix.width, f->fmt.pix.height);
} else {
/* use existing values as default */
f->fmt.pix.width = port->try_mbus_framefmt.width;
f->fmt.pix.height = port->try_mbus_framefmt.height;
}
calc_size:
/* That we have a fmt calculate imagesize and bytesperline */
return vip_calc_format_size(port, fmt, f);
}
static int vip_g_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
struct vip_dev *dev = stream->port->dev;
struct vip_fmt *fmt = port->fmt;
/* Use last known values or defaults */
f->fmt.pix.width = stream->width;
f->fmt.pix.height = stream->height;
f->fmt.pix.pixelformat = port->fmt->fourcc;
f->fmt.pix.field = stream->sup_field;
f->fmt.pix.colorspace = port->fmt->colorspace;
f->fmt.pix.bytesperline = stream->bytesperline;
f->fmt.pix.sizeimage = stream->sizeimage;
vip_dbg(3, dev,
"g_fmt fourcc:%s code: %04x size: %dx%d bpl:%d img_size:%d\n",
fourcc_to_str(f->fmt.pix.pixelformat),
fmt->code,
f->fmt.pix.width, f->fmt.pix.height,
f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
vip_dbg(3, dev, "g_fmt vpdma data type: 0x%02X\n",
port->fmt->vpdma_fmt[0]->data_type);
return 0;
}
static int vip_s_fmt_vid_cap(struct file *file, void *priv,
struct v4l2_format *f)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
struct v4l2_subdev_format sfmt;
struct v4l2_mbus_framefmt *mf;
enum vip_csc_state csc_direction;
int ret;
vip_dbg(3, dev, "s_fmt input fourcc:%s size: %dx%d\n",
fourcc_to_str(f->fmt.pix.pixelformat),
f->fmt.pix.width, f->fmt.pix.height);
ret = vip_try_fmt_vid_cap(file, priv, f);
if (ret)
return ret;
vip_dbg(3, dev, "s_fmt try_fmt fourcc:%s size: %dx%d\n",
fourcc_to_str(f->fmt.pix.pixelformat),
f->fmt.pix.width, f->fmt.pix.height);
if (vb2_is_busy(&stream->vb_vidq)) {
vip_err(dev, "%s queue busy\n", __func__);
return -EBUSY;
}
/*
* Check if we need the scaler or not
*
* Since on previous S_FMT call the scaler might have been
* allocated if it is not needed in this instance we will
* attempt to free it just in case.
*
* free_scaler() is harmless unless the current port
* allocated it.
*/
if (f->fmt.pix.width == port->try_mbus_framefmt.width &&
f->fmt.pix.height == port->try_mbus_framefmt.height)
free_scaler(port);
else
allocate_scaler(port);
port->fmt = find_port_format_by_pix(port,
f->fmt.pix.pixelformat);
stream->width = f->fmt.pix.width;
stream->height = f->fmt.pix.height;
stream->bytesperline = f->fmt.pix.bytesperline;
stream->sizeimage = f->fmt.pix.sizeimage;
stream->sup_field = f->fmt.pix.field;
port->c_rect.left = 0;
port->c_rect.top = 0;
port->c_rect.width = stream->width;
port->c_rect.height = stream->height;
/*
* Check if we need the csc unit or not
*
* Since on previous S_FMT call, the csc might have been
* allocated if it is not needed in this instance we will
* attempt to free it just in case.
*
* free_csc() is harmless unless the current port
* allocated it.
*/
csc_direction = vip_csc_direction(port->fmt->code, port->fmt->fourcc);
if (csc_direction == VIP_CSC_NA)
free_csc(port);
else
allocate_csc(port, csc_direction);
if (stream->sup_field == V4L2_FIELD_ALTERNATE)
port->flags |= FLAG_INTERLACED;
else
port->flags &= ~FLAG_INTERLACED;
vip_dbg(3, dev, "s_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
fourcc_to_str(f->fmt.pix.pixelformat),
f->fmt.pix.width, f->fmt.pix.height,
f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
mf = &sfmt.format;
v4l2_fill_mbus_format(mf, &f->fmt.pix, port->fmt->code);
/* Make sure to use the subdev size found in the try_fmt */
mf->width = port->try_mbus_framefmt.width;
mf->height = port->try_mbus_framefmt.height;
vip_dbg(3, dev, "s_fmt pix_to_mbus mbus_code: %04X size: %dx%d\n",
mf->code,
mf->width, mf->height);
sfmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
sfmt.pad = 0;
ret = v4l2_subdev_call(port->subdev, pad, set_fmt, NULL, &sfmt);
if (ret) {
vip_dbg(1, dev, "set_fmt failed in subdev\n");
return ret;
}
/* Save it */
port->mbus_framefmt = *mf;
vip_dbg(3, dev, "s_fmt subdev fmt mbus_code: %04X size: %dx%d\n",
port->mbus_framefmt.code,
port->mbus_framefmt.width, port->mbus_framefmt.height);
vip_dbg(3, dev, "s_fmt vpdma data type: 0x%02X\n",
port->fmt->vpdma_fmt[0]->data_type);
return 0;
}
static void vip_disable_sc_path(struct vip_stream *stream)
{
struct vip_dev *dev = stream->port->dev;
struct vip_port *port = stream->port;
vip_dbg(3, dev, "%s:\n", __func__);
if (port->scaler)
vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 0);
if (port->csc != VIP_CSC_NA)
vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 0);
}
/*
* Set the registers that are modified when the video format changes.
*/
static void set_fmt_params(struct vip_stream *stream)
{
struct vip_dev *dev = stream->port->dev;
struct vip_port *port = stream->port;
stream->sequence = 0;
stream->field = V4L2_FIELD_TOP;
if (port->csc == VIP_CSC_Y2R) {
port->flags &= ~FLAG_MULT_PORT;
/* Set alpha component in background color */
vpdma_set_bg_color(dev->shared->vpdma,
(struct vpdma_data_format *)
port->fmt->vpdma_fmt[0],
0xff);
if (port->port_id == VIP_PORTA) {
/*
* Input A: YUV422
* Output: Y_UP/UV_UP: RGB
* CSC_SRC_SELECT = 1
* RGB_OUT_HI_SELECT = 1
* RGB_SRC_SELECT = 1
* MULTI_CHANNEL_SELECT = 0
*/
vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 1);
vip_set_slice_path(dev,
VIP_MULTI_CHANNEL_DATA_SELECT, 0);
vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 1);
vip_set_slice_path(dev, VIP_RGB_SRC_DATA_SELECT, 1);
} else {
/*
* Input B: YUV422
* Output: Y_UP/UV_UP: RGB
* CSC_SRC_SELECT = 2
* RGB_OUT_LO_SELECT = 1
* MULTI_CHANNEL_SELECT = 0
*/
vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 2);
vip_set_slice_path(dev,
VIP_MULTI_CHANNEL_DATA_SELECT, 0);
vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 1);
}
/* We are done */
return;
} else if (port->csc == VIP_CSC_R2Y) {
port->flags &= ~FLAG_MULT_PORT;
if (port->scaler && port->fmt->coplanar) {
if (port->port_id == VIP_PORTA) {
/*
* Input A: RGB
* Output: Y_UP/UV_UP: Scaled YUV420
* CSC_SRC_SELECT = 4
* SC_SRC_SELECT = 1
* CHR_DS_1_SRC_SELECT = 1
* CHR_DS_1_BYPASS = 0
* RGB_OUT_HI_SELECT = 0
*/
vip_set_slice_path(dev,
VIP_CSC_SRC_DATA_SELECT, 4);
vip_set_slice_path(dev,
VIP_SC_SRC_DATA_SELECT, 1);
vip_set_slice_path(dev,
VIP_CHR_DS_1_SRC_DATA_SELECT,
1);
vip_set_slice_path(dev,
VIP_CHR_DS_1_DATA_BYPASS, 0);
vip_set_slice_path(dev,
VIP_RGB_OUT_HI_DATA_SELECT,
0);
} else {
vip_err(dev, "RGB sensor can only be on Port A\n");
}
} else if (port->scaler) {
if (port->port_id == VIP_PORTA) {
/*
* Input A: RGB
* Output: Y_UP: Scaled YUV422
* CSC_SRC_SELECT = 4
* SC_SRC_SELECT = 1
* CHR_DS_1_SRC_SELECT = 1
* CHR_DS_1_BYPASS = 1
* RGB_OUT_HI_SELECT = 0
*/
vip_set_slice_path(dev,
VIP_CSC_SRC_DATA_SELECT, 4);
vip_set_slice_path(dev,
VIP_SC_SRC_DATA_SELECT, 1);
vip_set_slice_path(dev,
VIP_CHR_DS_1_SRC_DATA_SELECT,
1);
vip_set_slice_path(dev,
VIP_CHR_DS_1_DATA_BYPASS, 1);
vip_set_slice_path(dev,
VIP_RGB_OUT_HI_DATA_SELECT,
0);
} else {
vip_err(dev, "RGB sensor can only be on Port A\n");
}
} else if (port->fmt->coplanar) {
if (port->port_id == VIP_PORTA) {
/*
* Input A: RGB
* Output: Y_UP/UV_UP: YUV420
* CSC_SRC_SELECT = 4
* CHR_DS_1_SRC_SELECT = 2
* CHR_DS_1_BYPASS = 0
* RGB_OUT_HI_SELECT = 0
*/
vip_set_slice_path(dev,
VIP_CSC_SRC_DATA_SELECT, 4);
vip_set_slice_path(dev,
VIP_CHR_DS_1_SRC_DATA_SELECT,
2);
vip_set_slice_path(dev,
VIP_CHR_DS_1_DATA_BYPASS, 0);
vip_set_slice_path(dev,
VIP_RGB_OUT_HI_DATA_SELECT,
0);
} else {
vip_err(dev, "RGB sensor can only be on Port A\n");
}
} else {
if (port->port_id == VIP_PORTA) {
/*
* Input A: RGB
* Output: Y_UP/UV_UP: YUV420
* CSC_SRC_SELECT = 4
* CHR_DS_1_SRC_SELECT = 2
* CHR_DS_1_BYPASS = 1
* RGB_OUT_HI_SELECT = 0
*/
vip_set_slice_path(dev,
VIP_CSC_SRC_DATA_SELECT, 4);
vip_set_slice_path(dev,
VIP_CHR_DS_1_SRC_DATA_SELECT,
2);
vip_set_slice_path(dev,
VIP_CHR_DS_1_DATA_BYPASS, 1);
vip_set_slice_path(dev,
VIP_RGB_OUT_HI_DATA_SELECT,
0);
} else {
vip_err(dev, "RGB sensor can only be on Port A\n");
}
}
/* We are done */
return;
} else if (vip_is_fmt_rgb(port->fmt->fourcc)) {
port->flags |= FLAG_MULT_PORT;
/* Set alpha component in background color */
vpdma_set_bg_color(dev->shared->vpdma,
(struct vpdma_data_format *)
port->fmt->vpdma_fmt[0],
0xff);
if (port->port_id == VIP_PORTA) {
/*
* Input A: RGB
* Output: Y_LO/UV_LO: RGB
* RGB_OUT_LO_SELECT = 1
* MULTI_CHANNEL_SELECT = 1
*/
vip_set_slice_path(dev,
VIP_MULTI_CHANNEL_DATA_SELECT, 1);
vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 1);
} else {
vip_err(dev, "RGB sensor can only be on Port A\n");
}
/* We are done */
return;
}
if (port->scaler && port->fmt->coplanar) {
port->flags &= ~FLAG_MULT_PORT;
if (port->port_id == VIP_PORTA) {
/*
* Input A: YUV422
* Output: Y_UP/UV_UP: Scaled YUV420
* SC_SRC_SELECT = 2
* CHR_DS_1_SRC_SELECT = 1
* CHR_DS_1_BYPASS = 0
* RGB_OUT_HI_SELECT = 0
*/
vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 2);
vip_set_slice_path(dev,
VIP_CHR_DS_1_SRC_DATA_SELECT, 1);
vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
} else {
/*
* Input B: YUV422
* Output: Y_LO/UV_LO: Scaled YUV420
* SC_SRC_SELECT = 3
* CHR_DS_2_SRC_SELECT = 1
* RGB_OUT_LO_SELECT = 0
* MULTI_CHANNEL_SELECT = 0
*/
vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 3);
vip_set_slice_path(dev,
VIP_CHR_DS_2_SRC_DATA_SELECT, 1);
vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
vip_set_slice_path(dev,
VIP_MULTI_CHANNEL_DATA_SELECT, 0);
}
} else if (port->scaler) {
port->flags &= ~FLAG_MULT_PORT;
if (port->port_id == VIP_PORTA) {
/*
* Input A: YUV422
* Output: Y_UP: Scaled YUV422
* SC_SRC_SELECT = 2
* CHR_DS_1_SRC_SELECT = 1
* CHR_DS_1_BYPASS = 1
* RGB_OUT_HI_SELECT = 0
*/
vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 2);
vip_set_slice_path(dev,
VIP_CHR_DS_1_SRC_DATA_SELECT, 1);
vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 1);
vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
} else {
/*
* Input B: YUV422
* Output: UV_UP: Scaled YUV422
* SC_SRC_SELECT = 3
* CHR_DS_2_SRC_SELECT = 1
* CHR_DS_1_BYPASS = 1
* CHR_DS_2_BYPASS = 1
* RGB_OUT_HI_SELECT = 0
*/
vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 3);
vip_set_slice_path(dev,
VIP_CHR_DS_2_SRC_DATA_SELECT, 1);
vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 1);
vip_set_slice_path(dev, VIP_CHR_DS_2_DATA_BYPASS, 1);
vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
}
} else if (port->fmt->coplanar) {
port->flags &= ~FLAG_MULT_PORT;
if (port->port_id == VIP_PORTA) {
/*
* Input A: YUV422
* Output: Y_UP/UV_UP: YUV420
* CHR_DS_1_SRC_SELECT = 3
* CHR_DS_1_BYPASS = 0
* RGB_OUT_HI_SELECT = 0
*/
vip_set_slice_path(dev,
VIP_CHR_DS_1_SRC_DATA_SELECT, 3);
vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
} else {
/*
* Input B: YUV422
* Output: Y_LO/UV_LO: YUV420
* CHR_DS_2_SRC_SELECT = 4
* CHR_DS_2_BYPASS = 0
* RGB_OUT_LO_SELECT = 0
* MULTI_CHANNEL_SELECT = 0
*/
vip_set_slice_path(dev,
VIP_CHR_DS_2_SRC_DATA_SELECT, 4);
vip_set_slice_path(dev, VIP_CHR_DS_2_DATA_BYPASS, 0);
vip_set_slice_path(dev,
VIP_MULTI_CHANNEL_DATA_SELECT, 0);
vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
}
} else {
port->flags |= FLAG_MULT_PORT;
/*
* Input A/B: YUV422
* Output: Y_LO: YUV422 - UV_LO: YUV422
* MULTI_CHANNEL_SELECT = 1
* RGB_OUT_LO_SELECT = 0
*/
vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 1);
vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
}
}
static int vip_g_selection(struct file *file, void *fh,
struct v4l2_selection *s)
{
struct vip_stream *stream = file2stream(file);
switch (s->target) {
case V4L2_SEL_TGT_COMPOSE_DEFAULT:
case V4L2_SEL_TGT_COMPOSE_BOUNDS:
case V4L2_SEL_TGT_CROP_BOUNDS:
case V4L2_SEL_TGT_CROP_DEFAULT:
s->r.left = 0;
s->r.top = 0;
s->r.width = stream->width;
s->r.height = stream->height;
return 0;
case V4L2_SEL_TGT_COMPOSE:
case V4L2_SEL_TGT_CROP:
s->r = stream->port->c_rect;
return 0;
}
return -EINVAL;
}
static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
{
if (a->left < b->left || a->top < b->top)
return 0;
if (a->left + a->width > b->left + b->width)
return 0;
if (a->top + a->height > b->top + b->height)
return 0;
return 1;
}
static int vip_s_selection(struct file *file, void *fh,
struct v4l2_selection *s)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
struct v4l2_rect r = s->r;
v4l_bound_align_image(&r.width, 0, stream->width, 0,
&r.height, 0, stream->height, 0, 0);
r.left = clamp_t(unsigned int, r.left, 0, stream->width - r.width);
r.top = clamp_t(unsigned int, r.top, 0, stream->height - r.height);
if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
return -ERANGE;
if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
return -ERANGE;
s->r = r;
stream->port->c_rect = r;
vip_dbg(1, port->dev, "cropped (%d,%d)/%dx%d of %dx%d\n",
r.left, r.top, r.width, r.height,
stream->width, stream->height);
return 0;
}
static long vip_ioctl_default(struct file *file, void *fh, bool valid_prio,
unsigned int cmd, void *arg)
{
struct vip_stream *stream = file2stream(file);
struct vip_port *port = stream->port;
if (!valid_prio) {
vip_err(port->dev, "%s device busy\n", __func__);
return -EBUSY;
}
switch (cmd) {
default:
return -ENOTTY;
}
}
static const struct v4l2_ioctl_ops vip_ioctl_ops = {
.vidioc_querycap = vip_querycap,
.vidioc_enum_input = vip_enuminput,
.vidioc_g_input = vip_g_input,
.vidioc_s_input = vip_s_input,
.vidioc_querystd = vip_querystd,
.vidioc_g_std = vip_g_std,
.vidioc_s_std = vip_s_std,
.vidioc_enum_fmt_vid_cap = vip_enum_fmt_vid_cap,
.vidioc_g_fmt_vid_cap = vip_g_fmt_vid_cap,
.vidioc_try_fmt_vid_cap = vip_try_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = vip_s_fmt_vid_cap,
.vidioc_enum_frameintervals = vip_enum_frameintervals,
.vidioc_enum_framesizes = vip_enum_framesizes,
.vidioc_s_parm = vip_s_parm,
.vidioc_g_parm = vip_g_parm,
.vidioc_g_selection = vip_g_selection,
.vidioc_s_selection = vip_s_selection,
.vidioc_reqbufs = vb2_ioctl_reqbufs,
.vidioc_create_bufs = vb2_ioctl_create_bufs,
.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
.vidioc_querybuf = vb2_ioctl_querybuf,
.vidioc_qbuf = vb2_ioctl_qbuf,
.vidioc_dqbuf = vb2_ioctl_dqbuf,
.vidioc_expbuf = vb2_ioctl_expbuf,
.vidioc_streamon = vb2_ioctl_streamon,
.vidioc_streamoff = vb2_ioctl_streamoff,
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
.vidioc_default = vip_ioctl_default,
};
/*
* Videobuf operations
*/
static int vip_queue_setup(struct vb2_queue *vq, const void *parg,
unsigned int *nbuffers, unsigned int *nplanes,
unsigned int sizes[], void *alloc_ctxs[])
{
const struct v4l2_format *fmt = parg;
struct vip_stream *stream = vb2_get_drv_priv(vq);
struct vip_dev *dev = stream->port->dev;
if (fmt && fmt->fmt.pix.sizeimage < stream->sizeimage)
return -EINVAL;
*nplanes = 1;
sizes[0] = stream->sizeimage;
alloc_ctxs[0] = dev->alloc_ctx;
vip_dbg(1, dev, "get %d buffer(s) of size %d each.\n",
*nbuffers, sizes[0]);
return 0;
}
static int vip_buf_prepare(struct vb2_buffer *vb)
{
struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
struct vip_dev *dev = stream->port->dev;
if (vb2_plane_size(vb, 0) < stream->sizeimage) {
vip_dbg(1, dev,
"%s data will not fit into plane (%lu < %lu)\n",
__func__, vb2_plane_size(vb, 0),
(long)stream->sizeimage);
return -EINVAL;
}
vb2_set_plane_payload(vb, 0, stream->sizeimage);
return 0;
}
static void vip_buf_queue(struct vb2_buffer *vb)
{
struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
struct vip_dev *dev = stream->port->dev;
struct vip_buffer *buf = container_of(vb, struct vip_buffer,
vb.vb2_buf);
unsigned long flags;
spin_lock_irqsave(&dev->slock, flags);
list_add_tail(&buf->list, &stream->vidq);
spin_unlock_irqrestore(&dev->slock, flags);
}
static int vip_setup_scaler(struct vip_stream *stream)
{
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
struct sc_data *sc = dev->sc;
struct csc_data *csc = dev->csc;
struct vpdma_data *vpdma = dev->shared->vpdma;
struct vip_mmr_adb *mmr_adb = port->mmr_adb.addr;
int list_num = stream->list_num;
int timeout = 500;
/* if scaler not associated with this port then skip */
if (port->scaler) {
sc_set_hs_coeffs(sc, port->sc_coeff_h.addr,
port->mbus_framefmt.width,
port->c_rect.width);
sc_set_vs_coeffs(sc, port->sc_coeff_v.addr,
port->mbus_framefmt.height,
port->c_rect.height);
sc_config_scaler(sc, &mmr_adb->sc_regs0[0],
&mmr_adb->sc_regs8[0], &mmr_adb->sc_regs17[0],
port->mbus_framefmt.width,
port->mbus_framefmt.height,
port->c_rect.width,
port->c_rect.height);
port->load_mmrs = true;
}
/* if csc not associated with this port then skip */
if (port->csc) {
csc_set_coeff(csc, &mmr_adb->csc_regs[0],
vip_code_to_colorspace(port->fmt->code),
vip_fourcc_to_colorspace(port->fmt->fourcc));
port->load_mmrs = true;
}
/* If coeff are already loaded then skip */
if (!sc->load_coeff_v && !sc->load_coeff_h && !port->load_mmrs)
return 0;
if (vpdma_list_busy(vpdma, list_num)) {
vip_dbg(3, dev, "%s: List %d is busy\n",
__func__, list_num);
}
/* Make sure we start with a clean list */
vpdma_reset_desc_list(&stream->desc_list);
/* config descriptors */
if (port->load_mmrs) {
vpdma_map_desc_buf(vpdma, &port->mmr_adb);
vpdma_add_cfd_adb(&stream->desc_list, CFD_MMR_CLIENT,
&port->mmr_adb);
port->load_mmrs = false;
vip_dbg(3, dev, "Added mmr_adb config desc\n");
}
if (sc->loaded_coeff_h != port->sc_coeff_h.dma_addr ||
sc->load_coeff_h) {
vpdma_map_desc_buf(vpdma, &port->sc_coeff_h);
vpdma_add_cfd_block(&stream->desc_list,
VIP_SLICE1_CFD_SC_CLIENT + dev->slice_id,
&port->sc_coeff_h, 0);
sc->loaded_coeff_h = port->sc_coeff_h.dma_addr;
sc->load_coeff_h = false;
vip_dbg(3, dev, "Added sc_coeff_h config desc\n");
}
if (sc->loaded_coeff_v != port->sc_coeff_v.dma_addr ||
sc->load_coeff_v) {
vpdma_map_desc_buf(vpdma, &port->sc_coeff_v);
vpdma_add_cfd_block(&stream->desc_list,
VIP_SLICE1_CFD_SC_CLIENT + dev->slice_id,
&port->sc_coeff_v, SC_COEF_SRAM_SIZE >> 4);
sc->loaded_coeff_v = port->sc_coeff_v.dma_addr;
sc->load_coeff_v = false;
vip_dbg(3, dev, "Added sc_coeff_v config desc\n");
}
vip_dbg(3, dev, "CFD_SC_CLIENT %d slice_id: %d\n",
VIP_SLICE1_CFD_SC_CLIENT + dev->slice_id, dev->slice_id);
vpdma_map_desc_buf(vpdma, &stream->desc_list.buf);
vip_dbg(3, dev, "Submitting desc on list# %d\n", list_num);
vpdma_submit_descs(vpdma, &stream->desc_list, list_num);
while (vpdma_list_busy(vpdma, list_num) && timeout--)
usleep_range(1000, 1100);
vpdma_unmap_desc_buf(dev->shared->vpdma, &port->mmr_adb);
vpdma_unmap_desc_buf(dev->shared->vpdma, &port->sc_coeff_h);
vpdma_unmap_desc_buf(dev->shared->vpdma, &port->sc_coeff_v);
vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
vpdma_reset_desc_list(&stream->desc_list);
if (timeout <= 0) {
vip_err(dev, "Timed out setting up scaler through VPDMA list\n");
return -EBUSY;
}
return 0;
}
static int vip_load_vpdma_list_fifo(struct vip_stream *stream)
{
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
struct vpdma_data *vpdma = dev->shared->vpdma;
int list_num = stream->list_num;
struct vip_buffer *buf;
unsigned long flags;
int timeout, i;
if (vpdma_list_busy(dev->shared->vpdma, stream->list_num))
return -EBUSY;
for (i = 0; i < VIP_VPDMA_FIFO_SIZE; i++) {
spin_lock_irqsave(&dev->slock, flags);
if (list_empty(&stream->vidq)) {
vip_err(dev, "No buffer left!");
spin_unlock_irqrestore(&dev->slock, flags);
return -EINVAL;
}
buf = list_entry(stream->vidq.next,
struct vip_buffer, list);
buf->drop = false;
list_move_tail(&buf->list, &stream->post_bufs);
spin_unlock_irqrestore(&dev->slock, flags);
vip_dbg(2, dev, "%s: start_dma vb2 buf idx:%d\n",
__func__, buf->vb.vb2_buf.index);
start_dma(stream, buf);
timeout = 500;
while (vpdma_list_busy(vpdma, list_num) && timeout--)
usleep_range(1000, 1100);
if (timeout <= 0) {
vip_err(dev, "Timed out loading VPDMA list fifo\n");
return -EBUSY;
}
}
return 0;
}
static int vip_start_streaming(struct vb2_queue *vq, unsigned int count)
{
struct vip_stream *stream = vb2_get_drv_priv(vq);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
int ret;
vip_setup_scaler(stream);
/*
* Make sure the scaler is configured before the datapath is
* enabled. The scaler can only load the coefficient
* parameters when it is idle. If the scaler path is enabled
* and video data is being received then the VPDMA transfer will
* stall indefinetely.
*/
set_fmt_params(stream);
vip_setup_parser(port);
if (port->subdev) {
ret = v4l2_subdev_call(port->subdev, video, s_stream, 1);
if (ret) {
vip_dbg(1, dev, "stream on failed in subdev\n");
return ret;
}
}
stream->sequence = 0;
stream->field = V4L2_FIELD_TOP;
populate_desc_list(stream);
ret = vip_load_vpdma_list_fifo(stream);
if (ret)
return ret;
stream->num_recovery = 0;
clear_irqs(dev, dev->slice_id, stream->list_num);
enable_irqs(dev, dev->slice_id, stream->list_num);
vip_schedule_next_buffer(stream);
vip_parser_stop_imm(port, false);
vip_enable_parser(port, true);
return 0;
}
/*
* Abort streaming and wait for last buffer
*/
static void vip_stop_streaming(struct vb2_queue *vq)
{
struct vip_stream *stream = vb2_get_drv_priv(vq);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
struct vip_buffer *buf;
int ret;
vip_dbg(2, dev, "%s:\n", __func__);
vip_parser_stop_imm(port, true);
vip_enable_parser(port, false);
vip_disable_sc_path(stream);
disable_irqs(dev, dev->slice_id, stream->list_num);
clear_irqs(dev, dev->slice_id, stream->list_num);
if (port->subdev) {
ret = v4l2_subdev_call(port->subdev, video, s_stream, 0);
if (ret)
vip_dbg(1, dev, "stream on failed in subdev\n");
}
stop_dma(stream, true);
/* release all active buffers */
while (!list_empty(&stream->post_bufs)) {
buf = list_entry(stream->post_bufs.next,
struct vip_buffer, list);
list_del(&buf->list);
if (buf->drop == 1)
list_add_tail(&buf->list, &stream->dropq);
else
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
}
while (!list_empty(&stream->vidq)) {
buf = list_entry(stream->vidq.next, struct vip_buffer, list);
list_del(&buf->list);
vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
}
if (!vb2_is_streaming(vq))
return;
vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
vpdma_reset_desc_list(&stream->desc_list);
}
static struct vb2_ops vip_video_qops = {
.queue_setup = vip_queue_setup,
.buf_prepare = vip_buf_prepare,
.buf_queue = vip_buf_queue,
.start_streaming = vip_start_streaming,
.stop_streaming = vip_stop_streaming,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
};
/*
* File operations
*/
static int vip_init_dev(struct vip_dev *dev)
{
if (dev->num_ports != 0)
goto done;
vip_set_clock_enable(dev, 1);
vip_module_reset(dev, VIP_SC_RST, false);
vip_module_reset(dev, VIP_CSC_RST, false);
done:
dev->num_ports++;
return 0;
}
static inline bool is_scaler_available(struct vip_port *port)
{
if (port->num_streams_configured == 1)
if (port->dev->sc_assigned == VIP_NOT_ASSIGNED ||
port->dev->sc_assigned == port->port_id)
return true;
return false;
}
static inline bool allocate_scaler(struct vip_port *port)
{
if (port->num_streams_configured == 1) {
if (port->dev->sc_assigned == VIP_NOT_ASSIGNED ||
port->dev->sc_assigned == port->port_id) {
port->dev->sc_assigned = port->port_id;
port->scaler = true;
return true;
}
}
return false;
}
static inline void free_scaler(struct vip_port *port)
{
if (port->dev->sc_assigned == port->port_id) {
port->dev->sc_assigned = VIP_NOT_ASSIGNED;
port->scaler = false;
}
}
static bool is_csc_available(struct vip_port *port)
{
if (port->num_streams_configured == 1)
if (port->dev->csc_assigned == VIP_NOT_ASSIGNED ||
port->dev->csc_assigned == port->port_id)
return true;
return false;
}
static bool allocate_csc(struct vip_port *port,
enum vip_csc_state csc_direction)
{
/* Is CSC needed? */
if (csc_direction != VIP_CSC_NA) {
if (is_csc_available(port)) {
port->dev->csc_assigned = port->port_id;
port->csc = csc_direction;
vip_dbg(1, port->dev, "%s: csc allocated: dir: %d\n",
__func__, csc_direction);
return true;
}
}
return false;
}
static void free_csc(struct vip_port *port)
{
if (port->dev->csc_assigned == port->port_id) {
port->dev->csc_assigned = VIP_NOT_ASSIGNED;
port->csc = VIP_CSC_NA;
vip_dbg(1, port->dev, "%s: csc freed\n",
__func__);
}
}
static int vip_init_port(struct vip_port *port)
{
int ret;
struct vip_dev *dev = port->dev;
struct vip_fmt *fmt;
struct v4l2_subdev_format sd_fmt;
struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
if (port->num_streams != 0)
goto done;
ret = vip_init_dev(port->dev);
if (ret)
goto done;
/* Get subdevice current frame format */
sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
sd_fmt.pad = 0;
ret = v4l2_subdev_call(port->subdev, pad, get_fmt, NULL, &sd_fmt);
if (ret)
vip_dbg(1, dev, "init_port get_fmt failed in subdev: (%d)\n",
ret);
/* try to find one that matches */
fmt = find_port_format_by_code(port, mbus_fmt->code);
if (!fmt) {
vip_dbg(1, dev, "subdev default mbus_fmt %04x is not matched.\n",
mbus_fmt->code);
/* if all else fails just pick the first one */
fmt = port->active_fmt[0];
mbus_fmt->code = fmt->code;
sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
sd_fmt.pad = 0;
ret = v4l2_subdev_call(port->subdev, pad, set_fmt,
NULL, &sd_fmt);
if (ret)
vip_dbg(1, dev, "init_port set_fmt failed in subdev: (%d)\n",
ret);
}
/* Assign current format */
port->fmt = fmt;
port->mbus_framefmt = *mbus_fmt;
vip_dbg(3, dev, "vip_init_port: g_mbus_fmt subdev mbus_code: %04X fourcc:%s size: %dx%d\n",
fmt->code,
fourcc_to_str(fmt->fourcc),
mbus_fmt->width, mbus_fmt->height);
if (mbus_fmt->field == V4L2_FIELD_ALTERNATE)
port->flags |= FLAG_INTERLACED;
else
port->flags &= ~FLAG_INTERLACED;
port->c_rect.left = 0;
port->c_rect.top = 0;
port->c_rect.width = mbus_fmt->width;
port->c_rect.height = mbus_fmt->height;
ret = vpdma_alloc_desc_buf(&port->sc_coeff_h, SC_COEF_SRAM_SIZE);
if (ret != 0)
return ret;
ret = vpdma_alloc_desc_buf(&port->sc_coeff_v, SC_COEF_SRAM_SIZE);
if (ret != 0)
goto free_sc_h;
ret = vpdma_alloc_desc_buf(&port->mmr_adb, sizeof(struct vip_mmr_adb));
if (ret != 0)
goto free_sc_v;
init_adb_hdrs(port);
vip_enable_parser(port, false);
done:
port->num_streams++;
return 0;
free_sc_v:
vpdma_free_desc_buf(&port->sc_coeff_v);
free_sc_h:
vpdma_free_desc_buf(&port->sc_coeff_h);
return ret;
}
static int vip_init_stream(struct vip_stream *stream)
{
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
struct vip_fmt *fmt;
struct v4l2_mbus_framefmt *mbus_fmt;
struct v4l2_format f;
int ret;
ret = vip_init_port(port);
if (ret != 0)
return ret;
fmt = port->fmt;
mbus_fmt = &port->mbus_framefmt;
/* Properly calculate the sizeimage and bytesperline values. */
v4l2_fill_pix_format(&f.fmt.pix, mbus_fmt);
f.fmt.pix.pixelformat = fmt->fourcc;
ret = vip_calc_format_size(port, fmt, &f);
if (ret)
return ret;
stream->width = f.fmt.pix.width;
stream->height = f.fmt.pix.height;
stream->sup_field = f.fmt.pix.field;
stream->bytesperline = f.fmt.pix.bytesperline;
stream->sizeimage = f.fmt.pix.sizeimage;
vip_dbg(3, dev, "init_stream fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
fourcc_to_str(f.fmt.pix.pixelformat),
f.fmt.pix.width, f.fmt.pix.height,
f.fmt.pix.bytesperline, f.fmt.pix.sizeimage);
vip_dbg(3, dev, "init_stream vpdma data type: 0x%02X\n",
port->fmt->vpdma_fmt[0]->data_type);
ret = vpdma_create_desc_list(&stream->desc_list, VIP_DESC_LIST_SIZE,
VPDMA_LIST_TYPE_NORMAL);
if (ret != 0)
return ret;
stream->write_desc = (struct vpdma_dtd *)stream->desc_list.buf.addr
+ 15;
vip_dbg(1, dev, "%s: stream instance %pa\n",
__func__, &stream);
return 0;
}
static void vip_release_dev(struct vip_dev *dev)
{
/*
* On last close, disable clocks to conserve power
*/
if (--dev->num_ports == 0) {
/* reset the scaler module */
vip_module_reset(dev, VIP_SC_RST, true);
vip_module_reset(dev, VIP_CSC_RST, true);
vip_set_clock_enable(dev, 0);
}
}
static int vip_set_crop_parser(struct vip_port *port)
{
struct vip_dev *dev = port->dev;
struct vip_parser_data *parser = dev->parser;
u32 hcrop = 0, vcrop = 0;
u32 width = port->mbus_framefmt.width;
if (port->fmt->vpdma_fmt[0] == &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8]) {
/*
* Special case since we are faking a YUV422 16bit format
* to have the vpdma perform the needed byte swap
* we need to adjust the pixel width accordingly
* otherwise the parser will attempt to collect more pixels
* then available and the vpdma transfer will exceed the
* allocated frame buffer.
*/
width >>= 1;
vip_dbg(1, dev, "%s: 8 bit raw detected, adjusting width to %d\n",
__func__, width);
}
/*
* Set Parser Crop parameters to source size otherwise
* scaler and colorspace converter will yield garbage.
*/
hcrop = VIP_ACT_BYPASS;
insert_field(&hcrop, 0, VIP_ACT_SKIP_NUMPIX_MASK,
VIP_ACT_SKIP_NUMPIX_SHFT);
insert_field(&hcrop, width,
VIP_ACT_USE_NUMPIX_MASK, VIP_ACT_USE_NUMPIX_SHFT);
reg_write(parser, VIP_PARSER_CROP_H_PORT(port->port_id), hcrop);
insert_field(&vcrop, 0, VIP_ACT_SKIP_NUMLINES_MASK,
VIP_ACT_SKIP_NUMLINES_SHFT);
insert_field(&vcrop, port->mbus_framefmt.height,
VIP_ACT_USE_NUMLINES_MASK, VIP_ACT_USE_NUMLINES_SHFT);
reg_write(parser, VIP_PARSER_CROP_V_PORT(port->port_id), vcrop);
return 0;
}
static int vip_setup_parser(struct vip_port *port)
{
struct vip_dev *dev = port->dev;
struct vip_parser_data *parser = dev->parser;
struct v4l2_of_endpoint *endpoint = port->endpoint;
int iface, sync_type;
u32 flags = 0, config0;
/* Reset the port */
vip_reset_parser(port, true);
usleep_range(200, 250);
vip_reset_parser(port, false);
config0 = reg_read(parser, VIP_PARSER_PORT(port->port_id));
if (endpoint->bus_type == V4L2_MBUS_BT656) {
flags = endpoint->bus.parallel.flags;
iface = DUAL_8B_INTERFACE;
/*
* Ideally, this should come from subdev
* port->fmt can be anything once CSC is enabled
*/
if (vip_is_mbuscode_rgb(port->fmt->code)) {
sync_type = EMBEDDED_SYNC_SINGLE_RGB_OR_YUV444;
} else {
switch (endpoint->bus.parallel.num_channels) {
case 4:
sync_type = EMBEDDED_SYNC_4X_MULTIPLEXED_YUV422;
break;
case 2:
sync_type = EMBEDDED_SYNC_2X_MULTIPLEXED_YUV422;
break;
case 1:
sync_type = EMBEDDED_SYNC_SINGLE_YUV422;
break;
default:
sync_type =
EMBEDDED_SYNC_LINE_MULTIPLEXED_YUV422;
}
if (endpoint->bus.parallel.pixmux == 0)
sync_type =
EMBEDDED_SYNC_LINE_MULTIPLEXED_YUV422;
}
} else if (endpoint->bus_type == V4L2_MBUS_PARALLEL) {
flags = endpoint->bus.parallel.flags;
switch (endpoint->bus.parallel.bus_width) {
case 24:
iface = SINGLE_24B_INTERFACE;
break;
case 16:
iface = SINGLE_16B_INTERFACE;
break;
case 8:
default:
iface = DUAL_8B_INTERFACE;
}
if (vip_is_mbuscode_rgb(port->fmt->code))
sync_type = DISCRETE_SYNC_SINGLE_RGB_24B;
else
sync_type = DISCRETE_SYNC_SINGLE_YUV422;
if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
config0 |= VIP_HSYNC_POLARITY;
else if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
config0 &= ~VIP_HSYNC_POLARITY;
if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
config0 |= VIP_VSYNC_POLARITY;
else if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
config0 &= ~VIP_VSYNC_POLARITY;
config0 &= ~VIP_USE_ACTVID_HSYNC_ONLY;
config0 |= VIP_ACTVID_POLARITY;
config0 |= VIP_DISCRETE_BASIC_MODE;
} else {
vip_err(dev, "Device doesn't support CSI2");
return -EINVAL;
}
if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING) {
vip_set_pclk_invert(port);
config0 |= VIP_PIXCLK_EDGE_POLARITY;
} else {
config0 &= ~VIP_PIXCLK_EDGE_POLARITY;
}
config0 |= ((sync_type & VIP_SYNC_TYPE_MASK) << VIP_SYNC_TYPE_SHFT);
reg_write(parser, VIP_PARSER_PORT(port->port_id), config0);
vip_set_data_interface(port, iface);
vip_set_crop_parser(port);
return 0;
}
static __maybe_unused void vip_enable_parser(struct vip_port *port, bool on)
{
u32 config0;
struct vip_dev *dev = port->dev;
struct vip_parser_data *parser = dev->parser;
config0 = reg_read(parser, VIP_PARSER_PORT(port->port_id));
if (on) {
config0 |= VIP_PORT_ENABLE;
config0 &= ~(VIP_ASYNC_FIFO_RD | VIP_ASYNC_FIFO_WR);
} else {
config0 &= ~VIP_PORT_ENABLE;
config0 |= (VIP_ASYNC_FIFO_RD | VIP_ASYNC_FIFO_WR);
}
reg_write(parser, VIP_PARSER_PORT(port->port_id), config0);
}
static __maybe_unused void vip_reset_parser(struct vip_port *port, bool on)
{
u32 config0;
struct vip_dev *dev = port->dev;
struct vip_parser_data *parser = dev->parser;
config0 = reg_read(parser, VIP_PARSER_PORT(port->port_id));
if (on)
config0 |= VIP_SW_RESET;
else
config0 &= ~VIP_SW_RESET;
reg_write(parser, VIP_PARSER_PORT(port->port_id), config0);
}
static __maybe_unused void vip_parser_stop_imm(struct vip_port *port, bool on)
{
u32 config0;
struct vip_dev *dev = port->dev;
struct vip_parser_data *parser = dev->parser;
config0 = reg_read(parser, VIP_PARSER_STOP_IMM_PORT(port->port_id));
if (on)
config0 = 0xffffffff;
else
config0 = 0;
reg_write(parser, VIP_PARSER_STOP_IMM_PORT(port->port_id), config0);
}
static void vip_release_stream(struct vip_stream *stream)
{
struct vip_dev *dev = stream->port->dev;
vip_dbg(1, dev, "%s: stream instance %pa\n",
__func__, &stream);
vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
vpdma_free_desc_buf(&stream->desc_list.buf);
vpdma_free_desc_list(&stream->desc_list);
}
static void vip_release_port(struct vip_port *port)
{
struct vip_dev *dev = port->dev;
vip_dbg(1, dev, "%s: port instance %pa\n",
__func__, &port);
vpdma_free_desc_buf(&port->mmr_adb);
vpdma_free_desc_buf(&port->sc_coeff_h);
vpdma_free_desc_buf(&port->sc_coeff_v);
}
static void stop_dma(struct vip_stream *stream, bool clear_list)
{
struct vip_dev *dev = stream->port->dev;
int ch, size = 0;
/* Create a list of channels to be cleared */
for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++) {
if (stream->vpdma_channels[ch] == 1) {
stream->vpdma_channels_to_abort[size++] = ch;
vip_dbg(2, dev, "Clear channel no: %d\n", ch);
}
}
/* Clear all the used channels for the list */
vpdma_list_cleanup(dev->shared->vpdma, stream->list_num,
stream->vpdma_channels_to_abort, size);
if (clear_list)
for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++)
stream->vpdma_channels[ch] = 0;
}
static int vip_open(struct file *file)
{
struct vip_stream *stream = video_drvdata(file);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
int ret = 0;
vip_dbg(2, dev, "vip_open\n");
mutex_lock(&dev->mutex);
ret = v4l2_fh_open(file);
if (ret) {
vip_err(dev, "v4l2_fh_open failed\n");
goto unlock;
}
/*
* If this is the first open file.
* Then initialize hw module.
*/
if (!v4l2_fh_is_singular_file(file))
goto unlock;
if (vip_init_stream(stream))
ret = -ENODEV;
unlock:
mutex_unlock(&dev->mutex);
return ret;
}
static int vip_release(struct file *file)
{
struct vip_stream *stream = video_drvdata(file);
struct vip_port *port = stream->port;
struct vip_dev *dev = port->dev;
bool fh_singular;
int ret;
vip_dbg(2, dev, "vip_release\n");
mutex_lock(&dev->mutex);
/* Save the singular status before we call the clean-up helper */
fh_singular = v4l2_fh_is_singular_file(file);
/* the release helper will cleanup any on-going streaming */
ret = _vb2_fop_release(file, NULL);
free_csc(port);
free_scaler(port);
/*
* If this is the last open file.
* Then de-initialize hw module.
*/
if (fh_singular) {
vip_release_stream(stream);
if (--port->num_streams == 0) {
vip_release_port(port);
vip_release_dev(port->dev);
}
}
mutex_unlock(&dev->mutex);
return ret;
}
static const struct v4l2_file_operations vip_fops = {
.owner = THIS_MODULE,
.open = vip_open,
.release = vip_release,
.read = vb2_fop_read,
.poll = vb2_fop_poll,
.unlocked_ioctl = video_ioctl2,
.mmap = vb2_fop_mmap,
};
static struct video_device vip_videodev = {
.name = VIP_MODULE_NAME,
.fops = &vip_fops,
.ioctl_ops = &vip_ioctl_ops,
.minor = -1,
.release = video_device_release,
.tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
};
static int alloc_stream(struct vip_port *port, int stream_id, int vfl_type)
{
struct vip_stream *stream;
struct vip_dev *dev = port->dev;
struct vb2_queue *q;
struct video_device *vfd;
struct vip_buffer *buf;
int ret, i;
stream = kzalloc(sizeof(*stream), GFP_KERNEL);
if (!stream)
return -ENOMEM;
stream->port = port;
stream->stream_id = stream_id;
stream->vfl_type = vfl_type;
stream->list_num = vpdma_hwlist_alloc(dev->shared->vpdma, stream);
if (stream->list_num < 0) {
vip_err(dev, "Could not get VPDMA hwlist");
ret = -ENODEV;
goto do_free_stream;
}
INIT_LIST_HEAD(&stream->post_bufs);
if (vfl_type == VFL_TYPE_GRABBER)
port->cap_streams[stream_id] = stream;
else
port->vbi_streams[stream_id] = stream;
/*
* Initialize queue
*/
q = &stream->vb_vidq;
q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
q->drv_priv = stream;
q->buf_struct_size = sizeof(struct vip_buffer);
q->ops = &vip_video_qops;
q->mem_ops = &vb2_dma_contig_memops;
q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
q->lock = &dev->mutex;
q->min_buffers_needed = 3;
ret = vb2_queue_init(q);
if (ret)
goto do_free_stream;
INIT_WORK(&stream->recovery_work, vip_overflow_recovery_work);
INIT_LIST_HEAD(&stream->vidq);
/* Allocate/populate Drop queue entries */
INIT_LIST_HEAD(&stream->dropq);
for (i = 0; i < VIP_DROPQ_SIZE; i++) {
buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
if (!buf) {
ret = -ENOMEM;
goto do_free_stream;
}
buf->drop = true;
list_add(&buf->list, &stream->dropq);
}
vfd = video_device_alloc();
if (!vfd)
goto do_free_stream;
*vfd = vip_videodev;
vfd->v4l2_dev = &dev->v4l2_dev;
vfd->queue = q;
vfd->lock = &dev->mutex;
video_set_drvdata(vfd, stream);
ret = video_register_device(vfd, vfl_type, -1);
if (ret) {
vip_err(dev, "Failed to register video device\n");
goto do_free_stream;
}
stream->vfd = vfd;
vip_info(dev, "device registered as %s\n",
video_device_node_name(vfd));
return 0;
do_free_stream:
kfree(stream);
return ret;
}
static void free_stream(struct vip_stream *stream)
{
struct vip_dev *dev;
struct vip_buffer *buf;
struct list_head *pos, *q;
if (!stream)
return;
dev = stream->port->dev;
/* Free up the Drop queue */
list_for_each_safe(pos, q, &stream->dropq) {
buf = list_entry(pos,
struct vip_buffer, list);
vip_dbg(1, dev, "dropq buffer\n");
list_del(pos);
kfree(buf);
}
video_unregister_device(stream->vfd);
vpdma_hwlist_release(dev->shared->vpdma, stream->list_num);
stream->port->cap_streams[stream->stream_id] = NULL;
kfree(stream);
}
static int get_subdev_active_format(struct vip_port *port,
struct v4l2_subdev *subdev)
{
struct vip_dev *dev = port->dev;
struct vip_fmt *fmt;
struct v4l2_subdev_mbus_code_enum mbus_code;
int ret = 0;
unsigned int k, i, j;
/* Enumerate sub device formats and enable all matching local formats */
port->num_active_fmt = 0;
for (k = 0, i = 0;
(ret != -EINVAL);
k++) {
memset(&mbus_code, 0, sizeof(mbus_code));
mbus_code.index = k;
ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
NULL, &mbus_code);
if (ret == 0) {
vip_dbg(2, dev,
"subdev %s: code: %04x idx: %d\n",
subdev->name, mbus_code.code, k);
for (j = 0; j < ARRAY_SIZE(vip_formats); j++) {
fmt = &vip_formats[j];
if (mbus_code.code == fmt->code) {
port->active_fmt[i] = fmt;
vip_dbg(2, dev,
"matched fourcc: %s: code: %04x idx: %d\n",
fourcc_to_str(fmt->fourcc),
fmt->code, i);
port->num_active_fmt = ++i;
}
}
}
}
if (i == 0) {
vip_err(dev, "No suitable format reported by subdev %s\n",
subdev->name);
return -EINVAL;
}
return 0;
}
static int alloc_port(struct vip_dev *dev, int id)
{
struct vip_port *port;
port = devm_kzalloc(&dev->pdev->dev, sizeof(*port), GFP_KERNEL);
if (!port)
return -ENOMEM;
dev->ports[id] = port;
port->dev = dev;
port->port_id = id;
port->num_streams = 0;
return 0;
}
static void free_port(struct vip_port *port)
{
if (!port)
return;
v4l2_async_notifier_unregister(&port->notifier);
free_stream(port->cap_streams[0]);
}
static int get_field(u32 value, u32 mask, int shift)
{
return (value & (mask << shift)) >> shift;
}
static int vip_of_probe(struct platform_device *pdev);
static void vip_vpdma_fw_cb(struct platform_device *pdev)
{
dev_info(&pdev->dev, "VPDMA firmware loaded\n");
if (pdev->dev.of_node)
vip_of_probe(pdev);
}
static int vip_create_streams(struct vip_port *port,
struct v4l2_subdev *subdev)
{
struct v4l2_of_bus_parallel *bus;
int i;
for (i = 0; i < VIP_CAP_STREAMS_PER_PORT; i++)
free_stream(port->cap_streams[i]);
if (get_subdev_active_format(port, subdev))
return -ENODEV;
port->subdev = subdev;
if (port->endpoint->bus_type == V4L2_MBUS_PARALLEL) {
port->flags |= FLAG_MULT_PORT;
port->num_streams_configured = 1;
alloc_stream(port, 0, VFL_TYPE_GRABBER);
} else if (port->endpoint->bus_type == V4L2_MBUS_BT656) {
port->flags |= FLAG_MULT_PORT;
bus = &port->endpoint->bus.parallel;
port->num_streams_configured = bus->num_channels;
for (i = 0; i < bus->num_channels; i++) {
if (bus->channels[i] >= 16)
continue;
alloc_stream(port, bus->channels[i], VFL_TYPE_GRABBER);
}
}
return 0;
}
static int vip_async_bound(struct v4l2_async_notifier *notifier,
struct v4l2_subdev *subdev,
struct v4l2_async_subdev *asd)
{
struct vip_port *port = notifier_to_vip_port(notifier);
struct vip_async_config *config = &port->config;
struct vip_dev *dev = port->dev;
unsigned int idx = asd - &config->asd[0];
int ret;
vip_dbg(1, dev, "vip_async_bound\n");
if (idx > config->asd_sizes)
return -EINVAL;
if (port->subdev) {
if (asd < port->subdev->asd)
/* Notified of a subdev earlier in the array */
vip_info(dev, "Switching to subdev %s (High priority)",
subdev->name);
else {
vip_info(dev, "Rejecting subdev %s (Low priority)",
subdev->name);
return 0;
}
}
port->endpoint = &config->endpoints[idx];
vip_info(dev, "Port %c: Using subdev %s for capture\n",
port->port_id == VIP_PORTA ? 'A' : 'B', subdev->name);
ret = vip_create_streams(port, subdev);
if (ret)
return ret;
return 0;
}
static int vip_async_complete(struct v4l2_async_notifier *notifier)
{
struct vip_port *port = notifier_to_vip_port(notifier);
struct vip_dev *dev = port->dev;
vip_dbg(1, dev, "vip_async_complete\n");
return 0;
}
static struct device_node *
of_get_next_available_port(const struct device_node *parent,
struct device_node *prev)
{
struct device_node *port = NULL;
if (!parent)
return NULL;
do {
port = of_get_next_available_child(parent, prev);
if (!port)
return NULL;
prev = port;
} while (of_node_cmp(port->name, "port") != 0);
return port;
}
static struct device_node *
of_get_next_endpoint(const struct device_node *parent,
struct device_node *prev)
{
struct device_node *ep = NULL;
if (!parent)
return NULL;
do {
ep = of_get_next_child(parent, prev);
if (!ep)
return NULL;
prev = ep;
} while (of_node_cmp(ep->name, "endpoint") != 0);
return ep;
}
static int vip_register_subdev_notif(struct vip_port *port,
struct device_node *port_node)
{
struct vip_async_config *config = &port->config;
struct v4l2_async_notifier *notifier = &port->notifier;
struct v4l2_async_subdev *asd;
struct vip_dev *dev = port->dev;
struct device_node *ep_node = NULL, *subdev_node, *subdev_ep;
int i = 0, ret;
while (i < VIP_MAX_SUBDEV) {
subdev_node = NULL;
subdev_ep = NULL;
ep_node = of_get_next_endpoint(port_node, ep_node);
if (!ep_node) {
vip_dbg(3, dev, "can't get next endpoint: loop: %d\n",
i);
break;
}
subdev_node = of_graph_get_remote_port_parent(ep_node);
if (!subdev_node) {
vip_dbg(3, dev, "can't get remote parent: loop: %d\n",
i);
goto of_node_cleanup;
}
subdev_ep = of_parse_phandle(ep_node, "remote-endpoint", 0);
if (!subdev_ep) {
vip_dbg(3, dev, "can't get remote-endpoint: loop: %d\n",
i);
goto of_node_cleanup;
}
ret = v4l2_of_parse_endpoint(subdev_ep, &config->endpoints[i]);
if (ret) {
vip_dbg(3, dev, "Failed to parse endpoint: loop: %d\n",
i);
goto of_node_cleanup;
}
asd = &config->asd[i];
asd->match_type = V4L2_ASYNC_MATCH_OF;
asd->match.of.node = subdev_node;
config->asd_list[i] = asd;
i++;
of_node_cleanup:
if (!subdev_ep)
of_node_put(subdev_ep);
if (!subdev_node)
of_node_put(subdev_node);
}
if (i == 0) {
vip_err(dev, "Port %c enabled but no endpoints found\n",
port->port_id == VIP_PORTA ? 'A' : 'B');
ret = -EINVAL;
goto skip_async;
}
config->asd_sizes = i;
notifier->bound = vip_async_bound;
notifier->complete = vip_async_complete;
notifier->subdevs = config->asd_list;
notifier->num_subdevs = config->asd_sizes;
vip_dbg(1, dev, "register async notifier for %d subdevs\n", i);
ret = v4l2_async_notifier_register(&dev->v4l2_dev, notifier);
if (ret) {
vip_dbg(1, dev, "Error registering async notifier\n");
ret = -EINVAL;
}
skip_async:
if (!subdev_ep)
of_node_put(subdev_ep);
if (!subdev_node)
of_node_put(subdev_node);
if (!ep_node)
of_node_put(ep_node);
return ret;
}
static int vip_of_probe(struct platform_device *pdev)
{
struct vip_shared *shared = platform_get_drvdata(pdev);
struct regmap *syscon_pol = NULL;
u32 syscon_pol_offset = 0;
struct vip_port *port;
struct vip_dev *dev;
struct device_node *parent = pdev->dev.of_node;
struct device_node *port_node = NULL;
int ret, slice_id, port_id;
u32 regval = 0;
if (parent && of_property_read_bool(parent, "syscon-pol")) {
syscon_pol = syscon_regmap_lookup_by_phandle(parent,
"syscon-pol");
if (IS_ERR(syscon_pol)) {
dev_err(&pdev->dev, "failed to get syscon-pol regmap\n");
return PTR_ERR(syscon_pol);
}
if (of_property_read_u32_index(parent, "syscon-pol", 1,
&syscon_pol_offset)) {
dev_err(&pdev->dev, "failed to get syscon-pol offset\n");
return -EINVAL;
}
}
while (1) {
port_node = of_get_next_available_port(parent, port_node);
if (!port_node)
break;
/* Find the port from <REG> */
of_property_read_u32(port_node, "reg", ®val);
switch (regval) {
case 0:
slice_id = VIP_SLICE1; port_id = VIP_PORTA;
break;
case 1:
slice_id = VIP_SLICE2; port_id = VIP_PORTA;
break;
case 2:
slice_id = VIP_SLICE1; port_id = VIP_PORTB;
break;
case 3:
slice_id = VIP_SLICE2; port_id = VIP_PORTB;
break;
default:
dev_err(&pdev->dev, "Unknown port reg=<%d>\n", regval);
continue;
}
dev = shared->devs[slice_id];
dev->syscon_pol = syscon_pol;
dev->syscon_pol_offset = syscon_pol_offset;
alloc_port(dev, port_id);
port = dev->ports[port_id];
ret = vip_register_subdev_notif(port, port_node);
of_node_put(port_node);
}
return 0;
}
static const struct of_device_id vip_of_match[];
static int vip_probe(struct platform_device *pdev)
{
struct vip_dev *dev;
struct vip_shared *shared;
struct vip_parser_data *parser;
const struct of_device_id *of_dev_id;
struct pinctrl *pinctrl;
int ret, slice = VIP_SLICE1;
u32 tmp, pid;
struct v4l2_ctrl_handler *hdl;
pm_runtime_enable(&pdev->dev);
ret = pm_runtime_get_sync(&pdev->dev);
if (ret)
goto err_runtime_get;
of_dev_id = of_match_device(vip_of_match, &pdev->dev);
if (!of_dev_id) {
dev_err(&pdev->dev, "%s: Unable to match device\n", __func__);
return -ENODEV;
}
shared = devm_kzalloc(&pdev->dev, sizeof(*shared), GFP_KERNEL);
if (!shared)
return -ENOMEM;
shared->res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vip");
shared->base = devm_ioremap_resource(&pdev->dev, shared->res);
if (IS_ERR(shared->base)) {
dev_err(&pdev->dev, "failed to ioremap\n");
ret = PTR_ERR(shared->base);
goto err_runtime_get;
}
pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
/* Make sure H/W module has the right functionality */
pid = reg_read(shared, VIP_PID);
tmp = get_field(pid, VIP_PID_FUNC_MASK, VIP_PID_FUNC_SHIFT);
if (tmp != VIP_PID_FUNC) {
dev_info(&pdev->dev, "vip: unexpected PID function: 0x%x\n",
tmp);
ret = -ENODEV;
goto err_runtime_get;
}
/* enable clocks, so the firmware will load properly */
vip_shared_set_clock_enable(shared, 1);
vip_top_vpdma_reset(shared);
platform_set_drvdata(pdev, shared);
for (slice = VIP_SLICE1; slice < VIP_NUM_SLICES; slice++) {
dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
if (!dev) {
ret = -ENOMEM;
goto err_runtime_get;
}
dev->instance_id = (int)of_dev_id->data;
snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
"%s%d-s%d", VIP_MODULE_NAME, dev->instance_id, slice);
dev->irq = platform_get_irq(pdev, slice);
if (!dev->irq) {
dev_err(&pdev->dev, "Could not get IRQ");
goto err_runtime_get;
}
if (devm_request_irq(&pdev->dev, dev->irq, vip_irq,
0, dev->v4l2_dev.name, dev) < 0) {
ret = -ENOMEM;
goto dev_unreg;
}
spin_lock_init(&dev->slock);
spin_lock_init(&dev->lock);
ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
if (ret)
goto err_runtime_get;
mutex_init(&dev->mutex);
hdl = &dev->ctrl_handler;
v4l2_ctrl_handler_init(hdl, 11);
dev->v4l2_dev.ctrl_handler = hdl;
dev->slice_id = slice;
dev->pdev = pdev;
dev->res = shared->res;
dev->base = shared->base;
dev->shared = shared;
shared->devs[slice] = dev;
dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
if (IS_ERR(dev->alloc_ctx)) {
vip_err(dev, "Failed to alloc vb2 context\n");
ret = PTR_ERR(dev->alloc_ctx);
goto dev_unreg;
}
vip_top_reset(dev);
vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 1);
parser = devm_kzalloc(&pdev->dev, sizeof(*dev->parser),
GFP_KERNEL);
if (!parser)
return PTR_ERR(parser);
parser->res = platform_get_resource_byname(pdev,
IORESOURCE_MEM,
(slice == 0) ?
"parser0" :
"parser1");
parser->base = devm_ioremap_resource(&pdev->dev, parser->res);
if (IS_ERR(parser->base)) {
ret = PTR_ERR(parser->base);
goto ctx_clean;
}
parser->pdev = pdev;
dev->parser = parser;
dev->sc_assigned = VIP_NOT_ASSIGNED;
dev->sc = sc_create_inst(pdev, slice);
if (IS_ERR(dev->sc)) {
ret = PTR_ERR(dev->sc);
goto ctx_clean;
}
dev->csc_assigned = VIP_NOT_ASSIGNED;
dev->csc = csc_create(pdev, (slice == 0) ? "csc0" : "csc1");
if (IS_ERR(dev->sc)) {
ret = PTR_ERR(dev->sc);
goto ctx_clean;
}
}
shared->vpdma = &shared->vpdma_data;
ret = vpdma_create(pdev, shared->vpdma, vip_vpdma_fw_cb);
if (ret) {
dev_err(&pdev->dev, "Creating VPDMA failed");
goto ctx_clean;
}
return 0;
ctx_clean:
vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
dev_unreg:
v4l2_device_unregister(&dev->v4l2_dev);
err_runtime_get:
if (slice == VIP_SLICE1) {
pm_runtime_disable(&pdev->dev);
return ret;
} else {
return 0;
}
}
static int vip_remove(struct platform_device *pdev)
{
struct vip_shared *shared = platform_get_drvdata(pdev);
struct vip_dev *dev;
int slice;
for (slice = 0; slice < VIP_NUM_SLICES; slice++) {
dev = shared->devs[slice];
if (!dev)
continue;
free_port(dev->ports[VIP_PORTA]);
free_port(dev->ports[VIP_PORTB]);
vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
}
pm_runtime_put_sync(&pdev->dev);
pm_runtime_disable(&pdev->dev);
return 0;
}
#if defined(CONFIG_OF)
static const struct of_device_id vip_of_match[] = {
{
.compatible = "ti,vip1", .data = (void *)VIP_INSTANCE1,
},
{
.compatible = "ti,vip2", .data = (void *)VIP_INSTANCE2,
},
{
.compatible = "ti,vip3", .data = (void *)VIP_INSTANCE3,
},
{},
};
MODULE_DEVICE_TABLE(of, vip_of_match);
#endif
static struct platform_driver vip_pdrv = {
.probe = vip_probe,
.remove = vip_remove,
.driver = {
.name = VIP_MODULE_NAME,
.of_match_table = of_match_ptr(vip_of_match),
},
};
module_platform_driver(vip_pdrv);
MODULE_DESCRIPTION("TI VIP driver");
MODULE_AUTHOR("Texas Instruments");
MODULE_LICENSE("GPL v2");