2 * V4L2 Capture CSI Subdev for Freescale i.MX5/6 SOC
4 * Copyright (c) 2014-2017 Mentor Graphics Inc.
5 * Copyright (C) 2017 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 #include <linux/delay.h>
13 #include <linux/gcd.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/of_graph.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-fwnode.h>
23 #include <media/v4l2-mc.h>
24 #include <media/v4l2-subdev.h>
25 #include <media/videobuf2-dma-contig.h>
26 #include <video/imx-ipu-v3.h>
27 #include <media/imx.h>
28 #include "imx-media.h"
31 * Min/Max supported width and heights.
33 * We allow planar output, so we have to align width by 16 pixels
34 * to meet IDMAC alignment requirements.
36 * TODO: move this into pad format negotiation, if capture device
37 * has not requested planar formats, we should allow 8 pixel
44 #define W_ALIGN 4 /* multiple of 16 pixels */
45 #define H_ALIGN 1 /* multiple of 2 lines */
46 #define S_ALIGN 1 /* multiple of 2 */
49 * struct csi_skip_desc - CSI frame skipping descriptor
50 * @keep - number of frames kept per max_ratio frames
51 * @max_ratio - width of skip_smfc, written to MAX_RATIO bitfield
52 * @skip_smfc - skip pattern written to the SKIP_SMFC bitfield
54 struct csi_skip_desc {
63 struct imx_media_dev *md;
64 struct v4l2_subdev sd;
65 struct media_pad pad[CSI_NUM_PADS];
66 /* the video device at IDMAC output pad */
67 struct imx_media_video_dev *vdev;
68 struct imx_media_fim *fim;
72 /* lock to protect all members below */
75 int active_output_pad;
77 struct ipuv3_channel *idmac_ch;
78 struct ipu_smfc *smfc;
81 struct v4l2_mbus_framefmt format_mbus[CSI_NUM_PADS];
82 const struct imx_media_pixfmt *cc[CSI_NUM_PADS];
83 struct v4l2_fract frame_interval[CSI_NUM_PADS];
84 struct v4l2_rect crop;
85 struct v4l2_rect compose;
86 const struct csi_skip_desc *skip;
88 /* active vb2 buffers to send to video dev sink */
89 struct imx_media_buffer *active_vb2_buf[2];
90 struct imx_media_dma_buf underrun_buf;
92 int ipu_buf_num; /* ipu double buffer index: 0-1 */
94 /* the sink for the captured frames */
95 struct media_entity *sink;
96 enum ipu_csi_dest dest;
97 /* the source subdev */
98 struct v4l2_subdev *src_sd;
100 /* the mipi virtual channel number at link validate */
103 /* the upstream endpoint CSI is receiving from */
104 struct v4l2_fwnode_endpoint upstream_ep;
106 spinlock_t irqlock; /* protect eof_irq handler */
107 struct timer_list eof_timeout_timer;
111 struct v4l2_ctrl_handler ctrl_hdlr;
113 int stream_count; /* streaming counter */
114 u32 frame_sequence; /* frame sequence counter */
115 bool last_eof; /* waiting for last EOF at stream off */
116 bool nfb4eof; /* NFB4EOF encountered during streaming */
117 struct completion last_eof_comp;
120 static inline struct csi_priv *sd_to_dev(struct v4l2_subdev *sdev)
122 return container_of(sdev, struct csi_priv, sd);
125 static inline bool is_parallel_bus(struct v4l2_fwnode_endpoint *ep)
127 return ep->bus_type != V4L2_MBUS_CSI2_DPHY;
130 static inline bool is_parallel_16bit_bus(struct v4l2_fwnode_endpoint *ep)
132 return is_parallel_bus(ep) && ep->bus.parallel.bus_width >= 16;
136 * Check for conditions that require the IPU to handle the
137 * data internally as generic data, aka passthrough mode:
138 * - raw bayer media bus formats, or
139 * - the CSI is receiving from a 16-bit parallel bus, or
140 * - the CSI is receiving from an 8-bit parallel bus and the incoming
141 * media bus format is other than UYVY8_2X8/YUYV8_2X8.
143 static inline bool requires_passthrough(struct v4l2_fwnode_endpoint *ep,
144 struct v4l2_mbus_framefmt *infmt,
145 const struct imx_media_pixfmt *incc)
147 return incc->bayer || is_parallel_16bit_bus(ep) ||
148 (is_parallel_bus(ep) &&
149 infmt->code != MEDIA_BUS_FMT_UYVY8_2X8 &&
150 infmt->code != MEDIA_BUS_FMT_YUYV8_2X8);
154 * Parses the fwnode endpoint from the source pad of the entity
155 * connected to this CSI. This will either be the entity directly
156 * upstream from the CSI-2 receiver, or directly upstream from the
157 * video mux. The endpoint is needed to determine the bus type and
158 * bus config coming into the CSI.
160 static int csi_get_upstream_endpoint(struct csi_priv *priv,
161 struct v4l2_fwnode_endpoint *ep)
163 struct device_node *endpoint, *port;
164 struct media_entity *src;
165 struct v4l2_subdev *sd;
166 struct media_pad *pad;
168 if (!IS_ENABLED(CONFIG_OF))
174 src = &priv->src_sd->entity;
176 if (src->function == MEDIA_ENT_F_VID_MUX) {
178 * CSI is connected directly to video mux, skip up to
179 * CSI-2 receiver if it is in the path, otherwise stay
182 sd = imx_media_find_upstream_subdev(priv->md, src,
183 IMX_MEDIA_GRP_ID_CSI2);
188 /* get source pad of entity directly upstream from src */
189 pad = imx_media_find_upstream_pad(priv->md, src, 0);
193 sd = media_entity_to_v4l2_subdev(pad->entity);
196 * NOTE: this assumes an OF-graph port id is the same as a
199 port = of_graph_get_port_by_id(sd->dev->of_node, pad->index);
203 endpoint = of_get_next_child(port, NULL);
208 v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint), ep);
209 of_node_put(endpoint);
214 static void csi_idmac_put_ipu_resources(struct csi_priv *priv)
217 ipu_idmac_put(priv->idmac_ch);
218 priv->idmac_ch = NULL;
221 ipu_smfc_put(priv->smfc);
225 static int csi_idmac_get_ipu_resources(struct csi_priv *priv)
228 struct ipu_smfc *smfc;
229 struct ipuv3_channel *idmac_ch;
231 ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id;
233 smfc = ipu_smfc_get(priv->ipu, ch_num);
235 v4l2_err(&priv->sd, "failed to get SMFC\n");
241 idmac_ch = ipu_idmac_get(priv->ipu, ch_num);
242 if (IS_ERR(idmac_ch)) {
243 v4l2_err(&priv->sd, "could not get IDMAC channel %u\n",
245 ret = PTR_ERR(idmac_ch);
248 priv->idmac_ch = idmac_ch;
252 csi_idmac_put_ipu_resources(priv);
256 static void csi_vb2_buf_done(struct csi_priv *priv)
258 struct imx_media_video_dev *vdev = priv->vdev;
259 struct imx_media_buffer *done, *next;
260 struct vb2_buffer *vb;
263 done = priv->active_vb2_buf[priv->ipu_buf_num];
265 done->vbuf.field = vdev->fmt.fmt.pix.field;
266 done->vbuf.sequence = priv->frame_sequence;
267 vb = &done->vbuf.vb2_buf;
268 vb->timestamp = ktime_get_ns();
269 vb2_buffer_done(vb, priv->nfb4eof ?
270 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
273 priv->frame_sequence++;
274 priv->nfb4eof = false;
276 /* get next queued buffer */
277 next = imx_media_capture_device_next_buf(vdev);
279 phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0);
280 priv->active_vb2_buf[priv->ipu_buf_num] = next;
282 phys = priv->underrun_buf.phys;
283 priv->active_vb2_buf[priv->ipu_buf_num] = NULL;
286 if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num))
287 ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num);
289 ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, phys);
292 static irqreturn_t csi_idmac_eof_interrupt(int irq, void *dev_id)
294 struct csi_priv *priv = dev_id;
296 spin_lock(&priv->irqlock);
298 if (priv->last_eof) {
299 complete(&priv->last_eof_comp);
300 priv->last_eof = false;
305 /* call frame interval monitor */
306 imx_media_fim_eof_monitor(priv->fim, ktime_get());
308 csi_vb2_buf_done(priv);
310 /* select new IPU buf */
311 ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num);
312 /* toggle IPU double-buffer index */
313 priv->ipu_buf_num ^= 1;
315 /* bump the EOF timeout timer */
316 mod_timer(&priv->eof_timeout_timer,
317 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
320 spin_unlock(&priv->irqlock);
324 static irqreturn_t csi_idmac_nfb4eof_interrupt(int irq, void *dev_id)
326 struct csi_priv *priv = dev_id;
328 spin_lock(&priv->irqlock);
331 * this is not an unrecoverable error, just mark
332 * the next captured frame with vb2 error flag.
334 priv->nfb4eof = true;
336 v4l2_err(&priv->sd, "NFB4EOF\n");
338 spin_unlock(&priv->irqlock);
344 * EOF timeout timer function. This is an unrecoverable condition
345 * without a stream restart.
347 static void csi_idmac_eof_timeout(struct timer_list *t)
349 struct csi_priv *priv = from_timer(priv, t, eof_timeout_timer);
350 struct imx_media_video_dev *vdev = priv->vdev;
352 v4l2_err(&priv->sd, "EOF timeout\n");
354 /* signal a fatal error to capture device */
355 imx_media_capture_device_error(vdev);
358 static void csi_idmac_setup_vb2_buf(struct csi_priv *priv, dma_addr_t *phys)
360 struct imx_media_video_dev *vdev = priv->vdev;
361 struct imx_media_buffer *buf;
364 for (i = 0; i < 2; i++) {
365 buf = imx_media_capture_device_next_buf(vdev);
367 priv->active_vb2_buf[i] = buf;
368 phys[i] = vb2_dma_contig_plane_dma_addr(
369 &buf->vbuf.vb2_buf, 0);
371 priv->active_vb2_buf[i] = NULL;
372 phys[i] = priv->underrun_buf.phys;
377 static void csi_idmac_unsetup_vb2_buf(struct csi_priv *priv,
378 enum vb2_buffer_state return_status)
380 struct imx_media_buffer *buf;
383 /* return any remaining active frames with return_status */
384 for (i = 0; i < 2; i++) {
385 buf = priv->active_vb2_buf[i];
387 struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
389 vb->timestamp = ktime_get_ns();
390 vb2_buffer_done(vb, return_status);
395 /* init the SMFC IDMAC channel */
396 static int csi_idmac_setup_channel(struct csi_priv *priv)
398 struct imx_media_video_dev *vdev = priv->vdev;
399 const struct imx_media_pixfmt *incc;
400 struct v4l2_mbus_framefmt *infmt;
401 struct ipu_image image;
402 u32 passthrough_bits;
403 u32 passthrough_cycles;
409 infmt = &priv->format_mbus[CSI_SINK_PAD];
410 incc = priv->cc[CSI_SINK_PAD];
412 ipu_cpmem_zero(priv->idmac_ch);
414 memset(&image, 0, sizeof(image));
415 image.pix = vdev->fmt.fmt.pix;
416 image.rect.width = image.pix.width;
417 image.rect.height = image.pix.height;
419 csi_idmac_setup_vb2_buf(priv, phys);
421 image.phys0 = phys[0];
422 image.phys1 = phys[1];
424 passthrough = requires_passthrough(&priv->upstream_ep, infmt, incc);
425 passthrough_cycles = 1;
427 switch (image.pix.pixelformat) {
428 case V4L2_PIX_FMT_SBGGR8:
429 case V4L2_PIX_FMT_SGBRG8:
430 case V4L2_PIX_FMT_SGRBG8:
431 case V4L2_PIX_FMT_SRGGB8:
432 case V4L2_PIX_FMT_GREY:
434 passthrough_bits = 8;
436 case V4L2_PIX_FMT_SBGGR16:
437 case V4L2_PIX_FMT_SGBRG16:
438 case V4L2_PIX_FMT_SGRBG16:
439 case V4L2_PIX_FMT_SRGGB16:
440 case V4L2_PIX_FMT_Y16:
442 passthrough_bits = 16;
444 case V4L2_PIX_FMT_YUV420:
445 case V4L2_PIX_FMT_NV12:
446 burst_size = (image.pix.width & 0x3f) ?
447 ((image.pix.width & 0x1f) ?
448 ((image.pix.width & 0xf) ? 8 : 16) : 32) : 64;
449 passthrough_bits = 16;
450 /* Skip writing U and V components to odd rows */
451 ipu_cpmem_skip_odd_chroma_rows(priv->idmac_ch);
453 case V4L2_PIX_FMT_YUYV:
454 case V4L2_PIX_FMT_UYVY:
455 burst_size = (image.pix.width & 0x1f) ?
456 ((image.pix.width & 0xf) ? 8 : 16) : 32;
457 passthrough_bits = 16;
459 case V4L2_PIX_FMT_RGB565:
462 passthrough_bits = 8;
463 passthrough_cycles = incc->cycles;
466 /* fallthrough - non-passthrough RGB565 (CSI-2 bus) */
468 burst_size = (image.pix.width & 0xf) ? 8 : 16;
469 passthrough_bits = 16;
474 ipu_cpmem_set_resolution(priv->idmac_ch,
475 image.rect.width * passthrough_cycles,
477 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
478 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
479 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
480 ipu_cpmem_set_format_passthrough(priv->idmac_ch,
483 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
488 ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
491 * Set the channel for the direct CSI-->memory via SMFC
492 * use-case to very high priority, by enabling the watermark
493 * signal in the SMFC, enabling WM in the channel, and setting
494 * the channel priority to high.
496 * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority
499 * The WM's are set very low by intention here to ensure that
500 * the SMFC FIFOs do not overflow.
502 ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
503 ipu_cpmem_set_high_priority(priv->idmac_ch);
504 ipu_idmac_enable_watermark(priv->idmac_ch, true);
505 ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
507 burst_size = passthrough ?
508 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
510 ipu_smfc_set_burstsize(priv->smfc, burst_size);
512 if (image.pix.field == V4L2_FIELD_NONE &&
513 V4L2_FIELD_HAS_BOTH(infmt->field))
514 ipu_cpmem_interlaced_scan(priv->idmac_ch,
515 image.pix.bytesperline);
517 ipu_idmac_set_double_buffer(priv->idmac_ch, true);
522 csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
526 static void csi_idmac_unsetup(struct csi_priv *priv,
527 enum vb2_buffer_state state)
529 ipu_idmac_disable_channel(priv->idmac_ch);
530 ipu_smfc_disable(priv->smfc);
532 csi_idmac_unsetup_vb2_buf(priv, state);
535 static int csi_idmac_setup(struct csi_priv *priv)
539 ret = csi_idmac_setup_channel(priv);
543 ipu_cpmem_dump(priv->idmac_ch);
546 ipu_smfc_enable(priv->smfc);
548 /* set buffers ready */
549 ipu_idmac_select_buffer(priv->idmac_ch, 0);
550 ipu_idmac_select_buffer(priv->idmac_ch, 1);
552 /* enable the channels */
553 ipu_idmac_enable_channel(priv->idmac_ch);
558 static int csi_idmac_start(struct csi_priv *priv)
560 struct imx_media_video_dev *vdev = priv->vdev;
561 struct v4l2_pix_format *outfmt;
564 ret = csi_idmac_get_ipu_resources(priv);
568 ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
570 outfmt = &vdev->fmt.fmt.pix;
572 ret = imx_media_alloc_dma_buf(priv->md, &priv->underrun_buf,
577 priv->ipu_buf_num = 0;
579 /* init EOF completion waitq */
580 init_completion(&priv->last_eof_comp);
581 priv->frame_sequence = 0;
582 priv->last_eof = false;
583 priv->nfb4eof = false;
585 ret = csi_idmac_setup(priv);
587 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
588 goto out_free_dma_buf;
591 priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
594 ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
595 csi_idmac_nfb4eof_interrupt, 0,
596 "imx-smfc-nfb4eof", priv);
599 "Error registering NFB4EOF irq: %d\n", ret);
603 priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
606 ret = devm_request_irq(priv->dev, priv->eof_irq,
607 csi_idmac_eof_interrupt, 0,
608 "imx-smfc-eof", priv);
611 "Error registering eof irq: %d\n", ret);
612 goto out_free_nfb4eof_irq;
615 /* start the EOF timeout timer */
616 mod_timer(&priv->eof_timeout_timer,
617 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
621 out_free_nfb4eof_irq:
622 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
624 csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
626 imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
628 csi_idmac_put_ipu_resources(priv);
632 static void csi_idmac_stop(struct csi_priv *priv)
637 /* mark next EOF interrupt as the last before stream off */
638 spin_lock_irqsave(&priv->irqlock, flags);
639 priv->last_eof = true;
640 spin_unlock_irqrestore(&priv->irqlock, flags);
643 * and then wait for interrupt handler to mark completion.
645 ret = wait_for_completion_timeout(
646 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
648 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
650 devm_free_irq(priv->dev, priv->eof_irq, priv);
651 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
653 csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
655 imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
657 /* cancel the EOF timeout timer */
658 del_timer_sync(&priv->eof_timeout_timer);
660 csi_idmac_put_ipu_resources(priv);
663 /* Update the CSI whole sensor and active windows */
664 static int csi_setup(struct csi_priv *priv)
666 struct v4l2_mbus_framefmt *infmt, *outfmt;
667 const struct imx_media_pixfmt *incc;
668 struct v4l2_mbus_config mbus_cfg;
669 struct v4l2_mbus_framefmt if_fmt;
670 struct v4l2_rect crop;
672 infmt = &priv->format_mbus[CSI_SINK_PAD];
673 incc = priv->cc[CSI_SINK_PAD];
674 outfmt = &priv->format_mbus[priv->active_output_pad];
676 /* compose mbus_config from the upstream endpoint */
677 mbus_cfg.type = priv->upstream_ep.bus_type;
678 mbus_cfg.flags = is_parallel_bus(&priv->upstream_ep) ?
679 priv->upstream_ep.bus.parallel.flags :
680 priv->upstream_ep.bus.mipi_csi2.flags;
683 * we need to pass input frame to CSI interface, but
684 * with translated field type from output format
687 if_fmt.field = outfmt->field;
691 * if cycles is set, we need to handle this over multiple cycles as
694 if (is_parallel_bus(&priv->upstream_ep) && incc->cycles) {
695 if_fmt.width *= incc->cycles;
696 crop.width *= incc->cycles;
699 ipu_csi_set_window(priv->csi, &crop);
701 ipu_csi_set_downsize(priv->csi,
702 priv->crop.width == 2 * priv->compose.width,
703 priv->crop.height == 2 * priv->compose.height);
705 ipu_csi_init_interface(priv->csi, &mbus_cfg, &if_fmt);
707 ipu_csi_set_dest(priv->csi, priv->dest);
709 if (priv->dest == IPU_CSI_DEST_IDMAC)
710 ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc,
711 priv->skip->max_ratio - 1, 0);
713 ipu_csi_dump(priv->csi);
718 static int csi_start(struct csi_priv *priv)
720 struct v4l2_fract *output_fi;
723 output_fi = &priv->frame_interval[priv->active_output_pad];
725 if (priv->dest == IPU_CSI_DEST_IDMAC) {
726 ret = csi_idmac_start(priv);
731 ret = csi_setup(priv);
735 /* start the frame interval monitor */
736 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) {
737 ret = imx_media_fim_set_stream(priv->fim, output_fi, true);
742 ret = ipu_csi_enable(priv->csi);
744 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
751 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
752 imx_media_fim_set_stream(priv->fim, NULL, false);
754 if (priv->dest == IPU_CSI_DEST_IDMAC)
755 csi_idmac_stop(priv);
759 static void csi_stop(struct csi_priv *priv)
761 if (priv->dest == IPU_CSI_DEST_IDMAC) {
762 csi_idmac_stop(priv);
764 /* stop the frame interval monitor */
766 imx_media_fim_set_stream(priv->fim, NULL, false);
769 ipu_csi_disable(priv->csi);
772 static const struct csi_skip_desc csi_skip[12] = {
773 { 1, 1, 0x00 }, /* Keep all frames */
774 { 5, 6, 0x10 }, /* Skip every sixth frame */
775 { 4, 5, 0x08 }, /* Skip every fifth frame */
776 { 3, 4, 0x04 }, /* Skip every fourth frame */
777 { 2, 3, 0x02 }, /* Skip every third frame */
778 { 3, 5, 0x0a }, /* Skip frames 1 and 3 of every 5 */
779 { 1, 2, 0x01 }, /* Skip every second frame */
780 { 2, 5, 0x0b }, /* Keep frames 1 and 4 of every 5 */
781 { 1, 3, 0x03 }, /* Keep one in three frames */
782 { 1, 4, 0x07 }, /* Keep one in four frames */
783 { 1, 5, 0x0f }, /* Keep one in five frames */
784 { 1, 6, 0x1f }, /* Keep one in six frames */
787 static void csi_apply_skip_interval(const struct csi_skip_desc *skip,
788 struct v4l2_fract *interval)
792 interval->numerator *= skip->max_ratio;
793 interval->denominator *= skip->keep;
795 /* Reduce fraction to lowest terms */
796 div = gcd(interval->numerator, interval->denominator);
798 interval->numerator /= div;
799 interval->denominator /= div;
804 * Find the skip pattern to produce the output frame interval closest to the
805 * requested one, for the given input frame interval. Updates the output frame
806 * interval to the exact value.
808 static const struct csi_skip_desc *csi_find_best_skip(struct v4l2_fract *in,
809 struct v4l2_fract *out)
811 const struct csi_skip_desc *skip = &csi_skip[0], *best_skip = skip;
812 u32 min_err = UINT_MAX;
816 /* Default to 1:1 ratio */
817 if (out->numerator == 0 || out->denominator == 0 ||
818 in->numerator == 0 || in->denominator == 0) {
823 want_us = div_u64((u64)USEC_PER_SEC * out->numerator, out->denominator);
825 /* Find the reduction closest to the requested time per frame */
826 for (i = 0; i < ARRAY_SIZE(csi_skip); i++, skip++) {
829 tmp = div_u64((u64)USEC_PER_SEC * in->numerator *
830 skip->max_ratio, in->denominator * skip->keep);
832 err = abs((s64)tmp - want_us);
840 csi_apply_skip_interval(best_skip, out);
846 * V4L2 subdev operations.
849 static int csi_g_frame_interval(struct v4l2_subdev *sd,
850 struct v4l2_subdev_frame_interval *fi)
852 struct csi_priv *priv = v4l2_get_subdevdata(sd);
854 if (fi->pad >= CSI_NUM_PADS)
857 mutex_lock(&priv->lock);
859 fi->interval = priv->frame_interval[fi->pad];
861 mutex_unlock(&priv->lock);
866 static int csi_s_frame_interval(struct v4l2_subdev *sd,
867 struct v4l2_subdev_frame_interval *fi)
869 struct csi_priv *priv = v4l2_get_subdevdata(sd);
870 struct v4l2_fract *input_fi;
873 mutex_lock(&priv->lock);
875 input_fi = &priv->frame_interval[CSI_SINK_PAD];
879 /* No limits on input frame interval */
880 /* Reset output intervals and frame skipping ratio to 1:1 */
881 priv->frame_interval[CSI_SRC_PAD_IDMAC] = fi->interval;
882 priv->frame_interval[CSI_SRC_PAD_DIRECT] = fi->interval;
883 priv->skip = &csi_skip[0];
885 case CSI_SRC_PAD_IDMAC:
887 * frame interval at IDMAC output pad depends on input
888 * interval, modified by frame skipping.
890 priv->skip = csi_find_best_skip(input_fi, &fi->interval);
892 case CSI_SRC_PAD_DIRECT:
894 * frame interval at DIRECT output pad is same as input
897 fi->interval = *input_fi;
904 priv->frame_interval[fi->pad] = fi->interval;
906 mutex_unlock(&priv->lock);
910 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
912 struct csi_priv *priv = v4l2_get_subdevdata(sd);
915 mutex_lock(&priv->lock);
917 if (!priv->src_sd || !priv->sink) {
923 * enable/disable streaming only if stream_count is
924 * going from 0 to 1 / 1 to 0.
926 if (priv->stream_count != !enable)
930 /* upstream must be started first, before starting CSI */
931 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
932 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
936 dev_dbg(priv->dev, "stream ON\n");
937 ret = csi_start(priv);
939 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
943 dev_dbg(priv->dev, "stream OFF\n");
944 /* CSI must be stopped first, then stop upstream */
946 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
950 priv->stream_count += enable ? 1 : -1;
951 if (priv->stream_count < 0)
952 priv->stream_count = 0;
954 mutex_unlock(&priv->lock);
958 static int csi_link_setup(struct media_entity *entity,
959 const struct media_pad *local,
960 const struct media_pad *remote, u32 flags)
962 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
963 struct csi_priv *priv = v4l2_get_subdevdata(sd);
964 struct v4l2_subdev *remote_sd;
967 dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
968 local->entity->name);
970 mutex_lock(&priv->lock);
972 if (local->flags & MEDIA_PAD_FL_SINK) {
973 if (!is_media_entity_v4l2_subdev(remote->entity)) {
978 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
980 if (flags & MEDIA_LNK_FL_ENABLED) {
985 priv->src_sd = remote_sd;
993 /* this is a source pad */
995 if (flags & MEDIA_LNK_FL_ENABLED) {
1001 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1002 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1007 /* record which output pad is now active */
1008 priv->active_output_pad = local->index;
1010 /* set CSI destination */
1011 if (local->index == CSI_SRC_PAD_IDMAC) {
1012 if (!is_media_entity_v4l2_video_device(remote->entity)) {
1018 ret = imx_media_fim_add_controls(priv->fim);
1023 priv->dest = IPU_CSI_DEST_IDMAC;
1025 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1030 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1031 switch (remote_sd->grp_id) {
1032 case IMX_MEDIA_GRP_ID_VDIC:
1033 priv->dest = IPU_CSI_DEST_VDIC;
1035 case IMX_MEDIA_GRP_ID_IC_PRP:
1036 priv->dest = IPU_CSI_DEST_IC;
1044 priv->sink = remote->entity;
1046 mutex_unlock(&priv->lock);
1050 static int csi_link_validate(struct v4l2_subdev *sd,
1051 struct media_link *link,
1052 struct v4l2_subdev_format *source_fmt,
1053 struct v4l2_subdev_format *sink_fmt)
1055 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1056 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1060 ret = v4l2_subdev_link_validate_default(sd, link,
1061 source_fmt, sink_fmt);
1065 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1067 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1071 mutex_lock(&priv->lock);
1073 priv->upstream_ep = upstream_ep;
1074 is_csi2 = !is_parallel_bus(&upstream_ep);
1078 * NOTE! It seems the virtual channels from the mipi csi-2
1079 * receiver are used only for routing by the video mux's,
1080 * or for hard-wired routing to the CSI's. Once the stream
1081 * enters the CSI's however, they are treated internally
1082 * in the IPU as virtual channel 0.
1085 mutex_unlock(&priv->lock);
1086 vc_num = imx_media_find_mipi_csi2_channel(priv->md,
1090 mutex_lock(&priv->lock);
1092 ipu_csi_set_mipi_datatype(priv->csi, vc_num,
1093 &priv->format_mbus[CSI_SINK_PAD]);
1096 /* select either parallel or MIPI-CSI2 as input to CSI */
1097 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
1099 mutex_unlock(&priv->lock);
1103 static struct v4l2_mbus_framefmt *
1104 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1105 unsigned int pad, enum v4l2_subdev_format_whence which)
1107 if (which == V4L2_SUBDEV_FORMAT_TRY)
1108 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad);
1110 return &priv->format_mbus[pad];
1113 static struct v4l2_rect *
1114 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1115 enum v4l2_subdev_format_whence which)
1117 if (which == V4L2_SUBDEV_FORMAT_TRY)
1118 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD);
1123 static struct v4l2_rect *
1124 __csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1125 enum v4l2_subdev_format_whence which)
1127 if (which == V4L2_SUBDEV_FORMAT_TRY)
1128 return v4l2_subdev_get_try_compose(&priv->sd, cfg,
1131 return &priv->compose;
1134 static void csi_try_crop(struct csi_priv *priv,
1135 struct v4l2_rect *crop,
1136 struct v4l2_subdev_pad_config *cfg,
1137 struct v4l2_mbus_framefmt *infmt,
1138 struct v4l2_fwnode_endpoint *upstream_ep)
1140 crop->width = min_t(__u32, infmt->width, crop->width);
1141 if (crop->left + crop->width > infmt->width)
1142 crop->left = infmt->width - crop->width;
1143 /* adjust crop left/width to h/w alignment restrictions */
1145 crop->width &= ~0x7;
1148 * FIXME: not sure why yet, but on interlaced bt.656,
1149 * changing the vertical cropping causes loss of vertical
1150 * sync, so fix it to NTSC/PAL active lines. NTSC contains
1151 * 2 extra lines of active video that need to be cropped.
1153 if (upstream_ep->bus_type == V4L2_MBUS_BT656 &&
1154 (V4L2_FIELD_HAS_BOTH(infmt->field) ||
1155 infmt->field == V4L2_FIELD_ALTERNATE)) {
1156 crop->height = infmt->height;
1157 crop->top = (infmt->height == 480) ? 2 : 0;
1159 crop->height = min_t(__u32, infmt->height, crop->height);
1160 if (crop->top + crop->height > infmt->height)
1161 crop->top = infmt->height - crop->height;
1165 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
1166 struct v4l2_subdev_pad_config *cfg,
1167 struct v4l2_subdev_mbus_code_enum *code)
1169 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1170 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1171 const struct imx_media_pixfmt *incc;
1172 struct v4l2_mbus_framefmt *infmt;
1175 mutex_lock(&priv->lock);
1177 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which);
1178 incc = imx_media_find_mbus_format(infmt->code, CS_SEL_ANY, true);
1180 switch (code->pad) {
1182 ret = imx_media_enum_mbus_format(&code->code, code->index,
1185 case CSI_SRC_PAD_DIRECT:
1186 case CSI_SRC_PAD_IDMAC:
1187 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1189 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1193 if (requires_passthrough(&upstream_ep, infmt, incc)) {
1194 if (code->index != 0) {
1198 code->code = infmt->code;
1200 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1201 CS_SEL_YUV : CS_SEL_RGB;
1202 ret = imx_media_enum_ipu_format(&code->code,
1212 mutex_unlock(&priv->lock);
1216 static int csi_enum_frame_size(struct v4l2_subdev *sd,
1217 struct v4l2_subdev_pad_config *cfg,
1218 struct v4l2_subdev_frame_size_enum *fse)
1220 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1221 struct v4l2_rect *crop;
1224 if (fse->pad >= CSI_NUM_PADS ||
1225 fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3))
1228 mutex_lock(&priv->lock);
1230 if (fse->pad == CSI_SINK_PAD) {
1231 fse->min_width = MIN_W;
1232 fse->max_width = MAX_W;
1233 fse->min_height = MIN_H;
1234 fse->max_height = MAX_H;
1236 crop = __csi_get_crop(priv, cfg, fse->which);
1238 fse->min_width = fse->index & 1 ?
1239 crop->width / 2 : crop->width;
1240 fse->max_width = fse->min_width;
1241 fse->min_height = fse->index & 2 ?
1242 crop->height / 2 : crop->height;
1243 fse->max_height = fse->min_height;
1246 mutex_unlock(&priv->lock);
1250 static int csi_enum_frame_interval(struct v4l2_subdev *sd,
1251 struct v4l2_subdev_pad_config *cfg,
1252 struct v4l2_subdev_frame_interval_enum *fie)
1254 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1255 struct v4l2_fract *input_fi;
1256 struct v4l2_rect *crop;
1259 if (fie->pad >= CSI_NUM_PADS ||
1260 fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ?
1261 1 : ARRAY_SIZE(csi_skip)))
1264 mutex_lock(&priv->lock);
1266 input_fi = &priv->frame_interval[CSI_SINK_PAD];
1267 crop = __csi_get_crop(priv, cfg, fie->which);
1269 if ((fie->width != crop->width && fie->width != crop->width / 2) ||
1270 (fie->height != crop->height && fie->height != crop->height / 2)) {
1275 fie->interval = *input_fi;
1277 if (fie->pad == CSI_SRC_PAD_IDMAC)
1278 csi_apply_skip_interval(&csi_skip[fie->index],
1282 mutex_unlock(&priv->lock);
1286 static int csi_get_fmt(struct v4l2_subdev *sd,
1287 struct v4l2_subdev_pad_config *cfg,
1288 struct v4l2_subdev_format *sdformat)
1290 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1291 struct v4l2_mbus_framefmt *fmt;
1294 if (sdformat->pad >= CSI_NUM_PADS)
1297 mutex_lock(&priv->lock);
1299 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1305 sdformat->format = *fmt;
1307 mutex_unlock(&priv->lock);
1311 static void csi_try_fmt(struct csi_priv *priv,
1312 struct v4l2_fwnode_endpoint *upstream_ep,
1313 struct v4l2_subdev_pad_config *cfg,
1314 struct v4l2_subdev_format *sdformat,
1315 struct v4l2_rect *crop,
1316 struct v4l2_rect *compose,
1317 const struct imx_media_pixfmt **cc)
1319 const struct imx_media_pixfmt *incc;
1320 struct v4l2_mbus_framefmt *infmt;
1323 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
1325 switch (sdformat->pad) {
1326 case CSI_SRC_PAD_DIRECT:
1327 case CSI_SRC_PAD_IDMAC:
1328 incc = imx_media_find_mbus_format(infmt->code,
1331 sdformat->format.width = compose->width;
1332 sdformat->format.height = compose->height;
1334 if (requires_passthrough(upstream_ep, infmt, incc)) {
1335 sdformat->format.code = infmt->code;
1338 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1339 CS_SEL_YUV : CS_SEL_RGB;
1341 *cc = imx_media_find_ipu_format(sdformat->format.code,
1344 imx_media_enum_ipu_format(&code, 0, cs_sel);
1345 *cc = imx_media_find_ipu_format(code, cs_sel);
1346 sdformat->format.code = (*cc)->codes[0];
1350 if (sdformat->pad == CSI_SRC_PAD_DIRECT ||
1351 sdformat->format.field != V4L2_FIELD_NONE)
1352 sdformat->format.field = infmt->field;
1355 * translate V4L2_FIELD_ALTERNATE to SEQ_TB or SEQ_BT
1356 * depending on input height (assume NTSC top-bottom
1357 * order if 480 lines, otherwise PAL bottom-top order).
1359 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) {
1360 sdformat->format.field = (infmt->height == 480) ?
1361 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1364 /* propagate colorimetry from sink */
1365 sdformat->format.colorspace = infmt->colorspace;
1366 sdformat->format.xfer_func = infmt->xfer_func;
1367 sdformat->format.quantization = infmt->quantization;
1368 sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
1371 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1372 W_ALIGN, &sdformat->format.height,
1373 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1375 /* Reset crop and compose rectangles */
1378 crop->width = sdformat->format.width;
1379 crop->height = sdformat->format.height;
1380 csi_try_crop(priv, crop, cfg, &sdformat->format, upstream_ep);
1383 compose->width = crop->width;
1384 compose->height = crop->height;
1386 *cc = imx_media_find_mbus_format(sdformat->format.code,
1389 imx_media_enum_mbus_format(&code, 0,
1391 *cc = imx_media_find_mbus_format(code,
1393 sdformat->format.code = (*cc)->codes[0];
1396 imx_media_fill_default_mbus_fields(
1397 &sdformat->format, infmt,
1398 priv->active_output_pad == CSI_SRC_PAD_DIRECT);
1403 static int csi_set_fmt(struct v4l2_subdev *sd,
1404 struct v4l2_subdev_pad_config *cfg,
1405 struct v4l2_subdev_format *sdformat)
1407 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1408 struct imx_media_video_dev *vdev = priv->vdev;
1409 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1410 const struct imx_media_pixfmt *cc;
1411 struct v4l2_pix_format vdev_fmt;
1412 struct v4l2_mbus_framefmt *fmt;
1413 struct v4l2_rect *crop, *compose;
1416 if (sdformat->pad >= CSI_NUM_PADS)
1419 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1421 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1425 mutex_lock(&priv->lock);
1427 if (priv->stream_count > 0) {
1432 crop = __csi_get_crop(priv, cfg, sdformat->which);
1433 compose = __csi_get_compose(priv, cfg, sdformat->which);
1435 csi_try_fmt(priv, &upstream_ep, cfg, sdformat, crop, compose, &cc);
1437 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1438 *fmt = sdformat->format;
1440 if (sdformat->pad == CSI_SINK_PAD) {
1443 /* propagate format to source pads */
1444 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1445 const struct imx_media_pixfmt *outcc;
1446 struct v4l2_mbus_framefmt *outfmt;
1447 struct v4l2_subdev_format format;
1450 format.which = sdformat->which;
1451 format.format = sdformat->format;
1452 csi_try_fmt(priv, &upstream_ep, cfg, &format,
1453 NULL, compose, &outcc);
1455 outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which);
1456 *outfmt = format.format;
1458 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1459 priv->cc[pad] = outcc;
1463 if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY)
1466 priv->cc[sdformat->pad] = cc;
1468 /* propagate IDMAC output pad format to capture device */
1469 imx_media_mbus_fmt_to_pix_fmt(&vdev_fmt,
1470 &priv->format_mbus[CSI_SRC_PAD_IDMAC],
1471 priv->cc[CSI_SRC_PAD_IDMAC]);
1472 mutex_unlock(&priv->lock);
1473 imx_media_capture_device_set_format(vdev, &vdev_fmt);
1477 mutex_unlock(&priv->lock);
1481 static int csi_get_selection(struct v4l2_subdev *sd,
1482 struct v4l2_subdev_pad_config *cfg,
1483 struct v4l2_subdev_selection *sel)
1485 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1486 struct v4l2_mbus_framefmt *infmt;
1487 struct v4l2_rect *crop, *compose;
1490 if (sel->pad != CSI_SINK_PAD)
1493 mutex_lock(&priv->lock);
1495 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1496 crop = __csi_get_crop(priv, cfg, sel->which);
1497 compose = __csi_get_compose(priv, cfg, sel->which);
1499 switch (sel->target) {
1500 case V4L2_SEL_TGT_CROP_BOUNDS:
1503 sel->r.width = infmt->width;
1504 sel->r.height = infmt->height;
1506 case V4L2_SEL_TGT_CROP:
1509 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1512 sel->r.width = crop->width;
1513 sel->r.height = crop->height;
1515 case V4L2_SEL_TGT_COMPOSE:
1522 mutex_unlock(&priv->lock);
1526 static int csi_set_scale(u32 *compose, u32 crop, u32 flags)
1528 if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) ==
1529 (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) &&
1530 *compose != crop && *compose != crop / 2)
1533 if (*compose <= crop / 2 ||
1534 (*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) ||
1535 (*compose < crop && (flags & V4L2_SEL_FLAG_LE)))
1536 *compose = crop / 2;
1543 static int csi_set_selection(struct v4l2_subdev *sd,
1544 struct v4l2_subdev_pad_config *cfg,
1545 struct v4l2_subdev_selection *sel)
1547 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1548 struct v4l2_fwnode_endpoint upstream_ep = { .bus_type = 0 };
1549 struct v4l2_mbus_framefmt *infmt;
1550 struct v4l2_rect *crop, *compose;
1553 if (sel->pad != CSI_SINK_PAD)
1556 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1558 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1562 mutex_lock(&priv->lock);
1564 if (priv->stream_count > 0) {
1569 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1570 crop = __csi_get_crop(priv, cfg, sel->which);
1571 compose = __csi_get_compose(priv, cfg, sel->which);
1573 switch (sel->target) {
1574 case V4L2_SEL_TGT_CROP:
1576 * Modifying the crop rectangle always changes the format on
1577 * the source pads. If the KEEP_CONFIG flag is set, just return
1578 * the current crop rectangle.
1580 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1581 sel->r = priv->crop;
1582 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1587 csi_try_crop(priv, &sel->r, cfg, infmt, &upstream_ep);
1591 /* Reset scaling to 1:1 */
1592 compose->width = crop->width;
1593 compose->height = crop->height;
1595 case V4L2_SEL_TGT_COMPOSE:
1597 * Modifying the compose rectangle always changes the format on
1598 * the source pads. If the KEEP_CONFIG flag is set, just return
1599 * the current compose rectangle.
1601 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1602 sel->r = priv->compose;
1603 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1610 ret = csi_set_scale(&sel->r.width, crop->width, sel->flags);
1613 ret = csi_set_scale(&sel->r.height, crop->height, sel->flags);
1624 /* Reset source pads to sink compose rectangle */
1625 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1626 struct v4l2_mbus_framefmt *outfmt;
1628 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which);
1629 outfmt->width = compose->width;
1630 outfmt->height = compose->height;
1634 mutex_unlock(&priv->lock);
1638 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1639 struct v4l2_event_subscription *sub)
1641 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1646 return v4l2_event_subscribe(fh, sub, 0, NULL);
1649 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1650 struct v4l2_event_subscription *sub)
1652 return v4l2_event_unsubscribe(fh, sub);
1656 * retrieve our pads parsed from the OF graph by the media device
1658 static int csi_registered(struct v4l2_subdev *sd)
1660 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1661 struct ipu_csi *csi;
1665 /* get media device */
1666 priv->md = dev_get_drvdata(sd->v4l2_dev->dev);
1668 /* get handle to IPU CSI */
1669 csi = ipu_csi_get(priv->ipu, priv->csi_id);
1671 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1672 return PTR_ERR(csi);
1676 for (i = 0; i < CSI_NUM_PADS; i++) {
1677 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
1678 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1681 if (i != CSI_SINK_PAD)
1682 imx_media_enum_ipu_format(&code, 0, CS_SEL_YUV);
1684 /* set a default mbus format */
1685 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1686 640, 480, code, V4L2_FIELD_NONE,
1691 /* init default frame interval */
1692 priv->frame_interval[i].numerator = 1;
1693 priv->frame_interval[i].denominator = 30;
1696 /* disable frame skipping */
1697 priv->skip = &csi_skip[0];
1699 /* init default crop and compose rectangle sizes */
1700 priv->crop.width = 640;
1701 priv->crop.height = 480;
1702 priv->compose.width = 640;
1703 priv->compose.height = 480;
1705 priv->fim = imx_media_fim_init(&priv->sd);
1706 if (IS_ERR(priv->fim)) {
1707 ret = PTR_ERR(priv->fim);
1711 ret = media_entity_pads_init(&sd->entity, CSI_NUM_PADS, priv->pad);
1715 ret = imx_media_capture_device_register(priv->vdev);
1719 ret = imx_media_add_video_device(priv->md, priv->vdev);
1725 imx_media_capture_device_unregister(priv->vdev);
1728 imx_media_fim_free(priv->fim);
1730 ipu_csi_put(priv->csi);
1734 static void csi_unregistered(struct v4l2_subdev *sd)
1736 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1738 imx_media_capture_device_unregister(priv->vdev);
1741 imx_media_fim_free(priv->fim);
1744 ipu_csi_put(priv->csi);
1747 static const struct media_entity_operations csi_entity_ops = {
1748 .link_setup = csi_link_setup,
1749 .link_validate = v4l2_subdev_link_validate,
1752 static const struct v4l2_subdev_core_ops csi_core_ops = {
1753 .subscribe_event = csi_subscribe_event,
1754 .unsubscribe_event = csi_unsubscribe_event,
1757 static const struct v4l2_subdev_video_ops csi_video_ops = {
1758 .g_frame_interval = csi_g_frame_interval,
1759 .s_frame_interval = csi_s_frame_interval,
1760 .s_stream = csi_s_stream,
1763 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1764 .init_cfg = imx_media_init_cfg,
1765 .enum_mbus_code = csi_enum_mbus_code,
1766 .enum_frame_size = csi_enum_frame_size,
1767 .enum_frame_interval = csi_enum_frame_interval,
1768 .get_fmt = csi_get_fmt,
1769 .set_fmt = csi_set_fmt,
1770 .get_selection = csi_get_selection,
1771 .set_selection = csi_set_selection,
1772 .link_validate = csi_link_validate,
1775 static const struct v4l2_subdev_ops csi_subdev_ops = {
1776 .core = &csi_core_ops,
1777 .video = &csi_video_ops,
1778 .pad = &csi_pad_ops,
1781 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1782 .registered = csi_registered,
1783 .unregistered = csi_unregistered,
1786 static int imx_csi_parse_endpoint(struct device *dev,
1787 struct v4l2_fwnode_endpoint *vep,
1788 struct v4l2_async_subdev *asd)
1790 return fwnode_device_is_available(asd->match.fwnode) ? 0 : -EINVAL;
1793 static int imx_csi_async_register(struct csi_priv *priv)
1795 struct v4l2_async_notifier *notifier;
1796 struct fwnode_handle *fwnode;
1800 notifier = kzalloc(sizeof(*notifier), GFP_KERNEL);
1804 v4l2_async_notifier_init(notifier);
1806 fwnode = dev_fwnode(priv->dev);
1808 /* get this CSI's port id */
1809 ret = fwnode_property_read_u32(fwnode, "reg", &port);
1813 ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
1814 priv->dev->parent, notifier, sizeof(struct v4l2_async_subdev),
1815 port, imx_csi_parse_endpoint);
1819 ret = v4l2_async_subdev_notifier_register(&priv->sd, notifier);
1823 ret = v4l2_async_register_subdev(&priv->sd);
1825 goto out_unregister;
1827 priv->sd.subdev_notifier = notifier;
1832 v4l2_async_notifier_unregister(notifier);
1834 v4l2_async_notifier_cleanup(notifier);
1841 static int imx_csi_probe(struct platform_device *pdev)
1843 struct ipu_client_platformdata *pdata;
1844 struct pinctrl *pinctrl;
1845 struct csi_priv *priv;
1848 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1852 platform_set_drvdata(pdev, &priv->sd);
1853 priv->dev = &pdev->dev;
1855 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1859 /* get parent IPU */
1860 priv->ipu = dev_get_drvdata(priv->dev->parent);
1862 /* get our CSI id */
1863 pdata = priv->dev->platform_data;
1864 priv->csi_id = pdata->csi;
1865 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1867 timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0);
1868 spin_lock_init(&priv->irqlock);
1870 v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1871 v4l2_set_subdevdata(&priv->sd, priv);
1872 priv->sd.internal_ops = &csi_internal_ops;
1873 priv->sd.entity.ops = &csi_entity_ops;
1874 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1875 priv->sd.dev = &pdev->dev;
1876 priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
1877 priv->sd.owner = THIS_MODULE;
1878 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1879 priv->sd.grp_id = priv->csi_id ?
1880 IMX_MEDIA_GRP_ID_CSI1 : IMX_MEDIA_GRP_ID_CSI0;
1881 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
1882 priv->sd.grp_id, ipu_get_num(priv->ipu));
1884 priv->vdev = imx_media_capture_device_init(&priv->sd,
1886 if (IS_ERR(priv->vdev))
1887 return PTR_ERR(priv->vdev);
1889 mutex_init(&priv->lock);
1891 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1892 priv->sd.ctrl_handler = &priv->ctrl_hdlr;
1895 * The IPUv3 driver did not assign an of_node to this
1896 * device. As a result, pinctrl does not automatically
1897 * configure our pin groups, so we need to do that manually
1898 * here, after setting this device's of_node.
1900 priv->dev->of_node = pdata->of_node;
1901 pinctrl = devm_pinctrl_get_select_default(priv->dev);
1902 if (IS_ERR(pinctrl)) {
1903 ret = PTR_ERR(pinctrl);
1905 "devm_pinctrl_get_select_default() failed: %d\n", ret);
1910 ret = imx_csi_async_register(priv);
1916 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1917 mutex_destroy(&priv->lock);
1918 imx_media_capture_device_remove(priv->vdev);
1922 static int imx_csi_remove(struct platform_device *pdev)
1924 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1925 struct csi_priv *priv = sd_to_dev(sd);
1927 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1928 mutex_destroy(&priv->lock);
1929 imx_media_capture_device_remove(priv->vdev);
1930 v4l2_async_unregister_subdev(sd);
1931 media_entity_cleanup(&sd->entity);
1936 static const struct platform_device_id imx_csi_ids[] = {
1937 { .name = "imx-ipuv3-csi" },
1940 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
1942 static struct platform_driver imx_csi_driver = {
1943 .probe = imx_csi_probe,
1944 .remove = imx_csi_remove,
1945 .id_table = imx_csi_ids,
1947 .name = "imx-ipuv3-csi",
1950 module_platform_driver(imx_csi_driver);
1952 MODULE_DESCRIPTION("i.MX CSI subdev driver");
1953 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
1954 MODULE_LICENSE("GPL");
1955 MODULE_ALIAS("platform:imx-ipuv3-csi");