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