1 From a961b21904be2f527afd87000d6bbf11d4ddecc8 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.org>
3 Date: Mon, 11 Sep 2017 15:42:28 +0100
4 Subject: [PATCH 332/454] bcm2835-unicam: Driver for CCP2/CSI2 camera interface
6 Add driver for the Unicam camera receiver block on
9 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
11 drivers/media/platform/Kconfig | 1 +
12 drivers/media/platform/Makefile | 1 +
13 drivers/media/platform/bcm2835/Kconfig | 14 +
14 drivers/media/platform/bcm2835/Makefile | 3 +
15 .../media/platform/bcm2835/bcm2835-unicam.c | 2100 +++++++++++++++++
16 .../media/platform/bcm2835/vc4-regs-unicam.h | 264 +++
17 6 files changed, 2383 insertions(+)
18 create mode 100644 drivers/media/platform/bcm2835/Kconfig
19 create mode 100644 drivers/media/platform/bcm2835/Makefile
20 create mode 100644 drivers/media/platform/bcm2835/bcm2835-unicam.c
21 create mode 100644 drivers/media/platform/bcm2835/vc4-regs-unicam.h
23 --- a/drivers/media/platform/Kconfig
24 +++ b/drivers/media/platform/Kconfig
25 @@ -150,6 +150,7 @@ source "drivers/media/platform/am437x/Kc
26 source "drivers/media/platform/xilinx/Kconfig"
27 source "drivers/media/platform/rcar-vin/Kconfig"
28 source "drivers/media/platform/atmel/Kconfig"
29 +source "drivers/media/platform/bcm2835/Kconfig"
32 tristate "TI CAL (Camera Adaptation Layer) driver"
33 --- a/drivers/media/platform/Makefile
34 +++ b/drivers/media/platform/Makefile
35 @@ -91,3 +91,4 @@ obj-$(CONFIG_VIDEO_QCOM_CAMSS) += qcom/
36 obj-$(CONFIG_VIDEO_QCOM_VENUS) += qcom/venus/
41 +++ b/drivers/media/platform/bcm2835/Kconfig
43 +# Broadcom VideoCore4 V4L2 camera support
45 +config VIDEO_BCM2835_UNICAM
46 + tristate "Broadcom BCM2835 Unicam video capture driver"
47 + depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
48 + depends on ARCH_BCM2835 || COMPILE_TEST
49 + select VIDEOBUF2_DMA_CONTIG
52 + Say Y here to enable V4L2 subdevice for CSI2 receiver.
53 + This is a V4L2 subdevice that interfaces directly to the VC4 peripheral.
55 + To compile this driver as a module, choose M here. The module
56 + will be called bcm2835-unicam.
58 +++ b/drivers/media/platform/bcm2835/Makefile
60 +# Makefile for BCM2835 Unicam driver
62 +obj-$(CONFIG_VIDEO_BCM2835_UNICAM) += bcm2835-unicam.o
64 +++ b/drivers/media/platform/bcm2835/bcm2835-unicam.c
67 + * BCM2835 Unicam capture Driver
69 + * Copyright (C) 2017 - Raspberry Pi (Trading) Ltd.
71 + * Dave Stevenson <dave.stevenson@raspberrypi.org>
73 + * Based on TI am437x driver by Benoit Parrot and Lad, Prabhakar and
74 + * TI CAL camera interface driver by Benoit Parrot.
77 + * There are two camera drivers in the kernel for BCM283x - this one
78 + * and bcm2835-camera (currently in staging).
80 + * This driver directly controls the Unicam peripheral - there is no
81 + * involvement with the VideoCore firmware. Unicam receives CSI-2 or
82 + * CCP2 data and writes it into SDRAM. The only potential processing options are
83 + * to repack Bayer data into an alternate format, and applying windowing.
84 + * The repacking does not shift the data, so could repack V4L2_PIX_FMT_Sxxxx10P
85 + * to V4L2_PIX_FMT_Sxxxx10, or V4L2_PIX_FMT_Sxxxx12P to V4L2_PIX_FMT_Sxxxx12,
86 + * but not generically up to V4L2_PIX_FMT_Sxxxx16.
87 + * Adding support for repacking and windowing may be added later.
89 + * It should be possible to connect this driver to any sensor with a
90 + * suitable output interface and V4L2 subdevice driver.
92 + * bcm2835-camera uses the VideoCore firmware to control the sensor,
93 + * Unicam, ISP, and all tuner control loops. Fully processed frames are
94 + * delivered to the driver by the firmware. It only has sensor drivers
95 + * for Omnivision OV5647, and Sony IMX219 sensors.
97 + * The two drivers are mutually exclusive for the same Unicam instance.
98 + * The VideoCore firmware checks the device tree configuration during boot.
99 + * If it finds device tree nodes called csi0 or csi1 it will block the
100 + * firmware from accessing the peripheral, and bcm2835-camera will
101 + * not be able to stream data.
104 + * This program is free software; you may redistribute it and/or modify
105 + * it under the terms of the GNU General Public License as published by
106 + * the Free Software Foundation; version 2 of the License.
108 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
109 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
110 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
111 + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
112 + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
113 + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
114 + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
118 +#include <linux/clk.h>
119 +#include <linux/delay.h>
120 +#include <linux/device.h>
121 +#include <linux/err.h>
122 +#include <linux/init.h>
123 +#include <linux/interrupt.h>
124 +#include <linux/io.h>
125 +#include <linux/module.h>
126 +#include <linux/of_device.h>
127 +#include <linux/of_graph.h>
128 +#include <linux/pinctrl/consumer.h>
129 +#include <linux/platform_device.h>
130 +#include <linux/pm_runtime.h>
131 +#include <linux/slab.h>
132 +#include <linux/uaccess.h>
133 +#include <linux/videodev2.h>
135 +#include <media/v4l2-common.h>
136 +#include <media/v4l2-ctrls.h>
137 +#include <media/v4l2-dev.h>
138 +#include <media/v4l2-device.h>
139 +#include <media/v4l2-dv-timings.h>
140 +#include <media/v4l2-event.h>
141 +#include <media/v4l2-ioctl.h>
142 +#include <media/v4l2-fwnode.h>
143 +#include <media/videobuf2-dma-contig.h>
145 +#include "vc4-regs-unicam.h"
147 +#define UNICAM_MODULE_NAME "unicam"
148 +#define UNICAM_VERSION "0.1.0"
151 +module_param(debug, int, 0644);
152 +MODULE_PARM_DESC(debug, "Debug level 0-3");
154 +#define unicam_dbg(level, dev, fmt, arg...) \
155 + v4l2_dbg(level, debug, &(dev)->v4l2_dev, fmt, ##arg)
156 +#define unicam_info(dev, fmt, arg...) \
157 + v4l2_info(&(dev)->v4l2_dev, fmt, ##arg)
158 +#define unicam_err(dev, fmt, arg...) \
159 + v4l2_err(&(dev)->v4l2_dev, fmt, ##arg)
162 + * Stride is a 16 bit register, but also has to be a multiple of 16.
164 +#define BPL_ALIGNMENT 16
165 +#define MAX_BYTESPERLINE ((1 << 16) - BPL_ALIGNMENT)
167 + * Max width is therefore determined by the max stride divided by
168 + * the number of bits per pixel. Take 32bpp as a
170 + * No imposed limit on the height, so adopt a square image for want
171 + * of anything better.
173 +#define MAX_WIDTH (MAX_BYTESPERLINE / 4)
174 +#define MAX_HEIGHT MAX_WIDTH
175 +/* Define a nominal minimum image size */
176 +#define MIN_WIDTH 16
177 +#define MIN_HEIGHT 16
179 + * Whilst Unicam doesn't require any additional padding on the image
180 + * height, various other parts of the BCM283x frameworks require a multiple
182 + * Seeing as image buffers are significantly larger than this extra
183 + * padding, add it in order to simplify integration.
185 +#define HEIGHT_ALIGNMENT 16
188 + * struct unicam_fmt - Unicam media bus format information
189 + * @pixelformat: V4L2 pixel format FCC identifier.
190 + * @code: V4L2 media bus format code.
191 + * @depth: Bits per pixel (when stored in memory).
192 + * @csi_dt: CSI data type.
201 +static const struct unicam_fmt formats[] = {
204 + .fourcc = V4L2_PIX_FMT_YUYV,
205 + .code = MEDIA_BUS_FMT_YUYV8_2X8,
209 + .fourcc = V4L2_PIX_FMT_UYVY,
210 + .code = MEDIA_BUS_FMT_UYVY8_2X8,
214 + .fourcc = V4L2_PIX_FMT_YVYU,
215 + .code = MEDIA_BUS_FMT_YVYU8_2X8,
219 + .fourcc = V4L2_PIX_FMT_VYUY,
220 + .code = MEDIA_BUS_FMT_VYUY8_2X8,
224 + .fourcc = V4L2_PIX_FMT_YUYV,
225 + .code = MEDIA_BUS_FMT_YUYV8_1X16,
229 + .fourcc = V4L2_PIX_FMT_UYVY,
230 + .code = MEDIA_BUS_FMT_UYVY8_1X16,
234 + .fourcc = V4L2_PIX_FMT_YVYU,
235 + .code = MEDIA_BUS_FMT_YVYU8_1X16,
239 + .fourcc = V4L2_PIX_FMT_VYUY,
240 + .code = MEDIA_BUS_FMT_VYUY8_1X16,
245 + .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
246 + .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
250 + .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
251 + .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
255 + .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
256 + .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
260 + .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
261 + .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
265 + .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
266 + .code = MEDIA_BUS_FMT_RGB888_1X24,
270 + .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
271 + .code = MEDIA_BUS_FMT_BGR888_1X24,
275 + .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
276 + .code = MEDIA_BUS_FMT_ARGB8888_1X32,
280 + /* Bayer Formats */
281 + .fourcc = V4L2_PIX_FMT_SBGGR8,
282 + .code = MEDIA_BUS_FMT_SBGGR8_1X8,
286 + .fourcc = V4L2_PIX_FMT_SGBRG8,
287 + .code = MEDIA_BUS_FMT_SGBRG8_1X8,
291 + .fourcc = V4L2_PIX_FMT_SGRBG8,
292 + .code = MEDIA_BUS_FMT_SGRBG8_1X8,
296 + .fourcc = V4L2_PIX_FMT_SRGGB8,
297 + .code = MEDIA_BUS_FMT_SRGGB8_1X8,
301 + .fourcc = V4L2_PIX_FMT_SBGGR10P,
302 + .code = MEDIA_BUS_FMT_SBGGR10_1X10,
306 + .fourcc = V4L2_PIX_FMT_SGBRG10P,
307 + .code = MEDIA_BUS_FMT_SGBRG10_1X10,
311 + .fourcc = V4L2_PIX_FMT_SGRBG10P,
312 + .code = MEDIA_BUS_FMT_SGRBG10_1X10,
316 + .fourcc = V4L2_PIX_FMT_SRGGB10P,
317 + .code = MEDIA_BUS_FMT_SRGGB10_1X10,
321 + .fourcc = V4L2_PIX_FMT_SBGGR12P,
322 + .code = MEDIA_BUS_FMT_SBGGR12_1X12,
326 + .fourcc = V4L2_PIX_FMT_SGBRG12P,
327 + .code = MEDIA_BUS_FMT_SGBRG12_1X12,
331 + .fourcc = V4L2_PIX_FMT_SGRBG12P,
332 + .code = MEDIA_BUS_FMT_SGRBG12_1X12,
336 + .fourcc = V4L2_PIX_FMT_SRGGB12P,
337 + .code = MEDIA_BUS_FMT_SRGGB12_1X12,
342 + * 14 and 16 bit Bayer formats could be supported, but there are no V4L2
343 + * defines for 14bit packed Bayer, and no CSI2 data_type for raw 16.
347 +struct unicam_dmaqueue {
348 + struct list_head active;
351 +struct unicam_buffer {
352 + struct vb2_v4l2_buffer vb;
353 + struct list_head list;
357 + /* peripheral base address */
358 + void __iomem *base;
359 + /* clock gating base address */
360 + void __iomem *clk_gate_base;
363 +#define MAX_POSSIBLE_PIX_FMTS (ARRAY_SIZE(formats))
365 +struct unicam_device {
366 + /* V4l2 specific parameters */
367 + /* Identifies video device for this channel */
368 + struct video_device video_dev;
369 + struct v4l2_ctrl_handler ctrl_handler;
371 + struct v4l2_fwnode_endpoint endpoint;
373 + struct v4l2_async_subdev asd;
376 + struct unicam_cfg cfg;
380 + struct v4l2_device v4l2_dev;
381 + /* parent device */
382 + struct platform_device *pdev;
383 + /* subdevice async Notifier */
384 + struct v4l2_async_notifier notifier;
385 + unsigned int sequence;
387 + /* ptr to sub device */
388 + struct v4l2_subdev *sensor;
389 + /* Pad config for the sensor */
390 + struct v4l2_subdev_pad_config *sensor_config;
391 + /* current input at the sub device */
394 + /* Pointer pointing to current v4l2_buffer */
395 + struct unicam_buffer *cur_frm;
396 + /* Pointer pointing to next v4l2_buffer */
397 + struct unicam_buffer *next_frm;
399 + /* video capture */
400 + const struct unicam_fmt *fmt;
401 + /* Used to store current pixel format */
402 + struct v4l2_format v_fmt;
403 + /* Used to store current mbus frame format */
404 + struct v4l2_mbus_framefmt m_fmt;
406 + struct unicam_fmt active_fmts[MAX_POSSIBLE_PIX_FMTS];
407 + int num_active_fmt;
408 + unsigned int virtual_channel;
409 + enum v4l2_mbus_type bus_type;
411 + * Stores bus.mipi_csi2.flags for CSI2 sensors, or
412 + * bus.mipi_csi1.strobe for CCP2.
414 + unsigned int bus_flags;
415 + unsigned int max_data_lanes;
416 + unsigned int active_data_lanes;
418 + struct v4l2_rect crop;
420 + /* Currently selected input on subdev */
423 + /* Buffer queue used in video-buf */
424 + struct vb2_queue buffer_queue;
425 + /* Queue of filled frames */
426 + struct unicam_dmaqueue dma_queue;
427 + /* IRQ lock for DMA queue */
428 + spinlock_t dma_queue_lock;
429 + /* lock used to access this structure */
431 + /* Flag to denote that we are processing buffers */
435 +/* Hardware access */
436 +#define clk_write(dev, val) writel((val) | 0x5a000000, (dev)->clk_gate_base)
437 +#define clk_read(dev) readl((dev)->clk_gate_base)
439 +#define reg_read(dev, offset) readl((dev)->base + (offset))
440 +#define reg_write(dev, offset, val) writel(val, (dev)->base + (offset))
442 +#define reg_read_field(dev, offset, mask) get_field(reg_read((dev), (offset), \
445 +static inline int get_field(u32 value, u32 mask)
447 + return (value & mask) >> __ffs(mask);
450 +static inline void set_field(u32 *valp, u32 field, u32 mask)
455 + val |= (field << __ffs(mask)) & mask;
459 +static inline void reg_write_field(struct unicam_cfg *dev, u32 offset,
460 + u32 field, u32 mask)
462 + u32 val = reg_read((dev), (offset));
464 + set_field(&val, field, mask);
465 + reg_write((dev), (offset), val);
468 +/* Power management functions */
469 +static inline int unicam_runtime_get(struct unicam_device *dev)
473 + r = pm_runtime_get_sync(&dev->pdev->dev);
478 +static inline void unicam_runtime_put(struct unicam_device *dev)
480 + pm_runtime_put_sync(&dev->pdev->dev);
483 +/* Format setup functions */
484 +static int find_mbus_depth_by_code(u32 code)
486 + const struct unicam_fmt *fmt;
489 + for (k = 0; k < ARRAY_SIZE(formats); k++) {
491 + if (fmt->code == code)
498 +static const struct unicam_fmt *find_format_by_code(struct unicam_device *dev,
501 + const struct unicam_fmt *fmt;
504 + for (k = 0; k < dev->num_active_fmt; k++) {
505 + fmt = &dev->active_fmts[k];
506 + if (fmt->code == code)
513 +static const struct unicam_fmt *find_format_by_pix(struct unicam_device *dev,
516 + const struct unicam_fmt *fmt;
519 + for (k = 0; k < dev->num_active_fmt; k++) {
520 + fmt = &dev->active_fmts[k];
521 + if (fmt->fourcc == pixelformat)
528 +static void dump_active_formats(struct unicam_device *dev)
532 + for (i = 0; i < dev->num_active_fmt; i++) {
533 + unicam_dbg(3, dev, "active_fmt[%d] (%p) is code %04x, fourcc " V4L2_FOURCC_CONV ", depth %d\n",
534 + i, &dev->active_fmts[i], dev->active_fmts[i].code,
535 + V4L2_FOURCC_CONV_ARGS(dev->active_fmts[i].fourcc),
536 + dev->active_fmts[i].depth);
540 +static inline unsigned int bytes_per_line(u32 width,
541 + const struct unicam_fmt *fmt)
543 + return ALIGN((width * fmt->depth) >> 3, BPL_ALIGNMENT);
546 +static int __subdev_get_format(struct unicam_device *dev,
547 + struct v4l2_mbus_framefmt *fmt)
549 + struct v4l2_subdev_format sd_fmt = {0};
550 + struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
553 + sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
556 + ret = v4l2_subdev_call(dev->sensor, pad, get_fmt, dev->sensor_config,
563 + unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__,
564 + fmt->width, fmt->height, fmt->code);
569 +static int __subdev_set_format(struct unicam_device *dev,
570 + struct v4l2_mbus_framefmt *fmt)
572 + struct v4l2_subdev_format sd_fmt = {
573 + .which = V4L2_SUBDEV_FORMAT_ACTIVE,
575 + struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
580 + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
585 + unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__,
586 + fmt->width, fmt->height, fmt->code);
591 +static int unicam_calc_format_size_bpl(struct unicam_device *dev,
592 + const struct unicam_fmt *fmt,
593 + struct v4l2_format *f)
595 + unsigned int min_bytesperline;
597 + v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, MAX_WIDTH, 2,
598 + &f->fmt.pix.height, MIN_HEIGHT, MAX_HEIGHT, 0,
601 + min_bytesperline = bytes_per_line(f->fmt.pix.width, fmt);
603 + if (f->fmt.pix.bytesperline > min_bytesperline &&
604 + f->fmt.pix.bytesperline <= MAX_BYTESPERLINE)
605 + f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
608 + f->fmt.pix.bytesperline = min_bytesperline;
610 + /* Align height up for compatibility with other hardware blocks */
611 + f->fmt.pix.sizeimage = ALIGN(f->fmt.pix.height, HEIGHT_ALIGNMENT) *
612 + f->fmt.pix.bytesperline;
614 + unicam_dbg(3, dev, "%s: fourcc: " V4L2_FOURCC_CONV " size: %dx%d bpl:%d img_size:%d\n",
616 + V4L2_FOURCC_CONV_ARGS(f->fmt.pix.pixelformat),
617 + f->fmt.pix.width, f->fmt.pix.height,
618 + f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
623 +static int unicam_reset_format(struct unicam_device *dev)
625 + struct v4l2_mbus_framefmt mbus_fmt;
628 + ret = __subdev_get_format(dev, &mbus_fmt);
630 + unicam_err(dev, "Failed to get_format - ret %d\n", ret);
634 + if (mbus_fmt.code != dev->fmt->code) {
635 + unicam_err(dev, "code mismatch - fmt->code %08x, mbus_fmt.code %08x\n",
636 + dev->fmt->code, mbus_fmt.code);
640 + v4l2_fill_pix_format(&dev->v_fmt.fmt.pix, &mbus_fmt);
641 + dev->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
643 + unicam_calc_format_size_bpl(dev, dev->fmt, &dev->v_fmt);
645 + dev->m_fmt = mbus_fmt;
650 +static void unicam_wr_dma_addr(struct unicam_device *dev, unsigned int dmaaddr)
652 + unicam_dbg(1, dev, "wr_dma_addr %08x-%08x\n",
653 + dmaaddr, dmaaddr + dev->v_fmt.fmt.pix.sizeimage);
654 + reg_write(&dev->cfg, UNICAM_IBSA0, dmaaddr);
655 + reg_write(&dev->cfg, UNICAM_IBEA0,
656 + dmaaddr + dev->v_fmt.fmt.pix.sizeimage);
659 +static inline void unicam_schedule_next_buffer(struct unicam_device *dev)
661 + struct unicam_dmaqueue *dma_q = &dev->dma_queue;
662 + struct unicam_buffer *buf;
665 + buf = list_entry(dma_q->active.next, struct unicam_buffer, list);
666 + dev->next_frm = buf;
667 + list_del(&buf->list);
669 + addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
670 + unicam_wr_dma_addr(dev, addr);
673 +static inline void unicam_process_buffer_complete(struct unicam_device *dev)
675 + dev->cur_frm->vb.field = dev->m_fmt.field;
676 + dev->cur_frm->vb.sequence = dev->sequence++;
678 + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
679 + dev->cur_frm = dev->next_frm;
683 + * unicam_isr : ISR handler for unicam capture
685 + * @dev_id: dev_id ptr
687 + * It changes status of the captured buffer, takes next buffer from the queue
688 + * and sets its address in unicam registers
690 +static irqreturn_t unicam_isr(int irq, void *dev)
692 + struct unicam_device *unicam = (struct unicam_device *)dev;
693 + struct unicam_cfg *cfg = &unicam->cfg;
694 + struct unicam_dmaqueue *dma_q = &unicam->dma_queue;
698 + * Don't service interrupts if not streaming.
699 + * Avoids issues if the VPU should enable the
700 + * peripheral without the kernel knowing (that
701 + * shouldn't happen, but causes issues if it does).
703 + if (!unicam->streaming)
704 + return IRQ_HANDLED;
706 + sta = reg_read(cfg, UNICAM_STA);
707 + /* Write value back to clear the interrupts */
708 + reg_write(cfg, UNICAM_STA, sta);
710 + ista = reg_read(cfg, UNICAM_ISTA);
711 + /* Write value back to clear the interrupts */
712 + reg_write(cfg, UNICAM_ISTA, ista);
714 + if (!(sta && (UNICAM_IS | UNICAM_PI0)))
715 + return IRQ_HANDLED;
717 + if (ista & UNICAM_FSI) {
719 + * Timestamp is to be when the first data byte was captured,
722 + if (unicam->cur_frm)
723 + unicam->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
725 + if (ista & UNICAM_FEI || sta & UNICAM_PI0) {
727 + * Ensure we have swapped buffers already as we can't
728 + * stop the peripheral. Overwrite the frame we've just
729 + * captured instead.
731 + if (unicam->cur_frm && unicam->cur_frm != unicam->next_frm)
732 + unicam_process_buffer_complete(unicam);
735 + if (ista & (UNICAM_FSI | UNICAM_LCI)) {
736 + spin_lock(&unicam->dma_queue_lock);
737 + if (!list_empty(&dma_q->active) &&
738 + unicam->cur_frm == unicam->next_frm)
739 + unicam_schedule_next_buffer(unicam);
740 + spin_unlock(&unicam->dma_queue_lock);
743 + if (reg_read(&unicam->cfg, UNICAM_ICTL) & UNICAM_FCM) {
744 + /* Switch out of trigger mode if selected */
745 + reg_write_field(&unicam->cfg, UNICAM_ICTL, 1, UNICAM_TFC);
746 + reg_write_field(&unicam->cfg, UNICAM_ICTL, 0, UNICAM_FCM);
748 + return IRQ_HANDLED;
751 +static int unicam_querycap(struct file *file, void *priv,
752 + struct v4l2_capability *cap)
754 + struct unicam_device *dev = video_drvdata(file);
756 + strlcpy(cap->driver, UNICAM_MODULE_NAME, sizeof(cap->driver));
757 + strlcpy(cap->card, UNICAM_MODULE_NAME, sizeof(cap->card));
759 + snprintf(cap->bus_info, sizeof(cap->bus_info),
760 + "platform:%s", dev->v4l2_dev.name);
765 +static int unicam_enum_fmt_vid_cap(struct file *file, void *priv,
766 + struct v4l2_fmtdesc *f)
768 + struct unicam_device *dev = video_drvdata(file);
769 + const struct unicam_fmt *fmt = NULL;
771 + if (f->index >= dev->num_active_fmt)
774 + fmt = &dev->active_fmts[f->index];
776 + f->pixelformat = fmt->fourcc;
781 +static int unicam_g_fmt_vid_cap(struct file *file, void *priv,
782 + struct v4l2_format *f)
784 + struct unicam_device *dev = video_drvdata(file);
791 +static int unicam_try_fmt_vid_cap(struct file *file, void *priv,
792 + struct v4l2_format *f)
794 + struct unicam_device *dev = video_drvdata(file);
795 + const struct unicam_fmt *fmt;
796 + struct v4l2_subdev_format sd_fmt = {
797 + .which = V4L2_SUBDEV_FORMAT_TRY,
799 + struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
802 + fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
804 + unicam_dbg(3, dev, "Fourcc format (0x%08x) not found. Use default of %08X\n",
805 + f->fmt.pix.pixelformat, dev->active_fmts[0].fourcc);
807 + /* Just get the first one enumerated */
808 + fmt = &dev->active_fmts[0];
809 + f->fmt.pix.pixelformat = fmt->fourcc;
812 + v4l2_fill_mbus_format(mbus_fmt, &f->fmt.pix, fmt->code);
814 + * No support for receiving interlaced video, so never
815 + * request it from the sensor subdev.
817 + mbus_fmt->field = V4L2_FIELD_NONE;
819 + ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
821 + if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
824 + if (mbus_fmt->field != V4L2_FIELD_NONE)
825 + unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
827 + v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
829 + * Use current colorspace for now, it will get
830 + * updated properly during s_fmt
832 + f->fmt.pix.colorspace = dev->v_fmt.fmt.pix.colorspace;
833 + return unicam_calc_format_size_bpl(dev, fmt, f);
836 +static int unicam_s_fmt_vid_cap(struct file *file, void *priv,
837 + struct v4l2_format *f)
839 + struct unicam_device *dev = video_drvdata(file);
840 + struct vb2_queue *q = &dev->buffer_queue;
841 + const struct unicam_fmt *fmt;
842 + struct v4l2_mbus_framefmt mbus_fmt = {0};
845 + if (vb2_is_busy(q))
848 + ret = unicam_try_fmt_vid_cap(file, priv, f);
852 + fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
854 + /* Unknown pixel format - adopt a default */
855 + fmt = &dev->active_fmts[0];
856 + f->fmt.pix.pixelformat = fmt->fourcc;
860 + v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, fmt->code);
862 + ret = __subdev_set_format(dev, &mbus_fmt);
864 + unicam_dbg(3, dev, "%s __subdev_set_format failed %d\n",
869 + /* Just double check nothing has gone wrong */
870 + if (mbus_fmt.code != fmt->code) {
872 + "%s subdev changed format on us, this should not happen\n",
878 + dev->v_fmt.fmt.pix.pixelformat = f->fmt.pix.pixelformat;
879 + dev->v_fmt.fmt.pix.bytesperline = f->fmt.pix.bytesperline;
880 + unicam_reset_format(dev);
882 + unicam_dbg(3, dev, "%s %dx%d, mbus_fmt %08X, V4L2 pix " V4L2_FOURCC_CONV ".\n",
883 + __func__, dev->v_fmt.fmt.pix.width,
884 + dev->v_fmt.fmt.pix.height, mbus_fmt.code,
885 + V4L2_FOURCC_CONV_ARGS(dev->v_fmt.fmt.pix.pixelformat));
892 +static int unicam_queue_setup(struct vb2_queue *vq,
893 + unsigned int *nbuffers,
894 + unsigned int *nplanes,
895 + unsigned int sizes[],
896 + struct device *alloc_devs[])
898 + struct unicam_device *dev = vb2_get_drv_priv(vq);
899 + unsigned int size = dev->v_fmt.fmt.pix.sizeimage;
901 + if (vq->num_buffers + *nbuffers < 3)
902 + *nbuffers = 3 - vq->num_buffers;
905 + if (sizes[0] < size) {
906 + unicam_err(dev, "sizes[0] %i < size %u\n", sizes[0],
919 +static int unicam_buffer_prepare(struct vb2_buffer *vb)
921 + struct unicam_device *dev = vb2_get_drv_priv(vb->vb2_queue);
922 + struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
924 + unsigned long size;
926 + if (WARN_ON(!dev->fmt))
929 + size = dev->v_fmt.fmt.pix.sizeimage;
930 + if (vb2_plane_size(vb, 0) < size) {
931 + unicam_err(dev, "data will not fit into plane (%lu < %lu)\n",
932 + vb2_plane_size(vb, 0), size);
936 + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
940 +static void unicam_buffer_queue(struct vb2_buffer *vb)
942 + struct unicam_device *dev = vb2_get_drv_priv(vb->vb2_queue);
943 + struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
945 + struct unicam_dmaqueue *dma_queue = &dev->dma_queue;
946 + unsigned long flags = 0;
948 + /* recheck locking */
949 + spin_lock_irqsave(&dev->dma_queue_lock, flags);
950 + list_add_tail(&buf->list, &dma_queue->active);
951 + spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
954 +static void unicam_wr_dma_config(struct unicam_device *dev,
955 + unsigned int stride)
957 + reg_write(&dev->cfg, UNICAM_IBLS, stride);
960 +static void unicam_set_packing_config(struct unicam_device *dev)
962 + int mbus_depth = find_mbus_depth_by_code(dev->fmt->code);
963 + int v4l2_depth = dev->fmt->depth;
967 + if (mbus_depth == v4l2_depth) {
968 + unpack = UNICAM_PUM_NONE;
969 + pack = UNICAM_PPM_NONE;
971 + switch (mbus_depth) {
973 + unpack = UNICAM_PUM_UNPACK8;
976 + unpack = UNICAM_PUM_UNPACK10;
979 + unpack = UNICAM_PUM_UNPACK12;
982 + unpack = UNICAM_PUM_UNPACK14;
985 + unpack = UNICAM_PUM_UNPACK16;
988 + unpack = UNICAM_PUM_NONE;
991 + switch (v4l2_depth) {
993 + pack = UNICAM_PPM_PACK8;
996 + pack = UNICAM_PPM_PACK10;
999 + pack = UNICAM_PPM_PACK12;
1002 + pack = UNICAM_PPM_PACK14;
1005 + pack = UNICAM_PPM_PACK16;
1008 + pack = UNICAM_PPM_NONE;
1014 + set_field(&val, 2, UNICAM_DEBL_MASK);
1015 + set_field(&val, unpack, UNICAM_PUM_MASK);
1016 + set_field(&val, pack, UNICAM_PPM_MASK);
1017 + reg_write(&dev->cfg, UNICAM_IPIPE, val);
1020 +static void unicam_cfg_image_id(struct unicam_device *dev)
1022 + struct unicam_cfg *cfg = &dev->cfg;
1024 + if (dev->bus_type == V4L2_MBUS_CSI2) {
1026 + reg_write(cfg, UNICAM_IDI0,
1027 + (dev->virtual_channel << 6) | dev->fmt->csi_dt);
1030 + reg_write(cfg, UNICAM_IDI0, (0x80 | dev->fmt->csi_dt));
1034 +void unicam_start_rx(struct unicam_device *dev, unsigned long addr)
1036 + struct unicam_cfg *cfg = &dev->cfg;
1037 + int line_int_freq = dev->v_fmt.fmt.pix.height >> 2;
1041 + if (line_int_freq < 128)
1042 + line_int_freq = 128;
1044 + /* Enable lane clocks */
1046 + for (i = 0; i < dev->active_data_lanes; i++)
1047 + val = val << 2 | 1;
1048 + clk_write(cfg, val);
1051 + reg_write(cfg, UNICAM_CTRL, UNICAM_MEM);
1053 + /* Enable analogue control, and leave in reset. */
1055 + set_field(&val, 7, UNICAM_CTATADJ_MASK);
1056 + set_field(&val, 7, UNICAM_PTATADJ_MASK);
1057 + reg_write(cfg, UNICAM_ANA, val);
1058 + usleep_range(1000, 2000);
1060 + /* Come out of reset */
1061 + reg_write_field(cfg, UNICAM_ANA, 0, UNICAM_AR);
1063 + /* Peripheral reset */
1064 + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPR);
1065 + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPR);
1067 + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPE);
1069 + /* Enable Rx control. */
1070 + val = reg_read(cfg, UNICAM_CTRL);
1071 + if (dev->bus_type == V4L2_MBUS_CSI2) {
1072 + set_field(&val, UNICAM_CPM_CSI2, UNICAM_CPM_MASK);
1073 + set_field(&val, UNICAM_DCM_STROBE, UNICAM_DCM_MASK);
1075 + set_field(&val, UNICAM_CPM_CCP2, UNICAM_CPM_MASK);
1076 + set_field(&val, dev->bus_flags, UNICAM_DCM_MASK);
1078 + /* Packet framer timeout */
1079 + set_field(&val, 0xf, UNICAM_PFT_MASK);
1080 + set_field(&val, 128, UNICAM_OET_MASK);
1081 + reg_write(cfg, UNICAM_CTRL, val);
1083 + reg_write(cfg, UNICAM_IHWIN, 0);
1084 + reg_write(cfg, UNICAM_IVWIN, 0);
1086 + /* AXI bus access QoS setup */
1087 + val = reg_read(&dev->cfg, UNICAM_PRI);
1088 + set_field(&val, 0, UNICAM_BL_MASK);
1089 + set_field(&val, 0, UNICAM_BS_MASK);
1090 + set_field(&val, 0xe, UNICAM_PP_MASK);
1091 + set_field(&val, 8, UNICAM_NP_MASK);
1092 + set_field(&val, 2, UNICAM_PT_MASK);
1093 + set_field(&val, 1, UNICAM_PE);
1094 + reg_write(cfg, UNICAM_PRI, val);
1096 + reg_write_field(cfg, UNICAM_ANA, 0, UNICAM_DDL);
1098 + /* Always start in trigger frame capture mode (UNICAM_FCM set) */
1099 + val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_FCM;
1100 + set_field(&val, line_int_freq, UNICAM_LCIE_MASK);
1101 + reg_write(cfg, UNICAM_ICTL, val);
1102 + reg_write(cfg, UNICAM_STA, UNICAM_STA_MASK_ALL);
1103 + reg_write(cfg, UNICAM_ISTA, UNICAM_ISTA_MASK_ALL);
1105 + /* tclk_term_en */
1106 + reg_write_field(cfg, UNICAM_CLT, 2, UNICAM_CLT1_MASK);
1108 + reg_write_field(cfg, UNICAM_CLT, 6, UNICAM_CLT2_MASK);
1110 + reg_write_field(cfg, UNICAM_DLT, 2, UNICAM_DLT1_MASK);
1112 + reg_write_field(cfg, UNICAM_DLT, 6, UNICAM_DLT2_MASK);
1114 + reg_write_field(cfg, UNICAM_DLT, 0, UNICAM_DLT3_MASK);
1116 + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_SOE);
1118 + /* Packet compare setup - required to avoid missing frame ends */
1120 + set_field(&val, 1, UNICAM_PCE);
1121 + set_field(&val, 1, UNICAM_GI);
1122 + set_field(&val, 1, UNICAM_CPH);
1123 + set_field(&val, 0, UNICAM_PCVC_MASK);
1124 + set_field(&val, 1, UNICAM_PCDT_MASK);
1125 + reg_write(cfg, UNICAM_CMP0, val);
1127 + /* Enable clock lane and set up terminations */
1129 + if (dev->bus_type == V4L2_MBUS_CSI2) {
1131 + set_field(&val, 1, UNICAM_CLE);
1132 + set_field(&val, 1, UNICAM_CLLPE);
1133 + if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1134 + set_field(&val, 1, UNICAM_CLTRE);
1135 + set_field(&val, 1, UNICAM_CLHSE);
1139 + set_field(&val, 1, UNICAM_CLE);
1140 + set_field(&val, 1, UNICAM_CLHSE);
1141 + set_field(&val, 1, UNICAM_CLTRE);
1143 + reg_write(cfg, UNICAM_CLK, val);
1146 + * Enable required data lanes with appropriate terminations.
1147 + * The same value needs to be written to UNICAM_DATn registers for
1148 + * the active lanes, and 0 for inactive ones.
1151 + if (dev->bus_type == V4L2_MBUS_CSI2) {
1153 + set_field(&val, 1, UNICAM_DLE);
1154 + set_field(&val, 1, UNICAM_DLLPE);
1155 + if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1156 + set_field(&val, 1, UNICAM_DLTRE);
1157 + set_field(&val, 1, UNICAM_DLHSE);
1161 + set_field(&val, 1, UNICAM_DLE);
1162 + set_field(&val, 1, UNICAM_DLHSE);
1163 + set_field(&val, 1, UNICAM_DLTRE);
1165 + reg_write(cfg, UNICAM_DAT0, val);
1167 + if (dev->active_data_lanes == 1)
1169 + reg_write(cfg, UNICAM_DAT1, val);
1171 + if (dev->max_data_lanes > 2) {
1173 + * Registers UNICAM_DAT2 and UNICAM_DAT3 only valid if the
1174 + * instance supports more than 2 data lanes.
1176 + if (dev->active_data_lanes == 2)
1178 + reg_write(cfg, UNICAM_DAT2, val);
1180 + if (dev->active_data_lanes == 3)
1182 + reg_write(cfg, UNICAM_DAT3, val);
1185 + unicam_wr_dma_config(dev, dev->v_fmt.fmt.pix.bytesperline);
1186 + unicam_wr_dma_addr(dev, addr);
1187 + unicam_set_packing_config(dev);
1188 + unicam_cfg_image_id(dev);
1190 + /* Disabled embedded data */
1192 + set_field(&val, 0, UNICAM_EDL_MASK);
1193 + reg_write(cfg, UNICAM_DCS, val);
1195 + val = reg_read(cfg, UNICAM_MISC);
1196 + set_field(&val, 1, UNICAM_FL0);
1197 + set_field(&val, 1, UNICAM_FL1);
1198 + reg_write(cfg, UNICAM_MISC, val);
1200 + /* Enable peripheral */
1201 + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPE);
1203 + /* Load image pointers */
1204 + reg_write_field(cfg, UNICAM_ICTL, 1, UNICAM_LIP_MASK);
1207 + * Enable trigger only for the first frame to
1208 + * sync correctly to the FS from the source.
1210 + reg_write_field(cfg, UNICAM_ICTL, 1, UNICAM_TFC);
1213 +static void unicam_disable(struct unicam_device *dev)
1215 + struct unicam_cfg *cfg = &dev->cfg;
1217 + /* Analogue lane control disable */
1218 + reg_write_field(cfg, UNICAM_ANA, 1, UNICAM_DDL);
1220 + /* Stop the output engine */
1221 + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_SOE);
1223 + /* Disable the data lanes. */
1224 + reg_write(cfg, UNICAM_DAT0, 0);
1225 + reg_write(cfg, UNICAM_DAT1, 0);
1227 + if (dev->max_data_lanes > 2) {
1228 + reg_write(cfg, UNICAM_DAT2, 0);
1229 + reg_write(cfg, UNICAM_DAT3, 0);
1232 + /* Peripheral reset */
1233 + reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPR);
1234 + usleep_range(50, 100);
1235 + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPR);
1237 + /* Disable peripheral */
1238 + reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPE);
1240 + /* Disable all lane clocks */
1241 + clk_write(cfg, 0);
1244 +static int unicam_start_streaming(struct vb2_queue *vq, unsigned int count)
1246 + struct unicam_device *dev = vb2_get_drv_priv(vq);
1247 + struct unicam_dmaqueue *dma_q = &dev->dma_queue;
1248 + struct unicam_buffer *buf, *tmp;
1249 + unsigned long addr = 0;
1250 + unsigned long flags;
1253 + spin_lock_irqsave(&dev->dma_queue_lock, flags);
1254 + buf = list_entry(dma_q->active.next, struct unicam_buffer, list);
1255 + dev->cur_frm = buf;
1256 + dev->next_frm = buf;
1257 + list_del(&buf->list);
1258 + spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
1260 + addr = vb2_dma_contig_plane_dma_addr(&dev->cur_frm->vb.vb2_buf, 0);
1261 + dev->sequence = 0;
1263 + ret = unicam_runtime_get(dev);
1265 + unicam_dbg(3, dev, "unicam_runtime_get failed\n");
1266 + goto err_release_buffers;
1269 + dev->active_data_lanes = dev->max_data_lanes;
1270 + if (dev->bus_type == V4L2_MBUS_CSI2 &&
1271 + v4l2_subdev_has_op(dev->sensor, video, g_mbus_config)) {
1272 + struct v4l2_mbus_config mbus_config;
1274 + ret = v4l2_subdev_call(dev->sensor, video, g_mbus_config,
1277 + unicam_dbg(3, dev, "g_mbus_config failed\n");
1281 + dev->active_data_lanes =
1282 + (mbus_config.flags & V4L2_MBUS_CSI2_LANE_MASK) >>
1283 + __ffs(V4L2_MBUS_CSI2_LANE_MASK);
1284 + if (!dev->active_data_lanes)
1285 + dev->active_data_lanes = dev->max_data_lanes;
1287 + if (dev->active_data_lanes > dev->max_data_lanes) {
1288 + unicam_err(dev, "Device has requested %u data lanes, which is >%u configured in DT\n",
1289 + dev->active_data_lanes, dev->max_data_lanes);
1294 + unicam_dbg(1, dev, "Running with %u data lanes\n",
1295 + dev->active_data_lanes);
1297 + ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
1299 + unicam_err(dev, "failed to set up clock\n");
1303 + ret = clk_prepare_enable(dev->clock);
1305 + unicam_err(dev, "Failed to enable CSI clock: %d\n", ret);
1308 + ret = v4l2_subdev_call(dev->sensor, core, s_power, 1);
1309 + if (ret < 0 && ret != -ENOIOCTLCMD) {
1310 + unicam_err(dev, "power on failed in subdev\n");
1311 + goto err_clock_unprepare;
1313 + dev->streaming = 1;
1315 + unicam_start_rx(dev, addr);
1317 + ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1319 + unicam_err(dev, "stream on failed in subdev\n");
1320 + goto err_disable_unicam;
1325 +err_disable_unicam:
1326 + unicam_disable(dev);
1327 + v4l2_subdev_call(dev->sensor, core, s_power, 0);
1328 +err_clock_unprepare:
1329 + clk_disable_unprepare(dev->clock);
1331 + unicam_runtime_put(dev);
1332 +err_release_buffers:
1333 + list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
1334 + list_del(&buf->list);
1335 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
1337 + if (dev->cur_frm != dev->next_frm)
1338 + vb2_buffer_done(&dev->next_frm->vb.vb2_buf,
1339 + VB2_BUF_STATE_QUEUED);
1340 + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
1341 + dev->next_frm = NULL;
1342 + dev->cur_frm = NULL;
1347 +static void unicam_stop_streaming(struct vb2_queue *vq)
1349 + struct unicam_device *dev = vb2_get_drv_priv(vq);
1350 + struct unicam_dmaqueue *dma_q = &dev->dma_queue;
1351 + struct unicam_buffer *buf, *tmp;
1352 + unsigned long flags;
1354 + if (v4l2_subdev_call(dev->sensor, video, s_stream, 0) < 0)
1355 + unicam_err(dev, "stream off failed in subdev\n");
1357 + unicam_disable(dev);
1359 + /* Release all active buffers */
1360 + spin_lock_irqsave(&dev->dma_queue_lock, flags);
1361 + list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
1362 + list_del(&buf->list);
1363 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1366 + if (dev->cur_frm == dev->next_frm) {
1367 + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1369 + vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1370 + vb2_buffer_done(&dev->next_frm->vb.vb2_buf,
1371 + VB2_BUF_STATE_ERROR);
1373 + dev->cur_frm = NULL;
1374 + dev->next_frm = NULL;
1375 + spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
1377 + if (v4l2_subdev_has_op(dev->sensor, core, s_power)) {
1378 + if (v4l2_subdev_call(dev->sensor, core, s_power, 0) < 0)
1379 + unicam_err(dev, "power off failed in subdev\n");
1382 + clk_disable_unprepare(dev->clock);
1383 + unicam_runtime_put(dev);
1386 +static int unicam_enum_input(struct file *file, void *priv,
1387 + struct v4l2_input *inp)
1389 + struct unicam_device *dev = video_drvdata(file);
1391 + if (inp->index != 0)
1394 + inp->type = V4L2_INPUT_TYPE_CAMERA;
1395 + if (v4l2_subdev_has_op(dev->sensor, video, s_dv_timings)) {
1396 + inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1398 + } else if (v4l2_subdev_has_op(dev->sensor, video, s_std)) {
1399 + inp->capabilities = V4L2_IN_CAP_STD;
1400 + if (v4l2_subdev_call(dev->sensor, video, g_tvnorms, &inp->std)
1402 + inp->std = V4L2_STD_ALL;
1404 + inp->capabilities = 0;
1407 + sprintf(inp->name, "Camera 0");
1411 +static int unicam_g_input(struct file *file, void *priv, unsigned int *i)
1418 +static int unicam_s_input(struct file *file, void *priv, unsigned int i)
1421 + * FIXME: Ideally we would like to be able to query the source
1422 + * subdevice for information over the input connectors it supports,
1423 + * and map that through in to a call to video_ops->s_routing.
1424 + * There is no infrastructure support for defining that within
1425 + * devicetree at present. Until that is implemented we can't
1426 + * map a user physical connector number to s_routing input number.
1434 +static int unicam_querystd(struct file *file, void *priv,
1437 + struct unicam_device *dev = video_drvdata(file);
1439 + return v4l2_subdev_call(dev->sensor, video, querystd, std);
1442 +static int unicam_g_std(struct file *file, void *priv, v4l2_std_id *std)
1444 + struct unicam_device *dev = video_drvdata(file);
1446 + return v4l2_subdev_call(dev->sensor, video, g_std, std);
1449 +static int unicam_s_std(struct file *file, void *priv, v4l2_std_id std)
1451 + struct unicam_device *dev = video_drvdata(file);
1453 + v4l2_std_id current_std;
1455 + ret = v4l2_subdev_call(dev->sensor, video, g_std, ¤t_std);
1459 + if (std == current_std)
1462 + if (vb2_is_busy(&dev->buffer_queue))
1465 + ret = v4l2_subdev_call(dev->sensor, video, s_std, std);
1467 + /* Force recomputation of bytesperline */
1468 + dev->v_fmt.fmt.pix.bytesperline = 0;
1470 + unicam_reset_format(dev);
1475 +static int unicam_s_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1477 + struct unicam_device *dev = video_drvdata(file);
1479 + return v4l2_subdev_call(dev->sensor, pad, set_edid, edid);
1482 +static int unicam_g_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1484 + struct unicam_device *dev = video_drvdata(file);
1486 + return v4l2_subdev_call(dev->sensor, pad, get_edid, edid);
1489 +static int unicam_g_dv_timings(struct file *file, void *priv,
1490 + struct v4l2_dv_timings *timings)
1492 + struct unicam_device *dev = video_drvdata(file);
1494 + return v4l2_subdev_call(dev->sensor, video, g_dv_timings, timings);
1497 +static int unicam_s_dv_timings(struct file *file, void *priv,
1498 + struct v4l2_dv_timings *timings)
1500 + struct unicam_device *dev = video_drvdata(file);
1501 + struct v4l2_dv_timings current_timings;
1504 + ret = v4l2_subdev_call(dev->sensor, video, g_dv_timings,
1505 + ¤t_timings);
1507 + if (v4l2_match_dv_timings(timings, ¤t_timings, 0, false))
1510 + if (vb2_is_busy(&dev->buffer_queue))
1513 + ret = v4l2_subdev_call(dev->sensor, video, s_dv_timings, timings);
1515 + /* Force recomputation of bytesperline */
1516 + dev->v_fmt.fmt.pix.bytesperline = 0;
1518 + unicam_reset_format(dev);
1523 +static int unicam_query_dv_timings(struct file *file, void *priv,
1524 + struct v4l2_dv_timings *timings)
1526 + struct unicam_device *dev = video_drvdata(file);
1528 + return v4l2_subdev_call(dev->sensor, video, query_dv_timings, timings);
1531 +static int unicam_enum_dv_timings(struct file *file, void *priv,
1532 + struct v4l2_enum_dv_timings *timings)
1534 + struct unicam_device *dev = video_drvdata(file);
1536 + return v4l2_subdev_call(dev->sensor, pad, enum_dv_timings, timings);
1539 +static int unicam_dv_timings_cap(struct file *file, void *priv,
1540 + struct v4l2_dv_timings_cap *cap)
1542 + struct unicam_device *dev = video_drvdata(file);
1544 + return v4l2_subdev_call(dev->sensor, pad, dv_timings_cap, cap);
1547 +static int unicam_subscribe_event(struct v4l2_fh *fh,
1548 + const struct v4l2_event_subscription *sub)
1550 + switch (sub->type) {
1551 + case V4L2_EVENT_SOURCE_CHANGE:
1552 + return v4l2_event_subscribe(fh, sub, 4, NULL);
1555 + return v4l2_ctrl_subscribe_event(fh, sub);
1558 +static int unicam_log_status(struct file *file, void *fh)
1560 + struct unicam_device *dev = video_drvdata(file);
1561 + struct unicam_cfg *cfg = &dev->cfg;
1564 + /* status for sub devices */
1565 + v4l2_device_call_all(&dev->v4l2_dev, 0, core, log_status);
1567 + unicam_info(dev, "-----Receiver status-----\n");
1568 + unicam_info(dev, "V4L2 width/height: %ux%u\n",
1569 + dev->v_fmt.fmt.pix.width, dev->v_fmt.fmt.pix.height);
1570 + unicam_info(dev, "Mediabus format: %08x\n", dev->fmt->code);
1571 + unicam_info(dev, "V4L2 format: " V4L2_FOURCC_CONV "\n",
1572 + V4L2_FOURCC_CONV_ARGS(dev->v_fmt.fmt.pix.pixelformat));
1573 + reg = reg_read(&dev->cfg, UNICAM_IPIPE);
1574 + unicam_info(dev, "Unpacking/packing: %u / %u\n",
1575 + get_field(reg, UNICAM_PUM_MASK),
1576 + get_field(reg, UNICAM_PPM_MASK));
1577 + unicam_info(dev, "----Live data----\n");
1578 + unicam_info(dev, "Programmed stride: %4u\n",
1579 + reg_read(cfg, UNICAM_IBLS));
1580 + unicam_info(dev, "Detected resolution: %ux%u\n",
1581 + reg_read(cfg, UNICAM_IHSTA),
1582 + reg_read(cfg, UNICAM_IVSTA));
1583 + unicam_info(dev, "Write pointer: %08x\n",
1584 + reg_read(cfg, UNICAM_IBWP));
1589 +static void unicam_notify(struct v4l2_subdev *sd,
1590 + unsigned int notification, void *arg)
1592 + struct unicam_device *dev =
1593 + container_of(sd->v4l2_dev, struct unicam_device, v4l2_dev);
1595 + switch (notification) {
1596 + case V4L2_DEVICE_NOTIFY_EVENT:
1597 + v4l2_event_queue(&dev->video_dev, arg);
1604 +static const struct vb2_ops unicam_video_qops = {
1605 + .wait_prepare = vb2_ops_wait_prepare,
1606 + .wait_finish = vb2_ops_wait_finish,
1607 + .queue_setup = unicam_queue_setup,
1608 + .buf_prepare = unicam_buffer_prepare,
1609 + .buf_queue = unicam_buffer_queue,
1610 + .start_streaming = unicam_start_streaming,
1611 + .stop_streaming = unicam_stop_streaming,
1614 +/* unicam capture driver file operations */
1615 +static const struct v4l2_file_operations unicam_fops = {
1616 + .owner = THIS_MODULE,
1617 + .open = v4l2_fh_open,
1618 + .release = vb2_fop_release,
1619 + .read = vb2_fop_read,
1620 + .poll = vb2_fop_poll,
1621 + .unlocked_ioctl = video_ioctl2,
1622 + .mmap = vb2_fop_mmap,
1625 +/* unicam capture ioctl operations */
1626 +static const struct v4l2_ioctl_ops unicam_ioctl_ops = {
1627 + .vidioc_querycap = unicam_querycap,
1628 + .vidioc_enum_fmt_vid_cap = unicam_enum_fmt_vid_cap,
1629 + .vidioc_g_fmt_vid_cap = unicam_g_fmt_vid_cap,
1630 + .vidioc_s_fmt_vid_cap = unicam_s_fmt_vid_cap,
1631 + .vidioc_try_fmt_vid_cap = unicam_try_fmt_vid_cap,
1633 + .vidioc_enum_input = unicam_enum_input,
1634 + .vidioc_g_input = unicam_g_input,
1635 + .vidioc_s_input = unicam_s_input,
1637 + .vidioc_querystd = unicam_querystd,
1638 + .vidioc_s_std = unicam_s_std,
1639 + .vidioc_g_std = unicam_g_std,
1641 + .vidioc_g_edid = unicam_g_edid,
1642 + .vidioc_s_edid = unicam_s_edid,
1644 + .vidioc_s_dv_timings = unicam_s_dv_timings,
1645 + .vidioc_g_dv_timings = unicam_g_dv_timings,
1646 + .vidioc_query_dv_timings = unicam_query_dv_timings,
1647 + .vidioc_enum_dv_timings = unicam_enum_dv_timings,
1648 + .vidioc_dv_timings_cap = unicam_dv_timings_cap,
1650 + .vidioc_reqbufs = vb2_ioctl_reqbufs,
1651 + .vidioc_create_bufs = vb2_ioctl_create_bufs,
1652 + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1653 + .vidioc_querybuf = vb2_ioctl_querybuf,
1654 + .vidioc_qbuf = vb2_ioctl_qbuf,
1655 + .vidioc_dqbuf = vb2_ioctl_dqbuf,
1656 + .vidioc_expbuf = vb2_ioctl_expbuf,
1657 + .vidioc_streamon = vb2_ioctl_streamon,
1658 + .vidioc_streamoff = vb2_ioctl_streamoff,
1660 + .vidioc_log_status = unicam_log_status,
1661 + .vidioc_subscribe_event = unicam_subscribe_event,
1662 + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1666 + * Adds an entry to the active_fmts array
1667 + * Returns non-zero if attempting to write off the end of the array.
1669 +static int unicam_add_active_format(struct unicam_device *unicam,
1670 + const struct unicam_fmt *fmt)
1672 + //Ensure we don't run off the end of the array.
1673 + if (unicam->num_active_fmt >= MAX_POSSIBLE_PIX_FMTS)
1676 + unicam->active_fmts[unicam->num_active_fmt] = *fmt;
1677 + unicam_dbg(2, unicam,
1678 + "matched fourcc: " V4L2_FOURCC_CONV ": code: %04x idx: %d\n",
1679 + V4L2_FOURCC_CONV_ARGS(fmt->fourcc),
1680 + fmt->code, unicam->num_active_fmt);
1681 + unicam->num_active_fmt++;
1687 +unicam_async_bound(struct v4l2_async_notifier *notifier,
1688 + struct v4l2_subdev *subdev,
1689 + struct v4l2_async_subdev *asd)
1691 + struct unicam_device *unicam = container_of(notifier->v4l2_dev,
1692 + struct unicam_device, v4l2_dev);
1693 + struct v4l2_subdev_mbus_code_enum mbus_code;
1697 + if (unicam->sensor) {
1698 + unicam_info(unicam, "Rejecting subdev %s (Already set!!)",
1703 + unicam->sensor = subdev;
1704 + unicam_dbg(1, unicam, "Using sensor %s for capture\n", subdev->name);
1706 + /* Enumerate sub device formats and enable all matching local formats */
1707 + unicam->num_active_fmt = 0;
1708 + unicam_dbg(2, unicam, "Get supported formats...\n");
1709 + for (j = 0; ret != -EINVAL && ret != -ENOIOCTLCMD; ++j) {
1710 + const struct unicam_fmt *fmt = NULL;
1713 + memset(&mbus_code, 0, sizeof(mbus_code));
1714 + mbus_code.index = j;
1715 + ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
1716 + NULL, &mbus_code);
1718 + unicam_dbg(2, unicam,
1719 + "subdev->enum_mbus_code idx %d returned %d - continue\n",
1724 + unicam_dbg(2, unicam, "subdev %s: code: %04x idx: %d\n",
1725 + subdev->name, mbus_code.code, j);
1727 + for (k = 0; k < ARRAY_SIZE(formats); k++) {
1728 + if (mbus_code.code == formats[k].code) {
1729 + fmt = &formats[k];
1733 + unicam_dbg(2, unicam, "fmt %04x returned as %p, V4L2 FOURCC %04x, csi_dt %02X\n",
1734 + mbus_code.code, fmt, fmt ? fmt->fourcc : 0,
1735 + fmt ? fmt->csi_dt : 0);
1737 + if (unicam_add_active_format(unicam, fmt)) {
1738 + unicam_dbg(1, unicam, "Active fmt list truncated\n");
1743 + unicam_dbg(2, unicam,
1744 + "Done all formats\n");
1745 + dump_active_formats(unicam);
1750 +static int unicam_probe_complete(struct unicam_device *unicam)
1752 + struct video_device *vdev;
1753 + struct vb2_queue *q;
1754 + struct v4l2_mbus_framefmt mbus_fmt = {0};
1755 + const struct unicam_fmt *fmt;
1758 + v4l2_set_subdev_hostdata(unicam->sensor, unicam);
1760 + unicam->v4l2_dev.notify = unicam_notify;
1762 + unicam->sensor_config = v4l2_subdev_alloc_pad_config(unicam->sensor);
1763 + if (!unicam->sensor_config)
1766 + ret = __subdev_get_format(unicam, &mbus_fmt);
1768 + unicam_err(unicam, "Failed to get_format - ret %d\n", ret);
1772 + fmt = find_format_by_code(unicam, mbus_fmt.code);
1774 + /* Default image format not valid. Choose first active fmt. */
1775 + fmt = &unicam->active_fmts[0];
1776 + mbus_fmt.code = fmt->code;
1777 + ret = __subdev_set_format(unicam, &mbus_fmt);
1781 + if (mbus_fmt.field != V4L2_FIELD_NONE) {
1782 + /* Interlaced not supported - disable it now. */
1783 + mbus_fmt.field = V4L2_FIELD_NONE;
1784 + ret = __subdev_set_format(unicam, &mbus_fmt);
1789 + unicam->fmt = fmt;
1790 + unicam->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
1792 + /* Read current subdev format */
1793 + unicam_reset_format(unicam);
1795 + if (v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
1796 + v4l2_std_id tvnorms;
1798 + if (WARN_ON(!v4l2_subdev_has_op(unicam->sensor, video,
1801 + * Subdevice should not advertise s_std but not
1806 + ret = v4l2_subdev_call(unicam->sensor, video,
1807 + g_tvnorms, &tvnorms);
1810 + unicam->video_dev.tvnorms |= tvnorms;
1813 + spin_lock_init(&unicam->dma_queue_lock);
1814 + mutex_init(&unicam->lock);
1816 + /* Add controls from the subdevice */
1817 + ret = v4l2_ctrl_add_handler(&unicam->ctrl_handler,
1818 + unicam->sensor->ctrl_handler, NULL);
1822 + q = &unicam->buffer_queue;
1823 + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1824 + q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
1825 + q->drv_priv = unicam;
1826 + q->ops = &unicam_video_qops;
1827 + q->mem_ops = &vb2_dma_contig_memops;
1828 + q->buf_struct_size = sizeof(struct unicam_buffer);
1829 + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1830 + q->lock = &unicam->lock;
1831 + q->min_buffers_needed = 2;
1832 + q->dev = &unicam->pdev->dev;
1834 + ret = vb2_queue_init(q);
1836 + unicam_err(unicam, "vb2_queue_init() failed\n");
1840 + INIT_LIST_HEAD(&unicam->dma_queue.active);
1842 + vdev = &unicam->video_dev;
1843 + strlcpy(vdev->name, UNICAM_MODULE_NAME, sizeof(vdev->name));
1844 + vdev->release = video_device_release_empty;
1845 + vdev->fops = &unicam_fops;
1846 + vdev->ioctl_ops = &unicam_ioctl_ops;
1847 + vdev->v4l2_dev = &unicam->v4l2_dev;
1848 + vdev->vfl_dir = VFL_DIR_RX;
1850 + vdev->lock = &unicam->lock;
1851 + vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1852 + V4L2_CAP_READWRITE;
1854 + /* If the source has no controls then remove our ctrl handler. */
1855 + if (list_empty(&unicam->ctrl_handler.ctrls))
1856 + unicam->v4l2_dev.ctrl_handler = NULL;
1858 + video_set_drvdata(vdev, unicam);
1859 + ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1861 + unicam_err(unicam, "Unable to register video device.\n");
1865 + if (!v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
1866 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_STD);
1867 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_STD);
1868 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUMSTD);
1870 + if (!v4l2_subdev_has_op(unicam->sensor, video, querystd))
1871 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_QUERYSTD);
1872 + if (!v4l2_subdev_has_op(unicam->sensor, video, s_dv_timings)) {
1873 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_EDID);
1874 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_EDID);
1875 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_DV_TIMINGS_CAP);
1876 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_DV_TIMINGS);
1877 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_DV_TIMINGS);
1878 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUM_DV_TIMINGS);
1879 + v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_QUERY_DV_TIMINGS);
1882 + ret = v4l2_device_register_subdev_nodes(&unicam->v4l2_dev);
1884 + unicam_err(unicam,
1885 + "Unable to register subdev nodes.\n");
1886 + video_unregister_device(&unicam->video_dev);
1893 +static int unicam_async_complete(struct v4l2_async_notifier *notifier)
1895 + struct unicam_device *unicam = container_of(notifier->v4l2_dev,
1896 + struct unicam_device, v4l2_dev);
1898 + return unicam_probe_complete(unicam);
1901 +static const struct v4l2_async_notifier_operations unicam_async_ops = {
1902 + .bound = unicam_async_bound,
1903 + .complete = unicam_async_complete,
1906 +static int of_unicam_connect_subdevs(struct unicam_device *dev)
1908 + struct platform_device *pdev = dev->pdev;
1909 + struct device_node *parent, *ep_node = NULL, *remote_ep = NULL,
1910 + *sensor_node = NULL;
1911 + struct v4l2_fwnode_endpoint *ep;
1912 + struct v4l2_async_subdev *asd;
1913 + struct v4l2_async_subdev **subdevs = NULL;
1914 + unsigned int peripheral_data_lanes;
1915 + int ret = -EINVAL;
1916 + unsigned int lane;
1918 + parent = pdev->dev.of_node;
1921 + ep = &dev->endpoint;
1923 + ep_node = of_graph_get_next_endpoint(parent, NULL);
1925 + unicam_dbg(3, dev, "can't get next endpoint\n");
1926 + goto cleanup_exit;
1929 + unicam_dbg(3, dev, "ep_node is %s\n", ep_node->name);
1931 + v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), ep);
1933 + for (lane = 0; lane < ep->bus.mipi_csi2.num_data_lanes; lane++) {
1934 + if (ep->bus.mipi_csi2.data_lanes[lane] != lane + 1) {
1935 + unicam_err(dev, "Local endpoint - data lane reordering not supported\n");
1936 + goto cleanup_exit;
1940 + peripheral_data_lanes = ep->bus.mipi_csi2.num_data_lanes;
1942 + sensor_node = of_graph_get_remote_port_parent(ep_node);
1943 + if (!sensor_node) {
1944 + unicam_dbg(3, dev, "can't get remote parent\n");
1945 + goto cleanup_exit;
1947 + unicam_dbg(3, dev, "sensor_node is %s\n", sensor_node->name);
1948 + asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
1949 + asd->match.fwnode.fwnode = of_fwnode_handle(sensor_node);
1951 + remote_ep = of_graph_get_remote_endpoint(ep_node);
1953 + unicam_dbg(3, dev, "can't get remote-endpoint\n");
1954 + goto cleanup_exit;
1956 + unicam_dbg(3, dev, "remote_ep is %s\n", remote_ep->name);
1957 + v4l2_fwnode_endpoint_parse(of_fwnode_handle(remote_ep), ep);
1958 + unicam_dbg(3, dev, "parsed remote_ep to endpoint. nr_of_link_frequencies %u, bus_type %u\n",
1959 + ep->nr_of_link_frequencies, ep->bus_type);
1961 + switch (ep->bus_type) {
1962 + case V4L2_MBUS_CSI2:
1963 + if (ep->bus.mipi_csi2.num_data_lanes >
1964 + peripheral_data_lanes) {
1965 + unicam_err(dev, "Subdevice %s wants too many data lanes (%u > %u)\n",
1966 + sensor_node->name,
1967 + ep->bus.mipi_csi2.num_data_lanes,
1968 + peripheral_data_lanes);
1969 + goto cleanup_exit;
1972 + lane < ep->bus.mipi_csi2.num_data_lanes;
1974 + if (ep->bus.mipi_csi2.data_lanes[lane] != lane + 1) {
1975 + unicam_err(dev, "Subdevice %s - incompatible data lane config\n",
1976 + sensor_node->name);
1977 + goto cleanup_exit;
1980 + dev->max_data_lanes = ep->bus.mipi_csi2.num_data_lanes;
1981 + dev->bus_flags = ep->bus.mipi_csi2.flags;
1983 + case V4L2_MBUS_CCP2:
1984 + if (ep->bus.mipi_csi1.clock_lane != 0 ||
1985 + ep->bus.mipi_csi1.data_lane != 1) {
1986 + unicam_err(dev, "Subdevice %s incompatible lane config\n",
1987 + sensor_node->name);
1988 + goto cleanup_exit;
1990 + dev->max_data_lanes = 1;
1991 + dev->bus_flags = ep->bus.mipi_csi1.strobe;
1994 + /* Unsupported bus type */
1995 + unicam_err(dev, "sub-device %s is not a CSI2 or CCP2 device %d\n",
1996 + sensor_node->name, ep->bus_type);
1997 + goto cleanup_exit;
2000 + /* Store bus type - CSI2 or CCP2 */
2001 + dev->bus_type = ep->bus_type;
2002 + unicam_dbg(3, dev, "bus_type is %d\n", dev->bus_type);
2004 + /* Store Virtual Channel number */
2005 + dev->virtual_channel = ep->base.id;
2007 + unicam_dbg(3, dev, "v4l2-endpoint: %s\n",
2008 + dev->bus_type == V4L2_MBUS_CSI2 ? "CSI2" : "CCP2");
2009 + unicam_dbg(3, dev, "Virtual Channel=%d\n", dev->virtual_channel);
2010 + if (dev->bus_type == V4L2_MBUS_CSI2)
2011 + unicam_dbg(3, dev, "flags=0x%08x\n", ep->bus.mipi_csi2.flags);
2012 + unicam_dbg(3, dev, "num_data_lanes=%d\n", dev->max_data_lanes);
2014 + unicam_dbg(1, dev, "found sub-device %s\n", sensor_node->name);
2016 + subdevs = devm_kzalloc(&dev->pdev->dev, sizeof(*subdevs), GFP_KERNEL);
2019 + goto cleanup_exit;
2022 + dev->notifier.subdevs = subdevs;
2023 + dev->notifier.num_subdevs = 1;
2024 + dev->notifier.ops = &unicam_async_ops;
2025 + ret = v4l2_async_notifier_register(&dev->v4l2_dev,
2028 + unicam_err(dev, "Error registering async notifier - ret %d\n",
2035 + of_node_put(remote_ep);
2037 + of_node_put(sensor_node);
2039 + of_node_put(ep_node);
2044 +static int unicam_probe(struct platform_device *pdev)
2046 + struct unicam_cfg *unicam_cfg;
2047 + struct unicam_device *unicam;
2048 + struct v4l2_ctrl_handler *hdl;
2049 + struct resource *res;
2052 + unicam = devm_kzalloc(&pdev->dev, sizeof(*unicam), GFP_KERNEL);
2056 + unicam->pdev = pdev;
2057 + unicam_cfg = &unicam->cfg;
2059 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2060 + unicam_cfg->base = devm_ioremap_resource(&pdev->dev, res);
2061 + if (IS_ERR(unicam_cfg->base)) {
2062 + unicam_err(unicam, "Failed to get main io block\n");
2063 + return PTR_ERR(unicam_cfg->base);
2066 + res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2067 + unicam_cfg->clk_gate_base = devm_ioremap_resource(&pdev->dev, res);
2068 + if (IS_ERR(unicam_cfg->clk_gate_base)) {
2069 + unicam_err(unicam, "Failed to get 2nd io block\n");
2070 + return PTR_ERR(unicam_cfg->clk_gate_base);
2073 + unicam->clock = devm_clk_get(&pdev->dev, "lp");
2074 + if (IS_ERR(unicam->clock)) {
2075 + unicam_err(unicam, "Failed to get clock\n");
2076 + return PTR_ERR(unicam->clock);
2079 + ret = platform_get_irq(pdev, 0);
2081 + dev_err(&pdev->dev, "No IRQ resource\n");
2085 + ret = devm_request_irq(&pdev->dev, ret, unicam_isr, 0,
2086 + "unicam_capture0", unicam);
2088 + dev_err(&pdev->dev, "Unable to request interrupt\n");
2092 + ret = v4l2_device_register(&pdev->dev, &unicam->v4l2_dev);
2094 + unicam_err(unicam,
2095 + "Unable to register v4l2 device.\n");
2099 + /* Reserve space for the controls */
2100 + hdl = &unicam->ctrl_handler;
2101 + ret = v4l2_ctrl_handler_init(hdl, 16);
2103 + goto probe_out_v4l2_unregister;
2104 + unicam->v4l2_dev.ctrl_handler = hdl;
2106 + /* set the driver data in platform device */
2107 + platform_set_drvdata(pdev, unicam);
2109 + ret = of_unicam_connect_subdevs(unicam);
2111 + dev_err(&pdev->dev, "Failed to connect subdevs\n");
2115 + /* Enable the block power domain */
2116 + pm_runtime_enable(&pdev->dev);
2121 + v4l2_ctrl_handler_free(hdl);
2122 +probe_out_v4l2_unregister:
2123 + v4l2_device_unregister(&unicam->v4l2_dev);
2127 +static int unicam_remove(struct platform_device *pdev)
2129 + struct unicam_device *unicam = platform_get_drvdata(pdev);
2131 + unicam_dbg(2, unicam, "%s\n", __func__);
2133 + pm_runtime_disable(&pdev->dev);
2135 + v4l2_async_notifier_unregister(&unicam->notifier);
2136 + v4l2_ctrl_handler_free(&unicam->ctrl_handler);
2137 + v4l2_device_unregister(&unicam->v4l2_dev);
2138 + video_unregister_device(&unicam->video_dev);
2139 + if (unicam->sensor_config)
2140 + v4l2_subdev_free_pad_config(unicam->sensor_config);
2145 +static const struct of_device_id unicam_of_match[] = {
2146 + { .compatible = "brcm,bcm2835-unicam", },
2147 + { /* sentinel */ },
2149 +MODULE_DEVICE_TABLE(of, unicam_of_match);
2151 +static struct platform_driver unicam_driver = {
2152 + .probe = unicam_probe,
2153 + .remove = unicam_remove,
2155 + .name = UNICAM_MODULE_NAME,
2156 + .of_match_table = of_match_ptr(unicam_of_match),
2160 +module_platform_driver(unicam_driver);
2162 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.org>");
2163 +MODULE_DESCRIPTION("BCM2835 Unicam driver");
2164 +MODULE_LICENSE("GPL");
2165 +MODULE_VERSION(UNICAM_VERSION);
2167 +++ b/drivers/media/platform/bcm2835/vc4-regs-unicam.h
2170 + * Copyright (C) 2017 Raspberry Pi Trading.
2171 + * Dave Stevenson <dave.stevenson@raspberrypi.org>
2173 + * This program is free software; you can redistribute it and/or modify
2174 + * it under the terms of the GNU General Public License version 2 as
2175 + * published by the Free Software Foundation.
2177 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2178 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2179 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2180 + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2181 + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2182 + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2183 + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2187 +#ifndef VC4_REGS_UNICAM_H
2188 +#define VC4_REGS_UNICAM_H
2191 + * The following values are taken from files found within the code drop
2192 + * made by Broadcom for the BCM21553 Graphics Driver, predominantly in
2193 + * brcm_usrlib/dag/vmcsx/vcinclude/hardware_vc4.h.
2194 + * They have been modified to be only the register offset.
2196 +#define UNICAM_CTRL 0x000
2197 +#define UNICAM_STA 0x004
2198 +#define UNICAM_ANA 0x008
2199 +#define UNICAM_PRI 0x00c
2200 +#define UNICAM_CLK 0x010
2201 +#define UNICAM_CLT 0x014
2202 +#define UNICAM_DAT0 0x018
2203 +#define UNICAM_DAT1 0x01c
2204 +#define UNICAM_DAT2 0x020
2205 +#define UNICAM_DAT3 0x024
2206 +#define UNICAM_DLT 0x028
2207 +#define UNICAM_CMP0 0x02c
2208 +#define UNICAM_CMP1 0x030
2209 +#define UNICAM_CAP0 0x034
2210 +#define UNICAM_CAP1 0x038
2211 +#define UNICAM_ICTL 0x100
2212 +#define UNICAM_ISTA 0x104
2213 +#define UNICAM_IDI0 0x108
2214 +#define UNICAM_IPIPE 0x10c
2215 +#define UNICAM_IBSA0 0x110
2216 +#define UNICAM_IBEA0 0x114
2217 +#define UNICAM_IBLS 0x118
2218 +#define UNICAM_IBWP 0x11c
2219 +#define UNICAM_IHWIN 0x120
2220 +#define UNICAM_IHSTA 0x124
2221 +#define UNICAM_IVWIN 0x128
2222 +#define UNICAM_IVSTA 0x12c
2223 +#define UNICAM_ICC 0x130
2224 +#define UNICAM_ICS 0x134
2225 +#define UNICAM_IDC 0x138
2226 +#define UNICAM_IDPO 0x13c
2227 +#define UNICAM_IDCA 0x140
2228 +#define UNICAM_IDCD 0x144
2229 +#define UNICAM_IDS 0x148
2230 +#define UNICAM_DCS 0x200
2231 +#define UNICAM_DBSA0 0x204
2232 +#define UNICAM_DBEA0 0x208
2233 +#define UNICAM_DBWP 0x20c
2234 +#define UNICAM_DBCTL 0x300
2235 +#define UNICAM_IBSA1 0x304
2236 +#define UNICAM_IBEA1 0x308
2237 +#define UNICAM_IDI1 0x30c
2238 +#define UNICAM_DBSA1 0x310
2239 +#define UNICAM_DBEA1 0x314
2240 +#define UNICAM_MISC 0x400
2243 + * The following bitmasks are from the kernel released by Broadcom
2244 + * for Android - https://android.googlesource.com/kernel/bcm/
2245 + * The Rhea, Hawaii, and Java chips all contain the same VideoCore4
2246 + * Unicam block as BCM2835, as defined in eg
2247 + * arch/arm/mach-rhea/include/mach/rdb_A0/brcm_rdb_cam.h and similar.
2248 + * Values reworked to use the kernel BIT and GENMASK macros.
2250 + * Some of the bit mnenomics have been amended to match the datasheet.
2252 +/* UNICAM_CTRL Register */
2253 +#define UNICAM_CPE BIT(0)
2254 +#define UNICAM_MEM BIT(1)
2255 +#define UNICAM_CPR BIT(2)
2256 +#define UNICAM_CPM_MASK GENMASK(3, 3)
2257 +#define UNICAM_CPM_CSI2 0
2258 +#define UNICAM_CPM_CCP2 1
2259 +#define UNICAM_SOE BIT(4)
2260 +#define UNICAM_DCM_MASK GENMASK(5, 5)
2261 +#define UNICAM_DCM_STROBE 0
2262 +#define UNICAM_DCM_DATA 1
2263 +#define UNICAM_SLS BIT(6)
2264 +#define UNICAM_PFT_MASK GENMASK(11, 8)
2265 +#define UNICAM_OET_MASK GENMASK(20, 12)
2267 +/* UNICAM_STA Register */
2268 +#define UNICAM_SYN BIT(0)
2269 +#define UNICAM_CS BIT(1)
2270 +#define UNICAM_SBE BIT(2)
2271 +#define UNICAM_PBE BIT(3)
2272 +#define UNICAM_HOE BIT(4)
2273 +#define UNICAM_PLE BIT(5)
2274 +#define UNICAM_SSC BIT(6)
2275 +#define UNICAM_CRCE BIT(7)
2276 +#define UNICAM_OES BIT(8)
2277 +#define UNICAM_IFO BIT(9)
2278 +#define UNICAM_OFO BIT(10)
2279 +#define UNICAM_BFO BIT(11)
2280 +#define UNICAM_DL BIT(12)
2281 +#define UNICAM_PS BIT(13)
2282 +#define UNICAM_IS BIT(14)
2283 +#define UNICAM_PI0 BIT(15)
2284 +#define UNICAM_PI1 BIT(16)
2285 +#define UNICAM_FSI_S BIT(17)
2286 +#define UNICAM_FEI_S BIT(18)
2287 +#define UNICAM_LCI_S BIT(19)
2288 +#define UNICAM_BUF0_RDY BIT(20)
2289 +#define UNICAM_BUF0_NO BIT(21)
2290 +#define UNICAM_BUF1_RDY BIT(22)
2291 +#define UNICAM_BUF1_NO BIT(23)
2292 +#define UNICAM_DI BIT(24)
2294 +#define UNICAM_STA_MASK_ALL \
2308 +/* UNICAM_ANA Register */
2309 +#define UNICAM_APD BIT(0)
2310 +#define UNICAM_BPD BIT(1)
2311 +#define UNICAM_AR BIT(2)
2312 +#define UNICAM_DDL BIT(3)
2313 +#define UNICAM_CTATADJ_MASK GENMASK(7, 4)
2314 +#define UNICAM_PTATADJ_MASK GENMASK(11, 8)
2316 +/* UNICAM_PRI Register */
2317 +#define UNICAM_PE BIT(0)
2318 +#define UNICAM_PT_MASK GENMASK(2, 1)
2319 +#define UNICAM_NP_MASK GENMASK(7, 4)
2320 +#define UNICAM_PP_MASK GENMASK(11, 8)
2321 +#define UNICAM_BS_MASK GENMASK(15, 12)
2322 +#define UNICAM_BL_MASK GENMASK(17, 16)
2324 +/* UNICAM_CLK Register */
2325 +#define UNICAM_CLE BIT(0)
2326 +#define UNICAM_CLPD BIT(1)
2327 +#define UNICAM_CLLPE BIT(2)
2328 +#define UNICAM_CLHSE BIT(3)
2329 +#define UNICAM_CLTRE BIT(4)
2330 +#define UNICAM_CLAC_MASK GENMASK(8, 5)
2331 +#define UNICAM_CLSTE BIT(29)
2333 +/* UNICAM_CLT Register */
2334 +#define UNICAM_CLT1_MASK GENMASK(7, 0)
2335 +#define UNICAM_CLT2_MASK GENMASK(15, 8)
2337 +/* UNICAM_DATn Registers */
2338 +#define UNICAM_DLE BIT(0)
2339 +#define UNICAM_DLPD BIT(1)
2340 +#define UNICAM_DLLPE BIT(2)
2341 +#define UNICAM_DLHSE BIT(3)
2342 +#define UNICAM_DLTRE BIT(4)
2343 +#define UNICAM_DLSM BIT(5)
2344 +#define UNICAM_DLFO BIT(28)
2345 +#define UNICAM_DLSTE BIT(29)
2347 +#define UNICAM_DAT_MASK_ALL (UNICAM_DLSTE + UNICAM_DLFO)
2349 +/* UNICAM_DLT Register */
2350 +#define UNICAM_DLT1_MASK GENMASK(7, 0)
2351 +#define UNICAM_DLT2_MASK GENMASK(15, 8)
2352 +#define UNICAM_DLT3_MASK GENMASK(23, 16)
2354 +/* UNICAM_ICTL Register */
2355 +#define UNICAM_FSIE BIT(0)
2356 +#define UNICAM_FEIE BIT(1)
2357 +#define UNICAM_IBOB BIT(2)
2358 +#define UNICAM_FCM BIT(3)
2359 +#define UNICAM_TFC BIT(4)
2360 +#define UNICAM_LIP_MASK GENMASK(6, 5)
2361 +#define UNICAM_LCIE_MASK GENMASK(28, 16)
2363 +/* UNICAM_IDI0/1 Register */
2364 +#define UNICAM_ID0_MASK GENMASK(7, 0)
2365 +#define UNICAM_ID1_MASK GENMASK(15, 8)
2366 +#define UNICAM_ID2_MASK GENMASK(23, 16)
2367 +#define UNICAM_ID3_MASK GENMASK(31, 24)
2369 +/* UNICAM_ISTA Register */
2370 +#define UNICAM_FSI BIT(0)
2371 +#define UNICAM_FEI BIT(1)
2372 +#define UNICAM_LCI BIT(2)
2374 +#define UNICAM_ISTA_MASK_ALL (UNICAM_FSI + UNICAM_FEI + UNICAM_LCI)
2376 +/* UNICAM_IPIPE Register */
2377 +#define UNICAM_PUM_MASK GENMASK(2, 0)
2378 + /* Unpacking modes */
2379 + #define UNICAM_PUM_NONE 0
2380 + #define UNICAM_PUM_UNPACK6 1
2381 + #define UNICAM_PUM_UNPACK7 2
2382 + #define UNICAM_PUM_UNPACK8 3
2383 + #define UNICAM_PUM_UNPACK10 4
2384 + #define UNICAM_PUM_UNPACK12 5
2385 + #define UNICAM_PUM_UNPACK14 6
2386 + #define UNICAM_PUM_UNPACK16 7
2387 +#define UNICAM_DDM_MASK GENMASK(6, 3)
2388 +#define UNICAM_PPM_MASK GENMASK(9, 7)
2389 + /* Packing modes */
2390 + #define UNICAM_PPM_NONE 0
2391 + #define UNICAM_PPM_PACK8 1
2392 + #define UNICAM_PPM_PACK10 2
2393 + #define UNICAM_PPM_PACK12 3
2394 + #define UNICAM_PPM_PACK14 4
2395 + #define UNICAM_PPM_PACK16 5
2396 +#define UNICAM_DEM_MASK GENMASK(11, 10)
2397 +#define UNICAM_DEBL_MASK GENMASK(14, 12)
2398 +#define UNICAM_ICM_MASK GENMASK(16, 15)
2399 +#define UNICAM_IDM_MASK GENMASK(17, 17)
2401 +/* UNICAM_ICC Register */
2402 +#define UNICAM_ICFL_MASK GENMASK(4, 0)
2403 +#define UNICAM_ICFH_MASK GENMASK(9, 5)
2404 +#define UNICAM_ICST_MASK GENMASK(12, 10)
2405 +#define UNICAM_ICLT_MASK GENMASK(15, 13)
2406 +#define UNICAM_ICLL_MASK GENMASK(31, 16)
2408 +/* UNICAM_DCS Register */
2409 +#define UNICAM_DIE BIT(0)
2410 +#define UNICAM_DIM BIT(1)
2411 +#define UNICAM_DBOB BIT(3)
2412 +#define UNICAM_FDE BIT(4)
2413 +#define UNICAM_LDP BIT(5)
2414 +#define UNICAM_EDL_MASK GENMASK(15, 8)
2416 +/* UNICAM_DBCTL Register */
2417 +#define UNICAM_DBEN BIT(0)
2418 +#define UNICAM_BUF0_IE BIT(1)
2419 +#define UNICAM_BUF1_IE BIT(2)
2421 +/* UNICAM_CMP[0,1] register */
2422 +#define UNICAM_PCE BIT(31)
2423 +#define UNICAM_GI BIT(9)
2424 +#define UNICAM_CPH BIT(8)
2425 +#define UNICAM_PCVC_MASK GENMASK(7, 6)
2426 +#define UNICAM_PCDT_MASK GENMASK(5, 0)
2428 +/* UNICAM_MISC register */
2429 +#define UNICAM_FL0 BIT(6)
2430 +#define UNICAM_FL1 BIT(9)