1 From 33a897162230dfc35b854aae2bec1ce8c2996642 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Wed, 1 Apr 2020 08:39:49 +0100
4 Subject: [PATCH] media: bcm2835-unicam: Driver for CCP2/CSI2 camera
7 Add driver for the Unicam camera receiver block on
10 This commit is made up of a series of changes cherry-picked from the
13 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
14 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
17 drivers/media/platform/Kconfig | 1 +
18 drivers/media/platform/Makefile | 2 +
19 drivers/media/platform/bcm2835/Kconfig | 14 +
20 drivers/media/platform/bcm2835/Makefile | 3 +
21 .../media/platform/bcm2835/bcm2835-unicam.c | 2369 +++++++++++++++++
22 .../media/platform/bcm2835/vc4-regs-unicam.h | 253 ++
23 7 files changed, 2643 insertions(+), 1 deletion(-)
24 create mode 100644 drivers/media/platform/bcm2835/Kconfig
25 create mode 100644 drivers/media/platform/bcm2835/Makefile
26 create mode 100644 drivers/media/platform/bcm2835/bcm2835-unicam.c
27 create mode 100644 drivers/media/platform/bcm2835/vc4-regs-unicam.h
31 @@ -3206,7 +3206,7 @@ F: Documentation/devicetree/bindings/med
32 F: drivers/staging/media/rpivid
34 BROADCOM BCM2835 CAMERA DRIVER
35 -M: Dave Stevenson <dave.stevenson@raspberrypi.org>
36 +M: Raspberry Pi Kernel Maintenance <kernel-list@raspberrypi.com>
37 L: linux-media@vger.kernel.org
39 F: drivers/media/platform/bcm2835/
40 --- a/drivers/media/platform/Kconfig
41 +++ b/drivers/media/platform/Kconfig
42 @@ -146,6 +146,7 @@ source "drivers/media/platform/am437x/Kc
43 source "drivers/media/platform/xilinx/Kconfig"
44 source "drivers/media/platform/rcar-vin/Kconfig"
45 source "drivers/media/platform/atmel/Kconfig"
46 +source "drivers/media/platform/bcm2835/Kconfig"
47 source "drivers/media/platform/sunxi/Kconfig"
50 --- a/drivers/media/platform/Makefile
51 +++ b/drivers/media/platform/Makefile
52 @@ -100,4 +100,6 @@ obj-y += meson/
60 +++ b/drivers/media/platform/bcm2835/Kconfig
62 +# Broadcom VideoCore4 V4L2 camera support
64 +config VIDEO_BCM2835_UNICAM
65 + tristate "Broadcom BCM2835 Unicam video capture driver"
66 + depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && MEDIA_CONTROLLER
67 + depends on ARCH_BCM2835 || COMPILE_TEST
68 + select VIDEOBUF2_DMA_CONTIG
71 + Say Y here to enable V4L2 subdevice for CSI2 receiver.
72 + This is a V4L2 subdevice that interfaces directly to the VC4 peripheral.
74 + To compile this driver as a module, choose M here. The module
75 + will be called bcm2835-unicam.
77 +++ b/drivers/media/platform/bcm2835/Makefile
79 +# Makefile for BCM2835 Unicam driver
81 +obj-$(CONFIG_VIDEO_BCM2835_UNICAM) += bcm2835-unicam.o
83 +++ b/drivers/media/platform/bcm2835/bcm2835-unicam.c
85 +// SPDX-License-Identifier: GPL-2.0-only
87 + * BCM2835 Unicam Capture Driver
89 + * Copyright (C) 2017-2020 - Raspberry Pi (Trading) Ltd.
91 + * Dave Stevenson <dave.stevenson@raspberrypi.com>
93 + * Based on TI am437x driver by
94 + * Benoit Parrot <bparrot@ti.com>
95 + * Lad, Prabhakar <prabhakar.csengg@gmail.com>
97 + * and TI CAL camera interface driver by
98 + * Benoit Parrot <bparrot@ti.com>
101 + * There are two camera drivers in the kernel for BCM283x - this one
102 + * and bcm2835-camera (currently in staging).
104 + * This driver directly controls the Unicam peripheral - there is no
105 + * involvement with the VideoCore firmware. Unicam receives CSI-2 or
106 + * CCP2 data and writes it into SDRAM.
107 + * The only potential processing options are to repack Bayer data into an
108 + * alternate format, and applying windowing.
109 + * The repacking does not shift the data, so can repack V4L2_PIX_FMT_Sxxxx10P
110 + * to V4L2_PIX_FMT_Sxxxx10, or V4L2_PIX_FMT_Sxxxx12P to V4L2_PIX_FMT_Sxxxx12,
111 + * but not generically up to V4L2_PIX_FMT_Sxxxx16. The driver will add both
112 + * formats where the relevant formats are defined, and will automatically
113 + * configure the repacking as required.
114 + * Support for windowing may be added later.
116 + * It should be possible to connect this driver to any sensor with a
117 + * suitable output interface and V4L2 subdevice driver.
119 + * bcm2835-camera uses the VideoCore firmware to control the sensor,
120 + * Unicam, ISP, and all tuner control loops. Fully processed frames are
121 + * delivered to the driver by the firmware. It only has sensor drivers
122 + * for Omnivision OV5647, and Sony IMX219 sensors.
124 + * The two drivers are mutually exclusive for the same Unicam instance.
125 + * The VideoCore firmware checks the device tree configuration during boot.
126 + * If it finds device tree nodes called csi0 or csi1 it will block the
127 + * firmware from accessing the peripheral, and bcm2835-camera will
128 + * not be able to stream data.
131 +#include <linux/clk.h>
132 +#include <linux/delay.h>
133 +#include <linux/device.h>
134 +#include <linux/err.h>
135 +#include <linux/init.h>
136 +#include <linux/interrupt.h>
137 +#include <linux/io.h>
138 +#include <linux/module.h>
139 +#include <linux/of_device.h>
140 +#include <linux/of_graph.h>
141 +#include <linux/pinctrl/consumer.h>
142 +#include <linux/platform_device.h>
143 +#include <linux/pm_runtime.h>
144 +#include <linux/slab.h>
145 +#include <linux/uaccess.h>
146 +#include <linux/videodev2.h>
148 +#include <media/v4l2-common.h>
149 +#include <media/v4l2-ctrls.h>
150 +#include <media/v4l2-dev.h>
151 +#include <media/v4l2-device.h>
152 +#include <media/v4l2-dv-timings.h>
153 +#include <media/v4l2-event.h>
154 +#include <media/v4l2-ioctl.h>
155 +#include <media/v4l2-fwnode.h>
156 +#include <media/videobuf2-dma-contig.h>
158 +#include "vc4-regs-unicam.h"
160 +#define UNICAM_MODULE_NAME "unicam"
161 +#define UNICAM_VERSION "0.1.0"
164 +module_param(debug, int, 0644);
165 +MODULE_PARM_DESC(debug, "Debug level 0-3");
167 +#define unicam_dbg(level, dev, fmt, arg...) \
168 + v4l2_dbg(level, debug, &(dev)->v4l2_dev, fmt, ##arg)
169 +#define unicam_info(dev, fmt, arg...) \
170 + v4l2_info(&(dev)->v4l2_dev, fmt, ##arg)
171 +#define unicam_err(dev, fmt, arg...) \
172 + v4l2_err(&(dev)->v4l2_dev, fmt, ##arg)
174 +/* To protect against a dodgy sensor driver never returning an error from
175 + * enum_mbus_code, set a maximum index value to be used.
177 +#define MAX_ENUM_MBUS_CODE 128
180 + * Stride is a 16 bit register, but also has to be a multiple of 32.
182 +#define BPL_ALIGNMENT 32
183 +#define MAX_BYTESPERLINE ((1 << 16) - BPL_ALIGNMENT)
185 + * Max width is therefore determined by the max stride divided by
186 + * the number of bits per pixel. Take 32bpp as a
188 + * No imposed limit on the height, so adopt a square image for want
189 + * of anything better.
191 +#define MAX_WIDTH (MAX_BYTESPERLINE / 4)
192 +#define MAX_HEIGHT MAX_WIDTH
193 +/* Define a nominal minimum image size */
194 +#define MIN_WIDTH 16
195 +#define MIN_HEIGHT 16
198 + * struct unicam_fmt - Unicam media bus format information
199 + * @pixelformat: V4L2 pixel format FCC identifier. 0 if n/a.
200 + * @repacked_fourcc: V4L2 pixel format FCC identifier if the data is expanded
201 + * out to 16bpp. 0 if n/a.
202 + * @code: V4L2 media bus format code.
203 + * @depth: Bits per pixel as delivered from the source.
204 + * @csi_dt: CSI data type.
205 + * @check_variants: Flag to denote that there are multiple mediabus formats
206 + * still in the list that could match this V4L2 format.
210 + u32 repacked_fourcc;
217 +static const struct unicam_fmt formats[] = {
220 + .fourcc = V4L2_PIX_FMT_YUYV,
221 + .code = MEDIA_BUS_FMT_YUYV8_2X8,
224 + .check_variants = 1,
226 + .fourcc = V4L2_PIX_FMT_UYVY,
227 + .code = MEDIA_BUS_FMT_UYVY8_2X8,
230 + .check_variants = 1,
232 + .fourcc = V4L2_PIX_FMT_YVYU,
233 + .code = MEDIA_BUS_FMT_YVYU8_2X8,
236 + .check_variants = 1,
238 + .fourcc = V4L2_PIX_FMT_VYUY,
239 + .code = MEDIA_BUS_FMT_VYUY8_2X8,
242 + .check_variants = 1,
244 + .fourcc = V4L2_PIX_FMT_YUYV,
245 + .code = MEDIA_BUS_FMT_YUYV8_1X16,
249 + .fourcc = V4L2_PIX_FMT_UYVY,
250 + .code = MEDIA_BUS_FMT_UYVY8_1X16,
254 + .fourcc = V4L2_PIX_FMT_YVYU,
255 + .code = MEDIA_BUS_FMT_YVYU8_1X16,
259 + .fourcc = V4L2_PIX_FMT_VYUY,
260 + .code = MEDIA_BUS_FMT_VYUY8_1X16,
265 + .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
266 + .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
270 + .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
271 + .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
275 + .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
276 + .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
280 + .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
281 + .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
285 + .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
286 + .code = MEDIA_BUS_FMT_RGB888_1X24,
290 + .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
291 + .code = MEDIA_BUS_FMT_BGR888_1X24,
295 + .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
296 + .code = MEDIA_BUS_FMT_ARGB8888_1X32,
300 + /* Bayer Formats */
301 + .fourcc = V4L2_PIX_FMT_SBGGR8,
302 + .code = MEDIA_BUS_FMT_SBGGR8_1X8,
306 + .fourcc = V4L2_PIX_FMT_SGBRG8,
307 + .code = MEDIA_BUS_FMT_SGBRG8_1X8,
311 + .fourcc = V4L2_PIX_FMT_SGRBG8,
312 + .code = MEDIA_BUS_FMT_SGRBG8_1X8,
316 + .fourcc = V4L2_PIX_FMT_SRGGB8,
317 + .code = MEDIA_BUS_FMT_SRGGB8_1X8,
321 + .fourcc = V4L2_PIX_FMT_SBGGR10P,
322 + .repacked_fourcc = V4L2_PIX_FMT_SBGGR10,
323 + .code = MEDIA_BUS_FMT_SBGGR10_1X10,
327 + .fourcc = V4L2_PIX_FMT_SGBRG10P,
328 + .repacked_fourcc = V4L2_PIX_FMT_SGBRG10,
329 + .code = MEDIA_BUS_FMT_SGBRG10_1X10,
333 + .fourcc = V4L2_PIX_FMT_SGRBG10P,
334 + .repacked_fourcc = V4L2_PIX_FMT_SGRBG10,
335 + .code = MEDIA_BUS_FMT_SGRBG10_1X10,
339 + .fourcc = V4L2_PIX_FMT_SRGGB10P,
340 + .repacked_fourcc = V4L2_PIX_FMT_SRGGB10,
341 + .code = MEDIA_BUS_FMT_SRGGB10_1X10,
345 + .fourcc = V4L2_PIX_FMT_SBGGR12P,
346 + .repacked_fourcc = V4L2_PIX_FMT_SBGGR12,
347 + .code = MEDIA_BUS_FMT_SBGGR12_1X12,
351 + .fourcc = V4L2_PIX_FMT_SGBRG12P,
352 + .repacked_fourcc = V4L2_PIX_FMT_SGBRG12,
353 + .code = MEDIA_BUS_FMT_SGBRG12_1X12,
357 + .fourcc = V4L2_PIX_FMT_SGRBG12P,
358 + .repacked_fourcc = V4L2_PIX_FMT_SGRBG12,
359 + .code = MEDIA_BUS_FMT_SGRBG12_1X12,
363 + .fourcc = V4L2_PIX_FMT_SRGGB12P,
364 + .repacked_fourcc = V4L2_PIX_FMT_SRGGB12,
365 + .code = MEDIA_BUS_FMT_SRGGB12_1X12,
369 + .fourcc = V4L2_PIX_FMT_SBGGR14P,
370 + .code = MEDIA_BUS_FMT_SBGGR14_1X14,
374 + .fourcc = V4L2_PIX_FMT_SGBRG14P,
375 + .code = MEDIA_BUS_FMT_SGBRG14_1X14,
379 + .fourcc = V4L2_PIX_FMT_SGRBG14P,
380 + .code = MEDIA_BUS_FMT_SGRBG14_1X14,
384 + .fourcc = V4L2_PIX_FMT_SRGGB14P,
385 + .code = MEDIA_BUS_FMT_SRGGB14_1X14,
390 + * 16 bit Bayer formats could be supported, but there is no CSI2
391 + * data_type defined for raw 16, and no sensors that produce it at
395 + /* Greyscale formats */
396 + .fourcc = V4L2_PIX_FMT_GREY,
397 + .code = MEDIA_BUS_FMT_Y8_1X8,
401 + .fourcc = V4L2_PIX_FMT_Y10P,
402 + .repacked_fourcc = V4L2_PIX_FMT_Y10,
403 + .code = MEDIA_BUS_FMT_Y10_1X10,
407 + /* NB There is no packed V4L2 fourcc for this format. */
408 + .repacked_fourcc = V4L2_PIX_FMT_Y12,
409 + .code = MEDIA_BUS_FMT_Y12_1X12,
415 +struct unicam_dmaqueue {
416 + struct list_head active;
419 +struct unicam_buffer {
420 + struct vb2_v4l2_buffer vb;
421 + struct list_head list;
425 + /* peripheral base address */
426 + void __iomem *base;
427 + /* clock gating base address */
428 + void __iomem *clk_gate_base;
431 +#define MAX_POSSIBLE_PIX_FMTS (ARRAY_SIZE(formats))
433 +struct unicam_device {
434 + /* V4l2 specific parameters */
435 + /* Identifies video device for this channel */
436 + struct video_device video_dev;
437 + struct v4l2_ctrl_handler ctrl_handler;
439 + struct v4l2_fwnode_endpoint endpoint;
441 + struct v4l2_async_subdev asd;
444 + struct unicam_cfg cfg;
448 + struct v4l2_device v4l2_dev;
449 + struct media_device mdev;
450 + struct media_pad pad;
452 + /* parent device */
453 + struct platform_device *pdev;
454 + /* subdevice async Notifier */
455 + struct v4l2_async_notifier notifier;
456 + unsigned int sequence;
458 + /* ptr to sub device */
459 + struct v4l2_subdev *sensor;
460 + /* Pad config for the sensor */
461 + struct v4l2_subdev_pad_config *sensor_config;
462 + /* current input at the sub device */
465 + /* Pointer pointing to current v4l2_buffer */
466 + struct unicam_buffer *cur_frm;
467 + /* Pointer pointing to next v4l2_buffer */
468 + struct unicam_buffer *next_frm;
470 + /* video capture */
471 + const struct unicam_fmt *fmt;
472 + /* Used to store current pixel format */
473 + struct v4l2_format v_fmt;
474 + /* Used to store current mbus frame format */
475 + struct v4l2_mbus_framefmt m_fmt;
477 + unsigned int virtual_channel;
478 + enum v4l2_mbus_type bus_type;
480 + * Stores bus.mipi_csi2.flags for CSI2 sensors, or
481 + * bus.mipi_csi1.strobe for CCP2.
483 + unsigned int bus_flags;
484 + unsigned int max_data_lanes;
485 + unsigned int active_data_lanes;
487 + struct v4l2_rect crop;
489 + /* Currently selected input on subdev */
492 + /* Buffer queue used in video-buf */
493 + struct vb2_queue buffer_queue;
494 + /* Queue of filled frames */
495 + struct unicam_dmaqueue dma_queue;
496 + /* IRQ lock for DMA queue */
497 + spinlock_t dma_queue_lock;
498 + /* lock used to access this structure */
500 + /* Flag to denote that we are processing buffers */
504 +/* Hardware access */
505 +#define clk_write(dev, val) writel((val) | 0x5a000000, (dev)->clk_gate_base)
506 +#define clk_read(dev) readl((dev)->clk_gate_base)
508 +#define reg_read(dev, offset) readl((dev)->base + (offset))
509 +#define reg_write(dev, offset, val) writel(val, (dev)->base + (offset))
511 +#define reg_read_field(dev, offset, mask) get_field(reg_read((dev), (offset), \
514 +static inline int get_field(u32 value, u32 mask)
516 + return (value & mask) >> __ffs(mask);
519 +static inline void set_field(u32 *valp, u32 field, u32 mask)
524 + val |= (field << __ffs(mask)) & mask;
528 +static inline void reg_write_field(struct unicam_cfg *dev, u32 offset,
529 + u32 field, u32 mask)
531 + u32 val = reg_read((dev), (offset));
533 + set_field(&val, field, mask);
534 + reg_write((dev), (offset), val);
537 +/* Power management functions */
538 +static inline int unicam_runtime_get(struct unicam_device *dev)
540 + return pm_runtime_get_sync(&dev->pdev->dev);
543 +static inline void unicam_runtime_put(struct unicam_device *dev)
545 + pm_runtime_put_sync(&dev->pdev->dev);
548 +/* Format setup functions */
549 +static const struct unicam_fmt *find_format_by_code(u32 code)
553 + for (i = 0; i < ARRAY_SIZE(formats); i++) {
554 + if (formats[i].code == code)
555 + return &formats[i];
561 +static int check_mbus_format(struct unicam_device *dev,
562 + const struct unicam_fmt *format)
564 + struct v4l2_subdev_mbus_code_enum mbus_code;
568 + for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
569 + memset(&mbus_code, 0, sizeof(mbus_code));
570 + mbus_code.index = i;
571 + mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
573 + ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
576 + if (!ret && mbus_code.code == format->code)
583 +static const struct unicam_fmt *find_format_by_pix(struct unicam_device *dev,
588 + for (i = 0; i < ARRAY_SIZE(formats); i++) {
589 + if (formats[i].fourcc == pixelformat ||
590 + formats[i].repacked_fourcc == pixelformat) {
591 + if (formats[i].check_variants &&
592 + !check_mbus_format(dev, &formats[i]))
594 + return &formats[i];
601 +static inline unsigned int bytes_per_line(u32 width,
602 + const struct unicam_fmt *fmt,
605 + if (v4l2_fourcc == fmt->repacked_fourcc)
606 + /* Repacking always goes to 16bpp */
607 + return ALIGN(width << 1, BPL_ALIGNMENT);
609 + return ALIGN((width * fmt->depth) >> 3, BPL_ALIGNMENT);
612 +static int __subdev_get_format(struct unicam_device *dev,
613 + struct v4l2_mbus_framefmt *fmt)
615 + struct v4l2_subdev_format sd_fmt = {
616 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
620 + ret = v4l2_subdev_call(dev->sensor, pad, get_fmt, dev->sensor_config,
625 + *fmt = sd_fmt.format;
627 + unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__,
628 + fmt->width, fmt->height, fmt->code);
633 +static int __subdev_set_format(struct unicam_device *dev,
634 + struct v4l2_mbus_framefmt *fmt)
636 + struct v4l2_subdev_format sd_fmt = {
637 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
641 + sd_fmt.format = *fmt;
643 + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
648 + unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__,
649 + fmt->width, fmt->height, fmt->code);
654 +static int unicam_calc_format_size_bpl(struct unicam_device *dev,
655 + const struct unicam_fmt *fmt,
656 + struct v4l2_format *f)
658 + unsigned int min_bytesperline;
660 + v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, MAX_WIDTH, 2,
661 + &f->fmt.pix.height, MIN_HEIGHT, MAX_HEIGHT, 0,
664 + min_bytesperline = bytes_per_line(f->fmt.pix.width, fmt,
665 + f->fmt.pix.pixelformat);
667 + if (f->fmt.pix.bytesperline > min_bytesperline &&
668 + f->fmt.pix.bytesperline <= MAX_BYTESPERLINE)
669 + f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
672 + f->fmt.pix.bytesperline = min_bytesperline;
674 + f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
676 + unicam_dbg(3, dev, "%s: fourcc: %08X size: %dx%d bpl:%d img_size:%d\n",
678 + f->fmt.pix.pixelformat,
679 + f->fmt.pix.width, f->fmt.pix.height,
680 + f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
685 +static int unicam_reset_format(struct unicam_device *dev)
687 + struct v4l2_mbus_framefmt mbus_fmt;
690 + ret = __subdev_get_format(dev, &mbus_fmt);
692 + unicam_err(dev, "Failed to get_format - ret %d\n", ret);
696 + if (mbus_fmt.code != dev->fmt->code) {
697 + unicam_err(dev, "code mismatch - fmt->code %08x, mbus_fmt.code %08x\n",
698 + dev->fmt->code, mbus_fmt.code);
702 + v4l2_fill_pix_format(&dev->v_fmt.fmt.pix, &mbus_fmt);
703 + dev->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
705 + unicam_calc_format_size_bpl(dev, dev->fmt, &dev->v_fmt);
707 + dev->m_fmt = mbus_fmt;
712 +static void unicam_wr_dma_addr(struct unicam_device *dev, dma_addr_t dmaaddr)
715 + * dmaaddr should be a 32-bit address with the top two bits set to 0x3
716 + * to signify uncached access through the Videocore memory controller.
718 + BUG_ON((dmaaddr >> 30) != 0x3);
720 + reg_write(&dev->cfg, UNICAM_IBSA0, dmaaddr);
721 + reg_write(&dev->cfg, UNICAM_IBEA0,
722 + dmaaddr + dev->v_fmt.fmt.pix.sizeimage);
725 +static inline unsigned int unicam_get_lines_done(struct unicam_device *dev)
727 + dma_addr_t start_addr, cur_addr;
728 + unsigned int stride = dev->v_fmt.fmt.pix.bytesperline;
729 + struct unicam_buffer *frm = dev->cur_frm;
734 + start_addr = vb2_dma_contig_plane_dma_addr(&frm->vb.vb2_buf, 0);
735 + cur_addr = reg_read(&dev->cfg, UNICAM_IBWP);
736 + return (unsigned int)(cur_addr - start_addr) / stride;
739 +static inline void unicam_schedule_next_buffer(struct unicam_device *dev)
741 + struct unicam_dmaqueue *dma_q = &dev->dma_queue;
742 + struct unicam_buffer *buf;
745 + buf = list_entry(dma_q->active.next, struct unicam_buffer, list);
746 + dev->next_frm = buf;
747 + list_del(&buf->list);
749 + addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
750 + unicam_wr_dma_addr(dev, addr);
753 +static inline void unicam_process_buffer_complete(struct unicam_device *dev)
755 + dev->cur_frm->vb.field = dev->m_fmt.field;
756 + dev->cur_frm->vb.sequence = dev->sequence++;
758 + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
759 + dev->cur_frm = dev->next_frm;
763 + * unicam_isr : ISR handler for unicam capture
765 + * @dev_id: dev_id ptr
767 + * It changes status of the captured buffer, takes next buffer from the queue
768 + * and sets its address in unicam registers
770 +static irqreturn_t unicam_isr(int irq, void *dev)
772 + struct unicam_device *unicam = (struct unicam_device *)dev;
773 + struct unicam_cfg *cfg = &unicam->cfg;
774 + struct unicam_dmaqueue *dma_q = &unicam->dma_queue;
775 + unsigned int lines_done = unicam_get_lines_done(dev);
776 + unsigned int sequence = unicam->sequence;
780 + * Don't service interrupts if not streaming.
781 + * Avoids issues if the VPU should enable the
782 + * peripheral without the kernel knowing (that
783 + * shouldn't happen, but causes issues if it does).
785 + if (!unicam->streaming)
786 + return IRQ_HANDLED;
788 + sta = reg_read(cfg, UNICAM_STA);
789 + /* Write value back to clear the interrupts */
790 + reg_write(cfg, UNICAM_STA, sta);
792 + ista = reg_read(cfg, UNICAM_ISTA);
793 + /* Write value back to clear the interrupts */
794 + reg_write(cfg, UNICAM_ISTA, ista);
796 + unicam_dbg(3, unicam, "ISR: ISTA: 0x%X, STA: 0x%X, sequence %d, lines done %d",
797 + ista, sta, sequence, lines_done);
799 + if (!(sta && (UNICAM_IS | UNICAM_PI0)))
800 + return IRQ_HANDLED;
802 + if (ista & UNICAM_FSI) {
804 + * Timestamp is to be when the first data byte was captured,
807 + if (unicam->cur_frm)
808 + unicam->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
810 + if (ista & UNICAM_FEI || sta & UNICAM_PI0) {
812 + * Ensure we have swapped buffers already as we can't
813 + * stop the peripheral. Overwrite the frame we've just
814 + * captured instead.
816 + if (unicam->cur_frm && unicam->cur_frm != unicam->next_frm)
817 + unicam_process_buffer_complete(unicam);
820 + /* Cannot swap buffer at frame end, there may be a race condition
821 + * where the HW does not actually swap it if the new frame has
824 + if (ista & (UNICAM_FSI | UNICAM_LCI) && !(ista & UNICAM_FEI)) {
825 + spin_lock(&unicam->dma_queue_lock);
826 + if (!list_empty(&dma_q->active) &&
827 + unicam->cur_frm == unicam->next_frm)
828 + unicam_schedule_next_buffer(unicam);
829 + spin_unlock(&unicam->dma_queue_lock);
832 + if (reg_read(&unicam->cfg, UNICAM_ICTL) & UNICAM_FCM) {
833 + /* Switch out of trigger mode if selected */
834 + reg_write_field(&unicam->cfg, UNICAM_ICTL, 1, UNICAM_TFC);
835 + reg_write_field(&unicam->cfg, UNICAM_ICTL, 0, UNICAM_FCM);
837 + return IRQ_HANDLED;
840 +static int unicam_querycap(struct file *file, void *priv,
841 + struct v4l2_capability *cap)
843 + struct unicam_device *dev = video_drvdata(file);
845 + strlcpy(cap->driver, UNICAM_MODULE_NAME, sizeof(cap->driver));
846 + strlcpy(cap->card, UNICAM_MODULE_NAME, sizeof(cap->card));
848 + snprintf(cap->bus_info, sizeof(cap->bus_info),
849 + "platform:%s", dev->v4l2_dev.name);
854 +static int unicam_enum_fmt_vid_cap(struct file *file, void *priv,
855 + struct v4l2_fmtdesc *f)
857 + struct unicam_device *dev = video_drvdata(file);
858 + struct v4l2_subdev_mbus_code_enum mbus_code;
859 + const struct unicam_fmt *fmt = NULL;
864 + for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
865 + memset(&mbus_code, 0, sizeof(mbus_code));
866 + mbus_code.index = i;
868 + ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
872 + "subdev->enum_mbus_code idx %d returned %d - index invalid\n",
877 + fmt = find_format_by_code(mbus_code.code);
880 + if (index == f->index) {
881 + f->pixelformat = fmt->fourcc;
886 + if (fmt->repacked_fourcc) {
887 + if (index == f->index) {
888 + f->pixelformat = fmt->repacked_fourcc;
899 +static int unicam_g_fmt_vid_cap(struct file *file, void *priv,
900 + struct v4l2_format *f)
902 + struct unicam_device *dev = video_drvdata(file);
910 +const struct unicam_fmt *get_first_supported_format(struct unicam_device *dev)
912 + struct v4l2_subdev_mbus_code_enum mbus_code;
913 + const struct unicam_fmt *fmt = NULL;
917 + for (j = 0; ret != -EINVAL && ret != -ENOIOCTLCMD; ++j) {
918 + memset(&mbus_code, 0, sizeof(mbus_code));
919 + mbus_code.index = j;
920 + ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
924 + "subdev->enum_mbus_code idx %d returned %d - continue\n",
929 + unicam_dbg(2, dev, "subdev %s: code: 0x%08x idx: %d\n",
930 + dev->sensor->name, mbus_code.code, j);
932 + fmt = find_format_by_code(mbus_code.code);
933 + unicam_dbg(2, dev, "fmt 0x%08x returned as %p, V4L2 FOURCC 0x%08x, csi_dt 0x%02x\n",
934 + mbus_code.code, fmt, fmt ? fmt->fourcc : 0,
935 + fmt ? fmt->csi_dt : 0);
943 +static int unicam_try_fmt_vid_cap(struct file *file, void *priv,
944 + struct v4l2_format *f)
946 + struct unicam_device *dev = video_drvdata(file);
947 + struct v4l2_subdev_format sd_fmt = {
948 + .which = V4L2_SUBDEV_FORMAT_TRY,
950 + struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
951 + const struct unicam_fmt *fmt;
954 + fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
956 + /* Pixel format not supported by unicam. Choose the first
957 + * supported format, and let the sensor choose something else.
959 + unicam_dbg(3, dev, "Fourcc format (0x%08x) not found. Use first format.\n",
960 + f->fmt.pix.pixelformat);
963 + f->fmt.pix.pixelformat = fmt->fourcc;
966 + v4l2_fill_mbus_format(mbus_fmt, &f->fmt.pix, fmt->code);
968 + * No support for receiving interlaced video, so never
969 + * request it from the sensor subdev.
971 + mbus_fmt->field = V4L2_FIELD_NONE;
973 + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
975 + if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
978 + if (mbus_fmt->field != V4L2_FIELD_NONE)
979 + unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
981 + v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
982 + if (mbus_fmt->code != fmt->code) {
983 + /* Sensor has returned an alternate format */
984 + fmt = find_format_by_code(mbus_fmt->code);
986 + /* The alternate format is one unicam can't support.
987 + * Find the first format that is supported by both, and
990 + fmt = get_first_supported_format(dev);
991 + mbus_fmt->code = fmt->code;
993 + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt,
994 + dev->sensor_config, &sd_fmt);
995 + if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
998 + if (mbus_fmt->field != V4L2_FIELD_NONE)
999 + unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
1001 + v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
1003 + if (mbus_fmt->code != fmt->code) {
1004 + /* We've set a format that the sensor reports
1005 + * as being supported, but it refuses to set it.
1006 + * Not much else we can do.
1007 + * Assume that the sensor driver may accept the
1008 + * format when it is set (rather than tried).
1010 + unicam_err(dev, "Sensor won't accept default format, and Unicam can't support sensor default\n");
1015 + f->fmt.pix.pixelformat = fmt->fourcc;
1017 + f->fmt.pix.pixelformat = fmt->repacked_fourcc;
1020 + return unicam_calc_format_size_bpl(dev, fmt, f);
1023 +static int unicam_s_fmt_vid_cap(struct file *file, void *priv,
1024 + struct v4l2_format *f)
1026 + struct unicam_device *dev = video_drvdata(file);
1027 + struct vb2_queue *q = &dev->buffer_queue;
1028 + struct v4l2_mbus_framefmt mbus_fmt = {0};
1029 + const struct unicam_fmt *fmt;
1032 + if (vb2_is_busy(q))
1035 + ret = unicam_try_fmt_vid_cap(file, priv, f);
1039 + fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
1041 + /* Unknown pixel format - adopt a default.
1042 + * This shouldn't happen as try_fmt should have resolved any
1045 + fmt = get_first_supported_format(dev);
1047 + /* It shouldn't be possible to get here with no
1048 + * supported formats
1051 + f->fmt.pix.pixelformat = fmt->fourcc;
1055 + v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, fmt->code);
1057 + ret = __subdev_set_format(dev, &mbus_fmt);
1059 + unicam_dbg(3, dev, "%s __subdev_set_format failed %d\n",
1064 + /* Just double check nothing has gone wrong */
1065 + if (mbus_fmt.code != fmt->code) {
1066 + unicam_dbg(3, dev,
1067 + "%s subdev changed format on us, this should not happen\n",
1073 + dev->v_fmt.fmt.pix.pixelformat = f->fmt.pix.pixelformat;
1074 + dev->v_fmt.fmt.pix.bytesperline = f->fmt.pix.bytesperline;
1075 + unicam_reset_format(dev);
1077 + unicam_dbg(3, dev, "%s %dx%d, mbus_fmt 0x%08X, V4L2 pix 0x%08X.\n",
1078 + __func__, dev->v_fmt.fmt.pix.width,
1079 + dev->v_fmt.fmt.pix.height, mbus_fmt.code,
1080 + dev->v_fmt.fmt.pix.pixelformat);
1087 +static int unicam_queue_setup(struct vb2_queue *vq,
1088 + unsigned int *nbuffers,
1089 + unsigned int *nplanes,
1090 + unsigned int sizes[],
1091 + struct device *alloc_devs[])
1093 + struct unicam_device *dev = vb2_get_drv_priv(vq);
1094 + unsigned int size = dev->v_fmt.fmt.pix.sizeimage;
1096 + if (vq->num_buffers + *nbuffers < 3)
1097 + *nbuffers = 3 - vq->num_buffers;
1100 + if (sizes[0] < size) {
1101 + unicam_err(dev, "sizes[0] %i < size %u\n", sizes[0],
1114 +static int unicam_buffer_prepare(struct vb2_buffer *vb)
1116 + struct unicam_device *dev = vb2_get_drv_priv(vb->vb2_queue);
1117 + struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
1119 + unsigned long size;
1121 + if (WARN_ON(!dev->fmt))
1124 + size = dev->v_fmt.fmt.pix.sizeimage;
1125 + if (vb2_plane_size(vb, 0) < size) {
1126 + unicam_err(dev, "data will not fit into plane (%lu < %lu)\n",
1127 + vb2_plane_size(vb, 0), size);
1131 + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
1135 +static void unicam_buffer_queue(struct vb2_buffer *vb)
1137 + struct unicam_device *dev = vb2_get_drv_priv(vb->vb2_queue);
1138 + struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
1140 + struct unicam_dmaqueue *dma_queue = &dev->dma_queue;
1141 + unsigned long flags = 0;
1143 + spin_lock_irqsave(&dev->dma_queue_lock, flags);
1144 + list_add_tail(&buf->list, &dma_queue->active);
1145 + spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
1148 +static void unicam_set_packing_config(struct unicam_device *dev)
1153 + if (dev->v_fmt.fmt.pix.pixelformat == dev->fmt->fourcc) {
1154 + unpack = UNICAM_PUM_NONE;
1155 + pack = UNICAM_PPM_NONE;
1157 + switch (dev->fmt->depth) {
1159 + unpack = UNICAM_PUM_UNPACK8;
1162 + unpack = UNICAM_PUM_UNPACK10;
1165 + unpack = UNICAM_PUM_UNPACK12;
1168 + unpack = UNICAM_PUM_UNPACK14;
1171 + unpack = UNICAM_PUM_UNPACK16;
1174 + unpack = UNICAM_PUM_NONE;
1178 + /* Repacking is always to 16bpp */
1179 + pack = UNICAM_PPM_PACK16;
1183 + set_field(&val, unpack, UNICAM_PUM_MASK);
1184 + set_field(&val, pack, UNICAM_PPM_MASK);
1185 + reg_write(&dev->cfg, UNICAM_IPIPE, val);
1188 +static void unicam_cfg_image_id(struct unicam_device *dev)
1190 + struct unicam_cfg *cfg = &dev->cfg;
1192 + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1194 + reg_write(cfg, UNICAM_IDI0,
1195 + (dev->virtual_channel << 6) | dev->fmt->csi_dt);
1198 + reg_write(cfg, UNICAM_IDI0, (0x80 | dev->fmt->csi_dt));
1202 +static void unicam_start_rx(struct unicam_device *dev, unsigned long addr)
1204 + struct unicam_cfg *cfg = &dev->cfg;
1205 + int line_int_freq = dev->v_fmt.fmt.pix.height >> 2;
1209 + if (line_int_freq < 128)
1210 + line_int_freq = 128;
1212 + /* Enable lane clocks */
1214 + for (i = 0; i < dev->active_data_lanes; i++)
1215 + val = val << 2 | 1;
1216 + clk_write(cfg, val);
1219 + reg_write(cfg, UNICAM_CTRL, UNICAM_MEM);
1221 + /* Enable analogue control, and leave in reset. */
1223 + set_field(&val, 7, UNICAM_CTATADJ_MASK);
1224 + set_field(&val, 7, UNICAM_PTATADJ_MASK);
1225 + reg_write(cfg, UNICAM_ANA, val);
1226 + usleep_range(1000, 2000);
1228 + /* Come out of reset */
1229 + reg_write_field(cfg, UNICAM_ANA, 0, UNICAM_AR);
1231 + /* Peripheral reset */
1232 + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPR);
1233 + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPR);
1235 + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPE);
1237 + /* Enable Rx control. */
1238 + val = reg_read(cfg, UNICAM_CTRL);
1239 + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1240 + set_field(&val, UNICAM_CPM_CSI2, UNICAM_CPM_MASK);
1241 + set_field(&val, UNICAM_DCM_STROBE, UNICAM_DCM_MASK);
1243 + set_field(&val, UNICAM_CPM_CCP2, UNICAM_CPM_MASK);
1244 + set_field(&val, dev->bus_flags, UNICAM_DCM_MASK);
1246 + /* Packet framer timeout */
1247 + set_field(&val, 0xf, UNICAM_PFT_MASK);
1248 + set_field(&val, 128, UNICAM_OET_MASK);
1249 + reg_write(cfg, UNICAM_CTRL, val);
1251 + reg_write(cfg, UNICAM_IHWIN, 0);
1252 + reg_write(cfg, UNICAM_IVWIN, 0);
1254 + /* AXI bus access QoS setup */
1255 + val = reg_read(&dev->cfg, UNICAM_PRI);
1256 + set_field(&val, 0, UNICAM_BL_MASK);
1257 + set_field(&val, 0, UNICAM_BS_MASK);
1258 + set_field(&val, 0xe, UNICAM_PP_MASK);
1259 + set_field(&val, 8, UNICAM_NP_MASK);
1260 + set_field(&val, 2, UNICAM_PT_MASK);
1261 + set_field(&val, 1, UNICAM_PE);
1262 + reg_write(cfg, UNICAM_PRI, val);
1264 + reg_write_field(cfg, UNICAM_ANA, 0, UNICAM_DDL);
1266 + /* Always start in trigger frame capture mode (UNICAM_FCM set) */
1267 + val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_FCM;
1268 + set_field(&val, line_int_freq, UNICAM_LCIE_MASK);
1269 + reg_write(cfg, UNICAM_ICTL, val);
1270 + reg_write(cfg, UNICAM_STA, UNICAM_STA_MASK_ALL);
1271 + reg_write(cfg, UNICAM_ISTA, UNICAM_ISTA_MASK_ALL);
1273 + /* tclk_term_en */
1274 + reg_write_field(cfg, UNICAM_CLT, 2, UNICAM_CLT1_MASK);
1276 + reg_write_field(cfg, UNICAM_CLT, 6, UNICAM_CLT2_MASK);
1278 + reg_write_field(cfg, UNICAM_DLT, 2, UNICAM_DLT1_MASK);
1280 + reg_write_field(cfg, UNICAM_DLT, 6, UNICAM_DLT2_MASK);
1282 + reg_write_field(cfg, UNICAM_DLT, 0, UNICAM_DLT3_MASK);
1284 + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_SOE);
1286 + /* Packet compare setup - required to avoid missing frame ends */
1288 + set_field(&val, 1, UNICAM_PCE);
1289 + set_field(&val, 1, UNICAM_GI);
1290 + set_field(&val, 1, UNICAM_CPH);
1291 + set_field(&val, 0, UNICAM_PCVC_MASK);
1292 + set_field(&val, 1, UNICAM_PCDT_MASK);
1293 + reg_write(cfg, UNICAM_CMP0, val);
1295 + /* Enable clock lane and set up terminations */
1297 + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1299 + set_field(&val, 1, UNICAM_CLE);
1300 + set_field(&val, 1, UNICAM_CLLPE);
1301 + if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1302 + set_field(&val, 1, UNICAM_CLTRE);
1303 + set_field(&val, 1, UNICAM_CLHSE);
1307 + set_field(&val, 1, UNICAM_CLE);
1308 + set_field(&val, 1, UNICAM_CLHSE);
1309 + set_field(&val, 1, UNICAM_CLTRE);
1311 + reg_write(cfg, UNICAM_CLK, val);
1314 + * Enable required data lanes with appropriate terminations.
1315 + * The same value needs to be written to UNICAM_DATn registers for
1316 + * the active lanes, and 0 for inactive ones.
1319 + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1321 + set_field(&val, 1, UNICAM_DLE);
1322 + set_field(&val, 1, UNICAM_DLLPE);
1323 + if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1324 + set_field(&val, 1, UNICAM_DLTRE);
1325 + set_field(&val, 1, UNICAM_DLHSE);
1329 + set_field(&val, 1, UNICAM_DLE);
1330 + set_field(&val, 1, UNICAM_DLHSE);
1331 + set_field(&val, 1, UNICAM_DLTRE);
1333 + reg_write(cfg, UNICAM_DAT0, val);
1335 + if (dev->active_data_lanes == 1)
1337 + reg_write(cfg, UNICAM_DAT1, val);
1339 + if (dev->max_data_lanes > 2) {
1341 + * Registers UNICAM_DAT2 and UNICAM_DAT3 only valid if the
1342 + * instance supports more than 2 data lanes.
1344 + if (dev->active_data_lanes == 2)
1346 + reg_write(cfg, UNICAM_DAT2, val);
1348 + if (dev->active_data_lanes == 3)
1350 + reg_write(cfg, UNICAM_DAT3, val);
1353 + reg_write(&dev->cfg, UNICAM_IBLS, dev->v_fmt.fmt.pix.bytesperline);
1354 + unicam_wr_dma_addr(dev, addr);
1355 + unicam_set_packing_config(dev);
1356 + unicam_cfg_image_id(dev);
1358 + /* Disabled embedded data */
1360 + set_field(&val, 0, UNICAM_EDL_MASK);
1361 + reg_write(cfg, UNICAM_DCS, val);
1363 + val = reg_read(cfg, UNICAM_MISC);
1364 + set_field(&val, 1, UNICAM_FL0);
1365 + set_field(&val, 1, UNICAM_FL1);
1366 + reg_write(cfg, UNICAM_MISC, val);
1368 + /* Enable peripheral */
1369 + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPE);
1371 + /* Load image pointers */
1372 + reg_write_field(cfg, UNICAM_ICTL, 1, UNICAM_LIP_MASK);
1375 + * Enable trigger only for the first frame to
1376 + * sync correctly to the FS from the source.
1378 + reg_write_field(cfg, UNICAM_ICTL, 1, UNICAM_TFC);
1381 +static void unicam_disable(struct unicam_device *dev)
1383 + struct unicam_cfg *cfg = &dev->cfg;
1385 + /* Analogue lane control disable */
1386 + reg_write_field(cfg, UNICAM_ANA, 1, UNICAM_DDL);
1388 + /* Stop the output engine */
1389 + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_SOE);
1391 + /* Disable the data lanes. */
1392 + reg_write(cfg, UNICAM_DAT0, 0);
1393 + reg_write(cfg, UNICAM_DAT1, 0);
1395 + if (dev->max_data_lanes > 2) {
1396 + reg_write(cfg, UNICAM_DAT2, 0);
1397 + reg_write(cfg, UNICAM_DAT3, 0);
1400 + /* Peripheral reset */
1401 + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPR);
1402 + usleep_range(50, 100);
1403 + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPR);
1405 + /* Disable peripheral */
1406 + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPE);
1408 + /* Disable all lane clocks */
1409 + clk_write(cfg, 0);
1412 +static int unicam_start_streaming(struct vb2_queue *vq, unsigned int count)
1414 + struct unicam_device *dev = vb2_get_drv_priv(vq);
1415 + struct unicam_dmaqueue *dma_q = &dev->dma_queue;
1416 + struct unicam_buffer *buf, *tmp;
1417 + unsigned long addr = 0;
1418 + unsigned long flags;
1421 + spin_lock_irqsave(&dev->dma_queue_lock, flags);
1422 + buf = list_entry(dma_q->active.next, struct unicam_buffer, list);
1423 + dev->cur_frm = buf;
1424 + dev->next_frm = buf;
1425 + list_del(&buf->list);
1426 + spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
1428 + addr = vb2_dma_contig_plane_dma_addr(&dev->cur_frm->vb.vb2_buf, 0);
1429 + dev->sequence = 0;
1431 + ret = unicam_runtime_get(dev);
1433 + unicam_dbg(3, dev, "unicam_runtime_get failed\n");
1434 + goto err_release_buffers;
1437 + dev->active_data_lanes = dev->max_data_lanes;
1438 + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY &&
1439 + v4l2_subdev_has_op(dev->sensor, video, g_mbus_config)) {
1440 + struct v4l2_mbus_config mbus_config;
1442 + ret = v4l2_subdev_call(dev->sensor, video, g_mbus_config,
1445 + unicam_dbg(3, dev, "g_mbus_config failed\n");
1449 + dev->active_data_lanes =
1450 + (mbus_config.flags & V4L2_MBUS_CSI2_LANE_MASK) >>
1451 + __ffs(V4L2_MBUS_CSI2_LANE_MASK);
1452 + if (!dev->active_data_lanes)
1453 + dev->active_data_lanes = dev->max_data_lanes;
1455 + if (dev->active_data_lanes > dev->max_data_lanes) {
1456 + unicam_err(dev, "Device has requested %u data lanes, which is >%u configured in DT\n",
1457 + dev->active_data_lanes, dev->max_data_lanes);
1462 + unicam_dbg(1, dev, "Running with %u data lanes\n",
1463 + dev->active_data_lanes);
1465 + ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
1467 + unicam_err(dev, "failed to set up clock\n");
1471 + ret = clk_prepare_enable(dev->clock);
1473 + unicam_err(dev, "Failed to enable CSI clock: %d\n", ret);
1476 + dev->streaming = 1;
1478 + unicam_start_rx(dev, addr);
1480 + ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1482 + unicam_err(dev, "stream on failed in subdev\n");
1483 + goto err_disable_unicam;
1488 +err_disable_unicam:
1489 + unicam_disable(dev);
1490 + clk_disable_unprepare(dev->clock);
1492 + unicam_runtime_put(dev);
1493 +err_release_buffers:
1494 + list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
1495 + list_del(&buf->list);
1496 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
1498 + if (dev->cur_frm != dev->next_frm)
1499 + vb2_buffer_done(&dev->next_frm->vb.vb2_buf,
1500 + VB2_BUF_STATE_QUEUED);
1501 + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
1502 + dev->next_frm = NULL;
1503 + dev->cur_frm = NULL;
1508 +static void unicam_stop_streaming(struct vb2_queue *vq)
1510 + struct unicam_device *dev = vb2_get_drv_priv(vq);
1511 + struct unicam_dmaqueue *dma_q = &dev->dma_queue;
1512 + struct unicam_buffer *buf, *tmp;
1513 + unsigned long flags;
1515 + if (v4l2_subdev_call(dev->sensor, video, s_stream, 0) < 0)
1516 + unicam_err(dev, "stream off failed in subdev\n");
1518 + unicam_disable(dev);
1520 + /* Release all active buffers */
1521 + spin_lock_irqsave(&dev->dma_queue_lock, flags);
1522 + list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
1523 + list_del(&buf->list);
1524 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1527 + if (dev->cur_frm == dev->next_frm) {
1528 + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1530 + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1531 + vb2_buffer_done(&dev->next_frm->vb.vb2_buf,
1532 + VB2_BUF_STATE_ERROR);
1534 + dev->cur_frm = NULL;
1535 + dev->next_frm = NULL;
1536 + spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
1538 + clk_disable_unprepare(dev->clock);
1539 + unicam_runtime_put(dev);
1542 +static int unicam_enum_input(struct file *file, void *priv,
1543 + struct v4l2_input *inp)
1545 + struct unicam_device *dev = video_drvdata(file);
1547 + if (inp->index != 0)
1550 + inp->type = V4L2_INPUT_TYPE_CAMERA;
1551 + if (v4l2_subdev_has_op(dev->sensor, video, s_dv_timings)) {
1552 + inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1554 + } else if (v4l2_subdev_has_op(dev->sensor, video, s_std)) {
1555 + inp->capabilities = V4L2_IN_CAP_STD;
1556 + if (v4l2_subdev_call(dev->sensor, video, g_tvnorms, &inp->std)
1558 + inp->std = V4L2_STD_ALL;
1560 + inp->capabilities = 0;
1563 + sprintf(inp->name, "Camera 0");
1567 +static int unicam_g_input(struct file *file, void *priv, unsigned int *i)
1574 +static int unicam_s_input(struct file *file, void *priv, unsigned int i)
1577 + * FIXME: Ideally we would like to be able to query the source
1578 + * subdevice for information over the input connectors it supports,
1579 + * and map that through in to a call to video_ops->s_routing.
1580 + * There is no infrastructure support for defining that within
1581 + * devicetree at present. Until that is implemented we can't
1582 + * map a user physical connector number to s_routing input number.
1590 +static int unicam_querystd(struct file *file, void *priv,
1593 + struct unicam_device *dev = video_drvdata(file);
1595 + return v4l2_subdev_call(dev->sensor, video, querystd, std);
1598 +static int unicam_g_std(struct file *file, void *priv, v4l2_std_id *std)
1600 + struct unicam_device *dev = video_drvdata(file);
1602 + return v4l2_subdev_call(dev->sensor, video, g_std, std);
1605 +static int unicam_s_std(struct file *file, void *priv, v4l2_std_id std)
1607 + struct unicam_device *dev = video_drvdata(file);
1609 + v4l2_std_id current_std;
1611 + ret = v4l2_subdev_call(dev->sensor, video, g_std, ¤t_std);
1615 + if (std == current_std)
1618 + if (vb2_is_busy(&dev->buffer_queue))
1621 + ret = v4l2_subdev_call(dev->sensor, video, s_std, std);
1623 + /* Force recomputation of bytesperline */
1624 + dev->v_fmt.fmt.pix.bytesperline = 0;
1626 + unicam_reset_format(dev);
1631 +static int unicam_s_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1633 + struct unicam_device *dev = video_drvdata(file);
1635 + return v4l2_subdev_call(dev->sensor, pad, set_edid, edid);
1638 +static int unicam_g_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1640 + struct unicam_device *dev = video_drvdata(file);
1642 + return v4l2_subdev_call(dev->sensor, pad, get_edid, edid);
1645 +static int unicam_enum_framesizes(struct file *file, void *priv,
1646 + struct v4l2_frmsizeenum *fsize)
1648 + struct unicam_device *dev = video_drvdata(file);
1649 + const struct unicam_fmt *fmt;
1650 + struct v4l2_subdev_frame_size_enum fse;
1653 + /* check for valid format */
1654 + fmt = find_format_by_pix(dev, fsize->pixel_format);
1656 + unicam_dbg(3, dev, "Invalid pixel code: %x\n",
1657 + fsize->pixel_format);
1661 + fse.index = fsize->index;
1663 + fse.code = fmt->code;
1665 + ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_size, NULL, &fse);
1669 + unicam_dbg(1, dev, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
1670 + __func__, fse.index, fse.code, fse.min_width, fse.max_width,
1671 + fse.min_height, fse.max_height);
1673 + fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1674 + fsize->discrete.width = fse.max_width;
1675 + fsize->discrete.height = fse.max_height;
1680 +static int unicam_enum_frameintervals(struct file *file, void *priv,
1681 + struct v4l2_frmivalenum *fival)
1683 + struct unicam_device *dev = video_drvdata(file);
1684 + const struct unicam_fmt *fmt;
1685 + struct v4l2_subdev_frame_interval_enum fie = {
1686 + .index = fival->index,
1687 + .width = fival->width,
1688 + .height = fival->height,
1689 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1693 + fmt = find_format_by_pix(dev, fival->pixel_format);
1697 + fie.code = fmt->code;
1698 + ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_interval,
1703 + fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1704 + fival->discrete = fie.interval;
1709 +static int unicam_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1711 + struct unicam_device *dev = video_drvdata(file);
1713 + return v4l2_g_parm_cap(video_devdata(file), dev->sensor, a);
1716 +static int unicam_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1718 + struct unicam_device *dev = video_drvdata(file);
1720 + return v4l2_s_parm_cap(video_devdata(file), dev->sensor, a);
1723 +static int unicam_g_dv_timings(struct file *file, void *priv,
1724 + struct v4l2_dv_timings *timings)
1726 + struct unicam_device *dev = video_drvdata(file);
1728 + return v4l2_subdev_call(dev->sensor, video, g_dv_timings, timings);
1731 +static int unicam_s_dv_timings(struct file *file, void *priv,
1732 + struct v4l2_dv_timings *timings)
1734 + struct unicam_device *dev = video_drvdata(file);
1735 + struct v4l2_dv_timings current_timings;
1738 + ret = v4l2_subdev_call(dev->sensor, video, g_dv_timings,
1739 + ¤t_timings);
1741 + if (v4l2_match_dv_timings(timings, ¤t_timings, 0, false))
1744 + if (vb2_is_busy(&dev->buffer_queue))
1747 + ret = v4l2_subdev_call(dev->sensor, video, s_dv_timings, timings);
1749 + /* Force recomputation of bytesperline */
1750 + dev->v_fmt.fmt.pix.bytesperline = 0;
1752 + unicam_reset_format(dev);
1757 +static int unicam_query_dv_timings(struct file *file, void *priv,
1758 + struct v4l2_dv_timings *timings)
1760 + struct unicam_device *dev = video_drvdata(file);
1762 + return v4l2_subdev_call(dev->sensor, video, query_dv_timings, timings);
1765 +static int unicam_enum_dv_timings(struct file *file, void *priv,
1766 + struct v4l2_enum_dv_timings *timings)
1768 + struct unicam_device *dev = video_drvdata(file);
1770 + return v4l2_subdev_call(dev->sensor, pad, enum_dv_timings, timings);
1773 +static int unicam_dv_timings_cap(struct file *file, void *priv,
1774 + struct v4l2_dv_timings_cap *cap)
1776 + struct unicam_device *dev = video_drvdata(file);
1778 + return v4l2_subdev_call(dev->sensor, pad, dv_timings_cap, cap);
1781 +static int unicam_subscribe_event(struct v4l2_fh *fh,
1782 + const struct v4l2_event_subscription *sub)
1784 + switch (sub->type) {
1785 + case V4L2_EVENT_SOURCE_CHANGE:
1786 + return v4l2_event_subscribe(fh, sub, 4, NULL);
1789 + return v4l2_ctrl_subscribe_event(fh, sub);
1792 +static int unicam_log_status(struct file *file, void *fh)
1794 + struct unicam_device *dev = video_drvdata(file);
1795 + struct unicam_cfg *cfg = &dev->cfg;
1798 + /* status for sub devices */
1799 + v4l2_device_call_all(&dev->v4l2_dev, 0, core, log_status);
1801 + unicam_info(dev, "-----Receiver status-----\n");
1802 + unicam_info(dev, "V4L2 width/height: %ux%u\n",
1803 + dev->v_fmt.fmt.pix.width, dev->v_fmt.fmt.pix.height);
1804 + unicam_info(dev, "Mediabus format: %08x\n", dev->fmt->code);
1805 + unicam_info(dev, "V4L2 format: %08x\n",
1806 + dev->v_fmt.fmt.pix.pixelformat);
1807 + reg = reg_read(&dev->cfg, UNICAM_IPIPE);
1808 + unicam_info(dev, "Unpacking/packing: %u / %u\n",
1809 + get_field(reg, UNICAM_PUM_MASK),
1810 + get_field(reg, UNICAM_PPM_MASK));
1811 + unicam_info(dev, "----Live data----\n");
1812 + unicam_info(dev, "Programmed stride: %4u\n",
1813 + reg_read(cfg, UNICAM_IBLS));
1814 + unicam_info(dev, "Detected resolution: %ux%u\n",
1815 + reg_read(cfg, UNICAM_IHSTA),
1816 + reg_read(cfg, UNICAM_IVSTA));
1817 + unicam_info(dev, "Write pointer: %08x\n",
1818 + reg_read(cfg, UNICAM_IBWP));
1823 +static void unicam_notify(struct v4l2_subdev *sd,
1824 + unsigned int notification, void *arg)
1826 + struct unicam_device *dev =
1827 + container_of(sd->v4l2_dev, struct unicam_device, v4l2_dev);
1829 + switch (notification) {
1830 + case V4L2_DEVICE_NOTIFY_EVENT:
1831 + v4l2_event_queue(&dev->video_dev, arg);
1838 +static const struct vb2_ops unicam_video_qops = {
1839 + .wait_prepare = vb2_ops_wait_prepare,
1840 + .wait_finish = vb2_ops_wait_finish,
1841 + .queue_setup = unicam_queue_setup,
1842 + .buf_prepare = unicam_buffer_prepare,
1843 + .buf_queue = unicam_buffer_queue,
1844 + .start_streaming = unicam_start_streaming,
1845 + .stop_streaming = unicam_stop_streaming,
1849 + * unicam_open : This function is based on the v4l2_fh_open helper function.
1850 + * It has been augmented to handle sensor subdevice power management,
1852 +static int unicam_open(struct file *file)
1854 + struct unicam_device *dev = video_drvdata(file);
1857 + mutex_lock(&dev->lock);
1859 + ret = v4l2_fh_open(file);
1861 + unicam_err(dev, "v4l2_fh_open failed\n");
1865 + if (!v4l2_fh_is_singular_file(file))
1868 + ret = v4l2_subdev_call(dev->sensor, core, s_power, 1);
1869 + if (ret < 0 && ret != -ENOIOCTLCMD) {
1870 + v4l2_fh_release(file);
1877 + mutex_unlock(&dev->lock);
1881 +static int unicam_release(struct file *file)
1883 + struct unicam_device *dev = video_drvdata(file);
1884 + struct v4l2_subdev *sd = dev->sensor;
1888 + mutex_lock(&dev->lock);
1890 + fh_singular = v4l2_fh_is_singular_file(file);
1892 + ret = _vb2_fop_release(file, NULL);
1895 + v4l2_subdev_call(sd, core, s_power, 0);
1897 + mutex_unlock(&dev->lock);
1902 +/* unicam capture driver file operations */
1903 +static const struct v4l2_file_operations unicam_fops = {
1904 + .owner = THIS_MODULE,
1905 + .open = unicam_open,
1906 + .release = unicam_release,
1907 + .read = vb2_fop_read,
1908 + .poll = vb2_fop_poll,
1909 + .unlocked_ioctl = video_ioctl2,
1910 + .mmap = vb2_fop_mmap,
1913 +/* unicam capture ioctl operations */
1914 +static const struct v4l2_ioctl_ops unicam_ioctl_ops = {
1915 + .vidioc_querycap = unicam_querycap,
1916 + .vidioc_enum_fmt_vid_cap = unicam_enum_fmt_vid_cap,
1917 + .vidioc_g_fmt_vid_cap = unicam_g_fmt_vid_cap,
1918 + .vidioc_s_fmt_vid_cap = unicam_s_fmt_vid_cap,
1919 + .vidioc_try_fmt_vid_cap = unicam_try_fmt_vid_cap,
1921 + .vidioc_enum_input = unicam_enum_input,
1922 + .vidioc_g_input = unicam_g_input,
1923 + .vidioc_s_input = unicam_s_input,
1925 + .vidioc_querystd = unicam_querystd,
1926 + .vidioc_s_std = unicam_s_std,
1927 + .vidioc_g_std = unicam_g_std,
1929 + .vidioc_g_edid = unicam_g_edid,
1930 + .vidioc_s_edid = unicam_s_edid,
1932 + .vidioc_enum_framesizes = unicam_enum_framesizes,
1933 + .vidioc_enum_frameintervals = unicam_enum_frameintervals,
1935 + .vidioc_g_parm = unicam_g_parm,
1936 + .vidioc_s_parm = unicam_s_parm,
1938 + .vidioc_s_dv_timings = unicam_s_dv_timings,
1939 + .vidioc_g_dv_timings = unicam_g_dv_timings,
1940 + .vidioc_query_dv_timings = unicam_query_dv_timings,
1941 + .vidioc_enum_dv_timings = unicam_enum_dv_timings,
1942 + .vidioc_dv_timings_cap = unicam_dv_timings_cap,
1944 + .vidioc_reqbufs = vb2_ioctl_reqbufs,
1945 + .vidioc_create_bufs = vb2_ioctl_create_bufs,
1946 + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1947 + .vidioc_querybuf = vb2_ioctl_querybuf,
1948 + .vidioc_qbuf = vb2_ioctl_qbuf,
1949 + .vidioc_dqbuf = vb2_ioctl_dqbuf,
1950 + .vidioc_expbuf = vb2_ioctl_expbuf,
1951 + .vidioc_streamon = vb2_ioctl_streamon,
1952 + .vidioc_streamoff = vb2_ioctl_streamoff,
1954 + .vidioc_log_status = unicam_log_status,
1955 + .vidioc_subscribe_event = unicam_subscribe_event,
1956 + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1960 +unicam_async_bound(struct v4l2_async_notifier *notifier,
1961 + struct v4l2_subdev *subdev,
1962 + struct v4l2_async_subdev *asd)
1964 + struct unicam_device *unicam = container_of(notifier->v4l2_dev,
1965 + struct unicam_device, v4l2_dev);
1967 + if (unicam->sensor) {
1968 + unicam_info(unicam, "Rejecting subdev %s (Already set!!)",
1973 + unicam->sensor = subdev;
1974 + unicam_dbg(1, unicam, "Using sensor %s for capture\n", subdev->name);
1979 +static int unicam_probe_complete(struct unicam_device *unicam)
1981 + struct video_device *vdev;
1982 + struct vb2_queue *q;
1983 + struct v4l2_mbus_framefmt mbus_fmt = {0};
1984 + const struct unicam_fmt *fmt;
1987 + v4l2_set_subdev_hostdata(unicam->sensor, unicam);
1989 + unicam->v4l2_dev.notify = unicam_notify;
1991 + unicam->sensor_config = v4l2_subdev_alloc_pad_config(unicam->sensor);
1992 + if (!unicam->sensor_config)
1995 + ret = __subdev_get_format(unicam, &mbus_fmt);
1997 + unicam_err(unicam, "Failed to get_format - ret %d\n", ret);
2001 + fmt = find_format_by_code(mbus_fmt.code);
2003 + /* Find the first format that the sensor and unicam both
2006 + fmt = get_first_supported_format(unicam);
2009 + /* No compatible formats */
2012 + mbus_fmt.code = fmt->code;
2013 + ret = __subdev_set_format(unicam, &mbus_fmt);
2017 + if (mbus_fmt.field != V4L2_FIELD_NONE) {
2018 + /* Interlaced not supported - disable it now. */
2019 + mbus_fmt.field = V4L2_FIELD_NONE;
2020 + ret = __subdev_set_format(unicam, &mbus_fmt);
2025 + unicam->fmt = fmt;
2027 + unicam->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
2029 + unicam->v_fmt.fmt.pix.pixelformat = fmt->repacked_fourcc;
2031 + /* Read current subdev format */
2032 + unicam_reset_format(unicam);
2034 + if (v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
2035 + v4l2_std_id tvnorms;
2037 + if (WARN_ON(!v4l2_subdev_has_op(unicam->sensor, video,
2040 + * Subdevice should not advertise s_std but not
2045 + ret = v4l2_subdev_call(unicam->sensor, video,
2046 + g_tvnorms, &tvnorms);
2049 + unicam->video_dev.tvnorms |= tvnorms;
2052 + spin_lock_init(&unicam->dma_queue_lock);
2053 + mutex_init(&unicam->lock);
2055 + /* Add controls from the subdevice */
2056 + ret = v4l2_ctrl_add_handler(&unicam->ctrl_handler,
2057 + unicam->sensor->ctrl_handler, NULL, true);
2061 + q = &unicam->buffer_queue;
2062 + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2063 + q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2064 + q->drv_priv = unicam;
2065 + q->ops = &unicam_video_qops;
2066 + q->mem_ops = &vb2_dma_contig_memops;
2067 + q->buf_struct_size = sizeof(struct unicam_buffer);
2068 + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2069 + q->lock = &unicam->lock;
2070 + q->min_buffers_needed = 2;
2071 + q->dev = &unicam->pdev->dev;
2073 + ret = vb2_queue_init(q);
2075 + unicam_err(unicam, "vb2_queue_init() failed\n");
2079 + INIT_LIST_HEAD(&unicam->dma_queue.active);
2081 + vdev = &unicam->video_dev;
2082 + strlcpy(vdev->name, UNICAM_MODULE_NAME, sizeof(vdev->name));
2083 + vdev->release = video_device_release_empty;
2084 + vdev->fops = &unicam_fops;
2085 + vdev->ioctl_ops = &unicam_ioctl_ops;
2086 + vdev->v4l2_dev = &unicam->v4l2_dev;
2087 + vdev->vfl_dir = VFL_DIR_RX;
2089 + vdev->lock = &unicam->lock;
2090 + vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
2091 + V4L2_CAP_READWRITE;
2093 + /* If the source has no controls then remove our ctrl handler. */
2094 + if (list_empty(&unicam->ctrl_handler.ctrls))
2095 + unicam->v4l2_dev.ctrl_handler = NULL;
2097 + video_set_drvdata(vdev, unicam);
2098 + vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
2100 + if (!v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
2101 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_STD);
2102 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_STD);
2103 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUMSTD);
2105 + if (!v4l2_subdev_has_op(unicam->sensor, video, querystd))
2106 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_QUERYSTD);
2107 + if (!v4l2_subdev_has_op(unicam->sensor, video, s_dv_timings)) {
2108 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_EDID);
2109 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_EDID);
2110 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_DV_TIMINGS_CAP);
2111 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_DV_TIMINGS);
2112 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_DV_TIMINGS);
2113 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUM_DV_TIMINGS);
2114 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_QUERY_DV_TIMINGS);
2116 + if (!v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_interval))
2117 + v4l2_disable_ioctl(&unicam->video_dev,
2118 + VIDIOC_ENUM_FRAMEINTERVALS);
2119 + if (!v4l2_subdev_has_op(unicam->sensor, video, g_frame_interval))
2120 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_PARM);
2121 + if (!v4l2_subdev_has_op(unicam->sensor, video, s_frame_interval))
2122 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_PARM);
2124 + if (!v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_size))
2125 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUM_FRAMESIZES);
2127 + ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
2129 + unicam_err(unicam, "Unable to register video device.\n");
2133 + ret = v4l2_device_register_ro_subdev_nodes(&unicam->v4l2_dev);
2135 + unicam_err(unicam,
2136 + "Unable to register subdev nodes.\n");
2137 + video_unregister_device(&unicam->video_dev);
2141 + ret = media_create_pad_link(&unicam->sensor->entity, 0,
2142 + &unicam->video_dev.entity, 0,
2143 + MEDIA_LNK_FL_ENABLED |
2144 + MEDIA_LNK_FL_IMMUTABLE);
2146 + unicam_err(unicam, "Unable to create pad links.\n");
2147 + video_unregister_device(&unicam->video_dev);
2154 +static int unicam_async_complete(struct v4l2_async_notifier *notifier)
2156 + struct unicam_device *unicam = container_of(notifier->v4l2_dev,
2157 + struct unicam_device, v4l2_dev);
2159 + return unicam_probe_complete(unicam);
2162 +static const struct v4l2_async_notifier_operations unicam_async_ops = {
2163 + .bound = unicam_async_bound,
2164 + .complete = unicam_async_complete,
2167 +static int of_unicam_connect_subdevs(struct unicam_device *dev)
2169 + struct platform_device *pdev = dev->pdev;
2170 + struct device_node *parent, *ep_node = NULL, *remote_ep = NULL,
2171 + *sensor_node = NULL;
2172 + struct v4l2_fwnode_endpoint *ep;
2173 + struct v4l2_async_subdev *asd;
2174 + unsigned int peripheral_data_lanes;
2175 + int ret = -EINVAL;
2176 + unsigned int lane;
2178 + parent = pdev->dev.of_node;
2181 + ep = &dev->endpoint;
2183 + ep_node = of_graph_get_next_endpoint(parent, NULL);
2185 + unicam_dbg(3, dev, "can't get next endpoint\n");
2186 + goto cleanup_exit;
2189 + unicam_dbg(3, dev, "ep_node is %s\n", ep_node->name);
2191 + v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), ep);
2193 + for (lane = 0; lane < ep->bus.mipi_csi2.num_data_lanes; lane++) {
2194 + if (ep->bus.mipi_csi2.data_lanes[lane] != lane + 1) {
2195 + unicam_err(dev, "Local endpoint - data lane reordering not supported\n");
2196 + goto cleanup_exit;
2200 + peripheral_data_lanes = ep->bus.mipi_csi2.num_data_lanes;
2202 + sensor_node = of_graph_get_remote_port_parent(ep_node);
2203 + if (!sensor_node) {
2204 + unicam_dbg(3, dev, "can't get remote parent\n");
2205 + goto cleanup_exit;
2207 + unicam_dbg(3, dev, "sensor_node is %s\n", sensor_node->name);
2208 + asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
2209 + asd->match.fwnode = of_fwnode_handle(sensor_node);
2211 + remote_ep = of_graph_get_remote_endpoint(ep_node);
2213 + unicam_dbg(3, dev, "can't get remote-endpoint\n");
2214 + goto cleanup_exit;
2216 + unicam_dbg(3, dev, "remote_ep is %s\n", remote_ep->name);
2217 + v4l2_fwnode_endpoint_parse(of_fwnode_handle(remote_ep), ep);
2218 + unicam_dbg(3, dev, "parsed remote_ep to endpoint. nr_of_link_frequencies %u, bus_type %u\n",
2219 + ep->nr_of_link_frequencies, ep->bus_type);
2221 + switch (ep->bus_type) {
2222 + case V4L2_MBUS_CSI2_DPHY:
2223 + if (ep->bus.mipi_csi2.num_data_lanes >
2224 + peripheral_data_lanes) {
2225 + unicam_err(dev, "Subdevice %s wants too many data lanes (%u > %u)\n",
2226 + sensor_node->name,
2227 + ep->bus.mipi_csi2.num_data_lanes,
2228 + peripheral_data_lanes);
2229 + goto cleanup_exit;
2232 + lane < ep->bus.mipi_csi2.num_data_lanes;
2234 + if (ep->bus.mipi_csi2.data_lanes[lane] != lane + 1) {
2235 + unicam_err(dev, "Subdevice %s - incompatible data lane config\n",
2236 + sensor_node->name);
2237 + goto cleanup_exit;
2240 + dev->max_data_lanes = ep->bus.mipi_csi2.num_data_lanes;
2241 + dev->bus_flags = ep->bus.mipi_csi2.flags;
2243 + case V4L2_MBUS_CCP2:
2244 + if (ep->bus.mipi_csi1.clock_lane != 0 ||
2245 + ep->bus.mipi_csi1.data_lane != 1) {
2246 + unicam_err(dev, "Subdevice %s incompatible lane config\n",
2247 + sensor_node->name);
2248 + goto cleanup_exit;
2250 + dev->max_data_lanes = 1;
2251 + dev->bus_flags = ep->bus.mipi_csi1.strobe;
2254 + /* Unsupported bus type */
2255 + unicam_err(dev, "sub-device %s is not a CSI2 or CCP2 device %d\n",
2256 + sensor_node->name, ep->bus_type);
2257 + goto cleanup_exit;
2260 + /* Store bus type - CSI2 or CCP2 */
2261 + dev->bus_type = ep->bus_type;
2262 + unicam_dbg(3, dev, "bus_type is %d\n", dev->bus_type);
2264 + /* Store Virtual Channel number */
2265 + dev->virtual_channel = ep->base.id;
2267 + unicam_dbg(3, dev, "v4l2-endpoint: %s\n",
2268 + dev->bus_type == V4L2_MBUS_CSI2_DPHY ? "CSI2" : "CCP2");
2269 + unicam_dbg(3, dev, "Virtual Channel=%d\n", dev->virtual_channel);
2270 + if (dev->bus_type == V4L2_MBUS_CSI2_DPHY)
2271 + unicam_dbg(3, dev, "flags=0x%08x\n", ep->bus.mipi_csi2.flags);
2272 + unicam_dbg(3, dev, "num_data_lanes=%d\n", dev->max_data_lanes);
2274 + unicam_dbg(1, dev, "found sub-device %s\n", sensor_node->name);
2276 + v4l2_async_notifier_init(&dev->notifier);
2278 + ret = v4l2_async_notifier_add_subdev(&dev->notifier, asd);
2280 + unicam_err(dev, "Error adding subdevice - ret %d\n", ret);
2281 + goto cleanup_exit;
2284 + dev->notifier.ops = &unicam_async_ops;
2285 + ret = v4l2_async_notifier_register(&dev->v4l2_dev,
2288 + unicam_err(dev, "Error registering async notifier - ret %d\n",
2295 + of_node_put(remote_ep);
2297 + of_node_put(sensor_node);
2299 + of_node_put(ep_node);
2304 +static int unicam_probe(struct platform_device *pdev)
2306 + struct unicam_cfg *unicam_cfg;
2307 + struct unicam_device *unicam;
2308 + struct v4l2_ctrl_handler *hdl;
2309 + struct resource *res;
2312 + unicam = devm_kzalloc(&pdev->dev, sizeof(*unicam), GFP_KERNEL);
2316 + unicam->pdev = pdev;
2317 + unicam_cfg = &unicam->cfg;
2319 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2320 + unicam_cfg->base = devm_ioremap_resource(&pdev->dev, res);
2321 + if (IS_ERR(unicam_cfg->base)) {
2322 + unicam_err(unicam, "Failed to get main io block\n");
2323 + return PTR_ERR(unicam_cfg->base);
2326 + res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2327 + unicam_cfg->clk_gate_base = devm_ioremap_resource(&pdev->dev, res);
2328 + if (IS_ERR(unicam_cfg->clk_gate_base)) {
2329 + unicam_err(unicam, "Failed to get 2nd io block\n");
2330 + return PTR_ERR(unicam_cfg->clk_gate_base);
2333 + unicam->clock = devm_clk_get(&pdev->dev, "lp");
2334 + if (IS_ERR(unicam->clock)) {
2335 + unicam_err(unicam, "Failed to get clock\n");
2336 + return PTR_ERR(unicam->clock);
2339 + ret = platform_get_irq(pdev, 0);
2341 + dev_err(&pdev->dev, "No IRQ resource\n");
2345 + ret = devm_request_irq(&pdev->dev, ret, unicam_isr, 0,
2346 + "unicam_capture0", unicam);
2348 + dev_err(&pdev->dev, "Unable to request interrupt\n");
2352 + unicam->mdev.dev = &pdev->dev;
2353 + strscpy(unicam->mdev.model, UNICAM_MODULE_NAME,
2354 + sizeof(unicam->mdev.model));
2355 + strscpy(unicam->mdev.serial, "", sizeof(unicam->mdev.serial));
2356 + snprintf(unicam->mdev.bus_info, sizeof(unicam->mdev.bus_info),
2358 + pdev->dev.driver->name, dev_name(&pdev->dev));
2359 + unicam->mdev.hw_revision = 1;
2361 + media_entity_pads_init(&unicam->video_dev.entity, 1, &unicam->pad);
2362 + media_device_init(&unicam->mdev);
2364 + unicam->v4l2_dev.mdev = &unicam->mdev;
2366 + ret = v4l2_device_register(&pdev->dev, &unicam->v4l2_dev);
2368 + unicam_err(unicam,
2369 + "Unable to register v4l2 device.\n");
2370 + goto media_cleanup;
2373 + ret = media_device_register(&unicam->mdev);
2375 + unicam_err(unicam,
2376 + "Unable to register media-controller device.\n");
2377 + goto probe_out_v4l2_unregister;
2380 + /* Reserve space for the controls */
2381 + hdl = &unicam->ctrl_handler;
2382 + ret = v4l2_ctrl_handler_init(hdl, 16);
2384 + goto media_unregister;
2385 + unicam->v4l2_dev.ctrl_handler = hdl;
2387 + /* set the driver data in platform device */
2388 + platform_set_drvdata(pdev, unicam);
2390 + ret = of_unicam_connect_subdevs(unicam);
2392 + dev_err(&pdev->dev, "Failed to connect subdevs\n");
2396 + /* Enable the block power domain */
2397 + pm_runtime_enable(&pdev->dev);
2402 + v4l2_ctrl_handler_free(hdl);
2404 + media_device_unregister(&unicam->mdev);
2405 +probe_out_v4l2_unregister:
2406 + v4l2_device_unregister(&unicam->v4l2_dev);
2408 + media_device_cleanup(&unicam->mdev);
2413 +static int unicam_remove(struct platform_device *pdev)
2415 + struct unicam_device *unicam = platform_get_drvdata(pdev);
2417 + unicam_dbg(2, unicam, "%s\n", __func__);
2419 + pm_runtime_disable(&pdev->dev);
2421 + v4l2_async_notifier_unregister(&unicam->notifier);
2422 + v4l2_ctrl_handler_free(&unicam->ctrl_handler);
2423 + v4l2_device_unregister(&unicam->v4l2_dev);
2424 + video_unregister_device(&unicam->video_dev);
2425 + if (unicam->sensor_config)
2426 + v4l2_subdev_free_pad_config(unicam->sensor_config);
2427 + media_device_unregister(&unicam->mdev);
2428 + media_device_cleanup(&unicam->mdev);
2433 +static const struct of_device_id unicam_of_match[] = {
2434 + { .compatible = "brcm,bcm2835-unicam", },
2435 + { /* sentinel */ },
2437 +MODULE_DEVICE_TABLE(of, unicam_of_match);
2439 +static struct platform_driver unicam_driver = {
2440 + .probe = unicam_probe,
2441 + .remove = unicam_remove,
2443 + .name = UNICAM_MODULE_NAME,
2444 + .of_match_table = of_match_ptr(unicam_of_match),
2448 +module_platform_driver(unicam_driver);
2450 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
2451 +MODULE_DESCRIPTION("BCM2835 Unicam driver");
2452 +MODULE_LICENSE("GPL");
2453 +MODULE_VERSION(UNICAM_VERSION);
2455 +++ b/drivers/media/platform/bcm2835/vc4-regs-unicam.h
2457 +/* SPDX-License-Identifier: GPL-2.0-only */
2460 + * Copyright (C) 2017-2020 Raspberry Pi Trading.
2461 + * Dave Stevenson <dave.stevenson@raspberrypi.com>
2464 +#ifndef VC4_REGS_UNICAM_H
2465 +#define VC4_REGS_UNICAM_H
2468 + * The following values are taken from files found within the code drop
2469 + * made by Broadcom for the BCM21553 Graphics Driver, predominantly in
2470 + * brcm_usrlib/dag/vmcsx/vcinclude/hardware_vc4.h.
2471 + * They have been modified to be only the register offset.
2473 +#define UNICAM_CTRL 0x000
2474 +#define UNICAM_STA 0x004
2475 +#define UNICAM_ANA 0x008
2476 +#define UNICAM_PRI 0x00c
2477 +#define UNICAM_CLK 0x010
2478 +#define UNICAM_CLT 0x014
2479 +#define UNICAM_DAT0 0x018
2480 +#define UNICAM_DAT1 0x01c
2481 +#define UNICAM_DAT2 0x020
2482 +#define UNICAM_DAT3 0x024
2483 +#define UNICAM_DLT 0x028
2484 +#define UNICAM_CMP0 0x02c
2485 +#define UNICAM_CMP1 0x030
2486 +#define UNICAM_CAP0 0x034
2487 +#define UNICAM_CAP1 0x038
2488 +#define UNICAM_ICTL 0x100
2489 +#define UNICAM_ISTA 0x104
2490 +#define UNICAM_IDI0 0x108
2491 +#define UNICAM_IPIPE 0x10c
2492 +#define UNICAM_IBSA0 0x110
2493 +#define UNICAM_IBEA0 0x114
2494 +#define UNICAM_IBLS 0x118
2495 +#define UNICAM_IBWP 0x11c
2496 +#define UNICAM_IHWIN 0x120
2497 +#define UNICAM_IHSTA 0x124
2498 +#define UNICAM_IVWIN 0x128
2499 +#define UNICAM_IVSTA 0x12c
2500 +#define UNICAM_ICC 0x130
2501 +#define UNICAM_ICS 0x134
2502 +#define UNICAM_IDC 0x138
2503 +#define UNICAM_IDPO 0x13c
2504 +#define UNICAM_IDCA 0x140
2505 +#define UNICAM_IDCD 0x144
2506 +#define UNICAM_IDS 0x148
2507 +#define UNICAM_DCS 0x200
2508 +#define UNICAM_DBSA0 0x204
2509 +#define UNICAM_DBEA0 0x208
2510 +#define UNICAM_DBWP 0x20c
2511 +#define UNICAM_DBCTL 0x300
2512 +#define UNICAM_IBSA1 0x304
2513 +#define UNICAM_IBEA1 0x308
2514 +#define UNICAM_IDI1 0x30c
2515 +#define UNICAM_DBSA1 0x310
2516 +#define UNICAM_DBEA1 0x314
2517 +#define UNICAM_MISC 0x400
2520 + * The following bitmasks are from the kernel released by Broadcom
2521 + * for Android - https://android.googlesource.com/kernel/bcm/
2522 + * The Rhea, Hawaii, and Java chips all contain the same VideoCore4
2523 + * Unicam block as BCM2835, as defined in eg
2524 + * arch/arm/mach-rhea/include/mach/rdb_A0/brcm_rdb_cam.h and similar.
2525 + * Values reworked to use the kernel BIT and GENMASK macros.
2527 + * Some of the bit mnenomics have been amended to match the datasheet.
2529 +/* UNICAM_CTRL Register */
2530 +#define UNICAM_CPE BIT(0)
2531 +#define UNICAM_MEM BIT(1)
2532 +#define UNICAM_CPR BIT(2)
2533 +#define UNICAM_CPM_MASK GENMASK(3, 3)
2534 +#define UNICAM_CPM_CSI2 0
2535 +#define UNICAM_CPM_CCP2 1
2536 +#define UNICAM_SOE BIT(4)
2537 +#define UNICAM_DCM_MASK GENMASK(5, 5)
2538 +#define UNICAM_DCM_STROBE 0
2539 +#define UNICAM_DCM_DATA 1
2540 +#define UNICAM_SLS BIT(6)
2541 +#define UNICAM_PFT_MASK GENMASK(11, 8)
2542 +#define UNICAM_OET_MASK GENMASK(20, 12)
2544 +/* UNICAM_STA Register */
2545 +#define UNICAM_SYN BIT(0)
2546 +#define UNICAM_CS BIT(1)
2547 +#define UNICAM_SBE BIT(2)
2548 +#define UNICAM_PBE BIT(3)
2549 +#define UNICAM_HOE BIT(4)
2550 +#define UNICAM_PLE BIT(5)
2551 +#define UNICAM_SSC BIT(6)
2552 +#define UNICAM_CRCE BIT(7)
2553 +#define UNICAM_OES BIT(8)
2554 +#define UNICAM_IFO BIT(9)
2555 +#define UNICAM_OFO BIT(10)
2556 +#define UNICAM_BFO BIT(11)
2557 +#define UNICAM_DL BIT(12)
2558 +#define UNICAM_PS BIT(13)
2559 +#define UNICAM_IS BIT(14)
2560 +#define UNICAM_PI0 BIT(15)
2561 +#define UNICAM_PI1 BIT(16)
2562 +#define UNICAM_FSI_S BIT(17)
2563 +#define UNICAM_FEI_S BIT(18)
2564 +#define UNICAM_LCI_S BIT(19)
2565 +#define UNICAM_BUF0_RDY BIT(20)
2566 +#define UNICAM_BUF0_NO BIT(21)
2567 +#define UNICAM_BUF1_RDY BIT(22)
2568 +#define UNICAM_BUF1_NO BIT(23)
2569 +#define UNICAM_DI BIT(24)
2571 +#define UNICAM_STA_MASK_ALL \
2585 +/* UNICAM_ANA Register */
2586 +#define UNICAM_APD BIT(0)
2587 +#define UNICAM_BPD BIT(1)
2588 +#define UNICAM_AR BIT(2)
2589 +#define UNICAM_DDL BIT(3)
2590 +#define UNICAM_CTATADJ_MASK GENMASK(7, 4)
2591 +#define UNICAM_PTATADJ_MASK GENMASK(11, 8)
2593 +/* UNICAM_PRI Register */
2594 +#define UNICAM_PE BIT(0)
2595 +#define UNICAM_PT_MASK GENMASK(2, 1)
2596 +#define UNICAM_NP_MASK GENMASK(7, 4)
2597 +#define UNICAM_PP_MASK GENMASK(11, 8)
2598 +#define UNICAM_BS_MASK GENMASK(15, 12)
2599 +#define UNICAM_BL_MASK GENMASK(17, 16)
2601 +/* UNICAM_CLK Register */
2602 +#define UNICAM_CLE BIT(0)
2603 +#define UNICAM_CLPD BIT(1)
2604 +#define UNICAM_CLLPE BIT(2)
2605 +#define UNICAM_CLHSE BIT(3)
2606 +#define UNICAM_CLTRE BIT(4)
2607 +#define UNICAM_CLAC_MASK GENMASK(8, 5)
2608 +#define UNICAM_CLSTE BIT(29)
2610 +/* UNICAM_CLT Register */
2611 +#define UNICAM_CLT1_MASK GENMASK(7, 0)
2612 +#define UNICAM_CLT2_MASK GENMASK(15, 8)
2614 +/* UNICAM_DATn Registers */
2615 +#define UNICAM_DLE BIT(0)
2616 +#define UNICAM_DLPD BIT(1)
2617 +#define UNICAM_DLLPE BIT(2)
2618 +#define UNICAM_DLHSE BIT(3)
2619 +#define UNICAM_DLTRE BIT(4)
2620 +#define UNICAM_DLSM BIT(5)
2621 +#define UNICAM_DLFO BIT(28)
2622 +#define UNICAM_DLSTE BIT(29)
2624 +#define UNICAM_DAT_MASK_ALL (UNICAM_DLSTE + UNICAM_DLFO)
2626 +/* UNICAM_DLT Register */
2627 +#define UNICAM_DLT1_MASK GENMASK(7, 0)
2628 +#define UNICAM_DLT2_MASK GENMASK(15, 8)
2629 +#define UNICAM_DLT3_MASK GENMASK(23, 16)
2631 +/* UNICAM_ICTL Register */
2632 +#define UNICAM_FSIE BIT(0)
2633 +#define UNICAM_FEIE BIT(1)
2634 +#define UNICAM_IBOB BIT(2)
2635 +#define UNICAM_FCM BIT(3)
2636 +#define UNICAM_TFC BIT(4)
2637 +#define UNICAM_LIP_MASK GENMASK(6, 5)
2638 +#define UNICAM_LCIE_MASK GENMASK(28, 16)
2640 +/* UNICAM_IDI0/1 Register */
2641 +#define UNICAM_ID0_MASK GENMASK(7, 0)
2642 +#define UNICAM_ID1_MASK GENMASK(15, 8)
2643 +#define UNICAM_ID2_MASK GENMASK(23, 16)
2644 +#define UNICAM_ID3_MASK GENMASK(31, 24)
2646 +/* UNICAM_ISTA Register */
2647 +#define UNICAM_FSI BIT(0)
2648 +#define UNICAM_FEI BIT(1)
2649 +#define UNICAM_LCI BIT(2)
2651 +#define UNICAM_ISTA_MASK_ALL (UNICAM_FSI + UNICAM_FEI + UNICAM_LCI)
2653 +/* UNICAM_IPIPE Register */
2654 +#define UNICAM_PUM_MASK GENMASK(2, 0)
2655 + /* Unpacking modes */
2656 + #define UNICAM_PUM_NONE 0
2657 + #define UNICAM_PUM_UNPACK6 1
2658 + #define UNICAM_PUM_UNPACK7 2
2659 + #define UNICAM_PUM_UNPACK8 3
2660 + #define UNICAM_PUM_UNPACK10 4
2661 + #define UNICAM_PUM_UNPACK12 5
2662 + #define UNICAM_PUM_UNPACK14 6
2663 + #define UNICAM_PUM_UNPACK16 7
2664 +#define UNICAM_DDM_MASK GENMASK(6, 3)
2665 +#define UNICAM_PPM_MASK GENMASK(9, 7)
2666 + /* Packing modes */
2667 + #define UNICAM_PPM_NONE 0
2668 + #define UNICAM_PPM_PACK8 1
2669 + #define UNICAM_PPM_PACK10 2
2670 + #define UNICAM_PPM_PACK12 3
2671 + #define UNICAM_PPM_PACK14 4
2672 + #define UNICAM_PPM_PACK16 5
2673 +#define UNICAM_DEM_MASK GENMASK(11, 10)
2674 +#define UNICAM_DEBL_MASK GENMASK(14, 12)
2675 +#define UNICAM_ICM_MASK GENMASK(16, 15)
2676 +#define UNICAM_IDM_MASK GENMASK(17, 17)
2678 +/* UNICAM_ICC Register */
2679 +#define UNICAM_ICFL_MASK GENMASK(4, 0)
2680 +#define UNICAM_ICFH_MASK GENMASK(9, 5)
2681 +#define UNICAM_ICST_MASK GENMASK(12, 10)
2682 +#define UNICAM_ICLT_MASK GENMASK(15, 13)
2683 +#define UNICAM_ICLL_MASK GENMASK(31, 16)
2685 +/* UNICAM_DCS Register */
2686 +#define UNICAM_DIE BIT(0)
2687 +#define UNICAM_DIM BIT(1)
2688 +#define UNICAM_DBOB BIT(3)
2689 +#define UNICAM_FDE BIT(4)
2690 +#define UNICAM_LDP BIT(5)
2691 +#define UNICAM_EDL_MASK GENMASK(15, 8)
2693 +/* UNICAM_DBCTL Register */
2694 +#define UNICAM_DBEN BIT(0)
2695 +#define UNICAM_BUF0_IE BIT(1)
2696 +#define UNICAM_BUF1_IE BIT(2)
2698 +/* UNICAM_CMP[0,1] register */
2699 +#define UNICAM_PCE BIT(31)
2700 +#define UNICAM_GI BIT(9)
2701 +#define UNICAM_CPH BIT(8)
2702 +#define UNICAM_PCVC_MASK GENMASK(7, 6)
2703 +#define UNICAM_PCDT_MASK GENMASK(5, 0)
2705 +/* UNICAM_MISC register */
2706 +#define UNICAM_FL0 BIT(6)
2707 +#define UNICAM_FL1 BIT(9)