1 From ad386ab52fe80a35042a1a0d0eef0c87d2419dfc Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Mon, 4 May 2020 12:25:41 +0300
4 Subject: [PATCH] media: bcm2835-unicam: Driver for CCP2/CSI2 camera
7 Add a driver for the Unicam camera receiver block on BCM283x processors.
8 Compared to the bcm2835-camera driver present in staging, this driver
9 handles the Unicam block only (CSI-2 receiver), and doesn't depend on
10 the VC4 firmware running on the VPU.
12 The commit is made up of a series of changes cherry-picked from the
13 rpi-5.4.y branch of https://github.com/raspberrypi/linux/ with
14 additional enhancements, forward-ported to the mainline kernel.
16 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
17 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
18 Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
19 Reported-by: kbuild test robot <lkp@intel.com>
22 drivers/media/platform/bcm2835/Kconfig | 15 +
23 drivers/media/platform/bcm2835/Makefile | 3 +
24 .../media/platform/bcm2835/bcm2835-unicam.c | 2825 +++++++++++++++++
25 .../media/platform/bcm2835/vc4-regs-unicam.h | 253 ++
26 5 files changed, 3097 insertions(+), 1 deletion(-)
27 create mode 100644 drivers/media/platform/bcm2835/Kconfig
28 create mode 100644 drivers/media/platform/bcm2835/Makefile
29 create mode 100644 drivers/media/platform/bcm2835/bcm2835-unicam.c
30 create mode 100644 drivers/media/platform/bcm2835/vc4-regs-unicam.h
34 @@ -3430,7 +3430,7 @@ M: Raspberry Pi Kernel Maintenance <kern
35 L: linux-media@vger.kernel.org
37 F: drivers/media/platform/bcm2835/
38 -F: Documentation/devicetree/bindings/media/bcm2835-unicam.txt
39 +F: Documentation/devicetree/bindings/media/brcm,bcm2835-unicam.yaml
41 BROADCOM BCM47XX MIPS ARCHITECTURE
42 M: Hauke Mehrtens <hauke@hauke-m.de>
44 +++ b/drivers/media/platform/bcm2835/Kconfig
46 +# Broadcom VideoCore4 V4L2 camera support
48 +config VIDEO_BCM2835_UNICAM
49 + tristate "Broadcom BCM2835 Unicam video capture driver"
50 + depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && MEDIA_CONTROLLER
51 + depends on ARCH_BCM2835 || COMPILE_TEST
52 + select VIDEOBUF2_DMA_CONTIG
55 + Say Y here to enable support for the BCM2835 CSI-2 receiver. This is a
56 + V4L2 driver that controls the CSI-2 receiver directly, independently
57 + from the VC4 firmware.
59 + To compile this driver as a module, choose M here. The module will be
60 + called bcm2835-unicam.
62 +++ b/drivers/media/platform/bcm2835/Makefile
64 +# Makefile for BCM2835 Unicam driver
66 +obj-$(CONFIG_VIDEO_BCM2835_UNICAM) += bcm2835-unicam.o
68 +++ b/drivers/media/platform/bcm2835/bcm2835-unicam.c
70 +// SPDX-License-Identifier: GPL-2.0-only
72 + * BCM2835 Unicam Capture Driver
74 + * Copyright (C) 2017-2020 - Raspberry Pi (Trading) Ltd.
76 + * Dave Stevenson <dave.stevenson@raspberrypi.com>
78 + * Based on TI am437x driver by
79 + * Benoit Parrot <bparrot@ti.com>
80 + * Lad, Prabhakar <prabhakar.csengg@gmail.com>
82 + * and TI CAL camera interface driver by
83 + * Benoit Parrot <bparrot@ti.com>
86 + * There are two camera drivers in the kernel for BCM283x - this one
87 + * and bcm2835-camera (currently in staging).
89 + * This driver directly controls the Unicam peripheral - there is no
90 + * involvement with the VideoCore firmware. Unicam receives CSI-2 or
91 + * CCP2 data and writes it into SDRAM.
92 + * The only potential processing options are to repack Bayer data into an
93 + * alternate format, and applying windowing.
94 + * The repacking does not shift the data, so can repack V4L2_PIX_FMT_Sxxxx10P
95 + * to V4L2_PIX_FMT_Sxxxx10, or V4L2_PIX_FMT_Sxxxx12P to V4L2_PIX_FMT_Sxxxx12,
96 + * but not generically up to V4L2_PIX_FMT_Sxxxx16. The driver will add both
97 + * formats where the relevant formats are defined, and will automatically
98 + * configure the repacking as required.
99 + * Support for windowing may be added later.
101 + * It should be possible to connect this driver to any sensor with a
102 + * suitable output interface and V4L2 subdevice driver.
104 + * bcm2835-camera uses the VideoCore firmware to control the sensor,
105 + * Unicam, ISP, and all tuner control loops. Fully processed frames are
106 + * delivered to the driver by the firmware. It only has sensor drivers
107 + * for Omnivision OV5647, and Sony IMX219 sensors.
109 + * The two drivers are mutually exclusive for the same Unicam instance.
110 + * The VideoCore firmware checks the device tree configuration during boot.
111 + * If it finds device tree nodes called csi0 or csi1 it will block the
112 + * firmware from accessing the peripheral, and bcm2835-camera will
113 + * not be able to stream data.
116 +#include <linux/clk.h>
117 +#include <linux/delay.h>
118 +#include <linux/device.h>
119 +#include <linux/dma-mapping.h>
120 +#include <linux/err.h>
121 +#include <linux/init.h>
122 +#include <linux/interrupt.h>
123 +#include <linux/io.h>
124 +#include <linux/module.h>
125 +#include <linux/of_device.h>
126 +#include <linux/of_graph.h>
127 +#include <linux/pinctrl/consumer.h>
128 +#include <linux/platform_device.h>
129 +#include <linux/pm_runtime.h>
130 +#include <linux/slab.h>
131 +#include <linux/uaccess.h>
132 +#include <linux/videodev2.h>
134 +#include <media/v4l2-common.h>
135 +#include <media/v4l2-ctrls.h>
136 +#include <media/v4l2-dev.h>
137 +#include <media/v4l2-device.h>
138 +#include <media/v4l2-dv-timings.h>
139 +#include <media/v4l2-event.h>
140 +#include <media/v4l2-ioctl.h>
141 +#include <media/v4l2-fwnode.h>
142 +#include <media/videobuf2-dma-contig.h>
144 +#include "vc4-regs-unicam.h"
146 +#define UNICAM_MODULE_NAME "unicam"
147 +#define UNICAM_VERSION "0.1.0"
150 +module_param(debug, int, 0644);
151 +MODULE_PARM_DESC(debug, "Debug level 0-3");
153 +#define unicam_dbg(level, dev, fmt, arg...) \
154 + v4l2_dbg(level, debug, &(dev)->v4l2_dev, fmt, ##arg)
155 +#define unicam_info(dev, fmt, arg...) \
156 + v4l2_info(&(dev)->v4l2_dev, fmt, ##arg)
157 +#define unicam_err(dev, fmt, arg...) \
158 + v4l2_err(&(dev)->v4l2_dev, fmt, ##arg)
161 + * To protect against a dodgy sensor driver never returning an error from
162 + * enum_mbus_code, set a maximum index value to be used.
164 +#define MAX_ENUM_MBUS_CODE 128
167 + * Stride is a 16 bit register, but also has to be a multiple of 32.
169 +#define BPL_ALIGNMENT 32
170 +#define MAX_BYTESPERLINE ((1 << 16) - BPL_ALIGNMENT)
172 + * Max width is therefore determined by the max stride divided by
173 + * the number of bits per pixel. Take 32bpp as a
175 + * No imposed limit on the height, so adopt a square image for want
176 + * of anything better.
178 +#define MAX_WIDTH (MAX_BYTESPERLINE / 4)
179 +#define MAX_HEIGHT MAX_WIDTH
180 +/* Define a nominal minimum image size */
181 +#define MIN_WIDTH 16
182 +#define MIN_HEIGHT 16
183 +/* Default size of the embedded buffer */
184 +#define UNICAM_EMBEDDED_SIZE 8192
187 + * Size of the dummy buffer. Can be any size really, but the DMA
188 + * allocation works in units of page sizes.
190 +#define DUMMY_BUF_SIZE (PAGE_SIZE)
199 + * struct unicam_fmt - Unicam media bus format information
200 + * @pixelformat: V4L2 pixel format FCC identifier. 0 if n/a.
201 + * @repacked_fourcc: V4L2 pixel format FCC identifier if the data is expanded
202 + * out to 16bpp. 0 if n/a.
203 + * @code: V4L2 media bus format code.
204 + * @depth: Bits per pixel as delivered from the source.
205 + * @csi_dt: CSI data type.
206 + * @check_variants: Flag to denote that there are multiple mediabus formats
207 + * still in the list that could match this V4L2 format.
211 + u32 repacked_fourcc;
218 +static const struct unicam_fmt formats[] = {
221 + .fourcc = V4L2_PIX_FMT_YUYV,
222 + .code = MEDIA_BUS_FMT_YUYV8_2X8,
225 + .check_variants = 1,
227 + .fourcc = V4L2_PIX_FMT_UYVY,
228 + .code = MEDIA_BUS_FMT_UYVY8_2X8,
231 + .check_variants = 1,
233 + .fourcc = V4L2_PIX_FMT_YVYU,
234 + .code = MEDIA_BUS_FMT_YVYU8_2X8,
237 + .check_variants = 1,
239 + .fourcc = V4L2_PIX_FMT_VYUY,
240 + .code = MEDIA_BUS_FMT_VYUY8_2X8,
243 + .check_variants = 1,
245 + .fourcc = V4L2_PIX_FMT_YUYV,
246 + .code = MEDIA_BUS_FMT_YUYV8_1X16,
250 + .fourcc = V4L2_PIX_FMT_UYVY,
251 + .code = MEDIA_BUS_FMT_UYVY8_1X16,
255 + .fourcc = V4L2_PIX_FMT_YVYU,
256 + .code = MEDIA_BUS_FMT_YVYU8_1X16,
260 + .fourcc = V4L2_PIX_FMT_VYUY,
261 + .code = MEDIA_BUS_FMT_VYUY8_1X16,
266 + .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
267 + .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
271 + .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
272 + .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
276 + .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
277 + .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
281 + .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
282 + .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
286 + .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
287 + .code = MEDIA_BUS_FMT_RGB888_1X24,
291 + .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
292 + .code = MEDIA_BUS_FMT_BGR888_1X24,
296 + .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
297 + .code = MEDIA_BUS_FMT_ARGB8888_1X32,
301 + /* Bayer Formats */
302 + .fourcc = V4L2_PIX_FMT_SBGGR8,
303 + .code = MEDIA_BUS_FMT_SBGGR8_1X8,
307 + .fourcc = V4L2_PIX_FMT_SGBRG8,
308 + .code = MEDIA_BUS_FMT_SGBRG8_1X8,
312 + .fourcc = V4L2_PIX_FMT_SGRBG8,
313 + .code = MEDIA_BUS_FMT_SGRBG8_1X8,
317 + .fourcc = V4L2_PIX_FMT_SRGGB8,
318 + .code = MEDIA_BUS_FMT_SRGGB8_1X8,
322 + .fourcc = V4L2_PIX_FMT_SBGGR10P,
323 + .repacked_fourcc = V4L2_PIX_FMT_SBGGR10,
324 + .code = MEDIA_BUS_FMT_SBGGR10_1X10,
328 + .fourcc = V4L2_PIX_FMT_SGBRG10P,
329 + .repacked_fourcc = V4L2_PIX_FMT_SGBRG10,
330 + .code = MEDIA_BUS_FMT_SGBRG10_1X10,
334 + .fourcc = V4L2_PIX_FMT_SGRBG10P,
335 + .repacked_fourcc = V4L2_PIX_FMT_SGRBG10,
336 + .code = MEDIA_BUS_FMT_SGRBG10_1X10,
340 + .fourcc = V4L2_PIX_FMT_SRGGB10P,
341 + .repacked_fourcc = V4L2_PIX_FMT_SRGGB10,
342 + .code = MEDIA_BUS_FMT_SRGGB10_1X10,
346 + .fourcc = V4L2_PIX_FMT_SBGGR12P,
347 + .repacked_fourcc = V4L2_PIX_FMT_SBGGR12,
348 + .code = MEDIA_BUS_FMT_SBGGR12_1X12,
352 + .fourcc = V4L2_PIX_FMT_SGBRG12P,
353 + .repacked_fourcc = V4L2_PIX_FMT_SGBRG12,
354 + .code = MEDIA_BUS_FMT_SGBRG12_1X12,
358 + .fourcc = V4L2_PIX_FMT_SGRBG12P,
359 + .repacked_fourcc = V4L2_PIX_FMT_SGRBG12,
360 + .code = MEDIA_BUS_FMT_SGRBG12_1X12,
364 + .fourcc = V4L2_PIX_FMT_SRGGB12P,
365 + .repacked_fourcc = V4L2_PIX_FMT_SRGGB12,
366 + .code = MEDIA_BUS_FMT_SRGGB12_1X12,
370 + .fourcc = V4L2_PIX_FMT_SBGGR14P,
371 + .code = MEDIA_BUS_FMT_SBGGR14_1X14,
375 + .fourcc = V4L2_PIX_FMT_SGBRG14P,
376 + .code = MEDIA_BUS_FMT_SGBRG14_1X14,
380 + .fourcc = V4L2_PIX_FMT_SGRBG14P,
381 + .code = MEDIA_BUS_FMT_SGRBG14_1X14,
385 + .fourcc = V4L2_PIX_FMT_SRGGB14P,
386 + .code = MEDIA_BUS_FMT_SRGGB14_1X14,
391 + * 16 bit Bayer formats could be supported, but there is no CSI2
392 + * data_type defined for raw 16, and no sensors that produce it at
396 + /* Greyscale formats */
397 + .fourcc = V4L2_PIX_FMT_GREY,
398 + .code = MEDIA_BUS_FMT_Y8_1X8,
402 + .fourcc = V4L2_PIX_FMT_Y10P,
403 + .repacked_fourcc = V4L2_PIX_FMT_Y10,
404 + .code = MEDIA_BUS_FMT_Y10_1X10,
408 + /* NB There is no packed V4L2 fourcc for this format. */
409 + .repacked_fourcc = V4L2_PIX_FMT_Y12,
410 + .code = MEDIA_BUS_FMT_Y12_1X12,
414 + /* Embedded data format */
416 + .fourcc = V4L2_META_FMT_SENSOR_DATA,
417 + .code = MEDIA_BUS_FMT_SENSOR_DATA,
422 +struct unicam_buffer {
423 + struct vb2_v4l2_buffer vb;
424 + struct list_head list;
427 +static inline struct unicam_buffer *to_unicam_buffer(struct vb2_buffer *vb)
429 + return container_of(vb, struct unicam_buffer, vb.vb2_buf);
432 +struct unicam_node {
436 + unsigned int pad_id;
437 + /* Pointer pointing to current v4l2_buffer */
438 + struct unicam_buffer *cur_frm;
439 + /* Pointer pointing to next v4l2_buffer */
440 + struct unicam_buffer *next_frm;
441 + /* video capture */
442 + const struct unicam_fmt *fmt;
443 + /* Used to store current pixel format */
444 + struct v4l2_format v_fmt;
445 + /* Used to store current mbus frame format */
446 + struct v4l2_mbus_framefmt m_fmt;
447 + /* Buffer queue used in video-buf */
448 + struct vb2_queue buffer_queue;
449 + /* Queue of filled frames */
450 + struct list_head dma_queue;
451 + /* IRQ lock for DMA queue */
452 + spinlock_t dma_queue_lock;
453 + /* lock used to access this structure */
455 + /* Identifies video device for this channel */
456 + struct video_device video_dev;
457 + /* Pointer to the parent handle */
458 + struct unicam_device *dev;
459 + struct media_pad pad;
460 + unsigned int embedded_lines;
462 + * Dummy buffer intended to be used by unicam
463 + * if we have no other queued buffers to swap to.
465 + void *dummy_buf_cpu_addr;
466 + dma_addr_t dummy_buf_dma_addr;
469 +struct unicam_device {
472 + /* V4l2 specific parameters */
473 + struct v4l2_async_subdev asd;
475 + /* peripheral base address */
476 + void __iomem *base;
477 + /* clock gating base address */
478 + void __iomem *clk_gate_base;
482 + struct v4l2_device v4l2_dev;
483 + struct media_device mdev;
485 + /* parent device */
486 + struct platform_device *pdev;
487 + /* subdevice async Notifier */
488 + struct v4l2_async_notifier notifier;
489 + unsigned int sequence;
491 + /* ptr to sub device */
492 + struct v4l2_subdev *sensor;
493 + /* Pad config for the sensor */
494 + struct v4l2_subdev_pad_config *sensor_config;
496 + enum v4l2_mbus_type bus_type;
498 + * Stores bus.mipi_csi2.flags for CSI2 sensors, or
499 + * bus.mipi_csi1.strobe for CCP2.
501 + unsigned int bus_flags;
502 + unsigned int max_data_lanes;
503 + unsigned int active_data_lanes;
504 + bool sensor_embedded_data;
506 + struct unicam_node node[MAX_NODES];
507 + struct v4l2_ctrl_handler ctrl_handler;
510 +static inline struct unicam_device *
511 +to_unicam_device(struct v4l2_device *v4l2_dev)
513 + return container_of(v4l2_dev, struct unicam_device, v4l2_dev);
516 +/* Hardware access */
517 +static inline void clk_write(struct unicam_device *dev, u32 val)
519 + writel(val | 0x5a000000, dev->clk_gate_base);
522 +static inline u32 reg_read(struct unicam_device *dev, u32 offset)
524 + return readl(dev->base + offset);
527 +static inline void reg_write(struct unicam_device *dev, u32 offset, u32 val)
529 + writel(val, dev->base + offset);
532 +static inline int get_field(u32 value, u32 mask)
534 + return (value & mask) >> __ffs(mask);
537 +static inline void set_field(u32 *valp, u32 field, u32 mask)
542 + val |= (field << __ffs(mask)) & mask;
546 +static inline u32 reg_read_field(struct unicam_device *dev, u32 offset,
549 + return get_field(reg_read(dev, offset), mask);
552 +static inline void reg_write_field(struct unicam_device *dev, u32 offset,
553 + u32 field, u32 mask)
555 + u32 val = reg_read(dev, offset);
557 + set_field(&val, field, mask);
558 + reg_write(dev, offset, val);
561 +/* Power management functions */
562 +static inline int unicam_runtime_get(struct unicam_device *dev)
564 + return pm_runtime_get_sync(&dev->pdev->dev);
567 +static inline void unicam_runtime_put(struct unicam_device *dev)
569 + pm_runtime_put_sync(&dev->pdev->dev);
572 +/* Format setup functions */
573 +static const struct unicam_fmt *find_format_by_code(u32 code)
577 + for (i = 0; i < ARRAY_SIZE(formats); i++) {
578 + if (formats[i].code == code)
579 + return &formats[i];
585 +static int check_mbus_format(struct unicam_device *dev,
586 + const struct unicam_fmt *format)
591 + for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
592 + struct v4l2_subdev_mbus_code_enum mbus_code = {
595 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
598 + ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
601 + if (!ret && mbus_code.code == format->code)
608 +static const struct unicam_fmt *find_format_by_pix(struct unicam_device *dev,
613 + for (i = 0; i < ARRAY_SIZE(formats); i++) {
614 + if (formats[i].fourcc == pixelformat ||
615 + formats[i].repacked_fourcc == pixelformat) {
616 + if (formats[i].check_variants &&
617 + !check_mbus_format(dev, &formats[i]))
619 + return &formats[i];
626 +static inline unsigned int bytes_per_line(u32 width,
627 + const struct unicam_fmt *fmt,
630 + if (v4l2_fourcc == fmt->repacked_fourcc)
631 + /* Repacking always goes to 16bpp */
632 + return ALIGN(width << 1, BPL_ALIGNMENT);
634 + return ALIGN((width * fmt->depth) >> 3, BPL_ALIGNMENT);
637 +static int __subdev_get_format(struct unicam_device *dev,
638 + struct v4l2_mbus_framefmt *fmt, int pad_id)
640 + struct v4l2_subdev_format sd_fmt = {
641 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
646 + ret = v4l2_subdev_call(dev->sensor, pad, get_fmt, dev->sensor_config,
651 + *fmt = sd_fmt.format;
653 + unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__,
654 + fmt->width, fmt->height, fmt->code);
659 +static int __subdev_set_format(struct unicam_device *dev,
660 + struct v4l2_mbus_framefmt *fmt, int pad_id)
662 + struct v4l2_subdev_format sd_fmt = {
663 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
668 + sd_fmt.format = *fmt;
670 + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
675 + *fmt = sd_fmt.format;
677 + if (pad_id == IMAGE_PAD)
678 + unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__, fmt->width,
679 + fmt->height, fmt->code);
681 + unicam_dbg(1, dev, "%s Embedded data code:%04x\n", __func__,
682 + sd_fmt.format.code);
687 +static int unicam_calc_format_size_bpl(struct unicam_device *dev,
688 + const struct unicam_fmt *fmt,
689 + struct v4l2_format *f)
691 + unsigned int min_bytesperline;
693 + v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, MAX_WIDTH, 2,
694 + &f->fmt.pix.height, MIN_HEIGHT, MAX_HEIGHT, 0,
697 + min_bytesperline = bytes_per_line(f->fmt.pix.width, fmt,
698 + f->fmt.pix.pixelformat);
700 + if (f->fmt.pix.bytesperline > min_bytesperline &&
701 + f->fmt.pix.bytesperline <= MAX_BYTESPERLINE)
702 + f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
705 + f->fmt.pix.bytesperline = min_bytesperline;
707 + f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
709 + unicam_dbg(3, dev, "%s: fourcc: %08X size: %dx%d bpl:%d img_size:%d\n",
711 + f->fmt.pix.pixelformat,
712 + f->fmt.pix.width, f->fmt.pix.height,
713 + f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
718 +static int unicam_reset_format(struct unicam_node *node)
720 + struct unicam_device *dev = node->dev;
721 + struct v4l2_mbus_framefmt mbus_fmt;
724 + if (dev->sensor_embedded_data || node->pad_id != METADATA_PAD) {
725 + ret = __subdev_get_format(dev, &mbus_fmt, node->pad_id);
727 + unicam_err(dev, "Failed to get_format - ret %d\n", ret);
731 + if (mbus_fmt.code != node->fmt->code) {
732 + unicam_err(dev, "code mismatch - fmt->code %08x, mbus_fmt.code %08x\n",
733 + node->fmt->code, mbus_fmt.code);
738 + if (node->pad_id == IMAGE_PAD) {
739 + v4l2_fill_pix_format(&node->v_fmt.fmt.pix, &mbus_fmt);
740 + node->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
741 + unicam_calc_format_size_bpl(dev, node->fmt, &node->v_fmt);
743 + node->v_fmt.type = V4L2_BUF_TYPE_META_CAPTURE;
744 + node->v_fmt.fmt.meta.dataformat = V4L2_META_FMT_SENSOR_DATA;
745 + if (dev->sensor_embedded_data) {
746 + node->v_fmt.fmt.meta.buffersize =
747 + mbus_fmt.width * mbus_fmt.height;
748 + node->embedded_lines = mbus_fmt.height;
750 + node->v_fmt.fmt.meta.buffersize = UNICAM_EMBEDDED_SIZE;
751 + node->embedded_lines = 1;
755 + node->m_fmt = mbus_fmt;
759 +static void unicam_wr_dma_addr(struct unicam_device *dev, dma_addr_t dmaaddr,
760 + unsigned int buffer_size, int pad_id)
762 + dma_addr_t endaddr = dmaaddr + buffer_size;
765 + * dmaaddr and endaddr should be a 32-bit address with the top two bits
766 + * set to 0x3 to signify uncached access through the Videocore memory
769 + WARN_ON((dmaaddr >> 30) != 0x3 || (endaddr >> 30) != 0x3);
771 + if (pad_id == IMAGE_PAD) {
772 + reg_write(dev, UNICAM_IBSA0, dmaaddr);
773 + reg_write(dev, UNICAM_IBEA0, endaddr);
775 + reg_write(dev, UNICAM_DBSA0, dmaaddr);
776 + reg_write(dev, UNICAM_DBEA0, endaddr);
780 +static inline unsigned int unicam_get_lines_done(struct unicam_device *dev)
782 + dma_addr_t start_addr, cur_addr;
783 + unsigned int stride = dev->node[IMAGE_PAD].v_fmt.fmt.pix.bytesperline;
784 + struct unicam_buffer *frm = dev->node[IMAGE_PAD].cur_frm;
789 + start_addr = vb2_dma_contig_plane_dma_addr(&frm->vb.vb2_buf, 0);
790 + cur_addr = reg_read(dev, UNICAM_IBWP);
791 + return (unsigned int)(cur_addr - start_addr) / stride;
794 +static inline void unicam_schedule_next_buffer(struct unicam_node *node)
796 + struct unicam_device *dev = node->dev;
797 + struct unicam_buffer *buf;
801 + buf = list_first_entry(&node->dma_queue, struct unicam_buffer, list);
802 + node->next_frm = buf;
803 + list_del(&buf->list);
805 + addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
806 + size = (node->pad_id == IMAGE_PAD) ?
807 + node->v_fmt.fmt.pix.sizeimage :
808 + node->v_fmt.fmt.meta.buffersize;
810 + unicam_wr_dma_addr(dev, addr, size, node->pad_id);
813 +static inline void unicam_schedule_dummy_buffer(struct unicam_node *node)
815 + struct unicam_device *dev = node->dev;
817 + unicam_dbg(3, dev, "Scheduling dummy buffer for node %d\n",
820 + unicam_wr_dma_addr(dev, node->dummy_buf_dma_addr, DUMMY_BUF_SIZE,
822 + node->next_frm = NULL;
825 +static inline void unicam_process_buffer_complete(struct unicam_node *node,
826 + unsigned int sequence)
828 + node->cur_frm->vb.field = node->m_fmt.field;
829 + node->cur_frm->vb.sequence = sequence;
831 + vb2_buffer_done(&node->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
834 +static bool unicam_all_nodes_streaming(struct unicam_device *dev)
838 + ret = dev->node[IMAGE_PAD].open && dev->node[IMAGE_PAD].streaming;
839 + ret &= !dev->node[METADATA_PAD].open ||
840 + dev->node[METADATA_PAD].streaming;
844 +static bool unicam_all_nodes_disabled(struct unicam_device *dev)
846 + return !dev->node[IMAGE_PAD].streaming &&
847 + !dev->node[METADATA_PAD].streaming;
850 +static void unicam_queue_event_sof(struct unicam_device *unicam)
852 + struct v4l2_event event = {
853 + .type = V4L2_EVENT_FRAME_SYNC,
854 + .u.frame_sync.frame_sequence = unicam->sequence,
857 + v4l2_event_queue(&unicam->node[IMAGE_PAD].video_dev, &event);
861 + * unicam_isr : ISR handler for unicam capture
863 + * @dev_id: dev_id ptr
865 + * It changes status of the captured buffer, takes next buffer from the queue
866 + * and sets its address in unicam registers
868 +static irqreturn_t unicam_isr(int irq, void *dev)
870 + struct unicam_device *unicam = dev;
871 + unsigned int lines_done = unicam_get_lines_done(dev);
872 + unsigned int sequence = unicam->sequence;
878 + * Don't service interrupts if not streaming.
879 + * Avoids issues if the VPU should enable the
880 + * peripheral without the kernel knowing (that
881 + * shouldn't happen, but causes issues if it does).
883 + if (unicam_all_nodes_disabled(unicam))
886 + sta = reg_read(unicam, UNICAM_STA);
887 + /* Write value back to clear the interrupts */
888 + reg_write(unicam, UNICAM_STA, sta);
890 + ista = reg_read(unicam, UNICAM_ISTA);
891 + /* Write value back to clear the interrupts */
892 + reg_write(unicam, UNICAM_ISTA, ista);
894 + unicam_dbg(3, unicam, "ISR: ISTA: 0x%X, STA: 0x%X, sequence %d, lines done %d",
895 + ista, sta, sequence, lines_done);
897 + if (!(sta & (UNICAM_IS | UNICAM_PI0)))
898 + return IRQ_HANDLED;
901 + * We must run the frame end handler first. If we have a valid next_frm
902 + * and we get a simultaneout FE + FS interrupt, running the FS handler
903 + * first would null out the next_frm ptr and we would have lost the
906 + if (ista & UNICAM_FEI || sta & UNICAM_PI0) {
908 + * Ensure we have swapped buffers already as we can't
909 + * stop the peripheral. If no buffer is available, use a
910 + * dummy buffer to dump out frames until we get a new buffer
913 + for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
914 + if (!unicam->node[i].streaming)
917 + if (unicam->node[i].cur_frm)
918 + unicam_process_buffer_complete(&unicam->node[i],
920 + unicam->node[i].cur_frm = unicam->node[i].next_frm;
922 + unicam->sequence++;
925 + if (ista & UNICAM_FSI) {
927 + * Timestamp is to be when the first data byte was captured,
930 + ts = ktime_get_ns();
931 + for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
932 + if (!unicam->node[i].streaming)
935 + if (unicam->node[i].cur_frm)
936 + unicam->node[i].cur_frm->vb.vb2_buf.timestamp =
939 + * Set the next frame output to go to a dummy frame
940 + * if we have not managed to obtain another frame
943 + unicam_schedule_dummy_buffer(&unicam->node[i]);
946 + unicam_queue_event_sof(unicam);
950 + * Cannot swap buffer at frame end, there may be a race condition
951 + * where the HW does not actually swap it if the new frame has
954 + if (ista & (UNICAM_FSI | UNICAM_LCI) && !(ista & UNICAM_FEI)) {
955 + for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
956 + if (!unicam->node[i].streaming)
959 + spin_lock(&unicam->node[i].dma_queue_lock);
960 + if (!list_empty(&unicam->node[i].dma_queue) &&
961 + !unicam->node[i].next_frm)
962 + unicam_schedule_next_buffer(&unicam->node[i]);
963 + spin_unlock(&unicam->node[i].dma_queue_lock);
967 + if (reg_read(unicam, UNICAM_ICTL) & UNICAM_FCM) {
968 + /* Switch out of trigger mode if selected */
969 + reg_write_field(unicam, UNICAM_ICTL, 1, UNICAM_TFC);
970 + reg_write_field(unicam, UNICAM_ICTL, 0, UNICAM_FCM);
972 + return IRQ_HANDLED;
975 +static int unicam_querycap(struct file *file, void *priv,
976 + struct v4l2_capability *cap)
978 + struct unicam_node *node = video_drvdata(file);
979 + struct unicam_device *dev = node->dev;
981 + strlcpy(cap->driver, UNICAM_MODULE_NAME, sizeof(cap->driver));
982 + strlcpy(cap->card, UNICAM_MODULE_NAME, sizeof(cap->card));
984 + snprintf(cap->bus_info, sizeof(cap->bus_info),
985 + "platform:%s", dev_name(&dev->pdev->dev));
987 + cap->capabilities |= V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_META_CAPTURE;
992 +static int unicam_enum_fmt_vid_cap(struct file *file, void *priv,
993 + struct v4l2_fmtdesc *f)
995 + struct unicam_node *node = video_drvdata(file);
996 + struct unicam_device *dev = node->dev;
997 + unsigned int index = 0;
1001 + if (node->pad_id != IMAGE_PAD)
1004 + for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
1005 + struct v4l2_subdev_mbus_code_enum mbus_code = {
1008 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1010 + const struct unicam_fmt *fmt;
1012 + ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
1013 + NULL, &mbus_code);
1015 + unicam_dbg(2, dev,
1016 + "subdev->enum_mbus_code idx %d returned %d - index invalid\n",
1021 + fmt = find_format_by_code(mbus_code.code);
1023 + if (fmt->fourcc) {
1024 + if (index == f->index) {
1025 + f->pixelformat = fmt->fourcc;
1030 + if (fmt->repacked_fourcc) {
1031 + if (index == f->index) {
1032 + f->pixelformat = fmt->repacked_fourcc;
1043 +static int unicam_g_fmt_vid_cap(struct file *file, void *priv,
1044 + struct v4l2_format *f)
1046 + struct v4l2_mbus_framefmt mbus_fmt = {0};
1047 + struct unicam_node *node = video_drvdata(file);
1048 + struct unicam_device *dev = node->dev;
1049 + const struct unicam_fmt *fmt = NULL;
1052 + if (node->pad_id != IMAGE_PAD)
1056 + * If a flip has occurred in the sensor, the fmt code might have
1057 + * changed. So we will need to re-fetch the format from the subdevice.
1059 + ret = __subdev_get_format(dev, &mbus_fmt, node->pad_id);
1063 + /* Find the V4L2 format from mbus code. We must match a known format. */
1064 + fmt = find_format_by_code(mbus_fmt.code);
1069 + node->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
1076 +const struct unicam_fmt *get_first_supported_format(struct unicam_device *dev)
1078 + struct v4l2_subdev_mbus_code_enum mbus_code;
1079 + const struct unicam_fmt *fmt = NULL;
1083 + for (i = 0; ret != -EINVAL && ret != -ENOIOCTLCMD; ++i) {
1084 + memset(&mbus_code, 0, sizeof(mbus_code));
1085 + mbus_code.index = i;
1086 + mbus_code.pad = IMAGE_PAD;
1087 + mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1089 + ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
1092 + unicam_dbg(2, dev,
1093 + "subdev->enum_mbus_code idx %u returned %d - continue\n",
1098 + unicam_dbg(2, dev, "subdev %s: code: 0x%08x idx: %u\n",
1099 + dev->sensor->name, mbus_code.code, i);
1101 + fmt = find_format_by_code(mbus_code.code);
1102 + unicam_dbg(2, dev, "fmt 0x%08x returned as %p, V4L2 FOURCC 0x%08x, csi_dt 0x%02x\n",
1103 + mbus_code.code, fmt, fmt ? fmt->fourcc : 0,
1104 + fmt ? fmt->csi_dt : 0);
1112 +static int unicam_try_fmt_vid_cap(struct file *file, void *priv,
1113 + struct v4l2_format *f)
1115 + struct unicam_node *node = video_drvdata(file);
1116 + struct unicam_device *dev = node->dev;
1117 + struct v4l2_subdev_format sd_fmt = {
1118 + .which = V4L2_SUBDEV_FORMAT_TRY,
1121 + struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
1122 + const struct unicam_fmt *fmt;
1125 + if (node->pad_id != IMAGE_PAD)
1128 + fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
1131 + * Pixel format not supported by unicam. Choose the first
1132 + * supported format, and let the sensor choose something else.
1134 + unicam_dbg(3, dev, "Fourcc format (0x%08x) not found. Use first format.\n",
1135 + f->fmt.pix.pixelformat);
1137 + fmt = &formats[0];
1138 + f->fmt.pix.pixelformat = fmt->fourcc;
1141 + v4l2_fill_mbus_format(mbus_fmt, &f->fmt.pix, fmt->code);
1143 + * No support for receiving interlaced video, so never
1144 + * request it from the sensor subdev.
1146 + mbus_fmt->field = V4L2_FIELD_NONE;
1148 + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
1150 + if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1153 + if (mbus_fmt->field != V4L2_FIELD_NONE)
1154 + unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
1156 + v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
1157 + if (mbus_fmt->code != fmt->code) {
1158 + /* Sensor has returned an alternate format */
1159 + fmt = find_format_by_code(mbus_fmt->code);
1162 + * The alternate format is one unicam can't support.
1163 + * Find the first format that is supported by both, and
1166 + fmt = get_first_supported_format(dev);
1167 + mbus_fmt->code = fmt->code;
1169 + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt,
1170 + dev->sensor_config, &sd_fmt);
1171 + if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1174 + if (mbus_fmt->field != V4L2_FIELD_NONE)
1175 + unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
1177 + v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
1179 + if (mbus_fmt->code != fmt->code) {
1181 + * We've set a format that the sensor reports
1182 + * as being supported, but it refuses to set it.
1183 + * Not much else we can do.
1184 + * Assume that the sensor driver may accept the
1185 + * format when it is set (rather than tried).
1187 + unicam_err(dev, "Sensor won't accept default format, and Unicam can't support sensor default\n");
1192 + f->fmt.pix.pixelformat = fmt->fourcc;
1194 + f->fmt.pix.pixelformat = fmt->repacked_fourcc;
1197 + return unicam_calc_format_size_bpl(dev, fmt, f);
1200 +static int unicam_s_fmt_vid_cap(struct file *file, void *priv,
1201 + struct v4l2_format *f)
1203 + struct unicam_node *node = video_drvdata(file);
1204 + struct unicam_device *dev = node->dev;
1205 + struct vb2_queue *q = &node->buffer_queue;
1206 + struct v4l2_mbus_framefmt mbus_fmt = {0};
1207 + const struct unicam_fmt *fmt;
1210 + if (vb2_is_busy(q))
1213 + ret = unicam_try_fmt_vid_cap(file, priv, f);
1217 + fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
1220 + * Unknown pixel format - adopt a default.
1221 + * This shouldn't happen as try_fmt should have resolved any
1224 + fmt = get_first_supported_format(dev);
1227 + * It shouldn't be possible to get here with no
1228 + * supported formats
1231 + f->fmt.pix.pixelformat = fmt->fourcc;
1235 + v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, fmt->code);
1237 + ret = __subdev_set_format(dev, &mbus_fmt, node->pad_id);
1239 + unicam_dbg(3, dev, "%s __subdev_set_format failed %d\n",
1244 + /* Just double check nothing has gone wrong */
1245 + if (mbus_fmt.code != fmt->code) {
1246 + unicam_dbg(3, dev,
1247 + "%s subdev changed format on us, this should not happen\n",
1253 + node->v_fmt.fmt.pix.pixelformat = f->fmt.pix.pixelformat;
1254 + node->v_fmt.fmt.pix.bytesperline = f->fmt.pix.bytesperline;
1255 + unicam_reset_format(node);
1257 + unicam_dbg(3, dev,
1258 + "%s %dx%d, mbus_fmt 0x%08X, V4L2 pix 0x%08X.\n",
1259 + __func__, node->v_fmt.fmt.pix.width,
1260 + node->v_fmt.fmt.pix.height, mbus_fmt.code,
1261 + node->v_fmt.fmt.pix.pixelformat);
1268 +static int unicam_enum_fmt_meta_cap(struct file *file, void *priv,
1269 + struct v4l2_fmtdesc *f)
1271 + struct unicam_node *node = video_drvdata(file);
1272 + struct unicam_device *dev = node->dev;
1273 + const struct unicam_fmt *fmt;
1277 + if (node->pad_id != METADATA_PAD || f->index != 0)
1280 + if (dev->sensor_embedded_data) {
1281 + struct v4l2_subdev_mbus_code_enum mbus_code = {
1282 + .index = f->index,
1283 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1284 + .pad = METADATA_PAD,
1287 + ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
1290 + unicam_dbg(2, dev,
1291 + "subdev->enum_mbus_code idx 0 returned %d - index invalid\n",
1296 + code = mbus_code.code;
1298 + code = MEDIA_BUS_FMT_SENSOR_DATA;
1301 + fmt = find_format_by_code(code);
1303 + f->pixelformat = fmt->fourcc;
1308 +static int unicam_g_fmt_meta_cap(struct file *file, void *priv,
1309 + struct v4l2_format *f)
1311 + struct unicam_node *node = video_drvdata(file);
1313 + if (node->pad_id != METADATA_PAD)
1321 +static int unicam_queue_setup(struct vb2_queue *vq,
1322 + unsigned int *nbuffers,
1323 + unsigned int *nplanes,
1324 + unsigned int sizes[],
1325 + struct device *alloc_devs[])
1327 + struct unicam_node *node = vb2_get_drv_priv(vq);
1328 + struct unicam_device *dev = node->dev;
1329 + unsigned int size = node->pad_id == IMAGE_PAD ?
1330 + node->v_fmt.fmt.pix.sizeimage :
1331 + node->v_fmt.fmt.meta.buffersize;
1333 + if (vq->num_buffers + *nbuffers < 3)
1334 + *nbuffers = 3 - vq->num_buffers;
1337 + if (sizes[0] < size) {
1338 + unicam_err(dev, "sizes[0] %i < size %u\n", sizes[0],
1351 +static int unicam_buffer_prepare(struct vb2_buffer *vb)
1353 + struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
1354 + struct unicam_device *dev = node->dev;
1355 + struct unicam_buffer *buf = to_unicam_buffer(vb);
1356 + unsigned long size;
1358 + if (WARN_ON(!node->fmt))
1361 + size = node->pad_id == IMAGE_PAD ? node->v_fmt.fmt.pix.sizeimage :
1362 + node->v_fmt.fmt.meta.buffersize;
1363 + if (vb2_plane_size(vb, 0) < size) {
1364 + unicam_err(dev, "data will not fit into plane (%lu < %lu)\n",
1365 + vb2_plane_size(vb, 0), size);
1369 + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
1373 +static void unicam_buffer_queue(struct vb2_buffer *vb)
1375 + struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
1376 + struct unicam_buffer *buf = to_unicam_buffer(vb);
1377 + unsigned long flags;
1379 + spin_lock_irqsave(&node->dma_queue_lock, flags);
1380 + list_add_tail(&buf->list, &node->dma_queue);
1381 + spin_unlock_irqrestore(&node->dma_queue_lock, flags);
1384 +static void unicam_set_packing_config(struct unicam_device *dev)
1389 + if (dev->node[IMAGE_PAD].v_fmt.fmt.pix.pixelformat ==
1390 + dev->node[IMAGE_PAD].fmt->fourcc) {
1391 + unpack = UNICAM_PUM_NONE;
1392 + pack = UNICAM_PPM_NONE;
1394 + switch (dev->node[IMAGE_PAD].fmt->depth) {
1396 + unpack = UNICAM_PUM_UNPACK8;
1399 + unpack = UNICAM_PUM_UNPACK10;
1402 + unpack = UNICAM_PUM_UNPACK12;
1405 + unpack = UNICAM_PUM_UNPACK14;
1408 + unpack = UNICAM_PUM_UNPACK16;
1411 + unpack = UNICAM_PUM_NONE;
1415 + /* Repacking is always to 16bpp */
1416 + pack = UNICAM_PPM_PACK16;
1420 + set_field(&val, unpack, UNICAM_PUM_MASK);
1421 + set_field(&val, pack, UNICAM_PPM_MASK);
1422 + reg_write(dev, UNICAM_IPIPE, val);
1425 +static void unicam_cfg_image_id(struct unicam_device *dev)
1427 + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1428 + /* CSI2 mode, hardcode VC 0 for now. */
1429 + reg_write(dev, UNICAM_IDI0,
1430 + (0 << 6) | dev->node[IMAGE_PAD].fmt->csi_dt);
1433 + reg_write(dev, UNICAM_IDI0,
1434 + 0x80 | dev->node[IMAGE_PAD].fmt->csi_dt);
1438 +static void unicam_enable_ed(struct unicam_device *dev)
1440 + u32 val = reg_read(dev, UNICAM_DCS);
1442 + set_field(&val, 2, UNICAM_EDL_MASK);
1443 + /* Do not wrap at the end of the embedded data buffer */
1444 + set_field(&val, 0, UNICAM_DBOB);
1446 + reg_write(dev, UNICAM_DCS, val);
1449 +static void unicam_start_rx(struct unicam_device *dev, dma_addr_t *addr)
1451 + int line_int_freq = dev->node[IMAGE_PAD].v_fmt.fmt.pix.height >> 2;
1452 + unsigned int size, i;
1455 + if (line_int_freq < 128)
1456 + line_int_freq = 128;
1458 + /* Enable lane clocks */
1460 + for (i = 0; i < dev->active_data_lanes; i++)
1461 + val = val << 2 | 1;
1462 + clk_write(dev, val);
1465 + reg_write(dev, UNICAM_CTRL, UNICAM_MEM);
1467 + /* Enable analogue control, and leave in reset. */
1469 + set_field(&val, 7, UNICAM_CTATADJ_MASK);
1470 + set_field(&val, 7, UNICAM_PTATADJ_MASK);
1471 + reg_write(dev, UNICAM_ANA, val);
1472 + usleep_range(1000, 2000);
1474 + /* Come out of reset */
1475 + reg_write_field(dev, UNICAM_ANA, 0, UNICAM_AR);
1477 + /* Peripheral reset */
1478 + reg_write_field(dev, UNICAM_CTRL, 1, UNICAM_CPR);
1479 + reg_write_field(dev, UNICAM_CTRL, 0, UNICAM_CPR);
1481 + reg_write_field(dev, UNICAM_CTRL, 0, UNICAM_CPE);
1483 + /* Enable Rx control. */
1484 + val = reg_read(dev, UNICAM_CTRL);
1485 + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1486 + set_field(&val, UNICAM_CPM_CSI2, UNICAM_CPM_MASK);
1487 + set_field(&val, UNICAM_DCM_STROBE, UNICAM_DCM_MASK);
1489 + set_field(&val, UNICAM_CPM_CCP2, UNICAM_CPM_MASK);
1490 + set_field(&val, dev->bus_flags, UNICAM_DCM_MASK);
1492 + /* Packet framer timeout */
1493 + set_field(&val, 0xf, UNICAM_PFT_MASK);
1494 + set_field(&val, 128, UNICAM_OET_MASK);
1495 + reg_write(dev, UNICAM_CTRL, val);
1497 + reg_write(dev, UNICAM_IHWIN, 0);
1498 + reg_write(dev, UNICAM_IVWIN, 0);
1500 + /* AXI bus access QoS setup */
1501 + val = reg_read(dev, UNICAM_PRI);
1502 + set_field(&val, 0, UNICAM_BL_MASK);
1503 + set_field(&val, 0, UNICAM_BS_MASK);
1504 + set_field(&val, 0xe, UNICAM_PP_MASK);
1505 + set_field(&val, 8, UNICAM_NP_MASK);
1506 + set_field(&val, 2, UNICAM_PT_MASK);
1507 + set_field(&val, 1, UNICAM_PE);
1508 + reg_write(dev, UNICAM_PRI, val);
1510 + reg_write_field(dev, UNICAM_ANA, 0, UNICAM_DDL);
1512 + /* Always start in trigger frame capture mode (UNICAM_FCM set) */
1513 + val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_FCM | UNICAM_IBOB;
1514 + set_field(&val, line_int_freq, UNICAM_LCIE_MASK);
1515 + reg_write(dev, UNICAM_ICTL, val);
1516 + reg_write(dev, UNICAM_STA, UNICAM_STA_MASK_ALL);
1517 + reg_write(dev, UNICAM_ISTA, UNICAM_ISTA_MASK_ALL);
1519 + /* tclk_term_en */
1520 + reg_write_field(dev, UNICAM_CLT, 2, UNICAM_CLT1_MASK);
1522 + reg_write_field(dev, UNICAM_CLT, 6, UNICAM_CLT2_MASK);
1524 + reg_write_field(dev, UNICAM_DLT, 2, UNICAM_DLT1_MASK);
1526 + reg_write_field(dev, UNICAM_DLT, 6, UNICAM_DLT2_MASK);
1528 + reg_write_field(dev, UNICAM_DLT, 0, UNICAM_DLT3_MASK);
1530 + reg_write_field(dev, UNICAM_CTRL, 0, UNICAM_SOE);
1532 + /* Packet compare setup - required to avoid missing frame ends */
1534 + set_field(&val, 1, UNICAM_PCE);
1535 + set_field(&val, 1, UNICAM_GI);
1536 + set_field(&val, 1, UNICAM_CPH);
1537 + set_field(&val, 0, UNICAM_PCVC_MASK);
1538 + set_field(&val, 1, UNICAM_PCDT_MASK);
1539 + reg_write(dev, UNICAM_CMP0, val);
1541 + /* Enable clock lane and set up terminations */
1543 + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1545 + set_field(&val, 1, UNICAM_CLE);
1546 + set_field(&val, 1, UNICAM_CLLPE);
1547 + if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1548 + set_field(&val, 1, UNICAM_CLTRE);
1549 + set_field(&val, 1, UNICAM_CLHSE);
1553 + set_field(&val, 1, UNICAM_CLE);
1554 + set_field(&val, 1, UNICAM_CLHSE);
1555 + set_field(&val, 1, UNICAM_CLTRE);
1557 + reg_write(dev, UNICAM_CLK, val);
1560 + * Enable required data lanes with appropriate terminations.
1561 + * The same value needs to be written to UNICAM_DATn registers for
1562 + * the active lanes, and 0 for inactive ones.
1565 + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1567 + set_field(&val, 1, UNICAM_DLE);
1568 + set_field(&val, 1, UNICAM_DLLPE);
1569 + if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1570 + set_field(&val, 1, UNICAM_DLTRE);
1571 + set_field(&val, 1, UNICAM_DLHSE);
1575 + set_field(&val, 1, UNICAM_DLE);
1576 + set_field(&val, 1, UNICAM_DLHSE);
1577 + set_field(&val, 1, UNICAM_DLTRE);
1579 + reg_write(dev, UNICAM_DAT0, val);
1581 + if (dev->active_data_lanes == 1)
1583 + reg_write(dev, UNICAM_DAT1, val);
1585 + if (dev->max_data_lanes > 2) {
1587 + * Registers UNICAM_DAT2 and UNICAM_DAT3 only valid if the
1588 + * instance supports more than 2 data lanes.
1590 + if (dev->active_data_lanes == 2)
1592 + reg_write(dev, UNICAM_DAT2, val);
1594 + if (dev->active_data_lanes == 3)
1596 + reg_write(dev, UNICAM_DAT3, val);
1599 + reg_write(dev, UNICAM_IBLS,
1600 + dev->node[IMAGE_PAD].v_fmt.fmt.pix.bytesperline);
1601 + size = dev->node[IMAGE_PAD].v_fmt.fmt.pix.sizeimage;
1602 + unicam_wr_dma_addr(dev, addr[IMAGE_PAD], size, IMAGE_PAD);
1603 + unicam_set_packing_config(dev);
1604 + unicam_cfg_image_id(dev);
1606 + val = reg_read(dev, UNICAM_MISC);
1607 + set_field(&val, 1, UNICAM_FL0);
1608 + set_field(&val, 1, UNICAM_FL1);
1609 + reg_write(dev, UNICAM_MISC, val);
1611 + if (dev->node[METADATA_PAD].streaming && dev->sensor_embedded_data) {
1612 + size = dev->node[METADATA_PAD].v_fmt.fmt.meta.buffersize;
1613 + unicam_enable_ed(dev);
1614 + unicam_wr_dma_addr(dev, addr[METADATA_PAD], size, METADATA_PAD);
1617 + /* Enable peripheral */
1618 + reg_write_field(dev, UNICAM_CTRL, 1, UNICAM_CPE);
1620 + /* Load image pointers */
1621 + reg_write_field(dev, UNICAM_ICTL, 1, UNICAM_LIP_MASK);
1623 + /* Load embedded data buffer pointers if needed */
1624 + if (dev->node[METADATA_PAD].streaming && dev->sensor_embedded_data)
1625 + reg_write_field(dev, UNICAM_DCS, 1, UNICAM_LDP);
1628 + * Enable trigger only for the first frame to
1629 + * sync correctly to the FS from the source.
1631 + reg_write_field(dev, UNICAM_ICTL, 1, UNICAM_TFC);
1634 +static void unicam_disable(struct unicam_device *dev)
1636 + /* Analogue lane control disable */
1637 + reg_write_field(dev, UNICAM_ANA, 1, UNICAM_DDL);
1639 + /* Stop the output engine */
1640 + reg_write_field(dev, UNICAM_CTRL, 1, UNICAM_SOE);
1642 + /* Disable the data lanes. */
1643 + reg_write(dev, UNICAM_DAT0, 0);
1644 + reg_write(dev, UNICAM_DAT1, 0);
1646 + if (dev->max_data_lanes > 2) {
1647 + reg_write(dev, UNICAM_DAT2, 0);
1648 + reg_write(dev, UNICAM_DAT3, 0);
1651 + /* Peripheral reset */
1652 + reg_write_field(dev, UNICAM_CTRL, 1, UNICAM_CPR);
1653 + usleep_range(50, 100);
1654 + reg_write_field(dev, UNICAM_CTRL, 0, UNICAM_CPR);
1656 + /* Disable peripheral */
1657 + reg_write_field(dev, UNICAM_CTRL, 0, UNICAM_CPE);
1659 + /* Clear ED setup */
1660 + reg_write(dev, UNICAM_DCS, 0);
1662 + /* Disable all lane clocks */
1663 + clk_write(dev, 0);
1666 +static void unicam_return_buffers(struct unicam_node *node)
1668 + struct unicam_buffer *buf, *tmp;
1669 + unsigned long flags;
1671 + spin_lock_irqsave(&node->dma_queue_lock, flags);
1672 + list_for_each_entry_safe(buf, tmp, &node->dma_queue, list) {
1673 + list_del(&buf->list);
1674 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1677 + if (node->cur_frm)
1678 + vb2_buffer_done(&node->cur_frm->vb.vb2_buf,
1679 + VB2_BUF_STATE_ERROR);
1680 + if (node->next_frm && node->cur_frm != node->next_frm)
1681 + vb2_buffer_done(&node->next_frm->vb.vb2_buf,
1682 + VB2_BUF_STATE_ERROR);
1684 + node->cur_frm = NULL;
1685 + node->next_frm = NULL;
1686 + spin_unlock_irqrestore(&node->dma_queue_lock, flags);
1689 +static int unicam_start_streaming(struct vb2_queue *vq, unsigned int count)
1691 + struct unicam_node *node = vb2_get_drv_priv(vq);
1692 + struct unicam_device *dev = node->dev;
1693 + dma_addr_t buffer_addr[MAX_NODES] = { 0 };
1694 + unsigned long flags;
1698 + node->streaming = true;
1699 + if (!unicam_all_nodes_streaming(dev)) {
1700 + unicam_dbg(3, dev, "Not all nodes are streaming yet.");
1704 + dev->sequence = 0;
1705 + ret = unicam_runtime_get(dev);
1707 + unicam_dbg(3, dev, "unicam_runtime_get failed\n");
1708 + goto err_streaming;
1712 + * TODO: Retrieve the number of active data lanes from the connected
1715 + dev->active_data_lanes = dev->max_data_lanes;
1717 + ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
1719 + unicam_err(dev, "failed to set up clock\n");
1723 + ret = clk_prepare_enable(dev->clock);
1725 + unicam_err(dev, "Failed to enable CSI clock: %d\n", ret);
1729 + for (i = 0; i < ARRAY_SIZE(dev->node); i++) {
1730 + struct unicam_buffer *buf;
1732 + if (!dev->node[i].streaming)
1735 + spin_lock_irqsave(&dev->node[i].dma_queue_lock, flags);
1736 + buf = list_first_entry(&dev->node[i].dma_queue,
1737 + struct unicam_buffer, list);
1738 + dev->node[i].cur_frm = buf;
1739 + dev->node[i].next_frm = buf;
1740 + list_del(&buf->list);
1741 + spin_unlock_irqrestore(&dev->node[i].dma_queue_lock, flags);
1744 + vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
1747 + unicam_start_rx(dev, buffer_addr);
1749 + ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1751 + unicam_err(dev, "stream on failed in subdev\n");
1752 + goto err_disable_unicam;
1757 +err_disable_unicam:
1758 + unicam_disable(dev);
1759 + clk_disable_unprepare(dev->clock);
1761 + unicam_runtime_put(dev);
1763 + unicam_return_buffers(node);
1764 + node->streaming = false;
1769 +static void unicam_stop_streaming(struct vb2_queue *vq)
1771 + struct unicam_node *node = vb2_get_drv_priv(vq);
1772 + struct unicam_device *dev = node->dev;
1774 + node->streaming = false;
1776 + if (node->pad_id == IMAGE_PAD) {
1778 + * Stop streaming the sensor and disable the peripheral.
1779 + * We cannot continue streaming embedded data with the
1780 + * image pad disabled.
1782 + if (v4l2_subdev_call(dev->sensor, video, s_stream, 0) < 0)
1783 + unicam_err(dev, "stream off failed in subdev\n");
1785 + unicam_disable(dev);
1786 + clk_disable_unprepare(dev->clock);
1787 + unicam_runtime_put(dev);
1789 + } else if (node->pad_id == METADATA_PAD) {
1791 + * Allow the hardware to spin in the dummy buffer.
1792 + * This is only really needed if the embedded data pad is
1793 + * disabled before the image pad.
1795 + unicam_wr_dma_addr(dev, node->dummy_buf_dma_addr,
1796 + DUMMY_BUF_SIZE, METADATA_PAD);
1799 + /* Clear all queued buffers for the node */
1800 + unicam_return_buffers(node);
1803 +static int unicam_enum_input(struct file *file, void *priv,
1804 + struct v4l2_input *inp)
1806 + struct unicam_node *node = video_drvdata(file);
1807 + struct unicam_device *dev = node->dev;
1809 + if (inp->index != 0)
1812 + inp->type = V4L2_INPUT_TYPE_CAMERA;
1813 + if (v4l2_subdev_has_op(dev->sensor, video, s_dv_timings)) {
1814 + inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1816 + } else if (v4l2_subdev_has_op(dev->sensor, video, s_std)) {
1817 + inp->capabilities = V4L2_IN_CAP_STD;
1818 + if (v4l2_subdev_call(dev->sensor, video, g_tvnorms, &inp->std)
1820 + inp->std = V4L2_STD_ALL;
1822 + inp->capabilities = 0;
1825 + sprintf(inp->name, "Camera 0");
1829 +static int unicam_g_input(struct file *file, void *priv, unsigned int *i)
1836 +static int unicam_s_input(struct file *file, void *priv, unsigned int i)
1839 + * FIXME: Ideally we would like to be able to query the source
1840 + * subdevice for information over the input connectors it supports,
1841 + * and map that through in to a call to video_ops->s_routing.
1842 + * There is no infrastructure support for defining that within
1843 + * devicetree at present. Until that is implemented we can't
1844 + * map a user physical connector number to s_routing input number.
1852 +static int unicam_querystd(struct file *file, void *priv,
1855 + struct unicam_node *node = video_drvdata(file);
1856 + struct unicam_device *dev = node->dev;
1858 + return v4l2_subdev_call(dev->sensor, video, querystd, std);
1861 +static int unicam_g_std(struct file *file, void *priv, v4l2_std_id *std)
1863 + struct unicam_node *node = video_drvdata(file);
1864 + struct unicam_device *dev = node->dev;
1866 + return v4l2_subdev_call(dev->sensor, video, g_std, std);
1869 +static int unicam_s_std(struct file *file, void *priv, v4l2_std_id std)
1871 + struct unicam_node *node = video_drvdata(file);
1872 + struct unicam_device *dev = node->dev;
1874 + v4l2_std_id current_std;
1876 + ret = v4l2_subdev_call(dev->sensor, video, g_std, ¤t_std);
1880 + if (std == current_std)
1883 + if (vb2_is_busy(&node->buffer_queue))
1886 + ret = v4l2_subdev_call(dev->sensor, video, s_std, std);
1888 + /* Force recomputation of bytesperline */
1889 + node->v_fmt.fmt.pix.bytesperline = 0;
1891 + unicam_reset_format(node);
1896 +static int unicam_s_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1898 + struct unicam_node *node = video_drvdata(file);
1899 + struct unicam_device *dev = node->dev;
1901 + return v4l2_subdev_call(dev->sensor, pad, set_edid, edid);
1904 +static int unicam_g_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1906 + struct unicam_node *node = video_drvdata(file);
1907 + struct unicam_device *dev = node->dev;
1909 + return v4l2_subdev_call(dev->sensor, pad, get_edid, edid);
1912 +static int unicam_s_selection(struct file *file, void *priv,
1913 + struct v4l2_selection *sel)
1915 + struct unicam_node *node = video_drvdata(file);
1916 + struct unicam_device *dev = node->dev;
1917 + struct v4l2_subdev_selection sdsel = {
1918 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1919 + .target = sel->target,
1920 + .flags = sel->flags,
1924 + return v4l2_subdev_call(dev->sensor, pad, set_selection, NULL, &sdsel);
1927 +static int unicam_g_selection(struct file *file, void *priv,
1928 + struct v4l2_selection *sel)
1930 + struct unicam_node *node = video_drvdata(file);
1931 + struct unicam_device *dev = node->dev;
1932 + struct v4l2_subdev_selection sdsel = {
1933 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1934 + .target = sel->target,
1938 + ret = v4l2_subdev_call(dev->sensor, pad, get_selection, NULL, &sdsel);
1945 +static int unicam_enum_framesizes(struct file *file, void *priv,
1946 + struct v4l2_frmsizeenum *fsize)
1948 + struct unicam_node *node = video_drvdata(file);
1949 + struct unicam_device *dev = node->dev;
1950 + const struct unicam_fmt *fmt;
1951 + struct v4l2_subdev_frame_size_enum fse;
1954 + /* check for valid format */
1955 + fmt = find_format_by_pix(dev, fsize->pixel_format);
1957 + unicam_dbg(3, dev, "Invalid pixel code: %x\n",
1958 + fsize->pixel_format);
1961 + fse.code = fmt->code;
1963 + fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1964 + fse.index = fsize->index;
1965 + fse.pad = node->pad_id;
1967 + ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_size, NULL, &fse);
1971 + unicam_dbg(1, dev, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
1972 + __func__, fse.index, fse.code, fse.min_width, fse.max_width,
1973 + fse.min_height, fse.max_height);
1975 + fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1976 + fsize->discrete.width = fse.max_width;
1977 + fsize->discrete.height = fse.max_height;
1982 +static int unicam_enum_frameintervals(struct file *file, void *priv,
1983 + struct v4l2_frmivalenum *fival)
1985 + struct unicam_node *node = video_drvdata(file);
1986 + struct unicam_device *dev = node->dev;
1987 + const struct unicam_fmt *fmt;
1988 + struct v4l2_subdev_frame_interval_enum fie = {
1989 + .index = fival->index,
1990 + .width = fival->width,
1991 + .height = fival->height,
1992 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1996 + fmt = find_format_by_pix(dev, fival->pixel_format);
2000 + fie.code = fmt->code;
2001 + ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_interval,
2006 + fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
2007 + fival->discrete = fie.interval;
2012 +static int unicam_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
2014 + struct unicam_node *node = video_drvdata(file);
2015 + struct unicam_device *dev = node->dev;
2017 + return v4l2_g_parm_cap(video_devdata(file), dev->sensor, a);
2020 +static int unicam_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
2022 + struct unicam_node *node = video_drvdata(file);
2023 + struct unicam_device *dev = node->dev;
2025 + return v4l2_s_parm_cap(video_devdata(file), dev->sensor, a);
2028 +static int unicam_g_dv_timings(struct file *file, void *priv,
2029 + struct v4l2_dv_timings *timings)
2031 + struct unicam_node *node = video_drvdata(file);
2032 + struct unicam_device *dev = node->dev;
2034 + return v4l2_subdev_call(dev->sensor, video, g_dv_timings, timings);
2037 +static int unicam_s_dv_timings(struct file *file, void *priv,
2038 + struct v4l2_dv_timings *timings)
2040 + struct unicam_node *node = video_drvdata(file);
2041 + struct unicam_device *dev = node->dev;
2042 + struct v4l2_dv_timings current_timings;
2045 + ret = v4l2_subdev_call(dev->sensor, video, g_dv_timings,
2046 + ¤t_timings);
2048 + if (v4l2_match_dv_timings(timings, ¤t_timings, 0, false))
2051 + if (vb2_is_busy(&node->buffer_queue))
2054 + ret = v4l2_subdev_call(dev->sensor, video, s_dv_timings, timings);
2056 + /* Force recomputation of bytesperline */
2057 + node->v_fmt.fmt.pix.bytesperline = 0;
2059 + unicam_reset_format(node);
2064 +static int unicam_query_dv_timings(struct file *file, void *priv,
2065 + struct v4l2_dv_timings *timings)
2067 + struct unicam_node *node = video_drvdata(file);
2068 + struct unicam_device *dev = node->dev;
2070 + return v4l2_subdev_call(dev->sensor, video, query_dv_timings, timings);
2073 +static int unicam_enum_dv_timings(struct file *file, void *priv,
2074 + struct v4l2_enum_dv_timings *timings)
2076 + struct unicam_node *node = video_drvdata(file);
2077 + struct unicam_device *dev = node->dev;
2079 + return v4l2_subdev_call(dev->sensor, pad, enum_dv_timings, timings);
2082 +static int unicam_dv_timings_cap(struct file *file, void *priv,
2083 + struct v4l2_dv_timings_cap *cap)
2085 + struct unicam_node *node = video_drvdata(file);
2086 + struct unicam_device *dev = node->dev;
2088 + return v4l2_subdev_call(dev->sensor, pad, dv_timings_cap, cap);
2091 +static int unicam_subscribe_event(struct v4l2_fh *fh,
2092 + const struct v4l2_event_subscription *sub)
2094 + switch (sub->type) {
2095 + case V4L2_EVENT_FRAME_SYNC:
2096 + return v4l2_event_subscribe(fh, sub, 2, NULL);
2097 + case V4L2_EVENT_SOURCE_CHANGE:
2098 + return v4l2_event_subscribe(fh, sub, 4, NULL);
2101 + return v4l2_ctrl_subscribe_event(fh, sub);
2104 +static int unicam_log_status(struct file *file, void *fh)
2106 + struct unicam_node *node = video_drvdata(file);
2107 + struct unicam_device *dev = node->dev;
2110 + /* status for sub devices */
2111 + v4l2_device_call_all(&dev->v4l2_dev, 0, core, log_status);
2113 + unicam_info(dev, "-----Receiver status-----\n");
2114 + unicam_info(dev, "V4L2 width/height: %ux%u\n",
2115 + node->v_fmt.fmt.pix.width, node->v_fmt.fmt.pix.height);
2116 + unicam_info(dev, "Mediabus format: %08x\n", node->fmt->code);
2117 + unicam_info(dev, "V4L2 format: %08x\n",
2118 + node->v_fmt.fmt.pix.pixelformat);
2119 + reg = reg_read(dev, UNICAM_IPIPE);
2120 + unicam_info(dev, "Unpacking/packing: %u / %u\n",
2121 + get_field(reg, UNICAM_PUM_MASK),
2122 + get_field(reg, UNICAM_PPM_MASK));
2123 + unicam_info(dev, "----Live data----\n");
2124 + unicam_info(dev, "Programmed stride: %4u\n",
2125 + reg_read(dev, UNICAM_IBLS));
2126 + unicam_info(dev, "Detected resolution: %ux%u\n",
2127 + reg_read(dev, UNICAM_IHSTA),
2128 + reg_read(dev, UNICAM_IVSTA));
2129 + unicam_info(dev, "Write pointer: %08x\n",
2130 + reg_read(dev, UNICAM_IBWP));
2135 +static void unicam_notify(struct v4l2_subdev *sd,
2136 + unsigned int notification, void *arg)
2138 + struct unicam_device *dev = to_unicam_device(sd->v4l2_dev);
2140 + switch (notification) {
2141 + case V4L2_DEVICE_NOTIFY_EVENT:
2142 + v4l2_event_queue(&dev->node[IMAGE_PAD].video_dev, arg);
2149 +static const struct vb2_ops unicam_video_qops = {
2150 + .wait_prepare = vb2_ops_wait_prepare,
2151 + .wait_finish = vb2_ops_wait_finish,
2152 + .queue_setup = unicam_queue_setup,
2153 + .buf_prepare = unicam_buffer_prepare,
2154 + .buf_queue = unicam_buffer_queue,
2155 + .start_streaming = unicam_start_streaming,
2156 + .stop_streaming = unicam_stop_streaming,
2160 + * unicam_v4l2_open : This function is based on the v4l2_fh_open helper
2161 + * function. It has been augmented to handle sensor subdevice power management,
2163 +static int unicam_v4l2_open(struct file *file)
2165 + struct unicam_node *node = video_drvdata(file);
2166 + struct unicam_device *dev = node->dev;
2169 + mutex_lock(&node->lock);
2171 + ret = v4l2_fh_open(file);
2173 + unicam_err(dev, "v4l2_fh_open failed\n");
2179 + if (!v4l2_fh_is_singular_file(file))
2182 + ret = v4l2_subdev_call(dev->sensor, core, s_power, 1);
2183 + if (ret < 0 && ret != -ENOIOCTLCMD) {
2184 + v4l2_fh_release(file);
2192 + mutex_unlock(&node->lock);
2196 +static int unicam_v4l2_release(struct file *file)
2198 + struct unicam_node *node = video_drvdata(file);
2199 + struct unicam_device *dev = node->dev;
2200 + struct v4l2_subdev *sd = dev->sensor;
2204 + mutex_lock(&node->lock);
2206 + fh_singular = v4l2_fh_is_singular_file(file);
2208 + ret = _vb2_fop_release(file, NULL);
2211 + v4l2_subdev_call(sd, core, s_power, 0);
2214 + mutex_unlock(&node->lock);
2219 +/* unicam capture driver file operations */
2220 +static const struct v4l2_file_operations unicam_fops = {
2221 + .owner = THIS_MODULE,
2222 + .open = unicam_v4l2_open,
2223 + .release = unicam_v4l2_release,
2224 + .read = vb2_fop_read,
2225 + .poll = vb2_fop_poll,
2226 + .unlocked_ioctl = video_ioctl2,
2227 + .mmap = vb2_fop_mmap,
2230 +/* unicam capture ioctl operations */
2231 +static const struct v4l2_ioctl_ops unicam_ioctl_ops = {
2232 + .vidioc_querycap = unicam_querycap,
2233 + .vidioc_enum_fmt_vid_cap = unicam_enum_fmt_vid_cap,
2234 + .vidioc_g_fmt_vid_cap = unicam_g_fmt_vid_cap,
2235 + .vidioc_s_fmt_vid_cap = unicam_s_fmt_vid_cap,
2236 + .vidioc_try_fmt_vid_cap = unicam_try_fmt_vid_cap,
2238 + .vidioc_enum_fmt_meta_cap = unicam_enum_fmt_meta_cap,
2239 + .vidioc_g_fmt_meta_cap = unicam_g_fmt_meta_cap,
2240 + .vidioc_s_fmt_meta_cap = unicam_g_fmt_meta_cap,
2241 + .vidioc_try_fmt_meta_cap = unicam_g_fmt_meta_cap,
2243 + .vidioc_enum_input = unicam_enum_input,
2244 + .vidioc_g_input = unicam_g_input,
2245 + .vidioc_s_input = unicam_s_input,
2247 + .vidioc_querystd = unicam_querystd,
2248 + .vidioc_s_std = unicam_s_std,
2249 + .vidioc_g_std = unicam_g_std,
2251 + .vidioc_g_edid = unicam_g_edid,
2252 + .vidioc_s_edid = unicam_s_edid,
2254 + .vidioc_enum_framesizes = unicam_enum_framesizes,
2255 + .vidioc_enum_frameintervals = unicam_enum_frameintervals,
2257 + .vidioc_g_selection = unicam_g_selection,
2258 + .vidioc_s_selection = unicam_s_selection,
2260 + .vidioc_g_parm = unicam_g_parm,
2261 + .vidioc_s_parm = unicam_s_parm,
2263 + .vidioc_s_dv_timings = unicam_s_dv_timings,
2264 + .vidioc_g_dv_timings = unicam_g_dv_timings,
2265 + .vidioc_query_dv_timings = unicam_query_dv_timings,
2266 + .vidioc_enum_dv_timings = unicam_enum_dv_timings,
2267 + .vidioc_dv_timings_cap = unicam_dv_timings_cap,
2269 + .vidioc_reqbufs = vb2_ioctl_reqbufs,
2270 + .vidioc_create_bufs = vb2_ioctl_create_bufs,
2271 + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2272 + .vidioc_querybuf = vb2_ioctl_querybuf,
2273 + .vidioc_qbuf = vb2_ioctl_qbuf,
2274 + .vidioc_dqbuf = vb2_ioctl_dqbuf,
2275 + .vidioc_expbuf = vb2_ioctl_expbuf,
2276 + .vidioc_streamon = vb2_ioctl_streamon,
2277 + .vidioc_streamoff = vb2_ioctl_streamoff,
2279 + .vidioc_log_status = unicam_log_status,
2280 + .vidioc_subscribe_event = unicam_subscribe_event,
2281 + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2285 +unicam_async_bound(struct v4l2_async_notifier *notifier,
2286 + struct v4l2_subdev *subdev,
2287 + struct v4l2_async_subdev *asd)
2289 + struct unicam_device *unicam = to_unicam_device(notifier->v4l2_dev);
2291 + if (unicam->sensor) {
2292 + unicam_info(unicam, "Rejecting subdev %s (Already set!!)",
2297 + unicam->sensor = subdev;
2298 + unicam_dbg(1, unicam, "Using sensor %s for capture\n", subdev->name);
2303 +static void unicam_release(struct kref *kref)
2305 + struct unicam_device *unicam =
2306 + container_of(kref, struct unicam_device, kref);
2308 + v4l2_ctrl_handler_free(&unicam->ctrl_handler);
2309 + media_device_cleanup(&unicam->mdev);
2311 + if (unicam->sensor_config)
2312 + v4l2_subdev_free_pad_config(unicam->sensor_config);
2317 +static void unicam_put(struct unicam_device *unicam)
2319 + kref_put(&unicam->kref, unicam_release);
2322 +static void unicam_get(struct unicam_device *unicam)
2324 + kref_get(&unicam->kref);
2327 +static void unicam_node_release(struct video_device *vdev)
2329 + struct unicam_node *node = video_get_drvdata(vdev);
2331 + unicam_put(node->dev);
2334 +static int register_node(struct unicam_device *unicam, struct unicam_node *node,
2335 + enum v4l2_buf_type type, int pad_id)
2337 + struct video_device *vdev;
2338 + struct vb2_queue *q;
2339 + struct v4l2_mbus_framefmt mbus_fmt = {0};
2340 + const struct unicam_fmt *fmt;
2343 + if (pad_id == IMAGE_PAD) {
2344 + ret = __subdev_get_format(unicam, &mbus_fmt, pad_id);
2346 + unicam_err(unicam, "Failed to get_format - ret %d\n",
2351 + fmt = find_format_by_code(mbus_fmt.code);
2354 + * Find the first format that the sensor and unicam both
2357 + fmt = get_first_supported_format(unicam);
2360 + /* No compatible formats */
2363 + mbus_fmt.code = fmt->code;
2364 + ret = __subdev_set_format(unicam, &mbus_fmt, pad_id);
2368 + if (mbus_fmt.field != V4L2_FIELD_NONE) {
2369 + /* Interlaced not supported - disable it now. */
2370 + mbus_fmt.field = V4L2_FIELD_NONE;
2371 + ret = __subdev_set_format(unicam, &mbus_fmt, pad_id);
2376 + node->v_fmt.fmt.pix.pixelformat = fmt->fourcc ? fmt->fourcc
2377 + : fmt->repacked_fourcc;
2379 + /* Fix this node format as embedded data. */
2380 + fmt = find_format_by_code(MEDIA_BUS_FMT_SENSOR_DATA);
2381 + node->v_fmt.fmt.meta.dataformat = fmt->fourcc;
2384 + node->dev = unicam;
2385 + node->pad_id = pad_id;
2388 + /* Read current subdev format */
2389 + unicam_reset_format(node);
2391 + if (v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
2392 + v4l2_std_id tvnorms;
2394 + if (WARN_ON(!v4l2_subdev_has_op(unicam->sensor, video,
2397 + * Subdevice should not advertise s_std but not
2402 + ret = v4l2_subdev_call(unicam->sensor, video,
2403 + g_tvnorms, &tvnorms);
2406 + node->video_dev.tvnorms |= tvnorms;
2409 + spin_lock_init(&node->dma_queue_lock);
2410 + mutex_init(&node->lock);
2412 + vdev = &node->video_dev;
2413 + if (pad_id == IMAGE_PAD) {
2414 + /* Add controls from the subdevice */
2415 + ret = v4l2_ctrl_add_handler(&unicam->ctrl_handler,
2416 + unicam->sensor->ctrl_handler, NULL,
2422 + * If the sensor subdevice has any controls, associate the node
2423 + * with the ctrl handler to allow access from userland.
2425 + if (!list_empty(&unicam->ctrl_handler.ctrls))
2426 + vdev->ctrl_handler = &unicam->ctrl_handler;
2429 + q = &node->buffer_queue;
2431 + q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2432 + q->drv_priv = node;
2433 + q->ops = &unicam_video_qops;
2434 + q->mem_ops = &vb2_dma_contig_memops;
2435 + q->buf_struct_size = sizeof(struct unicam_buffer);
2436 + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2437 + q->lock = &node->lock;
2438 + q->min_buffers_needed = 2;
2439 + q->dev = &unicam->pdev->dev;
2441 + ret = vb2_queue_init(q);
2443 + unicam_err(unicam, "vb2_queue_init() failed\n");
2447 + INIT_LIST_HEAD(&node->dma_queue);
2449 + vdev->release = unicam_node_release;
2450 + vdev->fops = &unicam_fops;
2451 + vdev->ioctl_ops = &unicam_ioctl_ops;
2452 + vdev->v4l2_dev = &unicam->v4l2_dev;
2453 + vdev->vfl_dir = VFL_DIR_RX;
2455 + vdev->lock = &node->lock;
2456 + vdev->device_caps = (pad_id == IMAGE_PAD) ?
2457 + (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING) :
2458 + (V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING);
2460 + /* Define the device names */
2461 + snprintf(vdev->name, sizeof(vdev->name), "%s-%s", UNICAM_MODULE_NAME,
2462 + pad_id == IMAGE_PAD ? "image" : "embedded");
2464 + video_set_drvdata(vdev, node);
2465 + if (pad_id == IMAGE_PAD)
2466 + vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
2467 + node->pad.flags = MEDIA_PAD_FL_SINK;
2468 + media_entity_pads_init(&vdev->entity, 1, &node->pad);
2470 + node->dummy_buf_cpu_addr = dma_alloc_coherent(&unicam->pdev->dev,
2472 + &node->dummy_buf_dma_addr,
2474 + if (!node->dummy_buf_cpu_addr) {
2475 + unicam_err(unicam, "Unable to allocate dummy buffer.\n");
2479 + if (pad_id == METADATA_PAD) {
2480 + v4l2_disable_ioctl(vdev, VIDIOC_DQEVENT);
2481 + v4l2_disable_ioctl(vdev, VIDIOC_SUBSCRIBE_EVENT);
2482 + v4l2_disable_ioctl(vdev, VIDIOC_UNSUBSCRIBE_EVENT);
2484 + if (pad_id == METADATA_PAD ||
2485 + !v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
2486 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_STD);
2487 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_STD);
2488 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUMSTD);
2490 + if (pad_id == METADATA_PAD ||
2491 + !v4l2_subdev_has_op(unicam->sensor, video, querystd))
2492 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_QUERYSTD);
2493 + if (pad_id == METADATA_PAD ||
2494 + !v4l2_subdev_has_op(unicam->sensor, video, s_dv_timings)) {
2495 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_EDID);
2496 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_EDID);
2497 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_DV_TIMINGS_CAP);
2498 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_DV_TIMINGS);
2499 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_DV_TIMINGS);
2500 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUM_DV_TIMINGS);
2501 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_QUERY_DV_TIMINGS);
2503 + if (pad_id == METADATA_PAD ||
2504 + !v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_interval))
2505 + v4l2_disable_ioctl(&node->video_dev,
2506 + VIDIOC_ENUM_FRAMEINTERVALS);
2507 + if (pad_id == METADATA_PAD ||
2508 + !v4l2_subdev_has_op(unicam->sensor, video, g_frame_interval))
2509 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_PARM);
2510 + if (pad_id == METADATA_PAD ||
2511 + !v4l2_subdev_has_op(unicam->sensor, video, s_frame_interval))
2512 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_PARM);
2514 + if (pad_id == METADATA_PAD ||
2515 + !v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_size))
2516 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUM_FRAMESIZES);
2518 + if (node->pad_id == METADATA_PAD ||
2519 + !v4l2_subdev_has_op(unicam->sensor, pad, set_selection))
2520 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_SELECTION);
2522 + if (node->pad_id == METADATA_PAD ||
2523 + !v4l2_subdev_has_op(unicam->sensor, pad, get_selection))
2524 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_SELECTION);
2526 + ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
2528 + unicam_err(unicam, "Unable to register video device %s\n",
2534 + * Acquire a reference to unicam, which will be released when the video
2535 + * device will be unregistered and userspace will have closed all open
2538 + unicam_get(unicam);
2539 + node->registered = true;
2541 + if (pad_id != METADATA_PAD || unicam->sensor_embedded_data) {
2542 + ret = media_create_pad_link(&unicam->sensor->entity, pad_id,
2543 + &node->video_dev.entity, 0,
2544 + MEDIA_LNK_FL_ENABLED |
2545 + MEDIA_LNK_FL_IMMUTABLE);
2547 + unicam_err(unicam, "Unable to create pad link for %s\n",
2554 +static void unregister_nodes(struct unicam_device *unicam)
2558 + for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
2559 + struct unicam_node *node = &unicam->node[i];
2561 + if (node->dummy_buf_cpu_addr) {
2562 + dma_free_coherent(&unicam->pdev->dev, DUMMY_BUF_SIZE,
2563 + node->dummy_buf_cpu_addr,
2564 + node->dummy_buf_dma_addr);
2567 + if (node->registered) {
2568 + node->registered = false;
2569 + video_unregister_device(&node->video_dev);
2574 +static int unicam_probe_complete(struct unicam_device *unicam)
2578 + unicam->v4l2_dev.notify = unicam_notify;
2580 + unicam->sensor_config = v4l2_subdev_alloc_pad_config(unicam->sensor);
2581 + if (!unicam->sensor_config)
2584 + unicam->sensor_embedded_data = (unicam->sensor->entity.num_pads >= 2);
2586 + ret = register_node(unicam, &unicam->node[IMAGE_PAD],
2587 + V4L2_BUF_TYPE_VIDEO_CAPTURE, IMAGE_PAD);
2589 + unicam_err(unicam, "Unable to register image video device.\n");
2593 + ret = register_node(unicam, &unicam->node[METADATA_PAD],
2594 + V4L2_BUF_TYPE_META_CAPTURE, METADATA_PAD);
2596 + unicam_err(unicam, "Unable to register metadata video device.\n");
2600 + ret = v4l2_device_register_ro_subdev_nodes(&unicam->v4l2_dev);
2602 + unicam_err(unicam, "Unable to register subdev nodes.\n");
2607 + * Release the initial reference, all references are now owned by the
2610 + unicam_put(unicam);
2614 + unregister_nodes(unicam);
2615 + unicam_put(unicam);
2620 +static int unicam_async_complete(struct v4l2_async_notifier *notifier)
2622 + struct unicam_device *unicam = to_unicam_device(notifier->v4l2_dev);
2624 + return unicam_probe_complete(unicam);
2627 +static const struct v4l2_async_notifier_operations unicam_async_ops = {
2628 + .bound = unicam_async_bound,
2629 + .complete = unicam_async_complete,
2632 +static int of_unicam_connect_subdevs(struct unicam_device *dev)
2634 + struct platform_device *pdev = dev->pdev;
2635 + struct v4l2_fwnode_endpoint ep = { 0 };
2636 + struct device_node *ep_node;
2637 + struct device_node *sensor_node;
2638 + unsigned int lane;
2639 + int ret = -EINVAL;
2641 + if (of_property_read_u32(pdev->dev.of_node, "brcm,num-data-lanes",
2642 + &dev->max_data_lanes) < 0) {
2643 + unicam_err(dev, "number of data lanes not set\n");
2647 + /* Get the local endpoint and remote device. */
2648 + ep_node = of_graph_get_next_endpoint(pdev->dev.of_node, NULL);
2650 + unicam_dbg(3, dev, "can't get next endpoint\n");
2654 + unicam_dbg(3, dev, "ep_node is %pOF\n", ep_node);
2656 + sensor_node = of_graph_get_remote_port_parent(ep_node);
2657 + if (!sensor_node) {
2658 + unicam_dbg(3, dev, "can't get remote parent\n");
2659 + goto cleanup_exit;
2662 + unicam_dbg(1, dev, "found subdevice %pOF\n", sensor_node);
2664 + /* Parse the local endpoint and validate its configuration. */
2665 + v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
2667 + unicam_dbg(3, dev, "parsed local endpoint, bus_type %u\n",
2670 + dev->bus_type = ep.bus_type;
2672 + switch (ep.bus_type) {
2673 + case V4L2_MBUS_CSI2_DPHY:
2674 + switch (ep.bus.mipi_csi2.num_data_lanes) {
2681 + unicam_err(dev, "subdevice %pOF: %u data lanes not supported\n",
2683 + ep.bus.mipi_csi2.num_data_lanes);
2684 + goto cleanup_exit;
2687 + for (lane = 0; lane < ep.bus.mipi_csi2.num_data_lanes; lane++) {
2688 + if (ep.bus.mipi_csi2.data_lanes[lane] != lane + 1) {
2689 + unicam_err(dev, "subdevice %pOF: data lanes reordering not supported\n",
2691 + goto cleanup_exit;
2695 + if (ep.bus.mipi_csi2.num_data_lanes > dev->max_data_lanes) {
2696 + unicam_err(dev, "subdevice requires %u data lanes when %u are supported\n",
2697 + ep.bus.mipi_csi2.num_data_lanes,
2698 + dev->max_data_lanes);
2701 + dev->max_data_lanes = ep.bus.mipi_csi2.num_data_lanes;
2702 + dev->bus_flags = ep.bus.mipi_csi2.flags;
2706 + case V4L2_MBUS_CCP2:
2707 + if (ep.bus.mipi_csi1.clock_lane != 0 ||
2708 + ep.bus.mipi_csi1.data_lane != 1) {
2709 + unicam_err(dev, "subdevice %pOF: unsupported lanes configuration\n",
2711 + goto cleanup_exit;
2714 + dev->max_data_lanes = 1;
2715 + dev->bus_flags = ep.bus.mipi_csi1.strobe;
2719 + /* Unsupported bus type */
2720 + unicam_err(dev, "subdevice %pOF: unsupported bus type %u\n",
2721 + sensor_node, ep.bus_type);
2722 + goto cleanup_exit;
2725 + unicam_dbg(3, dev, "subdevice %pOF: %s bus, %u data lanes, flags=0x%08x\n",
2727 + dev->bus_type == V4L2_MBUS_CSI2_DPHY ? "CSI-2" : "CCP2",
2728 + dev->max_data_lanes, dev->bus_flags);
2730 + /* Initialize and register the async notifier. */
2731 + v4l2_async_notifier_init(&dev->notifier);
2732 + dev->notifier.ops = &unicam_async_ops;
2734 + dev->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
2735 + dev->asd.match.fwnode = of_fwnode_handle(sensor_node);
2736 + ret = v4l2_async_notifier_add_subdev(&dev->notifier, &dev->asd);
2738 + unicam_err(dev, "Error adding subdevice: %d\n", ret);
2739 + goto cleanup_exit;
2742 + ret = v4l2_async_notifier_register(&dev->v4l2_dev, &dev->notifier);
2744 + unicam_err(dev, "Error registering async notifier: %d\n", ret);
2749 + of_node_put(sensor_node);
2750 + of_node_put(ep_node);
2755 +static int unicam_probe(struct platform_device *pdev)
2757 + struct unicam_device *unicam;
2760 + unicam = kzalloc(sizeof(*unicam), GFP_KERNEL);
2764 + kref_init(&unicam->kref);
2765 + unicam->pdev = pdev;
2767 + unicam->base = devm_platform_ioremap_resource(pdev, 0);
2768 + if (IS_ERR(unicam->base)) {
2769 + unicam_err(unicam, "Failed to get main io block\n");
2770 + ret = PTR_ERR(unicam->base);
2771 + goto err_unicam_put;
2774 + unicam->clk_gate_base = devm_platform_ioremap_resource(pdev, 1);
2775 + if (IS_ERR(unicam->clk_gate_base)) {
2776 + unicam_err(unicam, "Failed to get 2nd io block\n");
2777 + ret = PTR_ERR(unicam->clk_gate_base);
2778 + goto err_unicam_put;
2781 + unicam->clock = devm_clk_get(&pdev->dev, "lp");
2782 + if (IS_ERR(unicam->clock)) {
2783 + unicam_err(unicam, "Failed to get clock\n");
2784 + ret = PTR_ERR(unicam->clock);
2785 + goto err_unicam_put;
2788 + ret = platform_get_irq(pdev, 0);
2790 + dev_err(&pdev->dev, "No IRQ resource\n");
2792 + goto err_unicam_put;
2795 + ret = devm_request_irq(&pdev->dev, ret, unicam_isr, 0,
2796 + "unicam_capture0", unicam);
2798 + dev_err(&pdev->dev, "Unable to request interrupt\n");
2800 + goto err_unicam_put;
2803 + unicam->mdev.dev = &pdev->dev;
2804 + strscpy(unicam->mdev.model, UNICAM_MODULE_NAME,
2805 + sizeof(unicam->mdev.model));
2806 + strscpy(unicam->mdev.serial, "", sizeof(unicam->mdev.serial));
2807 + snprintf(unicam->mdev.bus_info, sizeof(unicam->mdev.bus_info),
2808 + "platform:%s", dev_name(&pdev->dev));
2809 + unicam->mdev.hw_revision = 0;
2811 + media_device_init(&unicam->mdev);
2813 + unicam->v4l2_dev.mdev = &unicam->mdev;
2815 + ret = v4l2_device_register(&pdev->dev, &unicam->v4l2_dev);
2817 + unicam_err(unicam,
2818 + "Unable to register v4l2 device.\n");
2819 + goto err_unicam_put;
2822 + ret = media_device_register(&unicam->mdev);
2824 + unicam_err(unicam,
2825 + "Unable to register media-controller device.\n");
2826 + goto err_v4l2_unregister;
2829 + /* Reserve space for the controls */
2830 + ret = v4l2_ctrl_handler_init(&unicam->ctrl_handler, 16);
2832 + goto err_media_unregister;
2834 + /* set the driver data in platform device */
2835 + platform_set_drvdata(pdev, unicam);
2837 + ret = of_unicam_connect_subdevs(unicam);
2839 + dev_err(&pdev->dev, "Failed to connect subdevs\n");
2840 + goto err_media_unregister;
2843 + /* Enable the block power domain */
2844 + pm_runtime_enable(&pdev->dev);
2848 +err_media_unregister:
2849 + media_device_unregister(&unicam->mdev);
2850 +err_v4l2_unregister:
2851 + v4l2_device_unregister(&unicam->v4l2_dev);
2853 + unicam_put(unicam);
2858 +static int unicam_remove(struct platform_device *pdev)
2860 + struct unicam_device *unicam = platform_get_drvdata(pdev);
2862 + unicam_dbg(2, unicam, "%s\n", __func__);
2864 + v4l2_async_notifier_unregister(&unicam->notifier);
2865 + v4l2_device_unregister(&unicam->v4l2_dev);
2866 + media_device_unregister(&unicam->mdev);
2867 + unregister_nodes(unicam);
2869 + pm_runtime_disable(&pdev->dev);
2874 +static const struct of_device_id unicam_of_match[] = {
2875 + { .compatible = "brcm,bcm2835-unicam", },
2876 + { /* sentinel */ },
2878 +MODULE_DEVICE_TABLE(of, unicam_of_match);
2880 +static struct platform_driver unicam_driver = {
2881 + .probe = unicam_probe,
2882 + .remove = unicam_remove,
2884 + .name = UNICAM_MODULE_NAME,
2885 + .of_match_table = of_match_ptr(unicam_of_match),
2889 +module_platform_driver(unicam_driver);
2891 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
2892 +MODULE_DESCRIPTION("BCM2835 Unicam driver");
2893 +MODULE_LICENSE("GPL");
2894 +MODULE_VERSION(UNICAM_VERSION);
2896 +++ b/drivers/media/platform/bcm2835/vc4-regs-unicam.h
2898 +/* SPDX-License-Identifier: GPL-2.0-only */
2901 + * Copyright (C) 2017-2020 Raspberry Pi Trading.
2902 + * Dave Stevenson <dave.stevenson@raspberrypi.com>
2905 +#ifndef VC4_REGS_UNICAM_H
2906 +#define VC4_REGS_UNICAM_H
2909 + * The following values are taken from files found within the code drop
2910 + * made by Broadcom for the BCM21553 Graphics Driver, predominantly in
2911 + * brcm_usrlib/dag/vmcsx/vcinclude/hardware_vc4.h.
2912 + * They have been modified to be only the register offset.
2914 +#define UNICAM_CTRL 0x000
2915 +#define UNICAM_STA 0x004
2916 +#define UNICAM_ANA 0x008
2917 +#define UNICAM_PRI 0x00c
2918 +#define UNICAM_CLK 0x010
2919 +#define UNICAM_CLT 0x014
2920 +#define UNICAM_DAT0 0x018
2921 +#define UNICAM_DAT1 0x01c
2922 +#define UNICAM_DAT2 0x020
2923 +#define UNICAM_DAT3 0x024
2924 +#define UNICAM_DLT 0x028
2925 +#define UNICAM_CMP0 0x02c
2926 +#define UNICAM_CMP1 0x030
2927 +#define UNICAM_CAP0 0x034
2928 +#define UNICAM_CAP1 0x038
2929 +#define UNICAM_ICTL 0x100
2930 +#define UNICAM_ISTA 0x104
2931 +#define UNICAM_IDI0 0x108
2932 +#define UNICAM_IPIPE 0x10c
2933 +#define UNICAM_IBSA0 0x110
2934 +#define UNICAM_IBEA0 0x114
2935 +#define UNICAM_IBLS 0x118
2936 +#define UNICAM_IBWP 0x11c
2937 +#define UNICAM_IHWIN 0x120
2938 +#define UNICAM_IHSTA 0x124
2939 +#define UNICAM_IVWIN 0x128
2940 +#define UNICAM_IVSTA 0x12c
2941 +#define UNICAM_ICC 0x130
2942 +#define UNICAM_ICS 0x134
2943 +#define UNICAM_IDC 0x138
2944 +#define UNICAM_IDPO 0x13c
2945 +#define UNICAM_IDCA 0x140
2946 +#define UNICAM_IDCD 0x144
2947 +#define UNICAM_IDS 0x148
2948 +#define UNICAM_DCS 0x200
2949 +#define UNICAM_DBSA0 0x204
2950 +#define UNICAM_DBEA0 0x208
2951 +#define UNICAM_DBWP 0x20c
2952 +#define UNICAM_DBCTL 0x300
2953 +#define UNICAM_IBSA1 0x304
2954 +#define UNICAM_IBEA1 0x308
2955 +#define UNICAM_IDI1 0x30c
2956 +#define UNICAM_DBSA1 0x310
2957 +#define UNICAM_DBEA1 0x314
2958 +#define UNICAM_MISC 0x400
2961 + * The following bitmasks are from the kernel released by Broadcom
2962 + * for Android - https://android.googlesource.com/kernel/bcm/
2963 + * The Rhea, Hawaii, and Java chips all contain the same VideoCore4
2964 + * Unicam block as BCM2835, as defined in eg
2965 + * arch/arm/mach-rhea/include/mach/rdb_A0/brcm_rdb_cam.h and similar.
2966 + * Values reworked to use the kernel BIT and GENMASK macros.
2968 + * Some of the bit mnenomics have been amended to match the datasheet.
2970 +/* UNICAM_CTRL Register */
2971 +#define UNICAM_CPE BIT(0)
2972 +#define UNICAM_MEM BIT(1)
2973 +#define UNICAM_CPR BIT(2)
2974 +#define UNICAM_CPM_MASK GENMASK(3, 3)
2975 +#define UNICAM_CPM_CSI2 0
2976 +#define UNICAM_CPM_CCP2 1
2977 +#define UNICAM_SOE BIT(4)
2978 +#define UNICAM_DCM_MASK GENMASK(5, 5)
2979 +#define UNICAM_DCM_STROBE 0
2980 +#define UNICAM_DCM_DATA 1
2981 +#define UNICAM_SLS BIT(6)
2982 +#define UNICAM_PFT_MASK GENMASK(11, 8)
2983 +#define UNICAM_OET_MASK GENMASK(20, 12)
2985 +/* UNICAM_STA Register */
2986 +#define UNICAM_SYN BIT(0)
2987 +#define UNICAM_CS BIT(1)
2988 +#define UNICAM_SBE BIT(2)
2989 +#define UNICAM_PBE BIT(3)
2990 +#define UNICAM_HOE BIT(4)
2991 +#define UNICAM_PLE BIT(5)
2992 +#define UNICAM_SSC BIT(6)
2993 +#define UNICAM_CRCE BIT(7)
2994 +#define UNICAM_OES BIT(8)
2995 +#define UNICAM_IFO BIT(9)
2996 +#define UNICAM_OFO BIT(10)
2997 +#define UNICAM_BFO BIT(11)
2998 +#define UNICAM_DL BIT(12)
2999 +#define UNICAM_PS BIT(13)
3000 +#define UNICAM_IS BIT(14)
3001 +#define UNICAM_PI0 BIT(15)
3002 +#define UNICAM_PI1 BIT(16)
3003 +#define UNICAM_FSI_S BIT(17)
3004 +#define UNICAM_FEI_S BIT(18)
3005 +#define UNICAM_LCI_S BIT(19)
3006 +#define UNICAM_BUF0_RDY BIT(20)
3007 +#define UNICAM_BUF0_NO BIT(21)
3008 +#define UNICAM_BUF1_RDY BIT(22)
3009 +#define UNICAM_BUF1_NO BIT(23)
3010 +#define UNICAM_DI BIT(24)
3012 +#define UNICAM_STA_MASK_ALL \
3026 +/* UNICAM_ANA Register */
3027 +#define UNICAM_APD BIT(0)
3028 +#define UNICAM_BPD BIT(1)
3029 +#define UNICAM_AR BIT(2)
3030 +#define UNICAM_DDL BIT(3)
3031 +#define UNICAM_CTATADJ_MASK GENMASK(7, 4)
3032 +#define UNICAM_PTATADJ_MASK GENMASK(11, 8)
3034 +/* UNICAM_PRI Register */
3035 +#define UNICAM_PE BIT(0)
3036 +#define UNICAM_PT_MASK GENMASK(2, 1)
3037 +#define UNICAM_NP_MASK GENMASK(7, 4)
3038 +#define UNICAM_PP_MASK GENMASK(11, 8)
3039 +#define UNICAM_BS_MASK GENMASK(15, 12)
3040 +#define UNICAM_BL_MASK GENMASK(17, 16)
3042 +/* UNICAM_CLK Register */
3043 +#define UNICAM_CLE BIT(0)
3044 +#define UNICAM_CLPD BIT(1)
3045 +#define UNICAM_CLLPE BIT(2)
3046 +#define UNICAM_CLHSE BIT(3)
3047 +#define UNICAM_CLTRE BIT(4)
3048 +#define UNICAM_CLAC_MASK GENMASK(8, 5)
3049 +#define UNICAM_CLSTE BIT(29)
3051 +/* UNICAM_CLT Register */
3052 +#define UNICAM_CLT1_MASK GENMASK(7, 0)
3053 +#define UNICAM_CLT2_MASK GENMASK(15, 8)
3055 +/* UNICAM_DATn Registers */
3056 +#define UNICAM_DLE BIT(0)
3057 +#define UNICAM_DLPD BIT(1)
3058 +#define UNICAM_DLLPE BIT(2)
3059 +#define UNICAM_DLHSE BIT(3)
3060 +#define UNICAM_DLTRE BIT(4)
3061 +#define UNICAM_DLSM BIT(5)
3062 +#define UNICAM_DLFO BIT(28)
3063 +#define UNICAM_DLSTE BIT(29)
3065 +#define UNICAM_DAT_MASK_ALL (UNICAM_DLSTE + UNICAM_DLFO)
3067 +/* UNICAM_DLT Register */
3068 +#define UNICAM_DLT1_MASK GENMASK(7, 0)
3069 +#define UNICAM_DLT2_MASK GENMASK(15, 8)
3070 +#define UNICAM_DLT3_MASK GENMASK(23, 16)
3072 +/* UNICAM_ICTL Register */
3073 +#define UNICAM_FSIE BIT(0)
3074 +#define UNICAM_FEIE BIT(1)
3075 +#define UNICAM_IBOB BIT(2)
3076 +#define UNICAM_FCM BIT(3)
3077 +#define UNICAM_TFC BIT(4)
3078 +#define UNICAM_LIP_MASK GENMASK(6, 5)
3079 +#define UNICAM_LCIE_MASK GENMASK(28, 16)
3081 +/* UNICAM_IDI0/1 Register */
3082 +#define UNICAM_ID0_MASK GENMASK(7, 0)
3083 +#define UNICAM_ID1_MASK GENMASK(15, 8)
3084 +#define UNICAM_ID2_MASK GENMASK(23, 16)
3085 +#define UNICAM_ID3_MASK GENMASK(31, 24)
3087 +/* UNICAM_ISTA Register */
3088 +#define UNICAM_FSI BIT(0)
3089 +#define UNICAM_FEI BIT(1)
3090 +#define UNICAM_LCI BIT(2)
3092 +#define UNICAM_ISTA_MASK_ALL (UNICAM_FSI + UNICAM_FEI + UNICAM_LCI)
3094 +/* UNICAM_IPIPE Register */
3095 +#define UNICAM_PUM_MASK GENMASK(2, 0)
3096 + /* Unpacking modes */
3097 + #define UNICAM_PUM_NONE 0
3098 + #define UNICAM_PUM_UNPACK6 1
3099 + #define UNICAM_PUM_UNPACK7 2
3100 + #define UNICAM_PUM_UNPACK8 3
3101 + #define UNICAM_PUM_UNPACK10 4
3102 + #define UNICAM_PUM_UNPACK12 5
3103 + #define UNICAM_PUM_UNPACK14 6
3104 + #define UNICAM_PUM_UNPACK16 7
3105 +#define UNICAM_DDM_MASK GENMASK(6, 3)
3106 +#define UNICAM_PPM_MASK GENMASK(9, 7)
3107 + /* Packing modes */
3108 + #define UNICAM_PPM_NONE 0
3109 + #define UNICAM_PPM_PACK8 1
3110 + #define UNICAM_PPM_PACK10 2
3111 + #define UNICAM_PPM_PACK12 3
3112 + #define UNICAM_PPM_PACK14 4
3113 + #define UNICAM_PPM_PACK16 5
3114 +#define UNICAM_DEM_MASK GENMASK(11, 10)
3115 +#define UNICAM_DEBL_MASK GENMASK(14, 12)
3116 +#define UNICAM_ICM_MASK GENMASK(16, 15)
3117 +#define UNICAM_IDM_MASK GENMASK(17, 17)
3119 +/* UNICAM_ICC Register */
3120 +#define UNICAM_ICFL_MASK GENMASK(4, 0)
3121 +#define UNICAM_ICFH_MASK GENMASK(9, 5)
3122 +#define UNICAM_ICST_MASK GENMASK(12, 10)
3123 +#define UNICAM_ICLT_MASK GENMASK(15, 13)
3124 +#define UNICAM_ICLL_MASK GENMASK(31, 16)
3126 +/* UNICAM_DCS Register */
3127 +#define UNICAM_DIE BIT(0)
3128 +#define UNICAM_DIM BIT(1)
3129 +#define UNICAM_DBOB BIT(3)
3130 +#define UNICAM_FDE BIT(4)
3131 +#define UNICAM_LDP BIT(5)
3132 +#define UNICAM_EDL_MASK GENMASK(15, 8)
3134 +/* UNICAM_DBCTL Register */
3135 +#define UNICAM_DBEN BIT(0)
3136 +#define UNICAM_BUF0_IE BIT(1)
3137 +#define UNICAM_BUF1_IE BIT(2)
3139 +/* UNICAM_CMP[0,1] register */
3140 +#define UNICAM_PCE BIT(31)
3141 +#define UNICAM_GI BIT(9)
3142 +#define UNICAM_CPH BIT(8)
3143 +#define UNICAM_PCVC_MASK GENMASK(7, 6)
3144 +#define UNICAM_PCDT_MASK GENMASK(5, 0)
3146 +/* UNICAM_MISC register */
3147 +#define UNICAM_FL0 BIT(6)
3148 +#define UNICAM_FL1 BIT(9)