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