1 // SPDX-License-Identifier: GPL-2.0+
3 * V4L2 Capture CSI Subdev for Freescale i.MX5/6 SOC
5 * Copyright (c) 2014-2017 Mentor Graphics Inc.
6 * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
8 #include <linux/delay.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"
27 * Min/Max supported width and heights.
29 * We allow planar output, so we have to align width by 16 pixels
30 * to meet IDMAC alignment requirements.
32 * TODO: move this into pad format negotiation, if capture device
33 * has not requested planar formats, we should allow 8 pixel
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 */
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
50 struct csi_skip_desc {
59 struct v4l2_subdev sd;
60 struct media_pad pad[CSI_NUM_PADS];
61 /* the video device at IDMAC output pad */
62 struct imx_media_video_dev *vdev;
63 struct imx_media_fim *fim;
67 /* lock to protect all members below */
70 int active_output_pad;
72 struct ipuv3_channel *idmac_ch;
73 struct ipu_smfc *smfc;
76 struct v4l2_mbus_framefmt format_mbus[CSI_NUM_PADS];
77 const struct imx_media_pixfmt *cc[CSI_NUM_PADS];
78 struct v4l2_fract frame_interval[CSI_NUM_PADS];
79 struct v4l2_rect crop;
80 struct v4l2_rect compose;
81 const struct csi_skip_desc *skip;
83 /* active vb2 buffers to send to video dev sink */
84 struct imx_media_buffer *active_vb2_buf[2];
85 struct imx_media_dma_buf underrun_buf;
87 int ipu_buf_num; /* ipu double buffer index: 0-1 */
89 /* the sink for the captured frames */
90 struct media_entity *sink;
91 enum ipu_csi_dest dest;
92 /* the source subdev */
93 struct v4l2_subdev *src_sd;
95 /* the mipi virtual channel number at link validate */
98 /* the upstream endpoint CSI is receiving from */
99 struct v4l2_fwnode_endpoint upstream_ep;
101 spinlock_t irqlock; /* protect eof_irq handler */
102 struct timer_list eof_timeout_timer;
106 struct v4l2_ctrl_handler ctrl_hdlr;
108 int stream_count; /* streaming counter */
109 u32 frame_sequence; /* frame sequence counter */
110 bool last_eof; /* waiting for last EOF at stream off */
111 bool nfb4eof; /* NFB4EOF encountered during streaming */
112 bool interweave_swap; /* swap top/bottom lines when interweaving */
113 struct completion last_eof_comp;
116 static inline struct csi_priv *sd_to_dev(struct v4l2_subdev *sdev)
118 return container_of(sdev, struct csi_priv, sd);
121 static inline bool is_parallel_bus(struct v4l2_fwnode_endpoint *ep)
123 return ep->bus_type != V4L2_MBUS_CSI2_DPHY;
126 static inline bool is_parallel_16bit_bus(struct v4l2_fwnode_endpoint *ep)
128 return is_parallel_bus(ep) && ep->bus.parallel.bus_width >= 16;
132 * Check for conditions that require the IPU to handle the
133 * data internally as generic data, aka passthrough mode:
134 * - raw bayer media bus formats, or
135 * - the CSI is receiving from a 16-bit parallel bus, or
136 * - the CSI is receiving from an 8-bit parallel bus and the incoming
137 * media bus format is other than UYVY8_2X8/YUYV8_2X8.
139 static inline bool requires_passthrough(struct v4l2_fwnode_endpoint *ep,
140 struct v4l2_mbus_framefmt *infmt,
141 const struct imx_media_pixfmt *incc)
143 return incc->bayer || is_parallel_16bit_bus(ep) ||
144 (is_parallel_bus(ep) &&
145 infmt->code != MEDIA_BUS_FMT_UYVY8_2X8 &&
146 infmt->code != MEDIA_BUS_FMT_YUYV8_2X8);
150 * Parses the fwnode endpoint from the source pad of the entity
151 * connected to this CSI. This will either be the entity directly
152 * upstream from the CSI-2 receiver, directly upstream from the
153 * video mux, or directly upstream from the CSI itself. The endpoint
154 * is needed to determine the bus type and bus config coming into
157 static int csi_get_upstream_endpoint(struct csi_priv *priv,
158 struct v4l2_fwnode_endpoint *ep)
160 struct device_node *endpoint, *port;
161 struct media_entity *src;
162 struct v4l2_subdev *sd;
163 struct media_pad *pad;
165 if (!IS_ENABLED(CONFIG_OF))
174 if (src->function == MEDIA_ENT_F_VID_MUX) {
176 * CSI is connected directly to video mux, skip up to
177 * CSI-2 receiver if it is in the path, otherwise stay
180 sd = imx_media_pipeline_subdev(src, IMX_MEDIA_GRP_ID_CSI2,
187 * If the source is neither the video mux nor the CSI-2 receiver,
188 * get the source pad directly upstream from CSI itself.
190 if (src->function != MEDIA_ENT_F_VID_MUX &&
191 sd->grp_id != IMX_MEDIA_GRP_ID_CSI2)
192 src = &priv->sd.entity;
194 /* get source pad of entity directly upstream from src */
195 pad = imx_media_pipeline_pad(src, 0, 0, true);
199 sd = media_entity_to_v4l2_subdev(pad->entity);
202 * NOTE: this assumes an OF-graph port id is the same as a
205 port = of_graph_get_port_by_id(sd->dev->of_node, pad->index);
209 endpoint = of_get_next_child(port, NULL);
214 v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint), ep);
215 of_node_put(endpoint);
220 static void csi_idmac_put_ipu_resources(struct csi_priv *priv)
223 ipu_idmac_put(priv->idmac_ch);
224 priv->idmac_ch = NULL;
227 ipu_smfc_put(priv->smfc);
231 static int csi_idmac_get_ipu_resources(struct csi_priv *priv)
234 struct ipu_smfc *smfc;
235 struct ipuv3_channel *idmac_ch;
237 ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id;
239 smfc = ipu_smfc_get(priv->ipu, ch_num);
241 v4l2_err(&priv->sd, "failed to get SMFC\n");
247 idmac_ch = ipu_idmac_get(priv->ipu, ch_num);
248 if (IS_ERR(idmac_ch)) {
249 v4l2_err(&priv->sd, "could not get IDMAC channel %u\n",
251 ret = PTR_ERR(idmac_ch);
254 priv->idmac_ch = idmac_ch;
258 csi_idmac_put_ipu_resources(priv);
262 static void csi_vb2_buf_done(struct csi_priv *priv)
264 struct imx_media_video_dev *vdev = priv->vdev;
265 struct imx_media_buffer *done, *next;
266 struct vb2_buffer *vb;
269 done = priv->active_vb2_buf[priv->ipu_buf_num];
271 done->vbuf.field = vdev->fmt.fmt.pix.field;
272 done->vbuf.sequence = priv->frame_sequence;
273 vb = &done->vbuf.vb2_buf;
274 vb->timestamp = ktime_get_ns();
275 vb2_buffer_done(vb, priv->nfb4eof ?
276 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
279 priv->frame_sequence++;
280 priv->nfb4eof = false;
282 /* get next queued buffer */
283 next = imx_media_capture_device_next_buf(vdev);
285 phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0);
286 priv->active_vb2_buf[priv->ipu_buf_num] = next;
288 phys = priv->underrun_buf.phys;
289 priv->active_vb2_buf[priv->ipu_buf_num] = NULL;
292 if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num))
293 ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num);
295 if (priv->interweave_swap)
296 phys += vdev->fmt.fmt.pix.bytesperline;
298 ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, phys);
301 static irqreturn_t csi_idmac_eof_interrupt(int irq, void *dev_id)
303 struct csi_priv *priv = dev_id;
305 spin_lock(&priv->irqlock);
307 if (priv->last_eof) {
308 complete(&priv->last_eof_comp);
309 priv->last_eof = false;
314 /* call frame interval monitor */
315 imx_media_fim_eof_monitor(priv->fim, ktime_get());
317 csi_vb2_buf_done(priv);
319 /* select new IPU buf */
320 ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num);
321 /* toggle IPU double-buffer index */
322 priv->ipu_buf_num ^= 1;
324 /* bump the EOF timeout timer */
325 mod_timer(&priv->eof_timeout_timer,
326 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
329 spin_unlock(&priv->irqlock);
333 static irqreturn_t csi_idmac_nfb4eof_interrupt(int irq, void *dev_id)
335 struct csi_priv *priv = dev_id;
337 spin_lock(&priv->irqlock);
340 * this is not an unrecoverable error, just mark
341 * the next captured frame with vb2 error flag.
343 priv->nfb4eof = true;
345 v4l2_err(&priv->sd, "NFB4EOF\n");
347 spin_unlock(&priv->irqlock);
353 * EOF timeout timer function. This is an unrecoverable condition
354 * without a stream restart.
356 static void csi_idmac_eof_timeout(struct timer_list *t)
358 struct csi_priv *priv = from_timer(priv, t, eof_timeout_timer);
359 struct imx_media_video_dev *vdev = priv->vdev;
361 v4l2_err(&priv->sd, "EOF timeout\n");
363 /* signal a fatal error to capture device */
364 imx_media_capture_device_error(vdev);
367 static void csi_idmac_setup_vb2_buf(struct csi_priv *priv, dma_addr_t *phys)
369 struct imx_media_video_dev *vdev = priv->vdev;
370 struct imx_media_buffer *buf;
373 for (i = 0; i < 2; i++) {
374 buf = imx_media_capture_device_next_buf(vdev);
376 priv->active_vb2_buf[i] = buf;
377 phys[i] = vb2_dma_contig_plane_dma_addr(
378 &buf->vbuf.vb2_buf, 0);
380 priv->active_vb2_buf[i] = NULL;
381 phys[i] = priv->underrun_buf.phys;
386 static void csi_idmac_unsetup_vb2_buf(struct csi_priv *priv,
387 enum vb2_buffer_state return_status)
389 struct imx_media_buffer *buf;
392 /* return any remaining active frames with return_status */
393 for (i = 0; i < 2; i++) {
394 buf = priv->active_vb2_buf[i];
396 struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
398 vb->timestamp = ktime_get_ns();
399 vb2_buffer_done(vb, return_status);
404 /* init the SMFC IDMAC channel */
405 static int csi_idmac_setup_channel(struct csi_priv *priv)
407 struct imx_media_video_dev *vdev = priv->vdev;
408 const struct imx_media_pixfmt *incc;
409 struct v4l2_mbus_framefmt *infmt;
410 struct v4l2_mbus_framefmt *outfmt;
411 bool passthrough, interweave;
412 struct ipu_image image;
413 u32 passthrough_bits;
414 u32 passthrough_cycles;
419 infmt = &priv->format_mbus[CSI_SINK_PAD];
420 incc = priv->cc[CSI_SINK_PAD];
421 outfmt = &priv->format_mbus[CSI_SRC_PAD_IDMAC];
423 ipu_cpmem_zero(priv->idmac_ch);
425 memset(&image, 0, sizeof(image));
426 image.pix = vdev->fmt.fmt.pix;
427 image.rect = vdev->compose;
429 csi_idmac_setup_vb2_buf(priv, phys);
431 image.phys0 = phys[0];
432 image.phys1 = phys[1];
434 passthrough = requires_passthrough(&priv->upstream_ep, infmt, incc);
435 passthrough_cycles = 1;
438 * If the field type at capture interface is interlaced, and
439 * the output IDMAC pad is sequential, enable interweave at
440 * the IDMAC output channel.
442 interweave = V4L2_FIELD_IS_INTERLACED(image.pix.field) &&
443 V4L2_FIELD_IS_SEQUENTIAL(outfmt->field);
444 priv->interweave_swap = interweave &&
445 image.pix.field == V4L2_FIELD_INTERLACED_BT;
447 switch (image.pix.pixelformat) {
448 case V4L2_PIX_FMT_SBGGR8:
449 case V4L2_PIX_FMT_SGBRG8:
450 case V4L2_PIX_FMT_SGRBG8:
451 case V4L2_PIX_FMT_SRGGB8:
452 case V4L2_PIX_FMT_GREY:
454 passthrough_bits = 8;
456 case V4L2_PIX_FMT_SBGGR16:
457 case V4L2_PIX_FMT_SGBRG16:
458 case V4L2_PIX_FMT_SGRBG16:
459 case V4L2_PIX_FMT_SRGGB16:
460 case V4L2_PIX_FMT_Y16:
462 passthrough_bits = 16;
464 case V4L2_PIX_FMT_YUV420:
465 case V4L2_PIX_FMT_YVU420:
466 case V4L2_PIX_FMT_NV12:
467 burst_size = (image.pix.width & 0x3f) ?
468 ((image.pix.width & 0x1f) ?
469 ((image.pix.width & 0xf) ? 8 : 16) : 32) : 64;
470 passthrough_bits = 16;
472 * Skip writing U and V components to odd rows (but not
473 * when enabling IDMAC interweaving, they are incompatible).
476 ipu_cpmem_skip_odd_chroma_rows(priv->idmac_ch);
478 case V4L2_PIX_FMT_YUYV:
479 case V4L2_PIX_FMT_UYVY:
480 burst_size = (image.pix.width & 0x1f) ?
481 ((image.pix.width & 0xf) ? 8 : 16) : 32;
482 passthrough_bits = 16;
484 case V4L2_PIX_FMT_RGB565:
487 passthrough_bits = 8;
488 passthrough_cycles = incc->cycles;
491 /* fallthrough - non-passthrough RGB565 (CSI-2 bus) */
493 burst_size = (image.pix.width & 0xf) ? 8 : 16;
494 passthrough_bits = 16;
499 if (priv->interweave_swap) {
500 /* start interweave scan at 1st top line (2nd line) */
501 image.phys0 += image.pix.bytesperline;
502 image.phys1 += image.pix.bytesperline;
505 ipu_cpmem_set_resolution(priv->idmac_ch,
506 image.rect.width * passthrough_cycles,
508 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
509 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
510 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
511 ipu_cpmem_set_format_passthrough(priv->idmac_ch,
514 if (priv->interweave_swap) {
515 /* start interweave scan at 1st top line (2nd line) */
519 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
524 ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
527 * Set the channel for the direct CSI-->memory via SMFC
528 * use-case to very high priority, by enabling the watermark
529 * signal in the SMFC, enabling WM in the channel, and setting
530 * the channel priority to high.
532 * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority
535 * The WM's are set very low by intention here to ensure that
536 * the SMFC FIFOs do not overflow.
538 ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
539 ipu_cpmem_set_high_priority(priv->idmac_ch);
540 ipu_idmac_enable_watermark(priv->idmac_ch, true);
541 ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
543 burst_size = passthrough ?
544 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
546 ipu_smfc_set_burstsize(priv->smfc, burst_size);
549 ipu_cpmem_interlaced_scan(priv->idmac_ch,
550 priv->interweave_swap ?
551 -image.pix.bytesperline :
552 image.pix.bytesperline,
553 image.pix.pixelformat);
555 ipu_idmac_set_double_buffer(priv->idmac_ch, true);
560 csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
564 static void csi_idmac_unsetup(struct csi_priv *priv,
565 enum vb2_buffer_state state)
567 ipu_idmac_disable_channel(priv->idmac_ch);
568 ipu_smfc_disable(priv->smfc);
570 csi_idmac_unsetup_vb2_buf(priv, state);
573 static int csi_idmac_setup(struct csi_priv *priv)
577 ret = csi_idmac_setup_channel(priv);
581 ipu_cpmem_dump(priv->idmac_ch);
584 ipu_smfc_enable(priv->smfc);
586 /* set buffers ready */
587 ipu_idmac_select_buffer(priv->idmac_ch, 0);
588 ipu_idmac_select_buffer(priv->idmac_ch, 1);
590 /* enable the channels */
591 ipu_idmac_enable_channel(priv->idmac_ch);
596 static int csi_idmac_start(struct csi_priv *priv)
598 struct imx_media_video_dev *vdev = priv->vdev;
599 struct v4l2_pix_format *outfmt;
602 ret = csi_idmac_get_ipu_resources(priv);
606 ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
608 outfmt = &vdev->fmt.fmt.pix;
610 ret = imx_media_alloc_dma_buf(priv->dev, &priv->underrun_buf,
615 priv->ipu_buf_num = 0;
617 /* init EOF completion waitq */
618 init_completion(&priv->last_eof_comp);
619 priv->frame_sequence = 0;
620 priv->last_eof = false;
621 priv->nfb4eof = false;
623 ret = csi_idmac_setup(priv);
625 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
626 goto out_free_dma_buf;
629 priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
632 ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
633 csi_idmac_nfb4eof_interrupt, 0,
634 "imx-smfc-nfb4eof", priv);
637 "Error registering NFB4EOF irq: %d\n", ret);
641 priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
644 ret = devm_request_irq(priv->dev, priv->eof_irq,
645 csi_idmac_eof_interrupt, 0,
646 "imx-smfc-eof", priv);
649 "Error registering eof irq: %d\n", ret);
650 goto out_free_nfb4eof_irq;
653 /* start the EOF timeout timer */
654 mod_timer(&priv->eof_timeout_timer,
655 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
659 out_free_nfb4eof_irq:
660 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
662 csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
664 imx_media_free_dma_buf(priv->dev, &priv->underrun_buf);
666 csi_idmac_put_ipu_resources(priv);
670 static void csi_idmac_wait_last_eof(struct csi_priv *priv)
675 /* mark next EOF interrupt as the last before stream off */
676 spin_lock_irqsave(&priv->irqlock, flags);
677 priv->last_eof = true;
678 spin_unlock_irqrestore(&priv->irqlock, flags);
681 * and then wait for interrupt handler to mark completion.
683 ret = wait_for_completion_timeout(
684 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
686 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
689 static void csi_idmac_stop(struct csi_priv *priv)
691 devm_free_irq(priv->dev, priv->eof_irq, priv);
692 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
694 csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
696 imx_media_free_dma_buf(priv->dev, &priv->underrun_buf);
698 /* cancel the EOF timeout timer */
699 del_timer_sync(&priv->eof_timeout_timer);
701 csi_idmac_put_ipu_resources(priv);
704 /* Update the CSI whole sensor and active windows */
705 static int csi_setup(struct csi_priv *priv)
707 struct v4l2_mbus_framefmt *infmt, *outfmt;
708 const struct imx_media_pixfmt *incc;
709 struct v4l2_mbus_config mbus_cfg;
710 struct v4l2_mbus_framefmt if_fmt;
711 struct v4l2_rect crop;
713 infmt = &priv->format_mbus[CSI_SINK_PAD];
714 incc = priv->cc[CSI_SINK_PAD];
715 outfmt = &priv->format_mbus[priv->active_output_pad];
717 /* compose mbus_config from the upstream endpoint */
718 mbus_cfg.type = priv->upstream_ep.bus_type;
719 mbus_cfg.flags = is_parallel_bus(&priv->upstream_ep) ?
720 priv->upstream_ep.bus.parallel.flags :
721 priv->upstream_ep.bus.mipi_csi2.flags;
727 * if cycles is set, we need to handle this over multiple cycles as
730 if (is_parallel_bus(&priv->upstream_ep) && incc->cycles) {
731 if_fmt.width *= incc->cycles;
732 crop.width *= incc->cycles;
735 ipu_csi_set_window(priv->csi, &crop);
737 ipu_csi_set_downsize(priv->csi,
738 priv->crop.width == 2 * priv->compose.width,
739 priv->crop.height == 2 * priv->compose.height);
741 ipu_csi_init_interface(priv->csi, &mbus_cfg, &if_fmt, outfmt);
743 ipu_csi_set_dest(priv->csi, priv->dest);
745 if (priv->dest == IPU_CSI_DEST_IDMAC)
746 ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc,
747 priv->skip->max_ratio - 1, 0);
749 ipu_csi_dump(priv->csi);
754 static int csi_start(struct csi_priv *priv)
756 struct v4l2_fract *output_fi;
759 output_fi = &priv->frame_interval[priv->active_output_pad];
762 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
763 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
767 if (priv->dest == IPU_CSI_DEST_IDMAC) {
768 ret = csi_idmac_start(priv);
773 ret = csi_setup(priv);
777 /* start the frame interval monitor */
778 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) {
779 ret = imx_media_fim_set_stream(priv->fim, output_fi, true);
784 ret = ipu_csi_enable(priv->csi);
786 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
793 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
794 imx_media_fim_set_stream(priv->fim, NULL, false);
796 if (priv->dest == IPU_CSI_DEST_IDMAC)
797 csi_idmac_stop(priv);
799 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
803 static void csi_stop(struct csi_priv *priv)
805 if (priv->dest == IPU_CSI_DEST_IDMAC)
806 csi_idmac_wait_last_eof(priv);
809 * Disable the CSI asap, after syncing with the last EOF.
810 * Doing so after the IDMA channel is disabled has shown to
811 * create hard system-wide hangs.
813 ipu_csi_disable(priv->csi);
816 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
818 if (priv->dest == IPU_CSI_DEST_IDMAC) {
819 csi_idmac_stop(priv);
821 /* stop the frame interval monitor */
823 imx_media_fim_set_stream(priv->fim, NULL, false);
827 static const struct csi_skip_desc csi_skip[12] = {
828 { 1, 1, 0x00 }, /* Keep all frames */
829 { 5, 6, 0x10 }, /* Skip every sixth frame */
830 { 4, 5, 0x08 }, /* Skip every fifth frame */
831 { 3, 4, 0x04 }, /* Skip every fourth frame */
832 { 2, 3, 0x02 }, /* Skip every third frame */
833 { 3, 5, 0x0a }, /* Skip frames 1 and 3 of every 5 */
834 { 1, 2, 0x01 }, /* Skip every second frame */
835 { 2, 5, 0x0b }, /* Keep frames 1 and 4 of every 5 */
836 { 1, 3, 0x03 }, /* Keep one in three frames */
837 { 1, 4, 0x07 }, /* Keep one in four frames */
838 { 1, 5, 0x0f }, /* Keep one in five frames */
839 { 1, 6, 0x1f }, /* Keep one in six frames */
842 static void csi_apply_skip_interval(const struct csi_skip_desc *skip,
843 struct v4l2_fract *interval)
847 interval->numerator *= skip->max_ratio;
848 interval->denominator *= skip->keep;
850 /* Reduce fraction to lowest terms */
851 div = gcd(interval->numerator, interval->denominator);
853 interval->numerator /= div;
854 interval->denominator /= div;
859 * Find the skip pattern to produce the output frame interval closest to the
860 * requested one, for the given input frame interval. Updates the output frame
861 * interval to the exact value.
863 static const struct csi_skip_desc *csi_find_best_skip(struct v4l2_fract *in,
864 struct v4l2_fract *out)
866 const struct csi_skip_desc *skip = &csi_skip[0], *best_skip = skip;
867 u32 min_err = UINT_MAX;
871 /* Default to 1:1 ratio */
872 if (out->numerator == 0 || out->denominator == 0 ||
873 in->numerator == 0 || in->denominator == 0) {
878 want_us = div_u64((u64)USEC_PER_SEC * out->numerator, out->denominator);
880 /* Find the reduction closest to the requested time per frame */
881 for (i = 0; i < ARRAY_SIZE(csi_skip); i++, skip++) {
884 tmp = div_u64((u64)USEC_PER_SEC * in->numerator *
885 skip->max_ratio, in->denominator * skip->keep);
887 err = abs((s64)tmp - want_us);
895 csi_apply_skip_interval(best_skip, out);
901 * V4L2 subdev operations.
904 static int csi_g_frame_interval(struct v4l2_subdev *sd,
905 struct v4l2_subdev_frame_interval *fi)
907 struct csi_priv *priv = v4l2_get_subdevdata(sd);
909 if (fi->pad >= CSI_NUM_PADS)
912 mutex_lock(&priv->lock);
914 fi->interval = priv->frame_interval[fi->pad];
916 mutex_unlock(&priv->lock);
921 static int csi_s_frame_interval(struct v4l2_subdev *sd,
922 struct v4l2_subdev_frame_interval *fi)
924 struct csi_priv *priv = v4l2_get_subdevdata(sd);
925 struct v4l2_fract *input_fi;
928 mutex_lock(&priv->lock);
930 input_fi = &priv->frame_interval[CSI_SINK_PAD];
934 /* No limits on valid input frame intervals */
935 if (fi->interval.numerator == 0 ||
936 fi->interval.denominator == 0)
937 fi->interval = *input_fi;
938 /* Reset output intervals and frame skipping ratio to 1:1 */
939 priv->frame_interval[CSI_SRC_PAD_IDMAC] = fi->interval;
940 priv->frame_interval[CSI_SRC_PAD_DIRECT] = fi->interval;
941 priv->skip = &csi_skip[0];
943 case CSI_SRC_PAD_IDMAC:
945 * frame interval at IDMAC output pad depends on input
946 * interval, modified by frame skipping.
948 priv->skip = csi_find_best_skip(input_fi, &fi->interval);
950 case CSI_SRC_PAD_DIRECT:
952 * frame interval at DIRECT output pad is same as input
955 fi->interval = *input_fi;
962 priv->frame_interval[fi->pad] = fi->interval;
964 mutex_unlock(&priv->lock);
968 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
970 struct csi_priv *priv = v4l2_get_subdevdata(sd);
973 mutex_lock(&priv->lock);
975 if (!priv->src_sd || !priv->sink) {
981 * enable/disable streaming only if stream_count is
982 * going from 0 to 1 / 1 to 0.
984 if (priv->stream_count != !enable)
988 dev_dbg(priv->dev, "stream ON\n");
989 ret = csi_start(priv);
993 dev_dbg(priv->dev, "stream OFF\n");
998 priv->stream_count += enable ? 1 : -1;
999 if (priv->stream_count < 0)
1000 priv->stream_count = 0;
1002 mutex_unlock(&priv->lock);
1006 static int csi_link_setup(struct media_entity *entity,
1007 const struct media_pad *local,
1008 const struct media_pad *remote, u32 flags)
1010 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1011 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1012 struct v4l2_subdev *remote_sd;
1015 dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
1016 local->entity->name);
1018 mutex_lock(&priv->lock);
1020 if (local->flags & MEDIA_PAD_FL_SINK) {
1021 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1026 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1028 if (flags & MEDIA_LNK_FL_ENABLED) {
1033 priv->src_sd = remote_sd;
1035 priv->src_sd = NULL;
1041 /* this is a source pad */
1043 if (flags & MEDIA_LNK_FL_ENABLED) {
1049 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1050 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1052 /* do not apply IC burst alignment in csi_try_crop */
1053 priv->active_output_pad = CSI_SRC_PAD_IDMAC;
1057 /* record which output pad is now active */
1058 priv->active_output_pad = local->index;
1060 /* set CSI destination */
1061 if (local->index == CSI_SRC_PAD_IDMAC) {
1062 if (!is_media_entity_v4l2_video_device(remote->entity)) {
1068 ret = imx_media_fim_add_controls(priv->fim);
1073 priv->dest = IPU_CSI_DEST_IDMAC;
1075 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1080 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1081 switch (remote_sd->grp_id) {
1082 case IMX_MEDIA_GRP_ID_IPU_VDIC:
1083 priv->dest = IPU_CSI_DEST_VDIC;
1085 case IMX_MEDIA_GRP_ID_IPU_IC_PRP:
1086 priv->dest = IPU_CSI_DEST_IC;
1094 priv->sink = remote->entity;
1096 mutex_unlock(&priv->lock);
1100 static int csi_link_validate(struct v4l2_subdev *sd,
1101 struct media_link *link,
1102 struct v4l2_subdev_format *source_fmt,
1103 struct v4l2_subdev_format *sink_fmt)
1105 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1106 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1110 ret = v4l2_subdev_link_validate_default(sd, link,
1111 source_fmt, sink_fmt);
1115 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1117 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1121 mutex_lock(&priv->lock);
1123 priv->upstream_ep = upstream_ep;
1124 is_csi2 = !is_parallel_bus(&upstream_ep);
1128 * NOTE! It seems the virtual channels from the mipi csi-2
1129 * receiver are used only for routing by the video mux's,
1130 * or for hard-wired routing to the CSI's. Once the stream
1131 * enters the CSI's however, they are treated internally
1132 * in the IPU as virtual channel 0.
1135 mutex_unlock(&priv->lock);
1136 vc_num = imx_media_find_mipi_csi2_channel(&priv->sd.entity);
1139 mutex_lock(&priv->lock);
1141 ipu_csi_set_mipi_datatype(priv->csi, vc_num,
1142 &priv->format_mbus[CSI_SINK_PAD]);
1145 /* select either parallel or MIPI-CSI2 as input to CSI */
1146 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
1148 mutex_unlock(&priv->lock);
1152 static struct v4l2_mbus_framefmt *
1153 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1154 unsigned int pad, enum v4l2_subdev_format_whence which)
1156 if (which == V4L2_SUBDEV_FORMAT_TRY)
1157 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad);
1159 return &priv->format_mbus[pad];
1162 static struct v4l2_rect *
1163 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1164 enum v4l2_subdev_format_whence which)
1166 if (which == V4L2_SUBDEV_FORMAT_TRY)
1167 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD);
1172 static struct v4l2_rect *
1173 __csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1174 enum v4l2_subdev_format_whence which)
1176 if (which == V4L2_SUBDEV_FORMAT_TRY)
1177 return v4l2_subdev_get_try_compose(&priv->sd, cfg,
1180 return &priv->compose;
1183 static void csi_try_crop(struct csi_priv *priv,
1184 struct v4l2_rect *crop,
1185 struct v4l2_subdev_pad_config *cfg,
1186 struct v4l2_mbus_framefmt *infmt,
1187 struct v4l2_fwnode_endpoint *upstream_ep)
1191 crop->width = min_t(__u32, infmt->width, crop->width);
1192 if (crop->left + crop->width > infmt->width)
1193 crop->left = infmt->width - crop->width;
1194 /* adjust crop left/width to h/w alignment restrictions */
1196 if (priv->active_output_pad == CSI_SRC_PAD_DIRECT)
1197 crop->width &= ~0x7; /* multiple of 8 pixels (IC burst) */
1199 crop->width &= ~0x1; /* multiple of 2 pixels */
1201 in_height = infmt->height;
1202 if (infmt->field == V4L2_FIELD_ALTERNATE)
1206 * FIXME: not sure why yet, but on interlaced bt.656,
1207 * changing the vertical cropping causes loss of vertical
1208 * sync, so fix it to NTSC/PAL active lines. NTSC contains
1209 * 2 extra lines of active video that need to be cropped.
1211 if (upstream_ep->bus_type == V4L2_MBUS_BT656 &&
1212 (V4L2_FIELD_HAS_BOTH(infmt->field) ||
1213 infmt->field == V4L2_FIELD_ALTERNATE)) {
1214 crop->height = in_height;
1215 crop->top = (in_height == 480) ? 2 : 0;
1217 crop->height = min_t(__u32, in_height, crop->height);
1218 if (crop->top + crop->height > in_height)
1219 crop->top = in_height - crop->height;
1223 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
1224 struct v4l2_subdev_pad_config *cfg,
1225 struct v4l2_subdev_mbus_code_enum *code)
1227 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1228 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1229 const struct imx_media_pixfmt *incc;
1230 struct v4l2_mbus_framefmt *infmt;
1233 mutex_lock(&priv->lock);
1235 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which);
1236 incc = imx_media_find_mbus_format(infmt->code, CS_SEL_ANY, true);
1238 switch (code->pad) {
1240 ret = imx_media_enum_mbus_format(&code->code, code->index,
1243 case CSI_SRC_PAD_DIRECT:
1244 case CSI_SRC_PAD_IDMAC:
1245 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1247 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1251 if (requires_passthrough(&upstream_ep, infmt, incc)) {
1252 if (code->index != 0) {
1256 code->code = infmt->code;
1258 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1259 CS_SEL_YUV : CS_SEL_RGB;
1260 ret = imx_media_enum_ipu_format(&code->code,
1270 mutex_unlock(&priv->lock);
1274 static int csi_enum_frame_size(struct v4l2_subdev *sd,
1275 struct v4l2_subdev_pad_config *cfg,
1276 struct v4l2_subdev_frame_size_enum *fse)
1278 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1279 struct v4l2_rect *crop;
1282 if (fse->pad >= CSI_NUM_PADS ||
1283 fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3))
1286 mutex_lock(&priv->lock);
1288 if (fse->pad == CSI_SINK_PAD) {
1289 fse->min_width = MIN_W;
1290 fse->max_width = MAX_W;
1291 fse->min_height = MIN_H;
1292 fse->max_height = MAX_H;
1294 crop = __csi_get_crop(priv, cfg, fse->which);
1296 fse->min_width = fse->index & 1 ?
1297 crop->width / 2 : crop->width;
1298 fse->max_width = fse->min_width;
1299 fse->min_height = fse->index & 2 ?
1300 crop->height / 2 : crop->height;
1301 fse->max_height = fse->min_height;
1304 mutex_unlock(&priv->lock);
1308 static int csi_enum_frame_interval(struct v4l2_subdev *sd,
1309 struct v4l2_subdev_pad_config *cfg,
1310 struct v4l2_subdev_frame_interval_enum *fie)
1312 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1313 struct v4l2_fract *input_fi;
1314 struct v4l2_rect *crop;
1317 if (fie->pad >= CSI_NUM_PADS ||
1318 fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ?
1319 1 : ARRAY_SIZE(csi_skip)))
1322 mutex_lock(&priv->lock);
1324 input_fi = &priv->frame_interval[CSI_SINK_PAD];
1325 crop = __csi_get_crop(priv, cfg, fie->which);
1327 if ((fie->width != crop->width && fie->width != crop->width / 2) ||
1328 (fie->height != crop->height && fie->height != crop->height / 2)) {
1333 fie->interval = *input_fi;
1335 if (fie->pad == CSI_SRC_PAD_IDMAC)
1336 csi_apply_skip_interval(&csi_skip[fie->index],
1340 mutex_unlock(&priv->lock);
1344 static int csi_get_fmt(struct v4l2_subdev *sd,
1345 struct v4l2_subdev_pad_config *cfg,
1346 struct v4l2_subdev_format *sdformat)
1348 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1349 struct v4l2_mbus_framefmt *fmt;
1352 if (sdformat->pad >= CSI_NUM_PADS)
1355 mutex_lock(&priv->lock);
1357 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1363 sdformat->format = *fmt;
1365 mutex_unlock(&priv->lock);
1369 static void csi_try_field(struct csi_priv *priv,
1370 struct v4l2_subdev_pad_config *cfg,
1371 struct v4l2_subdev_format *sdformat)
1373 struct v4l2_mbus_framefmt *infmt =
1374 __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
1376 /* no restrictions on sink pad field type */
1377 if (sdformat->pad == CSI_SINK_PAD)
1380 switch (infmt->field) {
1381 case V4L2_FIELD_SEQ_TB:
1382 case V4L2_FIELD_SEQ_BT:
1384 * If the user requests sequential at the source pad,
1385 * allow it (along with possibly inverting field order).
1386 * Otherwise passthrough the field type.
1388 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field))
1389 sdformat->format.field = infmt->field;
1391 case V4L2_FIELD_ALTERNATE:
1393 * This driver does not support alternate field mode, and
1394 * the CSI captures a whole frame, so the CSI never presents
1395 * alternate mode at its source pads. If user has not
1396 * already requested sequential, translate ALTERNATE at
1397 * sink pad to SEQ_TB or SEQ_BT at the source pad depending
1398 * on input height (assume NTSC BT order if 480 total active
1399 * frame lines, otherwise PAL TB order).
1401 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field))
1402 sdformat->format.field = (infmt->height == 480 / 2) ?
1403 V4L2_FIELD_SEQ_BT : V4L2_FIELD_SEQ_TB;
1406 /* Passthrough for all other input field types */
1407 sdformat->format.field = infmt->field;
1412 static void csi_try_fmt(struct csi_priv *priv,
1413 struct v4l2_fwnode_endpoint *upstream_ep,
1414 struct v4l2_subdev_pad_config *cfg,
1415 struct v4l2_subdev_format *sdformat,
1416 struct v4l2_rect *crop,
1417 struct v4l2_rect *compose,
1418 const struct imx_media_pixfmt **cc)
1420 const struct imx_media_pixfmt *incc;
1421 struct v4l2_mbus_framefmt *infmt;
1424 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
1426 switch (sdformat->pad) {
1427 case CSI_SRC_PAD_DIRECT:
1428 case CSI_SRC_PAD_IDMAC:
1429 incc = imx_media_find_mbus_format(infmt->code,
1432 sdformat->format.width = compose->width;
1433 sdformat->format.height = compose->height;
1435 if (requires_passthrough(upstream_ep, infmt, incc)) {
1436 sdformat->format.code = infmt->code;
1439 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1440 CS_SEL_YUV : CS_SEL_RGB;
1442 *cc = imx_media_find_ipu_format(sdformat->format.code,
1445 imx_media_enum_ipu_format(&code, 0, cs_sel);
1446 *cc = imx_media_find_ipu_format(code, cs_sel);
1447 sdformat->format.code = (*cc)->codes[0];
1451 csi_try_field(priv, cfg, sdformat);
1453 /* propagate colorimetry from sink */
1454 sdformat->format.colorspace = infmt->colorspace;
1455 sdformat->format.xfer_func = infmt->xfer_func;
1456 sdformat->format.quantization = infmt->quantization;
1457 sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
1461 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1462 W_ALIGN, &sdformat->format.height,
1463 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1465 *cc = imx_media_find_mbus_format(sdformat->format.code,
1468 imx_media_enum_mbus_format(&code, 0,
1470 *cc = imx_media_find_mbus_format(code,
1472 sdformat->format.code = (*cc)->codes[0];
1475 csi_try_field(priv, cfg, sdformat);
1477 imx_media_fill_default_mbus_fields(
1478 &sdformat->format, infmt,
1479 priv->active_output_pad == CSI_SRC_PAD_DIRECT);
1481 /* Reset crop and compose rectangles */
1484 crop->width = sdformat->format.width;
1485 crop->height = sdformat->format.height;
1486 if (sdformat->format.field == V4L2_FIELD_ALTERNATE)
1488 csi_try_crop(priv, crop, cfg, &sdformat->format, upstream_ep);
1491 compose->width = crop->width;
1492 compose->height = crop->height;
1498 static int csi_set_fmt(struct v4l2_subdev *sd,
1499 struct v4l2_subdev_pad_config *cfg,
1500 struct v4l2_subdev_format *sdformat)
1502 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1503 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1504 const struct imx_media_pixfmt *cc;
1505 struct v4l2_mbus_framefmt *fmt;
1506 struct v4l2_rect *crop, *compose;
1509 if (sdformat->pad >= CSI_NUM_PADS)
1512 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1514 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1518 mutex_lock(&priv->lock);
1520 if (priv->stream_count > 0) {
1525 crop = __csi_get_crop(priv, cfg, sdformat->which);
1526 compose = __csi_get_compose(priv, cfg, sdformat->which);
1528 csi_try_fmt(priv, &upstream_ep, cfg, sdformat, crop, compose, &cc);
1530 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1531 *fmt = sdformat->format;
1533 if (sdformat->pad == CSI_SINK_PAD) {
1536 /* propagate format to source pads */
1537 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1538 const struct imx_media_pixfmt *outcc;
1539 struct v4l2_mbus_framefmt *outfmt;
1540 struct v4l2_subdev_format format;
1543 format.which = sdformat->which;
1544 format.format = sdformat->format;
1545 csi_try_fmt(priv, &upstream_ep, cfg, &format,
1546 NULL, compose, &outcc);
1548 outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which);
1549 *outfmt = format.format;
1551 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1552 priv->cc[pad] = outcc;
1556 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1557 priv->cc[sdformat->pad] = cc;
1560 mutex_unlock(&priv->lock);
1564 static int csi_get_selection(struct v4l2_subdev *sd,
1565 struct v4l2_subdev_pad_config *cfg,
1566 struct v4l2_subdev_selection *sel)
1568 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1569 struct v4l2_mbus_framefmt *infmt;
1570 struct v4l2_rect *crop, *compose;
1573 if (sel->pad != CSI_SINK_PAD)
1576 mutex_lock(&priv->lock);
1578 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1579 crop = __csi_get_crop(priv, cfg, sel->which);
1580 compose = __csi_get_compose(priv, cfg, sel->which);
1582 switch (sel->target) {
1583 case V4L2_SEL_TGT_CROP_BOUNDS:
1586 sel->r.width = infmt->width;
1587 sel->r.height = infmt->height;
1588 if (infmt->field == V4L2_FIELD_ALTERNATE)
1591 case V4L2_SEL_TGT_CROP:
1594 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1597 sel->r.width = crop->width;
1598 sel->r.height = crop->height;
1600 case V4L2_SEL_TGT_COMPOSE:
1607 mutex_unlock(&priv->lock);
1611 static int csi_set_scale(u32 *compose, u32 crop, u32 flags)
1613 if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) ==
1614 (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) &&
1615 *compose != crop && *compose != crop / 2)
1618 if (*compose <= crop / 2 ||
1619 (*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) ||
1620 (*compose < crop && (flags & V4L2_SEL_FLAG_LE)))
1621 *compose = crop / 2;
1628 static int csi_set_selection(struct v4l2_subdev *sd,
1629 struct v4l2_subdev_pad_config *cfg,
1630 struct v4l2_subdev_selection *sel)
1632 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1633 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1634 struct v4l2_mbus_framefmt *infmt;
1635 struct v4l2_rect *crop, *compose;
1638 if (sel->pad != CSI_SINK_PAD)
1641 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1643 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1647 mutex_lock(&priv->lock);
1649 if (priv->stream_count > 0) {
1654 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1655 crop = __csi_get_crop(priv, cfg, sel->which);
1656 compose = __csi_get_compose(priv, cfg, sel->which);
1658 switch (sel->target) {
1659 case V4L2_SEL_TGT_CROP:
1661 * Modifying the crop rectangle always changes the format on
1662 * the source pads. If the KEEP_CONFIG flag is set, just return
1663 * the current crop rectangle.
1665 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1666 sel->r = priv->crop;
1667 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1672 csi_try_crop(priv, &sel->r, cfg, infmt, &upstream_ep);
1676 /* Reset scaling to 1:1 */
1677 compose->width = crop->width;
1678 compose->height = crop->height;
1680 case V4L2_SEL_TGT_COMPOSE:
1682 * Modifying the compose rectangle always changes the format on
1683 * the source pads. If the KEEP_CONFIG flag is set, just return
1684 * the current compose rectangle.
1686 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1687 sel->r = priv->compose;
1688 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1695 ret = csi_set_scale(&sel->r.width, crop->width, sel->flags);
1698 ret = csi_set_scale(&sel->r.height, crop->height, sel->flags);
1709 /* Reset source pads to sink compose rectangle */
1710 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1711 struct v4l2_mbus_framefmt *outfmt;
1713 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which);
1714 outfmt->width = compose->width;
1715 outfmt->height = compose->height;
1719 mutex_unlock(&priv->lock);
1723 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1724 struct v4l2_event_subscription *sub)
1726 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1731 return v4l2_event_subscribe(fh, sub, 0, NULL);
1734 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1735 struct v4l2_event_subscription *sub)
1737 return v4l2_event_unsubscribe(fh, sub);
1741 * retrieve our pads parsed from the OF graph by the media device
1743 static int csi_registered(struct v4l2_subdev *sd)
1745 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1746 struct ipu_csi *csi;
1750 /* get handle to IPU CSI */
1751 csi = ipu_csi_get(priv->ipu, priv->csi_id);
1753 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1754 return PTR_ERR(csi);
1758 for (i = 0; i < CSI_NUM_PADS; i++) {
1759 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
1760 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1763 if (i != CSI_SINK_PAD)
1764 imx_media_enum_ipu_format(&code, 0, CS_SEL_YUV);
1766 /* set a default mbus format */
1767 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1768 640, 480, code, V4L2_FIELD_NONE,
1773 /* init default frame interval */
1774 priv->frame_interval[i].numerator = 1;
1775 priv->frame_interval[i].denominator = 30;
1778 /* disable frame skipping */
1779 priv->skip = &csi_skip[0];
1781 /* init default crop and compose rectangle sizes */
1782 priv->crop.width = 640;
1783 priv->crop.height = 480;
1784 priv->compose.width = 640;
1785 priv->compose.height = 480;
1787 priv->fim = imx_media_fim_init(&priv->sd);
1788 if (IS_ERR(priv->fim)) {
1789 ret = PTR_ERR(priv->fim);
1793 ret = media_entity_pads_init(&sd->entity, CSI_NUM_PADS, priv->pad);
1797 ret = imx_media_capture_device_register(priv->vdev);
1805 imx_media_fim_free(priv->fim);
1807 ipu_csi_put(priv->csi);
1811 static void csi_unregistered(struct v4l2_subdev *sd)
1813 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1815 imx_media_capture_device_unregister(priv->vdev);
1818 imx_media_fim_free(priv->fim);
1821 ipu_csi_put(priv->csi);
1824 static const struct media_entity_operations csi_entity_ops = {
1825 .link_setup = csi_link_setup,
1826 .link_validate = v4l2_subdev_link_validate,
1829 static const struct v4l2_subdev_core_ops csi_core_ops = {
1830 .subscribe_event = csi_subscribe_event,
1831 .unsubscribe_event = csi_unsubscribe_event,
1834 static const struct v4l2_subdev_video_ops csi_video_ops = {
1835 .g_frame_interval = csi_g_frame_interval,
1836 .s_frame_interval = csi_s_frame_interval,
1837 .s_stream = csi_s_stream,
1840 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1841 .init_cfg = imx_media_init_cfg,
1842 .enum_mbus_code = csi_enum_mbus_code,
1843 .enum_frame_size = csi_enum_frame_size,
1844 .enum_frame_interval = csi_enum_frame_interval,
1845 .get_fmt = csi_get_fmt,
1846 .set_fmt = csi_set_fmt,
1847 .get_selection = csi_get_selection,
1848 .set_selection = csi_set_selection,
1849 .link_validate = csi_link_validate,
1852 static const struct v4l2_subdev_ops csi_subdev_ops = {
1853 .core = &csi_core_ops,
1854 .video = &csi_video_ops,
1855 .pad = &csi_pad_ops,
1858 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1859 .registered = csi_registered,
1860 .unregistered = csi_unregistered,
1863 static int imx_csi_parse_endpoint(struct device *dev,
1864 struct v4l2_fwnode_endpoint *vep,
1865 struct v4l2_async_subdev *asd)
1867 return fwnode_device_is_available(asd->match.fwnode) ? 0 : -ENOTCONN;
1870 static int imx_csi_async_register(struct csi_priv *priv)
1872 struct v4l2_async_notifier *notifier;
1873 struct fwnode_handle *fwnode;
1877 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
1881 v4l2_async_notifier_init(notifier);
1883 fwnode = dev_fwnode(priv->dev);
1885 /* get this CSI's port id */
1886 ret = fwnode_property_read_u32(fwnode, "reg", &port);
1890 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
1891 priv->dev->parent, notifier, sizeof(struct v4l2_async_subdev),
1892 port, imx_csi_parse_endpoint);
1896 ret = v4l2_async_subdev_notifier_register(&priv->sd, notifier);
1900 ret = v4l2_async_register_subdev(&priv->sd);
1902 goto out_unregister;
1904 priv->sd.subdev_notifier = notifier;
1909 v4l2_async_notifier_unregister(notifier);
1911 v4l2_async_notifier_cleanup(notifier);
1918 static int imx_csi_probe(struct platform_device *pdev)
1920 struct ipu_client_platformdata *pdata;
1921 struct pinctrl *pinctrl;
1922 struct csi_priv *priv;
1925 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1929 platform_set_drvdata(pdev, &priv->sd);
1930 priv->dev = &pdev->dev;
1932 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1936 /* get parent IPU */
1937 priv->ipu = dev_get_drvdata(priv->dev->parent);
1939 /* get our CSI id */
1940 pdata = priv->dev->platform_data;
1941 priv->csi_id = pdata->csi;
1942 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1944 priv->active_output_pad = CSI_SRC_PAD_IDMAC;
1946 timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0);
1947 spin_lock_init(&priv->irqlock);
1949 v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1950 v4l2_set_subdevdata(&priv->sd, priv);
1951 priv->sd.internal_ops = &csi_internal_ops;
1952 priv->sd.entity.ops = &csi_entity_ops;
1953 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1954 priv->sd.dev = &pdev->dev;
1955 priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
1956 priv->sd.owner = THIS_MODULE;
1957 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1958 priv->sd.grp_id = priv->csi_id ?
1959 IMX_MEDIA_GRP_ID_IPU_CSI1 : IMX_MEDIA_GRP_ID_IPU_CSI0;
1960 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
1961 priv->sd.grp_id, ipu_get_num(priv->ipu));
1963 priv->vdev = imx_media_capture_device_init(priv->sd.dev, &priv->sd,
1965 if (IS_ERR(priv->vdev))
1966 return PTR_ERR(priv->vdev);
1968 mutex_init(&priv->lock);
1970 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1971 priv->sd.ctrl_handler = &priv->ctrl_hdlr;
1974 * The IPUv3 driver did not assign an of_node to this
1975 * device. As a result, pinctrl does not automatically
1976 * configure our pin groups, so we need to do that manually
1977 * here, after setting this device's of_node.
1979 priv->dev->of_node = pdata->of_node;
1980 pinctrl = devm_pinctrl_get_select_default(priv->dev);
1981 if (IS_ERR(pinctrl)) {
1982 ret = PTR_ERR(pinctrl);
1984 "devm_pinctrl_get_select_default() failed: %d\n", ret);
1989 ret = imx_csi_async_register(priv);
1995 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1996 mutex_destroy(&priv->lock);
1997 imx_media_capture_device_remove(priv->vdev);
2001 static int imx_csi_remove(struct platform_device *pdev)
2003 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
2004 struct csi_priv *priv = sd_to_dev(sd);
2006 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
2007 mutex_destroy(&priv->lock);
2008 imx_media_capture_device_remove(priv->vdev);
2009 v4l2_async_unregister_subdev(sd);
2010 media_entity_cleanup(&sd->entity);
2015 static const struct platform_device_id imx_csi_ids[] = {
2016 { .name = "imx-ipuv3-csi" },
2019 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
2021 static struct platform_driver imx_csi_driver = {
2022 .probe = imx_csi_probe,
2023 .remove = imx_csi_remove,
2024 .id_table = imx_csi_ids,
2026 .name = "imx-ipuv3-csi",
2029 module_platform_driver(imx_csi_driver);
2031 MODULE_DESCRIPTION("i.MX CSI subdev driver");
2032 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
2033 MODULE_LICENSE("GPL");
2034 MODULE_ALIAS("platform:imx-ipuv3-csi");