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_video_dev vdev;
34 struct v4l2_subdev *src_sd;
38 struct imx_media_dev *md;
40 struct media_pad vdev_pad;
42 struct mutex mutex; /* capture device mutex */
44 /* the videobuf2 queue */
46 /* list of ready imx_media_buffer's from q */
47 struct list_head ready_q;
51 /* controls inherited from subdevs */
52 struct v4l2_ctrl_handler ctrl_hdlr;
55 bool stop; /* streaming is stopping */
58 #define to_capture_priv(v) container_of(v, struct capture_priv, vdev)
60 /* In bytes, per queue */
61 #define VID_MEM_LIMIT SZ_64M
63 static const struct vb2_ops capture_qops;
69 static int vidioc_querycap(struct file *file, void *fh,
70 struct v4l2_capability *cap)
72 struct capture_priv *priv = video_drvdata(file);
74 strscpy(cap->driver, IMX_CAPTURE_NAME, sizeof(cap->driver));
75 strscpy(cap->card, IMX_CAPTURE_NAME, sizeof(cap->card));
76 snprintf(cap->bus_info, sizeof(cap->bus_info),
77 "platform:%s", priv->src_sd->name);
82 static int capture_enum_framesizes(struct file *file, void *fh,
83 struct v4l2_frmsizeenum *fsize)
85 struct capture_priv *priv = video_drvdata(file);
86 const struct imx_media_pixfmt *cc;
87 struct v4l2_subdev_frame_size_enum fse = {
88 .index = fsize->index,
89 .pad = priv->src_sd_pad,
90 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
94 cc = imx_media_find_pixel_format(fsize->pixel_format, PIXFMT_SEL_ANY);
98 fse.code = cc->codes ? cc->codes[0] : 0;
100 ret = v4l2_subdev_call(priv->src_sd, pad, enum_frame_size, NULL, &fse);
104 if (fse.min_width == fse.max_width &&
105 fse.min_height == fse.max_height) {
106 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
107 fsize->discrete.width = fse.min_width;
108 fsize->discrete.height = fse.min_height;
110 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
111 fsize->stepwise.min_width = fse.min_width;
112 fsize->stepwise.max_width = fse.max_width;
113 fsize->stepwise.min_height = fse.min_height;
114 fsize->stepwise.max_height = fse.max_height;
115 fsize->stepwise.step_width = 1;
116 fsize->stepwise.step_height = 1;
122 static int capture_enum_frameintervals(struct file *file, void *fh,
123 struct v4l2_frmivalenum *fival)
125 struct capture_priv *priv = video_drvdata(file);
126 const struct imx_media_pixfmt *cc;
127 struct v4l2_subdev_frame_interval_enum fie = {
128 .index = fival->index,
129 .pad = priv->src_sd_pad,
130 .width = fival->width,
131 .height = fival->height,
132 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
136 cc = imx_media_find_pixel_format(fival->pixel_format, PIXFMT_SEL_ANY);
140 fie.code = cc->codes ? cc->codes[0] : 0;
142 ret = v4l2_subdev_call(priv->src_sd, pad, enum_frame_interval,
147 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
148 fival->discrete = fie.interval;
153 static int capture_enum_fmt_vid_cap(struct file *file, void *fh,
154 struct v4l2_fmtdesc *f)
156 struct capture_priv *priv = video_drvdata(file);
157 const struct imx_media_pixfmt *cc_src;
158 struct v4l2_subdev_format fmt_src;
162 fmt_src.pad = priv->src_sd_pad;
163 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
164 ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
166 v4l2_err(priv->src_sd, "failed to get src_sd format\n");
170 cc_src = imx_media_find_ipu_format(fmt_src.format.code,
173 enum imx_pixfmt_sel fmt_sel =
174 (cc_src->cs == IPUV3_COLORSPACE_YUV) ?
175 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB;
177 ret = imx_media_enum_pixel_formats(&fourcc, f->index, fmt_sel);
181 cc_src = imx_media_find_mbus_format(fmt_src.format.code,
183 if (WARN_ON(!cc_src))
188 fourcc = cc_src->fourcc;
191 f->pixelformat = fourcc;
196 static int capture_g_fmt_vid_cap(struct file *file, void *fh,
197 struct v4l2_format *f)
199 struct capture_priv *priv = video_drvdata(file);
206 static int __capture_try_fmt_vid_cap(struct capture_priv *priv,
207 struct v4l2_subdev_format *fmt_src,
208 struct v4l2_format *f,
209 const struct imx_media_pixfmt **retcc,
210 struct v4l2_rect *compose)
212 const struct imx_media_pixfmt *cc, *cc_src;
214 cc_src = imx_media_find_ipu_format(fmt_src->format.code,
217 enum imx_pixfmt_sel fmt_sel;
220 fmt_sel = (cc_src->cs == IPUV3_COLORSPACE_YUV) ?
221 PIXFMT_SEL_YUV : PIXFMT_SEL_RGB;
222 fourcc = f->fmt.pix.pixelformat;
224 cc = imx_media_find_pixel_format(fourcc, fmt_sel);
226 imx_media_enum_pixel_formats(&fourcc, 0, fmt_sel);
227 cc = imx_media_find_pixel_format(fourcc, fmt_sel);
230 cc_src = imx_media_find_mbus_format(fmt_src->format.code,
232 if (WARN_ON(!cc_src))
238 /* allow IDMAC interweave but enforce field order from source */
239 if (V4L2_FIELD_IS_INTERLACED(f->fmt.pix.field)) {
240 switch (fmt_src->format.field) {
241 case V4L2_FIELD_SEQ_TB:
242 fmt_src->format.field = V4L2_FIELD_INTERLACED_TB;
244 case V4L2_FIELD_SEQ_BT:
245 fmt_src->format.field = V4L2_FIELD_INTERLACED_BT;
252 imx_media_mbus_fmt_to_pix_fmt(&f->fmt.pix, &fmt_src->format, cc);
260 compose->width = fmt_src->format.width;
261 compose->height = fmt_src->format.height;
267 static int capture_try_fmt_vid_cap(struct file *file, void *fh,
268 struct v4l2_format *f)
270 struct capture_priv *priv = video_drvdata(file);
271 struct v4l2_subdev_format fmt_src;
274 fmt_src.pad = priv->src_sd_pad;
275 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
276 ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
280 return __capture_try_fmt_vid_cap(priv, &fmt_src, f, NULL, NULL);
283 static int capture_s_fmt_vid_cap(struct file *file, void *fh,
284 struct v4l2_format *f)
286 struct capture_priv *priv = video_drvdata(file);
287 struct v4l2_subdev_format fmt_src;
290 if (vb2_is_busy(&priv->q)) {
291 v4l2_err(priv->src_sd, "%s queue busy\n", __func__);
295 fmt_src.pad = priv->src_sd_pad;
296 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
297 ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
301 ret = __capture_try_fmt_vid_cap(priv, &fmt_src, f, &priv->vdev.cc,
302 &priv->vdev.compose);
306 priv->vdev.fmt.fmt.pix = f->fmt.pix;
311 static int capture_querystd(struct file *file, void *fh, v4l2_std_id *std)
313 struct capture_priv *priv = video_drvdata(file);
315 return v4l2_subdev_call(priv->src_sd, video, querystd, std);
318 static int capture_g_std(struct file *file, void *fh, v4l2_std_id *std)
320 struct capture_priv *priv = video_drvdata(file);
322 return v4l2_subdev_call(priv->src_sd, video, g_std, std);
325 static int capture_s_std(struct file *file, void *fh, v4l2_std_id std)
327 struct capture_priv *priv = video_drvdata(file);
329 if (vb2_is_busy(&priv->q))
332 return v4l2_subdev_call(priv->src_sd, video, s_std, std);
335 static int capture_g_selection(struct file *file, void *fh,
336 struct v4l2_selection *s)
338 struct capture_priv *priv = video_drvdata(file);
341 case V4L2_SEL_TGT_COMPOSE:
342 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
343 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
344 /* The compose rectangle is fixed to the source format. */
345 s->r = priv->vdev.compose;
347 case V4L2_SEL_TGT_COMPOSE_PADDED:
349 * The hardware writes with a configurable but fixed DMA burst
350 * size. If the source format width is not burst size aligned,
351 * the written frame contains padding to the right.
355 s->r.width = priv->vdev.fmt.fmt.pix.width;
356 s->r.height = priv->vdev.fmt.fmt.pix.height;
365 static int capture_g_parm(struct file *file, void *fh,
366 struct v4l2_streamparm *a)
368 struct capture_priv *priv = video_drvdata(file);
369 struct v4l2_subdev_frame_interval fi;
372 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
375 memset(&fi, 0, sizeof(fi));
376 fi.pad = priv->src_sd_pad;
377 ret = v4l2_subdev_call(priv->src_sd, video, g_frame_interval, &fi);
381 a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
382 a->parm.capture.timeperframe = fi.interval;
387 static int capture_s_parm(struct file *file, void *fh,
388 struct v4l2_streamparm *a)
390 struct capture_priv *priv = video_drvdata(file);
391 struct v4l2_subdev_frame_interval fi;
394 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
397 memset(&fi, 0, sizeof(fi));
398 fi.pad = priv->src_sd_pad;
399 fi.interval = a->parm.capture.timeperframe;
400 ret = v4l2_subdev_call(priv->src_sd, video, s_frame_interval, &fi);
404 a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
405 a->parm.capture.timeperframe = fi.interval;
410 static int capture_subscribe_event(struct v4l2_fh *fh,
411 const struct v4l2_event_subscription *sub)
414 case V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR:
415 return v4l2_event_subscribe(fh, sub, 0, NULL);
416 case V4L2_EVENT_SOURCE_CHANGE:
417 return v4l2_src_change_event_subscribe(fh, sub);
418 case V4L2_EVENT_CTRL:
419 return v4l2_ctrl_subscribe_event(fh, sub);
425 static const struct v4l2_ioctl_ops capture_ioctl_ops = {
426 .vidioc_querycap = vidioc_querycap,
428 .vidioc_enum_framesizes = capture_enum_framesizes,
429 .vidioc_enum_frameintervals = capture_enum_frameintervals,
431 .vidioc_enum_fmt_vid_cap = capture_enum_fmt_vid_cap,
432 .vidioc_g_fmt_vid_cap = capture_g_fmt_vid_cap,
433 .vidioc_try_fmt_vid_cap = capture_try_fmt_vid_cap,
434 .vidioc_s_fmt_vid_cap = capture_s_fmt_vid_cap,
436 .vidioc_querystd = capture_querystd,
437 .vidioc_g_std = capture_g_std,
438 .vidioc_s_std = capture_s_std,
440 .vidioc_g_selection = capture_g_selection,
442 .vidioc_g_parm = capture_g_parm,
443 .vidioc_s_parm = capture_s_parm,
445 .vidioc_reqbufs = vb2_ioctl_reqbufs,
446 .vidioc_create_bufs = vb2_ioctl_create_bufs,
447 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
448 .vidioc_querybuf = vb2_ioctl_querybuf,
449 .vidioc_qbuf = vb2_ioctl_qbuf,
450 .vidioc_dqbuf = vb2_ioctl_dqbuf,
451 .vidioc_expbuf = vb2_ioctl_expbuf,
452 .vidioc_streamon = vb2_ioctl_streamon,
453 .vidioc_streamoff = vb2_ioctl_streamoff,
455 .vidioc_subscribe_event = capture_subscribe_event,
456 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
463 static int capture_queue_setup(struct vb2_queue *vq,
464 unsigned int *nbuffers,
465 unsigned int *nplanes,
466 unsigned int sizes[],
467 struct device *alloc_devs[])
469 struct capture_priv *priv = vb2_get_drv_priv(vq);
470 struct v4l2_pix_format *pix = &priv->vdev.fmt.fmt.pix;
471 unsigned int count = *nbuffers;
473 if (vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
477 if (*nplanes != 1 || sizes[0] < pix->sizeimage)
479 count += vq->num_buffers;
482 count = min_t(__u32, VID_MEM_LIMIT / pix->sizeimage, count);
485 *nbuffers = (count < vq->num_buffers) ? 0 :
486 count - vq->num_buffers;
491 sizes[0] = pix->sizeimage;
496 static int capture_buf_init(struct vb2_buffer *vb)
498 struct imx_media_buffer *buf = to_imx_media_vb(vb);
500 INIT_LIST_HEAD(&buf->list);
505 static int capture_buf_prepare(struct vb2_buffer *vb)
507 struct vb2_queue *vq = vb->vb2_queue;
508 struct capture_priv *priv = vb2_get_drv_priv(vq);
509 struct v4l2_pix_format *pix = &priv->vdev.fmt.fmt.pix;
511 if (vb2_plane_size(vb, 0) < pix->sizeimage) {
512 v4l2_err(priv->src_sd,
513 "data will not fit into plane (%lu < %lu)\n",
514 vb2_plane_size(vb, 0), (long)pix->sizeimage);
518 vb2_set_plane_payload(vb, 0, pix->sizeimage);
523 static void capture_buf_queue(struct vb2_buffer *vb)
525 struct capture_priv *priv = vb2_get_drv_priv(vb->vb2_queue);
526 struct imx_media_buffer *buf = to_imx_media_vb(vb);
529 spin_lock_irqsave(&priv->q_lock, flags);
531 list_add_tail(&buf->list, &priv->ready_q);
533 spin_unlock_irqrestore(&priv->q_lock, flags);
536 static int capture_validate_fmt(struct capture_priv *priv)
538 struct v4l2_subdev_format fmt_src;
539 const struct imx_media_pixfmt *cc;
540 struct v4l2_rect compose;
541 struct v4l2_format f;
544 fmt_src.pad = priv->src_sd_pad;
545 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
546 ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
550 v4l2_fill_pix_format(&f.fmt.pix, &fmt_src.format);
552 ret = __capture_try_fmt_vid_cap(priv, &fmt_src, &f, &cc, &compose);
556 return (priv->vdev.fmt.fmt.pix.width != f.fmt.pix.width ||
557 priv->vdev.fmt.fmt.pix.height != f.fmt.pix.height ||
558 priv->vdev.cc->cs != cc->cs ||
559 priv->vdev.compose.width != compose.width ||
560 priv->vdev.compose.height != compose.height) ? -EPIPE : 0;
563 static int capture_start_streaming(struct vb2_queue *vq, unsigned int count)
565 struct capture_priv *priv = vb2_get_drv_priv(vq);
566 struct imx_media_buffer *buf, *tmp;
570 ret = capture_validate_fmt(priv);
572 v4l2_err(priv->src_sd, "capture format not valid\n");
576 ret = imx_media_pipeline_set_stream(priv->md, &priv->src_sd->entity,
579 v4l2_err(priv->src_sd, "pipeline start failed with %d\n", ret);
588 spin_lock_irqsave(&priv->q_lock, flags);
589 list_for_each_entry_safe(buf, tmp, &priv->ready_q, list) {
590 list_del(&buf->list);
591 vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED);
593 spin_unlock_irqrestore(&priv->q_lock, flags);
597 static void capture_stop_streaming(struct vb2_queue *vq)
599 struct capture_priv *priv = vb2_get_drv_priv(vq);
600 struct imx_media_buffer *frame;
601 struct imx_media_buffer *tmp;
605 spin_lock_irqsave(&priv->q_lock, flags);
607 spin_unlock_irqrestore(&priv->q_lock, flags);
609 ret = imx_media_pipeline_set_stream(priv->md, &priv->src_sd->entity,
612 v4l2_warn(priv->src_sd, "pipeline stop failed with %d\n", ret);
614 /* release all active buffers */
615 spin_lock_irqsave(&priv->q_lock, flags);
616 list_for_each_entry_safe(frame, tmp, &priv->ready_q, list) {
617 list_del(&frame->list);
618 vb2_buffer_done(&frame->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
620 spin_unlock_irqrestore(&priv->q_lock, flags);
623 static const struct vb2_ops capture_qops = {
624 .queue_setup = capture_queue_setup,
625 .buf_init = capture_buf_init,
626 .buf_prepare = capture_buf_prepare,
627 .buf_queue = capture_buf_queue,
628 .wait_prepare = vb2_ops_wait_prepare,
629 .wait_finish = vb2_ops_wait_finish,
630 .start_streaming = capture_start_streaming,
631 .stop_streaming = capture_stop_streaming,
637 static int capture_open(struct file *file)
639 struct capture_priv *priv = video_drvdata(file);
640 struct video_device *vfd = priv->vdev.vfd;
643 if (mutex_lock_interruptible(&priv->mutex))
646 ret = v4l2_fh_open(file);
648 v4l2_err(priv->src_sd, "v4l2_fh_open failed\n");
650 ret = v4l2_pipeline_pm_get(&vfd->entity);
652 v4l2_fh_release(file);
654 mutex_unlock(&priv->mutex);
658 static int capture_release(struct file *file)
660 struct capture_priv *priv = video_drvdata(file);
661 struct video_device *vfd = priv->vdev.vfd;
662 struct vb2_queue *vq = &priv->q;
664 mutex_lock(&priv->mutex);
666 if (file->private_data == vq->owner) {
667 vb2_queue_release(vq);
671 v4l2_pipeline_pm_put(&vfd->entity);
673 v4l2_fh_release(file);
674 mutex_unlock(&priv->mutex);
678 static const struct v4l2_file_operations capture_fops = {
679 .owner = THIS_MODULE,
680 .open = capture_open,
681 .release = capture_release,
682 .poll = vb2_fop_poll,
683 .unlocked_ioctl = video_ioctl2,
684 .mmap = vb2_fop_mmap,
687 static struct video_device capture_videodev = {
688 .fops = &capture_fops,
689 .ioctl_ops = &capture_ioctl_ops,
691 .release = video_device_release,
692 .vfl_dir = VFL_DIR_RX,
693 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
694 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING,
697 struct imx_media_buffer *
698 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev)
700 struct capture_priv *priv = to_capture_priv(vdev);
701 struct imx_media_buffer *buf = NULL;
704 spin_lock_irqsave(&priv->q_lock, flags);
706 /* get next queued buffer */
707 if (!list_empty(&priv->ready_q)) {
708 buf = list_entry(priv->ready_q.next, struct imx_media_buffer,
710 list_del(&buf->list);
713 spin_unlock_irqrestore(&priv->q_lock, flags);
717 EXPORT_SYMBOL_GPL(imx_media_capture_device_next_buf);
719 void imx_media_capture_device_error(struct imx_media_video_dev *vdev)
721 struct capture_priv *priv = to_capture_priv(vdev);
722 struct vb2_queue *vq = &priv->q;
725 if (!vb2_is_streaming(vq))
728 spin_lock_irqsave(&priv->q_lock, flags);
730 spin_unlock_irqrestore(&priv->q_lock, flags);
732 EXPORT_SYMBOL_GPL(imx_media_capture_device_error);
734 int imx_media_capture_device_register(struct imx_media_video_dev *vdev)
736 struct capture_priv *priv = to_capture_priv(vdev);
737 struct v4l2_subdev *sd = priv->src_sd;
738 struct v4l2_device *v4l2_dev = sd->v4l2_dev;
739 struct video_device *vfd = vdev->vfd;
740 struct vb2_queue *vq = &priv->q;
741 struct v4l2_subdev_format fmt_src;
744 /* get media device */
745 priv->md = container_of(v4l2_dev->mdev, struct imx_media_dev, md);
747 vfd->v4l2_dev = v4l2_dev;
749 ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
751 v4l2_err(sd, "Failed to register video device\n");
755 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
756 vq->io_modes = VB2_MMAP | VB2_DMABUF;
758 vq->buf_struct_size = sizeof(struct imx_media_buffer);
759 vq->ops = &capture_qops;
760 vq->mem_ops = &vb2_dma_contig_memops;
761 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
762 vq->lock = &priv->mutex;
763 vq->min_buffers_needed = 2;
766 ret = vb2_queue_init(vq);
768 v4l2_err(sd, "vb2_queue_init failed\n");
772 INIT_LIST_HEAD(&priv->ready_q);
774 /* create the link from the src_sd devnode pad to device node */
775 ret = media_create_pad_link(&sd->entity, priv->src_sd_pad,
778 v4l2_err(sd, "failed to create link to device node\n");
782 /* setup default format */
783 fmt_src.pad = priv->src_sd_pad;
784 fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
785 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt_src);
787 v4l2_err(sd, "failed to get src_sd format\n");
791 vdev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
792 imx_media_mbus_fmt_to_pix_fmt(&vdev->fmt.fmt.pix,
793 &fmt_src.format, NULL);
794 vdev->compose.width = fmt_src.format.width;
795 vdev->compose.height = fmt_src.format.height;
796 vdev->cc = imx_media_find_pixel_format(vdev->fmt.fmt.pix.pixelformat,
799 v4l2_info(sd, "Registered %s as /dev/%s\n", vfd->name,
800 video_device_node_name(vfd));
802 vfd->ctrl_handler = &priv->ctrl_hdlr;
804 /* add vdev to the video device list */
805 imx_media_add_video_device(priv->md, vdev);
809 video_unregister_device(vfd);
812 EXPORT_SYMBOL_GPL(imx_media_capture_device_register);
814 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev)
816 struct capture_priv *priv = to_capture_priv(vdev);
817 struct video_device *vfd = priv->vdev.vfd;
819 mutex_lock(&priv->mutex);
821 if (video_is_registered(vfd)) {
822 video_unregister_device(vfd);
823 media_entity_cleanup(&vfd->entity);
826 mutex_unlock(&priv->mutex);
828 EXPORT_SYMBOL_GPL(imx_media_capture_device_unregister);
830 struct imx_media_video_dev *
831 imx_media_capture_device_init(struct device *dev, struct v4l2_subdev *src_sd,
834 struct capture_priv *priv;
835 struct video_device *vfd;
838 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
840 return ERR_PTR(-ENOMEM);
842 priv->src_sd = src_sd;
843 priv->src_sd_pad = pad;
846 mutex_init(&priv->mutex);
847 spin_lock_init(&priv->q_lock);
849 snprintf(capture_videodev.name, sizeof(capture_videodev.name),
850 "%s capture", src_sd->name);
852 vfd = video_device_alloc();
854 return ERR_PTR(-ENOMEM);
856 *vfd = capture_videodev;
857 vfd->lock = &priv->mutex;
858 vfd->queue = &priv->q;
859 priv->vdev.vfd = vfd;
861 priv->vdev_pad.flags = MEDIA_PAD_FL_SINK;
862 ret = media_entity_pads_init(&vfd->entity, 1, &priv->vdev_pad);
864 video_device_release(vfd);
868 INIT_LIST_HEAD(&priv->vdev.list);
870 video_set_drvdata(vfd, priv);
872 v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
876 EXPORT_SYMBOL_GPL(imx_media_capture_device_init);
878 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev)
880 struct capture_priv *priv = to_capture_priv(vdev);
882 v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
884 EXPORT_SYMBOL_GPL(imx_media_capture_device_remove);
886 MODULE_DESCRIPTION("i.MX5/6 v4l2 video capture interface driver");
887 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
888 MODULE_LICENSE("GPL");