1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * V4L2 Capture CSI Subdev for Freescale i.MX5/6 SOC 4 * 5 * Copyright (c) 2014-2017 Mentor Graphics Inc. 6 * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de> 7 */ 8 #include <linux/delay.h> 9 #include <linux/gcd.h> 10 #include <linux/interrupt.h> 11 #include <linux/module.h> 12 #include <linux/of_graph.h> 13 #include <linux/pinctrl/consumer.h> 14 #include <linux/platform_device.h> 15 #include <media/v4l2-ctrls.h> 16 #include <media/v4l2-device.h> 17 #include <media/v4l2-event.h> 18 #include <media/v4l2-fwnode.h> 19 #include <media/v4l2-mc.h> 20 #include <media/v4l2-subdev.h> 21 #include <media/videobuf2-dma-contig.h> 22 #include <video/imx-ipu-v3.h> 23 #include <media/imx.h> 24 #include "imx-media.h" 25 26 /* 27 * Min/Max supported width and heights. 28 * 29 * We allow planar output, so we have to align width by 16 pixels 30 * to meet IDMAC alignment requirements. 31 * 32 * TODO: move this into pad format negotiation, if capture device 33 * has not requested planar formats, we should allow 8 pixel 34 * alignment. 35 */ 36 #define MIN_W 32 37 #define MIN_H 32 38 #define MAX_W 4096 39 #define MAX_H 4096 40 #define W_ALIGN 1 /* multiple of 2 pixels */ 41 #define H_ALIGN 1 /* multiple of 2 lines */ 42 #define S_ALIGN 1 /* multiple of 2 */ 43 44 /* 45 * struct csi_skip_desc - CSI frame skipping descriptor 46 * @keep - number of frames kept per max_ratio frames 47 * @max_ratio - width of skip_smfc, written to MAX_RATIO bitfield 48 * @skip_smfc - skip pattern written to the SKIP_SMFC bitfield 49 */ 50 struct csi_skip_desc { 51 u8 keep; 52 u8 max_ratio; 53 u8 skip_smfc; 54 }; 55 56 struct csi_priv { 57 struct device *dev; 58 struct ipu_soc *ipu; 59 struct v4l2_subdev sd; 60 struct media_pad pad[CSI_NUM_PADS]; 61 struct v4l2_async_notifier notifier; 62 63 /* the video device at IDMAC output pad */ 64 struct imx_media_video_dev *vdev; 65 struct imx_media_fim *fim; 66 int csi_id; 67 int smfc_id; 68 69 /* lock to protect all members below */ 70 struct mutex lock; 71 72 int active_output_pad; 73 74 struct ipuv3_channel *idmac_ch; 75 struct ipu_smfc *smfc; 76 struct ipu_csi *csi; 77 78 struct v4l2_mbus_framefmt format_mbus[CSI_NUM_PADS]; 79 const struct imx_media_pixfmt *cc[CSI_NUM_PADS]; 80 struct v4l2_fract frame_interval[CSI_NUM_PADS]; 81 struct v4l2_rect crop; 82 struct v4l2_rect compose; 83 const struct csi_skip_desc *skip; 84 85 /* active vb2 buffers to send to video dev sink */ 86 struct imx_media_buffer *active_vb2_buf[2]; 87 struct imx_media_dma_buf underrun_buf; 88 89 int ipu_buf_num; /* ipu double buffer index: 0-1 */ 90 91 /* the sink for the captured frames */ 92 struct media_entity *sink; 93 enum ipu_csi_dest dest; 94 /* the source subdev */ 95 struct v4l2_subdev *src_sd; 96 97 /* the mipi virtual channel number at link validate */ 98 int vc_num; 99 100 /* the upstream endpoint CSI is receiving from */ 101 struct v4l2_fwnode_endpoint upstream_ep; 102 103 spinlock_t irqlock; /* protect eof_irq handler */ 104 struct timer_list eof_timeout_timer; 105 int eof_irq; 106 int nfb4eof_irq; 107 108 struct v4l2_ctrl_handler ctrl_hdlr; 109 110 int stream_count; /* streaming counter */ 111 u32 frame_sequence; /* frame sequence counter */ 112 bool last_eof; /* waiting for last EOF at stream off */ 113 bool nfb4eof; /* NFB4EOF encountered during streaming */ 114 bool interweave_swap; /* swap top/bottom lines when interweaving */ 115 struct completion last_eof_comp; 116 }; 117 118 static inline struct csi_priv *sd_to_dev(struct v4l2_subdev *sdev) 119 { 120 return container_of(sdev, struct csi_priv, sd); 121 } 122 123 static inline struct csi_priv *notifier_to_dev(struct v4l2_async_notifier *n) 124 { 125 return container_of(n, struct csi_priv, notifier); 126 } 127 128 static inline bool is_parallel_bus(struct v4l2_fwnode_endpoint *ep) 129 { 130 return ep->bus_type != V4L2_MBUS_CSI2_DPHY; 131 } 132 133 static inline bool is_parallel_16bit_bus(struct v4l2_fwnode_endpoint *ep) 134 { 135 return is_parallel_bus(ep) && ep->bus.parallel.bus_width >= 16; 136 } 137 138 /* 139 * Check for conditions that require the IPU to handle the 140 * data internally as generic data, aka passthrough mode: 141 * - raw bayer media bus formats, or 142 * - the CSI is receiving from a 16-bit parallel bus, or 143 * - the CSI is receiving from an 8-bit parallel bus and the incoming 144 * media bus format is other than UYVY8_2X8/YUYV8_2X8. 145 */ 146 static inline bool requires_passthrough(struct v4l2_fwnode_endpoint *ep, 147 struct v4l2_mbus_framefmt *infmt, 148 const struct imx_media_pixfmt *incc) 149 { 150 return incc->bayer || is_parallel_16bit_bus(ep) || 151 (is_parallel_bus(ep) && 152 infmt->code != MEDIA_BUS_FMT_UYVY8_2X8 && 153 infmt->code != MEDIA_BUS_FMT_YUYV8_2X8); 154 } 155 156 /* 157 * Parses the fwnode endpoint from the source pad of the entity 158 * connected to this CSI. This will either be the entity directly 159 * upstream from the CSI-2 receiver, directly upstream from the 160 * video mux, or directly upstream from the CSI itself. The endpoint 161 * is needed to determine the bus type and bus config coming into 162 * the CSI. 163 */ 164 static int csi_get_upstream_endpoint(struct csi_priv *priv, 165 struct v4l2_fwnode_endpoint *ep) 166 { 167 struct fwnode_handle *endpoint; 168 struct v4l2_subdev *sd; 169 struct media_pad *pad; 170 171 if (!IS_ENABLED(CONFIG_OF)) 172 return -ENXIO; 173 174 if (!priv->src_sd) 175 return -EPIPE; 176 177 sd = priv->src_sd; 178 179 switch (sd->grp_id) { 180 case IMX_MEDIA_GRP_ID_CSI_MUX: 181 /* 182 * CSI is connected directly to CSI mux, skip up to 183 * CSI-2 receiver if it is in the path, otherwise stay 184 * with the CSI mux. 185 */ 186 sd = imx_media_pipeline_subdev(&sd->entity, 187 IMX_MEDIA_GRP_ID_CSI2, 188 true); 189 if (IS_ERR(sd)) 190 sd = priv->src_sd; 191 break; 192 case IMX_MEDIA_GRP_ID_CSI2: 193 break; 194 default: 195 /* 196 * the source is neither the CSI mux nor the CSI-2 receiver, 197 * get the source pad directly upstream from CSI itself. 198 */ 199 sd = &priv->sd; 200 break; 201 } 202 203 /* get source pad of entity directly upstream from sd */ 204 pad = imx_media_pipeline_pad(&sd->entity, 0, 0, true); 205 if (!pad) 206 return -ENODEV; 207 208 endpoint = imx_media_get_pad_fwnode(pad); 209 if (IS_ERR(endpoint)) 210 return PTR_ERR(endpoint); 211 212 v4l2_fwnode_endpoint_parse(endpoint, ep); 213 214 fwnode_handle_put(endpoint); 215 216 return 0; 217 } 218 219 static void csi_idmac_put_ipu_resources(struct csi_priv *priv) 220 { 221 if (priv->idmac_ch) 222 ipu_idmac_put(priv->idmac_ch); 223 priv->idmac_ch = NULL; 224 225 if (priv->smfc) 226 ipu_smfc_put(priv->smfc); 227 priv->smfc = NULL; 228 } 229 230 static int csi_idmac_get_ipu_resources(struct csi_priv *priv) 231 { 232 int ch_num, ret; 233 struct ipu_smfc *smfc; 234 struct ipuv3_channel *idmac_ch; 235 236 ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id; 237 238 smfc = ipu_smfc_get(priv->ipu, ch_num); 239 if (IS_ERR(smfc)) { 240 v4l2_err(&priv->sd, "failed to get SMFC\n"); 241 ret = PTR_ERR(smfc); 242 goto out; 243 } 244 priv->smfc = smfc; 245 246 idmac_ch = ipu_idmac_get(priv->ipu, ch_num); 247 if (IS_ERR(idmac_ch)) { 248 v4l2_err(&priv->sd, "could not get IDMAC channel %u\n", 249 ch_num); 250 ret = PTR_ERR(idmac_ch); 251 goto out; 252 } 253 priv->idmac_ch = idmac_ch; 254 255 return 0; 256 out: 257 csi_idmac_put_ipu_resources(priv); 258 return ret; 259 } 260 261 static void csi_vb2_buf_done(struct csi_priv *priv) 262 { 263 struct imx_media_video_dev *vdev = priv->vdev; 264 struct imx_media_buffer *done, *next; 265 struct vb2_buffer *vb; 266 dma_addr_t phys; 267 268 done = priv->active_vb2_buf[priv->ipu_buf_num]; 269 if (done) { 270 done->vbuf.field = vdev->fmt.field; 271 done->vbuf.sequence = priv->frame_sequence; 272 vb = &done->vbuf.vb2_buf; 273 vb->timestamp = ktime_get_ns(); 274 vb2_buffer_done(vb, priv->nfb4eof ? 275 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); 276 } 277 278 priv->frame_sequence++; 279 priv->nfb4eof = false; 280 281 /* get next queued buffer */ 282 next = imx_media_capture_device_next_buf(vdev); 283 if (next) { 284 phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0); 285 priv->active_vb2_buf[priv->ipu_buf_num] = next; 286 } else { 287 phys = priv->underrun_buf.phys; 288 priv->active_vb2_buf[priv->ipu_buf_num] = NULL; 289 } 290 291 if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num)) 292 ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num); 293 294 if (priv->interweave_swap) 295 phys += vdev->fmt.bytesperline; 296 297 ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, phys); 298 } 299 300 static irqreturn_t csi_idmac_eof_interrupt(int irq, void *dev_id) 301 { 302 struct csi_priv *priv = dev_id; 303 304 spin_lock(&priv->irqlock); 305 306 if (priv->last_eof) { 307 complete(&priv->last_eof_comp); 308 priv->last_eof = false; 309 goto unlock; 310 } 311 312 if (priv->fim) 313 /* call frame interval monitor */ 314 imx_media_fim_eof_monitor(priv->fim, ktime_get()); 315 316 csi_vb2_buf_done(priv); 317 318 /* select new IPU buf */ 319 ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num); 320 /* toggle IPU double-buffer index */ 321 priv->ipu_buf_num ^= 1; 322 323 /* bump the EOF timeout timer */ 324 mod_timer(&priv->eof_timeout_timer, 325 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); 326 327 unlock: 328 spin_unlock(&priv->irqlock); 329 return IRQ_HANDLED; 330 } 331 332 static irqreturn_t csi_idmac_nfb4eof_interrupt(int irq, void *dev_id) 333 { 334 struct csi_priv *priv = dev_id; 335 336 spin_lock(&priv->irqlock); 337 338 /* 339 * this is not an unrecoverable error, just mark 340 * the next captured frame with vb2 error flag. 341 */ 342 priv->nfb4eof = true; 343 344 v4l2_err(&priv->sd, "NFB4EOF\n"); 345 346 spin_unlock(&priv->irqlock); 347 348 return IRQ_HANDLED; 349 } 350 351 /* 352 * EOF timeout timer function. This is an unrecoverable condition 353 * without a stream restart. 354 */ 355 static void csi_idmac_eof_timeout(struct timer_list *t) 356 { 357 struct csi_priv *priv = from_timer(priv, t, eof_timeout_timer); 358 struct imx_media_video_dev *vdev = priv->vdev; 359 360 v4l2_err(&priv->sd, "EOF timeout\n"); 361 362 /* signal a fatal error to capture device */ 363 imx_media_capture_device_error(vdev); 364 } 365 366 static void csi_idmac_setup_vb2_buf(struct csi_priv *priv, dma_addr_t *phys) 367 { 368 struct imx_media_video_dev *vdev = priv->vdev; 369 struct imx_media_buffer *buf; 370 int i; 371 372 for (i = 0; i < 2; i++) { 373 buf = imx_media_capture_device_next_buf(vdev); 374 if (buf) { 375 priv->active_vb2_buf[i] = buf; 376 phys[i] = vb2_dma_contig_plane_dma_addr( 377 &buf->vbuf.vb2_buf, 0); 378 } else { 379 priv->active_vb2_buf[i] = NULL; 380 phys[i] = priv->underrun_buf.phys; 381 } 382 } 383 } 384 385 static void csi_idmac_unsetup_vb2_buf(struct csi_priv *priv, 386 enum vb2_buffer_state return_status) 387 { 388 struct imx_media_buffer *buf; 389 int i; 390 391 /* return any remaining active frames with return_status */ 392 for (i = 0; i < 2; i++) { 393 buf = priv->active_vb2_buf[i]; 394 if (buf) { 395 struct vb2_buffer *vb = &buf->vbuf.vb2_buf; 396 397 vb->timestamp = ktime_get_ns(); 398 vb2_buffer_done(vb, return_status); 399 } 400 } 401 } 402 403 /* init the SMFC IDMAC channel */ 404 static int csi_idmac_setup_channel(struct csi_priv *priv) 405 { 406 struct imx_media_video_dev *vdev = priv->vdev; 407 const struct imx_media_pixfmt *incc; 408 struct v4l2_mbus_framefmt *infmt; 409 struct v4l2_mbus_framefmt *outfmt; 410 bool passthrough, interweave; 411 struct ipu_image image; 412 u32 passthrough_bits; 413 u32 passthrough_cycles; 414 dma_addr_t phys[2]; 415 u32 burst_size; 416 int ret; 417 418 infmt = &priv->format_mbus[CSI_SINK_PAD]; 419 incc = priv->cc[CSI_SINK_PAD]; 420 outfmt = &priv->format_mbus[CSI_SRC_PAD_IDMAC]; 421 422 ipu_cpmem_zero(priv->idmac_ch); 423 424 memset(&image, 0, sizeof(image)); 425 image.pix = vdev->fmt; 426 image.rect = vdev->compose; 427 428 csi_idmac_setup_vb2_buf(priv, phys); 429 430 image.phys0 = phys[0]; 431 image.phys1 = phys[1]; 432 433 passthrough = requires_passthrough(&priv->upstream_ep, infmt, incc); 434 passthrough_cycles = 1; 435 436 /* 437 * If the field type at capture interface is interlaced, and 438 * the output IDMAC pad is sequential, enable interweave at 439 * the IDMAC output channel. 440 */ 441 interweave = V4L2_FIELD_IS_INTERLACED(image.pix.field) && 442 V4L2_FIELD_IS_SEQUENTIAL(outfmt->field); 443 priv->interweave_swap = interweave && 444 image.pix.field == V4L2_FIELD_INTERLACED_BT; 445 446 switch (image.pix.pixelformat) { 447 case V4L2_PIX_FMT_SBGGR8: 448 case V4L2_PIX_FMT_SGBRG8: 449 case V4L2_PIX_FMT_SGRBG8: 450 case V4L2_PIX_FMT_SRGGB8: 451 case V4L2_PIX_FMT_GREY: 452 burst_size = 16; 453 passthrough_bits = 8; 454 break; 455 case V4L2_PIX_FMT_SBGGR16: 456 case V4L2_PIX_FMT_SGBRG16: 457 case V4L2_PIX_FMT_SGRBG16: 458 case V4L2_PIX_FMT_SRGGB16: 459 case V4L2_PIX_FMT_Y10: 460 case V4L2_PIX_FMT_Y12: 461 burst_size = 8; 462 passthrough_bits = 16; 463 break; 464 case V4L2_PIX_FMT_YUV420: 465 case V4L2_PIX_FMT_YVU420: 466 case V4L2_PIX_FMT_NV12: 467 burst_size = (image.pix.width & 0x3f) ? 468 ((image.pix.width & 0x1f) ? 469 ((image.pix.width & 0xf) ? 8 : 16) : 32) : 64; 470 passthrough_bits = 16; 471 /* 472 * Skip writing U and V components to odd rows (but not 473 * when enabling IDMAC interweaving, they are incompatible). 474 */ 475 if (!interweave) 476 ipu_cpmem_skip_odd_chroma_rows(priv->idmac_ch); 477 break; 478 case V4L2_PIX_FMT_YUYV: 479 case V4L2_PIX_FMT_UYVY: 480 burst_size = (image.pix.width & 0x1f) ? 481 ((image.pix.width & 0xf) ? 8 : 16) : 32; 482 passthrough_bits = 16; 483 break; 484 case V4L2_PIX_FMT_RGB565: 485 if (passthrough) { 486 burst_size = 16; 487 passthrough_bits = 8; 488 passthrough_cycles = incc->cycles; 489 break; 490 } 491 fallthrough; /* non-passthrough RGB565 (CSI-2 bus) */ 492 default: 493 burst_size = (image.pix.width & 0xf) ? 8 : 16; 494 passthrough_bits = 16; 495 break; 496 } 497 498 if (passthrough) { 499 if (priv->interweave_swap) { 500 /* start interweave scan at 1st top line (2nd line) */ 501 image.phys0 += image.pix.bytesperline; 502 image.phys1 += image.pix.bytesperline; 503 } 504 505 ipu_cpmem_set_resolution(priv->idmac_ch, 506 image.rect.width * passthrough_cycles, 507 image.rect.height); 508 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline); 509 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0); 510 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1); 511 ipu_cpmem_set_format_passthrough(priv->idmac_ch, 512 passthrough_bits); 513 } else { 514 if (priv->interweave_swap) { 515 /* start interweave scan at 1st top line (2nd line) */ 516 image.rect.top = 1; 517 } 518 519 ret = ipu_cpmem_set_image(priv->idmac_ch, &image); 520 if (ret) 521 goto unsetup_vb2; 522 } 523 524 ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size); 525 526 /* 527 * Set the channel for the direct CSI-->memory via SMFC 528 * use-case to very high priority, by enabling the watermark 529 * signal in the SMFC, enabling WM in the channel, and setting 530 * the channel priority to high. 531 * 532 * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority 533 * value. 534 * 535 * The WM's are set very low by intention here to ensure that 536 * the SMFC FIFOs do not overflow. 537 */ 538 ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01); 539 ipu_cpmem_set_high_priority(priv->idmac_ch); 540 ipu_idmac_enable_watermark(priv->idmac_ch, true); 541 ipu_cpmem_set_axi_id(priv->idmac_ch, 0); 542 543 burst_size = passthrough ? 544 (burst_size >> 3) - 1 : (burst_size >> 2) - 1; 545 546 ipu_smfc_set_burstsize(priv->smfc, burst_size); 547 548 if (interweave) 549 ipu_cpmem_interlaced_scan(priv->idmac_ch, 550 priv->interweave_swap ? 551 -image.pix.bytesperline : 552 image.pix.bytesperline, 553 image.pix.pixelformat); 554 555 ipu_idmac_set_double_buffer(priv->idmac_ch, true); 556 557 return 0; 558 559 unsetup_vb2: 560 csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED); 561 return ret; 562 } 563 564 static void csi_idmac_unsetup(struct csi_priv *priv, 565 enum vb2_buffer_state state) 566 { 567 ipu_idmac_disable_channel(priv->idmac_ch); 568 ipu_smfc_disable(priv->smfc); 569 570 csi_idmac_unsetup_vb2_buf(priv, state); 571 } 572 573 static int csi_idmac_setup(struct csi_priv *priv) 574 { 575 int ret; 576 577 ret = csi_idmac_setup_channel(priv); 578 if (ret) 579 return ret; 580 581 ipu_cpmem_dump(priv->idmac_ch); 582 ipu_dump(priv->ipu); 583 584 ipu_smfc_enable(priv->smfc); 585 586 /* set buffers ready */ 587 ipu_idmac_select_buffer(priv->idmac_ch, 0); 588 ipu_idmac_select_buffer(priv->idmac_ch, 1); 589 590 /* enable the channels */ 591 ipu_idmac_enable_channel(priv->idmac_ch); 592 593 return 0; 594 } 595 596 static int csi_idmac_start(struct csi_priv *priv) 597 { 598 struct imx_media_video_dev *vdev = priv->vdev; 599 int ret; 600 601 ret = csi_idmac_get_ipu_resources(priv); 602 if (ret) 603 return ret; 604 605 ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num); 606 607 ret = imx_media_alloc_dma_buf(priv->dev, &priv->underrun_buf, 608 vdev->fmt.sizeimage); 609 if (ret) 610 goto out_put_ipu; 611 612 priv->ipu_buf_num = 0; 613 614 /* init EOF completion waitq */ 615 init_completion(&priv->last_eof_comp); 616 priv->frame_sequence = 0; 617 priv->last_eof = false; 618 priv->nfb4eof = false; 619 620 ret = csi_idmac_setup(priv); 621 if (ret) { 622 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret); 623 goto out_free_dma_buf; 624 } 625 626 priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu, 627 priv->idmac_ch, 628 IPU_IRQ_NFB4EOF); 629 ret = devm_request_irq(priv->dev, priv->nfb4eof_irq, 630 csi_idmac_nfb4eof_interrupt, 0, 631 "imx-smfc-nfb4eof", priv); 632 if (ret) { 633 v4l2_err(&priv->sd, 634 "Error registering NFB4EOF irq: %d\n", ret); 635 goto out_unsetup; 636 } 637 638 priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch, 639 IPU_IRQ_EOF); 640 641 ret = devm_request_irq(priv->dev, priv->eof_irq, 642 csi_idmac_eof_interrupt, 0, 643 "imx-smfc-eof", priv); 644 if (ret) { 645 v4l2_err(&priv->sd, 646 "Error registering eof irq: %d\n", ret); 647 goto out_free_nfb4eof_irq; 648 } 649 650 /* start the EOF timeout timer */ 651 mod_timer(&priv->eof_timeout_timer, 652 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); 653 654 return 0; 655 656 out_free_nfb4eof_irq: 657 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv); 658 out_unsetup: 659 csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED); 660 out_free_dma_buf: 661 imx_media_free_dma_buf(priv->dev, &priv->underrun_buf); 662 out_put_ipu: 663 csi_idmac_put_ipu_resources(priv); 664 return ret; 665 } 666 667 static void csi_idmac_wait_last_eof(struct csi_priv *priv) 668 { 669 unsigned long flags; 670 int ret; 671 672 /* mark next EOF interrupt as the last before stream off */ 673 spin_lock_irqsave(&priv->irqlock, flags); 674 priv->last_eof = true; 675 spin_unlock_irqrestore(&priv->irqlock, flags); 676 677 /* 678 * and then wait for interrupt handler to mark completion. 679 */ 680 ret = wait_for_completion_timeout( 681 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); 682 if (ret == 0) 683 v4l2_warn(&priv->sd, "wait last EOF timeout\n"); 684 } 685 686 static void csi_idmac_stop(struct csi_priv *priv) 687 { 688 devm_free_irq(priv->dev, priv->eof_irq, priv); 689 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv); 690 691 csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR); 692 693 imx_media_free_dma_buf(priv->dev, &priv->underrun_buf); 694 695 /* cancel the EOF timeout timer */ 696 del_timer_sync(&priv->eof_timeout_timer); 697 698 csi_idmac_put_ipu_resources(priv); 699 } 700 701 /* Update the CSI whole sensor and active windows */ 702 static int csi_setup(struct csi_priv *priv) 703 { 704 struct v4l2_mbus_framefmt *infmt, *outfmt; 705 const struct imx_media_pixfmt *incc; 706 struct v4l2_mbus_config mbus_cfg; 707 struct v4l2_mbus_framefmt if_fmt; 708 struct v4l2_rect crop; 709 710 infmt = &priv->format_mbus[CSI_SINK_PAD]; 711 incc = priv->cc[CSI_SINK_PAD]; 712 outfmt = &priv->format_mbus[priv->active_output_pad]; 713 714 /* compose mbus_config from the upstream endpoint */ 715 mbus_cfg.type = priv->upstream_ep.bus_type; 716 mbus_cfg.flags = is_parallel_bus(&priv->upstream_ep) ? 717 priv->upstream_ep.bus.parallel.flags : 718 priv->upstream_ep.bus.mipi_csi2.flags; 719 720 if_fmt = *infmt; 721 crop = priv->crop; 722 723 /* 724 * if cycles is set, we need to handle this over multiple cycles as 725 * generic/bayer data 726 */ 727 if (is_parallel_bus(&priv->upstream_ep) && incc->cycles) { 728 if_fmt.width *= incc->cycles; 729 crop.width *= incc->cycles; 730 } 731 732 ipu_csi_set_window(priv->csi, &crop); 733 734 ipu_csi_set_downsize(priv->csi, 735 priv->crop.width == 2 * priv->compose.width, 736 priv->crop.height == 2 * priv->compose.height); 737 738 ipu_csi_init_interface(priv->csi, &mbus_cfg, &if_fmt, outfmt); 739 740 ipu_csi_set_dest(priv->csi, priv->dest); 741 742 if (priv->dest == IPU_CSI_DEST_IDMAC) 743 ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc, 744 priv->skip->max_ratio - 1, 0); 745 746 ipu_csi_dump(priv->csi); 747 748 return 0; 749 } 750 751 static int csi_start(struct csi_priv *priv) 752 { 753 struct v4l2_fract *output_fi; 754 int ret; 755 756 output_fi = &priv->frame_interval[priv->active_output_pad]; 757 758 /* start upstream */ 759 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1); 760 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0; 761 if (ret) 762 return ret; 763 764 if (priv->dest == IPU_CSI_DEST_IDMAC) { 765 ret = csi_idmac_start(priv); 766 if (ret) 767 goto stop_upstream; 768 } 769 770 ret = csi_setup(priv); 771 if (ret) 772 goto idmac_stop; 773 774 /* start the frame interval monitor */ 775 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) { 776 ret = imx_media_fim_set_stream(priv->fim, output_fi, true); 777 if (ret) 778 goto idmac_stop; 779 } 780 781 ret = ipu_csi_enable(priv->csi); 782 if (ret) { 783 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret); 784 goto fim_off; 785 } 786 787 return 0; 788 789 fim_off: 790 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) 791 imx_media_fim_set_stream(priv->fim, NULL, false); 792 idmac_stop: 793 if (priv->dest == IPU_CSI_DEST_IDMAC) 794 csi_idmac_stop(priv); 795 stop_upstream: 796 v4l2_subdev_call(priv->src_sd, video, s_stream, 0); 797 return ret; 798 } 799 800 static void csi_stop(struct csi_priv *priv) 801 { 802 if (priv->dest == IPU_CSI_DEST_IDMAC) 803 csi_idmac_wait_last_eof(priv); 804 805 /* 806 * Disable the CSI asap, after syncing with the last EOF. 807 * Doing so after the IDMA channel is disabled has shown to 808 * create hard system-wide hangs. 809 */ 810 ipu_csi_disable(priv->csi); 811 812 /* stop upstream */ 813 v4l2_subdev_call(priv->src_sd, video, s_stream, 0); 814 815 if (priv->dest == IPU_CSI_DEST_IDMAC) { 816 csi_idmac_stop(priv); 817 818 /* stop the frame interval monitor */ 819 if (priv->fim) 820 imx_media_fim_set_stream(priv->fim, NULL, false); 821 } 822 } 823 824 static const struct csi_skip_desc csi_skip[12] = { 825 { 1, 1, 0x00 }, /* Keep all frames */ 826 { 5, 6, 0x10 }, /* Skip every sixth frame */ 827 { 4, 5, 0x08 }, /* Skip every fifth frame */ 828 { 3, 4, 0x04 }, /* Skip every fourth frame */ 829 { 2, 3, 0x02 }, /* Skip every third frame */ 830 { 3, 5, 0x0a }, /* Skip frames 1 and 3 of every 5 */ 831 { 1, 2, 0x01 }, /* Skip every second frame */ 832 { 2, 5, 0x0b }, /* Keep frames 1 and 4 of every 5 */ 833 { 1, 3, 0x03 }, /* Keep one in three frames */ 834 { 1, 4, 0x07 }, /* Keep one in four frames */ 835 { 1, 5, 0x0f }, /* Keep one in five frames */ 836 { 1, 6, 0x1f }, /* Keep one in six frames */ 837 }; 838 839 static void csi_apply_skip_interval(const struct csi_skip_desc *skip, 840 struct v4l2_fract *interval) 841 { 842 unsigned int div; 843 844 interval->numerator *= skip->max_ratio; 845 interval->denominator *= skip->keep; 846 847 /* Reduce fraction to lowest terms */ 848 div = gcd(interval->numerator, interval->denominator); 849 if (div > 1) { 850 interval->numerator /= div; 851 interval->denominator /= div; 852 } 853 } 854 855 /* 856 * Find the skip pattern to produce the output frame interval closest to the 857 * requested one, for the given input frame interval. Updates the output frame 858 * interval to the exact value. 859 */ 860 static const struct csi_skip_desc *csi_find_best_skip(struct v4l2_fract *in, 861 struct v4l2_fract *out) 862 { 863 const struct csi_skip_desc *skip = &csi_skip[0], *best_skip = skip; 864 u32 min_err = UINT_MAX; 865 u64 want_us; 866 int i; 867 868 /* Default to 1:1 ratio */ 869 if (out->numerator == 0 || out->denominator == 0 || 870 in->numerator == 0 || in->denominator == 0) { 871 *out = *in; 872 return best_skip; 873 } 874 875 want_us = div_u64((u64)USEC_PER_SEC * out->numerator, out->denominator); 876 877 /* Find the reduction closest to the requested time per frame */ 878 for (i = 0; i < ARRAY_SIZE(csi_skip); i++, skip++) { 879 u64 tmp, err; 880 881 tmp = div_u64((u64)USEC_PER_SEC * in->numerator * 882 skip->max_ratio, in->denominator * skip->keep); 883 884 err = abs((s64)tmp - want_us); 885 if (err < min_err) { 886 min_err = err; 887 best_skip = skip; 888 } 889 } 890 891 *out = *in; 892 csi_apply_skip_interval(best_skip, out); 893 894 return best_skip; 895 } 896 897 /* 898 * V4L2 subdev operations. 899 */ 900 901 static int csi_g_frame_interval(struct v4l2_subdev *sd, 902 struct v4l2_subdev_frame_interval *fi) 903 { 904 struct csi_priv *priv = v4l2_get_subdevdata(sd); 905 906 if (fi->pad >= CSI_NUM_PADS) 907 return -EINVAL; 908 909 mutex_lock(&priv->lock); 910 911 fi->interval = priv->frame_interval[fi->pad]; 912 913 mutex_unlock(&priv->lock); 914 915 return 0; 916 } 917 918 static int csi_s_frame_interval(struct v4l2_subdev *sd, 919 struct v4l2_subdev_frame_interval *fi) 920 { 921 struct csi_priv *priv = v4l2_get_subdevdata(sd); 922 struct v4l2_fract *input_fi; 923 int ret = 0; 924 925 mutex_lock(&priv->lock); 926 927 input_fi = &priv->frame_interval[CSI_SINK_PAD]; 928 929 switch (fi->pad) { 930 case CSI_SINK_PAD: 931 /* No limits on valid input frame intervals */ 932 if (fi->interval.numerator == 0 || 933 fi->interval.denominator == 0) 934 fi->interval = *input_fi; 935 /* Reset output intervals and frame skipping ratio to 1:1 */ 936 priv->frame_interval[CSI_SRC_PAD_IDMAC] = fi->interval; 937 priv->frame_interval[CSI_SRC_PAD_DIRECT] = fi->interval; 938 priv->skip = &csi_skip[0]; 939 break; 940 case CSI_SRC_PAD_IDMAC: 941 /* 942 * frame interval at IDMAC output pad depends on input 943 * interval, modified by frame skipping. 944 */ 945 priv->skip = csi_find_best_skip(input_fi, &fi->interval); 946 break; 947 case CSI_SRC_PAD_DIRECT: 948 /* 949 * frame interval at DIRECT output pad is same as input 950 * interval. 951 */ 952 fi->interval = *input_fi; 953 break; 954 default: 955 ret = -EINVAL; 956 goto out; 957 } 958 959 priv->frame_interval[fi->pad] = fi->interval; 960 out: 961 mutex_unlock(&priv->lock); 962 return ret; 963 } 964 965 static int csi_s_stream(struct v4l2_subdev *sd, int enable) 966 { 967 struct csi_priv *priv = v4l2_get_subdevdata(sd); 968 int ret = 0; 969 970 mutex_lock(&priv->lock); 971 972 if (!priv->src_sd || !priv->sink) { 973 ret = -EPIPE; 974 goto out; 975 } 976 977 /* 978 * enable/disable streaming only if stream_count is 979 * going from 0 to 1 / 1 to 0. 980 */ 981 if (priv->stream_count != !enable) 982 goto update_count; 983 984 if (enable) { 985 dev_dbg(priv->dev, "stream ON\n"); 986 ret = csi_start(priv); 987 if (ret) 988 goto out; 989 } else { 990 dev_dbg(priv->dev, "stream OFF\n"); 991 csi_stop(priv); 992 } 993 994 update_count: 995 priv->stream_count += enable ? 1 : -1; 996 if (priv->stream_count < 0) 997 priv->stream_count = 0; 998 out: 999 mutex_unlock(&priv->lock); 1000 return ret; 1001 } 1002 1003 static int csi_link_setup(struct media_entity *entity, 1004 const struct media_pad *local, 1005 const struct media_pad *remote, u32 flags) 1006 { 1007 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); 1008 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1009 struct v4l2_subdev *remote_sd; 1010 int ret = 0; 1011 1012 dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name, 1013 local->entity->name); 1014 1015 mutex_lock(&priv->lock); 1016 1017 if (local->flags & MEDIA_PAD_FL_SINK) { 1018 if (!is_media_entity_v4l2_subdev(remote->entity)) { 1019 ret = -EINVAL; 1020 goto out; 1021 } 1022 1023 remote_sd = media_entity_to_v4l2_subdev(remote->entity); 1024 1025 if (flags & MEDIA_LNK_FL_ENABLED) { 1026 if (priv->src_sd) { 1027 ret = -EBUSY; 1028 goto out; 1029 } 1030 priv->src_sd = remote_sd; 1031 } else { 1032 priv->src_sd = NULL; 1033 } 1034 1035 goto out; 1036 } 1037 1038 /* this is a source pad */ 1039 1040 if (flags & MEDIA_LNK_FL_ENABLED) { 1041 if (priv->sink) { 1042 ret = -EBUSY; 1043 goto out; 1044 } 1045 } else { 1046 v4l2_ctrl_handler_free(&priv->ctrl_hdlr); 1047 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0); 1048 priv->sink = NULL; 1049 /* do not apply IC burst alignment in csi_try_crop */ 1050 priv->active_output_pad = CSI_SRC_PAD_IDMAC; 1051 goto out; 1052 } 1053 1054 /* record which output pad is now active */ 1055 priv->active_output_pad = local->index; 1056 1057 /* set CSI destination */ 1058 if (local->index == CSI_SRC_PAD_IDMAC) { 1059 if (!is_media_entity_v4l2_video_device(remote->entity)) { 1060 ret = -EINVAL; 1061 goto out; 1062 } 1063 1064 if (priv->fim) { 1065 ret = imx_media_fim_add_controls(priv->fim); 1066 if (ret) 1067 goto out; 1068 } 1069 1070 priv->dest = IPU_CSI_DEST_IDMAC; 1071 } else { 1072 if (!is_media_entity_v4l2_subdev(remote->entity)) { 1073 ret = -EINVAL; 1074 goto out; 1075 } 1076 1077 remote_sd = media_entity_to_v4l2_subdev(remote->entity); 1078 switch (remote_sd->grp_id) { 1079 case IMX_MEDIA_GRP_ID_IPU_VDIC: 1080 priv->dest = IPU_CSI_DEST_VDIC; 1081 break; 1082 case IMX_MEDIA_GRP_ID_IPU_IC_PRP: 1083 priv->dest = IPU_CSI_DEST_IC; 1084 break; 1085 default: 1086 ret = -EINVAL; 1087 goto out; 1088 } 1089 } 1090 1091 priv->sink = remote->entity; 1092 out: 1093 mutex_unlock(&priv->lock); 1094 return ret; 1095 } 1096 1097 static int csi_link_validate(struct v4l2_subdev *sd, 1098 struct media_link *link, 1099 struct v4l2_subdev_format *source_fmt, 1100 struct v4l2_subdev_format *sink_fmt) 1101 { 1102 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1103 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 }; 1104 bool is_csi2; 1105 int ret; 1106 1107 ret = v4l2_subdev_link_validate_default(sd, link, 1108 source_fmt, sink_fmt); 1109 if (ret) 1110 return ret; 1111 1112 ret = csi_get_upstream_endpoint(priv, &upstream_ep); 1113 if (ret) { 1114 v4l2_err(&priv->sd, "failed to find upstream endpoint\n"); 1115 return ret; 1116 } 1117 1118 mutex_lock(&priv->lock); 1119 1120 priv->upstream_ep = upstream_ep; 1121 is_csi2 = !is_parallel_bus(&upstream_ep); 1122 if (is_csi2) { 1123 /* 1124 * NOTE! It seems the virtual channels from the mipi csi-2 1125 * receiver are used only for routing by the video mux's, 1126 * or for hard-wired routing to the CSI's. Once the stream 1127 * enters the CSI's however, they are treated internally 1128 * in the IPU as virtual channel 0. 1129 */ 1130 ipu_csi_set_mipi_datatype(priv->csi, 0, 1131 &priv->format_mbus[CSI_SINK_PAD]); 1132 } 1133 1134 /* select either parallel or MIPI-CSI2 as input to CSI */ 1135 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2); 1136 1137 mutex_unlock(&priv->lock); 1138 return ret; 1139 } 1140 1141 static struct v4l2_mbus_framefmt * 1142 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg, 1143 unsigned int pad, enum v4l2_subdev_format_whence which) 1144 { 1145 if (which == V4L2_SUBDEV_FORMAT_TRY) 1146 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad); 1147 else 1148 return &priv->format_mbus[pad]; 1149 } 1150 1151 static struct v4l2_rect * 1152 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg, 1153 enum v4l2_subdev_format_whence which) 1154 { 1155 if (which == V4L2_SUBDEV_FORMAT_TRY) 1156 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD); 1157 else 1158 return &priv->crop; 1159 } 1160 1161 static struct v4l2_rect * 1162 __csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg, 1163 enum v4l2_subdev_format_whence which) 1164 { 1165 if (which == V4L2_SUBDEV_FORMAT_TRY) 1166 return v4l2_subdev_get_try_compose(&priv->sd, cfg, 1167 CSI_SINK_PAD); 1168 else 1169 return &priv->compose; 1170 } 1171 1172 static void csi_try_crop(struct csi_priv *priv, 1173 struct v4l2_rect *crop, 1174 struct v4l2_subdev_pad_config *cfg, 1175 struct v4l2_mbus_framefmt *infmt, 1176 struct v4l2_fwnode_endpoint *upstream_ep) 1177 { 1178 u32 in_height; 1179 1180 crop->width = min_t(__u32, infmt->width, crop->width); 1181 if (crop->left + crop->width > infmt->width) 1182 crop->left = infmt->width - crop->width; 1183 /* adjust crop left/width to h/w alignment restrictions */ 1184 crop->left &= ~0x3; 1185 if (priv->active_output_pad == CSI_SRC_PAD_DIRECT) 1186 crop->width &= ~0x7; /* multiple of 8 pixels (IC burst) */ 1187 else 1188 crop->width &= ~0x1; /* multiple of 2 pixels */ 1189 1190 in_height = infmt->height; 1191 if (infmt->field == V4L2_FIELD_ALTERNATE) 1192 in_height *= 2; 1193 1194 /* 1195 * FIXME: not sure why yet, but on interlaced bt.656, 1196 * changing the vertical cropping causes loss of vertical 1197 * sync, so fix it to NTSC/PAL active lines. NTSC contains 1198 * 2 extra lines of active video that need to be cropped. 1199 */ 1200 if (upstream_ep->bus_type == V4L2_MBUS_BT656 && 1201 (V4L2_FIELD_HAS_BOTH(infmt->field) || 1202 infmt->field == V4L2_FIELD_ALTERNATE)) { 1203 crop->height = in_height; 1204 crop->top = (in_height == 480) ? 2 : 0; 1205 } else { 1206 crop->height = min_t(__u32, in_height, crop->height); 1207 if (crop->top + crop->height > in_height) 1208 crop->top = in_height - crop->height; 1209 } 1210 } 1211 1212 static int csi_enum_mbus_code(struct v4l2_subdev *sd, 1213 struct v4l2_subdev_pad_config *cfg, 1214 struct v4l2_subdev_mbus_code_enum *code) 1215 { 1216 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1217 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 }; 1218 const struct imx_media_pixfmt *incc; 1219 struct v4l2_mbus_framefmt *infmt; 1220 int ret = 0; 1221 1222 mutex_lock(&priv->lock); 1223 1224 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which); 1225 incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY); 1226 1227 switch (code->pad) { 1228 case CSI_SINK_PAD: 1229 ret = imx_media_enum_mbus_formats(&code->code, code->index, 1230 PIXFMT_SEL_ANY); 1231 break; 1232 case CSI_SRC_PAD_DIRECT: 1233 case CSI_SRC_PAD_IDMAC: 1234 ret = csi_get_upstream_endpoint(priv, &upstream_ep); 1235 if (ret) { 1236 v4l2_err(&priv->sd, "failed to find upstream endpoint\n"); 1237 goto out; 1238 } 1239 1240 if (requires_passthrough(&upstream_ep, infmt, incc)) { 1241 if (code->index != 0) { 1242 ret = -EINVAL; 1243 goto out; 1244 } 1245 code->code = infmt->code; 1246 } else { 1247 enum imx_pixfmt_sel fmt_sel = 1248 (incc->cs == IPUV3_COLORSPACE_YUV) ? 1249 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB; 1250 1251 ret = imx_media_enum_ipu_formats(&code->code, 1252 code->index, 1253 fmt_sel); 1254 } 1255 break; 1256 default: 1257 ret = -EINVAL; 1258 } 1259 1260 out: 1261 mutex_unlock(&priv->lock); 1262 return ret; 1263 } 1264 1265 static int csi_enum_frame_size(struct v4l2_subdev *sd, 1266 struct v4l2_subdev_pad_config *cfg, 1267 struct v4l2_subdev_frame_size_enum *fse) 1268 { 1269 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1270 struct v4l2_rect *crop; 1271 int ret = 0; 1272 1273 if (fse->pad >= CSI_NUM_PADS || 1274 fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3)) 1275 return -EINVAL; 1276 1277 mutex_lock(&priv->lock); 1278 1279 if (fse->pad == CSI_SINK_PAD) { 1280 fse->min_width = MIN_W; 1281 fse->max_width = MAX_W; 1282 fse->min_height = MIN_H; 1283 fse->max_height = MAX_H; 1284 } else { 1285 crop = __csi_get_crop(priv, cfg, fse->which); 1286 1287 fse->min_width = fse->index & 1 ? 1288 crop->width / 2 : crop->width; 1289 fse->max_width = fse->min_width; 1290 fse->min_height = fse->index & 2 ? 1291 crop->height / 2 : crop->height; 1292 fse->max_height = fse->min_height; 1293 } 1294 1295 mutex_unlock(&priv->lock); 1296 return ret; 1297 } 1298 1299 static int csi_enum_frame_interval(struct v4l2_subdev *sd, 1300 struct v4l2_subdev_pad_config *cfg, 1301 struct v4l2_subdev_frame_interval_enum *fie) 1302 { 1303 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1304 struct v4l2_fract *input_fi; 1305 struct v4l2_rect *crop; 1306 int ret = 0; 1307 1308 if (fie->pad >= CSI_NUM_PADS || 1309 fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ? 1310 1 : ARRAY_SIZE(csi_skip))) 1311 return -EINVAL; 1312 1313 mutex_lock(&priv->lock); 1314 1315 input_fi = &priv->frame_interval[CSI_SINK_PAD]; 1316 crop = __csi_get_crop(priv, cfg, fie->which); 1317 1318 if ((fie->width != crop->width && fie->width != crop->width / 2) || 1319 (fie->height != crop->height && fie->height != crop->height / 2)) { 1320 ret = -EINVAL; 1321 goto out; 1322 } 1323 1324 fie->interval = *input_fi; 1325 1326 if (fie->pad == CSI_SRC_PAD_IDMAC) 1327 csi_apply_skip_interval(&csi_skip[fie->index], 1328 &fie->interval); 1329 1330 out: 1331 mutex_unlock(&priv->lock); 1332 return ret; 1333 } 1334 1335 static int csi_get_fmt(struct v4l2_subdev *sd, 1336 struct v4l2_subdev_pad_config *cfg, 1337 struct v4l2_subdev_format *sdformat) 1338 { 1339 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1340 struct v4l2_mbus_framefmt *fmt; 1341 int ret = 0; 1342 1343 if (sdformat->pad >= CSI_NUM_PADS) 1344 return -EINVAL; 1345 1346 mutex_lock(&priv->lock); 1347 1348 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which); 1349 if (!fmt) { 1350 ret = -EINVAL; 1351 goto out; 1352 } 1353 1354 sdformat->format = *fmt; 1355 out: 1356 mutex_unlock(&priv->lock); 1357 return ret; 1358 } 1359 1360 static void csi_try_field(struct csi_priv *priv, 1361 struct v4l2_subdev_pad_config *cfg, 1362 struct v4l2_subdev_format *sdformat) 1363 { 1364 struct v4l2_mbus_framefmt *infmt = 1365 __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which); 1366 1367 /* 1368 * no restrictions on sink pad field type except must 1369 * be initialized. 1370 */ 1371 if (sdformat->pad == CSI_SINK_PAD) { 1372 if (sdformat->format.field == V4L2_FIELD_ANY) 1373 sdformat->format.field = V4L2_FIELD_NONE; 1374 return; 1375 } 1376 1377 switch (infmt->field) { 1378 case V4L2_FIELD_SEQ_TB: 1379 case V4L2_FIELD_SEQ_BT: 1380 /* 1381 * If the user requests sequential at the source pad, 1382 * allow it (along with possibly inverting field order). 1383 * Otherwise passthrough the field type. 1384 */ 1385 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field)) 1386 sdformat->format.field = infmt->field; 1387 break; 1388 case V4L2_FIELD_ALTERNATE: 1389 /* 1390 * This driver does not support alternate field mode, and 1391 * the CSI captures a whole frame, so the CSI never presents 1392 * alternate mode at its source pads. If user has not 1393 * already requested sequential, translate ALTERNATE at 1394 * sink pad to SEQ_TB or SEQ_BT at the source pad depending 1395 * on input height (assume NTSC BT order if 480 total active 1396 * frame lines, otherwise PAL TB order). 1397 */ 1398 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field)) 1399 sdformat->format.field = (infmt->height == 480 / 2) ? 1400 V4L2_FIELD_SEQ_BT : V4L2_FIELD_SEQ_TB; 1401 break; 1402 default: 1403 /* Passthrough for all other input field types */ 1404 sdformat->format.field = infmt->field; 1405 break; 1406 } 1407 } 1408 1409 static void csi_try_fmt(struct csi_priv *priv, 1410 struct v4l2_fwnode_endpoint *upstream_ep, 1411 struct v4l2_subdev_pad_config *cfg, 1412 struct v4l2_subdev_format *sdformat, 1413 struct v4l2_rect *crop, 1414 struct v4l2_rect *compose, 1415 const struct imx_media_pixfmt **cc) 1416 { 1417 const struct imx_media_pixfmt *incc; 1418 struct v4l2_mbus_framefmt *infmt; 1419 u32 code; 1420 1421 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which); 1422 1423 switch (sdformat->pad) { 1424 case CSI_SRC_PAD_DIRECT: 1425 case CSI_SRC_PAD_IDMAC: 1426 incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY); 1427 1428 sdformat->format.width = compose->width; 1429 sdformat->format.height = compose->height; 1430 1431 if (requires_passthrough(upstream_ep, infmt, incc)) { 1432 sdformat->format.code = infmt->code; 1433 *cc = incc; 1434 } else { 1435 enum imx_pixfmt_sel fmt_sel = 1436 (incc->cs == IPUV3_COLORSPACE_YUV) ? 1437 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB; 1438 1439 *cc = imx_media_find_ipu_format(sdformat->format.code, 1440 fmt_sel); 1441 if (!*cc) { 1442 imx_media_enum_ipu_formats(&code, 0, fmt_sel); 1443 *cc = imx_media_find_ipu_format(code, fmt_sel); 1444 sdformat->format.code = (*cc)->codes[0]; 1445 } 1446 } 1447 1448 csi_try_field(priv, cfg, sdformat); 1449 1450 /* propagate colorimetry from sink */ 1451 sdformat->format.colorspace = infmt->colorspace; 1452 sdformat->format.xfer_func = infmt->xfer_func; 1453 sdformat->format.quantization = infmt->quantization; 1454 sdformat->format.ycbcr_enc = infmt->ycbcr_enc; 1455 1456 break; 1457 case CSI_SINK_PAD: 1458 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W, 1459 W_ALIGN, &sdformat->format.height, 1460 MIN_H, MAX_H, H_ALIGN, S_ALIGN); 1461 1462 *cc = imx_media_find_mbus_format(sdformat->format.code, 1463 PIXFMT_SEL_ANY); 1464 if (!*cc) { 1465 imx_media_enum_mbus_formats(&code, 0, 1466 PIXFMT_SEL_YUV_RGB); 1467 *cc = imx_media_find_mbus_format(code, 1468 PIXFMT_SEL_YUV_RGB); 1469 sdformat->format.code = (*cc)->codes[0]; 1470 } 1471 1472 csi_try_field(priv, cfg, sdformat); 1473 1474 /* Reset crop and compose rectangles */ 1475 crop->left = 0; 1476 crop->top = 0; 1477 crop->width = sdformat->format.width; 1478 crop->height = sdformat->format.height; 1479 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) 1480 crop->height *= 2; 1481 csi_try_crop(priv, crop, cfg, &sdformat->format, upstream_ep); 1482 compose->left = 0; 1483 compose->top = 0; 1484 compose->width = crop->width; 1485 compose->height = crop->height; 1486 1487 break; 1488 } 1489 1490 imx_media_try_colorimetry(&sdformat->format, 1491 priv->active_output_pad == CSI_SRC_PAD_DIRECT); 1492 } 1493 1494 static int csi_set_fmt(struct v4l2_subdev *sd, 1495 struct v4l2_subdev_pad_config *cfg, 1496 struct v4l2_subdev_format *sdformat) 1497 { 1498 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1499 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 }; 1500 const struct imx_media_pixfmt *cc; 1501 struct v4l2_mbus_framefmt *fmt; 1502 struct v4l2_rect *crop, *compose; 1503 int ret; 1504 1505 if (sdformat->pad >= CSI_NUM_PADS) 1506 return -EINVAL; 1507 1508 ret = csi_get_upstream_endpoint(priv, &upstream_ep); 1509 if (ret) { 1510 v4l2_err(&priv->sd, "failed to find upstream endpoint\n"); 1511 return ret; 1512 } 1513 1514 mutex_lock(&priv->lock); 1515 1516 if (priv->stream_count > 0) { 1517 ret = -EBUSY; 1518 goto out; 1519 } 1520 1521 crop = __csi_get_crop(priv, cfg, sdformat->which); 1522 compose = __csi_get_compose(priv, cfg, sdformat->which); 1523 1524 csi_try_fmt(priv, &upstream_ep, cfg, sdformat, crop, compose, &cc); 1525 1526 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which); 1527 *fmt = sdformat->format; 1528 1529 if (sdformat->pad == CSI_SINK_PAD) { 1530 int pad; 1531 1532 /* propagate format to source pads */ 1533 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) { 1534 const struct imx_media_pixfmt *outcc; 1535 struct v4l2_mbus_framefmt *outfmt; 1536 struct v4l2_subdev_format format; 1537 1538 format.pad = pad; 1539 format.which = sdformat->which; 1540 format.format = sdformat->format; 1541 csi_try_fmt(priv, &upstream_ep, cfg, &format, 1542 NULL, compose, &outcc); 1543 1544 outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which); 1545 *outfmt = format.format; 1546 1547 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE) 1548 priv->cc[pad] = outcc; 1549 } 1550 } 1551 1552 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE) 1553 priv->cc[sdformat->pad] = cc; 1554 1555 out: 1556 mutex_unlock(&priv->lock); 1557 return ret; 1558 } 1559 1560 static int csi_get_selection(struct v4l2_subdev *sd, 1561 struct v4l2_subdev_pad_config *cfg, 1562 struct v4l2_subdev_selection *sel) 1563 { 1564 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1565 struct v4l2_mbus_framefmt *infmt; 1566 struct v4l2_rect *crop, *compose; 1567 int ret = 0; 1568 1569 if (sel->pad != CSI_SINK_PAD) 1570 return -EINVAL; 1571 1572 mutex_lock(&priv->lock); 1573 1574 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which); 1575 crop = __csi_get_crop(priv, cfg, sel->which); 1576 compose = __csi_get_compose(priv, cfg, sel->which); 1577 1578 switch (sel->target) { 1579 case V4L2_SEL_TGT_CROP_BOUNDS: 1580 sel->r.left = 0; 1581 sel->r.top = 0; 1582 sel->r.width = infmt->width; 1583 sel->r.height = infmt->height; 1584 if (infmt->field == V4L2_FIELD_ALTERNATE) 1585 sel->r.height *= 2; 1586 break; 1587 case V4L2_SEL_TGT_CROP: 1588 sel->r = *crop; 1589 break; 1590 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 1591 sel->r.left = 0; 1592 sel->r.top = 0; 1593 sel->r.width = crop->width; 1594 sel->r.height = crop->height; 1595 break; 1596 case V4L2_SEL_TGT_COMPOSE: 1597 sel->r = *compose; 1598 break; 1599 default: 1600 ret = -EINVAL; 1601 } 1602 1603 mutex_unlock(&priv->lock); 1604 return ret; 1605 } 1606 1607 static int csi_set_scale(u32 *compose, u32 crop, u32 flags) 1608 { 1609 if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) == 1610 (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) && 1611 *compose != crop && *compose != crop / 2) 1612 return -ERANGE; 1613 1614 if (*compose <= crop / 2 || 1615 (*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) || 1616 (*compose < crop && (flags & V4L2_SEL_FLAG_LE))) 1617 *compose = crop / 2; 1618 else 1619 *compose = crop; 1620 1621 return 0; 1622 } 1623 1624 static int csi_set_selection(struct v4l2_subdev *sd, 1625 struct v4l2_subdev_pad_config *cfg, 1626 struct v4l2_subdev_selection *sel) 1627 { 1628 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1629 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 }; 1630 struct v4l2_mbus_framefmt *infmt; 1631 struct v4l2_rect *crop, *compose; 1632 int pad, ret; 1633 1634 if (sel->pad != CSI_SINK_PAD) 1635 return -EINVAL; 1636 1637 ret = csi_get_upstream_endpoint(priv, &upstream_ep); 1638 if (ret) { 1639 v4l2_err(&priv->sd, "failed to find upstream endpoint\n"); 1640 return ret; 1641 } 1642 1643 mutex_lock(&priv->lock); 1644 1645 if (priv->stream_count > 0) { 1646 ret = -EBUSY; 1647 goto out; 1648 } 1649 1650 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which); 1651 crop = __csi_get_crop(priv, cfg, sel->which); 1652 compose = __csi_get_compose(priv, cfg, sel->which); 1653 1654 switch (sel->target) { 1655 case V4L2_SEL_TGT_CROP: 1656 /* 1657 * Modifying the crop rectangle always changes the format on 1658 * the source pads. If the KEEP_CONFIG flag is set, just return 1659 * the current crop rectangle. 1660 */ 1661 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) { 1662 sel->r = priv->crop; 1663 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) 1664 *crop = sel->r; 1665 goto out; 1666 } 1667 1668 csi_try_crop(priv, &sel->r, cfg, infmt, &upstream_ep); 1669 1670 *crop = sel->r; 1671 1672 /* Reset scaling to 1:1 */ 1673 compose->width = crop->width; 1674 compose->height = crop->height; 1675 break; 1676 case V4L2_SEL_TGT_COMPOSE: 1677 /* 1678 * Modifying the compose rectangle always changes the format on 1679 * the source pads. If the KEEP_CONFIG flag is set, just return 1680 * the current compose rectangle. 1681 */ 1682 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) { 1683 sel->r = priv->compose; 1684 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) 1685 *compose = sel->r; 1686 goto out; 1687 } 1688 1689 sel->r.left = 0; 1690 sel->r.top = 0; 1691 ret = csi_set_scale(&sel->r.width, crop->width, sel->flags); 1692 if (ret) 1693 goto out; 1694 ret = csi_set_scale(&sel->r.height, crop->height, sel->flags); 1695 if (ret) 1696 goto out; 1697 1698 *compose = sel->r; 1699 break; 1700 default: 1701 ret = -EINVAL; 1702 goto out; 1703 } 1704 1705 /* Reset source pads to sink compose rectangle */ 1706 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) { 1707 struct v4l2_mbus_framefmt *outfmt; 1708 1709 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which); 1710 outfmt->width = compose->width; 1711 outfmt->height = compose->height; 1712 } 1713 1714 out: 1715 mutex_unlock(&priv->lock); 1716 return ret; 1717 } 1718 1719 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, 1720 struct v4l2_event_subscription *sub) 1721 { 1722 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR) 1723 return -EINVAL; 1724 if (sub->id != 0) 1725 return -EINVAL; 1726 1727 return v4l2_event_subscribe(fh, sub, 0, NULL); 1728 } 1729 1730 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, 1731 struct v4l2_event_subscription *sub) 1732 { 1733 return v4l2_event_unsubscribe(fh, sub); 1734 } 1735 1736 static int csi_registered(struct v4l2_subdev *sd) 1737 { 1738 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1739 struct ipu_csi *csi; 1740 int i, ret; 1741 u32 code; 1742 1743 /* get handle to IPU CSI */ 1744 csi = ipu_csi_get(priv->ipu, priv->csi_id); 1745 if (IS_ERR(csi)) { 1746 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id); 1747 return PTR_ERR(csi); 1748 } 1749 priv->csi = csi; 1750 1751 for (i = 0; i < CSI_NUM_PADS; i++) { 1752 code = 0; 1753 if (i != CSI_SINK_PAD) 1754 imx_media_enum_ipu_formats(&code, 0, PIXFMT_SEL_YUV); 1755 1756 /* set a default mbus format */ 1757 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i], 1758 IMX_MEDIA_DEF_PIX_WIDTH, 1759 IMX_MEDIA_DEF_PIX_HEIGHT, code, 1760 V4L2_FIELD_NONE, &priv->cc[i]); 1761 if (ret) 1762 goto put_csi; 1763 1764 /* init default frame interval */ 1765 priv->frame_interval[i].numerator = 1; 1766 priv->frame_interval[i].denominator = 30; 1767 } 1768 1769 /* disable frame skipping */ 1770 priv->skip = &csi_skip[0]; 1771 1772 /* init default crop and compose rectangle sizes */ 1773 priv->crop.width = IMX_MEDIA_DEF_PIX_WIDTH; 1774 priv->crop.height = IMX_MEDIA_DEF_PIX_HEIGHT; 1775 priv->compose.width = IMX_MEDIA_DEF_PIX_WIDTH; 1776 priv->compose.height = IMX_MEDIA_DEF_PIX_HEIGHT; 1777 1778 priv->fim = imx_media_fim_init(&priv->sd); 1779 if (IS_ERR(priv->fim)) { 1780 ret = PTR_ERR(priv->fim); 1781 goto put_csi; 1782 } 1783 1784 priv->vdev = imx_media_capture_device_init(priv->sd.dev, &priv->sd, 1785 CSI_SRC_PAD_IDMAC, true); 1786 if (IS_ERR(priv->vdev)) { 1787 ret = PTR_ERR(priv->vdev); 1788 goto free_fim; 1789 } 1790 1791 ret = imx_media_capture_device_register(priv->vdev, 0); 1792 if (ret) 1793 goto remove_vdev; 1794 1795 return 0; 1796 1797 remove_vdev: 1798 imx_media_capture_device_remove(priv->vdev); 1799 free_fim: 1800 if (priv->fim) 1801 imx_media_fim_free(priv->fim); 1802 put_csi: 1803 ipu_csi_put(priv->csi); 1804 return ret; 1805 } 1806 1807 static void csi_unregistered(struct v4l2_subdev *sd) 1808 { 1809 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1810 1811 imx_media_capture_device_unregister(priv->vdev); 1812 imx_media_capture_device_remove(priv->vdev); 1813 1814 if (priv->fim) 1815 imx_media_fim_free(priv->fim); 1816 1817 if (priv->csi) 1818 ipu_csi_put(priv->csi); 1819 } 1820 1821 /* 1822 * The CSI has only one fwnode endpoint, at the sink pad. Verify the 1823 * endpoint belongs to us, and return CSI_SINK_PAD. 1824 */ 1825 static int csi_get_fwnode_pad(struct media_entity *entity, 1826 struct fwnode_endpoint *endpoint) 1827 { 1828 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); 1829 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1830 struct fwnode_handle *csi_port = dev_fwnode(priv->dev); 1831 struct fwnode_handle *csi_ep; 1832 int ret; 1833 1834 csi_ep = fwnode_get_next_child_node(csi_port, NULL); 1835 1836 ret = endpoint->local_fwnode == csi_ep ? CSI_SINK_PAD : -ENXIO; 1837 1838 fwnode_handle_put(csi_ep); 1839 1840 return ret; 1841 } 1842 1843 static const struct media_entity_operations csi_entity_ops = { 1844 .link_setup = csi_link_setup, 1845 .link_validate = v4l2_subdev_link_validate, 1846 .get_fwnode_pad = csi_get_fwnode_pad, 1847 }; 1848 1849 static const struct v4l2_subdev_core_ops csi_core_ops = { 1850 .subscribe_event = csi_subscribe_event, 1851 .unsubscribe_event = csi_unsubscribe_event, 1852 }; 1853 1854 static const struct v4l2_subdev_video_ops csi_video_ops = { 1855 .g_frame_interval = csi_g_frame_interval, 1856 .s_frame_interval = csi_s_frame_interval, 1857 .s_stream = csi_s_stream, 1858 }; 1859 1860 static const struct v4l2_subdev_pad_ops csi_pad_ops = { 1861 .init_cfg = imx_media_init_cfg, 1862 .enum_mbus_code = csi_enum_mbus_code, 1863 .enum_frame_size = csi_enum_frame_size, 1864 .enum_frame_interval = csi_enum_frame_interval, 1865 .get_fmt = csi_get_fmt, 1866 .set_fmt = csi_set_fmt, 1867 .get_selection = csi_get_selection, 1868 .set_selection = csi_set_selection, 1869 .link_validate = csi_link_validate, 1870 }; 1871 1872 static const struct v4l2_subdev_ops csi_subdev_ops = { 1873 .core = &csi_core_ops, 1874 .video = &csi_video_ops, 1875 .pad = &csi_pad_ops, 1876 }; 1877 1878 static const struct v4l2_subdev_internal_ops csi_internal_ops = { 1879 .registered = csi_registered, 1880 .unregistered = csi_unregistered, 1881 }; 1882 1883 static int imx_csi_notify_bound(struct v4l2_async_notifier *notifier, 1884 struct v4l2_subdev *sd, 1885 struct v4l2_async_subdev *asd) 1886 { 1887 struct csi_priv *priv = notifier_to_dev(notifier); 1888 struct media_pad *sink = &priv->sd.entity.pads[CSI_SINK_PAD]; 1889 1890 /* 1891 * If the subdev is a video mux, it must be one of the CSI 1892 * muxes. Mark it as such via its group id. 1893 */ 1894 if (sd->entity.function == MEDIA_ENT_F_VID_MUX) 1895 sd->grp_id = IMX_MEDIA_GRP_ID_CSI_MUX; 1896 1897 return v4l2_create_fwnode_links_to_pad(sd, sink, 0); 1898 } 1899 1900 static const struct v4l2_async_notifier_operations csi_notify_ops = { 1901 .bound = imx_csi_notify_bound, 1902 }; 1903 1904 static int imx_csi_async_register(struct csi_priv *priv) 1905 { 1906 struct v4l2_async_subdev *asd = NULL; 1907 struct fwnode_handle *ep; 1908 unsigned int port; 1909 int ret; 1910 1911 v4l2_async_notifier_init(&priv->notifier); 1912 1913 /* get this CSI's port id */ 1914 ret = fwnode_property_read_u32(dev_fwnode(priv->dev), "reg", &port); 1915 if (ret < 0) 1916 return ret; 1917 1918 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(priv->dev->parent), 1919 port, 0, 1920 FWNODE_GRAPH_ENDPOINT_NEXT); 1921 if (ep) { 1922 asd = v4l2_async_notifier_add_fwnode_remote_subdev( 1923 &priv->notifier, ep, struct v4l2_async_subdev); 1924 1925 fwnode_handle_put(ep); 1926 1927 if (IS_ERR(asd)) { 1928 ret = PTR_ERR(asd); 1929 /* OK if asd already exists */ 1930 if (ret != -EEXIST) 1931 return ret; 1932 } 1933 } 1934 1935 priv->notifier.ops = &csi_notify_ops; 1936 1937 ret = v4l2_async_subdev_notifier_register(&priv->sd, 1938 &priv->notifier); 1939 if (ret) 1940 return ret; 1941 1942 return v4l2_async_register_subdev(&priv->sd); 1943 } 1944 1945 static int imx_csi_probe(struct platform_device *pdev) 1946 { 1947 struct ipu_client_platformdata *pdata; 1948 struct pinctrl *pinctrl; 1949 struct csi_priv *priv; 1950 int i, ret; 1951 1952 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 1953 if (!priv) 1954 return -ENOMEM; 1955 1956 platform_set_drvdata(pdev, &priv->sd); 1957 priv->dev = &pdev->dev; 1958 1959 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32)); 1960 if (ret) 1961 return ret; 1962 1963 /* get parent IPU */ 1964 priv->ipu = dev_get_drvdata(priv->dev->parent); 1965 1966 /* get our CSI id */ 1967 pdata = priv->dev->platform_data; 1968 priv->csi_id = pdata->csi; 1969 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2; 1970 1971 priv->active_output_pad = CSI_SRC_PAD_IDMAC; 1972 1973 timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0); 1974 spin_lock_init(&priv->irqlock); 1975 1976 v4l2_subdev_init(&priv->sd, &csi_subdev_ops); 1977 v4l2_set_subdevdata(&priv->sd, priv); 1978 priv->sd.internal_ops = &csi_internal_ops; 1979 priv->sd.entity.ops = &csi_entity_ops; 1980 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER; 1981 priv->sd.dev = &pdev->dev; 1982 priv->sd.fwnode = of_fwnode_handle(pdata->of_node); 1983 priv->sd.owner = THIS_MODULE; 1984 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; 1985 priv->sd.grp_id = priv->csi_id ? 1986 IMX_MEDIA_GRP_ID_IPU_CSI1 : IMX_MEDIA_GRP_ID_IPU_CSI0; 1987 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name), 1988 priv->sd.grp_id, ipu_get_num(priv->ipu)); 1989 1990 for (i = 0; i < CSI_NUM_PADS; i++) 1991 priv->pad[i].flags = (i == CSI_SINK_PAD) ? 1992 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE; 1993 1994 ret = media_entity_pads_init(&priv->sd.entity, CSI_NUM_PADS, 1995 priv->pad); 1996 if (ret) 1997 return ret; 1998 1999 mutex_init(&priv->lock); 2000 2001 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0); 2002 priv->sd.ctrl_handler = &priv->ctrl_hdlr; 2003 2004 /* 2005 * The IPUv3 driver did not assign an of_node to this 2006 * device. As a result, pinctrl does not automatically 2007 * configure our pin groups, so we need to do that manually 2008 * here, after setting this device's of_node. 2009 */ 2010 priv->dev->of_node = pdata->of_node; 2011 pinctrl = devm_pinctrl_get_select_default(priv->dev); 2012 if (IS_ERR(pinctrl)) { 2013 ret = PTR_ERR(pinctrl); 2014 dev_dbg(priv->dev, 2015 "devm_pinctrl_get_select_default() failed: %d\n", ret); 2016 if (ret != -ENODEV) 2017 goto free; 2018 } 2019 2020 ret = imx_csi_async_register(priv); 2021 if (ret) 2022 goto cleanup; 2023 2024 return 0; 2025 2026 cleanup: 2027 v4l2_async_notifier_unregister(&priv->notifier); 2028 v4l2_async_notifier_cleanup(&priv->notifier); 2029 free: 2030 v4l2_ctrl_handler_free(&priv->ctrl_hdlr); 2031 mutex_destroy(&priv->lock); 2032 return ret; 2033 } 2034 2035 static int imx_csi_remove(struct platform_device *pdev) 2036 { 2037 struct v4l2_subdev *sd = platform_get_drvdata(pdev); 2038 struct csi_priv *priv = sd_to_dev(sd); 2039 2040 v4l2_ctrl_handler_free(&priv->ctrl_hdlr); 2041 mutex_destroy(&priv->lock); 2042 v4l2_async_notifier_unregister(&priv->notifier); 2043 v4l2_async_notifier_cleanup(&priv->notifier); 2044 v4l2_async_unregister_subdev(sd); 2045 media_entity_cleanup(&sd->entity); 2046 2047 return 0; 2048 } 2049 2050 static const struct platform_device_id imx_csi_ids[] = { 2051 { .name = "imx-ipuv3-csi" }, 2052 { }, 2053 }; 2054 MODULE_DEVICE_TABLE(platform, imx_csi_ids); 2055 2056 static struct platform_driver imx_csi_driver = { 2057 .probe = imx_csi_probe, 2058 .remove = imx_csi_remove, 2059 .id_table = imx_csi_ids, 2060 .driver = { 2061 .name = "imx-ipuv3-csi", 2062 }, 2063 }; 2064 module_platform_driver(imx_csi_driver); 2065 2066 MODULE_DESCRIPTION("i.MX CSI subdev driver"); 2067 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>"); 2068 MODULE_LICENSE("GPL"); 2069 MODULE_ALIAS("platform:imx-ipuv3-csi"); 2070