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.fmt.pix.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.fmt.pix.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.fmt.pix; 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 struct v4l2_pix_format *outfmt; 600 int ret; 601 602 ret = csi_idmac_get_ipu_resources(priv); 603 if (ret) 604 return ret; 605 606 ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num); 607 608 outfmt = &vdev->fmt.fmt.pix; 609 610 ret = imx_media_alloc_dma_buf(priv->dev, &priv->underrun_buf, 611 outfmt->sizeimage); 612 if (ret) 613 goto out_put_ipu; 614 615 priv->ipu_buf_num = 0; 616 617 /* init EOF completion waitq */ 618 init_completion(&priv->last_eof_comp); 619 priv->frame_sequence = 0; 620 priv->last_eof = false; 621 priv->nfb4eof = false; 622 623 ret = csi_idmac_setup(priv); 624 if (ret) { 625 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret); 626 goto out_free_dma_buf; 627 } 628 629 priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu, 630 priv->idmac_ch, 631 IPU_IRQ_NFB4EOF); 632 ret = devm_request_irq(priv->dev, priv->nfb4eof_irq, 633 csi_idmac_nfb4eof_interrupt, 0, 634 "imx-smfc-nfb4eof", priv); 635 if (ret) { 636 v4l2_err(&priv->sd, 637 "Error registering NFB4EOF irq: %d\n", ret); 638 goto out_unsetup; 639 } 640 641 priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch, 642 IPU_IRQ_EOF); 643 644 ret = devm_request_irq(priv->dev, priv->eof_irq, 645 csi_idmac_eof_interrupt, 0, 646 "imx-smfc-eof", priv); 647 if (ret) { 648 v4l2_err(&priv->sd, 649 "Error registering eof irq: %d\n", ret); 650 goto out_free_nfb4eof_irq; 651 } 652 653 /* start the EOF timeout timer */ 654 mod_timer(&priv->eof_timeout_timer, 655 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); 656 657 return 0; 658 659 out_free_nfb4eof_irq: 660 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv); 661 out_unsetup: 662 csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED); 663 out_free_dma_buf: 664 imx_media_free_dma_buf(priv->dev, &priv->underrun_buf); 665 out_put_ipu: 666 csi_idmac_put_ipu_resources(priv); 667 return ret; 668 } 669 670 static void csi_idmac_wait_last_eof(struct csi_priv *priv) 671 { 672 unsigned long flags; 673 int ret; 674 675 /* mark next EOF interrupt as the last before stream off */ 676 spin_lock_irqsave(&priv->irqlock, flags); 677 priv->last_eof = true; 678 spin_unlock_irqrestore(&priv->irqlock, flags); 679 680 /* 681 * and then wait for interrupt handler to mark completion. 682 */ 683 ret = wait_for_completion_timeout( 684 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT)); 685 if (ret == 0) 686 v4l2_warn(&priv->sd, "wait last EOF timeout\n"); 687 } 688 689 static void csi_idmac_stop(struct csi_priv *priv) 690 { 691 devm_free_irq(priv->dev, priv->eof_irq, priv); 692 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv); 693 694 csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR); 695 696 imx_media_free_dma_buf(priv->dev, &priv->underrun_buf); 697 698 /* cancel the EOF timeout timer */ 699 del_timer_sync(&priv->eof_timeout_timer); 700 701 csi_idmac_put_ipu_resources(priv); 702 } 703 704 /* Update the CSI whole sensor and active windows */ 705 static int csi_setup(struct csi_priv *priv) 706 { 707 struct v4l2_mbus_framefmt *infmt, *outfmt; 708 const struct imx_media_pixfmt *incc; 709 struct v4l2_mbus_config mbus_cfg; 710 struct v4l2_mbus_framefmt if_fmt; 711 struct v4l2_rect crop; 712 713 infmt = &priv->format_mbus[CSI_SINK_PAD]; 714 incc = priv->cc[CSI_SINK_PAD]; 715 outfmt = &priv->format_mbus[priv->active_output_pad]; 716 717 /* compose mbus_config from the upstream endpoint */ 718 mbus_cfg.type = priv->upstream_ep.bus_type; 719 mbus_cfg.flags = is_parallel_bus(&priv->upstream_ep) ? 720 priv->upstream_ep.bus.parallel.flags : 721 priv->upstream_ep.bus.mipi_csi2.flags; 722 723 if_fmt = *infmt; 724 crop = priv->crop; 725 726 /* 727 * if cycles is set, we need to handle this over multiple cycles as 728 * generic/bayer data 729 */ 730 if (is_parallel_bus(&priv->upstream_ep) && incc->cycles) { 731 if_fmt.width *= incc->cycles; 732 crop.width *= incc->cycles; 733 } 734 735 ipu_csi_set_window(priv->csi, &crop); 736 737 ipu_csi_set_downsize(priv->csi, 738 priv->crop.width == 2 * priv->compose.width, 739 priv->crop.height == 2 * priv->compose.height); 740 741 ipu_csi_init_interface(priv->csi, &mbus_cfg, &if_fmt, outfmt); 742 743 ipu_csi_set_dest(priv->csi, priv->dest); 744 745 if (priv->dest == IPU_CSI_DEST_IDMAC) 746 ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc, 747 priv->skip->max_ratio - 1, 0); 748 749 ipu_csi_dump(priv->csi); 750 751 return 0; 752 } 753 754 static int csi_start(struct csi_priv *priv) 755 { 756 struct v4l2_fract *output_fi; 757 int ret; 758 759 output_fi = &priv->frame_interval[priv->active_output_pad]; 760 761 /* start upstream */ 762 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1); 763 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0; 764 if (ret) 765 return ret; 766 767 if (priv->dest == IPU_CSI_DEST_IDMAC) { 768 ret = csi_idmac_start(priv); 769 if (ret) 770 goto stop_upstream; 771 } 772 773 ret = csi_setup(priv); 774 if (ret) 775 goto idmac_stop; 776 777 /* start the frame interval monitor */ 778 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) { 779 ret = imx_media_fim_set_stream(priv->fim, output_fi, true); 780 if (ret) 781 goto idmac_stop; 782 } 783 784 ret = ipu_csi_enable(priv->csi); 785 if (ret) { 786 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret); 787 goto fim_off; 788 } 789 790 return 0; 791 792 fim_off: 793 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) 794 imx_media_fim_set_stream(priv->fim, NULL, false); 795 idmac_stop: 796 if (priv->dest == IPU_CSI_DEST_IDMAC) 797 csi_idmac_stop(priv); 798 stop_upstream: 799 v4l2_subdev_call(priv->src_sd, video, s_stream, 0); 800 return ret; 801 } 802 803 static void csi_stop(struct csi_priv *priv) 804 { 805 if (priv->dest == IPU_CSI_DEST_IDMAC) 806 csi_idmac_wait_last_eof(priv); 807 808 /* 809 * Disable the CSI asap, after syncing with the last EOF. 810 * Doing so after the IDMA channel is disabled has shown to 811 * create hard system-wide hangs. 812 */ 813 ipu_csi_disable(priv->csi); 814 815 /* stop upstream */ 816 v4l2_subdev_call(priv->src_sd, video, s_stream, 0); 817 818 if (priv->dest == IPU_CSI_DEST_IDMAC) { 819 csi_idmac_stop(priv); 820 821 /* stop the frame interval monitor */ 822 if (priv->fim) 823 imx_media_fim_set_stream(priv->fim, NULL, false); 824 } 825 } 826 827 static const struct csi_skip_desc csi_skip[12] = { 828 { 1, 1, 0x00 }, /* Keep all frames */ 829 { 5, 6, 0x10 }, /* Skip every sixth frame */ 830 { 4, 5, 0x08 }, /* Skip every fifth frame */ 831 { 3, 4, 0x04 }, /* Skip every fourth frame */ 832 { 2, 3, 0x02 }, /* Skip every third frame */ 833 { 3, 5, 0x0a }, /* Skip frames 1 and 3 of every 5 */ 834 { 1, 2, 0x01 }, /* Skip every second frame */ 835 { 2, 5, 0x0b }, /* Keep frames 1 and 4 of every 5 */ 836 { 1, 3, 0x03 }, /* Keep one in three frames */ 837 { 1, 4, 0x07 }, /* Keep one in four frames */ 838 { 1, 5, 0x0f }, /* Keep one in five frames */ 839 { 1, 6, 0x1f }, /* Keep one in six frames */ 840 }; 841 842 static void csi_apply_skip_interval(const struct csi_skip_desc *skip, 843 struct v4l2_fract *interval) 844 { 845 unsigned int div; 846 847 interval->numerator *= skip->max_ratio; 848 interval->denominator *= skip->keep; 849 850 /* Reduce fraction to lowest terms */ 851 div = gcd(interval->numerator, interval->denominator); 852 if (div > 1) { 853 interval->numerator /= div; 854 interval->denominator /= div; 855 } 856 } 857 858 /* 859 * Find the skip pattern to produce the output frame interval closest to the 860 * requested one, for the given input frame interval. Updates the output frame 861 * interval to the exact value. 862 */ 863 static const struct csi_skip_desc *csi_find_best_skip(struct v4l2_fract *in, 864 struct v4l2_fract *out) 865 { 866 const struct csi_skip_desc *skip = &csi_skip[0], *best_skip = skip; 867 u32 min_err = UINT_MAX; 868 u64 want_us; 869 int i; 870 871 /* Default to 1:1 ratio */ 872 if (out->numerator == 0 || out->denominator == 0 || 873 in->numerator == 0 || in->denominator == 0) { 874 *out = *in; 875 return best_skip; 876 } 877 878 want_us = div_u64((u64)USEC_PER_SEC * out->numerator, out->denominator); 879 880 /* Find the reduction closest to the requested time per frame */ 881 for (i = 0; i < ARRAY_SIZE(csi_skip); i++, skip++) { 882 u64 tmp, err; 883 884 tmp = div_u64((u64)USEC_PER_SEC * in->numerator * 885 skip->max_ratio, in->denominator * skip->keep); 886 887 err = abs((s64)tmp - want_us); 888 if (err < min_err) { 889 min_err = err; 890 best_skip = skip; 891 } 892 } 893 894 *out = *in; 895 csi_apply_skip_interval(best_skip, out); 896 897 return best_skip; 898 } 899 900 /* 901 * V4L2 subdev operations. 902 */ 903 904 static int csi_g_frame_interval(struct v4l2_subdev *sd, 905 struct v4l2_subdev_frame_interval *fi) 906 { 907 struct csi_priv *priv = v4l2_get_subdevdata(sd); 908 909 if (fi->pad >= CSI_NUM_PADS) 910 return -EINVAL; 911 912 mutex_lock(&priv->lock); 913 914 fi->interval = priv->frame_interval[fi->pad]; 915 916 mutex_unlock(&priv->lock); 917 918 return 0; 919 } 920 921 static int csi_s_frame_interval(struct v4l2_subdev *sd, 922 struct v4l2_subdev_frame_interval *fi) 923 { 924 struct csi_priv *priv = v4l2_get_subdevdata(sd); 925 struct v4l2_fract *input_fi; 926 int ret = 0; 927 928 mutex_lock(&priv->lock); 929 930 input_fi = &priv->frame_interval[CSI_SINK_PAD]; 931 932 switch (fi->pad) { 933 case CSI_SINK_PAD: 934 /* No limits on valid input frame intervals */ 935 if (fi->interval.numerator == 0 || 936 fi->interval.denominator == 0) 937 fi->interval = *input_fi; 938 /* Reset output intervals and frame skipping ratio to 1:1 */ 939 priv->frame_interval[CSI_SRC_PAD_IDMAC] = fi->interval; 940 priv->frame_interval[CSI_SRC_PAD_DIRECT] = fi->interval; 941 priv->skip = &csi_skip[0]; 942 break; 943 case CSI_SRC_PAD_IDMAC: 944 /* 945 * frame interval at IDMAC output pad depends on input 946 * interval, modified by frame skipping. 947 */ 948 priv->skip = csi_find_best_skip(input_fi, &fi->interval); 949 break; 950 case CSI_SRC_PAD_DIRECT: 951 /* 952 * frame interval at DIRECT output pad is same as input 953 * interval. 954 */ 955 fi->interval = *input_fi; 956 break; 957 default: 958 ret = -EINVAL; 959 goto out; 960 } 961 962 priv->frame_interval[fi->pad] = fi->interval; 963 out: 964 mutex_unlock(&priv->lock); 965 return ret; 966 } 967 968 static int csi_s_stream(struct v4l2_subdev *sd, int enable) 969 { 970 struct csi_priv *priv = v4l2_get_subdevdata(sd); 971 int ret = 0; 972 973 mutex_lock(&priv->lock); 974 975 if (!priv->src_sd || !priv->sink) { 976 ret = -EPIPE; 977 goto out; 978 } 979 980 /* 981 * enable/disable streaming only if stream_count is 982 * going from 0 to 1 / 1 to 0. 983 */ 984 if (priv->stream_count != !enable) 985 goto update_count; 986 987 if (enable) { 988 dev_dbg(priv->dev, "stream ON\n"); 989 ret = csi_start(priv); 990 if (ret) 991 goto out; 992 } else { 993 dev_dbg(priv->dev, "stream OFF\n"); 994 csi_stop(priv); 995 } 996 997 update_count: 998 priv->stream_count += enable ? 1 : -1; 999 if (priv->stream_count < 0) 1000 priv->stream_count = 0; 1001 out: 1002 mutex_unlock(&priv->lock); 1003 return ret; 1004 } 1005 1006 static int csi_link_setup(struct media_entity *entity, 1007 const struct media_pad *local, 1008 const struct media_pad *remote, u32 flags) 1009 { 1010 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); 1011 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1012 struct v4l2_subdev *remote_sd; 1013 int ret = 0; 1014 1015 dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name, 1016 local->entity->name); 1017 1018 mutex_lock(&priv->lock); 1019 1020 if (local->flags & MEDIA_PAD_FL_SINK) { 1021 if (!is_media_entity_v4l2_subdev(remote->entity)) { 1022 ret = -EINVAL; 1023 goto out; 1024 } 1025 1026 remote_sd = media_entity_to_v4l2_subdev(remote->entity); 1027 1028 if (flags & MEDIA_LNK_FL_ENABLED) { 1029 if (priv->src_sd) { 1030 ret = -EBUSY; 1031 goto out; 1032 } 1033 priv->src_sd = remote_sd; 1034 } else { 1035 priv->src_sd = NULL; 1036 } 1037 1038 goto out; 1039 } 1040 1041 /* this is a source pad */ 1042 1043 if (flags & MEDIA_LNK_FL_ENABLED) { 1044 if (priv->sink) { 1045 ret = -EBUSY; 1046 goto out; 1047 } 1048 } else { 1049 v4l2_ctrl_handler_free(&priv->ctrl_hdlr); 1050 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0); 1051 priv->sink = NULL; 1052 /* do not apply IC burst alignment in csi_try_crop */ 1053 priv->active_output_pad = CSI_SRC_PAD_IDMAC; 1054 goto out; 1055 } 1056 1057 /* record which output pad is now active */ 1058 priv->active_output_pad = local->index; 1059 1060 /* set CSI destination */ 1061 if (local->index == CSI_SRC_PAD_IDMAC) { 1062 if (!is_media_entity_v4l2_video_device(remote->entity)) { 1063 ret = -EINVAL; 1064 goto out; 1065 } 1066 1067 if (priv->fim) { 1068 ret = imx_media_fim_add_controls(priv->fim); 1069 if (ret) 1070 goto out; 1071 } 1072 1073 priv->dest = IPU_CSI_DEST_IDMAC; 1074 } else { 1075 if (!is_media_entity_v4l2_subdev(remote->entity)) { 1076 ret = -EINVAL; 1077 goto out; 1078 } 1079 1080 remote_sd = media_entity_to_v4l2_subdev(remote->entity); 1081 switch (remote_sd->grp_id) { 1082 case IMX_MEDIA_GRP_ID_IPU_VDIC: 1083 priv->dest = IPU_CSI_DEST_VDIC; 1084 break; 1085 case IMX_MEDIA_GRP_ID_IPU_IC_PRP: 1086 priv->dest = IPU_CSI_DEST_IC; 1087 break; 1088 default: 1089 ret = -EINVAL; 1090 goto out; 1091 } 1092 } 1093 1094 priv->sink = remote->entity; 1095 out: 1096 mutex_unlock(&priv->lock); 1097 return ret; 1098 } 1099 1100 static int csi_link_validate(struct v4l2_subdev *sd, 1101 struct media_link *link, 1102 struct v4l2_subdev_format *source_fmt, 1103 struct v4l2_subdev_format *sink_fmt) 1104 { 1105 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1106 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 }; 1107 bool is_csi2; 1108 int ret; 1109 1110 ret = v4l2_subdev_link_validate_default(sd, link, 1111 source_fmt, sink_fmt); 1112 if (ret) 1113 return ret; 1114 1115 ret = csi_get_upstream_endpoint(priv, &upstream_ep); 1116 if (ret) { 1117 v4l2_err(&priv->sd, "failed to find upstream endpoint\n"); 1118 return ret; 1119 } 1120 1121 mutex_lock(&priv->lock); 1122 1123 priv->upstream_ep = upstream_ep; 1124 is_csi2 = !is_parallel_bus(&upstream_ep); 1125 if (is_csi2) { 1126 /* 1127 * NOTE! It seems the virtual channels from the mipi csi-2 1128 * receiver are used only for routing by the video mux's, 1129 * or for hard-wired routing to the CSI's. Once the stream 1130 * enters the CSI's however, they are treated internally 1131 * in the IPU as virtual channel 0. 1132 */ 1133 ipu_csi_set_mipi_datatype(priv->csi, 0, 1134 &priv->format_mbus[CSI_SINK_PAD]); 1135 } 1136 1137 /* select either parallel or MIPI-CSI2 as input to CSI */ 1138 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2); 1139 1140 mutex_unlock(&priv->lock); 1141 return ret; 1142 } 1143 1144 static struct v4l2_mbus_framefmt * 1145 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg, 1146 unsigned int pad, enum v4l2_subdev_format_whence which) 1147 { 1148 if (which == V4L2_SUBDEV_FORMAT_TRY) 1149 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad); 1150 else 1151 return &priv->format_mbus[pad]; 1152 } 1153 1154 static struct v4l2_rect * 1155 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg, 1156 enum v4l2_subdev_format_whence which) 1157 { 1158 if (which == V4L2_SUBDEV_FORMAT_TRY) 1159 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD); 1160 else 1161 return &priv->crop; 1162 } 1163 1164 static struct v4l2_rect * 1165 __csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg, 1166 enum v4l2_subdev_format_whence which) 1167 { 1168 if (which == V4L2_SUBDEV_FORMAT_TRY) 1169 return v4l2_subdev_get_try_compose(&priv->sd, cfg, 1170 CSI_SINK_PAD); 1171 else 1172 return &priv->compose; 1173 } 1174 1175 static void csi_try_crop(struct csi_priv *priv, 1176 struct v4l2_rect *crop, 1177 struct v4l2_subdev_pad_config *cfg, 1178 struct v4l2_mbus_framefmt *infmt, 1179 struct v4l2_fwnode_endpoint *upstream_ep) 1180 { 1181 u32 in_height; 1182 1183 crop->width = min_t(__u32, infmt->width, crop->width); 1184 if (crop->left + crop->width > infmt->width) 1185 crop->left = infmt->width - crop->width; 1186 /* adjust crop left/width to h/w alignment restrictions */ 1187 crop->left &= ~0x3; 1188 if (priv->active_output_pad == CSI_SRC_PAD_DIRECT) 1189 crop->width &= ~0x7; /* multiple of 8 pixels (IC burst) */ 1190 else 1191 crop->width &= ~0x1; /* multiple of 2 pixels */ 1192 1193 in_height = infmt->height; 1194 if (infmt->field == V4L2_FIELD_ALTERNATE) 1195 in_height *= 2; 1196 1197 /* 1198 * FIXME: not sure why yet, but on interlaced bt.656, 1199 * changing the vertical cropping causes loss of vertical 1200 * sync, so fix it to NTSC/PAL active lines. NTSC contains 1201 * 2 extra lines of active video that need to be cropped. 1202 */ 1203 if (upstream_ep->bus_type == V4L2_MBUS_BT656 && 1204 (V4L2_FIELD_HAS_BOTH(infmt->field) || 1205 infmt->field == V4L2_FIELD_ALTERNATE)) { 1206 crop->height = in_height; 1207 crop->top = (in_height == 480) ? 2 : 0; 1208 } else { 1209 crop->height = min_t(__u32, in_height, crop->height); 1210 if (crop->top + crop->height > in_height) 1211 crop->top = in_height - crop->height; 1212 } 1213 } 1214 1215 static int csi_enum_mbus_code(struct v4l2_subdev *sd, 1216 struct v4l2_subdev_pad_config *cfg, 1217 struct v4l2_subdev_mbus_code_enum *code) 1218 { 1219 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1220 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 }; 1221 const struct imx_media_pixfmt *incc; 1222 struct v4l2_mbus_framefmt *infmt; 1223 int ret = 0; 1224 1225 mutex_lock(&priv->lock); 1226 1227 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which); 1228 incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY); 1229 1230 switch (code->pad) { 1231 case CSI_SINK_PAD: 1232 ret = imx_media_enum_mbus_formats(&code->code, code->index, 1233 PIXFMT_SEL_ANY); 1234 break; 1235 case CSI_SRC_PAD_DIRECT: 1236 case CSI_SRC_PAD_IDMAC: 1237 ret = csi_get_upstream_endpoint(priv, &upstream_ep); 1238 if (ret) { 1239 v4l2_err(&priv->sd, "failed to find upstream endpoint\n"); 1240 goto out; 1241 } 1242 1243 if (requires_passthrough(&upstream_ep, infmt, incc)) { 1244 if (code->index != 0) { 1245 ret = -EINVAL; 1246 goto out; 1247 } 1248 code->code = infmt->code; 1249 } else { 1250 enum imx_pixfmt_sel fmt_sel = 1251 (incc->cs == IPUV3_COLORSPACE_YUV) ? 1252 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB; 1253 1254 ret = imx_media_enum_ipu_formats(&code->code, 1255 code->index, 1256 fmt_sel); 1257 } 1258 break; 1259 default: 1260 ret = -EINVAL; 1261 } 1262 1263 out: 1264 mutex_unlock(&priv->lock); 1265 return ret; 1266 } 1267 1268 static int csi_enum_frame_size(struct v4l2_subdev *sd, 1269 struct v4l2_subdev_pad_config *cfg, 1270 struct v4l2_subdev_frame_size_enum *fse) 1271 { 1272 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1273 struct v4l2_rect *crop; 1274 int ret = 0; 1275 1276 if (fse->pad >= CSI_NUM_PADS || 1277 fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3)) 1278 return -EINVAL; 1279 1280 mutex_lock(&priv->lock); 1281 1282 if (fse->pad == CSI_SINK_PAD) { 1283 fse->min_width = MIN_W; 1284 fse->max_width = MAX_W; 1285 fse->min_height = MIN_H; 1286 fse->max_height = MAX_H; 1287 } else { 1288 crop = __csi_get_crop(priv, cfg, fse->which); 1289 1290 fse->min_width = fse->index & 1 ? 1291 crop->width / 2 : crop->width; 1292 fse->max_width = fse->min_width; 1293 fse->min_height = fse->index & 2 ? 1294 crop->height / 2 : crop->height; 1295 fse->max_height = fse->min_height; 1296 } 1297 1298 mutex_unlock(&priv->lock); 1299 return ret; 1300 } 1301 1302 static int csi_enum_frame_interval(struct v4l2_subdev *sd, 1303 struct v4l2_subdev_pad_config *cfg, 1304 struct v4l2_subdev_frame_interval_enum *fie) 1305 { 1306 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1307 struct v4l2_fract *input_fi; 1308 struct v4l2_rect *crop; 1309 int ret = 0; 1310 1311 if (fie->pad >= CSI_NUM_PADS || 1312 fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ? 1313 1 : ARRAY_SIZE(csi_skip))) 1314 return -EINVAL; 1315 1316 mutex_lock(&priv->lock); 1317 1318 input_fi = &priv->frame_interval[CSI_SINK_PAD]; 1319 crop = __csi_get_crop(priv, cfg, fie->which); 1320 1321 if ((fie->width != crop->width && fie->width != crop->width / 2) || 1322 (fie->height != crop->height && fie->height != crop->height / 2)) { 1323 ret = -EINVAL; 1324 goto out; 1325 } 1326 1327 fie->interval = *input_fi; 1328 1329 if (fie->pad == CSI_SRC_PAD_IDMAC) 1330 csi_apply_skip_interval(&csi_skip[fie->index], 1331 &fie->interval); 1332 1333 out: 1334 mutex_unlock(&priv->lock); 1335 return ret; 1336 } 1337 1338 static int csi_get_fmt(struct v4l2_subdev *sd, 1339 struct v4l2_subdev_pad_config *cfg, 1340 struct v4l2_subdev_format *sdformat) 1341 { 1342 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1343 struct v4l2_mbus_framefmt *fmt; 1344 int ret = 0; 1345 1346 if (sdformat->pad >= CSI_NUM_PADS) 1347 return -EINVAL; 1348 1349 mutex_lock(&priv->lock); 1350 1351 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which); 1352 if (!fmt) { 1353 ret = -EINVAL; 1354 goto out; 1355 } 1356 1357 sdformat->format = *fmt; 1358 out: 1359 mutex_unlock(&priv->lock); 1360 return ret; 1361 } 1362 1363 static void csi_try_field(struct csi_priv *priv, 1364 struct v4l2_subdev_pad_config *cfg, 1365 struct v4l2_subdev_format *sdformat) 1366 { 1367 struct v4l2_mbus_framefmt *infmt = 1368 __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which); 1369 1370 /* 1371 * no restrictions on sink pad field type except must 1372 * be initialized. 1373 */ 1374 if (sdformat->pad == CSI_SINK_PAD) { 1375 if (sdformat->format.field == V4L2_FIELD_ANY) 1376 sdformat->format.field = V4L2_FIELD_NONE; 1377 return; 1378 } 1379 1380 switch (infmt->field) { 1381 case V4L2_FIELD_SEQ_TB: 1382 case V4L2_FIELD_SEQ_BT: 1383 /* 1384 * If the user requests sequential at the source pad, 1385 * allow it (along with possibly inverting field order). 1386 * Otherwise passthrough the field type. 1387 */ 1388 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field)) 1389 sdformat->format.field = infmt->field; 1390 break; 1391 case V4L2_FIELD_ALTERNATE: 1392 /* 1393 * This driver does not support alternate field mode, and 1394 * the CSI captures a whole frame, so the CSI never presents 1395 * alternate mode at its source pads. If user has not 1396 * already requested sequential, translate ALTERNATE at 1397 * sink pad to SEQ_TB or SEQ_BT at the source pad depending 1398 * on input height (assume NTSC BT order if 480 total active 1399 * frame lines, otherwise PAL TB order). 1400 */ 1401 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field)) 1402 sdformat->format.field = (infmt->height == 480 / 2) ? 1403 V4L2_FIELD_SEQ_BT : V4L2_FIELD_SEQ_TB; 1404 break; 1405 default: 1406 /* Passthrough for all other input field types */ 1407 sdformat->format.field = infmt->field; 1408 break; 1409 } 1410 } 1411 1412 static void csi_try_fmt(struct csi_priv *priv, 1413 struct v4l2_fwnode_endpoint *upstream_ep, 1414 struct v4l2_subdev_pad_config *cfg, 1415 struct v4l2_subdev_format *sdformat, 1416 struct v4l2_rect *crop, 1417 struct v4l2_rect *compose, 1418 const struct imx_media_pixfmt **cc) 1419 { 1420 const struct imx_media_pixfmt *incc; 1421 struct v4l2_mbus_framefmt *infmt; 1422 u32 code; 1423 1424 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which); 1425 1426 switch (sdformat->pad) { 1427 case CSI_SRC_PAD_DIRECT: 1428 case CSI_SRC_PAD_IDMAC: 1429 incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY); 1430 1431 sdformat->format.width = compose->width; 1432 sdformat->format.height = compose->height; 1433 1434 if (requires_passthrough(upstream_ep, infmt, incc)) { 1435 sdformat->format.code = infmt->code; 1436 *cc = incc; 1437 } else { 1438 enum imx_pixfmt_sel fmt_sel = 1439 (incc->cs == IPUV3_COLORSPACE_YUV) ? 1440 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB; 1441 1442 *cc = imx_media_find_ipu_format(sdformat->format.code, 1443 fmt_sel); 1444 if (!*cc) { 1445 imx_media_enum_ipu_formats(&code, 0, fmt_sel); 1446 *cc = imx_media_find_ipu_format(code, fmt_sel); 1447 sdformat->format.code = (*cc)->codes[0]; 1448 } 1449 } 1450 1451 csi_try_field(priv, cfg, sdformat); 1452 1453 /* propagate colorimetry from sink */ 1454 sdformat->format.colorspace = infmt->colorspace; 1455 sdformat->format.xfer_func = infmt->xfer_func; 1456 sdformat->format.quantization = infmt->quantization; 1457 sdformat->format.ycbcr_enc = infmt->ycbcr_enc; 1458 1459 break; 1460 case CSI_SINK_PAD: 1461 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W, 1462 W_ALIGN, &sdformat->format.height, 1463 MIN_H, MAX_H, H_ALIGN, S_ALIGN); 1464 1465 *cc = imx_media_find_mbus_format(sdformat->format.code, 1466 PIXFMT_SEL_ANY); 1467 if (!*cc) { 1468 imx_media_enum_mbus_formats(&code, 0, 1469 PIXFMT_SEL_YUV_RGB); 1470 *cc = imx_media_find_mbus_format(code, 1471 PIXFMT_SEL_YUV_RGB); 1472 sdformat->format.code = (*cc)->codes[0]; 1473 } 1474 1475 csi_try_field(priv, cfg, sdformat); 1476 1477 /* Reset crop and compose rectangles */ 1478 crop->left = 0; 1479 crop->top = 0; 1480 crop->width = sdformat->format.width; 1481 crop->height = sdformat->format.height; 1482 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) 1483 crop->height *= 2; 1484 csi_try_crop(priv, crop, cfg, &sdformat->format, upstream_ep); 1485 compose->left = 0; 1486 compose->top = 0; 1487 compose->width = crop->width; 1488 compose->height = crop->height; 1489 1490 break; 1491 } 1492 1493 imx_media_try_colorimetry(&sdformat->format, 1494 priv->active_output_pad == CSI_SRC_PAD_DIRECT); 1495 } 1496 1497 static int csi_set_fmt(struct v4l2_subdev *sd, 1498 struct v4l2_subdev_pad_config *cfg, 1499 struct v4l2_subdev_format *sdformat) 1500 { 1501 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1502 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 }; 1503 const struct imx_media_pixfmt *cc; 1504 struct v4l2_mbus_framefmt *fmt; 1505 struct v4l2_rect *crop, *compose; 1506 int ret; 1507 1508 if (sdformat->pad >= CSI_NUM_PADS) 1509 return -EINVAL; 1510 1511 ret = csi_get_upstream_endpoint(priv, &upstream_ep); 1512 if (ret) { 1513 v4l2_err(&priv->sd, "failed to find upstream endpoint\n"); 1514 return ret; 1515 } 1516 1517 mutex_lock(&priv->lock); 1518 1519 if (priv->stream_count > 0) { 1520 ret = -EBUSY; 1521 goto out; 1522 } 1523 1524 crop = __csi_get_crop(priv, cfg, sdformat->which); 1525 compose = __csi_get_compose(priv, cfg, sdformat->which); 1526 1527 csi_try_fmt(priv, &upstream_ep, cfg, sdformat, crop, compose, &cc); 1528 1529 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which); 1530 *fmt = sdformat->format; 1531 1532 if (sdformat->pad == CSI_SINK_PAD) { 1533 int pad; 1534 1535 /* propagate format to source pads */ 1536 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) { 1537 const struct imx_media_pixfmt *outcc; 1538 struct v4l2_mbus_framefmt *outfmt; 1539 struct v4l2_subdev_format format; 1540 1541 format.pad = pad; 1542 format.which = sdformat->which; 1543 format.format = sdformat->format; 1544 csi_try_fmt(priv, &upstream_ep, cfg, &format, 1545 NULL, compose, &outcc); 1546 1547 outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which); 1548 *outfmt = format.format; 1549 1550 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE) 1551 priv->cc[pad] = outcc; 1552 } 1553 } 1554 1555 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE) 1556 priv->cc[sdformat->pad] = cc; 1557 1558 out: 1559 mutex_unlock(&priv->lock); 1560 return ret; 1561 } 1562 1563 static int csi_get_selection(struct v4l2_subdev *sd, 1564 struct v4l2_subdev_pad_config *cfg, 1565 struct v4l2_subdev_selection *sel) 1566 { 1567 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1568 struct v4l2_mbus_framefmt *infmt; 1569 struct v4l2_rect *crop, *compose; 1570 int ret = 0; 1571 1572 if (sel->pad != CSI_SINK_PAD) 1573 return -EINVAL; 1574 1575 mutex_lock(&priv->lock); 1576 1577 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which); 1578 crop = __csi_get_crop(priv, cfg, sel->which); 1579 compose = __csi_get_compose(priv, cfg, sel->which); 1580 1581 switch (sel->target) { 1582 case V4L2_SEL_TGT_CROP_BOUNDS: 1583 sel->r.left = 0; 1584 sel->r.top = 0; 1585 sel->r.width = infmt->width; 1586 sel->r.height = infmt->height; 1587 if (infmt->field == V4L2_FIELD_ALTERNATE) 1588 sel->r.height *= 2; 1589 break; 1590 case V4L2_SEL_TGT_CROP: 1591 sel->r = *crop; 1592 break; 1593 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 1594 sel->r.left = 0; 1595 sel->r.top = 0; 1596 sel->r.width = crop->width; 1597 sel->r.height = crop->height; 1598 break; 1599 case V4L2_SEL_TGT_COMPOSE: 1600 sel->r = *compose; 1601 break; 1602 default: 1603 ret = -EINVAL; 1604 } 1605 1606 mutex_unlock(&priv->lock); 1607 return ret; 1608 } 1609 1610 static int csi_set_scale(u32 *compose, u32 crop, u32 flags) 1611 { 1612 if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) == 1613 (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) && 1614 *compose != crop && *compose != crop / 2) 1615 return -ERANGE; 1616 1617 if (*compose <= crop / 2 || 1618 (*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) || 1619 (*compose < crop && (flags & V4L2_SEL_FLAG_LE))) 1620 *compose = crop / 2; 1621 else 1622 *compose = crop; 1623 1624 return 0; 1625 } 1626 1627 static int csi_set_selection(struct v4l2_subdev *sd, 1628 struct v4l2_subdev_pad_config *cfg, 1629 struct v4l2_subdev_selection *sel) 1630 { 1631 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1632 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 }; 1633 struct v4l2_mbus_framefmt *infmt; 1634 struct v4l2_rect *crop, *compose; 1635 int pad, ret; 1636 1637 if (sel->pad != CSI_SINK_PAD) 1638 return -EINVAL; 1639 1640 ret = csi_get_upstream_endpoint(priv, &upstream_ep); 1641 if (ret) { 1642 v4l2_err(&priv->sd, "failed to find upstream endpoint\n"); 1643 return ret; 1644 } 1645 1646 mutex_lock(&priv->lock); 1647 1648 if (priv->stream_count > 0) { 1649 ret = -EBUSY; 1650 goto out; 1651 } 1652 1653 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which); 1654 crop = __csi_get_crop(priv, cfg, sel->which); 1655 compose = __csi_get_compose(priv, cfg, sel->which); 1656 1657 switch (sel->target) { 1658 case V4L2_SEL_TGT_CROP: 1659 /* 1660 * Modifying the crop rectangle always changes the format on 1661 * the source pads. If the KEEP_CONFIG flag is set, just return 1662 * the current crop rectangle. 1663 */ 1664 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) { 1665 sel->r = priv->crop; 1666 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) 1667 *crop = sel->r; 1668 goto out; 1669 } 1670 1671 csi_try_crop(priv, &sel->r, cfg, infmt, &upstream_ep); 1672 1673 *crop = sel->r; 1674 1675 /* Reset scaling to 1:1 */ 1676 compose->width = crop->width; 1677 compose->height = crop->height; 1678 break; 1679 case V4L2_SEL_TGT_COMPOSE: 1680 /* 1681 * Modifying the compose rectangle always changes the format on 1682 * the source pads. If the KEEP_CONFIG flag is set, just return 1683 * the current compose rectangle. 1684 */ 1685 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) { 1686 sel->r = priv->compose; 1687 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) 1688 *compose = sel->r; 1689 goto out; 1690 } 1691 1692 sel->r.left = 0; 1693 sel->r.top = 0; 1694 ret = csi_set_scale(&sel->r.width, crop->width, sel->flags); 1695 if (ret) 1696 goto out; 1697 ret = csi_set_scale(&sel->r.height, crop->height, sel->flags); 1698 if (ret) 1699 goto out; 1700 1701 *compose = sel->r; 1702 break; 1703 default: 1704 ret = -EINVAL; 1705 goto out; 1706 } 1707 1708 /* Reset source pads to sink compose rectangle */ 1709 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) { 1710 struct v4l2_mbus_framefmt *outfmt; 1711 1712 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which); 1713 outfmt->width = compose->width; 1714 outfmt->height = compose->height; 1715 } 1716 1717 out: 1718 mutex_unlock(&priv->lock); 1719 return ret; 1720 } 1721 1722 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, 1723 struct v4l2_event_subscription *sub) 1724 { 1725 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR) 1726 return -EINVAL; 1727 if (sub->id != 0) 1728 return -EINVAL; 1729 1730 return v4l2_event_subscribe(fh, sub, 0, NULL); 1731 } 1732 1733 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, 1734 struct v4l2_event_subscription *sub) 1735 { 1736 return v4l2_event_unsubscribe(fh, sub); 1737 } 1738 1739 static int csi_registered(struct v4l2_subdev *sd) 1740 { 1741 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1742 struct ipu_csi *csi; 1743 int i, ret; 1744 u32 code; 1745 1746 /* get handle to IPU CSI */ 1747 csi = ipu_csi_get(priv->ipu, priv->csi_id); 1748 if (IS_ERR(csi)) { 1749 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id); 1750 return PTR_ERR(csi); 1751 } 1752 priv->csi = csi; 1753 1754 for (i = 0; i < CSI_NUM_PADS; i++) { 1755 code = 0; 1756 if (i != CSI_SINK_PAD) 1757 imx_media_enum_ipu_formats(&code, 0, PIXFMT_SEL_YUV); 1758 1759 /* set a default mbus format */ 1760 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i], 1761 640, 480, code, V4L2_FIELD_NONE, 1762 &priv->cc[i]); 1763 if (ret) 1764 goto put_csi; 1765 1766 /* init default frame interval */ 1767 priv->frame_interval[i].numerator = 1; 1768 priv->frame_interval[i].denominator = 30; 1769 } 1770 1771 /* disable frame skipping */ 1772 priv->skip = &csi_skip[0]; 1773 1774 /* init default crop and compose rectangle sizes */ 1775 priv->crop.width = 640; 1776 priv->crop.height = 480; 1777 priv->compose.width = 640; 1778 priv->compose.height = 480; 1779 1780 priv->fim = imx_media_fim_init(&priv->sd); 1781 if (IS_ERR(priv->fim)) { 1782 ret = PTR_ERR(priv->fim); 1783 goto put_csi; 1784 } 1785 1786 priv->vdev = imx_media_capture_device_init(priv->sd.dev, 1787 &priv->sd, 1788 CSI_SRC_PAD_IDMAC); 1789 if (IS_ERR(priv->vdev)) { 1790 ret = PTR_ERR(priv->vdev); 1791 goto free_fim; 1792 } 1793 1794 ret = imx_media_capture_device_register(priv->vdev); 1795 if (ret) 1796 goto remove_vdev; 1797 1798 return 0; 1799 1800 remove_vdev: 1801 imx_media_capture_device_remove(priv->vdev); 1802 free_fim: 1803 if (priv->fim) 1804 imx_media_fim_free(priv->fim); 1805 put_csi: 1806 ipu_csi_put(priv->csi); 1807 return ret; 1808 } 1809 1810 static void csi_unregistered(struct v4l2_subdev *sd) 1811 { 1812 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1813 1814 imx_media_capture_device_unregister(priv->vdev); 1815 imx_media_capture_device_remove(priv->vdev); 1816 1817 if (priv->fim) 1818 imx_media_fim_free(priv->fim); 1819 1820 if (priv->csi) 1821 ipu_csi_put(priv->csi); 1822 } 1823 1824 /* 1825 * The CSI has only one fwnode endpoint, at the sink pad. Verify the 1826 * endpoint belongs to us, and return CSI_SINK_PAD. 1827 */ 1828 static int csi_get_fwnode_pad(struct media_entity *entity, 1829 struct fwnode_endpoint *endpoint) 1830 { 1831 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); 1832 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1833 struct fwnode_handle *csi_port = dev_fwnode(priv->dev); 1834 struct fwnode_handle *csi_ep; 1835 int ret; 1836 1837 csi_ep = fwnode_get_next_child_node(csi_port, NULL); 1838 1839 ret = endpoint->local_fwnode == csi_ep ? CSI_SINK_PAD : -ENXIO; 1840 1841 fwnode_handle_put(csi_ep); 1842 1843 return ret; 1844 } 1845 1846 static const struct media_entity_operations csi_entity_ops = { 1847 .link_setup = csi_link_setup, 1848 .link_validate = v4l2_subdev_link_validate, 1849 .get_fwnode_pad = csi_get_fwnode_pad, 1850 }; 1851 1852 static const struct v4l2_subdev_core_ops csi_core_ops = { 1853 .subscribe_event = csi_subscribe_event, 1854 .unsubscribe_event = csi_unsubscribe_event, 1855 }; 1856 1857 static const struct v4l2_subdev_video_ops csi_video_ops = { 1858 .g_frame_interval = csi_g_frame_interval, 1859 .s_frame_interval = csi_s_frame_interval, 1860 .s_stream = csi_s_stream, 1861 }; 1862 1863 static const struct v4l2_subdev_pad_ops csi_pad_ops = { 1864 .init_cfg = imx_media_init_cfg, 1865 .enum_mbus_code = csi_enum_mbus_code, 1866 .enum_frame_size = csi_enum_frame_size, 1867 .enum_frame_interval = csi_enum_frame_interval, 1868 .get_fmt = csi_get_fmt, 1869 .set_fmt = csi_set_fmt, 1870 .get_selection = csi_get_selection, 1871 .set_selection = csi_set_selection, 1872 .link_validate = csi_link_validate, 1873 }; 1874 1875 static const struct v4l2_subdev_ops csi_subdev_ops = { 1876 .core = &csi_core_ops, 1877 .video = &csi_video_ops, 1878 .pad = &csi_pad_ops, 1879 }; 1880 1881 static const struct v4l2_subdev_internal_ops csi_internal_ops = { 1882 .registered = csi_registered, 1883 .unregistered = csi_unregistered, 1884 }; 1885 1886 static int imx_csi_notify_bound(struct v4l2_async_notifier *notifier, 1887 struct v4l2_subdev *sd, 1888 struct v4l2_async_subdev *asd) 1889 { 1890 struct csi_priv *priv = notifier_to_dev(notifier); 1891 struct media_pad *sink = &priv->sd.entity.pads[CSI_SINK_PAD]; 1892 1893 /* 1894 * If the subdev is a video mux, it must be one of the CSI 1895 * muxes. Mark it as such via its group id. 1896 */ 1897 if (sd->entity.function == MEDIA_ENT_F_VID_MUX) 1898 sd->grp_id = IMX_MEDIA_GRP_ID_CSI_MUX; 1899 1900 return v4l2_create_fwnode_links_to_pad(sd, sink); 1901 } 1902 1903 static const struct v4l2_async_notifier_operations csi_notify_ops = { 1904 .bound = imx_csi_notify_bound, 1905 }; 1906 1907 static int imx_csi_async_register(struct csi_priv *priv) 1908 { 1909 struct v4l2_async_subdev *asd = NULL; 1910 struct fwnode_handle *ep; 1911 unsigned int port; 1912 int ret; 1913 1914 v4l2_async_notifier_init(&priv->notifier); 1915 1916 /* get this CSI's port id */ 1917 ret = fwnode_property_read_u32(dev_fwnode(priv->dev), "reg", &port); 1918 if (ret < 0) 1919 return ret; 1920 1921 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(priv->dev->parent), 1922 port, 0, 1923 FWNODE_GRAPH_ENDPOINT_NEXT); 1924 if (ep) { 1925 asd = kzalloc(sizeof(*asd), GFP_KERNEL); 1926 if (!asd) { 1927 fwnode_handle_put(ep); 1928 return -ENOMEM; 1929 } 1930 1931 ret = v4l2_async_notifier_add_fwnode_remote_subdev( 1932 &priv->notifier, ep, asd); 1933 1934 fwnode_handle_put(ep); 1935 1936 if (ret) { 1937 kfree(asd); 1938 /* OK if asd already exists */ 1939 if (ret != -EEXIST) 1940 return ret; 1941 } 1942 } 1943 1944 priv->notifier.ops = &csi_notify_ops; 1945 1946 ret = v4l2_async_subdev_notifier_register(&priv->sd, 1947 &priv->notifier); 1948 if (ret) 1949 return ret; 1950 1951 return v4l2_async_register_subdev(&priv->sd); 1952 } 1953 1954 static int imx_csi_probe(struct platform_device *pdev) 1955 { 1956 struct ipu_client_platformdata *pdata; 1957 struct pinctrl *pinctrl; 1958 struct csi_priv *priv; 1959 int i, ret; 1960 1961 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 1962 if (!priv) 1963 return -ENOMEM; 1964 1965 platform_set_drvdata(pdev, &priv->sd); 1966 priv->dev = &pdev->dev; 1967 1968 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32)); 1969 if (ret) 1970 return ret; 1971 1972 /* get parent IPU */ 1973 priv->ipu = dev_get_drvdata(priv->dev->parent); 1974 1975 /* get our CSI id */ 1976 pdata = priv->dev->platform_data; 1977 priv->csi_id = pdata->csi; 1978 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2; 1979 1980 priv->active_output_pad = CSI_SRC_PAD_IDMAC; 1981 1982 timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0); 1983 spin_lock_init(&priv->irqlock); 1984 1985 v4l2_subdev_init(&priv->sd, &csi_subdev_ops); 1986 v4l2_set_subdevdata(&priv->sd, priv); 1987 priv->sd.internal_ops = &csi_internal_ops; 1988 priv->sd.entity.ops = &csi_entity_ops; 1989 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER; 1990 priv->sd.dev = &pdev->dev; 1991 priv->sd.fwnode = of_fwnode_handle(pdata->of_node); 1992 priv->sd.owner = THIS_MODULE; 1993 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; 1994 priv->sd.grp_id = priv->csi_id ? 1995 IMX_MEDIA_GRP_ID_IPU_CSI1 : IMX_MEDIA_GRP_ID_IPU_CSI0; 1996 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name), 1997 priv->sd.grp_id, ipu_get_num(priv->ipu)); 1998 1999 for (i = 0; i < CSI_NUM_PADS; i++) 2000 priv->pad[i].flags = (i == CSI_SINK_PAD) ? 2001 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE; 2002 2003 ret = media_entity_pads_init(&priv->sd.entity, CSI_NUM_PADS, 2004 priv->pad); 2005 if (ret) 2006 return ret; 2007 2008 mutex_init(&priv->lock); 2009 2010 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0); 2011 priv->sd.ctrl_handler = &priv->ctrl_hdlr; 2012 2013 /* 2014 * The IPUv3 driver did not assign an of_node to this 2015 * device. As a result, pinctrl does not automatically 2016 * configure our pin groups, so we need to do that manually 2017 * here, after setting this device's of_node. 2018 */ 2019 priv->dev->of_node = pdata->of_node; 2020 pinctrl = devm_pinctrl_get_select_default(priv->dev); 2021 if (IS_ERR(pinctrl)) { 2022 ret = PTR_ERR(pinctrl); 2023 dev_dbg(priv->dev, 2024 "devm_pinctrl_get_select_default() failed: %d\n", ret); 2025 if (ret != -ENODEV) 2026 goto free; 2027 } 2028 2029 ret = imx_csi_async_register(priv); 2030 if (ret) 2031 goto cleanup; 2032 2033 return 0; 2034 2035 cleanup: 2036 v4l2_async_notifier_unregister(&priv->notifier); 2037 v4l2_async_notifier_cleanup(&priv->notifier); 2038 free: 2039 v4l2_ctrl_handler_free(&priv->ctrl_hdlr); 2040 mutex_destroy(&priv->lock); 2041 return ret; 2042 } 2043 2044 static int imx_csi_remove(struct platform_device *pdev) 2045 { 2046 struct v4l2_subdev *sd = platform_get_drvdata(pdev); 2047 struct csi_priv *priv = sd_to_dev(sd); 2048 2049 v4l2_ctrl_handler_free(&priv->ctrl_hdlr); 2050 mutex_destroy(&priv->lock); 2051 v4l2_async_notifier_unregister(&priv->notifier); 2052 v4l2_async_notifier_cleanup(&priv->notifier); 2053 v4l2_async_unregister_subdev(sd); 2054 media_entity_cleanup(&sd->entity); 2055 2056 return 0; 2057 } 2058 2059 static const struct platform_device_id imx_csi_ids[] = { 2060 { .name = "imx-ipuv3-csi" }, 2061 { }, 2062 }; 2063 MODULE_DEVICE_TABLE(platform, imx_csi_ids); 2064 2065 static struct platform_driver imx_csi_driver = { 2066 .probe = imx_csi_probe, 2067 .remove = imx_csi_remove, 2068 .id_table = imx_csi_ids, 2069 .driver = { 2070 .name = "imx-ipuv3-csi", 2071 }, 2072 }; 2073 module_platform_driver(imx_csi_driver); 2074 2075 MODULE_DESCRIPTION("i.MX CSI subdev driver"); 2076 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>"); 2077 MODULE_LICENSE("GPL"); 2078 MODULE_ALIAS("platform:imx-ipuv3-csi"); 2079