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 imx_media_fim_set_stream(priv->fim, output_fi, true); 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_mbus_config mbus_cfg = { .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_mbus_config(priv, &mbus_cfg); 1117 if (ret) { 1118 v4l2_err(&priv->sd, 1119 "failed to get upstream media bus configuration\n"); 1120 return ret; 1121 } 1122 1123 mutex_lock(&priv->lock); 1124 1125 priv->mbus_cfg = mbus_cfg; 1126 is_csi2 = !is_parallel_bus(&mbus_cfg); 1127 if (is_csi2) { 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 ipu_csi_set_mipi_datatype(priv->csi, 0, 1136 &priv->format_mbus[CSI_SINK_PAD]); 1137 } 1138 1139 /* select either parallel or MIPI-CSI2 as input to CSI */ 1140 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2); 1141 1142 mutex_unlock(&priv->lock); 1143 return ret; 1144 } 1145 1146 static struct v4l2_mbus_framefmt * 1147 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_state *sd_state, 1148 unsigned int pad, enum v4l2_subdev_format_whence which) 1149 { 1150 if (which == V4L2_SUBDEV_FORMAT_TRY) 1151 return v4l2_subdev_get_try_format(&priv->sd, sd_state, pad); 1152 else 1153 return &priv->format_mbus[pad]; 1154 } 1155 1156 static struct v4l2_rect * 1157 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_state *sd_state, 1158 enum v4l2_subdev_format_whence which) 1159 { 1160 if (which == V4L2_SUBDEV_FORMAT_TRY) 1161 return v4l2_subdev_get_try_crop(&priv->sd, sd_state, 1162 CSI_SINK_PAD); 1163 else 1164 return &priv->crop; 1165 } 1166 1167 static struct v4l2_rect * 1168 __csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_state *sd_state, 1169 enum v4l2_subdev_format_whence which) 1170 { 1171 if (which == V4L2_SUBDEV_FORMAT_TRY) 1172 return v4l2_subdev_get_try_compose(&priv->sd, sd_state, 1173 CSI_SINK_PAD); 1174 else 1175 return &priv->compose; 1176 } 1177 1178 static void csi_try_crop(struct csi_priv *priv, 1179 struct v4l2_rect *crop, 1180 struct v4l2_subdev_state *sd_state, 1181 struct v4l2_mbus_framefmt *infmt, 1182 struct v4l2_mbus_config *mbus_cfg) 1183 { 1184 u32 in_height; 1185 1186 crop->width = min_t(__u32, infmt->width, crop->width); 1187 if (crop->left + crop->width > infmt->width) 1188 crop->left = infmt->width - crop->width; 1189 /* adjust crop left/width to h/w alignment restrictions */ 1190 crop->left &= ~0x3; 1191 if (priv->active_output_pad == CSI_SRC_PAD_DIRECT) 1192 crop->width &= ~0x7; /* multiple of 8 pixels (IC burst) */ 1193 else 1194 crop->width &= ~0x1; /* multiple of 2 pixels */ 1195 1196 in_height = infmt->height; 1197 if (infmt->field == V4L2_FIELD_ALTERNATE) 1198 in_height *= 2; 1199 1200 /* 1201 * FIXME: not sure why yet, but on interlaced bt.656, 1202 * changing the vertical cropping causes loss of vertical 1203 * sync, so fix it to NTSC/PAL active lines. NTSC contains 1204 * 2 extra lines of active video that need to be cropped. 1205 */ 1206 if (mbus_cfg->type == V4L2_MBUS_BT656 && 1207 (V4L2_FIELD_HAS_BOTH(infmt->field) || 1208 infmt->field == V4L2_FIELD_ALTERNATE)) { 1209 crop->height = in_height; 1210 crop->top = (in_height == 480) ? 2 : 0; 1211 } else { 1212 crop->height = min_t(__u32, in_height, crop->height); 1213 if (crop->top + crop->height > in_height) 1214 crop->top = in_height - crop->height; 1215 } 1216 } 1217 1218 static int csi_enum_mbus_code(struct v4l2_subdev *sd, 1219 struct v4l2_subdev_state *sd_state, 1220 struct v4l2_subdev_mbus_code_enum *code) 1221 { 1222 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1223 struct v4l2_mbus_config mbus_cfg = { .type = 0 }; 1224 const struct imx_media_pixfmt *incc; 1225 struct v4l2_mbus_framefmt *infmt; 1226 int ret = 0; 1227 1228 mutex_lock(&priv->lock); 1229 1230 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, code->which); 1231 incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY); 1232 1233 switch (code->pad) { 1234 case CSI_SINK_PAD: 1235 ret = imx_media_enum_mbus_formats(&code->code, code->index, 1236 PIXFMT_SEL_ANY); 1237 break; 1238 case CSI_SRC_PAD_DIRECT: 1239 case CSI_SRC_PAD_IDMAC: 1240 ret = csi_get_upstream_mbus_config(priv, &mbus_cfg); 1241 if (ret) { 1242 v4l2_err(&priv->sd, 1243 "failed to get upstream media bus configuration\n"); 1244 goto out; 1245 } 1246 1247 if (requires_passthrough(&mbus_cfg, infmt, incc)) { 1248 if (code->index != 0) { 1249 ret = -EINVAL; 1250 goto out; 1251 } 1252 code->code = infmt->code; 1253 } else { 1254 enum imx_pixfmt_sel fmt_sel = 1255 (incc->cs == IPUV3_COLORSPACE_YUV) ? 1256 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB; 1257 1258 ret = imx_media_enum_ipu_formats(&code->code, 1259 code->index, 1260 fmt_sel); 1261 } 1262 break; 1263 default: 1264 ret = -EINVAL; 1265 } 1266 1267 out: 1268 mutex_unlock(&priv->lock); 1269 return ret; 1270 } 1271 1272 static int csi_enum_frame_size(struct v4l2_subdev *sd, 1273 struct v4l2_subdev_state *sd_state, 1274 struct v4l2_subdev_frame_size_enum *fse) 1275 { 1276 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1277 struct v4l2_rect *crop; 1278 int ret = 0; 1279 1280 if (fse->pad >= CSI_NUM_PADS || 1281 fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3)) 1282 return -EINVAL; 1283 1284 mutex_lock(&priv->lock); 1285 1286 if (fse->pad == CSI_SINK_PAD) { 1287 fse->min_width = MIN_W; 1288 fse->max_width = MAX_W; 1289 fse->min_height = MIN_H; 1290 fse->max_height = MAX_H; 1291 } else { 1292 crop = __csi_get_crop(priv, sd_state, fse->which); 1293 1294 fse->min_width = fse->index & 1 ? 1295 crop->width / 2 : crop->width; 1296 fse->max_width = fse->min_width; 1297 fse->min_height = fse->index & 2 ? 1298 crop->height / 2 : crop->height; 1299 fse->max_height = fse->min_height; 1300 } 1301 1302 mutex_unlock(&priv->lock); 1303 return ret; 1304 } 1305 1306 static int csi_enum_frame_interval(struct v4l2_subdev *sd, 1307 struct v4l2_subdev_state *sd_state, 1308 struct v4l2_subdev_frame_interval_enum *fie) 1309 { 1310 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1311 struct v4l2_fract *input_fi; 1312 struct v4l2_rect *crop; 1313 int ret = 0; 1314 1315 if (fie->pad >= CSI_NUM_PADS || 1316 fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ? 1317 1 : ARRAY_SIZE(csi_skip))) 1318 return -EINVAL; 1319 1320 mutex_lock(&priv->lock); 1321 1322 input_fi = &priv->frame_interval[CSI_SINK_PAD]; 1323 crop = __csi_get_crop(priv, sd_state, fie->which); 1324 1325 if ((fie->width != crop->width && fie->width != crop->width / 2) || 1326 (fie->height != crop->height && fie->height != crop->height / 2)) { 1327 ret = -EINVAL; 1328 goto out; 1329 } 1330 1331 fie->interval = *input_fi; 1332 1333 if (fie->pad == CSI_SRC_PAD_IDMAC) 1334 csi_apply_skip_interval(&csi_skip[fie->index], 1335 &fie->interval); 1336 1337 out: 1338 mutex_unlock(&priv->lock); 1339 return ret; 1340 } 1341 1342 static int csi_get_fmt(struct v4l2_subdev *sd, 1343 struct v4l2_subdev_state *sd_state, 1344 struct v4l2_subdev_format *sdformat) 1345 { 1346 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1347 struct v4l2_mbus_framefmt *fmt; 1348 int ret = 0; 1349 1350 if (sdformat->pad >= CSI_NUM_PADS) 1351 return -EINVAL; 1352 1353 mutex_lock(&priv->lock); 1354 1355 fmt = __csi_get_fmt(priv, sd_state, sdformat->pad, sdformat->which); 1356 if (!fmt) { 1357 ret = -EINVAL; 1358 goto out; 1359 } 1360 1361 sdformat->format = *fmt; 1362 out: 1363 mutex_unlock(&priv->lock); 1364 return ret; 1365 } 1366 1367 static void csi_try_field(struct csi_priv *priv, 1368 struct v4l2_subdev_state *sd_state, 1369 struct v4l2_subdev_format *sdformat) 1370 { 1371 struct v4l2_mbus_framefmt *infmt = 1372 __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sdformat->which); 1373 1374 /* 1375 * no restrictions on sink pad field type except must 1376 * be initialized. 1377 */ 1378 if (sdformat->pad == CSI_SINK_PAD) { 1379 if (sdformat->format.field == V4L2_FIELD_ANY) 1380 sdformat->format.field = V4L2_FIELD_NONE; 1381 return; 1382 } 1383 1384 switch (infmt->field) { 1385 case V4L2_FIELD_SEQ_TB: 1386 case V4L2_FIELD_SEQ_BT: 1387 /* 1388 * If the user requests sequential at the source pad, 1389 * allow it (along with possibly inverting field order). 1390 * Otherwise passthrough the field type. 1391 */ 1392 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field)) 1393 sdformat->format.field = infmt->field; 1394 break; 1395 case V4L2_FIELD_ALTERNATE: 1396 /* 1397 * This driver does not support alternate field mode, and 1398 * the CSI captures a whole frame, so the CSI never presents 1399 * alternate mode at its source pads. If user has not 1400 * already requested sequential, translate ALTERNATE at 1401 * sink pad to SEQ_TB or SEQ_BT at the source pad depending 1402 * on input height (assume NTSC BT order if 480 total active 1403 * frame lines, otherwise PAL TB order). 1404 */ 1405 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field)) 1406 sdformat->format.field = (infmt->height == 480 / 2) ? 1407 V4L2_FIELD_SEQ_BT : V4L2_FIELD_SEQ_TB; 1408 break; 1409 default: 1410 /* Passthrough for all other input field types */ 1411 sdformat->format.field = infmt->field; 1412 break; 1413 } 1414 } 1415 1416 static void csi_try_fmt(struct csi_priv *priv, 1417 struct v4l2_mbus_config *mbus_cfg, 1418 struct v4l2_subdev_state *sd_state, 1419 struct v4l2_subdev_format *sdformat, 1420 struct v4l2_rect *crop, 1421 struct v4l2_rect *compose, 1422 const struct imx_media_pixfmt **cc) 1423 { 1424 const struct imx_media_pixfmt *incc; 1425 struct v4l2_mbus_framefmt *infmt; 1426 u32 code; 1427 1428 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sdformat->which); 1429 1430 switch (sdformat->pad) { 1431 case CSI_SRC_PAD_DIRECT: 1432 case CSI_SRC_PAD_IDMAC: 1433 incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY); 1434 1435 sdformat->format.width = compose->width; 1436 sdformat->format.height = compose->height; 1437 1438 if (requires_passthrough(mbus_cfg, infmt, incc)) { 1439 sdformat->format.code = infmt->code; 1440 *cc = incc; 1441 } else { 1442 enum imx_pixfmt_sel fmt_sel = 1443 (incc->cs == IPUV3_COLORSPACE_YUV) ? 1444 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB; 1445 1446 *cc = imx_media_find_ipu_format(sdformat->format.code, 1447 fmt_sel); 1448 if (!*cc) { 1449 imx_media_enum_ipu_formats(&code, 0, fmt_sel); 1450 *cc = imx_media_find_ipu_format(code, fmt_sel); 1451 sdformat->format.code = (*cc)->codes[0]; 1452 } 1453 } 1454 1455 csi_try_field(priv, sd_state, sdformat); 1456 1457 /* propagate colorimetry from sink */ 1458 sdformat->format.colorspace = infmt->colorspace; 1459 sdformat->format.xfer_func = infmt->xfer_func; 1460 sdformat->format.quantization = infmt->quantization; 1461 sdformat->format.ycbcr_enc = infmt->ycbcr_enc; 1462 1463 break; 1464 case CSI_SINK_PAD: 1465 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W, 1466 W_ALIGN, &sdformat->format.height, 1467 MIN_H, MAX_H, H_ALIGN, S_ALIGN); 1468 1469 *cc = imx_media_find_mbus_format(sdformat->format.code, 1470 PIXFMT_SEL_ANY); 1471 if (!*cc) { 1472 imx_media_enum_mbus_formats(&code, 0, 1473 PIXFMT_SEL_YUV_RGB); 1474 *cc = imx_media_find_mbus_format(code, 1475 PIXFMT_SEL_YUV_RGB); 1476 sdformat->format.code = (*cc)->codes[0]; 1477 } 1478 1479 csi_try_field(priv, sd_state, sdformat); 1480 1481 /* Reset crop and compose rectangles */ 1482 crop->left = 0; 1483 crop->top = 0; 1484 crop->width = sdformat->format.width; 1485 crop->height = sdformat->format.height; 1486 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) 1487 crop->height *= 2; 1488 csi_try_crop(priv, crop, sd_state, &sdformat->format, mbus_cfg); 1489 compose->left = 0; 1490 compose->top = 0; 1491 compose->width = crop->width; 1492 compose->height = crop->height; 1493 1494 break; 1495 } 1496 1497 imx_media_try_colorimetry(&sdformat->format, 1498 priv->active_output_pad == CSI_SRC_PAD_DIRECT); 1499 } 1500 1501 static int csi_set_fmt(struct v4l2_subdev *sd, 1502 struct v4l2_subdev_state *sd_state, 1503 struct v4l2_subdev_format *sdformat) 1504 { 1505 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1506 struct v4l2_mbus_config mbus_cfg = { .type = 0 }; 1507 const struct imx_media_pixfmt *cc; 1508 struct v4l2_mbus_framefmt *fmt; 1509 struct v4l2_rect *crop, *compose; 1510 int ret; 1511 1512 if (sdformat->pad >= CSI_NUM_PADS) 1513 return -EINVAL; 1514 1515 ret = csi_get_upstream_mbus_config(priv, &mbus_cfg); 1516 if (ret) { 1517 v4l2_err(&priv->sd, 1518 "failed to get upstream media bus configuration\n"); 1519 return ret; 1520 } 1521 1522 mutex_lock(&priv->lock); 1523 1524 if (priv->stream_count > 0) { 1525 ret = -EBUSY; 1526 goto out; 1527 } 1528 1529 crop = __csi_get_crop(priv, sd_state, sdformat->which); 1530 compose = __csi_get_compose(priv, sd_state, sdformat->which); 1531 1532 csi_try_fmt(priv, &mbus_cfg, sd_state, sdformat, crop, compose, &cc); 1533 1534 fmt = __csi_get_fmt(priv, sd_state, sdformat->pad, sdformat->which); 1535 *fmt = sdformat->format; 1536 1537 if (sdformat->pad == CSI_SINK_PAD) { 1538 int pad; 1539 1540 /* propagate format to source pads */ 1541 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) { 1542 const struct imx_media_pixfmt *outcc; 1543 struct v4l2_mbus_framefmt *outfmt; 1544 struct v4l2_subdev_format format; 1545 1546 format.pad = pad; 1547 format.which = sdformat->which; 1548 format.format = sdformat->format; 1549 csi_try_fmt(priv, &mbus_cfg, sd_state, &format, NULL, 1550 compose, &outcc); 1551 1552 outfmt = __csi_get_fmt(priv, sd_state, pad, 1553 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_ACTIVE) 1562 priv->cc[sdformat->pad] = cc; 1563 1564 out: 1565 mutex_unlock(&priv->lock); 1566 return ret; 1567 } 1568 1569 static int csi_get_selection(struct v4l2_subdev *sd, 1570 struct v4l2_subdev_state *sd_state, 1571 struct v4l2_subdev_selection *sel) 1572 { 1573 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1574 struct v4l2_mbus_framefmt *infmt; 1575 struct v4l2_rect *crop, *compose; 1576 int ret = 0; 1577 1578 if (sel->pad != CSI_SINK_PAD) 1579 return -EINVAL; 1580 1581 mutex_lock(&priv->lock); 1582 1583 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sel->which); 1584 crop = __csi_get_crop(priv, sd_state, sel->which); 1585 compose = __csi_get_compose(priv, sd_state, sel->which); 1586 1587 switch (sel->target) { 1588 case V4L2_SEL_TGT_CROP_BOUNDS: 1589 sel->r.left = 0; 1590 sel->r.top = 0; 1591 sel->r.width = infmt->width; 1592 sel->r.height = infmt->height; 1593 if (infmt->field == V4L2_FIELD_ALTERNATE) 1594 sel->r.height *= 2; 1595 break; 1596 case V4L2_SEL_TGT_CROP: 1597 sel->r = *crop; 1598 break; 1599 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 1600 sel->r.left = 0; 1601 sel->r.top = 0; 1602 sel->r.width = crop->width; 1603 sel->r.height = crop->height; 1604 break; 1605 case V4L2_SEL_TGT_COMPOSE: 1606 sel->r = *compose; 1607 break; 1608 default: 1609 ret = -EINVAL; 1610 } 1611 1612 mutex_unlock(&priv->lock); 1613 return ret; 1614 } 1615 1616 static int csi_set_scale(u32 *compose, u32 crop, u32 flags) 1617 { 1618 if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) == 1619 (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) && 1620 *compose != crop && *compose != crop / 2) 1621 return -ERANGE; 1622 1623 if (*compose <= crop / 2 || 1624 (*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) || 1625 (*compose < crop && (flags & V4L2_SEL_FLAG_LE))) 1626 *compose = crop / 2; 1627 else 1628 *compose = crop; 1629 1630 return 0; 1631 } 1632 1633 static int csi_set_selection(struct v4l2_subdev *sd, 1634 struct v4l2_subdev_state *sd_state, 1635 struct v4l2_subdev_selection *sel) 1636 { 1637 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1638 struct v4l2_mbus_config mbus_cfg = { .type = 0 }; 1639 struct v4l2_mbus_framefmt *infmt; 1640 struct v4l2_rect *crop, *compose; 1641 int pad, ret; 1642 1643 if (sel->pad != CSI_SINK_PAD) 1644 return -EINVAL; 1645 1646 ret = csi_get_upstream_mbus_config(priv, &mbus_cfg); 1647 if (ret) { 1648 v4l2_err(&priv->sd, 1649 "failed to get upstream media bus configuration\n"); 1650 return ret; 1651 } 1652 1653 mutex_lock(&priv->lock); 1654 1655 if (priv->stream_count > 0) { 1656 ret = -EBUSY; 1657 goto out; 1658 } 1659 1660 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sel->which); 1661 crop = __csi_get_crop(priv, sd_state, sel->which); 1662 compose = __csi_get_compose(priv, sd_state, sel->which); 1663 1664 switch (sel->target) { 1665 case V4L2_SEL_TGT_CROP: 1666 /* 1667 * Modifying the crop rectangle always changes the format on 1668 * the source pads. If the KEEP_CONFIG flag is set, just return 1669 * the current crop rectangle. 1670 */ 1671 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) { 1672 sel->r = priv->crop; 1673 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) 1674 *crop = sel->r; 1675 goto out; 1676 } 1677 1678 csi_try_crop(priv, &sel->r, sd_state, infmt, &mbus_cfg); 1679 1680 *crop = sel->r; 1681 1682 /* Reset scaling to 1:1 */ 1683 compose->width = crop->width; 1684 compose->height = crop->height; 1685 break; 1686 case V4L2_SEL_TGT_COMPOSE: 1687 /* 1688 * Modifying the compose rectangle always changes the format on 1689 * the source pads. If the KEEP_CONFIG flag is set, just return 1690 * the current compose rectangle. 1691 */ 1692 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) { 1693 sel->r = priv->compose; 1694 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) 1695 *compose = sel->r; 1696 goto out; 1697 } 1698 1699 sel->r.left = 0; 1700 sel->r.top = 0; 1701 ret = csi_set_scale(&sel->r.width, crop->width, sel->flags); 1702 if (ret) 1703 goto out; 1704 ret = csi_set_scale(&sel->r.height, crop->height, sel->flags); 1705 if (ret) 1706 goto out; 1707 1708 *compose = sel->r; 1709 break; 1710 default: 1711 ret = -EINVAL; 1712 goto out; 1713 } 1714 1715 /* Reset source pads to sink compose rectangle */ 1716 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) { 1717 struct v4l2_mbus_framefmt *outfmt; 1718 1719 outfmt = __csi_get_fmt(priv, sd_state, pad, sel->which); 1720 outfmt->width = compose->width; 1721 outfmt->height = compose->height; 1722 } 1723 1724 out: 1725 mutex_unlock(&priv->lock); 1726 return ret; 1727 } 1728 1729 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, 1730 struct v4l2_event_subscription *sub) 1731 { 1732 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR) 1733 return -EINVAL; 1734 if (sub->id != 0) 1735 return -EINVAL; 1736 1737 return v4l2_event_subscribe(fh, sub, 0, NULL); 1738 } 1739 1740 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh, 1741 struct v4l2_event_subscription *sub) 1742 { 1743 return v4l2_event_unsubscribe(fh, sub); 1744 } 1745 1746 static int csi_registered(struct v4l2_subdev *sd) 1747 { 1748 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1749 struct ipu_csi *csi; 1750 int i, ret; 1751 u32 code; 1752 1753 /* get handle to IPU CSI */ 1754 csi = ipu_csi_get(priv->ipu, priv->csi_id); 1755 if (IS_ERR(csi)) { 1756 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id); 1757 return PTR_ERR(csi); 1758 } 1759 priv->csi = csi; 1760 1761 for (i = 0; i < CSI_NUM_PADS; i++) { 1762 code = 0; 1763 if (i != CSI_SINK_PAD) 1764 imx_media_enum_ipu_formats(&code, 0, PIXFMT_SEL_YUV); 1765 1766 /* set a default mbus format */ 1767 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i], 1768 IMX_MEDIA_DEF_PIX_WIDTH, 1769 IMX_MEDIA_DEF_PIX_HEIGHT, code, 1770 V4L2_FIELD_NONE, &priv->cc[i]); 1771 if (ret) 1772 goto put_csi; 1773 1774 /* init default frame interval */ 1775 priv->frame_interval[i].numerator = 1; 1776 priv->frame_interval[i].denominator = 30; 1777 } 1778 1779 /* disable frame skipping */ 1780 priv->skip = &csi_skip[0]; 1781 1782 /* init default crop and compose rectangle sizes */ 1783 priv->crop.width = IMX_MEDIA_DEF_PIX_WIDTH; 1784 priv->crop.height = IMX_MEDIA_DEF_PIX_HEIGHT; 1785 priv->compose.width = IMX_MEDIA_DEF_PIX_WIDTH; 1786 priv->compose.height = IMX_MEDIA_DEF_PIX_HEIGHT; 1787 1788 priv->fim = imx_media_fim_init(&priv->sd); 1789 if (IS_ERR(priv->fim)) { 1790 ret = PTR_ERR(priv->fim); 1791 goto put_csi; 1792 } 1793 1794 priv->vdev = imx_media_capture_device_init(priv->sd.dev, &priv->sd, 1795 CSI_SRC_PAD_IDMAC, true); 1796 if (IS_ERR(priv->vdev)) { 1797 ret = PTR_ERR(priv->vdev); 1798 goto free_fim; 1799 } 1800 1801 ret = imx_media_capture_device_register(priv->vdev, 0); 1802 if (ret) 1803 goto remove_vdev; 1804 1805 return 0; 1806 1807 remove_vdev: 1808 imx_media_capture_device_remove(priv->vdev); 1809 free_fim: 1810 if (priv->fim) 1811 imx_media_fim_free(priv->fim); 1812 put_csi: 1813 ipu_csi_put(priv->csi); 1814 return ret; 1815 } 1816 1817 static void csi_unregistered(struct v4l2_subdev *sd) 1818 { 1819 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1820 1821 imx_media_capture_device_unregister(priv->vdev); 1822 imx_media_capture_device_remove(priv->vdev); 1823 1824 if (priv->fim) 1825 imx_media_fim_free(priv->fim); 1826 1827 if (priv->csi) 1828 ipu_csi_put(priv->csi); 1829 } 1830 1831 /* 1832 * The CSI has only one fwnode endpoint, at the sink pad. Verify the 1833 * endpoint belongs to us, and return CSI_SINK_PAD. 1834 */ 1835 static int csi_get_fwnode_pad(struct media_entity *entity, 1836 struct fwnode_endpoint *endpoint) 1837 { 1838 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); 1839 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1840 struct fwnode_handle *csi_port = dev_fwnode(priv->dev); 1841 struct fwnode_handle *csi_ep; 1842 int ret; 1843 1844 csi_ep = fwnode_get_next_child_node(csi_port, NULL); 1845 1846 ret = endpoint->local_fwnode == csi_ep ? CSI_SINK_PAD : -ENXIO; 1847 1848 fwnode_handle_put(csi_ep); 1849 1850 return ret; 1851 } 1852 1853 static const struct media_entity_operations csi_entity_ops = { 1854 .link_setup = csi_link_setup, 1855 .link_validate = v4l2_subdev_link_validate, 1856 .get_fwnode_pad = csi_get_fwnode_pad, 1857 }; 1858 1859 static const struct v4l2_subdev_core_ops csi_core_ops = { 1860 .subscribe_event = csi_subscribe_event, 1861 .unsubscribe_event = csi_unsubscribe_event, 1862 }; 1863 1864 static const struct v4l2_subdev_video_ops csi_video_ops = { 1865 .g_frame_interval = csi_g_frame_interval, 1866 .s_frame_interval = csi_s_frame_interval, 1867 .s_stream = csi_s_stream, 1868 }; 1869 1870 static const struct v4l2_subdev_pad_ops csi_pad_ops = { 1871 .init_cfg = imx_media_init_cfg, 1872 .enum_mbus_code = csi_enum_mbus_code, 1873 .enum_frame_size = csi_enum_frame_size, 1874 .enum_frame_interval = csi_enum_frame_interval, 1875 .get_fmt = csi_get_fmt, 1876 .set_fmt = csi_set_fmt, 1877 .get_selection = csi_get_selection, 1878 .set_selection = csi_set_selection, 1879 .link_validate = csi_link_validate, 1880 }; 1881 1882 static const struct v4l2_subdev_ops csi_subdev_ops = { 1883 .core = &csi_core_ops, 1884 .video = &csi_video_ops, 1885 .pad = &csi_pad_ops, 1886 }; 1887 1888 static const struct v4l2_subdev_internal_ops csi_internal_ops = { 1889 .registered = csi_registered, 1890 .unregistered = csi_unregistered, 1891 }; 1892 1893 static int imx_csi_notify_bound(struct v4l2_async_notifier *notifier, 1894 struct v4l2_subdev *sd, 1895 struct v4l2_async_subdev *asd) 1896 { 1897 struct csi_priv *priv = notifier_to_dev(notifier); 1898 struct media_pad *sink = &priv->sd.entity.pads[CSI_SINK_PAD]; 1899 1900 /* 1901 * If the subdev is a video mux, it must be one of the CSI 1902 * muxes. Mark it as such via its group id. 1903 */ 1904 if (sd->entity.function == MEDIA_ENT_F_VID_MUX) 1905 sd->grp_id = IMX_MEDIA_GRP_ID_CSI_MUX; 1906 1907 return v4l2_create_fwnode_links_to_pad(sd, sink, 0); 1908 } 1909 1910 static const struct v4l2_async_notifier_operations csi_notify_ops = { 1911 .bound = imx_csi_notify_bound, 1912 }; 1913 1914 static int imx_csi_async_register(struct csi_priv *priv) 1915 { 1916 struct v4l2_async_subdev *asd = NULL; 1917 struct fwnode_handle *ep; 1918 unsigned int port; 1919 int ret; 1920 1921 v4l2_async_nf_init(&priv->notifier); 1922 1923 /* get this CSI's port id */ 1924 ret = fwnode_property_read_u32(dev_fwnode(priv->dev), "reg", &port); 1925 if (ret < 0) 1926 return ret; 1927 1928 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(priv->dev->parent), 1929 port, 0, 1930 FWNODE_GRAPH_ENDPOINT_NEXT); 1931 if (ep) { 1932 asd = v4l2_async_nf_add_fwnode_remote(&priv->notifier, ep, 1933 struct v4l2_async_subdev); 1934 1935 fwnode_handle_put(ep); 1936 1937 if (IS_ERR(asd)) { 1938 ret = PTR_ERR(asd); 1939 /* OK if asd already exists */ 1940 if (ret != -EEXIST) 1941 return ret; 1942 } 1943 } 1944 1945 priv->notifier.ops = &csi_notify_ops; 1946 1947 ret = v4l2_async_subdev_nf_register(&priv->sd, &priv->notifier); 1948 if (ret) 1949 return ret; 1950 1951 return v4l2_async_register_subdev(&priv->sd); 1952 } 1953 1954 static int imx_csi_probe(struct platform_device *pdev) 1955 { 1956 struct ipu_client_platformdata *pdata; 1957 struct pinctrl *pinctrl; 1958 struct csi_priv *priv; 1959 int i, ret; 1960 1961 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 1962 if (!priv) 1963 return -ENOMEM; 1964 1965 platform_set_drvdata(pdev, &priv->sd); 1966 priv->dev = &pdev->dev; 1967 1968 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32)); 1969 if (ret) 1970 return ret; 1971 1972 /* get parent IPU */ 1973 priv->ipu = dev_get_drvdata(priv->dev->parent); 1974 1975 /* get our CSI id */ 1976 pdata = priv->dev->platform_data; 1977 priv->csi_id = pdata->csi; 1978 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2; 1979 1980 priv->active_output_pad = CSI_SRC_PAD_IDMAC; 1981 1982 timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0); 1983 spin_lock_init(&priv->irqlock); 1984 1985 v4l2_subdev_init(&priv->sd, &csi_subdev_ops); 1986 v4l2_set_subdevdata(&priv->sd, priv); 1987 priv->sd.internal_ops = &csi_internal_ops; 1988 priv->sd.entity.ops = &csi_entity_ops; 1989 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER; 1990 priv->sd.dev = &pdev->dev; 1991 priv->sd.fwnode = of_fwnode_handle(pdata->of_node); 1992 priv->sd.owner = THIS_MODULE; 1993 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; 1994 priv->sd.grp_id = priv->csi_id ? 1995 IMX_MEDIA_GRP_ID_IPU_CSI1 : IMX_MEDIA_GRP_ID_IPU_CSI0; 1996 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name), 1997 priv->sd.grp_id, ipu_get_num(priv->ipu)); 1998 1999 for (i = 0; i < CSI_NUM_PADS; i++) 2000 priv->pad[i].flags = (i == CSI_SINK_PAD) ? 2001 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE; 2002 2003 ret = media_entity_pads_init(&priv->sd.entity, CSI_NUM_PADS, 2004 priv->pad); 2005 if (ret) 2006 return ret; 2007 2008 mutex_init(&priv->lock); 2009 2010 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0); 2011 priv->sd.ctrl_handler = &priv->ctrl_hdlr; 2012 2013 /* 2014 * The IPUv3 driver did not assign an of_node to this 2015 * device. As a result, pinctrl does not automatically 2016 * configure our pin groups, so we need to do that manually 2017 * here, after setting this device's of_node. 2018 */ 2019 priv->dev->of_node = pdata->of_node; 2020 pinctrl = devm_pinctrl_get_select_default(priv->dev); 2021 if (IS_ERR(pinctrl)) { 2022 ret = PTR_ERR(pinctrl); 2023 dev_dbg(priv->dev, 2024 "devm_pinctrl_get_select_default() failed: %d\n", ret); 2025 if (ret != -ENODEV) 2026 goto free; 2027 } 2028 2029 ret = imx_csi_async_register(priv); 2030 if (ret) 2031 goto cleanup; 2032 2033 return 0; 2034 2035 cleanup: 2036 v4l2_async_nf_unregister(&priv->notifier); 2037 v4l2_async_nf_cleanup(&priv->notifier); 2038 free: 2039 v4l2_ctrl_handler_free(&priv->ctrl_hdlr); 2040 mutex_destroy(&priv->lock); 2041 return ret; 2042 } 2043 2044 static void imx_csi_remove(struct platform_device *pdev) 2045 { 2046 struct v4l2_subdev *sd = platform_get_drvdata(pdev); 2047 struct csi_priv *priv = sd_to_dev(sd); 2048 2049 v4l2_ctrl_handler_free(&priv->ctrl_hdlr); 2050 mutex_destroy(&priv->lock); 2051 v4l2_async_nf_unregister(&priv->notifier); 2052 v4l2_async_nf_cleanup(&priv->notifier); 2053 v4l2_async_unregister_subdev(sd); 2054 media_entity_cleanup(&sd->entity); 2055 } 2056 2057 static const struct platform_device_id imx_csi_ids[] = { 2058 { .name = "imx-ipuv3-csi" }, 2059 { }, 2060 }; 2061 MODULE_DEVICE_TABLE(platform, imx_csi_ids); 2062 2063 static struct platform_driver imx_csi_driver = { 2064 .probe = imx_csi_probe, 2065 .remove_new = imx_csi_remove, 2066 .id_table = imx_csi_ids, 2067 .driver = { 2068 .name = "imx-ipuv3-csi", 2069 }, 2070 }; 2071 module_platform_driver(imx_csi_driver); 2072 2073 MODULE_DESCRIPTION("i.MX CSI subdev driver"); 2074 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>"); 2075 MODULE_LICENSE("GPL"); 2076