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