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