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;
336 case V4L2_PIX_FMT_YUV420:
337 case V4L2_PIX_FMT_NV12:
338 burst_size = (image.pix.width & 0x3f) ?
339 ((image.pix.width & 0x1f) ?
340 ((image.pix.width & 0xf) ? 8 : 16) : 32) : 64;
341 passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 &&
342 sensor_ep->bus.parallel.bus_width >= 16);
343 passthrough_bits = 16;
345 case V4L2_PIX_FMT_YUYV:
346 case V4L2_PIX_FMT_UYVY:
347 burst_size = (image.pix.width & 0x1f) ?
348 ((image.pix.width & 0xf) ? 8 : 16) : 32;
349 passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 &&
350 sensor_ep->bus.parallel.bus_width >= 16);
351 passthrough_bits = 16;
354 burst_size = (image.pix.width & 0xf) ? 8 : 16;
355 passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 &&
356 sensor_ep->bus.parallel.bus_width >= 16);
357 passthrough_bits = 16;
362 ipu_cpmem_set_resolution(priv->idmac_ch, image.rect.width,
364 ipu_cpmem_set_stride(priv->idmac_ch, image.pix.bytesperline);
365 ipu_cpmem_set_buffer(priv->idmac_ch, 0, image.phys0);
366 ipu_cpmem_set_buffer(priv->idmac_ch, 1, image.phys1);
367 ipu_cpmem_set_format_passthrough(priv->idmac_ch,
370 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
375 ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
378 * Set the channel for the direct CSI-->memory via SMFC
379 * use-case to very high priority, by enabling the watermark
380 * signal in the SMFC, enabling WM in the channel, and setting
381 * the channel priority to high.
383 * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority
386 * The WM's are set very low by intention here to ensure that
387 * the SMFC FIFOs do not overflow.
389 ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
390 ipu_cpmem_set_high_priority(priv->idmac_ch);
391 ipu_idmac_enable_watermark(priv->idmac_ch, true);
392 ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
394 burst_size = passthrough ?
395 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
397 ipu_smfc_set_burstsize(priv->smfc, burst_size);
399 if (image.pix.field == V4L2_FIELD_NONE &&
400 V4L2_FIELD_HAS_BOTH(infmt->field))
401 ipu_cpmem_interlaced_scan(priv->idmac_ch,
402 image.pix.bytesperline);
404 ipu_idmac_set_double_buffer(priv->idmac_ch, true);
409 csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
413 static void csi_idmac_unsetup(struct csi_priv *priv,
414 enum vb2_buffer_state state)
416 ipu_idmac_disable_channel(priv->idmac_ch);
417 ipu_smfc_disable(priv->smfc);
419 csi_idmac_unsetup_vb2_buf(priv, state);
422 static int csi_idmac_setup(struct csi_priv *priv)
426 ret = csi_idmac_setup_channel(priv);
430 ipu_cpmem_dump(priv->idmac_ch);
433 ipu_smfc_enable(priv->smfc);
435 /* set buffers ready */
436 ipu_idmac_select_buffer(priv->idmac_ch, 0);
437 ipu_idmac_select_buffer(priv->idmac_ch, 1);
439 /* enable the channels */
440 ipu_idmac_enable_channel(priv->idmac_ch);
445 static int csi_idmac_start(struct csi_priv *priv)
447 struct imx_media_video_dev *vdev = priv->vdev;
448 struct v4l2_pix_format *outfmt;
451 ret = csi_idmac_get_ipu_resources(priv);
455 ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
457 outfmt = &vdev->fmt.fmt.pix;
459 ret = imx_media_alloc_dma_buf(priv->md, &priv->underrun_buf,
464 priv->ipu_buf_num = 0;
466 /* init EOF completion waitq */
467 init_completion(&priv->last_eof_comp);
468 priv->last_eof = false;
469 priv->nfb4eof = false;
471 ret = csi_idmac_setup(priv);
473 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
474 goto out_free_dma_buf;
477 priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
480 ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
481 csi_idmac_nfb4eof_interrupt, 0,
482 "imx-smfc-nfb4eof", priv);
485 "Error registering NFB4EOF irq: %d\n", ret);
489 priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
492 ret = devm_request_irq(priv->dev, priv->eof_irq,
493 csi_idmac_eof_interrupt, 0,
494 "imx-smfc-eof", priv);
497 "Error registering eof irq: %d\n", ret);
498 goto out_free_nfb4eof_irq;
501 /* start the EOF timeout timer */
502 mod_timer(&priv->eof_timeout_timer,
503 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
507 out_free_nfb4eof_irq:
508 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
510 csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
512 imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
514 csi_idmac_put_ipu_resources(priv);
518 static void csi_idmac_stop(struct csi_priv *priv)
523 /* mark next EOF interrupt as the last before stream off */
524 spin_lock_irqsave(&priv->irqlock, flags);
525 priv->last_eof = true;
526 spin_unlock_irqrestore(&priv->irqlock, flags);
529 * and then wait for interrupt handler to mark completion.
531 ret = wait_for_completion_timeout(
532 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
534 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
536 devm_free_irq(priv->dev, priv->eof_irq, priv);
537 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
539 csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
541 imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
543 /* cancel the EOF timeout timer */
544 del_timer_sync(&priv->eof_timeout_timer);
546 csi_idmac_put_ipu_resources(priv);
549 /* Update the CSI whole sensor and active windows */
550 static int csi_setup(struct csi_priv *priv)
552 struct v4l2_mbus_framefmt *infmt, *outfmt;
553 struct v4l2_mbus_config sensor_mbus_cfg;
554 struct v4l2_fwnode_endpoint *sensor_ep;
555 struct v4l2_mbus_framefmt if_fmt;
557 infmt = &priv->format_mbus[CSI_SINK_PAD];
558 outfmt = &priv->format_mbus[priv->active_output_pad];
559 sensor_ep = &priv->sensor->sensor_ep;
561 /* compose mbus_config from sensor endpoint */
562 sensor_mbus_cfg.type = sensor_ep->bus_type;
563 sensor_mbus_cfg.flags = (sensor_ep->bus_type == V4L2_MBUS_CSI2) ?
564 sensor_ep->bus.mipi_csi2.flags :
565 sensor_ep->bus.parallel.flags;
568 * we need to pass input sensor frame to CSI interface, but
569 * with translated field type from output format
572 if_fmt.field = outfmt->field;
574 ipu_csi_set_window(priv->csi, &priv->crop);
576 ipu_csi_set_downsize(priv->csi,
577 priv->crop.width == 2 * outfmt->width,
578 priv->crop.height == 2 * outfmt->height);
580 ipu_csi_init_interface(priv->csi, &sensor_mbus_cfg, &if_fmt);
582 ipu_csi_set_dest(priv->csi, priv->dest);
584 ipu_csi_dump(priv->csi);
589 static int csi_start(struct csi_priv *priv)
595 v4l2_err(&priv->sd, "no sensor attached\n");
599 ret = v4l2_subdev_call(priv->sensor->sd, sensor,
600 g_skip_frames, &bad_frames);
601 if (!ret && bad_frames) {
602 struct v4l2_fract *fi = &priv->frame_interval;
606 * This sensor has bad frames when it is turned on,
607 * add a delay to avoid them before enabling the CSI
608 * hardware. Especially for sensors with a bt.656 interface,
609 * any shifts in the SAV/EAV sync codes will cause the CSI
610 * to lose vert/horiz sync.
612 delay_usec = DIV_ROUND_UP_ULL(
613 (u64)USEC_PER_SEC * fi->numerator * bad_frames,
615 usleep_range(delay_usec, delay_usec + 1000);
618 if (priv->dest == IPU_CSI_DEST_IDMAC) {
619 ret = csi_idmac_start(priv);
624 ret = csi_setup(priv);
628 /* start the frame interval monitor */
629 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) {
630 ret = imx_media_fim_set_stream(priv->fim, &priv->frame_interval,
636 ret = ipu_csi_enable(priv->csi);
638 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
645 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
646 imx_media_fim_set_stream(priv->fim, &priv->frame_interval,
649 if (priv->dest == IPU_CSI_DEST_IDMAC)
650 csi_idmac_stop(priv);
654 static void csi_stop(struct csi_priv *priv)
656 if (priv->dest == IPU_CSI_DEST_IDMAC) {
657 csi_idmac_stop(priv);
659 /* stop the frame interval monitor */
661 imx_media_fim_set_stream(priv->fim,
662 &priv->frame_interval,
666 ipu_csi_disable(priv->csi);
670 * V4L2 subdev operations.
673 static int csi_g_frame_interval(struct v4l2_subdev *sd,
674 struct v4l2_subdev_frame_interval *fi)
676 struct csi_priv *priv = v4l2_get_subdevdata(sd);
678 mutex_lock(&priv->lock);
679 fi->interval = priv->frame_interval;
680 mutex_unlock(&priv->lock);
685 static int csi_s_frame_interval(struct v4l2_subdev *sd,
686 struct v4l2_subdev_frame_interval *fi)
688 struct csi_priv *priv = v4l2_get_subdevdata(sd);
690 mutex_lock(&priv->lock);
692 /* Output pads mirror active input pad, no limits on input pads */
693 if (fi->pad == CSI_SRC_PAD_IDMAC || fi->pad == CSI_SRC_PAD_DIRECT)
694 fi->interval = priv->frame_interval;
696 priv->frame_interval = fi->interval;
698 mutex_unlock(&priv->lock);
703 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
705 struct csi_priv *priv = v4l2_get_subdevdata(sd);
708 mutex_lock(&priv->lock);
710 if (!priv->src_sd || !priv->sink) {
716 * enable/disable streaming only if stream_count is
717 * going from 0 to 1 / 1 to 0.
719 if (priv->stream_count != !enable)
723 /* upstream must be started first, before starting CSI */
724 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
725 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
729 dev_dbg(priv->dev, "stream ON\n");
730 ret = csi_start(priv);
732 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
736 dev_dbg(priv->dev, "stream OFF\n");
737 /* CSI must be stopped first, then stop upstream */
739 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
743 priv->stream_count += enable ? 1 : -1;
744 WARN_ON(priv->stream_count < 0);
746 mutex_unlock(&priv->lock);
750 static int csi_link_setup(struct media_entity *entity,
751 const struct media_pad *local,
752 const struct media_pad *remote, u32 flags)
754 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
755 struct csi_priv *priv = v4l2_get_subdevdata(sd);
756 struct v4l2_subdev *remote_sd;
759 dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
760 local->entity->name);
762 mutex_lock(&priv->lock);
764 if (local->flags & MEDIA_PAD_FL_SINK) {
765 if (!is_media_entity_v4l2_subdev(remote->entity)) {
770 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
772 if (flags & MEDIA_LNK_FL_ENABLED) {
777 priv->src_sd = remote_sd;
785 /* this is a source pad */
787 if (flags & MEDIA_LNK_FL_ENABLED) {
793 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
794 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
799 /* record which output pad is now active */
800 priv->active_output_pad = local->index;
802 /* set CSI destination */
803 if (local->index == CSI_SRC_PAD_IDMAC) {
804 if (!is_media_entity_v4l2_video_device(remote->entity)) {
810 ret = imx_media_fim_add_controls(priv->fim);
815 priv->dest = IPU_CSI_DEST_IDMAC;
817 if (!is_media_entity_v4l2_subdev(remote->entity)) {
822 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
823 switch (remote_sd->grp_id) {
824 case IMX_MEDIA_GRP_ID_VDIC:
825 priv->dest = IPU_CSI_DEST_VDIC;
827 case IMX_MEDIA_GRP_ID_IC_PRP:
828 priv->dest = IPU_CSI_DEST_IC;
836 priv->sink = remote->entity;
838 mutex_unlock(&priv->lock);
842 static int csi_link_validate(struct v4l2_subdev *sd,
843 struct media_link *link,
844 struct v4l2_subdev_format *source_fmt,
845 struct v4l2_subdev_format *sink_fmt)
847 struct csi_priv *priv = v4l2_get_subdevdata(sd);
848 struct v4l2_fwnode_endpoint *sensor_ep;
849 const struct imx_media_pixfmt *incc;
850 struct imx_media_subdev *sensor;
854 ret = v4l2_subdev_link_validate_default(sd, link,
855 source_fmt, sink_fmt);
859 sensor = __imx_media_find_sensor(priv->md, &priv->sd.entity);
860 if (IS_ERR(sensor)) {
861 v4l2_err(&priv->sd, "no sensor attached\n");
862 return PTR_ERR(priv->sensor);
865 mutex_lock(&priv->lock);
867 priv->sensor = sensor;
868 sensor_ep = &priv->sensor->sensor_ep;
869 is_csi2 = (sensor_ep->bus_type == V4L2_MBUS_CSI2);
870 incc = priv->cc[CSI_SINK_PAD];
872 if (priv->dest != IPU_CSI_DEST_IDMAC &&
873 (incc->bayer || (!is_csi2 &&
874 sensor_ep->bus.parallel.bus_width >= 16))) {
876 "bayer/16-bit parallel buses must go to IDMAC pad\n");
884 * NOTE! It seems the virtual channels from the mipi csi-2
885 * receiver are used only for routing by the video mux's,
886 * or for hard-wired routing to the CSI's. Once the stream
887 * enters the CSI's however, they are treated internally
888 * in the IPU as virtual channel 0.
891 mutex_unlock(&priv->lock);
892 vc_num = imx_media_find_mipi_csi2_channel(priv->md,
896 mutex_lock(&priv->lock);
898 ipu_csi_set_mipi_datatype(priv->csi, vc_num,
899 &priv->format_mbus[CSI_SINK_PAD]);
902 /* select either parallel or MIPI-CSI2 as input to CSI */
903 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
905 mutex_unlock(&priv->lock);
909 static struct v4l2_mbus_framefmt *
910 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
911 unsigned int pad, enum v4l2_subdev_format_whence which)
913 if (which == V4L2_SUBDEV_FORMAT_TRY)
914 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad);
916 return &priv->format_mbus[pad];
919 static struct v4l2_rect *
920 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
921 enum v4l2_subdev_format_whence which)
923 if (which == V4L2_SUBDEV_FORMAT_TRY)
924 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD);
929 static void csi_try_crop(struct csi_priv *priv,
930 struct v4l2_rect *crop,
931 struct v4l2_subdev_pad_config *cfg,
932 struct v4l2_mbus_framefmt *infmt,
933 struct imx_media_subdev *sensor)
935 struct v4l2_fwnode_endpoint *sensor_ep;
937 sensor_ep = &sensor->sensor_ep;
939 crop->width = min_t(__u32, infmt->width, crop->width);
940 if (crop->left + crop->width > infmt->width)
941 crop->left = infmt->width - crop->width;
942 /* adjust crop left/width to h/w alignment restrictions */
947 * FIXME: not sure why yet, but on interlaced bt.656,
948 * changing the vertical cropping causes loss of vertical
949 * sync, so fix it to NTSC/PAL active lines. NTSC contains
950 * 2 extra lines of active video that need to be cropped.
952 if (sensor_ep->bus_type == V4L2_MBUS_BT656 &&
953 (V4L2_FIELD_HAS_BOTH(infmt->field) ||
954 infmt->field == V4L2_FIELD_ALTERNATE)) {
955 crop->height = infmt->height;
956 crop->top = (infmt->height == 480) ? 2 : 0;
958 crop->height = min_t(__u32, infmt->height, crop->height);
959 if (crop->top + crop->height > infmt->height)
960 crop->top = infmt->height - crop->height;
964 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
965 struct v4l2_subdev_pad_config *cfg,
966 struct v4l2_subdev_mbus_code_enum *code)
968 struct csi_priv *priv = v4l2_get_subdevdata(sd);
969 const struct imx_media_pixfmt *incc;
970 struct v4l2_mbus_framefmt *infmt;
973 mutex_lock(&priv->lock);
975 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which);
976 incc = imx_media_find_mbus_format(infmt->code, CS_SEL_ANY, true);
980 ret = imx_media_enum_mbus_format(&code->code, code->index,
983 case CSI_SRC_PAD_DIRECT:
984 case CSI_SRC_PAD_IDMAC:
986 if (code->index != 0) {
990 code->code = infmt->code;
992 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
993 CS_SEL_YUV : CS_SEL_RGB;
994 ret = imx_media_enum_ipu_format(&code->code,
1004 mutex_unlock(&priv->lock);
1008 static int csi_get_fmt(struct v4l2_subdev *sd,
1009 struct v4l2_subdev_pad_config *cfg,
1010 struct v4l2_subdev_format *sdformat)
1012 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1013 struct v4l2_mbus_framefmt *fmt;
1016 if (sdformat->pad >= CSI_NUM_PADS)
1019 mutex_lock(&priv->lock);
1021 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1027 sdformat->format = *fmt;
1029 mutex_unlock(&priv->lock);
1033 static void csi_try_fmt(struct csi_priv *priv,
1034 struct imx_media_subdev *sensor,
1035 struct v4l2_subdev_pad_config *cfg,
1036 struct v4l2_subdev_format *sdformat,
1037 struct v4l2_rect *crop,
1038 const struct imx_media_pixfmt **cc)
1040 const struct imx_media_pixfmt *incc;
1041 struct v4l2_mbus_framefmt *infmt;
1044 switch (sdformat->pad) {
1045 case CSI_SRC_PAD_DIRECT:
1046 case CSI_SRC_PAD_IDMAC:
1047 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD,
1049 incc = imx_media_find_mbus_format(infmt->code,
1052 if (sdformat->format.width < crop->width * 3 / 4)
1053 sdformat->format.width = crop->width / 2;
1055 sdformat->format.width = crop->width;
1057 if (sdformat->format.height < crop->height * 3 / 4)
1058 sdformat->format.height = crop->height / 2;
1060 sdformat->format.height = crop->height;
1063 sdformat->format.code = infmt->code;
1066 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1067 CS_SEL_YUV : CS_SEL_RGB;
1069 *cc = imx_media_find_ipu_format(sdformat->format.code,
1072 imx_media_enum_ipu_format(&code, 0, cs_sel);
1073 *cc = imx_media_find_ipu_format(code, cs_sel);
1074 sdformat->format.code = (*cc)->codes[0];
1078 if (sdformat->pad == CSI_SRC_PAD_DIRECT ||
1079 sdformat->format.field != V4L2_FIELD_NONE)
1080 sdformat->format.field = infmt->field;
1083 * translate V4L2_FIELD_ALTERNATE to SEQ_TB or SEQ_BT
1084 * depending on input height (assume NTSC top-bottom
1085 * order if 480 lines, otherwise PAL bottom-top order).
1087 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) {
1088 sdformat->format.field = (infmt->height == 480) ?
1089 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1093 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1094 W_ALIGN, &sdformat->format.height,
1095 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1098 crop->width = sdformat->format.width;
1099 crop->height = sdformat->format.height;
1100 csi_try_crop(priv, crop, cfg, &sdformat->format, sensor);
1102 *cc = imx_media_find_mbus_format(sdformat->format.code,
1105 imx_media_enum_mbus_format(&code, 0,
1107 *cc = imx_media_find_mbus_format(code,
1109 sdformat->format.code = (*cc)->codes[0];
1115 static int csi_set_fmt(struct v4l2_subdev *sd,
1116 struct v4l2_subdev_pad_config *cfg,
1117 struct v4l2_subdev_format *sdformat)
1119 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1120 struct imx_media_video_dev *vdev = priv->vdev;
1121 const struct imx_media_pixfmt *cc;
1122 struct imx_media_subdev *sensor;
1123 struct v4l2_pix_format vdev_fmt;
1124 struct v4l2_mbus_framefmt *fmt;
1125 struct v4l2_rect *crop;
1128 if (sdformat->pad >= CSI_NUM_PADS)
1131 sensor = imx_media_find_sensor(priv->md, &priv->sd.entity);
1132 if (IS_ERR(sensor)) {
1133 v4l2_err(&priv->sd, "no sensor attached\n");
1134 return PTR_ERR(sensor);
1137 mutex_lock(&priv->lock);
1139 if (priv->stream_count > 0) {
1144 crop = __csi_get_crop(priv, cfg, sdformat->which);
1146 csi_try_fmt(priv, sensor, cfg, sdformat, crop, &cc);
1148 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1149 *fmt = sdformat->format;
1151 if (sdformat->pad == CSI_SINK_PAD) {
1154 /* propagate format to source pads */
1155 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1156 const struct imx_media_pixfmt *outcc;
1157 struct v4l2_mbus_framefmt *outfmt;
1158 struct v4l2_subdev_format format;
1161 format.which = sdformat->which;
1162 format.format = sdformat->format;
1163 csi_try_fmt(priv, sensor, cfg, &format, crop, &outcc);
1165 outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which);
1166 *outfmt = format.format;
1168 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1169 priv->cc[pad] = outcc;
1173 if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY)
1176 priv->cc[sdformat->pad] = cc;
1178 /* propagate IDMAC output pad format to capture device */
1179 imx_media_mbus_fmt_to_pix_fmt(&vdev_fmt,
1180 &priv->format_mbus[CSI_SRC_PAD_IDMAC],
1181 priv->cc[CSI_SRC_PAD_IDMAC]);
1182 mutex_unlock(&priv->lock);
1183 imx_media_capture_device_set_format(vdev, &vdev_fmt);
1187 mutex_unlock(&priv->lock);
1191 static int csi_get_selection(struct v4l2_subdev *sd,
1192 struct v4l2_subdev_pad_config *cfg,
1193 struct v4l2_subdev_selection *sel)
1195 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1196 struct v4l2_mbus_framefmt *infmt;
1197 struct v4l2_rect *crop;
1200 if (sel->pad >= CSI_NUM_PADS || sel->pad == CSI_SINK_PAD)
1203 mutex_lock(&priv->lock);
1205 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1206 crop = __csi_get_crop(priv, cfg, sel->which);
1208 switch (sel->target) {
1209 case V4L2_SEL_TGT_CROP_BOUNDS:
1212 sel->r.width = infmt->width;
1213 sel->r.height = infmt->height;
1215 case V4L2_SEL_TGT_CROP:
1222 mutex_unlock(&priv->lock);
1226 static int csi_set_selection(struct v4l2_subdev *sd,
1227 struct v4l2_subdev_pad_config *cfg,
1228 struct v4l2_subdev_selection *sel)
1230 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1231 struct v4l2_mbus_framefmt *infmt;
1232 struct imx_media_subdev *sensor;
1233 struct v4l2_rect *crop;
1236 if (sel->pad >= CSI_NUM_PADS ||
1237 sel->pad == CSI_SINK_PAD ||
1238 sel->target != V4L2_SEL_TGT_CROP)
1241 sensor = imx_media_find_sensor(priv->md, &priv->sd.entity);
1242 if (IS_ERR(sensor)) {
1243 v4l2_err(&priv->sd, "no sensor attached\n");
1244 return PTR_ERR(sensor);
1247 mutex_lock(&priv->lock);
1249 if (priv->stream_count > 0) {
1254 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1255 crop = __csi_get_crop(priv, cfg, sel->which);
1258 * Modifying the crop rectangle always changes the format on the source
1259 * pad. If the KEEP_CONFIG flag is set, just return the current crop
1262 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1263 sel->r = priv->crop;
1264 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1269 csi_try_crop(priv, &sel->r, cfg, infmt, sensor);
1273 /* Update the source pad formats */
1274 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1275 struct v4l2_mbus_framefmt *outfmt;
1277 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which);
1278 outfmt->width = crop->width;
1279 outfmt->height = crop->height;
1283 mutex_unlock(&priv->lock);
1287 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1288 struct v4l2_event_subscription *sub)
1290 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1295 return v4l2_event_subscribe(fh, sub, 0, NULL);
1298 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1299 struct v4l2_event_subscription *sub)
1301 return v4l2_event_unsubscribe(fh, sub);
1305 * retrieve our pads parsed from the OF graph by the media device
1307 static int csi_registered(struct v4l2_subdev *sd)
1309 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1313 /* get media device */
1314 priv->md = dev_get_drvdata(sd->v4l2_dev->dev);
1316 /* get handle to IPU CSI */
1317 priv->csi = ipu_csi_get(priv->ipu, priv->csi_id);
1318 if (IS_ERR(priv->csi)) {
1319 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1320 return PTR_ERR(priv->csi);
1323 for (i = 0; i < CSI_NUM_PADS; i++) {
1324 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
1325 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1328 if (i != CSI_SINK_PAD)
1329 imx_media_enum_ipu_format(&code, 0, CS_SEL_YUV);
1331 /* set a default mbus format */
1332 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1333 640, 480, code, V4L2_FIELD_NONE,
1339 /* init default frame interval */
1340 priv->frame_interval.numerator = 1;
1341 priv->frame_interval.denominator = 30;
1343 priv->fim = imx_media_fim_init(&priv->sd);
1344 if (IS_ERR(priv->fim)) {
1345 ret = PTR_ERR(priv->fim);
1349 ret = media_entity_pads_init(&sd->entity, CSI_NUM_PADS, priv->pad);
1353 ret = imx_media_capture_device_register(priv->vdev);
1357 ret = imx_media_add_video_device(priv->md, priv->vdev);
1363 imx_media_capture_device_unregister(priv->vdev);
1366 imx_media_fim_free(priv->fim);
1368 ipu_csi_put(priv->csi);
1372 static void csi_unregistered(struct v4l2_subdev *sd)
1374 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1376 imx_media_capture_device_unregister(priv->vdev);
1379 imx_media_fim_free(priv->fim);
1381 if (!IS_ERR_OR_NULL(priv->csi))
1382 ipu_csi_put(priv->csi);
1385 static const struct media_entity_operations csi_entity_ops = {
1386 .link_setup = csi_link_setup,
1387 .link_validate = v4l2_subdev_link_validate,
1390 static const struct v4l2_subdev_core_ops csi_core_ops = {
1391 .subscribe_event = csi_subscribe_event,
1392 .unsubscribe_event = csi_unsubscribe_event,
1395 static const struct v4l2_subdev_video_ops csi_video_ops = {
1396 .g_frame_interval = csi_g_frame_interval,
1397 .s_frame_interval = csi_s_frame_interval,
1398 .s_stream = csi_s_stream,
1401 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1402 .enum_mbus_code = csi_enum_mbus_code,
1403 .get_fmt = csi_get_fmt,
1404 .set_fmt = csi_set_fmt,
1405 .get_selection = csi_get_selection,
1406 .set_selection = csi_set_selection,
1407 .link_validate = csi_link_validate,
1410 static const struct v4l2_subdev_ops csi_subdev_ops = {
1411 .core = &csi_core_ops,
1412 .video = &csi_video_ops,
1413 .pad = &csi_pad_ops,
1416 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1417 .registered = csi_registered,
1418 .unregistered = csi_unregistered,
1421 static int imx_csi_probe(struct platform_device *pdev)
1423 struct ipu_client_platformdata *pdata;
1424 struct pinctrl *pinctrl;
1425 struct csi_priv *priv;
1428 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1432 platform_set_drvdata(pdev, &priv->sd);
1433 priv->dev = &pdev->dev;
1435 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1439 /* get parent IPU */
1440 priv->ipu = dev_get_drvdata(priv->dev->parent);
1442 /* get our CSI id */
1443 pdata = priv->dev->platform_data;
1444 priv->csi_id = pdata->csi;
1445 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1447 init_timer(&priv->eof_timeout_timer);
1448 priv->eof_timeout_timer.data = (unsigned long)priv;
1449 priv->eof_timeout_timer.function = csi_idmac_eof_timeout;
1450 spin_lock_init(&priv->irqlock);
1452 v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1453 v4l2_set_subdevdata(&priv->sd, priv);
1454 priv->sd.internal_ops = &csi_internal_ops;
1455 priv->sd.entity.ops = &csi_entity_ops;
1456 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1457 priv->sd.dev = &pdev->dev;
1458 priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
1459 priv->sd.owner = THIS_MODULE;
1460 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1461 priv->sd.grp_id = priv->csi_id ?
1462 IMX_MEDIA_GRP_ID_CSI1 : IMX_MEDIA_GRP_ID_CSI0;
1463 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
1464 priv->sd.grp_id, ipu_get_num(priv->ipu));
1466 priv->vdev = imx_media_capture_device_init(&priv->sd,
1468 if (IS_ERR(priv->vdev))
1469 return PTR_ERR(priv->vdev);
1471 mutex_init(&priv->lock);
1473 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1474 priv->sd.ctrl_handler = &priv->ctrl_hdlr;
1477 * The IPUv3 driver did not assign an of_node to this
1478 * device. As a result, pinctrl does not automatically
1479 * configure our pin groups, so we need to do that manually
1480 * here, after setting this device's of_node.
1482 priv->dev->of_node = pdata->of_node;
1483 pinctrl = devm_pinctrl_get_select_default(priv->dev);
1485 ret = v4l2_async_register_subdev(&priv->sd);
1491 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1492 mutex_destroy(&priv->lock);
1493 imx_media_capture_device_remove(priv->vdev);
1497 static int imx_csi_remove(struct platform_device *pdev)
1499 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1500 struct csi_priv *priv = sd_to_dev(sd);
1502 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1503 mutex_destroy(&priv->lock);
1504 imx_media_capture_device_remove(priv->vdev);
1505 v4l2_async_unregister_subdev(sd);
1506 media_entity_cleanup(&sd->entity);
1511 static const struct platform_device_id imx_csi_ids[] = {
1512 { .name = "imx-ipuv3-csi" },
1515 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
1517 static struct platform_driver imx_csi_driver = {
1518 .probe = imx_csi_probe,
1519 .remove = imx_csi_remove,
1520 .id_table = imx_csi_ids,
1522 .name = "imx-ipuv3-csi",
1525 module_platform_driver(imx_csi_driver);
1527 MODULE_DESCRIPTION("i.MX CSI subdev driver");
1528 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
1529 MODULE_LICENSE("GPL");
1530 MODULE_ALIAS("platform:imx-ipuv3-csi");