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 struct v4l2_async_notifier notifier;
63 /* the video device at IDMAC output pad */
64 struct imx_media_video_dev *vdev;
65 struct imx_media_fim *fim;
69 /* lock to protect all members below */
72 int active_output_pad;
74 struct ipuv3_channel *idmac_ch;
75 struct ipu_smfc *smfc;
78 struct v4l2_mbus_framefmt format_mbus[CSI_NUM_PADS];
79 const struct imx_media_pixfmt *cc[CSI_NUM_PADS];
80 struct v4l2_fract frame_interval[CSI_NUM_PADS];
81 struct v4l2_rect crop;
82 struct v4l2_rect compose;
83 const struct csi_skip_desc *skip;
85 /* active vb2 buffers to send to video dev sink */
86 struct imx_media_buffer *active_vb2_buf[2];
87 struct imx_media_dma_buf underrun_buf;
89 int ipu_buf_num; /* ipu double buffer index: 0-1 */
91 /* the sink for the captured frames */
92 struct media_entity *sink;
93 enum ipu_csi_dest dest;
94 /* the source subdev */
95 struct v4l2_subdev *src_sd;
97 /* the mipi virtual channel number at link validate */
100 /* media bus config of the upstream subdevice CSI is receiving from */
101 struct v4l2_mbus_config mbus_cfg;
103 spinlock_t irqlock; /* protect eof_irq handler */
104 struct timer_list eof_timeout_timer;
108 struct v4l2_ctrl_handler ctrl_hdlr;
110 int stream_count; /* streaming counter */
111 u32 frame_sequence; /* frame sequence counter */
112 bool last_eof; /* waiting for last EOF at stream off */
113 bool nfb4eof; /* NFB4EOF encountered during streaming */
114 bool interweave_swap; /* swap top/bottom lines when interweaving */
115 struct completion last_eof_comp;
118 static inline struct csi_priv *sd_to_dev(struct v4l2_subdev *sdev)
120 return container_of(sdev, struct csi_priv, sd);
123 static inline struct csi_priv *notifier_to_dev(struct v4l2_async_notifier *n)
125 return container_of(n, struct csi_priv, notifier);
128 static inline bool is_parallel_bus(struct v4l2_mbus_config *mbus_cfg)
130 return mbus_cfg->type != V4L2_MBUS_CSI2_DPHY;
133 static inline bool is_parallel_16bit_bus(struct v4l2_mbus_config *mbus_cfg)
135 return is_parallel_bus(mbus_cfg) && mbus_cfg->bus.parallel.bus_width >= 16;
139 * Check for conditions that require the IPU to handle the
140 * data internally as generic data, aka passthrough mode:
141 * - raw bayer media bus formats, or
142 * - BT.656 and BT.1120 (8/10-bit YUV422) data can always be processed
144 * - the CSI is receiving from a 16-bit parallel bus, or
145 * - the CSI is receiving from an 8-bit parallel bus and the incoming
146 * media bus format is other than UYVY8_2X8/YUYV8_2X8.
148 static inline bool requires_passthrough(struct v4l2_mbus_config *mbus_cfg,
149 struct v4l2_mbus_framefmt *infmt,
150 const struct imx_media_pixfmt *incc)
152 if (mbus_cfg->type == V4L2_MBUS_BT656) // including BT.1120
155 return incc->bayer || is_parallel_16bit_bus(mbus_cfg) ||
156 (is_parallel_bus(mbus_cfg) &&
157 infmt->code != MEDIA_BUS_FMT_UYVY8_2X8 &&
158 infmt->code != MEDIA_BUS_FMT_YUYV8_2X8);
162 * Queries the media bus config of the upstream entity that provides data to
163 * the CSI. This will either be the entity directly upstream from the CSI-2
164 * receiver, directly upstream from a video mux, or directly upstream from
167 static int csi_get_upstream_mbus_config(struct csi_priv *priv,
168 struct v4l2_mbus_config *mbus_cfg)
170 struct v4l2_subdev *sd, *remote_sd;
171 struct media_pad *remote_pad;
179 switch (sd->grp_id) {
180 case IMX_MEDIA_GRP_ID_CSI_MUX:
182 * CSI is connected directly to CSI mux, skip up to
183 * CSI-2 receiver if it is in the path, otherwise stay
186 sd = imx_media_pipeline_subdev(&sd->entity,
187 IMX_MEDIA_GRP_ID_CSI2,
192 case IMX_MEDIA_GRP_ID_CSI2:
196 * the source is neither the CSI mux nor the CSI-2 receiver,
197 * get the source pad directly upstream from CSI itself.
203 /* get source pad of entity directly upstream from sd */
204 remote_pad = media_entity_remote_pad_unique(&sd->entity,
205 MEDIA_PAD_FL_SOURCE);
206 if (IS_ERR(remote_pad))
207 return PTR_ERR(remote_pad);
209 remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity);
211 ret = v4l2_subdev_call(remote_sd, pad, get_mbus_config,
212 remote_pad->index, mbus_cfg);
213 if (ret == -ENOIOCTLCMD)
215 "entity %s does not implement get_mbus_config()\n",
216 remote_pad->entity->name);
221 static void csi_idmac_put_ipu_resources(struct csi_priv *priv)
224 ipu_idmac_put(priv->idmac_ch);
225 priv->idmac_ch = NULL;
228 ipu_smfc_put(priv->smfc);
232 static int csi_idmac_get_ipu_resources(struct csi_priv *priv)
235 struct ipu_smfc *smfc;
236 struct ipuv3_channel *idmac_ch;
238 ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id;
240 smfc = ipu_smfc_get(priv->ipu, ch_num);
242 v4l2_err(&priv->sd, "failed to get SMFC\n");
248 idmac_ch = ipu_idmac_get(priv->ipu, ch_num);
249 if (IS_ERR(idmac_ch)) {
250 v4l2_err(&priv->sd, "could not get IDMAC channel %u\n",
252 ret = PTR_ERR(idmac_ch);
255 priv->idmac_ch = idmac_ch;
259 csi_idmac_put_ipu_resources(priv);
263 static void csi_vb2_buf_done(struct csi_priv *priv)
265 struct imx_media_video_dev *vdev = priv->vdev;
266 struct imx_media_buffer *done, *next;
267 struct vb2_buffer *vb;
270 done = priv->active_vb2_buf[priv->ipu_buf_num];
272 done->vbuf.field = vdev->fmt.field;
273 done->vbuf.sequence = priv->frame_sequence;
274 vb = &done->vbuf.vb2_buf;
275 vb->timestamp = ktime_get_ns();
276 vb2_buffer_done(vb, priv->nfb4eof ?
277 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
280 priv->frame_sequence++;
281 priv->nfb4eof = false;
283 /* get next queued buffer */
284 next = imx_media_capture_device_next_buf(vdev);
286 phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0);
287 priv->active_vb2_buf[priv->ipu_buf_num] = next;
289 phys = priv->underrun_buf.phys;
290 priv->active_vb2_buf[priv->ipu_buf_num] = NULL;
293 if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num))
294 ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num);
296 if (priv->interweave_swap)
297 phys += vdev->fmt.bytesperline;
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 v4l2_mbus_framefmt *outfmt;
412 bool passthrough, interweave;
413 struct ipu_image image;
414 u32 passthrough_bits;
415 u32 passthrough_cycles;
420 infmt = &priv->format_mbus[CSI_SINK_PAD];
421 incc = priv->cc[CSI_SINK_PAD];
422 outfmt = &priv->format_mbus[CSI_SRC_PAD_IDMAC];
424 ipu_cpmem_zero(priv->idmac_ch);
426 memset(&image, 0, sizeof(image));
427 image.pix = vdev->fmt;
428 image.rect = vdev->compose;
430 csi_idmac_setup_vb2_buf(priv, phys);
432 image.phys0 = phys[0];
433 image.phys1 = phys[1];
435 passthrough = requires_passthrough(&priv->mbus_cfg, infmt, incc);
436 passthrough_cycles = 1;
439 * If the field type at capture interface is interlaced, and
440 * the output IDMAC pad is sequential, enable interweave at
441 * the IDMAC output channel.
443 interweave = V4L2_FIELD_IS_INTERLACED(image.pix.field) &&
444 V4L2_FIELD_IS_SEQUENTIAL(outfmt->field);
445 priv->interweave_swap = interweave &&
446 image.pix.field == V4L2_FIELD_INTERLACED_BT;
448 switch (image.pix.pixelformat) {
449 case V4L2_PIX_FMT_SBGGR8:
450 case V4L2_PIX_FMT_SGBRG8:
451 case V4L2_PIX_FMT_SGRBG8:
452 case V4L2_PIX_FMT_SRGGB8:
453 case V4L2_PIX_FMT_GREY:
455 passthrough_bits = 8;
457 case V4L2_PIX_FMT_SBGGR16:
458 case V4L2_PIX_FMT_SGBRG16:
459 case V4L2_PIX_FMT_SGRBG16:
460 case V4L2_PIX_FMT_SRGGB16:
461 case V4L2_PIX_FMT_Y10:
462 case V4L2_PIX_FMT_Y12:
464 passthrough_bits = 16;
466 case V4L2_PIX_FMT_YUV420:
467 case V4L2_PIX_FMT_YVU420:
468 case V4L2_PIX_FMT_NV12:
469 burst_size = (image.pix.width & 0x3f) ?
470 ((image.pix.width & 0x1f) ?
471 ((image.pix.width & 0xf) ? 8 : 16) : 32) : 64;
472 passthrough_bits = 16;
474 * Skip writing U and V components to odd rows (but not
475 * when enabling IDMAC interweaving, they are incompatible).
478 ipu_cpmem_skip_odd_chroma_rows(priv->idmac_ch);
480 case V4L2_PIX_FMT_YUYV:
481 case V4L2_PIX_FMT_UYVY:
482 burst_size = (image.pix.width & 0x1f) ?
483 ((image.pix.width & 0xf) ? 8 : 16) : 32;
484 passthrough_bits = 16;
486 case V4L2_PIX_FMT_RGB565:
489 passthrough_bits = 8;
490 passthrough_cycles = incc->cycles;
493 fallthrough; /* non-passthrough RGB565 (CSI-2 bus) */
495 burst_size = (image.pix.width & 0xf) ? 8 : 16;
496 passthrough_bits = 16;
501 if (priv->interweave_swap) {
502 /* start interweave scan at 1st top line (2nd line) */
503 image.phys0 += image.pix.bytesperline;
504 image.phys1 += image.pix.bytesperline;
507 ipu_cpmem_set_resolution(priv->idmac_ch,
508 image.rect.width * passthrough_cycles,
510 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
511 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
512 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
513 ipu_cpmem_set_format_passthrough(priv->idmac_ch,
516 if (priv->interweave_swap) {
517 /* start interweave scan at 1st top line (2nd line) */
521 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
526 ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
529 * Set the channel for the direct CSI-->memory via SMFC
530 * use-case to very high priority, by enabling the watermark
531 * signal in the SMFC, enabling WM in the channel, and setting
532 * the channel priority to high.
534 * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority
537 * The WM's are set very low by intention here to ensure that
538 * the SMFC FIFOs do not overflow.
540 ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
541 ipu_cpmem_set_high_priority(priv->idmac_ch);
542 ipu_idmac_enable_watermark(priv->idmac_ch, true);
543 ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
545 burst_size = passthrough ?
546 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
548 ipu_smfc_set_burstsize(priv->smfc, burst_size);
551 ipu_cpmem_interlaced_scan(priv->idmac_ch,
552 priv->interweave_swap ?
553 -image.pix.bytesperline :
554 image.pix.bytesperline,
555 image.pix.pixelformat);
557 ipu_idmac_set_double_buffer(priv->idmac_ch, true);
562 csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
566 static void csi_idmac_unsetup(struct csi_priv *priv,
567 enum vb2_buffer_state state)
569 ipu_idmac_disable_channel(priv->idmac_ch);
570 ipu_smfc_disable(priv->smfc);
572 csi_idmac_unsetup_vb2_buf(priv, state);
575 static int csi_idmac_setup(struct csi_priv *priv)
579 ret = csi_idmac_setup_channel(priv);
583 ipu_cpmem_dump(priv->idmac_ch);
586 ipu_smfc_enable(priv->smfc);
588 /* set buffers ready */
589 ipu_idmac_select_buffer(priv->idmac_ch, 0);
590 ipu_idmac_select_buffer(priv->idmac_ch, 1);
592 /* enable the channels */
593 ipu_idmac_enable_channel(priv->idmac_ch);
598 static int csi_idmac_start(struct csi_priv *priv)
600 struct imx_media_video_dev *vdev = priv->vdev;
603 ret = csi_idmac_get_ipu_resources(priv);
607 ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
609 ret = imx_media_alloc_dma_buf(priv->dev, &priv->underrun_buf,
610 vdev->fmt.sizeimage);
614 priv->ipu_buf_num = 0;
616 /* init EOF completion waitq */
617 init_completion(&priv->last_eof_comp);
618 priv->frame_sequence = 0;
619 priv->last_eof = false;
620 priv->nfb4eof = false;
622 ret = csi_idmac_setup(priv);
624 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
625 goto out_free_dma_buf;
628 priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
631 ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
632 csi_idmac_nfb4eof_interrupt, 0,
633 "imx-smfc-nfb4eof", priv);
636 "Error registering NFB4EOF irq: %d\n", ret);
640 priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
643 ret = devm_request_irq(priv->dev, priv->eof_irq,
644 csi_idmac_eof_interrupt, 0,
645 "imx-smfc-eof", priv);
648 "Error registering eof irq: %d\n", ret);
649 goto out_free_nfb4eof_irq;
652 /* start the EOF timeout timer */
653 mod_timer(&priv->eof_timeout_timer,
654 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
658 out_free_nfb4eof_irq:
659 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
661 csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
663 imx_media_free_dma_buf(priv->dev, &priv->underrun_buf);
665 csi_idmac_put_ipu_resources(priv);
669 static void csi_idmac_wait_last_eof(struct csi_priv *priv)
674 /* mark next EOF interrupt as the last before stream off */
675 spin_lock_irqsave(&priv->irqlock, flags);
676 priv->last_eof = true;
677 spin_unlock_irqrestore(&priv->irqlock, flags);
680 * and then wait for interrupt handler to mark completion.
682 ret = wait_for_completion_timeout(
683 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
685 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
688 static void csi_idmac_stop(struct csi_priv *priv)
690 devm_free_irq(priv->dev, priv->eof_irq, priv);
691 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
693 csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
695 imx_media_free_dma_buf(priv->dev, &priv->underrun_buf);
697 /* cancel the EOF timeout timer */
698 del_timer_sync(&priv->eof_timeout_timer);
700 csi_idmac_put_ipu_resources(priv);
703 /* Update the CSI whole sensor and active windows */
704 static int csi_setup(struct csi_priv *priv)
706 struct v4l2_mbus_framefmt *infmt, *outfmt;
707 const struct imx_media_pixfmt *incc;
708 struct v4l2_mbus_framefmt if_fmt;
709 struct v4l2_rect crop;
711 infmt = &priv->format_mbus[CSI_SINK_PAD];
712 incc = priv->cc[CSI_SINK_PAD];
713 outfmt = &priv->format_mbus[priv->active_output_pad];
719 * if cycles is set, we need to handle this over multiple cycles as
722 if (is_parallel_bus(&priv->mbus_cfg) && incc->cycles) {
723 if_fmt.width *= incc->cycles;
724 crop.width *= incc->cycles;
727 ipu_csi_set_window(priv->csi, &crop);
729 ipu_csi_set_downsize(priv->csi,
730 priv->crop.width == 2 * priv->compose.width,
731 priv->crop.height == 2 * priv->compose.height);
733 ipu_csi_init_interface(priv->csi, &priv->mbus_cfg, &if_fmt, outfmt);
735 ipu_csi_set_dest(priv->csi, priv->dest);
737 if (priv->dest == IPU_CSI_DEST_IDMAC)
738 ipu_csi_set_skip_smfc(priv->csi, priv->skip->skip_smfc,
739 priv->skip->max_ratio - 1, 0);
741 ipu_csi_dump(priv->csi);
746 static int csi_start(struct csi_priv *priv)
748 struct v4l2_fract *input_fi, *output_fi;
751 input_fi = &priv->frame_interval[CSI_SINK_PAD];
752 output_fi = &priv->frame_interval[priv->active_output_pad];
755 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
756 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
760 /* Skip first few frames from a BT.656 source */
761 if (priv->mbus_cfg.type == V4L2_MBUS_BT656) {
762 u32 delay_usec, bad_frames = 20;
764 delay_usec = DIV_ROUND_UP_ULL((u64)USEC_PER_SEC *
765 input_fi->numerator * bad_frames,
766 input_fi->denominator);
768 usleep_range(delay_usec, delay_usec + 1000);
771 if (priv->dest == IPU_CSI_DEST_IDMAC) {
772 ret = csi_idmac_start(priv);
777 ret = csi_setup(priv);
781 /* start the frame interval monitor */
782 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
783 imx_media_fim_set_stream(priv->fim, output_fi, true);
785 ret = ipu_csi_enable(priv->csi);
787 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
794 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
795 imx_media_fim_set_stream(priv->fim, NULL, false);
797 if (priv->dest == IPU_CSI_DEST_IDMAC)
798 csi_idmac_stop(priv);
800 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
804 static void csi_stop(struct csi_priv *priv)
806 if (priv->dest == IPU_CSI_DEST_IDMAC)
807 csi_idmac_wait_last_eof(priv);
810 * Disable the CSI asap, after syncing with the last EOF.
811 * Doing so after the IDMA channel is disabled has shown to
812 * create hard system-wide hangs.
814 ipu_csi_disable(priv->csi);
817 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
819 if (priv->dest == IPU_CSI_DEST_IDMAC) {
820 csi_idmac_stop(priv);
822 /* stop the frame interval monitor */
824 imx_media_fim_set_stream(priv->fim, NULL, false);
828 static const struct csi_skip_desc csi_skip[12] = {
829 { 1, 1, 0x00 }, /* Keep all frames */
830 { 5, 6, 0x10 }, /* Skip every sixth frame */
831 { 4, 5, 0x08 }, /* Skip every fifth frame */
832 { 3, 4, 0x04 }, /* Skip every fourth frame */
833 { 2, 3, 0x02 }, /* Skip every third frame */
834 { 3, 5, 0x0a }, /* Skip frames 1 and 3 of every 5 */
835 { 1, 2, 0x01 }, /* Skip every second frame */
836 { 2, 5, 0x0b }, /* Keep frames 1 and 4 of every 5 */
837 { 1, 3, 0x03 }, /* Keep one in three frames */
838 { 1, 4, 0x07 }, /* Keep one in four frames */
839 { 1, 5, 0x0f }, /* Keep one in five frames */
840 { 1, 6, 0x1f }, /* Keep one in six frames */
843 static void csi_apply_skip_interval(const struct csi_skip_desc *skip,
844 struct v4l2_fract *interval)
848 interval->numerator *= skip->max_ratio;
849 interval->denominator *= skip->keep;
851 /* Reduce fraction to lowest terms */
852 div = gcd(interval->numerator, interval->denominator);
854 interval->numerator /= div;
855 interval->denominator /= div;
860 * Find the skip pattern to produce the output frame interval closest to the
861 * requested one, for the given input frame interval. Updates the output frame
862 * interval to the exact value.
864 static const struct csi_skip_desc *csi_find_best_skip(struct v4l2_fract *in,
865 struct v4l2_fract *out)
867 const struct csi_skip_desc *skip = &csi_skip[0], *best_skip = skip;
868 u32 min_err = UINT_MAX;
872 /* Default to 1:1 ratio */
873 if (out->numerator == 0 || out->denominator == 0 ||
874 in->numerator == 0 || in->denominator == 0) {
879 want_us = div_u64((u64)USEC_PER_SEC * out->numerator, out->denominator);
881 /* Find the reduction closest to the requested time per frame */
882 for (i = 0; i < ARRAY_SIZE(csi_skip); i++, skip++) {
885 tmp = div_u64((u64)USEC_PER_SEC * in->numerator *
886 skip->max_ratio, in->denominator * skip->keep);
888 err = abs((s64)tmp - want_us);
896 csi_apply_skip_interval(best_skip, out);
902 * V4L2 subdev operations.
905 static int csi_get_frame_interval(struct v4l2_subdev *sd,
906 struct v4l2_subdev_state *sd_state,
907 struct v4l2_subdev_frame_interval *fi)
909 struct csi_priv *priv = v4l2_get_subdevdata(sd);
912 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
913 * subdev active state API.
915 if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
918 if (fi->pad >= CSI_NUM_PADS)
921 mutex_lock(&priv->lock);
923 fi->interval = priv->frame_interval[fi->pad];
925 mutex_unlock(&priv->lock);
930 static int csi_set_frame_interval(struct v4l2_subdev *sd,
931 struct v4l2_subdev_state *sd_state,
932 struct v4l2_subdev_frame_interval *fi)
934 struct csi_priv *priv = v4l2_get_subdevdata(sd);
935 struct v4l2_fract *input_fi;
939 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
940 * subdev active state API.
942 if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
945 mutex_lock(&priv->lock);
947 input_fi = &priv->frame_interval[CSI_SINK_PAD];
951 /* No limits on valid input frame intervals */
952 if (fi->interval.numerator == 0 ||
953 fi->interval.denominator == 0)
954 fi->interval = *input_fi;
955 /* Reset output intervals and frame skipping ratio to 1:1 */
956 priv->frame_interval[CSI_SRC_PAD_IDMAC] = fi->interval;
957 priv->frame_interval[CSI_SRC_PAD_DIRECT] = fi->interval;
958 priv->skip = &csi_skip[0];
960 case CSI_SRC_PAD_IDMAC:
962 * frame interval at IDMAC output pad depends on input
963 * interval, modified by frame skipping.
965 priv->skip = csi_find_best_skip(input_fi, &fi->interval);
967 case CSI_SRC_PAD_DIRECT:
969 * frame interval at DIRECT output pad is same as input
972 fi->interval = *input_fi;
979 priv->frame_interval[fi->pad] = fi->interval;
981 mutex_unlock(&priv->lock);
985 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
987 struct csi_priv *priv = v4l2_get_subdevdata(sd);
990 mutex_lock(&priv->lock);
992 if (!priv->src_sd || !priv->sink) {
998 * enable/disable streaming only if stream_count is
999 * going from 0 to 1 / 1 to 0.
1001 if (priv->stream_count != !enable)
1005 dev_dbg(priv->dev, "stream ON\n");
1006 ret = csi_start(priv);
1010 dev_dbg(priv->dev, "stream OFF\n");
1015 priv->stream_count += enable ? 1 : -1;
1016 if (priv->stream_count < 0)
1017 priv->stream_count = 0;
1019 mutex_unlock(&priv->lock);
1023 static int csi_link_setup(struct media_entity *entity,
1024 const struct media_pad *local,
1025 const struct media_pad *remote, u32 flags)
1027 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1028 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1029 struct v4l2_subdev *remote_sd;
1032 dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
1033 local->entity->name);
1035 mutex_lock(&priv->lock);
1037 if (local->flags & MEDIA_PAD_FL_SINK) {
1038 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1043 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1045 if (flags & MEDIA_LNK_FL_ENABLED) {
1050 priv->src_sd = remote_sd;
1052 priv->src_sd = NULL;
1058 /* this is a source pad */
1060 if (flags & MEDIA_LNK_FL_ENABLED) {
1066 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1067 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1069 /* do not apply IC burst alignment in csi_try_crop */
1070 priv->active_output_pad = CSI_SRC_PAD_IDMAC;
1074 /* record which output pad is now active */
1075 priv->active_output_pad = local->index;
1077 /* set CSI destination */
1078 if (local->index == CSI_SRC_PAD_IDMAC) {
1079 if (!is_media_entity_v4l2_video_device(remote->entity)) {
1085 ret = imx_media_fim_add_controls(priv->fim);
1090 priv->dest = IPU_CSI_DEST_IDMAC;
1092 if (!is_media_entity_v4l2_subdev(remote->entity)) {
1097 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
1098 switch (remote_sd->grp_id) {
1099 case IMX_MEDIA_GRP_ID_IPU_VDIC:
1100 priv->dest = IPU_CSI_DEST_VDIC;
1102 case IMX_MEDIA_GRP_ID_IPU_IC_PRP:
1103 priv->dest = IPU_CSI_DEST_IC;
1111 priv->sink = remote->entity;
1113 mutex_unlock(&priv->lock);
1117 static int csi_link_validate(struct v4l2_subdev *sd,
1118 struct media_link *link,
1119 struct v4l2_subdev_format *source_fmt,
1120 struct v4l2_subdev_format *sink_fmt)
1122 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1123 struct v4l2_mbus_config mbus_cfg = { .type = 0 };
1127 ret = v4l2_subdev_link_validate_default(sd, link,
1128 source_fmt, sink_fmt);
1132 ret = csi_get_upstream_mbus_config(priv, &mbus_cfg);
1135 "failed to get upstream media bus configuration\n");
1139 mutex_lock(&priv->lock);
1141 priv->mbus_cfg = mbus_cfg;
1142 is_csi2 = !is_parallel_bus(&mbus_cfg);
1145 * NOTE! It seems the virtual channels from the mipi csi-2
1146 * receiver are used only for routing by the video mux's,
1147 * or for hard-wired routing to the CSI's. Once the stream
1148 * enters the CSI's however, they are treated internally
1149 * in the IPU as virtual channel 0.
1151 ipu_csi_set_mipi_datatype(priv->csi, 0,
1152 &priv->format_mbus[CSI_SINK_PAD]);
1155 /* select either parallel or MIPI-CSI2 as input to CSI */
1156 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
1158 mutex_unlock(&priv->lock);
1162 static struct v4l2_mbus_framefmt *
1163 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_state *sd_state,
1164 unsigned int pad, enum v4l2_subdev_format_whence which)
1166 if (which == V4L2_SUBDEV_FORMAT_TRY)
1167 return v4l2_subdev_state_get_format(sd_state, pad);
1169 return &priv->format_mbus[pad];
1172 static struct v4l2_rect *
1173 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_state *sd_state,
1174 enum v4l2_subdev_format_whence which)
1176 if (which == V4L2_SUBDEV_FORMAT_TRY)
1177 return v4l2_subdev_state_get_crop(sd_state, CSI_SINK_PAD);
1182 static struct v4l2_rect *
1183 __csi_get_compose(struct csi_priv *priv, struct v4l2_subdev_state *sd_state,
1184 enum v4l2_subdev_format_whence which)
1186 if (which == V4L2_SUBDEV_FORMAT_TRY)
1187 return v4l2_subdev_state_get_compose(sd_state, CSI_SINK_PAD);
1189 return &priv->compose;
1192 static void csi_try_crop(struct csi_priv *priv,
1193 struct v4l2_rect *crop,
1194 struct v4l2_subdev_state *sd_state,
1195 struct v4l2_mbus_framefmt *infmt,
1196 struct v4l2_mbus_config *mbus_cfg)
1200 crop->width = min_t(__u32, infmt->width, crop->width);
1201 if (crop->left + crop->width > infmt->width)
1202 crop->left = infmt->width - crop->width;
1203 /* adjust crop left/width to h/w alignment restrictions */
1205 if (priv->active_output_pad == CSI_SRC_PAD_DIRECT)
1206 crop->width &= ~0x7; /* multiple of 8 pixels (IC burst) */
1208 crop->width &= ~0x1; /* multiple of 2 pixels */
1210 in_height = infmt->height;
1211 if (infmt->field == V4L2_FIELD_ALTERNATE)
1215 * FIXME: not sure why yet, but on interlaced bt.656,
1216 * changing the vertical cropping causes loss of vertical
1217 * sync, so fix it to NTSC/PAL active lines. NTSC contains
1218 * 2 extra lines of active video that need to be cropped.
1220 if (mbus_cfg->type == V4L2_MBUS_BT656 &&
1221 (V4L2_FIELD_HAS_BOTH(infmt->field) ||
1222 infmt->field == V4L2_FIELD_ALTERNATE)) {
1223 crop->height = in_height;
1224 crop->top = (in_height == 480) ? 2 : 0;
1226 crop->height = min_t(__u32, in_height, crop->height);
1227 if (crop->top + crop->height > in_height)
1228 crop->top = in_height - crop->height;
1232 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
1233 struct v4l2_subdev_state *sd_state,
1234 struct v4l2_subdev_mbus_code_enum *code)
1236 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1237 struct v4l2_mbus_config mbus_cfg = { .type = 0 };
1238 const struct imx_media_pixfmt *incc;
1239 struct v4l2_mbus_framefmt *infmt;
1242 mutex_lock(&priv->lock);
1244 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, code->which);
1245 incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY);
1247 switch (code->pad) {
1249 ret = imx_media_enum_mbus_formats(&code->code, code->index,
1252 case CSI_SRC_PAD_DIRECT:
1253 case CSI_SRC_PAD_IDMAC:
1254 ret = csi_get_upstream_mbus_config(priv, &mbus_cfg);
1257 "failed to get upstream media bus configuration\n");
1261 if (requires_passthrough(&mbus_cfg, infmt, incc)) {
1262 if (code->index != 0) {
1266 code->code = infmt->code;
1268 enum imx_pixfmt_sel fmt_sel =
1269 (incc->cs == IPUV3_COLORSPACE_YUV) ?
1270 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB;
1272 ret = imx_media_enum_ipu_formats(&code->code,
1282 mutex_unlock(&priv->lock);
1286 static int csi_enum_frame_size(struct v4l2_subdev *sd,
1287 struct v4l2_subdev_state *sd_state,
1288 struct v4l2_subdev_frame_size_enum *fse)
1290 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1291 struct v4l2_rect *crop;
1294 if (fse->pad >= CSI_NUM_PADS ||
1295 fse->index > (fse->pad == CSI_SINK_PAD ? 0 : 3))
1298 mutex_lock(&priv->lock);
1300 if (fse->pad == CSI_SINK_PAD) {
1301 fse->min_width = MIN_W;
1302 fse->max_width = MAX_W;
1303 fse->min_height = MIN_H;
1304 fse->max_height = MAX_H;
1306 crop = __csi_get_crop(priv, sd_state, fse->which);
1308 fse->min_width = fse->index & 1 ?
1309 crop->width / 2 : crop->width;
1310 fse->max_width = fse->min_width;
1311 fse->min_height = fse->index & 2 ?
1312 crop->height / 2 : crop->height;
1313 fse->max_height = fse->min_height;
1316 mutex_unlock(&priv->lock);
1320 static int csi_enum_frame_interval(struct v4l2_subdev *sd,
1321 struct v4l2_subdev_state *sd_state,
1322 struct v4l2_subdev_frame_interval_enum *fie)
1324 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1325 struct v4l2_fract *input_fi;
1326 struct v4l2_rect *crop;
1329 if (fie->pad >= CSI_NUM_PADS ||
1330 fie->index >= (fie->pad != CSI_SRC_PAD_IDMAC ?
1331 1 : ARRAY_SIZE(csi_skip)))
1334 mutex_lock(&priv->lock);
1336 input_fi = &priv->frame_interval[CSI_SINK_PAD];
1337 crop = __csi_get_crop(priv, sd_state, fie->which);
1339 if ((fie->width != crop->width && fie->width != crop->width / 2) ||
1340 (fie->height != crop->height && fie->height != crop->height / 2)) {
1345 fie->interval = *input_fi;
1347 if (fie->pad == CSI_SRC_PAD_IDMAC)
1348 csi_apply_skip_interval(&csi_skip[fie->index],
1352 mutex_unlock(&priv->lock);
1356 static int csi_get_fmt(struct v4l2_subdev *sd,
1357 struct v4l2_subdev_state *sd_state,
1358 struct v4l2_subdev_format *sdformat)
1360 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1361 struct v4l2_mbus_framefmt *fmt;
1364 if (sdformat->pad >= CSI_NUM_PADS)
1367 mutex_lock(&priv->lock);
1369 fmt = __csi_get_fmt(priv, sd_state, sdformat->pad, sdformat->which);
1375 sdformat->format = *fmt;
1377 mutex_unlock(&priv->lock);
1381 static void csi_try_field(struct csi_priv *priv,
1382 struct v4l2_subdev_state *sd_state,
1383 struct v4l2_subdev_format *sdformat)
1385 struct v4l2_mbus_framefmt *infmt =
1386 __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sdformat->which);
1389 * no restrictions on sink pad field type except must
1392 if (sdformat->pad == CSI_SINK_PAD) {
1393 if (sdformat->format.field == V4L2_FIELD_ANY)
1394 sdformat->format.field = V4L2_FIELD_NONE;
1398 switch (infmt->field) {
1399 case V4L2_FIELD_SEQ_TB:
1400 case V4L2_FIELD_SEQ_BT:
1402 * If the user requests sequential at the source pad,
1403 * allow it (along with possibly inverting field order).
1404 * Otherwise passthrough the field type.
1406 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field))
1407 sdformat->format.field = infmt->field;
1409 case V4L2_FIELD_ALTERNATE:
1411 * This driver does not support alternate field mode, and
1412 * the CSI captures a whole frame, so the CSI never presents
1413 * alternate mode at its source pads. If user has not
1414 * already requested sequential, translate ALTERNATE at
1415 * sink pad to SEQ_TB or SEQ_BT at the source pad depending
1416 * on input height (assume NTSC BT order if 480 total active
1417 * frame lines, otherwise PAL TB order).
1419 if (!V4L2_FIELD_IS_SEQUENTIAL(sdformat->format.field))
1420 sdformat->format.field = (infmt->height == 480 / 2) ?
1421 V4L2_FIELD_SEQ_BT : V4L2_FIELD_SEQ_TB;
1424 /* Passthrough for all other input field types */
1425 sdformat->format.field = infmt->field;
1430 static void csi_try_fmt(struct csi_priv *priv,
1431 struct v4l2_mbus_config *mbus_cfg,
1432 struct v4l2_subdev_state *sd_state,
1433 struct v4l2_subdev_format *sdformat,
1434 struct v4l2_rect *crop,
1435 struct v4l2_rect *compose,
1436 const struct imx_media_pixfmt **cc)
1438 const struct imx_media_pixfmt *incc;
1439 struct v4l2_mbus_framefmt *infmt;
1442 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sdformat->which);
1444 switch (sdformat->pad) {
1445 case CSI_SRC_PAD_DIRECT:
1446 case CSI_SRC_PAD_IDMAC:
1447 incc = imx_media_find_mbus_format(infmt->code, PIXFMT_SEL_ANY);
1449 sdformat->format.width = compose->width;
1450 sdformat->format.height = compose->height;
1452 if (requires_passthrough(mbus_cfg, infmt, incc)) {
1453 sdformat->format.code = infmt->code;
1456 enum imx_pixfmt_sel fmt_sel =
1457 (incc->cs == IPUV3_COLORSPACE_YUV) ?
1458 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB;
1460 *cc = imx_media_find_ipu_format(sdformat->format.code,
1463 imx_media_enum_ipu_formats(&code, 0, fmt_sel);
1464 *cc = imx_media_find_ipu_format(code, fmt_sel);
1465 sdformat->format.code = (*cc)->codes[0];
1469 csi_try_field(priv, sd_state, sdformat);
1471 /* propagate colorimetry from sink */
1472 sdformat->format.colorspace = infmt->colorspace;
1473 sdformat->format.xfer_func = infmt->xfer_func;
1474 sdformat->format.quantization = infmt->quantization;
1475 sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
1479 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1480 W_ALIGN, &sdformat->format.height,
1481 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1483 *cc = imx_media_find_mbus_format(sdformat->format.code,
1486 imx_media_enum_mbus_formats(&code, 0,
1487 PIXFMT_SEL_YUV_RGB);
1488 *cc = imx_media_find_mbus_format(code,
1489 PIXFMT_SEL_YUV_RGB);
1490 sdformat->format.code = (*cc)->codes[0];
1493 csi_try_field(priv, sd_state, sdformat);
1495 /* Reset crop and compose rectangles */
1498 crop->width = sdformat->format.width;
1499 crop->height = sdformat->format.height;
1500 if (sdformat->format.field == V4L2_FIELD_ALTERNATE)
1502 csi_try_crop(priv, crop, sd_state, &sdformat->format, mbus_cfg);
1505 compose->width = crop->width;
1506 compose->height = crop->height;
1511 imx_media_try_colorimetry(&sdformat->format,
1512 priv->active_output_pad == CSI_SRC_PAD_DIRECT);
1515 static int csi_set_fmt(struct v4l2_subdev *sd,
1516 struct v4l2_subdev_state *sd_state,
1517 struct v4l2_subdev_format *sdformat)
1519 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1520 struct v4l2_mbus_config mbus_cfg = { .type = 0 };
1521 const struct imx_media_pixfmt *cc;
1522 struct v4l2_mbus_framefmt *fmt;
1523 struct v4l2_rect *crop, *compose;
1526 if (sdformat->pad >= CSI_NUM_PADS)
1529 ret = csi_get_upstream_mbus_config(priv, &mbus_cfg);
1532 "failed to get upstream media bus configuration\n");
1536 mutex_lock(&priv->lock);
1538 if (priv->stream_count > 0) {
1543 crop = __csi_get_crop(priv, sd_state, sdformat->which);
1544 compose = __csi_get_compose(priv, sd_state, sdformat->which);
1546 csi_try_fmt(priv, &mbus_cfg, sd_state, sdformat, crop, compose, &cc);
1548 fmt = __csi_get_fmt(priv, sd_state, sdformat->pad, sdformat->which);
1549 *fmt = sdformat->format;
1551 if (sdformat->pad == CSI_SINK_PAD) {
1554 /* propagate format to source pads */
1555 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1556 const struct imx_media_pixfmt *outcc;
1557 struct v4l2_mbus_framefmt *outfmt;
1558 struct v4l2_subdev_format format;
1561 format.which = sdformat->which;
1562 format.format = sdformat->format;
1563 csi_try_fmt(priv, &mbus_cfg, sd_state, &format, NULL,
1566 outfmt = __csi_get_fmt(priv, sd_state, pad,
1568 *outfmt = format.format;
1570 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1571 priv->cc[pad] = outcc;
1575 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1576 priv->cc[sdformat->pad] = cc;
1579 mutex_unlock(&priv->lock);
1583 static int csi_get_selection(struct v4l2_subdev *sd,
1584 struct v4l2_subdev_state *sd_state,
1585 struct v4l2_subdev_selection *sel)
1587 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1588 struct v4l2_mbus_framefmt *infmt;
1589 struct v4l2_rect *crop, *compose;
1592 if (sel->pad != CSI_SINK_PAD)
1595 mutex_lock(&priv->lock);
1597 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sel->which);
1598 crop = __csi_get_crop(priv, sd_state, sel->which);
1599 compose = __csi_get_compose(priv, sd_state, sel->which);
1601 switch (sel->target) {
1602 case V4L2_SEL_TGT_CROP_BOUNDS:
1605 sel->r.width = infmt->width;
1606 sel->r.height = infmt->height;
1607 if (infmt->field == V4L2_FIELD_ALTERNATE)
1610 case V4L2_SEL_TGT_CROP:
1613 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1616 sel->r.width = crop->width;
1617 sel->r.height = crop->height;
1619 case V4L2_SEL_TGT_COMPOSE:
1626 mutex_unlock(&priv->lock);
1630 static int csi_set_scale(u32 *compose, u32 crop, u32 flags)
1632 if ((flags & (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE)) ==
1633 (V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE) &&
1634 *compose != crop && *compose != crop / 2)
1637 if (*compose <= crop / 2 ||
1638 (*compose < crop * 3 / 4 && !(flags & V4L2_SEL_FLAG_GE)) ||
1639 (*compose < crop && (flags & V4L2_SEL_FLAG_LE)))
1640 *compose = crop / 2;
1647 static int csi_set_selection(struct v4l2_subdev *sd,
1648 struct v4l2_subdev_state *sd_state,
1649 struct v4l2_subdev_selection *sel)
1651 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1652 struct v4l2_mbus_config mbus_cfg = { .type = 0 };
1653 struct v4l2_mbus_framefmt *infmt;
1654 struct v4l2_rect *crop, *compose;
1657 if (sel->pad != CSI_SINK_PAD)
1660 ret = csi_get_upstream_mbus_config(priv, &mbus_cfg);
1663 "failed to get upstream media bus configuration\n");
1667 mutex_lock(&priv->lock);
1669 if (priv->stream_count > 0) {
1674 infmt = __csi_get_fmt(priv, sd_state, CSI_SINK_PAD, sel->which);
1675 crop = __csi_get_crop(priv, sd_state, sel->which);
1676 compose = __csi_get_compose(priv, sd_state, sel->which);
1678 switch (sel->target) {
1679 case V4L2_SEL_TGT_CROP:
1681 * Modifying the crop rectangle always changes the format on
1682 * the source pads. If the KEEP_CONFIG flag is set, just return
1683 * the current crop rectangle.
1685 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1686 sel->r = priv->crop;
1687 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1692 csi_try_crop(priv, &sel->r, sd_state, infmt, &mbus_cfg);
1696 /* Reset scaling to 1:1 */
1697 compose->width = crop->width;
1698 compose->height = crop->height;
1700 case V4L2_SEL_TGT_COMPOSE:
1702 * Modifying the compose rectangle always changes the format on
1703 * the source pads. If the KEEP_CONFIG flag is set, just return
1704 * the current compose rectangle.
1706 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1707 sel->r = priv->compose;
1708 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1715 ret = csi_set_scale(&sel->r.width, crop->width, sel->flags);
1718 ret = csi_set_scale(&sel->r.height, crop->height, sel->flags);
1729 /* Reset source pads to sink compose rectangle */
1730 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1731 struct v4l2_mbus_framefmt *outfmt;
1733 outfmt = __csi_get_fmt(priv, sd_state, pad, sel->which);
1734 outfmt->width = compose->width;
1735 outfmt->height = compose->height;
1739 mutex_unlock(&priv->lock);
1743 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1744 struct v4l2_event_subscription *sub)
1746 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1751 return v4l2_event_subscribe(fh, sub, 0, NULL);
1754 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1755 struct v4l2_event_subscription *sub)
1757 return v4l2_event_unsubscribe(fh, sub);
1760 static int csi_registered(struct v4l2_subdev *sd)
1762 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1763 struct ipu_csi *csi;
1767 /* get handle to IPU CSI */
1768 csi = ipu_csi_get(priv->ipu, priv->csi_id);
1770 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1771 return PTR_ERR(csi);
1775 for (i = 0; i < CSI_NUM_PADS; i++) {
1777 if (i != CSI_SINK_PAD)
1778 imx_media_enum_ipu_formats(&code, 0, PIXFMT_SEL_YUV);
1780 /* set a default mbus format */
1781 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1782 IMX_MEDIA_DEF_PIX_WIDTH,
1783 IMX_MEDIA_DEF_PIX_HEIGHT, code,
1784 V4L2_FIELD_NONE, &priv->cc[i]);
1788 /* init default frame interval */
1789 priv->frame_interval[i].numerator = 1;
1790 priv->frame_interval[i].denominator = 30;
1793 /* disable frame skipping */
1794 priv->skip = &csi_skip[0];
1796 /* init default crop and compose rectangle sizes */
1797 priv->crop.width = IMX_MEDIA_DEF_PIX_WIDTH;
1798 priv->crop.height = IMX_MEDIA_DEF_PIX_HEIGHT;
1799 priv->compose.width = IMX_MEDIA_DEF_PIX_WIDTH;
1800 priv->compose.height = IMX_MEDIA_DEF_PIX_HEIGHT;
1802 priv->fim = imx_media_fim_init(&priv->sd);
1803 if (IS_ERR(priv->fim)) {
1804 ret = PTR_ERR(priv->fim);
1808 priv->vdev = imx_media_capture_device_init(priv->sd.dev, &priv->sd,
1809 CSI_SRC_PAD_IDMAC, true);
1810 if (IS_ERR(priv->vdev)) {
1811 ret = PTR_ERR(priv->vdev);
1815 ret = imx_media_capture_device_register(priv->vdev, 0);
1822 imx_media_capture_device_remove(priv->vdev);
1825 imx_media_fim_free(priv->fim);
1827 ipu_csi_put(priv->csi);
1831 static void csi_unregistered(struct v4l2_subdev *sd)
1833 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1835 imx_media_capture_device_unregister(priv->vdev);
1836 imx_media_capture_device_remove(priv->vdev);
1839 imx_media_fim_free(priv->fim);
1842 ipu_csi_put(priv->csi);
1846 * The CSI has only one fwnode endpoint, at the sink pad. Verify the
1847 * endpoint belongs to us, and return CSI_SINK_PAD.
1849 static int csi_get_fwnode_pad(struct media_entity *entity,
1850 struct fwnode_endpoint *endpoint)
1852 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1853 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1854 struct fwnode_handle *csi_port = dev_fwnode(priv->dev);
1855 struct fwnode_handle *csi_ep;
1858 csi_ep = fwnode_get_next_child_node(csi_port, NULL);
1860 ret = endpoint->local_fwnode == csi_ep ? CSI_SINK_PAD : -ENXIO;
1862 fwnode_handle_put(csi_ep);
1867 static const struct media_entity_operations csi_entity_ops = {
1868 .link_setup = csi_link_setup,
1869 .link_validate = v4l2_subdev_link_validate,
1870 .get_fwnode_pad = csi_get_fwnode_pad,
1873 static const struct v4l2_subdev_core_ops csi_core_ops = {
1874 .subscribe_event = csi_subscribe_event,
1875 .unsubscribe_event = csi_unsubscribe_event,
1878 static const struct v4l2_subdev_video_ops csi_video_ops = {
1879 .s_stream = csi_s_stream,
1882 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1883 .enum_mbus_code = csi_enum_mbus_code,
1884 .enum_frame_size = csi_enum_frame_size,
1885 .enum_frame_interval = csi_enum_frame_interval,
1886 .get_fmt = csi_get_fmt,
1887 .set_fmt = csi_set_fmt,
1888 .get_selection = csi_get_selection,
1889 .set_selection = csi_set_selection,
1890 .get_frame_interval = csi_get_frame_interval,
1891 .set_frame_interval = csi_set_frame_interval,
1892 .link_validate = csi_link_validate,
1895 static const struct v4l2_subdev_ops csi_subdev_ops = {
1896 .core = &csi_core_ops,
1897 .video = &csi_video_ops,
1898 .pad = &csi_pad_ops,
1901 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1902 .init_state = imx_media_init_state,
1903 .registered = csi_registered,
1904 .unregistered = csi_unregistered,
1907 static int imx_csi_notify_bound(struct v4l2_async_notifier *notifier,
1908 struct v4l2_subdev *sd,
1909 struct v4l2_async_connection *asd)
1911 struct csi_priv *priv = notifier_to_dev(notifier);
1912 struct media_pad *sink = &priv->sd.entity.pads[CSI_SINK_PAD];
1915 * If the subdev is a video mux, it must be one of the CSI
1916 * muxes. Mark it as such via its group id.
1918 if (sd->entity.function == MEDIA_ENT_F_VID_MUX)
1919 sd->grp_id = IMX_MEDIA_GRP_ID_CSI_MUX;
1921 return v4l2_create_fwnode_links_to_pad(sd, sink, 0);
1924 static const struct v4l2_async_notifier_operations csi_notify_ops = {
1925 .bound = imx_csi_notify_bound,
1928 static int imx_csi_async_register(struct csi_priv *priv)
1930 struct v4l2_async_connection *asd = NULL;
1931 struct fwnode_handle *ep;
1935 v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd);
1937 /* get this CSI's port id */
1938 ret = fwnode_property_read_u32(dev_fwnode(priv->dev), "reg", &port);
1942 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(priv->dev->parent),
1944 FWNODE_GRAPH_ENDPOINT_NEXT);
1946 asd = v4l2_async_nf_add_fwnode_remote(&priv->notifier, ep,
1947 struct v4l2_async_connection);
1949 fwnode_handle_put(ep);
1953 /* OK if asd already exists */
1959 priv->notifier.ops = &csi_notify_ops;
1961 ret = v4l2_async_nf_register(&priv->notifier);
1965 return v4l2_async_register_subdev(&priv->sd);
1968 static int imx_csi_probe(struct platform_device *pdev)
1970 struct ipu_client_platformdata *pdata;
1971 struct pinctrl *pinctrl;
1972 struct csi_priv *priv;
1975 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1979 platform_set_drvdata(pdev, &priv->sd);
1980 priv->dev = &pdev->dev;
1982 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1986 /* get parent IPU */
1987 priv->ipu = dev_get_drvdata(priv->dev->parent);
1989 /* get our CSI id */
1990 pdata = priv->dev->platform_data;
1991 priv->csi_id = pdata->csi;
1992 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1994 priv->active_output_pad = CSI_SRC_PAD_IDMAC;
1996 timer_setup(&priv->eof_timeout_timer, csi_idmac_eof_timeout, 0);
1997 spin_lock_init(&priv->irqlock);
1999 v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
2000 v4l2_set_subdevdata(&priv->sd, priv);
2001 priv->sd.internal_ops = &csi_internal_ops;
2002 priv->sd.entity.ops = &csi_entity_ops;
2003 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
2004 priv->sd.dev = &pdev->dev;
2005 priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
2006 priv->sd.owner = THIS_MODULE;
2007 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
2008 priv->sd.grp_id = priv->csi_id ?
2009 IMX_MEDIA_GRP_ID_IPU_CSI1 : IMX_MEDIA_GRP_ID_IPU_CSI0;
2010 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
2011 priv->sd.grp_id, ipu_get_num(priv->ipu));
2013 for (i = 0; i < CSI_NUM_PADS; i++)
2014 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
2015 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
2017 ret = media_entity_pads_init(&priv->sd.entity, CSI_NUM_PADS,
2022 mutex_init(&priv->lock);
2024 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
2025 priv->sd.ctrl_handler = &priv->ctrl_hdlr;
2028 * The IPUv3 driver did not assign an of_node to this
2029 * device. As a result, pinctrl does not automatically
2030 * configure our pin groups, so we need to do that manually
2031 * here, after setting this device's of_node.
2033 priv->dev->of_node = pdata->of_node;
2034 pinctrl = devm_pinctrl_get_select_default(priv->dev);
2035 if (IS_ERR(pinctrl)) {
2036 ret = PTR_ERR(pinctrl);
2038 "devm_pinctrl_get_select_default() failed: %d\n", ret);
2043 ret = imx_csi_async_register(priv);
2050 v4l2_async_nf_unregister(&priv->notifier);
2051 v4l2_async_nf_cleanup(&priv->notifier);
2053 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
2054 mutex_destroy(&priv->lock);
2058 static void imx_csi_remove(struct platform_device *pdev)
2060 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
2061 struct csi_priv *priv = sd_to_dev(sd);
2063 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
2064 mutex_destroy(&priv->lock);
2065 v4l2_async_nf_unregister(&priv->notifier);
2066 v4l2_async_nf_cleanup(&priv->notifier);
2067 v4l2_async_unregister_subdev(sd);
2068 media_entity_cleanup(&sd->entity);
2071 static const struct platform_device_id imx_csi_ids[] = {
2072 { .name = "imx-ipuv3-csi" },
2075 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
2077 static struct platform_driver imx_csi_driver = {
2078 .probe = imx_csi_probe,
2079 .remove_new = imx_csi_remove,
2080 .id_table = imx_csi_ids,
2082 .name = "imx-ipuv3-csi",
2085 module_platform_driver(imx_csi_driver);
2087 MODULE_DESCRIPTION("i.MX CSI subdev driver");
2088 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
2089 MODULE_LICENSE("GPL");