1 // SPDX-License-Identifier: GPL-2.0+
3 * Video Capture Subdev for Freescale i.MX5/6 SOC
5 * Copyright (c) 2012-2016 Mentor Graphics Inc.
7 #include <linux/delay.h>
9 #include <linux/module.h>
10 #include <linux/of_platform.h>
11 #include <linux/pinctrl/consumer.h>
12 #include <linux/platform_device.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/timer.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-device.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-fwnode.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-mc.h>
23 #include <media/v4l2-subdev.h>
24 #include <media/videobuf2-dma-contig.h>
25 #include <video/imx-ipu-v3.h>
26 #include <media/imx.h>
27 #include "imx-media.h"
29 #define IMX_CAPTURE_NAME "imx-capture"
32 struct imx_media_dev *md; /* Media device */
33 struct device *dev; /* Physical device */
35 struct imx_media_video_dev vdev; /* Video device */
36 struct media_pad vdev_pad; /* Video device pad */
38 struct v4l2_subdev *src_sd; /* Source subdev */
39 int src_sd_pad; /* Source subdev pad */
41 struct mutex mutex; /* Protect vdev operations */
43 struct vb2_queue q; /* The videobuf2 queue */
44 struct list_head ready_q; /* List of queued buffers */
45 spinlock_t q_lock; /* Protect ready_q */
47 struct v4l2_ctrl_handler ctrl_hdlr; /* Controls inherited from subdevs */
49 bool legacy_api; /* Use the legacy (pre-MC) API */
52 #define to_capture_priv(v) container_of(v, struct capture_priv, vdev)
54 /* In bytes, per queue */
55 #define VID_MEM_LIMIT SZ_64M
57 /* -----------------------------------------------------------------------------
58 * MC-Centric Video IOCTLs
61 static const struct imx_media_pixfmt *capture_find_format(u32 code, u32 fourcc)
63 const struct imx_media_pixfmt *cc;
65 cc = imx_media_find_ipu_format(code, PIXFMT_SEL_YUV_RGB);
67 enum imx_pixfmt_sel fmt_sel = cc->cs == IPUV3_COLORSPACE_YUV
68 ? PIXFMT_SEL_YUV : PIXFMT_SEL_RGB;
70 cc = imx_media_find_pixel_format(fourcc, fmt_sel);
72 imx_media_enum_pixel_formats(&fourcc, 0, fmt_sel, 0);
73 cc = imx_media_find_pixel_format(fourcc, fmt_sel);
79 return imx_media_find_mbus_format(code, PIXFMT_SEL_ANY);
82 static int capture_querycap(struct file *file, void *fh,
83 struct v4l2_capability *cap)
85 struct capture_priv *priv = video_drvdata(file);
87 strscpy(cap->driver, IMX_CAPTURE_NAME, sizeof(cap->driver));
88 strscpy(cap->card, IMX_CAPTURE_NAME, sizeof(cap->card));
89 snprintf(cap->bus_info, sizeof(cap->bus_info),
90 "platform:%s", dev_name(priv->dev));
95 static int capture_enum_fmt_vid_cap(struct file *file, void *fh,
96 struct v4l2_fmtdesc *f)
98 return imx_media_enum_pixel_formats(&f->pixelformat, f->index,
99 PIXFMT_SEL_ANY, f->mbus_code);
102 static int capture_enum_framesizes(struct file *file, void *fh,
103 struct v4l2_frmsizeenum *fsize)
105 const struct imx_media_pixfmt *cc;
107 if (fsize->index > 0)
110 cc = imx_media_find_pixel_format(fsize->pixel_format, PIXFMT_SEL_ANY);
115 * TODO: The constraints are hardware-specific and may depend on the
116 * pixel format. This should come from the driver using
119 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
120 fsize->stepwise.min_width = 1;
121 fsize->stepwise.max_width = 65535;
122 fsize->stepwise.min_height = 1;
123 fsize->stepwise.max_height = 65535;
124 fsize->stepwise.step_width = 1;
125 fsize->stepwise.step_height = 1;
130 static int capture_g_fmt_vid_cap(struct file *file, void *fh,
131 struct v4l2_format *f)
133 struct capture_priv *priv = video_drvdata(file);
135 f->fmt.pix = priv->vdev.fmt;
140 static const struct imx_media_pixfmt *
141 __capture_try_fmt(struct v4l2_pix_format *pixfmt, struct v4l2_rect *compose)
143 struct v4l2_mbus_framefmt fmt_src;
144 const struct imx_media_pixfmt *cc;
147 * Find the pixel format, default to the first supported format if not
150 cc = imx_media_find_pixel_format(pixfmt->pixelformat, PIXFMT_SEL_ANY);
152 imx_media_enum_pixel_formats(&pixfmt->pixelformat, 0,
154 cc = imx_media_find_pixel_format(pixfmt->pixelformat,
158 /* Allow IDMAC interweave but enforce field order from source. */
159 if (V4L2_FIELD_IS_INTERLACED(pixfmt->field)) {
160 switch (pixfmt->field) {
161 case V4L2_FIELD_SEQ_TB:
162 pixfmt->field = V4L2_FIELD_INTERLACED_TB;
164 case V4L2_FIELD_SEQ_BT:
165 pixfmt->field = V4L2_FIELD_INTERLACED_BT;
172 v4l2_fill_mbus_format(&fmt_src, pixfmt, 0);
173 imx_media_mbus_fmt_to_pix_fmt(pixfmt, &fmt_src, cc);
176 compose->width = fmt_src.width;
177 compose->height = fmt_src.height;
183 static int capture_try_fmt_vid_cap(struct file *file, void *fh,
184 struct v4l2_format *f)
186 __capture_try_fmt(&f->fmt.pix, NULL);
190 static int capture_s_fmt_vid_cap(struct file *file, void *fh,
191 struct v4l2_format *f)
193 struct capture_priv *priv = video_drvdata(file);
194 const struct imx_media_pixfmt *cc;
196 if (vb2_is_busy(&priv->q)) {
197 dev_err(priv->dev, "%s queue busy\n", __func__);
201 cc = __capture_try_fmt(&f->fmt.pix, &priv->vdev.compose);
204 priv->vdev.fmt = f->fmt.pix;
209 static int capture_g_selection(struct file *file, void *fh,
210 struct v4l2_selection *s)
212 struct capture_priv *priv = video_drvdata(file);
215 case V4L2_SEL_TGT_COMPOSE:
216 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
217 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
218 /* The compose rectangle is fixed to the source format. */
219 s->r = priv->vdev.compose;
221 case V4L2_SEL_TGT_COMPOSE_PADDED:
223 * The hardware writes with a configurable but fixed DMA burst
224 * size. If the source format width is not burst size aligned,
225 * the written frame contains padding to the right.
229 s->r.width = priv->vdev.fmt.width;
230 s->r.height = priv->vdev.fmt.height;
239 static int capture_subscribe_event(struct v4l2_fh *fh,
240 const struct v4l2_event_subscription *sub)
243 case V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR:
244 return v4l2_event_subscribe(fh, sub, 0, NULL);
250 static const struct v4l2_ioctl_ops capture_ioctl_ops = {
251 .vidioc_querycap = capture_querycap,
253 .vidioc_enum_fmt_vid_cap = capture_enum_fmt_vid_cap,
254 .vidioc_enum_framesizes = capture_enum_framesizes,
256 .vidioc_g_fmt_vid_cap = capture_g_fmt_vid_cap,
257 .vidioc_try_fmt_vid_cap = capture_try_fmt_vid_cap,
258 .vidioc_s_fmt_vid_cap = capture_s_fmt_vid_cap,
260 .vidioc_g_selection = capture_g_selection,
262 .vidioc_reqbufs = vb2_ioctl_reqbufs,
263 .vidioc_create_bufs = vb2_ioctl_create_bufs,
264 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
265 .vidioc_querybuf = vb2_ioctl_querybuf,
266 .vidioc_qbuf = vb2_ioctl_qbuf,
267 .vidioc_dqbuf = vb2_ioctl_dqbuf,
268 .vidioc_expbuf = vb2_ioctl_expbuf,
269 .vidioc_streamon = vb2_ioctl_streamon,
270 .vidioc_streamoff = vb2_ioctl_streamoff,
272 .vidioc_subscribe_event = capture_subscribe_event,
273 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
276 /* -----------------------------------------------------------------------------
277 * Legacy Video IOCTLs
280 static int capture_legacy_enum_framesizes(struct file *file, void *fh,
281 struct v4l2_frmsizeenum *fsize)
283 struct capture_priv *priv = video_drvdata(file);
284 const struct imx_media_pixfmt *cc;
285 struct v4l2_subdev_frame_size_enum fse = {
286 .index = fsize->index,
287 .pad = priv->src_sd_pad,
288 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
292 cc = imx_media_find_pixel_format(fsize->pixel_format, PIXFMT_SEL_ANY);
296 fse.code = cc->codes ? cc->codes[0] : 0;
298 ret = v4l2_subdev_call(priv->src_sd, pad, enum_frame_size, NULL, &fse);
302 if (fse.min_width == fse.max_width &&
303 fse.min_height == fse.max_height) {
304 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
305 fsize->discrete.width = fse.min_width;
306 fsize->discrete.height = fse.min_height;
308 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
309 fsize->stepwise.min_width = fse.min_width;
310 fsize->stepwise.max_width = fse.max_width;
311 fsize->stepwise.min_height = fse.min_height;
312 fsize->stepwise.max_height = fse.max_height;
313 fsize->stepwise.step_width = 1;
314 fsize->stepwise.step_height = 1;
320 static int capture_legacy_enum_frameintervals(struct file *file, void *fh,
321 struct v4l2_frmivalenum *fival)
323 struct capture_priv *priv = video_drvdata(file);
324 const struct imx_media_pixfmt *cc;
325 struct v4l2_subdev_frame_interval_enum fie = {
326 .index = fival->index,
327 .pad = priv->src_sd_pad,
328 .width = fival->width,
329 .height = fival->height,
330 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
334 cc = imx_media_find_pixel_format(fival->pixel_format, PIXFMT_SEL_ANY);
338 fie.code = cc->codes ? cc->codes[0] : 0;
340 ret = v4l2_subdev_call(priv->src_sd, pad, enum_frame_interval,
345 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
346 fival->discrete = fie.interval;
351 static int capture_legacy_enum_fmt_vid_cap(struct file *file, void *fh,
352 struct v4l2_fmtdesc *f)
354 struct capture_priv *priv = video_drvdata(file);
355 const struct imx_media_pixfmt *cc_src;
356 struct v4l2_subdev_format fmt_src;
360 fmt_src.pad = priv->src_sd_pad;
361 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
362 ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
364 dev_err(priv->dev, "failed to get src_sd format\n");
368 cc_src = imx_media_find_ipu_format(fmt_src.format.code,
371 enum imx_pixfmt_sel fmt_sel =
372 (cc_src->cs == IPUV3_COLORSPACE_YUV) ?
373 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB;
375 ret = imx_media_enum_pixel_formats(&fourcc, f->index, fmt_sel,
380 cc_src = imx_media_find_mbus_format(fmt_src.format.code,
382 if (WARN_ON(!cc_src))
387 fourcc = cc_src->fourcc;
390 f->pixelformat = fourcc;
395 static const struct imx_media_pixfmt *
396 __capture_legacy_try_fmt(struct capture_priv *priv,
397 struct v4l2_subdev_format *fmt_src,
398 struct v4l2_pix_format *pixfmt)
400 const struct imx_media_pixfmt *cc;
402 cc = capture_find_format(fmt_src->format.code, pixfmt->pixelformat);
406 /* allow IDMAC interweave but enforce field order from source */
407 if (V4L2_FIELD_IS_INTERLACED(pixfmt->field)) {
408 switch (fmt_src->format.field) {
409 case V4L2_FIELD_SEQ_TB:
410 fmt_src->format.field = V4L2_FIELD_INTERLACED_TB;
412 case V4L2_FIELD_SEQ_BT:
413 fmt_src->format.field = V4L2_FIELD_INTERLACED_BT;
420 imx_media_mbus_fmt_to_pix_fmt(pixfmt, &fmt_src->format, cc);
425 static int capture_legacy_try_fmt_vid_cap(struct file *file, void *fh,
426 struct v4l2_format *f)
428 struct capture_priv *priv = video_drvdata(file);
429 struct v4l2_subdev_format fmt_src;
432 fmt_src.pad = priv->src_sd_pad;
433 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
434 ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
438 if (!__capture_legacy_try_fmt(priv, &fmt_src, &f->fmt.pix))
444 static int capture_legacy_s_fmt_vid_cap(struct file *file, void *fh,
445 struct v4l2_format *f)
447 struct capture_priv *priv = video_drvdata(file);
448 struct v4l2_subdev_format fmt_src;
449 const struct imx_media_pixfmt *cc;
452 if (vb2_is_busy(&priv->q)) {
453 dev_err(priv->dev, "%s queue busy\n", __func__);
457 fmt_src.pad = priv->src_sd_pad;
458 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
459 ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
463 cc = __capture_legacy_try_fmt(priv, &fmt_src, &f->fmt.pix);
468 priv->vdev.fmt = f->fmt.pix;
469 priv->vdev.compose.width = fmt_src.format.width;
470 priv->vdev.compose.height = fmt_src.format.height;
475 static int capture_legacy_querystd(struct file *file, void *fh,
478 struct capture_priv *priv = video_drvdata(file);
480 return v4l2_subdev_call(priv->src_sd, video, querystd, std);
483 static int capture_legacy_g_std(struct file *file, void *fh, v4l2_std_id *std)
485 struct capture_priv *priv = video_drvdata(file);
487 return v4l2_subdev_call(priv->src_sd, video, g_std, std);
490 static int capture_legacy_s_std(struct file *file, void *fh, v4l2_std_id std)
492 struct capture_priv *priv = video_drvdata(file);
494 if (vb2_is_busy(&priv->q))
497 return v4l2_subdev_call(priv->src_sd, video, s_std, std);
500 static int capture_legacy_g_parm(struct file *file, void *fh,
501 struct v4l2_streamparm *a)
503 struct capture_priv *priv = video_drvdata(file);
504 struct v4l2_subdev_frame_interval fi;
507 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
510 memset(&fi, 0, sizeof(fi));
511 fi.pad = priv->src_sd_pad;
512 ret = v4l2_subdev_call(priv->src_sd, video, g_frame_interval, &fi);
516 a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
517 a->parm.capture.timeperframe = fi.interval;
522 static int capture_legacy_s_parm(struct file *file, void *fh,
523 struct v4l2_streamparm *a)
525 struct capture_priv *priv = video_drvdata(file);
526 struct v4l2_subdev_frame_interval fi;
529 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
532 memset(&fi, 0, sizeof(fi));
533 fi.pad = priv->src_sd_pad;
534 fi.interval = a->parm.capture.timeperframe;
535 ret = v4l2_subdev_call(priv->src_sd, video, s_frame_interval, &fi);
539 a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
540 a->parm.capture.timeperframe = fi.interval;
545 static int capture_legacy_subscribe_event(struct v4l2_fh *fh,
546 const struct v4l2_event_subscription *sub)
549 case V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR:
550 return v4l2_event_subscribe(fh, sub, 0, NULL);
551 case V4L2_EVENT_SOURCE_CHANGE:
552 return v4l2_src_change_event_subscribe(fh, sub);
553 case V4L2_EVENT_CTRL:
554 return v4l2_ctrl_subscribe_event(fh, sub);
560 static const struct v4l2_ioctl_ops capture_legacy_ioctl_ops = {
561 .vidioc_querycap = capture_querycap,
563 .vidioc_enum_framesizes = capture_legacy_enum_framesizes,
564 .vidioc_enum_frameintervals = capture_legacy_enum_frameintervals,
566 .vidioc_enum_fmt_vid_cap = capture_legacy_enum_fmt_vid_cap,
567 .vidioc_g_fmt_vid_cap = capture_g_fmt_vid_cap,
568 .vidioc_try_fmt_vid_cap = capture_legacy_try_fmt_vid_cap,
569 .vidioc_s_fmt_vid_cap = capture_legacy_s_fmt_vid_cap,
571 .vidioc_querystd = capture_legacy_querystd,
572 .vidioc_g_std = capture_legacy_g_std,
573 .vidioc_s_std = capture_legacy_s_std,
575 .vidioc_g_selection = capture_g_selection,
577 .vidioc_g_parm = capture_legacy_g_parm,
578 .vidioc_s_parm = capture_legacy_s_parm,
580 .vidioc_reqbufs = vb2_ioctl_reqbufs,
581 .vidioc_create_bufs = vb2_ioctl_create_bufs,
582 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
583 .vidioc_querybuf = vb2_ioctl_querybuf,
584 .vidioc_qbuf = vb2_ioctl_qbuf,
585 .vidioc_dqbuf = vb2_ioctl_dqbuf,
586 .vidioc_expbuf = vb2_ioctl_expbuf,
587 .vidioc_streamon = vb2_ioctl_streamon,
588 .vidioc_streamoff = vb2_ioctl_streamoff,
590 .vidioc_subscribe_event = capture_legacy_subscribe_event,
591 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
594 /* -----------------------------------------------------------------------------
598 static int capture_queue_setup(struct vb2_queue *vq,
599 unsigned int *nbuffers,
600 unsigned int *nplanes,
601 unsigned int sizes[],
602 struct device *alloc_devs[])
604 struct capture_priv *priv = vb2_get_drv_priv(vq);
605 struct v4l2_pix_format *pix = &priv->vdev.fmt;
606 unsigned int count = *nbuffers;
608 if (vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
612 if (*nplanes != 1 || sizes[0] < pix->sizeimage)
614 count += vq->num_buffers;
617 count = min_t(__u32, VID_MEM_LIMIT / pix->sizeimage, count);
620 *nbuffers = (count < vq->num_buffers) ? 0 :
621 count - vq->num_buffers;
626 sizes[0] = pix->sizeimage;
631 static int capture_buf_init(struct vb2_buffer *vb)
633 struct imx_media_buffer *buf = to_imx_media_vb(vb);
635 INIT_LIST_HEAD(&buf->list);
640 static int capture_buf_prepare(struct vb2_buffer *vb)
642 struct vb2_queue *vq = vb->vb2_queue;
643 struct capture_priv *priv = vb2_get_drv_priv(vq);
644 struct v4l2_pix_format *pix = &priv->vdev.fmt;
646 if (vb2_plane_size(vb, 0) < pix->sizeimage) {
648 "data will not fit into plane (%lu < %lu)\n",
649 vb2_plane_size(vb, 0), (long)pix->sizeimage);
653 vb2_set_plane_payload(vb, 0, pix->sizeimage);
658 static void capture_buf_queue(struct vb2_buffer *vb)
660 struct capture_priv *priv = vb2_get_drv_priv(vb->vb2_queue);
661 struct imx_media_buffer *buf = to_imx_media_vb(vb);
664 spin_lock_irqsave(&priv->q_lock, flags);
666 list_add_tail(&buf->list, &priv->ready_q);
668 spin_unlock_irqrestore(&priv->q_lock, flags);
671 static int capture_validate_fmt(struct capture_priv *priv)
673 struct v4l2_subdev_format fmt_src;
674 const struct imx_media_pixfmt *cc;
677 /* Retrieve the media bus format on the source subdev. */
678 fmt_src.pad = priv->src_sd_pad;
679 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
680 ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
685 * Verify that the media bus size matches the size set on the video
686 * node. It is sufficient to check the compose rectangle size without
687 * checking the rounded size from vdev.fmt, as the rounded size is
688 * derived directly from the compose rectangle size, and will thus
689 * always match if the compose rectangle matches.
691 if (priv->vdev.compose.width != fmt_src.format.width ||
692 priv->vdev.compose.height != fmt_src.format.height)
696 * Verify that the media bus code is compatible with the pixel format
697 * set on the video node.
699 cc = capture_find_format(fmt_src.format.code, 0);
700 if (!cc || priv->vdev.cc->cs != cc->cs)
706 static int capture_start_streaming(struct vb2_queue *vq, unsigned int count)
708 struct capture_priv *priv = vb2_get_drv_priv(vq);
709 struct imx_media_buffer *buf, *tmp;
713 ret = capture_validate_fmt(priv);
715 dev_err(priv->dev, "capture format not valid\n");
719 ret = imx_media_pipeline_set_stream(priv->md, &priv->src_sd->entity,
722 dev_err(priv->dev, "pipeline start failed with %d\n", ret);
729 spin_lock_irqsave(&priv->q_lock, flags);
730 list_for_each_entry_safe(buf, tmp, &priv->ready_q, list) {
731 list_del(&buf->list);
732 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED);
734 spin_unlock_irqrestore(&priv->q_lock, flags);
738 static void capture_stop_streaming(struct vb2_queue *vq)
740 struct capture_priv *priv = vb2_get_drv_priv(vq);
741 struct imx_media_buffer *frame;
742 struct imx_media_buffer *tmp;
746 ret = imx_media_pipeline_set_stream(priv->md, &priv->src_sd->entity,
749 dev_warn(priv->dev, "pipeline stop failed with %d\n", ret);
751 /* release all active buffers */
752 spin_lock_irqsave(&priv->q_lock, flags);
753 list_for_each_entry_safe(frame, tmp, &priv->ready_q, list) {
754 list_del(&frame->list);
755 vb2_buffer_done(&frame->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
757 spin_unlock_irqrestore(&priv->q_lock, flags);
760 static const struct vb2_ops capture_qops = {
761 .queue_setup = capture_queue_setup,
762 .buf_init = capture_buf_init,
763 .buf_prepare = capture_buf_prepare,
764 .buf_queue = capture_buf_queue,
765 .wait_prepare = vb2_ops_wait_prepare,
766 .wait_finish = vb2_ops_wait_finish,
767 .start_streaming = capture_start_streaming,
768 .stop_streaming = capture_stop_streaming,
771 /* -----------------------------------------------------------------------------
775 static int capture_open(struct file *file)
777 struct capture_priv *priv = video_drvdata(file);
778 struct video_device *vfd = priv->vdev.vfd;
781 if (mutex_lock_interruptible(&priv->mutex))
784 ret = v4l2_fh_open(file);
786 dev_err(priv->dev, "v4l2_fh_open failed\n");
790 ret = v4l2_pipeline_pm_get(&vfd->entity);
792 v4l2_fh_release(file);
795 mutex_unlock(&priv->mutex);
799 static int capture_release(struct file *file)
801 struct capture_priv *priv = video_drvdata(file);
802 struct video_device *vfd = priv->vdev.vfd;
803 struct vb2_queue *vq = &priv->q;
805 mutex_lock(&priv->mutex);
807 if (file->private_data == vq->owner) {
808 vb2_queue_release(vq);
812 v4l2_pipeline_pm_put(&vfd->entity);
814 v4l2_fh_release(file);
815 mutex_unlock(&priv->mutex);
819 static const struct v4l2_file_operations capture_fops = {
820 .owner = THIS_MODULE,
821 .open = capture_open,
822 .release = capture_release,
823 .poll = vb2_fop_poll,
824 .unlocked_ioctl = video_ioctl2,
825 .mmap = vb2_fop_mmap,
828 /* -----------------------------------------------------------------------------
832 struct imx_media_buffer *
833 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev)
835 struct capture_priv *priv = to_capture_priv(vdev);
836 struct imx_media_buffer *buf = NULL;
839 spin_lock_irqsave(&priv->q_lock, flags);
841 /* get next queued buffer */
842 if (!list_empty(&priv->ready_q)) {
843 buf = list_entry(priv->ready_q.next, struct imx_media_buffer,
845 list_del(&buf->list);
848 spin_unlock_irqrestore(&priv->q_lock, flags);
852 EXPORT_SYMBOL_GPL(imx_media_capture_device_next_buf);
854 void imx_media_capture_device_error(struct imx_media_video_dev *vdev)
856 struct capture_priv *priv = to_capture_priv(vdev);
857 struct vb2_queue *vq = &priv->q;
860 if (!vb2_is_streaming(vq))
863 spin_lock_irqsave(&priv->q_lock, flags);
865 spin_unlock_irqrestore(&priv->q_lock, flags);
867 EXPORT_SYMBOL_GPL(imx_media_capture_device_error);
869 static int capture_init_format(struct capture_priv *priv)
871 struct v4l2_subdev_format fmt_src = {
872 .pad = priv->src_sd_pad,
873 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
875 struct imx_media_video_dev *vdev = &priv->vdev;
878 if (priv->legacy_api) {
879 ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL,
882 dev_err(priv->dev, "failed to get source format\n");
886 fmt_src.format.code = MEDIA_BUS_FMT_UYVY8_2X8;
887 fmt_src.format.width = IMX_MEDIA_DEF_PIX_WIDTH;
888 fmt_src.format.height = IMX_MEDIA_DEF_PIX_HEIGHT;
891 imx_media_mbus_fmt_to_pix_fmt(&vdev->fmt, &fmt_src.format, NULL);
892 vdev->compose.width = fmt_src.format.width;
893 vdev->compose.height = fmt_src.format.height;
895 vdev->cc = imx_media_find_pixel_format(vdev->fmt.pixelformat,
901 int imx_media_capture_device_register(struct imx_media_video_dev *vdev,
904 struct capture_priv *priv = to_capture_priv(vdev);
905 struct v4l2_subdev *sd = priv->src_sd;
906 struct v4l2_device *v4l2_dev = sd->v4l2_dev;
907 struct video_device *vfd = vdev->vfd;
910 /* get media device */
911 priv->md = container_of(v4l2_dev->mdev, struct imx_media_dev, md);
913 vfd->v4l2_dev = v4l2_dev;
915 /* Initialize the default format and compose rectangle. */
916 ret = capture_init_format(priv);
920 /* Register the video device. */
921 ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
923 dev_err(priv->dev, "Failed to register video device\n");
927 dev_info(priv->dev, "Registered %s as /dev/%s\n", vfd->name,
928 video_device_node_name(vfd));
930 /* Create the link from the src_sd devnode pad to device node. */
931 if (link_flags & MEDIA_LNK_FL_IMMUTABLE)
932 link_flags |= MEDIA_LNK_FL_ENABLED;
933 ret = media_create_pad_link(&sd->entity, priv->src_sd_pad,
934 &vfd->entity, 0, link_flags);
936 dev_err(priv->dev, "failed to create link to device node\n");
937 video_unregister_device(vfd);
941 /* Add vdev to the video devices list. */
942 imx_media_add_video_device(priv->md, vdev);
946 EXPORT_SYMBOL_GPL(imx_media_capture_device_register);
948 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev)
950 struct capture_priv *priv = to_capture_priv(vdev);
951 struct video_device *vfd = priv->vdev.vfd;
953 media_entity_cleanup(&vfd->entity);
954 video_unregister_device(vfd);
956 EXPORT_SYMBOL_GPL(imx_media_capture_device_unregister);
958 struct imx_media_video_dev *
959 imx_media_capture_device_init(struct device *dev, struct v4l2_subdev *src_sd,
960 int pad, bool legacy_api)
962 struct capture_priv *priv;
963 struct video_device *vfd;
964 struct vb2_queue *vq;
967 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
969 return ERR_PTR(-ENOMEM);
971 priv->src_sd = src_sd;
972 priv->src_sd_pad = pad;
974 priv->legacy_api = legacy_api;
976 mutex_init(&priv->mutex);
977 INIT_LIST_HEAD(&priv->ready_q);
978 spin_lock_init(&priv->q_lock);
980 /* Allocate and initialize the video device. */
981 vfd = video_device_alloc();
983 return ERR_PTR(-ENOMEM);
985 vfd->fops = &capture_fops;
986 vfd->ioctl_ops = legacy_api ? &capture_legacy_ioctl_ops
987 : &capture_ioctl_ops;
989 vfd->release = video_device_release;
990 vfd->vfl_dir = VFL_DIR_RX;
991 vfd->tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
992 vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING
993 | (!legacy_api ? V4L2_CAP_IO_MC : 0);
994 vfd->lock = &priv->mutex;
995 vfd->queue = &priv->q;
997 snprintf(vfd->name, sizeof(vfd->name), "%s capture", src_sd->name);
999 video_set_drvdata(vfd, priv);
1000 priv->vdev.vfd = vfd;
1001 INIT_LIST_HEAD(&priv->vdev.list);
1003 /* Initialize the video device pad. */
1004 priv->vdev_pad.flags = MEDIA_PAD_FL_SINK;
1005 ret = media_entity_pads_init(&vfd->entity, 1, &priv->vdev_pad);
1007 video_device_release(vfd);
1008 return ERR_PTR(ret);
1011 /* Initialize the vb2 queue. */
1013 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1014 vq->io_modes = VB2_MMAP | VB2_DMABUF;
1015 vq->drv_priv = priv;
1016 vq->buf_struct_size = sizeof(struct imx_media_buffer);
1017 vq->ops = &capture_qops;
1018 vq->mem_ops = &vb2_dma_contig_memops;
1019 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1020 vq->lock = &priv->mutex;
1021 vq->min_buffers_needed = 2;
1022 vq->dev = priv->dev;
1024 ret = vb2_queue_init(vq);
1026 dev_err(priv->dev, "vb2_queue_init failed\n");
1027 video_device_release(vfd);
1028 return ERR_PTR(ret);
1032 /* Initialize the control handler. */
1033 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
1034 vfd->ctrl_handler = &priv->ctrl_hdlr;
1039 EXPORT_SYMBOL_GPL(imx_media_capture_device_init);
1041 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev)
1043 struct capture_priv *priv = to_capture_priv(vdev);
1045 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
1047 EXPORT_SYMBOL_GPL(imx_media_capture_device_remove);
1049 MODULE_DESCRIPTION("i.MX5/6 v4l2 video capture interface driver");
1050 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
1051 MODULE_LICENSE("GPL");