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