]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/platform/omap3isp/ispvideo.c
3953aec7c5c5954efbf6a3aedddc110bd6105a13
[karo-tx-linux.git] / drivers / media / platform / omap3isp / ispvideo.c
1 /*
2  * ispvideo.c
3  *
4  * TI OMAP3 ISP - Generic video node
5  *
6  * Copyright (C) 2009-2010 Nokia Corporation
7  *
8  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9  *           Sakari Ailus <sakari.ailus@iki.fi>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  * 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/omap-iommu.h>
31 #include <linux/pagemap.h>
32 #include <linux/scatterlist.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/vmalloc.h>
36 #include <media/v4l2-dev.h>
37 #include <media/v4l2-ioctl.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  * IOMMU management
330  */
331
332 #define IOMMU_FLAG      (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
333
334 /*
335  * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
336  * @dev: Device pointer specific to the OMAP3 ISP.
337  * @sglist: Pointer to source Scatter gather list to allocate.
338  * @sglen: Number of elements of the scatter-gatter list.
339  *
340  * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
341  * we ran out of memory.
342  */
343 static dma_addr_t
344 ispmmu_vmap(struct isp_device *isp, const struct scatterlist *sglist, int sglen)
345 {
346         struct sg_table *sgt;
347         u32 da;
348
349         sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
350         if (sgt == NULL)
351                 return -ENOMEM;
352
353         sgt->sgl = (struct scatterlist *)sglist;
354         sgt->nents = sglen;
355         sgt->orig_nents = sglen;
356
357         da = omap_iommu_vmap(isp->domain, isp->dev, 0, sgt, IOMMU_FLAG);
358         if (IS_ERR_VALUE(da))
359                 kfree(sgt);
360
361         return da;
362 }
363
364 /*
365  * ispmmu_vunmap - Unmap a device address from the ISP MMU
366  * @dev: Device pointer specific to the OMAP3 ISP.
367  * @da: Device address generated from a ispmmu_vmap call.
368  */
369 static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
370 {
371         struct sg_table *sgt;
372
373         sgt = omap_iommu_vunmap(isp->domain, isp->dev, (u32)da);
374         kfree(sgt);
375 }
376
377 /* -----------------------------------------------------------------------------
378  * Video queue operations
379  */
380
381 static void isp_video_queue_prepare(struct isp_video_queue *queue,
382                                     unsigned int *nbuffers, unsigned int *size)
383 {
384         struct isp_video_fh *vfh =
385                 container_of(queue, struct isp_video_fh, queue);
386         struct isp_video *video = vfh->video;
387
388         *size = vfh->format.fmt.pix.sizeimage;
389         if (*size == 0)
390                 return;
391
392         *nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size));
393 }
394
395 static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
396 {
397         struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
398         struct isp_buffer *buffer = to_isp_buffer(buf);
399         struct isp_video *video = vfh->video;
400
401         if (buffer->isp_addr) {
402                 ispmmu_vunmap(video->isp, buffer->isp_addr);
403                 buffer->isp_addr = 0;
404         }
405 }
406
407 static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
408 {
409         struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
410         struct isp_buffer *buffer = to_isp_buffer(buf);
411         struct isp_video *video = vfh->video;
412         unsigned long addr;
413
414         addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen);
415         if (IS_ERR_VALUE(addr))
416                 return -EIO;
417
418         if (!IS_ALIGNED(addr, 32)) {
419                 dev_dbg(video->isp->dev, "Buffer address must be "
420                         "aligned to 32 bytes boundary.\n");
421                 ispmmu_vunmap(video->isp, buffer->isp_addr);
422                 return -EINVAL;
423         }
424
425         buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
426         buffer->isp_addr = addr;
427         return 0;
428 }
429
430 /*
431  * isp_video_buffer_queue - Add buffer to streaming queue
432  * @buf: Video buffer
433  *
434  * In memory-to-memory mode, start streaming on the pipeline if buffers are
435  * queued on both the input and the output, if the pipeline isn't already busy.
436  * If the pipeline is busy, it will be restarted in the output module interrupt
437  * handler.
438  */
439 static void isp_video_buffer_queue(struct isp_video_buffer *buf)
440 {
441         struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
442         struct isp_buffer *buffer = to_isp_buffer(buf);
443         struct isp_video *video = vfh->video;
444         struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
445         enum isp_pipeline_state state;
446         unsigned long flags;
447         unsigned int empty;
448         unsigned int start;
449
450         empty = list_empty(&video->dmaqueue);
451         list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
452
453         if (empty) {
454                 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
455                         state = ISP_PIPELINE_QUEUE_OUTPUT;
456                 else
457                         state = ISP_PIPELINE_QUEUE_INPUT;
458
459                 spin_lock_irqsave(&pipe->lock, flags);
460                 pipe->state |= state;
461                 video->ops->queue(video, buffer);
462                 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
463
464                 start = isp_pipeline_ready(pipe);
465                 if (start)
466                         pipe->state |= ISP_PIPELINE_STREAM;
467                 spin_unlock_irqrestore(&pipe->lock, flags);
468
469                 if (start)
470                         omap3isp_pipeline_set_stream(pipe,
471                                                 ISP_PIPELINE_STREAM_SINGLESHOT);
472         }
473 }
474
475 static const struct isp_video_queue_operations isp_video_queue_ops = {
476         .queue_prepare = &isp_video_queue_prepare,
477         .buffer_prepare = &isp_video_buffer_prepare,
478         .buffer_queue = &isp_video_buffer_queue,
479         .buffer_cleanup = &isp_video_buffer_cleanup,
480 };
481
482 /*
483  * omap3isp_video_buffer_next - Complete the current buffer and return the next
484  * @video: ISP video object
485  *
486  * Remove the current video buffer from the DMA queue and fill its timestamp,
487  * field count and state fields before waking up its completion handler.
488  *
489  * For capture video nodes the buffer state is set to ISP_BUF_STATE_DONE if no
490  * error has been flagged in the pipeline, or to ISP_BUF_STATE_ERROR otherwise.
491  * For video output nodes the buffer state is always set to ISP_BUF_STATE_DONE.
492  *
493  * The DMA queue is expected to contain at least one buffer.
494  *
495  * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
496  * empty.
497  */
498 struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
499 {
500         struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
501         struct isp_video_queue *queue = video->queue;
502         enum isp_pipeline_state state;
503         struct isp_video_buffer *buf;
504         unsigned long flags;
505         struct timespec ts;
506
507         spin_lock_irqsave(&queue->irqlock, flags);
508         if (WARN_ON(list_empty(&video->dmaqueue))) {
509                 spin_unlock_irqrestore(&queue->irqlock, flags);
510                 return NULL;
511         }
512
513         buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
514                                irqlist);
515         list_del(&buf->irqlist);
516         spin_unlock_irqrestore(&queue->irqlock, flags);
517
518         ktime_get_ts(&ts);
519         buf->vbuf.timestamp.tv_sec = ts.tv_sec;
520         buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
521
522         /* Do frame number propagation only if this is the output video node.
523          * Frame number either comes from the CSI receivers or it gets
524          * incremented here if H3A is not active.
525          * Note: There is no guarantee that the output buffer will finish
526          * first, so the input number might lag behind by 1 in some cases.
527          */
528         if (video == pipe->output && !pipe->do_propagation)
529                 buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
530         else
531                 buf->vbuf.sequence = atomic_read(&pipe->frame_number);
532
533         /* Report pipeline errors to userspace on the capture device side. */
534         if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
535                 buf->state = ISP_BUF_STATE_ERROR;
536                 pipe->error = false;
537         } else {
538                 buf->state = ISP_BUF_STATE_DONE;
539         }
540
541         wake_up(&buf->wait);
542
543         if (list_empty(&video->dmaqueue)) {
544                 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
545                         state = ISP_PIPELINE_QUEUE_OUTPUT
546                               | ISP_PIPELINE_STREAM;
547                 else
548                         state = ISP_PIPELINE_QUEUE_INPUT
549                               | ISP_PIPELINE_STREAM;
550
551                 spin_lock_irqsave(&pipe->lock, flags);
552                 pipe->state &= ~state;
553                 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
554                         video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
555                 spin_unlock_irqrestore(&pipe->lock, flags);
556                 return NULL;
557         }
558
559         if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
560                 spin_lock_irqsave(&pipe->lock, flags);
561                 pipe->state &= ~ISP_PIPELINE_STREAM;
562                 spin_unlock_irqrestore(&pipe->lock, flags);
563         }
564
565         buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
566                                irqlist);
567         buf->state = ISP_BUF_STATE_ACTIVE;
568         return to_isp_buffer(buf);
569 }
570
571 /*
572  * omap3isp_video_resume - Perform resume operation on the buffers
573  * @video: ISP video object
574  * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
575  *
576  * This function is intended to be used on suspend/resume scenario. It
577  * requests video queue layer to discard buffers marked as DONE if it's in
578  * continuous mode and requests ISP modules to queue again the ACTIVE buffer
579  * if there's any.
580  */
581 void omap3isp_video_resume(struct isp_video *video, int continuous)
582 {
583         struct isp_buffer *buf = NULL;
584
585         if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
586                 omap3isp_video_queue_discard_done(video->queue);
587
588         if (!list_empty(&video->dmaqueue)) {
589                 buf = list_first_entry(&video->dmaqueue,
590                                        struct isp_buffer, buffer.irqlist);
591                 video->ops->queue(video, buf);
592                 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
593         } else {
594                 if (continuous)
595                         video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
596         }
597 }
598
599 /* -----------------------------------------------------------------------------
600  * V4L2 ioctls
601  */
602
603 static int
604 isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
605 {
606         struct isp_video *video = video_drvdata(file);
607
608         strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
609         strlcpy(cap->card, video->video.name, sizeof(cap->card));
610         strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
611
612         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
613                 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
614         else
615                 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
616
617         return 0;
618 }
619
620 static int
621 isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
622 {
623         struct isp_video_fh *vfh = to_isp_video_fh(fh);
624         struct isp_video *video = video_drvdata(file);
625
626         if (format->type != video->type)
627                 return -EINVAL;
628
629         mutex_lock(&video->mutex);
630         *format = vfh->format;
631         mutex_unlock(&video->mutex);
632
633         return 0;
634 }
635
636 static int
637 isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
638 {
639         struct isp_video_fh *vfh = to_isp_video_fh(fh);
640         struct isp_video *video = video_drvdata(file);
641         struct v4l2_mbus_framefmt fmt;
642
643         if (format->type != video->type)
644                 return -EINVAL;
645
646         mutex_lock(&video->mutex);
647
648         /* Fill the bytesperline and sizeimage fields by converting to media bus
649          * format and back to pixel format.
650          */
651         isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
652         isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
653
654         vfh->format = *format;
655
656         mutex_unlock(&video->mutex);
657         return 0;
658 }
659
660 static int
661 isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
662 {
663         struct isp_video *video = video_drvdata(file);
664         struct v4l2_subdev_format fmt;
665         struct v4l2_subdev *subdev;
666         u32 pad;
667         int ret;
668
669         if (format->type != video->type)
670                 return -EINVAL;
671
672         subdev = isp_video_remote_subdev(video, &pad);
673         if (subdev == NULL)
674                 return -EINVAL;
675
676         isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
677
678         fmt.pad = pad;
679         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
680         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
681         if (ret)
682                 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
683
684         isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
685         return 0;
686 }
687
688 static int
689 isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
690 {
691         struct isp_video *video = video_drvdata(file);
692         struct v4l2_subdev *subdev;
693         int ret;
694
695         subdev = isp_video_remote_subdev(video, NULL);
696         if (subdev == NULL)
697                 return -EINVAL;
698
699         mutex_lock(&video->mutex);
700         ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
701         mutex_unlock(&video->mutex);
702
703         return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
704 }
705
706 static int
707 isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
708 {
709         struct isp_video *video = video_drvdata(file);
710         struct v4l2_subdev_format format;
711         struct v4l2_subdev *subdev;
712         u32 pad;
713         int ret;
714
715         subdev = isp_video_remote_subdev(video, &pad);
716         if (subdev == NULL)
717                 return -EINVAL;
718
719         /* Try the get crop operation first and fallback to get format if not
720          * implemented.
721          */
722         ret = v4l2_subdev_call(subdev, video, g_crop, crop);
723         if (ret != -ENOIOCTLCMD)
724                 return ret;
725
726         format.pad = pad;
727         format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
728         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
729         if (ret < 0)
730                 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
731
732         crop->c.left = 0;
733         crop->c.top = 0;
734         crop->c.width = format.format.width;
735         crop->c.height = format.format.height;
736
737         return 0;
738 }
739
740 static int
741 isp_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
742 {
743         struct isp_video *video = video_drvdata(file);
744         struct v4l2_subdev *subdev;
745         int ret;
746
747         subdev = isp_video_remote_subdev(video, NULL);
748         if (subdev == NULL)
749                 return -EINVAL;
750
751         mutex_lock(&video->mutex);
752         ret = v4l2_subdev_call(subdev, video, s_crop, crop);
753         mutex_unlock(&video->mutex);
754
755         return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
756 }
757
758 static int
759 isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
760 {
761         struct isp_video_fh *vfh = to_isp_video_fh(fh);
762         struct isp_video *video = video_drvdata(file);
763
764         if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
765             video->type != a->type)
766                 return -EINVAL;
767
768         memset(a, 0, sizeof(*a));
769         a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
770         a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
771         a->parm.output.timeperframe = vfh->timeperframe;
772
773         return 0;
774 }
775
776 static int
777 isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
778 {
779         struct isp_video_fh *vfh = to_isp_video_fh(fh);
780         struct isp_video *video = video_drvdata(file);
781
782         if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
783             video->type != a->type)
784                 return -EINVAL;
785
786         if (a->parm.output.timeperframe.denominator == 0)
787                 a->parm.output.timeperframe.denominator = 1;
788
789         vfh->timeperframe = a->parm.output.timeperframe;
790
791         return 0;
792 }
793
794 static int
795 isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
796 {
797         struct isp_video_fh *vfh = to_isp_video_fh(fh);
798
799         return omap3isp_video_queue_reqbufs(&vfh->queue, rb);
800 }
801
802 static int
803 isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
804 {
805         struct isp_video_fh *vfh = to_isp_video_fh(fh);
806
807         return omap3isp_video_queue_querybuf(&vfh->queue, b);
808 }
809
810 static int
811 isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
812 {
813         struct isp_video_fh *vfh = to_isp_video_fh(fh);
814
815         return omap3isp_video_queue_qbuf(&vfh->queue, b);
816 }
817
818 static int
819 isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
820 {
821         struct isp_video_fh *vfh = to_isp_video_fh(fh);
822
823         return omap3isp_video_queue_dqbuf(&vfh->queue, b,
824                                           file->f_flags & O_NONBLOCK);
825 }
826
827 static int isp_video_check_external_subdevs(struct isp_video *video,
828                                             struct isp_pipeline *pipe)
829 {
830         struct isp_device *isp = video->isp;
831         struct media_entity *ents[] = {
832                 &isp->isp_csi2a.subdev.entity,
833                 &isp->isp_csi2c.subdev.entity,
834                 &isp->isp_ccp2.subdev.entity,
835                 &isp->isp_ccdc.subdev.entity
836         };
837         struct media_pad *source_pad;
838         struct media_entity *source = NULL;
839         struct media_entity *sink;
840         struct v4l2_subdev_format fmt;
841         struct v4l2_ext_controls ctrls;
842         struct v4l2_ext_control ctrl;
843         unsigned int i;
844         int ret = 0;
845
846         for (i = 0; i < ARRAY_SIZE(ents); i++) {
847                 /* Is the entity part of the pipeline? */
848                 if (!(pipe->entities & (1 << ents[i]->id)))
849                         continue;
850
851                 /* ISP entities have always sink pad == 0. Find source. */
852                 source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
853                 if (source_pad == NULL)
854                         continue;
855
856                 source = source_pad->entity;
857                 sink = ents[i];
858                 break;
859         }
860
861         if (!source) {
862                 dev_warn(isp->dev, "can't find source, failing now\n");
863                 return ret;
864         }
865
866         if (media_entity_type(source) != MEDIA_ENT_T_V4L2_SUBDEV)
867                 return 0;
868
869         pipe->external = media_entity_to_v4l2_subdev(source);
870
871         fmt.pad = source_pad->index;
872         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
873         ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
874                                pad, get_fmt, NULL, &fmt);
875         if (unlikely(ret < 0)) {
876                 dev_warn(isp->dev, "get_fmt returned null!\n");
877                 return ret;
878         }
879
880         pipe->external_width =
881                 omap3isp_video_format_info(fmt.format.code)->width;
882
883         memset(&ctrls, 0, sizeof(ctrls));
884         memset(&ctrl, 0, sizeof(ctrl));
885
886         ctrl.id = V4L2_CID_PIXEL_RATE;
887
888         ctrls.count = 1;
889         ctrls.controls = &ctrl;
890
891         ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
892         if (ret < 0) {
893                 dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
894                          pipe->external->name);
895                 return ret;
896         }
897
898         pipe->external_rate = ctrl.value64;
899
900         if (pipe->entities & (1 << isp->isp_ccdc.subdev.entity.id)) {
901                 unsigned int rate = UINT_MAX;
902                 /*
903                  * Check that maximum allowed CCDC pixel rate isn't
904                  * exceeded by the pixel rate.
905                  */
906                 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
907                 if (pipe->external_rate > rate)
908                         return -ENOSPC;
909         }
910
911         return 0;
912 }
913
914 /*
915  * Stream management
916  *
917  * Every ISP pipeline has a single input and a single output. The input can be
918  * either a sensor or a video node. The output is always a video node.
919  *
920  * As every pipeline has an output video node, the ISP video objects at the
921  * pipeline output stores the pipeline state. It tracks the streaming state of
922  * both the input and output, as well as the availability of buffers.
923  *
924  * In sensor-to-memory mode, frames are always available at the pipeline input.
925  * Starting the sensor usually requires I2C transfers and must be done in
926  * interruptible context. The pipeline is started and stopped synchronously
927  * to the stream on/off commands. All modules in the pipeline will get their
928  * subdev set stream handler called. The module at the end of the pipeline must
929  * delay starting the hardware until buffers are available at its output.
930  *
931  * In memory-to-memory mode, starting/stopping the stream requires
932  * synchronization between the input and output. ISP modules can't be stopped
933  * in the middle of a frame, and at least some of the modules seem to become
934  * busy as soon as they're started, even if they don't receive a frame start
935  * event. For that reason frames need to be processed in single-shot mode. The
936  * driver needs to wait until a frame is completely processed and written to
937  * memory before restarting the pipeline for the next frame. Pipelined
938  * processing might be possible but requires more testing.
939  *
940  * Stream start must be delayed until buffers are available at both the input
941  * and output. The pipeline must be started in the videobuf queue callback with
942  * the buffers queue spinlock held. The modules subdev set stream operation must
943  * not sleep.
944  */
945 static int
946 isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
947 {
948         struct isp_video_fh *vfh = to_isp_video_fh(fh);
949         struct isp_video *video = video_drvdata(file);
950         enum isp_pipeline_state state;
951         struct isp_pipeline *pipe;
952         unsigned long flags;
953         int ret;
954
955         if (type != video->type)
956                 return -EINVAL;
957
958         mutex_lock(&video->stream_lock);
959
960         if (video->streaming) {
961                 mutex_unlock(&video->stream_lock);
962                 return -EBUSY;
963         }
964
965         /* Start streaming on the pipeline. No link touching an entity in the
966          * pipeline can be activated or deactivated once streaming is started.
967          */
968         pipe = video->video.entity.pipe
969              ? to_isp_pipeline(&video->video.entity) : &video->pipe;
970
971         pipe->entities = 0;
972
973         if (video->isp->pdata->set_constraints)
974                 video->isp->pdata->set_constraints(video->isp, true);
975         pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
976         pipe->max_rate = pipe->l3_ick;
977
978         ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
979         if (ret < 0)
980                 goto err_pipeline_start;
981
982         /* Verify that the currently configured format matches the output of
983          * the connected subdev.
984          */
985         ret = isp_video_check_format(video, vfh);
986         if (ret < 0)
987                 goto err_check_format;
988
989         video->bpl_padding = ret;
990         video->bpl_value = vfh->format.fmt.pix.bytesperline;
991
992         ret = isp_video_get_graph_data(video, pipe);
993         if (ret < 0)
994                 goto err_check_format;
995
996         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
997                 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
998         else
999                 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1000
1001         ret = isp_video_check_external_subdevs(video, pipe);
1002         if (ret < 0)
1003                 goto err_check_format;
1004
1005         pipe->error = false;
1006
1007         spin_lock_irqsave(&pipe->lock, flags);
1008         pipe->state &= ~ISP_PIPELINE_STREAM;
1009         pipe->state |= state;
1010         spin_unlock_irqrestore(&pipe->lock, flags);
1011
1012         /* Set the maximum time per frame as the value requested by userspace.
1013          * This is a soft limit that can be overridden if the hardware doesn't
1014          * support the request limit.
1015          */
1016         if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1017                 pipe->max_timeperframe = vfh->timeperframe;
1018
1019         video->queue = &vfh->queue;
1020         INIT_LIST_HEAD(&video->dmaqueue);
1021         atomic_set(&pipe->frame_number, -1);
1022
1023         ret = omap3isp_video_queue_streamon(&vfh->queue);
1024         if (ret < 0)
1025                 goto err_check_format;
1026
1027         /* In sensor-to-memory mode, the stream can be started synchronously
1028          * to the stream on command. In memory-to-memory mode, it will be
1029          * started when buffers are queued on both the input and output.
1030          */
1031         if (pipe->input == NULL) {
1032                 ret = omap3isp_pipeline_set_stream(pipe,
1033                                               ISP_PIPELINE_STREAM_CONTINUOUS);
1034                 if (ret < 0)
1035                         goto err_set_stream;
1036                 spin_lock_irqsave(&video->queue->irqlock, flags);
1037                 if (list_empty(&video->dmaqueue))
1038                         video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1039                 spin_unlock_irqrestore(&video->queue->irqlock, flags);
1040         }
1041
1042         video->streaming = 1;
1043
1044         mutex_unlock(&video->stream_lock);
1045         return 0;
1046
1047 err_set_stream:
1048         omap3isp_video_queue_streamoff(&vfh->queue);
1049 err_check_format:
1050         media_entity_pipeline_stop(&video->video.entity);
1051 err_pipeline_start:
1052         if (video->isp->pdata->set_constraints)
1053                 video->isp->pdata->set_constraints(video->isp, false);
1054         /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1055          * will get triggered the next time the CCDC is powered up will try to
1056          * access buffers that might have been freed but still present in the
1057          * DMA queue. This can easily get triggered if the above
1058          * omap3isp_pipeline_set_stream() call fails on a system with a
1059          * free-running sensor.
1060          */
1061         INIT_LIST_HEAD(&video->dmaqueue);
1062         video->queue = NULL;
1063
1064         mutex_unlock(&video->stream_lock);
1065         return ret;
1066 }
1067
1068 static int
1069 isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1070 {
1071         struct isp_video_fh *vfh = to_isp_video_fh(fh);
1072         struct isp_video *video = video_drvdata(file);
1073         struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1074         enum isp_pipeline_state state;
1075         unsigned int streaming;
1076         unsigned long flags;
1077
1078         if (type != video->type)
1079                 return -EINVAL;
1080
1081         mutex_lock(&video->stream_lock);
1082
1083         /* Make sure we're not streaming yet. */
1084         mutex_lock(&vfh->queue.lock);
1085         streaming = vfh->queue.streaming;
1086         mutex_unlock(&vfh->queue.lock);
1087
1088         if (!streaming)
1089                 goto done;
1090
1091         /* Update the pipeline state. */
1092         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1093                 state = ISP_PIPELINE_STREAM_OUTPUT
1094                       | ISP_PIPELINE_QUEUE_OUTPUT;
1095         else
1096                 state = ISP_PIPELINE_STREAM_INPUT
1097                       | ISP_PIPELINE_QUEUE_INPUT;
1098
1099         spin_lock_irqsave(&pipe->lock, flags);
1100         pipe->state &= ~state;
1101         spin_unlock_irqrestore(&pipe->lock, flags);
1102
1103         /* Stop the stream. */
1104         omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1105         omap3isp_video_queue_streamoff(&vfh->queue);
1106         video->queue = NULL;
1107         video->streaming = 0;
1108
1109         if (video->isp->pdata->set_constraints)
1110                 video->isp->pdata->set_constraints(video->isp, false);
1111         media_entity_pipeline_stop(&video->video.entity);
1112
1113 done:
1114         mutex_unlock(&video->stream_lock);
1115         return 0;
1116 }
1117
1118 static int
1119 isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1120 {
1121         if (input->index > 0)
1122                 return -EINVAL;
1123
1124         strlcpy(input->name, "camera", sizeof(input->name));
1125         input->type = V4L2_INPUT_TYPE_CAMERA;
1126
1127         return 0;
1128 }
1129
1130 static int
1131 isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1132 {
1133         *input = 0;
1134
1135         return 0;
1136 }
1137
1138 static int
1139 isp_video_s_input(struct file *file, void *fh, unsigned int input)
1140 {
1141         return input == 0 ? 0 : -EINVAL;
1142 }
1143
1144 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1145         .vidioc_querycap                = isp_video_querycap,
1146         .vidioc_g_fmt_vid_cap           = isp_video_get_format,
1147         .vidioc_s_fmt_vid_cap           = isp_video_set_format,
1148         .vidioc_try_fmt_vid_cap         = isp_video_try_format,
1149         .vidioc_g_fmt_vid_out           = isp_video_get_format,
1150         .vidioc_s_fmt_vid_out           = isp_video_set_format,
1151         .vidioc_try_fmt_vid_out         = isp_video_try_format,
1152         .vidioc_cropcap                 = isp_video_cropcap,
1153         .vidioc_g_crop                  = isp_video_get_crop,
1154         .vidioc_s_crop                  = isp_video_set_crop,
1155         .vidioc_g_parm                  = isp_video_get_param,
1156         .vidioc_s_parm                  = isp_video_set_param,
1157         .vidioc_reqbufs                 = isp_video_reqbufs,
1158         .vidioc_querybuf                = isp_video_querybuf,
1159         .vidioc_qbuf                    = isp_video_qbuf,
1160         .vidioc_dqbuf                   = isp_video_dqbuf,
1161         .vidioc_streamon                = isp_video_streamon,
1162         .vidioc_streamoff               = isp_video_streamoff,
1163         .vidioc_enum_input              = isp_video_enum_input,
1164         .vidioc_g_input                 = isp_video_g_input,
1165         .vidioc_s_input                 = isp_video_s_input,
1166 };
1167
1168 /* -----------------------------------------------------------------------------
1169  * V4L2 file operations
1170  */
1171
1172 static int isp_video_open(struct file *file)
1173 {
1174         struct isp_video *video = video_drvdata(file);
1175         struct isp_video_fh *handle;
1176         int ret = 0;
1177
1178         handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1179         if (handle == NULL)
1180                 return -ENOMEM;
1181
1182         v4l2_fh_init(&handle->vfh, &video->video);
1183         v4l2_fh_add(&handle->vfh);
1184
1185         /* If this is the first user, initialise the pipeline. */
1186         if (omap3isp_get(video->isp) == NULL) {
1187                 ret = -EBUSY;
1188                 goto done;
1189         }
1190
1191         ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1192         if (ret < 0) {
1193                 omap3isp_put(video->isp);
1194                 goto done;
1195         }
1196
1197         omap3isp_video_queue_init(&handle->queue, video->type,
1198                                   &isp_video_queue_ops, video->isp->dev,
1199                                   sizeof(struct isp_buffer));
1200
1201         memset(&handle->format, 0, sizeof(handle->format));
1202         handle->format.type = video->type;
1203         handle->timeperframe.denominator = 1;
1204
1205         handle->video = video;
1206         file->private_data = &handle->vfh;
1207
1208 done:
1209         if (ret < 0) {
1210                 v4l2_fh_del(&handle->vfh);
1211                 kfree(handle);
1212         }
1213
1214         return ret;
1215 }
1216
1217 static int isp_video_release(struct file *file)
1218 {
1219         struct isp_video *video = video_drvdata(file);
1220         struct v4l2_fh *vfh = file->private_data;
1221         struct isp_video_fh *handle = to_isp_video_fh(vfh);
1222
1223         /* Disable streaming and free the buffers queue resources. */
1224         isp_video_streamoff(file, vfh, video->type);
1225
1226         mutex_lock(&handle->queue.lock);
1227         omap3isp_video_queue_cleanup(&handle->queue);
1228         mutex_unlock(&handle->queue.lock);
1229
1230         omap3isp_pipeline_pm_use(&video->video.entity, 0);
1231
1232         /* Release the file handle. */
1233         v4l2_fh_del(vfh);
1234         kfree(handle);
1235         file->private_data = NULL;
1236
1237         omap3isp_put(video->isp);
1238
1239         return 0;
1240 }
1241
1242 static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1243 {
1244         struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1245         struct isp_video_queue *queue = &vfh->queue;
1246
1247         return omap3isp_video_queue_poll(queue, file, wait);
1248 }
1249
1250 static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1251 {
1252         struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1253
1254         return omap3isp_video_queue_mmap(&vfh->queue, vma);
1255 }
1256
1257 static struct v4l2_file_operations isp_video_fops = {
1258         .owner = THIS_MODULE,
1259         .unlocked_ioctl = video_ioctl2,
1260         .open = isp_video_open,
1261         .release = isp_video_release,
1262         .poll = isp_video_poll,
1263         .mmap = isp_video_mmap,
1264 };
1265
1266 /* -----------------------------------------------------------------------------
1267  * ISP video core
1268  */
1269
1270 static const struct isp_video_operations isp_video_dummy_ops = {
1271 };
1272
1273 int omap3isp_video_init(struct isp_video *video, const char *name)
1274 {
1275         const char *direction;
1276         int ret;
1277
1278         switch (video->type) {
1279         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1280                 direction = "output";
1281                 video->pad.flags = MEDIA_PAD_FL_SINK
1282                                    | MEDIA_PAD_FL_MUST_CONNECT;
1283                 break;
1284         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1285                 direction = "input";
1286                 video->pad.flags = MEDIA_PAD_FL_SOURCE
1287                                    | MEDIA_PAD_FL_MUST_CONNECT;
1288                 video->video.vfl_dir = VFL_DIR_TX;
1289                 break;
1290
1291         default:
1292                 return -EINVAL;
1293         }
1294
1295         ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1296         if (ret < 0)
1297                 return ret;
1298
1299         mutex_init(&video->mutex);
1300         atomic_set(&video->active, 0);
1301
1302         spin_lock_init(&video->pipe.lock);
1303         mutex_init(&video->stream_lock);
1304
1305         /* Initialize the video device. */
1306         if (video->ops == NULL)
1307                 video->ops = &isp_video_dummy_ops;
1308
1309         video->video.fops = &isp_video_fops;
1310         snprintf(video->video.name, sizeof(video->video.name),
1311                  "OMAP3 ISP %s %s", name, direction);
1312         video->video.vfl_type = VFL_TYPE_GRABBER;
1313         video->video.release = video_device_release_empty;
1314         video->video.ioctl_ops = &isp_video_ioctl_ops;
1315         video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1316
1317         video_set_drvdata(&video->video, video);
1318
1319         return 0;
1320 }
1321
1322 void omap3isp_video_cleanup(struct isp_video *video)
1323 {
1324         media_entity_cleanup(&video->video.entity);
1325         mutex_destroy(&video->stream_lock);
1326         mutex_destroy(&video->mutex);
1327 }
1328
1329 int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1330 {
1331         int ret;
1332
1333         video->video.v4l2_dev = vdev;
1334
1335         ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1336         if (ret < 0)
1337                 dev_err(video->isp->dev,
1338                         "%s: could not register video device (%d)\n",
1339                         __func__, ret);
1340
1341         return ret;
1342 }
1343
1344 void omap3isp_video_unregister(struct isp_video *video)
1345 {
1346         if (video_is_registered(&video->video))
1347                 video_unregister_device(&video->video);
1348 }