GNU Linux-libre 4.9.333-gnu1
[releases.git] / drivers / staging / media / omap4iss / iss_video.c
1 /*
2  * TI OMAP4 ISS V4L2 Driver - Generic video node
3  *
4  * Copyright (C) 2012 Texas Instruments, Inc.
5  *
6  * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/mm.h>
16 #include <linux/pagemap.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/module.h>
21
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-mc.h>
25
26 #include <asm/cacheflush.h>
27
28 #include "iss_video.h"
29 #include "iss.h"
30
31 /* -----------------------------------------------------------------------------
32  * Helper functions
33  */
34
35 static struct iss_format_info formats[] = {
36         { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
37           MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
38           V4L2_PIX_FMT_GREY, 8, "Greyscale 8 bpp", },
39         { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
40           MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
41           V4L2_PIX_FMT_Y10, 10, "Greyscale 10 bpp", },
42         { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
43           MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
44           V4L2_PIX_FMT_Y12, 12, "Greyscale 12 bpp", },
45         { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
46           MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
47           V4L2_PIX_FMT_SBGGR8, 8, "BGGR Bayer 8 bpp", },
48         { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
49           MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
50           V4L2_PIX_FMT_SGBRG8, 8, "GBRG Bayer 8 bpp", },
51         { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
52           MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
53           V4L2_PIX_FMT_SGRBG8, 8, "GRBG Bayer 8 bpp", },
54         { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
55           MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
56           V4L2_PIX_FMT_SRGGB8, 8, "RGGB Bayer 8 bpp", },
57         { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
58           MEDIA_BUS_FMT_SGRBG10_1X10, 0,
59           V4L2_PIX_FMT_SGRBG10DPCM8, 8, "GRBG Bayer 10 bpp DPCM8",  },
60         { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
61           MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
62           V4L2_PIX_FMT_SBGGR10, 10, "BGGR Bayer 10 bpp", },
63         { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
64           MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
65           V4L2_PIX_FMT_SGBRG10, 10, "GBRG Bayer 10 bpp", },
66         { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
67           MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
68           V4L2_PIX_FMT_SGRBG10, 10, "GRBG Bayer 10 bpp", },
69         { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
70           MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
71           V4L2_PIX_FMT_SRGGB10, 10, "RGGB Bayer 10 bpp", },
72         { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
73           MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
74           V4L2_PIX_FMT_SBGGR12, 12, "BGGR Bayer 12 bpp", },
75         { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
76           MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
77           V4L2_PIX_FMT_SGBRG12, 12, "GBRG Bayer 12 bpp", },
78         { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
79           MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
80           V4L2_PIX_FMT_SGRBG12, 12, "GRBG Bayer 12 bpp", },
81         { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
82           MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
83           V4L2_PIX_FMT_SRGGB12, 12, "RGGB Bayer 12 bpp", },
84         { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
85           MEDIA_BUS_FMT_UYVY8_1X16, 0,
86           V4L2_PIX_FMT_UYVY, 16, "YUV 4:2:2 (UYVY)", },
87         { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
88           MEDIA_BUS_FMT_YUYV8_1X16, 0,
89           V4L2_PIX_FMT_YUYV, 16, "YUV 4:2:2 (YUYV)", },
90         { MEDIA_BUS_FMT_YUYV8_1_5X8, MEDIA_BUS_FMT_YUYV8_1_5X8,
91           MEDIA_BUS_FMT_YUYV8_1_5X8, 0,
92           V4L2_PIX_FMT_NV12, 8, "YUV 4:2:0 (NV12)", },
93 };
94
95 const struct iss_format_info *
96 omap4iss_video_format_info(u32 code)
97 {
98         unsigned int i;
99
100         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
101                 if (formats[i].code == code)
102                         return &formats[i];
103         }
104
105         return NULL;
106 }
107
108 /*
109  * iss_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
110  * @video: ISS video instance
111  * @mbus: v4l2_mbus_framefmt format (input)
112  * @pix: v4l2_pix_format format (output)
113  *
114  * Fill the output pix structure with information from the input mbus format.
115  * The bytesperline and sizeimage fields are computed from the requested bytes
116  * per line value in the pix format and information from the video instance.
117  *
118  * Return the number of padding bytes at end of line.
119  */
120 static unsigned int iss_video_mbus_to_pix(const struct iss_video *video,
121                                           const struct v4l2_mbus_framefmt *mbus,
122                                           struct v4l2_pix_format *pix)
123 {
124         unsigned int bpl = pix->bytesperline;
125         unsigned int min_bpl;
126         unsigned int i;
127
128         memset(pix, 0, sizeof(*pix));
129         pix->width = mbus->width;
130         pix->height = mbus->height;
131
132         /* Skip the last format in the loop so that it will be selected if no
133          * match is found.
134          */
135         for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
136                 if (formats[i].code == mbus->code)
137                         break;
138         }
139
140         min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
141
142         /* Clamp the requested bytes per line value. If the maximum bytes per
143          * line value is zero, the module doesn't support user configurable line
144          * sizes. Override the requested value with the minimum in that case.
145          */
146         if (video->bpl_max)
147                 bpl = clamp(bpl, min_bpl, video->bpl_max);
148         else
149                 bpl = min_bpl;
150
151         if (!video->bpl_zero_padding || bpl != min_bpl)
152                 bpl = ALIGN(bpl, video->bpl_alignment);
153
154         pix->pixelformat = formats[i].pixelformat;
155         pix->bytesperline = bpl;
156         pix->sizeimage = pix->bytesperline * pix->height;
157         pix->colorspace = mbus->colorspace;
158         pix->field = mbus->field;
159
160         /* FIXME: Special case for NV12! We should make this nicer... */
161         if (pix->pixelformat == V4L2_PIX_FMT_NV12)
162                 pix->sizeimage += (pix->bytesperline * pix->height) / 2;
163
164         return bpl - min_bpl;
165 }
166
167 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
168                                   struct v4l2_mbus_framefmt *mbus)
169 {
170         unsigned int i;
171
172         memset(mbus, 0, sizeof(*mbus));
173         mbus->width = pix->width;
174         mbus->height = pix->height;
175
176         /* Skip the last format in the loop so that it will be selected if no
177          * match is found.
178          */
179         for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
180                 if (formats[i].pixelformat == pix->pixelformat)
181                         break;
182         }
183
184         mbus->code = formats[i].code;
185         mbus->colorspace = pix->colorspace;
186         mbus->field = pix->field;
187 }
188
189 static struct v4l2_subdev *
190 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
191 {
192         struct media_pad *remote;
193
194         remote = media_entity_remote_pad(&video->pad);
195
196         if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
197                 return NULL;
198
199         if (pad)
200                 *pad = remote->index;
201
202         return media_entity_to_v4l2_subdev(remote->entity);
203 }
204
205 /* Return a pointer to the ISS video instance at the far end of the pipeline. */
206 static struct iss_video *
207 iss_video_far_end(struct iss_video *video)
208 {
209         struct media_entity_graph graph;
210         struct media_entity *entity = &video->video.entity;
211         struct media_device *mdev = entity->graph_obj.mdev;
212         struct iss_video *far_end = NULL;
213
214         mutex_lock(&mdev->graph_mutex);
215
216         if (media_entity_graph_walk_init(&graph, mdev)) {
217                 mutex_unlock(&mdev->graph_mutex);
218                 return NULL;
219         }
220
221         media_entity_graph_walk_start(&graph, entity);
222
223         while ((entity = media_entity_graph_walk_next(&graph))) {
224                 if (entity == &video->video.entity)
225                         continue;
226
227                 if (!is_media_entity_v4l2_video_device(entity))
228                         continue;
229
230                 far_end = to_iss_video(media_entity_to_video_device(entity));
231                 if (far_end->type != video->type)
232                         break;
233
234                 far_end = NULL;
235         }
236
237         mutex_unlock(&mdev->graph_mutex);
238
239         media_entity_graph_walk_cleanup(&graph);
240
241         return far_end;
242 }
243
244 static int
245 __iss_video_get_format(struct iss_video *video,
246                        struct v4l2_mbus_framefmt *format)
247 {
248         struct v4l2_subdev_format fmt;
249         struct v4l2_subdev *subdev;
250         u32 pad;
251         int ret;
252
253         subdev = iss_video_remote_subdev(video, &pad);
254         if (!subdev)
255                 return -EINVAL;
256
257         memset(&fmt, 0, sizeof(fmt));
258         fmt.pad = pad;
259         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
260
261         mutex_lock(&video->mutex);
262         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
263         mutex_unlock(&video->mutex);
264
265         if (ret)
266                 return ret;
267
268         *format = fmt.format;
269         return 0;
270 }
271
272 static int
273 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
274 {
275         struct v4l2_mbus_framefmt format;
276         struct v4l2_pix_format pixfmt;
277         int ret;
278
279         ret = __iss_video_get_format(video, &format);
280         if (ret < 0)
281                 return ret;
282
283         pixfmt.bytesperline = 0;
284         ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
285
286         if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat ||
287             vfh->format.fmt.pix.height != pixfmt.height ||
288             vfh->format.fmt.pix.width != pixfmt.width ||
289             vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline ||
290             vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage)
291                 return -EINVAL;
292
293         return ret;
294 }
295
296 /* -----------------------------------------------------------------------------
297  * Video queue operations
298  */
299
300 static int iss_video_queue_setup(struct vb2_queue *vq,
301                                  unsigned int *count, unsigned int *num_planes,
302                                  unsigned int sizes[], struct device *alloc_devs[])
303 {
304         struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
305         struct iss_video *video = vfh->video;
306
307         /* Revisit multi-planar support for NV12 */
308         *num_planes = 1;
309
310         sizes[0] = vfh->format.fmt.pix.sizeimage;
311         if (sizes[0] == 0)
312                 return -EINVAL;
313
314         *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
315
316         return 0;
317 }
318
319 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
320 {
321         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
322         struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
323
324         if (buffer->iss_addr)
325                 buffer->iss_addr = 0;
326 }
327
328 static int iss_video_buf_prepare(struct vb2_buffer *vb)
329 {
330         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
331         struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
332         struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
333         struct iss_video *video = vfh->video;
334         unsigned long size = vfh->format.fmt.pix.sizeimage;
335         dma_addr_t addr;
336
337         if (vb2_plane_size(vb, 0) < size)
338                 return -ENOBUFS;
339
340         addr = vb2_dma_contig_plane_dma_addr(vb, 0);
341         if (!IS_ALIGNED(addr, 32)) {
342                 dev_dbg(video->iss->dev,
343                         "Buffer address must be aligned to 32 bytes boundary.\n");
344                 return -EINVAL;
345         }
346
347         vb2_set_plane_payload(vb, 0, size);
348         buffer->iss_addr = addr;
349         return 0;
350 }
351
352 static void iss_video_buf_queue(struct vb2_buffer *vb)
353 {
354         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
355         struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
356         struct iss_video *video = vfh->video;
357         struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
358         struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
359         unsigned long flags;
360         bool empty;
361
362         spin_lock_irqsave(&video->qlock, flags);
363
364         /* Mark the buffer is faulty and give it back to the queue immediately
365          * if the video node has registered an error. vb2 will perform the same
366          * check when preparing the buffer, but that is inherently racy, so we
367          * need to handle the race condition with an authoritative check here.
368          */
369         if (unlikely(video->error)) {
370                 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
371                 spin_unlock_irqrestore(&video->qlock, flags);
372                 return;
373         }
374
375         empty = list_empty(&video->dmaqueue);
376         list_add_tail(&buffer->list, &video->dmaqueue);
377
378         spin_unlock_irqrestore(&video->qlock, flags);
379
380         if (empty) {
381                 enum iss_pipeline_state state;
382                 unsigned int start;
383
384                 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
385                         state = ISS_PIPELINE_QUEUE_OUTPUT;
386                 else
387                         state = ISS_PIPELINE_QUEUE_INPUT;
388
389                 spin_lock_irqsave(&pipe->lock, flags);
390                 pipe->state |= state;
391                 video->ops->queue(video, buffer);
392                 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
393
394                 start = iss_pipeline_ready(pipe);
395                 if (start)
396                         pipe->state |= ISS_PIPELINE_STREAM;
397                 spin_unlock_irqrestore(&pipe->lock, flags);
398
399                 if (start)
400                         omap4iss_pipeline_set_stream(pipe,
401                                                 ISS_PIPELINE_STREAM_SINGLESHOT);
402         }
403 }
404
405 static const struct vb2_ops iss_video_vb2ops = {
406         .queue_setup    = iss_video_queue_setup,
407         .buf_prepare    = iss_video_buf_prepare,
408         .buf_queue      = iss_video_buf_queue,
409         .buf_cleanup    = iss_video_buf_cleanup,
410 };
411
412 /*
413  * omap4iss_video_buffer_next - Complete the current buffer and return the next
414  * @video: ISS video object
415  *
416  * Remove the current video buffer from the DMA queue and fill its timestamp,
417  * field count and state fields before waking up its completion handler.
418  *
419  * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
420  * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
421  *
422  * The DMA queue is expected to contain at least one buffer.
423  *
424  * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
425  * empty.
426  */
427 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
428 {
429         struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
430         enum iss_pipeline_state state;
431         struct iss_buffer *buf;
432         unsigned long flags;
433
434         spin_lock_irqsave(&video->qlock, flags);
435         if (WARN_ON(list_empty(&video->dmaqueue))) {
436                 spin_unlock_irqrestore(&video->qlock, flags);
437                 return NULL;
438         }
439
440         buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
441                                list);
442         list_del(&buf->list);
443         spin_unlock_irqrestore(&video->qlock, flags);
444
445         buf->vb.vb2_buf.timestamp = ktime_get_ns();
446
447         /* Do frame number propagation only if this is the output video node.
448          * Frame number either comes from the CSI receivers or it gets
449          * incremented here if H3A is not active.
450          * Note: There is no guarantee that the output buffer will finish
451          * first, so the input number might lag behind by 1 in some cases.
452          */
453         if (video == pipe->output && !pipe->do_propagation)
454                 buf->vb.sequence =
455                         atomic_inc_return(&pipe->frame_number);
456         else
457                 buf->vb.sequence = atomic_read(&pipe->frame_number);
458
459         vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
460                         VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
461         pipe->error = false;
462
463         spin_lock_irqsave(&video->qlock, flags);
464         if (list_empty(&video->dmaqueue)) {
465                 spin_unlock_irqrestore(&video->qlock, flags);
466                 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
467                         state = ISS_PIPELINE_QUEUE_OUTPUT
468                               | ISS_PIPELINE_STREAM;
469                 else
470                         state = ISS_PIPELINE_QUEUE_INPUT
471                               | ISS_PIPELINE_STREAM;
472
473                 spin_lock_irqsave(&pipe->lock, flags);
474                 pipe->state &= ~state;
475                 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
476                         video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
477                 spin_unlock_irqrestore(&pipe->lock, flags);
478                 return NULL;
479         }
480
481         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) {
482                 spin_lock(&pipe->lock);
483                 pipe->state &= ~ISS_PIPELINE_STREAM;
484                 spin_unlock(&pipe->lock);
485         }
486
487         buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
488                                list);
489         spin_unlock_irqrestore(&video->qlock, flags);
490         buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
491         return buf;
492 }
493
494 /*
495  * omap4iss_video_cancel_stream - Cancel stream on a video node
496  * @video: ISS video object
497  *
498  * Cancelling a stream mark all buffers on the video node as erroneous and makes
499  * sure no new buffer can be queued.
500  */
501 void omap4iss_video_cancel_stream(struct iss_video *video)
502 {
503         unsigned long flags;
504
505         spin_lock_irqsave(&video->qlock, flags);
506
507         while (!list_empty(&video->dmaqueue)) {
508                 struct iss_buffer *buf;
509
510                 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
511                                        list);
512                 list_del(&buf->list);
513                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
514         }
515
516         vb2_queue_error(video->queue);
517         video->error = true;
518
519         spin_unlock_irqrestore(&video->qlock, flags);
520 }
521
522 /* -----------------------------------------------------------------------------
523  * V4L2 ioctls
524  */
525
526 static int
527 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
528 {
529         struct iss_video *video = video_drvdata(file);
530
531         strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
532         strlcpy(cap->card, video->video.name, sizeof(cap->card));
533         strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
534
535         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
536                 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
537         else
538                 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
539
540         cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
541                           | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
542
543         return 0;
544 }
545
546 static int
547 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
548 {
549         struct iss_video *video = video_drvdata(file);
550         struct v4l2_mbus_framefmt format;
551         unsigned int index = f->index;
552         unsigned int i;
553         int ret;
554
555         if (f->type != video->type)
556                 return -EINVAL;
557
558         ret = __iss_video_get_format(video, &format);
559         if (ret < 0)
560                 return ret;
561
562         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
563                 const struct iss_format_info *info = &formats[i];
564
565                 if (format.code != info->code)
566                         continue;
567
568                 if (index == 0) {
569                         f->pixelformat = info->pixelformat;
570                         strlcpy(f->description, info->description,
571                                 sizeof(f->description));
572                         return 0;
573                 }
574
575                 index--;
576         }
577
578         return -EINVAL;
579 }
580
581 static int
582 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
583 {
584         struct iss_video_fh *vfh = to_iss_video_fh(fh);
585         struct iss_video *video = video_drvdata(file);
586
587         if (format->type != video->type)
588                 return -EINVAL;
589
590         mutex_lock(&video->mutex);
591         *format = vfh->format;
592         mutex_unlock(&video->mutex);
593
594         return 0;
595 }
596
597 static int
598 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
599 {
600         struct iss_video_fh *vfh = to_iss_video_fh(fh);
601         struct iss_video *video = video_drvdata(file);
602         struct v4l2_mbus_framefmt fmt;
603
604         if (format->type != video->type)
605                 return -EINVAL;
606
607         mutex_lock(&video->mutex);
608
609         /* Fill the bytesperline and sizeimage fields by converting to media bus
610          * format and back to pixel format.
611          */
612         iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
613         iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
614
615         vfh->format = *format;
616
617         mutex_unlock(&video->mutex);
618         return 0;
619 }
620
621 static int
622 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
623 {
624         struct iss_video *video = video_drvdata(file);
625         struct v4l2_subdev_format fmt;
626         struct v4l2_subdev *subdev;
627         u32 pad;
628         int ret;
629
630         if (format->type != video->type)
631                 return -EINVAL;
632
633         subdev = iss_video_remote_subdev(video, &pad);
634         if (!subdev)
635                 return -EINVAL;
636
637         iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
638
639         fmt.pad = pad;
640         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
641         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
642         if (ret)
643                 return ret;
644
645         iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
646         return 0;
647 }
648
649 static int
650 iss_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
651 {
652         struct iss_video *video = video_drvdata(file);
653         struct v4l2_subdev_format format;
654         struct v4l2_subdev *subdev;
655         struct v4l2_subdev_selection sdsel = {
656                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
657                 .target = sel->target,
658         };
659         u32 pad;
660         int ret;
661
662         switch (sel->target) {
663         case V4L2_SEL_TGT_CROP:
664         case V4L2_SEL_TGT_CROP_BOUNDS:
665         case V4L2_SEL_TGT_CROP_DEFAULT:
666                 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
667                         return -EINVAL;
668                 break;
669         case V4L2_SEL_TGT_COMPOSE:
670         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
671         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
672                 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
673                         return -EINVAL;
674                 break;
675         default:
676                 return -EINVAL;
677         }
678         subdev = iss_video_remote_subdev(video, &pad);
679         if (subdev == NULL)
680                 return -EINVAL;
681
682         /* Try the get selection operation first and fallback to get format if not
683          * implemented.
684          */
685         sdsel.pad = pad;
686         ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
687         if (!ret)
688                 sel->r = sdsel.r;
689         if (ret != -ENOIOCTLCMD)
690                 return ret;
691
692         format.pad = pad;
693         format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
694         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
695         if (ret < 0)
696                 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
697
698         sel->r.left = 0;
699         sel->r.top = 0;
700         sel->r.width = format.format.width;
701         sel->r.height = format.format.height;
702
703         return 0;
704 }
705
706 static int
707 iss_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
708 {
709         struct iss_video *video = video_drvdata(file);
710         struct v4l2_subdev *subdev;
711         struct v4l2_subdev_selection sdsel = {
712                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
713                 .target = sel->target,
714                 .flags = sel->flags,
715                 .r = sel->r,
716         };
717         u32 pad;
718         int ret;
719
720         switch (sel->target) {
721         case V4L2_SEL_TGT_CROP:
722                 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
723                         return -EINVAL;
724                 break;
725         case V4L2_SEL_TGT_COMPOSE:
726                 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
727                         return -EINVAL;
728                 break;
729         default:
730                 return -EINVAL;
731         }
732         subdev = iss_video_remote_subdev(video, &pad);
733         if (subdev == NULL)
734                 return -EINVAL;
735
736         sdsel.pad = pad;
737         mutex_lock(&video->mutex);
738         ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
739         mutex_unlock(&video->mutex);
740         if (!ret)
741                 sel->r = sdsel.r;
742
743         return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
744 }
745
746 static int
747 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
748 {
749         struct iss_video_fh *vfh = to_iss_video_fh(fh);
750         struct iss_video *video = video_drvdata(file);
751
752         if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
753             video->type != a->type)
754                 return -EINVAL;
755
756         memset(a, 0, sizeof(*a));
757         a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
758         a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
759         a->parm.output.timeperframe = vfh->timeperframe;
760
761         return 0;
762 }
763
764 static int
765 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
766 {
767         struct iss_video_fh *vfh = to_iss_video_fh(fh);
768         struct iss_video *video = video_drvdata(file);
769
770         if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
771             video->type != a->type)
772                 return -EINVAL;
773
774         if (a->parm.output.timeperframe.denominator == 0)
775                 a->parm.output.timeperframe.denominator = 1;
776
777         vfh->timeperframe = a->parm.output.timeperframe;
778
779         return 0;
780 }
781
782 static int
783 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
784 {
785         struct iss_video_fh *vfh = to_iss_video_fh(fh);
786
787         return vb2_reqbufs(&vfh->queue, rb);
788 }
789
790 static int
791 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
792 {
793         struct iss_video_fh *vfh = to_iss_video_fh(fh);
794
795         return vb2_querybuf(&vfh->queue, b);
796 }
797
798 static int
799 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
800 {
801         struct iss_video_fh *vfh = to_iss_video_fh(fh);
802
803         return vb2_qbuf(&vfh->queue, b);
804 }
805
806 static int
807 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
808 {
809         struct iss_video_fh *vfh = to_iss_video_fh(fh);
810
811         return vb2_expbuf(&vfh->queue, e);
812 }
813
814 static int
815 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
816 {
817         struct iss_video_fh *vfh = to_iss_video_fh(fh);
818
819         return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
820 }
821
822 /*
823  * Stream management
824  *
825  * Every ISS pipeline has a single input and a single output. The input can be
826  * either a sensor or a video node. The output is always a video node.
827  *
828  * As every pipeline has an output video node, the ISS video objects at the
829  * pipeline output stores the pipeline state. It tracks the streaming state of
830  * both the input and output, as well as the availability of buffers.
831  *
832  * In sensor-to-memory mode, frames are always available at the pipeline input.
833  * Starting the sensor usually requires I2C transfers and must be done in
834  * interruptible context. The pipeline is started and stopped synchronously
835  * to the stream on/off commands. All modules in the pipeline will get their
836  * subdev set stream handler called. The module at the end of the pipeline must
837  * delay starting the hardware until buffers are available at its output.
838  *
839  * In memory-to-memory mode, starting/stopping the stream requires
840  * synchronization between the input and output. ISS modules can't be stopped
841  * in the middle of a frame, and at least some of the modules seem to become
842  * busy as soon as they're started, even if they don't receive a frame start
843  * event. For that reason frames need to be processed in single-shot mode. The
844  * driver needs to wait until a frame is completely processed and written to
845  * memory before restarting the pipeline for the next frame. Pipelined
846  * processing might be possible but requires more testing.
847  *
848  * Stream start must be delayed until buffers are available at both the input
849  * and output. The pipeline must be started in the videobuf queue callback with
850  * the buffers queue spinlock held. The modules subdev set stream operation must
851  * not sleep.
852  */
853 static int
854 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
855 {
856         struct iss_video_fh *vfh = to_iss_video_fh(fh);
857         struct iss_video *video = video_drvdata(file);
858         struct media_entity_graph graph;
859         struct media_entity *entity = &video->video.entity;
860         enum iss_pipeline_state state;
861         struct iss_pipeline *pipe;
862         struct iss_video *far_end;
863         unsigned long flags;
864         int ret;
865
866         if (type != video->type)
867                 return -EINVAL;
868
869         mutex_lock(&video->stream_lock);
870
871         /* Start streaming on the pipeline. No link touching an entity in the
872          * pipeline can be activated or deactivated once streaming is started.
873          */
874         pipe = entity->pipe
875              ? to_iss_pipeline(entity) : &video->pipe;
876         pipe->external = NULL;
877         pipe->external_rate = 0;
878         pipe->external_bpp = 0;
879
880         ret = media_entity_enum_init(&pipe->ent_enum, entity->graph_obj.mdev);
881         if (ret)
882                 goto err_graph_walk_init;
883
884         ret = media_entity_graph_walk_init(&graph, entity->graph_obj.mdev);
885         if (ret)
886                 goto err_graph_walk_init;
887
888         if (video->iss->pdata->set_constraints)
889                 video->iss->pdata->set_constraints(video->iss, true);
890
891         ret = media_entity_pipeline_start(entity, &pipe->pipe);
892         if (ret < 0)
893                 goto err_media_entity_pipeline_start;
894
895         media_entity_graph_walk_start(&graph, entity);
896         while ((entity = media_entity_graph_walk_next(&graph)))
897                 media_entity_enum_set(&pipe->ent_enum, entity);
898
899         /* Verify that the currently configured format matches the output of
900          * the connected subdev.
901          */
902         ret = iss_video_check_format(video, vfh);
903         if (ret < 0)
904                 goto err_iss_video_check_format;
905
906         video->bpl_padding = ret;
907         video->bpl_value = vfh->format.fmt.pix.bytesperline;
908
909         /* Find the ISS video node connected at the far end of the pipeline and
910          * update the pipeline.
911          */
912         far_end = iss_video_far_end(video);
913
914         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
915                 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
916                 pipe->input = far_end;
917                 pipe->output = video;
918         } else {
919                 if (!far_end) {
920                         ret = -EPIPE;
921                         goto err_iss_video_check_format;
922                 }
923
924                 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
925                 pipe->input = video;
926                 pipe->output = far_end;
927         }
928
929         spin_lock_irqsave(&pipe->lock, flags);
930         pipe->state &= ~ISS_PIPELINE_STREAM;
931         pipe->state |= state;
932         spin_unlock_irqrestore(&pipe->lock, flags);
933
934         /* Set the maximum time per frame as the value requested by userspace.
935          * This is a soft limit that can be overridden if the hardware doesn't
936          * support the request limit.
937          */
938         if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
939                 pipe->max_timeperframe = vfh->timeperframe;
940
941         video->queue = &vfh->queue;
942         INIT_LIST_HEAD(&video->dmaqueue);
943         video->error = false;
944         atomic_set(&pipe->frame_number, -1);
945
946         ret = vb2_streamon(&vfh->queue, type);
947         if (ret < 0)
948                 goto err_iss_video_check_format;
949
950         /* In sensor-to-memory mode, the stream can be started synchronously
951          * to the stream on command. In memory-to-memory mode, it will be
952          * started when buffers are queued on both the input and output.
953          */
954         if (!pipe->input) {
955                 unsigned long flags;
956
957                 ret = omap4iss_pipeline_set_stream(pipe,
958                                               ISS_PIPELINE_STREAM_CONTINUOUS);
959                 if (ret < 0)
960                         goto err_omap4iss_set_stream;
961                 spin_lock_irqsave(&video->qlock, flags);
962                 if (list_empty(&video->dmaqueue))
963                         video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
964                 spin_unlock_irqrestore(&video->qlock, flags);
965         }
966
967         media_entity_graph_walk_cleanup(&graph);
968
969         mutex_unlock(&video->stream_lock);
970
971         return 0;
972
973 err_omap4iss_set_stream:
974         vb2_streamoff(&vfh->queue, type);
975 err_iss_video_check_format:
976         media_entity_pipeline_stop(&video->video.entity);
977 err_media_entity_pipeline_start:
978         if (video->iss->pdata->set_constraints)
979                 video->iss->pdata->set_constraints(video->iss, false);
980         video->queue = NULL;
981
982         media_entity_graph_walk_cleanup(&graph);
983
984 err_graph_walk_init:
985         media_entity_enum_cleanup(&pipe->ent_enum);
986
987         mutex_unlock(&video->stream_lock);
988
989         return ret;
990 }
991
992 static int
993 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
994 {
995         struct iss_video_fh *vfh = to_iss_video_fh(fh);
996         struct iss_video *video = video_drvdata(file);
997         struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
998         enum iss_pipeline_state state;
999         unsigned long flags;
1000
1001         if (type != video->type)
1002                 return -EINVAL;
1003
1004         mutex_lock(&video->stream_lock);
1005
1006         if (!vb2_is_streaming(&vfh->queue))
1007                 goto done;
1008
1009         /* Update the pipeline state. */
1010         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1011                 state = ISS_PIPELINE_STREAM_OUTPUT
1012                       | ISS_PIPELINE_QUEUE_OUTPUT;
1013         else
1014                 state = ISS_PIPELINE_STREAM_INPUT
1015                       | ISS_PIPELINE_QUEUE_INPUT;
1016
1017         spin_lock_irqsave(&pipe->lock, flags);
1018         pipe->state &= ~state;
1019         spin_unlock_irqrestore(&pipe->lock, flags);
1020
1021         /* Stop the stream. */
1022         omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
1023         vb2_streamoff(&vfh->queue, type);
1024         video->queue = NULL;
1025
1026         media_entity_enum_cleanup(&pipe->ent_enum);
1027
1028         if (video->iss->pdata->set_constraints)
1029                 video->iss->pdata->set_constraints(video->iss, false);
1030         media_entity_pipeline_stop(&video->video.entity);
1031
1032 done:
1033         mutex_unlock(&video->stream_lock);
1034         return 0;
1035 }
1036
1037 static int
1038 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1039 {
1040         if (input->index > 0)
1041                 return -EINVAL;
1042
1043         strlcpy(input->name, "camera", sizeof(input->name));
1044         input->type = V4L2_INPUT_TYPE_CAMERA;
1045
1046         return 0;
1047 }
1048
1049 static int
1050 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
1051 {
1052         *input = 0;
1053
1054         return 0;
1055 }
1056
1057 static int
1058 iss_video_s_input(struct file *file, void *fh, unsigned int input)
1059 {
1060         return input == 0 ? 0 : -EINVAL;
1061 }
1062
1063 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
1064         .vidioc_querycap                = iss_video_querycap,
1065         .vidioc_enum_fmt_vid_cap        = iss_video_enum_format,
1066         .vidioc_g_fmt_vid_cap           = iss_video_get_format,
1067         .vidioc_s_fmt_vid_cap           = iss_video_set_format,
1068         .vidioc_try_fmt_vid_cap         = iss_video_try_format,
1069         .vidioc_g_fmt_vid_out           = iss_video_get_format,
1070         .vidioc_s_fmt_vid_out           = iss_video_set_format,
1071         .vidioc_try_fmt_vid_out         = iss_video_try_format,
1072         .vidioc_g_selection             = iss_video_get_selection,
1073         .vidioc_s_selection             = iss_video_set_selection,
1074         .vidioc_g_parm                  = iss_video_get_param,
1075         .vidioc_s_parm                  = iss_video_set_param,
1076         .vidioc_reqbufs                 = iss_video_reqbufs,
1077         .vidioc_querybuf                = iss_video_querybuf,
1078         .vidioc_qbuf                    = iss_video_qbuf,
1079         .vidioc_expbuf                  = iss_video_expbuf,
1080         .vidioc_dqbuf                   = iss_video_dqbuf,
1081         .vidioc_streamon                = iss_video_streamon,
1082         .vidioc_streamoff               = iss_video_streamoff,
1083         .vidioc_enum_input              = iss_video_enum_input,
1084         .vidioc_g_input                 = iss_video_g_input,
1085         .vidioc_s_input                 = iss_video_s_input,
1086 };
1087
1088 /* -----------------------------------------------------------------------------
1089  * V4L2 file operations
1090  */
1091
1092 static int iss_video_open(struct file *file)
1093 {
1094         struct iss_video *video = video_drvdata(file);
1095         struct iss_video_fh *handle;
1096         struct vb2_queue *q;
1097         int ret = 0;
1098
1099         handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1100         if (!handle)
1101                 return -ENOMEM;
1102
1103         v4l2_fh_init(&handle->vfh, &video->video);
1104         v4l2_fh_add(&handle->vfh);
1105
1106         /* If this is the first user, initialise the pipeline. */
1107         if (!omap4iss_get(video->iss)) {
1108                 ret = -EBUSY;
1109                 goto done;
1110         }
1111
1112         ret = v4l2_pipeline_pm_use(&video->video.entity, 1);
1113         if (ret < 0) {
1114                 omap4iss_put(video->iss);
1115                 goto done;
1116         }
1117
1118         q = &handle->queue;
1119
1120         q->type = video->type;
1121         q->io_modes = VB2_MMAP | VB2_DMABUF;
1122         q->drv_priv = handle;
1123         q->ops = &iss_video_vb2ops;
1124         q->mem_ops = &vb2_dma_contig_memops;
1125         q->buf_struct_size = sizeof(struct iss_buffer);
1126         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1127         q->dev = video->iss->dev;
1128
1129         ret = vb2_queue_init(q);
1130         if (ret) {
1131                 omap4iss_put(video->iss);
1132                 goto done;
1133         }
1134
1135         memset(&handle->format, 0, sizeof(handle->format));
1136         handle->format.type = video->type;
1137         handle->timeperframe.denominator = 1;
1138
1139         handle->video = video;
1140         file->private_data = &handle->vfh;
1141
1142 done:
1143         if (ret < 0) {
1144                 v4l2_fh_del(&handle->vfh);
1145                 kfree(handle);
1146         }
1147
1148         return ret;
1149 }
1150
1151 static int iss_video_release(struct file *file)
1152 {
1153         struct iss_video *video = video_drvdata(file);
1154         struct v4l2_fh *vfh = file->private_data;
1155         struct iss_video_fh *handle = to_iss_video_fh(vfh);
1156
1157         /* Disable streaming and free the buffers queue resources. */
1158         iss_video_streamoff(file, vfh, video->type);
1159
1160         v4l2_pipeline_pm_use(&video->video.entity, 0);
1161
1162         /* Release the videobuf2 queue */
1163         vb2_queue_release(&handle->queue);
1164
1165         v4l2_fh_del(vfh);
1166         kfree(handle);
1167         file->private_data = NULL;
1168
1169         omap4iss_put(video->iss);
1170
1171         return 0;
1172 }
1173
1174 static unsigned int iss_video_poll(struct file *file, poll_table *wait)
1175 {
1176         struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1177
1178         return vb2_poll(&vfh->queue, file, wait);
1179 }
1180
1181 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1182 {
1183         struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1184
1185         return vb2_mmap(&vfh->queue, vma);
1186 }
1187
1188 static struct v4l2_file_operations iss_video_fops = {
1189         .owner = THIS_MODULE,
1190         .unlocked_ioctl = video_ioctl2,
1191         .open = iss_video_open,
1192         .release = iss_video_release,
1193         .poll = iss_video_poll,
1194         .mmap = iss_video_mmap,
1195 };
1196
1197 /* -----------------------------------------------------------------------------
1198  * ISS video core
1199  */
1200
1201 static const struct iss_video_operations iss_video_dummy_ops = {
1202 };
1203
1204 int omap4iss_video_init(struct iss_video *video, const char *name)
1205 {
1206         const char *direction;
1207         int ret;
1208
1209         switch (video->type) {
1210         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1211                 direction = "output";
1212                 video->pad.flags = MEDIA_PAD_FL_SINK;
1213                 break;
1214         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1215                 direction = "input";
1216                 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1217                 break;
1218
1219         default:
1220                 return -EINVAL;
1221         }
1222
1223         ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1224         if (ret < 0)
1225                 return ret;
1226
1227         spin_lock_init(&video->qlock);
1228         mutex_init(&video->mutex);
1229         atomic_set(&video->active, 0);
1230
1231         spin_lock_init(&video->pipe.lock);
1232         mutex_init(&video->stream_lock);
1233
1234         /* Initialize the video device. */
1235         if (!video->ops)
1236                 video->ops = &iss_video_dummy_ops;
1237
1238         video->video.fops = &iss_video_fops;
1239         snprintf(video->video.name, sizeof(video->video.name),
1240                  "OMAP4 ISS %s %s", name, direction);
1241         video->video.vfl_type = VFL_TYPE_GRABBER;
1242         video->video.release = video_device_release_empty;
1243         video->video.ioctl_ops = &iss_video_ioctl_ops;
1244         video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1245
1246         video_set_drvdata(&video->video, video);
1247
1248         return 0;
1249 }
1250
1251 void omap4iss_video_cleanup(struct iss_video *video)
1252 {
1253         media_entity_cleanup(&video->video.entity);
1254         mutex_destroy(&video->stream_lock);
1255         mutex_destroy(&video->mutex);
1256 }
1257
1258 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1259 {
1260         int ret;
1261
1262         video->video.v4l2_dev = vdev;
1263
1264         ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1265         if (ret < 0)
1266                 dev_err(video->iss->dev,
1267                         "could not register video device (%d)\n", ret);
1268
1269         return ret;
1270 }
1271
1272 void omap4iss_video_unregister(struct iss_video *video)
1273 {
1274         video_unregister_device(&video->video);
1275 }