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;
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, directly upstream from the
157 * video mux, or directly upstream from the CSI itself. The endpoint
158 * is needed to determine the bus type and bus config coming into
161 static int csi_get_upstream_endpoint(struct csi_priv *priv,
162 struct v4l2_fwnode_endpoint *ep)
164 struct device_node *endpoint, *port;
165 struct media_entity *src;
166 struct v4l2_subdev *sd;
167 struct media_pad *pad;
169 if (!IS_ENABLED(CONFIG_OF))
178 if (src->function == MEDIA_ENT_F_VID_MUX) {
180 * CSI is connected directly to video mux, skip up to
181 * CSI-2 receiver if it is in the path, otherwise stay
184 sd = imx_media_find_upstream_subdev(priv->md, src,
185 IMX_MEDIA_GRP_ID_CSI2);
191 * If the source is neither the video mux nor the CSI-2 receiver,
192 * get the source pad directly upstream from CSI itself.
194 if (src->function != MEDIA_ENT_F_VID_MUX &&
195 sd->grp_id != IMX_MEDIA_GRP_ID_CSI2)
196 src = &priv->sd.entity;
198 /* get source pad of entity directly upstream from src */
199 pad = imx_media_find_upstream_pad(priv->md, src, 0);
203 sd = media_entity_to_v4l2_subdev(pad->entity);
206 * NOTE: this assumes an OF-graph port id is the same as a
209 port = of_graph_get_port_by_id(sd->dev->of_node, pad->index);
213 endpoint = of_get_next_child(port, NULL);
218 v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint), ep);
219 of_node_put(endpoint);
224 static void csi_idmac_put_ipu_resources(struct csi_priv *priv)
227 ipu_idmac_put(priv->idmac_ch);
228 priv->idmac_ch = NULL;
231 ipu_smfc_put(priv->smfc);
235 static int csi_idmac_get_ipu_resources(struct csi_priv *priv)
238 struct ipu_smfc *smfc;
239 struct ipuv3_channel *idmac_ch;
241 ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id;
243 smfc = ipu_smfc_get(priv->ipu, ch_num);
245 v4l2_err(&priv->sd, "failed to get SMFC\n");
251 idmac_ch = ipu_idmac_get(priv->ipu, ch_num);
252 if (IS_ERR(idmac_ch)) {
253 v4l2_err(&priv->sd, "could not get IDMAC channel %u\n",
255 ret = PTR_ERR(idmac_ch);
258 priv->idmac_ch = idmac_ch;
262 csi_idmac_put_ipu_resources(priv);
266 static void csi_vb2_buf_done(struct csi_priv *priv)
268 struct imx_media_video_dev *vdev = priv->vdev;
269 struct imx_media_buffer *done, *next;
270 struct vb2_buffer *vb;
273 done = priv->active_vb2_buf[priv->ipu_buf_num];
275 done->vbuf.field = vdev->fmt.fmt.pix.field;
276 done->vbuf.sequence = priv->frame_sequence;
277 vb = &done->vbuf.vb2_buf;
278 vb->timestamp = ktime_get_ns();
279 vb2_buffer_done(vb, priv->nfb4eof ?
280 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
283 priv->frame_sequence++;
284 priv->nfb4eof = false;
286 /* get next queued buffer */
287 next = imx_media_capture_device_next_buf(vdev);
289 phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0);
290 priv->active_vb2_buf[priv->ipu_buf_num] = next;
292 phys = priv->underrun_buf.phys;
293 priv->active_vb2_buf[priv->ipu_buf_num] = NULL;
296 if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num))
297 ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num);
299 ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, phys);
302 static irqreturn_t csi_idmac_eof_interrupt(int irq, void *dev_id)
304 struct csi_priv *priv = dev_id;
306 spin_lock(&priv->irqlock);
308 if (priv->last_eof) {
309 complete(&priv->last_eof_comp);
310 priv->last_eof = false;
315 /* call frame interval monitor */
316 imx_media_fim_eof_monitor(priv->fim, ktime_get());
318 csi_vb2_buf_done(priv);
320 /* select new IPU buf */
321 ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num);
322 /* toggle IPU double-buffer index */
323 priv->ipu_buf_num ^= 1;
325 /* bump the EOF timeout timer */
326 mod_timer(&priv->eof_timeout_timer,
327 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
330 spin_unlock(&priv->irqlock);
334 static irqreturn_t csi_idmac_nfb4eof_interrupt(int irq, void *dev_id)
336 struct csi_priv *priv = dev_id;
338 spin_lock(&priv->irqlock);
341 * this is not an unrecoverable error, just mark
342 * the next captured frame with vb2 error flag.
344 priv->nfb4eof = true;
346 v4l2_err(&priv->sd, "NFB4EOF\n");
348 spin_unlock(&priv->irqlock);
354 * EOF timeout timer function. This is an unrecoverable condition
355 * without a stream restart.
357 static void csi_idmac_eof_timeout(struct timer_list *t)
359 struct csi_priv *priv = from_timer(priv, t, eof_timeout_timer);
360 struct imx_media_video_dev *vdev = priv->vdev;
362 v4l2_err(&priv->sd, "EOF timeout\n");
364 /* signal a fatal error to capture device */
365 imx_media_capture_device_error(vdev);
368 static void csi_idmac_setup_vb2_buf(struct csi_priv *priv, dma_addr_t *phys)
370 struct imx_media_video_dev *vdev = priv->vdev;
371 struct imx_media_buffer *buf;
374 for (i = 0; i < 2; i++) {
375 buf = imx_media_capture_device_next_buf(vdev);
377 priv->active_vb2_buf[i] = buf;
378 phys[i] = vb2_dma_contig_plane_dma_addr(
379 &buf->vbuf.vb2_buf, 0);
381 priv->active_vb2_buf[i] = NULL;
382 phys[i] = priv->underrun_buf.phys;
387 static void csi_idmac_unsetup_vb2_buf(struct csi_priv *priv,
388 enum vb2_buffer_state return_status)
390 struct imx_media_buffer *buf;
393 /* return any remaining active frames with return_status */
394 for (i = 0; i < 2; i++) {
395 buf = priv->active_vb2_buf[i];
397 struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
399 vb->timestamp = ktime_get_ns();
400 vb2_buffer_done(vb, return_status);
405 /* init the SMFC IDMAC channel */
406 static int csi_idmac_setup_channel(struct csi_priv *priv)
408 struct imx_media_video_dev *vdev = priv->vdev;
409 const struct imx_media_pixfmt *incc;
410 struct v4l2_mbus_framefmt *infmt;
411 struct ipu_image image;
412 u32 passthrough_bits;
413 u32 passthrough_cycles;
419 infmt = &priv->format_mbus[CSI_SINK_PAD];
420 incc = priv->cc[CSI_SINK_PAD];
422 ipu_cpmem_zero(priv->idmac_ch);
424 memset(&image, 0, sizeof(image));
425 image.pix = vdev->fmt.fmt.pix;
426 image.rect.width = image.pix.width;
427 image.rect.height = image.pix.height;
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;
437 switch (image.pix.pixelformat) {
438 case V4L2_PIX_FMT_SBGGR8:
439 case V4L2_PIX_FMT_SGBRG8:
440 case V4L2_PIX_FMT_SGRBG8:
441 case V4L2_PIX_FMT_SRGGB8:
442 case V4L2_PIX_FMT_GREY:
444 passthrough_bits = 8;
446 case V4L2_PIX_FMT_SBGGR16:
447 case V4L2_PIX_FMT_SGBRG16:
448 case V4L2_PIX_FMT_SGRBG16:
449 case V4L2_PIX_FMT_SRGGB16:
450 case V4L2_PIX_FMT_Y16:
452 passthrough_bits = 16;
454 case V4L2_PIX_FMT_YUV420:
455 case V4L2_PIX_FMT_NV12:
456 burst_size = (image.pix.width & 0x3f) ?
457 ((image.pix.width & 0x1f) ?
458 ((image.pix.width & 0xf) ? 8 : 16) : 32) : 64;
459 passthrough_bits = 16;
460 /* Skip writing U and V components to odd rows */
461 ipu_cpmem_skip_odd_chroma_rows(priv->idmac_ch);
463 case V4L2_PIX_FMT_YUYV:
464 case V4L2_PIX_FMT_UYVY:
465 burst_size = (image.pix.width & 0x1f) ?
466 ((image.pix.width & 0xf) ? 8 : 16) : 32;
467 passthrough_bits = 16;
469 case V4L2_PIX_FMT_RGB565:
472 passthrough_bits = 8;
473 passthrough_cycles = incc->cycles;
476 /* fallthrough - non-passthrough RGB565 (CSI-2 bus) */
478 burst_size = (image.pix.width & 0xf) ? 8 : 16;
479 passthrough_bits = 16;
484 ipu_cpmem_set_resolution(priv->idmac_ch,
485 image.rect.width * passthrough_cycles,
487 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
488 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
489 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
490 ipu_cpmem_set_format_passthrough(priv->idmac_ch,
493 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
498 ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
501 * Set the channel for the direct CSI-->memory via SMFC
502 * use-case to very high priority, by enabling the watermark
503 * signal in the SMFC, enabling WM in the channel, and setting
504 * the channel priority to high.
506 * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority
509 * The WM's are set very low by intention here to ensure that
510 * the SMFC FIFOs do not overflow.
512 ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
513 ipu_cpmem_set_high_priority(priv->idmac_ch);
514 ipu_idmac_enable_watermark(priv->idmac_ch, true);
515 ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
517 burst_size = passthrough ?
518 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
520 ipu_smfc_set_burstsize(priv->smfc, burst_size);
522 if (image.pix.field == V4L2_FIELD_NONE &&
523 V4L2_FIELD_HAS_BOTH(infmt->field))
524 ipu_cpmem_interlaced_scan(priv->idmac_ch,
525 image.pix.bytesperline);
527 ipu_idmac_set_double_buffer(priv->idmac_ch, true);
532 csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
536 static void csi_idmac_unsetup(struct csi_priv *priv,
537 enum vb2_buffer_state state)
539 ipu_idmac_disable_channel(priv->idmac_ch);
540 ipu_smfc_disable(priv->smfc);
542 csi_idmac_unsetup_vb2_buf(priv, state);
545 static int csi_idmac_setup(struct csi_priv *priv)
549 ret = csi_idmac_setup_channel(priv);
553 ipu_cpmem_dump(priv->idmac_ch);
556 ipu_smfc_enable(priv->smfc);
558 /* set buffers ready */
559 ipu_idmac_select_buffer(priv->idmac_ch, 0);
560 ipu_idmac_select_buffer(priv->idmac_ch, 1);
562 /* enable the channels */
563 ipu_idmac_enable_channel(priv->idmac_ch);
568 static int csi_idmac_start(struct csi_priv *priv)
570 struct imx_media_video_dev *vdev = priv->vdev;
571 struct v4l2_pix_format *outfmt;
574 ret = csi_idmac_get_ipu_resources(priv);
578 ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
580 outfmt = &vdev->fmt.fmt.pix;
582 ret = imx_media_alloc_dma_buf(priv->md, &priv->underrun_buf,
587 priv->ipu_buf_num = 0;
589 /* init EOF completion waitq */
590 init_completion(&priv->last_eof_comp);
591 priv->frame_sequence = 0;
592 priv->last_eof = false;
593 priv->nfb4eof = false;
595 ret = csi_idmac_setup(priv);
597 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
598 goto out_free_dma_buf;
601 priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
604 ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
605 csi_idmac_nfb4eof_interrupt, 0,
606 "imx-smfc-nfb4eof", priv);
609 "Error registering NFB4EOF irq: %d\n", ret);
613 priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
616 ret = devm_request_irq(priv->dev, priv->eof_irq,
617 csi_idmac_eof_interrupt, 0,
618 "imx-smfc-eof", priv);
621 "Error registering eof irq: %d\n", ret);
622 goto out_free_nfb4eof_irq;
625 /* start the EOF timeout timer */
626 mod_timer(&priv->eof_timeout_timer,
627 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
631 out_free_nfb4eof_irq:
632 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
634 csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
636 imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
638 csi_idmac_put_ipu_resources(priv);
642 static void csi_idmac_wait_last_eof(struct csi_priv *priv)
647 /* mark next EOF interrupt as the last before stream off */
648 spin_lock_irqsave(&priv->irqlock, flags);
649 priv->last_eof = true;
650 spin_unlock_irqrestore(&priv->irqlock, flags);
653 * and then wait for interrupt handler to mark completion.
655 ret = wait_for_completion_timeout(
656 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
658 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
661 static void csi_idmac_stop(struct csi_priv *priv)
663 devm_free_irq(priv->dev, priv->eof_irq, priv);
664 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
666 csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
668 imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
670 /* cancel the EOF timeout timer */
671 del_timer_sync(&priv->eof_timeout_timer);
673 csi_idmac_put_ipu_resources(priv);
676 /* Update the CSI whole sensor and active windows */
677 static int csi_setup(struct csi_priv *priv)
679 struct v4l2_mbus_framefmt *infmt, *outfmt;
680 const struct imx_media_pixfmt *incc;
681 struct v4l2_mbus_config mbus_cfg;
682 struct v4l2_mbus_framefmt if_fmt;
683 struct v4l2_rect crop;
685 infmt = &priv->format_mbus[CSI_SINK_PAD];
686 incc = priv->cc[CSI_SINK_PAD];
687 outfmt = &priv->format_mbus[priv->active_output_pad];
689 /* compose mbus_config from the upstream endpoint */
690 mbus_cfg.type = priv->upstream_ep.bus_type;
691 mbus_cfg.flags = is_parallel_bus(&priv->upstream_ep) ?
692 priv->upstream_ep.bus.parallel.flags :
693 priv->upstream_ep.bus.mipi_csi2.flags;
696 * we need to pass input frame to CSI interface, but
697 * with translated field type from output format
700 if_fmt.field = outfmt->field;
704 * if cycles is set, we need to handle this over multiple cycles as
707 if (is_parallel_bus(&priv->upstream_ep) && incc->cycles) {
708 if_fmt.width *= incc->cycles;
709 crop.width *= incc->cycles;
712 ipu_csi_set_window(priv->csi, &crop);
714 ipu_csi_set_downsize(priv->csi,
715 priv->crop.width == 2 * priv->compose.width,
716 priv->crop.height == 2 * priv->compose.height);
718 ipu_csi_init_interface(priv->csi, &mbus_cfg, &if_fmt);
720 ipu_csi_set_dest(priv->csi, priv->dest);
722 if (priv->dest == IPU_CSI_DEST_IDMAC)
723 ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc,
724 priv->skip->max_ratio - 1, 0);
726 ipu_csi_dump(priv->csi);
731 static int csi_start(struct csi_priv *priv)
733 struct v4l2_fract *input_fi, *output_fi;
736 input_fi = &priv->frame_interval[CSI_SINK_PAD];
737 output_fi = &priv->frame_interval[priv->active_output_pad];
740 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
741 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
745 /* Skip first few frames from a BT.656 source */
746 if (priv->upstream_ep.bus_type == V4L2_MBUS_BT656) {
747 u32 delay_usec, bad_frames = 20;
749 delay_usec = DIV_ROUND_UP_ULL((u64)USEC_PER_SEC *
750 input_fi->numerator * bad_frames,
751 input_fi->denominator);
753 usleep_range(delay_usec, delay_usec + 1000);
756 if (priv->dest == IPU_CSI_DEST_IDMAC) {
757 ret = csi_idmac_start(priv);
762 ret = csi_setup(priv);
766 /* start the frame interval monitor */
767 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) {
768 ret = imx_media_fim_set_stream(priv->fim, output_fi, true);
773 ret = ipu_csi_enable(priv->csi);
775 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
782 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
783 imx_media_fim_set_stream(priv->fim, NULL, false);
785 if (priv->dest == IPU_CSI_DEST_IDMAC)
786 csi_idmac_stop(priv);
788 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
792 static void csi_stop(struct csi_priv *priv)
794 if (priv->dest == IPU_CSI_DEST_IDMAC)
795 csi_idmac_wait_last_eof(priv);
798 * Disable the CSI asap, after syncing with the last EOF.
799 * Doing so after the IDMA channel is disabled has shown to
800 * create hard system-wide hangs.
802 ipu_csi_disable(priv->csi);
805 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
807 if (priv->dest == IPU_CSI_DEST_IDMAC) {
808 csi_idmac_stop(priv);
810 /* stop the frame interval monitor */
812 imx_media_fim_set_stream(priv->fim, NULL, false);
816 static const struct csi_skip_desc csi_skip[12] = {
817 { 1, 1, 0x00 }, /* Keep all frames */
818 { 5, 6, 0x10 }, /* Skip every sixth frame */
819 { 4, 5, 0x08 }, /* Skip every fifth frame */
820 { 3, 4, 0x04 }, /* Skip every fourth frame */
821 { 2, 3, 0x02 }, /* Skip every third frame */
822 { 3, 5, 0x0a }, /* Skip frames 1 and 3 of every 5 */
823 { 1, 2, 0x01 }, /* Skip every second frame */
824 { 2, 5, 0x0b }, /* Keep frames 1 and 4 of every 5 */
825 { 1, 3, 0x03 }, /* Keep one in three frames */
826 { 1, 4, 0x07 }, /* Keep one in four frames */
827 { 1, 5, 0x0f }, /* Keep one in five frames */
828 { 1, 6, 0x1f }, /* Keep one in six frames */
831 static void csi_apply_skip_interval(const struct csi_skip_desc *skip,
832 struct v4l2_fract *interval)
836 interval->numerator *= skip->max_ratio;
837 interval->denominator *= skip->keep;
839 /* Reduce fraction to lowest terms */
840 div = gcd(interval->numerator, interval->denominator);
842 interval->numerator /= div;
843 interval->denominator /= div;
848 * Find the skip pattern to produce the output frame interval closest to the
849 * requested one, for the given input frame interval. Updates the output frame
850 * interval to the exact value.
852 static const struct csi_skip_desc *csi_find_best_skip(struct v4l2_fract *in,
853 struct v4l2_fract *out)
855 const struct csi_skip_desc *skip = &csi_skip[0], *best_skip = skip;
856 u32 min_err = UINT_MAX;
860 /* Default to 1:1 ratio */
861 if (out->numerator == 0 || out->denominator == 0 ||
862 in->numerator == 0 || in->denominator == 0) {
867 want_us = div_u64((u64)USEC_PER_SEC * out->numerator, out->denominator);
869 /* Find the reduction closest to the requested time per frame */
870 for (i = 0; i < ARRAY_SIZE(csi_skip); i++, skip++) {
873 tmp = div_u64((u64)USEC_PER_SEC * in->numerator *
874 skip->max_ratio, in->denominator * skip->keep);
876 err = abs((s64)tmp - want_us);
884 csi_apply_skip_interval(best_skip, out);
890 * V4L2 subdev operations.
893 static int csi_g_frame_interval(struct v4l2_subdev *sd,
894 struct v4l2_subdev_frame_interval *fi)
896 struct csi_priv *priv = v4l2_get_subdevdata(sd);
898 if (fi->pad >= CSI_NUM_PADS)
901 mutex_lock(&priv->lock);
903 fi->interval = priv->frame_interval[fi->pad];
905 mutex_unlock(&priv->lock);
910 static int csi_s_frame_interval(struct v4l2_subdev *sd,
911 struct v4l2_subdev_frame_interval *fi)
913 struct csi_priv *priv = v4l2_get_subdevdata(sd);
914 struct v4l2_fract *input_fi;
917 mutex_lock(&priv->lock);
919 input_fi = &priv->frame_interval[CSI_SINK_PAD];
923 /* No limits on input frame interval */
924 /* Reset output intervals and frame skipping ratio to 1:1 */
925 priv->frame_interval[CSI_SRC_PAD_IDMAC] = fi->interval;
926 priv->frame_interval[CSI_SRC_PAD_DIRECT] = fi->interval;
927 priv->skip = &csi_skip[0];
929 case CSI_SRC_PAD_IDMAC:
931 * frame interval at IDMAC output pad depends on input
932 * interval, modified by frame skipping.
934 priv->skip = csi_find_best_skip(input_fi, &fi->interval);
936 case CSI_SRC_PAD_DIRECT:
938 * frame interval at DIRECT output pad is same as input
941 fi->interval = *input_fi;
948 priv->frame_interval[fi->pad] = fi->interval;
950 mutex_unlock(&priv->lock);
954 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
956 struct csi_priv *priv = v4l2_get_subdevdata(sd);
959 mutex_lock(&priv->lock);
961 if (!priv->src_sd || !priv->sink) {
967 * enable/disable streaming only if stream_count is
968 * going from 0 to 1 / 1 to 0.
970 if (priv->stream_count != !enable)
974 dev_dbg(priv->dev, "stream ON\n");
975 ret = csi_start(priv);
979 dev_dbg(priv->dev, "stream OFF\n");
984 priv->stream_count += enable ? 1 : -1;
985 if (priv->stream_count < 0)
986 priv->stream_count = 0;
988 mutex_unlock(&priv->lock);
992 static int csi_link_setup(struct media_entity *entity,
993 const struct media_pad *local,
994 const struct media_pad *remote, u32 flags)
996 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
997 struct csi_priv *priv = v4l2_get_subdevdata(sd);
998 struct v4l2_subdev *remote_sd;
1001 dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
1002 local->entity->name);
1004 mutex_lock(&priv->lock);
1006 if (local->flags & MEDIA_PAD_FL_SINK) {
1007 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1012 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1014 if (flags & MEDIA_LNK_FL_ENABLED) {
1019 priv->src_sd = remote_sd;
1021 priv->src_sd = NULL;
1027 /* this is a source pad */
1029 if (flags & MEDIA_LNK_FL_ENABLED) {
1035 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1036 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1041 /* record which output pad is now active */
1042 priv->active_output_pad = local->index;
1044 /* set CSI destination */
1045 if (local->index == CSI_SRC_PAD_IDMAC) {
1046 if (!is_media_entity_v4l2_video_device(remote->entity)) {
1052 ret = imx_media_fim_add_controls(priv->fim);
1057 priv->dest = IPU_CSI_DEST_IDMAC;
1059 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1064 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1065 switch (remote_sd->grp_id) {
1066 case IMX_MEDIA_GRP_ID_VDIC:
1067 priv->dest = IPU_CSI_DEST_VDIC;
1069 case IMX_MEDIA_GRP_ID_IC_PRP:
1070 priv->dest = IPU_CSI_DEST_IC;
1078 priv->sink = remote->entity;
1080 mutex_unlock(&priv->lock);
1084 static int csi_link_validate(struct v4l2_subdev *sd,
1085 struct media_link *link,
1086 struct v4l2_subdev_format *source_fmt,
1087 struct v4l2_subdev_format *sink_fmt)
1089 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1090 struct v4l2_fwnode_endpoint upstream_ep;
1094 ret = v4l2_subdev_link_validate_default(sd, link,
1095 source_fmt, sink_fmt);
1099 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1101 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1105 mutex_lock(&priv->lock);
1107 priv->upstream_ep = upstream_ep;
1108 is_csi2 = !is_parallel_bus(&upstream_ep);
1112 * NOTE! It seems the virtual channels from the mipi csi-2
1113 * receiver are used only for routing by the video mux's,
1114 * or for hard-wired routing to the CSI's. Once the stream
1115 * enters the CSI's however, they are treated internally
1116 * in the IPU as virtual channel 0.
1119 mutex_unlock(&priv->lock);
1120 vc_num = imx_media_find_mipi_csi2_channel(priv->md,
1124 mutex_lock(&priv->lock);
1126 ipu_csi_set_mipi_datatype(priv->csi, vc_num,
1127 &priv->format_mbus[CSI_SINK_PAD]);
1130 /* select either parallel or MIPI-CSI2 as input to CSI */
1131 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
1133 mutex_unlock(&priv->lock);
1137 static struct v4l2_mbus_framefmt *
1138 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1139 unsigned int pad, enum v4l2_subdev_format_whence which)
1141 if (which == V4L2_SUBDEV_FORMAT_TRY)
1142 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad);
1144 return &priv->format_mbus[pad];
1147 static struct v4l2_rect *
1148 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1149 enum v4l2_subdev_format_whence which)
1151 if (which == V4L2_SUBDEV_FORMAT_TRY)
1152 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD);
1157 static struct v4l2_rect *
1158 __csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
1159 enum v4l2_subdev_format_whence which)
1161 if (which == V4L2_SUBDEV_FORMAT_TRY)
1162 return v4l2_subdev_get_try_compose(&priv->sd, cfg,
1165 return &priv->compose;
1168 static void csi_try_crop(struct csi_priv *priv,
1169 struct v4l2_rect *crop,
1170 struct v4l2_subdev_pad_config *cfg,
1171 struct v4l2_mbus_framefmt *infmt,
1172 struct v4l2_fwnode_endpoint *upstream_ep)
1174 crop->width = min_t(__u32, infmt->width, crop->width);
1175 if (crop->left + crop->width > infmt->width)
1176 crop->left = infmt->width - crop->width;
1177 /* adjust crop left/width to h/w alignment restrictions */
1179 crop->width &= ~0x7;
1182 * FIXME: not sure why yet, but on interlaced bt.656,
1183 * changing the vertical cropping causes loss of vertical
1184 * sync, so fix it to NTSC/PAL active lines. NTSC contains
1185 * 2 extra lines of active video that need to be cropped.
1187 if (upstream_ep->bus_type == V4L2_MBUS_BT656 &&
1188 (V4L2_FIELD_HAS_BOTH(infmt->field) ||
1189 infmt->field == V4L2_FIELD_ALTERNATE)) {
1190 crop->height = infmt->height;
1191 crop->top = (infmt->height == 480) ? 2 : 0;
1193 crop->height = min_t(__u32, infmt->height, crop->height);
1194 if (crop->top + crop->height > infmt->height)
1195 crop->top = infmt->height - crop->height;
1199 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
1200 struct v4l2_subdev_pad_config *cfg,
1201 struct v4l2_subdev_mbus_code_enum *code)
1203 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1204 struct v4l2_fwnode_endpoint upstream_ep;
1205 const struct imx_media_pixfmt *incc;
1206 struct v4l2_mbus_framefmt *infmt;
1209 mutex_lock(&priv->lock);
1211 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which);
1212 incc = imx_media_find_mbus_format(infmt->code, CS_SEL_ANY, true);
1214 switch (code->pad) {
1216 ret = imx_media_enum_mbus_format(&code->code, code->index,
1219 case CSI_SRC_PAD_DIRECT:
1220 case CSI_SRC_PAD_IDMAC:
1221 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1223 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1227 if (requires_passthrough(&upstream_ep, infmt, incc)) {
1228 if (code->index != 0) {
1232 code->code = infmt->code;
1234 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1235 CS_SEL_YUV : CS_SEL_RGB;
1236 ret = imx_media_enum_ipu_format(&code->code,
1246 mutex_unlock(&priv->lock);
1250 static int csi_enum_frame_size(struct v4l2_subdev *sd,
1251 struct v4l2_subdev_pad_config *cfg,
1252 struct v4l2_subdev_frame_size_enum *fse)
1254 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1255 struct v4l2_rect *crop;
1258 if (fse->pad >= CSI_NUM_PADS ||
1259 fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3))
1262 mutex_lock(&priv->lock);
1264 if (fse->pad == CSI_SINK_PAD) {
1265 fse->min_width = MIN_W;
1266 fse->max_width = MAX_W;
1267 fse->min_height = MIN_H;
1268 fse->max_height = MAX_H;
1270 crop = __csi_get_crop(priv, cfg, fse->which);
1272 fse->min_width = fse->index & 1 ?
1273 crop->width / 2 : crop->width;
1274 fse->max_width = fse->min_width;
1275 fse->min_height = fse->index & 2 ?
1276 crop->height / 2 : crop->height;
1277 fse->max_height = fse->min_height;
1280 mutex_unlock(&priv->lock);
1284 static int csi_enum_frame_interval(struct v4l2_subdev *sd,
1285 struct v4l2_subdev_pad_config *cfg,
1286 struct v4l2_subdev_frame_interval_enum *fie)
1288 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1289 struct v4l2_fract *input_fi;
1290 struct v4l2_rect *crop;
1293 if (fie->pad >= CSI_NUM_PADS ||
1294 fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ?
1295 1 : ARRAY_SIZE(csi_skip)))
1298 mutex_lock(&priv->lock);
1300 input_fi = &priv->frame_interval[CSI_SINK_PAD];
1301 crop = __csi_get_crop(priv, cfg, fie->which);
1303 if ((fie->width != crop->width && fie->width != crop->width / 2) ||
1304 (fie->height != crop->height && fie->height != crop->height / 2)) {
1309 fie->interval = *input_fi;
1311 if (fie->pad == CSI_SRC_PAD_IDMAC)
1312 csi_apply_skip_interval(&csi_skip[fie->index],
1316 mutex_unlock(&priv->lock);
1320 static int csi_get_fmt(struct v4l2_subdev *sd,
1321 struct v4l2_subdev_pad_config *cfg,
1322 struct v4l2_subdev_format *sdformat)
1324 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1325 struct v4l2_mbus_framefmt *fmt;
1328 if (sdformat->pad >= CSI_NUM_PADS)
1331 mutex_lock(&priv->lock);
1333 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1339 sdformat->format = *fmt;
1341 mutex_unlock(&priv->lock);
1345 static void csi_try_fmt(struct csi_priv *priv,
1346 struct v4l2_fwnode_endpoint *upstream_ep,
1347 struct v4l2_subdev_pad_config *cfg,
1348 struct v4l2_subdev_format *sdformat,
1349 struct v4l2_rect *crop,
1350 struct v4l2_rect *compose,
1351 const struct imx_media_pixfmt **cc)
1353 const struct imx_media_pixfmt *incc;
1354 struct v4l2_mbus_framefmt *infmt;
1357 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
1359 switch (sdformat->pad) {
1360 case CSI_SRC_PAD_DIRECT:
1361 case CSI_SRC_PAD_IDMAC:
1362 incc = imx_media_find_mbus_format(infmt->code,
1365 sdformat->format.width = compose->width;
1366 sdformat->format.height = compose->height;
1368 if (requires_passthrough(upstream_ep, infmt, incc)) {
1369 sdformat->format.code = infmt->code;
1372 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1373 CS_SEL_YUV : CS_SEL_RGB;
1375 *cc = imx_media_find_ipu_format(sdformat->format.code,
1378 imx_media_enum_ipu_format(&code, 0, cs_sel);
1379 *cc = imx_media_find_ipu_format(code, cs_sel);
1380 sdformat->format.code = (*cc)->codes[0];
1384 if (sdformat->pad == CSI_SRC_PAD_DIRECT ||
1385 sdformat->format.field != V4L2_FIELD_NONE)
1386 sdformat->format.field = infmt->field;
1389 * translate V4L2_FIELD_ALTERNATE to SEQ_TB or SEQ_BT
1390 * depending on input height (assume NTSC top-bottom
1391 * order if 480 lines, otherwise PAL bottom-top order).
1393 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) {
1394 sdformat->format.field = (infmt->height == 480) ?
1395 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1398 /* propagate colorimetry from sink */
1399 sdformat->format.colorspace = infmt->colorspace;
1400 sdformat->format.xfer_func = infmt->xfer_func;
1401 sdformat->format.quantization = infmt->quantization;
1402 sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
1405 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1406 W_ALIGN, &sdformat->format.height,
1407 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1409 /* Reset crop and compose rectangles */
1412 crop->width = sdformat->format.width;
1413 crop->height = sdformat->format.height;
1414 csi_try_crop(priv, crop, cfg, &sdformat->format, upstream_ep);
1417 compose->width = crop->width;
1418 compose->height = crop->height;
1420 *cc = imx_media_find_mbus_format(sdformat->format.code,
1423 imx_media_enum_mbus_format(&code, 0,
1425 *cc = imx_media_find_mbus_format(code,
1427 sdformat->format.code = (*cc)->codes[0];
1430 imx_media_fill_default_mbus_fields(
1431 &sdformat->format, infmt,
1432 priv->active_output_pad == CSI_SRC_PAD_DIRECT);
1437 static int csi_set_fmt(struct v4l2_subdev *sd,
1438 struct v4l2_subdev_pad_config *cfg,
1439 struct v4l2_subdev_format *sdformat)
1441 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1442 struct imx_media_video_dev *vdev = priv->vdev;
1443 struct v4l2_fwnode_endpoint upstream_ep;
1444 const struct imx_media_pixfmt *cc;
1445 struct v4l2_pix_format vdev_fmt;
1446 struct v4l2_mbus_framefmt *fmt;
1447 struct v4l2_rect *crop, *compose;
1450 if (sdformat->pad >= CSI_NUM_PADS)
1453 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1455 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1459 mutex_lock(&priv->lock);
1461 if (priv->stream_count > 0) {
1466 crop = __csi_get_crop(priv, cfg, sdformat->which);
1467 compose = __csi_get_compose(priv, cfg, sdformat->which);
1469 csi_try_fmt(priv, &upstream_ep, cfg, sdformat, crop, compose, &cc);
1471 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1472 *fmt = sdformat->format;
1474 if (sdformat->pad == CSI_SINK_PAD) {
1477 /* propagate format to source pads */
1478 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1479 const struct imx_media_pixfmt *outcc;
1480 struct v4l2_mbus_framefmt *outfmt;
1481 struct v4l2_subdev_format format;
1484 format.which = sdformat->which;
1485 format.format = sdformat->format;
1486 csi_try_fmt(priv, &upstream_ep, cfg, &format,
1487 NULL, compose, &outcc);
1489 outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which);
1490 *outfmt = format.format;
1492 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1493 priv->cc[pad] = outcc;
1497 if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY)
1500 priv->cc[sdformat->pad] = cc;
1502 /* propagate IDMAC output pad format to capture device */
1503 imx_media_mbus_fmt_to_pix_fmt(&vdev_fmt,
1504 &priv->format_mbus[CSI_SRC_PAD_IDMAC],
1505 priv->cc[CSI_SRC_PAD_IDMAC]);
1506 mutex_unlock(&priv->lock);
1507 imx_media_capture_device_set_format(vdev, &vdev_fmt);
1511 mutex_unlock(&priv->lock);
1515 static int csi_get_selection(struct v4l2_subdev *sd,
1516 struct v4l2_subdev_pad_config *cfg,
1517 struct v4l2_subdev_selection *sel)
1519 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1520 struct v4l2_mbus_framefmt *infmt;
1521 struct v4l2_rect *crop, *compose;
1524 if (sel->pad != CSI_SINK_PAD)
1527 mutex_lock(&priv->lock);
1529 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1530 crop = __csi_get_crop(priv, cfg, sel->which);
1531 compose = __csi_get_compose(priv, cfg, sel->which);
1533 switch (sel->target) {
1534 case V4L2_SEL_TGT_CROP_BOUNDS:
1537 sel->r.width = infmt->width;
1538 sel->r.height = infmt->height;
1540 case V4L2_SEL_TGT_CROP:
1543 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1546 sel->r.width = crop->width;
1547 sel->r.height = crop->height;
1549 case V4L2_SEL_TGT_COMPOSE:
1556 mutex_unlock(&priv->lock);
1560 static int csi_set_scale(u32 *compose, u32 crop, u32 flags)
1562 if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) ==
1563 (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) &&
1564 *compose != crop && *compose != crop / 2)
1567 if (*compose <= crop / 2 ||
1568 (*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) ||
1569 (*compose < crop && (flags & V4L2_SEL_FLAG_LE)))
1570 *compose = crop / 2;
1577 static int csi_set_selection(struct v4l2_subdev *sd,
1578 struct v4l2_subdev_pad_config *cfg,
1579 struct v4l2_subdev_selection *sel)
1581 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1582 struct v4l2_fwnode_endpoint upstream_ep;
1583 struct v4l2_mbus_framefmt *infmt;
1584 struct v4l2_rect *crop, *compose;
1587 if (sel->pad != CSI_SINK_PAD)
1590 ret = csi_get_upstream_endpoint(priv, &upstream_ep);
1592 v4l2_err(&priv->sd, "failed to find upstream endpoint\n");
1596 mutex_lock(&priv->lock);
1598 if (priv->stream_count > 0) {
1603 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1604 crop = __csi_get_crop(priv, cfg, sel->which);
1605 compose = __csi_get_compose(priv, cfg, sel->which);
1607 switch (sel->target) {
1608 case V4L2_SEL_TGT_CROP:
1610 * Modifying the crop rectangle always changes the format on
1611 * the source pads. If the KEEP_CONFIG flag is set, just return
1612 * the current crop rectangle.
1614 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1615 sel->r = priv->crop;
1616 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1621 csi_try_crop(priv, &sel->r, cfg, infmt, &upstream_ep);
1625 /* Reset scaling to 1:1 */
1626 compose->width = crop->width;
1627 compose->height = crop->height;
1629 case V4L2_SEL_TGT_COMPOSE:
1631 * Modifying the compose rectangle always changes the format on
1632 * the source pads. If the KEEP_CONFIG flag is set, just return
1633 * the current compose rectangle.
1635 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1636 sel->r = priv->compose;
1637 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1644 ret = csi_set_scale(&sel->r.width, crop->width, sel->flags);
1647 ret = csi_set_scale(&sel->r.height, crop->height, sel->flags);
1658 /* Reset source pads to sink compose rectangle */
1659 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1660 struct v4l2_mbus_framefmt *outfmt;
1662 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which);
1663 outfmt->width = compose->width;
1664 outfmt->height = compose->height;
1668 mutex_unlock(&priv->lock);
1672 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1673 struct v4l2_event_subscription *sub)
1675 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1680 return v4l2_event_subscribe(fh, sub, 0, NULL);
1683 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1684 struct v4l2_event_subscription *sub)
1686 return v4l2_event_unsubscribe(fh, sub);
1690 * retrieve our pads parsed from the OF graph by the media device
1692 static int csi_registered(struct v4l2_subdev *sd)
1694 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1695 struct ipu_csi *csi;
1699 /* get media device */
1700 priv->md = dev_get_drvdata(sd->v4l2_dev->dev);
1702 /* get handle to IPU CSI */
1703 csi = ipu_csi_get(priv->ipu, priv->csi_id);
1705 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1706 return PTR_ERR(csi);
1710 for (i = 0; i < CSI_NUM_PADS; i++) {
1711 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
1712 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1715 if (i != CSI_SINK_PAD)
1716 imx_media_enum_ipu_format(&code, 0, CS_SEL_YUV);
1718 /* set a default mbus format */
1719 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1720 640, 480, code, V4L2_FIELD_NONE,
1725 /* init default frame interval */
1726 priv->frame_interval[i].numerator = 1;
1727 priv->frame_interval[i].denominator = 30;
1730 /* disable frame skipping */
1731 priv->skip = &csi_skip[0];
1733 /* init default crop and compose rectangle sizes */
1734 priv->crop.width = 640;
1735 priv->crop.height = 480;
1736 priv->compose.width = 640;
1737 priv->compose.height = 480;
1739 priv->fim = imx_media_fim_init(&priv->sd);
1740 if (IS_ERR(priv->fim)) {
1741 ret = PTR_ERR(priv->fim);
1745 ret = media_entity_pads_init(&sd->entity, CSI_NUM_PADS, priv->pad);
1749 ret = imx_media_capture_device_register(priv->vdev);
1753 ret = imx_media_add_video_device(priv->md, priv->vdev);
1759 imx_media_capture_device_unregister(priv->vdev);
1762 imx_media_fim_free(priv->fim);
1764 ipu_csi_put(priv->csi);
1768 static void csi_unregistered(struct v4l2_subdev *sd)
1770 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1772 imx_media_capture_device_unregister(priv->vdev);
1775 imx_media_fim_free(priv->fim);
1778 ipu_csi_put(priv->csi);
1781 static const struct media_entity_operations csi_entity_ops = {
1782 .link_setup = csi_link_setup,
1783 .link_validate = v4l2_subdev_link_validate,
1786 static const struct v4l2_subdev_core_ops csi_core_ops = {
1787 .subscribe_event = csi_subscribe_event,
1788 .unsubscribe_event = csi_unsubscribe_event,
1791 static const struct v4l2_subdev_video_ops csi_video_ops = {
1792 .g_frame_interval = csi_g_frame_interval,
1793 .s_frame_interval = csi_s_frame_interval,
1794 .s_stream = csi_s_stream,
1797 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1798 .init_cfg = imx_media_init_cfg,
1799 .enum_mbus_code = csi_enum_mbus_code,
1800 .enum_frame_size = csi_enum_frame_size,
1801 .enum_frame_interval = csi_enum_frame_interval,
1802 .get_fmt = csi_get_fmt,
1803 .set_fmt = csi_set_fmt,
1804 .get_selection = csi_get_selection,
1805 .set_selection = csi_set_selection,
1806 .link_validate = csi_link_validate,
1809 static const struct v4l2_subdev_ops csi_subdev_ops = {
1810 .core = &csi_core_ops,
1811 .video = &csi_video_ops,
1812 .pad = &csi_pad_ops,
1815 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1816 .registered = csi_registered,
1817 .unregistered = csi_unregistered,
1820 static int imx_csi_probe(struct platform_device *pdev)
1822 struct ipu_client_platformdata *pdata;
1823 struct pinctrl *pinctrl;
1824 struct csi_priv *priv;
1827 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1831 platform_set_drvdata(pdev, &priv->sd);
1832 priv->dev = &pdev->dev;
1834 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1838 /* get parent IPU */
1839 priv->ipu = dev_get_drvdata(priv->dev->parent);
1841 /* get our CSI id */
1842 pdata = priv->dev->platform_data;
1843 priv->csi_id = pdata->csi;
1844 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1846 timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0);
1847 spin_lock_init(&priv->irqlock);
1849 v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1850 v4l2_set_subdevdata(&priv->sd, priv);
1851 priv->sd.internal_ops = &csi_internal_ops;
1852 priv->sd.entity.ops = &csi_entity_ops;
1853 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1854 priv->sd.dev = &pdev->dev;
1855 priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
1856 priv->sd.owner = THIS_MODULE;
1857 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1858 priv->sd.grp_id = priv->csi_id ?
1859 IMX_MEDIA_GRP_ID_CSI1 : IMX_MEDIA_GRP_ID_CSI0;
1860 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
1861 priv->sd.grp_id, ipu_get_num(priv->ipu));
1863 priv->vdev = imx_media_capture_device_init(&priv->sd,
1865 if (IS_ERR(priv->vdev))
1866 return PTR_ERR(priv->vdev);
1868 mutex_init(&priv->lock);
1870 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1871 priv->sd.ctrl_handler = &priv->ctrl_hdlr;
1874 * The IPUv3 driver did not assign an of_node to this
1875 * device. As a result, pinctrl does not automatically
1876 * configure our pin groups, so we need to do that manually
1877 * here, after setting this device's of_node.
1879 priv->dev->of_node = pdata->of_node;
1880 pinctrl = devm_pinctrl_get_select_default(priv->dev);
1881 if (IS_ERR(pinctrl)) {
1882 ret = PTR_ERR(pinctrl);
1884 "devm_pinctrl_get_select_default() failed: %d\n", ret);
1889 ret = v4l2_async_register_subdev(&priv->sd);
1895 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1896 mutex_destroy(&priv->lock);
1897 imx_media_capture_device_remove(priv->vdev);
1901 static int imx_csi_remove(struct platform_device *pdev)
1903 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1904 struct csi_priv *priv = sd_to_dev(sd);
1906 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1907 mutex_destroy(&priv->lock);
1908 imx_media_capture_device_remove(priv->vdev);
1909 v4l2_async_unregister_subdev(sd);
1910 media_entity_cleanup(&sd->entity);
1915 static const struct platform_device_id imx_csi_ids[] = {
1916 { .name = "imx-ipuv3-csi" },
1919 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
1921 static struct platform_driver imx_csi_driver = {
1922 .probe = imx_csi_probe,
1923 .remove = imx_csi_remove,
1924 .id_table = imx_csi_ids,
1926 .name = "imx-ipuv3-csi",
1929 module_platform_driver(imx_csi_driver);
1931 MODULE_DESCRIPTION("i.MX CSI subdev driver");
1932 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
1933 MODULE_LICENSE("GPL");
1934 MODULE_ALIAS("platform:imx-ipuv3-csi");