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