1 // SPDX-License-Identifier: GPL-2.0
3 * V4L2 Capture CSI Subdev for Freescale i.MX6UL/L / i.MX7 SOC
5 * Copyright (c) 2019 Linaro Ltd
10 #include <linux/delay.h>
11 #include <linux/interrupt.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/of_graph.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/types.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-fwnode.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-mc.h>
27 #include <media/v4l2-subdev.h>
28 #include <media/videobuf2-dma-contig.h>
30 #define IMX7_CSI_PAD_SINK 0
31 #define IMX7_CSI_PAD_SRC 1
32 #define IMX7_CSI_PADS_NUM 2
34 /* csi control reg 1 */
35 #define BIT_SWAP16_EN BIT(31)
36 #define BIT_EXT_VSYNC BIT(30)
37 #define BIT_EOF_INT_EN BIT(29)
38 #define BIT_PRP_IF_EN BIT(28)
39 #define BIT_CCIR_MODE BIT(27)
40 #define BIT_COF_INT_EN BIT(26)
41 #define BIT_SF_OR_INTEN BIT(25)
42 #define BIT_RF_OR_INTEN BIT(24)
43 #define BIT_SFF_DMA_DONE_INTEN BIT(22)
44 #define BIT_STATFF_INTEN BIT(21)
45 #define BIT_FB2_DMA_DONE_INTEN BIT(20)
46 #define BIT_FB1_DMA_DONE_INTEN BIT(19)
47 #define BIT_RXFF_INTEN BIT(18)
48 #define BIT_SOF_POL BIT(17)
49 #define BIT_SOF_INTEN BIT(16)
50 #define BIT_MCLKDIV(n) ((n) << 12)
51 #define BIT_MCLKDIV_MASK (0xf << 12)
52 #define BIT_HSYNC_POL BIT(11)
53 #define BIT_CCIR_EN BIT(10)
54 #define BIT_MCLKEN BIT(9)
55 #define BIT_FCC BIT(8)
56 #define BIT_PACK_DIR BIT(7)
57 #define BIT_CLR_STATFIFO BIT(6)
58 #define BIT_CLR_RXFIFO BIT(5)
59 #define BIT_GCLK_MODE BIT(4)
60 #define BIT_INV_DATA BIT(3)
61 #define BIT_INV_PCLK BIT(2)
62 #define BIT_REDGE BIT(1)
63 #define BIT_PIXEL_BIT BIT(0)
66 #define BIT_DMA_BURST_TYPE_RFF_INCR4 (1 << 30)
67 #define BIT_DMA_BURST_TYPE_RFF_INCR8 (2 << 30)
68 #define BIT_DMA_BURST_TYPE_RFF_INCR16 (3 << 30)
69 #define BIT_DMA_BURST_TYPE_RFF_MASK (3 << 30)
72 #define BIT_FRMCNT(n) ((n) << 16)
73 #define BIT_FRMCNT_MASK (0xffff << 16)
74 #define BIT_FRMCNT_RST BIT(15)
75 #define BIT_DMA_REFLASH_RFF BIT(14)
76 #define BIT_DMA_REFLASH_SFF BIT(13)
77 #define BIT_DMA_REQ_EN_RFF BIT(12)
78 #define BIT_DMA_REQ_EN_SFF BIT(11)
79 #define BIT_STATFF_LEVEL(n) ((n) << 8)
80 #define BIT_STATFF_LEVEL_MASK (0x7 << 8)
81 #define BIT_HRESP_ERR_EN BIT(7)
82 #define BIT_RXFF_LEVEL(n) ((n) << 4)
83 #define BIT_RXFF_LEVEL_MASK (0x7 << 4)
84 #define BIT_TWO_8BIT_SENSOR BIT(3)
85 #define BIT_ZERO_PACK_EN BIT(2)
86 #define BIT_ECC_INT_EN BIT(1)
87 #define BIT_ECC_AUTO_EN BIT(0)
90 #define BIT_ADDR_CH_ERR_INT BIT(28)
91 #define BIT_FIELD0_INT BIT(27)
92 #define BIT_FIELD1_INT BIT(26)
93 #define BIT_SFF_OR_INT BIT(25)
94 #define BIT_RFF_OR_INT BIT(24)
95 #define BIT_DMA_TSF_DONE_SFF BIT(22)
96 #define BIT_STATFF_INT BIT(21)
97 #define BIT_DMA_TSF_DONE_FB2 BIT(20)
98 #define BIT_DMA_TSF_DONE_FB1 BIT(19)
99 #define BIT_RXFF_INT BIT(18)
100 #define BIT_EOF_INT BIT(17)
101 #define BIT_SOF_INT BIT(16)
102 #define BIT_F2_INT BIT(15)
103 #define BIT_F1_INT BIT(14)
104 #define BIT_COF_INT BIT(13)
105 #define BIT_HRESP_ERR_INT BIT(7)
106 #define BIT_ECC_INT BIT(1)
107 #define BIT_DRDY BIT(0)
109 /* csi image parameter reg */
110 #define BIT_IMAGE_WIDTH(n) ((n) << 16)
111 #define BIT_IMAGE_HEIGHT(n) (n)
113 /* csi control reg 18 */
114 #define BIT_CSI_HW_ENABLE BIT(31)
115 #define BIT_MIPI_DATA_FORMAT_RAW8 (0x2a << 25)
116 #define BIT_MIPI_DATA_FORMAT_RAW10 (0x2b << 25)
117 #define BIT_MIPI_DATA_FORMAT_RAW12 (0x2c << 25)
118 #define BIT_MIPI_DATA_FORMAT_RAW14 (0x2d << 25)
119 #define BIT_MIPI_DATA_FORMAT_YUV422_8B (0x1e << 25)
120 #define BIT_MIPI_DATA_FORMAT_MASK (0x3f << 25)
121 #define BIT_DATA_FROM_MIPI BIT(22)
122 #define BIT_MIPI_YU_SWAP BIT(21)
123 #define BIT_MIPI_DOUBLE_CMPNT BIT(20)
124 #define BIT_MASK_OPTION_FIRST_FRAME (0 << 18)
125 #define BIT_MASK_OPTION_CSI_EN (1 << 18)
126 #define BIT_MASK_OPTION_SECOND_FRAME (2 << 18)
127 #define BIT_MASK_OPTION_ON_DATA (3 << 18)
128 #define BIT_BASEADDR_CHG_ERR_EN BIT(9)
129 #define BIT_BASEADDR_SWITCH_SEL BIT(5)
130 #define BIT_BASEADDR_SWITCH_EN BIT(4)
131 #define BIT_PARALLEL24_EN BIT(3)
132 #define BIT_DEINTERLACE_EN BIT(2)
133 #define BIT_TVDECODER_IN_EN BIT(1)
134 #define BIT_NTSC_EN BIT(0)
136 #define CSI_MCLK_VF 1
137 #define CSI_MCLK_ENC 2
138 #define CSI_MCLK_RAW 4
139 #define CSI_MCLK_I2C 8
141 #define CSI_CSICR1 0x00
142 #define CSI_CSICR2 0x04
143 #define CSI_CSICR3 0x08
144 #define CSI_STATFIFO 0x0c
145 #define CSI_CSIRXFIFO 0x10
146 #define CSI_CSIRXCNT 0x14
147 #define CSI_CSISR 0x18
149 #define CSI_CSIDBG 0x1c
150 #define CSI_CSIDMASA_STATFIFO 0x20
151 #define CSI_CSIDMATS_STATFIFO 0x24
152 #define CSI_CSIDMASA_FB1 0x28
153 #define CSI_CSIDMASA_FB2 0x2c
154 #define CSI_CSIFBUF_PARA 0x30
155 #define CSI_CSIIMAG_PARA 0x34
157 #define CSI_CSICR18 0x48
158 #define CSI_CSICR19 0x4c
160 #define IMX7_CSI_VIDEO_NAME "imx-capture"
161 /* In bytes, per queue */
162 #define IMX7_CSI_VIDEO_MEM_LIMIT SZ_512M
163 #define IMX7_CSI_VIDEO_EOF_TIMEOUT 2000
165 #define IMX7_CSI_DEF_MBUS_CODE MEDIA_BUS_FMT_UYVY8_2X8
166 #define IMX7_CSI_DEF_PIX_FORMAT V4L2_PIX_FMT_UYVY
167 #define IMX7_CSI_DEF_PIX_WIDTH 640
168 #define IMX7_CSI_DEF_PIX_HEIGHT 480
175 struct imx7_csi_pixfmt {
176 /* the in-memory FourCC pixel format */
179 * the set of equivalent media bus codes for the fourcc.
180 * NOTE! codes pointer is NULL for in-memory-only formats.
183 int bpp; /* total bpp */
187 struct imx7_csi_vb2_buffer {
188 struct vb2_v4l2_buffer vbuf;
189 struct list_head list;
192 static inline struct imx7_csi_vb2_buffer *
193 to_imx7_csi_vb2_buffer(struct vb2_buffer *vb)
195 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
197 return container_of(vbuf, struct imx7_csi_vb2_buffer, vbuf);
200 struct imx7_csi_dma_buf {
209 /* Resources and locks */
210 void __iomem *regbase;
214 spinlock_t irqlock; /* Protects last_eof */
216 /* Media and V4L2 device */
217 struct media_device mdev;
218 struct v4l2_device v4l2_dev;
219 struct v4l2_async_notifier notifier;
220 struct media_pipeline pipe;
222 struct v4l2_subdev *src_sd;
226 struct v4l2_subdev sd;
227 struct media_pad pad[IMX7_CSI_PADS_NUM];
230 struct video_device *vdev; /* Video device */
231 struct media_pad vdev_pad; /* Video device pad */
233 struct v4l2_pix_format vdev_fmt; /* The user format */
234 const struct imx7_csi_pixfmt *vdev_cc;
235 struct v4l2_rect vdev_compose; /* The compose rectangle */
237 struct mutex vdev_mutex; /* Protect vdev operations */
239 struct vb2_queue q; /* The videobuf2 queue */
240 struct list_head ready_q; /* List of queued buffers */
241 spinlock_t q_lock; /* Protect ready_q */
243 /* Buffers and streaming state */
244 struct imx7_csi_vb2_buffer *active_vb2_buf[2];
245 struct imx7_csi_dma_buf underrun_buf;
252 struct completion last_eof_completion;
254 enum imx_csi_model model;
257 static struct imx7_csi *
258 imx7_csi_notifier_to_dev(struct v4l2_async_notifier *n)
260 return container_of(n, struct imx7_csi, notifier);
263 /* -----------------------------------------------------------------------------
264 * Hardware Configuration
267 static u32 imx7_csi_reg_read(struct imx7_csi *csi, unsigned int offset)
269 return readl(csi->regbase + offset);
272 static void imx7_csi_reg_write(struct imx7_csi *csi, unsigned int value,
275 writel(value, csi->regbase + offset);
278 static u32 imx7_csi_irq_clear(struct imx7_csi *csi)
282 isr = imx7_csi_reg_read(csi, CSI_CSISR);
283 imx7_csi_reg_write(csi, isr, CSI_CSISR);
288 static void imx7_csi_init_default(struct imx7_csi *csi)
290 imx7_csi_reg_write(csi, BIT_SOF_POL | BIT_REDGE | BIT_GCLK_MODE |
291 BIT_HSYNC_POL | BIT_FCC | BIT_MCLKDIV(1) |
292 BIT_MCLKEN, CSI_CSICR1);
293 imx7_csi_reg_write(csi, 0, CSI_CSICR2);
294 imx7_csi_reg_write(csi, BIT_FRMCNT_RST, CSI_CSICR3);
296 imx7_csi_reg_write(csi, BIT_IMAGE_WIDTH(IMX7_CSI_DEF_PIX_WIDTH) |
297 BIT_IMAGE_HEIGHT(IMX7_CSI_DEF_PIX_HEIGHT),
300 imx7_csi_reg_write(csi, BIT_DMA_REFLASH_RFF, CSI_CSICR3);
303 static void imx7_csi_hw_enable_irq(struct imx7_csi *csi)
305 u32 cr1 = imx7_csi_reg_read(csi, CSI_CSICR1);
307 cr1 |= BIT_RFF_OR_INT;
308 cr1 |= BIT_FB1_DMA_DONE_INTEN;
309 cr1 |= BIT_FB2_DMA_DONE_INTEN;
311 imx7_csi_reg_write(csi, cr1, CSI_CSICR1);
314 static void imx7_csi_hw_disable_irq(struct imx7_csi *csi)
316 u32 cr1 = imx7_csi_reg_read(csi, CSI_CSICR1);
318 cr1 &= ~BIT_RFF_OR_INT;
319 cr1 &= ~BIT_FB1_DMA_DONE_INTEN;
320 cr1 &= ~BIT_FB2_DMA_DONE_INTEN;
322 imx7_csi_reg_write(csi, cr1, CSI_CSICR1);
325 static void imx7_csi_hw_enable(struct imx7_csi *csi)
327 u32 cr = imx7_csi_reg_read(csi, CSI_CSICR18);
329 cr |= BIT_CSI_HW_ENABLE;
331 imx7_csi_reg_write(csi, cr, CSI_CSICR18);
334 static void imx7_csi_hw_disable(struct imx7_csi *csi)
336 u32 cr = imx7_csi_reg_read(csi, CSI_CSICR18);
338 cr &= ~BIT_CSI_HW_ENABLE;
340 imx7_csi_reg_write(csi, cr, CSI_CSICR18);
343 static void imx7_csi_dma_reflash(struct imx7_csi *csi)
347 cr3 = imx7_csi_reg_read(csi, CSI_CSICR3);
348 cr3 |= BIT_DMA_REFLASH_RFF;
349 imx7_csi_reg_write(csi, cr3, CSI_CSICR3);
352 static void imx7_csi_rx_fifo_clear(struct imx7_csi *csi)
354 u32 cr1 = imx7_csi_reg_read(csi, CSI_CSICR1) & ~BIT_FCC;
356 imx7_csi_reg_write(csi, cr1, CSI_CSICR1);
357 imx7_csi_reg_write(csi, cr1 | BIT_CLR_RXFIFO, CSI_CSICR1);
358 imx7_csi_reg_write(csi, cr1 | BIT_FCC, CSI_CSICR1);
361 static void imx7_csi_dmareq_rff_enable(struct imx7_csi *csi)
363 u32 cr3 = imx7_csi_reg_read(csi, CSI_CSICR3);
365 cr3 |= BIT_DMA_REQ_EN_RFF;
366 cr3 |= BIT_HRESP_ERR_EN;
367 cr3 &= ~BIT_RXFF_LEVEL_MASK;
368 cr3 |= BIT_RXFF_LEVEL(2);
370 imx7_csi_reg_write(csi, cr3, CSI_CSICR3);
373 static void imx7_csi_dmareq_rff_disable(struct imx7_csi *csi)
375 u32 cr3 = imx7_csi_reg_read(csi, CSI_CSICR3);
377 cr3 &= ~BIT_DMA_REQ_EN_RFF;
378 cr3 &= ~BIT_HRESP_ERR_EN;
379 imx7_csi_reg_write(csi, cr3, CSI_CSICR3);
382 static void imx7_csi_update_buf(struct imx7_csi *csi, dma_addr_t dma_addr,
386 imx7_csi_reg_write(csi, dma_addr, CSI_CSIDMASA_FB2);
388 imx7_csi_reg_write(csi, dma_addr, CSI_CSIDMASA_FB1);
391 static struct imx7_csi_vb2_buffer *imx7_csi_video_next_buf(struct imx7_csi *csi);
393 static void imx7_csi_setup_vb2_buf(struct imx7_csi *csi)
395 struct imx7_csi_vb2_buffer *buf;
396 struct vb2_buffer *vb2_buf;
399 for (i = 0; i < 2; i++) {
402 buf = imx7_csi_video_next_buf(csi);
404 csi->active_vb2_buf[i] = buf;
405 vb2_buf = &buf->vbuf.vb2_buf;
406 dma_addr = vb2_dma_contig_plane_dma_addr(vb2_buf, 0);
408 csi->active_vb2_buf[i] = NULL;
409 dma_addr = csi->underrun_buf.dma_addr;
412 imx7_csi_update_buf(csi, dma_addr, i);
416 static void imx7_csi_dma_unsetup_vb2_buf(struct imx7_csi *csi,
417 enum vb2_buffer_state return_status)
419 struct imx7_csi_vb2_buffer *buf;
422 /* return any remaining active frames with return_status */
423 for (i = 0; i < 2; i++) {
424 buf = csi->active_vb2_buf[i];
426 struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
428 vb->timestamp = ktime_get_ns();
429 vb2_buffer_done(vb, return_status);
430 csi->active_vb2_buf[i] = NULL;
435 static void imx7_csi_free_dma_buf(struct imx7_csi *csi,
436 struct imx7_csi_dma_buf *buf)
439 dma_free_coherent(csi->dev, buf->len, buf->virt, buf->dma_addr);
445 static int imx7_csi_alloc_dma_buf(struct imx7_csi *csi,
446 struct imx7_csi_dma_buf *buf, int size)
448 imx7_csi_free_dma_buf(csi, buf);
450 buf->len = PAGE_ALIGN(size);
451 buf->virt = dma_alloc_coherent(csi->dev, buf->len, &buf->dma_addr,
452 GFP_DMA | GFP_KERNEL);
459 static int imx7_csi_dma_setup(struct imx7_csi *csi)
463 ret = imx7_csi_alloc_dma_buf(csi, &csi->underrun_buf,
464 csi->vdev_fmt.sizeimage);
466 v4l2_warn(&csi->sd, "consider increasing the CMA area\n");
470 csi->frame_sequence = 0;
471 csi->last_eof = false;
472 init_completion(&csi->last_eof_completion);
474 imx7_csi_setup_vb2_buf(csi);
479 static void imx7_csi_dma_cleanup(struct imx7_csi *csi,
480 enum vb2_buffer_state return_status)
482 imx7_csi_dma_unsetup_vb2_buf(csi, return_status);
483 imx7_csi_free_dma_buf(csi, &csi->underrun_buf);
486 static void imx7_csi_dma_stop(struct imx7_csi *csi)
488 unsigned long timeout_jiffies;
492 /* mark next EOF interrupt as the last before stream off */
493 spin_lock_irqsave(&csi->irqlock, flags);
494 csi->last_eof = true;
495 spin_unlock_irqrestore(&csi->irqlock, flags);
498 * and then wait for interrupt handler to mark completion.
500 timeout_jiffies = msecs_to_jiffies(IMX7_CSI_VIDEO_EOF_TIMEOUT);
501 ret = wait_for_completion_timeout(&csi->last_eof_completion,
504 v4l2_warn(&csi->sd, "wait last EOF timeout\n");
506 imx7_csi_hw_disable_irq(csi);
509 static void imx7_csi_configure(struct imx7_csi *csi,
510 struct v4l2_subdev_state *sd_state)
512 struct v4l2_pix_format *out_pix = &csi->vdev_fmt;
513 int width = out_pix->width;
515 u32 cr3 = BIT_FRMCNT_RST;
518 cr18 = imx7_csi_reg_read(csi, CSI_CSICR18);
520 cr18 &= ~(BIT_CSI_HW_ENABLE | BIT_MIPI_DATA_FORMAT_MASK |
521 BIT_DATA_FROM_MIPI | BIT_MIPI_DOUBLE_CMPNT |
522 BIT_BASEADDR_CHG_ERR_EN | BIT_BASEADDR_SWITCH_SEL |
523 BIT_BASEADDR_SWITCH_EN | BIT_DEINTERLACE_EN);
525 if (out_pix->field == V4L2_FIELD_INTERLACED) {
526 cr18 |= BIT_DEINTERLACE_EN;
527 stride = out_pix->width;
531 cr1 = BIT_SOF_POL | BIT_REDGE | BIT_GCLK_MODE | BIT_HSYNC_POL
532 | BIT_FCC | BIT_MCLKDIV(1) | BIT_MCLKEN;
534 cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL |
535 BIT_BASEADDR_CHG_ERR_EN;
537 if (out_pix->pixelformat == V4L2_PIX_FMT_UYVY ||
538 out_pix->pixelformat == V4L2_PIX_FMT_YUYV)
541 const struct v4l2_mbus_framefmt *sink_fmt;
543 sink_fmt = v4l2_subdev_get_pad_format(&csi->sd, sd_state,
546 cr1 = BIT_SOF_POL | BIT_REDGE | BIT_HSYNC_POL | BIT_FCC
547 | BIT_MCLKDIV(1) | BIT_MCLKEN;
549 cr18 |= BIT_DATA_FROM_MIPI;
551 switch (sink_fmt->code) {
552 case MEDIA_BUS_FMT_Y8_1X8:
553 case MEDIA_BUS_FMT_SBGGR8_1X8:
554 case MEDIA_BUS_FMT_SGBRG8_1X8:
555 case MEDIA_BUS_FMT_SGRBG8_1X8:
556 case MEDIA_BUS_FMT_SRGGB8_1X8:
557 cr18 |= BIT_MIPI_DATA_FORMAT_RAW8;
559 case MEDIA_BUS_FMT_Y10_1X10:
560 case MEDIA_BUS_FMT_SBGGR10_1X10:
561 case MEDIA_BUS_FMT_SGBRG10_1X10:
562 case MEDIA_BUS_FMT_SGRBG10_1X10:
563 case MEDIA_BUS_FMT_SRGGB10_1X10:
564 cr3 |= BIT_TWO_8BIT_SENSOR;
565 cr18 |= BIT_MIPI_DATA_FORMAT_RAW10;
567 case MEDIA_BUS_FMT_Y12_1X12:
568 case MEDIA_BUS_FMT_SBGGR12_1X12:
569 case MEDIA_BUS_FMT_SGBRG12_1X12:
570 case MEDIA_BUS_FMT_SGRBG12_1X12:
571 case MEDIA_BUS_FMT_SRGGB12_1X12:
572 cr3 |= BIT_TWO_8BIT_SENSOR;
573 cr18 |= BIT_MIPI_DATA_FORMAT_RAW12;
575 case MEDIA_BUS_FMT_Y14_1X14:
576 case MEDIA_BUS_FMT_SBGGR14_1X14:
577 case MEDIA_BUS_FMT_SGBRG14_1X14:
578 case MEDIA_BUS_FMT_SGRBG14_1X14:
579 case MEDIA_BUS_FMT_SRGGB14_1X14:
580 cr3 |= BIT_TWO_8BIT_SENSOR;
581 cr18 |= BIT_MIPI_DATA_FORMAT_RAW14;
585 * The CSI bridge has a 16-bit input bus. Depending on the
586 * connected source, data may be transmitted with 8 or 10 bits
587 * per clock sample (in bits [9:2] or [9:0] respectively) or
588 * with 16 bits per clock sample (in bits [15:0]). The data is
589 * then packed into a 32-bit FIFO (as shown in figure 13-11 of
590 * the i.MX8MM reference manual rev. 3).
592 * The data packing in a 32-bit FIFO input word is controlled by
593 * the CR3 TWO_8BIT_SENSOR field (also known as SENSOR_16BITS in
594 * the i.MX8MM reference manual). When set to 0, data packing
595 * groups four 8-bit input samples (bits [9:2]). When set to 1,
596 * data packing groups two 16-bit input samples (bits [15:0]).
598 * The register field CR18 MIPI_DOUBLE_CMPNT also needs to be
599 * configured according to the input format for YUV 4:2:2 data.
600 * The field controls the gasket between the CSI-2 receiver and
601 * the CSI bridge. On i.MX7 and i.MX8MM, the field must be set
602 * to 1 when the CSIS outputs 16-bit samples. On i.MX8MQ, the
603 * gasket ignores the MIPI_DOUBLE_CMPNT bit and YUV 4:2:2 always
604 * uses 16-bit samples. Setting MIPI_DOUBLE_CMPNT in that case
605 * has no effect, but doesn't cause any issue.
607 case MEDIA_BUS_FMT_UYVY8_2X8:
608 case MEDIA_BUS_FMT_YUYV8_2X8:
609 cr18 |= BIT_MIPI_DATA_FORMAT_YUV422_8B;
611 case MEDIA_BUS_FMT_UYVY8_1X16:
612 case MEDIA_BUS_FMT_YUYV8_1X16:
613 cr3 |= BIT_TWO_8BIT_SENSOR;
614 cr18 |= BIT_MIPI_DATA_FORMAT_YUV422_8B |
615 BIT_MIPI_DOUBLE_CMPNT;
620 imx7_csi_reg_write(csi, cr1, CSI_CSICR1);
621 imx7_csi_reg_write(csi, BIT_DMA_BURST_TYPE_RFF_INCR16, CSI_CSICR2);
622 imx7_csi_reg_write(csi, cr3, CSI_CSICR3);
623 imx7_csi_reg_write(csi, cr18, CSI_CSICR18);
625 imx7_csi_reg_write(csi, (width * out_pix->height) >> 2, CSI_CSIRXCNT);
626 imx7_csi_reg_write(csi, BIT_IMAGE_WIDTH(width) |
627 BIT_IMAGE_HEIGHT(out_pix->height),
629 imx7_csi_reg_write(csi, stride, CSI_CSIFBUF_PARA);
632 static int imx7_csi_init(struct imx7_csi *csi,
633 struct v4l2_subdev_state *sd_state)
637 ret = clk_prepare_enable(csi->mclk);
641 imx7_csi_configure(csi, sd_state);
643 ret = imx7_csi_dma_setup(csi);
645 clk_disable_unprepare(csi->mclk);
652 static void imx7_csi_deinit(struct imx7_csi *csi,
653 enum vb2_buffer_state return_status)
655 imx7_csi_dma_cleanup(csi, return_status);
656 imx7_csi_init_default(csi);
657 imx7_csi_dmareq_rff_disable(csi);
658 clk_disable_unprepare(csi->mclk);
661 static void imx7_csi_baseaddr_switch_on_second_frame(struct imx7_csi *csi)
663 u32 cr18 = imx7_csi_reg_read(csi, CSI_CSICR18);
665 cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL |
666 BIT_BASEADDR_CHG_ERR_EN;
667 cr18 |= BIT_MASK_OPTION_SECOND_FRAME;
668 imx7_csi_reg_write(csi, cr18, CSI_CSICR18);
671 static void imx7_csi_enable(struct imx7_csi *csi)
673 /* Clear the Rx FIFO and reflash the DMA controller. */
674 imx7_csi_rx_fifo_clear(csi);
675 imx7_csi_dma_reflash(csi);
677 usleep_range(2000, 3000);
679 /* Clear and enable the interrupts. */
680 imx7_csi_irq_clear(csi);
681 imx7_csi_hw_enable_irq(csi);
683 /* Enable the RxFIFO DMA and the CSI. */
684 imx7_csi_dmareq_rff_enable(csi);
685 imx7_csi_hw_enable(csi);
687 if (csi->model == IMX7_CSI_IMX8MQ)
688 imx7_csi_baseaddr_switch_on_second_frame(csi);
691 static void imx7_csi_disable(struct imx7_csi *csi)
693 imx7_csi_dma_stop(csi);
695 imx7_csi_dmareq_rff_disable(csi);
697 imx7_csi_hw_disable_irq(csi);
699 imx7_csi_hw_disable(csi);
702 /* -----------------------------------------------------------------------------
706 static void imx7_csi_error_recovery(struct imx7_csi *csi)
708 imx7_csi_hw_disable(csi);
710 imx7_csi_rx_fifo_clear(csi);
712 imx7_csi_dma_reflash(csi);
714 imx7_csi_hw_enable(csi);
717 static void imx7_csi_vb2_buf_done(struct imx7_csi *csi)
719 struct imx7_csi_vb2_buffer *done, *next;
720 struct vb2_buffer *vb;
723 done = csi->active_vb2_buf[csi->buf_num];
725 done->vbuf.field = csi->vdev_fmt.field;
726 done->vbuf.sequence = csi->frame_sequence;
727 vb = &done->vbuf.vb2_buf;
728 vb->timestamp = ktime_get_ns();
729 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
731 csi->frame_sequence++;
733 /* get next queued buffer */
734 next = imx7_csi_video_next_buf(csi);
736 dma_addr = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0);
737 csi->active_vb2_buf[csi->buf_num] = next;
739 dma_addr = csi->underrun_buf.dma_addr;
740 csi->active_vb2_buf[csi->buf_num] = NULL;
743 imx7_csi_update_buf(csi, dma_addr, csi->buf_num);
746 static irqreturn_t imx7_csi_irq_handler(int irq, void *data)
748 struct imx7_csi *csi = data;
751 spin_lock(&csi->irqlock);
753 status = imx7_csi_irq_clear(csi);
755 if (status & BIT_RFF_OR_INT) {
756 dev_warn(csi->dev, "Rx fifo overflow\n");
757 imx7_csi_error_recovery(csi);
760 if (status & BIT_HRESP_ERR_INT) {
761 dev_warn(csi->dev, "Hresponse error detected\n");
762 imx7_csi_error_recovery(csi);
765 if (status & BIT_ADDR_CH_ERR_INT) {
766 imx7_csi_hw_disable(csi);
768 imx7_csi_dma_reflash(csi);
770 imx7_csi_hw_enable(csi);
773 if ((status & BIT_DMA_TSF_DONE_FB1) &&
774 (status & BIT_DMA_TSF_DONE_FB2)) {
776 * For both FB1 and FB2 interrupter bits set case,
777 * CSI DMA is work in one of FB1 and FB2 buffer,
778 * but software can not know the state.
779 * Skip it to avoid base address updated
780 * when csi work in field0 and field1 will write to
783 } else if (status & BIT_DMA_TSF_DONE_FB1) {
785 } else if (status & BIT_DMA_TSF_DONE_FB2) {
789 if ((status & BIT_DMA_TSF_DONE_FB1) ||
790 (status & BIT_DMA_TSF_DONE_FB2)) {
791 imx7_csi_vb2_buf_done(csi);
794 complete(&csi->last_eof_completion);
795 csi->last_eof = false;
799 spin_unlock(&csi->irqlock);
804 /* -----------------------------------------------------------------------------
808 #define IMX_BUS_FMTS(fmt...) (const u32[]) {fmt, 0}
811 * List of supported pixel formats for the subdevs. Keep V4L2_PIX_FMT_UYVY and
812 * MEDIA_BUS_FMT_UYVY8_2X8 first to match IMX7_CSI_DEF_PIX_FORMAT and
813 * IMX7_CSI_DEF_MBUS_CODE.
815 * TODO: Restrict the supported formats list based on the SoC integration.
817 * The CSI bridge can be configured to sample pixel components from the Rx queue
818 * in single (8bpp) or double (16bpp) component modes. Image format variants
819 * with different sample sizes (ie YUYV_2X8 vs YUYV_1X16) determine the pixel
820 * components sampling size per each clock cycle and their packing mode (see
821 * imx7_csi_configure() for details).
823 * As the CSI bridge can be interfaced with different IP blocks depending on the
824 * SoC model it is integrated on, the Rx queue sampling size should match the
825 * size of the samples transferred by the transmitting IP block. To avoid
826 * misconfigurations of the capture pipeline, the enumeration of the supported
827 * formats should be restricted to match the pixel source transmitting mode.
829 * Example: i.MX8MM SoC integrates the CSI bridge with the Samsung CSIS CSI-2
830 * receiver which operates in dual pixel sampling mode. The CSI bridge should
831 * only expose the 1X16 formats variant which instructs it to operate in dual
832 * pixel sampling mode. When the CSI bridge is instead integrated on an i.MX7,
833 * which supports both serial and parallel input, it should expose both
836 * This currently only applies to YUYV formats, but other formats might need to
837 * be handled in the same way.
839 static const struct imx7_csi_pixfmt pixel_formats[] = {
840 /*** YUV formats start here ***/
842 .fourcc = V4L2_PIX_FMT_UYVY,
843 .codes = IMX_BUS_FMTS(
844 MEDIA_BUS_FMT_UYVY8_2X8,
845 MEDIA_BUS_FMT_UYVY8_1X16
850 .fourcc = V4L2_PIX_FMT_YUYV,
851 .codes = IMX_BUS_FMTS(
852 MEDIA_BUS_FMT_YUYV8_2X8,
853 MEDIA_BUS_FMT_YUYV8_1X16
858 /*** raw bayer and grayscale formats start here ***/
860 .fourcc = V4L2_PIX_FMT_SBGGR8,
861 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR8_1X8),
864 .fourcc = V4L2_PIX_FMT_SGBRG8,
865 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG8_1X8),
868 .fourcc = V4L2_PIX_FMT_SGRBG8,
869 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG8_1X8),
872 .fourcc = V4L2_PIX_FMT_SRGGB8,
873 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB8_1X8),
876 .fourcc = V4L2_PIX_FMT_SBGGR10,
877 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR10_1X10),
880 .fourcc = V4L2_PIX_FMT_SGBRG10,
881 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG10_1X10),
884 .fourcc = V4L2_PIX_FMT_SGRBG10,
885 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG10_1X10),
888 .fourcc = V4L2_PIX_FMT_SRGGB10,
889 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB10_1X10),
892 .fourcc = V4L2_PIX_FMT_SBGGR12,
893 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR12_1X12),
896 .fourcc = V4L2_PIX_FMT_SGBRG12,
897 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG12_1X12),
900 .fourcc = V4L2_PIX_FMT_SGRBG12,
901 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG12_1X12),
904 .fourcc = V4L2_PIX_FMT_SRGGB12,
905 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB12_1X12),
908 .fourcc = V4L2_PIX_FMT_SBGGR14,
909 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR14_1X14),
912 .fourcc = V4L2_PIX_FMT_SGBRG14,
913 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG14_1X14),
916 .fourcc = V4L2_PIX_FMT_SGRBG14,
917 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG14_1X14),
920 .fourcc = V4L2_PIX_FMT_SRGGB14,
921 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB14_1X14),
924 .fourcc = V4L2_PIX_FMT_GREY,
925 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y8_1X8),
928 .fourcc = V4L2_PIX_FMT_Y10,
929 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y10_1X10),
932 .fourcc = V4L2_PIX_FMT_Y12,
933 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y12_1X12),
936 .fourcc = V4L2_PIX_FMT_Y14,
937 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y14_1X14),
943 * Search in the pixel_formats[] array for an entry with the given fourcc
946 static const struct imx7_csi_pixfmt *imx7_csi_find_pixel_format(u32 fourcc)
950 for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) {
951 const struct imx7_csi_pixfmt *fmt = &pixel_formats[i];
953 if (fmt->fourcc == fourcc)
961 * Search in the pixel_formats[] array for an entry with the given media
962 * bus code and return it.
964 static const struct imx7_csi_pixfmt *imx7_csi_find_mbus_format(u32 code)
968 for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) {
969 const struct imx7_csi_pixfmt *fmt = &pixel_formats[i];
975 for (j = 0; fmt->codes[j]; j++) {
976 if (code == fmt->codes[j])
985 * Enumerate entries in the pixel_formats[] array that match the
986 * requested search criteria. Return the media-bus code that matches
987 * the search criteria at the requested match index.
989 * @code: The returned media-bus code that matches the search criteria at
990 * the requested match index.
991 * @index: The requested match index.
993 static int imx7_csi_enum_mbus_formats(u32 *code, u32 index)
997 for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) {
998 const struct imx7_csi_pixfmt *fmt = &pixel_formats[i];
1004 for (j = 0; fmt->codes[j]; j++) {
1006 *code = fmt->codes[j];
1017 static int imx7_csi_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
1018 const struct v4l2_mbus_framefmt *mbus,
1019 const struct imx7_csi_pixfmt *cc)
1025 cc = imx7_csi_find_mbus_format(mbus->code);
1030 /* Round up width for minimum burst size */
1031 width = round_up(mbus->width, 8);
1033 /* Round up stride for IDMAC line start address alignment */
1034 stride = round_up((width * cc->bpp) >> 3, 8);
1037 pix->height = mbus->height;
1038 pix->pixelformat = cc->fourcc;
1039 pix->colorspace = mbus->colorspace;
1040 pix->xfer_func = mbus->xfer_func;
1041 pix->ycbcr_enc = mbus->ycbcr_enc;
1042 pix->quantization = mbus->quantization;
1043 pix->field = mbus->field;
1044 pix->bytesperline = stride;
1045 pix->sizeimage = stride * pix->height;
1050 /* -----------------------------------------------------------------------------
1051 * Video Capture Device - IOCTLs
1054 static int imx7_csi_video_querycap(struct file *file, void *fh,
1055 struct v4l2_capability *cap)
1057 struct imx7_csi *csi = video_drvdata(file);
1059 strscpy(cap->driver, IMX7_CSI_VIDEO_NAME, sizeof(cap->driver));
1060 strscpy(cap->card, IMX7_CSI_VIDEO_NAME, sizeof(cap->card));
1061 snprintf(cap->bus_info, sizeof(cap->bus_info),
1062 "platform:%s", dev_name(csi->dev));
1067 static int imx7_csi_video_enum_fmt_vid_cap(struct file *file, void *fh,
1068 struct v4l2_fmtdesc *f)
1070 unsigned int index = f->index;
1073 for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) {
1074 const struct imx7_csi_pixfmt *fmt = &pixel_formats[i];
1077 * If a media bus code is specified, only consider formats that
1086 for (j = 0; fmt->codes[j]; j++) {
1087 if (f->mbus_code == fmt->codes[j])
1096 f->pixelformat = fmt->fourcc;
1106 static int imx7_csi_video_enum_framesizes(struct file *file, void *fh,
1107 struct v4l2_frmsizeenum *fsize)
1109 const struct imx7_csi_pixfmt *cc;
1111 if (fsize->index > 0)
1114 cc = imx7_csi_find_pixel_format(fsize->pixel_format);
1119 * TODO: The constraints are hardware-specific and may depend on the
1120 * pixel format. This should come from the driver using
1121 * imx_media_capture.
1123 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1124 fsize->stepwise.min_width = 1;
1125 fsize->stepwise.max_width = 65535;
1126 fsize->stepwise.min_height = 1;
1127 fsize->stepwise.max_height = 65535;
1128 fsize->stepwise.step_width = 1;
1129 fsize->stepwise.step_height = 1;
1134 static int imx7_csi_video_g_fmt_vid_cap(struct file *file, void *fh,
1135 struct v4l2_format *f)
1137 struct imx7_csi *csi = video_drvdata(file);
1139 f->fmt.pix = csi->vdev_fmt;
1144 static const struct imx7_csi_pixfmt *
1145 __imx7_csi_video_try_fmt(struct v4l2_pix_format *pixfmt,
1146 struct v4l2_rect *compose)
1148 struct v4l2_mbus_framefmt fmt_src;
1149 const struct imx7_csi_pixfmt *cc;
1152 * Find the pixel format, default to the first supported format if not
1155 cc = imx7_csi_find_pixel_format(pixfmt->pixelformat);
1157 pixfmt->pixelformat = IMX7_CSI_DEF_PIX_FORMAT;
1158 cc = imx7_csi_find_pixel_format(pixfmt->pixelformat);
1161 /* Allow IDMAC interweave but enforce field order from source. */
1162 if (V4L2_FIELD_IS_INTERLACED(pixfmt->field)) {
1163 switch (pixfmt->field) {
1164 case V4L2_FIELD_SEQ_TB:
1165 pixfmt->field = V4L2_FIELD_INTERLACED_TB;
1167 case V4L2_FIELD_SEQ_BT:
1168 pixfmt->field = V4L2_FIELD_INTERLACED_BT;
1175 v4l2_fill_mbus_format(&fmt_src, pixfmt, 0);
1176 imx7_csi_mbus_fmt_to_pix_fmt(pixfmt, &fmt_src, cc);
1179 compose->width = fmt_src.width;
1180 compose->height = fmt_src.height;
1186 static int imx7_csi_video_try_fmt_vid_cap(struct file *file, void *fh,
1187 struct v4l2_format *f)
1189 __imx7_csi_video_try_fmt(&f->fmt.pix, NULL);
1193 static int imx7_csi_video_s_fmt_vid_cap(struct file *file, void *fh,
1194 struct v4l2_format *f)
1196 struct imx7_csi *csi = video_drvdata(file);
1197 const struct imx7_csi_pixfmt *cc;
1199 if (vb2_is_busy(&csi->q)) {
1200 dev_err(csi->dev, "%s queue busy\n", __func__);
1204 cc = __imx7_csi_video_try_fmt(&f->fmt.pix, &csi->vdev_compose);
1207 csi->vdev_fmt = f->fmt.pix;
1212 static int imx7_csi_video_g_selection(struct file *file, void *fh,
1213 struct v4l2_selection *s)
1215 struct imx7_csi *csi = video_drvdata(file);
1217 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1220 switch (s->target) {
1221 case V4L2_SEL_TGT_COMPOSE:
1222 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1223 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1224 /* The compose rectangle is fixed to the source format. */
1225 s->r = csi->vdev_compose;
1227 case V4L2_SEL_TGT_COMPOSE_PADDED:
1229 * The hardware writes with a configurable but fixed DMA burst
1230 * size. If the source format width is not burst size aligned,
1231 * the written frame contains padding to the right.
1235 s->r.width = csi->vdev_fmt.width;
1236 s->r.height = csi->vdev_fmt.height;
1245 static const struct v4l2_ioctl_ops imx7_csi_video_ioctl_ops = {
1246 .vidioc_querycap = imx7_csi_video_querycap,
1248 .vidioc_enum_fmt_vid_cap = imx7_csi_video_enum_fmt_vid_cap,
1249 .vidioc_enum_framesizes = imx7_csi_video_enum_framesizes,
1251 .vidioc_g_fmt_vid_cap = imx7_csi_video_g_fmt_vid_cap,
1252 .vidioc_try_fmt_vid_cap = imx7_csi_video_try_fmt_vid_cap,
1253 .vidioc_s_fmt_vid_cap = imx7_csi_video_s_fmt_vid_cap,
1255 .vidioc_g_selection = imx7_csi_video_g_selection,
1257 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1258 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1259 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1260 .vidioc_querybuf = vb2_ioctl_querybuf,
1261 .vidioc_qbuf = vb2_ioctl_qbuf,
1262 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1263 .vidioc_expbuf = vb2_ioctl_expbuf,
1264 .vidioc_streamon = vb2_ioctl_streamon,
1265 .vidioc_streamoff = vb2_ioctl_streamoff,
1268 /* -----------------------------------------------------------------------------
1269 * Video Capture Device - Queue Operations
1272 static int imx7_csi_video_queue_setup(struct vb2_queue *vq,
1273 unsigned int *nbuffers,
1274 unsigned int *nplanes,
1275 unsigned int sizes[],
1276 struct device *alloc_devs[])
1278 struct imx7_csi *csi = vb2_get_drv_priv(vq);
1279 struct v4l2_pix_format *pix = &csi->vdev_fmt;
1280 unsigned int count = *nbuffers;
1282 if (vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1286 if (*nplanes != 1 || sizes[0] < pix->sizeimage)
1288 count += vq->num_buffers;
1291 count = min_t(__u32, IMX7_CSI_VIDEO_MEM_LIMIT / pix->sizeimage, count);
1294 *nbuffers = (count < vq->num_buffers) ? 0 :
1295 count - vq->num_buffers;
1300 sizes[0] = pix->sizeimage;
1305 static int imx7_csi_video_buf_init(struct vb2_buffer *vb)
1307 struct imx7_csi_vb2_buffer *buf = to_imx7_csi_vb2_buffer(vb);
1309 INIT_LIST_HEAD(&buf->list);
1314 static int imx7_csi_video_buf_prepare(struct vb2_buffer *vb)
1316 struct imx7_csi *csi = vb2_get_drv_priv(vb->vb2_queue);
1317 struct v4l2_pix_format *pix = &csi->vdev_fmt;
1319 if (vb2_plane_size(vb, 0) < pix->sizeimage) {
1321 "data will not fit into plane (%lu < %lu)\n",
1322 vb2_plane_size(vb, 0), (long)pix->sizeimage);
1326 vb2_set_plane_payload(vb, 0, pix->sizeimage);
1331 static bool imx7_csi_fast_track_buffer(struct imx7_csi *csi,
1332 struct imx7_csi_vb2_buffer *buf)
1334 unsigned long flags;
1335 dma_addr_t dma_addr;
1339 if (!csi->is_streaming)
1342 dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vbuf.vb2_buf, 0);
1345 * buf_num holds the framebuffer ID of the most recently (*not* the
1346 * next anticipated) triggered interrupt. Without loss of generality,
1347 * if buf_num is 0, the hardware is capturing to FB2. If FB1 has been
1348 * programmed with a dummy buffer (as indicated by active_vb2_buf[0]
1349 * being NULL), then we can fast-track the new buffer by programming
1350 * its address in FB1 before the hardware completes FB2, instead of
1351 * adding it to the buffer queue and incurring a delay of one
1354 * The irqlock prevents races with the interrupt handler that updates
1355 * buf_num when it programs the next buffer, but we can still race with
1356 * the hardware if we program the buffer in FB1 just after the hardware
1357 * completes FB2 and switches to FB1 and before buf_num can be updated
1358 * by the interrupt handler for FB2. The fast-tracked buffer would
1359 * then be ignored by the hardware while the driver would think it has
1360 * successfully been processed.
1362 * To avoid this problem, if we can't avoid the race, we can detect
1363 * that we have lost it by checking, after programming the buffer in
1364 * FB1, if the interrupt flag indicating completion of FB2 has been
1365 * raised. If that is not the case, fast-tracking succeeded, and we can
1366 * update active_vb2_buf[0]. Otherwise, we may or may not have lost the
1367 * race (as the interrupt flag may have been raised just after
1368 * programming FB1 and before we read the interrupt status register),
1369 * and we need to assume the worst case of a race loss and queue the
1370 * buffer through the slow path.
1373 spin_lock_irqsave(&csi->irqlock, flags);
1375 buf_num = csi->buf_num;
1376 if (csi->active_vb2_buf[buf_num]) {
1377 spin_unlock_irqrestore(&csi->irqlock, flags);
1381 imx7_csi_update_buf(csi, dma_addr, buf_num);
1383 isr = imx7_csi_reg_read(csi, CSI_CSISR);
1384 if (isr & (buf_num ? BIT_DMA_TSF_DONE_FB1 : BIT_DMA_TSF_DONE_FB2)) {
1386 * The interrupt for the /other/ FB just came (the isr hasn't
1387 * run yet though, because we have the lock here); we can't be
1388 * sure we've programmed buf_num FB in time, so queue the buffer
1389 * to the buffer queue normally. No need to undo writing the FB
1390 * register, since we won't return it as active_vb2_buf is NULL,
1391 * so it's okay to potentially write it to both FB1 and FB2;
1392 * only the one where it was queued normally will be returned.
1394 spin_unlock_irqrestore(&csi->irqlock, flags);
1398 csi->active_vb2_buf[buf_num] = buf;
1400 spin_unlock_irqrestore(&csi->irqlock, flags);
1404 static void imx7_csi_video_buf_queue(struct vb2_buffer *vb)
1406 struct imx7_csi *csi = vb2_get_drv_priv(vb->vb2_queue);
1407 struct imx7_csi_vb2_buffer *buf = to_imx7_csi_vb2_buffer(vb);
1408 unsigned long flags;
1410 if (imx7_csi_fast_track_buffer(csi, buf))
1413 spin_lock_irqsave(&csi->q_lock, flags);
1415 list_add_tail(&buf->list, &csi->ready_q);
1417 spin_unlock_irqrestore(&csi->q_lock, flags);
1420 static int imx7_csi_video_validate_fmt(struct imx7_csi *csi)
1422 struct v4l2_subdev_format fmt_src = {
1423 .pad = IMX7_CSI_PAD_SRC,
1424 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1426 const struct imx7_csi_pixfmt *cc;
1429 /* Retrieve the media bus format on the source subdev. */
1430 ret = v4l2_subdev_call_state_active(&csi->sd, pad, get_fmt, &fmt_src);
1435 * Verify that the media bus size matches the size set on the video
1436 * node. It is sufficient to check the compose rectangle size without
1437 * checking the rounded size from pix_fmt, as the rounded size is
1438 * derived directly from the compose rectangle size, and will thus
1439 * always match if the compose rectangle matches.
1441 if (csi->vdev_compose.width != fmt_src.format.width ||
1442 csi->vdev_compose.height != fmt_src.format.height)
1446 * Verify that the media bus code is compatible with the pixel format
1447 * set on the video node.
1449 cc = imx7_csi_find_mbus_format(fmt_src.format.code);
1450 if (!cc || csi->vdev_cc->yuv != cc->yuv)
1456 static int imx7_csi_video_start_streaming(struct vb2_queue *vq,
1459 struct imx7_csi *csi = vb2_get_drv_priv(vq);
1460 struct imx7_csi_vb2_buffer *buf, *tmp;
1461 unsigned long flags;
1464 ret = imx7_csi_video_validate_fmt(csi);
1466 dev_err(csi->dev, "capture format not valid\n");
1470 mutex_lock(&csi->mdev.graph_mutex);
1472 ret = __video_device_pipeline_start(csi->vdev, &csi->pipe);
1476 ret = v4l2_subdev_call(&csi->sd, video, s_stream, 1);
1480 mutex_unlock(&csi->mdev.graph_mutex);
1485 __video_device_pipeline_stop(csi->vdev);
1487 mutex_unlock(&csi->mdev.graph_mutex);
1488 dev_err(csi->dev, "pipeline start failed with %d\n", ret);
1490 spin_lock_irqsave(&csi->q_lock, flags);
1491 list_for_each_entry_safe(buf, tmp, &csi->ready_q, list) {
1492 list_del(&buf->list);
1493 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED);
1495 spin_unlock_irqrestore(&csi->q_lock, flags);
1499 static void imx7_csi_video_stop_streaming(struct vb2_queue *vq)
1501 struct imx7_csi *csi = vb2_get_drv_priv(vq);
1502 struct imx7_csi_vb2_buffer *frame;
1503 struct imx7_csi_vb2_buffer *tmp;
1504 unsigned long flags;
1506 mutex_lock(&csi->mdev.graph_mutex);
1507 v4l2_subdev_call(&csi->sd, video, s_stream, 0);
1508 __video_device_pipeline_stop(csi->vdev);
1509 mutex_unlock(&csi->mdev.graph_mutex);
1511 /* release all active buffers */
1512 spin_lock_irqsave(&csi->q_lock, flags);
1513 list_for_each_entry_safe(frame, tmp, &csi->ready_q, list) {
1514 list_del(&frame->list);
1515 vb2_buffer_done(&frame->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
1517 spin_unlock_irqrestore(&csi->q_lock, flags);
1520 static const struct vb2_ops imx7_csi_video_qops = {
1521 .queue_setup = imx7_csi_video_queue_setup,
1522 .buf_init = imx7_csi_video_buf_init,
1523 .buf_prepare = imx7_csi_video_buf_prepare,
1524 .buf_queue = imx7_csi_video_buf_queue,
1525 .wait_prepare = vb2_ops_wait_prepare,
1526 .wait_finish = vb2_ops_wait_finish,
1527 .start_streaming = imx7_csi_video_start_streaming,
1528 .stop_streaming = imx7_csi_video_stop_streaming,
1531 /* -----------------------------------------------------------------------------
1532 * Video Capture Device - File Operations
1535 static int imx7_csi_video_open(struct file *file)
1537 struct imx7_csi *csi = video_drvdata(file);
1540 if (mutex_lock_interruptible(&csi->vdev_mutex))
1541 return -ERESTARTSYS;
1543 ret = v4l2_fh_open(file);
1545 dev_err(csi->dev, "v4l2_fh_open failed\n");
1549 ret = v4l2_pipeline_pm_get(&csi->vdev->entity);
1551 v4l2_fh_release(file);
1554 mutex_unlock(&csi->vdev_mutex);
1558 static int imx7_csi_video_release(struct file *file)
1560 struct imx7_csi *csi = video_drvdata(file);
1561 struct vb2_queue *vq = &csi->q;
1563 mutex_lock(&csi->vdev_mutex);
1565 if (file->private_data == vq->owner) {
1566 vb2_queue_release(vq);
1570 v4l2_pipeline_pm_put(&csi->vdev->entity);
1572 v4l2_fh_release(file);
1573 mutex_unlock(&csi->vdev_mutex);
1577 static const struct v4l2_file_operations imx7_csi_video_fops = {
1578 .owner = THIS_MODULE,
1579 .open = imx7_csi_video_open,
1580 .release = imx7_csi_video_release,
1581 .poll = vb2_fop_poll,
1582 .unlocked_ioctl = video_ioctl2,
1583 .mmap = vb2_fop_mmap,
1586 /* -----------------------------------------------------------------------------
1587 * Video Capture Device - Init & Cleanup
1590 static struct imx7_csi_vb2_buffer *imx7_csi_video_next_buf(struct imx7_csi *csi)
1592 struct imx7_csi_vb2_buffer *buf = NULL;
1593 unsigned long flags;
1595 spin_lock_irqsave(&csi->q_lock, flags);
1597 /* get next queued buffer */
1598 if (!list_empty(&csi->ready_q)) {
1599 buf = list_entry(csi->ready_q.next, struct imx7_csi_vb2_buffer,
1601 list_del(&buf->list);
1604 spin_unlock_irqrestore(&csi->q_lock, flags);
1609 static int imx7_csi_video_init_format(struct imx7_csi *csi)
1611 struct v4l2_mbus_framefmt format = { };
1613 format.code = IMX7_CSI_DEF_MBUS_CODE;
1614 format.width = IMX7_CSI_DEF_PIX_WIDTH;
1615 format.height = IMX7_CSI_DEF_PIX_HEIGHT;
1616 format.field = V4L2_FIELD_NONE;
1618 imx7_csi_mbus_fmt_to_pix_fmt(&csi->vdev_fmt, &format, NULL);
1619 csi->vdev_compose.width = format.width;
1620 csi->vdev_compose.height = format.height;
1622 csi->vdev_cc = imx7_csi_find_pixel_format(csi->vdev_fmt.pixelformat);
1627 static int imx7_csi_video_register(struct imx7_csi *csi)
1629 struct v4l2_subdev *sd = &csi->sd;
1630 struct v4l2_device *v4l2_dev = sd->v4l2_dev;
1631 struct video_device *vdev = csi->vdev;
1634 vdev->v4l2_dev = v4l2_dev;
1636 /* Initialize the default format and compose rectangle. */
1637 ret = imx7_csi_video_init_format(csi);
1641 /* Register the video device. */
1642 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1644 dev_err(csi->dev, "Failed to register video device\n");
1648 dev_info(csi->dev, "Registered %s as /dev/%s\n", vdev->name,
1649 video_device_node_name(vdev));
1651 /* Create the link from the CSI subdev to the video device. */
1652 ret = media_create_pad_link(&sd->entity, IMX7_CSI_PAD_SRC,
1653 &vdev->entity, 0, MEDIA_LNK_FL_IMMUTABLE |
1654 MEDIA_LNK_FL_ENABLED);
1656 dev_err(csi->dev, "failed to create link to device node\n");
1657 video_unregister_device(vdev);
1664 static void imx7_csi_video_unregister(struct imx7_csi *csi)
1666 media_entity_cleanup(&csi->vdev->entity);
1667 video_unregister_device(csi->vdev);
1670 static int imx7_csi_video_init(struct imx7_csi *csi)
1672 struct video_device *vdev;
1673 struct vb2_queue *vq;
1676 mutex_init(&csi->vdev_mutex);
1677 INIT_LIST_HEAD(&csi->ready_q);
1678 spin_lock_init(&csi->q_lock);
1680 /* Allocate and initialize the video device. */
1681 vdev = video_device_alloc();
1685 vdev->fops = &imx7_csi_video_fops;
1686 vdev->ioctl_ops = &imx7_csi_video_ioctl_ops;
1688 vdev->release = video_device_release;
1689 vdev->vfl_dir = VFL_DIR_RX;
1690 vdev->tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
1691 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING
1693 vdev->lock = &csi->vdev_mutex;
1694 vdev->queue = &csi->q;
1696 snprintf(vdev->name, sizeof(vdev->name), "%s capture", csi->sd.name);
1698 video_set_drvdata(vdev, csi);
1701 /* Initialize the video device pad. */
1702 csi->vdev_pad.flags = MEDIA_PAD_FL_SINK;
1703 ret = media_entity_pads_init(&vdev->entity, 1, &csi->vdev_pad);
1705 video_device_release(vdev);
1709 /* Initialize the vb2 queue. */
1711 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1712 vq->io_modes = VB2_MMAP | VB2_DMABUF;
1714 vq->buf_struct_size = sizeof(struct imx7_csi_vb2_buffer);
1715 vq->ops = &imx7_csi_video_qops;
1716 vq->mem_ops = &vb2_dma_contig_memops;
1717 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1718 vq->lock = &csi->vdev_mutex;
1719 vq->min_buffers_needed = 2;
1722 ret = vb2_queue_init(vq);
1724 dev_err(csi->dev, "vb2_queue_init failed\n");
1725 video_device_release(vdev);
1732 /* -----------------------------------------------------------------------------
1733 * V4L2 Subdev Operations
1736 static int imx7_csi_s_stream(struct v4l2_subdev *sd, int enable)
1738 struct imx7_csi *csi = v4l2_get_subdevdata(sd);
1739 struct v4l2_subdev_state *sd_state;
1742 sd_state = v4l2_subdev_lock_and_get_active_state(sd);
1745 ret = imx7_csi_init(csi, sd_state);
1749 ret = v4l2_subdev_call(csi->src_sd, video, s_stream, 1);
1751 imx7_csi_deinit(csi, VB2_BUF_STATE_QUEUED);
1755 imx7_csi_enable(csi);
1757 imx7_csi_disable(csi);
1759 v4l2_subdev_call(csi->src_sd, video, s_stream, 0);
1761 imx7_csi_deinit(csi, VB2_BUF_STATE_ERROR);
1764 csi->is_streaming = !!enable;
1767 v4l2_subdev_unlock_state(sd_state);
1772 static int imx7_csi_init_cfg(struct v4l2_subdev *sd,
1773 struct v4l2_subdev_state *sd_state)
1775 const struct imx7_csi_pixfmt *cc;
1778 cc = imx7_csi_find_mbus_format(IMX7_CSI_DEF_MBUS_CODE);
1780 for (i = 0; i < IMX7_CSI_PADS_NUM; i++) {
1781 struct v4l2_mbus_framefmt *mf =
1782 v4l2_subdev_get_pad_format(sd, sd_state, i);
1784 mf->code = IMX7_CSI_DEF_MBUS_CODE;
1785 mf->width = IMX7_CSI_DEF_PIX_WIDTH;
1786 mf->height = IMX7_CSI_DEF_PIX_HEIGHT;
1787 mf->field = V4L2_FIELD_NONE;
1789 mf->colorspace = V4L2_COLORSPACE_SRGB;
1790 mf->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(mf->colorspace);
1791 mf->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(mf->colorspace);
1792 mf->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(!cc->yuv,
1793 mf->colorspace, mf->ycbcr_enc);
1799 static int imx7_csi_enum_mbus_code(struct v4l2_subdev *sd,
1800 struct v4l2_subdev_state *sd_state,
1801 struct v4l2_subdev_mbus_code_enum *code)
1803 struct v4l2_mbus_framefmt *in_fmt;
1806 in_fmt = v4l2_subdev_get_pad_format(sd, sd_state, IMX7_CSI_PAD_SINK);
1808 switch (code->pad) {
1809 case IMX7_CSI_PAD_SINK:
1810 ret = imx7_csi_enum_mbus_formats(&code->code, code->index);
1813 case IMX7_CSI_PAD_SRC:
1814 if (code->index != 0) {
1819 code->code = in_fmt->code;
1831 * Default the colorspace in tryfmt to SRGB if set to an unsupported
1832 * colorspace or not initialized. Then set the remaining colorimetry
1833 * parameters based on the colorspace if they are uninitialized.
1835 * tryfmt->code must be set on entry.
1837 static void imx7_csi_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt)
1839 const struct imx7_csi_pixfmt *cc;
1840 bool is_rgb = false;
1842 cc = imx7_csi_find_mbus_format(tryfmt->code);
1846 switch (tryfmt->colorspace) {
1847 case V4L2_COLORSPACE_SMPTE170M:
1848 case V4L2_COLORSPACE_REC709:
1849 case V4L2_COLORSPACE_JPEG:
1850 case V4L2_COLORSPACE_SRGB:
1851 case V4L2_COLORSPACE_BT2020:
1852 case V4L2_COLORSPACE_OPRGB:
1853 case V4L2_COLORSPACE_DCI_P3:
1854 case V4L2_COLORSPACE_RAW:
1857 tryfmt->colorspace = V4L2_COLORSPACE_SRGB;
1861 if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT)
1863 V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
1865 if (tryfmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
1867 V4L2_MAP_YCBCR_ENC_DEFAULT(tryfmt->colorspace);
1869 if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT)
1870 tryfmt->quantization =
1871 V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb,
1876 static void imx7_csi_try_fmt(struct v4l2_subdev *sd,
1877 struct v4l2_subdev_state *sd_state,
1878 struct v4l2_subdev_format *sdformat,
1879 const struct imx7_csi_pixfmt **cc)
1881 const struct imx7_csi_pixfmt *in_cc;
1882 struct v4l2_mbus_framefmt *in_fmt;
1885 in_fmt = v4l2_subdev_get_pad_format(sd, sd_state, IMX7_CSI_PAD_SINK);
1887 switch (sdformat->pad) {
1888 case IMX7_CSI_PAD_SRC:
1889 in_cc = imx7_csi_find_mbus_format(in_fmt->code);
1891 sdformat->format.width = in_fmt->width;
1892 sdformat->format.height = in_fmt->height;
1893 sdformat->format.code = in_fmt->code;
1894 sdformat->format.field = in_fmt->field;
1897 sdformat->format.colorspace = in_fmt->colorspace;
1898 sdformat->format.xfer_func = in_fmt->xfer_func;
1899 sdformat->format.quantization = in_fmt->quantization;
1900 sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc;
1903 case IMX7_CSI_PAD_SINK:
1904 *cc = imx7_csi_find_mbus_format(sdformat->format.code);
1906 code = IMX7_CSI_DEF_MBUS_CODE;
1907 *cc = imx7_csi_find_mbus_format(code);
1908 sdformat->format.code = code;
1911 if (sdformat->format.field != V4L2_FIELD_INTERLACED)
1912 sdformat->format.field = V4L2_FIELD_NONE;
1916 imx7_csi_try_colorimetry(&sdformat->format);
1919 static int imx7_csi_set_fmt(struct v4l2_subdev *sd,
1920 struct v4l2_subdev_state *sd_state,
1921 struct v4l2_subdev_format *sdformat)
1923 struct imx7_csi *csi = v4l2_get_subdevdata(sd);
1924 const struct imx7_csi_pixfmt *outcc;
1925 struct v4l2_mbus_framefmt *outfmt;
1926 const struct imx7_csi_pixfmt *cc;
1927 struct v4l2_mbus_framefmt *fmt;
1928 struct v4l2_subdev_format format;
1930 if (csi->is_streaming)
1933 imx7_csi_try_fmt(sd, sd_state, sdformat, &cc);
1935 fmt = v4l2_subdev_get_pad_format(sd, sd_state, sdformat->pad);
1937 *fmt = sdformat->format;
1939 if (sdformat->pad == IMX7_CSI_PAD_SINK) {
1940 /* propagate format to source pads */
1941 format.pad = IMX7_CSI_PAD_SRC;
1942 format.which = sdformat->which;
1943 format.format = sdformat->format;
1944 imx7_csi_try_fmt(sd, sd_state, &format, &outcc);
1946 outfmt = v4l2_subdev_get_pad_format(sd, sd_state,
1948 *outfmt = format.format;
1954 static int imx7_csi_pad_link_validate(struct v4l2_subdev *sd,
1955 struct media_link *link,
1956 struct v4l2_subdev_format *source_fmt,
1957 struct v4l2_subdev_format *sink_fmt)
1959 struct imx7_csi *csi = v4l2_get_subdevdata(sd);
1960 struct media_pad *pad = NULL;
1965 * Validate the source link, and record whether the source uses the
1966 * parallel input or the CSI-2 receiver.
1968 ret = v4l2_subdev_link_validate_default(sd, link, source_fmt, sink_fmt);
1972 switch (csi->src_sd->entity.function) {
1973 case MEDIA_ENT_F_VID_IF_BRIDGE:
1974 /* The input is the CSI-2 receiver. */
1975 csi->is_csi2 = true;
1978 case MEDIA_ENT_F_VID_MUX:
1979 /* The input is the mux, check its input. */
1980 for (i = 0; i < csi->src_sd->entity.num_pads; i++) {
1981 struct media_pad *spad = &csi->src_sd->entity.pads[i];
1983 if (!(spad->flags & MEDIA_PAD_FL_SINK))
1986 pad = media_pad_remote_pad_first(spad);
1994 csi->is_csi2 = pad->entity->function == MEDIA_ENT_F_VID_IF_BRIDGE;
1999 * The input is an external entity, it must use the parallel
2002 csi->is_csi2 = false;
2009 static int imx7_csi_registered(struct v4l2_subdev *sd)
2011 struct imx7_csi *csi = v4l2_get_subdevdata(sd);
2014 ret = imx7_csi_video_init(csi);
2018 ret = imx7_csi_video_register(csi);
2022 ret = v4l2_device_register_subdev_nodes(&csi->v4l2_dev);
2026 ret = media_device_register(&csi->mdev);
2033 imx7_csi_video_unregister(csi);
2037 static void imx7_csi_unregistered(struct v4l2_subdev *sd)
2039 struct imx7_csi *csi = v4l2_get_subdevdata(sd);
2041 imx7_csi_video_unregister(csi);
2044 static const struct v4l2_subdev_video_ops imx7_csi_video_ops = {
2045 .s_stream = imx7_csi_s_stream,
2048 static const struct v4l2_subdev_pad_ops imx7_csi_pad_ops = {
2049 .init_cfg = imx7_csi_init_cfg,
2050 .enum_mbus_code = imx7_csi_enum_mbus_code,
2051 .get_fmt = v4l2_subdev_get_fmt,
2052 .set_fmt = imx7_csi_set_fmt,
2053 .link_validate = imx7_csi_pad_link_validate,
2056 static const struct v4l2_subdev_ops imx7_csi_subdev_ops = {
2057 .video = &imx7_csi_video_ops,
2058 .pad = &imx7_csi_pad_ops,
2061 static const struct v4l2_subdev_internal_ops imx7_csi_internal_ops = {
2062 .registered = imx7_csi_registered,
2063 .unregistered = imx7_csi_unregistered,
2066 /* -----------------------------------------------------------------------------
2067 * Media Entity Operations
2070 static const struct media_entity_operations imx7_csi_entity_ops = {
2071 .link_validate = v4l2_subdev_link_validate,
2072 .get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
2075 /* -----------------------------------------------------------------------------
2079 static int imx7_csi_notify_bound(struct v4l2_async_notifier *notifier,
2080 struct v4l2_subdev *sd,
2081 struct v4l2_async_subdev *asd)
2083 struct imx7_csi *csi = imx7_csi_notifier_to_dev(notifier);
2084 struct media_pad *sink = &csi->sd.entity.pads[IMX7_CSI_PAD_SINK];
2088 return v4l2_create_fwnode_links_to_pad(sd, sink, MEDIA_LNK_FL_ENABLED |
2089 MEDIA_LNK_FL_IMMUTABLE);
2092 static int imx7_csi_notify_complete(struct v4l2_async_notifier *notifier)
2094 struct imx7_csi *csi = imx7_csi_notifier_to_dev(notifier);
2096 return v4l2_device_register_subdev_nodes(&csi->v4l2_dev);
2099 static const struct v4l2_async_notifier_operations imx7_csi_notify_ops = {
2100 .bound = imx7_csi_notify_bound,
2101 .complete = imx7_csi_notify_complete,
2104 static int imx7_csi_async_register(struct imx7_csi *csi)
2106 struct v4l2_async_subdev *asd;
2107 struct fwnode_handle *ep;
2110 v4l2_async_nf_init(&csi->notifier);
2112 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csi->dev), 0, 0,
2113 FWNODE_GRAPH_ENDPOINT_NEXT);
2115 ret = dev_err_probe(csi->dev, -ENOTCONN,
2116 "Failed to get remote endpoint\n");
2120 asd = v4l2_async_nf_add_fwnode_remote(&csi->notifier, ep,
2121 struct v4l2_async_subdev);
2123 fwnode_handle_put(ep);
2126 ret = dev_err_probe(csi->dev, PTR_ERR(asd),
2127 "Failed to add remote subdev to notifier\n");
2131 csi->notifier.ops = &imx7_csi_notify_ops;
2133 ret = v4l2_async_nf_register(&csi->v4l2_dev, &csi->notifier);
2140 v4l2_async_nf_cleanup(&csi->notifier);
2144 static void imx7_csi_media_cleanup(struct imx7_csi *csi)
2146 v4l2_device_unregister(&csi->v4l2_dev);
2147 media_device_unregister(&csi->mdev);
2148 v4l2_subdev_cleanup(&csi->sd);
2149 media_device_cleanup(&csi->mdev);
2152 static const struct media_device_ops imx7_csi_media_ops = {
2153 .link_notify = v4l2_pipeline_link_notify,
2156 static int imx7_csi_media_dev_init(struct imx7_csi *csi)
2160 strscpy(csi->mdev.model, "imx-media", sizeof(csi->mdev.model));
2161 csi->mdev.ops = &imx7_csi_media_ops;
2162 csi->mdev.dev = csi->dev;
2164 csi->v4l2_dev.mdev = &csi->mdev;
2165 strscpy(csi->v4l2_dev.name, "imx-media",
2166 sizeof(csi->v4l2_dev.name));
2167 snprintf(csi->mdev.bus_info, sizeof(csi->mdev.bus_info),
2168 "platform:%s", dev_name(csi->mdev.dev));
2170 media_device_init(&csi->mdev);
2172 ret = v4l2_device_register(csi->dev, &csi->v4l2_dev);
2174 v4l2_err(&csi->v4l2_dev,
2175 "Failed to register v4l2_device: %d\n", ret);
2182 media_device_cleanup(&csi->mdev);
2187 static int imx7_csi_media_init(struct imx7_csi *csi)
2192 /* add media device */
2193 ret = imx7_csi_media_dev_init(csi);
2197 v4l2_subdev_init(&csi->sd, &imx7_csi_subdev_ops);
2198 v4l2_set_subdevdata(&csi->sd, csi);
2199 csi->sd.internal_ops = &imx7_csi_internal_ops;
2200 csi->sd.entity.ops = &imx7_csi_entity_ops;
2201 csi->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
2202 csi->sd.dev = csi->dev;
2203 csi->sd.owner = THIS_MODULE;
2204 csi->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
2205 snprintf(csi->sd.name, sizeof(csi->sd.name), "csi");
2207 for (i = 0; i < IMX7_CSI_PADS_NUM; i++)
2208 csi->pad[i].flags = (i == IMX7_CSI_PAD_SINK) ?
2209 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
2211 ret = media_entity_pads_init(&csi->sd.entity, IMX7_CSI_PADS_NUM,
2216 ret = v4l2_subdev_init_finalize(&csi->sd);
2220 ret = v4l2_device_register_subdev(&csi->v4l2_dev, &csi->sd);
2227 imx7_csi_media_cleanup(csi);
2231 static int imx7_csi_probe(struct platform_device *pdev)
2233 struct device *dev = &pdev->dev;
2234 struct imx7_csi *csi;
2237 csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL);
2242 platform_set_drvdata(pdev, csi);
2244 spin_lock_init(&csi->irqlock);
2246 /* Acquire resources and install interrupt handler. */
2247 csi->mclk = devm_clk_get(&pdev->dev, "mclk");
2248 if (IS_ERR(csi->mclk)) {
2249 ret = PTR_ERR(csi->mclk);
2250 dev_err(dev, "Failed to get mclk: %d", ret);
2254 csi->irq = platform_get_irq(pdev, 0);
2258 csi->regbase = devm_platform_ioremap_resource(pdev, 0);
2259 if (IS_ERR(csi->regbase))
2260 return PTR_ERR(csi->regbase);
2262 csi->model = (enum imx_csi_model)(uintptr_t)of_device_get_match_data(&pdev->dev);
2264 ret = devm_request_irq(dev, csi->irq, imx7_csi_irq_handler, 0, "csi",
2267 dev_err(dev, "Request CSI IRQ failed.\n");
2271 /* Initialize all the media device infrastructure. */
2272 ret = imx7_csi_media_init(csi);
2276 ret = imx7_csi_async_register(csi);
2278 goto err_media_cleanup;
2283 imx7_csi_media_cleanup(csi);
2288 static void imx7_csi_remove(struct platform_device *pdev)
2290 struct imx7_csi *csi = platform_get_drvdata(pdev);
2292 imx7_csi_media_cleanup(csi);
2294 v4l2_async_nf_unregister(&csi->notifier);
2295 v4l2_async_nf_cleanup(&csi->notifier);
2296 v4l2_async_unregister_subdev(&csi->sd);
2299 static const struct of_device_id imx7_csi_of_match[] = {
2300 { .compatible = "fsl,imx8mq-csi", .data = (void *)IMX7_CSI_IMX8MQ },
2301 { .compatible = "fsl,imx7-csi", .data = (void *)IMX7_CSI_IMX7 },
2302 { .compatible = "fsl,imx6ul-csi", .data = (void *)IMX7_CSI_IMX7 },
2305 MODULE_DEVICE_TABLE(of, imx7_csi_of_match);
2307 static struct platform_driver imx7_csi_driver = {
2308 .probe = imx7_csi_probe,
2309 .remove_new = imx7_csi_remove,
2311 .of_match_table = imx7_csi_of_match,
2315 module_platform_driver(imx7_csi_driver);
2317 MODULE_DESCRIPTION("i.MX7 CSI subdev driver");
2319 MODULE_LICENSE("GPL v2");
2320 MODULE_ALIAS("platform:imx7-csi");