2 * V4L2 Capture CSI Subdev for Freescale i.MX5/6 SOC
4 * Copyright (c) 2014-2017 Mentor Graphics Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/pinctrl/consumer.h>
15 #include <linux/platform_device.h>
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-event.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-mc.h>
21 #include <media/v4l2-subdev.h>
22 #include <media/videobuf2-dma-contig.h>
23 #include <video/imx-ipu-v3.h>
24 #include <media/imx.h>
25 #include "imx-media.h"
28 * Min/Max supported width and heights.
30 * We allow planar output, so we have to align width by 16 pixels
31 * to meet IDMAC alignment requirements.
33 * TODO: move this into pad format negotiation, if capture device
34 * has not requested planar formats, we should allow 8 pixel
41 #define W_ALIGN 4 /* multiple of 16 pixels */
42 #define H_ALIGN 1 /* multiple of 2 lines */
43 #define S_ALIGN 1 /* multiple of 2 */
48 struct imx_media_dev *md;
49 struct v4l2_subdev sd;
50 struct media_pad pad[CSI_NUM_PADS];
51 /* the video device at IDMAC output pad */
52 struct imx_media_video_dev *vdev;
53 struct imx_media_fim *fim;
57 /* lock to protect all members below */
60 int active_output_pad;
62 struct ipuv3_channel *idmac_ch;
63 struct ipu_smfc *smfc;
66 struct v4l2_mbus_framefmt format_mbus[CSI_NUM_PADS];
67 const struct imx_media_pixfmt *cc[CSI_NUM_PADS];
68 struct v4l2_fract frame_interval;
69 struct v4l2_rect crop;
71 /* active vb2 buffers to send to video dev sink */
72 struct imx_media_buffer *active_vb2_buf[2];
73 struct imx_media_dma_buf underrun_buf;
75 int ipu_buf_num; /* ipu double buffer index: 0-1 */
77 /* the sink for the captured frames */
78 struct media_entity *sink;
79 enum ipu_csi_dest dest;
80 /* the source subdev */
81 struct v4l2_subdev *src_sd;
83 /* the mipi virtual channel number at link validate */
86 /* the attached sensor at stream on */
87 struct imx_media_subdev *sensor;
89 spinlock_t irqlock; /* protect eof_irq handler */
90 struct timer_list eof_timeout_timer;
94 struct v4l2_ctrl_handler ctrl_hdlr;
96 int stream_count; /* streaming counter */
97 bool last_eof; /* waiting for last EOF at stream off */
98 bool nfb4eof; /* NFB4EOF encountered during streaming */
99 struct completion last_eof_comp;
102 static inline struct csi_priv *sd_to_dev(struct v4l2_subdev *sdev)
104 return container_of(sdev, struct csi_priv, sd);
107 static void csi_idmac_put_ipu_resources(struct csi_priv *priv)
109 if (!IS_ERR_OR_NULL(priv->idmac_ch))
110 ipu_idmac_put(priv->idmac_ch);
111 priv->idmac_ch = NULL;
113 if (!IS_ERR_OR_NULL(priv->smfc))
114 ipu_smfc_put(priv->smfc);
118 static int csi_idmac_get_ipu_resources(struct csi_priv *priv)
122 ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id;
124 priv->smfc = ipu_smfc_get(priv->ipu, ch_num);
125 if (IS_ERR(priv->smfc)) {
126 v4l2_err(&priv->sd, "failed to get SMFC\n");
127 ret = PTR_ERR(priv->smfc);
131 priv->idmac_ch = ipu_idmac_get(priv->ipu, ch_num);
132 if (IS_ERR(priv->idmac_ch)) {
133 v4l2_err(&priv->sd, "could not get IDMAC channel %u\n",
135 ret = PTR_ERR(priv->idmac_ch);
141 csi_idmac_put_ipu_resources(priv);
145 static void csi_vb2_buf_done(struct csi_priv *priv)
147 struct imx_media_video_dev *vdev = priv->vdev;
148 struct imx_media_buffer *done, *next;
149 struct vb2_buffer *vb;
152 done = priv->active_vb2_buf[priv->ipu_buf_num];
154 vb = &done->vbuf.vb2_buf;
155 vb->timestamp = ktime_get_ns();
156 vb2_buffer_done(vb, priv->nfb4eof ?
157 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
160 priv->nfb4eof = false;
162 /* get next queued buffer */
163 next = imx_media_capture_device_next_buf(vdev);
165 phys = vb2_dma_contig_plane_dma_addr(&next->vbuf.vb2_buf, 0);
166 priv->active_vb2_buf[priv->ipu_buf_num] = next;
168 phys = priv->underrun_buf.phys;
169 priv->active_vb2_buf[priv->ipu_buf_num] = NULL;
172 if (ipu_idmac_buffer_is_ready(priv->idmac_ch, priv->ipu_buf_num))
173 ipu_idmac_clear_buffer(priv->idmac_ch, priv->ipu_buf_num);
175 ipu_cpmem_set_buffer(priv->idmac_ch, priv->ipu_buf_num, phys);
178 static irqreturn_t csi_idmac_eof_interrupt(int irq, void *dev_id)
180 struct csi_priv *priv = dev_id;
182 spin_lock(&priv->irqlock);
184 if (priv->last_eof) {
185 complete(&priv->last_eof_comp);
186 priv->last_eof = false;
191 struct timespec cur_ts;
193 ktime_get_ts(&cur_ts);
194 /* call frame interval monitor */
195 imx_media_fim_eof_monitor(priv->fim, &cur_ts);
198 csi_vb2_buf_done(priv);
200 /* select new IPU buf */
201 ipu_idmac_select_buffer(priv->idmac_ch, priv->ipu_buf_num);
202 /* toggle IPU double-buffer index */
203 priv->ipu_buf_num ^= 1;
205 /* bump the EOF timeout timer */
206 mod_timer(&priv->eof_timeout_timer,
207 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
210 spin_unlock(&priv->irqlock);
214 static irqreturn_t csi_idmac_nfb4eof_interrupt(int irq, void *dev_id)
216 struct csi_priv *priv = dev_id;
218 spin_lock(&priv->irqlock);
221 * this is not an unrecoverable error, just mark
222 * the next captured frame with vb2 error flag.
224 priv->nfb4eof = true;
226 v4l2_err(&priv->sd, "NFB4EOF\n");
228 spin_unlock(&priv->irqlock);
234 * EOF timeout timer function. This is an unrecoverable condition
235 * without a stream restart.
237 static void csi_idmac_eof_timeout(unsigned long data)
239 struct csi_priv *priv = (struct csi_priv *)data;
240 struct imx_media_video_dev *vdev = priv->vdev;
242 v4l2_err(&priv->sd, "EOF timeout\n");
244 /* signal a fatal error to capture device */
245 imx_media_capture_device_error(vdev);
248 static void csi_idmac_setup_vb2_buf(struct csi_priv *priv, dma_addr_t *phys)
250 struct imx_media_video_dev *vdev = priv->vdev;
251 struct imx_media_buffer *buf;
254 for (i = 0; i < 2; i++) {
255 buf = imx_media_capture_device_next_buf(vdev);
257 priv->active_vb2_buf[i] = buf;
258 phys[i] = vb2_dma_contig_plane_dma_addr(
259 &buf->vbuf.vb2_buf, 0);
261 priv->active_vb2_buf[i] = NULL;
262 phys[i] = priv->underrun_buf.phys;
267 static void csi_idmac_unsetup_vb2_buf(struct csi_priv *priv,
268 enum vb2_buffer_state return_status)
270 struct imx_media_buffer *buf;
273 /* return any remaining active frames with return_status */
274 for (i = 0; i < 2; i++) {
275 buf = priv->active_vb2_buf[i];
277 struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
279 vb->timestamp = ktime_get_ns();
280 vb2_buffer_done(vb, return_status);
285 /* init the SMFC IDMAC channel */
286 static int csi_idmac_setup_channel(struct csi_priv *priv)
288 struct imx_media_video_dev *vdev = priv->vdev;
289 struct v4l2_fwnode_endpoint *sensor_ep;
290 struct v4l2_mbus_framefmt *infmt;
291 struct ipu_image image;
292 u32 passthrough_bits;
298 infmt = &priv->format_mbus[CSI_SINK_PAD];
299 sensor_ep = &priv->sensor->sensor_ep;
301 ipu_cpmem_zero(priv->idmac_ch);
303 memset(&image, 0, sizeof(image));
304 image.pix = vdev->fmt.fmt.pix;
305 image.rect.width = image.pix.width;
306 image.rect.height = image.pix.height;
308 csi_idmac_setup_vb2_buf(priv, phys);
310 image.phys0 = phys[0];
311 image.phys1 = phys[1];
314 * Check for conditions that require the IPU to handle the
315 * data internally as generic data, aka passthrough mode:
316 * - raw bayer formats
317 * - the sensor bus is 16-bit parallel
319 switch (image.pix.pixelformat) {
320 case V4L2_PIX_FMT_SBGGR8:
321 case V4L2_PIX_FMT_SGBRG8:
322 case V4L2_PIX_FMT_SGRBG8:
323 case V4L2_PIX_FMT_SRGGB8:
326 passthrough_bits = 8;
328 case V4L2_PIX_FMT_SBGGR16:
329 case V4L2_PIX_FMT_SGBRG16:
330 case V4L2_PIX_FMT_SGRBG16:
331 case V4L2_PIX_FMT_SRGGB16:
334 passthrough_bits = 16;
337 burst_size = (image.pix.width & 0xf) ? 8 : 16;
338 passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 &&
339 sensor_ep->bus.parallel.bus_width >= 16);
340 passthrough_bits = 16;
345 ipu_cpmem_set_resolution(priv->idmac_ch, image.rect.width,
347 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
348 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
349 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
350 ipu_cpmem_set_format_passthrough(priv->idmac_ch,
353 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
358 ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
361 * Set the channel for the direct CSI-->memory via SMFC
362 * use-case to very high priority, by enabling the watermark
363 * signal in the SMFC, enabling WM in the channel, and setting
364 * the channel priority to high.
366 * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority
369 * The WM's are set very low by intention here to ensure that
370 * the SMFC FIFOs do not overflow.
372 ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
373 ipu_cpmem_set_high_priority(priv->idmac_ch);
374 ipu_idmac_enable_watermark(priv->idmac_ch, true);
375 ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
377 burst_size = passthrough ?
378 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
380 ipu_smfc_set_burstsize(priv->smfc, burst_size);
382 if (image.pix.field == V4L2_FIELD_NONE &&
383 V4L2_FIELD_HAS_BOTH(infmt->field))
384 ipu_cpmem_interlaced_scan(priv->idmac_ch,
385 image.pix.bytesperline);
387 ipu_idmac_set_double_buffer(priv->idmac_ch, true);
392 csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
396 static void csi_idmac_unsetup(struct csi_priv *priv,
397 enum vb2_buffer_state state)
399 ipu_idmac_disable_channel(priv->idmac_ch);
400 ipu_smfc_disable(priv->smfc);
402 csi_idmac_unsetup_vb2_buf(priv, state);
405 static int csi_idmac_setup(struct csi_priv *priv)
409 ret = csi_idmac_setup_channel(priv);
413 ipu_cpmem_dump(priv->idmac_ch);
416 ipu_smfc_enable(priv->smfc);
418 /* set buffers ready */
419 ipu_idmac_select_buffer(priv->idmac_ch, 0);
420 ipu_idmac_select_buffer(priv->idmac_ch, 1);
422 /* enable the channels */
423 ipu_idmac_enable_channel(priv->idmac_ch);
428 static int csi_idmac_start(struct csi_priv *priv)
430 struct imx_media_video_dev *vdev = priv->vdev;
431 struct v4l2_pix_format *outfmt;
434 ret = csi_idmac_get_ipu_resources(priv);
438 ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
440 outfmt = &vdev->fmt.fmt.pix;
442 ret = imx_media_alloc_dma_buf(priv->md, &priv->underrun_buf,
447 priv->ipu_buf_num = 0;
449 /* init EOF completion waitq */
450 init_completion(&priv->last_eof_comp);
451 priv->last_eof = false;
452 priv->nfb4eof = false;
454 ret = csi_idmac_setup(priv);
456 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
457 goto out_free_dma_buf;
460 priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
463 ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
464 csi_idmac_nfb4eof_interrupt, 0,
465 "imx-smfc-nfb4eof", priv);
468 "Error registering NFB4EOF irq: %d\n", ret);
472 priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
475 ret = devm_request_irq(priv->dev, priv->eof_irq,
476 csi_idmac_eof_interrupt, 0,
477 "imx-smfc-eof", priv);
480 "Error registering eof irq: %d\n", ret);
481 goto out_free_nfb4eof_irq;
484 /* start the EOF timeout timer */
485 mod_timer(&priv->eof_timeout_timer,
486 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
490 out_free_nfb4eof_irq:
491 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
493 csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
495 imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
497 csi_idmac_put_ipu_resources(priv);
501 static void csi_idmac_stop(struct csi_priv *priv)
506 /* mark next EOF interrupt as the last before stream off */
507 spin_lock_irqsave(&priv->irqlock, flags);
508 priv->last_eof = true;
509 spin_unlock_irqrestore(&priv->irqlock, flags);
512 * and then wait for interrupt handler to mark completion.
514 ret = wait_for_completion_timeout(
515 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
517 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
519 devm_free_irq(priv->dev, priv->eof_irq, priv);
520 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
522 csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
524 imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
526 /* cancel the EOF timeout timer */
527 del_timer_sync(&priv->eof_timeout_timer);
529 csi_idmac_put_ipu_resources(priv);
532 /* Update the CSI whole sensor and active windows */
533 static int csi_setup(struct csi_priv *priv)
535 struct v4l2_mbus_framefmt *infmt, *outfmt;
536 struct v4l2_mbus_config sensor_mbus_cfg;
537 struct v4l2_fwnode_endpoint *sensor_ep;
538 struct v4l2_mbus_framefmt if_fmt;
540 infmt = &priv->format_mbus[CSI_SINK_PAD];
541 outfmt = &priv->format_mbus[priv->active_output_pad];
542 sensor_ep = &priv->sensor->sensor_ep;
544 /* compose mbus_config from sensor endpoint */
545 sensor_mbus_cfg.type = sensor_ep->bus_type;
546 sensor_mbus_cfg.flags = (sensor_ep->bus_type == V4L2_MBUS_CSI2) ?
547 sensor_ep->bus.mipi_csi2.flags :
548 sensor_ep->bus.parallel.flags;
551 * we need to pass input sensor frame to CSI interface, but
552 * with translated field type from output format
555 if_fmt.field = outfmt->field;
557 ipu_csi_set_window(priv->csi, &priv->crop);
559 ipu_csi_set_downsize(priv->csi,
560 priv->crop.width == 2 * outfmt->width,
561 priv->crop.height == 2 * outfmt->height);
563 ipu_csi_init_interface(priv->csi, &sensor_mbus_cfg, &if_fmt);
565 ipu_csi_set_dest(priv->csi, priv->dest);
567 ipu_csi_dump(priv->csi);
572 static int csi_start(struct csi_priv *priv)
578 v4l2_err(&priv->sd, "no sensor attached\n");
582 ret = v4l2_subdev_call(priv->sensor->sd, sensor,
583 g_skip_frames, &bad_frames);
584 if (!ret && bad_frames) {
585 struct v4l2_fract *fi = &priv->frame_interval;
589 * This sensor has bad frames when it is turned on,
590 * add a delay to avoid them before enabling the CSI
591 * hardware. Especially for sensors with a bt.656 interface,
592 * any shifts in the SAV/EAV sync codes will cause the CSI
593 * to lose vert/horiz sync.
595 delay_usec = DIV_ROUND_UP_ULL(
596 (u64)USEC_PER_SEC * fi->numerator * bad_frames,
598 usleep_range(delay_usec, delay_usec + 1000);
601 if (priv->dest == IPU_CSI_DEST_IDMAC) {
602 ret = csi_idmac_start(priv);
607 ret = csi_setup(priv);
611 /* start the frame interval monitor */
612 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) {
613 ret = imx_media_fim_set_stream(priv->fim, &priv->frame_interval,
619 ret = ipu_csi_enable(priv->csi);
621 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
628 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
629 imx_media_fim_set_stream(priv->fim, &priv->frame_interval,
632 if (priv->dest == IPU_CSI_DEST_IDMAC)
633 csi_idmac_stop(priv);
637 static void csi_stop(struct csi_priv *priv)
639 if (priv->dest == IPU_CSI_DEST_IDMAC) {
640 csi_idmac_stop(priv);
642 /* stop the frame interval monitor */
644 imx_media_fim_set_stream(priv->fim,
645 &priv->frame_interval,
649 ipu_csi_disable(priv->csi);
653 * V4L2 subdev operations.
656 static int csi_g_frame_interval(struct v4l2_subdev *sd,
657 struct v4l2_subdev_frame_interval *fi)
659 struct csi_priv *priv = v4l2_get_subdevdata(sd);
661 mutex_lock(&priv->lock);
662 fi->interval = priv->frame_interval;
663 mutex_unlock(&priv->lock);
668 static int csi_s_frame_interval(struct v4l2_subdev *sd,
669 struct v4l2_subdev_frame_interval *fi)
671 struct csi_priv *priv = v4l2_get_subdevdata(sd);
673 mutex_lock(&priv->lock);
675 /* Output pads mirror active input pad, no limits on input pads */
676 if (fi->pad == CSI_SRC_PAD_IDMAC || fi->pad == CSI_SRC_PAD_DIRECT)
677 fi->interval = priv->frame_interval;
679 priv->frame_interval = fi->interval;
681 mutex_unlock(&priv->lock);
686 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
688 struct csi_priv *priv = v4l2_get_subdevdata(sd);
691 mutex_lock(&priv->lock);
693 if (!priv->src_sd || !priv->sink) {
699 * enable/disable streaming only if stream_count is
700 * going from 0 to 1 / 1 to 0.
702 if (priv->stream_count != !enable)
706 /* upstream must be started first, before starting CSI */
707 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
708 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
712 dev_dbg(priv->dev, "stream ON\n");
713 ret = csi_start(priv);
715 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
719 dev_dbg(priv->dev, "stream OFF\n");
720 /* CSI must be stopped first, then stop upstream */
722 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
726 priv->stream_count += enable ? 1 : -1;
727 WARN_ON(priv->stream_count < 0);
729 mutex_unlock(&priv->lock);
733 static int csi_link_setup(struct media_entity *entity,
734 const struct media_pad *local,
735 const struct media_pad *remote, u32 flags)
737 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
738 struct csi_priv *priv = v4l2_get_subdevdata(sd);
739 struct v4l2_subdev *remote_sd;
742 dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
743 local->entity->name);
745 mutex_lock(&priv->lock);
747 if (local->flags & MEDIA_PAD_FL_SINK) {
748 if (!is_media_entity_v4l2_subdev(remote->entity)) {
753 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
755 if (flags & MEDIA_LNK_FL_ENABLED) {
760 priv->src_sd = remote_sd;
768 /* this is a source pad */
770 if (flags & MEDIA_LNK_FL_ENABLED) {
776 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
777 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
782 /* record which output pad is now active */
783 priv->active_output_pad = local->index;
785 /* set CSI destination */
786 if (local->index == CSI_SRC_PAD_IDMAC) {
787 if (!is_media_entity_v4l2_video_device(remote->entity)) {
793 ret = imx_media_fim_add_controls(priv->fim);
798 priv->dest = IPU_CSI_DEST_IDMAC;
800 if (!is_media_entity_v4l2_subdev(remote->entity)) {
805 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
806 switch (remote_sd->grp_id) {
807 case IMX_MEDIA_GRP_ID_VDIC:
808 priv->dest = IPU_CSI_DEST_VDIC;
810 case IMX_MEDIA_GRP_ID_IC_PRP:
811 priv->dest = IPU_CSI_DEST_IC;
819 priv->sink = remote->entity;
821 mutex_unlock(&priv->lock);
825 static int csi_link_validate(struct v4l2_subdev *sd,
826 struct media_link *link,
827 struct v4l2_subdev_format *source_fmt,
828 struct v4l2_subdev_format *sink_fmt)
830 struct csi_priv *priv = v4l2_get_subdevdata(sd);
831 struct v4l2_fwnode_endpoint *sensor_ep;
832 const struct imx_media_pixfmt *incc;
833 struct imx_media_subdev *sensor;
837 ret = v4l2_subdev_link_validate_default(sd, link,
838 source_fmt, sink_fmt);
842 sensor = __imx_media_find_sensor(priv->md, &priv->sd.entity);
843 if (IS_ERR(sensor)) {
844 v4l2_err(&priv->sd, "no sensor attached\n");
845 return PTR_ERR(priv->sensor);
848 mutex_lock(&priv->lock);
850 priv->sensor = sensor;
851 sensor_ep = &priv->sensor->sensor_ep;
852 is_csi2 = (sensor_ep->bus_type == V4L2_MBUS_CSI2);
853 incc = priv->cc[CSI_SINK_PAD];
855 if (priv->dest != IPU_CSI_DEST_IDMAC &&
856 (incc->bayer || (!is_csi2 &&
857 sensor_ep->bus.parallel.bus_width >= 16))) {
859 "bayer/16-bit parallel buses must go to IDMAC pad\n");
867 * NOTE! It seems the virtual channels from the mipi csi-2
868 * receiver are used only for routing by the video mux's,
869 * or for hard-wired routing to the CSI's. Once the stream
870 * enters the CSI's however, they are treated internally
871 * in the IPU as virtual channel 0.
874 mutex_unlock(&priv->lock);
875 vc_num = imx_media_find_mipi_csi2_channel(priv->md,
879 mutex_lock(&priv->lock);
881 ipu_csi_set_mipi_datatype(priv->csi, vc_num,
882 &priv->format_mbus[CSI_SINK_PAD]);
885 /* select either parallel or MIPI-CSI2 as input to CSI */
886 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
888 mutex_unlock(&priv->lock);
892 static struct v4l2_mbus_framefmt *
893 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
894 unsigned int pad, enum v4l2_subdev_format_whence which)
896 if (which == V4L2_SUBDEV_FORMAT_TRY)
897 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad);
899 return &priv->format_mbus[pad];
902 static struct v4l2_rect *
903 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
904 enum v4l2_subdev_format_whence which)
906 if (which == V4L2_SUBDEV_FORMAT_TRY)
907 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD);
912 static void csi_try_crop(struct csi_priv *priv,
913 struct v4l2_rect *crop,
914 struct v4l2_subdev_pad_config *cfg,
915 struct v4l2_mbus_framefmt *infmt,
916 struct imx_media_subdev *sensor)
918 struct v4l2_fwnode_endpoint *sensor_ep;
920 sensor_ep = &sensor->sensor_ep;
922 crop->width = min_t(__u32, infmt->width, crop->width);
923 if (crop->left + crop->width > infmt->width)
924 crop->left = infmt->width - crop->width;
925 /* adjust crop left/width to h/w alignment restrictions */
930 * FIXME: not sure why yet, but on interlaced bt.656,
931 * changing the vertical cropping causes loss of vertical
932 * sync, so fix it to NTSC/PAL active lines. NTSC contains
933 * 2 extra lines of active video that need to be cropped.
935 if (sensor_ep->bus_type == V4L2_MBUS_BT656 &&
936 (V4L2_FIELD_HAS_BOTH(infmt->field) ||
937 infmt->field == V4L2_FIELD_ALTERNATE)) {
938 crop->height = infmt->height;
939 crop->top = (infmt->height == 480) ? 2 : 0;
941 crop->height = min_t(__u32, infmt->height, crop->height);
942 if (crop->top + crop->height > infmt->height)
943 crop->top = infmt->height - crop->height;
947 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
948 struct v4l2_subdev_pad_config *cfg,
949 struct v4l2_subdev_mbus_code_enum *code)
951 struct csi_priv *priv = v4l2_get_subdevdata(sd);
952 const struct imx_media_pixfmt *incc;
953 struct v4l2_mbus_framefmt *infmt;
956 mutex_lock(&priv->lock);
958 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which);
959 incc = imx_media_find_mbus_format(infmt->code, CS_SEL_ANY, true);
963 ret = imx_media_enum_mbus_format(&code->code, code->index,
966 case CSI_SRC_PAD_DIRECT:
967 case CSI_SRC_PAD_IDMAC:
969 if (code->index != 0) {
973 code->code = infmt->code;
975 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
976 CS_SEL_YUV : CS_SEL_RGB;
977 ret = imx_media_enum_ipu_format(&code->code,
987 mutex_unlock(&priv->lock);
991 static int csi_get_fmt(struct v4l2_subdev *sd,
992 struct v4l2_subdev_pad_config *cfg,
993 struct v4l2_subdev_format *sdformat)
995 struct csi_priv *priv = v4l2_get_subdevdata(sd);
996 struct v4l2_mbus_framefmt *fmt;
999 if (sdformat->pad >= CSI_NUM_PADS)
1002 mutex_lock(&priv->lock);
1004 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1010 sdformat->format = *fmt;
1012 mutex_unlock(&priv->lock);
1016 static void csi_try_fmt(struct csi_priv *priv,
1017 struct imx_media_subdev *sensor,
1018 struct v4l2_subdev_pad_config *cfg,
1019 struct v4l2_subdev_format *sdformat,
1020 struct v4l2_rect *crop,
1021 const struct imx_media_pixfmt **cc)
1023 const struct imx_media_pixfmt *incc;
1024 struct v4l2_mbus_framefmt *infmt;
1027 switch (sdformat->pad) {
1028 case CSI_SRC_PAD_DIRECT:
1029 case CSI_SRC_PAD_IDMAC:
1030 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD,
1032 incc = imx_media_find_mbus_format(infmt->code,
1035 if (sdformat->format.width < crop->width * 3 / 4)
1036 sdformat->format.width = crop->width / 2;
1038 sdformat->format.width = crop->width;
1040 if (sdformat->format.height < crop->height * 3 / 4)
1041 sdformat->format.height = crop->height / 2;
1043 sdformat->format.height = crop->height;
1046 sdformat->format.code = infmt->code;
1049 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1050 CS_SEL_YUV : CS_SEL_RGB;
1052 *cc = imx_media_find_ipu_format(sdformat->format.code,
1055 imx_media_enum_ipu_format(&code, 0, cs_sel);
1056 *cc = imx_media_find_ipu_format(code, cs_sel);
1057 sdformat->format.code = (*cc)->codes[0];
1061 if (sdformat->pad == CSI_SRC_PAD_DIRECT ||
1062 sdformat->format.field != V4L2_FIELD_NONE)
1063 sdformat->format.field = infmt->field;
1066 * translate V4L2_FIELD_ALTERNATE to SEQ_TB or SEQ_BT
1067 * depending on input height (assume NTSC top-bottom
1068 * order if 480 lines, otherwise PAL bottom-top order).
1070 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) {
1071 sdformat->format.field = (infmt->height == 480) ?
1072 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1076 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1077 W_ALIGN, &sdformat->format.height,
1078 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1081 crop->width = sdformat->format.width;
1082 crop->height = sdformat->format.height;
1083 csi_try_crop(priv, crop, cfg, &sdformat->format, sensor);
1085 *cc = imx_media_find_mbus_format(sdformat->format.code,
1088 imx_media_enum_mbus_format(&code, 0,
1090 *cc = imx_media_find_mbus_format(code,
1092 sdformat->format.code = (*cc)->codes[0];
1098 static int csi_set_fmt(struct v4l2_subdev *sd,
1099 struct v4l2_subdev_pad_config *cfg,
1100 struct v4l2_subdev_format *sdformat)
1102 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1103 struct imx_media_video_dev *vdev = priv->vdev;
1104 const struct imx_media_pixfmt *cc;
1105 struct imx_media_subdev *sensor;
1106 struct v4l2_pix_format vdev_fmt;
1107 struct v4l2_mbus_framefmt *fmt;
1108 struct v4l2_rect *crop;
1111 if (sdformat->pad >= CSI_NUM_PADS)
1114 sensor = imx_media_find_sensor(priv->md, &priv->sd.entity);
1115 if (IS_ERR(sensor)) {
1116 v4l2_err(&priv->sd, "no sensor attached\n");
1117 return PTR_ERR(sensor);
1120 mutex_lock(&priv->lock);
1122 if (priv->stream_count > 0) {
1127 crop = __csi_get_crop(priv, cfg, sdformat->which);
1129 csi_try_fmt(priv, sensor, cfg, sdformat, crop, &cc);
1131 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1132 *fmt = sdformat->format;
1134 if (sdformat->pad == CSI_SINK_PAD) {
1137 /* propagate format to source pads */
1138 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1139 const struct imx_media_pixfmt *outcc;
1140 struct v4l2_mbus_framefmt *outfmt;
1141 struct v4l2_subdev_format format;
1144 format.which = sdformat->which;
1145 format.format = sdformat->format;
1146 csi_try_fmt(priv, sensor, cfg, &format, crop, &outcc);
1148 outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which);
1149 *outfmt = format.format;
1151 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1152 priv->cc[pad] = outcc;
1156 if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY)
1159 priv->cc[sdformat->pad] = cc;
1161 /* propagate IDMAC output pad format to capture device */
1162 imx_media_mbus_fmt_to_pix_fmt(&vdev_fmt,
1163 &priv->format_mbus[CSI_SRC_PAD_IDMAC],
1164 priv->cc[CSI_SRC_PAD_IDMAC]);
1165 mutex_unlock(&priv->lock);
1166 imx_media_capture_device_set_format(vdev, &vdev_fmt);
1170 mutex_unlock(&priv->lock);
1174 static int csi_get_selection(struct v4l2_subdev *sd,
1175 struct v4l2_subdev_pad_config *cfg,
1176 struct v4l2_subdev_selection *sel)
1178 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1179 struct v4l2_mbus_framefmt *infmt;
1180 struct v4l2_rect *crop;
1183 if (sel->pad >= CSI_NUM_PADS || sel->pad == CSI_SINK_PAD)
1186 mutex_lock(&priv->lock);
1188 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1189 crop = __csi_get_crop(priv, cfg, sel->which);
1191 switch (sel->target) {
1192 case V4L2_SEL_TGT_CROP_BOUNDS:
1195 sel->r.width = infmt->width;
1196 sel->r.height = infmt->height;
1198 case V4L2_SEL_TGT_CROP:
1205 mutex_unlock(&priv->lock);
1209 static int csi_set_selection(struct v4l2_subdev *sd,
1210 struct v4l2_subdev_pad_config *cfg,
1211 struct v4l2_subdev_selection *sel)
1213 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1214 struct v4l2_mbus_framefmt *infmt;
1215 struct imx_media_subdev *sensor;
1216 struct v4l2_rect *crop;
1219 if (sel->pad >= CSI_NUM_PADS ||
1220 sel->pad == CSI_SINK_PAD ||
1221 sel->target != V4L2_SEL_TGT_CROP)
1224 sensor = imx_media_find_sensor(priv->md, &priv->sd.entity);
1225 if (IS_ERR(sensor)) {
1226 v4l2_err(&priv->sd, "no sensor attached\n");
1227 return PTR_ERR(sensor);
1230 mutex_lock(&priv->lock);
1232 if (priv->stream_count > 0) {
1237 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1238 crop = __csi_get_crop(priv, cfg, sel->which);
1241 * Modifying the crop rectangle always changes the format on the source
1242 * pad. If the KEEP_CONFIG flag is set, just return the current crop
1245 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1246 sel->r = priv->crop;
1247 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1252 csi_try_crop(priv, &sel->r, cfg, infmt, sensor);
1256 /* Update the source pad formats */
1257 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1258 struct v4l2_mbus_framefmt *outfmt;
1260 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which);
1261 outfmt->width = crop->width;
1262 outfmt->height = crop->height;
1266 mutex_unlock(&priv->lock);
1270 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1271 struct v4l2_event_subscription *sub)
1273 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1278 return v4l2_event_subscribe(fh, sub, 0, NULL);
1281 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1282 struct v4l2_event_subscription *sub)
1284 return v4l2_event_unsubscribe(fh, sub);
1288 * retrieve our pads parsed from the OF graph by the media device
1290 static int csi_registered(struct v4l2_subdev *sd)
1292 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1296 /* get media device */
1297 priv->md = dev_get_drvdata(sd->v4l2_dev->dev);
1299 /* get handle to IPU CSI */
1300 priv->csi = ipu_csi_get(priv->ipu, priv->csi_id);
1301 if (IS_ERR(priv->csi)) {
1302 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1303 return PTR_ERR(priv->csi);
1306 for (i = 0; i < CSI_NUM_PADS; i++) {
1307 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
1308 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1311 if (i != CSI_SINK_PAD)
1312 imx_media_enum_ipu_format(&code, 0, CS_SEL_YUV);
1314 /* set a default mbus format */
1315 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1316 640, 480, code, V4L2_FIELD_NONE,
1322 /* init default frame interval */
1323 priv->frame_interval.numerator = 1;
1324 priv->frame_interval.denominator = 30;
1326 priv->fim = imx_media_fim_init(&priv->sd);
1327 if (IS_ERR(priv->fim)) {
1328 ret = PTR_ERR(priv->fim);
1332 ret = media_entity_pads_init(&sd->entity, CSI_NUM_PADS, priv->pad);
1336 ret = imx_media_capture_device_register(priv->vdev);
1340 ret = imx_media_add_video_device(priv->md, priv->vdev);
1346 imx_media_capture_device_unregister(priv->vdev);
1349 imx_media_fim_free(priv->fim);
1351 ipu_csi_put(priv->csi);
1355 static void csi_unregistered(struct v4l2_subdev *sd)
1357 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1359 imx_media_capture_device_unregister(priv->vdev);
1362 imx_media_fim_free(priv->fim);
1364 if (!IS_ERR_OR_NULL(priv->csi))
1365 ipu_csi_put(priv->csi);
1368 static const struct media_entity_operations csi_entity_ops = {
1369 .link_setup = csi_link_setup,
1370 .link_validate = v4l2_subdev_link_validate,
1373 static const struct v4l2_subdev_core_ops csi_core_ops = {
1374 .subscribe_event = csi_subscribe_event,
1375 .unsubscribe_event = csi_unsubscribe_event,
1378 static const struct v4l2_subdev_video_ops csi_video_ops = {
1379 .g_frame_interval = csi_g_frame_interval,
1380 .s_frame_interval = csi_s_frame_interval,
1381 .s_stream = csi_s_stream,
1384 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1385 .enum_mbus_code = csi_enum_mbus_code,
1386 .get_fmt = csi_get_fmt,
1387 .set_fmt = csi_set_fmt,
1388 .get_selection = csi_get_selection,
1389 .set_selection = csi_set_selection,
1390 .link_validate = csi_link_validate,
1393 static const struct v4l2_subdev_ops csi_subdev_ops = {
1394 .core = &csi_core_ops,
1395 .video = &csi_video_ops,
1396 .pad = &csi_pad_ops,
1399 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1400 .registered = csi_registered,
1401 .unregistered = csi_unregistered,
1404 static int imx_csi_probe(struct platform_device *pdev)
1406 struct ipu_client_platformdata *pdata;
1407 struct pinctrl *pinctrl;
1408 struct csi_priv *priv;
1411 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1415 platform_set_drvdata(pdev, &priv->sd);
1416 priv->dev = &pdev->dev;
1418 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1422 /* get parent IPU */
1423 priv->ipu = dev_get_drvdata(priv->dev->parent);
1425 /* get our CSI id */
1426 pdata = priv->dev->platform_data;
1427 priv->csi_id = pdata->csi;
1428 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1430 init_timer(&priv->eof_timeout_timer);
1431 priv->eof_timeout_timer.data = (unsigned long)priv;
1432 priv->eof_timeout_timer.function = csi_idmac_eof_timeout;
1433 spin_lock_init(&priv->irqlock);
1435 v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1436 v4l2_set_subdevdata(&priv->sd, priv);
1437 priv->sd.internal_ops = &csi_internal_ops;
1438 priv->sd.entity.ops = &csi_entity_ops;
1439 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1440 priv->sd.dev = &pdev->dev;
1441 priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
1442 priv->sd.owner = THIS_MODULE;
1443 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1444 priv->sd.grp_id = priv->csi_id ?
1445 IMX_MEDIA_GRP_ID_CSI1 : IMX_MEDIA_GRP_ID_CSI0;
1446 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
1447 priv->sd.grp_id, ipu_get_num(priv->ipu));
1449 priv->vdev = imx_media_capture_device_init(&priv->sd,
1451 if (IS_ERR(priv->vdev))
1452 return PTR_ERR(priv->vdev);
1454 mutex_init(&priv->lock);
1456 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1457 priv->sd.ctrl_handler = &priv->ctrl_hdlr;
1460 * The IPUv3 driver did not assign an of_node to this
1461 * device. As a result, pinctrl does not automatically
1462 * configure our pin groups, so we need to do that manually
1463 * here, after setting this device's of_node.
1465 priv->dev->of_node = pdata->of_node;
1466 pinctrl = devm_pinctrl_get_select_default(priv->dev);
1468 ret = v4l2_async_register_subdev(&priv->sd);
1474 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1475 mutex_destroy(&priv->lock);
1476 imx_media_capture_device_remove(priv->vdev);
1480 static int imx_csi_remove(struct platform_device *pdev)
1482 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1483 struct csi_priv *priv = sd_to_dev(sd);
1485 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1486 mutex_destroy(&priv->lock);
1487 imx_media_capture_device_remove(priv->vdev);
1488 v4l2_async_unregister_subdev(sd);
1489 media_entity_cleanup(&sd->entity);
1494 static const struct platform_device_id imx_csi_ids[] = {
1495 { .name = "imx-ipuv3-csi" },
1498 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
1500 static struct platform_driver imx_csi_driver = {
1501 .probe = imx_csi_probe,
1502 .remove = imx_csi_remove,
1503 .id_table = imx_csi_ids,
1505 .name = "imx-ipuv3-csi",
1508 module_platform_driver(imx_csi_driver);
1510 MODULE_DESCRIPTION("i.MX CSI subdev driver");
1511 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
1512 MODULE_LICENSE("GPL");
1513 MODULE_ALIAS("platform:imx-ipuv3-csi");