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