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
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 | 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
38 @@ -3566,6 +3566,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 @@ -45,6 +45,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_V4L2 && (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 +static unsigned int debug;
560 +module_param(debug, uint, 0644);
561 +MODULE_PARM_DESC(debug, "activates debug info");
563 +static unsigned int video_nr = 13;
564 +module_param(video_nr, uint, 0644);
565 +MODULE_PARM_DESC(video_nr, "base video device number");
567 +#define BCM2835_ISP_NAME "bcm2835-isp"
568 +#define BCM2835_ISP_ENTITY_NAME_LEN 32
570 +#define BCM2835_ISP_NUM_OUTPUTS 1
571 +#define BCM2835_ISP_NUM_CAPTURES 2
572 +#define BCM2835_ISP_NUM_METADATA 1
574 +#define BCM2835_ISP_NUM_NODES \
575 + (BCM2835_ISP_NUM_OUTPUTS + BCM2835_ISP_NUM_CAPTURES + \
576 + BCM2835_ISP_NUM_METADATA)
578 +/* Default frame dimension of 1280 pixels. */
579 +#define DEFAULT_DIM 1280U
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
585 +#define MAX_DIM 16384U
587 + * Minimum frame dimension of 64 pixels. Anything lower, and the tiling
588 + * algorithm may not be able to cope when applying filter context.
592 +/* Timeout for stop_streaming to allow all buffers to return */
593 +#define COMPLETE_TIMEOUT (2 * HZ)
595 +/* Per-queue, driver-specific private data */
596 +struct bcm2835_isp_q_data {
598 + * These parameters should be treated as gospel, with everything else
599 + * being determined from them.
601 + unsigned int bytesperline;
602 + unsigned int width;
603 + unsigned int height;
604 + unsigned int sizeimage;
605 + const struct bcm2835_isp_fmt *fmt;
609 + * Structure to describe a single node /dev/video<N> which represents a single
610 + * input or output queue to the ISP device.
612 +struct bcm2835_isp_node {
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;
624 + struct vb2_queue queue;
625 + unsigned int sequence;
627 + /* The list of formats supported on the node. */
628 + struct bcm2835_isp_fmt const **supported_fmts;
629 + unsigned int num_supported_fmts;
631 + struct bcm2835_isp_q_data q_data;
633 + /* Parent device structure */
634 + struct bcm2835_isp_dev *dev;
637 + bool media_node_registered;
641 + * Structure representing the entire ISP device, comprising several input and
642 + * output nodes /dev/video<N>.
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;
656 + struct bcm2835_isp_node node[BCM2835_ISP_NUM_NODES];
657 + struct media_pad pad[BCM2835_ISP_NUM_NODES];
658 + atomic_t num_streaming;
660 + /* Image pipeline controls. */
665 +struct bcm2835_isp_buffer {
666 + struct vb2_v4l2_buffer vb;
667 + struct mmal_buffer mmal;
671 +inline struct bcm2835_isp_dev *node_get_dev(struct bcm2835_isp_node *node)
676 +static inline bool node_is_output(struct bcm2835_isp_node *node)
678 + return node->queue.type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
681 +static inline bool node_is_capture(struct bcm2835_isp_node *node)
683 + return node->queue.type == V4L2_BUF_TYPE_VIDEO_CAPTURE;
686 +static inline bool node_is_stats(struct bcm2835_isp_node *node)
688 + return node->queue.type == V4L2_BUF_TYPE_META_CAPTURE;
691 +static inline enum v4l2_buf_type index_to_queue_type(int index)
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;
698 + return V4L2_BUF_TYPE_META_CAPTURE;
701 +static int set_isp_param(struct bcm2835_isp_node *node, u32 parameter,
702 + void *value, u32 value_size)
704 + struct bcm2835_isp_dev *dev = node_get_dev(node);
706 + return vchiq_mmal_port_parameter_set(dev->mmal_instance, node->port,
707 + parameter, value, value_size);
710 +static int set_wb_gains(struct bcm2835_isp_node *node)
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 }
718 + return set_isp_param(node, MMAL_PARAMETER_CUSTOM_AWB_GAINS,
719 + &gains, sizeof(gains));
722 +static int set_digital_gain(struct bcm2835_isp_node *node, uint32_t gain)
724 + struct mmal_parameter_rational digital_gain = {
729 + return set_isp_param(node, MMAL_PARAMETER_DIGITAL_GAIN,
730 + &digital_gain, sizeof(digital_gain));
733 +static const struct bcm2835_isp_fmt *get_fmt(u32 mmal_fmt)
737 + for (i = 0; i < ARRAY_SIZE(supported_formats); i++) {
738 + if (supported_formats[i].mmal_fmt == mmal_fmt)
739 + return &supported_formats[i];
745 +struct bcm2835_isp_fmt *find_format_by_fourcc(unsigned int fourcc,
746 + struct bcm2835_isp_node *node)
748 + const struct bcm2835_isp_fmt *fmt;
751 + for (i = 0; i < node->num_supported_fmts; i++) {
752 + fmt = node->supported_fmts[i];
753 + if (fmt->fourcc == fourcc)
761 +struct bcm2835_isp_fmt *find_format(struct v4l2_format *f,
762 + struct bcm2835_isp_node *node)
764 + return find_format_by_fourcc(node_is_stats(node) ?
765 + f->fmt.meta.dataformat :
766 + f->fmt.pix.pixelformat,
770 +/* vb2_to_mmal_buffer() - converts vb2 buffer header to MMAL
772 + * Copies all the required fields from a VB2 buffer to the MMAL buffer header,
773 + * ready for sending to the VPU.
775 +static void vb2_to_mmal_buffer(struct mmal_buffer *buf,
776 + struct vb2_v4l2_buffer *vb2)
780 + buf->mmal_flags = 0;
781 + if (vb2->flags & V4L2_BUF_FLAG_KEYFRAME)
782 + buf->mmal_flags |= MMAL_BUFFER_HEADER_FLAG_KEYFRAME;
784 + /* Data must be framed correctly as one frame per buffer. */
785 + buf->mmal_flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_END;
787 + buf->length = vb2->vb2_buf.planes[0].bytesused;
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.
793 + if (!buf->length || vb2->flags & V4L2_BUF_FLAG_LAST)
794 + buf->mmal_flags |= MMAL_BUFFER_HEADER_FLAG_EOS;
796 + /* vb2 timestamps in nsecs, mmal in usecs */
797 + pts = vb2->vb2_buf.timestamp;
800 + buf->dts = MMAL_TIME_UNKNOWN;
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)
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;
812 + q_buf = container_of(mmal_buf, struct bcm2835_isp_buffer, mmal);
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);
821 + v4l2_err(&dev->v4l2_dev,
822 + "%s: Unexpected event on output callback - %08x\n",
823 + __func__, mmal_buf->cmd);
826 + /* error in transfer */
828 + /* there was a buffer with the error so return it */
829 + vb2_buffer_done(&vb2->vb2_buf, VB2_BUF_STATE_ERROR);
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);
840 + if (!port->enabled)
841 + complete(&dev->frame_cmplt);
844 +static void setup_mmal_port_format(struct bcm2835_isp_node *node,
845 + struct vchiq_mmal_port *port)
847 + struct bcm2835_isp_q_data *q_data = &node->q_data;
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;
859 +static int setup_mmal_port(struct bcm2835_isp_node *node)
861 + struct bcm2835_isp_dev *dev = node_get_dev(node);
862 + unsigned int enable = 1;
865 + v4l2_dbg(2, debug, &dev->v4l2_dev, "%s: setup %s[%d]\n", __func__,
866 + node->name, node->id);
868 + vchiq_mmal_port_parameter_set(dev->mmal_instance, node->port,
869 + MMAL_PARAMETER_ZERO_COPY, &enable,
871 + setup_mmal_port_format(node, node->port);
872 + ret = vchiq_mmal_port_set_format(dev->mmal_instance, node->port);
874 + v4l2_dbg(1, debug, &dev->v4l2_dev,
875 + "%s: vchiq_mmal_port_set_format failed\n",
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);
891 +static int bcm2835_isp_mmal_buf_cleanup(struct mmal_buffer *mmal_buf)
893 + mmal_vchi_buffer_cleanup(mmal_buf);
895 + if (mmal_buf->dma_buf) {
896 + dma_buf_put(mmal_buf->dma_buf);
897 + mmal_buf->dma_buf = NULL;
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[])
909 + struct bcm2835_isp_node *node = vb2_get_drv_priv(q);
912 + if (setup_mmal_port(node))
915 + size = node->q_data.sizeimage;
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);
924 + return sizes[0] < size ? -EINVAL : 0;
929 + node->port->current_buffer.size = size;
931 + if (*nbuffers < node->port->minimum_buffer.num)
932 + *nbuffers = node->port->minimum_buffer.num;
934 + node->port->current_buffer.num = *nbuffers;
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);
942 +static int bcm2835_isp_buf_init(struct vb2_buffer *vb)
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);
950 + v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: vb %p\n", __func__, vb);
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);
958 +static int bcm2835_isp_buf_prepare(struct vb2_buffer *vb)
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;
968 + v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: type: %d ptr %p\n",
969 + __func__, vb->vb2_queue->type, vb);
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__);
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);
989 + if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type))
990 + vb2_set_plane_payload(vb, 0, node->q_data.sizeimage);
992 + switch (vb->memory) {
993 + case VB2_MEMORY_DMABUF:
994 + dma_buf = dma_buf_get(vb->planes[0].m.fd);
996 + if (dma_buf != buf->mmal.dma_buf) {
998 + * dmabuf either hasn't already been mapped, or it has
1001 + if (buf->mmal.dma_buf) {
1002 + v4l2_err(&dev->v4l2_dev,
1003 + "%s Buffer changed - why did the core not call cleanup?\n",
1005 + bcm2835_isp_mmal_buf_cleanup(&buf->mmal);
1008 + buf->mmal.dma_buf = dma_buf;
1011 + * Already have a reference to the buffer, so release it
1014 + dma_buf_put(dma_buf);
1018 + case VB2_MEMORY_MMAP:
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.
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);
1033 + v4l2_err(&dev->v4l2_dev,
1034 + "%s: Failed to expbuf idx %d, ret %d\n",
1035 + __func__, vb->index, ret);
1048 +static void bcm2835_isp_node_buffer_queue(struct vb2_buffer *buf)
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);
1057 + v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: node %s[%d], buffer %p\n",
1058 + __func__, node->name, node->id, buffer);
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);
1067 +static void bcm2835_isp_buffer_cleanup(struct vb2_buffer *vb)
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);
1073 + bcm2835_isp_mmal_buf_cleanup(&buffer->mmal);
1076 +static int bcm2835_isp_node_start_streaming(struct vb2_queue *q,
1077 + unsigned int count)
1079 + struct bcm2835_isp_node *node = vb2_get_drv_priv(q);
1080 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1083 + v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: node %s[%d] (count %u)\n",
1084 + __func__, node->name, node->id, count);
1086 + ret = vchiq_mmal_component_enable(dev->mmal_instance, dev->component);
1088 + v4l2_err(&dev->v4l2_dev, "%s: Failed enabling component, ret %d\n",
1093 + node->sequence = 0;
1094 + node->port->cb_ctx = node;
1095 + ret = vchiq_mmal_port_enable(dev->mmal_instance, node->port,
1098 + atomic_inc(&dev->num_streaming);
1100 + v4l2_err(&dev->v4l2_dev,
1101 + "%s: Failed enabling port, ret %d\n", __func__, ret);
1106 +static void bcm2835_isp_node_stop_streaming(struct vb2_queue *q)
1108 + struct bcm2835_isp_node *node = vb2_get_drv_priv(q);
1109 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1113 + v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: node %s[%d], mmal port %p\n",
1114 + __func__, node->name, node->id, node->port);
1116 + init_completion(&dev->frame_cmplt);
1118 + /* Disable MMAL port - this will flush buffers back */
1119 + ret = vchiq_mmal_port_disable(dev->mmal_instance, node->port);
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",
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);
1133 + v4l2_err(&dev->v4l2_dev,
1134 + "%s: Timeout waiting for buffers to be returned - %d outstanding\n",
1136 + atomic_read(&node->port->buffers_with_vpu));
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);
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,
1155 + v4l2_err(&dev->v4l2_dev,
1156 + "%s: Failed disabling component, ret %d\n",
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
1166 + vb2_wait_for_all_buffers(&node->queue);
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,
1180 +struct bcm2835_isp_fmt *get_default_format(struct bcm2835_isp_node *node)
1182 + return node->supported_fmts[0];
1185 +static inline unsigned int get_bytesperline(int width,
1186 + const struct bcm2835_isp_fmt *fmt)
1188 + /* GPU aligns 24bpp images to a multiple of 32 pixels (not bytes). */
1189 + if (fmt->depth == 24)
1190 + return ALIGN(width, 32) * 3;
1192 + return ALIGN((width * fmt->depth) >> 3, fmt->bytesperline_align);
1195 +static inline unsigned int get_sizeimage(int bpl, int width, int height,
1196 + const struct bcm2835_isp_fmt *fmt)
1198 + return (bpl * height * fmt->size_multiplier_x2) >> 1;
1201 +static int bcm2835_isp_s_ctrl(struct v4l2_ctrl *ctrl)
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];
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.
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.
1216 + switch (ctrl->id) {
1217 + case V4L2_CID_RED_BALANCE:
1218 + dev->r_gain = ctrl->val;
1219 + ret = set_wb_gains(node);
1221 + case V4L2_CID_BLUE_BALANCE:
1222 + dev->b_gain = ctrl->val;
1223 + ret = set_wb_gains(node);
1225 + case V4L2_CID_DIGITAL_GAIN:
1226 + ret = set_digital_gain(node, ctrl->val);
1228 + case V4L2_CID_USER_BCM2835_ISP_CC_MATRIX:
1229 + ret = set_isp_param(node, MMAL_PARAMETER_CUSTOM_CCM,
1231 + sizeof(struct bcm2835_isp_custom_ccm));
1233 + case V4L2_CID_USER_BCM2835_ISP_LENS_SHADING:
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;
1240 + v4l2_ls = (struct bcm2835_isp_lens_shading *)ctrl->p_new.p_u8;
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.
1247 + memcpy(&ls, v4l2_ls, sizeof(ls));
1249 + dmabuf = dma_buf_get(v4l2_ls->dmabuf);
1250 + if (IS_ERR_OR_NULL(dmabuf))
1253 + ret = vc_sm_cma_import_dmabuf(dmabuf, &vcsm_handle);
1255 + dma_buf_put(dmabuf);
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.
1266 + ret = set_isp_param(node,
1267 + MMAL_PARAMETER_LENS_SHADING_OVERRIDE,
1273 + vc_sm_cma_free(vcsm_handle);
1274 + dma_buf_put(dmabuf);
1277 + case V4L2_CID_USER_BCM2835_ISP_BLACK_LEVEL:
1278 + ret = set_isp_param(node, MMAL_PARAMETER_BLACK_LEVEL,
1280 + sizeof(struct bcm2835_isp_black_level));
1282 + case V4L2_CID_USER_BCM2835_ISP_GEQ:
1283 + ret = set_isp_param(node, MMAL_PARAMETER_GEQ,
1285 + sizeof(struct bcm2835_isp_geq));
1287 + case V4L2_CID_USER_BCM2835_ISP_GAMMA:
1288 + ret = set_isp_param(node, MMAL_PARAMETER_GAMMA,
1290 + sizeof(struct bcm2835_isp_gamma));
1292 + case V4L2_CID_USER_BCM2835_ISP_DENOISE:
1293 + ret = set_isp_param(node, MMAL_PARAMETER_DENOISE,
1295 + sizeof(struct bcm2835_isp_denoise));
1297 + case V4L2_CID_USER_BCM2835_ISP_SHARPEN:
1298 + ret = set_isp_param(node, MMAL_PARAMETER_SHARPEN,
1300 + sizeof(struct bcm2835_isp_sharpen));
1302 + case V4L2_CID_USER_BCM2835_ISP_DPC:
1303 + ret = set_isp_param(node, MMAL_PARAMETER_DPC,
1305 + sizeof(struct bcm2835_isp_dpc));
1308 + v4l2_info(&dev->v4l2_dev, "Unrecognised control\n");
1313 + v4l2_err(&dev->v4l2_dev, "%s: Failed setting ctrl \"%s\" (%08x), err %d\n",
1314 + __func__, ctrl->name, ctrl->id, ret);
1321 +static const struct v4l2_ctrl_ops bcm2835_isp_ctrl_ops = {
1322 + .s_ctrl = bcm2835_isp_s_ctrl,
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
1334 +static int populate_qdata_fmt(struct v4l2_format *f,
1335 + struct bcm2835_isp_node *node)
1337 + struct bcm2835_isp_dev *dev = node_get_dev(node);
1338 + struct bcm2835_isp_q_data *q_data = &node->q_data;
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);
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;
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;
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);
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;
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);
1372 + setup_mmal_port_format(node, node->port);
1373 + ret = vchiq_mmal_port_set_format(dev->mmal_instance, node->port);
1375 + v4l2_err(&dev->v4l2_dev,
1376 + "%s: Failed vchiq_mmal_port_set_format on port, ret %d\n",
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",
1385 + q_data->sizeimage,
1386 + node->port->minimum_buffer.size);
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);
1397 +static int bcm2835_isp_node_querycap(struct file *file, void *priv,
1398 + struct v4l2_capability *cap)
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);
1408 +static int bcm2835_isp_node_g_fmt(struct file *file, void *priv,
1409 + struct v4l2_format *f)
1411 + struct bcm2835_isp_node *node = video_drvdata(file);
1413 + if (f->type != node->queue.type)
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;
1421 + struct bcm2835_isp_q_data *q_data = &node->q_data;
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;
1435 +static int bcm2835_isp_node_enum_fmt(struct file *file, void *priv,
1436 + struct v4l2_fmtdesc *f)
1438 + struct bcm2835_isp_node *node = video_drvdata(file);
1440 + if (f->type != node->queue.type)
1443 + if (f->index < node->num_supported_fmts) {
1444 + /* Format found */
1445 + f->pixelformat = node->supported_fmts[f->index]->fourcc;
1453 +static int bcm2835_isp_enum_framesizes(struct file *file, void *priv,
1454 + struct v4l2_frmsizeenum *fsize)
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;
1460 + if (node_is_stats(node) || fsize->index)
1463 + fmt = find_format_by_fourcc(fsize->pixel_format, node);
1465 + v4l2_err(&dev->v4l2_dev, "Invalid pixel code: %x\n",
1466 + fsize->pixel_format);
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;
1475 + fsize->stepwise.min_height = MIN_DIM;
1476 + fsize->stepwise.max_height = MAX_DIM;
1477 + fsize->stepwise.step_height = fmt->step_size;
1482 +static int bcm2835_isp_node_try_fmt(struct file *file, void *priv,
1483 + struct v4l2_format *f)
1485 + struct bcm2835_isp_node *node = video_drvdata(file);
1486 + const struct bcm2835_isp_fmt *fmt;
1488 + if (f->type != node->queue.type)
1491 + fmt = find_format(f, node);
1493 + fmt = get_default_format(node);
1495 + if (!node_is_stats(node)) {
1496 + f->fmt.pix.width = max(min(f->fmt.pix.width, MAX_DIM),
1498 + f->fmt.pix.height = max(min(f->fmt.pix.height, MAX_DIM),
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,
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);
1510 + f->fmt.meta.dataformat = fmt->fourcc;
1511 + f->fmt.meta.buffersize = node->port->minimum_buffer.size;
1517 +static int bcm2835_isp_node_s_fmt(struct file *file, void *priv,
1518 + struct v4l2_format *f)
1520 + struct bcm2835_isp_node *node = video_drvdata(file);
1523 + if (f->type != node->queue.type)
1526 + ret = bcm2835_isp_node_try_fmt(file, priv, f);
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);
1534 + return populate_qdata_fmt(f, node);
1537 +static int bcm2835_isp_node_s_selection(struct file *file, void *fh,
1538 + struct v4l2_selection *s)
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);
1544 + /* This return value is required fro V4L2 compliance. */
1545 + if (node_is_stats(node))
1548 + if (!s->r.width || !s->r.height)
1551 + /* We can only set crop on the input. */
1552 + switch (s->target) {
1553 + case V4L2_SEL_TGT_CROP:
1555 + * Adjust the crop window if it goes outside of the frame
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);
1567 + case V4L2_SEL_TGT_CROP_DEFAULT:
1568 + /* Default (i.e. no) crop window. */
1571 + s->r.width = node->q_data.width;
1572 + s->r.height = node->q_data.height;
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;
1583 + return vchiq_mmal_port_parameter_set(dev->mmal_instance, node->port,
1584 + MMAL_PARAMETER_CROP,
1585 + &crop, sizeof(crop));
1588 +static int bcm2835_isp_node_g_selection(struct file *file, void *fh,
1589 + struct v4l2_selection *s)
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);
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,
1602 + MMAL_PARAMETER_CROP,
1603 + &crop, &crop_size);
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;
1611 + case V4L2_SEL_TGT_CROP_DEFAULT:
1612 + case V4L2_SEL_TGT_CROP_BOUNDS:
1613 + /* Default (i.e. no) crop window. */
1616 + s->r.width = node->q_data.width;
1617 + s->r.height = node->q_data.height;
1627 +static int bcm3285_isp_subscribe_event(struct v4l2_fh *fh,
1628 + const struct v4l2_event_subscription *s)
1630 + switch (s->type) {
1631 + /* Cannot change source parameters dynamically at runtime. */
1632 + case V4L2_EVENT_SOURCE_CHANGE:
1634 + case V4L2_EVENT_CTRL:
1635 + return v4l2_ctrl_subscribe_event(fh, s);
1637 + return v4l2_event_subscribe(fh, s, 4, NULL);
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,
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,
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,
1668 + .vidioc_streamon = vb2_ioctl_streamon,
1669 + .vidioc_streamoff = vb2_ioctl_streamoff,
1671 + .vidioc_subscribe_event = bcm3285_isp_subscribe_event,
1672 + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1676 + * Size of the array to provide to the VPU when asking for the list of supported
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.
1683 +#define MAX_SUPPORTED_ENCODINGS 50
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)
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);
1695 + ret = vchiq_mmal_port_parameter_get(dev->mmal_instance, node->port,
1696 + MMAL_PARAMETER_SUPPORTED_ENCODINGS,
1697 + &fourccs, ¶m_size);
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",
1704 + num_encodings = MAX_SUPPORTED_ENCODINGS;
1706 + v4l2_err(&dev->v4l2_dev, "%s: get_param ret %u.\n",
1711 + num_encodings = param_size / sizeof(u32);
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.
1718 + list = devm_kzalloc(dev->dev,
1719 + sizeof(struct bcm2835_isp_fmt *) * num_encodings,
1723 + node->supported_fmts = list;
1725 + for (i = 0, j = 0; i < num_encodings; i++) {
1726 + const struct bcm2835_isp_fmt *fmt = get_fmt(fourccs[i]);
1733 + node->num_supported_fmts = j;
1739 + * Register a device node /dev/video<N> to go along with one of the ISP's input
1740 + * or output nodes.
1742 +static int register_node(struct bcm2835_isp_dev *dev,
1743 + struct bcm2835_isp_node *node,
1746 + struct video_device *vfd;
1747 + struct vb2_queue *queue;
1750 + mutex_init(&node->lock);
1754 + queue = &node->queue;
1755 + queue->type = index_to_queue_type(index);
1757 + * Setup the node type-specific params.
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.
1764 + switch (queue->type) {
1765 + case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1766 + vfd->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
1768 + node->vfl_dir = VFL_DIR_TX;
1769 + node->name = "output";
1770 + node->port = &dev->component->input[node->id];
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);
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);
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);
1800 + ret = bcm2835_isp_get_supported_fmts(node);
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,
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;
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);
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;
1836 + ret = vb2_queue_init(queue);
1838 + v4l2_info(&dev->v4l2_dev, "vb2_queue_init failed\n");
1842 + /* Set some controls and defaults, but only on the VIDEO_OUTPUT node. */
1843 + if (node_is_output(node)) {
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,
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));
1860 + v4l2_err(&dev->v4l2_dev, "ctrl_handler init failed (%d)\n",
1862 + goto queue_cleanup;
1865 + dev->r_gain = 1000;
1866 + dev->b_gain = 1000;
1868 + v4l2_ctrl_new_std(&dev->ctrl_handler, &bcm2835_isp_ctrl_ops,
1869 + V4L2_CID_RED_BALANCE, 1, 0xffff, 1,
1872 + v4l2_ctrl_new_std(&dev->ctrl_handler, &bcm2835_isp_ctrl_ops,
1873 + V4L2_CID_BLUE_BALANCE, 1, 0xffff, 1,
1876 + v4l2_ctrl_new_std(&dev->ctrl_handler, &bcm2835_isp_ctrl_ops,
1877 + V4L2_CID_DIGITAL_GAIN, 1, 0xffff, 1, 1000);
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);
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",
1893 + v4l2_ctrl_handler_free(&dev->ctrl_handler);
1894 + goto ctrl_cleanup;
1898 + /* Define the device names */
1899 + snprintf(vfd->name, sizeof(node->vfd.name), "%s-%s%d", BCM2835_ISP_NAME,
1900 + node->name, node->id);
1902 + ret = video_register_device(vfd, VFL_TYPE_VIDEO, video_nr + index);
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;
1910 + node->registered = true;
1911 + video_set_drvdata(vfd, node);
1913 + v4l2_info(&dev->v4l2_dev,
1914 + "Device node %s[%d] registered as /dev/video%d\n",
1915 + node->name, node->id, vfd->num);
1920 + if (node_is_output(node))
1921 + v4l2_ctrl_handler_free(&dev->ctrl_handler);
1923 + vb2_queue_release(&node->queue);
1927 +/* Unregister one of the /dev/video<N> nodes associated with the ISP. */
1928 +static void unregister_node(struct bcm2835_isp_node *node)
1930 + struct bcm2835_isp_dev *dev = node_get_dev(node);
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);
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);
1944 + * node->supported_fmts.list is free'd automatically
1945 + * as a managed resource.
1947 + node->supported_fmts = NULL;
1948 + node->num_supported_fmts = 0;
1949 + node->vfd.ctrl_handler = NULL;
1950 + node->registered = false;
1953 +static void media_controller_unregister(struct bcm2835_isp_dev *dev)
1957 + v4l2_info(&dev->v4l2_dev, "Unregister from media controller\n");
1959 + if (dev->media_device_registered) {
1960 + media_device_unregister(&dev->mdev);
1961 + media_device_cleanup(&dev->mdev);
1962 + dev->media_device_registered = false;
1965 + kfree(dev->entity.name);
1966 + dev->entity.name = NULL;
1968 + if (dev->media_entity_registered) {
1969 + media_device_unregister_entity(&dev->entity);
1970 + dev->media_entity_registered = false;
1973 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
1974 + struct bcm2835_isp_node *node = &dev->node[i];
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);
1983 + node->media_node_registered = false;
1986 + dev->v4l2_dev.mdev = NULL;
1989 +static int media_controller_register_node(struct bcm2835_isp_dev *dev, int num)
1991 + struct bcm2835_isp_node *node = &dev->node[num];
1992 + struct media_entity *entity = &node->vfd.entity;
1993 + int output = node_is_output(node);
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);
2007 + goto error_no_mem;
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);
2015 + goto error_pads_init;
2016 + ret = media_device_register_entity(&dev->mdev, entity);
2018 + goto error_register_entity;
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) {
2025 + goto error_devnode_create;
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) {
2034 + goto error_create_intf_link;
2038 + ret = media_create_pad_link(entity, 0, &dev->entity, num,
2039 + MEDIA_LNK_FL_IMMUTABLE |
2040 + MEDIA_LNK_FL_ENABLED);
2042 + ret = media_create_pad_link(&dev->entity, num, entity, 0,
2043 + MEDIA_LNK_FL_IMMUTABLE |
2044 + MEDIA_LNK_FL_ENABLED);
2046 + goto error_create_pad_link;
2048 + dev->node[num].media_node_registered = true;
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:
2059 + kfree(entity->name);
2060 + entity->name = NULL;
2063 + v4l2_info(&dev->v4l2_dev, "Error registering node\n");
2068 +static int media_controller_register(struct bcm2835_isp_dev *dev)
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;
2083 + v4l2_dbg(2, debug, &dev->v4l2_dev, "Register entity for nodes\n");
2085 + name = kmalloc(BCM2835_ISP_ENTITY_NAME_LEN, GFP_KERNEL);
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;
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;
2100 + ret = media_entity_pads_init(&dev->entity, BCM2835_ISP_NUM_NODES,
2105 + ret = media_device_register_entity(&dev->mdev, &dev->entity);
2109 + dev->media_entity_registered = true;
2110 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
2111 + ret = media_controller_register_node(dev, i);
2116 + ret = media_device_register(&dev->mdev);
2118 + dev->media_device_registered = true;
2123 +static int bcm2835_isp_remove(struct platform_device *pdev)
2125 + struct bcm2835_isp_dev *dev = platform_get_drvdata(pdev);
2128 + media_controller_unregister(dev);
2130 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++)
2131 + unregister_node(&dev->node[i]);
2133 + v4l2_device_unregister(&dev->v4l2_dev);
2135 + if (dev->component)
2136 + vchiq_mmal_component_finalise(dev->mmal_instance,
2139 + vchiq_mmal_finalise(dev->mmal_instance);
2144 +static int bcm2835_isp_probe(struct platform_device *pdev)
2146 + struct bcm2835_isp_dev *dev;
2150 + dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2154 + dev->dev = &pdev->dev;
2156 + ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2160 + ret = vchiq_mmal_init(&dev->mmal_instance);
2162 + v4l2_device_unregister(&dev->v4l2_dev);
2166 + ret = vchiq_mmal_component_init(dev->mmal_instance, "ril.isp",
2169 + v4l2_err(&dev->v4l2_dev,
2170 + "%s: failed to create ril.isp component\n", __func__);
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);
2186 + atomic_set(&dev->num_streaming, 0);
2188 + for (i = 0; i < BCM2835_ISP_NUM_NODES; i++) {
2189 + struct bcm2835_isp_node *node = &dev->node[i];
2191 + ret = register_node(dev, node, i);
2196 + ret = media_controller_register(dev);
2200 + platform_set_drvdata(pdev, dev);
2201 + v4l2_info(&dev->v4l2_dev, "Loaded V4L2 %s\n", BCM2835_ISP_NAME);
2205 + bcm2835_isp_remove(pdev);
2210 +static struct platform_driver bcm2835_isp_pdrv = {
2211 + .probe = bcm2835_isp_probe,
2212 + .remove = bcm2835_isp_remove,
2214 + .name = BCM2835_ISP_NAME,
2218 +module_platform_driver(bcm2835_isp_pdrv);
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 @@
2229 #define MMAL_ENCODING_EGL_IMAGE MMAL_FOURCC('E', 'G', 'L', 'I')
2231 +/** ISP image statistics format
2233 +#define MMAL_ENCODING_BRCM_STATS MMAL_FOURCC('S', 'T', 'A', 'T')
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,
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,
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];
2311 +struct mmal_parameter_ccm {
2312 + struct mmal_parameter_rational ccm[3][3];
2316 +struct mmal_parameter_custom_ccm {
2317 + u32 enabled; /**< Enable the custom CCM. */
2318 + struct mmal_parameter_ccm ccm; /**< CCM to be used. */
2321 +struct mmal_parameter_lens_shading {
2323 + u32 grid_cell_size;
2327 + u32 mem_handle_table;
2328 + u32 ref_transform;
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
2343 +struct mmal_parameter_lens_shading_v2 {
2345 + u32 grid_cell_size;
2349 + u32 mem_handle_table;
2350 + u32 ref_transform;
2351 + u32 corner_sampled;
2352 + enum mmal_parameter_ls_gain_format_type gain_format;
2355 +struct mmal_parameter_black_level {
2357 + u16 black_level_r;
2358 + u16 black_level_g;
2359 + u16 black_level_b;
2360 + u8 pad_[2]; /* Unused */
2363 +struct mmal_parameter_geq {
2366 + struct mmal_parameter_rational slope;
2369 +#define MMAL_NUM_GAMMA_PTS 33
2370 +struct mmal_parameter_gamma {
2372 + u16 x[MMAL_NUM_GAMMA_PTS];
2373 + u16 y[MMAL_NUM_GAMMA_PTS];
2376 +struct mmal_parameter_denoise {
2379 + struct mmal_parameter_rational slope;
2380 + struct mmal_parameter_rational strength;
2383 +struct mmal_parameter_sharpen {
2385 + struct mmal_parameter_rational threshold;
2386 + struct mmal_parameter_rational strength;
2387 + struct mmal_parameter_rational limit;
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,
2397 +struct mmal_parameter_dpc {
2402 +struct mmal_parameter_crop {
2403 + struct vchiq_mmal_rect rect;