• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Samsung EXYNOS4x12 FIMC-IS (Imaging Subsystem) driver
3  *
4  * FIMC-IS ISP video input and video output DMA interface driver
5  *
6  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
7  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
8  *
9  * The hardware handling code derived from a driver written by
10  * Younghwan Joo <yhwan.joo@samsung.com>.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  */
16 
17 #include <linux/bitops.h>
18 #include <linux/device.h>
19 #include <linux/delay.h>
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/printk.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/slab.h>
27 #include <linux/videodev2.h>
28 
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/videobuf2-v4l2.h>
32 #include <media/videobuf2-dma-contig.h>
33 #include <media/exynos-fimc.h>
34 
35 #include "common.h"
36 #include "media-dev.h"
37 #include "fimc-is.h"
38 #include "fimc-isp-video.h"
39 #include "fimc-is-param.h"
40 
isp_video_capture_queue_setup(struct vb2_queue * vq,const void * parg,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],void * allocators[])41 static int isp_video_capture_queue_setup(struct vb2_queue *vq,
42 			const void *parg,
43 			unsigned int *num_buffers, unsigned int *num_planes,
44 			unsigned int sizes[], void *allocators[])
45 {
46 	const struct v4l2_format *pfmt = parg;
47 	struct fimc_isp *isp = vb2_get_drv_priv(vq);
48 	struct v4l2_pix_format_mplane *vid_fmt = &isp->video_capture.pixfmt;
49 	const struct v4l2_pix_format_mplane *pixm = NULL;
50 	const struct fimc_fmt *fmt;
51 	unsigned int wh, i;
52 
53 	if (pfmt) {
54 		pixm = &pfmt->fmt.pix_mp;
55 		fmt = fimc_isp_find_format(&pixm->pixelformat, NULL, -1);
56 		wh = pixm->width * pixm->height;
57 	} else {
58 		fmt = isp->video_capture.format;
59 		wh = vid_fmt->width * vid_fmt->height;
60 	}
61 
62 	if (fmt == NULL)
63 		return -EINVAL;
64 
65 	*num_buffers = clamp_t(u32, *num_buffers, FIMC_ISP_REQ_BUFS_MIN,
66 						FIMC_ISP_REQ_BUFS_MAX);
67 	*num_planes = fmt->memplanes;
68 
69 	for (i = 0; i < fmt->memplanes; i++) {
70 		unsigned int size = (wh * fmt->depth[i]) / 8;
71 		if (pixm)
72 			sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
73 		else
74 			sizes[i] = size;
75 		allocators[i] = isp->alloc_ctx;
76 	}
77 
78 	return 0;
79 }
80 
__get_isp_dma2(struct fimc_is * is)81 static inline struct param_dma_output *__get_isp_dma2(struct fimc_is *is)
82 {
83 	return &__get_curr_is_config(is)->isp.dma2_output;
84 }
85 
isp_video_capture_start_streaming(struct vb2_queue * q,unsigned int count)86 static int isp_video_capture_start_streaming(struct vb2_queue *q,
87 						unsigned int count)
88 {
89 	struct fimc_isp *isp = vb2_get_drv_priv(q);
90 	struct fimc_is *is = fimc_isp_to_is(isp);
91 	struct param_dma_output *dma = __get_isp_dma2(is);
92 	struct fimc_is_video *video = &isp->video_capture;
93 	int ret;
94 
95 	if (!test_bit(ST_ISP_VID_CAP_BUF_PREP, &isp->state) ||
96 	    test_bit(ST_ISP_VID_CAP_STREAMING, &isp->state))
97 		return 0;
98 
99 
100 	dma->cmd = DMA_OUTPUT_COMMAND_ENABLE;
101 	dma->notify_dma_done = DMA_OUTPUT_NOTIFY_DMA_DONE_ENABLE;
102 	dma->buffer_address = is->is_dma_p_region +
103 				DMA2_OUTPUT_ADDR_ARRAY_OFFS;
104 	dma->buffer_number = video->reqbufs_count;
105 	dma->dma_out_mask = video->buf_mask;
106 
107 	isp_dbg(2, &video->ve.vdev,
108 		"buf_count: %d, planes: %d, dma addr table: %#x\n",
109 		video->buf_count, video->format->memplanes,
110 		dma->buffer_address);
111 
112 	fimc_is_mem_barrier();
113 
114 	fimc_is_set_param_bit(is, PARAM_ISP_DMA2_OUTPUT);
115 	__fimc_is_hw_update_param(is, PARAM_ISP_DMA2_OUTPUT);
116 
117 	ret = fimc_is_itf_s_param(is, false);
118 	if (ret < 0)
119 		return ret;
120 
121 	ret = fimc_pipeline_call(&video->ve, set_stream, 1);
122 	if (ret < 0)
123 		return ret;
124 
125 	set_bit(ST_ISP_VID_CAP_STREAMING, &isp->state);
126 	return ret;
127 }
128 
isp_video_capture_stop_streaming(struct vb2_queue * q)129 static void isp_video_capture_stop_streaming(struct vb2_queue *q)
130 {
131 	struct fimc_isp *isp = vb2_get_drv_priv(q);
132 	struct fimc_is *is = fimc_isp_to_is(isp);
133 	struct param_dma_output *dma = __get_isp_dma2(is);
134 	int ret;
135 
136 	ret = fimc_pipeline_call(&isp->video_capture.ve, set_stream, 0);
137 	if (ret < 0)
138 		return;
139 
140 	dma->cmd = DMA_OUTPUT_COMMAND_DISABLE;
141 	dma->notify_dma_done = DMA_OUTPUT_NOTIFY_DMA_DONE_DISABLE;
142 	dma->buffer_number = 0;
143 	dma->buffer_address = 0;
144 	dma->dma_out_mask = 0;
145 
146 	fimc_is_set_param_bit(is, PARAM_ISP_DMA2_OUTPUT);
147 	__fimc_is_hw_update_param(is, PARAM_ISP_DMA2_OUTPUT);
148 
149 	ret = fimc_is_itf_s_param(is, false);
150 	if (ret < 0)
151 		dev_warn(&is->pdev->dev, "%s: DMA stop failed\n", __func__);
152 
153 	fimc_is_hw_set_isp_buf_mask(is, 0);
154 
155 	clear_bit(ST_ISP_VID_CAP_BUF_PREP, &isp->state);
156 	clear_bit(ST_ISP_VID_CAP_STREAMING, &isp->state);
157 
158 	isp->video_capture.buf_count = 0;
159 }
160 
isp_video_capture_buffer_prepare(struct vb2_buffer * vb)161 static int isp_video_capture_buffer_prepare(struct vb2_buffer *vb)
162 {
163 	struct fimc_isp *isp = vb2_get_drv_priv(vb->vb2_queue);
164 	struct fimc_is_video *video = &isp->video_capture;
165 	int i;
166 
167 	if (video->format == NULL)
168 		return -EINVAL;
169 
170 	for (i = 0; i < video->format->memplanes; i++) {
171 		unsigned long size = video->pixfmt.plane_fmt[i].sizeimage;
172 
173 		if (vb2_plane_size(vb, i) < size) {
174 			v4l2_err(&video->ve.vdev,
175 				 "User buffer too small (%ld < %ld)\n",
176 				 vb2_plane_size(vb, i), size);
177 			return -EINVAL;
178 		}
179 		vb2_set_plane_payload(vb, i, size);
180 	}
181 
182 	/* Check if we get one of the already known buffers. */
183 	if (test_bit(ST_ISP_VID_CAP_BUF_PREP, &isp->state)) {
184 		dma_addr_t dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
185 		int i;
186 
187 		for (i = 0; i < video->buf_count; i++)
188 			if (video->buffers[i]->dma_addr[0] == dma_addr)
189 				return 0;
190 		return -ENXIO;
191 	}
192 
193 	return 0;
194 }
195 
isp_video_capture_buffer_queue(struct vb2_buffer * vb)196 static void isp_video_capture_buffer_queue(struct vb2_buffer *vb)
197 {
198 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
199 	struct fimc_isp *isp = vb2_get_drv_priv(vb->vb2_queue);
200 	struct fimc_is_video *video = &isp->video_capture;
201 	struct fimc_is *is = fimc_isp_to_is(isp);
202 	struct isp_video_buf *ivb = to_isp_video_buf(vbuf);
203 	unsigned long flags;
204 	unsigned int i;
205 
206 	if (test_bit(ST_ISP_VID_CAP_BUF_PREP, &isp->state)) {
207 		spin_lock_irqsave(&is->slock, flags);
208 		video->buf_mask |= BIT(ivb->index);
209 		spin_unlock_irqrestore(&is->slock, flags);
210 	} else {
211 		unsigned int num_planes = video->format->memplanes;
212 
213 		ivb->index = video->buf_count;
214 		video->buffers[ivb->index] = ivb;
215 
216 		for (i = 0; i < num_planes; i++) {
217 			int buf_index = ivb->index * num_planes + i;
218 
219 			ivb->dma_addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
220 			is->is_p_region->shared[32 + buf_index] =
221 							ivb->dma_addr[i];
222 
223 			isp_dbg(2, &video->ve.vdev,
224 				"dma_buf %d (%d/%d/%d) addr: %pad\n",
225 				buf_index, ivb->index, i, vb->index,
226 				&ivb->dma_addr[i]);
227 		}
228 
229 		if (++video->buf_count < video->reqbufs_count)
230 			return;
231 
232 		video->buf_mask = (1UL << video->buf_count) - 1;
233 		set_bit(ST_ISP_VID_CAP_BUF_PREP, &isp->state);
234 	}
235 
236 	if (!test_bit(ST_ISP_VID_CAP_STREAMING, &isp->state))
237 		isp_video_capture_start_streaming(vb->vb2_queue, 0);
238 }
239 
240 /*
241  * FIMC-IS ISP input and output DMA interface interrupt handler.
242  * Locking: called with is->slock spinlock held.
243  */
fimc_isp_video_irq_handler(struct fimc_is * is)244 void fimc_isp_video_irq_handler(struct fimc_is *is)
245 {
246 	struct fimc_is_video *video = &is->isp.video_capture;
247 	struct vb2_v4l2_buffer *vbuf;
248 	int buf_index;
249 
250 	/* TODO: Ensure the DMA is really stopped in stop_streaming callback */
251 	if (!test_bit(ST_ISP_VID_CAP_STREAMING, &is->isp.state))
252 		return;
253 
254 	buf_index = (is->i2h_cmd.args[1] - 1) % video->buf_count;
255 	vbuf = &video->buffers[buf_index]->vb;
256 
257 	v4l2_get_timestamp(&vbuf->timestamp);
258 	vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
259 
260 	video->buf_mask &= ~BIT(buf_index);
261 	fimc_is_hw_set_isp_buf_mask(is, video->buf_mask);
262 }
263 
264 static const struct vb2_ops isp_video_capture_qops = {
265 	.queue_setup	 = isp_video_capture_queue_setup,
266 	.buf_prepare	 = isp_video_capture_buffer_prepare,
267 	.buf_queue	 = isp_video_capture_buffer_queue,
268 	.wait_prepare	 = vb2_ops_wait_prepare,
269 	.wait_finish	 = vb2_ops_wait_finish,
270 	.start_streaming = isp_video_capture_start_streaming,
271 	.stop_streaming	 = isp_video_capture_stop_streaming,
272 };
273 
isp_video_open(struct file * file)274 static int isp_video_open(struct file *file)
275 {
276 	struct fimc_isp *isp = video_drvdata(file);
277 	struct exynos_video_entity *ve = &isp->video_capture.ve;
278 	struct media_entity *me = &ve->vdev.entity;
279 	int ret;
280 
281 	if (mutex_lock_interruptible(&isp->video_lock))
282 		return -ERESTARTSYS;
283 
284 	ret = v4l2_fh_open(file);
285 	if (ret < 0)
286 		goto unlock;
287 
288 	ret = pm_runtime_get_sync(&isp->pdev->dev);
289 	if (ret < 0)
290 		goto rel_fh;
291 
292 	if (v4l2_fh_is_singular_file(file)) {
293 		mutex_lock(&me->parent->graph_mutex);
294 
295 		ret = fimc_pipeline_call(ve, open, me, true);
296 
297 		/* Mark the video pipeline as in use. */
298 		if (ret == 0)
299 			me->use_count++;
300 
301 		mutex_unlock(&me->parent->graph_mutex);
302 	}
303 	if (!ret)
304 		goto unlock;
305 rel_fh:
306 	v4l2_fh_release(file);
307 unlock:
308 	mutex_unlock(&isp->video_lock);
309 	return ret;
310 }
311 
isp_video_release(struct file * file)312 static int isp_video_release(struct file *file)
313 {
314 	struct fimc_isp *isp = video_drvdata(file);
315 	struct fimc_is_video *ivc = &isp->video_capture;
316 	struct media_entity *entity = &ivc->ve.vdev.entity;
317 	struct media_device *mdev = entity->parent;
318 
319 	mutex_lock(&isp->video_lock);
320 
321 	if (v4l2_fh_is_singular_file(file) && ivc->streaming) {
322 		media_entity_pipeline_stop(entity);
323 		ivc->streaming = 0;
324 	}
325 
326 	_vb2_fop_release(file, NULL);
327 
328 	if (v4l2_fh_is_singular_file(file)) {
329 		fimc_pipeline_call(&ivc->ve, close);
330 
331 		mutex_lock(&mdev->graph_mutex);
332 		entity->use_count--;
333 		mutex_unlock(&mdev->graph_mutex);
334 	}
335 
336 	pm_runtime_put(&isp->pdev->dev);
337 	mutex_unlock(&isp->video_lock);
338 
339 	return 0;
340 }
341 
342 static const struct v4l2_file_operations isp_video_fops = {
343 	.owner		= THIS_MODULE,
344 	.open		= isp_video_open,
345 	.release	= isp_video_release,
346 	.poll		= vb2_fop_poll,
347 	.unlocked_ioctl	= video_ioctl2,
348 	.mmap		= vb2_fop_mmap,
349 };
350 
351 /*
352  * Video node ioctl operations
353  */
isp_video_querycap(struct file * file,void * priv,struct v4l2_capability * cap)354 static int isp_video_querycap(struct file *file, void *priv,
355 					struct v4l2_capability *cap)
356 {
357 	struct fimc_isp *isp = video_drvdata(file);
358 
359 	__fimc_vidioc_querycap(&isp->pdev->dev, cap, V4L2_CAP_STREAMING);
360 	return 0;
361 }
362 
isp_video_enum_fmt_mplane(struct file * file,void * priv,struct v4l2_fmtdesc * f)363 static int isp_video_enum_fmt_mplane(struct file *file, void *priv,
364 					struct v4l2_fmtdesc *f)
365 {
366 	const struct fimc_fmt *fmt;
367 
368 	if (f->index >= FIMC_ISP_NUM_FORMATS)
369 		return -EINVAL;
370 
371 	fmt = fimc_isp_find_format(NULL, NULL, f->index);
372 	if (WARN_ON(fmt == NULL))
373 		return -EINVAL;
374 
375 	strlcpy(f->description, fmt->name, sizeof(f->description));
376 	f->pixelformat = fmt->fourcc;
377 
378 	return 0;
379 }
380 
isp_video_g_fmt_mplane(struct file * file,void * fh,struct v4l2_format * f)381 static int isp_video_g_fmt_mplane(struct file *file, void *fh,
382 					struct v4l2_format *f)
383 {
384 	struct fimc_isp *isp = video_drvdata(file);
385 
386 	f->fmt.pix_mp = isp->video_capture.pixfmt;
387 	return 0;
388 }
389 
__isp_video_try_fmt(struct fimc_isp * isp,struct v4l2_pix_format_mplane * pixm,const struct fimc_fmt ** fmt)390 static void __isp_video_try_fmt(struct fimc_isp *isp,
391 				struct v4l2_pix_format_mplane *pixm,
392 				const struct fimc_fmt **fmt)
393 {
394 	const struct fimc_fmt *__fmt;
395 
396 	__fmt = fimc_isp_find_format(&pixm->pixelformat, NULL, 2);
397 
398 	if (fmt)
399 		*fmt = __fmt;
400 
401 	pixm->colorspace = V4L2_COLORSPACE_SRGB;
402 	pixm->field = V4L2_FIELD_NONE;
403 	pixm->num_planes = __fmt->memplanes;
404 	pixm->pixelformat = __fmt->fourcc;
405 	/*
406 	 * TODO: double check with the docmentation these width/height
407 	 * constraints are correct.
408 	 */
409 	v4l_bound_align_image(&pixm->width, FIMC_ISP_SOURCE_WIDTH_MIN,
410 			      FIMC_ISP_SOURCE_WIDTH_MAX, 3,
411 			      &pixm->height, FIMC_ISP_SOURCE_HEIGHT_MIN,
412 			      FIMC_ISP_SOURCE_HEIGHT_MAX, 0, 0);
413 }
414 
isp_video_try_fmt_mplane(struct file * file,void * fh,struct v4l2_format * f)415 static int isp_video_try_fmt_mplane(struct file *file, void *fh,
416 					struct v4l2_format *f)
417 {
418 	struct fimc_isp *isp = video_drvdata(file);
419 
420 	__isp_video_try_fmt(isp, &f->fmt.pix_mp, NULL);
421 	return 0;
422 }
423 
isp_video_s_fmt_mplane(struct file * file,void * priv,struct v4l2_format * f)424 static int isp_video_s_fmt_mplane(struct file *file, void *priv,
425 					struct v4l2_format *f)
426 {
427 	struct fimc_isp *isp = video_drvdata(file);
428 	struct fimc_is *is = fimc_isp_to_is(isp);
429 	struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
430 	const struct fimc_fmt *ifmt = NULL;
431 	struct param_dma_output *dma = __get_isp_dma2(is);
432 
433 	__isp_video_try_fmt(isp, pixm, &ifmt);
434 
435 	if (WARN_ON(ifmt == NULL))
436 		return -EINVAL;
437 
438 	dma->format = DMA_OUTPUT_FORMAT_BAYER;
439 	dma->order = DMA_OUTPUT_ORDER_GB_BG;
440 	dma->plane = ifmt->memplanes;
441 	dma->bitwidth = ifmt->depth[0];
442 	dma->width = pixm->width;
443 	dma->height = pixm->height;
444 
445 	fimc_is_mem_barrier();
446 
447 	isp->video_capture.format = ifmt;
448 	isp->video_capture.pixfmt = *pixm;
449 
450 	return 0;
451 }
452 
453 /*
454  * Check for source/sink format differences at each link.
455  * Return 0 if the formats match or -EPIPE otherwise.
456  */
isp_video_pipeline_validate(struct fimc_isp * isp)457 static int isp_video_pipeline_validate(struct fimc_isp *isp)
458 {
459 	struct v4l2_subdev *sd = &isp->subdev;
460 	struct v4l2_subdev_format sink_fmt, src_fmt;
461 	struct media_pad *pad;
462 	int ret;
463 
464 	while (1) {
465 		/* Retrieve format at the sink pad */
466 		pad = &sd->entity.pads[0];
467 		if (!(pad->flags & MEDIA_PAD_FL_SINK))
468 			break;
469 		sink_fmt.pad = pad->index;
470 		sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
471 		ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
472 		if (ret < 0 && ret != -ENOIOCTLCMD)
473 			return -EPIPE;
474 
475 		/* Retrieve format at the source pad */
476 		pad = media_entity_remote_pad(pad);
477 		if (pad == NULL ||
478 		    media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
479 			break;
480 
481 		sd = media_entity_to_v4l2_subdev(pad->entity);
482 		src_fmt.pad = pad->index;
483 		src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
484 		ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
485 		if (ret < 0 && ret != -ENOIOCTLCMD)
486 			return -EPIPE;
487 
488 		if (src_fmt.format.width != sink_fmt.format.width ||
489 		    src_fmt.format.height != sink_fmt.format.height ||
490 		    src_fmt.format.code != sink_fmt.format.code)
491 			return -EPIPE;
492 	}
493 
494 	return 0;
495 }
496 
isp_video_streamon(struct file * file,void * priv,enum v4l2_buf_type type)497 static int isp_video_streamon(struct file *file, void *priv,
498 				      enum v4l2_buf_type type)
499 {
500 	struct fimc_isp *isp = video_drvdata(file);
501 	struct exynos_video_entity *ve = &isp->video_capture.ve;
502 	struct media_entity *me = &ve->vdev.entity;
503 	int ret;
504 
505 	ret = media_entity_pipeline_start(me, &ve->pipe->mp);
506 	if (ret < 0)
507 		return ret;
508 
509 	ret = isp_video_pipeline_validate(isp);
510 	if (ret < 0)
511 		goto p_stop;
512 
513 	ret = vb2_ioctl_streamon(file, priv, type);
514 	if (ret < 0)
515 		goto p_stop;
516 
517 	isp->video_capture.streaming = 1;
518 	return 0;
519 p_stop:
520 	media_entity_pipeline_stop(me);
521 	return ret;
522 }
523 
isp_video_streamoff(struct file * file,void * priv,enum v4l2_buf_type type)524 static int isp_video_streamoff(struct file *file, void *priv,
525 					enum v4l2_buf_type type)
526 {
527 	struct fimc_isp *isp = video_drvdata(file);
528 	struct fimc_is_video *video = &isp->video_capture;
529 	int ret;
530 
531 	ret = vb2_ioctl_streamoff(file, priv, type);
532 	if (ret < 0)
533 		return ret;
534 
535 	media_entity_pipeline_stop(&video->ve.vdev.entity);
536 	video->streaming = 0;
537 	return 0;
538 }
539 
isp_video_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * rb)540 static int isp_video_reqbufs(struct file *file, void *priv,
541 				struct v4l2_requestbuffers *rb)
542 {
543 	struct fimc_isp *isp = video_drvdata(file);
544 	int ret;
545 
546 	ret = vb2_ioctl_reqbufs(file, priv, rb);
547 	if (ret < 0)
548 		return ret;
549 
550 	if (rb->count && rb->count < FIMC_ISP_REQ_BUFS_MIN) {
551 		rb->count = 0;
552 		vb2_ioctl_reqbufs(file, priv, rb);
553 		ret = -ENOMEM;
554 	}
555 
556 	isp->video_capture.reqbufs_count = rb->count;
557 	return ret;
558 }
559 
560 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
561 	.vidioc_querycap		= isp_video_querycap,
562 	.vidioc_enum_fmt_vid_cap_mplane	= isp_video_enum_fmt_mplane,
563 	.vidioc_try_fmt_vid_cap_mplane	= isp_video_try_fmt_mplane,
564 	.vidioc_s_fmt_vid_cap_mplane	= isp_video_s_fmt_mplane,
565 	.vidioc_g_fmt_vid_cap_mplane	= isp_video_g_fmt_mplane,
566 	.vidioc_reqbufs			= isp_video_reqbufs,
567 	.vidioc_querybuf		= vb2_ioctl_querybuf,
568 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
569 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
570 	.vidioc_qbuf			= vb2_ioctl_qbuf,
571 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
572 	.vidioc_streamon		= isp_video_streamon,
573 	.vidioc_streamoff		= isp_video_streamoff,
574 };
575 
fimc_isp_video_device_register(struct fimc_isp * isp,struct v4l2_device * v4l2_dev,enum v4l2_buf_type type)576 int fimc_isp_video_device_register(struct fimc_isp *isp,
577 				   struct v4l2_device *v4l2_dev,
578 				   enum v4l2_buf_type type)
579 {
580 	struct vb2_queue *q = &isp->video_capture.vb_queue;
581 	struct fimc_is_video *iv;
582 	struct video_device *vdev;
583 	int ret;
584 
585 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
586 		iv = &isp->video_capture;
587 	else
588 		return -ENOSYS;
589 
590 	mutex_init(&isp->video_lock);
591 	INIT_LIST_HEAD(&iv->pending_buf_q);
592 	INIT_LIST_HEAD(&iv->active_buf_q);
593 	iv->format = fimc_isp_find_format(NULL, NULL, 0);
594 	iv->pixfmt.width = IS_DEFAULT_WIDTH;
595 	iv->pixfmt.height = IS_DEFAULT_HEIGHT;
596 	iv->pixfmt.pixelformat = iv->format->fourcc;
597 	iv->pixfmt.colorspace = V4L2_COLORSPACE_SRGB;
598 	iv->reqbufs_count = 0;
599 
600 	memset(q, 0, sizeof(*q));
601 	q->type = type;
602 	q->io_modes = VB2_MMAP | VB2_USERPTR;
603 	q->ops = &isp_video_capture_qops;
604 	q->mem_ops = &vb2_dma_contig_memops;
605 	q->buf_struct_size = sizeof(struct isp_video_buf);
606 	q->drv_priv = isp;
607 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
608 	q->lock = &isp->video_lock;
609 
610 	ret = vb2_queue_init(q);
611 	if (ret < 0)
612 		return ret;
613 
614 	vdev = &iv->ve.vdev;
615 	memset(vdev, 0, sizeof(*vdev));
616 	snprintf(vdev->name, sizeof(vdev->name), "fimc-is-isp.%s",
617 			type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ?
618 			"capture" : "output");
619 	vdev->queue = q;
620 	vdev->fops = &isp_video_fops;
621 	vdev->ioctl_ops = &isp_video_ioctl_ops;
622 	vdev->v4l2_dev = v4l2_dev;
623 	vdev->minor = -1;
624 	vdev->release = video_device_release_empty;
625 	vdev->lock = &isp->video_lock;
626 
627 	iv->pad.flags = MEDIA_PAD_FL_SINK;
628 	ret = media_entity_init(&vdev->entity, 1, &iv->pad, 0);
629 	if (ret < 0)
630 		return ret;
631 
632 	video_set_drvdata(vdev, isp);
633 
634 	ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
635 	if (ret < 0) {
636 		media_entity_cleanup(&vdev->entity);
637 		return ret;
638 	}
639 
640 	v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
641 		  vdev->name, video_device_node_name(vdev));
642 
643 	return 0;
644 }
645 
fimc_isp_video_device_unregister(struct fimc_isp * isp,enum v4l2_buf_type type)646 void fimc_isp_video_device_unregister(struct fimc_isp *isp,
647 				      enum v4l2_buf_type type)
648 {
649 	struct exynos_video_entity *ve;
650 
651 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
652 		ve = &isp->video_capture.ve;
653 	else
654 		return;
655 
656 	mutex_lock(&isp->video_lock);
657 
658 	if (video_is_registered(&ve->vdev)) {
659 		video_unregister_device(&ve->vdev);
660 		media_entity_cleanup(&ve->vdev.entity);
661 		ve->pipe = NULL;
662 	}
663 
664 	mutex_unlock(&isp->video_lock);
665 }
666