3f9149ed1575d43464384879f6d07dcb1a964eef
[openwrt/staging/wigyori.git] /
1 From 762245b7e1d8e846e4cc314dfad025185f224db1 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Thu, 23 Apr 2020 10:17:37 +0100
4 Subject: [PATCH] staging: vc04_services: ISP: Add a more complex ISP
5 processing component
6
7 Driver for the BCM2835 ISP hardware block. This driver uses the MMAL
8 component to program the ISP hardware through the VC firmware.
9
10 The ISP component can produce two video stream outputs, and Bayer
11 image statistics. This can't be encompassed in a simple V4L2
12 M2M device, so create a new device that registers 4 video nodes.
13
14 This patch squashes all the development patches from the earlier
15 rpi-5.4.y branch into one
16
17 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
18 ---
19 MAINTAINERS | 9 +
20 drivers/staging/vc04_services/Kconfig | 1 +
21 drivers/staging/vc04_services/Makefile | 1 +
22 .../staging/vc04_services/bcm2835-isp/Kconfig | 14 +
23 .../vc04_services/bcm2835-isp/Makefile | 8 +
24 .../bcm2835-isp/bcm2835-isp-ctrls.h | 67 +
25 .../bcm2835-isp/bcm2835-isp-fmts.h | 353 ++++
26 .../bcm2835-isp/bcm2835-v4l2-isp.c | 1694 +++++++++++++++++
27 .../vc04_services/vchiq-mmal/mmal-encodings.h | 4 +
28 .../vchiq-mmal/mmal-parameters.h | 153 +-
29 10 files changed, 2303 insertions(+), 1 deletion(-)
30 create mode 100644 drivers/staging/vc04_services/bcm2835-isp/Kconfig
31 create mode 100644 drivers/staging/vc04_services/bcm2835-isp/Makefile
32 create mode 100644 drivers/staging/vc04_services/bcm2835-isp/bcm2835-isp-ctrls.h
33 create mode 100644 drivers/staging/vc04_services/bcm2835-isp/bcm2835-isp-fmts.h
34 create mode 100644 drivers/staging/vc04_services/bcm2835-isp/bcm2835-v4l2-isp.c
35
36 --- a/MAINTAINERS
37 +++ b/MAINTAINERS
38 @@ -3568,6 +3568,15 @@ S: Maintained
39 F: drivers/media/platform/bcm2835/
40 F: Documentation/devicetree/bindings/media/brcm,bcm2835-unicam.yaml
41
42 +BROADCOM BCM2835 ISP DRIVER
43 +M: Raspberry Pi Kernel Maintenance <kernel-list@raspberrypi.com>
44 +L: linux-media@vger.kernel.org
45 +S: Maintained
46 +F: Documentation/media/uapi/v4l/pixfmt-meta-bcm2835-isp-stats.rst
47 +F: Documentation/media/v4l-drivers/bcm2835-isp.rst
48 +F: drivers/staging/vc04_services/bcm2835-isp
49 +F: include/uapi/linux/bcm2835-isp.h
50 +
51 BROADCOM BCM47XX MIPS ARCHITECTURE
52 M: Hauke Mehrtens <hauke@hauke-m.de>
53 M: Rafał Miłecki <zajec5@gmail.com>
54 --- a/drivers/staging/vc04_services/Kconfig
55 +++ b/drivers/staging/vc04_services/Kconfig
56 @@ -45,6 +45,7 @@ source "drivers/staging/vc04_services/bc
57
58 source "drivers/staging/vc04_services/vc-sm-cma/Kconfig"
59 source "drivers/staging/vc04_services/bcm2835-codec/Kconfig"
60 +source "drivers/staging/vc04_services/bcm2835-isp/Kconfig"
61
62 source "drivers/staging/vc04_services/vchiq-mmal/Kconfig"
63
64 --- a/drivers/staging/vc04_services/Makefile
65 +++ b/drivers/staging/vc04_services/Makefile
66 @@ -16,6 +16,7 @@ obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-
67 obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += vchiq-mmal/
68 obj-$(CONFIG_BCM_VC_SM_CMA) += vc-sm-cma/
69 obj-$(CONFIG_VIDEO_CODEC_BCM2835) += bcm2835-codec/
70 +obj-$(CONFIG_VIDEO_ISP_BCM2835) += bcm2835-isp/
71
72 ccflags-y += -I $(srctree)/$(src)/include
73
74 --- /dev/null
75 +++ b/drivers/staging/vc04_services/bcm2835-isp/Kconfig
76 @@ -0,0 +1,14 @@
77 +config VIDEO_ISP_BCM2835
78 + tristate "BCM2835 ISP support"
79 + depends on MEDIA_SUPPORT
80 + depends on VIDEO_V4L2 && (ARCH_BCM2835 || COMPILE_TEST)
81 + depends on MEDIA_CONTROLLER
82 + select BCM2835_VCHIQ_MMAL
83 + select VIDEOBUF2_DMA_CONTIG
84 + help
85 + This is the V4L2 driver for the Broadcom BCM2835 ISP hardware.
86 + This operates over the VCHIQ interface to a service running on
87 + VideoCore.
88 +
89 + To compile this driver as a module, choose M here: the module
90 + will be called bcm2835-isp.
91 --- /dev/null
92 +++ b/drivers/staging/vc04_services/bcm2835-isp/Makefile
93 @@ -0,0 +1,8 @@
94 +# SPDX-License-Identifier: GPL-2.0
95 +bcm2835-isp-objs := bcm2835-v4l2-isp.o
96 +
97 +obj-$(CONFIG_VIDEO_ISP_BCM2835) += bcm2835-isp.o
98 +
99 +ccflags-y += \
100 + -I$(srctree)/drivers/staging/vc04_services \
101 + -D__VCCOREVER__=0x04000000
102 --- /dev/null
103 +++ b/drivers/staging/vc04_services/bcm2835-isp/bcm2835-isp-ctrls.h
104 @@ -0,0 +1,67 @@
105 +/* SPDX-License-Identifier: GPL-2.0 */
106 +/*
107 + * Broadcom BCM2835 ISP driver
108 + *
109 + * Copyright © 2019-2020 Raspberry Pi (Trading) Ltd.
110 + *
111 + * Author: Naushir Patuck (naush@raspberrypi.com)
112 + *
113 + */
114 +
115 +#ifndef BCM2835_ISP_CTRLS
116 +#define BCM2835_ISP_CTRLS
117 +
118 +#include <linux/bcm2835-isp.h>
119 +
120 +struct bcm2835_isp_custom_ctrl {
121 + const char *name;
122 + u32 id;
123 + u32 size;
124 + u32 flags;
125 +};
126 +
127 +static const struct bcm2835_isp_custom_ctrl custom_ctrls[] = {
128 + {
129 + .name = "Colour Correction Matrix",
130 + .id = V4L2_CID_USER_BCM2835_ISP_CC_MATRIX,
131 + .size = sizeof(struct bcm2835_isp_custom_ccm),
132 + .flags = 0
133 + }, {
134 + .name = "Lens Shading",
135 + .id = V4L2_CID_USER_BCM2835_ISP_LENS_SHADING,
136 + .size = sizeof(struct bcm2835_isp_lens_shading),
137 + .flags = V4L2_CTRL_FLAG_EXECUTE_ON_WRITE
138 + }, {
139 + .name = "Black Level",
140 + .id = V4L2_CID_USER_BCM2835_ISP_BLACK_LEVEL,
141 + .size = sizeof(struct bcm2835_isp_black_level),
142 + .flags = 0
143 + }, {
144 + .name = "Green Equalisation",
145 + .id = V4L2_CID_USER_BCM2835_ISP_GEQ,
146 + .size = sizeof(struct bcm2835_isp_geq),
147 + .flags = 0
148 + }, {
149 + .name = "Gamma",
150 + .id = V4L2_CID_USER_BCM2835_ISP_GAMMA,
151 + .size = sizeof(struct bcm2835_isp_gamma),
152 + .flags = 0
153 + }, {
154 + .name = "Sharpen",
155 + .id = V4L2_CID_USER_BCM2835_ISP_SHARPEN,
156 + .size = sizeof(struct bcm2835_isp_sharpen),
157 + .flags = 0
158 + }, {
159 + .name = "Denoise",
160 + .id = V4L2_CID_USER_BCM2835_ISP_DENOISE,
161 + .size = sizeof(struct bcm2835_isp_denoise),
162 + .flags = 0
163 + }, {
164 + .name = "Defective Pixel Correction",
165 + .id = V4L2_CID_USER_BCM2835_ISP_DPC,
166 + .size = sizeof(struct bcm2835_isp_dpc),
167 + .flags = 0
168 + }
169 +};
170 +
171 +#endif
172 --- /dev/null
173 +++ b/drivers/staging/vc04_services/bcm2835-isp/bcm2835-isp-fmts.h
174 @@ -0,0 +1,353 @@
175 +/* SPDX-License-Identifier: GPL-2.0 */
176 +/*
177 + * Broadcom BCM2835 ISP driver
178 + *
179 + * Copyright © 2019-2020 Raspberry Pi (Trading) Ltd.
180 + *
181 + * Author: Naushir Patuck (naush@raspberrypi.com)
182 + *
183 + */
184 +
185 +#ifndef BCM2835_ISP_FMTS
186 +#define BCM2835_ISP_FMTS
187 +
188 +#include <linux/videodev2.h>
189 +#include "vchiq-mmal/mmal-encodings.h"
190 +
191 +struct bcm2835_isp_fmt {
192 + u32 fourcc;
193 + int depth;
194 + int bytesperline_align;
195 + u32 mmal_fmt;
196 + int size_multiplier_x2;
197 + enum v4l2_colorspace colorspace;
198 + unsigned int step_size;
199 +};
200 +
201 +static const struct bcm2835_isp_fmt supported_formats[] = {
202 + {
203 + /* YUV formats */
204 + .fourcc = V4L2_PIX_FMT_YUV420,
205 + .depth = 8,
206 + .bytesperline_align = 32,
207 + .mmal_fmt = MMAL_ENCODING_I420,
208 + .size_multiplier_x2 = 3,
209 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
210 + .step_size = 2,
211 + }, {
212 + .fourcc = V4L2_PIX_FMT_YVU420,
213 + .depth = 8,
214 + .bytesperline_align = 32,
215 + .mmal_fmt = MMAL_ENCODING_YV12,
216 + .size_multiplier_x2 = 3,
217 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
218 + .step_size = 2,
219 + }, {
220 + .fourcc = V4L2_PIX_FMT_NV12,
221 + .depth = 8,
222 + .bytesperline_align = 32,
223 + .mmal_fmt = MMAL_ENCODING_NV12,
224 + .size_multiplier_x2 = 3,
225 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
226 + .step_size = 2,
227 + }, {
228 + .fourcc = V4L2_PIX_FMT_NV21,
229 + .depth = 8,
230 + .bytesperline_align = 32,
231 + .mmal_fmt = MMAL_ENCODING_NV21,
232 + .size_multiplier_x2 = 3,
233 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
234 + .step_size = 2,
235 + }, {
236 + .fourcc = V4L2_PIX_FMT_YUYV,
237 + .depth = 16,
238 + .bytesperline_align = 64,
239 + .mmal_fmt = MMAL_ENCODING_YUYV,
240 + .size_multiplier_x2 = 2,
241 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
242 + .step_size = 2,
243 + }, {
244 + .fourcc = V4L2_PIX_FMT_UYVY,
245 + .depth = 16,
246 + .bytesperline_align = 64,
247 + .mmal_fmt = MMAL_ENCODING_UYVY,
248 + .size_multiplier_x2 = 2,
249 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
250 + .step_size = 2,
251 + }, {
252 + .fourcc = V4L2_PIX_FMT_YVYU,
253 + .depth = 16,
254 + .bytesperline_align = 64,
255 + .mmal_fmt = MMAL_ENCODING_YVYU,
256 + .size_multiplier_x2 = 2,
257 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
258 + .step_size = 2,
259 + }, {
260 + .fourcc = V4L2_PIX_FMT_VYUY,
261 + .depth = 16,
262 + .bytesperline_align = 64,
263 + .mmal_fmt = MMAL_ENCODING_VYUY,
264 + .size_multiplier_x2 = 2,
265 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
266 + .step_size = 2,
267 + }, {
268 + /* RGB formats */
269 + .fourcc = V4L2_PIX_FMT_RGB24,
270 + .depth = 24,
271 + .bytesperline_align = 32,
272 + .mmal_fmt = MMAL_ENCODING_RGB24,
273 + .size_multiplier_x2 = 2,
274 + .colorspace = V4L2_COLORSPACE_SRGB,
275 + .step_size = 1,
276 + }, {
277 + .fourcc = V4L2_PIX_FMT_RGB565,
278 + .depth = 16,
279 + .bytesperline_align = 32,
280 + .mmal_fmt = MMAL_ENCODING_RGB16,
281 + .size_multiplier_x2 = 2,
282 + .colorspace = V4L2_COLORSPACE_SRGB,
283 + .step_size = 1,
284 + }, {
285 + .fourcc = V4L2_PIX_FMT_BGR24,
286 + .depth = 24,
287 + .bytesperline_align = 32,
288 + .mmal_fmt = MMAL_ENCODING_BGR24,
289 + .size_multiplier_x2 = 2,
290 + .colorspace = V4L2_COLORSPACE_SRGB,
291 + .step_size = 1,
292 + }, {
293 + .fourcc = V4L2_PIX_FMT_XBGR32,
294 + .depth = 32,
295 + .bytesperline_align = 64,
296 + .mmal_fmt = MMAL_ENCODING_BGRA,
297 + .size_multiplier_x2 = 2,
298 + .colorspace = V4L2_COLORSPACE_SRGB,
299 + .step_size = 1,
300 + }, {
301 + .fourcc = V4L2_PIX_FMT_RGBX32,
302 + .depth = 32,
303 + .bytesperline_align = 64,
304 + .mmal_fmt = MMAL_ENCODING_RGBA,
305 + .size_multiplier_x2 = 2,
306 + .colorspace = V4L2_COLORSPACE_SRGB,
307 + .step_size = 1,
308 + }, {
309 + /* Bayer formats */
310 + /* 8 bit */
311 + .fourcc = V4L2_PIX_FMT_SRGGB8,
312 + .depth = 8,
313 + .bytesperline_align = 32,
314 + .mmal_fmt = MMAL_ENCODING_BAYER_SRGGB8,
315 + .size_multiplier_x2 = 2,
316 + .colorspace = V4L2_COLORSPACE_RAW,
317 + .step_size = 2,
318 + }, {
319 + .fourcc = V4L2_PIX_FMT_SBGGR8,
320 + .depth = 8,
321 + .bytesperline_align = 32,
322 + .mmal_fmt = MMAL_ENCODING_BAYER_SBGGR8,
323 + .size_multiplier_x2 = 2,
324 + .colorspace = V4L2_COLORSPACE_RAW,
325 + .step_size = 2,
326 + }, {
327 + .fourcc = V4L2_PIX_FMT_SGRBG8,
328 + .depth = 8,
329 + .bytesperline_align = 32,
330 + .mmal_fmt = MMAL_ENCODING_BAYER_SGRBG8,
331 + .size_multiplier_x2 = 2,
332 + .colorspace = V4L2_COLORSPACE_RAW,
333 + .step_size = 2,
334 + }, {
335 + .fourcc = V4L2_PIX_FMT_SGBRG8,
336 + .depth = 8,
337 + .bytesperline_align = 32,
338 + .mmal_fmt = MMAL_ENCODING_BAYER_SGBRG8,
339 + .size_multiplier_x2 = 2,
340 + .colorspace = V4L2_COLORSPACE_RAW,
341 + .step_size = 2,
342 + }, {
343 + /* 10 bit */
344 + .fourcc = V4L2_PIX_FMT_SRGGB10P,
345 + .depth = 10,
346 + .bytesperline_align = 32,
347 + .mmal_fmt = MMAL_ENCODING_BAYER_SRGGB10P,
348 + .size_multiplier_x2 = 2,
349 + .colorspace = V4L2_COLORSPACE_RAW,
350 + .step_size = 2,
351 + }, {
352 + .fourcc = V4L2_PIX_FMT_SBGGR10P,
353 + .depth = 10,
354 + .bytesperline_align = 32,
355 + .mmal_fmt = MMAL_ENCODING_BAYER_SBGGR10P,
356 + .size_multiplier_x2 = 2,
357 + .colorspace = V4L2_COLORSPACE_RAW,
358 + .step_size = 2,
359 + }, {
360 + .fourcc = V4L2_PIX_FMT_SGRBG10P,
361 + .depth = 10,
362 + .bytesperline_align = 32,
363 + .mmal_fmt = MMAL_ENCODING_BAYER_SGRBG10P,
364 + .size_multiplier_x2 = 2,
365 + .colorspace = V4L2_COLORSPACE_RAW,
366 + .step_size = 2,
367 + }, {
368 + .fourcc = V4L2_PIX_FMT_SGBRG10P,
369 + .depth = 10,
370 + .bytesperline_align = 32,
371 + .mmal_fmt = MMAL_ENCODING_BAYER_SGBRG10P,
372 + .size_multiplier_x2 = 2,
373 + .colorspace = V4L2_COLORSPACE_RAW,
374 + .step_size = 2,
375 + }, {
376 + /* 12 bit */
377 + .fourcc = V4L2_PIX_FMT_SRGGB12P,
378 + .depth = 12,
379 + .bytesperline_align = 32,
380 + .mmal_fmt = MMAL_ENCODING_BAYER_SRGGB12P,
381 + .size_multiplier_x2 = 2,
382 + .colorspace = V4L2_COLORSPACE_RAW,
383 + .step_size = 2,
384 + }, {
385 + .fourcc = V4L2_PIX_FMT_SBGGR12P,
386 + .depth = 12,
387 + .bytesperline_align = 32,
388 + .mmal_fmt = MMAL_ENCODING_BAYER_SBGGR12P,
389 + .size_multiplier_x2 = 2,
390 + .colorspace = V4L2_COLORSPACE_RAW,
391 + .step_size = 2,
392 + }, {
393 + .fourcc = V4L2_PIX_FMT_SGRBG12P,
394 + .depth = 12,
395 + .bytesperline_align = 32,
396 + .mmal_fmt = MMAL_ENCODING_BAYER_SGRBG12P,
397 + .size_multiplier_x2 = 2,
398 + .colorspace = V4L2_COLORSPACE_RAW,
399 + .step_size = 2,
400 + }, {
401 + .fourcc = V4L2_PIX_FMT_SGBRG12P,
402 + .depth = 12,
403 + .bytesperline_align = 32,
404 + .mmal_fmt = MMAL_ENCODING_BAYER_SGBRG12P,
405 + .size_multiplier_x2 = 2,
406 + .colorspace = V4L2_COLORSPACE_RAW,
407 + .step_size = 2,
408 + }, {
409 + /* 14 bit */
410 + .fourcc = V4L2_PIX_FMT_SRGGB14P,
411 + .depth = 14,
412 + .bytesperline_align = 32,
413 + .mmal_fmt = MMAL_ENCODING_BAYER_SRGGB14P,
414 + .size_multiplier_x2 = 2,
415 + .colorspace = V4L2_COLORSPACE_RAW,
416 + .step_size = 2,
417 + }, {
418 + .fourcc = V4L2_PIX_FMT_SBGGR14P,
419 + .depth = 14,
420 + .bytesperline_align = 32,
421 + .mmal_fmt = MMAL_ENCODING_BAYER_SBGGR14P,
422 + .size_multiplier_x2 = 2,
423 + .colorspace = V4L2_COLORSPACE_RAW,
424 + .step_size = 2,
425 + }, {
426 + .fourcc = V4L2_PIX_FMT_SGRBG14P,
427 + .depth = 14,
428 + .bytesperline_align = 32,
429 + .mmal_fmt = MMAL_ENCODING_BAYER_SGRBG14P,
430 + .size_multiplier_x2 = 2,
431 + .colorspace = V4L2_COLORSPACE_RAW,
432 + .step_size = 2,
433 + }, {
434 + .fourcc = V4L2_PIX_FMT_SGBRG14P,
435 + .depth = 14,
436 + .bytesperline_align = 32,
437 + .mmal_fmt = MMAL_ENCODING_BAYER_SGBRG14P,
438 + .size_multiplier_x2 = 2,
439 + .colorspace = V4L2_COLORSPACE_RAW,
440 + .step_size = 2,
441 + }, {
442 + /* 16 bit */
443 + .fourcc = V4L2_PIX_FMT_SRGGB16,
444 + .depth = 16,
445 + .bytesperline_align = 32,
446 + .mmal_fmt = MMAL_ENCODING_BAYER_SRGGB16,
447 + .size_multiplier_x2 = 2,
448 + .colorspace = V4L2_COLORSPACE_RAW,
449 + .step_size = 2,
450 + }, {
451 + .fourcc = V4L2_PIX_FMT_SBGGR16,
452 + .depth = 16,
453 + .bytesperline_align = 32,
454 + .mmal_fmt = MMAL_ENCODING_BAYER_SBGGR16,
455 + .size_multiplier_x2 = 2,
456 + .colorspace = V4L2_COLORSPACE_RAW,
457 + .step_size = 2,
458 + }, {
459 + .fourcc = V4L2_PIX_FMT_SGRBG16,
460 + .depth = 16,
461 + .bytesperline_align = 32,
462 + .mmal_fmt = MMAL_ENCODING_BAYER_SGRBG16,
463 + .size_multiplier_x2 = 2,
464 + .colorspace = V4L2_COLORSPACE_RAW,
465 + .step_size = 2,
466 + }, {
467 + .fourcc = V4L2_PIX_FMT_SGBRG16,
468 + .depth = 16,
469 + .bytesperline_align = 32,
470 + .mmal_fmt = MMAL_ENCODING_BAYER_SGBRG16,
471 + .size_multiplier_x2 = 2,
472 + .colorspace = V4L2_COLORSPACE_RAW,
473 + .step_size = 2,
474 + }, {
475 + /* Monochrome MIPI formats */
476 + /* 8 bit */
477 + .fourcc = V4L2_PIX_FMT_GREY,
478 + .depth = 8,
479 + .bytesperline_align = 32,
480 + .mmal_fmt = MMAL_ENCODING_GREY,
481 + .size_multiplier_x2 = 2,
482 + .colorspace = V4L2_COLORSPACE_RAW,
483 + .step_size = 2,
484 + }, {
485 + /* 10 bit */
486 + .fourcc = V4L2_PIX_FMT_Y10P,
487 + .depth = 10,
488 + .bytesperline_align = 32,
489 + .mmal_fmt = MMAL_ENCODING_Y10P,
490 + .size_multiplier_x2 = 2,
491 + .colorspace = V4L2_COLORSPACE_RAW,
492 + .step_size = 2,
493 + }, {
494 + /* 12 bit */
495 + .fourcc = V4L2_PIX_FMT_Y12P,
496 + .depth = 12,
497 + .bytesperline_align = 32,
498 + .mmal_fmt = MMAL_ENCODING_Y12P,
499 + .size_multiplier_x2 = 2,
500 + .colorspace = V4L2_COLORSPACE_RAW,
501 + .step_size = 2,
502 + }, {
503 + /* 14 bit */
504 + .fourcc = V4L2_PIX_FMT_Y14P,
505 + .depth = 14,
506 + .bytesperline_align = 32,
507 + .mmal_fmt = MMAL_ENCODING_Y14P,
508 + .size_multiplier_x2 = 2,
509 + .colorspace = V4L2_COLORSPACE_RAW,
510 + .step_size = 2,
511 + }, {
512 + /* 16 bit */
513 + .fourcc = V4L2_PIX_FMT_Y16,
514 + .depth = 16,
515 + .bytesperline_align = 32,
516 + .mmal_fmt = MMAL_ENCODING_Y16,
517 + .size_multiplier_x2 = 2,
518 + .colorspace = V4L2_COLORSPACE_RAW,
519 + .step_size = 2,
520 + }, {
521 + .fourcc = V4L2_META_FMT_BCM2835_ISP_STATS,
522 + .mmal_fmt = MMAL_ENCODING_BRCM_STATS,
523 + /* The rest are not valid fields for stats. */
524 + }
525 +};
526 +
527 +#endif
528 --- /dev/null
529 +++ b/drivers/staging/vc04_services/bcm2835-isp/bcm2835-v4l2-isp.c
530 @@ -0,0 +1,1694 @@
531 +// SPDX-License-Identifier: GPL-2.0
532 +/*
533 + * Broadcom BCM2835 ISP driver
534 + *
535 + * Copyright © 2019-2020 Raspberry Pi (Trading) Ltd.
536 + *
537 + * Author: Naushir Patuck (naush@raspberrypi.com)
538 + *
539 + */
540 +
541 +#include <linux/module.h>
542 +#include <linux/platform_device.h>
543 +
544 +#include <media/v4l2-ctrls.h>
545 +#include <media/v4l2-device.h>
546 +#include <media/v4l2-event.h>
547 +#include <media/v4l2-ioctl.h>
548 +#include <media/videobuf2-dma-contig.h>
549 +
550 +#include "vchiq-mmal/mmal-msg.h"
551 +#include "vchiq-mmal/mmal-parameters.h"
552 +#include "vchiq-mmal/mmal-vchiq.h"
553 +
554 +#include "vc-sm-cma/vc_sm_knl.h"
555 +
556 +#include "bcm2835-isp-ctrls.h"
557 +#include "bcm2835-isp-fmts.h"
558 +
559 +static unsigned int debug;
560 +module_param(debug, uint, 0644);
561 +MODULE_PARM_DESC(debug, "activates debug info");
562 +
563 +static unsigned int video_nr = 13;
564 +module_param(video_nr, uint, 0644);
565 +MODULE_PARM_DESC(video_nr, "base video device number");
566 +
567 +#define BCM2835_ISP_NAME "bcm2835-isp"
568 +#define BCM2835_ISP_ENTITY_NAME_LEN 32
569 +
570 +#define BCM2835_ISP_NUM_OUTPUTS 1
571 +#define BCM2835_ISP_NUM_CAPTURES 2
572 +#define BCM2835_ISP_NUM_METADATA 1
573 +
574 +#define BCM2835_ISP_NUM_NODES \
575 + (BCM2835_ISP_NUM_OUTPUTS + BCM2835_ISP_NUM_CAPTURES + \
576 + BCM2835_ISP_NUM_METADATA)
577 +
578 +/* Default frame dimension of 1280 pixels. */
579 +#define DEFAULT_DIM 1280U
580 +/*
581 + * Maximum frame dimension of 16384 pixels. Even though the ISP runs in tiles,
582 + * have a sensible limit so that we do not create an excessive number of tiles
583 + * to process.
584 + */
585 +#define MAX_DIM 16384U
586 +/*
587 + * Minimum frame dimension of 64 pixels. Anything lower, and the tiling
588 + * algorithm may not be able to cope when applying filter context.
589 + */
590 +#define MIN_DIM 64U
591 +
592 +/* Timeout for stop_streaming to allow all buffers to return */
593 +#define COMPLETE_TIMEOUT (2 * HZ)
594 +
595 +/* Per-queue, driver-specific private data */
596 +struct bcm2835_isp_q_data {
597 + /*
598 + * These parameters should be treated as gospel, with everything else
599 + * being determined from them.
600 + */
601 + unsigned int bytesperline;
602 + unsigned int width;
603 + unsigned int height;
604 + unsigned int sizeimage;
605 + const struct bcm2835_isp_fmt *fmt;
606 +};
607 +
608 +/*
609 + * Structure to describe a single node /dev/video<N> which represents a single
610 + * input or output queue to the ISP device.
611 + */
612 +struct bcm2835_isp_node {
613 + int vfl_dir;
614 + unsigned int id;
615 + const char *name;
616 + struct vchiq_mmal_port *port;
617 + struct video_device vfd;
618 + struct media_pad pad;
619 + struct media_intf_devnode *intf_devnode;
620 + struct media_link *intf_link;
621 + struct mutex lock; /* top level device node lock */
622 + struct mutex queue_lock;
623 +
624 + struct vb2_queue queue;
625 + unsigned int sequence;
626 +
627 + /* The list of formats supported on the node. */
628 + struct bcm2835_isp_fmt const **supported_fmts;
629 + unsigned int num_supported_fmts;
630 +
631 + struct bcm2835_isp_q_data q_data;
632 +
633 + /* Parent device structure */
634 + struct bcm2835_isp_dev *dev;
635 +
636 + bool registered;
637 + bool media_node_registered;
638 +};
639 +
640 +/*
641 + * Structure representing the entire ISP device, comprising several input and
642 + * output nodes /dev/video<N>.
643 + */
644 +struct bcm2835_isp_dev {
645 + struct v4l2_device v4l2_dev;
646 + struct device *dev;
647 + struct v4l2_ctrl_handler ctrl_handler;
648 + struct media_device mdev;
649 + struct media_entity entity;
650 + bool media_device_registered;
651 + bool media_entity_registered;
652 + struct vchiq_mmal_instance *mmal_instance;
653 + struct vchiq_mmal_component *component;
654 + struct completion frame_cmplt;
655 +
656 + struct bcm2835_isp_node node[BCM2835_ISP_NUM_NODES];
657 + struct media_pad pad[BCM2835_ISP_NUM_NODES];
658 + atomic_t num_streaming;
659 +
660 + /* Image pipeline controls. */
661 + int r_gain;
662 + int b_gain;
663 +};
664 +
665 +struct bcm2835_isp_buffer {
666 + struct vb2_v4l2_buffer vb;
667 + struct mmal_buffer mmal;
668 +};
669 +
670 +static
671 +inline struct bcm2835_isp_dev *node_get_dev(struct bcm2835_isp_node *node)
672 +{
673 + return node->dev;
674 +}
675 +
676 +static inline bool node_is_output(struct bcm2835_isp_node *node)
677 +{
678 + return node->queue.type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
679 +}
680 +
681 +static inline bool node_is_capture(struct bcm2835_isp_node *node)
682 +{
683 + return node->queue.type == V4L2_BUF_TYPE_VIDEO_CAPTURE;
684 +}
685 +
686 +static inline bool node_is_stats(struct bcm2835_isp_node *node)
687 +{
688 + return node->queue.type == V4L2_BUF_TYPE_META_CAPTURE;
689 +}
690 +
691 +static inline enum v4l2_buf_type index_to_queue_type(int index)
692 +{
693 + if (index < BCM2835_ISP_NUM_OUTPUTS)
694 + return V4L2_BUF_TYPE_VIDEO_OUTPUT;
695 + else if (index < BCM2835_ISP_NUM_OUTPUTS + BCM2835_ISP_NUM_CAPTURES)
696 + return V4L2_BUF_TYPE_VIDEO_CAPTURE;
697 + else
698 + return V4L2_BUF_TYPE_META_CAPTURE;
699 +}
700 +
701 +static int set_isp_param(struct bcm2835_isp_node *node, u32 parameter,
702 + void *value, u32 value_size)
703 +{
704 + struct bcm2835_isp_dev *dev = node_get_dev(node);
705 +
706 + return vchiq_mmal_port_parameter_set(dev->mmal_instance, node->port,
707 + parameter, value, value_size);
708 +}
709 +
710 +static int set_wb_gains(struct bcm2835_isp_node *node)
711 +{
712 + struct bcm2835_isp_dev *dev = node_get_dev(node);
713 + struct mmal_parameter_awbgains gains = {
714 + .r_gain = { dev->r_gain, 1000 },
715 + .b_gain = { dev->b_gain, 1000 }
716 + };
717 +
718 + return set_isp_param(node, MMAL_PARAMETER_CUSTOM_AWB_GAINS,
719 + &gains, sizeof(gains));
720 +}
721 +
722 +static int set_digital_gain(struct bcm2835_isp_node *node, uint32_t gain)
723 +{
724 + struct mmal_parameter_rational digital_gain = {
725 + .num = gain,
726 + .den = 1000
727 + };
728 +
729 + return set_isp_param(node, MMAL_PARAMETER_DIGITAL_GAIN,
730 + &digital_gain, sizeof(digital_gain));
731 +}
732 +
733 +static const struct bcm2835_isp_fmt *get_fmt(u32 mmal_fmt)
734 +{
735 + unsigned int i;
736 +
737 + for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
738 + if (supported_formats[i].mmal_fmt == mmal_fmt)
739 + return &supported_formats[i];
740 + }
741 + return NULL;
742 +}
743 +
744 +static const
745 +struct bcm2835_isp_fmt *find_format_by_fourcc(unsigned int fourcc,
746 + struct bcm2835_isp_node *node)
747 +{
748 + const struct bcm2835_isp_fmt *fmt;
749 + unsigned int i;
750 +
751 + for (i = 0; i < node->num_supported_fmts; i++) {
752 + fmt = node->supported_fmts[i];
753 + if (fmt->fourcc == fourcc)
754 + return fmt;
755 + }
756 +
757 + return NULL;
758 +}
759 +
760 +static const
761 +struct bcm2835_isp_fmt *find_format(struct v4l2_format *f,
762 + struct bcm2835_isp_node *node)
763 +{
764 + return find_format_by_fourcc(node_is_stats(node) ?
765 + f->fmt.meta.dataformat :
766 + f->fmt.pix.pixelformat,
767 + node);
768 +}
769 +
770 +/* vb2_to_mmal_buffer() - converts vb2 buffer header to MMAL
771 + *
772 + * Copies all the required fields from a VB2 buffer to the MMAL buffer header,
773 + * ready for sending to the VPU.
774 + */
775 +static void vb2_to_mmal_buffer(struct mmal_buffer *buf,
776 + struct vb2_v4l2_buffer *vb2)
777 +{
778 + u64 pts;
779 +
780 + buf->mmal_flags = 0;
781 + if (vb2->flags & V4L2_BUF_FLAG_KEYFRAME)
782 + buf->mmal_flags |= MMAL_BUFFER_HEADER_FLAG_KEYFRAME;
783 +
784 + /* Data must be framed correctly as one frame per buffer. */
785 + buf->mmal_flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
786 +
787 + buf->length = vb2->vb2_buf.planes[0].bytesused;
788 + /*
789 + * Minor ambiguity in the V4L2 spec as to whether passing in a 0 length
790 + * buffer, or one with V4L2_BUF_FLAG_LAST set denotes end of stream.
791 + * Handle either.
792 + */
793 + if (!buf->length || vb2->flags & V4L2_BUF_FLAG_LAST)
794 + buf->mmal_flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
795 +
796 + /* vb2 timestamps in nsecs, mmal in usecs */
797 + pts = vb2->vb2_buf.timestamp;
798 + do_div(pts, 1000);
799 + buf->pts = pts;
800 + buf->dts = MMAL_TIME_UNKNOWN;
801 +}
802 +
803 +static void mmal_buffer_cb(struct vchiq_mmal_instance *instance,
804 + struct vchiq_mmal_port *port, int status,
805 + struct mmal_buffer *mmal_buf)
806 +{
807 + struct bcm2835_isp_buffer *q_buf;
808 + struct bcm2835_isp_node *node = port->cb_ctx;
809 + struct bcm2835_isp_dev *dev = node_get_dev(node);
810 + struct vb2_v4l2_buffer *vb2;
811 +
812 + q_buf = container_of(mmal_buf, struct bcm2835_isp_buffer, mmal);
813 + vb2 = &q_buf->vb;
814 + v4l2_dbg(2, debug, &dev->v4l2_dev,
815 + "%s: port:%s[%d], status:%d, buf:%p, dmabuf:%p, length:%lu, flags %u, pts %lld\n",
816 + __func__, node_is_output(node) ? "input" : "output", node->id,
817 + status, mmal_buf, mmal_buf->dma_buf, mmal_buf->length,
818 + mmal_buf->mmal_flags, mmal_buf->pts);
819 +
820 + if (mmal_buf->cmd)
821 + v4l2_err(&dev->v4l2_dev,
822 + "%s: Unexpected event on output callback - %08x\n",
823 + __func__, mmal_buf->cmd);
824 +
825 + if (status) {
826 + /* error in transfer */
827 + if (vb2) {
828 + /* there was a buffer with the error so return it */
829 + vb2_buffer_done(&vb2->vb2_buf, VB2_BUF_STATE_ERROR);
830 + }
831 + return;
832 + }
833 +
834 + /* vb2 timestamps in nsecs, mmal in usecs */
835 + vb2->vb2_buf.timestamp = mmal_buf->pts * 1000;
836 + vb2->sequence = node->sequence++;
837 + vb2_set_plane_payload(&vb2->vb2_buf, 0, mmal_buf->length);
838 + vb2_buffer_done(&vb2->vb2_buf, VB2_BUF_STATE_DONE);
839 +
840 + if (!port->enabled)
841 + complete(&dev->frame_cmplt);
842 +}
843 +
844 +static void setup_mmal_port_format(struct bcm2835_isp_node *node,
845 + struct vchiq_mmal_port *port)
846 +{
847 + struct bcm2835_isp_q_data *q_data = &node->q_data;
848 +
849 + port->format.encoding = q_data->fmt->mmal_fmt;
850 + /* Raw image format - set width/height */
851 + port->es.video.width = (q_data->bytesperline << 3) / q_data->fmt->depth;
852 + port->es.video.height = q_data->height;
853 + port->es.video.crop.width = q_data->width;
854 + port->es.video.crop.height = q_data->height;
855 + port->es.video.crop.x = 0;
856 + port->es.video.crop.y = 0;
857 +};
858 +
859 +static int setup_mmal_port(struct bcm2835_isp_node *node)
860 +{
861 + struct bcm2835_isp_dev *dev = node_get_dev(node);
862 + unsigned int enable = 1;
863 + int ret;
864 +
865 + v4l2_dbg(2, debug, &dev->v4l2_dev, "%s: setup %s[%d]\n", __func__,
866 + node->name, node->id);
867 +
868 + vchiq_mmal_port_parameter_set(dev->mmal_instance, node->port,
869 + MMAL_PARAMETER_ZERO_COPY, &enable,
870 + sizeof(enable));
871 + setup_mmal_port_format(node, node->port);
872 + ret = vchiq_mmal_port_set_format(dev->mmal_instance, node->port);
873 + if (ret < 0) {
874 + v4l2_dbg(1, debug, &dev->v4l2_dev,
875 + "%s: vchiq_mmal_port_set_format failed\n",
876 + __func__);
877 + return ret;
878 + }
879 +
880 + if (node->q_data.sizeimage < node->port->minimum_buffer.size) {
881 + v4l2_err(&dev->v4l2_dev,
882 + "buffer size mismatch sizeimage %u < min size %u\n",
883 + node->q_data.sizeimage,
884 + node->port->minimum_buffer.size);
885 + return -EINVAL;
886 + }
887 +
888 + return 0;
889 +}
890 +
891 +static int bcm2835_isp_mmal_buf_cleanup(struct mmal_buffer *mmal_buf)
892 +{
893 + mmal_vchi_buffer_cleanup(mmal_buf);
894 +
895 + if (mmal_buf->dma_buf) {
896 + dma_buf_put(mmal_buf->dma_buf);
897 + mmal_buf->dma_buf = NULL;
898 + }
899 +
900 + return 0;
901 +}
902 +
903 +static int bcm2835_isp_node_queue_setup(struct vb2_queue *q,
904 + unsigned int *nbuffers,
905 + unsigned int *nplanes,
906 + unsigned int sizes[],
907 + struct device *alloc_devs[])
908 +{
909 + struct bcm2835_isp_node *node = vb2_get_drv_priv(q);
910 + unsigned int size;
911 +
912 + if (setup_mmal_port(node))
913 + return -EINVAL;
914 +
915 + size = node->q_data.sizeimage;
916 + if (size == 0) {
917 + v4l2_info(&node_get_dev(node)->v4l2_dev,
918 + "%s: Image size unset in queue_setup for node %s[%d]\n",
919 + __func__, node->name, node->id);
920 + return -EINVAL;
921 + }
922 +
923 + if (*nplanes)
924 + return sizes[0] < size ? -EINVAL : 0;
925 +
926 + *nplanes = 1;
927 + sizes[0] = size;
928 +
929 + node->port->current_buffer.size = size;
930 +
931 + if (*nbuffers < node->port->minimum_buffer.num)
932 + *nbuffers = node->port->minimum_buffer.num;
933 +
934 + node->port->current_buffer.num = *nbuffers;
935 +
936 + v4l2_dbg(2, debug, &node_get_dev(node)->v4l2_dev,
937 + "%s: Image size %u, nbuffers %u for node %s[%d]\n",
938 + __func__, sizes[0], *nbuffers, node->name, node->id);
939 + return 0;
940 +}
941 +
942 +static int bcm2835_isp_buf_init(struct vb2_buffer *vb)
943 +{
944 + struct bcm2835_isp_node *node = vb2_get_drv_priv(vb->vb2_queue);
945 + struct bcm2835_isp_dev *dev = node_get_dev(node);
946 + struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
947 + struct bcm2835_isp_buffer *buf =
948 + container_of(vb2, struct bcm2835_isp_buffer, vb);
949 +
950 + v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: vb %p\n", __func__, vb);
951 +
952 + buf->mmal.buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
953 + buf->mmal.buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
954 + mmal_vchi_buffer_init(dev->mmal_instance, &buf->mmal);
955 + return 0;
956 +}
957 +
958 +static int bcm2835_isp_buf_prepare(struct vb2_buffer *vb)
959 +{
960 + struct bcm2835_isp_node *node = vb2_get_drv_priv(vb->vb2_queue);
961 + struct bcm2835_isp_dev *dev = node_get_dev(node);
962 + struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
963 + struct bcm2835_isp_buffer *buf =
964 + container_of(vb2, struct bcm2835_isp_buffer, vb);
965 + struct dma_buf *dma_buf;
966 + int ret;
967 +
968 + v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: type: %d ptr %p\n",
969 + __func__, vb->vb2_queue->type, vb);
970 +
971 + if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
972 + if (vb2->field == V4L2_FIELD_ANY)
973 + vb2->field = V4L2_FIELD_NONE;
974 + if (vb2->field != V4L2_FIELD_NONE) {
975 + v4l2_err(&dev->v4l2_dev,
976 + "%s field isn't supported\n", __func__);
977 + return -EINVAL;
978 + }
979 + }
980 +
981 + if (vb2_plane_size(vb, 0) < node->q_data.sizeimage) {
982 + v4l2_err(&dev->v4l2_dev,
983 + "%s data will not fit into plane (%lu < %lu)\n",
984 + __func__, vb2_plane_size(vb, 0),
985 + (long)node->q_data.sizeimage);
986 + return -EINVAL;
987 + }
988 +
989 + if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type))
990 + vb2_set_plane_payload(vb, 0, node->q_data.sizeimage);
991 +
992 + switch (vb->memory) {
993 + case VB2_MEMORY_DMABUF:
994 + dma_buf = dma_buf_get(vb->planes[0].m.fd);
995 +
996 + if (dma_buf != buf->mmal.dma_buf) {
997 + /*
998 + * dmabuf either hasn't already been mapped, or it has
999 + * changed.
1000 + */
1001 + if (buf->mmal.dma_buf) {
1002 + v4l2_err(&dev->v4l2_dev,
1003 + "%s Buffer changed - why did the core not call cleanup?\n",
1004 + __func__);
1005 + bcm2835_isp_mmal_buf_cleanup(&buf->mmal);
1006 + }
1007 +
1008 + buf->mmal.dma_buf = dma_buf;
1009 + } else {
1010 + /*
1011 + * Already have a reference to the buffer, so release it
1012 + * here.
1013 + */
1014 + dma_buf_put(dma_buf);
1015 + }
1016 + ret = 0;
1017 + break;
1018 + case VB2_MEMORY_MMAP:
1019 + /*
1020 + * We want to do this at init, but vb2_core_expbuf checks that
1021 + * the index < q->num_buffers, and q->num_buffers only gets
1022 + * updated once all the buffers are allocated.
1023 + */
1024 + if (!buf->mmal.dma_buf) {
1025 + ret = vb2_core_expbuf_dmabuf(vb->vb2_queue,
1026 + vb->vb2_queue->type,
1027 + vb->index, 0, O_CLOEXEC,
1028 + &buf->mmal.dma_buf);
1029 + v4l2_dbg(3, debug, &dev->v4l2_dev,
1030 + "%s: exporting ptr %p to dmabuf %p\n",
1031 + __func__, vb, buf->mmal.dma_buf);
1032 + if (ret)
1033 + v4l2_err(&dev->v4l2_dev,
1034 + "%s: Failed to expbuf idx %d, ret %d\n",
1035 + __func__, vb->index, ret);
1036 + } else {
1037 + ret = 0;
1038 + }
1039 + break;
1040 + default:
1041 + ret = -EINVAL;
1042 + break;
1043 + }
1044 +
1045 + return ret;
1046 +}
1047 +
1048 +static void bcm2835_isp_node_buffer_queue(struct vb2_buffer *buf)
1049 +{
1050 + struct bcm2835_isp_node *node = vb2_get_drv_priv(buf->vb2_queue);
1051 + struct vb2_v4l2_buffer *vbuf =
1052 + container_of(buf, struct vb2_v4l2_buffer, vb2_buf);
1053 + struct bcm2835_isp_buffer *buffer =
1054 + container_of(vbuf, struct bcm2835_isp_buffer, vb);
1055 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1056 +
1057 + v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: node %s[%d], buffer %p\n",
1058 + __func__, node->name, node->id, buffer);
1059 +
1060 + vb2_to_mmal_buffer(&buffer->mmal, &buffer->vb);
1061 + v4l2_dbg(3, debug, &dev->v4l2_dev,
1062 + "%s: node %s[%d] - submitting mmal dmabuf %p\n", __func__,
1063 + node->name, node->id, buffer->mmal.dma_buf);
1064 + vchiq_mmal_submit_buffer(dev->mmal_instance, node->port, &buffer->mmal);
1065 +}
1066 +
1067 +static void bcm2835_isp_buffer_cleanup(struct vb2_buffer *vb)
1068 +{
1069 + struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
1070 + struct bcm2835_isp_buffer *buffer =
1071 + container_of(vb2, struct bcm2835_isp_buffer, vb);
1072 +
1073 + bcm2835_isp_mmal_buf_cleanup(&buffer->mmal);
1074 +}
1075 +
1076 +static int bcm2835_isp_node_start_streaming(struct vb2_queue *q,
1077 + unsigned int count)
1078 +{
1079 + struct bcm2835_isp_node *node = vb2_get_drv_priv(q);
1080 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1081 + int ret;
1082 +
1083 + v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: node %s[%d] (count %u)\n",
1084 + __func__, node->name, node->id, count);
1085 +
1086 + ret = vchiq_mmal_component_enable(dev->mmal_instance, dev->component);
1087 + if (ret) {
1088 + v4l2_err(&dev->v4l2_dev, "%s: Failed enabling component, ret %d\n",
1089 + __func__, ret);
1090 + return -EIO;
1091 + }
1092 +
1093 + node->sequence = 0;
1094 + node->port->cb_ctx = node;
1095 + ret = vchiq_mmal_port_enable(dev->mmal_instance, node->port,
1096 + mmal_buffer_cb);
1097 + if (!ret)
1098 + atomic_inc(&dev->num_streaming);
1099 + else
1100 + v4l2_err(&dev->v4l2_dev,
1101 + "%s: Failed enabling port, ret %d\n", __func__, ret);
1102 +
1103 + return ret;
1104 +}
1105 +
1106 +static void bcm2835_isp_node_stop_streaming(struct vb2_queue *q)
1107 +{
1108 + struct bcm2835_isp_node *node = vb2_get_drv_priv(q);
1109 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1110 + unsigned int i;
1111 + int ret;
1112 +
1113 + v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: node %s[%d], mmal port %p\n",
1114 + __func__, node->name, node->id, node->port);
1115 +
1116 + init_completion(&dev->frame_cmplt);
1117 +
1118 + /* Disable MMAL port - this will flush buffers back */
1119 + ret = vchiq_mmal_port_disable(dev->mmal_instance, node->port);
1120 + if (ret)
1121 + v4l2_err(&dev->v4l2_dev,
1122 + "%s: Failed disabling %s port, ret %d\n", __func__,
1123 + node_is_output(node) ? "i/p" : "o/p",
1124 + ret);
1125 +
1126 + while (atomic_read(&node->port->buffers_with_vpu)) {
1127 + v4l2_dbg(1, debug, &dev->v4l2_dev,
1128 + "%s: Waiting for buffers to be returned - %d outstanding\n",
1129 + __func__, atomic_read(&node->port->buffers_with_vpu));
1130 + ret = wait_for_completion_timeout(&dev->frame_cmplt,
1131 + COMPLETE_TIMEOUT);
1132 + if (ret <= 0) {
1133 + v4l2_err(&dev->v4l2_dev,
1134 + "%s: Timeout waiting for buffers to be returned - %d outstanding\n",
1135 + __func__,
1136 + atomic_read(&node->port->buffers_with_vpu));
1137 + break;
1138 + }
1139 + }
1140 +
1141 + /* Release the VCSM handle here to release the associated dmabuf */
1142 + for (i = 0; i < q->num_buffers; i++) {
1143 + struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(q->bufs[i]);
1144 + struct bcm2835_isp_buffer *buf =
1145 + container_of(vb2, struct bcm2835_isp_buffer, vb);
1146 + bcm2835_isp_mmal_buf_cleanup(&buf->mmal);
1147 + }
1148 +
1149 + atomic_dec(&dev->num_streaming);
1150 + /* If all ports disabled, then disable the component */
1151 + if (atomic_read(&dev->num_streaming) == 0) {
1152 + ret = vchiq_mmal_component_disable(dev->mmal_instance,
1153 + dev->component);
1154 + if (ret) {
1155 + v4l2_err(&dev->v4l2_dev,
1156 + "%s: Failed disabling component, ret %d\n",
1157 + __func__, ret);
1158 + }
1159 + }
1160 +
1161 + /*
1162 + * Simply wait for any vb2 buffers to finish. We could take steps to
1163 + * make them complete more quickly if we care, or even return them
1164 + * ourselves.
1165 + */
1166 + vb2_wait_for_all_buffers(&node->queue);
1167 +}
1168 +
1169 +static const struct vb2_ops bcm2835_isp_node_queue_ops = {
1170 + .queue_setup = bcm2835_isp_node_queue_setup,
1171 + .buf_init = bcm2835_isp_buf_init,
1172 + .buf_prepare = bcm2835_isp_buf_prepare,
1173 + .buf_queue = bcm2835_isp_node_buffer_queue,
1174 + .buf_cleanup = bcm2835_isp_buffer_cleanup,
1175 + .start_streaming = bcm2835_isp_node_start_streaming,
1176 + .stop_streaming = bcm2835_isp_node_stop_streaming,
1177 +};
1178 +
1179 +static const
1180 +struct bcm2835_isp_fmt *get_default_format(struct bcm2835_isp_node *node)
1181 +{
1182 + return node->supported_fmts[0];
1183 +}
1184 +
1185 +static inline unsigned int get_bytesperline(int width,
1186 + const struct bcm2835_isp_fmt *fmt)
1187 +{
1188 + /* GPU aligns 24bpp images to a multiple of 32 pixels (not bytes). */
1189 + if (fmt->depth == 24)
1190 + return ALIGN(width, 32) * 3;
1191 + else
1192 + return ALIGN((width * fmt->depth) >> 3, fmt->bytesperline_align);
1193 +}
1194 +
1195 +static inline unsigned int get_sizeimage(int bpl, int width, int height,
1196 + const struct bcm2835_isp_fmt *fmt)
1197 +{
1198 + return (bpl * height * fmt->size_multiplier_x2) >> 1;
1199 +}
1200 +
1201 +static int bcm2835_isp_s_ctrl(struct v4l2_ctrl *ctrl)
1202 +{
1203 + struct bcm2835_isp_dev *dev =
1204 + container_of(ctrl->handler, struct bcm2835_isp_dev, ctrl_handler);
1205 + struct bcm2835_isp_node *node = &dev->node[0];
1206 + int ret = 0;
1207 +
1208 + /*
1209 + * The ISP firmware driver will ensure these settings are applied on
1210 + * a frame boundary, so we are safe to write them as they come in.
1211 + *
1212 + * Note that the bcm2835_isp_* param structures are identical to the
1213 + * mmal-parameters.h definitions. This avoids the need for unnecessary
1214 + * field-by-field copying between structures.
1215 + */
1216 + switch (ctrl->id) {
1217 + case V4L2_CID_RED_BALANCE:
1218 + dev->r_gain = ctrl->val;
1219 + ret = set_wb_gains(node);
1220 + break;
1221 + case V4L2_CID_BLUE_BALANCE:
1222 + dev->b_gain = ctrl->val;
1223 + ret = set_wb_gains(node);
1224 + break;
1225 + case V4L2_CID_DIGITAL_GAIN:
1226 + ret = set_digital_gain(node, ctrl->val);
1227 + break;
1228 + case V4L2_CID_USER_BCM2835_ISP_CC_MATRIX:
1229 + ret = set_isp_param(node, MMAL_PARAMETER_CUSTOM_CCM,
1230 + ctrl->p_new.p_u8,
1231 + sizeof(struct bcm2835_isp_custom_ccm));
1232 + break;
1233 + case V4L2_CID_USER_BCM2835_ISP_LENS_SHADING:
1234 + {
1235 + struct bcm2835_isp_lens_shading *v4l2_ls;
1236 + struct mmal_parameter_lens_shading_v2 ls;
1237 + struct dma_buf *dmabuf;
1238 + void *vcsm_handle;
1239 +
1240 + v4l2_ls = (struct bcm2835_isp_lens_shading *)ctrl->p_new.p_u8;
1241 + /*
1242 + * struct bcm2835_isp_lens_shading and struct
1243 + * mmal_parameter_lens_shading_v2 match so that we can do a
1244 + * simple memcpy here.
1245 + * Only the dmabuf to the actual table needs any manipulation.
1246 + */
1247 + memcpy(&ls, v4l2_ls, sizeof(ls));
1248 +
1249 + dmabuf = dma_buf_get(v4l2_ls->dmabuf);
1250 + if (IS_ERR_OR_NULL(dmabuf))
1251 + return -EINVAL;
1252 +
1253 + ret = vc_sm_cma_import_dmabuf(dmabuf, &vcsm_handle);
1254 + if (ret) {
1255 + dma_buf_put(dmabuf);
1256 + return -EINVAL;
1257 + }
1258 +
1259 + ls.mem_handle_table = vc_sm_cma_int_handle(vcsm_handle);
1260 + if (ls.mem_handle_table)
1261 + /* The VPU will take a reference on the vcsm handle,
1262 + * which in turn will retain a reference on the dmabuf.
1263 + * This code can therefore safely release all
1264 + * references to the buffer.
1265 + */
1266 + ret = set_isp_param(node,
1267 + MMAL_PARAMETER_LENS_SHADING_OVERRIDE,
1268 + &ls,
1269 + sizeof(ls));
1270 + else
1271 + ret = -EINVAL;
1272 +
1273 + vc_sm_cma_free(vcsm_handle);
1274 + dma_buf_put(dmabuf);
1275 + break;
1276 + }
1277 + case V4L2_CID_USER_BCM2835_ISP_BLACK_LEVEL:
1278 + ret = set_isp_param(node, MMAL_PARAMETER_BLACK_LEVEL,
1279 + ctrl->p_new.p_u8,
1280 + sizeof(struct bcm2835_isp_black_level));
1281 + break;
1282 + case V4L2_CID_USER_BCM2835_ISP_GEQ:
1283 + ret = set_isp_param(node, MMAL_PARAMETER_GEQ,
1284 + ctrl->p_new.p_u8,
1285 + sizeof(struct bcm2835_isp_geq));
1286 + break;
1287 + case V4L2_CID_USER_BCM2835_ISP_GAMMA:
1288 + ret = set_isp_param(node, MMAL_PARAMETER_GAMMA,
1289 + ctrl->p_new.p_u8,
1290 + sizeof(struct bcm2835_isp_gamma));
1291 + break;
1292 + case V4L2_CID_USER_BCM2835_ISP_DENOISE:
1293 + ret = set_isp_param(node, MMAL_PARAMETER_DENOISE,
1294 + ctrl->p_new.p_u8,
1295 + sizeof(struct bcm2835_isp_denoise));
1296 + break;
1297 + case V4L2_CID_USER_BCM2835_ISP_SHARPEN:
1298 + ret = set_isp_param(node, MMAL_PARAMETER_SHARPEN,
1299 + ctrl->p_new.p_u8,
1300 + sizeof(struct bcm2835_isp_sharpen));
1301 + break;
1302 + case V4L2_CID_USER_BCM2835_ISP_DPC:
1303 + ret = set_isp_param(node, MMAL_PARAMETER_DPC,
1304 + ctrl->p_new.p_u8,
1305 + sizeof(struct bcm2835_isp_dpc));
1306 + break;
1307 + default:
1308 + v4l2_info(&dev->v4l2_dev, "Unrecognised control\n");
1309 + ret = -EINVAL;
1310 + }
1311 +
1312 + if (ret) {
1313 + v4l2_err(&dev->v4l2_dev, "%s: Failed setting ctrl \"%s\" (%08x), err %d\n",
1314 + __func__, ctrl->name, ctrl->id, ret);
1315 + ret = -EIO;
1316 + }
1317 +
1318 + return ret;
1319 +}
1320 +
1321 +static const struct v4l2_ctrl_ops bcm2835_isp_ctrl_ops = {
1322 + .s_ctrl = bcm2835_isp_s_ctrl,
1323 +};
1324 +
1325 +static const struct v4l2_file_operations bcm2835_isp_fops = {
1326 + .owner = THIS_MODULE,
1327 + .open = v4l2_fh_open,
1328 + .release = vb2_fop_release,
1329 + .poll = vb2_fop_poll,
1330 + .unlocked_ioctl = video_ioctl2,
1331 + .mmap = vb2_fop_mmap
1332 +};
1333 +
1334 +static int populate_qdata_fmt(struct v4l2_format *f,
1335 + struct bcm2835_isp_node *node)
1336 +{
1337 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1338 + struct bcm2835_isp_q_data *q_data = &node->q_data;
1339 + int ret;
1340 +
1341 + if (!node_is_stats(node)) {
1342 + v4l2_dbg(1, debug, &dev->v4l2_dev,
1343 + "%s: Setting pix format for type %d, wxh: %ux%u, fmt: %08x, size %u\n",
1344 + __func__, f->type, f->fmt.pix.width, f->fmt.pix.height,
1345 + f->fmt.pix.pixelformat, f->fmt.pix.sizeimage);
1346 +
1347 + q_data->fmt = find_format(f, node);
1348 + q_data->width = f->fmt.pix.width;
1349 + q_data->height = f->fmt.pix.height;
1350 + q_data->height = f->fmt.pix.height;
1351 +
1352 + /* All parameters should have been set correctly by try_fmt */
1353 + q_data->bytesperline = f->fmt.pix.bytesperline;
1354 + q_data->sizeimage = f->fmt.pix.sizeimage;
1355 + } else {
1356 + v4l2_dbg(1, debug, &dev->v4l2_dev,
1357 + "%s: Setting meta format for fmt: %08x, size %u\n",
1358 + __func__, f->fmt.meta.dataformat,
1359 + f->fmt.meta.buffersize);
1360 +
1361 + q_data->fmt = find_format(f, node);
1362 + q_data->width = 0;
1363 + q_data->height = 0;
1364 + q_data->bytesperline = 0;
1365 + q_data->sizeimage = f->fmt.meta.buffersize;
1366 + }
1367 +
1368 + v4l2_dbg(1, debug, &dev->v4l2_dev,
1369 + "%s: Calculated bpl as %u, size %u\n", __func__,
1370 + q_data->bytesperline, q_data->sizeimage);
1371 +
1372 + setup_mmal_port_format(node, node->port);
1373 + ret = vchiq_mmal_port_set_format(dev->mmal_instance, node->port);
1374 + if (ret) {
1375 + v4l2_err(&dev->v4l2_dev,
1376 + "%s: Failed vchiq_mmal_port_set_format on port, ret %d\n",
1377 + __func__, ret);
1378 + ret = -EINVAL;
1379 + }
1380 +
1381 + if (q_data->sizeimage < node->port->minimum_buffer.size) {
1382 + v4l2_err(&dev->v4l2_dev,
1383 + "%s: Current buffer size of %u < min buf size %u - driver mismatch to MMAL\n",
1384 + __func__,
1385 + q_data->sizeimage,
1386 + node->port->minimum_buffer.size);
1387 + }
1388 +
1389 + v4l2_dbg(1, debug, &dev->v4l2_dev,
1390 + "%s: Set format for type %d, wxh: %dx%d, fmt: %08x, size %u\n",
1391 + __func__, f->type, q_data->width, q_data->height,
1392 + q_data->fmt->fourcc, q_data->sizeimage);
1393 +
1394 + return ret;
1395 +}
1396 +
1397 +static int bcm2835_isp_node_querycap(struct file *file, void *priv,
1398 + struct v4l2_capability *cap)
1399 +{
1400 + strscpy(cap->driver, BCM2835_ISP_NAME, sizeof(cap->driver));
1401 + strscpy(cap->card, BCM2835_ISP_NAME, sizeof(cap->card));
1402 + snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1403 + BCM2835_ISP_NAME);
1404 +
1405 + return 0;
1406 +}
1407 +
1408 +static int bcm2835_isp_node_g_fmt(struct file *file, void *priv,
1409 + struct v4l2_format *f)
1410 +{
1411 + struct bcm2835_isp_node *node = video_drvdata(file);
1412 +
1413 + if (f->type != node->queue.type)
1414 + return -EINVAL;
1415 +
1416 + if (node_is_stats(node)) {
1417 + f->fmt.meta.dataformat = V4L2_META_FMT_BCM2835_ISP_STATS;
1418 + f->fmt.meta.buffersize =
1419 + node->port->minimum_buffer.size;
1420 + } else {
1421 + struct bcm2835_isp_q_data *q_data = &node->q_data;
1422 +
1423 + f->fmt.pix.width = q_data->width;
1424 + f->fmt.pix.height = q_data->height;
1425 + f->fmt.pix.field = V4L2_FIELD_NONE;
1426 + f->fmt.pix.pixelformat = q_data->fmt->fourcc;
1427 + f->fmt.pix.bytesperline = q_data->bytesperline;
1428 + f->fmt.pix.sizeimage = q_data->sizeimage;
1429 + f->fmt.pix.colorspace = q_data->fmt->colorspace;
1430 + }
1431 +
1432 + return 0;
1433 +}
1434 +
1435 +static int bcm2835_isp_node_enum_fmt(struct file *file, void *priv,
1436 + struct v4l2_fmtdesc *f)
1437 +{
1438 + struct bcm2835_isp_node *node = video_drvdata(file);
1439 +
1440 + if (f->type != node->queue.type)
1441 + return -EINVAL;
1442 +
1443 + if (f->index < node->num_supported_fmts) {
1444 + /* Format found */
1445 + f->pixelformat = node->supported_fmts[f->index]->fourcc;
1446 + f->flags = 0;
1447 + return 0;
1448 + }
1449 +
1450 + return -EINVAL;
1451 +}
1452 +
1453 +static int bcm2835_isp_enum_framesizes(struct file *file, void *priv,
1454 + struct v4l2_frmsizeenum *fsize)
1455 +{
1456 + struct bcm2835_isp_node *node = video_drvdata(file);
1457 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1458 + const struct bcm2835_isp_fmt *fmt;
1459 +
1460 + if (node_is_stats(node) || fsize->index)
1461 + return -EINVAL;
1462 +
1463 + fmt = find_format_by_fourcc(fsize->pixel_format, node);
1464 + if (!fmt) {
1465 + v4l2_err(&dev->v4l2_dev, "Invalid pixel code: %x\n",
1466 + fsize->pixel_format);
1467 + return -EINVAL;
1468 + }
1469 +
1470 + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1471 + fsize->stepwise.min_width = MIN_DIM;
1472 + fsize->stepwise.max_width = MAX_DIM;
1473 + fsize->stepwise.step_width = fmt->step_size;
1474 +
1475 + fsize->stepwise.min_height = MIN_DIM;
1476 + fsize->stepwise.max_height = MAX_DIM;
1477 + fsize->stepwise.step_height = fmt->step_size;
1478 +
1479 + return 0;
1480 +}
1481 +
1482 +static int bcm2835_isp_node_try_fmt(struct file *file, void *priv,
1483 + struct v4l2_format *f)
1484 +{
1485 + struct bcm2835_isp_node *node = video_drvdata(file);
1486 + const struct bcm2835_isp_fmt *fmt;
1487 +
1488 + if (f->type != node->queue.type)
1489 + return -EINVAL;
1490 +
1491 + fmt = find_format(f, node);
1492 + if (!fmt)
1493 + fmt = get_default_format(node);
1494 +
1495 + if (!node_is_stats(node)) {
1496 + f->fmt.pix.width = max(min(f->fmt.pix.width, MAX_DIM),
1497 + MIN_DIM);
1498 + f->fmt.pix.height = max(min(f->fmt.pix.height, MAX_DIM),
1499 + MIN_DIM);
1500 +
1501 + f->fmt.pix.pixelformat = fmt->fourcc;
1502 + f->fmt.pix.colorspace = fmt->colorspace;
1503 + f->fmt.pix.bytesperline = get_bytesperline(f->fmt.pix.width,
1504 + fmt);
1505 + f->fmt.pix.field = V4L2_FIELD_NONE;
1506 + f->fmt.pix.sizeimage =
1507 + get_sizeimage(f->fmt.pix.bytesperline, f->fmt.pix.width,
1508 + f->fmt.pix.height, fmt);
1509 + } else {
1510 + f->fmt.meta.dataformat = fmt->fourcc;
1511 + f->fmt.meta.buffersize = node->port->minimum_buffer.size;
1512 + }
1513 +
1514 + return 0;
1515 +}
1516 +
1517 +static int bcm2835_isp_node_s_fmt(struct file *file, void *priv,
1518 + struct v4l2_format *f)
1519 +{
1520 + struct bcm2835_isp_node *node = video_drvdata(file);
1521 + int ret;
1522 +
1523 + if (f->type != node->queue.type)
1524 + return -EINVAL;
1525 +
1526 + ret = bcm2835_isp_node_try_fmt(file, priv, f);
1527 + if (ret)
1528 + return ret;
1529 +
1530 + v4l2_dbg(1, debug, &node_get_dev(node)->v4l2_dev,
1531 + "%s: Set format for node %s[%d]\n",
1532 + __func__, node->name, node->id);
1533 +
1534 + return populate_qdata_fmt(f, node);
1535 +}
1536 +
1537 +static int bcm2835_isp_node_s_selection(struct file *file, void *fh,
1538 + struct v4l2_selection *s)
1539 +{
1540 + struct mmal_parameter_crop crop;
1541 + struct bcm2835_isp_node *node = video_drvdata(file);
1542 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1543 +
1544 + /* This return value is required fro V4L2 compliance. */
1545 + if (node_is_stats(node))
1546 + return -ENOTTY;
1547 +
1548 + if (!s->r.width || !s->r.height)
1549 + return -EINVAL;
1550 +
1551 + /* We can only set crop on the input. */
1552 + switch (s->target) {
1553 + case V4L2_SEL_TGT_CROP:
1554 + /*
1555 + * Adjust the crop window if it goes outside of the frame
1556 + * dimensions.
1557 + */
1558 + s->r.left = min((unsigned int)max(s->r.left, 0),
1559 + node->q_data.width - MIN_DIM);
1560 + s->r.top = min((unsigned int)max(s->r.top, 0),
1561 + node->q_data.height - MIN_DIM);
1562 + s->r.width = max(min(s->r.width,
1563 + node->q_data.width - s->r.left), MIN_DIM);
1564 + s->r.height = max(min(s->r.height,
1565 + node->q_data.height - s->r.top), MIN_DIM);
1566 + break;
1567 + case V4L2_SEL_TGT_CROP_DEFAULT:
1568 + /* Default (i.e. no) crop window. */
1569 + s->r.left = 0;
1570 + s->r.top = 0;
1571 + s->r.width = node->q_data.width;
1572 + s->r.height = node->q_data.height;
1573 + break;
1574 + default:
1575 + return -EINVAL;
1576 + }
1577 +
1578 + crop.rect.x = s->r.left;
1579 + crop.rect.y = s->r.top;
1580 + crop.rect.width = s->r.width;
1581 + crop.rect.height = s->r.height;
1582 +
1583 + return vchiq_mmal_port_parameter_set(dev->mmal_instance, node->port,
1584 + MMAL_PARAMETER_CROP,
1585 + &crop, sizeof(crop));
1586 +}
1587 +
1588 +static int bcm2835_isp_node_g_selection(struct file *file, void *fh,
1589 + struct v4l2_selection *s)
1590 +{
1591 + struct mmal_parameter_crop crop;
1592 + struct bcm2835_isp_node *node = video_drvdata(file);
1593 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1594 + u32 crop_size = sizeof(crop);
1595 + int ret;
1596 +
1597 + /* We can only return out an input crop. */
1598 + switch (s->target) {
1599 + case V4L2_SEL_TGT_CROP:
1600 + ret = vchiq_mmal_port_parameter_get(dev->mmal_instance,
1601 + node->port,
1602 + MMAL_PARAMETER_CROP,
1603 + &crop, &crop_size);
1604 + if (!ret) {
1605 + s->r.left = crop.rect.x;
1606 + s->r.top = crop.rect.y;
1607 + s->r.width = crop.rect.width;
1608 + s->r.height = crop.rect.height;
1609 + }
1610 + break;
1611 + case V4L2_SEL_TGT_CROP_DEFAULT:
1612 + case V4L2_SEL_TGT_CROP_BOUNDS:
1613 + /* Default (i.e. no) crop window. */
1614 + s->r.left = 0;
1615 + s->r.top = 0;
1616 + s->r.width = node->q_data.width;
1617 + s->r.height = node->q_data.height;
1618 + ret = 0;
1619 + break;
1620 + default:
1621 + ret = -EINVAL;
1622 + }
1623 +
1624 + return ret;
1625 +}
1626 +
1627 +static int bcm3285_isp_subscribe_event(struct v4l2_fh *fh,
1628 + const struct v4l2_event_subscription *s)
1629 +{
1630 + switch (s->type) {
1631 + /* Cannot change source parameters dynamically at runtime. */
1632 + case V4L2_EVENT_SOURCE_CHANGE:
1633 + return -EINVAL;
1634 + case V4L2_EVENT_CTRL:
1635 + return v4l2_ctrl_subscribe_event(fh, s);
1636 + default:
1637 + return v4l2_event_subscribe(fh, s, 4, NULL);
1638 + }
1639 +}
1640 +
1641 +static const struct v4l2_ioctl_ops bcm2835_isp_node_ioctl_ops = {
1642 + .vidioc_querycap = bcm2835_isp_node_querycap,
1643 + .vidioc_g_fmt_vid_cap = bcm2835_isp_node_g_fmt,
1644 + .vidioc_g_fmt_vid_out = bcm2835_isp_node_g_fmt,
1645 + .vidioc_g_fmt_meta_cap = bcm2835_isp_node_g_fmt,
1646 + .vidioc_s_fmt_vid_cap = bcm2835_isp_node_s_fmt,
1647 + .vidioc_s_fmt_vid_out = bcm2835_isp_node_s_fmt,
1648 + .vidioc_s_fmt_meta_cap = bcm2835_isp_node_s_fmt,
1649 + .vidioc_try_fmt_vid_cap = bcm2835_isp_node_try_fmt,
1650 + .vidioc_try_fmt_vid_out = bcm2835_isp_node_try_fmt,
1651 + .vidioc_try_fmt_meta_cap = bcm2835_isp_node_try_fmt,
1652 + .vidioc_s_selection = bcm2835_isp_node_s_selection,
1653 + .vidioc_g_selection = bcm2835_isp_node_g_selection,
1654 +
1655 + .vidioc_enum_fmt_vid_cap = bcm2835_isp_node_enum_fmt,
1656 + .vidioc_enum_fmt_vid_out = bcm2835_isp_node_enum_fmt,
1657 + .vidioc_enum_fmt_meta_cap = bcm2835_isp_node_enum_fmt,
1658 + .vidioc_enum_framesizes = bcm2835_isp_enum_framesizes,
1659 +
1660 + .vidioc_reqbufs = vb2_ioctl_reqbufs,
1661 + .vidioc_querybuf = vb2_ioctl_querybuf,
1662 + .vidioc_qbuf = vb2_ioctl_qbuf,
1663 + .vidioc_dqbuf = vb2_ioctl_dqbuf,
1664 + .vidioc_expbuf = vb2_ioctl_expbuf,
1665 + .vidioc_create_bufs = vb2_ioctl_create_bufs,
1666 + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1667 +
1668 + .vidioc_streamon = vb2_ioctl_streamon,
1669 + .vidioc_streamoff = vb2_ioctl_streamoff,
1670 +
1671 + .vidioc_subscribe_event = bcm3285_isp_subscribe_event,
1672 + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1673 +};
1674 +
1675 +/*
1676 + * Size of the array to provide to the VPU when asking for the list of supported
1677 + * formats.
1678 + *
1679 + * The ISP component currently advertises 44 input formats, so add a small
1680 + * overhead on that. Should the component advertise more formats then the excess
1681 + * will be dropped and a warning logged.
1682 + */
1683 +#define MAX_SUPPORTED_ENCODINGS 50
1684 +
1685 +/* Populate node->supported_fmts with the formats supported by those ports. */
1686 +static int bcm2835_isp_get_supported_fmts(struct bcm2835_isp_node *node)
1687 +{
1688 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1689 + struct bcm2835_isp_fmt const **list;
1690 + unsigned int i, j, num_encodings;
1691 + u32 fourccs[MAX_SUPPORTED_ENCODINGS];
1692 + u32 param_size = sizeof(fourccs);
1693 + int ret;
1694 +
1695 + ret = vchiq_mmal_port_parameter_get(dev->mmal_instance, node->port,
1696 + MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1697 + &fourccs, &param_size);
1698 +
1699 + if (ret) {
1700 + if (ret == MMAL_MSG_STATUS_ENOSPC) {
1701 + v4l2_err(&dev->v4l2_dev,
1702 + "%s: port has more encoding than we provided space for. Some are dropped.\n",
1703 + __func__);
1704 + num_encodings = MAX_SUPPORTED_ENCODINGS;
1705 + } else {
1706 + v4l2_err(&dev->v4l2_dev, "%s: get_param ret %u.\n",
1707 + __func__, ret);
1708 + return -EINVAL;
1709 + }
1710 + } else {
1711 + num_encodings = param_size / sizeof(u32);
1712 + }
1713 +
1714 + /*
1715 + * Assume at this stage that all encodings will be supported in V4L2.
1716 + * Any that aren't supported will waste a very small amount of memory.
1717 + */
1718 + list = devm_kzalloc(dev->dev,
1719 + sizeof(struct bcm2835_isp_fmt *) * num_encodings,
1720 + GFP_KERNEL);
1721 + if (!list)
1722 + return -ENOMEM;
1723 + node->supported_fmts = list;
1724 +
1725 + for (i = 0, j = 0; i < num_encodings; i++) {
1726 + const struct bcm2835_isp_fmt *fmt = get_fmt(fourccs[i]);
1727 +
1728 + if (fmt) {
1729 + list[j] = fmt;
1730 + j++;
1731 + }
1732 + }
1733 + node->num_supported_fmts = j;
1734 +
1735 + return 0;
1736 +}
1737 +
1738 +/*
1739 + * Register a device node /dev/video<N> to go along with one of the ISP's input
1740 + * or output nodes.
1741 + */
1742 +static int register_node(struct bcm2835_isp_dev *dev,
1743 + struct bcm2835_isp_node *node,
1744 + int index)
1745 +{
1746 + struct video_device *vfd;
1747 + struct vb2_queue *queue;
1748 + int ret;
1749 +
1750 + mutex_init(&node->lock);
1751 +
1752 + node->dev = dev;
1753 + vfd = &node->vfd;
1754 + queue = &node->queue;
1755 + queue->type = index_to_queue_type(index);
1756 + /*
1757 + * Setup the node type-specific params.
1758 + *
1759 + * Only the OUTPUT node can set controls and crop windows. However,
1760 + * we must allow the s/g_selection ioctl on the stats node as v4l2
1761 + * compliance expects it to return a -ENOTTY, and the framework
1762 + * does not handle it if the ioctl is disabled.
1763 + */
1764 + switch (queue->type) {
1765 + case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1766 + vfd->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
1767 + node->id = index;
1768 + node->vfl_dir = VFL_DIR_TX;
1769 + node->name = "output";
1770 + node->port = &dev->component->input[node->id];
1771 + break;
1772 + case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1773 + vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1774 + /* First Capture node starts at id 0, etc. */
1775 + node->id = index - BCM2835_ISP_NUM_OUTPUTS;
1776 + node->vfl_dir = VFL_DIR_RX;
1777 + node->name = "capture";
1778 + node->port = &dev->component->output[node->id];
1779 + v4l2_disable_ioctl(&node->vfd, VIDIOC_S_CTRL);
1780 + v4l2_disable_ioctl(&node->vfd, VIDIOC_S_SELECTION);
1781 + v4l2_disable_ioctl(&node->vfd, VIDIOC_G_SELECTION);
1782 + break;
1783 + case V4L2_BUF_TYPE_META_CAPTURE:
1784 + vfd->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
1785 + node->id = index - BCM2835_ISP_NUM_OUTPUTS;
1786 + node->vfl_dir = VFL_DIR_RX;
1787 + node->name = "stats";
1788 + node->port = &dev->component->output[node->id];
1789 + v4l2_disable_ioctl(&node->vfd, VIDIOC_S_CTRL);
1790 + v4l2_disable_ioctl(&node->vfd, VIDIOC_S_SELECTION);
1791 + v4l2_disable_ioctl(&node->vfd, VIDIOC_G_SELECTION);
1792 + break;
1793 + }
1794 +
1795 + /* We use the selection API instead of the old crop API. */
1796 + v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1797 + v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1798 + v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1799 +
1800 + ret = bcm2835_isp_get_supported_fmts(node);
1801 + if (ret)
1802 + return ret;
1803 +
1804 + /* Initialise the video node. */
1805 + vfd->vfl_type = VFL_TYPE_VIDEO;
1806 + vfd->fops = &bcm2835_isp_fops,
1807 + vfd->ioctl_ops = &bcm2835_isp_node_ioctl_ops,
1808 + vfd->minor = -1,
1809 + vfd->release = video_device_release_empty,
1810 + vfd->queue = &node->queue;
1811 + vfd->lock = &node->lock;
1812 + vfd->v4l2_dev = &dev->v4l2_dev;
1813 + vfd->vfl_dir = node->vfl_dir;
1814 +
1815 + node->q_data.fmt = get_default_format(node);
1816 + node->q_data.width = DEFAULT_DIM;
1817 + node->q_data.height = DEFAULT_DIM;
1818 + node->q_data.bytesperline =
1819 + get_bytesperline(DEFAULT_DIM, node->q_data.fmt);
1820 + node->q_data.sizeimage = node_is_stats(node) ?
1821 + node->port->recommended_buffer.size :
1822 + get_sizeimage(node->q_data.bytesperline,
1823 + node->q_data.width,
1824 + node->q_data.height,
1825 + node->q_data.fmt);
1826 +
1827 + queue->io_modes = VB2_MMAP | VB2_DMABUF;
1828 + queue->drv_priv = node;
1829 + queue->ops = &bcm2835_isp_node_queue_ops;
1830 + queue->mem_ops = &vb2_dma_contig_memops;
1831 + queue->buf_struct_size = sizeof(struct bcm2835_isp_buffer);
1832 + queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1833 + queue->dev = dev->dev;
1834 + queue->lock = &node->queue_lock;
1835 +
1836 + ret = vb2_queue_init(queue);
1837 + if (ret < 0) {
1838 + v4l2_info(&dev->v4l2_dev, "vb2_queue_init failed\n");
1839 + return ret;
1840 + }
1841 +
1842 + /* Set some controls and defaults, but only on the VIDEO_OUTPUT node. */
1843 + if (node_is_output(node)) {
1844 + unsigned int i;
1845 +
1846 + /* Use this ctrl template to assign custom ISP ctrls. */
1847 + struct v4l2_ctrl_config ctrl_template = {
1848 + .ops = &bcm2835_isp_ctrl_ops,
1849 + .type = V4L2_CTRL_TYPE_U8,
1850 + .def = 0,
1851 + .min = 0x00,
1852 + .max = 0xff,
1853 + .step = 1,
1854 + };
1855 +
1856 + /* 3 standard controls, and an array of custom controls */
1857 + ret = v4l2_ctrl_handler_init(&dev->ctrl_handler,
1858 + 3 + ARRAY_SIZE(custom_ctrls));
1859 + if (ret) {
1860 + v4l2_err(&dev->v4l2_dev, "ctrl_handler init failed (%d)\n",
1861 + ret);
1862 + goto queue_cleanup;
1863 + }
1864 +
1865 + dev->r_gain = 1000;
1866 + dev->b_gain = 1000;
1867 +
1868 + v4l2_ctrl_new_std(&dev->ctrl_handler, &bcm2835_isp_ctrl_ops,
1869 + V4L2_CID_RED_BALANCE, 1, 0xffff, 1,
1870 + dev->r_gain);
1871 +
1872 + v4l2_ctrl_new_std(&dev->ctrl_handler, &bcm2835_isp_ctrl_ops,
1873 + V4L2_CID_BLUE_BALANCE, 1, 0xffff, 1,
1874 + dev->b_gain);
1875 +
1876 + v4l2_ctrl_new_std(&dev->ctrl_handler, &bcm2835_isp_ctrl_ops,
1877 + V4L2_CID_DIGITAL_GAIN, 1, 0xffff, 1, 1000);
1878 +
1879 + for (i = 0; i < ARRAY_SIZE(custom_ctrls); i++) {
1880 + ctrl_template.name = custom_ctrls[i].name;
1881 + ctrl_template.id = custom_ctrls[i].id;
1882 + ctrl_template.dims[0] = custom_ctrls[i].size;
1883 + ctrl_template.flags = custom_ctrls[i].flags;
1884 + v4l2_ctrl_new_custom(&dev->ctrl_handler,
1885 + &ctrl_template, NULL);
1886 + }
1887 +
1888 + node->vfd.ctrl_handler = &dev->ctrl_handler;
1889 + if (dev->ctrl_handler.error) {
1890 + ret = dev->ctrl_handler.error;
1891 + v4l2_err(&dev->v4l2_dev, "controls init failed (%d)\n",
1892 + ret);
1893 + v4l2_ctrl_handler_free(&dev->ctrl_handler);
1894 + goto ctrl_cleanup;
1895 + }
1896 + }
1897 +
1898 + /* Define the device names */
1899 + snprintf(vfd->name, sizeof(node->vfd.name), "%s-%s%d", BCM2835_ISP_NAME,
1900 + node->name, node->id);
1901 +
1902 + ret = video_register_device(vfd, VFL_TYPE_VIDEO, video_nr + index);
1903 + if (ret) {
1904 + v4l2_err(&dev->v4l2_dev,
1905 + "Failed to register video %s[%d] device node\n",
1906 + node->name, node->id);
1907 + goto ctrl_cleanup;
1908 + }
1909 +
1910 + node->registered = true;
1911 + video_set_drvdata(vfd, node);
1912 +
1913 + v4l2_info(&dev->v4l2_dev,
1914 + "Device node %s[%d] registered as /dev/video%d\n",
1915 + node->name, node->id, vfd->num);
1916 +
1917 + return 0;
1918 +
1919 +ctrl_cleanup:
1920 + if (node_is_output(node))
1921 + v4l2_ctrl_handler_free(&dev->ctrl_handler);
1922 +queue_cleanup:
1923 + vb2_queue_release(&node->queue);
1924 + return ret;
1925 +}
1926 +
1927 +/* Unregister one of the /dev/video<N> nodes associated with the ISP. */
1928 +static void unregister_node(struct bcm2835_isp_node *node)
1929 +{
1930 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1931 +
1932 + v4l2_info(&dev->v4l2_dev,
1933 + "Unregistering node %s[%d] device node /dev/video%d\n",
1934 + node->name, node->id, node->vfd.num);
1935 +
1936 + if (node->registered) {
1937 + video_unregister_device(&node->vfd);
1938 + if (node_is_output(node))
1939 + v4l2_ctrl_handler_free(&dev->ctrl_handler);
1940 + vb2_queue_release(&node->queue);
1941 + }
1942 +
1943 + /*
1944 + * node->supported_fmts.list is free'd automatically
1945 + * as a managed resource.
1946 + */
1947 + node->supported_fmts = NULL;
1948 + node->num_supported_fmts = 0;
1949 + node->vfd.ctrl_handler = NULL;
1950 + node->registered = false;
1951 +}
1952 +
1953 +static void media_controller_unregister(struct bcm2835_isp_dev *dev)
1954 +{
1955 + unsigned int i;
1956 +
1957 + v4l2_info(&dev->v4l2_dev, "Unregister from media controller\n");
1958 +
1959 + if (dev->media_device_registered) {
1960 + media_device_unregister(&dev->mdev);
1961 + media_device_cleanup(&dev->mdev);
1962 + dev->media_device_registered = false;
1963 + }
1964 +
1965 + kfree(dev->entity.name);
1966 + dev->entity.name = NULL;
1967 +
1968 + if (dev->media_entity_registered) {
1969 + media_device_unregister_entity(&dev->entity);
1970 + dev->media_entity_registered = false;
1971 + }
1972 +
1973 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
1974 + struct bcm2835_isp_node *node = &dev->node[i];
1975 +
1976 + if (node->media_node_registered) {
1977 + media_remove_intf_links(node->intf_link->intf);
1978 + media_entity_remove_links(&dev->node[i].vfd.entity);
1979 + media_devnode_remove(node->intf_devnode);
1980 + media_device_unregister_entity(&node->vfd.entity);
1981 + kfree(node->vfd.entity.name);
1982 + }
1983 + node->media_node_registered = false;
1984 + }
1985 +
1986 + dev->v4l2_dev.mdev = NULL;
1987 +}
1988 +
1989 +static int media_controller_register_node(struct bcm2835_isp_dev *dev, int num)
1990 +{
1991 + struct bcm2835_isp_node *node = &dev->node[num];
1992 + struct media_entity *entity = &node->vfd.entity;
1993 + int output = node_is_output(node);
1994 + char *name;
1995 + int ret;
1996 +
1997 + v4l2_info(&dev->v4l2_dev,
1998 + "Register %s node %d with media controller\n",
1999 + output ? "output" : "capture", num);
2000 + entity->obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
2001 + entity->function = MEDIA_ENT_F_IO_V4L;
2002 + entity->info.dev.major = VIDEO_MAJOR;
2003 + entity->info.dev.minor = node->vfd.minor;
2004 + name = kmalloc(BCM2835_ISP_ENTITY_NAME_LEN, GFP_KERNEL);
2005 + if (!name) {
2006 + ret = -ENOMEM;
2007 + goto error_no_mem;
2008 + }
2009 + snprintf(name, BCM2835_ISP_ENTITY_NAME_LEN, "%s0-%s%d",
2010 + BCM2835_ISP_NAME, output ? "output" : "capture", num);
2011 + entity->name = name;
2012 + node->pad.flags = output ? MEDIA_PAD_FL_SOURCE : MEDIA_PAD_FL_SINK;
2013 + ret = media_entity_pads_init(entity, 1, &node->pad);
2014 + if (ret)
2015 + goto error_pads_init;
2016 + ret = media_device_register_entity(&dev->mdev, entity);
2017 + if (ret)
2018 + goto error_register_entity;
2019 +
2020 + node->intf_devnode = media_devnode_create(&dev->mdev,
2021 + MEDIA_INTF_T_V4L_VIDEO, 0,
2022 + VIDEO_MAJOR, node->vfd.minor);
2023 + if (!node->intf_devnode) {
2024 + ret = -ENOMEM;
2025 + goto error_devnode_create;
2026 + }
2027 +
2028 + node->intf_link = media_create_intf_link(entity,
2029 + &node->intf_devnode->intf,
2030 + MEDIA_LNK_FL_IMMUTABLE |
2031 + MEDIA_LNK_FL_ENABLED);
2032 + if (!node->intf_link) {
2033 + ret = -ENOMEM;
2034 + goto error_create_intf_link;
2035 + }
2036 +
2037 + if (output)
2038 + ret = media_create_pad_link(entity, 0, &dev->entity, num,
2039 + MEDIA_LNK_FL_IMMUTABLE |
2040 + MEDIA_LNK_FL_ENABLED);
2041 + else
2042 + ret = media_create_pad_link(&dev->entity, num, entity, 0,
2043 + MEDIA_LNK_FL_IMMUTABLE |
2044 + MEDIA_LNK_FL_ENABLED);
2045 + if (ret)
2046 + goto error_create_pad_link;
2047 +
2048 + dev->node[num].media_node_registered = true;
2049 + return 0;
2050 +
2051 +error_create_pad_link:
2052 + media_remove_intf_links(&node->intf_devnode->intf);
2053 +error_create_intf_link:
2054 + media_devnode_remove(node->intf_devnode);
2055 +error_devnode_create:
2056 + media_device_unregister_entity(&node->vfd.entity);
2057 +error_register_entity:
2058 +error_pads_init:
2059 + kfree(entity->name);
2060 + entity->name = NULL;
2061 +error_no_mem:
2062 + if (ret)
2063 + v4l2_info(&dev->v4l2_dev, "Error registering node\n");
2064 +
2065 + return ret;
2066 +}
2067 +
2068 +static int media_controller_register(struct bcm2835_isp_dev *dev)
2069 +{
2070 + char *name;
2071 + unsigned int i;
2072 + int ret;
2073 +
2074 + v4l2_dbg(2, debug, &dev->v4l2_dev, "Registering with media controller\n");
2075 + dev->mdev.dev = dev->dev;
2076 + strscpy(dev->mdev.model, "bcm2835-isp",
2077 + sizeof(dev->mdev.model));
2078 + strscpy(dev->mdev.bus_info, "platform:bcm2835-isp",
2079 + sizeof(dev->mdev.bus_info));
2080 + media_device_init(&dev->mdev);
2081 + dev->v4l2_dev.mdev = &dev->mdev;
2082 +
2083 + v4l2_dbg(2, debug, &dev->v4l2_dev, "Register entity for nodes\n");
2084 +
2085 + name = kmalloc(BCM2835_ISP_ENTITY_NAME_LEN, GFP_KERNEL);
2086 + if (!name) {
2087 + ret = -ENOMEM;
2088 + goto done;
2089 + }
2090 + snprintf(name, BCM2835_ISP_ENTITY_NAME_LEN, "bcm2835_isp0");
2091 + dev->entity.name = name;
2092 + dev->entity.obj_type = MEDIA_ENTITY_TYPE_BASE;
2093 + dev->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
2094 +
2095 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
2096 + dev->pad[i].flags = node_is_output(&dev->node[i]) ?
2097 + MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
2098 + }
2099 +
2100 + ret = media_entity_pads_init(&dev->entity, BCM2835_ISP_NUM_NODES,
2101 + dev->pad);
2102 + if (ret)
2103 + goto done;
2104 +
2105 + ret = media_device_register_entity(&dev->mdev, &dev->entity);
2106 + if (ret)
2107 + goto done;
2108 +
2109 + dev->media_entity_registered = true;
2110 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
2111 + ret = media_controller_register_node(dev, i);
2112 + if (ret)
2113 + goto done;
2114 + }
2115 +
2116 + ret = media_device_register(&dev->mdev);
2117 + if (!ret)
2118 + dev->media_device_registered = true;
2119 +done:
2120 + return ret;
2121 +}
2122 +
2123 +static int bcm2835_isp_remove(struct platform_device *pdev)
2124 +{
2125 + struct bcm2835_isp_dev *dev = platform_get_drvdata(pdev);
2126 + unsigned int i;
2127 +
2128 + media_controller_unregister(dev);
2129 +
2130 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++)
2131 + unregister_node(&dev->node[i]);
2132 +
2133 + v4l2_device_unregister(&dev->v4l2_dev);
2134 +
2135 + if (dev->component)
2136 + vchiq_mmal_component_finalise(dev->mmal_instance,
2137 + dev->component);
2138 +
2139 + vchiq_mmal_finalise(dev->mmal_instance);
2140 +
2141 + return 0;
2142 +}
2143 +
2144 +static int bcm2835_isp_probe(struct platform_device *pdev)
2145 +{
2146 + struct bcm2835_isp_dev *dev;
2147 + unsigned int i;
2148 + int ret;
2149 +
2150 + dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2151 + if (!dev)
2152 + return -ENOMEM;
2153 +
2154 + dev->dev = &pdev->dev;
2155 +
2156 + ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2157 + if (ret)
2158 + return ret;
2159 +
2160 + ret = vchiq_mmal_init(&dev->mmal_instance);
2161 + if (ret) {
2162 + v4l2_device_unregister(&dev->v4l2_dev);
2163 + return ret;
2164 + }
2165 +
2166 + ret = vchiq_mmal_component_init(dev->mmal_instance, "ril.isp",
2167 + &dev->component);
2168 + if (ret) {
2169 + v4l2_err(&dev->v4l2_dev,
2170 + "%s: failed to create ril.isp component\n", __func__);
2171 + goto error;
2172 + }
2173 +
2174 + if (dev->component->inputs < BCM2835_ISP_NUM_OUTPUTS ||
2175 + dev->component->outputs < BCM2835_ISP_NUM_CAPTURES +
2176 + BCM2835_ISP_NUM_METADATA) {
2177 + v4l2_err(&dev->v4l2_dev,
2178 + "%s: ril.isp returned %d i/p (%d expected), %d o/p (%d expected) ports\n",
2179 + __func__, dev->component->inputs,
2180 + BCM2835_ISP_NUM_OUTPUTS,
2181 + dev->component->outputs,
2182 + BCM2835_ISP_NUM_CAPTURES + BCM2835_ISP_NUM_METADATA);
2183 + goto error;
2184 + }
2185 +
2186 + atomic_set(&dev->num_streaming, 0);
2187 +
2188 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
2189 + struct bcm2835_isp_node *node = &dev->node[i];
2190 +
2191 + ret = register_node(dev, node, i);
2192 + if (ret)
2193 + goto error;
2194 + }
2195 +
2196 + ret = media_controller_register(dev);
2197 + if (ret)
2198 + goto error;
2199 +
2200 + platform_set_drvdata(pdev, dev);
2201 + v4l2_info(&dev->v4l2_dev, "Loaded V4L2 %s\n", BCM2835_ISP_NAME);
2202 + return 0;
2203 +
2204 +error:
2205 + bcm2835_isp_remove(pdev);
2206 +
2207 + return ret;
2208 +}
2209 +
2210 +static struct platform_driver bcm2835_isp_pdrv = {
2211 + .probe = bcm2835_isp_probe,
2212 + .remove = bcm2835_isp_remove,
2213 + .driver = {
2214 + .name = BCM2835_ISP_NAME,
2215 + },
2216 +};
2217 +
2218 +module_platform_driver(bcm2835_isp_pdrv);
2219 +
2220 +MODULE_DESCRIPTION("BCM2835 ISP driver");
2221 +MODULE_AUTHOR("Naushir Patuck <naush@raspberrypi.com>");
2222 +MODULE_LICENSE("GPL");
2223 +MODULE_VERSION("1.0");
2224 +MODULE_ALIAS("platform:bcm2835-isp");
2225 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-encodings.h
2226 +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-encodings.h
2227 @@ -113,6 +113,10 @@
2228 */
2229 #define MMAL_ENCODING_EGL_IMAGE MMAL_FOURCC('E', 'G', 'L', 'I')
2230
2231 +/** ISP image statistics format
2232 + */
2233 +#define MMAL_ENCODING_BRCM_STATS MMAL_FOURCC('S', 'T', 'A', 'T')
2234 +
2235 /* }@ */
2236
2237 /** \name Pre-defined audio encodings */
2238 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-parameters.h
2239 +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-parameters.h
2240 @@ -221,6 +221,62 @@ enum mmal_parameter_camera_type {
2241 MMAL_PARAMETER_SHUTTER_SPEED,
2242 /**< Takes a @ref MMAL_PARAMETER_AWB_GAINS_T */
2243 MMAL_PARAMETER_CUSTOM_AWB_GAINS,
2244 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_SETTINGS_T */
2245 + MMAL_PARAMETER_CAMERA_SETTINGS,
2246 + /**< Takes a @ref MMAL_PARAMETER_PRIVACY_INDICATOR_T */
2247 + MMAL_PARAMETER_PRIVACY_INDICATOR,
2248 + /**< Takes a @ref MMAL_PARAMETER_BOOLEAN_T */
2249 + MMAL_PARAMETER_VIDEO_DENOISE,
2250 + /**< Takes a @ref MMAL_PARAMETER_BOOLEAN_T */
2251 + MMAL_PARAMETER_STILLS_DENOISE,
2252 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_ANNOTATE_T */
2253 + MMAL_PARAMETER_ANNOTATE,
2254 + /**< Takes a @ref MMAL_PARAMETER_STEREOSCOPIC_MODE_T */
2255 + MMAL_PARAMETER_STEREOSCOPIC_MODE,
2256 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_INTERFACE_T */
2257 + MMAL_PARAMETER_CAMERA_INTERFACE,
2258 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_CLOCKING_MODE_T */
2259 + MMAL_PARAMETER_CAMERA_CLOCKING_MODE,
2260 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_RX_CONFIG_T */
2261 + MMAL_PARAMETER_CAMERA_RX_CONFIG,
2262 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_RX_TIMING_T */
2263 + MMAL_PARAMETER_CAMERA_RX_TIMING,
2264 + /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
2265 + MMAL_PARAMETER_DPF_CONFIG,
2266 +
2267 + /* 0x50 */
2268 + /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
2269 + MMAL_PARAMETER_JPEG_RESTART_INTERVAL,
2270 + /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
2271 + MMAL_PARAMETER_CAMERA_ISP_BLOCK_OVERRIDE,
2272 + /**< Takes a @ref MMAL_PARAMETER_LENS_SHADING_T */
2273 + MMAL_PARAMETER_LENS_SHADING_OVERRIDE,
2274 + /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
2275 + MMAL_PARAMETER_BLACK_LEVEL,
2276 + /**< Takes a @ref MMAL_PARAMETER_RESIZE_T */
2277 + MMAL_PARAMETER_RESIZE_PARAMS,
2278 + /**< Takes a @ref MMAL_PARAMETER_CROP_T */
2279 + MMAL_PARAMETER_CROP,
2280 + /**< Takes a @ref MMAL_PARAMETER_INT32_T */
2281 + MMAL_PARAMETER_OUTPUT_SHIFT,
2282 + /**< Takes a @ref MMAL_PARAMETER_INT32_T */
2283 + MMAL_PARAMETER_CCM_SHIFT,
2284 + /**< Takes a @ref MMAL_PARAMETER_CUSTOM_CCM_T */
2285 + MMAL_PARAMETER_CUSTOM_CCM,
2286 + /**< Takes a @ref MMAL_PARAMETER_RATIONAL_T */
2287 + MMAL_PARAMETER_ANALOG_GAIN,
2288 + /**< Takes a @ref MMAL_PARAMETER_RATIONAL_T */
2289 + MMAL_PARAMETER_DIGITAL_GAIN,
2290 + /**< Takes a @ref MMAL_PARAMETER_DENOISE_T */
2291 + MMAL_PARAMETER_DENOISE,
2292 + /**< Takes a @ref MMAL_PARAMETER_SHARPEN_T */
2293 + MMAL_PARAMETER_SHARPEN,
2294 + /**< Takes a @ref MMAL_PARAMETER_GEQ_T */
2295 + MMAL_PARAMETER_GEQ,
2296 + /**< Tales a @ref MMAP_PARAMETER_DPC_T */
2297 + MMAL_PARAMETER_DPC,
2298 + /**< Tales a @ref MMAP_PARAMETER_GAMMA_T */
2299 + MMAL_PARAMETER_GAMMA,
2300 };
2301
2302 struct mmal_parameter_rational {
2303 @@ -789,7 +845,102 @@ struct mmal_parameter_camera_info {
2304 struct mmal_parameter_camera_info_camera
2305 cameras[MMAL_PARAMETER_CAMERA_INFO_MAX_CAMERAS];
2306 struct mmal_parameter_camera_info_flash
2307 - flashes[MMAL_PARAMETER_CAMERA_INFO_MAX_FLASHES];
2308 + flashes[MMAL_PARAMETER_CAMERA_INFO_MAX_FLASHES];
2309 +};
2310 +
2311 +struct mmal_parameter_ccm {
2312 + struct mmal_parameter_rational ccm[3][3];
2313 + s32 offsets[3];
2314 +};
2315 +
2316 +struct mmal_parameter_custom_ccm {
2317 + u32 enabled; /**< Enable the custom CCM. */
2318 + struct mmal_parameter_ccm ccm; /**< CCM to be used. */
2319 +};
2320 +
2321 +struct mmal_parameter_lens_shading {
2322 + u32 enabled;
2323 + u32 grid_cell_size;
2324 + u32 grid_width;
2325 + u32 grid_stride;
2326 + u32 grid_height;
2327 + u32 mem_handle_table;
2328 + u32 ref_transform;
2329 +};
2330 +
2331 +enum mmal_parameter_ls_gain_format_type {
2332 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U0P8_1 = 0,
2333 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U1P7_0 = 1,
2334 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U1P7_1 = 2,
2335 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U2P6_0 = 3,
2336 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U2P6_1 = 4,
2337 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U3P5_0 = 5,
2338 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U3P5_1 = 6,
2339 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U4P10 = 7,
2340 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_DUMMY = 0x7FFFFFFF
2341 +};
2342 +
2343 +struct mmal_parameter_lens_shading_v2 {
2344 + u32 enabled;
2345 + u32 grid_cell_size;
2346 + u32 grid_width;
2347 + u32 grid_stride;
2348 + u32 grid_height;
2349 + u32 mem_handle_table;
2350 + u32 ref_transform;
2351 + u32 corner_sampled;
2352 + enum mmal_parameter_ls_gain_format_type gain_format;
2353 +};
2354 +
2355 +struct mmal_parameter_black_level {
2356 + u32 enabled;
2357 + u16 black_level_r;
2358 + u16 black_level_g;
2359 + u16 black_level_b;
2360 + u8 pad_[2]; /* Unused */
2361 +};
2362 +
2363 +struct mmal_parameter_geq {
2364 + u32 enabled;
2365 + u32 offset;
2366 + struct mmal_parameter_rational slope;
2367 +};
2368 +
2369 +#define MMAL_NUM_GAMMA_PTS 33
2370 +struct mmal_parameter_gamma {
2371 + u32 enabled;
2372 + u16 x[MMAL_NUM_GAMMA_PTS];
2373 + u16 y[MMAL_NUM_GAMMA_PTS];
2374 +};
2375 +
2376 +struct mmal_parameter_denoise {
2377 + u32 enabled;
2378 + u32 constant;
2379 + struct mmal_parameter_rational slope;
2380 + struct mmal_parameter_rational strength;
2381 +};
2382 +
2383 +struct mmal_parameter_sharpen {
2384 + u32 enabled;
2385 + struct mmal_parameter_rational threshold;
2386 + struct mmal_parameter_rational strength;
2387 + struct mmal_parameter_rational limit;
2388 +};
2389 +
2390 +enum mmal_dpc_mode {
2391 + MMAL_DPC_MODE_OFF = 0,
2392 + MMAL_DPC_MODE_NORMAL = 1,
2393 + MMAL_DPC_MODE_STRONG = 2,
2394 + MMAL_DPC_MODE_MAX = 0x7FFFFFFF,
2395 +};
2396 +
2397 +struct mmal_parameter_dpc {
2398 + u32 enabled;
2399 + u32 strength;
2400 +};
2401 +
2402 +struct mmal_parameter_crop {
2403 + struct vchiq_mmal_rect rect;
2404 };
2405
2406 #endif