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
|
From ce14be51d71bf39893786d380cbb82e81d2a10d5 Mon Sep 17 00:00:00 2001
From: Nick Hollinghurst <nick.hollinghurst@raspberrypi.org>
Date: Wed, 14 Jul 2021 09:32:49 +0100
Subject: [PATCH] Add new "pispbe" driver (though not yet the Makesfiles or DT
required to use it)
media: bcm2712: Initial commit of the PiSP BE driver
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
media: bcm2712_pisp_be: PiSP driver updates.
- Start registering video nodes from /dev/video20
- Formatting fixes
- Define MODULE_DEVICE_TABLE() to probe correctly
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
media: pisp_be: Improve image format support
Add a new format table that lists the V4L2 format enums and their properties.
Keep the exising 'RPBP' format to support the userland verification tools.
This format requires userland to fill all plane properties. Standard V4L2
formats will derive these properties from the format table.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
media: pisp_be: Advertise the meta output format explictily.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
drivers: pisp_be: Various updates and cleanups
- Switch to a single node group for now.
- Add a node description table to simplify node handling.
- Switch HoG output to V4L2_CAP_META_CAPTURE type.
- Use string descriptions for node names in logging messages.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
pisp_be: Updates for libcamera usage:
- Remove indexes from device entity names
- Add enumframesize and enumfmts ioctls
- Add default format to all nodes.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
v4l2: pisp_be: Move format definitions into v4l2 core
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
media: raspberrypi: Move PiSP common headers to a single location
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
media: raspberrypi: Remove old pispbe driver.
This is now supersede by the driver in drivers/media/platform/raspberrypi/
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
PISP-BE Driver: Automate buffer-cycling for TDN and Stitch state.
Remove "tdn-input" and "stitch-input" nodes altogether (the output
nodes must still be opened and REQBUFS called with 1 or 2 buffers).
Also, a bit of tidying of buffer address handling and locking.
PISP-BE driver: Turn debug level right down to reduce overly-chatty messages
media: bcm2712: Depend on CONFIG_PM
Depend on CONFIG_PM as the driver uses the runtime_pm infrastructure.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
drivers: media: pisp_be: Move BE driver to a raspberrypi directory
Move the pisp_be driver from drivers/media/platform/raspberrypi/ to
drivers/media/platform/raspberrypi/pisp_be/. This seems the accepted
convention in the drivers/media/platform/ directory structure.
Also rename the driver module from bcm2712_pisp_be to pisp_be.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
pisp_be: Updates for libcamera streaming:
- Add some required v4l2 formats
- Add buf_prepare ioctl
- Set plane offsets correctly before reprogramming
pisp_be: Reduce logging verbosity
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
pisp_be: Add buffer timestamps
While at it, remove duplicate code when checking if the HW has completed
multiple jobs.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
pisp_be: Remove queue size allocation constraint
PISP-BE driver: Fix ISR to handle multiple done/start events.
PISP-BE: Fix variable-name shadowing bugette
PISP-BE: Support for two node groups. Reorganize the driver.
To support 2 concurrent libcamera applications, we need 2 node groups,
need to allow multiple opens of each node (because libcamera does this)
and create a separate media device per group (to support file-locking).
This triggered significant rearrangement of the driver. Some calls
that we formerly intercepted have been delegated back to v4l2/vb2.
Logging changes arising from multiple v4l2_dev. Refactored probe()
and initialization. Avoid dynamically-allocated entity name strings.
drivers: media: pisp_be: Add vidioc_enum_fmt_meta_out
This was missing in the struct v4l2_ioctl_ops definition.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
drivers: media: pispe_be: Add Bayer compressed formats
Add PiSP Bayer compressed formats to the list of supported pixel formats
for the PiSP backend driver.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
drivers: meida: pisp_be: Fix overflow in plane size calculations
The calculations for buffer plane sizes can overflow because of the
plane factor shift. Fix this by using u64 integers for the calculations.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
drivers: media: pisp_be: Use 0P3 for plane factors
Use less precision for the plane factors to avoid any nasty overflows.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
media: pisp: Checkpatch and coding style fixups
Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
media: pisp_be: More coding style fixups
media: platform: bcm2712: pisp_be: Fix crash when buffer format not set
Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
media: platform: bcm2712: pisp_be: Allow non-SRGB colour spaces on RGB outputs
Allow colour spaces other than SRGB when the output format in question
is an RGB output. This commit merely ports over existing changes from
the vc4 ISP driver.
Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
media: platform: bcm2712: Tweak list of BE supported image formats
Remove RGB565 and 10- and 12-bit packed raw formats, which ISP-BE
can't support for input or output. Add NV12M and NV21M which it can.
(I didn't bother adding YUV422P, which apparently is not widely used.)
Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
pisp_be: Fill the hardware revision in the media entity struct
This can be used by userland to determine the hardware capabilities.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
bcm2712: Use BIT() macro
Use the BIT() macro instead of plain bit shifting.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
bcm2712: Invert condition in pispbe_schedule_internal()
Return earlier and save one indentation level
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
bcm2712: Invert condition in for loop
Save one indentation level by continuing if the node is not streaming.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
bcm2712: Do not declare a local variable
There already is a truct pispbe_node *node in the function scope.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
bcm21712: Siplify pispbe_schedule_one()
A little more verbose but easier to follow ?
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
bcm2712: Rename pispbe_schedule_all() to pispbe_schedule_any()
The pispbe_schedule_all() function name is misleading, as the function
schedule a single job from any of the node groups. Rename it.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: platform: bcm2712: Remove buffer auto-cycling from ISP-BE
Previously, the ISP-BE driver tried to automate "ping pong" buffers
for TDN and HDR state, but did not fully conceal them from users.
The automation has been removed: there are now separate output and
capture queues for each of TDN and Stitch, which must be managed by
user code (DMABUFs may be used to circulate buffers between queues).
Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
drivers: media: pisp_be: Cache BE config buffer vaddr
When programming a new job, we access at the config buffer, possibly
from ISR context. So fetch and the virtual address when queuing the
buffer.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
drivers: media: pisp_be: Remove all traces of ctrls and request API
These APIs are not (and will not) be used by the driver, so remove them.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
media: bcm2712: Replace v4l2_dbg with dev_dbg
Replace the v4l2 debug helpers with the device debug once, which are
preferred.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Remove of_match_ptr()
The of_match_ptr() usage could cause a compiler warning if
CONFIG_OF is not enabled, as the pispbe_of_match variable would
result unused.
As the of_match_table field of struct platform_driver exists
unconditionally, drop of_match_ptr() to avoid a warning.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
drivers: media: pispbe: Add local config buffer DMA allocation
When initialiasing the driver, allocate a number of tiles + config
structures used for storing hardware config internally in the driver.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
drivers: media: pispbe: Use local config buffers
Store a copy of the config + tiles buffer locally when the buffer gets
queued. This resolves the security issue where a userland process may
modify the config buffer after it has been queued.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
drivers: media: pispbe: Validate config buffers
Perform a basic config validation on the device output nodes to ensure
the buffer size and stride values do not result in a buffer overrun.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
media: bcm2712: Rework probe sequence order
Rework the probe sequence to:
1) Use dev_err_probe() when failing to get clocks
2) Disable clock on error path
3) Disable the node groups if they have been enabled and
propagate the error up
Also disable clocks in the remove() function.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Use pm_runtime_ops
Introduce usage of runtime resume and suspend operations.
The diver only uses a single clock source which is enable/disabled
at resume and suspend time.
Implement file open and release operations to control enablement of
the clock provider.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Demote info message
Demote info message about clock enablement to dev_dbg()
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Move pm_runtime calls to streamon/streamoff
Move the calls to pm_runtime_resume_and_get() and pm_runtime_put()
to the streamon and streamoff ioctl handlers.
Remove custom handlers for the open and close file operations and use
the framework provided helpers.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Use pm_runtime_autosuspend()
Use the _autosuspend() version of runtime_pm_put() in order to avoid
resuming and suspending the peripheral in between streaming sessions
closely apart one from the other.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
drivers: media: pisp_be: Conditionally check buffers when preparing jobs
When preparing a job, check the global enables in the config structure
to see if the Output0/1, Tdn and Stitch blocks are enabled, and only
test for a buffer queued if they are.
This will allow userland to control the outputs selectively without
disabling/re-enabling the respective device nodes.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
media: bcm2712: Rework media controller registration
The current implementation register the v4l2_device and the video
devices first, then creates the media controller and manually registers
entities there.
Rework the registration procedure to first create the v4l2_device and
register the media_device with it. Then create the video nodes which
gets automatically registered in the media graph by the core.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Create v4l2_subdev for ISP entity
Create a v4l2 subdevice to represent the PISPBE ISP entity.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Fix v4l2-compliance warn on QUERYCAP
Fix:
warn: v4l2-compliance.cpp(669): media bus_info
'platform:1000880000.pisp_be' differs from V4L2 bus_info
'platform:pispbe'
by populating the driver caps bus_info by using dev_name().
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Fix v4l2-compliance warn on invalid pixfmt
The V4L2 API for the TRY_FMT/S_FMT ioctl allows the ioctl handler to
return an error code only in specific conditions. If an invalid pixel
format is supplied it should be adjusted instead of an error being
returned.
Albeit, v4l2-compliance treats this situation as a warning and not as
an error because the behaviour has been discussed in length in the past.
warn: v4l2-test-formats.cpp(794): TRY_FMT cannot handle an invalid pixelformat.
warn: v4l2-test-formats.cpp(795): This may or may not be a problem. For more information see:
warn: v4l2-test-formats.cpp(796): http://www.mail-archive.com/linux-media@vger.kernel.org/msg56550.html
VIDIOC_TRY_FMT returned -1 (Invalid argument)
Regardless of the warning vs failure decision, adjust the try_format()
function implementation to use V4L2_PIX_FMT_YUV420M as default pixel
format if the supplied one is invalid.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Fix v4l2-compliance warn on HOG pix format
The try_format() implementation for the HOG video device node returns
an error if the supplied pixel format is not correct.
As per the video device output and capture video nodes, this contradicts
the V4L2 specification even if it is treated as a warning by
v4l2-compliance.
Fix this by forcing the buffer pixel format and size to the default
supported one. While at here, use the BIT() macro in the format
initialization function.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Fix formats enumeration
Right now a single implementation of enum_fmt() is used for all nodes
in a group. This means that all the BE supported formats are listed for
all the nodes. This is incorrect as the meta capture and output node
formats should be restricted, and the meta formats should not be
enumerated for video output and capture devices.
Fix this by restricting the enumeration of META formats to the config
and hog nodes. Split out from the list of supported_formats the
V4L2_META_FMT_RPI_BE_CFG which is only used for the meta_out node, while
V4L2_PIX_FMT_RPI_BE is kept in the list of supported_formats as it can
be used as an opaque format for both meta_cap, video_cap and video_out
nodes.
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
media: bcm2712: Minor fixes to support PiSP regression tests
Allow RGB input, not just Bayer (but only of those at once);
Allow Wallpaper image formats. XXX They are not yet size-checked;
Set "chicken bits" to test BURST_TRIM and AXI AWID/BID variation.
Convert some v4l2_err() to dev_err()
Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
drivers: media: pisp_be: Use the maximum number of config buffers
Set PISP_BE_NUM_CONFIG_BUFFERS the the maximum number of possible
buffers. In the worst case, this overallocates config buffers, but
given their size, it's not too much of a problem.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
media: pisp_be: Fix extra PM runtime put
vidioc_streamoff callback can be called even if vidioc_streamon was
never called. The driver currently does PM runtime get/put in these
callbacks, which may lead to a put without a matching get.
Fix this by moving the PM runtime get/put to vb2_ops's start_streaming &
stop_streaming, which the framework makes sure won't get extra calls.
Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
drivers: media: pisp_be: Don't report V4L2_PIX_FMT_RPI_BE format
This is an internal opaque format, not to be reported in enum_fmt.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
---
drivers/media/platform/Kconfig | 1 +
drivers/media/platform/Makefile | 1 +
drivers/media/platform/raspberrypi/Kconfig | 5 +
drivers/media/platform/raspberrypi/Makefile | 3 +
.../platform/raspberrypi/pisp_be/Kconfig | 12 +
.../platform/raspberrypi/pisp_be/Makefile | 6 +
.../platform/raspberrypi/pisp_be/pisp_be.c | 1985 +++++++++++++++++
.../raspberrypi/pisp_be/pisp_be_config.h | 533 +++++
.../raspberrypi/pisp_be/pisp_be_formats.h | 469 ++++
drivers/media/v4l2-core/v4l2-ioctl.c | 2 +
include/media/raspberrypi/pisp_common.h | 65 +
include/media/raspberrypi/pisp_types.h | 144 ++
include/uapi/linux/videodev2.h | 6 +
13 files changed, 3232 insertions(+)
create mode 100644 drivers/media/platform/raspberrypi/Kconfig
create mode 100644 drivers/media/platform/raspberrypi/Makefile
create mode 100644 drivers/media/platform/raspberrypi/pisp_be/Kconfig
create mode 100644 drivers/media/platform/raspberrypi/pisp_be/Makefile
create mode 100644 drivers/media/platform/raspberrypi/pisp_be/pisp_be.c
create mode 100644 drivers/media/platform/raspberrypi/pisp_be/pisp_be_config.h
create mode 100644 drivers/media/platform/raspberrypi/pisp_be/pisp_be_formats.h
create mode 100644 include/media/raspberrypi/pisp_common.h
create mode 100644 include/media/raspberrypi/pisp_types.h
--- a/drivers/media/platform/Kconfig
+++ b/drivers/media/platform/Kconfig
@@ -76,6 +76,7 @@ source "drivers/media/platform/mediatek/
source "drivers/media/platform/nvidia/Kconfig"
source "drivers/media/platform/nxp/Kconfig"
source "drivers/media/platform/qcom/Kconfig"
+source "drivers/media/platform/raspberrypi/Kconfig"
source "drivers/media/platform/renesas/Kconfig"
source "drivers/media/platform/rockchip/Kconfig"
source "drivers/media/platform/samsung/Kconfig"
--- a/drivers/media/platform/Makefile
+++ b/drivers/media/platform/Makefile
@@ -19,6 +19,7 @@ obj-y += mediatek/
obj-y += nvidia/
obj-y += nxp/
obj-y += qcom/
+obj-y += raspberrypi/
obj-y += renesas/
obj-y += rockchip/
obj-y += samsung/
--- /dev/null
+++ b/drivers/media/platform/raspberrypi/Kconfig
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+comment "Raspberry Pi media platform drivers"
+
+source "drivers/media/platform/raspberrypi/pisp_be/Kconfig"
--- /dev/null
+++ b/drivers/media/platform/raspberrypi/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-y += pisp_be/
--- /dev/null
+++ b/drivers/media/platform/raspberrypi/pisp_be/Kconfig
@@ -0,0 +1,12 @@
+config VIDEO_RASPBERRYPI_PISP_BE
+ tristate "Raspberry Pi PiSP Backend (BE) ISP driver"
+ depends on VIDEO_DEV && PM
+ select VIDEO_V4L2_SUBDEV_API
+ select MEDIA_CONTROLLER
+ select VIDEOBUF2_DMA_CONTIG
+ select V4L2_FWNODE
+ help
+ Say Y here to enable support for the PiSP Backend (BE) ISP driver.
+
+ To compile this driver as a module, choose M here. The module will be
+ called pisp-be.
--- /dev/null
+++ b/drivers/media/platform/raspberrypi/pisp_be/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for Raspberry Pi PiSP Backend driver
+#
+pisp-be-objs := pisp_be.o
+obj-$(CONFIG_VIDEO_RASPBERRYPI_PISP_BE) += pisp-be.o
--- /dev/null
+++ b/drivers/media/platform/raspberrypi/pisp_be/pisp_be.c
@@ -0,0 +1,1985 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * PiSP Back End driver.
+ * Copyright (c) 2021-2022 Raspberry Pi Limited.
+ *
+ */
+#include <linux/clk.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-ioctl.h>
+#include <media/videobuf2-dma-contig.h>
+#include <media/videobuf2-vmalloc.h>
+
+#include "pisp_be_config.h"
+#include "pisp_be_formats.h"
+
+MODULE_DESCRIPTION("PiSP Back End driver");
+MODULE_AUTHOR("David Plowman <david.plowman@raspberrypi.com>");
+MODULE_AUTHOR("Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>");
+MODULE_LICENSE("GPL v2");
+
+/* Offset to use when registering the /dev/videoX node */
+#define PISPBE_VIDEO_NODE_OFFSET 20
+
+/* Maximum number of config buffers possible */
+#define PISP_BE_NUM_CONFIG_BUFFERS VB2_MAX_FRAME
+
+/*
+ * We want to support 2 independent instances allowing 2 simultaneous users
+ * of the ISP-BE (of course they share hardware, platform resources and mutex).
+ * Each such instance comprises a group of device nodes representing input
+ * and output queues, and a media controller device node to describe them.
+ */
+#define PISPBE_NUM_NODE_GROUPS 2
+
+#define PISPBE_NAME "pispbe"
+
+/* Some ISP-BE registers */
+#define PISP_BE_VERSION_OFFSET (0x0)
+#define PISP_BE_CONTROL_OFFSET (0x4)
+#define PISP_BE_TILE_ADDR_LO_OFFSET (0x8)
+#define PISP_BE_TILE_ADDR_HI_OFFSET (0xc)
+#define PISP_BE_STATUS_OFFSET (0x10)
+#define PISP_BE_BATCH_STATUS_OFFSET (0x14)
+#define PISP_BE_INTERRUPT_EN_OFFSET (0x18)
+#define PISP_BE_INTERRUPT_STATUS_OFFSET (0x1c)
+#define PISP_BE_AXI_OFFSET (0x20)
+#define PISP_BE_CONFIG_BASE_OFFSET (0x40)
+#define PISP_BE_IO_INPUT_ADDR0_LO_OFFSET (PISP_BE_CONFIG_BASE_OFFSET)
+#define PISP_BE_GLOBAL_BAYER_ENABLE_OFFSET (PISP_BE_CONFIG_BASE_OFFSET + 0x70)
+#define PISP_BE_GLOBAL_RGB_ENABLE_OFFSET (PISP_BE_CONFIG_BASE_OFFSET + 0x74)
+#define N_HW_ADDRESSES 14
+#define N_HW_ENABLES 2
+
+#define PISP_BE_VERSION_2712C1 0x02252700
+#define PISP_BE_VERSION_MINOR_BITS 0xF
+
+/*
+ * This maps our nodes onto the inputs/outputs of the actual PiSP Back End.
+ * Be wary of the word "OUTPUT" which is used ambiguously here. In a V4L2
+ * context it means an input to the hardware (source image or metadata).
+ * Elsewhere it means an output from the hardware.
+ */
+enum node_ids {
+ MAIN_INPUT_NODE,
+ TDN_INPUT_NODE,
+ STITCH_INPUT_NODE,
+ HOG_OUTPUT_NODE,
+ OUTPUT0_NODE,
+ OUTPUT1_NODE,
+ TDN_OUTPUT_NODE,
+ STITCH_OUTPUT_NODE,
+ CONFIG_NODE,
+ PISPBE_NUM_NODES
+};
+
+struct node_description {
+ const char *ent_name;
+ enum v4l2_buf_type buf_type;
+ unsigned int caps;
+};
+
+static const struct node_description node_desc[PISPBE_NUM_NODES] = {
+ /* MAIN_INPUT_NODE */
+ {
+ .ent_name = PISPBE_NAME "-input",
+ .buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
+ .caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE,
+ },
+ /* TDN_INPUT_NODE */
+ {
+ .ent_name = PISPBE_NAME "-tdn_input",
+ .buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
+ .caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE,
+ },
+ /* STITCH_INPUT_NODE */
+ {
+ .ent_name = PISPBE_NAME "-stitch_input",
+ .buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
+ .caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE,
+ },
+ /* HOG_OUTPUT_NODE */
+ {
+ .ent_name = PISPBE_NAME "-hog_output",
+ .buf_type = V4L2_BUF_TYPE_META_CAPTURE,
+ .caps = V4L2_CAP_META_CAPTURE,
+ },
+ /* OUTPUT0_NODE */
+ {
+ .ent_name = PISPBE_NAME "-output0",
+ .buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
+ .caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE,
+ },
+ /* OUTPUT1_NODE */
+ {
+ .ent_name = PISPBE_NAME "-output1",
+ .buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
+ .caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE,
+ },
+ /* TDN_OUTPUT_NODE */
+ {
+ .ent_name = PISPBE_NAME "-tdn_output",
+ .buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
+ .caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE,
+ },
+ /* STITCH_OUTPUT_NODE */
+ {
+ .ent_name = PISPBE_NAME "-stitch_output",
+ .buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
+ .caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE,
+ },
+ /* CONFIG_NODE */
+ {
+ .ent_name = PISPBE_NAME "-config",
+ .buf_type = V4L2_BUF_TYPE_META_OUTPUT,
+ .caps = V4L2_CAP_META_OUTPUT,
+ }
+};
+
+#define NODE_DESC_IS_OUTPUT(desc) ( \
+ ((desc)->buf_type == V4L2_BUF_TYPE_META_OUTPUT) || \
+ ((desc)->buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT) || \
+ ((desc)->buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
+
+#define NODE_IS_META(node) ( \
+ ((node)->buf_type == V4L2_BUF_TYPE_META_OUTPUT) || \
+ ((node)->buf_type == V4L2_BUF_TYPE_META_CAPTURE))
+#define NODE_IS_OUTPUT(node) ( \
+ ((node)->buf_type == V4L2_BUF_TYPE_META_OUTPUT) || \
+ ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT) || \
+ ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
+#define NODE_IS_CAPTURE(node) ( \
+ ((node)->buf_type == V4L2_BUF_TYPE_META_CAPTURE) || \
+ ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) || \
+ ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
+#define NODE_IS_MPLANE(node) ( \
+ ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) || \
+ ((node)->buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
+
+/*
+ * Structure to describe a single node /dev/video<N> which represents a single
+ * input or output queue to the PiSP Back End device.
+ */
+struct pispbe_node {
+ unsigned int id;
+ int vfl_dir;
+ enum v4l2_buf_type buf_type;
+ struct video_device vfd;
+ struct media_pad pad;
+ struct media_intf_devnode *intf_devnode;
+ struct media_link *intf_link;
+ struct pispbe_node_group *node_group;
+ struct mutex node_lock;
+ struct mutex queue_lock;
+ spinlock_t ready_lock;
+ struct list_head ready_queue;
+ struct vb2_queue queue;
+ struct v4l2_format format;
+ const struct pisp_be_format *pisp_format;
+};
+
+/* For logging only, use the entity name with "pispbe" and separator removed */
+#define NODE_NAME(node) \
+ (node_desc[(node)->id].ent_name + sizeof(PISPBE_NAME))
+#define NODE_GET_V4L2(node) ((node)->node_group->v4l2_dev)
+
+/*
+ * Node group structure, which comprises all the input and output nodes that a
+ * single PiSP client will need, along with its own v4l2 and media devices.
+ */
+struct pispbe_node_group {
+ unsigned int id;
+ struct v4l2_device v4l2_dev;
+ struct v4l2_subdev sd;
+ struct pispbe_dev *pispbe;
+ struct media_device mdev;
+ struct pispbe_node node[PISPBE_NUM_NODES];
+ u32 streaming_map; /* bitmap of which nodes are streaming */
+ struct media_pad pad[PISPBE_NUM_NODES]; /* output pads first */
+ struct pisp_be_tiles_config *config;
+ dma_addr_t config_dma_addr;
+};
+
+/* Records details of the jobs currently running or queued on the h/w. */
+struct pispbe_job {
+ struct pispbe_node_group *node_group;
+ /*
+ * An array of buffer pointers - remember it's source buffers first,
+ * then captures, then metadata last.
+ */
+ struct pispbe_buffer *buf[PISPBE_NUM_NODES];
+};
+
+/*
+ * Structure representing the entire PiSP Back End device, comprising several
+ * node groups which share platform resources and a mutex for the actual HW.
+ */
+struct pispbe_dev {
+ struct device *dev;
+ struct pispbe_node_group node_group[PISPBE_NUM_NODE_GROUPS];
+ int hw_busy; /* non-zero if a job is queued or is being started */
+ struct pispbe_job queued_job, running_job;
+ void __iomem *be_reg_base;
+ struct clk *clk;
+ int irq;
+ u32 hw_version;
+ u8 done, started;
+ spinlock_t hw_lock; /* protects "hw_busy" flag and streaming_map */
+};
+
+static inline u32 read_reg(struct pispbe_dev *pispbe, unsigned int offset)
+{
+ return readl(pispbe->be_reg_base + offset);
+}
+
+static inline void write_reg(struct pispbe_dev *pispbe, unsigned int offset,
+ u32 val)
+{
+ writel(val, pispbe->be_reg_base + offset);
+}
+
+/* Check and initialize hardware. */
+static int hw_init(struct pispbe_dev *pispbe)
+{
+ u32 u;
+
+ /* Check the HW is present and has a known version */
+ u = read_reg(pispbe, PISP_BE_VERSION_OFFSET);
+ dev_info(pispbe->dev, "pispbe_probe: HW version: 0x%08x", u);
+ pispbe->hw_version = u;
+ if ((u & ~PISP_BE_VERSION_MINOR_BITS) != PISP_BE_VERSION_2712C1)
+ return -ENODEV;
+
+ /* Clear leftover interrupts */
+ write_reg(pispbe, PISP_BE_INTERRUPT_STATUS_OFFSET, 0xFFFFFFFFu);
+ u = read_reg(pispbe, PISP_BE_BATCH_STATUS_OFFSET);
+ dev_info(pispbe->dev, "pispbe_probe: BatchStatus: 0x%08x", u);
+ pispbe->done = (uint8_t)u;
+ pispbe->started = (uint8_t)(u >> 8);
+ u = read_reg(pispbe, PISP_BE_STATUS_OFFSET);
+ dev_info(pispbe->dev, "pispbe_probe: Status: 0x%08x", u);
+ if (u != 0 || pispbe->done != pispbe->started) {
+ dev_err(pispbe->dev, "pispbe_probe: HW is stuck or busy\n");
+ return -EBUSY;
+ }
+ /*
+ * AXI QOS=0, CACHE=4'b0010, PROT=3'b011
+ * Also set "chicken bits" 22:20 which enable sub-64-byte bursts
+ * and AXI AWID/BID variability (on versions which support this).
+ */
+ write_reg(pispbe, PISP_BE_AXI_OFFSET, 0x32703200u);
+
+ /* Enable both interrupt flags */
+ write_reg(pispbe, PISP_BE_INTERRUPT_EN_OFFSET, 0x00000003u);
+ return 0;
+}
+
+/*
+ * Queue a job to the h/w. If the h/w is idle it will begin immediately.
+ * Caller must ensure it is "safe to queue", i.e. we don't already have a
+ * queued, unstarted job.
+ */
+static void hw_queue_job(struct pispbe_dev *pispbe,
+ dma_addr_t hw_dma_addrs[N_HW_ADDRESSES],
+ u32 hw_enables[N_HW_ENABLES],
+ struct pisp_be_config *config, dma_addr_t tiles,
+ unsigned int num_tiles)
+{
+ unsigned int begin, end;
+ unsigned int u;
+
+ if (read_reg(pispbe, PISP_BE_STATUS_OFFSET) & 1)
+ dev_err(pispbe->dev, "ERROR: not safe to queue new job!\n");
+
+ /*
+ * Write configuration to hardware. DMA addresses and enable flags
+ * are passed separately, because the driver needs to sanitize them,
+ * and we don't want to modify (or be vulnerable to modifications of)
+ * the mmap'd buffer.
+ */
+ for (u = 0; u < N_HW_ADDRESSES; ++u) {
+ write_reg(pispbe, PISP_BE_IO_INPUT_ADDR0_LO_OFFSET + 8 * u,
+ (u32)(hw_dma_addrs[u]));
+ write_reg(pispbe, PISP_BE_IO_INPUT_ADDR0_LO_OFFSET + 8 * u + 4,
+ (u32)(hw_dma_addrs[u] >> 32));
+ }
+ write_reg(pispbe, PISP_BE_GLOBAL_BAYER_ENABLE_OFFSET, hw_enables[0]);
+ write_reg(pispbe, PISP_BE_GLOBAL_RGB_ENABLE_OFFSET, hw_enables[1]);
+
+ /*
+ * Everything else is as supplied by the user. XXX Buffer sizes not
+ * checked!
+ */
+ begin = offsetof(struct pisp_be_config, global.bayer_order) /
+ sizeof(u32);
+ end = offsetof(struct pisp_be_config, axi) / sizeof(u32);
+ for (u = begin; u < end; u++) {
+ unsigned int val = ((u32 *)config)[u];
+
+ write_reg(pispbe, PISP_BE_CONFIG_BASE_OFFSET + 4 * u, val);
+ }
+
+ /* Read back the addresses -- an error here could be fatal */
+ for (u = 0; u < N_HW_ADDRESSES; ++u) {
+ unsigned int offset = PISP_BE_IO_INPUT_ADDR0_LO_OFFSET + 8 * u;
+ u64 along = read_reg(pispbe, offset);
+
+ along += ((u64)read_reg(pispbe, offset + 4)) << 32;
+ if (along != (u64)(hw_dma_addrs[u])) {
+ dev_err(pispbe->dev,
+ "ISP BE config error: check if ISP RAMs enabled?\n");
+ return;
+ }
+ }
+
+ /*
+ * Write tile pointer to hardware. XXX Tile offsets and sizes not
+ * checked (and even if checked, the user could subsequently modify
+ * them)!
+ */
+ write_reg(pispbe, PISP_BE_TILE_ADDR_LO_OFFSET, (u32)tiles);
+ write_reg(pispbe, PISP_BE_TILE_ADDR_HI_OFFSET, (u32)(tiles >> 32));
+
+ /* Enqueue the job */
+ write_reg(pispbe, PISP_BE_CONTROL_OFFSET, 3 + 65536 * num_tiles);
+}
+
+struct pispbe_buffer {
+ struct vb2_v4l2_buffer vb;
+ struct list_head ready_list;
+ unsigned int config_index;
+};
+
+static int get_addr_3(dma_addr_t addr[3], struct pispbe_buffer *buf,
+ struct pispbe_node *node)
+{
+ unsigned int num_planes = node->format.fmt.pix_mp.num_planes;
+ unsigned int plane_factor = 0;
+ unsigned int size;
+ unsigned int p;
+
+ if (!buf || !node->pisp_format)
+ return 0;
+
+ WARN_ON(!NODE_IS_MPLANE(node));
+
+ /*
+ * Determine the base plane size. This will not be the same
+ * as node->format.fmt.pix_mp.plane_fmt[0].sizeimage for a single
+ * plane buffer in an mplane format.
+ */
+ size = node->format.fmt.pix_mp.plane_fmt[0].bytesperline *
+ node->format.fmt.pix_mp.height;
+
+ for (p = 0; p < num_planes && p < 3; p++) {
+ addr[p] = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, p);
+ plane_factor += node->pisp_format->plane_factor[p];
+ }
+
+ for (; p < MAX_PLANES && node->pisp_format->plane_factor[p]; p++) {
+ /*
+ * Calculate the address offset of this plane as needed
+ * by the hardware. This is specifically for non-mplane
+ * buffer formats, where there are 3 image planes, e.g.
+ * for the V4L2_PIX_FMT_YUV420 format.
+ */
+ addr[p] = addr[0] + ((size * plane_factor) >> 3);
+ plane_factor += node->pisp_format->plane_factor[p];
+ }
+
+ return num_planes;
+}
+
+static dma_addr_t get_addr(struct pispbe_buffer *buf)
+{
+ if (buf)
+ return vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
+ return 0;
+}
+
+static void
+fixup_addrs_enables(dma_addr_t addrs[N_HW_ADDRESSES],
+ u32 hw_enables[N_HW_ENABLES],
+ struct pisp_be_tiles_config *config,
+ struct pispbe_buffer *buf[PISPBE_NUM_NODES],
+ struct pispbe_node_group *node_group)
+{
+ int ret, i;
+
+ /* Take a copy of the "enable" bitmaps so we can modify them. */
+ hw_enables[0] = config->config.global.bayer_enables;
+ hw_enables[1] = config->config.global.rgb_enables;
+
+ /*
+ * Main input first. There are 3 address pointers, corresponding to up
+ * to 3 planes.
+ */
+ ret = get_addr_3(addrs, buf[MAIN_INPUT_NODE],
+ &node_group->node[MAIN_INPUT_NODE]);
+ if (ret <= 0) {
+ /*
+ * This shouldn't happen; pispbe_schedule_internal should insist
+ * on an input.
+ */
+ dev_warn(node_group->pispbe->dev,
+ "ISP-BE missing input\n");
+ hw_enables[0] = 0;
+ hw_enables[1] = 0;
+ return;
+ }
+
+ /*
+ * Now TDN/Stitch inputs and outputs. These are single-plane and only
+ * used with Bayer input. Input enables must match the requirements
+ * of the processing stages, otherwise the hardware can lock up!
+ */
+ if (hw_enables[0] & PISP_BE_BAYER_ENABLE_INPUT) {
+ addrs[3] = get_addr(buf[TDN_INPUT_NODE]);
+ if (addrs[3] == 0 ||
+ !(hw_enables[0] & PISP_BE_BAYER_ENABLE_TDN_INPUT) ||
+ !(hw_enables[0] & PISP_BE_BAYER_ENABLE_TDN) ||
+ (config->config.tdn.reset & 1)) {
+ hw_enables[0] &= ~(PISP_BE_BAYER_ENABLE_TDN_INPUT |
+ PISP_BE_BAYER_ENABLE_TDN_DECOMPRESS);
+ if (!(config->config.tdn.reset & 1))
+ hw_enables[0] &= ~PISP_BE_BAYER_ENABLE_TDN;
+ }
+
+ addrs[4] = get_addr(buf[STITCH_INPUT_NODE]);
+ if (addrs[4] == 0 ||
+ !(hw_enables[0] & PISP_BE_BAYER_ENABLE_STITCH_INPUT) ||
+ !(hw_enables[0] & PISP_BE_BAYER_ENABLE_STITCH)) {
+ hw_enables[0] &=
+ ~(PISP_BE_BAYER_ENABLE_STITCH_INPUT |
+ PISP_BE_BAYER_ENABLE_STITCH_DECOMPRESS |
+ PISP_BE_BAYER_ENABLE_STITCH);
+ }
+
+ addrs[5] = get_addr(buf[TDN_OUTPUT_NODE]);
+ if (addrs[5] == 0)
+ hw_enables[0] &= ~(PISP_BE_BAYER_ENABLE_TDN_COMPRESS |
+ PISP_BE_BAYER_ENABLE_TDN_OUTPUT);
+
+ addrs[6] = get_addr(buf[STITCH_OUTPUT_NODE]);
+ if (addrs[6] == 0)
+ hw_enables[0] &=
+ ~(PISP_BE_BAYER_ENABLE_STITCH_COMPRESS |
+ PISP_BE_BAYER_ENABLE_STITCH_OUTPUT);
+ } else {
+ /* No Bayer input? Disable entire Bayer pipe (else lockup) */
+ hw_enables[0] = 0;
+ }
+
+ /* Main image output channels. */
+ for (i = 0; i < PISP_BACK_END_NUM_OUTPUTS; i++) {
+ ret = get_addr_3(addrs + 7 + 3 * i, buf[OUTPUT0_NODE + i],
+ &node_group->node[OUTPUT0_NODE + i]);
+ if (ret <= 0)
+ hw_enables[1] &= ~(PISP_BE_RGB_ENABLE_OUTPUT0 << i);
+ }
+
+ /* HoG output (always single plane). */
+ addrs[13] = get_addr(buf[HOG_OUTPUT_NODE]);
+ if (addrs[13] == 0)
+ hw_enables[1] &= ~PISP_BE_RGB_ENABLE_HOG;
+}
+
+/*
+ * Internal function. Called from pispbe_schedule_one/all. Returns non-zero if
+ * we started a job.
+ *
+ * Warning: needs to be called with hw_lock taken, and releases it if it
+ * schedules a job.
+ */
+static int pispbe_schedule_internal(struct pispbe_node_group *node_group,
+ unsigned long flags)
+{
+ struct pisp_be_tiles_config *config_tiles_buffer;
+ struct pispbe_dev *pispbe = node_group->pispbe;
+ struct pispbe_buffer *buf[PISPBE_NUM_NODES];
+ dma_addr_t hw_dma_addrs[N_HW_ADDRESSES];
+ dma_addr_t tiles;
+ u32 hw_enables[N_HW_ENABLES];
+ struct pispbe_node *node;
+ unsigned long flags1;
+ unsigned int config_index;
+ int i;
+
+ /*
+ * To schedule a job, we need all streaming nodes (apart from Output0,
+ * Output1, Tdn and Stitch) to have a buffer ready, which must
+ * include at least a config buffer and a main input image.
+ *
+ * For Output0, Output1, Tdn and Stitch, a buffer only needs to be
+ * available if the blocks are enabled in the config.
+ *
+ * (Note that streaming_map is protected by hw_lock, which is held.)
+ */
+ if (((BIT(CONFIG_NODE) | BIT(MAIN_INPUT_NODE)) &
+ node_group->streaming_map) !=
+ (BIT(CONFIG_NODE) | BIT(MAIN_INPUT_NODE))) {
+ dev_dbg(pispbe->dev, "Nothing to do\n");
+ return 0;
+ }
+
+ node = &node_group->node[CONFIG_NODE];
+ spin_lock_irqsave(&node->ready_lock, flags1);
+ buf[CONFIG_NODE] =
+ list_first_entry_or_null(&node->ready_queue, struct pispbe_buffer,
+ ready_list);
+ spin_unlock_irqrestore(&node->ready_lock, flags1);
+
+ /* Exit early if no config buffer has been queued. */
+ if (!buf[CONFIG_NODE])
+ return 0;
+
+ config_index = buf[CONFIG_NODE]->vb.vb2_buf.index;
+ config_tiles_buffer = &node_group->config[config_index];
+ tiles = (dma_addr_t)node_group->config_dma_addr +
+ config_index * sizeof(struct pisp_be_tiles_config) +
+ offsetof(struct pisp_be_tiles_config, tiles);
+
+ /* remember: srcimages, captures then metadata */
+ for (i = 0; i < PISPBE_NUM_NODES; i++) {
+ unsigned int bayer_en =
+ config_tiles_buffer->config.global.bayer_enables;
+ unsigned int rgb_en =
+ config_tiles_buffer->config.global.rgb_enables;
+ bool ignore_buffers = false;
+
+ /* Config node is handled outside the loop above. */
+ if (i == CONFIG_NODE)
+ continue;
+
+ buf[i] = NULL;
+ if (!(node_group->streaming_map & BIT(i)))
+ continue;
+
+ if ((!(rgb_en & PISP_BE_RGB_ENABLE_OUTPUT0) &&
+ i == OUTPUT0_NODE) ||
+ (!(rgb_en & PISP_BE_RGB_ENABLE_OUTPUT1) &&
+ i == OUTPUT1_NODE) ||
+ (!(bayer_en & PISP_BE_BAYER_ENABLE_TDN_INPUT) &&
+ i == TDN_INPUT_NODE) ||
+ (!(bayer_en & PISP_BE_BAYER_ENABLE_TDN_OUTPUT) &&
+ i == TDN_OUTPUT_NODE) ||
+ (!(bayer_en & PISP_BE_BAYER_ENABLE_STITCH_INPUT) &&
+ i == STITCH_INPUT_NODE) ||
+ (!(bayer_en & PISP_BE_BAYER_ENABLE_STITCH_OUTPUT) &&
+ i == STITCH_OUTPUT_NODE)) {
+ /*
+ * Ignore Output0/Output1/Tdn/Stitch buffer check if the
+ * global enables aren't set for these blocks. If a
+ * buffer has been provided, we dequeue it back to the
+ * user with the other in-use buffers.
+ *
+ */
+ ignore_buffers = true;
+ }
+
+ node = &node_group->node[i];
+
+ spin_lock_irqsave(&node->ready_lock, flags1);
+ buf[i] = list_first_entry_or_null(&node->ready_queue,
+ struct pispbe_buffer,
+ ready_list);
+ spin_unlock_irqrestore(&node->ready_lock, flags1);
+ if (!buf[i] && !ignore_buffers) {
+ dev_dbg(pispbe->dev, "Nothing to do\n");
+ return 0;
+ }
+ }
+
+ /* Pull a buffer from each V4L2 queue to form the queued job */
+ for (i = 0; i < PISPBE_NUM_NODES; i++) {
+ if (buf[i]) {
+ node = &node_group->node[i];
+
+ spin_lock_irqsave(&node->ready_lock, flags1);
+ list_del(&buf[i]->ready_list);
+ spin_unlock_irqrestore(&node->ready_lock,
+ flags1);
+ }
+ pispbe->queued_job.buf[i] = buf[i];
+ }
+
+ pispbe->queued_job.node_group = node_group;
+ pispbe->hw_busy = 1;
+ spin_unlock_irqrestore(&pispbe->hw_lock, flags);
+
+ /*
+ * We can kick the job off without the hw_lock, as this can
+ * never run again until hw_busy is cleared, which will happen
+ * only when the following job has been queued.
+ */
+ dev_dbg(pispbe->dev, "Have buffers - starting hardware\n");
+
+ /* Convert buffers to DMA addresses for the hardware */
+ fixup_addrs_enables(hw_dma_addrs, hw_enables,
+ config_tiles_buffer, buf, node_group);
+ /*
+ * This could be a spot to fill in the
+ * buf[i]->vb.vb2_buf.planes[j].bytesused fields?
+ */
+ i = config_tiles_buffer->num_tiles;
+ if (i <= 0 || i > PISP_BACK_END_NUM_TILES ||
+ !((hw_enables[0] | hw_enables[1]) &
+ PISP_BE_BAYER_ENABLE_INPUT)) {
+ /*
+ * Bad job. We can't let it proceed as it could lock up
+ * the hardware, or worse!
+ *
+ * XXX How to deal with this most cleanly? For now, just
+ * force num_tiles to 0, which causes the H/W to do
+ * something bizarre but survivable. It increments
+ * (started,done) counters by more than 1, but we seem
+ * to survive...
+ */
+ dev_err(pispbe->dev, "PROBLEM: Bad job");
+ i = 0;
+ }
+ hw_queue_job(pispbe, hw_dma_addrs, hw_enables,
+ &config_tiles_buffer->config, tiles, i);
+
+ return 1;
+}
+
+/* Try and schedule a job for just a single node group. */
+static void pispbe_schedule_one(struct pispbe_node_group *node_group)
+{
+ struct pispbe_dev *pispbe = node_group->pispbe;
+ unsigned long flags;
+ int ret;
+
+ spin_lock_irqsave(&pispbe->hw_lock, flags);
+ if (pispbe->hw_busy) {
+ spin_unlock_irqrestore(&pispbe->hw_lock, flags);
+ return;
+ }
+
+ /* A non-zero return means the lock was released. */
+ ret = pispbe_schedule_internal(node_group, flags);
+ if (!ret)
+ spin_unlock_irqrestore(&pispbe->hw_lock, flags);
+}
+
+/* Try and schedule a job for any of the node groups. */
+static void pispbe_schedule_any(struct pispbe_dev *pispbe, int clear_hw_busy)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&pispbe->hw_lock, flags);
+
+ if (clear_hw_busy)
+ pispbe->hw_busy = 0;
+ if (pispbe->hw_busy == 0) {
+ unsigned int i;
+
+ for (i = 0; i < PISPBE_NUM_NODE_GROUPS; i++) {
+ /*
+ * A non-zero return from pispbe_schedule_internal means
+ * the lock was released.
+ */
+ if (pispbe_schedule_internal(&pispbe->node_group[i],
+ flags))
+ return;
+ }
+ }
+ spin_unlock_irqrestore(&pispbe->hw_lock, flags);
+}
+
+static void pispbe_isr_jobdone(struct pispbe_dev *pispbe,
+ struct pispbe_job *job)
+{
+ struct pispbe_buffer **buf = job->buf;
+ u64 ts = ktime_get_ns();
+ int i;
+
+ for (i = 0; i < PISPBE_NUM_NODES; i++) {
+ if (buf[i]) {
+ buf[i]->vb.vb2_buf.timestamp = ts;
+ vb2_buffer_done(&buf[i]->vb.vb2_buf,
+ VB2_BUF_STATE_DONE);
+ }
+ }
+}
+
+static irqreturn_t pispbe_isr(int irq, void *dev)
+{
+ struct pispbe_dev *pispbe = (struct pispbe_dev *)dev;
+ u8 started, done;
+ int can_queue_another = 0;
+ u32 u;
+
+ u = read_reg(pispbe, PISP_BE_INTERRUPT_STATUS_OFFSET);
+ if (u == 0)
+ return IRQ_NONE;
+
+ write_reg(pispbe, PISP_BE_INTERRUPT_STATUS_OFFSET, u);
+ dev_dbg(pispbe->dev, "Hardware interrupt\n");
+ u = read_reg(pispbe, PISP_BE_BATCH_STATUS_OFFSET);
+ done = (uint8_t)u;
+ started = (uint8_t)(u >> 8);
+ dev_dbg(pispbe->dev,
+ "H/W started %d done %d, previously started %d done %d\n",
+ (int)started, (int)done, (int)pispbe->started,
+ (int)pispbe->done);
+
+ /*
+ * Be aware that done can go up by 2 and started by 1 when: a job that
+ * we previously saw "start" now finishes, and we then queued a new job
+ * which we see both start and finish "simultaneously".
+ */
+ if (pispbe->running_job.node_group && pispbe->done != done) {
+ pispbe_isr_jobdone(pispbe, &pispbe->running_job);
+ memset(&pispbe->running_job, 0, sizeof(pispbe->running_job));
+ pispbe->done++;
+ dev_dbg(pispbe->dev, "Job done (1)\n");
+ }
+
+ if (pispbe->started != started) {
+ pispbe->started++;
+ can_queue_another = 1;
+ dev_dbg(pispbe->dev, "Job started\n");
+
+ if (pispbe->done != done && pispbe->queued_job.node_group) {
+ pispbe_isr_jobdone(pispbe, &pispbe->queued_job);
+ pispbe->done++;
+ dev_dbg(pispbe->dev, "Job done (2)\n");
+ } else {
+ pispbe->running_job = pispbe->queued_job;
+ }
+
+ memset(&pispbe->queued_job, 0, sizeof(pispbe->queued_job));
+ }
+
+ if (pispbe->done != done || pispbe->started != started) {
+ dev_err(pispbe->dev, "PROBLEM: counters not matching!\n");
+ pispbe->started = started;
+ pispbe->done = done;
+ }
+
+ /* check if there's more to do before going to sleep */
+ pispbe_schedule_any(pispbe, can_queue_another);
+
+ return IRQ_HANDLED;
+}
+
+static int pisp_be_validate_config(struct pispbe_node_group *node_group,
+ struct pisp_be_tiles_config *config)
+{
+ u32 bayer_enables = config->config.global.bayer_enables;
+ u32 rgb_enables = config->config.global.rgb_enables;
+ struct device *dev = node_group->pispbe->dev;
+ struct v4l2_format *fmt;
+ unsigned int bpl, size, i, j;
+
+ if (!(bayer_enables & PISP_BE_BAYER_ENABLE_INPUT) ==
+ !(rgb_enables & PISP_BE_RGB_ENABLE_INPUT)) {
+ dev_err(dev, "%s: Not one input enabled\n", __func__);
+ return -EIO;
+ }
+
+ /* Ensure output config strides and buffer sizes match the V4L2 formats. */
+ fmt = &node_group->node[TDN_OUTPUT_NODE].format;
+ if (bayer_enables & PISP_BE_BAYER_ENABLE_TDN_OUTPUT) {
+ bpl = config->config.tdn_output_format.stride;
+ size = bpl * config->config.tdn_output_format.height;
+ if (fmt->fmt.pix_mp.plane_fmt[0].bytesperline < bpl) {
+ dev_err(dev, "%s: bpl mismatch on tdn_output\n",
+ __func__);
+ return -EINVAL;
+ }
+ if (fmt->fmt.pix_mp.plane_fmt[0].sizeimage < size) {
+ dev_err(dev, "%s: size mismatch on tdn_output\n",
+ __func__);
+ return -EINVAL;
+ }
+ }
+
+ fmt = &node_group->node[STITCH_OUTPUT_NODE].format;
+ if (bayer_enables & PISP_BE_BAYER_ENABLE_STITCH_OUTPUT) {
+ bpl = config->config.stitch_output_format.stride;
+ size = bpl * config->config.stitch_output_format.height;
+ if (fmt->fmt.pix_mp.plane_fmt[0].bytesperline < bpl) {
+ dev_err(dev, "%s: bpl mismatch on stitch_output\n",
+ __func__);
+ return -EINVAL;
+ }
+ if (fmt->fmt.pix_mp.plane_fmt[0].sizeimage < size) {
+ dev_err(dev, "%s: size mismatch on stitch_output\n",
+ __func__);
+ return -EINVAL;
+ }
+ }
+
+ for (j = 0; j < PISP_BACK_END_NUM_OUTPUTS; j++) {
+ if (!(rgb_enables & PISP_BE_RGB_ENABLE_OUTPUT(j)))
+ continue;
+ if (config->config.output_format[j].image.format &
+ PISP_IMAGE_FORMAT_WALLPAPER_ROLL)
+ continue; /* TODO: Size checks for wallpaper formats */
+
+ fmt = &node_group->node[OUTPUT0_NODE + j].format;
+ for (i = 0; i < fmt->fmt.pix_mp.num_planes; i++) {
+ bpl = !i ? config->config.output_format[j].image.stride
+ : config->config.output_format[j].image.stride2;
+ size = bpl * config->config.output_format[j].image.height;
+
+ if (config->config.output_format[j].image.format &
+ PISP_IMAGE_FORMAT_SAMPLING_420)
+ size >>= 1;
+ if (fmt->fmt.pix_mp.plane_fmt[i].bytesperline < bpl) {
+ dev_err(dev, "%s: bpl mismatch on output %d\n",
+ __func__, j);
+ return -EINVAL;
+ }
+ if (fmt->fmt.pix_mp.plane_fmt[i].sizeimage < size) {
+ dev_err(dev, "%s: size mismatch on output\n",
+ __func__);
+ return -EINVAL;
+ }
+ }
+ }
+
+ return 0;
+}
+
+static int pispbe_node_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
+ unsigned int *nplanes, unsigned int sizes[],
+ struct device *alloc_devs[])
+{
+ struct pispbe_node *node = vb2_get_drv_priv(q);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+
+ *nplanes = 1;
+ if (NODE_IS_MPLANE(node)) {
+ unsigned int i;
+
+ *nplanes = node->format.fmt.pix_mp.num_planes;
+ for (i = 0; i < *nplanes; i++) {
+ unsigned int size =
+ node->format.fmt.pix_mp.plane_fmt[i].sizeimage;
+ if (sizes[i] && sizes[i] < size) {
+ dev_err(pispbe->dev, "%s: size %u < %u\n",
+ __func__, sizes[i], size);
+ return -EINVAL;
+ }
+ sizes[i] = size;
+ }
+ } else if (NODE_IS_META(node)) {
+ sizes[0] = node->format.fmt.meta.buffersize;
+ /*
+ * Limit the config node buffer count to the number of internal
+ * buffers allocated.
+ */
+ if (node->id == CONFIG_NODE)
+ *nbuffers = min_t(unsigned int, *nbuffers,
+ PISP_BE_NUM_CONFIG_BUFFERS);
+ }
+
+ dev_dbg(pispbe->dev,
+ "Image (or metadata) size %u, nbuffers %u for node %s\n",
+ sizes[0], *nbuffers, NODE_NAME(node));
+
+ return 0;
+}
+
+static int pispbe_node_buffer_prepare(struct vb2_buffer *vb)
+{
+ struct pispbe_node *node = vb2_get_drv_priv(vb->vb2_queue);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+ unsigned long size = 0;
+ unsigned int num_planes = NODE_IS_MPLANE(node) ?
+ node->format.fmt.pix_mp.num_planes : 1;
+ unsigned int i;
+
+ for (i = 0; i < num_planes; i++) {
+ size = NODE_IS_MPLANE(node)
+ ? node->format.fmt.pix_mp.plane_fmt[i].sizeimage
+ : node->format.fmt.meta.buffersize;
+
+ if (vb2_plane_size(vb, i) < size) {
+ dev_err(pispbe->dev,
+ "data will not fit into plane %d (%lu < %lu)\n",
+ i, vb2_plane_size(vb, i), size);
+ return -EINVAL;
+ }
+
+ vb2_set_plane_payload(vb, i, size);
+ }
+
+ if (node->id == CONFIG_NODE) {
+ void *dst = &node->node_group->config[vb->index];
+ void *src = vb2_plane_vaddr(vb, 0);
+
+ memcpy(dst, src, sizeof(struct pisp_be_tiles_config));
+ return pisp_be_validate_config(node->node_group, dst);
+ }
+
+ return 0;
+}
+
+static void pispbe_node_buffer_queue(struct vb2_buffer *buf)
+{
+ struct vb2_v4l2_buffer *vbuf =
+ container_of(buf, struct vb2_v4l2_buffer, vb2_buf);
+ struct pispbe_buffer *buffer =
+ container_of(vbuf, struct pispbe_buffer, vb);
+ struct pispbe_node *node = vb2_get_drv_priv(buf->vb2_queue);
+ struct pispbe_node_group *node_group = node->node_group;
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+ unsigned long flags;
+
+ dev_dbg(pispbe->dev, "%s: for node %s\n", __func__, NODE_NAME(node));
+ spin_lock_irqsave(&node->ready_lock, flags);
+ list_add_tail(&buffer->ready_list, &node->ready_queue);
+ spin_unlock_irqrestore(&node->ready_lock, flags);
+
+ /*
+ * Every time we add a buffer, check if there's now some work for the hw
+ * to do, but only for this client.
+ */
+ pispbe_schedule_one(node_group);
+}
+
+static int pispbe_node_start_streaming(struct vb2_queue *q, unsigned int count)
+{
+ unsigned long flags;
+ struct pispbe_node *node = vb2_get_drv_priv(q);
+ struct pispbe_node_group *node_group = node->node_group;
+ struct pispbe_dev *pispbe = node_group->pispbe;
+ int ret;
+
+ ret = pm_runtime_resume_and_get(pispbe->dev);
+ if (ret < 0)
+ return ret;
+
+ spin_lock_irqsave(&pispbe->hw_lock, flags);
+ node->node_group->streaming_map |= BIT(node->id);
+ spin_unlock_irqrestore(&pispbe->hw_lock, flags);
+
+ dev_dbg(pispbe->dev, "%s: for node %s (count %u)\n",
+ __func__, NODE_NAME(node), count);
+ dev_dbg(pispbe->dev, "Nodes streaming for this group now 0x%x\n",
+ node->node_group->streaming_map);
+
+ /* Maybe we're ready to run. */
+ pispbe_schedule_one(node_group);
+
+ return 0;
+}
+
+static void pispbe_node_stop_streaming(struct vb2_queue *q)
+{
+ struct pispbe_node *node = vb2_get_drv_priv(q);
+ struct pispbe_node_group *node_group = node->node_group;
+ struct pispbe_dev *pispbe = node_group->pispbe;
+ struct pispbe_buffer *buf;
+ unsigned long flags;
+
+ /*
+ * Now this is a bit awkward. In a simple M2M device we could just wait
+ * for all queued jobs to complete, but here there's a risk that a
+ * partial set of buffers was queued and cannot be run. For now, just
+ * cancel all buffers stuck in the "ready queue", then wait for any
+ * running job.
+ * XXX This may return buffers out of order.
+ */
+ dev_dbg(pispbe->dev, "%s: for node %s\n", __func__, NODE_NAME(node));
+ spin_lock_irqsave(&pispbe->hw_lock, flags);
+ do {
+ unsigned long flags1;
+
+ spin_lock_irqsave(&node->ready_lock, flags1);
+ buf = list_first_entry_or_null(&node->ready_queue,
+ struct pispbe_buffer,
+ ready_list);
+ if (buf) {
+ list_del(&buf->ready_list);
+ vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
+ }
+ spin_unlock_irqrestore(&node->ready_lock, flags1);
+ } while (buf);
+ spin_unlock_irqrestore(&pispbe->hw_lock, flags);
+
+ vb2_wait_for_all_buffers(&node->queue);
+
+ spin_lock_irqsave(&pispbe->hw_lock, flags);
+ node_group->streaming_map &= ~BIT(node->id);
+ spin_unlock_irqrestore(&pispbe->hw_lock, flags);
+
+ pm_runtime_mark_last_busy(pispbe->dev);
+ pm_runtime_put_autosuspend(pispbe->dev);
+
+ dev_dbg(pispbe->dev, "Nodes streaming for this group now 0x%x\n",
+ node_group->streaming_map);
+}
+
+static const struct vb2_ops pispbe_node_queue_ops = {
+ .queue_setup = pispbe_node_queue_setup,
+ .buf_prepare = pispbe_node_buffer_prepare,
+ .buf_queue = pispbe_node_buffer_queue,
+ .start_streaming = pispbe_node_start_streaming,
+ .stop_streaming = pispbe_node_stop_streaming,
+};
+
+static const struct v4l2_file_operations pispbe_fops = {
+ .owner = THIS_MODULE,
+ .open = v4l2_fh_open,
+ .release = vb2_fop_release,
+ .poll = vb2_fop_poll,
+ .unlocked_ioctl = video_ioctl2,
+ .mmap = vb2_fop_mmap
+};
+
+static int pispbe_node_querycap(struct file *file, void *priv,
+ struct v4l2_capability *cap)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+
+ strscpy(cap->driver, PISPBE_NAME, sizeof(cap->driver));
+ strscpy(cap->card, PISPBE_NAME, sizeof(cap->card));
+ snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
+ dev_name(pispbe->dev));
+
+ cap->capabilities = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
+ V4L2_CAP_VIDEO_OUTPUT_MPLANE |
+ V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS |
+ V4L2_CAP_META_OUTPUT | V4L2_CAP_META_CAPTURE;
+ cap->device_caps = node->vfd.device_caps;
+
+ dev_dbg(pispbe->dev, "Caps for node %s: %x and %x (dev %x)\n",
+ NODE_NAME(node), cap->capabilities, cap->device_caps,
+ node->vfd.device_caps);
+ return 0;
+}
+
+static int pispbe_node_g_fmt_vid_cap(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+
+ if (!NODE_IS_CAPTURE(node) || NODE_IS_META(node)) {
+ dev_err(pispbe->dev,
+ "Cannot get capture fmt for output node %s\n",
+ NODE_NAME(node));
+ return -EINVAL;
+ }
+ *f = node->format;
+ dev_dbg(pispbe->dev, "Get capture format for node %s\n",
+ NODE_NAME(node));
+ return 0;
+}
+
+static int pispbe_node_g_fmt_vid_out(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+
+ if (NODE_IS_CAPTURE(node) || NODE_IS_META(node)) {
+ dev_err(pispbe->dev,
+ "Cannot get capture fmt for output node %s\n",
+ NODE_NAME(node));
+ return -EINVAL;
+ }
+ *f = node->format;
+ dev_dbg(pispbe->dev, "Get output format for node %s\n",
+ NODE_NAME(node));
+ return 0;
+}
+
+static int pispbe_node_g_fmt_meta_out(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+
+ if (!NODE_IS_META(node) || NODE_IS_CAPTURE(node)) {
+ dev_err(pispbe->dev,
+ "Cannot get capture fmt for meta output node %s\n",
+ NODE_NAME(node));
+ return -EINVAL;
+ }
+ *f = node->format;
+ dev_dbg(pispbe->dev, "Get output format for meta node %s\n",
+ NODE_NAME(node));
+ return 0;
+}
+
+static int pispbe_node_g_fmt_meta_cap(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+
+ if (!NODE_IS_META(node) || NODE_IS_OUTPUT(node)) {
+ dev_err(pispbe->dev,
+ "Cannot get capture fmt for meta output node %s\n",
+ NODE_NAME(node));
+ return -EINVAL;
+ }
+ *f = node->format;
+ dev_dbg(pispbe->dev, "Get output format for meta node %s\n",
+ NODE_NAME(node));
+ return 0;
+}
+
+static int verify_be_pix_format(const struct v4l2_format *f,
+ struct pispbe_node *node)
+{
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+ unsigned int nplanes = f->fmt.pix_mp.num_planes;
+ unsigned int i;
+
+ if (f->fmt.pix_mp.width == 0 || f->fmt.pix_mp.height == 0) {
+ dev_err(pispbe->dev, "Details incorrect for output node %s\n",
+ NODE_NAME(node));
+ return -EINVAL;
+ }
+
+ if (nplanes == 0 || nplanes > MAX_PLANES) {
+ dev_err(pispbe->dev,
+ "Bad number of planes for output node %s, req =%d\n",
+ NODE_NAME(node), nplanes);
+ return -EINVAL;
+ }
+
+ for (i = 0; i < nplanes; i++) {
+ const struct v4l2_plane_pix_format *p;
+
+ p = &f->fmt.pix_mp.plane_fmt[i];
+ if (p->bytesperline == 0 || p->sizeimage == 0) {
+ dev_err(pispbe->dev,
+ "Invalid plane %d for output node %s\n",
+ i, NODE_NAME(node));
+ return -EINVAL;
+ }
+ }
+
+ return 0;
+}
+
+static const struct pisp_be_format *find_format(unsigned int fourcc)
+{
+ const struct pisp_be_format *fmt;
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
+ fmt = &supported_formats[i];
+ if (fmt->fourcc == fourcc)
+ return fmt;
+ }
+
+ return NULL;
+}
+
+static void set_plane_params(struct v4l2_format *f,
+ const struct pisp_be_format *fmt)
+{
+ unsigned int nplanes = f->fmt.pix_mp.num_planes;
+ unsigned int total_plane_factor = 0;
+ unsigned int i;
+
+ for (i = 0; i < MAX_PLANES; i++)
+ total_plane_factor += fmt->plane_factor[i];
+
+ for (i = 0; i < nplanes; i++) {
+ struct v4l2_plane_pix_format *p = &f->fmt.pix_mp.plane_fmt[i];
+ unsigned int bpl, plane_size;
+
+ bpl = (f->fmt.pix_mp.width * fmt->bit_depth) >> 3;
+ bpl = ALIGN(max(p->bytesperline, bpl), fmt->align);
+
+ plane_size = bpl * f->fmt.pix_mp.height *
+ (nplanes > 1 ? fmt->plane_factor[i] : total_plane_factor);
+ /*
+ * The shift is to divide out the plane_factor fixed point
+ * scaling of 8.
+ */
+ plane_size = max(p->sizeimage, plane_size >> 3);
+
+ p->bytesperline = bpl;
+ p->sizeimage = plane_size;
+ }
+}
+
+static int try_format(struct v4l2_format *f, struct pispbe_node *node)
+{
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+ const struct pisp_be_format *fmt;
+ unsigned int i;
+ bool is_rgb;
+ u32 pixfmt = f->fmt.pix_mp.pixelformat;
+
+ dev_dbg(pispbe->dev,
+ "%s: [%s] req %ux%u " V4L2_FOURCC_CONV ", planes %d\n",
+ __func__, NODE_NAME(node), f->fmt.pix_mp.width,
+ f->fmt.pix_mp.height, V4L2_FOURCC_CONV_ARGS(pixfmt),
+ f->fmt.pix_mp.num_planes);
+
+ if (pixfmt == V4L2_PIX_FMT_RPI_BE)
+ return verify_be_pix_format(f, node);
+
+ fmt = find_format(pixfmt);
+ if (!fmt)
+ fmt = find_format(V4L2_PIX_FMT_YUV420M);
+
+ f->fmt.pix_mp.pixelformat = fmt->fourcc;
+ f->fmt.pix_mp.num_planes = fmt->num_planes;
+ f->fmt.pix_mp.field = V4L2_FIELD_NONE;
+ f->fmt.pix_mp.width = max(min(f->fmt.pix_mp.width, 65536u),
+ PISP_BACK_END_MIN_TILE_WIDTH);
+ f->fmt.pix_mp.height = max(min(f->fmt.pix_mp.height, 65536u),
+ PISP_BACK_END_MIN_TILE_HEIGHT);
+
+ /*
+ * Fill in the actual colour space when the requested one was
+ * not supported. This also catches the case when the "default"
+ * colour space was requested (as that's never in the mask).
+ */
+ if (!(V4L2_COLORSPACE_MASK(f->fmt.pix_mp.colorspace) & fmt->colorspace_mask))
+ f->fmt.pix_mp.colorspace = fmt->colorspace_default;
+
+ /* In all cases, we only support the defaults for these: */
+ f->fmt.pix_mp.ycbcr_enc =
+ V4L2_MAP_YCBCR_ENC_DEFAULT(f->fmt.pix_mp.colorspace);
+ f->fmt.pix_mp.xfer_func =
+ V4L2_MAP_XFER_FUNC_DEFAULT(f->fmt.pix_mp.colorspace);
+
+ is_rgb = f->fmt.pix_mp.colorspace == V4L2_COLORSPACE_SRGB;
+ f->fmt.pix_mp.quantization =
+ V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb, f->fmt.pix_mp.colorspace,
+ f->fmt.pix_mp.ycbcr_enc);
+
+ /* Set plane size and bytes/line for each plane. */
+ set_plane_params(f, fmt);
+
+ for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
+ dev_dbg(pispbe->dev,
+ "%s: [%s] calc plane %d, %ux%u, depth %u, bpl %u size %u\n",
+ __func__, NODE_NAME(node), i, f->fmt.pix_mp.width,
+ f->fmt.pix_mp.height, fmt->bit_depth,
+ f->fmt.pix_mp.plane_fmt[i].bytesperline,
+ f->fmt.pix_mp.plane_fmt[i].sizeimage);
+ }
+
+ return 0;
+}
+
+static int pispbe_node_try_fmt_vid_cap(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+ int ret;
+
+ if (!NODE_IS_CAPTURE(node) || NODE_IS_META(node)) {
+ dev_err(pispbe->dev,
+ "Cannot set capture fmt for output node %s\n",
+ NODE_NAME(node));
+ return -EINVAL;
+ }
+
+ ret = try_format(f, node);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+static int pispbe_node_try_fmt_vid_out(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+ int ret;
+
+ if (!NODE_IS_OUTPUT(node) || NODE_IS_META(node)) {
+ dev_err(pispbe->dev,
+ "Cannot set capture fmt for output node %s\n",
+ NODE_NAME(node));
+ return -EINVAL;
+ }
+
+ ret = try_format(f, node);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+static int pispbe_node_try_fmt_meta_out(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+
+ if (!NODE_IS_META(node) || NODE_IS_CAPTURE(node)) {
+ dev_err(pispbe->dev,
+ "Cannot set capture fmt for meta output node %s\n",
+ NODE_NAME(node));
+ return -EINVAL;
+ }
+
+ f->fmt.meta.dataformat = V4L2_META_FMT_RPI_BE_CFG;
+ f->fmt.meta.buffersize = sizeof(struct pisp_be_tiles_config);
+
+ return 0;
+}
+
+static int pispbe_node_try_fmt_meta_cap(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+
+ if (!NODE_IS_META(node) || NODE_IS_OUTPUT(node)) {
+ dev_err(pispbe->dev,
+ "Cannot set capture fmt for meta output node %s\n",
+ NODE_NAME(node));
+ return -EINVAL;
+ }
+
+ f->fmt.meta.dataformat = V4L2_PIX_FMT_RPI_BE;
+ if (!f->fmt.meta.buffersize)
+ f->fmt.meta.buffersize = BIT(20);
+
+ return 0;
+}
+
+static int pispbe_node_s_fmt_vid_cap(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+ int ret = pispbe_node_try_fmt_vid_cap(file, priv, f);
+
+ if (ret < 0)
+ return ret;
+
+ node->format = *f;
+ node->pisp_format = find_format(f->fmt.pix_mp.pixelformat);
+
+ dev_dbg(pispbe->dev,
+ "Set capture format for node %s to " V4L2_FOURCC_CONV "\n",
+ NODE_NAME(node),
+ V4L2_FOURCC_CONV_ARGS(f->fmt.pix_mp.pixelformat));
+ return 0;
+}
+
+static int pispbe_node_s_fmt_vid_out(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+ int ret = pispbe_node_try_fmt_vid_out(file, priv, f);
+
+ if (ret < 0)
+ return ret;
+
+ node->format = *f;
+ node->pisp_format = find_format(f->fmt.pix_mp.pixelformat);
+
+ dev_dbg(pispbe->dev,
+ "Set output format for node %s to " V4L2_FOURCC_CONV "\n",
+ NODE_NAME(node),
+ V4L2_FOURCC_CONV_ARGS(f->fmt.pix_mp.pixelformat));
+ return 0;
+}
+
+static int pispbe_node_s_fmt_meta_out(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+ int ret = pispbe_node_try_fmt_meta_out(file, priv, f);
+
+ if (ret < 0)
+ return ret;
+
+ node->format = *f;
+ node->pisp_format = &meta_out_supported_formats[0];
+
+ dev_dbg(pispbe->dev,
+ "Set output format for meta node %s to " V4L2_FOURCC_CONV "\n",
+ NODE_NAME(node),
+ V4L2_FOURCC_CONV_ARGS(f->fmt.meta.dataformat));
+ return 0;
+}
+
+static int pispbe_node_s_fmt_meta_cap(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+ int ret = pispbe_node_try_fmt_meta_cap(file, priv, f);
+
+ if (ret < 0)
+ return ret;
+
+ node->format = *f;
+ node->pisp_format = find_format(f->fmt.meta.dataformat);
+
+ dev_dbg(pispbe->dev,
+ "Set capture format for meta node %s to " V4L2_FOURCC_CONV "\n",
+ NODE_NAME(node),
+ V4L2_FOURCC_CONV_ARGS(f->fmt.meta.dataformat));
+ return 0;
+}
+
+static int pispbe_node_enum_fmt(struct file *file, void *priv,
+ struct v4l2_fmtdesc *f)
+{
+ struct pispbe_node *node = video_drvdata(file);
+
+ if (f->type != node->queue.type)
+ return -EINVAL;
+
+ if (NODE_IS_META(node)) {
+ if (f->index)
+ return -EINVAL;
+
+ if (NODE_IS_OUTPUT(node))
+ f->pixelformat = V4L2_META_FMT_RPI_BE_CFG;
+ else
+ f->pixelformat = V4L2_PIX_FMT_RPI_BE;
+ f->flags = 0;
+ return 0;
+ }
+
+ if (f->index >= ARRAY_SIZE(supported_formats))
+ return -EINVAL;
+
+ f->pixelformat = supported_formats[f->index].fourcc;
+ f->flags = 0;
+
+ return 0;
+}
+
+static int pispbe_enum_framesizes(struct file *file, void *priv,
+ struct v4l2_frmsizeenum *fsize)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+
+ if (NODE_IS_META(node) || fsize->index)
+ return -EINVAL;
+
+ if (!find_format(fsize->pixel_format)) {
+ dev_err(pispbe->dev, "Invalid pixel code: %x\n",
+ fsize->pixel_format);
+ return -EINVAL;
+ }
+
+ fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
+ fsize->stepwise.min_width = 32;
+ fsize->stepwise.max_width = 65535;
+ fsize->stepwise.step_width = 2;
+
+ fsize->stepwise.min_height = 32;
+ fsize->stepwise.max_height = 65535;
+ fsize->stepwise.step_height = 2;
+
+ return 0;
+}
+
+static int pispbe_node_streamon(struct file *file, void *priv,
+ enum v4l2_buf_type type)
+{
+ struct pispbe_node *node = video_drvdata(file);
+ struct pispbe_dev *pispbe = node->node_group->pispbe;
+
+ /* Do we need a node->stream_lock mutex? */
+
+ dev_dbg(pispbe->dev, "Stream on for node %s\n", NODE_NAME(node));
+
+ /* Do we care about the type? Each node has only one queue. */
+
+ INIT_LIST_HEAD(&node->ready_queue);
+
+ /* locking should be handled by the queue->lock? */
+ return vb2_streamon(&node->queue, type);
+}
+
+static int pispbe_node_streamoff(struct file *file, void *priv,
+ enum v4l2_buf_type type)
+{
+ struct pispbe_node *node = video_drvdata(file);
+
+ return vb2_streamoff(&node->queue, type);
+}
+
+static const struct v4l2_ioctl_ops pispbe_node_ioctl_ops = {
+ .vidioc_querycap = pispbe_node_querycap,
+ .vidioc_g_fmt_vid_cap_mplane = pispbe_node_g_fmt_vid_cap,
+ .vidioc_g_fmt_vid_out_mplane = pispbe_node_g_fmt_vid_out,
+ .vidioc_g_fmt_meta_out = pispbe_node_g_fmt_meta_out,
+ .vidioc_g_fmt_meta_cap = pispbe_node_g_fmt_meta_cap,
+ .vidioc_try_fmt_vid_cap_mplane = pispbe_node_try_fmt_vid_cap,
+ .vidioc_try_fmt_vid_out_mplane = pispbe_node_try_fmt_vid_out,
+ .vidioc_try_fmt_meta_out = pispbe_node_try_fmt_meta_out,
+ .vidioc_try_fmt_meta_cap = pispbe_node_try_fmt_meta_cap,
+ .vidioc_s_fmt_vid_cap_mplane = pispbe_node_s_fmt_vid_cap,
+ .vidioc_s_fmt_vid_out_mplane = pispbe_node_s_fmt_vid_out,
+ .vidioc_s_fmt_meta_out = pispbe_node_s_fmt_meta_out,
+ .vidioc_s_fmt_meta_cap = pispbe_node_s_fmt_meta_cap,
+ .vidioc_enum_fmt_vid_cap = pispbe_node_enum_fmt,
+ .vidioc_enum_fmt_vid_out = pispbe_node_enum_fmt,
+ .vidioc_enum_fmt_meta_cap = pispbe_node_enum_fmt,
+ .vidioc_enum_fmt_meta_out = pispbe_node_enum_fmt,
+ .vidioc_enum_framesizes = pispbe_enum_framesizes,
+ .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_reqbufs = vb2_ioctl_reqbufs,
+ .vidioc_streamon = pispbe_node_streamon,
+ .vidioc_streamoff = pispbe_node_streamoff,
+};
+
+static const struct video_device pispbe_videodev = {
+ .name = PISPBE_NAME,
+ .vfl_dir = VFL_DIR_M2M, /* gets overwritten */
+ .fops = &pispbe_fops,
+ .ioctl_ops = &pispbe_node_ioctl_ops,
+ .minor = -1,
+ .release = video_device_release_empty,
+};
+
+static void node_set_default_format(struct pispbe_node *node)
+{
+ if (NODE_IS_META(node) && NODE_IS_OUTPUT(node)) {
+ /* Config node */
+ struct v4l2_format *f = &node->format;
+
+ f->fmt.meta.dataformat = V4L2_META_FMT_RPI_BE_CFG;
+ f->fmt.meta.buffersize = sizeof(struct pisp_be_tiles_config);
+ f->type = node->buf_type;
+ } else if (NODE_IS_META(node) && NODE_IS_CAPTURE(node)) {
+ /* HOG output node */
+ struct v4l2_format *f = &node->format;
+
+ f->fmt.meta.dataformat = V4L2_PIX_FMT_RPI_BE;
+ f->fmt.meta.buffersize = BIT(20);
+ f->type = node->buf_type;
+ } else {
+ struct v4l2_format f = {0};
+
+ f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_YUV420M;
+ f.fmt.pix_mp.width = 1920;
+ f.fmt.pix_mp.height = 1080;
+ f.type = node->buf_type;
+ try_format(&f, node);
+ node->format = f;
+ }
+
+ node->pisp_format = find_format(node->format.fmt.pix_mp.pixelformat);
+}
+
+/*
+ * Initialise a struct pispbe_node and register it as /dev/video<N>
+ * to represent one of the PiSP Back End's input or output streams.
+ */
+static int
+pispbe_init_node(struct pispbe_node_group *node_group, unsigned int id)
+{
+ bool output = NODE_DESC_IS_OUTPUT(&node_desc[id]);
+ struct pispbe_node *node = &node_group->node[id];
+ struct pispbe_dev *pispbe = node_group->pispbe;
+ struct media_entity *entity = &node->vfd.entity;
+ struct video_device *vdev = &node->vfd;
+ struct vb2_queue *q = &node->queue;
+ int ret;
+
+ node->id = id;
+ node->node_group = node_group;
+ node->buf_type = node_desc[id].buf_type;
+
+ mutex_init(&node->node_lock);
+ mutex_init(&node->queue_lock);
+ INIT_LIST_HEAD(&node->ready_queue);
+ spin_lock_init(&node->ready_lock);
+
+ node->format.type = node->buf_type;
+ node_set_default_format(node);
+
+ q->type = node->buf_type;
+ q->io_modes = VB2_MMAP | VB2_DMABUF;
+ q->mem_ops = &vb2_dma_contig_memops;
+ q->drv_priv = node;
+ q->ops = &pispbe_node_queue_ops;
+ q->buf_struct_size = sizeof(struct pispbe_buffer);
+ q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+ q->dev = node->node_group->pispbe->dev;
+ /* get V4L2 to handle node->queue locking */
+ q->lock = &node->queue_lock;
+
+ ret = vb2_queue_init(q);
+ if (ret < 0) {
+ dev_err(pispbe->dev, "vb2_queue_init failed\n");
+ return ret;
+ }
+
+ *vdev = pispbe_videodev; /* default initialization */
+ strscpy(vdev->name, node_desc[id].ent_name, sizeof(vdev->name));
+ vdev->v4l2_dev = &node_group->v4l2_dev;
+ vdev->vfl_dir = output ? VFL_DIR_TX : VFL_DIR_RX;
+ /* get V4L2 to serialise our ioctls */
+ vdev->lock = &node->node_lock;
+ vdev->queue = &node->queue;
+ vdev->device_caps = V4L2_CAP_STREAMING | node_desc[id].caps;
+
+ node->pad.flags = output ? MEDIA_PAD_FL_SOURCE : MEDIA_PAD_FL_SINK;
+ ret = media_entity_pads_init(entity, 1, &node->pad);
+ if (ret) {
+ dev_err(pispbe->dev,
+ "Failed to register media pads for %s device node\n",
+ NODE_NAME(node));
+ goto err_unregister_queue;
+ }
+
+ ret = video_register_device(vdev, VFL_TYPE_VIDEO,
+ PISPBE_VIDEO_NODE_OFFSET);
+ if (ret) {
+ dev_err(pispbe->dev,
+ "Failed to register video %s device node\n",
+ NODE_NAME(node));
+ goto err_unregister_queue;
+ }
+ video_set_drvdata(vdev, node);
+
+ if (output)
+ ret = media_create_pad_link(entity, 0, &node_group->sd.entity,
+ id, MEDIA_LNK_FL_IMMUTABLE |
+ MEDIA_LNK_FL_ENABLED);
+ else
+ ret = media_create_pad_link(&node_group->sd.entity, id, entity,
+ 0, MEDIA_LNK_FL_IMMUTABLE |
+ MEDIA_LNK_FL_ENABLED);
+ if (ret)
+ goto err_unregister_video_dev;
+
+ dev_info(pispbe->dev,
+ "%s device node registered as /dev/video%d\n",
+ NODE_NAME(node), node->vfd.num);
+ return 0;
+
+err_unregister_video_dev:
+ video_unregister_device(&node->vfd);
+err_unregister_queue:
+ vb2_queue_release(&node->queue);
+ return ret;
+}
+
+static const struct v4l2_subdev_pad_ops pispbe_pad_ops = {
+ .link_validate = v4l2_subdev_link_validate_default,
+};
+
+static const struct v4l2_subdev_ops pispbe_sd_ops = {
+ .pad = &pispbe_pad_ops,
+};
+
+static int pispbe_init_subdev(struct pispbe_node_group *node_group)
+{
+ struct pispbe_dev *pispbe = node_group->pispbe;
+ struct v4l2_subdev *sd = &node_group->sd;
+ unsigned int i;
+ int ret;
+
+ v4l2_subdev_init(sd, &pispbe_sd_ops);
+ sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
+ sd->owner = THIS_MODULE;
+ sd->dev = pispbe->dev;
+ strscpy(sd->name, PISPBE_NAME, sizeof(sd->name));
+
+ for (i = 0; i < PISPBE_NUM_NODES; i++)
+ node_group->pad[i].flags =
+ NODE_DESC_IS_OUTPUT(&node_desc[i]) ?
+ MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
+
+ ret = media_entity_pads_init(&sd->entity, PISPBE_NUM_NODES,
+ node_group->pad);
+ if (ret)
+ goto error;
+
+ ret = v4l2_device_register_subdev(&node_group->v4l2_dev, sd);
+ if (ret)
+ goto error;
+
+ return 0;
+
+error:
+ media_entity_cleanup(&sd->entity);
+ return ret;
+}
+
+static int pispbe_init_group(struct pispbe_dev *pispbe, unsigned int id)
+{
+ struct pispbe_node_group *node_group = &pispbe->node_group[id];
+ struct v4l2_device *v4l2_dev;
+ struct media_device *mdev;
+ unsigned int num_registered = 0;
+ int ret;
+
+ node_group->id = id;
+ node_group->pispbe = pispbe;
+ node_group->streaming_map = 0;
+
+ dev_info(pispbe->dev, "Register nodes for group %u\n", id);
+
+ /* Register v4l2_device and media_device */
+ mdev = &node_group->mdev;
+ mdev->hw_revision = node_group->pispbe->hw_version;
+ mdev->dev = node_group->pispbe->dev;
+ strscpy(mdev->model, PISPBE_NAME, sizeof(mdev->model));
+ snprintf(mdev->bus_info, sizeof(mdev->bus_info),
+ "platform:%s", dev_name(node_group->pispbe->dev));
+ media_device_init(mdev);
+
+ v4l2_dev = &node_group->v4l2_dev;
+ v4l2_dev->mdev = &node_group->mdev;
+ strscpy(v4l2_dev->name, PISPBE_NAME, sizeof(v4l2_dev->name));
+
+ ret = v4l2_device_register(pispbe->dev, &node_group->v4l2_dev);
+ if (ret)
+ goto err_media_dev_cleanup;
+
+ /* Register the PISPBE subdevice. */
+ ret = pispbe_init_subdev(node_group);
+ if (ret)
+ goto err_unregister_v4l2;
+
+ /* Create device video nodes */
+ for (; num_registered < PISPBE_NUM_NODES; num_registered++) {
+ ret = pispbe_init_node(node_group, num_registered);
+ if (ret)
+ goto err_unregister_nodes;
+ }
+
+ ret = media_device_register(mdev);
+ if (ret)
+ goto err_unregister_nodes;
+
+ node_group->config =
+ dma_alloc_coherent(pispbe->dev,
+ sizeof(struct pisp_be_tiles_config) *
+ PISP_BE_NUM_CONFIG_BUFFERS,
+ &node_group->config_dma_addr, GFP_KERNEL);
+ if (!node_group->config) {
+ dev_err(pispbe->dev, "Unable to allocate cached config buffers.\n");
+ ret = -ENOMEM;
+ goto err_unregister_mdev;
+ }
+
+ return 0;
+
+err_unregister_mdev:
+ media_device_unregister(mdev);
+err_unregister_nodes:
+ while (num_registered-- > 0) {
+ video_unregister_device(&node_group->node[num_registered].vfd);
+ vb2_queue_release(&node_group->node[num_registered].queue);
+ }
+ v4l2_device_unregister_subdev(&node_group->sd);
+ media_entity_cleanup(&node_group->sd.entity);
+err_unregister_v4l2:
+ v4l2_device_unregister(v4l2_dev);
+err_media_dev_cleanup:
+ media_device_cleanup(mdev);
+ return ret;
+}
+
+static void pispbe_destroy_node_group(struct pispbe_node_group *node_group)
+{
+ struct pispbe_dev *pispbe = node_group->pispbe;
+ int i;
+
+ if (node_group->config) {
+ dma_free_coherent(node_group->pispbe->dev,
+ sizeof(struct pisp_be_tiles_config) *
+ PISP_BE_NUM_CONFIG_BUFFERS,
+ node_group->config,
+ node_group->config_dma_addr);
+ }
+
+ dev_info(pispbe->dev, "Unregister from media controller\n");
+
+ v4l2_device_unregister_subdev(&node_group->sd);
+ media_entity_cleanup(&node_group->sd.entity);
+ media_device_unregister(&node_group->mdev);
+
+ for (i = PISPBE_NUM_NODES - 1; i >= 0; i--) {
+ video_unregister_device(&node_group->node[i].vfd);
+ vb2_queue_release(&node_group->node[i].queue);
+ }
+
+ media_device_cleanup(&node_group->mdev);
+ v4l2_device_unregister(&node_group->v4l2_dev);
+}
+
+static int pispbe_runtime_suspend(struct device *dev)
+{
+ struct pispbe_dev *pispbe = dev_get_drvdata(dev);
+
+ clk_disable_unprepare(pispbe->clk);
+
+ return 0;
+}
+
+static int pispbe_runtime_resume(struct device *dev)
+{
+ struct pispbe_dev *pispbe = dev_get_drvdata(dev);
+ int ret;
+
+ ret = clk_prepare_enable(pispbe->clk);
+ if (ret) {
+ dev_err(dev, "Unable to enable clock\n");
+ return ret;
+ }
+
+ dev_dbg(dev, "%s: Enabled clock, rate=%lu\n",
+ __func__, clk_get_rate(pispbe->clk));
+
+ return 0;
+}
+
+/*
+ * Probe the ISP-BE hardware block, as a single platform device.
+ * This will instantiate multiple "node groups" each with many device nodes.
+ */
+static int pispbe_probe(struct platform_device *pdev)
+{
+ unsigned int num_groups = 0;
+ struct pispbe_dev *pispbe;
+ int ret;
+
+ pispbe = devm_kzalloc(&pdev->dev, sizeof(*pispbe), GFP_KERNEL);
+ if (!pispbe)
+ return -ENOMEM;
+
+ dev_set_drvdata(&pdev->dev, pispbe);
+ pispbe->dev = &pdev->dev;
+ platform_set_drvdata(pdev, pispbe);
+
+ pispbe->be_reg_base = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(pispbe->be_reg_base)) {
+ dev_err(&pdev->dev, "Failed to get ISP-BE registers address\n");
+ return PTR_ERR(pispbe->be_reg_base);
+ }
+
+ pispbe->irq = platform_get_irq(pdev, 0);
+ if (pispbe->irq <= 0) {
+ dev_err(&pdev->dev, "No IRQ resource\n");
+ return -EINVAL;
+ }
+
+ ret = devm_request_irq(&pdev->dev, pispbe->irq, pispbe_isr, 0,
+ PISPBE_NAME, pispbe);
+ if (ret) {
+ dev_err(&pdev->dev, "Unable to request interrupt\n");
+ return ret;
+ }
+
+ ret = dma_set_mask_and_coherent(pispbe->dev, DMA_BIT_MASK(36));
+ if (ret)
+ return ret;
+
+ pispbe->clk = devm_clk_get(&pdev->dev, NULL);
+ if (IS_ERR(pispbe->clk))
+ return dev_err_probe(&pdev->dev, PTR_ERR(pispbe->clk),
+ "Failed to get clock");
+
+ /* Hardware initialisation */
+ pm_runtime_set_autosuspend_delay(pispbe->dev, 200);
+ pm_runtime_use_autosuspend(pispbe->dev);
+ pm_runtime_enable(pispbe->dev);
+
+ ret = pm_runtime_resume_and_get(pispbe->dev);
+ if (ret)
+ goto pm_runtime_disable_err;
+
+ pispbe->hw_busy = 0;
+ spin_lock_init(&pispbe->hw_lock);
+ ret = hw_init(pispbe);
+ if (ret)
+ goto pm_runtime_put_err;
+
+ /*
+ * Initialise and register devices for each node_group, including media
+ * device
+ */
+ for (num_groups = 0;
+ num_groups < PISPBE_NUM_NODE_GROUPS;
+ num_groups++) {
+ ret = pispbe_init_group(pispbe, num_groups);
+ if (ret)
+ goto disable_nodes_err;
+ }
+
+ pm_runtime_mark_last_busy(pispbe->dev);
+ pm_runtime_put_autosuspend(pispbe->dev);
+
+ return 0;
+
+disable_nodes_err:
+ while (num_groups-- > 0)
+ pispbe_destroy_node_group(&pispbe->node_group[num_groups]);
+pm_runtime_put_err:
+ pm_runtime_put(pispbe->dev);
+pm_runtime_disable_err:
+ pm_runtime_dont_use_autosuspend(pispbe->dev);
+ pm_runtime_disable(pispbe->dev);
+
+ dev_err(&pdev->dev, "%s: returning %d", __func__, ret);
+
+ return ret;
+}
+
+static int pispbe_remove(struct platform_device *pdev)
+{
+ struct pispbe_dev *pispbe = platform_get_drvdata(pdev);
+ int i;
+
+ for (i = PISPBE_NUM_NODE_GROUPS - 1; i >= 0; i--)
+ pispbe_destroy_node_group(&pispbe->node_group[i]);
+
+ pm_runtime_dont_use_autosuspend(pispbe->dev);
+ pm_runtime_disable(pispbe->dev);
+
+ return 0;
+}
+
+static const struct dev_pm_ops pispbe_pm_ops = {
+ SET_RUNTIME_PM_OPS(pispbe_runtime_suspend, pispbe_runtime_resume, NULL)
+};
+
+static const struct of_device_id pispbe_of_match[] = {
+ {
+ .compatible = "raspberrypi,pispbe",
+ },
+ { /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, pispbe_of_match);
+
+static struct platform_driver pispbe_pdrv = {
+ .probe = pispbe_probe,
+ .remove = pispbe_remove,
+ .driver = {
+ .name = PISPBE_NAME,
+ .of_match_table = pispbe_of_match,
+ .pm = &pispbe_pm_ops,
+ },
+};
+
+module_platform_driver(pispbe_pdrv);
--- /dev/null
+++ b/drivers/media/platform/raspberrypi/pisp_be/pisp_be_config.h
@@ -0,0 +1,533 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * PiSP Back End configuration definitions.
+ *
+ * Copyright (C) 2021 - Raspberry Pi Ltd
+ *
+ */
+#ifndef _PISP_BE_CONFIG_H_
+#define _PISP_BE_CONFIG_H_
+
+#include <linux/types.h>
+
+#include <media/raspberrypi/pisp_common.h>
+
+/* byte alignment for inputs */
+#define PISP_BACK_END_INPUT_ALIGN 4u
+/* alignment for compressed inputs */
+#define PISP_BACK_END_COMPRESSED_ALIGN 8u
+/* minimum required byte alignment for outputs */
+#define PISP_BACK_END_OUTPUT_MIN_ALIGN 16u
+/* preferred byte alignment for outputs */
+#define PISP_BACK_END_OUTPUT_MAX_ALIGN 64u
+
+/* minimum allowed tile width anywhere in the pipeline */
+#define PISP_BACK_END_MIN_TILE_WIDTH 16u
+/* minimum allowed tile width anywhere in the pipeline */
+#define PISP_BACK_END_MIN_TILE_HEIGHT 16u
+
+#define PISP_BACK_END_NUM_OUTPUTS 2
+#define PISP_BACK_END_HOG_OUTPUT 1
+
+#define PISP_BACK_END_NUM_TILES 64
+
+enum pisp_be_bayer_enable {
+ PISP_BE_BAYER_ENABLE_INPUT = 0x000001,
+ PISP_BE_BAYER_ENABLE_DECOMPRESS = 0x000002,
+ PISP_BE_BAYER_ENABLE_DPC = 0x000004,
+ PISP_BE_BAYER_ENABLE_GEQ = 0x000008,
+ PISP_BE_BAYER_ENABLE_TDN_INPUT = 0x000010,
+ PISP_BE_BAYER_ENABLE_TDN_DECOMPRESS = 0x000020,
+ PISP_BE_BAYER_ENABLE_TDN = 0x000040,
+ PISP_BE_BAYER_ENABLE_TDN_COMPRESS = 0x000080,
+ PISP_BE_BAYER_ENABLE_TDN_OUTPUT = 0x000100,
+ PISP_BE_BAYER_ENABLE_SDN = 0x000200,
+ PISP_BE_BAYER_ENABLE_BLC = 0x000400,
+ PISP_BE_BAYER_ENABLE_STITCH_INPUT = 0x000800,
+ PISP_BE_BAYER_ENABLE_STITCH_DECOMPRESS = 0x001000,
+ PISP_BE_BAYER_ENABLE_STITCH = 0x002000,
+ PISP_BE_BAYER_ENABLE_STITCH_COMPRESS = 0x004000,
+ PISP_BE_BAYER_ENABLE_STITCH_OUTPUT = 0x008000,
+ PISP_BE_BAYER_ENABLE_WBG = 0x010000,
+ PISP_BE_BAYER_ENABLE_CDN = 0x020000,
+ PISP_BE_BAYER_ENABLE_LSC = 0x040000,
+ PISP_BE_BAYER_ENABLE_TONEMAP = 0x080000,
+ PISP_BE_BAYER_ENABLE_CAC = 0x100000,
+ PISP_BE_BAYER_ENABLE_DEBIN = 0x200000,
+ PISP_BE_BAYER_ENABLE_DEMOSAIC = 0x400000,
+};
+
+enum pisp_be_rgb_enable {
+ PISP_BE_RGB_ENABLE_INPUT = 0x000001,
+ PISP_BE_RGB_ENABLE_CCM = 0x000002,
+ PISP_BE_RGB_ENABLE_SAT_CONTROL = 0x000004,
+ PISP_BE_RGB_ENABLE_YCBCR = 0x000008,
+ PISP_BE_RGB_ENABLE_FALSE_COLOUR = 0x000010,
+ PISP_BE_RGB_ENABLE_SHARPEN = 0x000020,
+ /* Preferred colours would occupy 0x000040 */
+ PISP_BE_RGB_ENABLE_YCBCR_INVERSE = 0x000080,
+ PISP_BE_RGB_ENABLE_GAMMA = 0x000100,
+ PISP_BE_RGB_ENABLE_CSC0 = 0x000200,
+ PISP_BE_RGB_ENABLE_CSC1 = 0x000400,
+ PISP_BE_RGB_ENABLE_DOWNSCALE0 = 0x001000,
+ PISP_BE_RGB_ENABLE_DOWNSCALE1 = 0x002000,
+ PISP_BE_RGB_ENABLE_RESAMPLE0 = 0x008000,
+ PISP_BE_RGB_ENABLE_RESAMPLE1 = 0x010000,
+ PISP_BE_RGB_ENABLE_OUTPUT0 = 0x040000,
+ PISP_BE_RGB_ENABLE_OUTPUT1 = 0x080000,
+ PISP_BE_RGB_ENABLE_HOG = 0x200000
+};
+
+#define PISP_BE_RGB_ENABLE_CSC(i) (PISP_BE_RGB_ENABLE_CSC0 << (i))
+#define PISP_BE_RGB_ENABLE_DOWNSCALE(i) (PISP_BE_RGB_ENABLE_DOWNSCALE0 << (i))
+#define PISP_BE_RGB_ENABLE_RESAMPLE(i) (PISP_BE_RGB_ENABLE_RESAMPLE0 << (i))
+#define PISP_BE_RGB_ENABLE_OUTPUT(i) (PISP_BE_RGB_ENABLE_OUTPUT0 << (i))
+
+/*
+ * We use the enable flags to show when blocks are "dirty", but we need some
+ * extra ones too.
+ */
+enum pisp_be_dirty {
+ PISP_BE_DIRTY_GLOBAL = 0x0001,
+ PISP_BE_DIRTY_SH_FC_COMBINE = 0x0002,
+ PISP_BE_DIRTY_CROP = 0x0004
+};
+
+struct pisp_be_global_config {
+ u32 bayer_enables;
+ u32 rgb_enables;
+ u8 bayer_order;
+ u8 pad[3];
+};
+
+struct pisp_be_input_buffer_config {
+ /* low 32 bits followed by high 32 bits (for each of up to 3 planes) */
+ u32 addr[3][2];
+};
+
+struct pisp_be_dpc_config {
+ u8 coeff_level;
+ u8 coeff_range;
+ u8 pad;
+#define PISP_BE_DPC_FLAG_FOLDBACK 1
+ u8 flags;
+};
+
+struct pisp_be_geq_config {
+ u16 offset;
+#define PISP_BE_GEQ_SHARPER BIT(15)
+#define PISP_BE_GEQ_SLOPE ((1 << 10) - 1)
+ /* top bit is the "sharper" flag, slope value is bottom 10 bits */
+ u16 slope_sharper;
+ u16 min;
+ u16 max;
+};
+
+struct pisp_be_tdn_input_buffer_config {
+ /* low 32 bits followed by high 32 bits */
+ u32 addr[2];
+};
+
+struct pisp_be_tdn_config {
+ u16 black_level;
+ u16 ratio;
+ u16 noise_constant;
+ u16 noise_slope;
+ u16 threshold;
+ u8 reset;
+ u8 pad;
+};
+
+struct pisp_be_tdn_output_buffer_config {
+ /* low 32 bits followed by high 32 bits */
+ u32 addr[2];
+};
+
+struct pisp_be_sdn_config {
+ u16 black_level;
+ u8 leakage;
+ u8 pad;
+ u16 noise_constant;
+ u16 noise_slope;
+ u16 noise_constant2;
+ u16 noise_slope2;
+};
+
+struct pisp_be_stitch_input_buffer_config {
+ /* low 32 bits followed by high 32 bits */
+ u32 addr[2];
+};
+
+#define PISP_BE_STITCH_STREAMING_LONG 0x8000
+#define PISP_BE_STITCH_EXPOSURE_RATIO_MASK 0x7fff
+
+struct pisp_be_stitch_config {
+ u16 threshold_lo;
+ u8 threshold_diff_power;
+ u8 pad;
+
+ /* top bit indicates whether streaming input is the long exposure */
+ u16 exposure_ratio;
+
+ u8 motion_threshold_256;
+ u8 motion_threshold_recip;
+};
+
+struct pisp_be_stitch_output_buffer_config {
+ /* low 32 bits followed by high 32 bits */
+ u32 addr[2];
+};
+
+struct pisp_be_cdn_config {
+ u16 thresh;
+ u8 iir_strength;
+ u8 g_adjust;
+};
+
+#define PISP_BE_LSC_LOG_GRID_SIZE 5
+#define PISP_BE_LSC_GRID_SIZE (1 << PISP_BE_LSC_LOG_GRID_SIZE)
+#define PISP_BE_LSC_STEP_PRECISION 18
+
+struct pisp_be_lsc_config {
+ /* (1<<18) / grid_cell_width */
+ u16 grid_step_x;
+ /* (1<<18) / grid_cell_height */
+ u16 grid_step_y;
+ /* RGB gains jointly encoded in 32 bits */
+ u32 lut_packed[PISP_BE_LSC_GRID_SIZE + 1]
+ [PISP_BE_LSC_GRID_SIZE + 1];
+};
+
+struct pisp_be_lsc_extra {
+ u16 offset_x;
+ u16 offset_y;
+};
+
+#define PISP_BE_CAC_LOG_GRID_SIZE 3
+#define PISP_BE_CAC_GRID_SIZE (1 << PISP_BE_CAC_LOG_GRID_SIZE)
+#define PISP_BE_CAC_STEP_PRECISION 20
+
+struct pisp_be_cac_config {
+ /* (1<<20) / grid_cell_width */
+ u16 grid_step_x;
+ /* (1<<20) / grid_cell_height */
+ u16 grid_step_y;
+ /* [gridy][gridx][rb][xy] */
+ s8 lut[PISP_BE_CAC_GRID_SIZE + 1][PISP_BE_CAC_GRID_SIZE + 1][2][2];
+};
+
+struct pisp_be_cac_extra {
+ u16 offset_x;
+ u16 offset_y;
+};
+
+#define PISP_BE_DEBIN_NUM_COEFFS 4
+
+struct pisp_be_debin_config {
+ s8 coeffs[PISP_BE_DEBIN_NUM_COEFFS];
+ s8 h_enable;
+ s8 v_enable;
+ s8 pad[2];
+};
+
+#define PISP_BE_TONEMAP_LUT_SIZE 64
+
+struct pisp_be_tonemap_config {
+ u16 detail_constant;
+ u16 detail_slope;
+ u16 iir_strength;
+ u16 strength;
+ u32 lut[PISP_BE_TONEMAP_LUT_SIZE];
+};
+
+struct pisp_be_demosaic_config {
+ u8 sharper;
+ u8 fc_mode;
+ u8 pad[2];
+};
+
+struct pisp_be_ccm_config {
+ s16 coeffs[9];
+ u8 pad[2];
+ s32 offsets[3];
+};
+
+struct pisp_be_sat_control_config {
+ u8 shift_r;
+ u8 shift_g;
+ u8 shift_b;
+ u8 pad;
+};
+
+struct pisp_be_false_colour_config {
+ u8 distance;
+ u8 pad[3];
+};
+
+#define PISP_BE_SHARPEN_SIZE 5
+#define PISP_BE_SHARPEN_FUNC_NUM_POINTS 9
+
+struct pisp_be_sharpen_config {
+ s8 kernel0[PISP_BE_SHARPEN_SIZE * PISP_BE_SHARPEN_SIZE];
+ s8 pad0[3];
+ s8 kernel1[PISP_BE_SHARPEN_SIZE * PISP_BE_SHARPEN_SIZE];
+ s8 pad1[3];
+ s8 kernel2[PISP_BE_SHARPEN_SIZE * PISP_BE_SHARPEN_SIZE];
+ s8 pad2[3];
+ s8 kernel3[PISP_BE_SHARPEN_SIZE * PISP_BE_SHARPEN_SIZE];
+ s8 pad3[3];
+ s8 kernel4[PISP_BE_SHARPEN_SIZE * PISP_BE_SHARPEN_SIZE];
+ s8 pad4[3];
+ u16 threshold_offset0;
+ u16 threshold_slope0;
+ u16 scale0;
+ u16 pad5;
+ u16 threshold_offset1;
+ u16 threshold_slope1;
+ u16 scale1;
+ u16 pad6;
+ u16 threshold_offset2;
+ u16 threshold_slope2;
+ u16 scale2;
+ u16 pad7;
+ u16 threshold_offset3;
+ u16 threshold_slope3;
+ u16 scale3;
+ u16 pad8;
+ u16 threshold_offset4;
+ u16 threshold_slope4;
+ u16 scale4;
+ u16 pad9;
+ u16 positive_strength;
+ u16 positive_pre_limit;
+ u16 positive_func[PISP_BE_SHARPEN_FUNC_NUM_POINTS];
+ u16 positive_limit;
+ u16 negative_strength;
+ u16 negative_pre_limit;
+ u16 negative_func[PISP_BE_SHARPEN_FUNC_NUM_POINTS];
+ u16 negative_limit;
+ u8 enables;
+ u8 white;
+ u8 black;
+ u8 grey;
+};
+
+struct pisp_be_sh_fc_combine_config {
+ u8 y_factor;
+ u8 c1_factor;
+ u8 c2_factor;
+ u8 pad;
+};
+
+#define PISP_BE_GAMMA_LUT_SIZE 64
+
+struct pisp_be_gamma_config {
+ u32 lut[PISP_BE_GAMMA_LUT_SIZE];
+};
+
+struct pisp_be_crop_config {
+ u16 offset_x, offset_y;
+ u16 width, height;
+};
+
+#define PISP_BE_RESAMPLE_FILTER_SIZE 96
+
+struct pisp_be_resample_config {
+ u16 scale_factor_h, scale_factor_v;
+ s16 coef[PISP_BE_RESAMPLE_FILTER_SIZE];
+};
+
+struct pisp_be_resample_extra {
+ u16 scaled_width;
+ u16 scaled_height;
+ s16 initial_phase_h[3];
+ s16 initial_phase_v[3];
+};
+
+struct pisp_be_downscale_config {
+ u16 scale_factor_h;
+ u16 scale_factor_v;
+ u16 scale_recip_h;
+ u16 scale_recip_v;
+};
+
+struct pisp_be_downscale_extra {
+ u16 scaled_width;
+ u16 scaled_height;
+};
+
+struct pisp_be_hog_config {
+ u8 compute_signed;
+ u8 channel_mix[3];
+ u32 stride;
+};
+
+struct pisp_be_axi_config {
+ u8 r_qos; /* Read QoS */
+ u8 r_cache_prot; /* Read { prot[2:0], cache[3:0] } */
+ u8 w_qos; /* Write QoS */
+ u8 w_cache_prot; /* Write { prot[2:0], cache[3:0] } */
+};
+
+enum pisp_be_transform {
+ PISP_BE_TRANSFORM_NONE = 0x0,
+ PISP_BE_TRANSFORM_HFLIP = 0x1,
+ PISP_BE_TRANSFORM_VFLIP = 0x2,
+ PISP_BE_TRANSFORM_ROT180 =
+ (PISP_BE_TRANSFORM_HFLIP | PISP_BE_TRANSFORM_VFLIP)
+};
+
+struct pisp_be_output_format_config {
+ struct pisp_image_format_config image;
+ u8 transform;
+ u8 pad[3];
+ u16 lo;
+ u16 hi;
+ u16 lo2;
+ u16 hi2;
+};
+
+struct pisp_be_output_buffer_config {
+ /* low 32 bits followed by high 32 bits (for each of 3 planes) */
+ u32 addr[3][2];
+};
+
+struct pisp_be_hog_buffer_config {
+ /* low 32 bits followed by high 32 bits */
+ u32 addr[2];
+};
+
+struct pisp_be_config {
+ /* I/O configuration: */
+ struct pisp_be_input_buffer_config input_buffer;
+ struct pisp_be_tdn_input_buffer_config tdn_input_buffer;
+ struct pisp_be_stitch_input_buffer_config stitch_input_buffer;
+ struct pisp_be_tdn_output_buffer_config tdn_output_buffer;
+ struct pisp_be_stitch_output_buffer_config stitch_output_buffer;
+ struct pisp_be_output_buffer_config
+ output_buffer[PISP_BACK_END_NUM_OUTPUTS];
+ struct pisp_be_hog_buffer_config hog_buffer;
+ /* Processing configuration: */
+ struct pisp_be_global_config global;
+ struct pisp_image_format_config input_format;
+ struct pisp_decompress_config decompress;
+ struct pisp_be_dpc_config dpc;
+ struct pisp_be_geq_config geq;
+ struct pisp_image_format_config tdn_input_format;
+ struct pisp_decompress_config tdn_decompress;
+ struct pisp_be_tdn_config tdn;
+ struct pisp_compress_config tdn_compress;
+ struct pisp_image_format_config tdn_output_format;
+ struct pisp_be_sdn_config sdn;
+ struct pisp_bla_config blc;
+ struct pisp_compress_config stitch_compress;
+ struct pisp_image_format_config stitch_output_format;
+ struct pisp_image_format_config stitch_input_format;
+ struct pisp_decompress_config stitch_decompress;
+ struct pisp_be_stitch_config stitch;
+ struct pisp_be_lsc_config lsc;
+ struct pisp_wbg_config wbg;
+ struct pisp_be_cdn_config cdn;
+ struct pisp_be_cac_config cac;
+ struct pisp_be_debin_config debin;
+ struct pisp_be_tonemap_config tonemap;
+ struct pisp_be_demosaic_config demosaic;
+ struct pisp_be_ccm_config ccm;
+ struct pisp_be_sat_control_config sat_control;
+ struct pisp_be_ccm_config ycbcr;
+ struct pisp_be_sharpen_config sharpen;
+ struct pisp_be_false_colour_config false_colour;
+ struct pisp_be_sh_fc_combine_config sh_fc_combine;
+ struct pisp_be_ccm_config ycbcr_inverse;
+ struct pisp_be_gamma_config gamma;
+ struct pisp_be_ccm_config csc[PISP_BACK_END_NUM_OUTPUTS];
+ struct pisp_be_downscale_config downscale[PISP_BACK_END_NUM_OUTPUTS];
+ struct pisp_be_resample_config resample[PISP_BACK_END_NUM_OUTPUTS];
+ struct pisp_be_output_format_config
+ output_format[PISP_BACK_END_NUM_OUTPUTS];
+ struct pisp_be_hog_config hog;
+ struct pisp_be_axi_config axi;
+ /* Non-register fields: */
+ struct pisp_be_lsc_extra lsc_extra;
+ struct pisp_be_cac_extra cac_extra;
+ struct pisp_be_downscale_extra
+ downscale_extra[PISP_BACK_END_NUM_OUTPUTS];
+ struct pisp_be_resample_extra resample_extra[PISP_BACK_END_NUM_OUTPUTS];
+ struct pisp_be_crop_config crop;
+ struct pisp_image_format_config hog_format;
+ u32 dirty_flags_bayer; /* these use pisp_be_bayer_enable */
+ u32 dirty_flags_rgb; /* use pisp_be_rgb_enable */
+ u32 dirty_flags_extra; /* these use pisp_be_dirty_t */
+};
+
+/*
+ * We also need a tile structure to describe the size of the tiles going
+ * through the pipeline.
+ */
+
+enum pisp_tile_edge {
+ PISP_LEFT_EDGE = (1 << 0),
+ PISP_RIGHT_EDGE = (1 << 1),
+ PISP_TOP_EDGE = (1 << 2),
+ PISP_BOTTOM_EDGE = (1 << 3)
+};
+
+struct pisp_tile {
+ u8 edge; // enum pisp_tile_edge
+ u8 pad0[3];
+ // 4 bytes
+ u32 input_addr_offset;
+ u32 input_addr_offset2;
+ u16 input_offset_x;
+ u16 input_offset_y;
+ u16 input_width;
+ u16 input_height;
+ // 20 bytes
+ u32 tdn_input_addr_offset;
+ u32 tdn_output_addr_offset;
+ u32 stitch_input_addr_offset;
+ u32 stitch_output_addr_offset;
+ // 36 bytes
+ u32 lsc_grid_offset_x;
+ u32 lsc_grid_offset_y;
+ // 44 bytes
+ u32 cac_grid_offset_x;
+ u32 cac_grid_offset_y;
+ // 52 bytes
+ u16 crop_x_start[PISP_BACK_END_NUM_OUTPUTS];
+ u16 crop_x_end[PISP_BACK_END_NUM_OUTPUTS];
+ u16 crop_y_start[PISP_BACK_END_NUM_OUTPUTS];
+ u16 crop_y_end[PISP_BACK_END_NUM_OUTPUTS];
+ // 68 bytes
+ /* Ordering is planes then branches */
+ u16 downscale_phase_x[3 * PISP_BACK_END_NUM_OUTPUTS];
+ u16 downscale_phase_y[3 * PISP_BACK_END_NUM_OUTPUTS];
+ // 92 bytes
+ u16 resample_in_width[PISP_BACK_END_NUM_OUTPUTS];
+ u16 resample_in_height[PISP_BACK_END_NUM_OUTPUTS];
+ // 100 bytes
+ /* Ordering is planes then branches */
+ u16 resample_phase_x[3 * PISP_BACK_END_NUM_OUTPUTS];
+ u16 resample_phase_y[3 * PISP_BACK_END_NUM_OUTPUTS];
+ // 124 bytes
+ u16 output_offset_x[PISP_BACK_END_NUM_OUTPUTS];
+ u16 output_offset_y[PISP_BACK_END_NUM_OUTPUTS];
+ u16 output_width[PISP_BACK_END_NUM_OUTPUTS];
+ u16 output_height[PISP_BACK_END_NUM_OUTPUTS];
+ // 140 bytes
+ u32 output_addr_offset[PISP_BACK_END_NUM_OUTPUTS];
+ u32 output_addr_offset2[PISP_BACK_END_NUM_OUTPUTS];
+ // 156 bytes
+ u32 output_hog_addr_offset;
+ // 160 bytes
+};
+
+static_assert(sizeof(struct pisp_tile) == 160);
+
+struct pisp_be_tiles_config {
+ struct pisp_be_config config;
+ struct pisp_tile tiles[PISP_BACK_END_NUM_TILES];
+ int num_tiles;
+};
+
+#endif /* _PISP_BE_CONFIG_H_ */
--- /dev/null
+++ b/drivers/media/platform/raspberrypi/pisp_be/pisp_be_formats.h
@@ -0,0 +1,469 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * PiSP Back End driver image format definitions.
+ *
+ * Copyright (c) 2021 Raspberry Pi Ltd
+ */
+
+#ifndef _PISP_BE_FORMATS_
+#define _PISP_BE_FORMATS_
+
+#include <linux/bits.h>
+#include <linux/videodev2.h>
+
+#define MAX_PLANES 3
+#define P3(x) ((x) * 8)
+
+struct pisp_be_format {
+ unsigned int fourcc;
+ unsigned int align;
+ unsigned int bit_depth;
+ /* 0P3 factor for plane sizing */
+ unsigned int plane_factor[MAX_PLANES];
+ unsigned int num_planes;
+ unsigned int colorspace_mask;
+ enum v4l2_colorspace colorspace_default;
+};
+
+#define V4L2_COLORSPACE_MASK(colorspace) BIT(colorspace)
+
+#define V4L2_COLORSPACE_MASK_JPEG V4L2_COLORSPACE_MASK(V4L2_COLORSPACE_JPEG)
+#define V4L2_COLORSPACE_MASK_SMPTE170M V4L2_COLORSPACE_MASK(V4L2_COLORSPACE_SMPTE170M)
+#define V4L2_COLORSPACE_MASK_REC709 V4L2_COLORSPACE_MASK(V4L2_COLORSPACE_REC709)
+#define V4L2_COLORSPACE_MASK_SRGB V4L2_COLORSPACE_MASK(V4L2_COLORSPACE_SRGB)
+#define V4L2_COLORSPACE_MASK_RAW V4L2_COLORSPACE_MASK(V4L2_COLORSPACE_RAW)
+
+/*
+ * All three colour spaces JPEG, SMPTE170M and REC709 are fundamentally sRGB
+ * underneath (as near as makes no difference to us), just with different YCbCr
+ * encodings. Therefore the ISP can generate sRGB on its main output and any of
+ * the others on its low resolution output. Applications should, when using both
+ * outputs, program the colour spaces on them to be the same, matching whatever
+ * is requested for the low resolution output, even if the main output is
+ * producing an RGB format. In turn this requires us to allow all these colour
+ * spaces for every YUV/RGB output format.
+ */
+#define V4L2_COLORSPACE_MASK_ALL_SRGB (V4L2_COLORSPACE_MASK_JPEG | \
+ V4L2_COLORSPACE_MASK_SRGB | \
+ V4L2_COLORSPACE_MASK_SMPTE170M | \
+ V4L2_COLORSPACE_MASK_REC709)
+
+static const struct pisp_be_format supported_formats[] = {
+ /* Single plane YUV formats */
+ {
+ .fourcc = V4L2_PIX_FMT_YUV420,
+ /* 128 alignment to ensure U/V planes are 64 byte aligned. */
+ .align = 128,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(0.25), P3(0.25) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_JPEG,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_YVU420,
+ /* 128 alignment to ensure U/V planes are 64 byte aligned. */
+ .align = 128,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(0.25), P3(0.25) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_NV12,
+ .align = 32,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(0.5) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_NV21,
+ .align = 32,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(0.5) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_YUYV,
+ .align = 64,
+ .bit_depth = 16,
+ .plane_factor = { P3(1) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_UYVY,
+ .align = 64,
+ .bit_depth = 16,
+ .plane_factor = { P3(1) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_YVYU,
+ .align = 64,
+ .bit_depth = 16,
+ .plane_factor = { P3(1) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_VYUY,
+ .align = 64,
+ .bit_depth = 16,
+ .plane_factor = { P3(1) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ /* Multiplane YUV formats */
+ {
+ .fourcc = V4L2_PIX_FMT_YUV420M,
+ .align = 64,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(0.25), P3(0.25) },
+ .num_planes = 3,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_JPEG,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_NV12M,
+ .align = 32,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(0.5) },
+ .num_planes = 2,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_NV21M,
+ .align = 32,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(0.5) },
+ .num_planes = 2,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_YVU420M,
+ .align = 64,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(0.25), P3(0.25) },
+ .num_planes = 3,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_YUV422M,
+ .align = 64,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(0.5), P3(0.5) },
+ .num_planes = 3,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_JPEG,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_YVU422M,
+ .align = 64,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(0.5), P3(0.5) },
+ .num_planes = 3,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_YUV444M,
+ .align = 64,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(1), P3(1) },
+ .num_planes = 3,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_JPEG,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_YVU444M,
+ .align = 64,
+ .bit_depth = 8,
+ .plane_factor = { P3(1), P3(1), P3(1) },
+ .num_planes = 3,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SMPTE170M,
+ },
+ /* RGB formats */
+ {
+ .fourcc = V4L2_PIX_FMT_RGB24,
+ .align = 32,
+ .bit_depth = 24,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SRGB,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_BGR24,
+ .align = 32,
+ .bit_depth = 24,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SRGB,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_XBGR32,
+ .align = 64,
+ .bit_depth = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SRGB,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_RGBX32,
+ .align = 64,
+ .bit_depth = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_ALL_SRGB,
+ .colorspace_default = V4L2_COLORSPACE_SRGB,
+ },
+ /* Bayer formats - 8-bit */
+ {
+ .fourcc = V4L2_PIX_FMT_SRGGB8,
+ .bit_depth = 8,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SBGGR8,
+ .bit_depth = 8,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SGRBG8,
+ .bit_depth = 8,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SGBRG8,
+ .bit_depth = 8,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ /* Bayer formats - 16-bit */
+ {
+ .fourcc = V4L2_PIX_FMT_SRGGB16,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SBGGR16,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SGRBG16,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SGBRG16,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ /* Bayer formats unpacked to 16bpp */
+ /* 10 bit */
+ .fourcc = V4L2_PIX_FMT_SRGGB10,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SBGGR10,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SGRBG10,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SGBRG10,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ /* 12 bit */
+ .fourcc = V4L2_PIX_FMT_SRGGB12,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SBGGR12,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SGRBG12,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SGBRG12,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ /* 14 bit */
+ .fourcc = V4L2_PIX_FMT_SRGGB14,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SBGGR14,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SGRBG14,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_SGBRG14,
+ .bit_depth = 16,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ /* Bayer formats - 16-bit PiSP Compressed */
+ {
+ .fourcc = V4L2_PIX_FMT_PISP_COMP1_BGGR,
+ .bit_depth = 8,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_PISP_COMP1_RGGB,
+ .bit_depth = 8,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_PISP_COMP1_GRBG,
+ .bit_depth = 8,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+ {
+ .fourcc = V4L2_PIX_FMT_PISP_COMP1_GBRG,
+ .bit_depth = 8,
+ .align = 32,
+ .plane_factor = { P3(1.0) },
+ .num_planes = 1,
+ .colorspace_mask = V4L2_COLORSPACE_MASK_RAW,
+ .colorspace_default = V4L2_COLORSPACE_RAW,
+ },
+};
+
+static const struct pisp_be_format meta_out_supported_formats[] = {
+ /* Configuration buffer format. */
+ {
+ .fourcc = V4L2_META_FMT_RPI_BE_CFG,
+ },
+};
+
+#endif /* _PISP_BE_FORMATS_ */
--- a/drivers/media/v4l2-core/v4l2-ioctl.c
+++ b/drivers/media/v4l2-core/v4l2-ioctl.c
@@ -1452,6 +1452,7 @@ static void v4l_fill_fmtdesc(struct v4l2
case V4L2_PIX_FMT_NV12M_10BE_8L128: descr = "10-bit NV12M (8x128 Linear, BE)"; break;
case V4L2_META_FMT_SENSOR_DATA: descr = "Sensor Ancillary Metadata"; break;
case V4L2_META_FMT_BCM2835_ISP_STATS: descr = "BCM2835 ISP Image Statistics"; break;
+ case V4L2_META_FMT_RPI_BE_CFG: descr = "PiSP Config format"; break;
default:
/* Compressed formats */
@@ -1503,6 +1504,7 @@ static void v4l_fill_fmtdesc(struct v4l2
case V4L2_PIX_FMT_MT21C: descr = "Mediatek Compressed Format"; break;
case V4L2_PIX_FMT_QC08C: descr = "QCOM Compressed 8-bit Format"; break;
case V4L2_PIX_FMT_QC10C: descr = "QCOM Compressed 10-bit Format"; break;
+ case V4L2_PIX_FMT_RPI_BE: descr = "PiSP Opaque Format"; break;
default:
if (fmt->description[0])
return;
--- /dev/null
+++ b/include/media/raspberrypi/pisp_common.h
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Raspberry Pi PiSP common configuration definitions.
+ *
+ * Copyright (C) 2021 - Raspberry Pi (Trading) Ltd.
+ *
+ */
+#ifndef _PISP_COMMON_H_
+#define _PISP_COMMON_H_
+
+#include <linux/types.h>
+
+#include "pisp_types.h"
+
+struct pisp_bla_config {
+ uint16_t black_level_r;
+ uint16_t black_level_gr;
+ uint16_t black_level_gb;
+ uint16_t black_level_b;
+ uint16_t output_black_level;
+ uint8_t pad[2];
+};
+
+struct pisp_wbg_config {
+ uint16_t gain_r;
+ uint16_t gain_g;
+ uint16_t gain_b;
+ uint8_t pad[2];
+};
+
+struct pisp_compress_config {
+ /* value subtracted from incoming data */
+ uint16_t offset;
+ uint8_t pad;
+ /* 1 => Companding; 2 => Delta (recommended); 3 => Combined (for HDR) */
+ uint8_t mode;
+};
+
+struct pisp_decompress_config {
+ /* value added to reconstructed data */
+ uint16_t offset;
+ uint8_t pad;
+ /* 1 => Companding; 2 => Delta (recommended); 3 => Combined (for HDR) */
+ uint8_t mode;
+};
+
+enum pisp_axi_flags {
+ /* round down bursts to end at a 32-byte boundary, to align following bursts */
+ PISP_AXI_FLAG_ALIGN = 128,
+ /* for FE writer: force WSTRB high, to pad output to 16-byte boundary */
+ PISP_AXI_FLAG_PAD = 64,
+ /* for FE writer: Use Output FIFO level to trigger "panic" */
+ PISP_AXI_FLAG_PANIC = 32
+};
+
+struct pisp_axi_config {
+ /* burst length minus one, which must be in the range 0:15; OR'd with flags */
+ uint8_t maxlen_flags;
+ /* { prot[2:0], cache[3:0] } fields, echoed on AXI bus */
+ uint8_t cache_prot;
+ /* QoS field(s) (4x4 bits for FE writer; 4 bits for other masters) */
+ uint16_t qos;
+};
+
+#endif /* _PISP_COMMON_H_ */
--- /dev/null
+++ b/include/media/raspberrypi/pisp_types.h
@@ -0,0 +1,144 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Raspberry Pi PiSP common types.
+ *
+ * Copyright (C) 2021 - Raspberry Pi (Trading) Ltd.
+ *
+ */
+#ifndef _PISP_TYPES_H_
+#define _PISP_TYPES_H_
+
+/* This definition must match the format description in the hardware exactly! */
+struct pisp_image_format_config {
+ /* size in pixels */
+ uint16_t width, height;
+ /* must match struct pisp_image_format below */
+ uint32_t format;
+ int32_t stride;
+ /* some planar image formats will need a second stride */
+ int32_t stride2;
+};
+
+static_assert(sizeof(struct pisp_image_format_config) == 16);
+
+enum pisp_bayer_order {
+ /*
+ * Note how bayer_order&1 tells you if G is on the even pixels of the
+ * checkerboard or not, and bayer_order&2 tells you if R is on the even
+ * rows or is swapped with B. Note that if the top (of the 8) bits is
+ * set, this denotes a monochrome or greyscale image, and the lower bits
+ * should all be ignored.
+ */
+ PISP_BAYER_ORDER_RGGB = 0,
+ PISP_BAYER_ORDER_GBRG = 1,
+ PISP_BAYER_ORDER_BGGR = 2,
+ PISP_BAYER_ORDER_GRBG = 3,
+ PISP_BAYER_ORDER_GREYSCALE = 128
+};
+
+enum pisp_image_format {
+ /*
+ * Precise values are mostly tbd. Generally these will be portmanteau
+ * values comprising bit fields and flags. This format must be shared
+ * throughout the PiSP.
+ */
+ PISP_IMAGE_FORMAT_BPS_8 = 0x00000000,
+ PISP_IMAGE_FORMAT_BPS_10 = 0x00000001,
+ PISP_IMAGE_FORMAT_BPS_12 = 0x00000002,
+ PISP_IMAGE_FORMAT_BPS_16 = 0x00000003,
+ PISP_IMAGE_FORMAT_BPS_MASK = 0x00000003,
+
+ PISP_IMAGE_FORMAT_PLANARITY_INTERLEAVED = 0x00000000,
+ PISP_IMAGE_FORMAT_PLANARITY_SEMI_PLANAR = 0x00000010,
+ PISP_IMAGE_FORMAT_PLANARITY_PLANAR = 0x00000020,
+ PISP_IMAGE_FORMAT_PLANARITY_MASK = 0x00000030,
+
+ PISP_IMAGE_FORMAT_SAMPLING_444 = 0x00000000,
+ PISP_IMAGE_FORMAT_SAMPLING_422 = 0x00000100,
+ PISP_IMAGE_FORMAT_SAMPLING_420 = 0x00000200,
+ PISP_IMAGE_FORMAT_SAMPLING_MASK = 0x00000300,
+
+ PISP_IMAGE_FORMAT_ORDER_NORMAL = 0x00000000,
+ PISP_IMAGE_FORMAT_ORDER_SWAPPED = 0x00001000,
+
+ PISP_IMAGE_FORMAT_SHIFT_0 = 0x00000000,
+ PISP_IMAGE_FORMAT_SHIFT_1 = 0x00010000,
+ PISP_IMAGE_FORMAT_SHIFT_2 = 0x00020000,
+ PISP_IMAGE_FORMAT_SHIFT_3 = 0x00030000,
+ PISP_IMAGE_FORMAT_SHIFT_4 = 0x00040000,
+ PISP_IMAGE_FORMAT_SHIFT_5 = 0x00050000,
+ PISP_IMAGE_FORMAT_SHIFT_6 = 0x00060000,
+ PISP_IMAGE_FORMAT_SHIFT_7 = 0x00070000,
+ PISP_IMAGE_FORMAT_SHIFT_8 = 0x00080000,
+ PISP_IMAGE_FORMAT_SHIFT_MASK = 0x000f0000,
+
+ PISP_IMAGE_FORMAT_UNCOMPRESSED = 0x00000000,
+ PISP_IMAGE_FORMAT_COMPRESSION_MODE_1 = 0x01000000,
+ PISP_IMAGE_FORMAT_COMPRESSION_MODE_2 = 0x02000000,
+ PISP_IMAGE_FORMAT_COMPRESSION_MODE_3 = 0x03000000,
+ PISP_IMAGE_FORMAT_COMPRESSION_MASK = 0x03000000,
+
+ PISP_IMAGE_FORMAT_HOG_SIGNED = 0x04000000,
+ PISP_IMAGE_FORMAT_HOG_UNSIGNED = 0x08000000,
+ PISP_IMAGE_FORMAT_INTEGRAL_IMAGE = 0x10000000,
+ PISP_IMAGE_FORMAT_WALLPAPER_ROLL = 0x20000000,
+ PISP_IMAGE_FORMAT_THREE_CHANNEL = 0x40000000,
+
+ /* Lastly a few specific instantiations of the above. */
+ PISP_IMAGE_FORMAT_SINGLE_16 = PISP_IMAGE_FORMAT_BPS_16,
+ PISP_IMAGE_FORMAT_THREE_16 =
+ PISP_IMAGE_FORMAT_BPS_16 | PISP_IMAGE_FORMAT_THREE_CHANNEL
+};
+
+#define PISP_IMAGE_FORMAT_bps_8(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_BPS_MASK) == PISP_IMAGE_FORMAT_BPS_8)
+#define PISP_IMAGE_FORMAT_bps_10(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_BPS_MASK) == PISP_IMAGE_FORMAT_BPS_10)
+#define PISP_IMAGE_FORMAT_bps_12(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_BPS_MASK) == PISP_IMAGE_FORMAT_BPS_12)
+#define PISP_IMAGE_FORMAT_bps_16(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_BPS_MASK) == PISP_IMAGE_FORMAT_BPS_16)
+#define PISP_IMAGE_FORMAT_bps(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_BPS_MASK) ? \
+ 8 + (2 << (((fmt)&PISP_IMAGE_FORMAT_BPS_MASK) - 1)) : \
+ 8)
+#define PISP_IMAGE_FORMAT_shift(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_SHIFT_MASK) / PISP_IMAGE_FORMAT_SHIFT_1)
+#define PISP_IMAGE_FORMAT_three_channel(fmt) \
+ ((fmt)&PISP_IMAGE_FORMAT_THREE_CHANNEL)
+#define PISP_IMAGE_FORMAT_single_channel(fmt) \
+ (!((fmt)&PISP_IMAGE_FORMAT_THREE_CHANNEL))
+#define PISP_IMAGE_FORMAT_compressed(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_COMPRESSION_MASK) != \
+ PISP_IMAGE_FORMAT_UNCOMPRESSED)
+#define PISP_IMAGE_FORMAT_sampling_444(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_SAMPLING_MASK) == \
+ PISP_IMAGE_FORMAT_SAMPLING_444)
+#define PISP_IMAGE_FORMAT_sampling_422(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_SAMPLING_MASK) == \
+ PISP_IMAGE_FORMAT_SAMPLING_422)
+#define PISP_IMAGE_FORMAT_sampling_420(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_SAMPLING_MASK) == \
+ PISP_IMAGE_FORMAT_SAMPLING_420)
+#define PISP_IMAGE_FORMAT_order_normal(fmt) \
+ (!((fmt)&PISP_IMAGE_FORMAT_ORDER_SWAPPED))
+#define PISP_IMAGE_FORMAT_order_swapped(fmt) \
+ ((fmt)&PISP_IMAGE_FORMAT_ORDER_SWAPPED)
+#define PISP_IMAGE_FORMAT_interleaved(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_PLANARITY_MASK) == \
+ PISP_IMAGE_FORMAT_PLANARITY_INTERLEAVED)
+#define PISP_IMAGE_FORMAT_semiplanar(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_PLANARITY_MASK) == \
+ PISP_IMAGE_FORMAT_PLANARITY_SEMI_PLANAR)
+#define PISP_IMAGE_FORMAT_planar(fmt) \
+ (((fmt)&PISP_IMAGE_FORMAT_PLANARITY_MASK) == \
+ PISP_IMAGE_FORMAT_PLANARITY_PLANAR)
+#define PISP_IMAGE_FORMAT_wallpaper(fmt) \
+ ((fmt)&PISP_IMAGE_FORMAT_WALLPAPER_ROLL)
+#define PISP_IMAGE_FORMAT_HOG(fmt) \
+ ((fmt) & \
+ (PISP_IMAGE_FORMAT_HOG_SIGNED | PISP_IMAGE_FORMAT_HOG_UNSIGNED))
+
+#define PISP_WALLPAPER_WIDTH 128 // in bytes
+
+#endif /* _PISP_TYPES_H_ */
--- a/include/uapi/linux/videodev2.h
+++ b/include/uapi/linux/videodev2.h
@@ -793,6 +793,9 @@ struct v4l2_pix_format {
#define V4L2_PIX_FMT_IPU3_SGRBG10 v4l2_fourcc('i', 'p', '3', 'G') /* IPU3 packed 10-bit GRBG bayer */
#define V4L2_PIX_FMT_IPU3_SRGGB10 v4l2_fourcc('i', 'p', '3', 'r') /* IPU3 packed 10-bit RGGB bayer */
+/* The pixel format for all our buffers (the precise format is found in the config buffer). */
+#define V4L2_PIX_FMT_RPI_BE v4l2_fourcc('R', 'P', 'B', 'P')
+
/* SDR formats - used only for Software Defined Radio devices */
#define V4L2_SDR_FMT_CU8 v4l2_fourcc('C', 'U', '0', '8') /* IQ u8 */
#define V4L2_SDR_FMT_CU16LE v4l2_fourcc('C', 'U', '1', '6') /* IQ u16le */
@@ -822,6 +825,9 @@ struct v4l2_pix_format {
#define V4L2_META_FMT_RK_ISP1_PARAMS v4l2_fourcc('R', 'K', '1', 'P') /* Rockchip ISP1 3A Parameters */
#define V4L2_META_FMT_RK_ISP1_STAT_3A v4l2_fourcc('R', 'K', '1', 'S') /* Rockchip ISP1 3A Statistics */
+/* The metadata format identifier for our configuration buffers. */
+#define V4L2_META_FMT_RPI_BE_CFG v4l2_fourcc('R', 'P', 'B', 'C')
+
/* priv field value to indicates that subsequent fields are valid. */
#define V4L2_PIX_FMT_PRIV_MAGIC 0xfeedcafe
|