]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/platform/omap3isp/ispvideo.c
Merge tag 'pstore-v4.8' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
[linux.git] / drivers / media / platform / omap3isp / ispvideo.c
1 /*
2  * ispvideo.c
3  *
4  * TI OMAP3 ISP - Generic video node
5  *
6  * Copyright (C) 2009-2010 Nokia Corporation
7  *
8  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9  *           Sakari Ailus <sakari.ailus@iki.fi>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15
16 #include <asm/cacheflush.h>
17 #include <linux/clk.h>
18 #include <linux/mm.h>
19 #include <linux/module.h>
20 #include <linux/pagemap.h>
21 #include <linux/scatterlist.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25
26 #include <media/v4l2-dev.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/v4l2-mc.h>
29 #include <media/videobuf2-dma-contig.h>
30
31 #include "ispvideo.h"
32 #include "isp.h"
33
34
35 /* -----------------------------------------------------------------------------
36  * Helper functions
37  */
38
39 /*
40  * NOTE: When adding new media bus codes, always remember to add
41  * corresponding in-memory formats to the table below!!!
42  */
43 static struct isp_format_info formats[] = {
44         { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
45           MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
46           V4L2_PIX_FMT_GREY, 8, 1, },
47         { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
48           MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
49           V4L2_PIX_FMT_Y10, 10, 2, },
50         { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
51           MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
52           V4L2_PIX_FMT_Y12, 12, 2, },
53         { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
54           MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
55           V4L2_PIX_FMT_SBGGR8, 8, 1, },
56         { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
57           MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
58           V4L2_PIX_FMT_SGBRG8, 8, 1, },
59         { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
60           MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
61           V4L2_PIX_FMT_SGRBG8, 8, 1, },
62         { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
63           MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
64           V4L2_PIX_FMT_SRGGB8, 8, 1, },
65         { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
66           MEDIA_BUS_FMT_SBGGR10_1X10, 0,
67           V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, },
68         { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
69           MEDIA_BUS_FMT_SGBRG10_1X10, 0,
70           V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, },
71         { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
72           MEDIA_BUS_FMT_SGRBG10_1X10, 0,
73           V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, },
74         { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
75           MEDIA_BUS_FMT_SRGGB10_1X10, 0,
76           V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, },
77         { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
78           MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
79           V4L2_PIX_FMT_SBGGR10, 10, 2, },
80         { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
81           MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
82           V4L2_PIX_FMT_SGBRG10, 10, 2, },
83         { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
84           MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
85           V4L2_PIX_FMT_SGRBG10, 10, 2, },
86         { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
87           MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
88           V4L2_PIX_FMT_SRGGB10, 10, 2, },
89         { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
90           MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
91           V4L2_PIX_FMT_SBGGR12, 12, 2, },
92         { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
93           MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
94           V4L2_PIX_FMT_SGBRG12, 12, 2, },
95         { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
96           MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
97           V4L2_PIX_FMT_SGRBG12, 12, 2, },
98         { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
99           MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
100           V4L2_PIX_FMT_SRGGB12, 12, 2, },
101         { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
102           MEDIA_BUS_FMT_UYVY8_1X16, 0,
103           V4L2_PIX_FMT_UYVY, 16, 2, },
104         { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
105           MEDIA_BUS_FMT_YUYV8_1X16, 0,
106           V4L2_PIX_FMT_YUYV, 16, 2, },
107         { MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_UYVY8_2X8,
108           MEDIA_BUS_FMT_UYVY8_2X8, 0,
109           V4L2_PIX_FMT_UYVY, 8, 2, },
110         { MEDIA_BUS_FMT_YUYV8_2X8, MEDIA_BUS_FMT_YUYV8_2X8,
111           MEDIA_BUS_FMT_YUYV8_2X8, 0,
112           V4L2_PIX_FMT_YUYV, 8, 2, },
113         /* Empty entry to catch the unsupported pixel code (0) used by the CCDC
114          * module and avoid NULL pointer dereferences.
115          */
116         { 0, }
117 };
118
119 const struct isp_format_info *omap3isp_video_format_info(u32 code)
120 {
121         unsigned int i;
122
123         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
124                 if (formats[i].code == code)
125                         return &formats[i];
126         }
127
128         return NULL;
129 }
130
131 /*
132  * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
133  * @video: ISP video instance
134  * @mbus: v4l2_mbus_framefmt format (input)
135  * @pix: v4l2_pix_format format (output)
136  *
137  * Fill the output pix structure with information from the input mbus format.
138  * The bytesperline and sizeimage fields are computed from the requested bytes
139  * per line value in the pix format and information from the video instance.
140  *
141  * Return the number of padding bytes at end of line.
142  */
143 static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
144                                           const struct v4l2_mbus_framefmt *mbus,
145                                           struct v4l2_pix_format *pix)
146 {
147         unsigned int bpl = pix->bytesperline;
148         unsigned int min_bpl;
149         unsigned int i;
150
151         memset(pix, 0, sizeof(*pix));
152         pix->width = mbus->width;
153         pix->height = mbus->height;
154
155         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
156                 if (formats[i].code == mbus->code)
157                         break;
158         }
159
160         if (WARN_ON(i == ARRAY_SIZE(formats)))
161                 return 0;
162
163         min_bpl = pix->width * formats[i].bpp;
164
165         /* Clamp the requested bytes per line value. If the maximum bytes per
166          * line value is zero, the module doesn't support user configurable line
167          * sizes. Override the requested value with the minimum in that case.
168          */
169         if (video->bpl_max)
170                 bpl = clamp(bpl, min_bpl, video->bpl_max);
171         else
172                 bpl = min_bpl;
173
174         if (!video->bpl_zero_padding || bpl != min_bpl)
175                 bpl = ALIGN(bpl, video->bpl_alignment);
176
177         pix->pixelformat = formats[i].pixelformat;
178         pix->bytesperline = bpl;
179         pix->sizeimage = pix->bytesperline * pix->height;
180         pix->colorspace = mbus->colorspace;
181         pix->field = mbus->field;
182
183         return bpl - min_bpl;
184 }
185
186 static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
187                                   struct v4l2_mbus_framefmt *mbus)
188 {
189         unsigned int i;
190
191         memset(mbus, 0, sizeof(*mbus));
192         mbus->width = pix->width;
193         mbus->height = pix->height;
194
195         /* Skip the last format in the loop so that it will be selected if no
196          * match is found.
197          */
198         for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
199                 if (formats[i].pixelformat == pix->pixelformat)
200                         break;
201         }
202
203         mbus->code = formats[i].code;
204         mbus->colorspace = pix->colorspace;
205         mbus->field = pix->field;
206 }
207
208 static struct v4l2_subdev *
209 isp_video_remote_subdev(struct isp_video *video, u32 *pad)
210 {
211         struct media_pad *remote;
212
213         remote = media_entity_remote_pad(&video->pad);
214
215         if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
216                 return NULL;
217
218         if (pad)
219                 *pad = remote->index;
220
221         return media_entity_to_v4l2_subdev(remote->entity);
222 }
223
224 /* Return a pointer to the ISP video instance at the far end of the pipeline. */
225 static int isp_video_get_graph_data(struct isp_video *video,
226                                     struct isp_pipeline *pipe)
227 {
228         struct media_entity_graph graph;
229         struct media_entity *entity = &video->video.entity;
230         struct media_device *mdev = entity->graph_obj.mdev;
231         struct isp_video *far_end = NULL;
232         int ret;
233
234         mutex_lock(&mdev->graph_mutex);
235         ret = media_entity_graph_walk_init(&graph, entity->graph_obj.mdev);
236         if (ret) {
237                 mutex_unlock(&mdev->graph_mutex);
238                 return ret;
239         }
240
241         media_entity_graph_walk_start(&graph, entity);
242
243         while ((entity = media_entity_graph_walk_next(&graph))) {
244                 struct isp_video *__video;
245
246                 media_entity_enum_set(&pipe->ent_enum, entity);
247
248                 if (far_end != NULL)
249                         continue;
250
251                 if (entity == &video->video.entity)
252                         continue;
253
254                 if (!is_media_entity_v4l2_video_device(entity))
255                         continue;
256
257                 __video = to_isp_video(media_entity_to_video_device(entity));
258                 if (__video->type != video->type)
259                         far_end = __video;
260         }
261
262         mutex_unlock(&mdev->graph_mutex);
263
264         media_entity_graph_walk_cleanup(&graph);
265
266         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
267                 pipe->input = far_end;
268                 pipe->output = video;
269         } else {
270                 if (far_end == NULL)
271                         return -EPIPE;
272
273                 pipe->input = video;
274                 pipe->output = far_end;
275         }
276
277         return 0;
278 }
279
280 static int
281 __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
282 {
283         struct v4l2_subdev_format fmt;
284         struct v4l2_subdev *subdev;
285         u32 pad;
286         int ret;
287
288         subdev = isp_video_remote_subdev(video, &pad);
289         if (subdev == NULL)
290                 return -EINVAL;
291
292         fmt.pad = pad;
293         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
294
295         mutex_lock(&video->mutex);
296         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
297         mutex_unlock(&video->mutex);
298
299         if (ret)
300                 return ret;
301
302         format->type = video->type;
303         return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
304 }
305
306 static int
307 isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
308 {
309         struct v4l2_format format;
310         int ret;
311
312         memcpy(&format, &vfh->format, sizeof(format));
313         ret = __isp_video_get_format(video, &format);
314         if (ret < 0)
315                 return ret;
316
317         if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
318             vfh->format.fmt.pix.height != format.fmt.pix.height ||
319             vfh->format.fmt.pix.width != format.fmt.pix.width ||
320             vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
321             vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage ||
322             vfh->format.fmt.pix.field != format.fmt.pix.field)
323                 return -EINVAL;
324
325         return 0;
326 }
327
328 /* -----------------------------------------------------------------------------
329  * Video queue operations
330  */
331
332 static int isp_video_queue_setup(struct vb2_queue *queue,
333                                  unsigned int *count, unsigned int *num_planes,
334                                  unsigned int sizes[], void *alloc_ctxs[])
335 {
336         struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
337         struct isp_video *video = vfh->video;
338
339         *num_planes = 1;
340
341         sizes[0] = vfh->format.fmt.pix.sizeimage;
342         if (sizes[0] == 0)
343                 return -EINVAL;
344
345         alloc_ctxs[0] = video->alloc_ctx;
346
347         *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
348
349         return 0;
350 }
351
352 static int isp_video_buffer_prepare(struct vb2_buffer *buf)
353 {
354         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf);
355         struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
356         struct isp_buffer *buffer = to_isp_buffer(vbuf);
357         struct isp_video *video = vfh->video;
358         dma_addr_t addr;
359
360         /* Refuse to prepare the buffer is the video node has registered an
361          * error. We don't need to take any lock here as the operation is
362          * inherently racy. The authoritative check will be performed in the
363          * queue handler, which can't return an error, this check is just a best
364          * effort to notify userspace as early as possible.
365          */
366         if (unlikely(video->error))
367                 return -EIO;
368
369         addr = vb2_dma_contig_plane_dma_addr(buf, 0);
370         if (!IS_ALIGNED(addr, 32)) {
371                 dev_dbg(video->isp->dev,
372                         "Buffer address must be aligned to 32 bytes boundary.\n");
373                 return -EINVAL;
374         }
375
376         vb2_set_plane_payload(&buffer->vb.vb2_buf, 0,
377                               vfh->format.fmt.pix.sizeimage);
378         buffer->dma = addr;
379
380         return 0;
381 }
382
383 /*
384  * isp_video_buffer_queue - Add buffer to streaming queue
385  * @buf: Video buffer
386  *
387  * In memory-to-memory mode, start streaming on the pipeline if buffers are
388  * queued on both the input and the output, if the pipeline isn't already busy.
389  * If the pipeline is busy, it will be restarted in the output module interrupt
390  * handler.
391  */
392 static void isp_video_buffer_queue(struct vb2_buffer *buf)
393 {
394         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf);
395         struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
396         struct isp_buffer *buffer = to_isp_buffer(vbuf);
397         struct isp_video *video = vfh->video;
398         struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
399         enum isp_pipeline_state state;
400         unsigned long flags;
401         unsigned int empty;
402         unsigned int start;
403
404         spin_lock_irqsave(&video->irqlock, flags);
405
406         if (unlikely(video->error)) {
407                 vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_ERROR);
408                 spin_unlock_irqrestore(&video->irqlock, flags);
409                 return;
410         }
411
412         empty = list_empty(&video->dmaqueue);
413         list_add_tail(&buffer->irqlist, &video->dmaqueue);
414
415         spin_unlock_irqrestore(&video->irqlock, flags);
416
417         if (empty) {
418                 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
419                         state = ISP_PIPELINE_QUEUE_OUTPUT;
420                 else
421                         state = ISP_PIPELINE_QUEUE_INPUT;
422
423                 spin_lock_irqsave(&pipe->lock, flags);
424                 pipe->state |= state;
425                 video->ops->queue(video, buffer);
426                 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
427
428                 start = isp_pipeline_ready(pipe);
429                 if (start)
430                         pipe->state |= ISP_PIPELINE_STREAM;
431                 spin_unlock_irqrestore(&pipe->lock, flags);
432
433                 if (start)
434                         omap3isp_pipeline_set_stream(pipe,
435                                                 ISP_PIPELINE_STREAM_SINGLESHOT);
436         }
437 }
438
439 /*
440  * omap3isp_video_return_buffers - Return all queued buffers to videobuf2
441  * @video: ISP video object
442  * @state: new state for the returned buffers
443  *
444  * Return all buffers queued on the video node to videobuf2 in the given state.
445  * The buffer state should be VB2_BUF_STATE_QUEUED if called due to an error
446  * when starting the stream, or VB2_BUF_STATE_ERROR otherwise.
447  *
448  * The function must be called with the video irqlock held.
449  */
450 static void omap3isp_video_return_buffers(struct isp_video *video,
451                                           enum vb2_buffer_state state)
452 {
453         while (!list_empty(&video->dmaqueue)) {
454                 struct isp_buffer *buf;
455
456                 buf = list_first_entry(&video->dmaqueue,
457                                        struct isp_buffer, irqlist);
458                 list_del(&buf->irqlist);
459                 vb2_buffer_done(&buf->vb.vb2_buf, state);
460         }
461 }
462
463 static int isp_video_start_streaming(struct vb2_queue *queue,
464                                      unsigned int count)
465 {
466         struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
467         struct isp_video *video = vfh->video;
468         struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
469         unsigned long flags;
470         int ret;
471
472         /* In sensor-to-memory mode, the stream can be started synchronously
473          * to the stream on command. In memory-to-memory mode, it will be
474          * started when buffers are queued on both the input and output.
475          */
476         if (pipe->input)
477                 return 0;
478
479         ret = omap3isp_pipeline_set_stream(pipe,
480                                            ISP_PIPELINE_STREAM_CONTINUOUS);
481         if (ret < 0) {
482                 spin_lock_irqsave(&video->irqlock, flags);
483                 omap3isp_video_return_buffers(video, VB2_BUF_STATE_QUEUED);
484                 spin_unlock_irqrestore(&video->irqlock, flags);
485                 return ret;
486         }
487
488         spin_lock_irqsave(&video->irqlock, flags);
489         if (list_empty(&video->dmaqueue))
490                 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
491         spin_unlock_irqrestore(&video->irqlock, flags);
492
493         return 0;
494 }
495
496 static const struct vb2_ops isp_video_queue_ops = {
497         .queue_setup = isp_video_queue_setup,
498         .buf_prepare = isp_video_buffer_prepare,
499         .buf_queue = isp_video_buffer_queue,
500         .start_streaming = isp_video_start_streaming,
501 };
502
503 /*
504  * omap3isp_video_buffer_next - Complete the current buffer and return the next
505  * @video: ISP video object
506  *
507  * Remove the current video buffer from the DMA queue and fill its timestamp and
508  * field count before handing it back to videobuf2.
509  *
510  * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no
511  * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
512  * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE.
513  *
514  * The DMA queue is expected to contain at least one buffer.
515  *
516  * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
517  * empty.
518  */
519 struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
520 {
521         struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
522         enum vb2_buffer_state vb_state;
523         struct isp_buffer *buf;
524         unsigned long flags;
525
526         spin_lock_irqsave(&video->irqlock, flags);
527         if (WARN_ON(list_empty(&video->dmaqueue))) {
528                 spin_unlock_irqrestore(&video->irqlock, flags);
529                 return NULL;
530         }
531
532         buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
533                                irqlist);
534         list_del(&buf->irqlist);
535         spin_unlock_irqrestore(&video->irqlock, flags);
536
537         buf->vb.vb2_buf.timestamp = ktime_get_ns();
538
539         /* Do frame number propagation only if this is the output video node.
540          * Frame number either comes from the CSI receivers or it gets
541          * incremented here if H3A is not active.
542          * Note: There is no guarantee that the output buffer will finish
543          * first, so the input number might lag behind by 1 in some cases.
544          */
545         if (video == pipe->output && !pipe->do_propagation)
546                 buf->vb.sequence =
547                         atomic_inc_return(&pipe->frame_number);
548         else
549                 buf->vb.sequence = atomic_read(&pipe->frame_number);
550
551         if (pipe->field != V4L2_FIELD_NONE)
552                 buf->vb.sequence /= 2;
553
554         buf->vb.field = pipe->field;
555
556         /* Report pipeline errors to userspace on the capture device side. */
557         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
558                 vb_state = VB2_BUF_STATE_ERROR;
559                 pipe->error = false;
560         } else {
561                 vb_state = VB2_BUF_STATE_DONE;
562         }
563
564         vb2_buffer_done(&buf->vb.vb2_buf, vb_state);
565
566         spin_lock_irqsave(&video->irqlock, flags);
567
568         if (list_empty(&video->dmaqueue)) {
569                 enum isp_pipeline_state state;
570
571                 spin_unlock_irqrestore(&video->irqlock, flags);
572
573                 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
574                         state = ISP_PIPELINE_QUEUE_OUTPUT
575                               | ISP_PIPELINE_STREAM;
576                 else
577                         state = ISP_PIPELINE_QUEUE_INPUT
578                               | ISP_PIPELINE_STREAM;
579
580                 spin_lock_irqsave(&pipe->lock, flags);
581                 pipe->state &= ~state;
582                 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
583                         video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
584                 spin_unlock_irqrestore(&pipe->lock, flags);
585                 return NULL;
586         }
587
588         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
589                 spin_lock(&pipe->lock);
590                 pipe->state &= ~ISP_PIPELINE_STREAM;
591                 spin_unlock(&pipe->lock);
592         }
593
594         buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
595                                irqlist);
596
597         spin_unlock_irqrestore(&video->irqlock, flags);
598
599         return buf;
600 }
601
602 /*
603  * omap3isp_video_cancel_stream - Cancel stream on a video node
604  * @video: ISP video object
605  *
606  * Cancelling a stream returns all buffers queued on the video node to videobuf2
607  * in the erroneous state and makes sure no new buffer can be queued.
608  */
609 void omap3isp_video_cancel_stream(struct isp_video *video)
610 {
611         unsigned long flags;
612
613         spin_lock_irqsave(&video->irqlock, flags);
614         omap3isp_video_return_buffers(video, VB2_BUF_STATE_ERROR);
615         video->error = true;
616         spin_unlock_irqrestore(&video->irqlock, flags);
617 }
618
619 /*
620  * omap3isp_video_resume - Perform resume operation on the buffers
621  * @video: ISP video object
622  * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
623  *
624  * This function is intended to be used on suspend/resume scenario. It
625  * requests video queue layer to discard buffers marked as DONE if it's in
626  * continuous mode and requests ISP modules to queue again the ACTIVE buffer
627  * if there's any.
628  */
629 void omap3isp_video_resume(struct isp_video *video, int continuous)
630 {
631         struct isp_buffer *buf = NULL;
632
633         if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
634                 mutex_lock(&video->queue_lock);
635                 vb2_discard_done(video->queue);
636                 mutex_unlock(&video->queue_lock);
637         }
638
639         if (!list_empty(&video->dmaqueue)) {
640                 buf = list_first_entry(&video->dmaqueue,
641                                        struct isp_buffer, irqlist);
642                 video->ops->queue(video, buf);
643                 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
644         } else {
645                 if (continuous)
646                         video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
647         }
648 }
649
650 /* -----------------------------------------------------------------------------
651  * V4L2 ioctls
652  */
653
654 static int
655 isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
656 {
657         struct isp_video *video = video_drvdata(file);
658
659         strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
660         strlcpy(cap->card, video->video.name, sizeof(cap->card));
661         strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
662
663         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
664                 | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
665
666         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
667                 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
668         else
669                 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
670
671         return 0;
672 }
673
674 static int
675 isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
676 {
677         struct isp_video_fh *vfh = to_isp_video_fh(fh);
678         struct isp_video *video = video_drvdata(file);
679
680         if (format->type != video->type)
681                 return -EINVAL;
682
683         mutex_lock(&video->mutex);
684         *format = vfh->format;
685         mutex_unlock(&video->mutex);
686
687         return 0;
688 }
689
690 static int
691 isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
692 {
693         struct isp_video_fh *vfh = to_isp_video_fh(fh);
694         struct isp_video *video = video_drvdata(file);
695         struct v4l2_mbus_framefmt fmt;
696
697         if (format->type != video->type)
698                 return -EINVAL;
699
700         /* Replace unsupported field orders with sane defaults. */
701         switch (format->fmt.pix.field) {
702         case V4L2_FIELD_NONE:
703                 /* Progressive is supported everywhere. */
704                 break;
705         case V4L2_FIELD_ALTERNATE:
706                 /* ALTERNATE is not supported on output nodes. */
707                 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
708                         format->fmt.pix.field = V4L2_FIELD_NONE;
709                 break;
710         case V4L2_FIELD_INTERLACED:
711                 /* The ISP has no concept of video standard, select the
712                  * top-bottom order when the unqualified interlaced order is
713                  * requested.
714                  */
715                 format->fmt.pix.field = V4L2_FIELD_INTERLACED_TB;
716                 /* Fall-through */
717         case V4L2_FIELD_INTERLACED_TB:
718         case V4L2_FIELD_INTERLACED_BT:
719                 /* Interlaced orders are only supported at the CCDC output. */
720                 if (video != &video->isp->isp_ccdc.video_out)
721                         format->fmt.pix.field = V4L2_FIELD_NONE;
722                 break;
723         case V4L2_FIELD_TOP:
724         case V4L2_FIELD_BOTTOM:
725         case V4L2_FIELD_SEQ_TB:
726         case V4L2_FIELD_SEQ_BT:
727         default:
728                 /* All other field orders are currently unsupported, default to
729                  * progressive.
730                  */
731                 format->fmt.pix.field = V4L2_FIELD_NONE;
732                 break;
733         }
734
735         /* Fill the bytesperline and sizeimage fields by converting to media bus
736          * format and back to pixel format.
737          */
738         isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
739         isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
740
741         mutex_lock(&video->mutex);
742         vfh->format = *format;
743         mutex_unlock(&video->mutex);
744
745         return 0;
746 }
747
748 static int
749 isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
750 {
751         struct isp_video *video = video_drvdata(file);
752         struct v4l2_subdev_format fmt;
753         struct v4l2_subdev *subdev;
754         u32 pad;
755         int ret;
756
757         if (format->type != video->type)
758                 return -EINVAL;
759
760         subdev = isp_video_remote_subdev(video, &pad);
761         if (subdev == NULL)
762                 return -EINVAL;
763
764         isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
765
766         fmt.pad = pad;
767         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
768         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
769         if (ret)
770                 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
771
772         isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
773         return 0;
774 }
775
776 static int
777 isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
778 {
779         struct isp_video *video = video_drvdata(file);
780         struct v4l2_subdev *subdev;
781         int ret;
782
783         subdev = isp_video_remote_subdev(video, NULL);
784         if (subdev == NULL)
785                 return -EINVAL;
786
787         mutex_lock(&video->mutex);
788         ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
789         mutex_unlock(&video->mutex);
790
791         return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
792 }
793
794 static int
795 isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
796 {
797         struct isp_video *video = video_drvdata(file);
798         struct v4l2_subdev_format format;
799         struct v4l2_subdev *subdev;
800         u32 pad;
801         int ret;
802
803         subdev = isp_video_remote_subdev(video, &pad);
804         if (subdev == NULL)
805                 return -EINVAL;
806
807         /* Try the get crop operation first and fallback to get format if not
808          * implemented.
809          */
810         ret = v4l2_subdev_call(subdev, video, g_crop, crop);
811         if (ret != -ENOIOCTLCMD)
812                 return ret;
813
814         format.pad = pad;
815         format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
816         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
817         if (ret < 0)
818                 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
819
820         crop->c.left = 0;
821         crop->c.top = 0;
822         crop->c.width = format.format.width;
823         crop->c.height = format.format.height;
824
825         return 0;
826 }
827
828 static int
829 isp_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
830 {
831         struct isp_video *video = video_drvdata(file);
832         struct v4l2_subdev *subdev;
833         int ret;
834
835         subdev = isp_video_remote_subdev(video, NULL);
836         if (subdev == NULL)
837                 return -EINVAL;
838
839         mutex_lock(&video->mutex);
840         ret = v4l2_subdev_call(subdev, video, s_crop, crop);
841         mutex_unlock(&video->mutex);
842
843         return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
844 }
845
846 static int
847 isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
848 {
849         struct isp_video_fh *vfh = to_isp_video_fh(fh);
850         struct isp_video *video = video_drvdata(file);
851
852         if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
853             video->type != a->type)
854                 return -EINVAL;
855
856         memset(a, 0, sizeof(*a));
857         a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
858         a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
859         a->parm.output.timeperframe = vfh->timeperframe;
860
861         return 0;
862 }
863
864 static int
865 isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
866 {
867         struct isp_video_fh *vfh = to_isp_video_fh(fh);
868         struct isp_video *video = video_drvdata(file);
869
870         if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
871             video->type != a->type)
872                 return -EINVAL;
873
874         if (a->parm.output.timeperframe.denominator == 0)
875                 a->parm.output.timeperframe.denominator = 1;
876
877         vfh->timeperframe = a->parm.output.timeperframe;
878
879         return 0;
880 }
881
882 static int
883 isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
884 {
885         struct isp_video_fh *vfh = to_isp_video_fh(fh);
886         struct isp_video *video = video_drvdata(file);
887         int ret;
888
889         mutex_lock(&video->queue_lock);
890         ret = vb2_reqbufs(&vfh->queue, rb);
891         mutex_unlock(&video->queue_lock);
892
893         return ret;
894 }
895
896 static int
897 isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
898 {
899         struct isp_video_fh *vfh = to_isp_video_fh(fh);
900         struct isp_video *video = video_drvdata(file);
901         int ret;
902
903         mutex_lock(&video->queue_lock);
904         ret = vb2_querybuf(&vfh->queue, b);
905         mutex_unlock(&video->queue_lock);
906
907         return ret;
908 }
909
910 static int
911 isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
912 {
913         struct isp_video_fh *vfh = to_isp_video_fh(fh);
914         struct isp_video *video = video_drvdata(file);
915         int ret;
916
917         mutex_lock(&video->queue_lock);
918         ret = vb2_qbuf(&vfh->queue, b);
919         mutex_unlock(&video->queue_lock);
920
921         return ret;
922 }
923
924 static int
925 isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
926 {
927         struct isp_video_fh *vfh = to_isp_video_fh(fh);
928         struct isp_video *video = video_drvdata(file);
929         int ret;
930
931         mutex_lock(&video->queue_lock);
932         ret = vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
933         mutex_unlock(&video->queue_lock);
934
935         return ret;
936 }
937
938 static int isp_video_check_external_subdevs(struct isp_video *video,
939                                             struct isp_pipeline *pipe)
940 {
941         struct isp_device *isp = video->isp;
942         struct media_entity *ents[] = {
943                 &isp->isp_csi2a.subdev.entity,
944                 &isp->isp_csi2c.subdev.entity,
945                 &isp->isp_ccp2.subdev.entity,
946                 &isp->isp_ccdc.subdev.entity
947         };
948         struct media_pad *source_pad;
949         struct media_entity *source = NULL;
950         struct media_entity *sink;
951         struct v4l2_subdev_format fmt;
952         struct v4l2_ext_controls ctrls;
953         struct v4l2_ext_control ctrl;
954         unsigned int i;
955         int ret;
956
957         /* Memory-to-memory pipelines have no external subdev. */
958         if (pipe->input != NULL)
959                 return 0;
960
961         for (i = 0; i < ARRAY_SIZE(ents); i++) {
962                 /* Is the entity part of the pipeline? */
963                 if (!media_entity_enum_test(&pipe->ent_enum, ents[i]))
964                         continue;
965
966                 /* ISP entities have always sink pad == 0. Find source. */
967                 source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
968                 if (source_pad == NULL)
969                         continue;
970
971                 source = source_pad->entity;
972                 sink = ents[i];
973                 break;
974         }
975
976         if (!source) {
977                 dev_warn(isp->dev, "can't find source, failing now\n");
978                 return -EINVAL;
979         }
980
981         if (!is_media_entity_v4l2_subdev(source))
982                 return 0;
983
984         pipe->external = media_entity_to_v4l2_subdev(source);
985
986         fmt.pad = source_pad->index;
987         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
988         ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
989                                pad, get_fmt, NULL, &fmt);
990         if (unlikely(ret < 0)) {
991                 dev_warn(isp->dev, "get_fmt returned null!\n");
992                 return ret;
993         }
994
995         pipe->external_width =
996                 omap3isp_video_format_info(fmt.format.code)->width;
997
998         memset(&ctrls, 0, sizeof(ctrls));
999         memset(&ctrl, 0, sizeof(ctrl));
1000
1001         ctrl.id = V4L2_CID_PIXEL_RATE;
1002
1003         ctrls.count = 1;
1004         ctrls.controls = &ctrl;
1005
1006         ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
1007         if (ret < 0) {
1008                 dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
1009                          pipe->external->name);
1010                 return ret;
1011         }
1012
1013         pipe->external_rate = ctrl.value64;
1014
1015         if (media_entity_enum_test(&pipe->ent_enum,
1016                                    &isp->isp_ccdc.subdev.entity)) {
1017                 unsigned int rate = UINT_MAX;
1018                 /*
1019                  * Check that maximum allowed CCDC pixel rate isn't
1020                  * exceeded by the pixel rate.
1021                  */
1022                 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
1023                 if (pipe->external_rate > rate)
1024                         return -ENOSPC;
1025         }
1026
1027         return 0;
1028 }
1029
1030 /*
1031  * Stream management
1032  *
1033  * Every ISP pipeline has a single input and a single output. The input can be
1034  * either a sensor or a video node. The output is always a video node.
1035  *
1036  * As every pipeline has an output video node, the ISP video objects at the
1037  * pipeline output stores the pipeline state. It tracks the streaming state of
1038  * both the input and output, as well as the availability of buffers.
1039  *
1040  * In sensor-to-memory mode, frames are always available at the pipeline input.
1041  * Starting the sensor usually requires I2C transfers and must be done in
1042  * interruptible context. The pipeline is started and stopped synchronously
1043  * to the stream on/off commands. All modules in the pipeline will get their
1044  * subdev set stream handler called. The module at the end of the pipeline must
1045  * delay starting the hardware until buffers are available at its output.
1046  *
1047  * In memory-to-memory mode, starting/stopping the stream requires
1048  * synchronization between the input and output. ISP modules can't be stopped
1049  * in the middle of a frame, and at least some of the modules seem to become
1050  * busy as soon as they're started, even if they don't receive a frame start
1051  * event. For that reason frames need to be processed in single-shot mode. The
1052  * driver needs to wait until a frame is completely processed and written to
1053  * memory before restarting the pipeline for the next frame. Pipelined
1054  * processing might be possible but requires more testing.
1055  *
1056  * Stream start must be delayed until buffers are available at both the input
1057  * and output. The pipeline must be started in the videobuf queue callback with
1058  * the buffers queue spinlock held. The modules subdev set stream operation must
1059  * not sleep.
1060  */
1061 static int
1062 isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1063 {
1064         struct isp_video_fh *vfh = to_isp_video_fh(fh);
1065         struct isp_video *video = video_drvdata(file);
1066         enum isp_pipeline_state state;
1067         struct isp_pipeline *pipe;
1068         unsigned long flags;
1069         int ret;
1070
1071         if (type != video->type)
1072                 return -EINVAL;
1073
1074         mutex_lock(&video->stream_lock);
1075
1076         /* Start streaming on the pipeline. No link touching an entity in the
1077          * pipeline can be activated or deactivated once streaming is started.
1078          */
1079         pipe = video->video.entity.pipe
1080              ? to_isp_pipeline(&video->video.entity) : &video->pipe;
1081
1082         ret = media_entity_enum_init(&pipe->ent_enum, &video->isp->media_dev);
1083         if (ret)
1084                 goto err_enum_init;
1085
1086         /* TODO: Implement PM QoS */
1087         pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1088         pipe->max_rate = pipe->l3_ick;
1089
1090         ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
1091         if (ret < 0)
1092                 goto err_pipeline_start;
1093
1094         /* Verify that the currently configured format matches the output of
1095          * the connected subdev.
1096          */
1097         ret = isp_video_check_format(video, vfh);
1098         if (ret < 0)
1099                 goto err_check_format;
1100
1101         video->bpl_padding = ret;
1102         video->bpl_value = vfh->format.fmt.pix.bytesperline;
1103
1104         ret = isp_video_get_graph_data(video, pipe);
1105         if (ret < 0)
1106                 goto err_check_format;
1107
1108         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1109                 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
1110         else
1111                 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1112
1113         ret = isp_video_check_external_subdevs(video, pipe);
1114         if (ret < 0)
1115                 goto err_check_format;
1116
1117         pipe->error = false;
1118
1119         spin_lock_irqsave(&pipe->lock, flags);
1120         pipe->state &= ~ISP_PIPELINE_STREAM;
1121         pipe->state |= state;
1122         spin_unlock_irqrestore(&pipe->lock, flags);
1123
1124         /* Set the maximum time per frame as the value requested by userspace.
1125          * This is a soft limit that can be overridden if the hardware doesn't
1126          * support the request limit.
1127          */
1128         if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1129                 pipe->max_timeperframe = vfh->timeperframe;
1130
1131         video->queue = &vfh->queue;
1132         INIT_LIST_HEAD(&video->dmaqueue);
1133         atomic_set(&pipe->frame_number, -1);
1134         pipe->field = vfh->format.fmt.pix.field;
1135
1136         mutex_lock(&video->queue_lock);
1137         ret = vb2_streamon(&vfh->queue, type);
1138         mutex_unlock(&video->queue_lock);
1139         if (ret < 0)
1140                 goto err_check_format;
1141
1142         mutex_unlock(&video->stream_lock);
1143
1144         return 0;
1145
1146 err_check_format:
1147         media_entity_pipeline_stop(&video->video.entity);
1148 err_pipeline_start:
1149         /* TODO: Implement PM QoS */
1150         /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1151          * will get triggered the next time the CCDC is powered up will try to
1152          * access buffers that might have been freed but still present in the
1153          * DMA queue. This can easily get triggered if the above
1154          * omap3isp_pipeline_set_stream() call fails on a system with a
1155          * free-running sensor.
1156          */
1157         INIT_LIST_HEAD(&video->dmaqueue);
1158         video->queue = NULL;
1159
1160         media_entity_enum_cleanup(&pipe->ent_enum);
1161
1162 err_enum_init:
1163         mutex_unlock(&video->stream_lock);
1164
1165         return ret;
1166 }
1167
1168 static int
1169 isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1170 {
1171         struct isp_video_fh *vfh = to_isp_video_fh(fh);
1172         struct isp_video *video = video_drvdata(file);
1173         struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1174         enum isp_pipeline_state state;
1175         unsigned int streaming;
1176         unsigned long flags;
1177
1178         if (type != video->type)
1179                 return -EINVAL;
1180
1181         mutex_lock(&video->stream_lock);
1182
1183         /* Make sure we're not streaming yet. */
1184         mutex_lock(&video->queue_lock);
1185         streaming = vb2_is_streaming(&vfh->queue);
1186         mutex_unlock(&video->queue_lock);
1187
1188         if (!streaming)
1189                 goto done;
1190
1191         /* Update the pipeline state. */
1192         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1193                 state = ISP_PIPELINE_STREAM_OUTPUT
1194                       | ISP_PIPELINE_QUEUE_OUTPUT;
1195         else
1196                 state = ISP_PIPELINE_STREAM_INPUT
1197                       | ISP_PIPELINE_QUEUE_INPUT;
1198
1199         spin_lock_irqsave(&pipe->lock, flags);
1200         pipe->state &= ~state;
1201         spin_unlock_irqrestore(&pipe->lock, flags);
1202
1203         /* Stop the stream. */
1204         omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1205         omap3isp_video_cancel_stream(video);
1206
1207         mutex_lock(&video->queue_lock);
1208         vb2_streamoff(&vfh->queue, type);
1209         mutex_unlock(&video->queue_lock);
1210         video->queue = NULL;
1211         video->error = false;
1212
1213         /* TODO: Implement PM QoS */
1214         media_entity_pipeline_stop(&video->video.entity);
1215
1216         media_entity_enum_cleanup(&pipe->ent_enum);
1217
1218 done:
1219         mutex_unlock(&video->stream_lock);
1220         return 0;
1221 }
1222
1223 static int
1224 isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1225 {
1226         if (input->index > 0)
1227                 return -EINVAL;
1228
1229         strlcpy(input->name, "camera", sizeof(input->name));
1230         input->type = V4L2_INPUT_TYPE_CAMERA;
1231
1232         return 0;
1233 }
1234
1235 static int
1236 isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1237 {
1238         *input = 0;
1239
1240         return 0;
1241 }
1242
1243 static int
1244 isp_video_s_input(struct file *file, void *fh, unsigned int input)
1245 {
1246         return input == 0 ? 0 : -EINVAL;
1247 }
1248
1249 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1250         .vidioc_querycap                = isp_video_querycap,
1251         .vidioc_g_fmt_vid_cap           = isp_video_get_format,
1252         .vidioc_s_fmt_vid_cap           = isp_video_set_format,
1253         .vidioc_try_fmt_vid_cap         = isp_video_try_format,
1254         .vidioc_g_fmt_vid_out           = isp_video_get_format,
1255         .vidioc_s_fmt_vid_out           = isp_video_set_format,
1256         .vidioc_try_fmt_vid_out         = isp_video_try_format,
1257         .vidioc_cropcap                 = isp_video_cropcap,
1258         .vidioc_g_crop                  = isp_video_get_crop,
1259         .vidioc_s_crop                  = isp_video_set_crop,
1260         .vidioc_g_parm                  = isp_video_get_param,
1261         .vidioc_s_parm                  = isp_video_set_param,
1262         .vidioc_reqbufs                 = isp_video_reqbufs,
1263         .vidioc_querybuf                = isp_video_querybuf,
1264         .vidioc_qbuf                    = isp_video_qbuf,
1265         .vidioc_dqbuf                   = isp_video_dqbuf,
1266         .vidioc_streamon                = isp_video_streamon,
1267         .vidioc_streamoff               = isp_video_streamoff,
1268         .vidioc_enum_input              = isp_video_enum_input,
1269         .vidioc_g_input                 = isp_video_g_input,
1270         .vidioc_s_input                 = isp_video_s_input,
1271 };
1272
1273 /* -----------------------------------------------------------------------------
1274  * V4L2 file operations
1275  */
1276
1277 static int isp_video_open(struct file *file)
1278 {
1279         struct isp_video *video = video_drvdata(file);
1280         struct isp_video_fh *handle;
1281         struct vb2_queue *queue;
1282         int ret = 0;
1283
1284         handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1285         if (handle == NULL)
1286                 return -ENOMEM;
1287
1288         v4l2_fh_init(&handle->vfh, &video->video);
1289         v4l2_fh_add(&handle->vfh);
1290
1291         /* If this is the first user, initialise the pipeline. */
1292         if (omap3isp_get(video->isp) == NULL) {
1293                 ret = -EBUSY;
1294                 goto done;
1295         }
1296
1297         ret = v4l2_pipeline_pm_use(&video->video.entity, 1);
1298         if (ret < 0) {
1299                 omap3isp_put(video->isp);
1300                 goto done;
1301         }
1302
1303         queue = &handle->queue;
1304         queue->type = video->type;
1305         queue->io_modes = VB2_MMAP | VB2_USERPTR;
1306         queue->drv_priv = handle;
1307         queue->ops = &isp_video_queue_ops;
1308         queue->mem_ops = &vb2_dma_contig_memops;
1309         queue->buf_struct_size = sizeof(struct isp_buffer);
1310         queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1311
1312         ret = vb2_queue_init(&handle->queue);
1313         if (ret < 0) {
1314                 omap3isp_put(video->isp);
1315                 goto done;
1316         }
1317
1318         memset(&handle->format, 0, sizeof(handle->format));
1319         handle->format.type = video->type;
1320         handle->timeperframe.denominator = 1;
1321
1322         handle->video = video;
1323         file->private_data = &handle->vfh;
1324
1325 done:
1326         if (ret < 0) {
1327                 v4l2_fh_del(&handle->vfh);
1328                 kfree(handle);
1329         }
1330
1331         return ret;
1332 }
1333
1334 static int isp_video_release(struct file *file)
1335 {
1336         struct isp_video *video = video_drvdata(file);
1337         struct v4l2_fh *vfh = file->private_data;
1338         struct isp_video_fh *handle = to_isp_video_fh(vfh);
1339
1340         /* Disable streaming and free the buffers queue resources. */
1341         isp_video_streamoff(file, vfh, video->type);
1342
1343         mutex_lock(&video->queue_lock);
1344         vb2_queue_release(&handle->queue);
1345         mutex_unlock(&video->queue_lock);
1346
1347         v4l2_pipeline_pm_use(&video->video.entity, 0);
1348
1349         /* Release the file handle. */
1350         v4l2_fh_del(vfh);
1351         kfree(handle);
1352         file->private_data = NULL;
1353
1354         omap3isp_put(video->isp);
1355
1356         return 0;
1357 }
1358
1359 static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1360 {
1361         struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1362         struct isp_video *video = video_drvdata(file);
1363         int ret;
1364
1365         mutex_lock(&video->queue_lock);
1366         ret = vb2_poll(&vfh->queue, file, wait);
1367         mutex_unlock(&video->queue_lock);
1368
1369         return ret;
1370 }
1371
1372 static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1373 {
1374         struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1375
1376         return vb2_mmap(&vfh->queue, vma);
1377 }
1378
1379 static struct v4l2_file_operations isp_video_fops = {
1380         .owner = THIS_MODULE,
1381         .unlocked_ioctl = video_ioctl2,
1382         .open = isp_video_open,
1383         .release = isp_video_release,
1384         .poll = isp_video_poll,
1385         .mmap = isp_video_mmap,
1386 };
1387
1388 /* -----------------------------------------------------------------------------
1389  * ISP video core
1390  */
1391
1392 static const struct isp_video_operations isp_video_dummy_ops = {
1393 };
1394
1395 int omap3isp_video_init(struct isp_video *video, const char *name)
1396 {
1397         const char *direction;
1398         int ret;
1399
1400         switch (video->type) {
1401         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1402                 direction = "output";
1403                 video->pad.flags = MEDIA_PAD_FL_SINK
1404                                    | MEDIA_PAD_FL_MUST_CONNECT;
1405                 break;
1406         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1407                 direction = "input";
1408                 video->pad.flags = MEDIA_PAD_FL_SOURCE
1409                                    | MEDIA_PAD_FL_MUST_CONNECT;
1410                 video->video.vfl_dir = VFL_DIR_TX;
1411                 break;
1412
1413         default:
1414                 return -EINVAL;
1415         }
1416
1417         video->alloc_ctx = vb2_dma_contig_init_ctx(video->isp->dev);
1418         if (IS_ERR(video->alloc_ctx))
1419                 return PTR_ERR(video->alloc_ctx);
1420
1421         ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1422         if (ret < 0) {
1423                 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1424                 return ret;
1425         }
1426
1427         mutex_init(&video->mutex);
1428         atomic_set(&video->active, 0);
1429
1430         spin_lock_init(&video->pipe.lock);
1431         mutex_init(&video->stream_lock);
1432         mutex_init(&video->queue_lock);
1433         spin_lock_init(&video->irqlock);
1434
1435         /* Initialize the video device. */
1436         if (video->ops == NULL)
1437                 video->ops = &isp_video_dummy_ops;
1438
1439         video->video.fops = &isp_video_fops;
1440         snprintf(video->video.name, sizeof(video->video.name),
1441                  "OMAP3 ISP %s %s", name, direction);
1442         video->video.vfl_type = VFL_TYPE_GRABBER;
1443         video->video.release = video_device_release_empty;
1444         video->video.ioctl_ops = &isp_video_ioctl_ops;
1445         video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1446
1447         video_set_drvdata(&video->video, video);
1448
1449         return 0;
1450 }
1451
1452 void omap3isp_video_cleanup(struct isp_video *video)
1453 {
1454         vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1455         media_entity_cleanup(&video->video.entity);
1456         mutex_destroy(&video->queue_lock);
1457         mutex_destroy(&video->stream_lock);
1458         mutex_destroy(&video->mutex);
1459 }
1460
1461 int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1462 {
1463         int ret;
1464
1465         video->video.v4l2_dev = vdev;
1466
1467         ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1468         if (ret < 0)
1469                 dev_err(video->isp->dev,
1470                         "%s: could not register video device (%d)\n",
1471                         __func__, ret);
1472
1473         return ret;
1474 }
1475
1476 void omap3isp_video_unregister(struct isp_video *video)
1477 {
1478         if (video_is_registered(&video->video))
1479                 video_unregister_device(&video->video);
1480 }