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