1 From 606a02e0617f3adbcdd7aa82913771403d137269 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Tue, 23 Jun 2020 10:05:57 +0100
4 Subject: [PATCH] drivers: media: Remove the downstream version of
7 About to be replaced by the upstream version.
9 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
11 drivers/media/platform/bcm2835/Kconfig | 14 -
12 drivers/media/platform/bcm2835/Makefile | 3 -
13 .../media/platform/bcm2835/bcm2835-unicam.c | 2873 -----------------
14 .../media/platform/bcm2835/vc4-regs-unicam.h | 253 --
15 4 files changed, 3143 deletions(-)
16 delete mode 100644 drivers/media/platform/bcm2835/Kconfig
17 delete mode 100644 drivers/media/platform/bcm2835/Makefile
18 delete mode 100644 drivers/media/platform/bcm2835/bcm2835-unicam.c
19 delete mode 100644 drivers/media/platform/bcm2835/vc4-regs-unicam.h
21 --- a/drivers/media/platform/bcm2835/Kconfig
24 -# Broadcom VideoCore4 V4L2 camera support
26 -config VIDEO_BCM2835_UNICAM
27 - tristate "Broadcom BCM2835 Unicam video capture driver"
28 - depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && MEDIA_CONTROLLER
29 - depends on ARCH_BCM2835 || COMPILE_TEST
30 - select VIDEOBUF2_DMA_CONTIG
33 - Say Y here to enable V4L2 subdevice for CSI2 receiver.
34 - This is a V4L2 subdevice that interfaces directly to the VC4 peripheral.
36 - To compile this driver as a module, choose M here. The module
37 - will be called bcm2835-unicam.
38 --- a/drivers/media/platform/bcm2835/Makefile
41 -# Makefile for BCM2835 Unicam driver
43 -obj-$(CONFIG_VIDEO_BCM2835_UNICAM) += bcm2835-unicam.o
44 --- a/drivers/media/platform/bcm2835/bcm2835-unicam.c
47 -// SPDX-License-Identifier: GPL-2.0-only
49 - * BCM2835 Unicam Capture Driver
51 - * Copyright (C) 2017-2020 - Raspberry Pi (Trading) Ltd.
53 - * Dave Stevenson <dave.stevenson@raspberrypi.com>
55 - * Based on TI am437x driver by
56 - * Benoit Parrot <bparrot@ti.com>
57 - * Lad, Prabhakar <prabhakar.csengg@gmail.com>
59 - * and TI CAL camera interface driver by
60 - * Benoit Parrot <bparrot@ti.com>
63 - * There are two camera drivers in the kernel for BCM283x - this one
64 - * and bcm2835-camera (currently in staging).
66 - * This driver directly controls the Unicam peripheral - there is no
67 - * involvement with the VideoCore firmware. Unicam receives CSI-2 or
68 - * CCP2 data and writes it into SDRAM.
69 - * The only potential processing options are to repack Bayer data into an
70 - * alternate format, and applying windowing.
71 - * The repacking does not shift the data, so can repack V4L2_PIX_FMT_Sxxxx10P
72 - * to V4L2_PIX_FMT_Sxxxx10, or V4L2_PIX_FMT_Sxxxx12P to V4L2_PIX_FMT_Sxxxx12,
73 - * but not generically up to V4L2_PIX_FMT_Sxxxx16. The driver will add both
74 - * formats where the relevant formats are defined, and will automatically
75 - * configure the repacking as required.
76 - * Support for windowing may be added later.
78 - * It should be possible to connect this driver to any sensor with a
79 - * suitable output interface and V4L2 subdevice driver.
81 - * bcm2835-camera uses the VideoCore firmware to control the sensor,
82 - * Unicam, ISP, and all tuner control loops. Fully processed frames are
83 - * delivered to the driver by the firmware. It only has sensor drivers
84 - * for Omnivision OV5647, and Sony IMX219 sensors.
86 - * The two drivers are mutually exclusive for the same Unicam instance.
87 - * The VideoCore firmware checks the device tree configuration during boot.
88 - * If it finds device tree nodes called csi0 or csi1 it will block the
89 - * firmware from accessing the peripheral, and bcm2835-camera will
90 - * not be able to stream data.
93 -#include <linux/clk.h>
94 -#include <linux/delay.h>
95 -#include <linux/device.h>
96 -#include <linux/dma-mapping.h>
97 -#include <linux/err.h>
98 -#include <linux/init.h>
99 -#include <linux/interrupt.h>
100 -#include <linux/io.h>
101 -#include <linux/module.h>
102 -#include <linux/of_device.h>
103 -#include <linux/of_graph.h>
104 -#include <linux/pinctrl/consumer.h>
105 -#include <linux/platform_device.h>
106 -#include <linux/pm_runtime.h>
107 -#include <linux/slab.h>
108 -#include <linux/uaccess.h>
109 -#include <linux/videodev2.h>
111 -#include <media/v4l2-common.h>
112 -#include <media/v4l2-ctrls.h>
113 -#include <media/v4l2-dev.h>
114 -#include <media/v4l2-device.h>
115 -#include <media/v4l2-dv-timings.h>
116 -#include <media/v4l2-event.h>
117 -#include <media/v4l2-ioctl.h>
118 -#include <media/v4l2-fwnode.h>
119 -#include <media/videobuf2-dma-contig.h>
121 -#include "vc4-regs-unicam.h"
123 -#define UNICAM_MODULE_NAME "unicam"
124 -#define UNICAM_VERSION "0.1.0"
127 -module_param(debug, int, 0644);
128 -MODULE_PARM_DESC(debug, "Debug level 0-3");
130 -#define unicam_dbg(level, dev, fmt, arg...) \
131 - v4l2_dbg(level, debug, &(dev)->v4l2_dev, fmt, ##arg)
132 -#define unicam_info(dev, fmt, arg...) \
133 - v4l2_info(&(dev)->v4l2_dev, fmt, ##arg)
134 -#define unicam_err(dev, fmt, arg...) \
135 - v4l2_err(&(dev)->v4l2_dev, fmt, ##arg)
137 -/* To protect against a dodgy sensor driver never returning an error from
138 - * enum_mbus_code, set a maximum index value to be used.
140 -#define MAX_ENUM_MBUS_CODE 128
143 - * Stride is a 16 bit register, but also has to be a multiple of 32.
145 -#define BPL_ALIGNMENT 32
146 -#define MAX_BYTESPERLINE ((1 << 16) - BPL_ALIGNMENT)
148 - * Max width is therefore determined by the max stride divided by
149 - * the number of bits per pixel. Take 32bpp as a
151 - * No imposed limit on the height, so adopt a square image for want
152 - * of anything better.
154 -#define MAX_WIDTH (MAX_BYTESPERLINE / 4)
155 -#define MAX_HEIGHT MAX_WIDTH
156 -/* Define a nominal minimum image size */
157 -#define MIN_WIDTH 16
158 -#define MIN_HEIGHT 16
159 -/* Default size of the embedded buffer */
160 -#define UNICAM_EMBEDDED_SIZE 8192
163 - * Size of the dummy buffer. Can be any size really, but the DMA
164 - * allocation works in units of page sizes.
166 -#define DUMMY_BUF_SIZE (PAGE_SIZE)
175 - * struct unicam_fmt - Unicam media bus format information
176 - * @pixelformat: V4L2 pixel format FCC identifier. 0 if n/a.
177 - * @repacked_fourcc: V4L2 pixel format FCC identifier if the data is expanded
178 - * out to 16bpp. 0 if n/a.
179 - * @code: V4L2 media bus format code.
180 - * @depth: Bits per pixel as delivered from the source.
181 - * @csi_dt: CSI data type.
182 - * @check_variants: Flag to denote that there are multiple mediabus formats
183 - * still in the list that could match this V4L2 format.
187 - u32 repacked_fourcc;
194 -static const struct unicam_fmt formats[] = {
197 - .fourcc = V4L2_PIX_FMT_YUYV,
198 - .code = MEDIA_BUS_FMT_YUYV8_2X8,
201 - .check_variants = 1,
203 - .fourcc = V4L2_PIX_FMT_UYVY,
204 - .code = MEDIA_BUS_FMT_UYVY8_2X8,
207 - .check_variants = 1,
209 - .fourcc = V4L2_PIX_FMT_YVYU,
210 - .code = MEDIA_BUS_FMT_YVYU8_2X8,
213 - .check_variants = 1,
215 - .fourcc = V4L2_PIX_FMT_VYUY,
216 - .code = MEDIA_BUS_FMT_VYUY8_2X8,
219 - .check_variants = 1,
221 - .fourcc = V4L2_PIX_FMT_YUYV,
222 - .code = MEDIA_BUS_FMT_YUYV8_1X16,
226 - .fourcc = V4L2_PIX_FMT_UYVY,
227 - .code = MEDIA_BUS_FMT_UYVY8_1X16,
231 - .fourcc = V4L2_PIX_FMT_YVYU,
232 - .code = MEDIA_BUS_FMT_YVYU8_1X16,
236 - .fourcc = V4L2_PIX_FMT_VYUY,
237 - .code = MEDIA_BUS_FMT_VYUY8_1X16,
242 - .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
243 - .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
247 - .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
248 - .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
252 - .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
253 - .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
257 - .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
258 - .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
262 - .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
263 - .code = MEDIA_BUS_FMT_RGB888_1X24,
267 - .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
268 - .code = MEDIA_BUS_FMT_BGR888_1X24,
272 - .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
273 - .code = MEDIA_BUS_FMT_ARGB8888_1X32,
277 - /* Bayer Formats */
278 - .fourcc = V4L2_PIX_FMT_SBGGR8,
279 - .code = MEDIA_BUS_FMT_SBGGR8_1X8,
283 - .fourcc = V4L2_PIX_FMT_SGBRG8,
284 - .code = MEDIA_BUS_FMT_SGBRG8_1X8,
288 - .fourcc = V4L2_PIX_FMT_SGRBG8,
289 - .code = MEDIA_BUS_FMT_SGRBG8_1X8,
293 - .fourcc = V4L2_PIX_FMT_SRGGB8,
294 - .code = MEDIA_BUS_FMT_SRGGB8_1X8,
298 - .fourcc = V4L2_PIX_FMT_SBGGR10P,
299 - .repacked_fourcc = V4L2_PIX_FMT_SBGGR10,
300 - .code = MEDIA_BUS_FMT_SBGGR10_1X10,
304 - .fourcc = V4L2_PIX_FMT_SGBRG10P,
305 - .repacked_fourcc = V4L2_PIX_FMT_SGBRG10,
306 - .code = MEDIA_BUS_FMT_SGBRG10_1X10,
310 - .fourcc = V4L2_PIX_FMT_SGRBG10P,
311 - .repacked_fourcc = V4L2_PIX_FMT_SGRBG10,
312 - .code = MEDIA_BUS_FMT_SGRBG10_1X10,
316 - .fourcc = V4L2_PIX_FMT_SRGGB10P,
317 - .repacked_fourcc = V4L2_PIX_FMT_SRGGB10,
318 - .code = MEDIA_BUS_FMT_SRGGB10_1X10,
322 - .fourcc = V4L2_PIX_FMT_SBGGR12P,
323 - .repacked_fourcc = V4L2_PIX_FMT_SBGGR12,
324 - .code = MEDIA_BUS_FMT_SBGGR12_1X12,
328 - .fourcc = V4L2_PIX_FMT_SGBRG12P,
329 - .repacked_fourcc = V4L2_PIX_FMT_SGBRG12,
330 - .code = MEDIA_BUS_FMT_SGBRG12_1X12,
334 - .fourcc = V4L2_PIX_FMT_SGRBG12P,
335 - .repacked_fourcc = V4L2_PIX_FMT_SGRBG12,
336 - .code = MEDIA_BUS_FMT_SGRBG12_1X12,
340 - .fourcc = V4L2_PIX_FMT_SRGGB12P,
341 - .repacked_fourcc = V4L2_PIX_FMT_SRGGB12,
342 - .code = MEDIA_BUS_FMT_SRGGB12_1X12,
346 - .fourcc = V4L2_PIX_FMT_SBGGR14P,
347 - .code = MEDIA_BUS_FMT_SBGGR14_1X14,
351 - .fourcc = V4L2_PIX_FMT_SGBRG14P,
352 - .code = MEDIA_BUS_FMT_SGBRG14_1X14,
356 - .fourcc = V4L2_PIX_FMT_SGRBG14P,
357 - .code = MEDIA_BUS_FMT_SGRBG14_1X14,
361 - .fourcc = V4L2_PIX_FMT_SRGGB14P,
362 - .code = MEDIA_BUS_FMT_SRGGB14_1X14,
367 - * 16 bit Bayer formats could be supported, but there is no CSI2
368 - * data_type defined for raw 16, and no sensors that produce it at
372 - /* Greyscale formats */
373 - .fourcc = V4L2_PIX_FMT_GREY,
374 - .code = MEDIA_BUS_FMT_Y8_1X8,
378 - .fourcc = V4L2_PIX_FMT_Y10P,
379 - .repacked_fourcc = V4L2_PIX_FMT_Y10,
380 - .code = MEDIA_BUS_FMT_Y10_1X10,
384 - /* NB There is no packed V4L2 fourcc for this format. */
385 - .repacked_fourcc = V4L2_PIX_FMT_Y12,
386 - .code = MEDIA_BUS_FMT_Y12_1X12,
390 - /* Embedded data format */
392 - .fourcc = V4L2_META_FMT_SENSOR_DATA,
393 - .code = MEDIA_BUS_FMT_SENSOR_DATA,
398 -struct unicam_dmaqueue {
399 - struct list_head active;
402 -struct unicam_buffer {
403 - struct vb2_v4l2_buffer vb;
404 - struct list_head list;
408 - /* peripheral base address */
409 - void __iomem *base;
410 - /* clock gating base address */
411 - void __iomem *clk_gate_base;
414 -#define MAX_POSSIBLE_PIX_FMTS (ARRAY_SIZE(formats))
416 -struct unicam_node {
420 - unsigned int pad_id;
421 - /* Pointer pointing to current v4l2_buffer */
422 - struct unicam_buffer *cur_frm;
423 - /* Pointer pointing to next v4l2_buffer */
424 - struct unicam_buffer *next_frm;
425 - /* video capture */
426 - const struct unicam_fmt *fmt;
427 - /* Used to store current pixel format */
428 - struct v4l2_format v_fmt;
429 - /* Used to store current mbus frame format */
430 - struct v4l2_mbus_framefmt m_fmt;
431 - /* Buffer queue used in video-buf */
432 - struct vb2_queue buffer_queue;
433 - /* Queue of filled frames */
434 - struct unicam_dmaqueue dma_queue;
435 - /* IRQ lock for DMA queue */
436 - spinlock_t dma_queue_lock;
437 - /* lock used to access this structure */
439 - /* Identifies video device for this channel */
440 - struct video_device video_dev;
441 - /* Pointer to the parent handle */
442 - struct unicam_device *dev;
443 - struct media_pad pad;
444 - struct v4l2_ctrl_handler ctrl_handler;
445 - unsigned int embedded_lines;
447 - * Dummy buffer intended to be used by unicam
448 - * if we have no other queued buffers to swap to.
450 - void *dummy_buf_cpu_addr;
451 - dma_addr_t dummy_buf_dma_addr;
454 -struct unicam_device {
455 - /* V4l2 specific parameters */
457 - struct v4l2_fwnode_endpoint endpoint;
459 - struct v4l2_async_subdev asd;
462 - struct unicam_cfg cfg;
466 - struct v4l2_device v4l2_dev;
467 - struct media_device mdev;
469 - /* parent device */
470 - struct platform_device *pdev;
471 - /* subdevice async Notifier */
472 - struct v4l2_async_notifier notifier;
473 - unsigned int sequence;
475 - /* ptr to sub device */
476 - struct v4l2_subdev *sensor;
477 - /* Pad config for the sensor */
478 - struct v4l2_subdev_pad_config *sensor_config;
480 - unsigned int virtual_channel;
481 - enum v4l2_mbus_type bus_type;
483 - * Stores bus.mipi_csi2.flags for CSI2 sensors, or
484 - * bus.mipi_csi1.strobe for CCP2.
486 - unsigned int bus_flags;
487 - unsigned int max_data_lanes;
488 - unsigned int active_data_lanes;
489 - bool sensor_embedded_data;
491 - struct unicam_node node[MAX_NODES];
494 -/* Hardware access */
495 -#define clk_write(dev, val) writel((val) | 0x5a000000, (dev)->clk_gate_base)
496 -#define clk_read(dev) readl((dev)->clk_gate_base)
498 -#define reg_read(dev, offset) readl((dev)->base + (offset))
499 -#define reg_write(dev, offset, val) writel(val, (dev)->base + (offset))
501 -#define reg_read_field(dev, offset, mask) get_field(reg_read((dev), (offset), \
504 -static inline int get_field(u32 value, u32 mask)
506 - return (value & mask) >> __ffs(mask);
509 -static inline void set_field(u32 *valp, u32 field, u32 mask)
514 - val |= (field << __ffs(mask)) & mask;
518 -static inline void reg_write_field(struct unicam_cfg *dev, u32 offset,
519 - u32 field, u32 mask)
521 - u32 val = reg_read((dev), (offset));
523 - set_field(&val, field, mask);
524 - reg_write((dev), (offset), val);
527 -/* Power management functions */
528 -static inline int unicam_runtime_get(struct unicam_device *dev)
530 - return pm_runtime_get_sync(&dev->pdev->dev);
533 -static inline void unicam_runtime_put(struct unicam_device *dev)
535 - pm_runtime_put_sync(&dev->pdev->dev);
538 -/* Format setup functions */
539 -static const struct unicam_fmt *find_format_by_code(u32 code)
543 - for (i = 0; i < ARRAY_SIZE(formats); i++) {
544 - if (formats[i].code == code)
545 - return &formats[i];
551 -static int check_mbus_format(struct unicam_device *dev,
552 - const struct unicam_fmt *format)
554 - struct v4l2_subdev_mbus_code_enum mbus_code;
558 - for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
559 - memset(&mbus_code, 0, sizeof(mbus_code));
560 - mbus_code.index = i;
561 - mbus_code.pad = IMAGE_PAD;
562 - mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
564 - ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
567 - if (!ret && mbus_code.code == format->code)
574 -static const struct unicam_fmt *find_format_by_pix(struct unicam_device *dev,
579 - for (i = 0; i < ARRAY_SIZE(formats); i++) {
580 - if (formats[i].fourcc == pixelformat ||
581 - formats[i].repacked_fourcc == pixelformat) {
582 - if (formats[i].check_variants &&
583 - !check_mbus_format(dev, &formats[i]))
585 - return &formats[i];
592 -static inline unsigned int bytes_per_line(u32 width,
593 - const struct unicam_fmt *fmt,
596 - if (v4l2_fourcc == fmt->repacked_fourcc)
597 - /* Repacking always goes to 16bpp */
598 - return ALIGN(width << 1, BPL_ALIGNMENT);
600 - return ALIGN((width * fmt->depth) >> 3, BPL_ALIGNMENT);
603 -static int __subdev_get_format(struct unicam_device *dev,
604 - struct v4l2_mbus_framefmt *fmt, int pad_id)
606 - struct v4l2_subdev_format sd_fmt = {
607 - .which = V4L2_SUBDEV_FORMAT_ACTIVE,
612 - ret = v4l2_subdev_call(dev->sensor, pad, get_fmt, dev->sensor_config,
617 - *fmt = sd_fmt.format;
619 - unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__,
620 - fmt->width, fmt->height, fmt->code);
625 -static int __subdev_set_format(struct unicam_device *dev,
626 - struct v4l2_mbus_framefmt *fmt, int pad_id)
628 - struct v4l2_subdev_format sd_fmt = {
629 - .which = V4L2_SUBDEV_FORMAT_ACTIVE,
634 - sd_fmt.format = *fmt;
636 - ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
641 - if (pad_id == IMAGE_PAD)
642 - unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__, fmt->width,
643 - fmt->height, fmt->code);
645 - unicam_dbg(1, dev, "%s Embedded data code:%04x\n", __func__,
646 - sd_fmt.format.code);
651 -static int unicam_calc_format_size_bpl(struct unicam_device *dev,
652 - const struct unicam_fmt *fmt,
653 - struct v4l2_format *f)
655 - unsigned int min_bytesperline;
657 - v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, MAX_WIDTH, 2,
658 - &f->fmt.pix.height, MIN_HEIGHT, MAX_HEIGHT, 0,
661 - min_bytesperline = bytes_per_line(f->fmt.pix.width, fmt,
662 - f->fmt.pix.pixelformat);
664 - if (f->fmt.pix.bytesperline > min_bytesperline &&
665 - f->fmt.pix.bytesperline <= MAX_BYTESPERLINE)
666 - f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
669 - f->fmt.pix.bytesperline = min_bytesperline;
671 - f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
673 - unicam_dbg(3, dev, "%s: fourcc: %08X size: %dx%d bpl:%d img_size:%d\n",
675 - f->fmt.pix.pixelformat,
676 - f->fmt.pix.width, f->fmt.pix.height,
677 - f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
682 -static int unicam_reset_format(struct unicam_node *node)
684 - struct unicam_device *dev = node->dev;
685 - struct v4l2_mbus_framefmt mbus_fmt;
688 - if (dev->sensor_embedded_data || node->pad_id != METADATA_PAD) {
689 - ret = __subdev_get_format(dev, &mbus_fmt, node->pad_id);
691 - unicam_err(dev, "Failed to get_format - ret %d\n", ret);
695 - if (mbus_fmt.code != node->fmt->code) {
696 - unicam_err(dev, "code mismatch - fmt->code %08x, mbus_fmt.code %08x\n",
697 - node->fmt->code, mbus_fmt.code);
702 - if (node->pad_id == IMAGE_PAD) {
703 - v4l2_fill_pix_format(&node->v_fmt.fmt.pix, &mbus_fmt);
704 - node->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
705 - unicam_calc_format_size_bpl(dev, node->fmt, &node->v_fmt);
707 - node->v_fmt.type = V4L2_BUF_TYPE_META_CAPTURE;
708 - node->v_fmt.fmt.meta.dataformat = V4L2_META_FMT_SENSOR_DATA;
709 - if (dev->sensor_embedded_data) {
710 - node->v_fmt.fmt.meta.buffersize =
711 - mbus_fmt.width * mbus_fmt.height;
712 - node->embedded_lines = mbus_fmt.height;
714 - node->v_fmt.fmt.meta.buffersize = UNICAM_EMBEDDED_SIZE;
715 - node->embedded_lines = 1;
719 - node->m_fmt = mbus_fmt;
723 -static void unicam_wr_dma_addr(struct unicam_cfg *cfg, dma_addr_t dmaaddr,
724 - unsigned int buffer_size, int pad_id)
726 - dma_addr_t endaddr = dmaaddr + buffer_size;
729 - * dmaaddr and endaddr should be a 32-bit address with the top two bits
730 - * set to 0x3 to signify uncached access through the Videocore memory
733 - BUG_ON((dmaaddr >> 30) != 0x3 && (endaddr >> 30) != 0x3);
735 - if (pad_id == IMAGE_PAD) {
736 - reg_write(cfg, UNICAM_IBSA0, dmaaddr);
737 - reg_write(cfg, UNICAM_IBEA0, endaddr);
739 - reg_write(cfg, UNICAM_DBSA0, dmaaddr);
740 - reg_write(cfg, UNICAM_DBEA0, endaddr);
744 -static inline unsigned int unicam_get_lines_done(struct unicam_device *dev)
746 - dma_addr_t start_addr, cur_addr;
747 - unsigned int stride = dev->node[IMAGE_PAD].v_fmt.fmt.pix.bytesperline;
748 - struct unicam_buffer *frm = dev->node[IMAGE_PAD].cur_frm;
753 - start_addr = vb2_dma_contig_plane_dma_addr(&frm->vb.vb2_buf, 0);
754 - cur_addr = reg_read(&dev->cfg, UNICAM_IBWP);
755 - return (unsigned int)(cur_addr - start_addr) / stride;
758 -static inline void unicam_schedule_next_buffer(struct unicam_node *node)
760 - struct unicam_device *dev = node->dev;
761 - struct unicam_dmaqueue *dma_q = &node->dma_queue;
762 - struct unicam_buffer *buf;
766 - buf = list_entry(dma_q->active.next, struct unicam_buffer, list);
767 - node->next_frm = buf;
768 - list_del(&buf->list);
770 - addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
771 - size = (node->pad_id == IMAGE_PAD) ?
772 - dev->node[IMAGE_PAD].v_fmt.fmt.pix.sizeimage :
773 - dev->node[METADATA_PAD].v_fmt.fmt.meta.buffersize;
775 - unicam_wr_dma_addr(&dev->cfg, addr, size, node->pad_id);
778 -static inline void unicam_schedule_dummy_buffer(struct unicam_node *node)
780 - struct unicam_device *dev = node->dev;
781 - dma_addr_t addr = node->dummy_buf_dma_addr;
783 - unicam_dbg(3, dev, "Scheduling dummy buffer for node %d\n",
786 - unicam_wr_dma_addr(&dev->cfg, addr, DUMMY_BUF_SIZE, node->pad_id);
787 - node->next_frm = NULL;
790 -static inline void unicam_process_buffer_complete(struct unicam_node *node,
791 - unsigned int sequence)
793 - node->cur_frm->vb.field = node->m_fmt.field;
794 - node->cur_frm->vb.sequence = sequence;
796 - vb2_buffer_done(&node->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
799 -static int unicam_num_nodes_streaming(struct unicam_device *dev)
801 - return dev->node[IMAGE_PAD].streaming +
802 - dev->node[METADATA_PAD].streaming;
805 -static int unicam_all_nodes_streaming(struct unicam_device *dev)
809 - ret = dev->node[IMAGE_PAD].open && dev->node[IMAGE_PAD].streaming;
810 - ret &= !dev->node[METADATA_PAD].open ||
811 - dev->node[METADATA_PAD].streaming;
815 -static void unicam_queue_event_sof(struct unicam_device *unicam)
817 - struct v4l2_event event = {
818 - .type = V4L2_EVENT_FRAME_SYNC,
819 - .u.frame_sync.frame_sequence = unicam->sequence,
822 - v4l2_event_queue(&unicam->node[IMAGE_PAD].video_dev, &event);
826 - * unicam_isr : ISR handler for unicam capture
828 - * @dev_id: dev_id ptr
830 - * It changes status of the captured buffer, takes next buffer from the queue
831 - * and sets its address in unicam registers
833 -static irqreturn_t unicam_isr(int irq, void *dev)
835 - struct unicam_device *unicam = (struct unicam_device *)dev;
836 - struct unicam_cfg *cfg = &unicam->cfg;
837 - unsigned int lines_done = unicam_get_lines_done(dev);
838 - unsigned int sequence = unicam->sequence;
839 - int num_nodes_streaming = unicam_num_nodes_streaming(dev);
844 - sta = reg_read(cfg, UNICAM_STA);
845 - /* Write value back to clear the interrupts */
846 - reg_write(cfg, UNICAM_STA, sta);
848 - ista = reg_read(cfg, UNICAM_ISTA);
849 - /* Write value back to clear the interrupts */
850 - reg_write(cfg, UNICAM_ISTA, ista);
852 - unicam_dbg(3, unicam, "ISR: ISTA: 0x%X, STA: 0x%X, sequence %d, lines done %d",
853 - ista, sta, sequence, lines_done);
855 - if (!(sta && (UNICAM_IS | UNICAM_PI0)))
856 - return IRQ_HANDLED;
859 - * We must run the frame end handler first. If we have a valid next_frm
860 - * and we get a simultaneout FE + FS interrupt, running the FS handler
861 - * first would null out the next_frm ptr and we would have lost the
864 - if (ista & UNICAM_FEI || sta & UNICAM_PI0) {
866 - * Ensure we have swapped buffers already as we can't
867 - * stop the peripheral. If no buffer is available, use a
868 - * dummy buffer to dump out frames until we get a new buffer
871 - for (i = 0; i < num_nodes_streaming; i++) {
872 - if (unicam->node[i].cur_frm)
873 - unicam_process_buffer_complete(&unicam->node[i],
875 - unicam->node[i].cur_frm = unicam->node[i].next_frm;
877 - unicam->sequence++;
880 - if (ista & UNICAM_FSI) {
882 - * Timestamp is to be when the first data byte was captured,
885 - ts = ktime_get_ns();
886 - for (i = 0; i < num_nodes_streaming; i++) {
887 - if (unicam->node[i].cur_frm)
888 - unicam->node[i].cur_frm->vb.vb2_buf.timestamp =
891 - * Set the next frame output to go to a dummy frame
892 - * if we have not managed to obtain another frame
895 - unicam_schedule_dummy_buffer(&unicam->node[i]);
898 - unicam_queue_event_sof(unicam);
901 - * Cannot swap buffer at frame end, there may be a race condition
902 - * where the HW does not actually swap it if the new frame has
905 - if (ista & (UNICAM_FSI | UNICAM_LCI) && !(ista & UNICAM_FEI)) {
906 - for (i = 0; i < num_nodes_streaming; i++) {
907 - spin_lock(&unicam->node[i].dma_queue_lock);
908 - if (!list_empty(&unicam->node[i].dma_queue.active) &&
909 - !unicam->node[i].next_frm)
910 - unicam_schedule_next_buffer(&unicam->node[i]);
911 - spin_unlock(&unicam->node[i].dma_queue_lock);
915 - if (reg_read(&unicam->cfg, UNICAM_ICTL) & UNICAM_FCM) {
916 - /* Switch out of trigger mode if selected */
917 - reg_write_field(&unicam->cfg, UNICAM_ICTL, 1, UNICAM_TFC);
918 - reg_write_field(&unicam->cfg, UNICAM_ICTL, 0, UNICAM_FCM);
920 - return IRQ_HANDLED;
923 -static int unicam_querycap(struct file *file, void *priv,
924 - struct v4l2_capability *cap)
926 - struct unicam_node *node = video_drvdata(file);
927 - struct unicam_device *dev = node->dev;
929 - strlcpy(cap->driver, UNICAM_MODULE_NAME, sizeof(cap->driver));
930 - strlcpy(cap->card, UNICAM_MODULE_NAME, sizeof(cap->card));
932 - snprintf(cap->bus_info, sizeof(cap->bus_info),
933 - "platform:%s", dev->v4l2_dev.name);
935 - cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
936 - V4L2_CAP_READWRITE | V4L2_CAP_DEVICE_CAPS |
937 - V4L2_CAP_META_CAPTURE;
939 - if (node->pad_id == IMAGE_PAD)
940 - cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
942 - cap->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
947 -static int unicam_enum_fmt_vid_cap(struct file *file, void *priv,
948 - struct v4l2_fmtdesc *f)
950 - struct unicam_node *node = video_drvdata(file);
951 - struct unicam_device *dev = node->dev;
952 - struct v4l2_subdev_mbus_code_enum mbus_code;
953 - const struct unicam_fmt *fmt = NULL;
958 - if (node->pad_id == METADATA_PAD)
961 - for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
962 - memset(&mbus_code, 0, sizeof(mbus_code));
963 - mbus_code.index = i;
964 - mbus_code.pad = IMAGE_PAD;
965 - mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
967 - ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
971 - "subdev->enum_mbus_code idx %d returned %d - index invalid\n",
976 - fmt = find_format_by_code(mbus_code.code);
979 - if (index == f->index) {
980 - f->pixelformat = fmt->fourcc;
985 - if (fmt->repacked_fourcc) {
986 - if (index == f->index) {
987 - f->pixelformat = fmt->repacked_fourcc;
998 -static int unicam_g_fmt_vid_cap(struct file *file, void *priv,
999 - struct v4l2_format *f)
1001 - struct v4l2_mbus_framefmt mbus_fmt = {0};
1002 - struct unicam_node *node = video_drvdata(file);
1003 - struct unicam_device *dev = node->dev;
1004 - const struct unicam_fmt *fmt = NULL;
1007 - if (node->pad_id != IMAGE_PAD)
1011 - * If a flip has occurred in the sensor, the fmt code might have
1012 - * changed. So we will need to re-fetch the format from the subdevice.
1014 - ret = __subdev_get_format(dev, &mbus_fmt, node->pad_id);
1018 - /* Find the V4L2 format from mbus code. We must match a known format. */
1019 - fmt = find_format_by_code(mbus_fmt.code);
1023 - if (node->fmt != fmt) {
1025 - * The sensor format has changed so the pixelformat needs to
1026 - * be updated. Try and retain the packed/unpacked choice if
1027 - * at all possible.
1029 - if (node->fmt->repacked_fourcc ==
1030 - node->v_fmt.fmt.pix.pixelformat)
1031 - /* Using the repacked format */
1032 - node->v_fmt.fmt.pix.pixelformat = fmt->repacked_fourcc;
1034 - /* Using the native format */
1035 - node->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
1046 -const struct unicam_fmt *get_first_supported_format(struct unicam_device *dev)
1048 - struct v4l2_subdev_mbus_code_enum mbus_code;
1049 - const struct unicam_fmt *fmt = NULL;
1053 - for (j = 0; ret != -EINVAL && ret != -ENOIOCTLCMD; ++j) {
1054 - memset(&mbus_code, 0, sizeof(mbus_code));
1055 - mbus_code.index = j;
1056 - mbus_code.pad = IMAGE_PAD;
1057 - mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1059 - ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
1062 - unicam_dbg(2, dev,
1063 - "subdev->enum_mbus_code idx %d returned %d - continue\n",
1068 - unicam_dbg(2, dev, "subdev %s: code: 0x%08x idx: %d\n",
1069 - dev->sensor->name, mbus_code.code, j);
1071 - fmt = find_format_by_code(mbus_code.code);
1072 - unicam_dbg(2, dev, "fmt 0x%08x returned as %p, V4L2 FOURCC 0x%08x, csi_dt 0x%02x\n",
1073 - mbus_code.code, fmt, fmt ? fmt->fourcc : 0,
1074 - fmt ? fmt->csi_dt : 0);
1082 -static int unicam_try_fmt_vid_cap(struct file *file, void *priv,
1083 - struct v4l2_format *f)
1085 - struct unicam_node *node = video_drvdata(file);
1086 - struct unicam_device *dev = node->dev;
1087 - struct v4l2_subdev_format sd_fmt = {
1088 - .which = V4L2_SUBDEV_FORMAT_TRY,
1091 - struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
1092 - const struct unicam_fmt *fmt;
1095 - if (node->pad_id == METADATA_PAD)
1098 - fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
1100 - /* Pixel format not supported by unicam. Choose the first
1101 - * supported format, and let the sensor choose something else.
1103 - unicam_dbg(3, dev, "Fourcc format (0x%08x) not found. Use first format.\n",
1104 - f->fmt.pix.pixelformat);
1106 - fmt = &formats[0];
1107 - f->fmt.pix.pixelformat = fmt->fourcc;
1110 - v4l2_fill_mbus_format(mbus_fmt, &f->fmt.pix, fmt->code);
1112 - * No support for receiving interlaced video, so never
1113 - * request it from the sensor subdev.
1115 - mbus_fmt->field = V4L2_FIELD_NONE;
1117 - ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
1119 - if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1122 - if (mbus_fmt->field != V4L2_FIELD_NONE)
1123 - unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
1125 - v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
1126 - if (mbus_fmt->code != fmt->code) {
1127 - /* Sensor has returned an alternate format */
1128 - fmt = find_format_by_code(mbus_fmt->code);
1130 - /* The alternate format is one unicam can't support.
1131 - * Find the first format that is supported by both, and
1134 - fmt = get_first_supported_format(dev);
1135 - mbus_fmt->code = fmt->code;
1137 - ret = v4l2_subdev_call(dev->sensor, pad, set_fmt,
1138 - dev->sensor_config, &sd_fmt);
1139 - if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1142 - if (mbus_fmt->field != V4L2_FIELD_NONE)
1143 - unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
1145 - v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
1147 - if (mbus_fmt->code != fmt->code) {
1148 - /* We've set a format that the sensor reports
1149 - * as being supported, but it refuses to set it.
1150 - * Not much else we can do.
1151 - * Assume that the sensor driver may accept the
1152 - * format when it is set (rather than tried).
1154 - unicam_err(dev, "Sensor won't accept default format, and Unicam can't support sensor default\n");
1159 - f->fmt.pix.pixelformat = fmt->fourcc;
1161 - f->fmt.pix.pixelformat = fmt->repacked_fourcc;
1164 - return unicam_calc_format_size_bpl(dev, fmt, f);
1167 -static int unicam_s_fmt_vid_cap(struct file *file, void *priv,
1168 - struct v4l2_format *f)
1170 - struct unicam_node *node = video_drvdata(file);
1171 - struct unicam_device *dev = node->dev;
1172 - struct vb2_queue *q = &node->buffer_queue;
1173 - struct v4l2_mbus_framefmt mbus_fmt = {0};
1174 - const struct unicam_fmt *fmt;
1177 - if (vb2_is_busy(q))
1180 - ret = unicam_try_fmt_vid_cap(file, priv, f);
1184 - fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
1186 - /* Unknown pixel format - adopt a default.
1187 - * This shouldn't happen as try_fmt should have resolved any
1190 - fmt = get_first_supported_format(dev);
1192 - /* It shouldn't be possible to get here with no
1193 - * supported formats
1196 - f->fmt.pix.pixelformat = fmt->fourcc;
1200 - v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, fmt->code);
1202 - ret = __subdev_set_format(dev, &mbus_fmt, node->pad_id);
1204 - unicam_dbg(3, dev, "%s __subdev_set_format failed %d\n",
1209 - /* Just double check nothing has gone wrong */
1210 - if (mbus_fmt.code != fmt->code) {
1211 - unicam_dbg(3, dev,
1212 - "%s subdev changed format on us, this should not happen\n",
1218 - node->v_fmt.fmt.pix.pixelformat = f->fmt.pix.pixelformat;
1219 - node->v_fmt.fmt.pix.bytesperline = f->fmt.pix.bytesperline;
1220 - unicam_reset_format(node);
1222 - unicam_dbg(3, dev,
1223 - "%s %dx%d, mbus_fmt 0x%08X, V4L2 pix 0x%08X.\n",
1224 - __func__, node->v_fmt.fmt.pix.width,
1225 - node->v_fmt.fmt.pix.height, mbus_fmt.code,
1226 - node->v_fmt.fmt.pix.pixelformat);
1233 -static int unicam_enum_fmt_meta_cap(struct file *file, void *priv,
1234 - struct v4l2_fmtdesc *f)
1236 - struct unicam_node *node = video_drvdata(file);
1237 - struct unicam_device *dev = node->dev;
1238 - struct v4l2_subdev_mbus_code_enum mbus_code;
1239 - const struct unicam_fmt *fmt = NULL;
1242 - if (node->pad_id != METADATA_PAD || f->index != 0)
1245 - if (dev->sensor_embedded_data) {
1246 - memset(&mbus_code, 0, sizeof(mbus_code));
1247 - mbus_code.index = f->index;
1248 - mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1249 - mbus_code.pad = METADATA_PAD;
1251 - ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
1254 - unicam_dbg(2, dev,
1255 - "subdev->enum_mbus_code idx 0 returned %d - index invalid\n",
1260 - mbus_code.code = MEDIA_BUS_FMT_SENSOR_DATA;
1263 - fmt = find_format_by_code(mbus_code.code);
1265 - f->pixelformat = fmt->fourcc;
1270 -static int unicam_g_fmt_meta_cap(struct file *file, void *priv,
1271 - struct v4l2_format *f)
1273 - struct unicam_node *node = video_drvdata(file);
1275 - if (node->pad_id != METADATA_PAD)
1283 -static int unicam_try_fmt_meta_cap(struct file *file, void *priv,
1284 - struct v4l2_format *f)
1286 - struct unicam_node *node = video_drvdata(file);
1288 - if (node->pad_id != METADATA_PAD)
1296 -static int unicam_s_fmt_meta_cap(struct file *file, void *priv,
1297 - struct v4l2_format *f)
1299 - struct unicam_node *node = video_drvdata(file);
1300 - struct unicam_device *dev = node->dev;
1301 - struct v4l2_mbus_framefmt mbus_fmt = { 0 };
1302 - const struct unicam_fmt *fmt;
1305 - if (node->pad_id == IMAGE_PAD)
1308 - if (dev->sensor_embedded_data) {
1309 - fmt = find_format_by_pix(dev, f->fmt.meta.dataformat);
1311 - unicam_err(dev, "unknown format: V4L2 pix 0x%08x\n",
1312 - f->fmt.meta.dataformat);
1315 - mbus_fmt.code = fmt->code;
1316 - ret = __subdev_set_format(dev, &mbus_fmt, node->pad_id);
1318 - unicam_dbg(3, dev, "%s __subdev_set_format failed %d\n",
1326 - unicam_dbg(3, dev, "%s size %d, V4L2 pix 0x%08x\n",
1327 - __func__, node->v_fmt.fmt.meta.buffersize,
1328 - node->v_fmt.fmt.meta.dataformat);
1333 -static int unicam_queue_setup(struct vb2_queue *vq,
1334 - unsigned int *nbuffers,
1335 - unsigned int *nplanes,
1336 - unsigned int sizes[],
1337 - struct device *alloc_devs[])
1339 - struct unicam_node *node = vb2_get_drv_priv(vq);
1340 - struct unicam_device *dev = node->dev;
1341 - unsigned int size = node->pad_id == IMAGE_PAD ?
1342 - node->v_fmt.fmt.pix.sizeimage :
1343 - node->v_fmt.fmt.meta.buffersize;
1345 - if (vq->num_buffers + *nbuffers < 3)
1346 - *nbuffers = 3 - vq->num_buffers;
1349 - if (sizes[0] < size) {
1350 - unicam_err(dev, "sizes[0] %i < size %u\n", sizes[0],
1363 -static int unicam_buffer_prepare(struct vb2_buffer *vb)
1365 - struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
1366 - struct unicam_device *dev = node->dev;
1367 - struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
1369 - unsigned long size;
1371 - if (WARN_ON(!node->fmt))
1374 - size = node->pad_id == IMAGE_PAD ? node->v_fmt.fmt.pix.sizeimage :
1375 - node->v_fmt.fmt.meta.buffersize;
1376 - if (vb2_plane_size(vb, 0) < size) {
1377 - unicam_err(dev, "data will not fit into plane (%lu < %lu)\n",
1378 - vb2_plane_size(vb, 0), size);
1382 - vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
1386 -static void unicam_buffer_queue(struct vb2_buffer *vb)
1388 - struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
1389 - struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
1391 - struct unicam_dmaqueue *dma_queue = &node->dma_queue;
1392 - unsigned long flags = 0;
1394 - spin_lock_irqsave(&node->dma_queue_lock, flags);
1395 - list_add_tail(&buf->list, &dma_queue->active);
1396 - spin_unlock_irqrestore(&node->dma_queue_lock, flags);
1399 -static void unicam_set_packing_config(struct unicam_device *dev)
1404 - if (dev->node[IMAGE_PAD].v_fmt.fmt.pix.pixelformat ==
1405 - dev->node[IMAGE_PAD].fmt->fourcc) {
1406 - unpack = UNICAM_PUM_NONE;
1407 - pack = UNICAM_PPM_NONE;
1409 - switch (dev->node[IMAGE_PAD].fmt->depth) {
1411 - unpack = UNICAM_PUM_UNPACK8;
1414 - unpack = UNICAM_PUM_UNPACK10;
1417 - unpack = UNICAM_PUM_UNPACK12;
1420 - unpack = UNICAM_PUM_UNPACK14;
1423 - unpack = UNICAM_PUM_UNPACK16;
1426 - unpack = UNICAM_PUM_NONE;
1430 - /* Repacking is always to 16bpp */
1431 - pack = UNICAM_PPM_PACK16;
1435 - set_field(&val, unpack, UNICAM_PUM_MASK);
1436 - set_field(&val, pack, UNICAM_PPM_MASK);
1437 - reg_write(&dev->cfg, UNICAM_IPIPE, val);
1440 -static void unicam_cfg_image_id(struct unicam_device *dev)
1442 - struct unicam_cfg *cfg = &dev->cfg;
1444 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1446 - reg_write(cfg, UNICAM_IDI0,
1447 - (dev->virtual_channel << 6) |
1448 - dev->node[IMAGE_PAD].fmt->csi_dt);
1451 - reg_write(cfg, UNICAM_IDI0,
1452 - 0x80 | dev->node[IMAGE_PAD].fmt->csi_dt);
1456 -static void unicam_enable_ed(struct unicam_device *dev)
1458 - struct unicam_cfg *cfg = &dev->cfg;
1459 - u32 val = reg_read(cfg, UNICAM_DCS);
1461 - set_field(&val, 2, UNICAM_EDL_MASK);
1462 - /* Do not wrap at the end of the embedded data buffer */
1463 - set_field(&val, 0, UNICAM_DBOB);
1465 - reg_write(cfg, UNICAM_DCS, val);
1468 -static void unicam_start_rx(struct unicam_device *dev, dma_addr_t *addr)
1470 - struct unicam_cfg *cfg = &dev->cfg;
1471 - int line_int_freq = dev->node[IMAGE_PAD].v_fmt.fmt.pix.height >> 2;
1472 - unsigned int size, i;
1475 - if (line_int_freq < 128)
1476 - line_int_freq = 128;
1478 - /* Enable lane clocks */
1480 - for (i = 0; i < dev->active_data_lanes; i++)
1481 - val = val << 2 | 1;
1482 - clk_write(cfg, val);
1485 - reg_write(cfg, UNICAM_CTRL, UNICAM_MEM);
1487 - /* Enable analogue control, and leave in reset. */
1489 - set_field(&val, 7, UNICAM_CTATADJ_MASK);
1490 - set_field(&val, 7, UNICAM_PTATADJ_MASK);
1491 - reg_write(cfg, UNICAM_ANA, val);
1492 - usleep_range(1000, 2000);
1494 - /* Come out of reset */
1495 - reg_write_field(cfg, UNICAM_ANA, 0, UNICAM_AR);
1497 - /* Peripheral reset */
1498 - reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPR);
1499 - reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPR);
1501 - reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPE);
1503 - /* Enable Rx control. */
1504 - val = reg_read(cfg, UNICAM_CTRL);
1505 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1506 - set_field(&val, UNICAM_CPM_CSI2, UNICAM_CPM_MASK);
1507 - set_field(&val, UNICAM_DCM_STROBE, UNICAM_DCM_MASK);
1509 - set_field(&val, UNICAM_CPM_CCP2, UNICAM_CPM_MASK);
1510 - set_field(&val, dev->bus_flags, UNICAM_DCM_MASK);
1512 - /* Packet framer timeout */
1513 - set_field(&val, 0xf, UNICAM_PFT_MASK);
1514 - set_field(&val, 128, UNICAM_OET_MASK);
1515 - reg_write(cfg, UNICAM_CTRL, val);
1517 - reg_write(cfg, UNICAM_IHWIN, 0);
1518 - reg_write(cfg, UNICAM_IVWIN, 0);
1520 - /* AXI bus access QoS setup */
1521 - val = reg_read(&dev->cfg, UNICAM_PRI);
1522 - set_field(&val, 0, UNICAM_BL_MASK);
1523 - set_field(&val, 0, UNICAM_BS_MASK);
1524 - set_field(&val, 0xe, UNICAM_PP_MASK);
1525 - set_field(&val, 8, UNICAM_NP_MASK);
1526 - set_field(&val, 2, UNICAM_PT_MASK);
1527 - set_field(&val, 1, UNICAM_PE);
1528 - reg_write(cfg, UNICAM_PRI, val);
1530 - reg_write_field(cfg, UNICAM_ANA, 0, UNICAM_DDL);
1532 - /* Always start in trigger frame capture mode (UNICAM_FCM set) */
1533 - val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_FCM | UNICAM_IBOB;
1534 - set_field(&val, line_int_freq, UNICAM_LCIE_MASK);
1535 - reg_write(cfg, UNICAM_ICTL, val);
1536 - reg_write(cfg, UNICAM_STA, UNICAM_STA_MASK_ALL);
1537 - reg_write(cfg, UNICAM_ISTA, UNICAM_ISTA_MASK_ALL);
1539 - /* tclk_term_en */
1540 - reg_write_field(cfg, UNICAM_CLT, 2, UNICAM_CLT1_MASK);
1542 - reg_write_field(cfg, UNICAM_CLT, 6, UNICAM_CLT2_MASK);
1544 - reg_write_field(cfg, UNICAM_DLT, 2, UNICAM_DLT1_MASK);
1546 - reg_write_field(cfg, UNICAM_DLT, 6, UNICAM_DLT2_MASK);
1548 - reg_write_field(cfg, UNICAM_DLT, 0, UNICAM_DLT3_MASK);
1550 - reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_SOE);
1552 - /* Packet compare setup - required to avoid missing frame ends */
1554 - set_field(&val, 1, UNICAM_PCE);
1555 - set_field(&val, 1, UNICAM_GI);
1556 - set_field(&val, 1, UNICAM_CPH);
1557 - set_field(&val, 0, UNICAM_PCVC_MASK);
1558 - set_field(&val, 1, UNICAM_PCDT_MASK);
1559 - reg_write(cfg, UNICAM_CMP0, val);
1561 - /* Enable clock lane and set up terminations */
1563 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1565 - set_field(&val, 1, UNICAM_CLE);
1566 - set_field(&val, 1, UNICAM_CLLPE);
1567 - if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1568 - set_field(&val, 1, UNICAM_CLTRE);
1569 - set_field(&val, 1, UNICAM_CLHSE);
1573 - set_field(&val, 1, UNICAM_CLE);
1574 - set_field(&val, 1, UNICAM_CLHSE);
1575 - set_field(&val, 1, UNICAM_CLTRE);
1577 - reg_write(cfg, UNICAM_CLK, val);
1580 - * Enable required data lanes with appropriate terminations.
1581 - * The same value needs to be written to UNICAM_DATn registers for
1582 - * the active lanes, and 0 for inactive ones.
1585 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1587 - set_field(&val, 1, UNICAM_DLE);
1588 - set_field(&val, 1, UNICAM_DLLPE);
1589 - if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1590 - set_field(&val, 1, UNICAM_DLTRE);
1591 - set_field(&val, 1, UNICAM_DLHSE);
1595 - set_field(&val, 1, UNICAM_DLE);
1596 - set_field(&val, 1, UNICAM_DLHSE);
1597 - set_field(&val, 1, UNICAM_DLTRE);
1599 - reg_write(cfg, UNICAM_DAT0, val);
1601 - if (dev->active_data_lanes == 1)
1603 - reg_write(cfg, UNICAM_DAT1, val);
1605 - if (dev->max_data_lanes > 2) {
1607 - * Registers UNICAM_DAT2 and UNICAM_DAT3 only valid if the
1608 - * instance supports more than 2 data lanes.
1610 - if (dev->active_data_lanes == 2)
1612 - reg_write(cfg, UNICAM_DAT2, val);
1614 - if (dev->active_data_lanes == 3)
1616 - reg_write(cfg, UNICAM_DAT3, val);
1619 - reg_write(&dev->cfg, UNICAM_IBLS,
1620 - dev->node[IMAGE_PAD].v_fmt.fmt.pix.bytesperline);
1621 - size = dev->node[IMAGE_PAD].v_fmt.fmt.pix.sizeimage;
1622 - unicam_wr_dma_addr(&dev->cfg, addr[IMAGE_PAD], size, IMAGE_PAD);
1623 - unicam_set_packing_config(dev);
1624 - unicam_cfg_image_id(dev);
1626 - val = reg_read(cfg, UNICAM_MISC);
1627 - set_field(&val, 1, UNICAM_FL0);
1628 - set_field(&val, 1, UNICAM_FL1);
1629 - reg_write(cfg, UNICAM_MISC, val);
1631 - if (dev->node[METADATA_PAD].streaming && dev->sensor_embedded_data) {
1632 - size = dev->node[METADATA_PAD].v_fmt.fmt.meta.buffersize;
1633 - unicam_enable_ed(dev);
1634 - unicam_wr_dma_addr(&dev->cfg, addr[METADATA_PAD], size,
1638 - /* Enable peripheral */
1639 - reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPE);
1641 - /* Load image pointers */
1642 - reg_write_field(cfg, UNICAM_ICTL, 1, UNICAM_LIP_MASK);
1644 - /* Load embedded data buffer pointers if needed */
1645 - if (dev->node[METADATA_PAD].streaming && dev->sensor_embedded_data)
1646 - reg_write_field(cfg, UNICAM_DCS, 1, UNICAM_LDP);
1649 - * Enable trigger only for the first frame to
1650 - * sync correctly to the FS from the source.
1652 - reg_write_field(cfg, UNICAM_ICTL, 1, UNICAM_TFC);
1655 -static void unicam_disable(struct unicam_device *dev)
1657 - struct unicam_cfg *cfg = &dev->cfg;
1659 - /* Analogue lane control disable */
1660 - reg_write_field(cfg, UNICAM_ANA, 1, UNICAM_DDL);
1662 - /* Stop the output engine */
1663 - reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_SOE);
1665 - /* Disable the data lanes. */
1666 - reg_write(cfg, UNICAM_DAT0, 0);
1667 - reg_write(cfg, UNICAM_DAT1, 0);
1669 - if (dev->max_data_lanes > 2) {
1670 - reg_write(cfg, UNICAM_DAT2, 0);
1671 - reg_write(cfg, UNICAM_DAT3, 0);
1674 - /* Peripheral reset */
1675 - reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPR);
1676 - usleep_range(50, 100);
1677 - reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPR);
1679 - /* Disable peripheral */
1680 - reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPE);
1682 - /* Clear ED setup */
1683 - reg_write(cfg, UNICAM_DCS, 0);
1685 - /* Disable all lane clocks */
1686 - clk_write(cfg, 0);
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 - struct unicam_buffer *buf;
1694 - dma_addr_t buffer_addr[MAX_NODES] = { 0 };
1695 - int num_nodes_streaming;
1696 - unsigned long flags;
1699 - node->streaming = 1;
1700 - if (!unicam_all_nodes_streaming(dev)) {
1701 - unicam_dbg(3, dev, "Not all nodes are streaming yet.");
1705 - dev->sequence = 0;
1706 - ret = unicam_runtime_get(dev);
1708 - unicam_dbg(3, dev, "unicam_runtime_get failed\n");
1712 - dev->active_data_lanes = dev->max_data_lanes;
1713 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY &&
1714 - v4l2_subdev_has_op(dev->sensor, video, g_mbus_config)) {
1715 - struct v4l2_mbus_config mbus_config;
1717 - ret = v4l2_subdev_call(dev->sensor, video, g_mbus_config,
1720 - unicam_dbg(3, dev, "g_mbus_config failed\n");
1724 - dev->active_data_lanes =
1725 - (mbus_config.flags & V4L2_MBUS_CSI2_LANE_MASK) >>
1726 - __ffs(V4L2_MBUS_CSI2_LANE_MASK);
1727 - if (!dev->active_data_lanes)
1728 - dev->active_data_lanes = dev->max_data_lanes;
1730 - if (dev->active_data_lanes > dev->max_data_lanes) {
1731 - unicam_err(dev, "Device has requested %u data lanes, which is >%u configured in DT\n",
1732 - dev->active_data_lanes, dev->max_data_lanes);
1737 - unicam_dbg(1, dev, "Running with %u data lanes\n",
1738 - dev->active_data_lanes);
1740 - ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
1742 - unicam_err(dev, "failed to set up clock\n");
1746 - ret = clk_prepare_enable(dev->clock);
1748 - unicam_err(dev, "Failed to enable CSI clock: %d\n", ret);
1752 - num_nodes_streaming = unicam_num_nodes_streaming(dev);
1753 - for (i = 0; i < num_nodes_streaming; i++) {
1754 - spin_lock_irqsave(&dev->node[i].dma_queue_lock, flags);
1755 - buf = list_entry(dev->node[i].dma_queue.active.next,
1756 - struct unicam_buffer, list);
1757 - dev->node[i].cur_frm = buf;
1758 - dev->node[i].next_frm = buf;
1759 - list_del(&buf->list);
1760 - spin_unlock_irqrestore(&dev->node[i].dma_queue_lock, flags);
1762 - vb2_dma_contig_plane_dma_addr(&dev->node[i].cur_frm->vb.vb2_buf,
1766 - unicam_start_rx(dev, buffer_addr);
1768 - ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1770 - unicam_err(dev, "stream on failed in subdev\n");
1771 - goto err_disable_unicam;
1776 -err_disable_unicam:
1777 - node->streaming = 0;
1778 - unicam_disable(dev);
1779 - clk_disable_unprepare(dev->clock);
1781 - unicam_runtime_put(dev);
1786 -static void unicam_stop_streaming(struct vb2_queue *vq)
1788 - struct unicam_node *node = vb2_get_drv_priv(vq);
1789 - struct unicam_device *dev = node->dev;
1790 - struct unicam_dmaqueue *dma_q = &node->dma_queue;
1791 - struct unicam_buffer *buf, *tmp;
1792 - unsigned long flags;
1794 - node->streaming = 0;
1796 - if (node->pad_id == IMAGE_PAD) {
1797 - /* Stop streaming the sensor and disable the peripheral.
1798 - * We cannot continue streaming embedded data with the
1799 - * image pad disabled.
1801 - if (v4l2_subdev_call(dev->sensor, video, s_stream, 0) < 0)
1802 - unicam_err(dev, "stream off failed in subdev\n");
1804 - unicam_disable(dev);
1805 - clk_disable_unprepare(dev->clock);
1806 - unicam_runtime_put(dev);
1808 - } else if (node->pad_id == METADATA_PAD) {
1809 - /* Allow the hardware to spin in the dummy buffer.
1810 - * This is only really needed if the embedded data pad is
1811 - * disabled before the image pad. The 0x3 in the top two bits
1812 - * signifies uncached accesses through the Videocore memory
1815 - unicam_wr_dma_addr(&dev->cfg, node->dummy_buf_dma_addr,
1816 - DUMMY_BUF_SIZE, METADATA_PAD);
1819 - /* Clear all queued buffers for the node */
1820 - spin_lock_irqsave(&node->dma_queue_lock, flags);
1821 - list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
1822 - list_del(&buf->list);
1823 - vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1826 - if (node->cur_frm)
1827 - vb2_buffer_done(&node->cur_frm->vb.vb2_buf,
1828 - VB2_BUF_STATE_ERROR);
1829 - if (node->next_frm && node->cur_frm != node->next_frm)
1830 - vb2_buffer_done(&node->next_frm->vb.vb2_buf,
1831 - VB2_BUF_STATE_ERROR);
1833 - node->cur_frm = NULL;
1834 - node->next_frm = NULL;
1835 - spin_unlock_irqrestore(&node->dma_queue_lock, flags);
1838 -static int unicam_enum_input(struct file *file, void *priv,
1839 - struct v4l2_input *inp)
1841 - struct unicam_node *node = video_drvdata(file);
1842 - struct unicam_device *dev = node->dev;
1844 - if (inp->index != 0)
1847 - inp->type = V4L2_INPUT_TYPE_CAMERA;
1848 - if (v4l2_subdev_has_op(dev->sensor, video, s_dv_timings)) {
1849 - inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1851 - } else if (v4l2_subdev_has_op(dev->sensor, video, s_std)) {
1852 - inp->capabilities = V4L2_IN_CAP_STD;
1853 - if (v4l2_subdev_call(dev->sensor, video, g_tvnorms, &inp->std)
1855 - inp->std = V4L2_STD_ALL;
1857 - inp->capabilities = 0;
1860 - sprintf(inp->name, "Camera 0");
1864 -static int unicam_g_input(struct file *file, void *priv, unsigned int *i)
1871 -static int unicam_s_input(struct file *file, void *priv, unsigned int i)
1874 - * FIXME: Ideally we would like to be able to query the source
1875 - * subdevice for information over the input connectors it supports,
1876 - * and map that through in to a call to video_ops->s_routing.
1877 - * There is no infrastructure support for defining that within
1878 - * devicetree at present. Until that is implemented we can't
1879 - * map a user physical connector number to s_routing input number.
1887 -static int unicam_querystd(struct file *file, void *priv,
1890 - struct unicam_node *node = video_drvdata(file);
1891 - struct unicam_device *dev = node->dev;
1893 - return v4l2_subdev_call(dev->sensor, video, querystd, std);
1896 -static int unicam_g_std(struct file *file, void *priv, v4l2_std_id *std)
1898 - struct unicam_node *node = video_drvdata(file);
1899 - struct unicam_device *dev = node->dev;
1901 - return v4l2_subdev_call(dev->sensor, video, g_std, std);
1904 -static int unicam_s_std(struct file *file, void *priv, v4l2_std_id std)
1906 - struct unicam_node *node = video_drvdata(file);
1907 - struct unicam_device *dev = node->dev;
1909 - v4l2_std_id current_std;
1911 - ret = v4l2_subdev_call(dev->sensor, video, g_std, ¤t_std);
1915 - if (std == current_std)
1918 - if (vb2_is_busy(&node->buffer_queue))
1921 - ret = v4l2_subdev_call(dev->sensor, video, s_std, std);
1923 - /* Force recomputation of bytesperline */
1924 - node->v_fmt.fmt.pix.bytesperline = 0;
1926 - unicam_reset_format(node);
1931 -static int unicam_s_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1933 - struct unicam_node *node = video_drvdata(file);
1934 - struct unicam_device *dev = node->dev;
1936 - return v4l2_subdev_call(dev->sensor, pad, set_edid, edid);
1939 -static int unicam_g_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1941 - struct unicam_node *node = video_drvdata(file);
1942 - struct unicam_device *dev = node->dev;
1944 - return v4l2_subdev_call(dev->sensor, pad, get_edid, edid);
1947 -static int unicam_s_selection(struct file *file, void *priv,
1948 - struct v4l2_selection *sel)
1950 - struct unicam_node *node = video_drvdata(file);
1951 - struct unicam_device *dev = node->dev;
1952 - struct v4l2_subdev_selection sdsel = {
1953 - .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1954 - .target = sel->target,
1955 - .flags = sel->flags,
1959 - return v4l2_subdev_call(dev->sensor, pad, set_selection, NULL, &sdsel);
1962 -static int unicam_g_selection(struct file *file, void *priv,
1963 - struct v4l2_selection *sel)
1965 - struct unicam_node *node = video_drvdata(file);
1966 - struct unicam_device *dev = node->dev;
1967 - struct v4l2_subdev_selection sdsel = {
1968 - .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1969 - .target = sel->target,
1973 - ret = v4l2_subdev_call(dev->sensor, pad, get_selection, NULL, &sdsel);
1980 -static int unicam_enum_framesizes(struct file *file, void *priv,
1981 - struct v4l2_frmsizeenum *fsize)
1983 - struct unicam_node *node = video_drvdata(file);
1984 - struct unicam_device *dev = node->dev;
1985 - const struct unicam_fmt *fmt;
1986 - struct v4l2_subdev_frame_size_enum fse;
1989 - if (node->pad_id == IMAGE_PAD) {
1990 - /* check for valid format */
1991 - fmt = find_format_by_pix(dev, fsize->pixel_format);
1993 - unicam_dbg(3, dev, "Invalid pixel code: %x\n",
1994 - fsize->pixel_format);
1997 - fse.code = fmt->code;
1999 - /* This pad is for embedded data, so just set the format */
2000 - fse.code = MEDIA_BUS_FMT_SENSOR_DATA;
2003 - fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
2004 - fse.index = fsize->index;
2005 - fse.pad = node->pad_id;
2007 - ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_size, NULL, &fse);
2011 - unicam_dbg(1, dev, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
2012 - __func__, fse.index, fse.code, fse.min_width, fse.max_width,
2013 - fse.min_height, fse.max_height);
2015 - fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
2016 - fsize->discrete.width = fse.max_width;
2017 - fsize->discrete.height = fse.max_height;
2022 -static int unicam_enum_frameintervals(struct file *file, void *priv,
2023 - struct v4l2_frmivalenum *fival)
2025 - struct unicam_node *node = video_drvdata(file);
2026 - struct unicam_device *dev = node->dev;
2027 - const struct unicam_fmt *fmt;
2028 - struct v4l2_subdev_frame_interval_enum fie = {
2029 - .index = fival->index,
2030 - .width = fival->width,
2031 - .height = fival->height,
2032 - .which = V4L2_SUBDEV_FORMAT_ACTIVE,
2036 - fmt = find_format_by_pix(dev, fival->pixel_format);
2040 - fie.code = fmt->code;
2041 - ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_interval,
2046 - fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
2047 - fival->discrete = fie.interval;
2052 -static int unicam_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
2054 - struct unicam_node *node = video_drvdata(file);
2055 - struct unicam_device *dev = node->dev;
2057 - return v4l2_g_parm_cap(video_devdata(file), dev->sensor, a);
2060 -static int unicam_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
2062 - struct unicam_node *node = video_drvdata(file);
2063 - struct unicam_device *dev = node->dev;
2065 - return v4l2_s_parm_cap(video_devdata(file), dev->sensor, a);
2068 -static int unicam_g_dv_timings(struct file *file, void *priv,
2069 - struct v4l2_dv_timings *timings)
2071 - struct unicam_node *node = video_drvdata(file);
2072 - struct unicam_device *dev = node->dev;
2074 - return v4l2_subdev_call(dev->sensor, video, g_dv_timings, timings);
2077 -static int unicam_s_dv_timings(struct file *file, void *priv,
2078 - struct v4l2_dv_timings *timings)
2080 - struct unicam_node *node = video_drvdata(file);
2081 - struct unicam_device *dev = node->dev;
2082 - struct v4l2_dv_timings current_timings;
2085 - ret = v4l2_subdev_call(dev->sensor, video, g_dv_timings,
2086 - ¤t_timings);
2088 - if (v4l2_match_dv_timings(timings, ¤t_timings, 0, false))
2091 - if (vb2_is_busy(&node->buffer_queue))
2094 - ret = v4l2_subdev_call(dev->sensor, video, s_dv_timings, timings);
2096 - /* Force recomputation of bytesperline */
2097 - node->v_fmt.fmt.pix.bytesperline = 0;
2099 - unicam_reset_format(node);
2104 -static int unicam_query_dv_timings(struct file *file, void *priv,
2105 - struct v4l2_dv_timings *timings)
2107 - struct unicam_node *node = video_drvdata(file);
2108 - struct unicam_device *dev = node->dev;
2110 - return v4l2_subdev_call(dev->sensor, video, query_dv_timings, timings);
2113 -static int unicam_enum_dv_timings(struct file *file, void *priv,
2114 - struct v4l2_enum_dv_timings *timings)
2116 - struct unicam_node *node = video_drvdata(file);
2117 - struct unicam_device *dev = node->dev;
2119 - return v4l2_subdev_call(dev->sensor, pad, enum_dv_timings, timings);
2122 -static int unicam_dv_timings_cap(struct file *file, void *priv,
2123 - struct v4l2_dv_timings_cap *cap)
2125 - struct unicam_node *node = video_drvdata(file);
2126 - struct unicam_device *dev = node->dev;
2128 - return v4l2_subdev_call(dev->sensor, pad, dv_timings_cap, cap);
2131 -static int unicam_subscribe_event(struct v4l2_fh *fh,
2132 - const struct v4l2_event_subscription *sub)
2134 - switch (sub->type) {
2135 - case V4L2_EVENT_FRAME_SYNC:
2136 - return v4l2_event_subscribe(fh, sub, 2, NULL);
2137 - case V4L2_EVENT_SOURCE_CHANGE:
2138 - return v4l2_event_subscribe(fh, sub, 4, NULL);
2141 - return v4l2_ctrl_subscribe_event(fh, sub);
2144 -static int unicam_log_status(struct file *file, void *fh)
2146 - struct unicam_node *node = video_drvdata(file);
2147 - struct unicam_device *dev = node->dev;
2148 - struct unicam_cfg *cfg = &dev->cfg;
2151 - /* status for sub devices */
2152 - v4l2_device_call_all(&dev->v4l2_dev, 0, core, log_status);
2154 - unicam_info(dev, "-----Receiver status-----\n");
2155 - unicam_info(dev, "V4L2 width/height: %ux%u\n",
2156 - node->v_fmt.fmt.pix.width, node->v_fmt.fmt.pix.height);
2157 - unicam_info(dev, "Mediabus format: %08x\n", node->fmt->code);
2158 - unicam_info(dev, "V4L2 format: %08x\n",
2159 - node->v_fmt.fmt.pix.pixelformat);
2160 - reg = reg_read(&dev->cfg, UNICAM_IPIPE);
2161 - unicam_info(dev, "Unpacking/packing: %u / %u\n",
2162 - get_field(reg, UNICAM_PUM_MASK),
2163 - get_field(reg, UNICAM_PPM_MASK));
2164 - unicam_info(dev, "----Live data----\n");
2165 - unicam_info(dev, "Programmed stride: %4u\n",
2166 - reg_read(cfg, UNICAM_IBLS));
2167 - unicam_info(dev, "Detected resolution: %ux%u\n",
2168 - reg_read(cfg, UNICAM_IHSTA),
2169 - reg_read(cfg, UNICAM_IVSTA));
2170 - unicam_info(dev, "Write pointer: %08x\n",
2171 - reg_read(cfg, UNICAM_IBWP));
2176 -static void unicam_notify(struct v4l2_subdev *sd,
2177 - unsigned int notification, void *arg)
2179 - struct unicam_device *dev =
2180 - container_of(sd->v4l2_dev, struct unicam_device, v4l2_dev);
2182 - switch (notification) {
2183 - case V4L2_DEVICE_NOTIFY_EVENT:
2184 - v4l2_event_queue(&dev->node[IMAGE_PAD].video_dev, arg);
2191 -static const struct vb2_ops unicam_video_qops = {
2192 - .wait_prepare = vb2_ops_wait_prepare,
2193 - .wait_finish = vb2_ops_wait_finish,
2194 - .queue_setup = unicam_queue_setup,
2195 - .buf_prepare = unicam_buffer_prepare,
2196 - .buf_queue = unicam_buffer_queue,
2197 - .start_streaming = unicam_start_streaming,
2198 - .stop_streaming = unicam_stop_streaming,
2202 - * unicam_open : This function is based on the v4l2_fh_open helper function.
2203 - * It has been augmented to handle sensor subdevice power management,
2205 -static int unicam_open(struct file *file)
2207 - struct unicam_node *node = video_drvdata(file);
2208 - struct unicam_device *dev = node->dev;
2211 - mutex_lock(&node->lock);
2213 - ret = v4l2_fh_open(file);
2215 - unicam_err(dev, "v4l2_fh_open failed\n");
2221 - if (!v4l2_fh_is_singular_file(file))
2224 - ret = v4l2_subdev_call(dev->sensor, core, s_power, 1);
2225 - if (ret < 0 && ret != -ENOIOCTLCMD) {
2226 - v4l2_fh_release(file);
2234 - mutex_unlock(&node->lock);
2238 -static int unicam_release(struct file *file)
2240 - struct unicam_node *node = video_drvdata(file);
2241 - struct unicam_device *dev = node->dev;
2242 - struct v4l2_subdev *sd = dev->sensor;
2246 - mutex_lock(&node->lock);
2248 - fh_singular = v4l2_fh_is_singular_file(file);
2250 - ret = _vb2_fop_release(file, NULL);
2253 - v4l2_subdev_call(sd, core, s_power, 0);
2256 - mutex_unlock(&node->lock);
2261 -/* unicam capture driver file operations */
2262 -static const struct v4l2_file_operations unicam_fops = {
2263 - .owner = THIS_MODULE,
2264 - .open = unicam_open,
2265 - .release = unicam_release,
2266 - .read = vb2_fop_read,
2267 - .poll = vb2_fop_poll,
2268 - .unlocked_ioctl = video_ioctl2,
2269 - .mmap = vb2_fop_mmap,
2272 -/* unicam capture ioctl operations */
2273 -static const struct v4l2_ioctl_ops unicam_ioctl_ops = {
2274 - .vidioc_querycap = unicam_querycap,
2275 - .vidioc_enum_fmt_vid_cap = unicam_enum_fmt_vid_cap,
2276 - .vidioc_g_fmt_vid_cap = unicam_g_fmt_vid_cap,
2277 - .vidioc_s_fmt_vid_cap = unicam_s_fmt_vid_cap,
2278 - .vidioc_try_fmt_vid_cap = unicam_try_fmt_vid_cap,
2280 - .vidioc_enum_fmt_meta_cap = unicam_enum_fmt_meta_cap,
2281 - .vidioc_g_fmt_meta_cap = unicam_g_fmt_meta_cap,
2282 - .vidioc_s_fmt_meta_cap = unicam_s_fmt_meta_cap,
2283 - .vidioc_try_fmt_meta_cap = unicam_try_fmt_meta_cap,
2285 - .vidioc_enum_input = unicam_enum_input,
2286 - .vidioc_g_input = unicam_g_input,
2287 - .vidioc_s_input = unicam_s_input,
2289 - .vidioc_querystd = unicam_querystd,
2290 - .vidioc_s_std = unicam_s_std,
2291 - .vidioc_g_std = unicam_g_std,
2293 - .vidioc_g_edid = unicam_g_edid,
2294 - .vidioc_s_edid = unicam_s_edid,
2296 - .vidioc_enum_framesizes = unicam_enum_framesizes,
2297 - .vidioc_enum_frameintervals = unicam_enum_frameintervals,
2299 - .vidioc_g_selection = unicam_g_selection,
2300 - .vidioc_s_selection = unicam_s_selection,
2302 - .vidioc_g_parm = unicam_g_parm,
2303 - .vidioc_s_parm = unicam_s_parm,
2305 - .vidioc_s_dv_timings = unicam_s_dv_timings,
2306 - .vidioc_g_dv_timings = unicam_g_dv_timings,
2307 - .vidioc_query_dv_timings = unicam_query_dv_timings,
2308 - .vidioc_enum_dv_timings = unicam_enum_dv_timings,
2309 - .vidioc_dv_timings_cap = unicam_dv_timings_cap,
2311 - .vidioc_reqbufs = vb2_ioctl_reqbufs,
2312 - .vidioc_create_bufs = vb2_ioctl_create_bufs,
2313 - .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2314 - .vidioc_querybuf = vb2_ioctl_querybuf,
2315 - .vidioc_qbuf = vb2_ioctl_qbuf,
2316 - .vidioc_dqbuf = vb2_ioctl_dqbuf,
2317 - .vidioc_expbuf = vb2_ioctl_expbuf,
2318 - .vidioc_streamon = vb2_ioctl_streamon,
2319 - .vidioc_streamoff = vb2_ioctl_streamoff,
2321 - .vidioc_log_status = unicam_log_status,
2322 - .vidioc_subscribe_event = unicam_subscribe_event,
2323 - .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2327 -unicam_async_bound(struct v4l2_async_notifier *notifier,
2328 - struct v4l2_subdev *subdev,
2329 - struct v4l2_async_subdev *asd)
2331 - struct unicam_device *unicam = container_of(notifier->v4l2_dev,
2332 - struct unicam_device, v4l2_dev);
2334 - if (unicam->sensor) {
2335 - unicam_info(unicam, "Rejecting subdev %s (Already set!!)",
2340 - unicam->sensor = subdev;
2341 - unicam_dbg(1, unicam, "Using sensor %s for capture\n", subdev->name);
2346 -static int register_node(struct unicam_device *unicam, struct unicam_node *node,
2347 - enum v4l2_buf_type type, int pad_id)
2349 - struct video_device *vdev;
2350 - struct vb2_queue *q;
2351 - struct v4l2_mbus_framefmt mbus_fmt = {0};
2352 - const struct unicam_fmt *fmt;
2355 - if (unicam->sensor_embedded_data || pad_id != METADATA_PAD) {
2356 - ret = __subdev_get_format(unicam, &mbus_fmt, pad_id);
2358 - unicam_err(unicam, "Failed to get_format - ret %d\n",
2363 - fmt = find_format_by_code(mbus_fmt.code);
2365 - /* Find the first format that the sensor and unicam both
2368 - fmt = get_first_supported_format(unicam);
2371 - /* No compatible formats */
2374 - mbus_fmt.code = fmt->code;
2375 - ret = __subdev_set_format(unicam, &mbus_fmt, pad_id);
2379 - if (mbus_fmt.field != V4L2_FIELD_NONE) {
2380 - /* Interlaced not supported - disable it now. */
2381 - mbus_fmt.field = V4L2_FIELD_NONE;
2382 - ret = __subdev_set_format(unicam, &mbus_fmt, pad_id);
2387 - /* Fix this node format as embedded data. */
2388 - fmt = find_format_by_code(MEDIA_BUS_FMT_SENSOR_DATA);
2391 - node->dev = unicam;
2392 - node->pad_id = pad_id;
2394 - if (fmt->fourcc) {
2395 - if (fmt->fourcc != V4L2_META_FMT_SENSOR_DATA)
2396 - node->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
2398 - node->v_fmt.fmt.meta.dataformat = fmt->fourcc;
2400 - node->v_fmt.fmt.pix.pixelformat = fmt->repacked_fourcc;
2403 - /* Read current subdev format */
2404 - unicam_reset_format(node);
2406 - if (v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
2407 - v4l2_std_id tvnorms;
2409 - if (WARN_ON(!v4l2_subdev_has_op(unicam->sensor, video,
2412 - * Subdevice should not advertise s_std but not
2417 - ret = v4l2_subdev_call(unicam->sensor, video,
2418 - g_tvnorms, &tvnorms);
2421 - node->video_dev.tvnorms |= tvnorms;
2424 - spin_lock_init(&node->dma_queue_lock);
2425 - mutex_init(&node->lock);
2427 - vdev = &node->video_dev;
2428 - if (pad_id == IMAGE_PAD) {
2429 - /* Add controls from the subdevice */
2430 - ret = v4l2_ctrl_add_handler(&node->ctrl_handler,
2431 - unicam->sensor->ctrl_handler, NULL,
2437 - * If the sensor subdevice has any controls, associate the node
2438 - * with the ctrl handler to allow access from userland.
2440 - if (!list_empty(&node->ctrl_handler.ctrls))
2441 - vdev->ctrl_handler = &node->ctrl_handler;
2444 - q = &node->buffer_queue;
2446 - q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2447 - q->drv_priv = node;
2448 - q->ops = &unicam_video_qops;
2449 - q->mem_ops = &vb2_dma_contig_memops;
2450 - q->buf_struct_size = sizeof(struct unicam_buffer);
2451 - q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2452 - q->lock = &node->lock;
2453 - q->min_buffers_needed = 1;
2454 - q->dev = &unicam->pdev->dev;
2456 - ret = vb2_queue_init(q);
2458 - unicam_err(unicam, "vb2_queue_init() failed\n");
2462 - INIT_LIST_HEAD(&node->dma_queue.active);
2464 - vdev->release = video_device_release_empty;
2465 - vdev->fops = &unicam_fops;
2466 - vdev->ioctl_ops = &unicam_ioctl_ops;
2467 - vdev->v4l2_dev = &unicam->v4l2_dev;
2468 - vdev->vfl_dir = VFL_DIR_RX;
2470 - vdev->lock = &node->lock;
2471 - vdev->device_caps = (pad_id == IMAGE_PAD) ?
2472 - (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING) :
2473 - (V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING);
2475 - /* Define the device names */
2476 - snprintf(vdev->name, sizeof(vdev->name), "%s-%s", UNICAM_MODULE_NAME,
2477 - node->pad_id == IMAGE_PAD ? "image" : "embedded");
2479 - video_set_drvdata(vdev, node);
2480 - vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
2482 - node->dummy_buf_cpu_addr = dma_alloc_coherent(&unicam->pdev->dev,
2484 - &node->dummy_buf_dma_addr,
2486 - if (!node->dummy_buf_cpu_addr) {
2487 - unicam_err(unicam, "Unable to allocate dummy buffer.\n");
2491 - if (node->pad_id == METADATA_PAD) {
2492 - v4l2_disable_ioctl(vdev, VIDIOC_DQEVENT);
2493 - v4l2_disable_ioctl(vdev, VIDIOC_SUBSCRIBE_EVENT);
2494 - v4l2_disable_ioctl(vdev, VIDIOC_UNSUBSCRIBE_EVENT);
2496 - if (node->pad_id == METADATA_PAD ||
2497 - !v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
2498 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_STD);
2499 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_STD);
2500 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUMSTD);
2502 - if (node->pad_id == METADATA_PAD ||
2503 - !v4l2_subdev_has_op(unicam->sensor, video, querystd))
2504 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_QUERYSTD);
2505 - if (node->pad_id == METADATA_PAD ||
2506 - !v4l2_subdev_has_op(unicam->sensor, video, s_dv_timings)) {
2507 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_EDID);
2508 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_EDID);
2509 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_DV_TIMINGS_CAP);
2510 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_DV_TIMINGS);
2511 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_DV_TIMINGS);
2512 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUM_DV_TIMINGS);
2513 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_QUERY_DV_TIMINGS);
2515 - if (node->pad_id == METADATA_PAD ||
2516 - !v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_interval))
2517 - v4l2_disable_ioctl(&node->video_dev,
2518 - VIDIOC_ENUM_FRAMEINTERVALS);
2519 - if (node->pad_id == METADATA_PAD ||
2520 - !v4l2_subdev_has_op(unicam->sensor, video, g_frame_interval))
2521 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_PARM);
2522 - if (node->pad_id == METADATA_PAD ||
2523 - !v4l2_subdev_has_op(unicam->sensor, video, s_frame_interval))
2524 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_PARM);
2526 - if (node->pad_id == METADATA_PAD ||
2527 - !v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_size))
2528 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUM_FRAMESIZES);
2530 - if (node->pad_id == METADATA_PAD ||
2531 - !v4l2_subdev_has_op(unicam->sensor, pad, set_selection))
2532 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_SELECTION);
2534 - if (node->pad_id == METADATA_PAD ||
2535 - !v4l2_subdev_has_op(unicam->sensor, pad, get_selection))
2536 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_SELECTION);
2538 - ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
2540 - unicam_err(unicam, "Unable to register video device.\n");
2543 - node->registered = 1;
2545 - if (unicam->sensor_embedded_data) {
2546 - ret = media_create_pad_link(&unicam->sensor->entity, pad_id,
2547 - &node->video_dev.entity, 0,
2548 - MEDIA_LNK_FL_ENABLED |
2549 - MEDIA_LNK_FL_IMMUTABLE);
2551 - unicam_err(unicam, "Unable to create pad links.\n");
2557 -static void unregister_nodes(struct unicam_device *unicam)
2559 - struct unicam_node *node;
2562 - for (i = 0; i < MAX_NODES; i++) {
2563 - node = &unicam->node[i];
2564 - if (node->dummy_buf_cpu_addr) {
2565 - dma_free_coherent(&unicam->pdev->dev, DUMMY_BUF_SIZE,
2566 - node->dummy_buf_cpu_addr,
2567 - node->dummy_buf_dma_addr);
2569 - if (node->registered) {
2570 - video_unregister_device(&node->video_dev);
2571 - node->registered = 0;
2576 -static int unicam_probe_complete(struct unicam_device *unicam)
2580 - v4l2_set_subdev_hostdata(unicam->sensor, unicam);
2582 - unicam->v4l2_dev.notify = unicam_notify;
2584 - unicam->sensor_config = v4l2_subdev_alloc_pad_config(unicam->sensor);
2585 - if (!unicam->sensor_config)
2588 - unicam->sensor_embedded_data = (unicam->sensor->entity.num_pads >= 2);
2590 - ret = register_node(unicam, &unicam->node[IMAGE_PAD],
2591 - V4L2_BUF_TYPE_VIDEO_CAPTURE, IMAGE_PAD);
2593 - unicam_err(unicam, "Unable to register subdev node 0.\n");
2597 - ret = register_node(unicam, &unicam->node[METADATA_PAD],
2598 - V4L2_BUF_TYPE_META_CAPTURE, METADATA_PAD);
2600 - unicam_err(unicam, "Unable to register subdev node 1.\n");
2604 - ret = v4l2_device_register_ro_subdev_nodes(&unicam->v4l2_dev);
2606 - unicam_err(unicam, "Unable to register subdev nodes.\n");
2613 - unregister_nodes(unicam);
2618 -static int unicam_async_complete(struct v4l2_async_notifier *notifier)
2620 - struct unicam_device *unicam = container_of(notifier->v4l2_dev,
2621 - struct unicam_device, v4l2_dev);
2623 - return unicam_probe_complete(unicam);
2626 -static const struct v4l2_async_notifier_operations unicam_async_ops = {
2627 - .bound = unicam_async_bound,
2628 - .complete = unicam_async_complete,
2631 -static int of_unicam_connect_subdevs(struct unicam_device *dev)
2633 - struct platform_device *pdev = dev->pdev;
2634 - struct device_node *parent, *ep_node = NULL, *remote_ep = NULL,
2635 - *sensor_node = NULL;
2636 - struct v4l2_fwnode_endpoint *ep;
2637 - struct v4l2_async_subdev *asd;
2638 - unsigned int peripheral_data_lanes;
2639 - int ret = -EINVAL;
2640 - unsigned int lane;
2642 - parent = pdev->dev.of_node;
2645 - ep = &dev->endpoint;
2647 - ep_node = of_graph_get_next_endpoint(parent, NULL);
2649 - unicam_dbg(3, dev, "can't get next endpoint\n");
2650 - goto cleanup_exit;
2653 - unicam_dbg(3, dev, "ep_node is %s\n", ep_node->name);
2655 - v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), ep);
2657 - for (lane = 0; lane < ep->bus.mipi_csi2.num_data_lanes; lane++) {
2658 - if (ep->bus.mipi_csi2.data_lanes[lane] != lane + 1) {
2659 - unicam_err(dev, "Local endpoint - data lane reordering not supported\n");
2660 - goto cleanup_exit;
2664 - peripheral_data_lanes = ep->bus.mipi_csi2.num_data_lanes;
2666 - sensor_node = of_graph_get_remote_port_parent(ep_node);
2667 - if (!sensor_node) {
2668 - unicam_dbg(3, dev, "can't get remote parent\n");
2669 - goto cleanup_exit;
2671 - unicam_dbg(3, dev, "sensor_node is %s\n", sensor_node->name);
2672 - asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
2673 - asd->match.fwnode = of_fwnode_handle(sensor_node);
2675 - remote_ep = of_graph_get_remote_endpoint(ep_node);
2677 - unicam_dbg(3, dev, "can't get remote-endpoint\n");
2678 - goto cleanup_exit;
2680 - unicam_dbg(3, dev, "remote_ep is %s\n", remote_ep->name);
2681 - v4l2_fwnode_endpoint_parse(of_fwnode_handle(remote_ep), ep);
2682 - unicam_dbg(3, dev, "parsed remote_ep to endpoint. nr_of_link_frequencies %u, bus_type %u\n",
2683 - ep->nr_of_link_frequencies, ep->bus_type);
2685 - switch (ep->bus_type) {
2686 - case V4L2_MBUS_CSI2_DPHY:
2687 - if (ep->bus.mipi_csi2.num_data_lanes >
2688 - peripheral_data_lanes) {
2689 - unicam_err(dev, "Subdevice %s wants too many data lanes (%u > %u)\n",
2690 - sensor_node->name,
2691 - ep->bus.mipi_csi2.num_data_lanes,
2692 - peripheral_data_lanes);
2693 - goto cleanup_exit;
2696 - lane < ep->bus.mipi_csi2.num_data_lanes;
2698 - if (ep->bus.mipi_csi2.data_lanes[lane] != lane + 1) {
2699 - unicam_err(dev, "Subdevice %s - incompatible data lane config\n",
2700 - sensor_node->name);
2701 - goto cleanup_exit;
2704 - dev->max_data_lanes = ep->bus.mipi_csi2.num_data_lanes;
2705 - dev->bus_flags = ep->bus.mipi_csi2.flags;
2707 - case V4L2_MBUS_CCP2:
2708 - if (ep->bus.mipi_csi1.clock_lane != 0 ||
2709 - ep->bus.mipi_csi1.data_lane != 1) {
2710 - unicam_err(dev, "Subdevice %s incompatible lane config\n",
2711 - sensor_node->name);
2712 - goto cleanup_exit;
2714 - dev->max_data_lanes = 1;
2715 - dev->bus_flags = ep->bus.mipi_csi1.strobe;
2718 - /* Unsupported bus type */
2719 - unicam_err(dev, "sub-device %s is not a CSI2 or CCP2 device %d\n",
2720 - sensor_node->name, ep->bus_type);
2721 - goto cleanup_exit;
2724 - /* Store bus type - CSI2 or CCP2 */
2725 - dev->bus_type = ep->bus_type;
2726 - unicam_dbg(3, dev, "bus_type is %d\n", dev->bus_type);
2728 - /* Store Virtual Channel number */
2729 - dev->virtual_channel = ep->base.id;
2731 - unicam_dbg(3, dev, "v4l2-endpoint: %s\n",
2732 - dev->bus_type == V4L2_MBUS_CSI2_DPHY ? "CSI2" : "CCP2");
2733 - unicam_dbg(3, dev, "Virtual Channel=%d\n", dev->virtual_channel);
2734 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY)
2735 - unicam_dbg(3, dev, "flags=0x%08x\n", ep->bus.mipi_csi2.flags);
2736 - unicam_dbg(3, dev, "num_data_lanes=%d\n", dev->max_data_lanes);
2738 - unicam_dbg(1, dev, "found sub-device %s\n", sensor_node->name);
2740 - v4l2_async_notifier_init(&dev->notifier);
2742 - ret = v4l2_async_notifier_add_subdev(&dev->notifier, asd);
2744 - unicam_err(dev, "Error adding subdevice - ret %d\n", ret);
2745 - goto cleanup_exit;
2748 - dev->notifier.ops = &unicam_async_ops;
2749 - ret = v4l2_async_notifier_register(&dev->v4l2_dev,
2752 - unicam_err(dev, "Error registering async notifier - ret %d\n",
2759 - of_node_put(remote_ep);
2761 - of_node_put(sensor_node);
2763 - of_node_put(ep_node);
2768 -static int unicam_probe(struct platform_device *pdev)
2770 - struct unicam_cfg *unicam_cfg;
2771 - struct unicam_device *unicam;
2772 - struct v4l2_ctrl_handler *hdl;
2773 - struct resource *res;
2776 - unicam = devm_kzalloc(&pdev->dev, sizeof(*unicam), GFP_KERNEL);
2780 - unicam->pdev = pdev;
2781 - unicam_cfg = &unicam->cfg;
2783 - res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2784 - unicam_cfg->base = devm_ioremap_resource(&pdev->dev, res);
2785 - if (IS_ERR(unicam_cfg->base)) {
2786 - unicam_err(unicam, "Failed to get main io block\n");
2787 - return PTR_ERR(unicam_cfg->base);
2790 - res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2791 - unicam_cfg->clk_gate_base = devm_ioremap_resource(&pdev->dev, res);
2792 - if (IS_ERR(unicam_cfg->clk_gate_base)) {
2793 - unicam_err(unicam, "Failed to get 2nd io block\n");
2794 - return PTR_ERR(unicam_cfg->clk_gate_base);
2797 - unicam->clock = devm_clk_get(&pdev->dev, "lp");
2798 - if (IS_ERR(unicam->clock)) {
2799 - unicam_err(unicam, "Failed to get clock\n");
2800 - return PTR_ERR(unicam->clock);
2803 - ret = platform_get_irq(pdev, 0);
2805 - dev_err(&pdev->dev, "No IRQ resource\n");
2809 - ret = devm_request_irq(&pdev->dev, ret, unicam_isr, 0,
2810 - "unicam_capture0", unicam);
2812 - dev_err(&pdev->dev, "Unable to request interrupt\n");
2816 - unicam->mdev.dev = &pdev->dev;
2817 - strscpy(unicam->mdev.model, UNICAM_MODULE_NAME,
2818 - sizeof(unicam->mdev.model));
2819 - strscpy(unicam->mdev.serial, "", sizeof(unicam->mdev.serial));
2820 - snprintf(unicam->mdev.bus_info, sizeof(unicam->mdev.bus_info),
2822 - pdev->dev.driver->name, dev_name(&pdev->dev));
2823 - unicam->mdev.hw_revision = 1;
2825 - media_entity_pads_init(&unicam->node[IMAGE_PAD].video_dev.entity, 1,
2826 - &unicam->node[IMAGE_PAD].pad);
2827 - media_entity_pads_init(&unicam->node[METADATA_PAD].video_dev.entity, 1,
2828 - &unicam->node[METADATA_PAD].pad);
2829 - media_device_init(&unicam->mdev);
2831 - unicam->v4l2_dev.mdev = &unicam->mdev;
2833 - ret = v4l2_device_register(&pdev->dev, &unicam->v4l2_dev);
2835 - unicam_err(unicam,
2836 - "Unable to register v4l2 device.\n");
2837 - goto media_cleanup;
2840 - ret = media_device_register(&unicam->mdev);
2842 - unicam_err(unicam,
2843 - "Unable to register media-controller device.\n");
2844 - goto probe_out_v4l2_unregister;
2847 - /* Reserve space for the controls */
2848 - hdl = &unicam->node[IMAGE_PAD].ctrl_handler;
2849 - ret = v4l2_ctrl_handler_init(hdl, 16);
2851 - goto media_unregister;
2853 - /* set the driver data in platform device */
2854 - platform_set_drvdata(pdev, unicam);
2856 - ret = of_unicam_connect_subdevs(unicam);
2858 - dev_err(&pdev->dev, "Failed to connect subdevs\n");
2862 - /* Enable the block power domain */
2863 - pm_runtime_enable(&pdev->dev);
2868 - v4l2_ctrl_handler_free(hdl);
2870 - media_device_unregister(&unicam->mdev);
2871 -probe_out_v4l2_unregister:
2872 - v4l2_device_unregister(&unicam->v4l2_dev);
2874 - media_device_cleanup(&unicam->mdev);
2879 -static int unicam_remove(struct platform_device *pdev)
2881 - struct unicam_device *unicam = platform_get_drvdata(pdev);
2883 - unicam_dbg(2, unicam, "%s\n", __func__);
2885 - pm_runtime_disable(&pdev->dev);
2887 - v4l2_async_notifier_unregister(&unicam->notifier);
2888 - v4l2_ctrl_handler_free(&unicam->node[IMAGE_PAD].ctrl_handler);
2889 - v4l2_device_unregister(&unicam->v4l2_dev);
2890 - unregister_nodes(unicam);
2891 - if (unicam->sensor_config)
2892 - v4l2_subdev_free_pad_config(unicam->sensor_config);
2893 - media_device_unregister(&unicam->mdev);
2894 - media_device_cleanup(&unicam->mdev);
2899 -static const struct of_device_id unicam_of_match[] = {
2900 - { .compatible = "brcm,bcm2835-unicam", },
2901 - { /* sentinel */ },
2903 -MODULE_DEVICE_TABLE(of, unicam_of_match);
2905 -static struct platform_driver unicam_driver = {
2906 - .probe = unicam_probe,
2907 - .remove = unicam_remove,
2909 - .name = UNICAM_MODULE_NAME,
2910 - .of_match_table = of_match_ptr(unicam_of_match),
2914 -module_platform_driver(unicam_driver);
2916 -MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
2917 -MODULE_DESCRIPTION("BCM2835 Unicam driver");
2918 -MODULE_LICENSE("GPL");
2919 -MODULE_VERSION(UNICAM_VERSION);
2920 --- a/drivers/media/platform/bcm2835/vc4-regs-unicam.h
2923 -/* SPDX-License-Identifier: GPL-2.0-only */
2926 - * Copyright (C) 2017-2020 Raspberry Pi Trading.
2927 - * Dave Stevenson <dave.stevenson@raspberrypi.com>
2930 -#ifndef VC4_REGS_UNICAM_H
2931 -#define VC4_REGS_UNICAM_H
2934 - * The following values are taken from files found within the code drop
2935 - * made by Broadcom for the BCM21553 Graphics Driver, predominantly in
2936 - * brcm_usrlib/dag/vmcsx/vcinclude/hardware_vc4.h.
2937 - * They have been modified to be only the register offset.
2939 -#define UNICAM_CTRL 0x000
2940 -#define UNICAM_STA 0x004
2941 -#define UNICAM_ANA 0x008
2942 -#define UNICAM_PRI 0x00c
2943 -#define UNICAM_CLK 0x010
2944 -#define UNICAM_CLT 0x014
2945 -#define UNICAM_DAT0 0x018
2946 -#define UNICAM_DAT1 0x01c
2947 -#define UNICAM_DAT2 0x020
2948 -#define UNICAM_DAT3 0x024
2949 -#define UNICAM_DLT 0x028
2950 -#define UNICAM_CMP0 0x02c
2951 -#define UNICAM_CMP1 0x030
2952 -#define UNICAM_CAP0 0x034
2953 -#define UNICAM_CAP1 0x038
2954 -#define UNICAM_ICTL 0x100
2955 -#define UNICAM_ISTA 0x104
2956 -#define UNICAM_IDI0 0x108
2957 -#define UNICAM_IPIPE 0x10c
2958 -#define UNICAM_IBSA0 0x110
2959 -#define UNICAM_IBEA0 0x114
2960 -#define UNICAM_IBLS 0x118
2961 -#define UNICAM_IBWP 0x11c
2962 -#define UNICAM_IHWIN 0x120
2963 -#define UNICAM_IHSTA 0x124
2964 -#define UNICAM_IVWIN 0x128
2965 -#define UNICAM_IVSTA 0x12c
2966 -#define UNICAM_ICC 0x130
2967 -#define UNICAM_ICS 0x134
2968 -#define UNICAM_IDC 0x138
2969 -#define UNICAM_IDPO 0x13c
2970 -#define UNICAM_IDCA 0x140
2971 -#define UNICAM_IDCD 0x144
2972 -#define UNICAM_IDS 0x148
2973 -#define UNICAM_DCS 0x200
2974 -#define UNICAM_DBSA0 0x204
2975 -#define UNICAM_DBEA0 0x208
2976 -#define UNICAM_DBWP 0x20c
2977 -#define UNICAM_DBCTL 0x300
2978 -#define UNICAM_IBSA1 0x304
2979 -#define UNICAM_IBEA1 0x308
2980 -#define UNICAM_IDI1 0x30c
2981 -#define UNICAM_DBSA1 0x310
2982 -#define UNICAM_DBEA1 0x314
2983 -#define UNICAM_MISC 0x400
2986 - * The following bitmasks are from the kernel released by Broadcom
2987 - * for Android - https://android.googlesource.com/kernel/bcm/
2988 - * The Rhea, Hawaii, and Java chips all contain the same VideoCore4
2989 - * Unicam block as BCM2835, as defined in eg
2990 - * arch/arm/mach-rhea/include/mach/rdb_A0/brcm_rdb_cam.h and similar.
2991 - * Values reworked to use the kernel BIT and GENMASK macros.
2993 - * Some of the bit mnenomics have been amended to match the datasheet.
2995 -/* UNICAM_CTRL Register */
2996 -#define UNICAM_CPE BIT(0)
2997 -#define UNICAM_MEM BIT(1)
2998 -#define UNICAM_CPR BIT(2)
2999 -#define UNICAM_CPM_MASK GENMASK(3, 3)
3000 -#define UNICAM_CPM_CSI2 0
3001 -#define UNICAM_CPM_CCP2 1
3002 -#define UNICAM_SOE BIT(4)
3003 -#define UNICAM_DCM_MASK GENMASK(5, 5)
3004 -#define UNICAM_DCM_STROBE 0
3005 -#define UNICAM_DCM_DATA 1
3006 -#define UNICAM_SLS BIT(6)
3007 -#define UNICAM_PFT_MASK GENMASK(11, 8)
3008 -#define UNICAM_OET_MASK GENMASK(20, 12)
3010 -/* UNICAM_STA Register */
3011 -#define UNICAM_SYN BIT(0)
3012 -#define UNICAM_CS BIT(1)
3013 -#define UNICAM_SBE BIT(2)
3014 -#define UNICAM_PBE BIT(3)
3015 -#define UNICAM_HOE BIT(4)
3016 -#define UNICAM_PLE BIT(5)
3017 -#define UNICAM_SSC BIT(6)
3018 -#define UNICAM_CRCE BIT(7)
3019 -#define UNICAM_OES BIT(8)
3020 -#define UNICAM_IFO BIT(9)
3021 -#define UNICAM_OFO BIT(10)
3022 -#define UNICAM_BFO BIT(11)
3023 -#define UNICAM_DL BIT(12)
3024 -#define UNICAM_PS BIT(13)
3025 -#define UNICAM_IS BIT(14)
3026 -#define UNICAM_PI0 BIT(15)
3027 -#define UNICAM_PI1 BIT(16)
3028 -#define UNICAM_FSI_S BIT(17)
3029 -#define UNICAM_FEI_S BIT(18)
3030 -#define UNICAM_LCI_S BIT(19)
3031 -#define UNICAM_BUF0_RDY BIT(20)
3032 -#define UNICAM_BUF0_NO BIT(21)
3033 -#define UNICAM_BUF1_RDY BIT(22)
3034 -#define UNICAM_BUF1_NO BIT(23)
3035 -#define UNICAM_DI BIT(24)
3037 -#define UNICAM_STA_MASK_ALL \
3051 -/* UNICAM_ANA Register */
3052 -#define UNICAM_APD BIT(0)
3053 -#define UNICAM_BPD BIT(1)
3054 -#define UNICAM_AR BIT(2)
3055 -#define UNICAM_DDL BIT(3)
3056 -#define UNICAM_CTATADJ_MASK GENMASK(7, 4)
3057 -#define UNICAM_PTATADJ_MASK GENMASK(11, 8)
3059 -/* UNICAM_PRI Register */
3060 -#define UNICAM_PE BIT(0)
3061 -#define UNICAM_PT_MASK GENMASK(2, 1)
3062 -#define UNICAM_NP_MASK GENMASK(7, 4)
3063 -#define UNICAM_PP_MASK GENMASK(11, 8)
3064 -#define UNICAM_BS_MASK GENMASK(15, 12)
3065 -#define UNICAM_BL_MASK GENMASK(17, 16)
3067 -/* UNICAM_CLK Register */
3068 -#define UNICAM_CLE BIT(0)
3069 -#define UNICAM_CLPD BIT(1)
3070 -#define UNICAM_CLLPE BIT(2)
3071 -#define UNICAM_CLHSE BIT(3)
3072 -#define UNICAM_CLTRE BIT(4)
3073 -#define UNICAM_CLAC_MASK GENMASK(8, 5)
3074 -#define UNICAM_CLSTE BIT(29)
3076 -/* UNICAM_CLT Register */
3077 -#define UNICAM_CLT1_MASK GENMASK(7, 0)
3078 -#define UNICAM_CLT2_MASK GENMASK(15, 8)
3080 -/* UNICAM_DATn Registers */
3081 -#define UNICAM_DLE BIT(0)
3082 -#define UNICAM_DLPD BIT(1)
3083 -#define UNICAM_DLLPE BIT(2)
3084 -#define UNICAM_DLHSE BIT(3)
3085 -#define UNICAM_DLTRE BIT(4)
3086 -#define UNICAM_DLSM BIT(5)
3087 -#define UNICAM_DLFO BIT(28)
3088 -#define UNICAM_DLSTE BIT(29)
3090 -#define UNICAM_DAT_MASK_ALL (UNICAM_DLSTE + UNICAM_DLFO)
3092 -/* UNICAM_DLT Register */
3093 -#define UNICAM_DLT1_MASK GENMASK(7, 0)
3094 -#define UNICAM_DLT2_MASK GENMASK(15, 8)
3095 -#define UNICAM_DLT3_MASK GENMASK(23, 16)
3097 -/* UNICAM_ICTL Register */
3098 -#define UNICAM_FSIE BIT(0)
3099 -#define UNICAM_FEIE BIT(1)
3100 -#define UNICAM_IBOB BIT(2)
3101 -#define UNICAM_FCM BIT(3)
3102 -#define UNICAM_TFC BIT(4)
3103 -#define UNICAM_LIP_MASK GENMASK(6, 5)
3104 -#define UNICAM_LCIE_MASK GENMASK(28, 16)
3106 -/* UNICAM_IDI0/1 Register */
3107 -#define UNICAM_ID0_MASK GENMASK(7, 0)
3108 -#define UNICAM_ID1_MASK GENMASK(15, 8)
3109 -#define UNICAM_ID2_MASK GENMASK(23, 16)
3110 -#define UNICAM_ID3_MASK GENMASK(31, 24)
3112 -/* UNICAM_ISTA Register */
3113 -#define UNICAM_FSI BIT(0)
3114 -#define UNICAM_FEI BIT(1)
3115 -#define UNICAM_LCI BIT(2)
3117 -#define UNICAM_ISTA_MASK_ALL (UNICAM_FSI + UNICAM_FEI + UNICAM_LCI)
3119 -/* UNICAM_IPIPE Register */
3120 -#define UNICAM_PUM_MASK GENMASK(2, 0)
3121 - /* Unpacking modes */
3122 - #define UNICAM_PUM_NONE 0
3123 - #define UNICAM_PUM_UNPACK6 1
3124 - #define UNICAM_PUM_UNPACK7 2
3125 - #define UNICAM_PUM_UNPACK8 3
3126 - #define UNICAM_PUM_UNPACK10 4
3127 - #define UNICAM_PUM_UNPACK12 5
3128 - #define UNICAM_PUM_UNPACK14 6
3129 - #define UNICAM_PUM_UNPACK16 7
3130 -#define UNICAM_DDM_MASK GENMASK(6, 3)
3131 -#define UNICAM_PPM_MASK GENMASK(9, 7)
3132 - /* Packing modes */
3133 - #define UNICAM_PPM_NONE 0
3134 - #define UNICAM_PPM_PACK8 1
3135 - #define UNICAM_PPM_PACK10 2
3136 - #define UNICAM_PPM_PACK12 3
3137 - #define UNICAM_PPM_PACK14 4
3138 - #define UNICAM_PPM_PACK16 5
3139 -#define UNICAM_DEM_MASK GENMASK(11, 10)
3140 -#define UNICAM_DEBL_MASK GENMASK(14, 12)
3141 -#define UNICAM_ICM_MASK GENMASK(16, 15)
3142 -#define UNICAM_IDM_MASK GENMASK(17, 17)
3144 -/* UNICAM_ICC Register */
3145 -#define UNICAM_ICFL_MASK GENMASK(4, 0)
3146 -#define UNICAM_ICFH_MASK GENMASK(9, 5)
3147 -#define UNICAM_ICST_MASK GENMASK(12, 10)
3148 -#define UNICAM_ICLT_MASK GENMASK(15, 13)
3149 -#define UNICAM_ICLL_MASK GENMASK(31, 16)
3151 -/* UNICAM_DCS Register */
3152 -#define UNICAM_DIE BIT(0)
3153 -#define UNICAM_DIM BIT(1)
3154 -#define UNICAM_DBOB BIT(3)
3155 -#define UNICAM_FDE BIT(4)
3156 -#define UNICAM_LDP BIT(5)
3157 -#define UNICAM_EDL_MASK GENMASK(15, 8)
3159 -/* UNICAM_DBCTL Register */
3160 -#define UNICAM_DBEN BIT(0)
3161 -#define UNICAM_BUF0_IE BIT(1)
3162 -#define UNICAM_BUF1_IE BIT(2)
3164 -/* UNICAM_CMP[0,1] register */
3165 -#define UNICAM_PCE BIT(31)
3166 -#define UNICAM_GI BIT(9)
3167 -#define UNICAM_CPH BIT(8)
3168 -#define UNICAM_PCVC_MASK GENMASK(7, 6)
3169 -#define UNICAM_PCDT_MASK GENMASK(5, 0)
3171 -/* UNICAM_MISC register */
3172 -#define UNICAM_FL0 BIT(6)
3173 -#define UNICAM_FL1 BIT(9)