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