1 // SPDX-License-Identifier: GPL-2.0-only
3 * BCM2835 Unicam Capture Driver
5 * Copyright (C) 2017-2020 - Raspberry Pi (Trading) Ltd.
7 * Dave Stevenson <dave.stevenson@raspberrypi.com>
9 * Based on TI am437x driver by
10 * Benoit Parrot <bparrot@ti.com>
11 * Lad, Prabhakar <prabhakar.csengg@gmail.com>
13 * and TI CAL camera interface driver by
14 * Benoit Parrot <bparrot@ti.com>
17 * There are two camera drivers in the kernel for BCM283x - this one
18 * and bcm2835-camera (currently in staging).
20 * This driver directly controls the Unicam peripheral - there is no
21 * involvement with the VideoCore firmware. Unicam receives CSI-2 or
22 * CCP2 data and writes it into SDRAM.
23 * The only potential processing options are to repack Bayer data into an
24 * alternate format, and applying windowing.
25 * The repacking does not shift the data, so can repack V4L2_PIX_FMT_Sxxxx10P
26 * to V4L2_PIX_FMT_Sxxxx10, or V4L2_PIX_FMT_Sxxxx12P to V4L2_PIX_FMT_Sxxxx12,
27 * but not generically up to V4L2_PIX_FMT_Sxxxx16. The driver will add both
28 * formats where the relevant formats are defined, and will automatically
29 * configure the repacking as required.
30 * Support for windowing may be added later.
32 * It should be possible to connect this driver to any sensor with a
33 * suitable output interface and V4L2 subdevice driver.
35 * bcm2835-camera uses the VideoCore firmware to control the sensor,
36 * Unicam, ISP, and all tuner control loops. Fully processed frames are
37 * delivered to the driver by the firmware. It only has sensor drivers
38 * for Omnivision OV5647, and Sony IMX219 sensors.
40 * The two drivers are mutually exclusive for the same Unicam instance.
41 * The VideoCore firmware checks the device tree configuration during boot.
42 * If it finds device tree nodes called csi0 or csi1 it will block the
43 * firmware from accessing the peripheral, and bcm2835-camera will
44 * not be able to stream data.
47 #include <linux/clk.h>
48 #include <linux/delay.h>
49 #include <linux/device.h>
50 #include <linux/dma-mapping.h>
51 #include <linux/err.h>
52 #include <linux/init.h>
53 #include <linux/interrupt.h>
55 #include <linux/module.h>
56 #include <linux/of_device.h>
57 #include <linux/of_graph.h>
58 #include <linux/pinctrl/consumer.h>
59 #include <linux/platform_device.h>
60 #include <linux/pm_runtime.h>
61 #include <linux/slab.h>
62 #include <linux/uaccess.h>
63 #include <linux/videodev2.h>
65 #include <media/v4l2-common.h>
66 #include <media/v4l2-ctrls.h>
67 #include <media/v4l2-dev.h>
68 #include <media/v4l2-device.h>
69 #include <media/v4l2-dv-timings.h>
70 #include <media/v4l2-event.h>
71 #include <media/v4l2-ioctl.h>
72 #include <media/v4l2-fwnode.h>
73 #include <media/videobuf2-dma-contig.h>
75 #include <media/v4l2-async.h>
77 #include "vc4-regs-unicam.h"
79 #define UNICAM_MODULE_NAME "unicam"
80 #define UNICAM_VERSION "0.1.0"
83 module_param(debug, int, 0644);
84 MODULE_PARM_DESC(debug, "Debug level 0-3");
86 #define unicam_dbg(level, dev, fmt, arg...) \
87 v4l2_dbg(level, debug, &(dev)->v4l2_dev, fmt, ##arg)
88 #define unicam_info(dev, fmt, arg...) \
89 v4l2_info(&(dev)->v4l2_dev, fmt, ##arg)
90 #define unicam_err(dev, fmt, arg...) \
91 v4l2_err(&(dev)->v4l2_dev, fmt, ##arg)
94 * To protect against a dodgy sensor driver never returning an error from
95 * enum_mbus_code, set a maximum index value to be used.
97 #define MAX_ENUM_MBUS_CODE 128
100 * Stride is a 16 bit register, but also has to be a multiple of 32.
102 #define BPL_ALIGNMENT 32
103 #define MAX_BYTESPERLINE ((1 << 16) - BPL_ALIGNMENT)
105 * Max width is therefore determined by the max stride divided by
106 * the number of bits per pixel. Take 32bpp as a
108 * No imposed limit on the height, so adopt a square image for want
109 * of anything better.
111 #define MAX_WIDTH (MAX_BYTESPERLINE / 4)
112 #define MAX_HEIGHT MAX_WIDTH
113 /* Define a nominal minimum image size */
115 #define MIN_HEIGHT 16
116 /* Default size of the embedded buffer */
117 #define UNICAM_EMBEDDED_SIZE 8192
120 * Size of the dummy buffer. Can be any size really, but the DMA
121 * allocation works in units of page sizes.
123 #define DUMMY_BUF_SIZE (PAGE_SIZE)
132 * struct unicam_fmt - Unicam media bus format information
133 * @pixelformat: V4L2 pixel format FCC identifier. 0 if n/a.
134 * @repacked_fourcc: V4L2 pixel format FCC identifier if the data is expanded
135 * out to 16bpp. 0 if n/a.
136 * @code: V4L2 media bus format code.
137 * @depth: Bits per pixel as delivered from the source.
138 * @csi_dt: CSI data type.
139 * @check_variants: Flag to denote that there are multiple mediabus formats
140 * still in the list that could match this V4L2 format.
151 static const struct unicam_fmt formats[] = {
154 .fourcc = V4L2_PIX_FMT_YUYV,
155 .code = MEDIA_BUS_FMT_YUYV8_2X8,
160 .fourcc = V4L2_PIX_FMT_UYVY,
161 .code = MEDIA_BUS_FMT_UYVY8_2X8,
166 .fourcc = V4L2_PIX_FMT_YVYU,
167 .code = MEDIA_BUS_FMT_YVYU8_2X8,
172 .fourcc = V4L2_PIX_FMT_VYUY,
173 .code = MEDIA_BUS_FMT_VYUY8_2X8,
178 .fourcc = V4L2_PIX_FMT_YUYV,
179 .code = MEDIA_BUS_FMT_YUYV8_1X16,
183 .fourcc = V4L2_PIX_FMT_UYVY,
184 .code = MEDIA_BUS_FMT_UYVY8_1X16,
188 .fourcc = V4L2_PIX_FMT_YVYU,
189 .code = MEDIA_BUS_FMT_YVYU8_1X16,
193 .fourcc = V4L2_PIX_FMT_VYUY,
194 .code = MEDIA_BUS_FMT_VYUY8_1X16,
199 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
200 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
204 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
205 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
209 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
210 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
214 .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
215 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
219 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
220 .code = MEDIA_BUS_FMT_RGB888_1X24,
224 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
225 .code = MEDIA_BUS_FMT_BGR888_1X24,
229 .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
230 .code = MEDIA_BUS_FMT_ARGB8888_1X32,
235 .fourcc = V4L2_PIX_FMT_SBGGR8,
236 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
240 .fourcc = V4L2_PIX_FMT_SGBRG8,
241 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
245 .fourcc = V4L2_PIX_FMT_SGRBG8,
246 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
250 .fourcc = V4L2_PIX_FMT_SRGGB8,
251 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
255 .fourcc = V4L2_PIX_FMT_SBGGR10P,
256 .repacked_fourcc = V4L2_PIX_FMT_SBGGR10,
257 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
261 .fourcc = V4L2_PIX_FMT_SGBRG10P,
262 .repacked_fourcc = V4L2_PIX_FMT_SGBRG10,
263 .code = MEDIA_BUS_FMT_SGBRG10_1X10,
267 .fourcc = V4L2_PIX_FMT_SGRBG10P,
268 .repacked_fourcc = V4L2_PIX_FMT_SGRBG10,
269 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
273 .fourcc = V4L2_PIX_FMT_SRGGB10P,
274 .repacked_fourcc = V4L2_PIX_FMT_SRGGB10,
275 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
279 .fourcc = V4L2_PIX_FMT_SBGGR12P,
280 .repacked_fourcc = V4L2_PIX_FMT_SBGGR12,
281 .code = MEDIA_BUS_FMT_SBGGR12_1X12,
285 .fourcc = V4L2_PIX_FMT_SGBRG12P,
286 .repacked_fourcc = V4L2_PIX_FMT_SGBRG12,
287 .code = MEDIA_BUS_FMT_SGBRG12_1X12,
291 .fourcc = V4L2_PIX_FMT_SGRBG12P,
292 .repacked_fourcc = V4L2_PIX_FMT_SGRBG12,
293 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
297 .fourcc = V4L2_PIX_FMT_SRGGB12P,
298 .repacked_fourcc = V4L2_PIX_FMT_SRGGB12,
299 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
303 .fourcc = V4L2_PIX_FMT_SBGGR14P,
304 .code = MEDIA_BUS_FMT_SBGGR14_1X14,
308 .fourcc = V4L2_PIX_FMT_SGBRG14P,
309 .code = MEDIA_BUS_FMT_SGBRG14_1X14,
313 .fourcc = V4L2_PIX_FMT_SGRBG14P,
314 .code = MEDIA_BUS_FMT_SGRBG14_1X14,
318 .fourcc = V4L2_PIX_FMT_SRGGB14P,
319 .code = MEDIA_BUS_FMT_SRGGB14_1X14,
324 * 16 bit Bayer formats could be supported, but there is no CSI2
325 * data_type defined for raw 16, and no sensors that produce it at
329 /* Greyscale formats */
330 .fourcc = V4L2_PIX_FMT_GREY,
331 .code = MEDIA_BUS_FMT_Y8_1X8,
335 .fourcc = V4L2_PIX_FMT_Y10P,
336 .repacked_fourcc = V4L2_PIX_FMT_Y10,
337 .code = MEDIA_BUS_FMT_Y10_1X10,
341 /* NB There is no packed V4L2 fourcc for this format. */
342 .repacked_fourcc = V4L2_PIX_FMT_Y12,
343 .code = MEDIA_BUS_FMT_Y12_1X12,
347 /* Embedded data format */
349 .fourcc = V4L2_META_FMT_SENSOR_DATA,
350 .code = MEDIA_BUS_FMT_SENSOR_DATA,
355 struct unicam_buffer {
356 struct vb2_v4l2_buffer vb;
357 struct list_head list;
360 static inline struct unicam_buffer *to_unicam_buffer(struct vb2_buffer *vb)
362 return container_of(vb, struct unicam_buffer, vb.vb2_buf);
370 /* Pointer pointing to current v4l2_buffer */
371 struct unicam_buffer *cur_frm;
372 /* Pointer pointing to next v4l2_buffer */
373 struct unicam_buffer *next_frm;
375 const struct unicam_fmt *fmt;
376 /* Used to store current pixel format */
377 struct v4l2_format v_fmt;
378 /* Used to store current mbus frame format */
379 struct v4l2_mbus_framefmt m_fmt;
380 /* Buffer queue used in video-buf */
381 struct vb2_queue buffer_queue;
382 /* Queue of filled frames */
383 struct list_head dma_queue;
384 /* IRQ lock for DMA queue */
385 spinlock_t dma_queue_lock;
386 /* lock used to access this structure */
388 /* Identifies video device for this channel */
389 struct video_device video_dev;
390 /* Pointer to the parent handle */
391 struct unicam_device *dev;
392 struct media_pad pad;
393 unsigned int embedded_lines;
395 * Dummy buffer intended to be used by unicam
396 * if we have no other queued buffers to swap to.
398 void *dummy_buf_cpu_addr;
399 dma_addr_t dummy_buf_dma_addr;
402 struct unicam_device {
405 /* V4l2 specific parameters */
406 struct v4l2_async_subdev asd;
408 /* peripheral base address */
410 /* clock gating base address */
411 void __iomem *clk_gate_base;
415 struct v4l2_device v4l2_dev;
416 struct media_device mdev;
419 struct platform_device *pdev;
420 /* subdevice async Notifier */
421 struct v4l2_async_notifier notifier;
422 unsigned int sequence;
424 /* ptr to sub device */
425 struct v4l2_subdev *sensor;
426 /* Pad config for the sensor */
427 struct v4l2_subdev_pad_config *sensor_config;
429 enum v4l2_mbus_type bus_type;
431 * Stores bus.mipi_csi2.flags for CSI2 sensors, or
432 * bus.mipi_csi1.strobe for CCP2.
434 unsigned int bus_flags;
435 unsigned int max_data_lanes;
436 unsigned int active_data_lanes;
437 bool sensor_embedded_data;
439 struct unicam_node node[MAX_NODES];
440 struct v4l2_ctrl_handler ctrl_handler;
443 static inline struct unicam_device *
444 to_unicam_device(struct v4l2_device *v4l2_dev)
446 return container_of(v4l2_dev, struct unicam_device, v4l2_dev);
449 /* Hardware access */
450 static inline void clk_write(struct unicam_device *dev, u32 val)
452 writel(val | 0x5a000000, dev->clk_gate_base);
455 static inline u32 reg_read(struct unicam_device *dev, u32 offset)
457 return readl(dev->base + offset);
460 static inline void reg_write(struct unicam_device *dev, u32 offset, u32 val)
462 writel(val, dev->base + offset);
465 static inline int get_field(u32 value, u32 mask)
467 return (value & mask) >> __ffs(mask);
470 static inline void set_field(u32 *valp, u32 field, u32 mask)
475 val |= (field << __ffs(mask)) & mask;
479 static inline u32 reg_read_field(struct unicam_device *dev, u32 offset,
482 return get_field(reg_read(dev, offset), mask);
485 static inline void reg_write_field(struct unicam_device *dev, u32 offset,
488 u32 val = reg_read(dev, offset);
490 set_field(&val, field, mask);
491 reg_write(dev, offset, val);
494 /* Power management functions */
495 static inline int unicam_runtime_get(struct unicam_device *dev)
497 return pm_runtime_get_sync(&dev->pdev->dev);
500 static inline void unicam_runtime_put(struct unicam_device *dev)
502 pm_runtime_put_sync(&dev->pdev->dev);
505 /* Format setup functions */
506 static const struct unicam_fmt *find_format_by_code(u32 code)
510 for (i = 0; i < ARRAY_SIZE(formats); i++) {
511 if (formats[i].code == code)
518 static int check_mbus_format(struct unicam_device *dev,
519 const struct unicam_fmt *format)
524 for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
525 struct v4l2_subdev_mbus_code_enum mbus_code = {
528 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
531 ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
534 if (!ret && mbus_code.code == format->code)
541 static const struct unicam_fmt *find_format_by_pix(struct unicam_device *dev,
546 for (i = 0; i < ARRAY_SIZE(formats); i++) {
547 if (formats[i].fourcc == pixelformat ||
548 formats[i].repacked_fourcc == pixelformat) {
549 if (formats[i].check_variants &&
550 !check_mbus_format(dev, &formats[i]))
559 static inline unsigned int bytes_per_line(u32 width,
560 const struct unicam_fmt *fmt,
563 if (v4l2_fourcc == fmt->repacked_fourcc)
564 /* Repacking always goes to 16bpp */
565 return ALIGN(width << 1, BPL_ALIGNMENT);
567 return ALIGN((width * fmt->depth) >> 3, BPL_ALIGNMENT);
570 static int __subdev_get_format(struct unicam_device *dev,
571 struct v4l2_mbus_framefmt *fmt, int pad_id)
573 struct v4l2_subdev_format sd_fmt = {
574 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
579 ret = v4l2_subdev_call(dev->sensor, pad, get_fmt, dev->sensor_config,
584 *fmt = sd_fmt.format;
586 unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__,
587 fmt->width, fmt->height, fmt->code);
592 static int __subdev_set_format(struct unicam_device *dev,
593 struct v4l2_mbus_framefmt *fmt, int pad_id)
595 struct v4l2_subdev_format sd_fmt = {
596 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
601 sd_fmt.format = *fmt;
603 ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
608 *fmt = sd_fmt.format;
610 if (pad_id == IMAGE_PAD)
611 unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__, fmt->width,
612 fmt->height, fmt->code);
614 unicam_dbg(1, dev, "%s Embedded data code:%04x\n", __func__,
620 static int unicam_calc_format_size_bpl(struct unicam_device *dev,
621 const struct unicam_fmt *fmt,
622 struct v4l2_format *f)
624 unsigned int min_bytesperline;
626 v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, MAX_WIDTH, 2,
627 &f->fmt.pix.height, MIN_HEIGHT, MAX_HEIGHT, 0,
630 min_bytesperline = bytes_per_line(f->fmt.pix.width, fmt,
631 f->fmt.pix.pixelformat);
633 if (f->fmt.pix.bytesperline > min_bytesperline &&
634 f->fmt.pix.bytesperline <= MAX_BYTESPERLINE)
635 f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
638 f->fmt.pix.bytesperline = min_bytesperline;
640 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
642 unicam_dbg(3, dev, "%s: fourcc: %08X size: %dx%d bpl:%d img_size:%d\n",
644 f->fmt.pix.pixelformat,
645 f->fmt.pix.width, f->fmt.pix.height,
646 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
651 static int unicam_reset_format(struct unicam_node *node)
653 struct unicam_device *dev = node->dev;
654 struct v4l2_mbus_framefmt mbus_fmt;
657 if (dev->sensor_embedded_data || node->pad_id != METADATA_PAD) {
658 ret = __subdev_get_format(dev, &mbus_fmt, node->pad_id);
660 unicam_err(dev, "Failed to get_format - ret %d\n", ret);
664 if (mbus_fmt.code != node->fmt->code) {
665 unicam_err(dev, "code mismatch - fmt->code %08x, mbus_fmt.code %08x\n",
666 node->fmt->code, mbus_fmt.code);
671 if (node->pad_id == IMAGE_PAD) {
672 v4l2_fill_pix_format(&node->v_fmt.fmt.pix, &mbus_fmt);
673 node->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
674 unicam_calc_format_size_bpl(dev, node->fmt, &node->v_fmt);
676 node->v_fmt.type = V4L2_BUF_TYPE_META_CAPTURE;
677 node->v_fmt.fmt.meta.dataformat = V4L2_META_FMT_SENSOR_DATA;
678 if (dev->sensor_embedded_data) {
679 node->v_fmt.fmt.meta.buffersize =
680 mbus_fmt.width * mbus_fmt.height;
681 node->embedded_lines = mbus_fmt.height;
683 node->v_fmt.fmt.meta.buffersize = UNICAM_EMBEDDED_SIZE;
684 node->embedded_lines = 1;
688 node->m_fmt = mbus_fmt;
692 static void unicam_wr_dma_addr(struct unicam_device *dev, dma_addr_t dmaaddr,
693 unsigned int buffer_size, int pad_id)
695 dma_addr_t endaddr = dmaaddr + buffer_size;
698 * dmaaddr and endaddr should be a 32-bit address with the top two bits
699 * set to 0x3 to signify uncached access through the Videocore memory
702 WARN_ON((dmaaddr >> 30) != 0x3 || (endaddr >> 30) != 0x3);
704 if (pad_id == IMAGE_PAD) {
705 reg_write(dev, UNICAM_IBSA0, dmaaddr);
706 reg_write(dev, UNICAM_IBEA0, endaddr);
708 reg_write(dev, UNICAM_DBSA0, dmaaddr);
709 reg_write(dev, UNICAM_DBEA0, endaddr);
713 static inline unsigned int unicam_get_lines_done(struct unicam_device *dev)
715 dma_addr_t start_addr, cur_addr;
716 unsigned int stride = dev->node[IMAGE_PAD].v_fmt.fmt.pix.bytesperline;
717 struct unicam_buffer *frm = dev->node[IMAGE_PAD].cur_frm;
722 start_addr = vb2_dma_contig_plane_dma_addr(&frm->vb.vb2_buf, 0);
723 cur_addr = reg_read(dev, UNICAM_IBWP);
724 return (unsigned int)(cur_addr - start_addr) / stride;
727 static inline void unicam_schedule_next_buffer(struct unicam_node *node)
729 struct unicam_device *dev = node->dev;
730 struct unicam_buffer *buf;
734 buf = list_first_entry(&node->dma_queue, struct unicam_buffer, list);
735 node->next_frm = buf;
736 list_del(&buf->list);
738 addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
739 size = (node->pad_id == IMAGE_PAD) ?
740 node->v_fmt.fmt.pix.sizeimage :
741 node->v_fmt.fmt.meta.buffersize;
743 unicam_wr_dma_addr(dev, addr, size, node->pad_id);
746 static inline void unicam_schedule_dummy_buffer(struct unicam_node *node)
748 struct unicam_device *dev = node->dev;
750 unicam_dbg(3, dev, "Scheduling dummy buffer for node %d\n",
753 unicam_wr_dma_addr(dev, node->dummy_buf_dma_addr, DUMMY_BUF_SIZE,
755 node->next_frm = NULL;
758 static inline void unicam_process_buffer_complete(struct unicam_node *node,
759 unsigned int sequence)
761 node->cur_frm->vb.field = node->m_fmt.field;
762 node->cur_frm->vb.sequence = sequence;
764 vb2_buffer_done(&node->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
767 static bool unicam_all_nodes_streaming(struct unicam_device *dev)
771 ret = dev->node[IMAGE_PAD].open && dev->node[IMAGE_PAD].streaming;
772 ret &= !dev->node[METADATA_PAD].open ||
773 dev->node[METADATA_PAD].streaming;
777 static bool unicam_all_nodes_disabled(struct unicam_device *dev)
779 return !dev->node[IMAGE_PAD].streaming &&
780 !dev->node[METADATA_PAD].streaming;
783 static void unicam_queue_event_sof(struct unicam_device *unicam)
785 struct v4l2_event event = {
786 .type = V4L2_EVENT_FRAME_SYNC,
787 .u.frame_sync.frame_sequence = unicam->sequence,
790 v4l2_event_queue(&unicam->node[IMAGE_PAD].video_dev, &event);
794 * unicam_isr : ISR handler for unicam capture
796 * @dev_id: dev_id ptr
798 * It changes status of the captured buffer, takes next buffer from the queue
799 * and sets its address in unicam registers
801 static irqreturn_t unicam_isr(int irq, void *dev)
803 struct unicam_device *unicam = dev;
804 unsigned int lines_done = unicam_get_lines_done(dev);
805 unsigned int sequence = unicam->sequence;
811 * Don't service interrupts if not streaming.
812 * Avoids issues if the VPU should enable the
813 * peripheral without the kernel knowing (that
814 * shouldn't happen, but causes issues if it does).
816 if (unicam_all_nodes_disabled(unicam))
819 sta = reg_read(unicam, UNICAM_STA);
820 /* Write value back to clear the interrupts */
821 reg_write(unicam, UNICAM_STA, sta);
823 ista = reg_read(unicam, UNICAM_ISTA);
824 /* Write value back to clear the interrupts */
825 reg_write(unicam, UNICAM_ISTA, ista);
827 unicam_dbg(3, unicam, "ISR: ISTA: 0x%X, STA: 0x%X, sequence %d, lines done %d",
828 ista, sta, sequence, lines_done);
830 if (!(sta & (UNICAM_IS | UNICAM_PI0)))
834 * We must run the frame end handler first. If we have a valid next_frm
835 * and we get a simultaneout FE + FS interrupt, running the FS handler
836 * first would null out the next_frm ptr and we would have lost the
839 if (ista & UNICAM_FEI || sta & UNICAM_PI0) {
841 * Ensure we have swapped buffers already as we can't
842 * stop the peripheral. If no buffer is available, use a
843 * dummy buffer to dump out frames until we get a new buffer
846 for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
847 if (!unicam->node[i].streaming)
850 if (unicam->node[i].cur_frm)
851 unicam_process_buffer_complete(&unicam->node[i],
853 unicam->node[i].cur_frm = unicam->node[i].next_frm;
858 if (ista & UNICAM_FSI) {
860 * Timestamp is to be when the first data byte was captured,
864 for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
865 if (!unicam->node[i].streaming)
868 if (unicam->node[i].cur_frm)
869 unicam->node[i].cur_frm->vb.vb2_buf.timestamp =
872 * Set the next frame output to go to a dummy frame
873 * if we have not managed to obtain another frame
876 unicam_schedule_dummy_buffer(&unicam->node[i]);
879 unicam_queue_event_sof(unicam);
883 * Cannot swap buffer at frame end, there may be a race condition
884 * where the HW does not actually swap it if the new frame has
887 if (ista & (UNICAM_FSI | UNICAM_LCI) && !(ista & UNICAM_FEI)) {
888 for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
889 if (!unicam->node[i].streaming)
892 spin_lock(&unicam->node[i].dma_queue_lock);
893 if (!list_empty(&unicam->node[i].dma_queue) &&
894 !unicam->node[i].next_frm)
895 unicam_schedule_next_buffer(&unicam->node[i]);
896 spin_unlock(&unicam->node[i].dma_queue_lock);
900 if (reg_read(unicam, UNICAM_ICTL) & UNICAM_FCM) {
901 /* Switch out of trigger mode if selected */
902 reg_write_field(unicam, UNICAM_ICTL, 1, UNICAM_TFC);
903 reg_write_field(unicam, UNICAM_ICTL, 0, UNICAM_FCM);
908 static int unicam_querycap(struct file *file, void *priv,
909 struct v4l2_capability *cap)
911 struct unicam_node *node = video_drvdata(file);
912 struct unicam_device *dev = node->dev;
914 strlcpy(cap->driver, UNICAM_MODULE_NAME, sizeof(cap->driver));
915 strlcpy(cap->card, UNICAM_MODULE_NAME, sizeof(cap->card));
917 snprintf(cap->bus_info, sizeof(cap->bus_info),
918 "platform:%s", dev_name(&dev->pdev->dev));
920 cap->capabilities |= V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_META_CAPTURE;
925 static int unicam_enum_fmt_vid_cap(struct file *file, void *priv,
926 struct v4l2_fmtdesc *f)
928 struct unicam_node *node = video_drvdata(file);
929 struct unicam_device *dev = node->dev;
930 unsigned int index = 0;
934 if (node->pad_id != IMAGE_PAD)
937 for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
938 struct v4l2_subdev_mbus_code_enum mbus_code = {
941 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
943 const struct unicam_fmt *fmt;
945 ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
949 "subdev->enum_mbus_code idx %d returned %d - index invalid\n",
954 fmt = find_format_by_code(mbus_code.code);
957 if (index == f->index) {
958 f->pixelformat = fmt->fourcc;
963 if (fmt->repacked_fourcc) {
964 if (index == f->index) {
965 f->pixelformat = fmt->repacked_fourcc;
976 static int unicam_g_fmt_vid_cap(struct file *file, void *priv,
977 struct v4l2_format *f)
979 struct v4l2_mbus_framefmt mbus_fmt = {0};
980 struct unicam_node *node = video_drvdata(file);
981 struct unicam_device *dev = node->dev;
982 const struct unicam_fmt *fmt = NULL;
985 if (node->pad_id != IMAGE_PAD)
989 * If a flip has occurred in the sensor, the fmt code might have
990 * changed. So we will need to re-fetch the format from the subdevice.
992 ret = __subdev_get_format(dev, &mbus_fmt, node->pad_id);
996 /* Find the V4L2 format from mbus code. We must match a known format. */
997 fmt = find_format_by_code(mbus_fmt.code);
1002 node->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
1009 const struct unicam_fmt *get_first_supported_format(struct unicam_device *dev)
1011 struct v4l2_subdev_mbus_code_enum mbus_code;
1012 const struct unicam_fmt *fmt = NULL;
1016 for (i = 0; ret != -EINVAL && ret != -ENOIOCTLCMD; ++i) {
1017 memset(&mbus_code, 0, sizeof(mbus_code));
1018 mbus_code.index = i;
1019 mbus_code.pad = IMAGE_PAD;
1020 mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1022 ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
1026 "subdev->enum_mbus_code idx %u returned %d - continue\n",
1031 unicam_dbg(2, dev, "subdev %s: code: 0x%08x idx: %u\n",
1032 dev->sensor->name, mbus_code.code, i);
1034 fmt = find_format_by_code(mbus_code.code);
1035 unicam_dbg(2, dev, "fmt 0x%08x returned as %p, V4L2 FOURCC 0x%08x, csi_dt 0x%02x\n",
1036 mbus_code.code, fmt, fmt ? fmt->fourcc : 0,
1037 fmt ? fmt->csi_dt : 0);
1045 static int unicam_try_fmt_vid_cap(struct file *file, void *priv,
1046 struct v4l2_format *f)
1048 struct unicam_node *node = video_drvdata(file);
1049 struct unicam_device *dev = node->dev;
1050 struct v4l2_subdev_format sd_fmt = {
1051 .which = V4L2_SUBDEV_FORMAT_TRY,
1054 struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
1055 const struct unicam_fmt *fmt;
1058 if (node->pad_id != IMAGE_PAD)
1061 fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
1064 * Pixel format not supported by unicam. Choose the first
1065 * supported format, and let the sensor choose something else.
1067 unicam_dbg(3, dev, "Fourcc format (0x%08x) not found. Use first format.\n",
1068 f->fmt.pix.pixelformat);
1071 f->fmt.pix.pixelformat = fmt->fourcc;
1074 v4l2_fill_mbus_format(mbus_fmt, &f->fmt.pix, fmt->code);
1076 * No support for receiving interlaced video, so never
1077 * request it from the sensor subdev.
1079 mbus_fmt->field = V4L2_FIELD_NONE;
1081 ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
1083 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1086 if (mbus_fmt->field != V4L2_FIELD_NONE)
1087 unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
1089 v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
1090 if (mbus_fmt->code != fmt->code) {
1091 /* Sensor has returned an alternate format */
1092 fmt = find_format_by_code(mbus_fmt->code);
1095 * The alternate format is one unicam can't support.
1096 * Find the first format that is supported by both, and
1099 fmt = get_first_supported_format(dev);
1100 mbus_fmt->code = fmt->code;
1102 ret = v4l2_subdev_call(dev->sensor, pad, set_fmt,
1103 dev->sensor_config, &sd_fmt);
1104 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1107 if (mbus_fmt->field != V4L2_FIELD_NONE)
1108 unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
1110 v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
1112 if (mbus_fmt->code != fmt->code) {
1114 * We've set a format that the sensor reports
1115 * as being supported, but it refuses to set it.
1116 * Not much else we can do.
1117 * Assume that the sensor driver may accept the
1118 * format when it is set (rather than tried).
1120 unicam_err(dev, "Sensor won't accept default format, and Unicam can't support sensor default\n");
1125 f->fmt.pix.pixelformat = fmt->fourcc;
1127 f->fmt.pix.pixelformat = fmt->repacked_fourcc;
1130 return unicam_calc_format_size_bpl(dev, fmt, f);
1133 static int unicam_s_fmt_vid_cap(struct file *file, void *priv,
1134 struct v4l2_format *f)
1136 struct unicam_node *node = video_drvdata(file);
1137 struct unicam_device *dev = node->dev;
1138 struct vb2_queue *q = &node->buffer_queue;
1139 struct v4l2_mbus_framefmt mbus_fmt = {0};
1140 const struct unicam_fmt *fmt;
1146 ret = unicam_try_fmt_vid_cap(file, priv, f);
1150 fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
1153 * Unknown pixel format - adopt a default.
1154 * This shouldn't happen as try_fmt should have resolved any
1157 fmt = get_first_supported_format(dev);
1160 * It shouldn't be possible to get here with no
1164 f->fmt.pix.pixelformat = fmt->fourcc;
1168 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, fmt->code);
1170 ret = __subdev_set_format(dev, &mbus_fmt, node->pad_id);
1172 unicam_dbg(3, dev, "%s __subdev_set_format failed %d\n",
1177 /* Just double check nothing has gone wrong */
1178 if (mbus_fmt.code != fmt->code) {
1180 "%s subdev changed format on us, this should not happen\n",
1186 node->v_fmt.fmt.pix.pixelformat = f->fmt.pix.pixelformat;
1187 node->v_fmt.fmt.pix.bytesperline = f->fmt.pix.bytesperline;
1188 unicam_reset_format(node);
1191 "%s %dx%d, mbus_fmt 0x%08X, V4L2 pix 0x%08X.\n",
1192 __func__, node->v_fmt.fmt.pix.width,
1193 node->v_fmt.fmt.pix.height, mbus_fmt.code,
1194 node->v_fmt.fmt.pix.pixelformat);
1201 static int unicam_enum_fmt_meta_cap(struct file *file, void *priv,
1202 struct v4l2_fmtdesc *f)
1204 struct unicam_node *node = video_drvdata(file);
1205 struct unicam_device *dev = node->dev;
1206 const struct unicam_fmt *fmt;
1210 if (node->pad_id != METADATA_PAD || f->index != 0)
1213 if (dev->sensor_embedded_data) {
1214 struct v4l2_subdev_mbus_code_enum mbus_code = {
1216 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1217 .pad = METADATA_PAD,
1220 ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
1224 "subdev->enum_mbus_code idx 0 returned %d - index invalid\n",
1229 code = mbus_code.code;
1231 code = MEDIA_BUS_FMT_SENSOR_DATA;
1234 fmt = find_format_by_code(code);
1236 f->pixelformat = fmt->fourcc;
1241 static int unicam_g_fmt_meta_cap(struct file *file, void *priv,
1242 struct v4l2_format *f)
1244 struct unicam_node *node = video_drvdata(file);
1246 if (node->pad_id != METADATA_PAD)
1254 static int unicam_queue_setup(struct vb2_queue *vq,
1255 unsigned int *nbuffers,
1256 unsigned int *nplanes,
1257 unsigned int sizes[],
1258 struct device *alloc_devs[])
1260 struct unicam_node *node = vb2_get_drv_priv(vq);
1261 struct unicam_device *dev = node->dev;
1262 unsigned int size = node->pad_id == IMAGE_PAD ?
1263 node->v_fmt.fmt.pix.sizeimage :
1264 node->v_fmt.fmt.meta.buffersize;
1266 if (vq->num_buffers + *nbuffers < 3)
1267 *nbuffers = 3 - vq->num_buffers;
1270 if (sizes[0] < size) {
1271 unicam_err(dev, "sizes[0] %i < size %u\n", sizes[0],
1284 static int unicam_buffer_prepare(struct vb2_buffer *vb)
1286 struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
1287 struct unicam_device *dev = node->dev;
1288 struct unicam_buffer *buf = to_unicam_buffer(vb);
1291 if (WARN_ON(!node->fmt))
1294 size = node->pad_id == IMAGE_PAD ? node->v_fmt.fmt.pix.sizeimage :
1295 node->v_fmt.fmt.meta.buffersize;
1296 if (vb2_plane_size(vb, 0) < size) {
1297 unicam_err(dev, "data will not fit into plane (%lu < %lu)\n",
1298 vb2_plane_size(vb, 0), size);
1302 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
1306 static void unicam_buffer_queue(struct vb2_buffer *vb)
1308 struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
1309 struct unicam_buffer *buf = to_unicam_buffer(vb);
1310 unsigned long flags;
1312 spin_lock_irqsave(&node->dma_queue_lock, flags);
1313 list_add_tail(&buf->list, &node->dma_queue);
1314 spin_unlock_irqrestore(&node->dma_queue_lock, flags);
1317 static void unicam_set_packing_config(struct unicam_device *dev)
1322 if (dev->node[IMAGE_PAD].v_fmt.fmt.pix.pixelformat ==
1323 dev->node[IMAGE_PAD].fmt->fourcc) {
1324 unpack = UNICAM_PUM_NONE;
1325 pack = UNICAM_PPM_NONE;
1327 switch (dev->node[IMAGE_PAD].fmt->depth) {
1329 unpack = UNICAM_PUM_UNPACK8;
1332 unpack = UNICAM_PUM_UNPACK10;
1335 unpack = UNICAM_PUM_UNPACK12;
1338 unpack = UNICAM_PUM_UNPACK14;
1341 unpack = UNICAM_PUM_UNPACK16;
1344 unpack = UNICAM_PUM_NONE;
1348 /* Repacking is always to 16bpp */
1349 pack = UNICAM_PPM_PACK16;
1353 set_field(&val, unpack, UNICAM_PUM_MASK);
1354 set_field(&val, pack, UNICAM_PPM_MASK);
1355 reg_write(dev, UNICAM_IPIPE, val);
1358 static void unicam_cfg_image_id(struct unicam_device *dev)
1360 if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1361 /* CSI2 mode, hardcode VC 0 for now. */
1362 reg_write(dev, UNICAM_IDI0,
1363 (0 << 6) | dev->node[IMAGE_PAD].fmt->csi_dt);
1366 reg_write(dev, UNICAM_IDI0,
1367 0x80 | dev->node[IMAGE_PAD].fmt->csi_dt);
1371 static void unicam_enable_ed(struct unicam_device *dev)
1373 u32 val = reg_read(dev, UNICAM_DCS);
1375 set_field(&val, 2, UNICAM_EDL_MASK);
1376 /* Do not wrap at the end of the embedded data buffer */
1377 set_field(&val, 0, UNICAM_DBOB);
1379 reg_write(dev, UNICAM_DCS, val);
1382 static void unicam_start_rx(struct unicam_device *dev, dma_addr_t *addr)
1384 int line_int_freq = dev->node[IMAGE_PAD].v_fmt.fmt.pix.height >> 2;
1385 unsigned int size, i;
1388 if (line_int_freq < 128)
1389 line_int_freq = 128;
1391 /* Enable lane clocks */
1393 for (i = 0; i < dev->active_data_lanes; i++)
1395 clk_write(dev, val);
1398 reg_write(dev, UNICAM_CTRL, UNICAM_MEM);
1400 /* Enable analogue control, and leave in reset. */
1402 set_field(&val, 7, UNICAM_CTATADJ_MASK);
1403 set_field(&val, 7, UNICAM_PTATADJ_MASK);
1404 reg_write(dev, UNICAM_ANA, val);
1405 usleep_range(1000, 2000);
1407 /* Come out of reset */
1408 reg_write_field(dev, UNICAM_ANA, 0, UNICAM_AR);
1410 /* Peripheral reset */
1411 reg_write_field(dev, UNICAM_CTRL, 1, UNICAM_CPR);
1412 reg_write_field(dev, UNICAM_CTRL, 0, UNICAM_CPR);
1414 reg_write_field(dev, UNICAM_CTRL, 0, UNICAM_CPE);
1416 /* Enable Rx control. */
1417 val = reg_read(dev, UNICAM_CTRL);
1418 if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1419 set_field(&val, UNICAM_CPM_CSI2, UNICAM_CPM_MASK);
1420 set_field(&val, UNICAM_DCM_STROBE, UNICAM_DCM_MASK);
1422 set_field(&val, UNICAM_CPM_CCP2, UNICAM_CPM_MASK);
1423 set_field(&val, dev->bus_flags, UNICAM_DCM_MASK);
1425 /* Packet framer timeout */
1426 set_field(&val, 0xf, UNICAM_PFT_MASK);
1427 set_field(&val, 128, UNICAM_OET_MASK);
1428 reg_write(dev, UNICAM_CTRL, val);
1430 reg_write(dev, UNICAM_IHWIN, 0);
1431 reg_write(dev, UNICAM_IVWIN, 0);
1433 /* AXI bus access QoS setup */
1434 val = reg_read(dev, UNICAM_PRI);
1435 set_field(&val, 0, UNICAM_BL_MASK);
1436 set_field(&val, 0, UNICAM_BS_MASK);
1437 set_field(&val, 0xe, UNICAM_PP_MASK);
1438 set_field(&val, 8, UNICAM_NP_MASK);
1439 set_field(&val, 2, UNICAM_PT_MASK);
1440 set_field(&val, 1, UNICAM_PE);
1441 reg_write(dev, UNICAM_PRI, val);
1443 reg_write_field(dev, UNICAM_ANA, 0, UNICAM_DDL);
1445 /* Always start in trigger frame capture mode (UNICAM_FCM set) */
1446 val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_FCM | UNICAM_IBOB;
1447 set_field(&val, line_int_freq, UNICAM_LCIE_MASK);
1448 reg_write(dev, UNICAM_ICTL, val);
1449 reg_write(dev, UNICAM_STA, UNICAM_STA_MASK_ALL);
1450 reg_write(dev, UNICAM_ISTA, UNICAM_ISTA_MASK_ALL);
1453 reg_write_field(dev, UNICAM_CLT, 2, UNICAM_CLT1_MASK);
1455 reg_write_field(dev, UNICAM_CLT, 6, UNICAM_CLT2_MASK);
1457 reg_write_field(dev, UNICAM_DLT, 2, UNICAM_DLT1_MASK);
1459 reg_write_field(dev, UNICAM_DLT, 6, UNICAM_DLT2_MASK);
1461 reg_write_field(dev, UNICAM_DLT, 0, UNICAM_DLT3_MASK);
1463 reg_write_field(dev, UNICAM_CTRL, 0, UNICAM_SOE);
1465 /* Packet compare setup - required to avoid missing frame ends */
1467 set_field(&val, 1, UNICAM_PCE);
1468 set_field(&val, 1, UNICAM_GI);
1469 set_field(&val, 1, UNICAM_CPH);
1470 set_field(&val, 0, UNICAM_PCVC_MASK);
1471 set_field(&val, 1, UNICAM_PCDT_MASK);
1472 reg_write(dev, UNICAM_CMP0, val);
1474 /* Enable clock lane and set up terminations */
1476 if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1478 set_field(&val, 1, UNICAM_CLE);
1479 set_field(&val, 1, UNICAM_CLLPE);
1480 if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1481 set_field(&val, 1, UNICAM_CLTRE);
1482 set_field(&val, 1, UNICAM_CLHSE);
1486 set_field(&val, 1, UNICAM_CLE);
1487 set_field(&val, 1, UNICAM_CLHSE);
1488 set_field(&val, 1, UNICAM_CLTRE);
1490 reg_write(dev, UNICAM_CLK, val);
1493 * Enable required data lanes with appropriate terminations.
1494 * The same value needs to be written to UNICAM_DATn registers for
1495 * the active lanes, and 0 for inactive ones.
1498 if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1500 set_field(&val, 1, UNICAM_DLE);
1501 set_field(&val, 1, UNICAM_DLLPE);
1502 if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1503 set_field(&val, 1, UNICAM_DLTRE);
1504 set_field(&val, 1, UNICAM_DLHSE);
1508 set_field(&val, 1, UNICAM_DLE);
1509 set_field(&val, 1, UNICAM_DLHSE);
1510 set_field(&val, 1, UNICAM_DLTRE);
1512 reg_write(dev, UNICAM_DAT0, val);
1514 if (dev->active_data_lanes == 1)
1516 reg_write(dev, UNICAM_DAT1, val);
1518 if (dev->max_data_lanes > 2) {
1520 * Registers UNICAM_DAT2 and UNICAM_DAT3 only valid if the
1521 * instance supports more than 2 data lanes.
1523 if (dev->active_data_lanes == 2)
1525 reg_write(dev, UNICAM_DAT2, val);
1527 if (dev->active_data_lanes == 3)
1529 reg_write(dev, UNICAM_DAT3, val);
1532 reg_write(dev, UNICAM_IBLS,
1533 dev->node[IMAGE_PAD].v_fmt.fmt.pix.bytesperline);
1534 size = dev->node[IMAGE_PAD].v_fmt.fmt.pix.sizeimage;
1535 unicam_wr_dma_addr(dev, addr[IMAGE_PAD], size, IMAGE_PAD);
1536 unicam_set_packing_config(dev);
1537 unicam_cfg_image_id(dev);
1539 val = reg_read(dev, UNICAM_MISC);
1540 set_field(&val, 1, UNICAM_FL0);
1541 set_field(&val, 1, UNICAM_FL1);
1542 reg_write(dev, UNICAM_MISC, val);
1544 if (dev->node[METADATA_PAD].streaming && dev->sensor_embedded_data) {
1545 size = dev->node[METADATA_PAD].v_fmt.fmt.meta.buffersize;
1546 unicam_enable_ed(dev);
1547 unicam_wr_dma_addr(dev, addr[METADATA_PAD], size, METADATA_PAD);
1550 /* Enable peripheral */
1551 reg_write_field(dev, UNICAM_CTRL, 1, UNICAM_CPE);
1553 /* Load image pointers */
1554 reg_write_field(dev, UNICAM_ICTL, 1, UNICAM_LIP_MASK);
1556 /* Load embedded data buffer pointers if needed */
1557 if (dev->node[METADATA_PAD].streaming && dev->sensor_embedded_data)
1558 reg_write_field(dev, UNICAM_DCS, 1, UNICAM_LDP);
1561 * Enable trigger only for the first frame to
1562 * sync correctly to the FS from the source.
1564 reg_write_field(dev, UNICAM_ICTL, 1, UNICAM_TFC);
1567 static void unicam_disable(struct unicam_device *dev)
1569 /* Analogue lane control disable */
1570 reg_write_field(dev, UNICAM_ANA, 1, UNICAM_DDL);
1572 /* Stop the output engine */
1573 reg_write_field(dev, UNICAM_CTRL, 1, UNICAM_SOE);
1575 /* Disable the data lanes. */
1576 reg_write(dev, UNICAM_DAT0, 0);
1577 reg_write(dev, UNICAM_DAT1, 0);
1579 if (dev->max_data_lanes > 2) {
1580 reg_write(dev, UNICAM_DAT2, 0);
1581 reg_write(dev, UNICAM_DAT3, 0);
1584 /* Peripheral reset */
1585 reg_write_field(dev, UNICAM_CTRL, 1, UNICAM_CPR);
1586 usleep_range(50, 100);
1587 reg_write_field(dev, UNICAM_CTRL, 0, UNICAM_CPR);
1589 /* Disable peripheral */
1590 reg_write_field(dev, UNICAM_CTRL, 0, UNICAM_CPE);
1592 /* Clear ED setup */
1593 reg_write(dev, UNICAM_DCS, 0);
1595 /* Disable all lane clocks */
1599 static void unicam_return_buffers(struct unicam_node *node)
1601 struct unicam_buffer *buf, *tmp;
1602 unsigned long flags;
1604 spin_lock_irqsave(&node->dma_queue_lock, flags);
1605 list_for_each_entry_safe(buf, tmp, &node->dma_queue, list) {
1606 list_del(&buf->list);
1607 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1611 vb2_buffer_done(&node->cur_frm->vb.vb2_buf,
1612 VB2_BUF_STATE_ERROR);
1613 if (node->next_frm && node->cur_frm != node->next_frm)
1614 vb2_buffer_done(&node->next_frm->vb.vb2_buf,
1615 VB2_BUF_STATE_ERROR);
1617 node->cur_frm = NULL;
1618 node->next_frm = NULL;
1619 spin_unlock_irqrestore(&node->dma_queue_lock, flags);
1622 static int unicam_start_streaming(struct vb2_queue *vq, unsigned int count)
1624 struct unicam_node *node = vb2_get_drv_priv(vq);
1625 struct unicam_device *dev = node->dev;
1626 dma_addr_t buffer_addr[MAX_NODES] = { 0 };
1627 unsigned long flags;
1631 node->streaming = true;
1632 if (!unicam_all_nodes_streaming(dev)) {
1633 unicam_dbg(3, dev, "Not all nodes are streaming yet.");
1638 ret = unicam_runtime_get(dev);
1640 unicam_dbg(3, dev, "unicam_runtime_get failed\n");
1645 * TODO: Retrieve the number of active data lanes from the connected
1648 dev->active_data_lanes = dev->max_data_lanes;
1650 ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
1652 unicam_err(dev, "failed to set up clock\n");
1656 ret = clk_prepare_enable(dev->clock);
1658 unicam_err(dev, "Failed to enable CSI clock: %d\n", ret);
1662 for (i = 0; i < ARRAY_SIZE(dev->node); i++) {
1663 struct unicam_buffer *buf;
1665 if (!dev->node[i].streaming)
1668 spin_lock_irqsave(&dev->node[i].dma_queue_lock, flags);
1669 buf = list_first_entry(&dev->node[i].dma_queue,
1670 struct unicam_buffer, list);
1671 dev->node[i].cur_frm = buf;
1672 dev->node[i].next_frm = buf;
1673 list_del(&buf->list);
1674 spin_unlock_irqrestore(&dev->node[i].dma_queue_lock, flags);
1677 vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
1680 unicam_start_rx(dev, buffer_addr);
1682 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1684 unicam_err(dev, "stream on failed in subdev\n");
1685 goto err_disable_unicam;
1691 unicam_disable(dev);
1692 clk_disable_unprepare(dev->clock);
1694 unicam_runtime_put(dev);
1696 unicam_return_buffers(node);
1697 node->streaming = false;
1702 static void unicam_stop_streaming(struct vb2_queue *vq)
1704 struct unicam_node *node = vb2_get_drv_priv(vq);
1705 struct unicam_device *dev = node->dev;
1707 node->streaming = false;
1709 if (node->pad_id == IMAGE_PAD) {
1711 * Stop streaming the sensor and disable the peripheral.
1712 * We cannot continue streaming embedded data with the
1713 * image pad disabled.
1715 if (v4l2_subdev_call(dev->sensor, video, s_stream, 0) < 0)
1716 unicam_err(dev, "stream off failed in subdev\n");
1718 unicam_disable(dev);
1719 clk_disable_unprepare(dev->clock);
1720 unicam_runtime_put(dev);
1722 } else if (node->pad_id == METADATA_PAD) {
1724 * Allow the hardware to spin in the dummy buffer.
1725 * This is only really needed if the embedded data pad is
1726 * disabled before the image pad.
1728 unicam_wr_dma_addr(dev, node->dummy_buf_dma_addr,
1729 DUMMY_BUF_SIZE, METADATA_PAD);
1732 /* Clear all queued buffers for the node */
1733 unicam_return_buffers(node);
1736 static int unicam_enum_input(struct file *file, void *priv,
1737 struct v4l2_input *inp)
1739 struct unicam_node *node = video_drvdata(file);
1740 struct unicam_device *dev = node->dev;
1742 if (inp->index != 0)
1745 inp->type = V4L2_INPUT_TYPE_CAMERA;
1746 if (v4l2_subdev_has_op(dev->sensor, video, s_dv_timings)) {
1747 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1749 } else if (v4l2_subdev_has_op(dev->sensor, video, s_std)) {
1750 inp->capabilities = V4L2_IN_CAP_STD;
1751 if (v4l2_subdev_call(dev->sensor, video, g_tvnorms, &inp->std)
1753 inp->std = V4L2_STD_ALL;
1755 inp->capabilities = 0;
1758 sprintf(inp->name, "Camera 0");
1762 static int unicam_g_input(struct file *file, void *priv, unsigned int *i)
1769 static int unicam_s_input(struct file *file, void *priv, unsigned int i)
1772 * FIXME: Ideally we would like to be able to query the source
1773 * subdevice for information over the input connectors it supports,
1774 * and map that through in to a call to video_ops->s_routing.
1775 * There is no infrastructure support for defining that within
1776 * devicetree at present. Until that is implemented we can't
1777 * map a user physical connector number to s_routing input number.
1785 static int unicam_querystd(struct file *file, void *priv,
1788 struct unicam_node *node = video_drvdata(file);
1789 struct unicam_device *dev = node->dev;
1791 return v4l2_subdev_call(dev->sensor, video, querystd, std);
1794 static int unicam_g_std(struct file *file, void *priv, v4l2_std_id *std)
1796 struct unicam_node *node = video_drvdata(file);
1797 struct unicam_device *dev = node->dev;
1799 return v4l2_subdev_call(dev->sensor, video, g_std, std);
1802 static int unicam_s_std(struct file *file, void *priv, v4l2_std_id std)
1804 struct unicam_node *node = video_drvdata(file);
1805 struct unicam_device *dev = node->dev;
1807 v4l2_std_id current_std;
1809 ret = v4l2_subdev_call(dev->sensor, video, g_std, ¤t_std);
1813 if (std == current_std)
1816 if (vb2_is_busy(&node->buffer_queue))
1819 ret = v4l2_subdev_call(dev->sensor, video, s_std, std);
1821 /* Force recomputation of bytesperline */
1822 node->v_fmt.fmt.pix.bytesperline = 0;
1824 unicam_reset_format(node);
1829 static int unicam_s_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1831 struct unicam_node *node = video_drvdata(file);
1832 struct unicam_device *dev = node->dev;
1834 return v4l2_subdev_call(dev->sensor, pad, set_edid, edid);
1837 static int unicam_g_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1839 struct unicam_node *node = video_drvdata(file);
1840 struct unicam_device *dev = node->dev;
1842 return v4l2_subdev_call(dev->sensor, pad, get_edid, edid);
1845 static int unicam_s_selection(struct file *file, void *priv,
1846 struct v4l2_selection *sel)
1848 struct unicam_node *node = video_drvdata(file);
1849 struct unicam_device *dev = node->dev;
1850 struct v4l2_subdev_selection sdsel = {
1851 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1852 .target = sel->target,
1853 .flags = sel->flags,
1857 return v4l2_subdev_call(dev->sensor, pad, set_selection, NULL, &sdsel);
1860 static int unicam_g_selection(struct file *file, void *priv,
1861 struct v4l2_selection *sel)
1863 struct unicam_node *node = video_drvdata(file);
1864 struct unicam_device *dev = node->dev;
1865 struct v4l2_subdev_selection sdsel = {
1866 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1867 .target = sel->target,
1871 ret = v4l2_subdev_call(dev->sensor, pad, get_selection, NULL, &sdsel);
1878 static int unicam_enum_framesizes(struct file *file, void *priv,
1879 struct v4l2_frmsizeenum *fsize)
1881 struct unicam_node *node = video_drvdata(file);
1882 struct unicam_device *dev = node->dev;
1883 const struct unicam_fmt *fmt;
1884 struct v4l2_subdev_frame_size_enum fse;
1887 /* check for valid format */
1888 fmt = find_format_by_pix(dev, fsize->pixel_format);
1890 unicam_dbg(3, dev, "Invalid pixel code: %x\n",
1891 fsize->pixel_format);
1894 fse.code = fmt->code;
1896 fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1897 fse.index = fsize->index;
1898 fse.pad = node->pad_id;
1900 ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_size, NULL, &fse);
1904 unicam_dbg(1, dev, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
1905 __func__, fse.index, fse.code, fse.min_width, fse.max_width,
1906 fse.min_height, fse.max_height);
1908 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1909 fsize->discrete.width = fse.max_width;
1910 fsize->discrete.height = fse.max_height;
1915 static int unicam_enum_frameintervals(struct file *file, void *priv,
1916 struct v4l2_frmivalenum *fival)
1918 struct unicam_node *node = video_drvdata(file);
1919 struct unicam_device *dev = node->dev;
1920 const struct unicam_fmt *fmt;
1921 struct v4l2_subdev_frame_interval_enum fie = {
1922 .index = fival->index,
1923 .width = fival->width,
1924 .height = fival->height,
1925 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1929 fmt = find_format_by_pix(dev, fival->pixel_format);
1933 fie.code = fmt->code;
1934 ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_interval,
1939 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1940 fival->discrete = fie.interval;
1945 static int unicam_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1947 struct unicam_node *node = video_drvdata(file);
1948 struct unicam_device *dev = node->dev;
1950 return v4l2_g_parm_cap(video_devdata(file), dev->sensor, a);
1953 static int unicam_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1955 struct unicam_node *node = video_drvdata(file);
1956 struct unicam_device *dev = node->dev;
1958 return v4l2_s_parm_cap(video_devdata(file), dev->sensor, a);
1961 static int unicam_g_dv_timings(struct file *file, void *priv,
1962 struct v4l2_dv_timings *timings)
1964 struct unicam_node *node = video_drvdata(file);
1965 struct unicam_device *dev = node->dev;
1967 return v4l2_subdev_call(dev->sensor, video, g_dv_timings, timings);
1970 static int unicam_s_dv_timings(struct file *file, void *priv,
1971 struct v4l2_dv_timings *timings)
1973 struct unicam_node *node = video_drvdata(file);
1974 struct unicam_device *dev = node->dev;
1975 struct v4l2_dv_timings current_timings;
1978 ret = v4l2_subdev_call(dev->sensor, video, g_dv_timings,
1981 if (v4l2_match_dv_timings(timings, ¤t_timings, 0, false))
1984 if (vb2_is_busy(&node->buffer_queue))
1987 ret = v4l2_subdev_call(dev->sensor, video, s_dv_timings, timings);
1989 /* Force recomputation of bytesperline */
1990 node->v_fmt.fmt.pix.bytesperline = 0;
1992 unicam_reset_format(node);
1997 static int unicam_query_dv_timings(struct file *file, void *priv,
1998 struct v4l2_dv_timings *timings)
2000 struct unicam_node *node = video_drvdata(file);
2001 struct unicam_device *dev = node->dev;
2003 return v4l2_subdev_call(dev->sensor, video, query_dv_timings, timings);
2006 static int unicam_enum_dv_timings(struct file *file, void *priv,
2007 struct v4l2_enum_dv_timings *timings)
2009 struct unicam_node *node = video_drvdata(file);
2010 struct unicam_device *dev = node->dev;
2012 return v4l2_subdev_call(dev->sensor, pad, enum_dv_timings, timings);
2015 static int unicam_dv_timings_cap(struct file *file, void *priv,
2016 struct v4l2_dv_timings_cap *cap)
2018 struct unicam_node *node = video_drvdata(file);
2019 struct unicam_device *dev = node->dev;
2021 return v4l2_subdev_call(dev->sensor, pad, dv_timings_cap, cap);
2024 static int unicam_subscribe_event(struct v4l2_fh *fh,
2025 const struct v4l2_event_subscription *sub)
2027 switch (sub->type) {
2028 case V4L2_EVENT_FRAME_SYNC:
2029 return v4l2_event_subscribe(fh, sub, 2, NULL);
2030 case V4L2_EVENT_SOURCE_CHANGE:
2031 return v4l2_event_subscribe(fh, sub, 4, NULL);
2034 return v4l2_ctrl_subscribe_event(fh, sub);
2037 static int unicam_log_status(struct file *file, void *fh)
2039 struct unicam_node *node = video_drvdata(file);
2040 struct unicam_device *dev = node->dev;
2043 /* status for sub devices */
2044 v4l2_device_call_all(&dev->v4l2_dev, 0, core, log_status);
2046 unicam_info(dev, "-----Receiver status-----\n");
2047 unicam_info(dev, "V4L2 width/height: %ux%u\n",
2048 node->v_fmt.fmt.pix.width, node->v_fmt.fmt.pix.height);
2049 unicam_info(dev, "Mediabus format: %08x\n", node->fmt->code);
2050 unicam_info(dev, "V4L2 format: %08x\n",
2051 node->v_fmt.fmt.pix.pixelformat);
2052 reg = reg_read(dev, UNICAM_IPIPE);
2053 unicam_info(dev, "Unpacking/packing: %u / %u\n",
2054 get_field(reg, UNICAM_PUM_MASK),
2055 get_field(reg, UNICAM_PPM_MASK));
2056 unicam_info(dev, "----Live data----\n");
2057 unicam_info(dev, "Programmed stride: %4u\n",
2058 reg_read(dev, UNICAM_IBLS));
2059 unicam_info(dev, "Detected resolution: %ux%u\n",
2060 reg_read(dev, UNICAM_IHSTA),
2061 reg_read(dev, UNICAM_IVSTA));
2062 unicam_info(dev, "Write pointer: %08x\n",
2063 reg_read(dev, UNICAM_IBWP));
2068 static void unicam_notify(struct v4l2_subdev *sd,
2069 unsigned int notification, void *arg)
2071 struct unicam_device *dev = to_unicam_device(sd->v4l2_dev);
2073 switch (notification) {
2074 case V4L2_DEVICE_NOTIFY_EVENT:
2075 v4l2_event_queue(&dev->node[IMAGE_PAD].video_dev, arg);
2082 static const struct vb2_ops unicam_video_qops = {
2083 .wait_prepare = vb2_ops_wait_prepare,
2084 .wait_finish = vb2_ops_wait_finish,
2085 .queue_setup = unicam_queue_setup,
2086 .buf_prepare = unicam_buffer_prepare,
2087 .buf_queue = unicam_buffer_queue,
2088 .start_streaming = unicam_start_streaming,
2089 .stop_streaming = unicam_stop_streaming,
2093 * unicam_v4l2_open : This function is based on the v4l2_fh_open helper
2094 * function. It has been augmented to handle sensor subdevice power management,
2096 static int unicam_v4l2_open(struct file *file)
2098 struct unicam_node *node = video_drvdata(file);
2099 struct unicam_device *dev = node->dev;
2102 mutex_lock(&node->lock);
2104 ret = v4l2_fh_open(file);
2106 unicam_err(dev, "v4l2_fh_open failed\n");
2112 if (!v4l2_fh_is_singular_file(file))
2115 ret = v4l2_subdev_call(dev->sensor, core, s_power, 1);
2116 if (ret < 0 && ret != -ENOIOCTLCMD) {
2117 v4l2_fh_release(file);
2125 mutex_unlock(&node->lock);
2129 static int unicam_v4l2_release(struct file *file)
2131 struct unicam_node *node = video_drvdata(file);
2132 struct unicam_device *dev = node->dev;
2133 struct v4l2_subdev *sd = dev->sensor;
2137 mutex_lock(&node->lock);
2139 fh_singular = v4l2_fh_is_singular_file(file);
2141 ret = _vb2_fop_release(file, NULL);
2144 v4l2_subdev_call(sd, core, s_power, 0);
2147 mutex_unlock(&node->lock);
2152 /* unicam capture driver file operations */
2153 static const struct v4l2_file_operations unicam_fops = {
2154 .owner = THIS_MODULE,
2155 .open = unicam_v4l2_open,
2156 .release = unicam_v4l2_release,
2157 .read = vb2_fop_read,
2158 .poll = vb2_fop_poll,
2159 .unlocked_ioctl = video_ioctl2,
2160 .mmap = vb2_fop_mmap,
2163 /* unicam capture ioctl operations */
2164 static const struct v4l2_ioctl_ops unicam_ioctl_ops = {
2165 .vidioc_querycap = unicam_querycap,
2166 .vidioc_enum_fmt_vid_cap = unicam_enum_fmt_vid_cap,
2167 .vidioc_g_fmt_vid_cap = unicam_g_fmt_vid_cap,
2168 .vidioc_s_fmt_vid_cap = unicam_s_fmt_vid_cap,
2169 .vidioc_try_fmt_vid_cap = unicam_try_fmt_vid_cap,
2171 .vidioc_enum_fmt_meta_cap = unicam_enum_fmt_meta_cap,
2172 .vidioc_g_fmt_meta_cap = unicam_g_fmt_meta_cap,
2173 .vidioc_s_fmt_meta_cap = unicam_g_fmt_meta_cap,
2174 .vidioc_try_fmt_meta_cap = unicam_g_fmt_meta_cap,
2176 .vidioc_enum_input = unicam_enum_input,
2177 .vidioc_g_input = unicam_g_input,
2178 .vidioc_s_input = unicam_s_input,
2180 .vidioc_querystd = unicam_querystd,
2181 .vidioc_s_std = unicam_s_std,
2182 .vidioc_g_std = unicam_g_std,
2184 .vidioc_g_edid = unicam_g_edid,
2185 .vidioc_s_edid = unicam_s_edid,
2187 .vidioc_enum_framesizes = unicam_enum_framesizes,
2188 .vidioc_enum_frameintervals = unicam_enum_frameintervals,
2190 .vidioc_g_selection = unicam_g_selection,
2191 .vidioc_s_selection = unicam_s_selection,
2193 .vidioc_g_parm = unicam_g_parm,
2194 .vidioc_s_parm = unicam_s_parm,
2196 .vidioc_s_dv_timings = unicam_s_dv_timings,
2197 .vidioc_g_dv_timings = unicam_g_dv_timings,
2198 .vidioc_query_dv_timings = unicam_query_dv_timings,
2199 .vidioc_enum_dv_timings = unicam_enum_dv_timings,
2200 .vidioc_dv_timings_cap = unicam_dv_timings_cap,
2202 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2203 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2204 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2205 .vidioc_querybuf = vb2_ioctl_querybuf,
2206 .vidioc_qbuf = vb2_ioctl_qbuf,
2207 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2208 .vidioc_expbuf = vb2_ioctl_expbuf,
2209 .vidioc_streamon = vb2_ioctl_streamon,
2210 .vidioc_streamoff = vb2_ioctl_streamoff,
2212 .vidioc_log_status = unicam_log_status,
2213 .vidioc_subscribe_event = unicam_subscribe_event,
2214 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2218 unicam_async_bound(struct v4l2_async_notifier *notifier,
2219 struct v4l2_subdev *subdev,
2220 struct v4l2_async_subdev *asd)
2222 struct unicam_device *unicam = to_unicam_device(notifier->v4l2_dev);
2224 if (unicam->sensor) {
2225 unicam_info(unicam, "Rejecting subdev %s (Already set!!)",
2230 unicam->sensor = subdev;
2231 unicam_dbg(1, unicam, "Using sensor %s for capture\n", subdev->name);
2236 static void unicam_release(struct kref *kref)
2238 struct unicam_device *unicam =
2239 container_of(kref, struct unicam_device, kref);
2241 v4l2_ctrl_handler_free(&unicam->ctrl_handler);
2242 media_device_cleanup(&unicam->mdev);
2244 if (unicam->sensor_config)
2245 v4l2_subdev_free_pad_config(unicam->sensor_config);
2250 static void unicam_put(struct unicam_device *unicam)
2252 kref_put(&unicam->kref, unicam_release);
2255 static void unicam_get(struct unicam_device *unicam)
2257 kref_get(&unicam->kref);
2260 static void unicam_node_release(struct video_device *vdev)
2262 struct unicam_node *node = video_get_drvdata(vdev);
2264 unicam_put(node->dev);
2267 static int register_node(struct unicam_device *unicam, struct unicam_node *node,
2268 enum v4l2_buf_type type, int pad_id)
2270 struct video_device *vdev;
2271 struct vb2_queue *q;
2272 struct v4l2_mbus_framefmt mbus_fmt = {0};
2273 const struct unicam_fmt *fmt;
2276 if (pad_id == IMAGE_PAD) {
2277 ret = __subdev_get_format(unicam, &mbus_fmt, pad_id);
2279 unicam_err(unicam, "Failed to get_format - ret %d\n",
2284 fmt = find_format_by_code(mbus_fmt.code);
2287 * Find the first format that the sensor and unicam both
2290 fmt = get_first_supported_format(unicam);
2293 /* No compatible formats */
2296 mbus_fmt.code = fmt->code;
2297 ret = __subdev_set_format(unicam, &mbus_fmt, pad_id);
2301 if (mbus_fmt.field != V4L2_FIELD_NONE) {
2302 /* Interlaced not supported - disable it now. */
2303 mbus_fmt.field = V4L2_FIELD_NONE;
2304 ret = __subdev_set_format(unicam, &mbus_fmt, pad_id);
2309 node->v_fmt.fmt.pix.pixelformat = fmt->fourcc ? fmt->fourcc
2310 : fmt->repacked_fourcc;
2312 /* Fix this node format as embedded data. */
2313 fmt = find_format_by_code(MEDIA_BUS_FMT_SENSOR_DATA);
2314 node->v_fmt.fmt.meta.dataformat = fmt->fourcc;
2318 node->pad_id = pad_id;
2321 /* Read current subdev format */
2322 unicam_reset_format(node);
2324 if (v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
2325 v4l2_std_id tvnorms;
2327 if (WARN_ON(!v4l2_subdev_has_op(unicam->sensor, video,
2330 * Subdevice should not advertise s_std but not
2335 ret = v4l2_subdev_call(unicam->sensor, video,
2336 g_tvnorms, &tvnorms);
2339 node->video_dev.tvnorms |= tvnorms;
2342 spin_lock_init(&node->dma_queue_lock);
2343 mutex_init(&node->lock);
2345 vdev = &node->video_dev;
2346 if (pad_id == IMAGE_PAD) {
2347 /* Add controls from the subdevice */
2348 ret = v4l2_ctrl_add_handler(&unicam->ctrl_handler,
2349 unicam->sensor->ctrl_handler, NULL,
2355 * If the sensor subdevice has any controls, associate the node
2356 * with the ctrl handler to allow access from userland.
2358 if (!list_empty(&unicam->ctrl_handler.ctrls))
2359 vdev->ctrl_handler = &unicam->ctrl_handler;
2362 q = &node->buffer_queue;
2364 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2366 q->ops = &unicam_video_qops;
2367 q->mem_ops = &vb2_dma_contig_memops;
2368 q->buf_struct_size = sizeof(struct unicam_buffer);
2369 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2370 q->lock = &node->lock;
2371 q->min_buffers_needed = 2;
2372 q->dev = &unicam->pdev->dev;
2374 ret = vb2_queue_init(q);
2376 unicam_err(unicam, "vb2_queue_init() failed\n");
2380 INIT_LIST_HEAD(&node->dma_queue);
2382 vdev->release = unicam_node_release;
2383 vdev->fops = &unicam_fops;
2384 vdev->ioctl_ops = &unicam_ioctl_ops;
2385 vdev->v4l2_dev = &unicam->v4l2_dev;
2386 vdev->vfl_dir = VFL_DIR_RX;
2388 vdev->lock = &node->lock;
2389 vdev->device_caps = (pad_id == IMAGE_PAD) ?
2390 (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING) :
2391 (V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING);
2393 /* Define the device names */
2394 snprintf(vdev->name, sizeof(vdev->name), "%s-%s", UNICAM_MODULE_NAME,
2395 pad_id == IMAGE_PAD ? "image" : "embedded");
2397 video_set_drvdata(vdev, node);
2398 if (pad_id == IMAGE_PAD)
2399 vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
2400 node->pad.flags = MEDIA_PAD_FL_SINK;
2401 media_entity_pads_init(&vdev->entity, 1, &node->pad);
2403 node->dummy_buf_cpu_addr = dma_alloc_coherent(&unicam->pdev->dev,
2405 &node->dummy_buf_dma_addr,
2407 if (!node->dummy_buf_cpu_addr) {
2408 unicam_err(unicam, "Unable to allocate dummy buffer.\n");
2412 if (pad_id == METADATA_PAD) {
2413 v4l2_disable_ioctl(vdev, VIDIOC_DQEVENT);
2414 v4l2_disable_ioctl(vdev, VIDIOC_SUBSCRIBE_EVENT);
2415 v4l2_disable_ioctl(vdev, VIDIOC_UNSUBSCRIBE_EVENT);
2417 if (pad_id == METADATA_PAD ||
2418 !v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
2419 v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_STD);
2420 v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_STD);
2421 v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUMSTD);
2423 if (pad_id == METADATA_PAD ||
2424 !v4l2_subdev_has_op(unicam->sensor, video, querystd))
2425 v4l2_disable_ioctl(&node->video_dev, VIDIOC_QUERYSTD);
2426 if (pad_id == METADATA_PAD ||
2427 !v4l2_subdev_has_op(unicam->sensor, video, s_dv_timings)) {
2428 v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_EDID);
2429 v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_EDID);
2430 v4l2_disable_ioctl(&node->video_dev, VIDIOC_DV_TIMINGS_CAP);
2431 v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_DV_TIMINGS);
2432 v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_DV_TIMINGS);
2433 v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUM_DV_TIMINGS);
2434 v4l2_disable_ioctl(&node->video_dev, VIDIOC_QUERY_DV_TIMINGS);
2436 if (pad_id == METADATA_PAD ||
2437 !v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_interval))
2438 v4l2_disable_ioctl(&node->video_dev,
2439 VIDIOC_ENUM_FRAMEINTERVALS);
2440 if (pad_id == METADATA_PAD ||
2441 !v4l2_subdev_has_op(unicam->sensor, video, g_frame_interval))
2442 v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_PARM);
2443 if (pad_id == METADATA_PAD ||
2444 !v4l2_subdev_has_op(unicam->sensor, video, s_frame_interval))
2445 v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_PARM);
2447 if (pad_id == METADATA_PAD ||
2448 !v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_size))
2449 v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUM_FRAMESIZES);
2451 if (node->pad_id == METADATA_PAD ||
2452 !v4l2_subdev_has_op(unicam->sensor, pad, set_selection))
2453 v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_SELECTION);
2455 if (node->pad_id == METADATA_PAD ||
2456 !v4l2_subdev_has_op(unicam->sensor, pad, get_selection))
2457 v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_SELECTION);
2459 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
2461 unicam_err(unicam, "Unable to register video device %s\n",
2467 * Acquire a reference to unicam, which will be released when the video
2468 * device will be unregistered and userspace will have closed all open
2472 node->registered = true;
2474 if (pad_id != METADATA_PAD || unicam->sensor_embedded_data) {
2475 ret = media_create_pad_link(&unicam->sensor->entity, pad_id,
2476 &node->video_dev.entity, 0,
2477 MEDIA_LNK_FL_ENABLED |
2478 MEDIA_LNK_FL_IMMUTABLE);
2480 unicam_err(unicam, "Unable to create pad link for %s\n",
2487 static void unregister_nodes(struct unicam_device *unicam)
2491 for (i = 0; i < ARRAY_SIZE(unicam->node); i++) {
2492 struct unicam_node *node = &unicam->node[i];
2494 if (node->dummy_buf_cpu_addr) {
2495 dma_free_coherent(&unicam->pdev->dev, DUMMY_BUF_SIZE,
2496 node->dummy_buf_cpu_addr,
2497 node->dummy_buf_dma_addr);
2500 if (node->registered) {
2501 node->registered = false;
2502 video_unregister_device(&node->video_dev);
2507 static int unicam_probe_complete(struct unicam_device *unicam)
2511 unicam->v4l2_dev.notify = unicam_notify;
2513 unicam->sensor_config = v4l2_subdev_alloc_pad_config(unicam->sensor);
2514 if (!unicam->sensor_config)
2517 unicam->sensor_embedded_data = (unicam->sensor->entity.num_pads >= 2);
2519 ret = register_node(unicam, &unicam->node[IMAGE_PAD],
2520 V4L2_BUF_TYPE_VIDEO_CAPTURE, IMAGE_PAD);
2522 unicam_err(unicam, "Unable to register image video device.\n");
2526 ret = register_node(unicam, &unicam->node[METADATA_PAD],
2527 V4L2_BUF_TYPE_META_CAPTURE, METADATA_PAD);
2529 unicam_err(unicam, "Unable to register metadata video device.\n");
2533 ret = v4l2_device_register_ro_subdev_nodes(&unicam->v4l2_dev);
2535 unicam_err(unicam, "Unable to register subdev nodes.\n");
2540 * Release the initial reference, all references are now owned by the
2547 unregister_nodes(unicam);
2553 static int unicam_async_complete(struct v4l2_async_notifier *notifier)
2555 struct unicam_device *unicam = to_unicam_device(notifier->v4l2_dev);
2557 return unicam_probe_complete(unicam);
2560 static const struct v4l2_async_notifier_operations unicam_async_ops = {
2561 .bound = unicam_async_bound,
2562 .complete = unicam_async_complete,
2565 static int of_unicam_connect_subdevs(struct unicam_device *dev)
2567 struct platform_device *pdev = dev->pdev;
2568 struct v4l2_fwnode_endpoint ep = { 0 };
2569 struct device_node *ep_node;
2570 struct device_node *sensor_node;
2574 if (of_property_read_u32(pdev->dev.of_node, "brcm,num-data-lanes",
2575 &dev->max_data_lanes) < 0) {
2576 unicam_err(dev, "number of data lanes not set\n");
2580 /* Get the local endpoint and remote device. */
2581 ep_node = of_graph_get_next_endpoint(pdev->dev.of_node, NULL);
2583 unicam_dbg(3, dev, "can't get next endpoint\n");
2587 unicam_dbg(3, dev, "ep_node is %pOF\n", ep_node);
2589 sensor_node = of_graph_get_remote_port_parent(ep_node);
2591 unicam_dbg(3, dev, "can't get remote parent\n");
2595 unicam_dbg(1, dev, "found subdevice %pOF\n", sensor_node);
2597 /* Parse the local endpoint and validate its configuration. */
2598 v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep);
2600 unicam_dbg(3, dev, "parsed local endpoint, bus_type %u\n",
2603 dev->bus_type = ep.bus_type;
2605 switch (ep.bus_type) {
2606 case V4L2_MBUS_CSI2_DPHY:
2607 switch (ep.bus.mipi_csi2.num_data_lanes) {
2614 unicam_err(dev, "subdevice %pOF: %u data lanes not supported\n",
2616 ep.bus.mipi_csi2.num_data_lanes);
2620 for (lane = 0; lane < ep.bus.mipi_csi2.num_data_lanes; lane++) {
2621 if (ep.bus.mipi_csi2.data_lanes[lane] != lane + 1) {
2622 unicam_err(dev, "subdevice %pOF: data lanes reordering not supported\n",
2628 if (ep.bus.mipi_csi2.num_data_lanes > dev->max_data_lanes) {
2629 unicam_err(dev, "subdevice requires %u data lanes when %u are supported\n",
2630 ep.bus.mipi_csi2.num_data_lanes,
2631 dev->max_data_lanes);
2634 dev->max_data_lanes = ep.bus.mipi_csi2.num_data_lanes;
2635 dev->bus_flags = ep.bus.mipi_csi2.flags;
2639 case V4L2_MBUS_CCP2:
2640 if (ep.bus.mipi_csi1.clock_lane != 0 ||
2641 ep.bus.mipi_csi1.data_lane != 1) {
2642 unicam_err(dev, "subdevice %pOF: unsupported lanes configuration\n",
2647 dev->max_data_lanes = 1;
2648 dev->bus_flags = ep.bus.mipi_csi1.strobe;
2652 /* Unsupported bus type */
2653 unicam_err(dev, "subdevice %pOF: unsupported bus type %u\n",
2654 sensor_node, ep.bus_type);
2658 unicam_dbg(3, dev, "subdevice %pOF: %s bus, %u data lanes, flags=0x%08x\n",
2660 dev->bus_type == V4L2_MBUS_CSI2_DPHY ? "CSI-2" : "CCP2",
2661 dev->max_data_lanes, dev->bus_flags);
2663 /* Initialize and register the async notifier. */
2664 v4l2_async_nf_init(&dev->notifier);
2665 dev->notifier.ops = &unicam_async_ops;
2667 dev->asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
2668 dev->asd.match.fwnode = of_fwnode_handle(sensor_node);
2669 ret = __v4l2_async_nf_add_subdev(&dev->notifier, &dev->asd);
2671 unicam_err(dev, "Error adding subdevice: %d\n", ret);
2675 ret = v4l2_async_nf_register(&dev->v4l2_dev, &dev->notifier);
2677 unicam_err(dev, "Error registering async notifier: %d\n", ret);
2682 of_node_put(sensor_node);
2683 of_node_put(ep_node);
2688 static int unicam_probe(struct platform_device *pdev)
2690 struct unicam_device *unicam;
2693 unicam = kzalloc(sizeof(*unicam), GFP_KERNEL);
2697 kref_init(&unicam->kref);
2698 unicam->pdev = pdev;
2700 unicam->base = devm_platform_ioremap_resource(pdev, 0);
2701 if (IS_ERR(unicam->base)) {
2702 unicam_err(unicam, "Failed to get main io block\n");
2703 ret = PTR_ERR(unicam->base);
2704 goto err_unicam_put;
2707 unicam->clk_gate_base = devm_platform_ioremap_resource(pdev, 1);
2708 if (IS_ERR(unicam->clk_gate_base)) {
2709 unicam_err(unicam, "Failed to get 2nd io block\n");
2710 ret = PTR_ERR(unicam->clk_gate_base);
2711 goto err_unicam_put;
2714 unicam->clock = devm_clk_get(&pdev->dev, "lp");
2715 if (IS_ERR(unicam->clock)) {
2716 unicam_err(unicam, "Failed to get clock\n");
2717 ret = PTR_ERR(unicam->clock);
2718 goto err_unicam_put;
2721 ret = platform_get_irq(pdev, 0);
2723 dev_err(&pdev->dev, "No IRQ resource\n");
2725 goto err_unicam_put;
2728 ret = devm_request_irq(&pdev->dev, ret, unicam_isr, 0,
2729 "unicam_capture0", unicam);
2731 dev_err(&pdev->dev, "Unable to request interrupt\n");
2733 goto err_unicam_put;
2736 unicam->mdev.dev = &pdev->dev;
2737 strscpy(unicam->mdev.model, UNICAM_MODULE_NAME,
2738 sizeof(unicam->mdev.model));
2739 strscpy(unicam->mdev.serial, "", sizeof(unicam->mdev.serial));
2740 snprintf(unicam->mdev.bus_info, sizeof(unicam->mdev.bus_info),
2741 "platform:%s", dev_name(&pdev->dev));
2742 unicam->mdev.hw_revision = 0;
2744 media_device_init(&unicam->mdev);
2746 unicam->v4l2_dev.mdev = &unicam->mdev;
2748 ret = v4l2_device_register(&pdev->dev, &unicam->v4l2_dev);
2751 "Unable to register v4l2 device.\n");
2752 goto err_unicam_put;
2755 ret = media_device_register(&unicam->mdev);
2758 "Unable to register media-controller device.\n");
2759 goto err_v4l2_unregister;
2762 /* Reserve space for the controls */
2763 ret = v4l2_ctrl_handler_init(&unicam->ctrl_handler, 16);
2765 goto err_media_unregister;
2767 /* set the driver data in platform device */
2768 platform_set_drvdata(pdev, unicam);
2770 ret = of_unicam_connect_subdevs(unicam);
2772 dev_err(&pdev->dev, "Failed to connect subdevs\n");
2773 goto err_media_unregister;
2776 /* Enable the block power domain */
2777 pm_runtime_enable(&pdev->dev);
2781 err_media_unregister:
2782 media_device_unregister(&unicam->mdev);
2783 err_v4l2_unregister:
2784 v4l2_device_unregister(&unicam->v4l2_dev);
2791 static int unicam_remove(struct platform_device *pdev)
2793 struct unicam_device *unicam = platform_get_drvdata(pdev);
2795 unicam_dbg(2, unicam, "%s\n", __func__);
2797 v4l2_async_nf_unregister(&unicam->notifier);
2798 v4l2_device_unregister(&unicam->v4l2_dev);
2799 media_device_unregister(&unicam->mdev);
2800 unregister_nodes(unicam);
2802 pm_runtime_disable(&pdev->dev);
2807 static const struct of_device_id unicam_of_match[] = {
2808 { .compatible = "brcm,bcm2835-unicam", },
2811 MODULE_DEVICE_TABLE(of, unicam_of_match);
2813 static struct platform_driver unicam_driver = {
2814 .probe = unicam_probe,
2815 .remove = unicam_remove,
2817 .name = UNICAM_MODULE_NAME,
2818 .of_match_table = of_match_ptr(unicam_of_match),
2822 module_platform_driver(unicam_driver);
2824 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
2825 MODULE_DESCRIPTION("BCM2835 Unicam driver");
2826 MODULE_LICENSE("GPL");
2827 MODULE_VERSION(UNICAM_VERSION);