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