1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * V4L2 Capture CSI Subdev for Freescale i.MX6UL/L / i.MX7 SOC 4 * 5 * Copyright (c) 2019 Linaro Ltd 6 * 7 */ 8 9 #include <linux/clk.h> 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> 22 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> 29 30 #define IMX7_CSI_PAD_SINK 0 31 #define IMX7_CSI_PAD_SRC 1 32 #define IMX7_CSI_PADS_NUM 2 33 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) 64 65 /* control reg 2 */ 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) 70 71 /* control reg 3 */ 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) 88 89 /* csi status reg */ 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) 108 109 /* csi image parameter reg */ 110 #define BIT_IMAGE_WIDTH(n) ((n) << 16) 111 #define BIT_IMAGE_HEIGHT(n) (n) 112 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) 135 136 #define CSI_MCLK_VF 1 137 #define CSI_MCLK_ENC 2 138 #define CSI_MCLK_RAW 4 139 #define CSI_MCLK_I2C 8 140 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 148 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 156 157 #define CSI_CSICR18 0x48 158 #define CSI_CSICR19 0x4c 159 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 164 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 169 170 enum imx_csi_model { 171 IMX7_CSI_IMX7 = 0, 172 IMX7_CSI_IMX8MQ, 173 }; 174 175 struct imx7_csi_pixfmt { 176 /* the in-memory FourCC pixel format */ 177 u32 fourcc; 178 /* 179 * the set of equivalent media bus codes for the fourcc. 180 * NOTE! codes pointer is NULL for in-memory-only formats. 181 */ 182 const u32 *codes; 183 int bpp; /* total bpp */ 184 bool yuv; 185 }; 186 187 struct imx7_csi_vb2_buffer { 188 struct vb2_v4l2_buffer vbuf; 189 struct list_head list; 190 }; 191 192 static inline struct imx7_csi_vb2_buffer * 193 to_imx7_csi_vb2_buffer(struct vb2_buffer *vb) 194 { 195 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 196 197 return container_of(vbuf, struct imx7_csi_vb2_buffer, vbuf); 198 } 199 200 struct imx7_csi_dma_buf { 201 void *virt; 202 dma_addr_t dma_addr; 203 unsigned long len; 204 }; 205 206 struct imx7_csi { 207 struct device *dev; 208 209 /* Resources and locks */ 210 void __iomem *regbase; 211 int irq; 212 struct clk *mclk; 213 214 struct mutex lock; /* Protects is_streaming, format_mbus, cc */ 215 spinlock_t irqlock; /* Protects last_eof */ 216 217 /* Media and V4L2 device */ 218 struct media_device mdev; 219 struct v4l2_device v4l2_dev; 220 struct v4l2_async_notifier notifier; 221 struct media_pipeline pipe; 222 223 struct v4l2_subdev *src_sd; 224 bool is_csi2; 225 226 /* V4L2 subdev */ 227 struct v4l2_subdev sd; 228 struct media_pad pad[IMX7_CSI_PADS_NUM]; 229 230 struct v4l2_mbus_framefmt format_mbus[IMX7_CSI_PADS_NUM]; 231 const struct imx7_csi_pixfmt *cc[IMX7_CSI_PADS_NUM]; 232 233 /* Video device */ 234 struct video_device *vdev; /* Video device */ 235 struct media_pad vdev_pad; /* Video device pad */ 236 237 struct v4l2_pix_format vdev_fmt; /* The user format */ 238 const struct imx7_csi_pixfmt *vdev_cc; 239 struct v4l2_rect vdev_compose; /* The compose rectangle */ 240 241 struct mutex vdev_mutex; /* Protect vdev operations */ 242 243 struct vb2_queue q; /* The videobuf2 queue */ 244 struct list_head ready_q; /* List of queued buffers */ 245 spinlock_t q_lock; /* Protect ready_q */ 246 247 /* Buffers and streaming state */ 248 struct imx7_csi_vb2_buffer *active_vb2_buf[2]; 249 struct imx7_csi_dma_buf underrun_buf; 250 251 bool is_streaming; 252 int buf_num; 253 u32 frame_sequence; 254 255 bool last_eof; 256 struct completion last_eof_completion; 257 258 enum imx_csi_model model; 259 }; 260 261 static struct imx7_csi * 262 imx7_csi_notifier_to_dev(struct v4l2_async_notifier *n) 263 { 264 return container_of(n, struct imx7_csi, notifier); 265 } 266 267 /* ----------------------------------------------------------------------------- 268 * Hardware Configuration 269 */ 270 271 static u32 imx7_csi_reg_read(struct imx7_csi *csi, unsigned int offset) 272 { 273 return readl(csi->regbase + offset); 274 } 275 276 static void imx7_csi_reg_write(struct imx7_csi *csi, unsigned int value, 277 unsigned int offset) 278 { 279 writel(value, csi->regbase + offset); 280 } 281 282 static u32 imx7_csi_irq_clear(struct imx7_csi *csi) 283 { 284 u32 isr; 285 286 isr = imx7_csi_reg_read(csi, CSI_CSISR); 287 imx7_csi_reg_write(csi, isr, CSI_CSISR); 288 289 return isr; 290 } 291 292 static void imx7_csi_init_default(struct imx7_csi *csi) 293 { 294 imx7_csi_reg_write(csi, BIT_SOF_POL | BIT_REDGE | BIT_GCLK_MODE | 295 BIT_HSYNC_POL | BIT_FCC | BIT_MCLKDIV(1) | 296 BIT_MCLKEN, CSI_CSICR1); 297 imx7_csi_reg_write(csi, 0, CSI_CSICR2); 298 imx7_csi_reg_write(csi, BIT_FRMCNT_RST, CSI_CSICR3); 299 300 imx7_csi_reg_write(csi, BIT_IMAGE_WIDTH(IMX7_CSI_DEF_PIX_WIDTH) | 301 BIT_IMAGE_HEIGHT(IMX7_CSI_DEF_PIX_HEIGHT), 302 CSI_CSIIMAG_PARA); 303 304 imx7_csi_reg_write(csi, BIT_DMA_REFLASH_RFF, CSI_CSICR3); 305 } 306 307 static void imx7_csi_hw_enable_irq(struct imx7_csi *csi) 308 { 309 u32 cr1 = imx7_csi_reg_read(csi, CSI_CSICR1); 310 311 cr1 |= BIT_RFF_OR_INT; 312 cr1 |= BIT_FB1_DMA_DONE_INTEN; 313 cr1 |= BIT_FB2_DMA_DONE_INTEN; 314 315 imx7_csi_reg_write(csi, cr1, CSI_CSICR1); 316 } 317 318 static void imx7_csi_hw_disable_irq(struct imx7_csi *csi) 319 { 320 u32 cr1 = imx7_csi_reg_read(csi, CSI_CSICR1); 321 322 cr1 &= ~BIT_RFF_OR_INT; 323 cr1 &= ~BIT_FB1_DMA_DONE_INTEN; 324 cr1 &= ~BIT_FB2_DMA_DONE_INTEN; 325 326 imx7_csi_reg_write(csi, cr1, CSI_CSICR1); 327 } 328 329 static void imx7_csi_hw_enable(struct imx7_csi *csi) 330 { 331 u32 cr = imx7_csi_reg_read(csi, CSI_CSICR18); 332 333 cr |= BIT_CSI_HW_ENABLE; 334 335 imx7_csi_reg_write(csi, cr, CSI_CSICR18); 336 } 337 338 static void imx7_csi_hw_disable(struct imx7_csi *csi) 339 { 340 u32 cr = imx7_csi_reg_read(csi, CSI_CSICR18); 341 342 cr &= ~BIT_CSI_HW_ENABLE; 343 344 imx7_csi_reg_write(csi, cr, CSI_CSICR18); 345 } 346 347 static void imx7_csi_dma_reflash(struct imx7_csi *csi) 348 { 349 u32 cr3; 350 351 cr3 = imx7_csi_reg_read(csi, CSI_CSICR3); 352 cr3 |= BIT_DMA_REFLASH_RFF; 353 imx7_csi_reg_write(csi, cr3, CSI_CSICR3); 354 } 355 356 static void imx7_csi_rx_fifo_clear(struct imx7_csi *csi) 357 { 358 u32 cr1 = imx7_csi_reg_read(csi, CSI_CSICR1) & ~BIT_FCC; 359 360 imx7_csi_reg_write(csi, cr1, CSI_CSICR1); 361 imx7_csi_reg_write(csi, cr1 | BIT_CLR_RXFIFO, CSI_CSICR1); 362 imx7_csi_reg_write(csi, cr1 | BIT_FCC, CSI_CSICR1); 363 } 364 365 static void imx7_csi_dmareq_rff_enable(struct imx7_csi *csi) 366 { 367 u32 cr3 = imx7_csi_reg_read(csi, CSI_CSICR3); 368 369 cr3 |= BIT_DMA_REQ_EN_RFF; 370 cr3 |= BIT_HRESP_ERR_EN; 371 cr3 &= ~BIT_RXFF_LEVEL_MASK; 372 cr3 |= BIT_RXFF_LEVEL(2); 373 374 imx7_csi_reg_write(csi, cr3, CSI_CSICR3); 375 } 376 377 static void imx7_csi_dmareq_rff_disable(struct imx7_csi *csi) 378 { 379 u32 cr3 = imx7_csi_reg_read(csi, CSI_CSICR3); 380 381 cr3 &= ~BIT_DMA_REQ_EN_RFF; 382 cr3 &= ~BIT_HRESP_ERR_EN; 383 imx7_csi_reg_write(csi, cr3, CSI_CSICR3); 384 } 385 386 static void imx7_csi_update_buf(struct imx7_csi *csi, dma_addr_t dma_addr, 387 int buf_num) 388 { 389 if (buf_num == 1) 390 imx7_csi_reg_write(csi, dma_addr, CSI_CSIDMASA_FB2); 391 else 392 imx7_csi_reg_write(csi, dma_addr, CSI_CSIDMASA_FB1); 393 } 394 395 static struct imx7_csi_vb2_buffer *imx7_csi_video_next_buf(struct imx7_csi *csi); 396 397 static void imx7_csi_setup_vb2_buf(struct imx7_csi *csi) 398 { 399 struct imx7_csi_vb2_buffer *buf; 400 struct vb2_buffer *vb2_buf; 401 int i; 402 403 for (i = 0; i < 2; i++) { 404 dma_addr_t dma_addr; 405 406 buf = imx7_csi_video_next_buf(csi); 407 if (buf) { 408 csi->active_vb2_buf[i] = buf; 409 vb2_buf = &buf->vbuf.vb2_buf; 410 dma_addr = vb2_dma_contig_plane_dma_addr(vb2_buf, 0); 411 } else { 412 csi->active_vb2_buf[i] = NULL; 413 dma_addr = csi->underrun_buf.dma_addr; 414 } 415 416 imx7_csi_update_buf(csi, dma_addr, i); 417 } 418 } 419 420 static void imx7_csi_dma_unsetup_vb2_buf(struct imx7_csi *csi, 421 enum vb2_buffer_state return_status) 422 { 423 struct imx7_csi_vb2_buffer *buf; 424 int i; 425 426 /* return any remaining active frames with return_status */ 427 for (i = 0; i < 2; i++) { 428 buf = csi->active_vb2_buf[i]; 429 if (buf) { 430 struct vb2_buffer *vb = &buf->vbuf.vb2_buf; 431 432 vb->timestamp = ktime_get_ns(); 433 vb2_buffer_done(vb, return_status); 434 csi->active_vb2_buf[i] = NULL; 435 } 436 } 437 } 438 439 static void imx7_csi_free_dma_buf(struct imx7_csi *csi, 440 struct imx7_csi_dma_buf *buf) 441 { 442 if (buf->virt) 443 dma_free_coherent(csi->dev, buf->len, buf->virt, buf->dma_addr); 444 445 buf->virt = NULL; 446 buf->dma_addr = 0; 447 } 448 449 static int imx7_csi_alloc_dma_buf(struct imx7_csi *csi, 450 struct imx7_csi_dma_buf *buf, int size) 451 { 452 imx7_csi_free_dma_buf(csi, buf); 453 454 buf->len = PAGE_ALIGN(size); 455 buf->virt = dma_alloc_coherent(csi->dev, buf->len, &buf->dma_addr, 456 GFP_DMA | GFP_KERNEL); 457 if (!buf->virt) 458 return -ENOMEM; 459 460 return 0; 461 } 462 463 static int imx7_csi_dma_setup(struct imx7_csi *csi) 464 { 465 int ret; 466 467 ret = imx7_csi_alloc_dma_buf(csi, &csi->underrun_buf, 468 csi->vdev_fmt.sizeimage); 469 if (ret < 0) { 470 v4l2_warn(&csi->sd, "consider increasing the CMA area\n"); 471 return ret; 472 } 473 474 csi->frame_sequence = 0; 475 csi->last_eof = false; 476 init_completion(&csi->last_eof_completion); 477 478 imx7_csi_setup_vb2_buf(csi); 479 480 return 0; 481 } 482 483 static void imx7_csi_dma_cleanup(struct imx7_csi *csi, 484 enum vb2_buffer_state return_status) 485 { 486 imx7_csi_dma_unsetup_vb2_buf(csi, return_status); 487 imx7_csi_free_dma_buf(csi, &csi->underrun_buf); 488 } 489 490 static void imx7_csi_dma_stop(struct imx7_csi *csi) 491 { 492 unsigned long timeout_jiffies; 493 unsigned long flags; 494 int ret; 495 496 /* mark next EOF interrupt as the last before stream off */ 497 spin_lock_irqsave(&csi->irqlock, flags); 498 csi->last_eof = true; 499 spin_unlock_irqrestore(&csi->irqlock, flags); 500 501 /* 502 * and then wait for interrupt handler to mark completion. 503 */ 504 timeout_jiffies = msecs_to_jiffies(IMX7_CSI_VIDEO_EOF_TIMEOUT); 505 ret = wait_for_completion_timeout(&csi->last_eof_completion, 506 timeout_jiffies); 507 if (ret == 0) 508 v4l2_warn(&csi->sd, "wait last EOF timeout\n"); 509 510 imx7_csi_hw_disable_irq(csi); 511 } 512 513 static void imx7_csi_configure(struct imx7_csi *csi) 514 { 515 struct v4l2_pix_format *out_pix = &csi->vdev_fmt; 516 int width = out_pix->width; 517 u32 stride = 0; 518 u32 cr3 = BIT_FRMCNT_RST; 519 u32 cr1, cr18; 520 521 cr18 = imx7_csi_reg_read(csi, CSI_CSICR18); 522 523 cr18 &= ~(BIT_CSI_HW_ENABLE | BIT_MIPI_DATA_FORMAT_MASK | 524 BIT_DATA_FROM_MIPI | BIT_MIPI_DOUBLE_CMPNT | 525 BIT_BASEADDR_CHG_ERR_EN | BIT_BASEADDR_SWITCH_SEL | 526 BIT_BASEADDR_SWITCH_EN | BIT_DEINTERLACE_EN); 527 528 if (out_pix->field == V4L2_FIELD_INTERLACED) { 529 cr18 |= BIT_DEINTERLACE_EN; 530 stride = out_pix->width; 531 } 532 533 if (!csi->is_csi2) { 534 cr1 = BIT_SOF_POL | BIT_REDGE | BIT_GCLK_MODE | BIT_HSYNC_POL 535 | BIT_FCC | BIT_MCLKDIV(1) | BIT_MCLKEN; 536 537 cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL | 538 BIT_BASEADDR_CHG_ERR_EN; 539 540 if (out_pix->pixelformat == V4L2_PIX_FMT_UYVY || 541 out_pix->pixelformat == V4L2_PIX_FMT_YUYV) 542 width *= 2; 543 } else { 544 cr1 = BIT_SOF_POL | BIT_REDGE | BIT_HSYNC_POL | BIT_FCC 545 | BIT_MCLKDIV(1) | BIT_MCLKEN; 546 547 cr18 |= BIT_DATA_FROM_MIPI; 548 549 switch (csi->format_mbus[IMX7_CSI_PAD_SINK].code) { 550 case MEDIA_BUS_FMT_Y8_1X8: 551 case MEDIA_BUS_FMT_SBGGR8_1X8: 552 case MEDIA_BUS_FMT_SGBRG8_1X8: 553 case MEDIA_BUS_FMT_SGRBG8_1X8: 554 case MEDIA_BUS_FMT_SRGGB8_1X8: 555 cr18 |= BIT_MIPI_DATA_FORMAT_RAW8; 556 break; 557 case MEDIA_BUS_FMT_Y10_1X10: 558 case MEDIA_BUS_FMT_SBGGR10_1X10: 559 case MEDIA_BUS_FMT_SGBRG10_1X10: 560 case MEDIA_BUS_FMT_SGRBG10_1X10: 561 case MEDIA_BUS_FMT_SRGGB10_1X10: 562 cr3 |= BIT_TWO_8BIT_SENSOR; 563 cr18 |= BIT_MIPI_DATA_FORMAT_RAW10; 564 break; 565 case MEDIA_BUS_FMT_Y12_1X12: 566 case MEDIA_BUS_FMT_SBGGR12_1X12: 567 case MEDIA_BUS_FMT_SGBRG12_1X12: 568 case MEDIA_BUS_FMT_SGRBG12_1X12: 569 case MEDIA_BUS_FMT_SRGGB12_1X12: 570 cr3 |= BIT_TWO_8BIT_SENSOR; 571 cr18 |= BIT_MIPI_DATA_FORMAT_RAW12; 572 break; 573 case MEDIA_BUS_FMT_Y14_1X14: 574 case MEDIA_BUS_FMT_SBGGR14_1X14: 575 case MEDIA_BUS_FMT_SGBRG14_1X14: 576 case MEDIA_BUS_FMT_SGRBG14_1X14: 577 case MEDIA_BUS_FMT_SRGGB14_1X14: 578 cr3 |= BIT_TWO_8BIT_SENSOR; 579 cr18 |= BIT_MIPI_DATA_FORMAT_RAW14; 580 break; 581 582 /* 583 * The CSI bridge has a 16-bit input bus. Depending on the 584 * connected source, data may be transmitted with 8 or 10 bits 585 * per clock sample (in bits [9:2] or [9:0] respectively) or 586 * with 16 bits per clock sample (in bits [15:0]). The data is 587 * then packed into a 32-bit FIFO (as shown in figure 13-11 of 588 * the i.MX8MM reference manual rev. 3). 589 * 590 * The data packing in a 32-bit FIFO input word is controlled by 591 * the CR3 TWO_8BIT_SENSOR field (also known as SENSOR_16BITS in 592 * the i.MX8MM reference manual). When set to 0, data packing 593 * groups four 8-bit input samples (bits [9:2]). When set to 1, 594 * data packing groups two 16-bit input samples (bits [15:0]). 595 * 596 * The register field CR18 MIPI_DOUBLE_CMPNT also needs to be 597 * configured according to the input format for YUV 4:2:2 data. 598 * The field controls the gasket between the CSI-2 receiver and 599 * the CSI bridge. On i.MX7 and i.MX8MM, the field must be set 600 * to 1 when the CSIS outputs 16-bit samples. On i.MX8MQ, the 601 * gasket ignores the MIPI_DOUBLE_CMPNT bit and YUV 4:2:2 always 602 * uses 16-bit samples. Setting MIPI_DOUBLE_CMPNT in that case 603 * has no effect, but doesn't cause any issue. 604 */ 605 case MEDIA_BUS_FMT_UYVY8_2X8: 606 case MEDIA_BUS_FMT_YUYV8_2X8: 607 cr18 |= BIT_MIPI_DATA_FORMAT_YUV422_8B; 608 break; 609 case MEDIA_BUS_FMT_UYVY8_1X16: 610 case MEDIA_BUS_FMT_YUYV8_1X16: 611 cr3 |= BIT_TWO_8BIT_SENSOR; 612 cr18 |= BIT_MIPI_DATA_FORMAT_YUV422_8B | 613 BIT_MIPI_DOUBLE_CMPNT; 614 break; 615 } 616 } 617 618 imx7_csi_reg_write(csi, cr1, CSI_CSICR1); 619 imx7_csi_reg_write(csi, BIT_DMA_BURST_TYPE_RFF_INCR16, CSI_CSICR2); 620 imx7_csi_reg_write(csi, cr3, CSI_CSICR3); 621 imx7_csi_reg_write(csi, cr18, CSI_CSICR18); 622 623 imx7_csi_reg_write(csi, (width * out_pix->height) >> 2, CSI_CSIRXCNT); 624 imx7_csi_reg_write(csi, BIT_IMAGE_WIDTH(width) | 625 BIT_IMAGE_HEIGHT(out_pix->height), 626 CSI_CSIIMAG_PARA); 627 imx7_csi_reg_write(csi, stride, CSI_CSIFBUF_PARA); 628 } 629 630 static int imx7_csi_init(struct imx7_csi *csi) 631 { 632 int ret; 633 634 ret = clk_prepare_enable(csi->mclk); 635 if (ret < 0) 636 return ret; 637 638 imx7_csi_configure(csi); 639 640 ret = imx7_csi_dma_setup(csi); 641 if (ret < 0) { 642 clk_disable_unprepare(csi->mclk); 643 return ret; 644 } 645 646 return 0; 647 } 648 649 static void imx7_csi_deinit(struct imx7_csi *csi, 650 enum vb2_buffer_state return_status) 651 { 652 imx7_csi_dma_cleanup(csi, return_status); 653 imx7_csi_init_default(csi); 654 imx7_csi_dmareq_rff_disable(csi); 655 clk_disable_unprepare(csi->mclk); 656 } 657 658 static void imx7_csi_baseaddr_switch_on_second_frame(struct imx7_csi *csi) 659 { 660 u32 cr18 = imx7_csi_reg_read(csi, CSI_CSICR18); 661 662 cr18 |= BIT_BASEADDR_SWITCH_EN | BIT_BASEADDR_SWITCH_SEL | 663 BIT_BASEADDR_CHG_ERR_EN; 664 cr18 |= BIT_MASK_OPTION_SECOND_FRAME; 665 imx7_csi_reg_write(csi, cr18, CSI_CSICR18); 666 } 667 668 static void imx7_csi_enable(struct imx7_csi *csi) 669 { 670 /* Clear the Rx FIFO and reflash the DMA controller. */ 671 imx7_csi_rx_fifo_clear(csi); 672 imx7_csi_dma_reflash(csi); 673 674 usleep_range(2000, 3000); 675 676 /* Clear and enable the interrupts. */ 677 imx7_csi_irq_clear(csi); 678 imx7_csi_hw_enable_irq(csi); 679 680 /* Enable the RxFIFO DMA and the CSI. */ 681 imx7_csi_dmareq_rff_enable(csi); 682 imx7_csi_hw_enable(csi); 683 684 if (csi->model == IMX7_CSI_IMX8MQ) 685 imx7_csi_baseaddr_switch_on_second_frame(csi); 686 } 687 688 static void imx7_csi_disable(struct imx7_csi *csi) 689 { 690 imx7_csi_dma_stop(csi); 691 692 imx7_csi_dmareq_rff_disable(csi); 693 694 imx7_csi_hw_disable_irq(csi); 695 696 imx7_csi_hw_disable(csi); 697 } 698 699 /* ----------------------------------------------------------------------------- 700 * Interrupt Handling 701 */ 702 703 static void imx7_csi_error_recovery(struct imx7_csi *csi) 704 { 705 imx7_csi_hw_disable(csi); 706 707 imx7_csi_rx_fifo_clear(csi); 708 709 imx7_csi_dma_reflash(csi); 710 711 imx7_csi_hw_enable(csi); 712 } 713 714 static void imx7_csi_vb2_buf_done(struct imx7_csi *csi) 715 { 716 struct imx7_csi_vb2_buffer *done, *next; 717 struct vb2_buffer *vb; 718 dma_addr_t dma_addr; 719 720 done = csi->active_vb2_buf[csi->buf_num]; 721 if (done) { 722 done->vbuf.field = csi->vdev_fmt.field; 723 done->vbuf.sequence = csi->frame_sequence; 724 vb = &done->vbuf.vb2_buf; 725 vb->timestamp = ktime_get_ns(); 726 vb2_buffer_done(vb, VB2_BUF_STATE_DONE); 727 } 728 csi->frame_sequence++; 729 730 /* get next queued buffer */ 731 next = imx7_csi_video_next_buf(csi); 732 if (next) { 733 dma_addr = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0); 734 csi->active_vb2_buf[csi->buf_num] = next; 735 } else { 736 dma_addr = csi->underrun_buf.dma_addr; 737 csi->active_vb2_buf[csi->buf_num] = NULL; 738 } 739 740 imx7_csi_update_buf(csi, dma_addr, csi->buf_num); 741 } 742 743 static irqreturn_t imx7_csi_irq_handler(int irq, void *data) 744 { 745 struct imx7_csi *csi = data; 746 u32 status; 747 748 spin_lock(&csi->irqlock); 749 750 status = imx7_csi_irq_clear(csi); 751 752 if (status & BIT_RFF_OR_INT) { 753 dev_warn(csi->dev, "Rx fifo overflow\n"); 754 imx7_csi_error_recovery(csi); 755 } 756 757 if (status & BIT_HRESP_ERR_INT) { 758 dev_warn(csi->dev, "Hresponse error detected\n"); 759 imx7_csi_error_recovery(csi); 760 } 761 762 if (status & BIT_ADDR_CH_ERR_INT) { 763 imx7_csi_hw_disable(csi); 764 765 imx7_csi_dma_reflash(csi); 766 767 imx7_csi_hw_enable(csi); 768 } 769 770 if ((status & BIT_DMA_TSF_DONE_FB1) && 771 (status & BIT_DMA_TSF_DONE_FB2)) { 772 /* 773 * For both FB1 and FB2 interrupter bits set case, 774 * CSI DMA is work in one of FB1 and FB2 buffer, 775 * but software can not know the state. 776 * Skip it to avoid base address updated 777 * when csi work in field0 and field1 will write to 778 * new base address. 779 */ 780 } else if (status & BIT_DMA_TSF_DONE_FB1) { 781 csi->buf_num = 0; 782 } else if (status & BIT_DMA_TSF_DONE_FB2) { 783 csi->buf_num = 1; 784 } 785 786 if ((status & BIT_DMA_TSF_DONE_FB1) || 787 (status & BIT_DMA_TSF_DONE_FB2)) { 788 imx7_csi_vb2_buf_done(csi); 789 790 if (csi->last_eof) { 791 complete(&csi->last_eof_completion); 792 csi->last_eof = false; 793 } 794 } 795 796 spin_unlock(&csi->irqlock); 797 798 return IRQ_HANDLED; 799 } 800 801 /* ----------------------------------------------------------------------------- 802 * Format Helpers 803 */ 804 805 #define IMX_BUS_FMTS(fmt...) (const u32[]) {fmt, 0} 806 807 /* 808 * List of supported pixel formats for the subdevs. Keep V4L2_PIX_FMT_UYVY and 809 * MEDIA_BUS_FMT_UYVY8_2X8 first to match IMX7_CSI_DEF_PIX_FORMAT and 810 * IMX7_CSI_DEF_MBUS_CODE. 811 * 812 * TODO: Restrict the supported formats list based on the SoC integration. 813 * 814 * The CSI bridge can be configured to sample pixel components from the Rx queue 815 * in single (8bpp) or double (16bpp) component modes. Image format variants 816 * with different sample sizes (ie YUYV_2X8 vs YUYV_1X16) determine the pixel 817 * components sampling size per each clock cycle and their packing mode (see 818 * imx7_csi_configure() for details). 819 * 820 * As the CSI bridge can be interfaced with different IP blocks depending on the 821 * SoC model it is integrated on, the Rx queue sampling size should match the 822 * size of the samples transferred by the transmitting IP block. To avoid 823 * misconfigurations of the capture pipeline, the enumeration of the supported 824 * formats should be restricted to match the pixel source transmitting mode. 825 * 826 * Example: i.MX8MM SoC integrates the CSI bridge with the Samsung CSIS CSI-2 827 * receiver which operates in dual pixel sampling mode. The CSI bridge should 828 * only expose the 1X16 formats variant which instructs it to operate in dual 829 * pixel sampling mode. When the CSI bridge is instead integrated on an i.MX7, 830 * which supports both serial and parallel input, it should expose both 831 * variants. 832 * 833 * This currently only applies to YUYV formats, but other formats might need to 834 * be handled in the same way. 835 */ 836 static const struct imx7_csi_pixfmt pixel_formats[] = { 837 /*** YUV formats start here ***/ 838 { 839 .fourcc = V4L2_PIX_FMT_UYVY, 840 .codes = IMX_BUS_FMTS( 841 MEDIA_BUS_FMT_UYVY8_2X8, 842 MEDIA_BUS_FMT_UYVY8_1X16 843 ), 844 .yuv = true, 845 .bpp = 16, 846 }, { 847 .fourcc = V4L2_PIX_FMT_YUYV, 848 .codes = IMX_BUS_FMTS( 849 MEDIA_BUS_FMT_YUYV8_2X8, 850 MEDIA_BUS_FMT_YUYV8_1X16 851 ), 852 .yuv = true, 853 .bpp = 16, 854 }, 855 /*** raw bayer and grayscale formats start here ***/ 856 { 857 .fourcc = V4L2_PIX_FMT_SBGGR8, 858 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR8_1X8), 859 .bpp = 8, 860 }, { 861 .fourcc = V4L2_PIX_FMT_SGBRG8, 862 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG8_1X8), 863 .bpp = 8, 864 }, { 865 .fourcc = V4L2_PIX_FMT_SGRBG8, 866 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG8_1X8), 867 .bpp = 8, 868 }, { 869 .fourcc = V4L2_PIX_FMT_SRGGB8, 870 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB8_1X8), 871 .bpp = 8, 872 }, { 873 .fourcc = V4L2_PIX_FMT_SBGGR10, 874 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR10_1X10), 875 .bpp = 16, 876 }, { 877 .fourcc = V4L2_PIX_FMT_SGBRG10, 878 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG10_1X10), 879 .bpp = 16, 880 }, { 881 .fourcc = V4L2_PIX_FMT_SGRBG10, 882 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG10_1X10), 883 .bpp = 16, 884 }, { 885 .fourcc = V4L2_PIX_FMT_SRGGB10, 886 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB10_1X10), 887 .bpp = 16, 888 }, { 889 .fourcc = V4L2_PIX_FMT_SBGGR12, 890 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR12_1X12), 891 .bpp = 16, 892 }, { 893 .fourcc = V4L2_PIX_FMT_SGBRG12, 894 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG12_1X12), 895 .bpp = 16, 896 }, { 897 .fourcc = V4L2_PIX_FMT_SGRBG12, 898 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG12_1X12), 899 .bpp = 16, 900 }, { 901 .fourcc = V4L2_PIX_FMT_SRGGB12, 902 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB12_1X12), 903 .bpp = 16, 904 }, { 905 .fourcc = V4L2_PIX_FMT_SBGGR14, 906 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SBGGR14_1X14), 907 .bpp = 16, 908 }, { 909 .fourcc = V4L2_PIX_FMT_SGBRG14, 910 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGBRG14_1X14), 911 .bpp = 16, 912 }, { 913 .fourcc = V4L2_PIX_FMT_SGRBG14, 914 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SGRBG14_1X14), 915 .bpp = 16, 916 }, { 917 .fourcc = V4L2_PIX_FMT_SRGGB14, 918 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_SRGGB14_1X14), 919 .bpp = 16, 920 }, { 921 .fourcc = V4L2_PIX_FMT_GREY, 922 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y8_1X8), 923 .bpp = 8, 924 }, { 925 .fourcc = V4L2_PIX_FMT_Y10, 926 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y10_1X10), 927 .bpp = 16, 928 }, { 929 .fourcc = V4L2_PIX_FMT_Y12, 930 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y12_1X12), 931 .bpp = 16, 932 }, { 933 .fourcc = V4L2_PIX_FMT_Y14, 934 .codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y14_1X14), 935 .bpp = 16, 936 }, 937 }; 938 939 /* 940 * Search in the pixel_formats[] array for an entry with the given fourcc 941 * return it. 942 */ 943 static const struct imx7_csi_pixfmt *imx7_csi_find_pixel_format(u32 fourcc) 944 { 945 unsigned int i; 946 947 for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) { 948 const struct imx7_csi_pixfmt *fmt = &pixel_formats[i]; 949 950 if (fmt->fourcc == fourcc) 951 return fmt; 952 } 953 954 return NULL; 955 } 956 957 /* 958 * Search in the pixel_formats[] array for an entry with the given media 959 * bus code and return it. 960 */ 961 static const struct imx7_csi_pixfmt *imx7_csi_find_mbus_format(u32 code) 962 { 963 unsigned int i; 964 965 for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) { 966 const struct imx7_csi_pixfmt *fmt = &pixel_formats[i]; 967 unsigned int j; 968 969 if (!fmt->codes) 970 continue; 971 972 for (j = 0; fmt->codes[j]; j++) { 973 if (code == fmt->codes[j]) 974 return fmt; 975 } 976 } 977 978 return NULL; 979 } 980 981 /* 982 * Enumerate entries in the pixel_formats[] array that match the 983 * requested search criteria. Return the media-bus code that matches 984 * the search criteria at the requested match index. 985 * 986 * @code: The returned media-bus code that matches the search criteria at 987 * the requested match index. 988 * @index: The requested match index. 989 */ 990 static int imx7_csi_enum_mbus_formats(u32 *code, u32 index) 991 { 992 unsigned int i; 993 994 for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) { 995 const struct imx7_csi_pixfmt *fmt = &pixel_formats[i]; 996 unsigned int j; 997 998 if (!fmt->codes) 999 continue; 1000 1001 for (j = 0; fmt->codes[j]; j++) { 1002 if (index == 0) { 1003 *code = fmt->codes[j]; 1004 return 0; 1005 } 1006 1007 index--; 1008 } 1009 } 1010 1011 return -EINVAL; 1012 } 1013 1014 static int imx7_csi_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix, 1015 const struct v4l2_mbus_framefmt *mbus, 1016 const struct imx7_csi_pixfmt *cc) 1017 { 1018 u32 width; 1019 u32 stride; 1020 1021 if (!cc) { 1022 cc = imx7_csi_find_mbus_format(mbus->code); 1023 if (!cc) 1024 return -EINVAL; 1025 } 1026 1027 /* Round up width for minimum burst size */ 1028 width = round_up(mbus->width, 8); 1029 1030 /* Round up stride for IDMAC line start address alignment */ 1031 stride = round_up((width * cc->bpp) >> 3, 8); 1032 1033 pix->width = width; 1034 pix->height = mbus->height; 1035 pix->pixelformat = cc->fourcc; 1036 pix->colorspace = mbus->colorspace; 1037 pix->xfer_func = mbus->xfer_func; 1038 pix->ycbcr_enc = mbus->ycbcr_enc; 1039 pix->quantization = mbus->quantization; 1040 pix->field = mbus->field; 1041 pix->bytesperline = stride; 1042 pix->sizeimage = stride * pix->height; 1043 1044 return 0; 1045 } 1046 1047 /* ----------------------------------------------------------------------------- 1048 * Video Capture Device - IOCTLs 1049 */ 1050 1051 static int imx7_csi_video_querycap(struct file *file, void *fh, 1052 struct v4l2_capability *cap) 1053 { 1054 struct imx7_csi *csi = video_drvdata(file); 1055 1056 strscpy(cap->driver, IMX7_CSI_VIDEO_NAME, sizeof(cap->driver)); 1057 strscpy(cap->card, IMX7_CSI_VIDEO_NAME, sizeof(cap->card)); 1058 snprintf(cap->bus_info, sizeof(cap->bus_info), 1059 "platform:%s", dev_name(csi->dev)); 1060 1061 return 0; 1062 } 1063 1064 static int imx7_csi_video_enum_fmt_vid_cap(struct file *file, void *fh, 1065 struct v4l2_fmtdesc *f) 1066 { 1067 unsigned int index = f->index; 1068 unsigned int i; 1069 1070 for (i = 0; i < ARRAY_SIZE(pixel_formats); i++) { 1071 const struct imx7_csi_pixfmt *fmt = &pixel_formats[i]; 1072 1073 /* 1074 * If a media bus code is specified, only consider formats that 1075 * match it. 1076 */ 1077 if (f->mbus_code) { 1078 unsigned int j; 1079 1080 if (!fmt->codes) 1081 continue; 1082 1083 for (j = 0; fmt->codes[j]; j++) { 1084 if (f->mbus_code == fmt->codes[j]) 1085 break; 1086 } 1087 1088 if (!fmt->codes[j]) 1089 continue; 1090 } 1091 1092 if (index == 0) { 1093 f->pixelformat = fmt->fourcc; 1094 return 0; 1095 } 1096 1097 index--; 1098 } 1099 1100 return -EINVAL; 1101 } 1102 1103 static int imx7_csi_video_enum_framesizes(struct file *file, void *fh, 1104 struct v4l2_frmsizeenum *fsize) 1105 { 1106 const struct imx7_csi_pixfmt *cc; 1107 1108 if (fsize->index > 0) 1109 return -EINVAL; 1110 1111 cc = imx7_csi_find_pixel_format(fsize->pixel_format); 1112 if (!cc) 1113 return -EINVAL; 1114 1115 /* 1116 * TODO: The constraints are hardware-specific and may depend on the 1117 * pixel format. This should come from the driver using 1118 * imx_media_capture. 1119 */ 1120 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS; 1121 fsize->stepwise.min_width = 1; 1122 fsize->stepwise.max_width = 65535; 1123 fsize->stepwise.min_height = 1; 1124 fsize->stepwise.max_height = 65535; 1125 fsize->stepwise.step_width = 1; 1126 fsize->stepwise.step_height = 1; 1127 1128 return 0; 1129 } 1130 1131 static int imx7_csi_video_g_fmt_vid_cap(struct file *file, void *fh, 1132 struct v4l2_format *f) 1133 { 1134 struct imx7_csi *csi = video_drvdata(file); 1135 1136 f->fmt.pix = csi->vdev_fmt; 1137 1138 return 0; 1139 } 1140 1141 static const struct imx7_csi_pixfmt * 1142 __imx7_csi_video_try_fmt(struct v4l2_pix_format *pixfmt, 1143 struct v4l2_rect *compose) 1144 { 1145 struct v4l2_mbus_framefmt fmt_src; 1146 const struct imx7_csi_pixfmt *cc; 1147 1148 /* 1149 * Find the pixel format, default to the first supported format if not 1150 * found. 1151 */ 1152 cc = imx7_csi_find_pixel_format(pixfmt->pixelformat); 1153 if (!cc) { 1154 pixfmt->pixelformat = IMX7_CSI_DEF_PIX_FORMAT; 1155 cc = imx7_csi_find_pixel_format(pixfmt->pixelformat); 1156 } 1157 1158 /* Allow IDMAC interweave but enforce field order from source. */ 1159 if (V4L2_FIELD_IS_INTERLACED(pixfmt->field)) { 1160 switch (pixfmt->field) { 1161 case V4L2_FIELD_SEQ_TB: 1162 pixfmt->field = V4L2_FIELD_INTERLACED_TB; 1163 break; 1164 case V4L2_FIELD_SEQ_BT: 1165 pixfmt->field = V4L2_FIELD_INTERLACED_BT; 1166 break; 1167 default: 1168 break; 1169 } 1170 } 1171 1172 v4l2_fill_mbus_format(&fmt_src, pixfmt, 0); 1173 imx7_csi_mbus_fmt_to_pix_fmt(pixfmt, &fmt_src, cc); 1174 1175 if (compose) { 1176 compose->width = fmt_src.width; 1177 compose->height = fmt_src.height; 1178 } 1179 1180 return cc; 1181 } 1182 1183 static int imx7_csi_video_try_fmt_vid_cap(struct file *file, void *fh, 1184 struct v4l2_format *f) 1185 { 1186 __imx7_csi_video_try_fmt(&f->fmt.pix, NULL); 1187 return 0; 1188 } 1189 1190 static int imx7_csi_video_s_fmt_vid_cap(struct file *file, void *fh, 1191 struct v4l2_format *f) 1192 { 1193 struct imx7_csi *csi = video_drvdata(file); 1194 const struct imx7_csi_pixfmt *cc; 1195 1196 if (vb2_is_busy(&csi->q)) { 1197 dev_err(csi->dev, "%s queue busy\n", __func__); 1198 return -EBUSY; 1199 } 1200 1201 cc = __imx7_csi_video_try_fmt(&f->fmt.pix, &csi->vdev_compose); 1202 1203 csi->vdev_cc = cc; 1204 csi->vdev_fmt = f->fmt.pix; 1205 1206 return 0; 1207 } 1208 1209 static int imx7_csi_video_g_selection(struct file *file, void *fh, 1210 struct v4l2_selection *s) 1211 { 1212 struct imx7_csi *csi = video_drvdata(file); 1213 1214 switch (s->target) { 1215 case V4L2_SEL_TGT_COMPOSE: 1216 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 1217 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 1218 /* The compose rectangle is fixed to the source format. */ 1219 s->r = csi->vdev_compose; 1220 break; 1221 case V4L2_SEL_TGT_COMPOSE_PADDED: 1222 /* 1223 * The hardware writes with a configurable but fixed DMA burst 1224 * size. If the source format width is not burst size aligned, 1225 * the written frame contains padding to the right. 1226 */ 1227 s->r.left = 0; 1228 s->r.top = 0; 1229 s->r.width = csi->vdev_fmt.width; 1230 s->r.height = csi->vdev_fmt.height; 1231 break; 1232 default: 1233 return -EINVAL; 1234 } 1235 1236 return 0; 1237 } 1238 1239 static const struct v4l2_ioctl_ops imx7_csi_video_ioctl_ops = { 1240 .vidioc_querycap = imx7_csi_video_querycap, 1241 1242 .vidioc_enum_fmt_vid_cap = imx7_csi_video_enum_fmt_vid_cap, 1243 .vidioc_enum_framesizes = imx7_csi_video_enum_framesizes, 1244 1245 .vidioc_g_fmt_vid_cap = imx7_csi_video_g_fmt_vid_cap, 1246 .vidioc_try_fmt_vid_cap = imx7_csi_video_try_fmt_vid_cap, 1247 .vidioc_s_fmt_vid_cap = imx7_csi_video_s_fmt_vid_cap, 1248 1249 .vidioc_g_selection = imx7_csi_video_g_selection, 1250 1251 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1252 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1253 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1254 .vidioc_querybuf = vb2_ioctl_querybuf, 1255 .vidioc_qbuf = vb2_ioctl_qbuf, 1256 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1257 .vidioc_expbuf = vb2_ioctl_expbuf, 1258 .vidioc_streamon = vb2_ioctl_streamon, 1259 .vidioc_streamoff = vb2_ioctl_streamoff, 1260 }; 1261 1262 /* ----------------------------------------------------------------------------- 1263 * Video Capture Device - Queue Operations 1264 */ 1265 1266 static int imx7_csi_video_queue_setup(struct vb2_queue *vq, 1267 unsigned int *nbuffers, 1268 unsigned int *nplanes, 1269 unsigned int sizes[], 1270 struct device *alloc_devs[]) 1271 { 1272 struct imx7_csi *csi = vb2_get_drv_priv(vq); 1273 struct v4l2_pix_format *pix = &csi->vdev_fmt; 1274 unsigned int count = *nbuffers; 1275 1276 if (vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1277 return -EINVAL; 1278 1279 if (*nplanes) { 1280 if (*nplanes != 1 || sizes[0] < pix->sizeimage) 1281 return -EINVAL; 1282 count += vq->num_buffers; 1283 } 1284 1285 count = min_t(__u32, IMX7_CSI_VIDEO_MEM_LIMIT / pix->sizeimage, count); 1286 1287 if (*nplanes) 1288 *nbuffers = (count < vq->num_buffers) ? 0 : 1289 count - vq->num_buffers; 1290 else 1291 *nbuffers = count; 1292 1293 *nplanes = 1; 1294 sizes[0] = pix->sizeimage; 1295 1296 return 0; 1297 } 1298 1299 static int imx7_csi_video_buf_init(struct vb2_buffer *vb) 1300 { 1301 struct imx7_csi_vb2_buffer *buf = to_imx7_csi_vb2_buffer(vb); 1302 1303 INIT_LIST_HEAD(&buf->list); 1304 1305 return 0; 1306 } 1307 1308 static int imx7_csi_video_buf_prepare(struct vb2_buffer *vb) 1309 { 1310 struct imx7_csi *csi = vb2_get_drv_priv(vb->vb2_queue); 1311 struct v4l2_pix_format *pix = &csi->vdev_fmt; 1312 1313 if (vb2_plane_size(vb, 0) < pix->sizeimage) { 1314 dev_err(csi->dev, 1315 "data will not fit into plane (%lu < %lu)\n", 1316 vb2_plane_size(vb, 0), (long)pix->sizeimage); 1317 return -EINVAL; 1318 } 1319 1320 vb2_set_plane_payload(vb, 0, pix->sizeimage); 1321 1322 return 0; 1323 } 1324 1325 static bool imx7_csi_fast_track_buffer(struct imx7_csi *csi, 1326 struct imx7_csi_vb2_buffer *buf) 1327 { 1328 unsigned long flags; 1329 dma_addr_t dma_addr; 1330 int buf_num; 1331 u32 isr; 1332 1333 if (!csi->is_streaming) 1334 return false; 1335 1336 dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vbuf.vb2_buf, 0); 1337 1338 /* 1339 * buf_num holds the framebuffer ID of the most recently (*not* the 1340 * next anticipated) triggered interrupt. Without loss of generality, 1341 * if buf_num is 0, the hardware is capturing to FB2. If FB1 has been 1342 * programmed with a dummy buffer (as indicated by active_vb2_buf[0] 1343 * being NULL), then we can fast-track the new buffer by programming 1344 * its address in FB1 before the hardware completes FB2, instead of 1345 * adding it to the buffer queue and incurring a delay of one 1346 * additional frame. 1347 * 1348 * The irqlock prevents races with the interrupt handler that updates 1349 * buf_num when it programs the next buffer, but we can still race with 1350 * the hardware if we program the buffer in FB1 just after the hardware 1351 * completes FB2 and switches to FB1 and before buf_num can be updated 1352 * by the interrupt handler for FB2. The fast-tracked buffer would 1353 * then be ignored by the hardware while the driver would think it has 1354 * successfully been processed. 1355 * 1356 * To avoid this problem, if we can't avoid the race, we can detect 1357 * that we have lost it by checking, after programming the buffer in 1358 * FB1, if the interrupt flag indicating completion of FB2 has been 1359 * raised. If that is not the case, fast-tracking succeeded, and we can 1360 * update active_vb2_buf[0]. Otherwise, we may or may not have lost the 1361 * race (as the interrupt flag may have been raised just after 1362 * programming FB1 and before we read the interrupt status register), 1363 * and we need to assume the worst case of a race loss and queue the 1364 * buffer through the slow path. 1365 */ 1366 1367 spin_lock_irqsave(&csi->irqlock, flags); 1368 1369 buf_num = csi->buf_num; 1370 if (csi->active_vb2_buf[buf_num]) { 1371 spin_unlock_irqrestore(&csi->irqlock, flags); 1372 return false; 1373 } 1374 1375 imx7_csi_update_buf(csi, dma_addr, buf_num); 1376 1377 isr = imx7_csi_reg_read(csi, CSI_CSISR); 1378 if (isr & (buf_num ? BIT_DMA_TSF_DONE_FB1 : BIT_DMA_TSF_DONE_FB2)) { 1379 /* 1380 * The interrupt for the /other/ FB just came (the isr hasn't 1381 * run yet though, because we have the lock here); we can't be 1382 * sure we've programmed buf_num FB in time, so queue the buffer 1383 * to the buffer queue normally. No need to undo writing the FB 1384 * register, since we won't return it as active_vb2_buf is NULL, 1385 * so it's okay to potentially write it to both FB1 and FB2; 1386 * only the one where it was queued normally will be returned. 1387 */ 1388 spin_unlock_irqrestore(&csi->irqlock, flags); 1389 return false; 1390 } 1391 1392 csi->active_vb2_buf[buf_num] = buf; 1393 1394 spin_unlock_irqrestore(&csi->irqlock, flags); 1395 return true; 1396 } 1397 1398 static void imx7_csi_video_buf_queue(struct vb2_buffer *vb) 1399 { 1400 struct imx7_csi *csi = vb2_get_drv_priv(vb->vb2_queue); 1401 struct imx7_csi_vb2_buffer *buf = to_imx7_csi_vb2_buffer(vb); 1402 unsigned long flags; 1403 1404 if (imx7_csi_fast_track_buffer(csi, buf)) 1405 return; 1406 1407 spin_lock_irqsave(&csi->q_lock, flags); 1408 1409 list_add_tail(&buf->list, &csi->ready_q); 1410 1411 spin_unlock_irqrestore(&csi->q_lock, flags); 1412 } 1413 1414 static int imx7_csi_video_validate_fmt(struct imx7_csi *csi) 1415 { 1416 struct v4l2_subdev_format fmt_src; 1417 const struct imx7_csi_pixfmt *cc; 1418 int ret; 1419 1420 /* Retrieve the media bus format on the source subdev. */ 1421 fmt_src.pad = IMX7_CSI_PAD_SRC; 1422 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE; 1423 ret = v4l2_subdev_call(&csi->sd, pad, get_fmt, NULL, &fmt_src); 1424 if (ret) 1425 return ret; 1426 1427 /* 1428 * Verify that the media bus size matches the size set on the video 1429 * node. It is sufficient to check the compose rectangle size without 1430 * checking the rounded size from pix_fmt, as the rounded size is 1431 * derived directly from the compose rectangle size, and will thus 1432 * always match if the compose rectangle matches. 1433 */ 1434 if (csi->vdev_compose.width != fmt_src.format.width || 1435 csi->vdev_compose.height != fmt_src.format.height) 1436 return -EPIPE; 1437 1438 /* 1439 * Verify that the media bus code is compatible with the pixel format 1440 * set on the video node. 1441 */ 1442 cc = imx7_csi_find_mbus_format(fmt_src.format.code); 1443 if (!cc || csi->vdev_cc->yuv != cc->yuv) 1444 return -EPIPE; 1445 1446 return 0; 1447 } 1448 1449 static int imx7_csi_video_start_streaming(struct vb2_queue *vq, 1450 unsigned int count) 1451 { 1452 struct imx7_csi *csi = vb2_get_drv_priv(vq); 1453 struct imx7_csi_vb2_buffer *buf, *tmp; 1454 unsigned long flags; 1455 int ret; 1456 1457 ret = imx7_csi_video_validate_fmt(csi); 1458 if (ret) { 1459 dev_err(csi->dev, "capture format not valid\n"); 1460 goto err_buffers; 1461 } 1462 1463 mutex_lock(&csi->mdev.graph_mutex); 1464 1465 ret = __video_device_pipeline_start(csi->vdev, &csi->pipe); 1466 if (ret) 1467 goto err_unlock; 1468 1469 ret = v4l2_subdev_call(&csi->sd, video, s_stream, 1); 1470 if (ret) 1471 goto err_stop; 1472 1473 mutex_unlock(&csi->mdev.graph_mutex); 1474 1475 return 0; 1476 1477 err_stop: 1478 __video_device_pipeline_stop(csi->vdev); 1479 err_unlock: 1480 mutex_unlock(&csi->mdev.graph_mutex); 1481 dev_err(csi->dev, "pipeline start failed with %d\n", ret); 1482 err_buffers: 1483 spin_lock_irqsave(&csi->q_lock, flags); 1484 list_for_each_entry_safe(buf, tmp, &csi->ready_q, list) { 1485 list_del(&buf->list); 1486 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED); 1487 } 1488 spin_unlock_irqrestore(&csi->q_lock, flags); 1489 return ret; 1490 } 1491 1492 static void imx7_csi_video_stop_streaming(struct vb2_queue *vq) 1493 { 1494 struct imx7_csi *csi = vb2_get_drv_priv(vq); 1495 struct imx7_csi_vb2_buffer *frame; 1496 struct imx7_csi_vb2_buffer *tmp; 1497 unsigned long flags; 1498 1499 mutex_lock(&csi->mdev.graph_mutex); 1500 v4l2_subdev_call(&csi->sd, video, s_stream, 0); 1501 __video_device_pipeline_stop(csi->vdev); 1502 mutex_unlock(&csi->mdev.graph_mutex); 1503 1504 /* release all active buffers */ 1505 spin_lock_irqsave(&csi->q_lock, flags); 1506 list_for_each_entry_safe(frame, tmp, &csi->ready_q, list) { 1507 list_del(&frame->list); 1508 vb2_buffer_done(&frame->vbuf.vb2_buf, VB2_BUF_STATE_ERROR); 1509 } 1510 spin_unlock_irqrestore(&csi->q_lock, flags); 1511 } 1512 1513 static const struct vb2_ops imx7_csi_video_qops = { 1514 .queue_setup = imx7_csi_video_queue_setup, 1515 .buf_init = imx7_csi_video_buf_init, 1516 .buf_prepare = imx7_csi_video_buf_prepare, 1517 .buf_queue = imx7_csi_video_buf_queue, 1518 .wait_prepare = vb2_ops_wait_prepare, 1519 .wait_finish = vb2_ops_wait_finish, 1520 .start_streaming = imx7_csi_video_start_streaming, 1521 .stop_streaming = imx7_csi_video_stop_streaming, 1522 }; 1523 1524 /* ----------------------------------------------------------------------------- 1525 * Video Capture Device - File Operations 1526 */ 1527 1528 static int imx7_csi_video_open(struct file *file) 1529 { 1530 struct imx7_csi *csi = video_drvdata(file); 1531 int ret; 1532 1533 if (mutex_lock_interruptible(&csi->vdev_mutex)) 1534 return -ERESTARTSYS; 1535 1536 ret = v4l2_fh_open(file); 1537 if (ret) { 1538 dev_err(csi->dev, "v4l2_fh_open failed\n"); 1539 goto out; 1540 } 1541 1542 ret = v4l2_pipeline_pm_get(&csi->vdev->entity); 1543 if (ret) 1544 v4l2_fh_release(file); 1545 1546 out: 1547 mutex_unlock(&csi->vdev_mutex); 1548 return ret; 1549 } 1550 1551 static int imx7_csi_video_release(struct file *file) 1552 { 1553 struct imx7_csi *csi = video_drvdata(file); 1554 struct vb2_queue *vq = &csi->q; 1555 1556 mutex_lock(&csi->vdev_mutex); 1557 1558 if (file->private_data == vq->owner) { 1559 vb2_queue_release(vq); 1560 vq->owner = NULL; 1561 } 1562 1563 v4l2_pipeline_pm_put(&csi->vdev->entity); 1564 1565 v4l2_fh_release(file); 1566 mutex_unlock(&csi->vdev_mutex); 1567 return 0; 1568 } 1569 1570 static const struct v4l2_file_operations imx7_csi_video_fops = { 1571 .owner = THIS_MODULE, 1572 .open = imx7_csi_video_open, 1573 .release = imx7_csi_video_release, 1574 .poll = vb2_fop_poll, 1575 .unlocked_ioctl = video_ioctl2, 1576 .mmap = vb2_fop_mmap, 1577 }; 1578 1579 /* ----------------------------------------------------------------------------- 1580 * Video Capture Device - Init & Cleanup 1581 */ 1582 1583 static struct imx7_csi_vb2_buffer *imx7_csi_video_next_buf(struct imx7_csi *csi) 1584 { 1585 struct imx7_csi_vb2_buffer *buf = NULL; 1586 unsigned long flags; 1587 1588 spin_lock_irqsave(&csi->q_lock, flags); 1589 1590 /* get next queued buffer */ 1591 if (!list_empty(&csi->ready_q)) { 1592 buf = list_entry(csi->ready_q.next, struct imx7_csi_vb2_buffer, 1593 list); 1594 list_del(&buf->list); 1595 } 1596 1597 spin_unlock_irqrestore(&csi->q_lock, flags); 1598 1599 return buf; 1600 } 1601 1602 static int imx7_csi_video_init_format(struct imx7_csi *csi) 1603 { 1604 struct v4l2_subdev_format fmt_src = { 1605 .pad = IMX7_CSI_PAD_SRC, 1606 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 1607 }; 1608 fmt_src.format.code = IMX7_CSI_DEF_MBUS_CODE; 1609 fmt_src.format.width = IMX7_CSI_DEF_PIX_WIDTH; 1610 fmt_src.format.height = IMX7_CSI_DEF_PIX_HEIGHT; 1611 1612 imx7_csi_mbus_fmt_to_pix_fmt(&csi->vdev_fmt, &fmt_src.format, NULL); 1613 csi->vdev_compose.width = fmt_src.format.width; 1614 csi->vdev_compose.height = fmt_src.format.height; 1615 1616 csi->vdev_cc = imx7_csi_find_pixel_format(csi->vdev_fmt.pixelformat); 1617 1618 return 0; 1619 } 1620 1621 static int imx7_csi_video_register(struct imx7_csi *csi) 1622 { 1623 struct v4l2_subdev *sd = &csi->sd; 1624 struct v4l2_device *v4l2_dev = sd->v4l2_dev; 1625 struct video_device *vdev = csi->vdev; 1626 int ret; 1627 1628 vdev->v4l2_dev = v4l2_dev; 1629 1630 /* Initialize the default format and compose rectangle. */ 1631 ret = imx7_csi_video_init_format(csi); 1632 if (ret < 0) 1633 return ret; 1634 1635 /* Register the video device. */ 1636 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); 1637 if (ret) { 1638 dev_err(csi->dev, "Failed to register video device\n"); 1639 return ret; 1640 } 1641 1642 dev_info(csi->dev, "Registered %s as /dev/%s\n", vdev->name, 1643 video_device_node_name(vdev)); 1644 1645 /* Create the link from the CSI subdev to the video device. */ 1646 ret = media_create_pad_link(&sd->entity, IMX7_CSI_PAD_SRC, 1647 &vdev->entity, 0, MEDIA_LNK_FL_IMMUTABLE | 1648 MEDIA_LNK_FL_ENABLED); 1649 if (ret) { 1650 dev_err(csi->dev, "failed to create link to device node\n"); 1651 video_unregister_device(vdev); 1652 return ret; 1653 } 1654 1655 return 0; 1656 } 1657 1658 static void imx7_csi_video_unregister(struct imx7_csi *csi) 1659 { 1660 media_entity_cleanup(&csi->vdev->entity); 1661 video_unregister_device(csi->vdev); 1662 } 1663 1664 static int imx7_csi_video_init(struct imx7_csi *csi) 1665 { 1666 struct video_device *vdev; 1667 struct vb2_queue *vq; 1668 int ret; 1669 1670 mutex_init(&csi->vdev_mutex); 1671 INIT_LIST_HEAD(&csi->ready_q); 1672 spin_lock_init(&csi->q_lock); 1673 1674 /* Allocate and initialize the video device. */ 1675 vdev = video_device_alloc(); 1676 if (!vdev) 1677 return -ENOMEM; 1678 1679 vdev->fops = &imx7_csi_video_fops; 1680 vdev->ioctl_ops = &imx7_csi_video_ioctl_ops; 1681 vdev->minor = -1; 1682 vdev->release = video_device_release; 1683 vdev->vfl_dir = VFL_DIR_RX; 1684 vdev->tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM; 1685 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING 1686 | V4L2_CAP_IO_MC; 1687 vdev->lock = &csi->vdev_mutex; 1688 vdev->queue = &csi->q; 1689 1690 snprintf(vdev->name, sizeof(vdev->name), "%s capture", csi->sd.name); 1691 1692 video_set_drvdata(vdev, csi); 1693 csi->vdev = vdev; 1694 1695 /* Initialize the video device pad. */ 1696 csi->vdev_pad.flags = MEDIA_PAD_FL_SINK; 1697 ret = media_entity_pads_init(&vdev->entity, 1, &csi->vdev_pad); 1698 if (ret) { 1699 video_device_release(vdev); 1700 return ret; 1701 } 1702 1703 /* Initialize the vb2 queue. */ 1704 vq = &csi->q; 1705 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1706 vq->io_modes = VB2_MMAP | VB2_DMABUF; 1707 vq->drv_priv = csi; 1708 vq->buf_struct_size = sizeof(struct imx7_csi_vb2_buffer); 1709 vq->ops = &imx7_csi_video_qops; 1710 vq->mem_ops = &vb2_dma_contig_memops; 1711 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1712 vq->lock = &csi->vdev_mutex; 1713 vq->min_buffers_needed = 2; 1714 vq->dev = csi->dev; 1715 1716 ret = vb2_queue_init(vq); 1717 if (ret) { 1718 dev_err(csi->dev, "vb2_queue_init failed\n"); 1719 video_device_release(vdev); 1720 return ret; 1721 } 1722 1723 return 0; 1724 } 1725 1726 /* ----------------------------------------------------------------------------- 1727 * V4L2 Subdev Operations 1728 */ 1729 1730 static int imx7_csi_s_stream(struct v4l2_subdev *sd, int enable) 1731 { 1732 struct imx7_csi *csi = v4l2_get_subdevdata(sd); 1733 int ret = 0; 1734 1735 mutex_lock(&csi->lock); 1736 1737 if (!csi->src_sd) { 1738 ret = -EPIPE; 1739 goto out_unlock; 1740 } 1741 1742 if (csi->is_streaming == !!enable) 1743 goto out_unlock; 1744 1745 if (enable) { 1746 ret = imx7_csi_init(csi); 1747 if (ret < 0) 1748 goto out_unlock; 1749 1750 ret = v4l2_subdev_call(csi->src_sd, video, s_stream, 1); 1751 if (ret < 0) { 1752 imx7_csi_deinit(csi, VB2_BUF_STATE_QUEUED); 1753 goto out_unlock; 1754 } 1755 1756 imx7_csi_enable(csi); 1757 } else { 1758 imx7_csi_disable(csi); 1759 1760 v4l2_subdev_call(csi->src_sd, video, s_stream, 0); 1761 1762 imx7_csi_deinit(csi, VB2_BUF_STATE_ERROR); 1763 } 1764 1765 csi->is_streaming = !!enable; 1766 1767 out_unlock: 1768 mutex_unlock(&csi->lock); 1769 1770 return ret; 1771 } 1772 1773 static struct v4l2_mbus_framefmt * 1774 imx7_csi_get_format(struct imx7_csi *csi, 1775 struct v4l2_subdev_state *sd_state, 1776 unsigned int pad, 1777 enum v4l2_subdev_format_whence which) 1778 { 1779 if (which == V4L2_SUBDEV_FORMAT_TRY) 1780 return v4l2_subdev_get_try_format(&csi->sd, sd_state, pad); 1781 1782 return &csi->format_mbus[pad]; 1783 } 1784 1785 static int imx7_csi_init_cfg(struct v4l2_subdev *sd, 1786 struct v4l2_subdev_state *sd_state) 1787 { 1788 const enum v4l2_subdev_format_whence which = 1789 sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE; 1790 struct imx7_csi *csi = v4l2_get_subdevdata(sd); 1791 const struct imx7_csi_pixfmt *cc; 1792 int i; 1793 1794 cc = imx7_csi_find_mbus_format(IMX7_CSI_DEF_MBUS_CODE); 1795 1796 for (i = 0; i < IMX7_CSI_PADS_NUM; i++) { 1797 struct v4l2_mbus_framefmt *mf = 1798 imx7_csi_get_format(csi, sd_state, i, which); 1799 1800 mf->code = IMX7_CSI_DEF_MBUS_CODE; 1801 mf->width = IMX7_CSI_DEF_PIX_WIDTH; 1802 mf->height = IMX7_CSI_DEF_PIX_HEIGHT; 1803 mf->field = V4L2_FIELD_NONE; 1804 1805 mf->colorspace = V4L2_COLORSPACE_SRGB; 1806 mf->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(mf->colorspace); 1807 mf->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(mf->colorspace); 1808 mf->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(!cc->yuv, 1809 mf->colorspace, mf->ycbcr_enc); 1810 1811 csi->cc[i] = cc; 1812 } 1813 1814 return 0; 1815 } 1816 1817 static int imx7_csi_enum_mbus_code(struct v4l2_subdev *sd, 1818 struct v4l2_subdev_state *sd_state, 1819 struct v4l2_subdev_mbus_code_enum *code) 1820 { 1821 struct imx7_csi *csi = v4l2_get_subdevdata(sd); 1822 struct v4l2_mbus_framefmt *in_fmt; 1823 int ret = 0; 1824 1825 mutex_lock(&csi->lock); 1826 1827 in_fmt = imx7_csi_get_format(csi, sd_state, IMX7_CSI_PAD_SINK, 1828 code->which); 1829 1830 switch (code->pad) { 1831 case IMX7_CSI_PAD_SINK: 1832 ret = imx7_csi_enum_mbus_formats(&code->code, code->index); 1833 break; 1834 case IMX7_CSI_PAD_SRC: 1835 if (code->index != 0) { 1836 ret = -EINVAL; 1837 goto out_unlock; 1838 } 1839 1840 code->code = in_fmt->code; 1841 break; 1842 default: 1843 ret = -EINVAL; 1844 } 1845 1846 out_unlock: 1847 mutex_unlock(&csi->lock); 1848 1849 return ret; 1850 } 1851 1852 static int imx7_csi_get_fmt(struct v4l2_subdev *sd, 1853 struct v4l2_subdev_state *sd_state, 1854 struct v4l2_subdev_format *sdformat) 1855 { 1856 struct imx7_csi *csi = v4l2_get_subdevdata(sd); 1857 struct v4l2_mbus_framefmt *fmt; 1858 int ret = 0; 1859 1860 mutex_lock(&csi->lock); 1861 1862 fmt = imx7_csi_get_format(csi, sd_state, sdformat->pad, 1863 sdformat->which); 1864 if (!fmt) { 1865 ret = -EINVAL; 1866 goto out_unlock; 1867 } 1868 1869 sdformat->format = *fmt; 1870 1871 out_unlock: 1872 mutex_unlock(&csi->lock); 1873 1874 return ret; 1875 } 1876 1877 /* 1878 * Default the colorspace in tryfmt to SRGB if set to an unsupported 1879 * colorspace or not initialized. Then set the remaining colorimetry 1880 * parameters based on the colorspace if they are uninitialized. 1881 * 1882 * tryfmt->code must be set on entry. 1883 */ 1884 static void imx7_csi_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt) 1885 { 1886 const struct imx7_csi_pixfmt *cc; 1887 bool is_rgb = false; 1888 1889 cc = imx7_csi_find_mbus_format(tryfmt->code); 1890 if (cc && !cc->yuv) 1891 is_rgb = true; 1892 1893 switch (tryfmt->colorspace) { 1894 case V4L2_COLORSPACE_SMPTE170M: 1895 case V4L2_COLORSPACE_REC709: 1896 case V4L2_COLORSPACE_JPEG: 1897 case V4L2_COLORSPACE_SRGB: 1898 case V4L2_COLORSPACE_BT2020: 1899 case V4L2_COLORSPACE_OPRGB: 1900 case V4L2_COLORSPACE_DCI_P3: 1901 case V4L2_COLORSPACE_RAW: 1902 break; 1903 default: 1904 tryfmt->colorspace = V4L2_COLORSPACE_SRGB; 1905 break; 1906 } 1907 1908 if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT) 1909 tryfmt->xfer_func = 1910 V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace); 1911 1912 if (tryfmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) 1913 tryfmt->ycbcr_enc = 1914 V4L2_MAP_YCBCR_ENC_DEFAULT(tryfmt->colorspace); 1915 1916 if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT) 1917 tryfmt->quantization = 1918 V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb, 1919 tryfmt->colorspace, 1920 tryfmt->ycbcr_enc); 1921 } 1922 1923 static int imx7_csi_try_fmt(struct imx7_csi *csi, 1924 struct v4l2_subdev_state *sd_state, 1925 struct v4l2_subdev_format *sdformat, 1926 const struct imx7_csi_pixfmt **cc) 1927 { 1928 const struct imx7_csi_pixfmt *in_cc; 1929 struct v4l2_mbus_framefmt *in_fmt; 1930 u32 code; 1931 1932 in_fmt = imx7_csi_get_format(csi, sd_state, IMX7_CSI_PAD_SINK, 1933 sdformat->which); 1934 if (!in_fmt) 1935 return -EINVAL; 1936 1937 switch (sdformat->pad) { 1938 case IMX7_CSI_PAD_SRC: 1939 in_cc = imx7_csi_find_mbus_format(in_fmt->code); 1940 1941 sdformat->format.width = in_fmt->width; 1942 sdformat->format.height = in_fmt->height; 1943 sdformat->format.code = in_fmt->code; 1944 sdformat->format.field = in_fmt->field; 1945 *cc = in_cc; 1946 1947 sdformat->format.colorspace = in_fmt->colorspace; 1948 sdformat->format.xfer_func = in_fmt->xfer_func; 1949 sdformat->format.quantization = in_fmt->quantization; 1950 sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc; 1951 break; 1952 case IMX7_CSI_PAD_SINK: 1953 *cc = imx7_csi_find_mbus_format(sdformat->format.code); 1954 if (!*cc) { 1955 code = IMX7_CSI_DEF_MBUS_CODE; 1956 *cc = imx7_csi_find_mbus_format(code); 1957 sdformat->format.code = code; 1958 } 1959 1960 if (sdformat->format.field != V4L2_FIELD_INTERLACED) 1961 sdformat->format.field = V4L2_FIELD_NONE; 1962 break; 1963 default: 1964 return -EINVAL; 1965 } 1966 1967 imx7_csi_try_colorimetry(&sdformat->format); 1968 1969 return 0; 1970 } 1971 1972 static int imx7_csi_set_fmt(struct v4l2_subdev *sd, 1973 struct v4l2_subdev_state *sd_state, 1974 struct v4l2_subdev_format *sdformat) 1975 { 1976 struct imx7_csi *csi = v4l2_get_subdevdata(sd); 1977 const struct imx7_csi_pixfmt *outcc; 1978 struct v4l2_mbus_framefmt *outfmt; 1979 const struct imx7_csi_pixfmt *cc; 1980 struct v4l2_mbus_framefmt *fmt; 1981 struct v4l2_subdev_format format; 1982 int ret = 0; 1983 1984 if (sdformat->pad >= IMX7_CSI_PADS_NUM) 1985 return -EINVAL; 1986 1987 mutex_lock(&csi->lock); 1988 1989 if (csi->is_streaming) { 1990 ret = -EBUSY; 1991 goto out_unlock; 1992 } 1993 1994 ret = imx7_csi_try_fmt(csi, sd_state, sdformat, &cc); 1995 if (ret < 0) 1996 goto out_unlock; 1997 1998 fmt = imx7_csi_get_format(csi, sd_state, sdformat->pad, 1999 sdformat->which); 2000 if (!fmt) { 2001 ret = -EINVAL; 2002 goto out_unlock; 2003 } 2004 2005 *fmt = sdformat->format; 2006 2007 if (sdformat->pad == IMX7_CSI_PAD_SINK) { 2008 /* propagate format to source pads */ 2009 format.pad = IMX7_CSI_PAD_SRC; 2010 format.which = sdformat->which; 2011 format.format = sdformat->format; 2012 if (imx7_csi_try_fmt(csi, sd_state, &format, &outcc)) { 2013 ret = -EINVAL; 2014 goto out_unlock; 2015 } 2016 outfmt = imx7_csi_get_format(csi, sd_state, IMX7_CSI_PAD_SRC, 2017 sdformat->which); 2018 *outfmt = format.format; 2019 2020 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE) 2021 csi->cc[IMX7_CSI_PAD_SRC] = outcc; 2022 } 2023 2024 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE) 2025 csi->cc[sdformat->pad] = cc; 2026 2027 out_unlock: 2028 mutex_unlock(&csi->lock); 2029 2030 return ret; 2031 } 2032 2033 static int imx7_csi_pad_link_validate(struct v4l2_subdev *sd, 2034 struct media_link *link, 2035 struct v4l2_subdev_format *source_fmt, 2036 struct v4l2_subdev_format *sink_fmt) 2037 { 2038 struct imx7_csi *csi = v4l2_get_subdevdata(sd); 2039 struct media_pad *pad = NULL; 2040 unsigned int i; 2041 int ret; 2042 2043 if (!csi->src_sd) 2044 return -EPIPE; 2045 2046 /* 2047 * Validate the source link, and record whether the source uses the 2048 * parallel input or the CSI-2 receiver. 2049 */ 2050 ret = v4l2_subdev_link_validate_default(sd, link, source_fmt, sink_fmt); 2051 if (ret) 2052 return ret; 2053 2054 switch (csi->src_sd->entity.function) { 2055 case MEDIA_ENT_F_VID_IF_BRIDGE: 2056 /* The input is the CSI-2 receiver. */ 2057 csi->is_csi2 = true; 2058 break; 2059 2060 case MEDIA_ENT_F_VID_MUX: 2061 /* The input is the mux, check its input. */ 2062 for (i = 0; i < csi->src_sd->entity.num_pads; i++) { 2063 struct media_pad *spad = &csi->src_sd->entity.pads[i]; 2064 2065 if (!(spad->flags & MEDIA_PAD_FL_SINK)) 2066 continue; 2067 2068 pad = media_pad_remote_pad_first(spad); 2069 if (pad) 2070 break; 2071 } 2072 2073 if (!pad) 2074 return -ENODEV; 2075 2076 csi->is_csi2 = pad->entity->function == MEDIA_ENT_F_VID_IF_BRIDGE; 2077 break; 2078 2079 default: 2080 /* 2081 * The input is an external entity, it must use the parallel 2082 * bus. 2083 */ 2084 csi->is_csi2 = false; 2085 break; 2086 } 2087 2088 return 0; 2089 } 2090 2091 static int imx7_csi_registered(struct v4l2_subdev *sd) 2092 { 2093 struct imx7_csi *csi = v4l2_get_subdevdata(sd); 2094 int ret; 2095 2096 ret = imx7_csi_video_init(csi); 2097 if (ret) 2098 return ret; 2099 2100 ret = imx7_csi_video_register(csi); 2101 if (ret) 2102 return ret; 2103 2104 ret = v4l2_device_register_subdev_nodes(&csi->v4l2_dev); 2105 if (ret) 2106 goto err_unreg; 2107 2108 ret = media_device_register(&csi->mdev); 2109 if (ret) 2110 goto err_unreg; 2111 2112 return 0; 2113 2114 err_unreg: 2115 imx7_csi_video_unregister(csi); 2116 return ret; 2117 } 2118 2119 static void imx7_csi_unregistered(struct v4l2_subdev *sd) 2120 { 2121 struct imx7_csi *csi = v4l2_get_subdevdata(sd); 2122 2123 imx7_csi_video_unregister(csi); 2124 } 2125 2126 static const struct v4l2_subdev_video_ops imx7_csi_video_ops = { 2127 .s_stream = imx7_csi_s_stream, 2128 }; 2129 2130 static const struct v4l2_subdev_pad_ops imx7_csi_pad_ops = { 2131 .init_cfg = imx7_csi_init_cfg, 2132 .enum_mbus_code = imx7_csi_enum_mbus_code, 2133 .get_fmt = imx7_csi_get_fmt, 2134 .set_fmt = imx7_csi_set_fmt, 2135 .link_validate = imx7_csi_pad_link_validate, 2136 }; 2137 2138 static const struct v4l2_subdev_ops imx7_csi_subdev_ops = { 2139 .video = &imx7_csi_video_ops, 2140 .pad = &imx7_csi_pad_ops, 2141 }; 2142 2143 static const struct v4l2_subdev_internal_ops imx7_csi_internal_ops = { 2144 .registered = imx7_csi_registered, 2145 .unregistered = imx7_csi_unregistered, 2146 }; 2147 2148 /* ----------------------------------------------------------------------------- 2149 * Media Entity Operations 2150 */ 2151 2152 static const struct media_entity_operations imx7_csi_entity_ops = { 2153 .link_validate = v4l2_subdev_link_validate, 2154 .get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1, 2155 }; 2156 2157 /* ----------------------------------------------------------------------------- 2158 * Probe & Remove 2159 */ 2160 2161 static int imx7_csi_notify_bound(struct v4l2_async_notifier *notifier, 2162 struct v4l2_subdev *sd, 2163 struct v4l2_async_subdev *asd) 2164 { 2165 struct imx7_csi *csi = imx7_csi_notifier_to_dev(notifier); 2166 struct media_pad *sink = &csi->sd.entity.pads[IMX7_CSI_PAD_SINK]; 2167 2168 csi->src_sd = sd; 2169 2170 return v4l2_create_fwnode_links_to_pad(sd, sink, MEDIA_LNK_FL_ENABLED | 2171 MEDIA_LNK_FL_IMMUTABLE); 2172 } 2173 2174 static int imx7_csi_notify_complete(struct v4l2_async_notifier *notifier) 2175 { 2176 struct imx7_csi *csi = imx7_csi_notifier_to_dev(notifier); 2177 2178 return v4l2_device_register_subdev_nodes(&csi->v4l2_dev); 2179 } 2180 2181 static const struct v4l2_async_notifier_operations imx7_csi_notify_ops = { 2182 .bound = imx7_csi_notify_bound, 2183 .complete = imx7_csi_notify_complete, 2184 }; 2185 2186 static int imx7_csi_async_register(struct imx7_csi *csi) 2187 { 2188 struct v4l2_async_subdev *asd; 2189 struct fwnode_handle *ep; 2190 int ret; 2191 2192 v4l2_async_nf_init(&csi->notifier); 2193 2194 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csi->dev), 0, 0, 2195 FWNODE_GRAPH_ENDPOINT_NEXT); 2196 if (ep) { 2197 asd = v4l2_async_nf_add_fwnode_remote(&csi->notifier, ep, 2198 struct v4l2_async_subdev); 2199 2200 fwnode_handle_put(ep); 2201 2202 if (IS_ERR(asd)) { 2203 ret = PTR_ERR(asd); 2204 /* OK if asd already exists */ 2205 if (ret != -EEXIST) 2206 return ret; 2207 } 2208 } 2209 2210 csi->notifier.ops = &imx7_csi_notify_ops; 2211 2212 ret = v4l2_async_nf_register(&csi->v4l2_dev, &csi->notifier); 2213 if (ret) 2214 return ret; 2215 2216 return 0; 2217 } 2218 2219 static void imx7_csi_media_cleanup(struct imx7_csi *csi) 2220 { 2221 v4l2_device_unregister(&csi->v4l2_dev); 2222 media_device_unregister(&csi->mdev); 2223 media_device_cleanup(&csi->mdev); 2224 } 2225 2226 static const struct media_device_ops imx7_csi_media_ops = { 2227 .link_notify = v4l2_pipeline_link_notify, 2228 }; 2229 2230 static int imx7_csi_media_dev_init(struct imx7_csi *csi) 2231 { 2232 int ret; 2233 2234 strscpy(csi->mdev.model, "imx-media", sizeof(csi->mdev.model)); 2235 csi->mdev.ops = &imx7_csi_media_ops; 2236 csi->mdev.dev = csi->dev; 2237 2238 csi->v4l2_dev.mdev = &csi->mdev; 2239 strscpy(csi->v4l2_dev.name, "imx-media", 2240 sizeof(csi->v4l2_dev.name)); 2241 snprintf(csi->mdev.bus_info, sizeof(csi->mdev.bus_info), 2242 "platform:%s", dev_name(csi->mdev.dev)); 2243 2244 media_device_init(&csi->mdev); 2245 2246 ret = v4l2_device_register(csi->dev, &csi->v4l2_dev); 2247 if (ret < 0) { 2248 v4l2_err(&csi->v4l2_dev, 2249 "Failed to register v4l2_device: %d\n", ret); 2250 goto cleanup; 2251 } 2252 2253 return 0; 2254 2255 cleanup: 2256 media_device_cleanup(&csi->mdev); 2257 2258 return ret; 2259 } 2260 2261 static int imx7_csi_media_init(struct imx7_csi *csi) 2262 { 2263 unsigned int i; 2264 int ret; 2265 2266 /* add media device */ 2267 ret = imx7_csi_media_dev_init(csi); 2268 if (ret) 2269 return ret; 2270 2271 v4l2_subdev_init(&csi->sd, &imx7_csi_subdev_ops); 2272 v4l2_set_subdevdata(&csi->sd, csi); 2273 csi->sd.internal_ops = &imx7_csi_internal_ops; 2274 csi->sd.entity.ops = &imx7_csi_entity_ops; 2275 csi->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; 2276 csi->sd.dev = csi->dev; 2277 csi->sd.owner = THIS_MODULE; 2278 csi->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; 2279 snprintf(csi->sd.name, sizeof(csi->sd.name), "csi"); 2280 2281 for (i = 0; i < IMX7_CSI_PADS_NUM; i++) 2282 csi->pad[i].flags = (i == IMX7_CSI_PAD_SINK) ? 2283 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE; 2284 2285 ret = media_entity_pads_init(&csi->sd.entity, IMX7_CSI_PADS_NUM, 2286 csi->pad); 2287 if (ret) 2288 goto error; 2289 2290 ret = v4l2_device_register_subdev(&csi->v4l2_dev, &csi->sd); 2291 if (ret) 2292 goto error; 2293 2294 return 0; 2295 2296 error: 2297 imx7_csi_media_cleanup(csi); 2298 return ret; 2299 } 2300 2301 static int imx7_csi_probe(struct platform_device *pdev) 2302 { 2303 struct device *dev = &pdev->dev; 2304 struct imx7_csi *csi; 2305 int ret; 2306 2307 csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL); 2308 if (!csi) 2309 return -ENOMEM; 2310 2311 csi->dev = dev; 2312 platform_set_drvdata(pdev, csi); 2313 2314 spin_lock_init(&csi->irqlock); 2315 mutex_init(&csi->lock); 2316 2317 /* Acquire resources and install interrupt handler. */ 2318 csi->mclk = devm_clk_get(&pdev->dev, "mclk"); 2319 if (IS_ERR(csi->mclk)) { 2320 ret = PTR_ERR(csi->mclk); 2321 dev_err(dev, "Failed to get mclk: %d", ret); 2322 goto destroy_mutex; 2323 } 2324 2325 csi->irq = platform_get_irq(pdev, 0); 2326 if (csi->irq < 0) { 2327 ret = csi->irq; 2328 goto destroy_mutex; 2329 } 2330 2331 csi->regbase = devm_platform_ioremap_resource(pdev, 0); 2332 if (IS_ERR(csi->regbase)) { 2333 ret = PTR_ERR(csi->regbase); 2334 goto destroy_mutex; 2335 } 2336 2337 csi->model = (enum imx_csi_model)(uintptr_t)of_device_get_match_data(&pdev->dev); 2338 2339 ret = devm_request_irq(dev, csi->irq, imx7_csi_irq_handler, 0, "csi", 2340 (void *)csi); 2341 if (ret < 0) { 2342 dev_err(dev, "Request CSI IRQ failed.\n"); 2343 goto destroy_mutex; 2344 } 2345 2346 /* Initialize all the media device infrastructure. */ 2347 ret = imx7_csi_media_init(csi); 2348 if (ret) 2349 goto destroy_mutex; 2350 2351 /* Set the default mbus formats. */ 2352 ret = imx7_csi_init_cfg(&csi->sd, NULL); 2353 if (ret) 2354 goto media_cleanup; 2355 2356 ret = imx7_csi_async_register(csi); 2357 if (ret) 2358 goto subdev_notifier_cleanup; 2359 2360 return 0; 2361 2362 subdev_notifier_cleanup: 2363 v4l2_async_nf_unregister(&csi->notifier); 2364 v4l2_async_nf_cleanup(&csi->notifier); 2365 media_cleanup: 2366 imx7_csi_media_cleanup(csi); 2367 2368 destroy_mutex: 2369 mutex_destroy(&csi->lock); 2370 2371 return ret; 2372 } 2373 2374 static int imx7_csi_remove(struct platform_device *pdev) 2375 { 2376 struct imx7_csi *csi = platform_get_drvdata(pdev); 2377 2378 imx7_csi_media_cleanup(csi); 2379 2380 v4l2_async_nf_unregister(&csi->notifier); 2381 v4l2_async_nf_cleanup(&csi->notifier); 2382 v4l2_async_unregister_subdev(&csi->sd); 2383 2384 mutex_destroy(&csi->lock); 2385 2386 return 0; 2387 } 2388 2389 static const struct of_device_id imx7_csi_of_match[] = { 2390 { .compatible = "fsl,imx8mq-csi", .data = (void *)IMX7_CSI_IMX8MQ }, 2391 { .compatible = "fsl,imx7-csi", .data = (void *)IMX7_CSI_IMX7 }, 2392 { .compatible = "fsl,imx6ul-csi", .data = (void *)IMX7_CSI_IMX7 }, 2393 { }, 2394 }; 2395 MODULE_DEVICE_TABLE(of, imx7_csi_of_match); 2396 2397 static struct platform_driver imx7_csi_driver = { 2398 .probe = imx7_csi_probe, 2399 .remove = imx7_csi_remove, 2400 .driver = { 2401 .of_match_table = imx7_csi_of_match, 2402 .name = "imx7-csi", 2403 }, 2404 }; 2405 module_platform_driver(imx7_csi_driver); 2406 2407 MODULE_DESCRIPTION("i.MX7 CSI subdev driver"); 2408 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>"); 2409 MODULE_LICENSE("GPL v2"); 2410 MODULE_ALIAS("platform:imx7-csi"); 2411