1 From cfa60cc9d644b8d12886332eceb7449e7397dff2 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
7 Driver for the BCM2835 ISP hardware block. This driver uses the MMAL
8 component to program the ISP hardware through the VC firmware.
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.
14 This patch squashes all the development patches from the earlier
15 rpi-5.4.y branch into one
17 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
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 | 1696 +++++++++++++++++
27 .../vc04_services/vchiq-mmal/mmal-encodings.h | 4 +
28 .../vchiq-mmal/mmal-parameters.h | 153 +-
29 10 files changed, 2305 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
38 @@ -4044,6 +4044,15 @@ S: Maintained
39 F: drivers/media/platform/bcm2835/
40 F: Documentation/devicetree/bindings/media/brcm,bcm2835-unicam.yaml
42 +BROADCOM BCM2835 ISP DRIVER
43 +M: Raspberry Pi Kernel Maintenance <kernel-list@raspberrypi.com>
44 +L: linux-media@vger.kernel.org
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
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 @@ -46,6 +46,7 @@ source "drivers/staging/vc04_services/bc
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"
62 source "drivers/staging/vc04_services/vchiq-mmal/Kconfig"
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/
72 ccflags-y += -I $(srctree)/$(src)/include
75 +++ b/drivers/staging/vc04_services/bcm2835-isp/Kconfig
77 +config VIDEO_ISP_BCM2835
78 + tristate "BCM2835 ISP support"
79 + depends on MEDIA_SUPPORT
80 + depends on VIDEO_DEV && (ARCH_BCM2835 || COMPILE_TEST)
81 + depends on MEDIA_CONTROLLER
82 + select BCM2835_VCHIQ_MMAL
83 + select VIDEOBUF2_DMA_CONTIG
85 + This is the V4L2 driver for the Broadcom BCM2835 ISP hardware.
86 + This operates over the VCHIQ interface to a service running on
89 + To compile this driver as a module, choose M here: the module
90 + will be called bcm2835-isp.
92 +++ b/drivers/staging/vc04_services/bcm2835-isp/Makefile
94 +# SPDX-License-Identifier: GPL-2.0
95 +bcm2835-isp-objs := bcm2835-v4l2-isp.o
97 +obj-$(CONFIG_VIDEO_ISP_BCM2835) += bcm2835-isp.o
100 + -I$(srctree)/drivers/staging/vc04_services \
101 + -D__VCCOREVER__=0x04000000
103 +++ b/drivers/staging/vc04_services/bcm2835-isp/bcm2835-isp-ctrls.h
105 +/* SPDX-License-Identifier: GPL-2.0 */
107 + * Broadcom BCM2835 ISP driver
109 + * Copyright © 2019-2020 Raspberry Pi (Trading) Ltd.
111 + * Author: Naushir Patuck (naush@raspberrypi.com)
115 +#ifndef BCM2835_ISP_CTRLS
116 +#define BCM2835_ISP_CTRLS
118 +#include <linux/bcm2835-isp.h>
120 +struct bcm2835_isp_custom_ctrl {
127 +static const struct bcm2835_isp_custom_ctrl custom_ctrls[] = {
129 + .name = "Colour Correction Matrix",
130 + .id = V4L2_CID_USER_BCM2835_ISP_CC_MATRIX,
131 + .size = sizeof(struct bcm2835_isp_custom_ccm),
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
139 + .name = "Black Level",
140 + .id = V4L2_CID_USER_BCM2835_ISP_BLACK_LEVEL,
141 + .size = sizeof(struct bcm2835_isp_black_level),
144 + .name = "Green Equalisation",
145 + .id = V4L2_CID_USER_BCM2835_ISP_GEQ,
146 + .size = sizeof(struct bcm2835_isp_geq),
150 + .id = V4L2_CID_USER_BCM2835_ISP_GAMMA,
151 + .size = sizeof(struct bcm2835_isp_gamma),
155 + .id = V4L2_CID_USER_BCM2835_ISP_SHARPEN,
156 + .size = sizeof(struct bcm2835_isp_sharpen),
160 + .id = V4L2_CID_USER_BCM2835_ISP_DENOISE,
161 + .size = sizeof(struct bcm2835_isp_denoise),
164 + .name = "Defective Pixel Correction",
165 + .id = V4L2_CID_USER_BCM2835_ISP_DPC,
166 + .size = sizeof(struct bcm2835_isp_dpc),
173 +++ b/drivers/staging/vc04_services/bcm2835-isp/bcm2835-isp-fmts.h
175 +/* SPDX-License-Identifier: GPL-2.0 */
177 + * Broadcom BCM2835 ISP driver
179 + * Copyright © 2019-2020 Raspberry Pi (Trading) Ltd.
181 + * Author: Naushir Patuck (naush@raspberrypi.com)
185 +#ifndef BCM2835_ISP_FMTS
186 +#define BCM2835_ISP_FMTS
188 +#include <linux/videodev2.h>
189 +#include "vchiq-mmal/mmal-encodings.h"
191 +struct bcm2835_isp_fmt {
194 + int bytesperline_align;
196 + int size_multiplier_x2;
197 + enum v4l2_colorspace colorspace;
198 + unsigned int step_size;
201 +static const struct bcm2835_isp_fmt supported_formats[] = {
204 + .fourcc = V4L2_PIX_FMT_YUV420,
206 + .bytesperline_align = 32,
207 + .mmal_fmt = MMAL_ENCODING_I420,
208 + .size_multiplier_x2 = 3,
209 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
212 + .fourcc = V4L2_PIX_FMT_YVU420,
214 + .bytesperline_align = 32,
215 + .mmal_fmt = MMAL_ENCODING_YV12,
216 + .size_multiplier_x2 = 3,
217 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
220 + .fourcc = V4L2_PIX_FMT_NV12,
222 + .bytesperline_align = 32,
223 + .mmal_fmt = MMAL_ENCODING_NV12,
224 + .size_multiplier_x2 = 3,
225 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
228 + .fourcc = V4L2_PIX_FMT_NV21,
230 + .bytesperline_align = 32,
231 + .mmal_fmt = MMAL_ENCODING_NV21,
232 + .size_multiplier_x2 = 3,
233 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
236 + .fourcc = V4L2_PIX_FMT_YUYV,
238 + .bytesperline_align = 64,
239 + .mmal_fmt = MMAL_ENCODING_YUYV,
240 + .size_multiplier_x2 = 2,
241 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
244 + .fourcc = V4L2_PIX_FMT_UYVY,
246 + .bytesperline_align = 64,
247 + .mmal_fmt = MMAL_ENCODING_UYVY,
248 + .size_multiplier_x2 = 2,
249 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
252 + .fourcc = V4L2_PIX_FMT_YVYU,
254 + .bytesperline_align = 64,
255 + .mmal_fmt = MMAL_ENCODING_YVYU,
256 + .size_multiplier_x2 = 2,
257 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
260 + .fourcc = V4L2_PIX_FMT_VYUY,
262 + .bytesperline_align = 64,
263 + .mmal_fmt = MMAL_ENCODING_VYUY,
264 + .size_multiplier_x2 = 2,
265 + .colorspace = V4L2_COLORSPACE_SMPTE170M,
269 + .fourcc = V4L2_PIX_FMT_RGB24,
271 + .bytesperline_align = 32,
272 + .mmal_fmt = MMAL_ENCODING_RGB24,
273 + .size_multiplier_x2 = 2,
274 + .colorspace = V4L2_COLORSPACE_SRGB,
277 + .fourcc = V4L2_PIX_FMT_RGB565,
279 + .bytesperline_align = 32,
280 + .mmal_fmt = MMAL_ENCODING_RGB16,
281 + .size_multiplier_x2 = 2,
282 + .colorspace = V4L2_COLORSPACE_SRGB,
285 + .fourcc = V4L2_PIX_FMT_BGR24,
287 + .bytesperline_align = 32,
288 + .mmal_fmt = MMAL_ENCODING_BGR24,
289 + .size_multiplier_x2 = 2,
290 + .colorspace = V4L2_COLORSPACE_SRGB,
293 + .fourcc = V4L2_PIX_FMT_XBGR32,
295 + .bytesperline_align = 64,
296 + .mmal_fmt = MMAL_ENCODING_BGRA,
297 + .size_multiplier_x2 = 2,
298 + .colorspace = V4L2_COLORSPACE_SRGB,
301 + .fourcc = V4L2_PIX_FMT_RGBX32,
303 + .bytesperline_align = 64,
304 + .mmal_fmt = MMAL_ENCODING_RGBA,
305 + .size_multiplier_x2 = 2,
306 + .colorspace = V4L2_COLORSPACE_SRGB,
309 + /* Bayer formats */
311 + .fourcc = V4L2_PIX_FMT_SRGGB8,
313 + .bytesperline_align = 32,
314 + .mmal_fmt = MMAL_ENCODING_BAYER_SRGGB8,
315 + .size_multiplier_x2 = 2,
316 + .colorspace = V4L2_COLORSPACE_RAW,
319 + .fourcc = V4L2_PIX_FMT_SBGGR8,
321 + .bytesperline_align = 32,
322 + .mmal_fmt = MMAL_ENCODING_BAYER_SBGGR8,
323 + .size_multiplier_x2 = 2,
324 + .colorspace = V4L2_COLORSPACE_RAW,
327 + .fourcc = V4L2_PIX_FMT_SGRBG8,
329 + .bytesperline_align = 32,
330 + .mmal_fmt = MMAL_ENCODING_BAYER_SGRBG8,
331 + .size_multiplier_x2 = 2,
332 + .colorspace = V4L2_COLORSPACE_RAW,
335 + .fourcc = V4L2_PIX_FMT_SGBRG8,
337 + .bytesperline_align = 32,
338 + .mmal_fmt = MMAL_ENCODING_BAYER_SGBRG8,
339 + .size_multiplier_x2 = 2,
340 + .colorspace = V4L2_COLORSPACE_RAW,
344 + .fourcc = V4L2_PIX_FMT_SRGGB10P,
346 + .bytesperline_align = 32,
347 + .mmal_fmt = MMAL_ENCODING_BAYER_SRGGB10P,
348 + .size_multiplier_x2 = 2,
349 + .colorspace = V4L2_COLORSPACE_RAW,
352 + .fourcc = V4L2_PIX_FMT_SBGGR10P,
354 + .bytesperline_align = 32,
355 + .mmal_fmt = MMAL_ENCODING_BAYER_SBGGR10P,
356 + .size_multiplier_x2 = 2,
357 + .colorspace = V4L2_COLORSPACE_RAW,
360 + .fourcc = V4L2_PIX_FMT_SGRBG10P,
362 + .bytesperline_align = 32,
363 + .mmal_fmt = MMAL_ENCODING_BAYER_SGRBG10P,
364 + .size_multiplier_x2 = 2,
365 + .colorspace = V4L2_COLORSPACE_RAW,
368 + .fourcc = V4L2_PIX_FMT_SGBRG10P,
370 + .bytesperline_align = 32,
371 + .mmal_fmt = MMAL_ENCODING_BAYER_SGBRG10P,
372 + .size_multiplier_x2 = 2,
373 + .colorspace = V4L2_COLORSPACE_RAW,
377 + .fourcc = V4L2_PIX_FMT_SRGGB12P,
379 + .bytesperline_align = 32,
380 + .mmal_fmt = MMAL_ENCODING_BAYER_SRGGB12P,
381 + .size_multiplier_x2 = 2,
382 + .colorspace = V4L2_COLORSPACE_RAW,
385 + .fourcc = V4L2_PIX_FMT_SBGGR12P,
387 + .bytesperline_align = 32,
388 + .mmal_fmt = MMAL_ENCODING_BAYER_SBGGR12P,
389 + .size_multiplier_x2 = 2,
390 + .colorspace = V4L2_COLORSPACE_RAW,
393 + .fourcc = V4L2_PIX_FMT_SGRBG12P,
395 + .bytesperline_align = 32,
396 + .mmal_fmt = MMAL_ENCODING_BAYER_SGRBG12P,
397 + .size_multiplier_x2 = 2,
398 + .colorspace = V4L2_COLORSPACE_RAW,
401 + .fourcc = V4L2_PIX_FMT_SGBRG12P,
403 + .bytesperline_align = 32,
404 + .mmal_fmt = MMAL_ENCODING_BAYER_SGBRG12P,
405 + .size_multiplier_x2 = 2,
406 + .colorspace = V4L2_COLORSPACE_RAW,
410 + .fourcc = V4L2_PIX_FMT_SRGGB14P,
412 + .bytesperline_align = 32,
413 + .mmal_fmt = MMAL_ENCODING_BAYER_SRGGB14P,
414 + .size_multiplier_x2 = 2,
415 + .colorspace = V4L2_COLORSPACE_RAW,
418 + .fourcc = V4L2_PIX_FMT_SBGGR14P,
420 + .bytesperline_align = 32,
421 + .mmal_fmt = MMAL_ENCODING_BAYER_SBGGR14P,
422 + .size_multiplier_x2 = 2,
423 + .colorspace = V4L2_COLORSPACE_RAW,
426 + .fourcc = V4L2_PIX_FMT_SGRBG14P,
428 + .bytesperline_align = 32,
429 + .mmal_fmt = MMAL_ENCODING_BAYER_SGRBG14P,
430 + .size_multiplier_x2 = 2,
431 + .colorspace = V4L2_COLORSPACE_RAW,
434 + .fourcc = V4L2_PIX_FMT_SGBRG14P,
436 + .bytesperline_align = 32,
437 + .mmal_fmt = MMAL_ENCODING_BAYER_SGBRG14P,
438 + .size_multiplier_x2 = 2,
439 + .colorspace = V4L2_COLORSPACE_RAW,
443 + .fourcc = V4L2_PIX_FMT_SRGGB16,
445 + .bytesperline_align = 32,
446 + .mmal_fmt = MMAL_ENCODING_BAYER_SRGGB16,
447 + .size_multiplier_x2 = 2,
448 + .colorspace = V4L2_COLORSPACE_RAW,
451 + .fourcc = V4L2_PIX_FMT_SBGGR16,
453 + .bytesperline_align = 32,
454 + .mmal_fmt = MMAL_ENCODING_BAYER_SBGGR16,
455 + .size_multiplier_x2 = 2,
456 + .colorspace = V4L2_COLORSPACE_RAW,
459 + .fourcc = V4L2_PIX_FMT_SGRBG16,
461 + .bytesperline_align = 32,
462 + .mmal_fmt = MMAL_ENCODING_BAYER_SGRBG16,
463 + .size_multiplier_x2 = 2,
464 + .colorspace = V4L2_COLORSPACE_RAW,
467 + .fourcc = V4L2_PIX_FMT_SGBRG16,
469 + .bytesperline_align = 32,
470 + .mmal_fmt = MMAL_ENCODING_BAYER_SGBRG16,
471 + .size_multiplier_x2 = 2,
472 + .colorspace = V4L2_COLORSPACE_RAW,
475 + /* Monochrome MIPI formats */
477 + .fourcc = V4L2_PIX_FMT_GREY,
479 + .bytesperline_align = 32,
480 + .mmal_fmt = MMAL_ENCODING_GREY,
481 + .size_multiplier_x2 = 2,
482 + .colorspace = V4L2_COLORSPACE_RAW,
486 + .fourcc = V4L2_PIX_FMT_Y10P,
488 + .bytesperline_align = 32,
489 + .mmal_fmt = MMAL_ENCODING_Y10P,
490 + .size_multiplier_x2 = 2,
491 + .colorspace = V4L2_COLORSPACE_RAW,
495 + .fourcc = V4L2_PIX_FMT_Y12P,
497 + .bytesperline_align = 32,
498 + .mmal_fmt = MMAL_ENCODING_Y12P,
499 + .size_multiplier_x2 = 2,
500 + .colorspace = V4L2_COLORSPACE_RAW,
504 + .fourcc = V4L2_PIX_FMT_Y14P,
506 + .bytesperline_align = 32,
507 + .mmal_fmt = MMAL_ENCODING_Y14P,
508 + .size_multiplier_x2 = 2,
509 + .colorspace = V4L2_COLORSPACE_RAW,
513 + .fourcc = V4L2_PIX_FMT_Y16,
515 + .bytesperline_align = 32,
516 + .mmal_fmt = MMAL_ENCODING_Y16,
517 + .size_multiplier_x2 = 2,
518 + .colorspace = V4L2_COLORSPACE_RAW,
521 + .fourcc = V4L2_META_FMT_BCM2835_ISP_STATS,
522 + .mmal_fmt = MMAL_ENCODING_BRCM_STATS,
523 + /* The rest are not valid fields for stats. */
529 +++ b/drivers/staging/vc04_services/bcm2835-isp/bcm2835-v4l2-isp.c
531 +// SPDX-License-Identifier: GPL-2.0
533 + * Broadcom BCM2835 ISP driver
535 + * Copyright © 2019-2020 Raspberry Pi (Trading) Ltd.
537 + * Author: Naushir Patuck (naush@raspberrypi.com)
541 +#include <linux/module.h>
542 +#include <linux/platform_device.h>
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>
550 +#include "vchiq-mmal/mmal-msg.h"
551 +#include "vchiq-mmal/mmal-parameters.h"
552 +#include "vchiq-mmal/mmal-vchiq.h"
554 +#include "vc-sm-cma/vc_sm_knl.h"
556 +#include "bcm2835-isp-ctrls.h"
557 +#include "bcm2835-isp-fmts.h"
559 +MODULE_IMPORT_NS(DMA_BUF);
561 +static unsigned int debug;
562 +module_param(debug, uint, 0644);
563 +MODULE_PARM_DESC(debug, "activates debug info");
565 +static unsigned int video_nr = 13;
566 +module_param(video_nr, uint, 0644);
567 +MODULE_PARM_DESC(video_nr, "base video device number");
569 +#define BCM2835_ISP_NAME "bcm2835-isp"
570 +#define BCM2835_ISP_ENTITY_NAME_LEN 32
572 +#define BCM2835_ISP_NUM_OUTPUTS 1
573 +#define BCM2835_ISP_NUM_CAPTURES 2
574 +#define BCM2835_ISP_NUM_METADATA 1
576 +#define BCM2835_ISP_NUM_NODES \
577 + (BCM2835_ISP_NUM_OUTPUTS + BCM2835_ISP_NUM_CAPTURES + \
578 + BCM2835_ISP_NUM_METADATA)
580 +/* Default frame dimension of 1280 pixels. */
581 +#define DEFAULT_DIM 1280U
583 + * Maximum frame dimension of 16384 pixels. Even though the ISP runs in tiles,
584 + * have a sensible limit so that we do not create an excessive number of tiles
587 +#define MAX_DIM 16384U
589 + * Minimum frame dimension of 64 pixels. Anything lower, and the tiling
590 + * algorithm may not be able to cope when applying filter context.
594 +/* Timeout for stop_streaming to allow all buffers to return */
595 +#define COMPLETE_TIMEOUT (2 * HZ)
597 +/* Per-queue, driver-specific private data */
598 +struct bcm2835_isp_q_data {
600 + * These parameters should be treated as gospel, with everything else
601 + * being determined from them.
603 + unsigned int bytesperline;
604 + unsigned int width;
605 + unsigned int height;
606 + unsigned int sizeimage;
607 + const struct bcm2835_isp_fmt *fmt;
611 + * Structure to describe a single node /dev/video<N> which represents a single
612 + * input or output queue to the ISP device.
614 +struct bcm2835_isp_node {
618 + struct vchiq_mmal_port *port;
619 + struct video_device vfd;
620 + struct media_pad pad;
621 + struct media_intf_devnode *intf_devnode;
622 + struct media_link *intf_link;
623 + struct mutex lock; /* top level device node lock */
624 + struct mutex queue_lock;
626 + struct vb2_queue queue;
627 + unsigned int sequence;
629 + /* The list of formats supported on the node. */
630 + struct bcm2835_isp_fmt const **supported_fmts;
631 + unsigned int num_supported_fmts;
633 + struct bcm2835_isp_q_data q_data;
635 + /* Parent device structure */
636 + struct bcm2835_isp_dev *dev;
639 + bool media_node_registered;
643 + * Structure representing the entire ISP device, comprising several input and
644 + * output nodes /dev/video<N>.
646 +struct bcm2835_isp_dev {
647 + struct v4l2_device v4l2_dev;
648 + struct device *dev;
649 + struct v4l2_ctrl_handler ctrl_handler;
650 + struct media_device mdev;
651 + struct media_entity entity;
652 + bool media_device_registered;
653 + bool media_entity_registered;
654 + struct vchiq_mmal_instance *mmal_instance;
655 + struct vchiq_mmal_component *component;
656 + struct completion frame_cmplt;
658 + struct bcm2835_isp_node node[BCM2835_ISP_NUM_NODES];
659 + struct media_pad pad[BCM2835_ISP_NUM_NODES];
660 + atomic_t num_streaming;
662 + /* Image pipeline controls. */
667 +struct bcm2835_isp_buffer {
668 + struct vb2_v4l2_buffer vb;
669 + struct mmal_buffer mmal;
673 +inline struct bcm2835_isp_dev *node_get_dev(struct bcm2835_isp_node *node)
678 +static inline bool node_is_output(struct bcm2835_isp_node *node)
680 + return node->queue.type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
683 +static inline bool node_is_capture(struct bcm2835_isp_node *node)
685 + return node->queue.type == V4L2_BUF_TYPE_VIDEO_CAPTURE;
688 +static inline bool node_is_stats(struct bcm2835_isp_node *node)
690 + return node->queue.type == V4L2_BUF_TYPE_META_CAPTURE;
693 +static inline enum v4l2_buf_type index_to_queue_type(int index)
695 + if (index < BCM2835_ISP_NUM_OUTPUTS)
696 + return V4L2_BUF_TYPE_VIDEO_OUTPUT;
697 + else if (index < BCM2835_ISP_NUM_OUTPUTS + BCM2835_ISP_NUM_CAPTURES)
698 + return V4L2_BUF_TYPE_VIDEO_CAPTURE;
700 + return V4L2_BUF_TYPE_META_CAPTURE;
703 +static int set_isp_param(struct bcm2835_isp_node *node, u32 parameter,
704 + void *value, u32 value_size)
706 + struct bcm2835_isp_dev *dev = node_get_dev(node);
708 + return vchiq_mmal_port_parameter_set(dev->mmal_instance, node->port,
709 + parameter, value, value_size);
712 +static int set_wb_gains(struct bcm2835_isp_node *node)
714 + struct bcm2835_isp_dev *dev = node_get_dev(node);
715 + struct mmal_parameter_awbgains gains = {
716 + .r_gain = { dev->r_gain, 1000 },
717 + .b_gain = { dev->b_gain, 1000 }
720 + return set_isp_param(node, MMAL_PARAMETER_CUSTOM_AWB_GAINS,
721 + &gains, sizeof(gains));
724 +static int set_digital_gain(struct bcm2835_isp_node *node, uint32_t gain)
726 + struct s32_fract digital_gain = {
728 + .denominator = 1000
731 + return set_isp_param(node, MMAL_PARAMETER_DIGITAL_GAIN,
732 + &digital_gain, sizeof(digital_gain));
735 +static const struct bcm2835_isp_fmt *get_fmt(u32 mmal_fmt)
739 + for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
740 + if (supported_formats[i].mmal_fmt == mmal_fmt)
741 + return &supported_formats[i];
747 +struct bcm2835_isp_fmt *find_format_by_fourcc(unsigned int fourcc,
748 + struct bcm2835_isp_node *node)
750 + const struct bcm2835_isp_fmt *fmt;
753 + for (i = 0; i < node->num_supported_fmts; i++) {
754 + fmt = node->supported_fmts[i];
755 + if (fmt->fourcc == fourcc)
763 +struct bcm2835_isp_fmt *find_format(struct v4l2_format *f,
764 + struct bcm2835_isp_node *node)
766 + return find_format_by_fourcc(node_is_stats(node) ?
767 + f->fmt.meta.dataformat :
768 + f->fmt.pix.pixelformat,
772 +/* vb2_to_mmal_buffer() - converts vb2 buffer header to MMAL
774 + * Copies all the required fields from a VB2 buffer to the MMAL buffer header,
775 + * ready for sending to the VPU.
777 +static void vb2_to_mmal_buffer(struct mmal_buffer *buf,
778 + struct vb2_v4l2_buffer *vb2)
782 + buf->mmal_flags = 0;
783 + if (vb2->flags & V4L2_BUF_FLAG_KEYFRAME)
784 + buf->mmal_flags |= MMAL_BUFFER_HEADER_FLAG_KEYFRAME;
786 + /* Data must be framed correctly as one frame per buffer. */
787 + buf->mmal_flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
789 + buf->length = vb2->vb2_buf.planes[0].bytesused;
791 + * Minor ambiguity in the V4L2 spec as to whether passing in a 0 length
792 + * buffer, or one with V4L2_BUF_FLAG_LAST set denotes end of stream.
795 + if (!buf->length || vb2->flags & V4L2_BUF_FLAG_LAST)
796 + buf->mmal_flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
798 + /* vb2 timestamps in nsecs, mmal in usecs */
799 + pts = vb2->vb2_buf.timestamp;
802 + buf->dts = MMAL_TIME_UNKNOWN;
805 +static void mmal_buffer_cb(struct vchiq_mmal_instance *instance,
806 + struct vchiq_mmal_port *port, int status,
807 + struct mmal_buffer *mmal_buf)
809 + struct bcm2835_isp_buffer *q_buf;
810 + struct bcm2835_isp_node *node = port->cb_ctx;
811 + struct bcm2835_isp_dev *dev = node_get_dev(node);
812 + struct vb2_v4l2_buffer *vb2;
814 + q_buf = container_of(mmal_buf, struct bcm2835_isp_buffer, mmal);
816 + v4l2_dbg(2, debug, &dev->v4l2_dev,
817 + "%s: port:%s[%d], status:%d, buf:%p, dmabuf:%p, length:%lu, flags %u, pts %lld\n",
818 + __func__, node_is_output(node) ? "input" : "output", node->id,
819 + status, mmal_buf, mmal_buf->dma_buf, mmal_buf->length,
820 + mmal_buf->mmal_flags, mmal_buf->pts);
823 + v4l2_err(&dev->v4l2_dev,
824 + "%s: Unexpected event on output callback - %08x\n",
825 + __func__, mmal_buf->cmd);
828 + /* error in transfer */
830 + /* there was a buffer with the error so return it */
831 + vb2_buffer_done(&vb2->vb2_buf, VB2_BUF_STATE_ERROR);
836 + /* vb2 timestamps in nsecs, mmal in usecs */
837 + vb2->vb2_buf.timestamp = mmal_buf->pts * 1000;
838 + vb2->sequence = node->sequence++;
839 + vb2_set_plane_payload(&vb2->vb2_buf, 0, mmal_buf->length);
840 + vb2_buffer_done(&vb2->vb2_buf, VB2_BUF_STATE_DONE);
842 + if (!port->enabled)
843 + complete(&dev->frame_cmplt);
846 +static void setup_mmal_port_format(struct bcm2835_isp_node *node,
847 + struct vchiq_mmal_port *port)
849 + struct bcm2835_isp_q_data *q_data = &node->q_data;
851 + port->format.encoding = q_data->fmt->mmal_fmt;
852 + /* Raw image format - set width/height */
853 + port->es.video.width = (q_data->bytesperline << 3) / q_data->fmt->depth;
854 + port->es.video.height = q_data->height;
855 + port->es.video.crop.width = q_data->width;
856 + port->es.video.crop.height = q_data->height;
857 + port->es.video.crop.x = 0;
858 + port->es.video.crop.y = 0;
861 +static int setup_mmal_port(struct bcm2835_isp_node *node)
863 + struct bcm2835_isp_dev *dev = node_get_dev(node);
864 + unsigned int enable = 1;
867 + v4l2_dbg(2, debug, &dev->v4l2_dev, "%s: setup %s[%d]\n", __func__,
868 + node->name, node->id);
870 + vchiq_mmal_port_parameter_set(dev->mmal_instance, node->port,
871 + MMAL_PARAMETER_ZERO_COPY, &enable,
873 + setup_mmal_port_format(node, node->port);
874 + ret = vchiq_mmal_port_set_format(dev->mmal_instance, node->port);
876 + v4l2_dbg(1, debug, &dev->v4l2_dev,
877 + "%s: vchiq_mmal_port_set_format failed\n",
882 + if (node->q_data.sizeimage < node->port->minimum_buffer.size) {
883 + v4l2_err(&dev->v4l2_dev,
884 + "buffer size mismatch sizeimage %u < min size %u\n",
885 + node->q_data.sizeimage,
886 + node->port->minimum_buffer.size);
893 +static int bcm2835_isp_mmal_buf_cleanup(struct mmal_buffer *mmal_buf)
895 + mmal_vchi_buffer_cleanup(mmal_buf);
897 + if (mmal_buf->dma_buf) {
898 + dma_buf_put(mmal_buf->dma_buf);
899 + mmal_buf->dma_buf = NULL;
905 +static int bcm2835_isp_node_queue_setup(struct vb2_queue *q,
906 + unsigned int *nbuffers,
907 + unsigned int *nplanes,
908 + unsigned int sizes[],
909 + struct device *alloc_devs[])
911 + struct bcm2835_isp_node *node = vb2_get_drv_priv(q);
914 + if (setup_mmal_port(node))
917 + size = node->q_data.sizeimage;
919 + v4l2_info(&node_get_dev(node)->v4l2_dev,
920 + "%s: Image size unset in queue_setup for node %s[%d]\n",
921 + __func__, node->name, node->id);
926 + return sizes[0] < size ? -EINVAL : 0;
931 + node->port->current_buffer.size = size;
933 + if (*nbuffers < node->port->minimum_buffer.num)
934 + *nbuffers = node->port->minimum_buffer.num;
936 + node->port->current_buffer.num = *nbuffers;
938 + v4l2_dbg(2, debug, &node_get_dev(node)->v4l2_dev,
939 + "%s: Image size %u, nbuffers %u for node %s[%d]\n",
940 + __func__, sizes[0], *nbuffers, node->name, node->id);
944 +static int bcm2835_isp_buf_init(struct vb2_buffer *vb)
946 + struct bcm2835_isp_node *node = vb2_get_drv_priv(vb->vb2_queue);
947 + struct bcm2835_isp_dev *dev = node_get_dev(node);
948 + struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
949 + struct bcm2835_isp_buffer *buf =
950 + container_of(vb2, struct bcm2835_isp_buffer, vb);
952 + v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: vb %p\n", __func__, vb);
954 + buf->mmal.buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
955 + buf->mmal.buffer_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
956 + mmal_vchi_buffer_init(dev->mmal_instance, &buf->mmal);
960 +static int bcm2835_isp_buf_prepare(struct vb2_buffer *vb)
962 + struct bcm2835_isp_node *node = vb2_get_drv_priv(vb->vb2_queue);
963 + struct bcm2835_isp_dev *dev = node_get_dev(node);
964 + struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
965 + struct bcm2835_isp_buffer *buf =
966 + container_of(vb2, struct bcm2835_isp_buffer, vb);
967 + struct dma_buf *dma_buf;
970 + v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: type: %d ptr %p\n",
971 + __func__, vb->vb2_queue->type, vb);
973 + if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
974 + if (vb2->field == V4L2_FIELD_ANY)
975 + vb2->field = V4L2_FIELD_NONE;
976 + if (vb2->field != V4L2_FIELD_NONE) {
977 + v4l2_err(&dev->v4l2_dev,
978 + "%s field isn't supported\n", __func__);
983 + if (vb2_plane_size(vb, 0) < node->q_data.sizeimage) {
984 + v4l2_err(&dev->v4l2_dev,
985 + "%s data will not fit into plane (%lu < %lu)\n",
986 + __func__, vb2_plane_size(vb, 0),
987 + (long)node->q_data.sizeimage);
991 + if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type))
992 + vb2_set_plane_payload(vb, 0, node->q_data.sizeimage);
994 + switch (vb->memory) {
995 + case VB2_MEMORY_DMABUF:
996 + dma_buf = dma_buf_get(vb->planes[0].m.fd);
998 + if (dma_buf != buf->mmal.dma_buf) {
1000 + * dmabuf either hasn't already been mapped, or it has
1003 + if (buf->mmal.dma_buf) {
1004 + v4l2_err(&dev->v4l2_dev,
1005 + "%s Buffer changed - why did the core not call cleanup?\n",
1007 + bcm2835_isp_mmal_buf_cleanup(&buf->mmal);
1010 + buf->mmal.dma_buf = dma_buf;
1013 + * Already have a reference to the buffer, so release it
1016 + dma_buf_put(dma_buf);
1020 + case VB2_MEMORY_MMAP:
1022 + * We want to do this at init, but vb2_core_expbuf checks that
1023 + * the index < q->num_buffers, and q->num_buffers only gets
1024 + * updated once all the buffers are allocated.
1026 + if (!buf->mmal.dma_buf) {
1027 + ret = vb2_core_expbuf_dmabuf(vb->vb2_queue,
1028 + vb->vb2_queue->type,
1029 + vb->index, 0, O_CLOEXEC,
1030 + &buf->mmal.dma_buf);
1031 + v4l2_dbg(3, debug, &dev->v4l2_dev,
1032 + "%s: exporting ptr %p to dmabuf %p\n",
1033 + __func__, vb, buf->mmal.dma_buf);
1035 + v4l2_err(&dev->v4l2_dev,
1036 + "%s: Failed to expbuf idx %d, ret %d\n",
1037 + __func__, vb->index, ret);
1050 +static void bcm2835_isp_node_buffer_queue(struct vb2_buffer *buf)
1052 + struct bcm2835_isp_node *node = vb2_get_drv_priv(buf->vb2_queue);
1053 + struct vb2_v4l2_buffer *vbuf =
1054 + container_of(buf, struct vb2_v4l2_buffer, vb2_buf);
1055 + struct bcm2835_isp_buffer *buffer =
1056 + container_of(vbuf, struct bcm2835_isp_buffer, vb);
1057 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1059 + v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: node %s[%d], buffer %p\n",
1060 + __func__, node->name, node->id, buffer);
1062 + vb2_to_mmal_buffer(&buffer->mmal, &buffer->vb);
1063 + v4l2_dbg(3, debug, &dev->v4l2_dev,
1064 + "%s: node %s[%d] - submitting mmal dmabuf %p\n", __func__,
1065 + node->name, node->id, buffer->mmal.dma_buf);
1066 + vchiq_mmal_submit_buffer(dev->mmal_instance, node->port, &buffer->mmal);
1069 +static void bcm2835_isp_buffer_cleanup(struct vb2_buffer *vb)
1071 + struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(vb);
1072 + struct bcm2835_isp_buffer *buffer =
1073 + container_of(vb2, struct bcm2835_isp_buffer, vb);
1075 + bcm2835_isp_mmal_buf_cleanup(&buffer->mmal);
1078 +static int bcm2835_isp_node_start_streaming(struct vb2_queue *q,
1079 + unsigned int count)
1081 + struct bcm2835_isp_node *node = vb2_get_drv_priv(q);
1082 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1085 + v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: node %s[%d] (count %u)\n",
1086 + __func__, node->name, node->id, count);
1088 + ret = vchiq_mmal_component_enable(dev->mmal_instance, dev->component);
1090 + v4l2_err(&dev->v4l2_dev, "%s: Failed enabling component, ret %d\n",
1095 + node->sequence = 0;
1096 + node->port->cb_ctx = node;
1097 + ret = vchiq_mmal_port_enable(dev->mmal_instance, node->port,
1100 + atomic_inc(&dev->num_streaming);
1102 + v4l2_err(&dev->v4l2_dev,
1103 + "%s: Failed enabling port, ret %d\n", __func__, ret);
1108 +static void bcm2835_isp_node_stop_streaming(struct vb2_queue *q)
1110 + struct bcm2835_isp_node *node = vb2_get_drv_priv(q);
1111 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1115 + v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: node %s[%d], mmal port %p\n",
1116 + __func__, node->name, node->id, node->port);
1118 + init_completion(&dev->frame_cmplt);
1120 + /* Disable MMAL port - this will flush buffers back */
1121 + ret = vchiq_mmal_port_disable(dev->mmal_instance, node->port);
1123 + v4l2_err(&dev->v4l2_dev,
1124 + "%s: Failed disabling %s port, ret %d\n", __func__,
1125 + node_is_output(node) ? "i/p" : "o/p",
1128 + while (atomic_read(&node->port->buffers_with_vpu)) {
1129 + v4l2_dbg(1, debug, &dev->v4l2_dev,
1130 + "%s: Waiting for buffers to be returned - %d outstanding\n",
1131 + __func__, atomic_read(&node->port->buffers_with_vpu));
1132 + ret = wait_for_completion_timeout(&dev->frame_cmplt,
1133 + COMPLETE_TIMEOUT);
1135 + v4l2_err(&dev->v4l2_dev,
1136 + "%s: Timeout waiting for buffers to be returned - %d outstanding\n",
1138 + atomic_read(&node->port->buffers_with_vpu));
1143 + /* Release the VCSM handle here to release the associated dmabuf */
1144 + for (i = 0; i < q->num_buffers; i++) {
1145 + struct vb2_v4l2_buffer *vb2 = to_vb2_v4l2_buffer(q->bufs[i]);
1146 + struct bcm2835_isp_buffer *buf =
1147 + container_of(vb2, struct bcm2835_isp_buffer, vb);
1148 + bcm2835_isp_mmal_buf_cleanup(&buf->mmal);
1151 + atomic_dec(&dev->num_streaming);
1152 + /* If all ports disabled, then disable the component */
1153 + if (atomic_read(&dev->num_streaming) == 0) {
1154 + ret = vchiq_mmal_component_disable(dev->mmal_instance,
1157 + v4l2_err(&dev->v4l2_dev,
1158 + "%s: Failed disabling component, ret %d\n",
1164 + * Simply wait for any vb2 buffers to finish. We could take steps to
1165 + * make them complete more quickly if we care, or even return them
1168 + vb2_wait_for_all_buffers(&node->queue);
1171 +static const struct vb2_ops bcm2835_isp_node_queue_ops = {
1172 + .queue_setup = bcm2835_isp_node_queue_setup,
1173 + .buf_init = bcm2835_isp_buf_init,
1174 + .buf_prepare = bcm2835_isp_buf_prepare,
1175 + .buf_queue = bcm2835_isp_node_buffer_queue,
1176 + .buf_cleanup = bcm2835_isp_buffer_cleanup,
1177 + .start_streaming = bcm2835_isp_node_start_streaming,
1178 + .stop_streaming = bcm2835_isp_node_stop_streaming,
1182 +struct bcm2835_isp_fmt *get_default_format(struct bcm2835_isp_node *node)
1184 + return node->supported_fmts[0];
1187 +static inline unsigned int get_bytesperline(int width,
1188 + const struct bcm2835_isp_fmt *fmt)
1190 + /* GPU aligns 24bpp images to a multiple of 32 pixels (not bytes). */
1191 + if (fmt->depth == 24)
1192 + return ALIGN(width, 32) * 3;
1194 + return ALIGN((width * fmt->depth) >> 3, fmt->bytesperline_align);
1197 +static inline unsigned int get_sizeimage(int bpl, int width, int height,
1198 + const struct bcm2835_isp_fmt *fmt)
1200 + return (bpl * height * fmt->size_multiplier_x2) >> 1;
1203 +static int bcm2835_isp_s_ctrl(struct v4l2_ctrl *ctrl)
1205 + struct bcm2835_isp_dev *dev =
1206 + container_of(ctrl->handler, struct bcm2835_isp_dev, ctrl_handler);
1207 + struct bcm2835_isp_node *node = &dev->node[0];
1211 + * The ISP firmware driver will ensure these settings are applied on
1212 + * a frame boundary, so we are safe to write them as they come in.
1214 + * Note that the bcm2835_isp_* param structures are identical to the
1215 + * mmal-parameters.h definitions. This avoids the need for unnecessary
1216 + * field-by-field copying between structures.
1218 + switch (ctrl->id) {
1219 + case V4L2_CID_RED_BALANCE:
1220 + dev->r_gain = ctrl->val;
1221 + ret = set_wb_gains(node);
1223 + case V4L2_CID_BLUE_BALANCE:
1224 + dev->b_gain = ctrl->val;
1225 + ret = set_wb_gains(node);
1227 + case V4L2_CID_DIGITAL_GAIN:
1228 + ret = set_digital_gain(node, ctrl->val);
1230 + case V4L2_CID_USER_BCM2835_ISP_CC_MATRIX:
1231 + ret = set_isp_param(node, MMAL_PARAMETER_CUSTOM_CCM,
1233 + sizeof(struct bcm2835_isp_custom_ccm));
1235 + case V4L2_CID_USER_BCM2835_ISP_LENS_SHADING:
1237 + struct bcm2835_isp_lens_shading *v4l2_ls;
1238 + struct mmal_parameter_lens_shading_v2 ls;
1239 + struct dma_buf *dmabuf;
1240 + void *vcsm_handle;
1242 + v4l2_ls = (struct bcm2835_isp_lens_shading *)ctrl->p_new.p_u8;
1244 + * struct bcm2835_isp_lens_shading and struct
1245 + * mmal_parameter_lens_shading_v2 match so that we can do a
1246 + * simple memcpy here.
1247 + * Only the dmabuf to the actual table needs any manipulation.
1249 + memcpy(&ls, v4l2_ls, sizeof(ls));
1251 + dmabuf = dma_buf_get(v4l2_ls->dmabuf);
1252 + if (IS_ERR_OR_NULL(dmabuf))
1255 + ret = vc_sm_cma_import_dmabuf(dmabuf, &vcsm_handle);
1257 + dma_buf_put(dmabuf);
1261 + ls.mem_handle_table = vc_sm_cma_int_handle(vcsm_handle);
1262 + if (ls.mem_handle_table)
1263 + /* The VPU will take a reference on the vcsm handle,
1264 + * which in turn will retain a reference on the dmabuf.
1265 + * This code can therefore safely release all
1266 + * references to the buffer.
1268 + ret = set_isp_param(node,
1269 + MMAL_PARAMETER_LENS_SHADING_OVERRIDE,
1275 + vc_sm_cma_free(vcsm_handle);
1276 + dma_buf_put(dmabuf);
1279 + case V4L2_CID_USER_BCM2835_ISP_BLACK_LEVEL:
1280 + ret = set_isp_param(node, MMAL_PARAMETER_BLACK_LEVEL,
1282 + sizeof(struct bcm2835_isp_black_level));
1284 + case V4L2_CID_USER_BCM2835_ISP_GEQ:
1285 + ret = set_isp_param(node, MMAL_PARAMETER_GEQ,
1287 + sizeof(struct bcm2835_isp_geq));
1289 + case V4L2_CID_USER_BCM2835_ISP_GAMMA:
1290 + ret = set_isp_param(node, MMAL_PARAMETER_GAMMA,
1292 + sizeof(struct bcm2835_isp_gamma));
1294 + case V4L2_CID_USER_BCM2835_ISP_DENOISE:
1295 + ret = set_isp_param(node, MMAL_PARAMETER_DENOISE,
1297 + sizeof(struct bcm2835_isp_denoise));
1299 + case V4L2_CID_USER_BCM2835_ISP_SHARPEN:
1300 + ret = set_isp_param(node, MMAL_PARAMETER_SHARPEN,
1302 + sizeof(struct bcm2835_isp_sharpen));
1304 + case V4L2_CID_USER_BCM2835_ISP_DPC:
1305 + ret = set_isp_param(node, MMAL_PARAMETER_DPC,
1307 + sizeof(struct bcm2835_isp_dpc));
1310 + v4l2_info(&dev->v4l2_dev, "Unrecognised control\n");
1315 + v4l2_err(&dev->v4l2_dev, "%s: Failed setting ctrl \"%s\" (%08x), err %d\n",
1316 + __func__, ctrl->name, ctrl->id, ret);
1323 +static const struct v4l2_ctrl_ops bcm2835_isp_ctrl_ops = {
1324 + .s_ctrl = bcm2835_isp_s_ctrl,
1327 +static const struct v4l2_file_operations bcm2835_isp_fops = {
1328 + .owner = THIS_MODULE,
1329 + .open = v4l2_fh_open,
1330 + .release = vb2_fop_release,
1331 + .poll = vb2_fop_poll,
1332 + .unlocked_ioctl = video_ioctl2,
1333 + .mmap = vb2_fop_mmap
1336 +static int populate_qdata_fmt(struct v4l2_format *f,
1337 + struct bcm2835_isp_node *node)
1339 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1340 + struct bcm2835_isp_q_data *q_data = &node->q_data;
1343 + if (!node_is_stats(node)) {
1344 + v4l2_dbg(1, debug, &dev->v4l2_dev,
1345 + "%s: Setting pix format for type %d, wxh: %ux%u, fmt: %08x, size %u\n",
1346 + __func__, f->type, f->fmt.pix.width, f->fmt.pix.height,
1347 + f->fmt.pix.pixelformat, f->fmt.pix.sizeimage);
1349 + q_data->fmt = find_format(f, node);
1350 + q_data->width = f->fmt.pix.width;
1351 + q_data->height = f->fmt.pix.height;
1352 + q_data->height = f->fmt.pix.height;
1354 + /* All parameters should have been set correctly by try_fmt */
1355 + q_data->bytesperline = f->fmt.pix.bytesperline;
1356 + q_data->sizeimage = f->fmt.pix.sizeimage;
1358 + v4l2_dbg(1, debug, &dev->v4l2_dev,
1359 + "%s: Setting meta format for fmt: %08x, size %u\n",
1360 + __func__, f->fmt.meta.dataformat,
1361 + f->fmt.meta.buffersize);
1363 + q_data->fmt = find_format(f, node);
1364 + q_data->width = 0;
1365 + q_data->height = 0;
1366 + q_data->bytesperline = 0;
1367 + q_data->sizeimage = f->fmt.meta.buffersize;
1370 + v4l2_dbg(1, debug, &dev->v4l2_dev,
1371 + "%s: Calculated bpl as %u, size %u\n", __func__,
1372 + q_data->bytesperline, q_data->sizeimage);
1374 + setup_mmal_port_format(node, node->port);
1375 + ret = vchiq_mmal_port_set_format(dev->mmal_instance, node->port);
1377 + v4l2_err(&dev->v4l2_dev,
1378 + "%s: Failed vchiq_mmal_port_set_format on port, ret %d\n",
1383 + if (q_data->sizeimage < node->port->minimum_buffer.size) {
1384 + v4l2_err(&dev->v4l2_dev,
1385 + "%s: Current buffer size of %u < min buf size %u - driver mismatch to MMAL\n",
1387 + q_data->sizeimage,
1388 + node->port->minimum_buffer.size);
1391 + v4l2_dbg(1, debug, &dev->v4l2_dev,
1392 + "%s: Set format for type %d, wxh: %dx%d, fmt: %08x, size %u\n",
1393 + __func__, f->type, q_data->width, q_data->height,
1394 + q_data->fmt->fourcc, q_data->sizeimage);
1399 +static int bcm2835_isp_node_querycap(struct file *file, void *priv,
1400 + struct v4l2_capability *cap)
1402 + strscpy(cap->driver, BCM2835_ISP_NAME, sizeof(cap->driver));
1403 + strscpy(cap->card, BCM2835_ISP_NAME, sizeof(cap->card));
1404 + snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1405 + BCM2835_ISP_NAME);
1410 +static int bcm2835_isp_node_g_fmt(struct file *file, void *priv,
1411 + struct v4l2_format *f)
1413 + struct bcm2835_isp_node *node = video_drvdata(file);
1415 + if (f->type != node->queue.type)
1418 + if (node_is_stats(node)) {
1419 + f->fmt.meta.dataformat = V4L2_META_FMT_BCM2835_ISP_STATS;
1420 + f->fmt.meta.buffersize =
1421 + node->port->minimum_buffer.size;
1423 + struct bcm2835_isp_q_data *q_data = &node->q_data;
1425 + f->fmt.pix.width = q_data->width;
1426 + f->fmt.pix.height = q_data->height;
1427 + f->fmt.pix.field = V4L2_FIELD_NONE;
1428 + f->fmt.pix.pixelformat = q_data->fmt->fourcc;
1429 + f->fmt.pix.bytesperline = q_data->bytesperline;
1430 + f->fmt.pix.sizeimage = q_data->sizeimage;
1431 + f->fmt.pix.colorspace = q_data->fmt->colorspace;
1437 +static int bcm2835_isp_node_enum_fmt(struct file *file, void *priv,
1438 + struct v4l2_fmtdesc *f)
1440 + struct bcm2835_isp_node *node = video_drvdata(file);
1442 + if (f->type != node->queue.type)
1445 + if (f->index < node->num_supported_fmts) {
1446 + /* Format found */
1447 + f->pixelformat = node->supported_fmts[f->index]->fourcc;
1455 +static int bcm2835_isp_enum_framesizes(struct file *file, void *priv,
1456 + struct v4l2_frmsizeenum *fsize)
1458 + struct bcm2835_isp_node *node = video_drvdata(file);
1459 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1460 + const struct bcm2835_isp_fmt *fmt;
1462 + if (node_is_stats(node) || fsize->index)
1465 + fmt = find_format_by_fourcc(fsize->pixel_format, node);
1467 + v4l2_err(&dev->v4l2_dev, "Invalid pixel code: %x\n",
1468 + fsize->pixel_format);
1472 + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1473 + fsize->stepwise.min_width = MIN_DIM;
1474 + fsize->stepwise.max_width = MAX_DIM;
1475 + fsize->stepwise.step_width = fmt->step_size;
1477 + fsize->stepwise.min_height = MIN_DIM;
1478 + fsize->stepwise.max_height = MAX_DIM;
1479 + fsize->stepwise.step_height = fmt->step_size;
1484 +static int bcm2835_isp_node_try_fmt(struct file *file, void *priv,
1485 + struct v4l2_format *f)
1487 + struct bcm2835_isp_node *node = video_drvdata(file);
1488 + const struct bcm2835_isp_fmt *fmt;
1490 + if (f->type != node->queue.type)
1493 + fmt = find_format(f, node);
1495 + fmt = get_default_format(node);
1497 + if (!node_is_stats(node)) {
1498 + f->fmt.pix.width = max(min(f->fmt.pix.width, MAX_DIM),
1500 + f->fmt.pix.height = max(min(f->fmt.pix.height, MAX_DIM),
1503 + f->fmt.pix.pixelformat = fmt->fourcc;
1504 + f->fmt.pix.colorspace = fmt->colorspace;
1505 + f->fmt.pix.bytesperline = get_bytesperline(f->fmt.pix.width,
1507 + f->fmt.pix.field = V4L2_FIELD_NONE;
1508 + f->fmt.pix.sizeimage =
1509 + get_sizeimage(f->fmt.pix.bytesperline, f->fmt.pix.width,
1510 + f->fmt.pix.height, fmt);
1512 + f->fmt.meta.dataformat = fmt->fourcc;
1513 + f->fmt.meta.buffersize = node->port->minimum_buffer.size;
1519 +static int bcm2835_isp_node_s_fmt(struct file *file, void *priv,
1520 + struct v4l2_format *f)
1522 + struct bcm2835_isp_node *node = video_drvdata(file);
1525 + if (f->type != node->queue.type)
1528 + ret = bcm2835_isp_node_try_fmt(file, priv, f);
1532 + v4l2_dbg(1, debug, &node_get_dev(node)->v4l2_dev,
1533 + "%s: Set format for node %s[%d]\n",
1534 + __func__, node->name, node->id);
1536 + return populate_qdata_fmt(f, node);
1539 +static int bcm2835_isp_node_s_selection(struct file *file, void *fh,
1540 + struct v4l2_selection *s)
1542 + struct mmal_parameter_crop crop;
1543 + struct bcm2835_isp_node *node = video_drvdata(file);
1544 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1546 + /* This return value is required fro V4L2 compliance. */
1547 + if (node_is_stats(node))
1550 + if (!s->r.width || !s->r.height)
1553 + /* We can only set crop on the input. */
1554 + switch (s->target) {
1555 + case V4L2_SEL_TGT_CROP:
1557 + * Adjust the crop window if it goes outside of the frame
1560 + s->r.left = min((unsigned int)max(s->r.left, 0),
1561 + node->q_data.width - MIN_DIM);
1562 + s->r.top = min((unsigned int)max(s->r.top, 0),
1563 + node->q_data.height - MIN_DIM);
1564 + s->r.width = max(min(s->r.width,
1565 + node->q_data.width - s->r.left), MIN_DIM);
1566 + s->r.height = max(min(s->r.height,
1567 + node->q_data.height - s->r.top), MIN_DIM);
1569 + case V4L2_SEL_TGT_CROP_DEFAULT:
1570 + /* Default (i.e. no) crop window. */
1573 + s->r.width = node->q_data.width;
1574 + s->r.height = node->q_data.height;
1580 + crop.rect.x = s->r.left;
1581 + crop.rect.y = s->r.top;
1582 + crop.rect.width = s->r.width;
1583 + crop.rect.height = s->r.height;
1585 + return vchiq_mmal_port_parameter_set(dev->mmal_instance, node->port,
1586 + MMAL_PARAMETER_CROP,
1587 + &crop, sizeof(crop));
1590 +static int bcm2835_isp_node_g_selection(struct file *file, void *fh,
1591 + struct v4l2_selection *s)
1593 + struct mmal_parameter_crop crop;
1594 + struct bcm2835_isp_node *node = video_drvdata(file);
1595 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1596 + u32 crop_size = sizeof(crop);
1599 + /* We can only return out an input crop. */
1600 + switch (s->target) {
1601 + case V4L2_SEL_TGT_CROP:
1602 + ret = vchiq_mmal_port_parameter_get(dev->mmal_instance,
1604 + MMAL_PARAMETER_CROP,
1605 + &crop, &crop_size);
1607 + s->r.left = crop.rect.x;
1608 + s->r.top = crop.rect.y;
1609 + s->r.width = crop.rect.width;
1610 + s->r.height = crop.rect.height;
1613 + case V4L2_SEL_TGT_CROP_DEFAULT:
1614 + case V4L2_SEL_TGT_CROP_BOUNDS:
1615 + /* Default (i.e. no) crop window. */
1618 + s->r.width = node->q_data.width;
1619 + s->r.height = node->q_data.height;
1629 +static int bcm3285_isp_subscribe_event(struct v4l2_fh *fh,
1630 + const struct v4l2_event_subscription *s)
1632 + switch (s->type) {
1633 + /* Cannot change source parameters dynamically at runtime. */
1634 + case V4L2_EVENT_SOURCE_CHANGE:
1636 + case V4L2_EVENT_CTRL:
1637 + return v4l2_ctrl_subscribe_event(fh, s);
1639 + return v4l2_event_subscribe(fh, s, 4, NULL);
1643 +static const struct v4l2_ioctl_ops bcm2835_isp_node_ioctl_ops = {
1644 + .vidioc_querycap = bcm2835_isp_node_querycap,
1645 + .vidioc_g_fmt_vid_cap = bcm2835_isp_node_g_fmt,
1646 + .vidioc_g_fmt_vid_out = bcm2835_isp_node_g_fmt,
1647 + .vidioc_g_fmt_meta_cap = bcm2835_isp_node_g_fmt,
1648 + .vidioc_s_fmt_vid_cap = bcm2835_isp_node_s_fmt,
1649 + .vidioc_s_fmt_vid_out = bcm2835_isp_node_s_fmt,
1650 + .vidioc_s_fmt_meta_cap = bcm2835_isp_node_s_fmt,
1651 + .vidioc_try_fmt_vid_cap = bcm2835_isp_node_try_fmt,
1652 + .vidioc_try_fmt_vid_out = bcm2835_isp_node_try_fmt,
1653 + .vidioc_try_fmt_meta_cap = bcm2835_isp_node_try_fmt,
1654 + .vidioc_s_selection = bcm2835_isp_node_s_selection,
1655 + .vidioc_g_selection = bcm2835_isp_node_g_selection,
1657 + .vidioc_enum_fmt_vid_cap = bcm2835_isp_node_enum_fmt,
1658 + .vidioc_enum_fmt_vid_out = bcm2835_isp_node_enum_fmt,
1659 + .vidioc_enum_fmt_meta_cap = bcm2835_isp_node_enum_fmt,
1660 + .vidioc_enum_framesizes = bcm2835_isp_enum_framesizes,
1662 + .vidioc_reqbufs = vb2_ioctl_reqbufs,
1663 + .vidioc_querybuf = vb2_ioctl_querybuf,
1664 + .vidioc_qbuf = vb2_ioctl_qbuf,
1665 + .vidioc_dqbuf = vb2_ioctl_dqbuf,
1666 + .vidioc_expbuf = vb2_ioctl_expbuf,
1667 + .vidioc_create_bufs = vb2_ioctl_create_bufs,
1668 + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1670 + .vidioc_streamon = vb2_ioctl_streamon,
1671 + .vidioc_streamoff = vb2_ioctl_streamoff,
1673 + .vidioc_subscribe_event = bcm3285_isp_subscribe_event,
1674 + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1678 + * Size of the array to provide to the VPU when asking for the list of supported
1681 + * The ISP component currently advertises 44 input formats, so add a small
1682 + * overhead on that. Should the component advertise more formats then the excess
1683 + * will be dropped and a warning logged.
1685 +#define MAX_SUPPORTED_ENCODINGS 50
1687 +/* Populate node->supported_fmts with the formats supported by those ports. */
1688 +static int bcm2835_isp_get_supported_fmts(struct bcm2835_isp_node *node)
1690 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1691 + struct bcm2835_isp_fmt const **list;
1692 + unsigned int i, j, num_encodings;
1693 + u32 fourccs[MAX_SUPPORTED_ENCODINGS];
1694 + u32 param_size = sizeof(fourccs);
1697 + ret = vchiq_mmal_port_parameter_get(dev->mmal_instance, node->port,
1698 + MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1699 + &fourccs, ¶m_size);
1702 + if (ret == MMAL_MSG_STATUS_ENOSPC) {
1703 + v4l2_err(&dev->v4l2_dev,
1704 + "%s: port has more encoding than we provided space for. Some are dropped.\n",
1706 + num_encodings = MAX_SUPPORTED_ENCODINGS;
1708 + v4l2_err(&dev->v4l2_dev, "%s: get_param ret %u.\n",
1713 + num_encodings = param_size / sizeof(u32);
1717 + * Assume at this stage that all encodings will be supported in V4L2.
1718 + * Any that aren't supported will waste a very small amount of memory.
1720 + list = devm_kzalloc(dev->dev,
1721 + sizeof(struct bcm2835_isp_fmt *) * num_encodings,
1725 + node->supported_fmts = list;
1727 + for (i = 0, j = 0; i < num_encodings; i++) {
1728 + const struct bcm2835_isp_fmt *fmt = get_fmt(fourccs[i]);
1735 + node->num_supported_fmts = j;
1741 + * Register a device node /dev/video<N> to go along with one of the ISP's input
1742 + * or output nodes.
1744 +static int register_node(struct bcm2835_isp_dev *dev,
1745 + struct bcm2835_isp_node *node,
1748 + struct video_device *vfd;
1749 + struct vb2_queue *queue;
1752 + mutex_init(&node->lock);
1756 + queue = &node->queue;
1757 + queue->type = index_to_queue_type(index);
1759 + * Setup the node type-specific params.
1761 + * Only the OUTPUT node can set controls and crop windows. However,
1762 + * we must allow the s/g_selection ioctl on the stats node as v4l2
1763 + * compliance expects it to return a -ENOTTY, and the framework
1764 + * does not handle it if the ioctl is disabled.
1766 + switch (queue->type) {
1767 + case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1768 + vfd->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
1770 + node->vfl_dir = VFL_DIR_TX;
1771 + node->name = "output";
1772 + node->port = &dev->component->input[node->id];
1774 + case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1775 + vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1776 + /* First Capture node starts at id 0, etc. */
1777 + node->id = index - BCM2835_ISP_NUM_OUTPUTS;
1778 + node->vfl_dir = VFL_DIR_RX;
1779 + node->name = "capture";
1780 + node->port = &dev->component->output[node->id];
1781 + v4l2_disable_ioctl(&node->vfd, VIDIOC_S_CTRL);
1782 + v4l2_disable_ioctl(&node->vfd, VIDIOC_S_SELECTION);
1783 + v4l2_disable_ioctl(&node->vfd, VIDIOC_G_SELECTION);
1785 + case V4L2_BUF_TYPE_META_CAPTURE:
1786 + vfd->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
1787 + node->id = index - BCM2835_ISP_NUM_OUTPUTS;
1788 + node->vfl_dir = VFL_DIR_RX;
1789 + node->name = "stats";
1790 + node->port = &dev->component->output[node->id];
1791 + v4l2_disable_ioctl(&node->vfd, VIDIOC_S_CTRL);
1792 + v4l2_disable_ioctl(&node->vfd, VIDIOC_S_SELECTION);
1793 + v4l2_disable_ioctl(&node->vfd, VIDIOC_G_SELECTION);
1797 + /* We use the selection API instead of the old crop API. */
1798 + v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1799 + v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1800 + v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1802 + ret = bcm2835_isp_get_supported_fmts(node);
1806 + /* Initialise the video node. */
1807 + vfd->vfl_type = VFL_TYPE_VIDEO;
1808 + vfd->fops = &bcm2835_isp_fops,
1809 + vfd->ioctl_ops = &bcm2835_isp_node_ioctl_ops,
1811 + vfd->release = video_device_release_empty,
1812 + vfd->queue = &node->queue;
1813 + vfd->lock = &node->lock;
1814 + vfd->v4l2_dev = &dev->v4l2_dev;
1815 + vfd->vfl_dir = node->vfl_dir;
1817 + node->q_data.fmt = get_default_format(node);
1818 + node->q_data.width = DEFAULT_DIM;
1819 + node->q_data.height = DEFAULT_DIM;
1820 + node->q_data.bytesperline =
1821 + get_bytesperline(DEFAULT_DIM, node->q_data.fmt);
1822 + node->q_data.sizeimage = node_is_stats(node) ?
1823 + node->port->recommended_buffer.size :
1824 + get_sizeimage(node->q_data.bytesperline,
1825 + node->q_data.width,
1826 + node->q_data.height,
1827 + node->q_data.fmt);
1829 + queue->io_modes = VB2_MMAP | VB2_DMABUF;
1830 + queue->drv_priv = node;
1831 + queue->ops = &bcm2835_isp_node_queue_ops;
1832 + queue->mem_ops = &vb2_dma_contig_memops;
1833 + queue->buf_struct_size = sizeof(struct bcm2835_isp_buffer);
1834 + queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1835 + queue->dev = dev->dev;
1836 + queue->lock = &node->queue_lock;
1838 + ret = vb2_queue_init(queue);
1840 + v4l2_info(&dev->v4l2_dev, "vb2_queue_init failed\n");
1844 + /* Set some controls and defaults, but only on the VIDEO_OUTPUT node. */
1845 + if (node_is_output(node)) {
1848 + /* Use this ctrl template to assign custom ISP ctrls. */
1849 + struct v4l2_ctrl_config ctrl_template = {
1850 + .ops = &bcm2835_isp_ctrl_ops,
1851 + .type = V4L2_CTRL_TYPE_U8,
1858 + /* 3 standard controls, and an array of custom controls */
1859 + ret = v4l2_ctrl_handler_init(&dev->ctrl_handler,
1860 + 3 + ARRAY_SIZE(custom_ctrls));
1862 + v4l2_err(&dev->v4l2_dev, "ctrl_handler init failed (%d)\n",
1864 + goto queue_cleanup;
1867 + dev->r_gain = 1000;
1868 + dev->b_gain = 1000;
1870 + v4l2_ctrl_new_std(&dev->ctrl_handler, &bcm2835_isp_ctrl_ops,
1871 + V4L2_CID_RED_BALANCE, 1, 0xffff, 1,
1874 + v4l2_ctrl_new_std(&dev->ctrl_handler, &bcm2835_isp_ctrl_ops,
1875 + V4L2_CID_BLUE_BALANCE, 1, 0xffff, 1,
1878 + v4l2_ctrl_new_std(&dev->ctrl_handler, &bcm2835_isp_ctrl_ops,
1879 + V4L2_CID_DIGITAL_GAIN, 1, 0xffff, 1, 1000);
1881 + for (i = 0; i < ARRAY_SIZE(custom_ctrls); i++) {
1882 + ctrl_template.name = custom_ctrls[i].name;
1883 + ctrl_template.id = custom_ctrls[i].id;
1884 + ctrl_template.dims[0] = custom_ctrls[i].size;
1885 + ctrl_template.flags = custom_ctrls[i].flags;
1886 + v4l2_ctrl_new_custom(&dev->ctrl_handler,
1887 + &ctrl_template, NULL);
1890 + node->vfd.ctrl_handler = &dev->ctrl_handler;
1891 + if (dev->ctrl_handler.error) {
1892 + ret = dev->ctrl_handler.error;
1893 + v4l2_err(&dev->v4l2_dev, "controls init failed (%d)\n",
1895 + v4l2_ctrl_handler_free(&dev->ctrl_handler);
1896 + goto ctrl_cleanup;
1900 + /* Define the device names */
1901 + snprintf(vfd->name, sizeof(node->vfd.name), "%s-%s%d", BCM2835_ISP_NAME,
1902 + node->name, node->id);
1904 + ret = video_register_device(vfd, VFL_TYPE_VIDEO, video_nr + index);
1906 + v4l2_err(&dev->v4l2_dev,
1907 + "Failed to register video %s[%d] device node\n",
1908 + node->name, node->id);
1909 + goto ctrl_cleanup;
1912 + node->registered = true;
1913 + video_set_drvdata(vfd, node);
1915 + v4l2_info(&dev->v4l2_dev,
1916 + "Device node %s[%d] registered as /dev/video%d\n",
1917 + node->name, node->id, vfd->num);
1922 + if (node_is_output(node))
1923 + v4l2_ctrl_handler_free(&dev->ctrl_handler);
1925 + vb2_queue_release(&node->queue);
1929 +/* Unregister one of the /dev/video<N> nodes associated with the ISP. */
1930 +static void unregister_node(struct bcm2835_isp_node *node)
1932 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1934 + v4l2_info(&dev->v4l2_dev,
1935 + "Unregistering node %s[%d] device node /dev/video%d\n",
1936 + node->name, node->id, node->vfd.num);
1938 + if (node->registered) {
1939 + video_unregister_device(&node->vfd);
1940 + if (node_is_output(node))
1941 + v4l2_ctrl_handler_free(&dev->ctrl_handler);
1942 + vb2_queue_release(&node->queue);
1946 + * node->supported_fmts.list is free'd automatically
1947 + * as a managed resource.
1949 + node->supported_fmts = NULL;
1950 + node->num_supported_fmts = 0;
1951 + node->vfd.ctrl_handler = NULL;
1952 + node->registered = false;
1955 +static void media_controller_unregister(struct bcm2835_isp_dev *dev)
1959 + v4l2_info(&dev->v4l2_dev, "Unregister from media controller\n");
1961 + if (dev->media_device_registered) {
1962 + media_device_unregister(&dev->mdev);
1963 + media_device_cleanup(&dev->mdev);
1964 + dev->media_device_registered = false;
1967 + kfree(dev->entity.name);
1968 + dev->entity.name = NULL;
1970 + if (dev->media_entity_registered) {
1971 + media_device_unregister_entity(&dev->entity);
1972 + dev->media_entity_registered = false;
1975 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
1976 + struct bcm2835_isp_node *node = &dev->node[i];
1978 + if (node->media_node_registered) {
1979 + media_remove_intf_links(node->intf_link->intf);
1980 + media_entity_remove_links(&dev->node[i].vfd.entity);
1981 + media_devnode_remove(node->intf_devnode);
1982 + media_device_unregister_entity(&node->vfd.entity);
1983 + kfree(node->vfd.entity.name);
1985 + node->media_node_registered = false;
1988 + dev->v4l2_dev.mdev = NULL;
1991 +static int media_controller_register_node(struct bcm2835_isp_dev *dev, int num)
1993 + struct bcm2835_isp_node *node = &dev->node[num];
1994 + struct media_entity *entity = &node->vfd.entity;
1995 + int output = node_is_output(node);
1999 + v4l2_info(&dev->v4l2_dev,
2000 + "Register %s node %d with media controller\n",
2001 + output ? "output" : "capture", num);
2002 + entity->obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
2003 + entity->function = MEDIA_ENT_F_IO_V4L;
2004 + entity->info.dev.major = VIDEO_MAJOR;
2005 + entity->info.dev.minor = node->vfd.minor;
2006 + name = kmalloc(BCM2835_ISP_ENTITY_NAME_LEN, GFP_KERNEL);
2009 + goto error_no_mem;
2011 + snprintf(name, BCM2835_ISP_ENTITY_NAME_LEN, "%s0-%s%d",
2012 + BCM2835_ISP_NAME, output ? "output" : "capture", num);
2013 + entity->name = name;
2014 + node->pad.flags = output ? MEDIA_PAD_FL_SOURCE : MEDIA_PAD_FL_SINK;
2015 + ret = media_entity_pads_init(entity, 1, &node->pad);
2017 + goto error_pads_init;
2018 + ret = media_device_register_entity(&dev->mdev, entity);
2020 + goto error_register_entity;
2022 + node->intf_devnode = media_devnode_create(&dev->mdev,
2023 + MEDIA_INTF_T_V4L_VIDEO, 0,
2024 + VIDEO_MAJOR, node->vfd.minor);
2025 + if (!node->intf_devnode) {
2027 + goto error_devnode_create;
2030 + node->intf_link = media_create_intf_link(entity,
2031 + &node->intf_devnode->intf,
2032 + MEDIA_LNK_FL_IMMUTABLE |
2033 + MEDIA_LNK_FL_ENABLED);
2034 + if (!node->intf_link) {
2036 + goto error_create_intf_link;
2040 + ret = media_create_pad_link(entity, 0, &dev->entity, num,
2041 + MEDIA_LNK_FL_IMMUTABLE |
2042 + MEDIA_LNK_FL_ENABLED);
2044 + ret = media_create_pad_link(&dev->entity, num, entity, 0,
2045 + MEDIA_LNK_FL_IMMUTABLE |
2046 + MEDIA_LNK_FL_ENABLED);
2048 + goto error_create_pad_link;
2050 + dev->node[num].media_node_registered = true;
2053 +error_create_pad_link:
2054 + media_remove_intf_links(&node->intf_devnode->intf);
2055 +error_create_intf_link:
2056 + media_devnode_remove(node->intf_devnode);
2057 +error_devnode_create:
2058 + media_device_unregister_entity(&node->vfd.entity);
2059 +error_register_entity:
2061 + kfree(entity->name);
2062 + entity->name = NULL;
2065 + v4l2_info(&dev->v4l2_dev, "Error registering node\n");
2070 +static int media_controller_register(struct bcm2835_isp_dev *dev)
2076 + v4l2_dbg(2, debug, &dev->v4l2_dev, "Registering with media controller\n");
2077 + dev->mdev.dev = dev->dev;
2078 + strscpy(dev->mdev.model, "bcm2835-isp",
2079 + sizeof(dev->mdev.model));
2080 + strscpy(dev->mdev.bus_info, "platform:bcm2835-isp",
2081 + sizeof(dev->mdev.bus_info));
2082 + media_device_init(&dev->mdev);
2083 + dev->v4l2_dev.mdev = &dev->mdev;
2085 + v4l2_dbg(2, debug, &dev->v4l2_dev, "Register entity for nodes\n");
2087 + name = kmalloc(BCM2835_ISP_ENTITY_NAME_LEN, GFP_KERNEL);
2092 + snprintf(name, BCM2835_ISP_ENTITY_NAME_LEN, "bcm2835_isp0");
2093 + dev->entity.name = name;
2094 + dev->entity.obj_type = MEDIA_ENTITY_TYPE_BASE;
2095 + dev->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
2097 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
2098 + dev->pad[i].flags = node_is_output(&dev->node[i]) ?
2099 + MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
2102 + ret = media_entity_pads_init(&dev->entity, BCM2835_ISP_NUM_NODES,
2107 + ret = media_device_register_entity(&dev->mdev, &dev->entity);
2111 + dev->media_entity_registered = true;
2112 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
2113 + ret = media_controller_register_node(dev, i);
2118 + ret = media_device_register(&dev->mdev);
2120 + dev->media_device_registered = true;
2125 +static int bcm2835_isp_remove(struct platform_device *pdev)
2127 + struct bcm2835_isp_dev *dev = platform_get_drvdata(pdev);
2130 + media_controller_unregister(dev);
2132 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++)
2133 + unregister_node(&dev->node[i]);
2135 + v4l2_device_unregister(&dev->v4l2_dev);
2137 + if (dev->component)
2138 + vchiq_mmal_component_finalise(dev->mmal_instance,
2141 + vchiq_mmal_finalise(dev->mmal_instance);
2146 +static int bcm2835_isp_probe(struct platform_device *pdev)
2148 + struct bcm2835_isp_dev *dev;
2152 + dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2156 + dev->dev = &pdev->dev;
2158 + ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2162 + ret = vchiq_mmal_init(&dev->mmal_instance);
2164 + v4l2_device_unregister(&dev->v4l2_dev);
2168 + ret = vchiq_mmal_component_init(dev->mmal_instance, "ril.isp",
2171 + v4l2_err(&dev->v4l2_dev,
2172 + "%s: failed to create ril.isp component\n", __func__);
2176 + if (dev->component->inputs < BCM2835_ISP_NUM_OUTPUTS ||
2177 + dev->component->outputs < BCM2835_ISP_NUM_CAPTURES +
2178 + BCM2835_ISP_NUM_METADATA) {
2179 + v4l2_err(&dev->v4l2_dev,
2180 + "%s: ril.isp returned %d i/p (%d expected), %d o/p (%d expected) ports\n",
2181 + __func__, dev->component->inputs,
2182 + BCM2835_ISP_NUM_OUTPUTS,
2183 + dev->component->outputs,
2184 + BCM2835_ISP_NUM_CAPTURES + BCM2835_ISP_NUM_METADATA);
2188 + atomic_set(&dev->num_streaming, 0);
2190 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
2191 + struct bcm2835_isp_node *node = &dev->node[i];
2193 + ret = register_node(dev, node, i);
2198 + ret = media_controller_register(dev);
2202 + platform_set_drvdata(pdev, dev);
2203 + v4l2_info(&dev->v4l2_dev, "Loaded V4L2 %s\n", BCM2835_ISP_NAME);
2207 + bcm2835_isp_remove(pdev);
2212 +static struct platform_driver bcm2835_isp_pdrv = {
2213 + .probe = bcm2835_isp_probe,
2214 + .remove = bcm2835_isp_remove,
2216 + .name = BCM2835_ISP_NAME,
2220 +module_platform_driver(bcm2835_isp_pdrv);
2222 +MODULE_DESCRIPTION("BCM2835 ISP driver");
2223 +MODULE_AUTHOR("Naushir Patuck <naush@raspberrypi.com>");
2224 +MODULE_LICENSE("GPL");
2225 +MODULE_VERSION("1.0");
2226 +MODULE_ALIAS("platform:bcm2835-isp");
2227 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-encodings.h
2228 +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-encodings.h
2229 @@ -113,6 +113,10 @@
2231 #define MMAL_ENCODING_EGL_IMAGE MMAL_FOURCC('E', 'G', 'L', 'I')
2233 +/** ISP image statistics format
2235 +#define MMAL_ENCODING_BRCM_STATS MMAL_FOURCC('S', 'T', 'A', 'T')
2239 /** \name Pre-defined audio encodings */
2240 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-parameters.h
2241 +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-parameters.h
2242 @@ -223,6 +223,62 @@ enum mmal_parameter_camera_type {
2243 MMAL_PARAMETER_SHUTTER_SPEED,
2244 /**< Takes a @ref MMAL_PARAMETER_AWB_GAINS_T */
2245 MMAL_PARAMETER_CUSTOM_AWB_GAINS,
2246 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_SETTINGS_T */
2247 + MMAL_PARAMETER_CAMERA_SETTINGS,
2248 + /**< Takes a @ref MMAL_PARAMETER_PRIVACY_INDICATOR_T */
2249 + MMAL_PARAMETER_PRIVACY_INDICATOR,
2250 + /**< Takes a @ref MMAL_PARAMETER_BOOLEAN_T */
2251 + MMAL_PARAMETER_VIDEO_DENOISE,
2252 + /**< Takes a @ref MMAL_PARAMETER_BOOLEAN_T */
2253 + MMAL_PARAMETER_STILLS_DENOISE,
2254 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_ANNOTATE_T */
2255 + MMAL_PARAMETER_ANNOTATE,
2256 + /**< Takes a @ref MMAL_PARAMETER_STEREOSCOPIC_MODE_T */
2257 + MMAL_PARAMETER_STEREOSCOPIC_MODE,
2258 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_INTERFACE_T */
2259 + MMAL_PARAMETER_CAMERA_INTERFACE,
2260 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_CLOCKING_MODE_T */
2261 + MMAL_PARAMETER_CAMERA_CLOCKING_MODE,
2262 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_RX_CONFIG_T */
2263 + MMAL_PARAMETER_CAMERA_RX_CONFIG,
2264 + /**< Takes a @ref MMAL_PARAMETER_CAMERA_RX_TIMING_T */
2265 + MMAL_PARAMETER_CAMERA_RX_TIMING,
2266 + /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
2267 + MMAL_PARAMETER_DPF_CONFIG,
2270 + /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
2271 + MMAL_PARAMETER_JPEG_RESTART_INTERVAL,
2272 + /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
2273 + MMAL_PARAMETER_CAMERA_ISP_BLOCK_OVERRIDE,
2274 + /**< Takes a @ref MMAL_PARAMETER_LENS_SHADING_T */
2275 + MMAL_PARAMETER_LENS_SHADING_OVERRIDE,
2276 + /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
2277 + MMAL_PARAMETER_BLACK_LEVEL,
2278 + /**< Takes a @ref MMAL_PARAMETER_RESIZE_T */
2279 + MMAL_PARAMETER_RESIZE_PARAMS,
2280 + /**< Takes a @ref MMAL_PARAMETER_CROP_T */
2281 + MMAL_PARAMETER_CROP,
2282 + /**< Takes a @ref MMAL_PARAMETER_INT32_T */
2283 + MMAL_PARAMETER_OUTPUT_SHIFT,
2284 + /**< Takes a @ref MMAL_PARAMETER_INT32_T */
2285 + MMAL_PARAMETER_CCM_SHIFT,
2286 + /**< Takes a @ref MMAL_PARAMETER_CUSTOM_CCM_T */
2287 + MMAL_PARAMETER_CUSTOM_CCM,
2288 + /**< Takes a @ref MMAL_PARAMETER_RATIONAL_T */
2289 + MMAL_PARAMETER_ANALOG_GAIN,
2290 + /**< Takes a @ref MMAL_PARAMETER_RATIONAL_T */
2291 + MMAL_PARAMETER_DIGITAL_GAIN,
2292 + /**< Takes a @ref MMAL_PARAMETER_DENOISE_T */
2293 + MMAL_PARAMETER_DENOISE,
2294 + /**< Takes a @ref MMAL_PARAMETER_SHARPEN_T */
2295 + MMAL_PARAMETER_SHARPEN,
2296 + /**< Takes a @ref MMAL_PARAMETER_GEQ_T */
2297 + MMAL_PARAMETER_GEQ,
2298 + /**< Tales a @ref MMAP_PARAMETER_DPC_T */
2299 + MMAL_PARAMETER_DPC,
2300 + /**< Tales a @ref MMAP_PARAMETER_GAMMA_T */
2301 + MMAL_PARAMETER_GAMMA,
2302 /**< Takes a @ref MMAL_PARAMETER_BOOLEAN_T */
2303 MMAL_PARAMETER_JPEG_IJG_SCALING,
2305 @@ -791,7 +847,102 @@ struct mmal_parameter_camera_info {
2306 struct mmal_parameter_camera_info_camera
2307 cameras[MMAL_PARAMETER_CAMERA_INFO_MAX_CAMERAS];
2308 struct mmal_parameter_camera_info_flash
2309 - flashes[MMAL_PARAMETER_CAMERA_INFO_MAX_FLASHES];
2310 + flashes[MMAL_PARAMETER_CAMERA_INFO_MAX_FLASHES];
2313 +struct mmal_parameter_ccm {
2314 + struct s32_fract ccm[3][3];
2318 +struct mmal_parameter_custom_ccm {
2319 + u32 enabled; /**< Enable the custom CCM. */
2320 + struct mmal_parameter_ccm ccm; /**< CCM to be used. */
2323 +struct mmal_parameter_lens_shading {
2325 + u32 grid_cell_size;
2329 + u32 mem_handle_table;
2330 + u32 ref_transform;
2333 +enum mmal_parameter_ls_gain_format_type {
2334 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U0P8_1 = 0,
2335 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U1P7_0 = 1,
2336 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U1P7_1 = 2,
2337 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U2P6_0 = 3,
2338 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U2P6_1 = 4,
2339 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U3P5_0 = 5,
2340 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U3P5_1 = 6,
2341 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_U4P10 = 7,
2342 + MMAL_PARAMETER_LS_GAIN_FORMAT_TYPE_DUMMY = 0x7FFFFFFF
2345 +struct mmal_parameter_lens_shading_v2 {
2347 + u32 grid_cell_size;
2351 + u32 mem_handle_table;
2352 + u32 ref_transform;
2353 + u32 corner_sampled;
2354 + enum mmal_parameter_ls_gain_format_type gain_format;
2357 +struct mmal_parameter_black_level {
2359 + u16 black_level_r;
2360 + u16 black_level_g;
2361 + u16 black_level_b;
2362 + u8 pad_[2]; /* Unused */
2365 +struct mmal_parameter_geq {
2368 + struct s32_fract slope;
2371 +#define MMAL_NUM_GAMMA_PTS 33
2372 +struct mmal_parameter_gamma {
2374 + u16 x[MMAL_NUM_GAMMA_PTS];
2375 + u16 y[MMAL_NUM_GAMMA_PTS];
2378 +struct mmal_parameter_denoise {
2381 + struct s32_fract slope;
2382 + struct s32_fract strength;
2385 +struct mmal_parameter_sharpen {
2387 + struct s32_fract threshold;
2388 + struct s32_fract strength;
2389 + struct s32_fract limit;
2392 +enum mmal_dpc_mode {
2393 + MMAL_DPC_MODE_OFF = 0,
2394 + MMAL_DPC_MODE_NORMAL = 1,
2395 + MMAL_DPC_MODE_STRONG = 2,
2396 + MMAL_DPC_MODE_MAX = 0x7FFFFFFF,
2399 +struct mmal_parameter_dpc {
2404 +struct mmal_parameter_crop {
2405 + struct vchiq_mmal_rect rect;