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