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