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 unsigned int burst_size;
292 struct ipu_image image;
297 infmt = &priv->format_mbus[CSI_SINK_PAD];
298 sensor_ep = &priv->sensor->sensor_ep;
300 ipu_cpmem_zero(priv->idmac_ch);
302 memset(&image, 0, sizeof(image));
303 image.pix = vdev->fmt.fmt.pix;
304 image.rect.width = image.pix.width;
305 image.rect.height = image.pix.height;
307 csi_idmac_setup_vb2_buf(priv, phys);
309 image.phys0 = phys[0];
310 image.phys1 = phys[1];
312 ret = ipu_cpmem_set_image(priv->idmac_ch, &image);
316 burst_size = (image.pix.width & 0xf) ? 8 : 16;
318 ipu_cpmem_set_burstsize(priv->idmac_ch, burst_size);
321 * If the sensor uses 16-bit parallel CSI bus, we must handle
322 * the data internally in the IPU as 16-bit generic, aka
325 passthrough = (sensor_ep->bus_type != V4L2_MBUS_CSI2 &&
326 sensor_ep->bus.parallel.bus_width >= 16);
329 ipu_cpmem_set_format_passthrough(priv->idmac_ch, 16);
332 * Set the channel for the direct CSI-->memory via SMFC
333 * use-case to very high priority, by enabling the watermark
334 * signal in the SMFC, enabling WM in the channel, and setting
335 * the channel priority to high.
337 * Refer to the i.mx6 rev. D TRM Table 36-8: Calculated priority
340 * The WM's are set very low by intention here to ensure that
341 * the SMFC FIFOs do not overflow.
343 ipu_smfc_set_watermark(priv->smfc, 0x02, 0x01);
344 ipu_cpmem_set_high_priority(priv->idmac_ch);
345 ipu_idmac_enable_watermark(priv->idmac_ch, true);
346 ipu_cpmem_set_axi_id(priv->idmac_ch, 0);
348 burst_size = passthrough ?
349 (burst_size >> 3) - 1 : (burst_size >> 2) - 1;
351 ipu_smfc_set_burstsize(priv->smfc, burst_size);
353 if (image.pix.field == V4L2_FIELD_NONE &&
354 V4L2_FIELD_HAS_BOTH(infmt->field))
355 ipu_cpmem_interlaced_scan(priv->idmac_ch,
356 image.pix.bytesperline);
358 ipu_idmac_set_double_buffer(priv->idmac_ch, true);
363 csi_idmac_unsetup_vb2_buf(priv, VB2_BUF_STATE_QUEUED);
367 static void csi_idmac_unsetup(struct csi_priv *priv,
368 enum vb2_buffer_state state)
370 ipu_idmac_disable_channel(priv->idmac_ch);
371 ipu_smfc_disable(priv->smfc);
373 csi_idmac_unsetup_vb2_buf(priv, state);
376 static int csi_idmac_setup(struct csi_priv *priv)
380 ret = csi_idmac_setup_channel(priv);
384 ipu_cpmem_dump(priv->idmac_ch);
387 ipu_smfc_enable(priv->smfc);
389 /* set buffers ready */
390 ipu_idmac_select_buffer(priv->idmac_ch, 0);
391 ipu_idmac_select_buffer(priv->idmac_ch, 1);
393 /* enable the channels */
394 ipu_idmac_enable_channel(priv->idmac_ch);
399 static int csi_idmac_start(struct csi_priv *priv)
401 struct imx_media_video_dev *vdev = priv->vdev;
402 struct v4l2_pix_format *outfmt;
405 ret = csi_idmac_get_ipu_resources(priv);
409 ipu_smfc_map_channel(priv->smfc, priv->csi_id, priv->vc_num);
411 outfmt = &vdev->fmt.fmt.pix;
413 ret = imx_media_alloc_dma_buf(priv->md, &priv->underrun_buf,
418 priv->ipu_buf_num = 0;
420 /* init EOF completion waitq */
421 init_completion(&priv->last_eof_comp);
422 priv->last_eof = false;
423 priv->nfb4eof = false;
425 ret = csi_idmac_setup(priv);
427 v4l2_err(&priv->sd, "csi_idmac_setup failed: %d\n", ret);
428 goto out_free_dma_buf;
431 priv->nfb4eof_irq = ipu_idmac_channel_irq(priv->ipu,
434 ret = devm_request_irq(priv->dev, priv->nfb4eof_irq,
435 csi_idmac_nfb4eof_interrupt, 0,
436 "imx-smfc-nfb4eof", priv);
439 "Error registering NFB4EOF irq: %d\n", ret);
443 priv->eof_irq = ipu_idmac_channel_irq(priv->ipu, priv->idmac_ch,
446 ret = devm_request_irq(priv->dev, priv->eof_irq,
447 csi_idmac_eof_interrupt, 0,
448 "imx-smfc-eof", priv);
451 "Error registering eof irq: %d\n", ret);
452 goto out_free_nfb4eof_irq;
455 /* start the EOF timeout timer */
456 mod_timer(&priv->eof_timeout_timer,
457 jiffies + msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
461 out_free_nfb4eof_irq:
462 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
464 csi_idmac_unsetup(priv, VB2_BUF_STATE_QUEUED);
466 imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
468 csi_idmac_put_ipu_resources(priv);
472 static void csi_idmac_stop(struct csi_priv *priv)
477 /* mark next EOF interrupt as the last before stream off */
478 spin_lock_irqsave(&priv->irqlock, flags);
479 priv->last_eof = true;
480 spin_unlock_irqrestore(&priv->irqlock, flags);
483 * and then wait for interrupt handler to mark completion.
485 ret = wait_for_completion_timeout(
486 &priv->last_eof_comp, msecs_to_jiffies(IMX_MEDIA_EOF_TIMEOUT));
488 v4l2_warn(&priv->sd, "wait last EOF timeout\n");
490 devm_free_irq(priv->dev, priv->eof_irq, priv);
491 devm_free_irq(priv->dev, priv->nfb4eof_irq, priv);
493 csi_idmac_unsetup(priv, VB2_BUF_STATE_ERROR);
495 imx_media_free_dma_buf(priv->md, &priv->underrun_buf);
497 /* cancel the EOF timeout timer */
498 del_timer_sync(&priv->eof_timeout_timer);
500 csi_idmac_put_ipu_resources(priv);
503 /* Update the CSI whole sensor and active windows */
504 static int csi_setup(struct csi_priv *priv)
506 struct v4l2_mbus_framefmt *infmt, *outfmt;
507 struct v4l2_mbus_config sensor_mbus_cfg;
508 struct v4l2_fwnode_endpoint *sensor_ep;
509 struct v4l2_mbus_framefmt if_fmt;
511 infmt = &priv->format_mbus[CSI_SINK_PAD];
512 outfmt = &priv->format_mbus[priv->active_output_pad];
513 sensor_ep = &priv->sensor->sensor_ep;
515 /* compose mbus_config from sensor endpoint */
516 sensor_mbus_cfg.type = sensor_ep->bus_type;
517 sensor_mbus_cfg.flags = (sensor_ep->bus_type == V4L2_MBUS_CSI2) ?
518 sensor_ep->bus.mipi_csi2.flags :
519 sensor_ep->bus.parallel.flags;
522 * we need to pass input sensor frame to CSI interface, but
523 * with translated field type from output format
526 if_fmt.field = outfmt->field;
528 ipu_csi_set_window(priv->csi, &priv->crop);
530 ipu_csi_set_downsize(priv->csi,
531 priv->crop.width == 2 * outfmt->width,
532 priv->crop.height == 2 * outfmt->height);
534 ipu_csi_init_interface(priv->csi, &sensor_mbus_cfg, &if_fmt);
536 ipu_csi_set_dest(priv->csi, priv->dest);
538 ipu_csi_dump(priv->csi);
543 static int csi_start(struct csi_priv *priv)
549 v4l2_err(&priv->sd, "no sensor attached\n");
553 ret = v4l2_subdev_call(priv->sensor->sd, sensor,
554 g_skip_frames, &bad_frames);
555 if (!ret && bad_frames) {
556 struct v4l2_fract *fi = &priv->frame_interval;
560 * This sensor has bad frames when it is turned on,
561 * add a delay to avoid them before enabling the CSI
562 * hardware. Especially for sensors with a bt.656 interface,
563 * any shifts in the SAV/EAV sync codes will cause the CSI
564 * to lose vert/horiz sync.
566 delay_usec = DIV_ROUND_UP_ULL(
567 (u64)USEC_PER_SEC * fi->numerator * bad_frames,
569 usleep_range(delay_usec, delay_usec + 1000);
572 if (priv->dest == IPU_CSI_DEST_IDMAC) {
573 ret = csi_idmac_start(priv);
578 ret = csi_setup(priv);
582 /* start the frame interval monitor */
583 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC) {
584 ret = imx_media_fim_set_stream(priv->fim, &priv->frame_interval,
590 ret = ipu_csi_enable(priv->csi);
592 v4l2_err(&priv->sd, "CSI enable error: %d\n", ret);
599 if (priv->fim && priv->dest == IPU_CSI_DEST_IDMAC)
600 imx_media_fim_set_stream(priv->fim, &priv->frame_interval,
603 if (priv->dest == IPU_CSI_DEST_IDMAC)
604 csi_idmac_stop(priv);
608 static void csi_stop(struct csi_priv *priv)
610 if (priv->dest == IPU_CSI_DEST_IDMAC) {
611 csi_idmac_stop(priv);
613 /* stop the frame interval monitor */
615 imx_media_fim_set_stream(priv->fim,
616 &priv->frame_interval,
620 ipu_csi_disable(priv->csi);
624 * V4L2 subdev operations.
627 static int csi_g_frame_interval(struct v4l2_subdev *sd,
628 struct v4l2_subdev_frame_interval *fi)
630 struct csi_priv *priv = v4l2_get_subdevdata(sd);
632 mutex_lock(&priv->lock);
633 fi->interval = priv->frame_interval;
634 mutex_unlock(&priv->lock);
639 static int csi_s_frame_interval(struct v4l2_subdev *sd,
640 struct v4l2_subdev_frame_interval *fi)
642 struct csi_priv *priv = v4l2_get_subdevdata(sd);
644 mutex_lock(&priv->lock);
646 /* Output pads mirror active input pad, no limits on input pads */
647 if (fi->pad == CSI_SRC_PAD_IDMAC || fi->pad == CSI_SRC_PAD_DIRECT)
648 fi->interval = priv->frame_interval;
650 priv->frame_interval = fi->interval;
652 mutex_unlock(&priv->lock);
657 static int csi_s_stream(struct v4l2_subdev *sd, int enable)
659 struct csi_priv *priv = v4l2_get_subdevdata(sd);
662 mutex_lock(&priv->lock);
664 if (!priv->src_sd || !priv->sink) {
670 * enable/disable streaming only if stream_count is
671 * going from 0 to 1 / 1 to 0.
673 if (priv->stream_count != !enable)
677 /* upstream must be started first, before starting CSI */
678 ret = v4l2_subdev_call(priv->src_sd, video, s_stream, 1);
679 ret = (ret && ret != -ENOIOCTLCMD) ? ret : 0;
683 dev_dbg(priv->dev, "stream ON\n");
684 ret = csi_start(priv);
686 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
690 dev_dbg(priv->dev, "stream OFF\n");
691 /* CSI must be stopped first, then stop upstream */
693 v4l2_subdev_call(priv->src_sd, video, s_stream, 0);
697 priv->stream_count += enable ? 1 : -1;
698 WARN_ON(priv->stream_count < 0);
700 mutex_unlock(&priv->lock);
704 static int csi_link_setup(struct media_entity *entity,
705 const struct media_pad *local,
706 const struct media_pad *remote, u32 flags)
708 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
709 struct csi_priv *priv = v4l2_get_subdevdata(sd);
710 struct v4l2_subdev *remote_sd;
713 dev_dbg(priv->dev, "link setup %s -> %s\n", remote->entity->name,
714 local->entity->name);
716 mutex_lock(&priv->lock);
718 if (local->flags & MEDIA_PAD_FL_SINK) {
719 if (!is_media_entity_v4l2_subdev(remote->entity)) {
724 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
726 if (flags & MEDIA_LNK_FL_ENABLED) {
731 priv->src_sd = remote_sd;
739 /* this is a source pad */
741 if (flags & MEDIA_LNK_FL_ENABLED) {
747 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
748 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
753 /* record which output pad is now active */
754 priv->active_output_pad = local->index;
756 /* set CSI destination */
757 if (local->index == CSI_SRC_PAD_IDMAC) {
758 if (!is_media_entity_v4l2_video_device(remote->entity)) {
764 ret = imx_media_fim_add_controls(priv->fim);
769 priv->dest = IPU_CSI_DEST_IDMAC;
771 if (!is_media_entity_v4l2_subdev(remote->entity)) {
776 remote_sd = media_entity_to_v4l2_subdev(remote->entity);
777 switch (remote_sd->grp_id) {
778 case IMX_MEDIA_GRP_ID_VDIC:
779 priv->dest = IPU_CSI_DEST_VDIC;
781 case IMX_MEDIA_GRP_ID_IC_PRP:
782 priv->dest = IPU_CSI_DEST_IC;
790 priv->sink = remote->entity;
792 mutex_unlock(&priv->lock);
796 static int csi_link_validate(struct v4l2_subdev *sd,
797 struct media_link *link,
798 struct v4l2_subdev_format *source_fmt,
799 struct v4l2_subdev_format *sink_fmt)
801 struct csi_priv *priv = v4l2_get_subdevdata(sd);
802 struct v4l2_fwnode_endpoint *sensor_ep;
803 struct imx_media_subdev *sensor;
807 ret = v4l2_subdev_link_validate_default(sd, link,
808 source_fmt, sink_fmt);
812 sensor = __imx_media_find_sensor(priv->md, &priv->sd.entity);
813 if (IS_ERR(sensor)) {
814 v4l2_err(&priv->sd, "no sensor attached\n");
815 return PTR_ERR(priv->sensor);
818 mutex_lock(&priv->lock);
820 priv->sensor = sensor;
821 sensor_ep = &priv->sensor->sensor_ep;
822 is_csi2 = (sensor_ep->bus_type == V4L2_MBUS_CSI2);
827 * NOTE! It seems the virtual channels from the mipi csi-2
828 * receiver are used only for routing by the video mux's,
829 * or for hard-wired routing to the CSI's. Once the stream
830 * enters the CSI's however, they are treated internally
831 * in the IPU as virtual channel 0.
834 mutex_unlock(&priv->lock);
835 vc_num = imx_media_find_mipi_csi2_channel(priv->md,
839 mutex_lock(&priv->lock);
841 ipu_csi_set_mipi_datatype(priv->csi, vc_num,
842 &priv->format_mbus[CSI_SINK_PAD]);
845 /* select either parallel or MIPI-CSI2 as input to CSI */
846 ipu_set_csi_src_mux(priv->ipu, priv->csi_id, is_csi2);
848 mutex_unlock(&priv->lock);
852 static struct v4l2_mbus_framefmt *
853 __csi_get_fmt(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
854 unsigned int pad, enum v4l2_subdev_format_whence which)
856 if (which == V4L2_SUBDEV_FORMAT_TRY)
857 return v4l2_subdev_get_try_format(&priv->sd, cfg, pad);
859 return &priv->format_mbus[pad];
862 static struct v4l2_rect *
863 __csi_get_crop(struct csi_priv *priv, struct v4l2_subdev_pad_config *cfg,
864 enum v4l2_subdev_format_whence which)
866 if (which == V4L2_SUBDEV_FORMAT_TRY)
867 return v4l2_subdev_get_try_crop(&priv->sd, cfg, CSI_SINK_PAD);
872 static void csi_try_crop(struct csi_priv *priv,
873 struct v4l2_rect *crop,
874 struct v4l2_subdev_pad_config *cfg,
875 struct v4l2_mbus_framefmt *infmt,
876 struct imx_media_subdev *sensor)
878 struct v4l2_fwnode_endpoint *sensor_ep;
880 sensor_ep = &sensor->sensor_ep;
882 crop->width = min_t(__u32, infmt->width, crop->width);
883 if (crop->left + crop->width > infmt->width)
884 crop->left = infmt->width - crop->width;
885 /* adjust crop left/width to h/w alignment restrictions */
890 * FIXME: not sure why yet, but on interlaced bt.656,
891 * changing the vertical cropping causes loss of vertical
892 * sync, so fix it to NTSC/PAL active lines. NTSC contains
893 * 2 extra lines of active video that need to be cropped.
895 if (sensor_ep->bus_type == V4L2_MBUS_BT656 &&
896 (V4L2_FIELD_HAS_BOTH(infmt->field) ||
897 infmt->field == V4L2_FIELD_ALTERNATE)) {
898 crop->height = infmt->height;
899 crop->top = (infmt->height == 480) ? 2 : 0;
901 crop->height = min_t(__u32, infmt->height, crop->height);
902 if (crop->top + crop->height > infmt->height)
903 crop->top = infmt->height - crop->height;
907 static int csi_enum_mbus_code(struct v4l2_subdev *sd,
908 struct v4l2_subdev_pad_config *cfg,
909 struct v4l2_subdev_mbus_code_enum *code)
911 struct csi_priv *priv = v4l2_get_subdevdata(sd);
912 const struct imx_media_pixfmt *incc;
913 struct v4l2_mbus_framefmt *infmt;
916 mutex_lock(&priv->lock);
918 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, code->which);
919 incc = imx_media_find_mbus_format(infmt->code, CS_SEL_ANY, true);
923 ret = imx_media_enum_mbus_format(&code->code, code->index,
926 case CSI_SRC_PAD_DIRECT:
927 case CSI_SRC_PAD_IDMAC:
929 if (code->index != 0) {
933 code->code = infmt->code;
935 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
936 CS_SEL_YUV : CS_SEL_RGB;
937 ret = imx_media_enum_ipu_format(&code->code,
947 mutex_unlock(&priv->lock);
951 static int csi_get_fmt(struct v4l2_subdev *sd,
952 struct v4l2_subdev_pad_config *cfg,
953 struct v4l2_subdev_format *sdformat)
955 struct csi_priv *priv = v4l2_get_subdevdata(sd);
956 struct v4l2_mbus_framefmt *fmt;
959 if (sdformat->pad >= CSI_NUM_PADS)
962 mutex_lock(&priv->lock);
964 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
970 sdformat->format = *fmt;
972 mutex_unlock(&priv->lock);
976 static void csi_try_fmt(struct csi_priv *priv,
977 struct imx_media_subdev *sensor,
978 struct v4l2_subdev_pad_config *cfg,
979 struct v4l2_subdev_format *sdformat,
980 struct v4l2_rect *crop,
981 const struct imx_media_pixfmt **cc)
983 const struct imx_media_pixfmt *incc;
984 struct v4l2_mbus_framefmt *infmt;
987 switch (sdformat->pad) {
988 case CSI_SRC_PAD_DIRECT:
989 case CSI_SRC_PAD_IDMAC:
990 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD,
992 incc = imx_media_find_mbus_format(infmt->code,
995 if (sdformat->format.width < crop->width * 3 / 4)
996 sdformat->format.width = crop->width / 2;
998 sdformat->format.width = crop->width;
1000 if (sdformat->format.height < crop->height * 3 / 4)
1001 sdformat->format.height = crop->height / 2;
1003 sdformat->format.height = crop->height;
1006 sdformat->format.code = infmt->code;
1009 u32 cs_sel = (incc->cs == IPUV3_COLORSPACE_YUV) ?
1010 CS_SEL_YUV : CS_SEL_RGB;
1012 *cc = imx_media_find_ipu_format(sdformat->format.code,
1015 imx_media_enum_ipu_format(&code, 0, cs_sel);
1016 *cc = imx_media_find_ipu_format(code, cs_sel);
1017 sdformat->format.code = (*cc)->codes[0];
1021 if (sdformat->pad == CSI_SRC_PAD_DIRECT ||
1022 sdformat->format.field != V4L2_FIELD_NONE)
1023 sdformat->format.field = infmt->field;
1026 * translate V4L2_FIELD_ALTERNATE to SEQ_TB or SEQ_BT
1027 * depending on input height (assume NTSC top-bottom
1028 * order if 480 lines, otherwise PAL bottom-top order).
1030 if (sdformat->format.field == V4L2_FIELD_ALTERNATE) {
1031 sdformat->format.field = (infmt->height == 480) ?
1032 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1036 v4l_bound_align_image(&sdformat->format.width, MIN_W, MAX_W,
1037 W_ALIGN, &sdformat->format.height,
1038 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
1041 crop->width = sdformat->format.width;
1042 crop->height = sdformat->format.height;
1043 csi_try_crop(priv, crop, cfg, &sdformat->format, sensor);
1045 *cc = imx_media_find_mbus_format(sdformat->format.code,
1048 imx_media_enum_mbus_format(&code, 0,
1050 *cc = imx_media_find_mbus_format(code,
1052 sdformat->format.code = (*cc)->codes[0];
1058 static int csi_set_fmt(struct v4l2_subdev *sd,
1059 struct v4l2_subdev_pad_config *cfg,
1060 struct v4l2_subdev_format *sdformat)
1062 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1063 struct imx_media_video_dev *vdev = priv->vdev;
1064 const struct imx_media_pixfmt *cc;
1065 struct imx_media_subdev *sensor;
1066 struct v4l2_pix_format vdev_fmt;
1067 struct v4l2_mbus_framefmt *fmt;
1068 struct v4l2_rect *crop;
1071 if (sdformat->pad >= CSI_NUM_PADS)
1074 sensor = imx_media_find_sensor(priv->md, &priv->sd.entity);
1075 if (IS_ERR(sensor)) {
1076 v4l2_err(&priv->sd, "no sensor attached\n");
1077 return PTR_ERR(sensor);
1080 mutex_lock(&priv->lock);
1082 if (priv->stream_count > 0) {
1087 crop = __csi_get_crop(priv, cfg, sdformat->which);
1089 csi_try_fmt(priv, sensor, cfg, sdformat, crop, &cc);
1091 fmt = __csi_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
1092 *fmt = sdformat->format;
1094 if (sdformat->pad == CSI_SINK_PAD) {
1097 /* propagate format to source pads */
1098 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1099 const struct imx_media_pixfmt *outcc;
1100 struct v4l2_mbus_framefmt *outfmt;
1101 struct v4l2_subdev_format format;
1104 format.which = sdformat->which;
1105 format.format = sdformat->format;
1106 csi_try_fmt(priv, sensor, cfg, &format, crop, &outcc);
1108 outfmt = __csi_get_fmt(priv, cfg, pad, sdformat->which);
1109 *outfmt = format.format;
1111 if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1112 priv->cc[pad] = outcc;
1116 if (sdformat->which == V4L2_SUBDEV_FORMAT_TRY)
1119 priv->cc[sdformat->pad] = cc;
1121 /* propagate IDMAC output pad format to capture device */
1122 imx_media_mbus_fmt_to_pix_fmt(&vdev_fmt,
1123 &priv->format_mbus[CSI_SRC_PAD_IDMAC],
1124 priv->cc[CSI_SRC_PAD_IDMAC]);
1125 mutex_unlock(&priv->lock);
1126 imx_media_capture_device_set_format(vdev, &vdev_fmt);
1130 mutex_unlock(&priv->lock);
1134 static int csi_get_selection(struct v4l2_subdev *sd,
1135 struct v4l2_subdev_pad_config *cfg,
1136 struct v4l2_subdev_selection *sel)
1138 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1139 struct v4l2_mbus_framefmt *infmt;
1140 struct v4l2_rect *crop;
1143 if (sel->pad >= CSI_NUM_PADS || sel->pad == CSI_SINK_PAD)
1146 mutex_lock(&priv->lock);
1148 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1149 crop = __csi_get_crop(priv, cfg, sel->which);
1151 switch (sel->target) {
1152 case V4L2_SEL_TGT_CROP_BOUNDS:
1155 sel->r.width = infmt->width;
1156 sel->r.height = infmt->height;
1158 case V4L2_SEL_TGT_CROP:
1165 mutex_unlock(&priv->lock);
1169 static int csi_set_selection(struct v4l2_subdev *sd,
1170 struct v4l2_subdev_pad_config *cfg,
1171 struct v4l2_subdev_selection *sel)
1173 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1174 struct v4l2_mbus_framefmt *infmt;
1175 struct imx_media_subdev *sensor;
1176 struct v4l2_rect *crop;
1179 if (sel->pad >= CSI_NUM_PADS ||
1180 sel->pad == CSI_SINK_PAD ||
1181 sel->target != V4L2_SEL_TGT_CROP)
1184 sensor = imx_media_find_sensor(priv->md, &priv->sd.entity);
1185 if (IS_ERR(sensor)) {
1186 v4l2_err(&priv->sd, "no sensor attached\n");
1187 return PTR_ERR(sensor);
1190 mutex_lock(&priv->lock);
1192 if (priv->stream_count > 0) {
1197 infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sel->which);
1198 crop = __csi_get_crop(priv, cfg, sel->which);
1201 * Modifying the crop rectangle always changes the format on the source
1202 * pad. If the KEEP_CONFIG flag is set, just return the current crop
1205 if (sel->flags & V4L2_SEL_FLAG_KEEP_CONFIG) {
1206 sel->r = priv->crop;
1207 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1212 csi_try_crop(priv, &sel->r, cfg, infmt, sensor);
1216 /* Update the source pad formats */
1217 for (pad = CSI_SINK_PAD + 1; pad < CSI_NUM_PADS; pad++) {
1218 struct v4l2_mbus_framefmt *outfmt;
1220 outfmt = __csi_get_fmt(priv, cfg, pad, sel->which);
1221 outfmt->width = crop->width;
1222 outfmt->height = crop->height;
1226 mutex_unlock(&priv->lock);
1230 static int csi_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1231 struct v4l2_event_subscription *sub)
1233 if (sub->type != V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR)
1238 return v4l2_event_subscribe(fh, sub, 0, NULL);
1241 static int csi_unsubscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1242 struct v4l2_event_subscription *sub)
1244 return v4l2_event_unsubscribe(fh, sub);
1248 * retrieve our pads parsed from the OF graph by the media device
1250 static int csi_registered(struct v4l2_subdev *sd)
1252 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1256 /* get media device */
1257 priv->md = dev_get_drvdata(sd->v4l2_dev->dev);
1259 /* get handle to IPU CSI */
1260 priv->csi = ipu_csi_get(priv->ipu, priv->csi_id);
1261 if (IS_ERR(priv->csi)) {
1262 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id);
1263 return PTR_ERR(priv->csi);
1266 for (i = 0; i < CSI_NUM_PADS; i++) {
1267 priv->pad[i].flags = (i == CSI_SINK_PAD) ?
1268 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1271 if (i != CSI_SINK_PAD)
1272 imx_media_enum_ipu_format(&code, 0, CS_SEL_YUV);
1274 /* set a default mbus format */
1275 ret = imx_media_init_mbus_fmt(&priv->format_mbus[i],
1276 640, 480, code, V4L2_FIELD_NONE,
1282 /* init default frame interval */
1283 priv->frame_interval.numerator = 1;
1284 priv->frame_interval.denominator = 30;
1286 priv->fim = imx_media_fim_init(&priv->sd);
1287 if (IS_ERR(priv->fim)) {
1288 ret = PTR_ERR(priv->fim);
1292 ret = media_entity_pads_init(&sd->entity, CSI_NUM_PADS, priv->pad);
1296 ret = imx_media_capture_device_register(priv->vdev);
1300 ret = imx_media_add_video_device(priv->md, priv->vdev);
1306 imx_media_capture_device_unregister(priv->vdev);
1309 imx_media_fim_free(priv->fim);
1311 ipu_csi_put(priv->csi);
1315 static void csi_unregistered(struct v4l2_subdev *sd)
1317 struct csi_priv *priv = v4l2_get_subdevdata(sd);
1319 imx_media_capture_device_unregister(priv->vdev);
1322 imx_media_fim_free(priv->fim);
1324 if (!IS_ERR_OR_NULL(priv->csi))
1325 ipu_csi_put(priv->csi);
1328 static const struct media_entity_operations csi_entity_ops = {
1329 .link_setup = csi_link_setup,
1330 .link_validate = v4l2_subdev_link_validate,
1333 static const struct v4l2_subdev_core_ops csi_core_ops = {
1334 .subscribe_event = csi_subscribe_event,
1335 .unsubscribe_event = csi_unsubscribe_event,
1338 static const struct v4l2_subdev_video_ops csi_video_ops = {
1339 .g_frame_interval = csi_g_frame_interval,
1340 .s_frame_interval = csi_s_frame_interval,
1341 .s_stream = csi_s_stream,
1344 static const struct v4l2_subdev_pad_ops csi_pad_ops = {
1345 .enum_mbus_code = csi_enum_mbus_code,
1346 .get_fmt = csi_get_fmt,
1347 .set_fmt = csi_set_fmt,
1348 .get_selection = csi_get_selection,
1349 .set_selection = csi_set_selection,
1350 .link_validate = csi_link_validate,
1353 static const struct v4l2_subdev_ops csi_subdev_ops = {
1354 .core = &csi_core_ops,
1355 .video = &csi_video_ops,
1356 .pad = &csi_pad_ops,
1359 static const struct v4l2_subdev_internal_ops csi_internal_ops = {
1360 .registered = csi_registered,
1361 .unregistered = csi_unregistered,
1364 static int imx_csi_probe(struct platform_device *pdev)
1366 struct ipu_client_platformdata *pdata;
1367 struct pinctrl *pinctrl;
1368 struct csi_priv *priv;
1371 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1375 platform_set_drvdata(pdev, &priv->sd);
1376 priv->dev = &pdev->dev;
1378 ret = dma_set_coherent_mask(priv->dev, DMA_BIT_MASK(32));
1382 /* get parent IPU */
1383 priv->ipu = dev_get_drvdata(priv->dev->parent);
1385 /* get our CSI id */
1386 pdata = priv->dev->platform_data;
1387 priv->csi_id = pdata->csi;
1388 priv->smfc_id = (priv->csi_id == 0) ? 0 : 2;
1390 init_timer(&priv->eof_timeout_timer);
1391 priv->eof_timeout_timer.data = (unsigned long)priv;
1392 priv->eof_timeout_timer.function = csi_idmac_eof_timeout;
1393 spin_lock_init(&priv->irqlock);
1395 v4l2_subdev_init(&priv->sd, &csi_subdev_ops);
1396 v4l2_set_subdevdata(&priv->sd, priv);
1397 priv->sd.internal_ops = &csi_internal_ops;
1398 priv->sd.entity.ops = &csi_entity_ops;
1399 priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1400 priv->sd.dev = &pdev->dev;
1401 priv->sd.fwnode = of_fwnode_handle(pdata->of_node);
1402 priv->sd.owner = THIS_MODULE;
1403 priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1404 priv->sd.grp_id = priv->csi_id ?
1405 IMX_MEDIA_GRP_ID_CSI1 : IMX_MEDIA_GRP_ID_CSI0;
1406 imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
1407 priv->sd.grp_id, ipu_get_num(priv->ipu));
1409 priv->vdev = imx_media_capture_device_init(&priv->sd,
1411 if (IS_ERR(priv->vdev))
1412 return PTR_ERR(priv->vdev);
1414 mutex_init(&priv->lock);
1416 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1417 priv->sd.ctrl_handler = &priv->ctrl_hdlr;
1420 * The IPUv3 driver did not assign an of_node to this
1421 * device. As a result, pinctrl does not automatically
1422 * configure our pin groups, so we need to do that manually
1423 * here, after setting this device's of_node.
1425 priv->dev->of_node = pdata->of_node;
1426 pinctrl = devm_pinctrl_get_select_default(priv->dev);
1428 ret = v4l2_async_register_subdev(&priv->sd);
1434 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1435 mutex_destroy(&priv->lock);
1436 imx_media_capture_device_remove(priv->vdev);
1440 static int imx_csi_remove(struct platform_device *pdev)
1442 struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1443 struct csi_priv *priv = sd_to_dev(sd);
1445 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1446 mutex_destroy(&priv->lock);
1447 imx_media_capture_device_remove(priv->vdev);
1448 v4l2_async_unregister_subdev(sd);
1449 media_entity_cleanup(&sd->entity);
1454 static const struct platform_device_id imx_csi_ids[] = {
1455 { .name = "imx-ipuv3-csi" },
1458 MODULE_DEVICE_TABLE(platform, imx_csi_ids);
1460 static struct platform_driver imx_csi_driver = {
1461 .probe = imx_csi_probe,
1462 .remove = imx_csi_remove,
1463 .id_table = imx_csi_ids,
1465 .name = "imx-ipuv3-csi",
1468 module_platform_driver(imx_csi_driver);
1470 MODULE_DESCRIPTION("i.MX CSI subdev driver");
1471 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
1472 MODULE_LICENSE("GPL");
1473 MODULE_ALIAS("platform:imx-ipuv3-csi");