1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Video Capture Subdev for Freescale i.MX5/6 SOC
4  *
5  * Copyright (c) 2012-2016 Mentor Graphics Inc.
6  */
7 #include <linux/delay.h>
8 #include <linux/fs.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"
28 
29 struct capture_priv {
30 	struct imx_media_video_dev vdev;
31 
32 	struct v4l2_subdev    *src_sd;
33 	int                   src_sd_pad;
34 	struct device         *dev;
35 
36 	struct imx_media_dev  *md;
37 
38 	struct media_pad      vdev_pad;
39 
40 	struct mutex          mutex;       /* capture device mutex */
41 
42 	/* the videobuf2 queue */
43 	struct vb2_queue       q;
44 	/* list of ready imx_media_buffer's from q */
45 	struct list_head       ready_q;
46 	/* protect ready_q */
47 	spinlock_t             q_lock;
48 
49 	/* controls inherited from subdevs */
50 	struct v4l2_ctrl_handler ctrl_hdlr;
51 
52 	/* misc status */
53 	bool                  stop;          /* streaming is stopping */
54 };
55 
56 #define to_capture_priv(v) container_of(v, struct capture_priv, vdev)
57 
58 /* In bytes, per queue */
59 #define VID_MEM_LIMIT	SZ_64M
60 
61 static const struct vb2_ops capture_qops;
62 
63 /*
64  * Video ioctls follow
65  */
66 
67 static int vidioc_querycap(struct file *file, void *fh,
68 			   struct v4l2_capability *cap)
69 {
70 	struct capture_priv *priv = video_drvdata(file);
71 
72 	strscpy(cap->driver, "imx-media-capture", sizeof(cap->driver));
73 	strscpy(cap->card, "imx-media-capture", sizeof(cap->card));
74 	snprintf(cap->bus_info, sizeof(cap->bus_info),
75 		 "platform:%s", priv->src_sd->name);
76 
77 	return 0;
78 }
79 
80 static int capture_enum_framesizes(struct file *file, void *fh,
81 				   struct v4l2_frmsizeenum *fsize)
82 {
83 	struct capture_priv *priv = video_drvdata(file);
84 	const struct imx_media_pixfmt *cc;
85 	struct v4l2_subdev_frame_size_enum fse = {
86 		.index = fsize->index,
87 		.pad = priv->src_sd_pad,
88 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
89 	};
90 	int ret;
91 
92 	cc = imx_media_find_format(fsize->pixel_format, CS_SEL_ANY, true);
93 	if (!cc)
94 		return -EINVAL;
95 
96 	fse.code = cc->codes[0];
97 
98 	ret = v4l2_subdev_call(priv->src_sd, pad, enum_frame_size, NULL, &fse);
99 	if (ret)
100 		return ret;
101 
102 	if (fse.min_width == fse.max_width &&
103 	    fse.min_height == fse.max_height) {
104 		fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
105 		fsize->discrete.width = fse.min_width;
106 		fsize->discrete.height = fse.min_height;
107 	} else {
108 		fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
109 		fsize->stepwise.min_width = fse.min_width;
110 		fsize->stepwise.max_width = fse.max_width;
111 		fsize->stepwise.min_height = fse.min_height;
112 		fsize->stepwise.max_height = fse.max_height;
113 		fsize->stepwise.step_width = 1;
114 		fsize->stepwise.step_height = 1;
115 	}
116 
117 	return 0;
118 }
119 
120 static int capture_enum_frameintervals(struct file *file, void *fh,
121 				       struct v4l2_frmivalenum *fival)
122 {
123 	struct capture_priv *priv = video_drvdata(file);
124 	const struct imx_media_pixfmt *cc;
125 	struct v4l2_subdev_frame_interval_enum fie = {
126 		.index = fival->index,
127 		.pad = priv->src_sd_pad,
128 		.width = fival->width,
129 		.height = fival->height,
130 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
131 	};
132 	int ret;
133 
134 	cc = imx_media_find_format(fival->pixel_format, CS_SEL_ANY, true);
135 	if (!cc)
136 		return -EINVAL;
137 
138 	fie.code = cc->codes[0];
139 
140 	ret = v4l2_subdev_call(priv->src_sd, pad, enum_frame_interval,
141 			       NULL, &fie);
142 	if (ret)
143 		return ret;
144 
145 	fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
146 	fival->discrete = fie.interval;
147 
148 	return 0;
149 }
150 
151 static int capture_enum_fmt_vid_cap(struct file *file, void *fh,
152 				    struct v4l2_fmtdesc *f)
153 {
154 	struct capture_priv *priv = video_drvdata(file);
155 	const struct imx_media_pixfmt *cc_src;
156 	struct v4l2_subdev_format fmt_src;
157 	u32 fourcc;
158 	int ret;
159 
160 	fmt_src.pad = priv->src_sd_pad;
161 	fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
162 	ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
163 	if (ret) {
164 		v4l2_err(priv->src_sd, "failed to get src_sd format\n");
165 		return ret;
166 	}
167 
168 	cc_src = imx_media_find_ipu_format(fmt_src.format.code, CS_SEL_ANY);
169 	if (cc_src) {
170 		u32 cs_sel = (cc_src->cs == IPUV3_COLORSPACE_YUV) ?
171 			CS_SEL_YUV : CS_SEL_RGB;
172 
173 		ret = imx_media_enum_format(&fourcc, f->index, cs_sel);
174 		if (ret)
175 			return ret;
176 	} else {
177 		cc_src = imx_media_find_mbus_format(fmt_src.format.code,
178 						    CS_SEL_ANY, true);
179 		if (WARN_ON(!cc_src))
180 			return -EINVAL;
181 
182 		if (f->index != 0)
183 			return -EINVAL;
184 		fourcc = cc_src->fourcc;
185 	}
186 
187 	f->pixelformat = fourcc;
188 
189 	return 0;
190 }
191 
192 static int capture_g_fmt_vid_cap(struct file *file, void *fh,
193 				 struct v4l2_format *f)
194 {
195 	struct capture_priv *priv = video_drvdata(file);
196 
197 	*f = priv->vdev.fmt;
198 
199 	return 0;
200 }
201 
202 static int __capture_try_fmt_vid_cap(struct capture_priv *priv,
203 				     struct v4l2_subdev_format *fmt_src,
204 				     struct v4l2_format *f,
205 				     const struct imx_media_pixfmt **retcc,
206 				     struct v4l2_rect *compose)
207 {
208 	const struct imx_media_pixfmt *cc, *cc_src;
209 
210 	cc_src = imx_media_find_ipu_format(fmt_src->format.code, CS_SEL_ANY);
211 	if (cc_src) {
212 		u32 fourcc, cs_sel;
213 
214 		cs_sel = (cc_src->cs == IPUV3_COLORSPACE_YUV) ?
215 			CS_SEL_YUV : CS_SEL_RGB;
216 		fourcc = f->fmt.pix.pixelformat;
217 
218 		cc = imx_media_find_format(fourcc, cs_sel, false);
219 		if (!cc) {
220 			imx_media_enum_format(&fourcc, 0, cs_sel);
221 			cc = imx_media_find_format(fourcc, cs_sel, false);
222 		}
223 	} else {
224 		cc_src = imx_media_find_mbus_format(fmt_src->format.code,
225 						    CS_SEL_ANY, true);
226 		if (WARN_ON(!cc_src))
227 			return -EINVAL;
228 
229 		cc = cc_src;
230 	}
231 
232 	/* allow IDMAC interweave but enforce field order from source */
233 	if (V4L2_FIELD_IS_INTERLACED(f->fmt.pix.field)) {
234 		switch (fmt_src->format.field) {
235 		case V4L2_FIELD_SEQ_TB:
236 			fmt_src->format.field = V4L2_FIELD_INTERLACED_TB;
237 			break;
238 		case V4L2_FIELD_SEQ_BT:
239 			fmt_src->format.field = V4L2_FIELD_INTERLACED_BT;
240 			break;
241 		default:
242 			break;
243 		}
244 	}
245 
246 	imx_media_mbus_fmt_to_pix_fmt(&f->fmt.pix, &fmt_src->format, cc);
247 
248 	if (retcc)
249 		*retcc = cc;
250 
251 	if (compose) {
252 		compose->left = 0;
253 		compose->top = 0;
254 		compose->width = fmt_src->format.width;
255 		compose->height = fmt_src->format.height;
256 	}
257 
258 	return 0;
259 }
260 
261 static int capture_try_fmt_vid_cap(struct file *file, void *fh,
262 				   struct v4l2_format *f)
263 {
264 	struct capture_priv *priv = video_drvdata(file);
265 	struct v4l2_subdev_format fmt_src;
266 	int ret;
267 
268 	fmt_src.pad = priv->src_sd_pad;
269 	fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
270 	ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
271 	if (ret)
272 		return ret;
273 
274 	return __capture_try_fmt_vid_cap(priv, &fmt_src, f, NULL, NULL);
275 }
276 
277 static int capture_s_fmt_vid_cap(struct file *file, void *fh,
278 				 struct v4l2_format *f)
279 {
280 	struct capture_priv *priv = video_drvdata(file);
281 	struct v4l2_subdev_format fmt_src;
282 	int ret;
283 
284 	if (vb2_is_busy(&priv->q)) {
285 		v4l2_err(priv->src_sd, "%s queue busy\n", __func__);
286 		return -EBUSY;
287 	}
288 
289 	fmt_src.pad = priv->src_sd_pad;
290 	fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
291 	ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
292 	if (ret)
293 		return ret;
294 
295 	ret = __capture_try_fmt_vid_cap(priv, &fmt_src, f, &priv->vdev.cc,
296 					&priv->vdev.compose);
297 	if (ret)
298 		return ret;
299 
300 	priv->vdev.fmt.fmt.pix = f->fmt.pix;
301 
302 	return 0;
303 }
304 
305 static int capture_querystd(struct file *file, void *fh, v4l2_std_id *std)
306 {
307 	struct capture_priv *priv = video_drvdata(file);
308 
309 	return v4l2_subdev_call(priv->src_sd, video, querystd, std);
310 }
311 
312 static int capture_g_std(struct file *file, void *fh, v4l2_std_id *std)
313 {
314 	struct capture_priv *priv = video_drvdata(file);
315 
316 	return v4l2_subdev_call(priv->src_sd, video, g_std, std);
317 }
318 
319 static int capture_s_std(struct file *file, void *fh, v4l2_std_id std)
320 {
321 	struct capture_priv *priv = video_drvdata(file);
322 
323 	if (vb2_is_busy(&priv->q))
324 		return -EBUSY;
325 
326 	return v4l2_subdev_call(priv->src_sd, video, s_std, std);
327 }
328 
329 static int capture_g_selection(struct file *file, void *fh,
330 			       struct v4l2_selection *s)
331 {
332 	struct capture_priv *priv = video_drvdata(file);
333 
334 	switch (s->target) {
335 	case V4L2_SEL_TGT_COMPOSE:
336 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
337 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
338 		/* The compose rectangle is fixed to the source format. */
339 		s->r = priv->vdev.compose;
340 		break;
341 	case V4L2_SEL_TGT_COMPOSE_PADDED:
342 		/*
343 		 * The hardware writes with a configurable but fixed DMA burst
344 		 * size. If the source format width is not burst size aligned,
345 		 * the written frame contains padding to the right.
346 		 */
347 		s->r.left = 0;
348 		s->r.top = 0;
349 		s->r.width = priv->vdev.fmt.fmt.pix.width;
350 		s->r.height = priv->vdev.fmt.fmt.pix.height;
351 		break;
352 	default:
353 		return -EINVAL;
354 	}
355 
356 	return 0;
357 }
358 
359 static int capture_g_parm(struct file *file, void *fh,
360 			  struct v4l2_streamparm *a)
361 {
362 	struct capture_priv *priv = video_drvdata(file);
363 	struct v4l2_subdev_frame_interval fi;
364 	int ret;
365 
366 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
367 		return -EINVAL;
368 
369 	memset(&fi, 0, sizeof(fi));
370 	fi.pad = priv->src_sd_pad;
371 	ret = v4l2_subdev_call(priv->src_sd, video, g_frame_interval, &fi);
372 	if (ret < 0)
373 		return ret;
374 
375 	a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
376 	a->parm.capture.timeperframe = fi.interval;
377 
378 	return 0;
379 }
380 
381 static int capture_s_parm(struct file *file, void *fh,
382 			  struct v4l2_streamparm *a)
383 {
384 	struct capture_priv *priv = video_drvdata(file);
385 	struct v4l2_subdev_frame_interval fi;
386 	int ret;
387 
388 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
389 		return -EINVAL;
390 
391 	memset(&fi, 0, sizeof(fi));
392 	fi.pad = priv->src_sd_pad;
393 	fi.interval = a->parm.capture.timeperframe;
394 	ret = v4l2_subdev_call(priv->src_sd, video, s_frame_interval, &fi);
395 	if (ret < 0)
396 		return ret;
397 
398 	a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
399 	a->parm.capture.timeperframe = fi.interval;
400 
401 	return 0;
402 }
403 
404 static int capture_subscribe_event(struct v4l2_fh *fh,
405 				   const struct v4l2_event_subscription *sub)
406 {
407 	switch (sub->type) {
408 	case V4L2_EVENT_IMX_FRAME_INTERVAL_ERROR:
409 		return v4l2_event_subscribe(fh, sub, 0, NULL);
410 	case V4L2_EVENT_SOURCE_CHANGE:
411 		return v4l2_src_change_event_subscribe(fh, sub);
412 	case V4L2_EVENT_CTRL:
413 		return v4l2_ctrl_subscribe_event(fh, sub);
414 	default:
415 		return -EINVAL;
416 	}
417 }
418 
419 static const struct v4l2_ioctl_ops capture_ioctl_ops = {
420 	.vidioc_querycap	= vidioc_querycap,
421 
422 	.vidioc_enum_framesizes = capture_enum_framesizes,
423 	.vidioc_enum_frameintervals = capture_enum_frameintervals,
424 
425 	.vidioc_enum_fmt_vid_cap        = capture_enum_fmt_vid_cap,
426 	.vidioc_g_fmt_vid_cap           = capture_g_fmt_vid_cap,
427 	.vidioc_try_fmt_vid_cap         = capture_try_fmt_vid_cap,
428 	.vidioc_s_fmt_vid_cap           = capture_s_fmt_vid_cap,
429 
430 	.vidioc_querystd        = capture_querystd,
431 	.vidioc_g_std           = capture_g_std,
432 	.vidioc_s_std           = capture_s_std,
433 
434 	.vidioc_g_selection	= capture_g_selection,
435 
436 	.vidioc_g_parm          = capture_g_parm,
437 	.vidioc_s_parm          = capture_s_parm,
438 
439 	.vidioc_reqbufs		= vb2_ioctl_reqbufs,
440 	.vidioc_create_bufs     = vb2_ioctl_create_bufs,
441 	.vidioc_prepare_buf     = vb2_ioctl_prepare_buf,
442 	.vidioc_querybuf	= vb2_ioctl_querybuf,
443 	.vidioc_qbuf		= vb2_ioctl_qbuf,
444 	.vidioc_dqbuf		= vb2_ioctl_dqbuf,
445 	.vidioc_expbuf		= vb2_ioctl_expbuf,
446 	.vidioc_streamon	= vb2_ioctl_streamon,
447 	.vidioc_streamoff	= vb2_ioctl_streamoff,
448 
449 	.vidioc_subscribe_event = capture_subscribe_event,
450 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
451 };
452 
453 /*
454  * Queue operations
455  */
456 
457 static int capture_queue_setup(struct vb2_queue *vq,
458 			       unsigned int *nbuffers,
459 			       unsigned int *nplanes,
460 			       unsigned int sizes[],
461 			       struct device *alloc_devs[])
462 {
463 	struct capture_priv *priv = vb2_get_drv_priv(vq);
464 	struct v4l2_pix_format *pix = &priv->vdev.fmt.fmt.pix;
465 	unsigned int count = *nbuffers;
466 
467 	if (vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
468 		return -EINVAL;
469 
470 	if (*nplanes) {
471 		if (*nplanes != 1 || sizes[0] < pix->sizeimage)
472 			return -EINVAL;
473 		count += vq->num_buffers;
474 	}
475 
476 	count = min_t(__u32, VID_MEM_LIMIT / pix->sizeimage, count);
477 
478 	if (*nplanes)
479 		*nbuffers = (count < vq->num_buffers) ? 0 :
480 			count - vq->num_buffers;
481 	else
482 		*nbuffers = count;
483 
484 	*nplanes = 1;
485 	sizes[0] = pix->sizeimage;
486 
487 	return 0;
488 }
489 
490 static int capture_buf_init(struct vb2_buffer *vb)
491 {
492 	struct imx_media_buffer *buf = to_imx_media_vb(vb);
493 
494 	INIT_LIST_HEAD(&buf->list);
495 
496 	return 0;
497 }
498 
499 static int capture_buf_prepare(struct vb2_buffer *vb)
500 {
501 	struct vb2_queue *vq = vb->vb2_queue;
502 	struct capture_priv *priv = vb2_get_drv_priv(vq);
503 	struct v4l2_pix_format *pix = &priv->vdev.fmt.fmt.pix;
504 
505 	if (vb2_plane_size(vb, 0) < pix->sizeimage) {
506 		v4l2_err(priv->src_sd,
507 			 "data will not fit into plane (%lu < %lu)\n",
508 			 vb2_plane_size(vb, 0), (long)pix->sizeimage);
509 		return -EINVAL;
510 	}
511 
512 	vb2_set_plane_payload(vb, 0, pix->sizeimage);
513 
514 	return 0;
515 }
516 
517 static void capture_buf_queue(struct vb2_buffer *vb)
518 {
519 	struct capture_priv *priv = vb2_get_drv_priv(vb->vb2_queue);
520 	struct imx_media_buffer *buf = to_imx_media_vb(vb);
521 	unsigned long flags;
522 
523 	spin_lock_irqsave(&priv->q_lock, flags);
524 
525 	list_add_tail(&buf->list, &priv->ready_q);
526 
527 	spin_unlock_irqrestore(&priv->q_lock, flags);
528 }
529 
530 static int capture_validate_fmt(struct capture_priv *priv)
531 {
532 	struct v4l2_subdev_format fmt_src;
533 	const struct imx_media_pixfmt *cc;
534 	struct v4l2_rect compose;
535 	struct v4l2_format f;
536 	int ret;
537 
538 	fmt_src.pad = priv->src_sd_pad;
539 	fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
540 	ret = v4l2_subdev_call(priv->src_sd, pad, get_fmt, NULL, &fmt_src);
541 	if (ret)
542 		return ret;
543 
544 	v4l2_fill_pix_format(&f.fmt.pix, &fmt_src.format);
545 
546 	ret = __capture_try_fmt_vid_cap(priv, &fmt_src, &f, &cc, &compose);
547 	if (ret)
548 		return ret;
549 
550 	return (priv->vdev.fmt.fmt.pix.width != f.fmt.pix.width ||
551 		priv->vdev.fmt.fmt.pix.height != f.fmt.pix.height ||
552 		priv->vdev.cc->cs != cc->cs ||
553 		priv->vdev.compose.width != compose.width ||
554 		priv->vdev.compose.height != compose.height) ? -EINVAL : 0;
555 }
556 
557 static int capture_start_streaming(struct vb2_queue *vq, unsigned int count)
558 {
559 	struct capture_priv *priv = vb2_get_drv_priv(vq);
560 	struct imx_media_buffer *buf, *tmp;
561 	unsigned long flags;
562 	int ret;
563 
564 	ret = capture_validate_fmt(priv);
565 	if (ret) {
566 		v4l2_err(priv->src_sd, "capture format not valid\n");
567 		goto return_bufs;
568 	}
569 
570 	ret = imx_media_pipeline_set_stream(priv->md, &priv->src_sd->entity,
571 					    true);
572 	if (ret) {
573 		v4l2_err(priv->src_sd, "pipeline start failed with %d\n", ret);
574 		goto return_bufs;
575 	}
576 
577 	priv->stop = false;
578 
579 	return 0;
580 
581 return_bufs:
582 	spin_lock_irqsave(&priv->q_lock, flags);
583 	list_for_each_entry_safe(buf, tmp, &priv->ready_q, list) {
584 		list_del(&buf->list);
585 		vb2_buffer_done(&buf->vbuf.vb2_buf, VB2_BUF_STATE_QUEUED);
586 	}
587 	spin_unlock_irqrestore(&priv->q_lock, flags);
588 	return ret;
589 }
590 
591 static void capture_stop_streaming(struct vb2_queue *vq)
592 {
593 	struct capture_priv *priv = vb2_get_drv_priv(vq);
594 	struct imx_media_buffer *frame;
595 	struct imx_media_buffer *tmp;
596 	unsigned long flags;
597 	int ret;
598 
599 	spin_lock_irqsave(&priv->q_lock, flags);
600 	priv->stop = true;
601 	spin_unlock_irqrestore(&priv->q_lock, flags);
602 
603 	ret = imx_media_pipeline_set_stream(priv->md, &priv->src_sd->entity,
604 					    false);
605 	if (ret)
606 		v4l2_warn(priv->src_sd, "pipeline stop failed with %d\n", ret);
607 
608 	/* release all active buffers */
609 	spin_lock_irqsave(&priv->q_lock, flags);
610 	list_for_each_entry_safe(frame, tmp, &priv->ready_q, list) {
611 		list_del(&frame->list);
612 		vb2_buffer_done(&frame->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
613 	}
614 	spin_unlock_irqrestore(&priv->q_lock, flags);
615 }
616 
617 static const struct vb2_ops capture_qops = {
618 	.queue_setup	 = capture_queue_setup,
619 	.buf_init        = capture_buf_init,
620 	.buf_prepare	 = capture_buf_prepare,
621 	.buf_queue	 = capture_buf_queue,
622 	.wait_prepare	 = vb2_ops_wait_prepare,
623 	.wait_finish	 = vb2_ops_wait_finish,
624 	.start_streaming = capture_start_streaming,
625 	.stop_streaming  = capture_stop_streaming,
626 };
627 
628 /*
629  * File operations
630  */
631 static int capture_open(struct file *file)
632 {
633 	struct capture_priv *priv = video_drvdata(file);
634 	struct video_device *vfd = priv->vdev.vfd;
635 	int ret;
636 
637 	if (mutex_lock_interruptible(&priv->mutex))
638 		return -ERESTARTSYS;
639 
640 	ret = v4l2_fh_open(file);
641 	if (ret)
642 		v4l2_err(priv->src_sd, "v4l2_fh_open failed\n");
643 
644 	ret = v4l2_pipeline_pm_use(&vfd->entity, 1);
645 	if (ret)
646 		v4l2_fh_release(file);
647 
648 	mutex_unlock(&priv->mutex);
649 	return ret;
650 }
651 
652 static int capture_release(struct file *file)
653 {
654 	struct capture_priv *priv = video_drvdata(file);
655 	struct video_device *vfd = priv->vdev.vfd;
656 	struct vb2_queue *vq = &priv->q;
657 
658 	mutex_lock(&priv->mutex);
659 
660 	if (file->private_data == vq->owner) {
661 		vb2_queue_release(vq);
662 		vq->owner = NULL;
663 	}
664 
665 	v4l2_pipeline_pm_use(&vfd->entity, 0);
666 
667 	v4l2_fh_release(file);
668 	mutex_unlock(&priv->mutex);
669 	return 0;
670 }
671 
672 static const struct v4l2_file_operations capture_fops = {
673 	.owner		= THIS_MODULE,
674 	.open		= capture_open,
675 	.release	= capture_release,
676 	.poll		= vb2_fop_poll,
677 	.unlocked_ioctl	= video_ioctl2,
678 	.mmap		= vb2_fop_mmap,
679 };
680 
681 static struct video_device capture_videodev = {
682 	.fops		= &capture_fops,
683 	.ioctl_ops	= &capture_ioctl_ops,
684 	.minor		= -1,
685 	.release	= video_device_release,
686 	.vfl_dir	= VFL_DIR_RX,
687 	.tvnorms	= V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
688 	.device_caps	= V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING,
689 };
690 
691 struct imx_media_buffer *
692 imx_media_capture_device_next_buf(struct imx_media_video_dev *vdev)
693 {
694 	struct capture_priv *priv = to_capture_priv(vdev);
695 	struct imx_media_buffer *buf = NULL;
696 	unsigned long flags;
697 
698 	spin_lock_irqsave(&priv->q_lock, flags);
699 
700 	/* get next queued buffer */
701 	if (!list_empty(&priv->ready_q)) {
702 		buf = list_entry(priv->ready_q.next, struct imx_media_buffer,
703 				 list);
704 		list_del(&buf->list);
705 	}
706 
707 	spin_unlock_irqrestore(&priv->q_lock, flags);
708 
709 	return buf;
710 }
711 EXPORT_SYMBOL_GPL(imx_media_capture_device_next_buf);
712 
713 void imx_media_capture_device_error(struct imx_media_video_dev *vdev)
714 {
715 	struct capture_priv *priv = to_capture_priv(vdev);
716 	struct vb2_queue *vq = &priv->q;
717 	unsigned long flags;
718 
719 	if (!vb2_is_streaming(vq))
720 		return;
721 
722 	spin_lock_irqsave(&priv->q_lock, flags);
723 	vb2_queue_error(vq);
724 	spin_unlock_irqrestore(&priv->q_lock, flags);
725 }
726 EXPORT_SYMBOL_GPL(imx_media_capture_device_error);
727 
728 int imx_media_capture_device_register(struct imx_media_video_dev *vdev)
729 {
730 	struct capture_priv *priv = to_capture_priv(vdev);
731 	struct v4l2_subdev *sd = priv->src_sd;
732 	struct v4l2_device *v4l2_dev = sd->v4l2_dev;
733 	struct video_device *vfd = vdev->vfd;
734 	struct vb2_queue *vq = &priv->q;
735 	struct v4l2_subdev_format fmt_src;
736 	int ret;
737 
738 	/* get media device */
739 	priv->md = container_of(v4l2_dev->mdev, struct imx_media_dev, md);
740 
741 	vfd->v4l2_dev = v4l2_dev;
742 
743 	ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
744 	if (ret) {
745 		v4l2_err(sd, "Failed to register video device\n");
746 		return ret;
747 	}
748 
749 	vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
750 	vq->io_modes = VB2_MMAP | VB2_DMABUF;
751 	vq->drv_priv = priv;
752 	vq->buf_struct_size = sizeof(struct imx_media_buffer);
753 	vq->ops = &capture_qops;
754 	vq->mem_ops = &vb2_dma_contig_memops;
755 	vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
756 	vq->lock = &priv->mutex;
757 	vq->min_buffers_needed = 2;
758 	vq->dev = priv->dev;
759 
760 	ret = vb2_queue_init(vq);
761 	if (ret) {
762 		v4l2_err(sd, "vb2_queue_init failed\n");
763 		goto unreg;
764 	}
765 
766 	INIT_LIST_HEAD(&priv->ready_q);
767 
768 	priv->vdev_pad.flags = MEDIA_PAD_FL_SINK;
769 	ret = media_entity_pads_init(&vfd->entity, 1, &priv->vdev_pad);
770 	if (ret) {
771 		v4l2_err(sd, "failed to init dev pad\n");
772 		goto unreg;
773 	}
774 
775 	/* create the link from the src_sd devnode pad to device node */
776 	ret = media_create_pad_link(&sd->entity, priv->src_sd_pad,
777 				    &vfd->entity, 0, 0);
778 	if (ret) {
779 		v4l2_err(sd, "failed to create link to device node\n");
780 		goto unreg;
781 	}
782 
783 	/* setup default format */
784 	fmt_src.pad = priv->src_sd_pad;
785 	fmt_src.which = V4L2_SUBDEV_FORMAT_ACTIVE;
786 	v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt_src);
787 	if (ret) {
788 		v4l2_err(sd, "failed to get src_sd format\n");
789 		goto unreg;
790 	}
791 
792 	vdev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
793 	imx_media_mbus_fmt_to_pix_fmt(&vdev->fmt.fmt.pix,
794 				      &fmt_src.format, NULL);
795 	vdev->compose.width = fmt_src.format.width;
796 	vdev->compose.height = fmt_src.format.height;
797 	vdev->cc = imx_media_find_format(vdev->fmt.fmt.pix.pixelformat,
798 					 CS_SEL_ANY, false);
799 
800 	v4l2_info(sd, "Registered %s as /dev/%s\n", vfd->name,
801 		  video_device_node_name(vfd));
802 
803 	vfd->ctrl_handler = &priv->ctrl_hdlr;
804 
805 	/* add vdev to the video device list */
806 	imx_media_add_video_device(priv->md, vdev);
807 
808 	return 0;
809 unreg:
810 	video_unregister_device(vfd);
811 	return ret;
812 }
813 EXPORT_SYMBOL_GPL(imx_media_capture_device_register);
814 
815 void imx_media_capture_device_unregister(struct imx_media_video_dev *vdev)
816 {
817 	struct capture_priv *priv = to_capture_priv(vdev);
818 	struct video_device *vfd = priv->vdev.vfd;
819 
820 	mutex_lock(&priv->mutex);
821 
822 	if (video_is_registered(vfd)) {
823 		video_unregister_device(vfd);
824 		media_entity_cleanup(&vfd->entity);
825 	}
826 
827 	mutex_unlock(&priv->mutex);
828 }
829 EXPORT_SYMBOL_GPL(imx_media_capture_device_unregister);
830 
831 struct imx_media_video_dev *
832 imx_media_capture_device_init(struct device *dev, struct v4l2_subdev *src_sd,
833 			      int pad)
834 {
835 	struct capture_priv *priv;
836 	struct video_device *vfd;
837 
838 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
839 	if (!priv)
840 		return ERR_PTR(-ENOMEM);
841 
842 	priv->src_sd = src_sd;
843 	priv->src_sd_pad = pad;
844 	priv->dev = dev;
845 
846 	mutex_init(&priv->mutex);
847 	spin_lock_init(&priv->q_lock);
848 
849 	snprintf(capture_videodev.name, sizeof(capture_videodev.name),
850 		 "%s capture", src_sd->name);
851 
852 	vfd = video_device_alloc();
853 	if (!vfd)
854 		return ERR_PTR(-ENOMEM);
855 
856 	*vfd = capture_videodev;
857 	vfd->lock = &priv->mutex;
858 	vfd->queue = &priv->q;
859 	priv->vdev.vfd = vfd;
860 
861 	INIT_LIST_HEAD(&priv->vdev.list);
862 
863 	video_set_drvdata(vfd, priv);
864 
865 	v4l2_ctrl_handler_init(&priv->ctrl_hdlr, 0);
866 
867 	return &priv->vdev;
868 }
869 EXPORT_SYMBOL_GPL(imx_media_capture_device_init);
870 
871 void imx_media_capture_device_remove(struct imx_media_video_dev *vdev)
872 {
873 	struct capture_priv *priv = to_capture_priv(vdev);
874 
875 	v4l2_ctrl_handler_free(&priv->ctrl_hdlr);
876 }
877 EXPORT_SYMBOL_GPL(imx_media_capture_device_remove);
878 
879 MODULE_DESCRIPTION("i.MX5/6 v4l2 video capture interface driver");
880 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
881 MODULE_LICENSE("GPL");
882