b4ffde87a4159f6fbca841c4cc72a78310627c12
[linux-2.6-microblaze.git] / drivers / staging / media / omap4iss / iss_video.c
1 /*
2  * TI OMAP4 ISS V4L2 Driver - Generic video node
3  *
4  * Copyright (C) 2012 Texas Instruments, Inc.
5  *
6  * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <asm/cacheflush.h>
15 #include <linux/clk.h>
16 #include <linux/mm.h>
17 #include <linux/pagemap.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/module.h>
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-ioctl.h>
24
25 #include "iss_video.h"
26 #include "iss.h"
27
28
29 /* -----------------------------------------------------------------------------
30  * Helper functions
31  */
32
33 static struct iss_format_info formats[] = {
34         { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
35           V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
36           V4L2_PIX_FMT_GREY, 8, "Greyscale 8 bpp", },
37         { V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10,
38           V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8,
39           V4L2_PIX_FMT_Y10, 10, "Greyscale 10 bpp", },
40         { V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10,
41           V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8,
42           V4L2_PIX_FMT_Y12, 12, "Greyscale 12 bpp", },
43         { V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
44           V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
45           V4L2_PIX_FMT_SBGGR8, 8, "BGGR Bayer 8 bpp", },
46         { V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
47           V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
48           V4L2_PIX_FMT_SGBRG8, 8, "GBRG Bayer 8 bpp", },
49         { V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
50           V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
51           V4L2_PIX_FMT_SGRBG8, 8, "GRBG Bayer 8 bpp", },
52         { V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
53           V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
54           V4L2_PIX_FMT_SRGGB8, 8, "RGGB Bayer 8 bpp", },
55         { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
56           V4L2_MBUS_FMT_SGRBG10_1X10, 0,
57           V4L2_PIX_FMT_SGRBG10DPCM8, 8, "GRBG Bayer 10 bpp DPCM8",  },
58         { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
59           V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8,
60           V4L2_PIX_FMT_SBGGR10, 10, "BGGR Bayer 10 bpp", },
61         { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
62           V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8,
63           V4L2_PIX_FMT_SGBRG10, 10, "GBRG Bayer 10 bpp", },
64         { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
65           V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8,
66           V4L2_PIX_FMT_SGRBG10, 10, "GRBG Bayer 10 bpp", },
67         { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
68           V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8,
69           V4L2_PIX_FMT_SRGGB10, 10, "RGGB Bayer 10 bpp", },
70         { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
71           V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8,
72           V4L2_PIX_FMT_SBGGR12, 12, "BGGR Bayer 12 bpp", },
73         { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
74           V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8,
75           V4L2_PIX_FMT_SGBRG12, 12, "GBRG Bayer 12 bpp", },
76         { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
77           V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8,
78           V4L2_PIX_FMT_SGRBG12, 12, "GRBG Bayer 12 bpp", },
79         { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
80           V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8,
81           V4L2_PIX_FMT_SRGGB12, 12, "RGGB Bayer 12 bpp", },
82         { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
83           V4L2_MBUS_FMT_UYVY8_1X16, 0,
84           V4L2_PIX_FMT_UYVY, 16, "YUV 4:2:2 (UYVY)", },
85         { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
86           V4L2_MBUS_FMT_YUYV8_1X16, 0,
87           V4L2_PIX_FMT_YUYV, 16, "YUV 4:2:2 (YUYV)", },
88         { V4L2_MBUS_FMT_YUYV8_1_5X8, V4L2_MBUS_FMT_YUYV8_1_5X8,
89           V4L2_MBUS_FMT_YUYV8_1_5X8, 0,
90           V4L2_PIX_FMT_NV12, 8, "YUV 4:2:0 (NV12)", },
91 };
92
93 const struct iss_format_info *
94 omap4iss_video_format_info(enum v4l2_mbus_pixelcode code)
95 {
96         unsigned int i;
97
98         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
99                 if (formats[i].code == code)
100                         return &formats[i];
101         }
102
103         return NULL;
104 }
105
106 /*
107  * iss_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
108  * @video: ISS video instance
109  * @mbus: v4l2_mbus_framefmt format (input)
110  * @pix: v4l2_pix_format format (output)
111  *
112  * Fill the output pix structure with information from the input mbus format.
113  * The bytesperline and sizeimage fields are computed from the requested bytes
114  * per line value in the pix format and information from the video instance.
115  *
116  * Return the number of padding bytes at end of line.
117  */
118 static unsigned int iss_video_mbus_to_pix(const struct iss_video *video,
119                                           const struct v4l2_mbus_framefmt *mbus,
120                                           struct v4l2_pix_format *pix)
121 {
122         unsigned int bpl = pix->bytesperline;
123         unsigned int min_bpl;
124         unsigned int i;
125
126         memset(pix, 0, sizeof(*pix));
127         pix->width = mbus->width;
128         pix->height = mbus->height;
129
130         /* Skip the last format in the loop so that it will be selected if no
131          * match is found.
132          */
133         for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
134                 if (formats[i].code == mbus->code)
135                         break;
136         }
137
138         min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
139
140         /* Clamp the requested bytes per line value. If the maximum bytes per
141          * line value is zero, the module doesn't support user configurable line
142          * sizes. Override the requested value with the minimum in that case.
143          */
144         if (video->bpl_max)
145                 bpl = clamp(bpl, min_bpl, video->bpl_max);
146         else
147                 bpl = min_bpl;
148
149         if (!video->bpl_zero_padding || bpl != min_bpl)
150                 bpl = ALIGN(bpl, video->bpl_alignment);
151
152         pix->pixelformat = formats[i].pixelformat;
153         pix->bytesperline = bpl;
154         pix->sizeimage = pix->bytesperline * pix->height;
155         pix->colorspace = mbus->colorspace;
156         pix->field = mbus->field;
157
158         /* FIXME: Special case for NV12! We should make this nicer... */
159         if (pix->pixelformat == V4L2_PIX_FMT_NV12)
160                 pix->sizeimage += (pix->bytesperline * pix->height) / 2;
161
162         return bpl - min_bpl;
163 }
164
165 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
166                                   struct v4l2_mbus_framefmt *mbus)
167 {
168         unsigned int i;
169
170         memset(mbus, 0, sizeof(*mbus));
171         mbus->width = pix->width;
172         mbus->height = pix->height;
173
174         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
175                 if (formats[i].pixelformat == pix->pixelformat)
176                         break;
177         }
178
179         if (WARN_ON(i == ARRAY_SIZE(formats)))
180                 return;
181
182         mbus->code = formats[i].code;
183         mbus->colorspace = pix->colorspace;
184         mbus->field = pix->field;
185 }
186
187 static struct v4l2_subdev *
188 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
189 {
190         struct media_pad *remote;
191
192         remote = media_entity_remote_pad(&video->pad);
193
194         if (remote == NULL ||
195             media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
196                 return NULL;
197
198         if (pad)
199                 *pad = remote->index;
200
201         return media_entity_to_v4l2_subdev(remote->entity);
202 }
203
204 /* Return a pointer to the ISS video instance at the far end of the pipeline. */
205 static struct iss_video *
206 iss_video_far_end(struct iss_video *video)
207 {
208         struct media_entity_graph graph;
209         struct media_entity *entity = &video->video.entity;
210         struct media_device *mdev = entity->parent;
211         struct iss_video *far_end = NULL;
212
213         mutex_lock(&mdev->graph_mutex);
214         media_entity_graph_walk_start(&graph, entity);
215
216         while ((entity = media_entity_graph_walk_next(&graph))) {
217                 if (entity == &video->video.entity)
218                         continue;
219
220                 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
221                         continue;
222
223                 far_end = to_iss_video(media_entity_to_video_device(entity));
224                 if (far_end->type != video->type)
225                         break;
226
227                 far_end = NULL;
228         }
229
230         mutex_unlock(&mdev->graph_mutex);
231         return far_end;
232 }
233
234 static int
235 __iss_video_get_format(struct iss_video *video, struct v4l2_format *format)
236 {
237         struct v4l2_subdev_format fmt;
238         struct v4l2_subdev *subdev;
239         u32 pad;
240         int ret;
241
242         subdev = iss_video_remote_subdev(video, &pad);
243         if (subdev == NULL)
244                 return -EINVAL;
245
246         fmt.pad = pad;
247         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
248
249         mutex_lock(&video->mutex);
250         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
251         mutex_unlock(&video->mutex);
252
253         if (ret)
254                 return ret;
255
256         format->type = video->type;
257         return iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
258 }
259
260 static int
261 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
262 {
263         struct v4l2_format format;
264         int ret;
265
266         memcpy(&format, &vfh->format, sizeof(format));
267         ret = __iss_video_get_format(video, &format);
268         if (ret < 0)
269                 return ret;
270
271         if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
272             vfh->format.fmt.pix.height != format.fmt.pix.height ||
273             vfh->format.fmt.pix.width != format.fmt.pix.width ||
274             vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
275             vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
276                 return -EINVAL;
277
278         return ret;
279 }
280
281 /* -----------------------------------------------------------------------------
282  * Video queue operations
283  */
284
285 static int iss_video_queue_setup(struct vb2_queue *vq,
286                                  const struct v4l2_format *fmt,
287                                  unsigned int *count, unsigned int *num_planes,
288                                  unsigned int sizes[], void *alloc_ctxs[])
289 {
290         struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
291         struct iss_video *video = vfh->video;
292
293         /* Revisit multi-planar support for NV12 */
294         *num_planes = 1;
295
296         sizes[0] = vfh->format.fmt.pix.sizeimage;
297         if (sizes[0] == 0)
298                 return -EINVAL;
299
300         alloc_ctxs[0] = video->alloc_ctx;
301
302         *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
303
304         return 0;
305 }
306
307 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
308 {
309         struct iss_buffer *buffer = container_of(vb, struct iss_buffer, vb);
310
311         if (buffer->iss_addr)
312                 buffer->iss_addr = 0;
313 }
314
315 static int iss_video_buf_prepare(struct vb2_buffer *vb)
316 {
317         struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
318         struct iss_buffer *buffer = container_of(vb, struct iss_buffer, vb);
319         struct iss_video *video = vfh->video;
320         unsigned long size = vfh->format.fmt.pix.sizeimage;
321         dma_addr_t addr;
322
323         if (vb2_plane_size(vb, 0) < size)
324                 return -ENOBUFS;
325
326         addr = vb2_dma_contig_plane_dma_addr(vb, 0);
327         if (!IS_ALIGNED(addr, 32)) {
328                 dev_dbg(video->iss->dev,
329                         "Buffer address must be aligned to 32 bytes boundary.\n");
330                 return -EINVAL;
331         }
332
333         vb2_set_plane_payload(vb, 0, size);
334         buffer->iss_addr = addr;
335         return 0;
336 }
337
338 static void iss_video_buf_queue(struct vb2_buffer *vb)
339 {
340         struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
341         struct iss_video *video = vfh->video;
342         struct iss_buffer *buffer = container_of(vb, struct iss_buffer, vb);
343         struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
344         unsigned int empty;
345         unsigned long flags;
346
347         spin_lock_irqsave(&video->qlock, flags);
348         empty = list_empty(&video->dmaqueue);
349         list_add_tail(&buffer->list, &video->dmaqueue);
350         spin_unlock_irqrestore(&video->qlock, flags);
351
352         if (empty) {
353                 enum iss_pipeline_state state;
354                 unsigned int start;
355
356                 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
357                         state = ISS_PIPELINE_QUEUE_OUTPUT;
358                 else
359                         state = ISS_PIPELINE_QUEUE_INPUT;
360
361                 spin_lock_irqsave(&pipe->lock, flags);
362                 pipe->state |= state;
363                 video->ops->queue(video, buffer);
364                 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
365
366                 start = iss_pipeline_ready(pipe);
367                 if (start)
368                         pipe->state |= ISS_PIPELINE_STREAM;
369                 spin_unlock_irqrestore(&pipe->lock, flags);
370
371                 if (start)
372                         omap4iss_pipeline_set_stream(pipe,
373                                                 ISS_PIPELINE_STREAM_SINGLESHOT);
374         }
375 }
376
377 static struct vb2_ops iss_video_vb2ops = {
378         .queue_setup    = iss_video_queue_setup,
379         .buf_prepare    = iss_video_buf_prepare,
380         .buf_queue      = iss_video_buf_queue,
381         .buf_cleanup    = iss_video_buf_cleanup,
382 };
383
384 /*
385  * omap4iss_video_buffer_next - Complete the current buffer and return the next
386  * @video: ISS video object
387  *
388  * Remove the current video buffer from the DMA queue and fill its timestamp,
389  * field count and state fields before waking up its completion handler.
390  *
391  * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
392  * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
393  *
394  * The DMA queue is expected to contain at least one buffer.
395  *
396  * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
397  * empty.
398  */
399 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
400 {
401         struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
402         enum iss_pipeline_state state;
403         struct iss_buffer *buf;
404         unsigned long flags;
405         struct timespec ts;
406
407         spin_lock_irqsave(&video->qlock, flags);
408         if (WARN_ON(list_empty(&video->dmaqueue))) {
409                 spin_unlock_irqrestore(&video->qlock, flags);
410                 return NULL;
411         }
412
413         buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
414                                list);
415         list_del(&buf->list);
416         spin_unlock_irqrestore(&video->qlock, flags);
417
418         ktime_get_ts(&ts);
419         buf->vb.v4l2_buf.timestamp.tv_sec = ts.tv_sec;
420         buf->vb.v4l2_buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
421
422         /* Do frame number propagation only if this is the output video node.
423          * Frame number either comes from the CSI receivers or it gets
424          * incremented here if H3A is not active.
425          * Note: There is no guarantee that the output buffer will finish
426          * first, so the input number might lag behind by 1 in some cases.
427          */
428         if (video == pipe->output && !pipe->do_propagation)
429                 buf->vb.v4l2_buf.sequence =
430                         atomic_inc_return(&pipe->frame_number);
431         else
432                 buf->vb.v4l2_buf.sequence = atomic_read(&pipe->frame_number);
433
434         vb2_buffer_done(&buf->vb, pipe->error ?
435                         VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
436         pipe->error = false;
437
438         spin_lock_irqsave(&video->qlock, flags);
439         if (list_empty(&video->dmaqueue)) {
440                 spin_unlock_irqrestore(&video->qlock, flags);
441                 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
442                         state = ISS_PIPELINE_QUEUE_OUTPUT
443                               | ISS_PIPELINE_STREAM;
444                 else
445                         state = ISS_PIPELINE_QUEUE_INPUT
446                               | ISS_PIPELINE_STREAM;
447
448                 spin_lock_irqsave(&pipe->lock, flags);
449                 pipe->state &= ~state;
450                 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
451                         video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
452                 spin_unlock_irqrestore(&pipe->lock, flags);
453                 return NULL;
454         }
455
456         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
457                 spin_lock_irqsave(&pipe->lock, flags);
458                 pipe->state &= ~ISS_PIPELINE_STREAM;
459                 spin_unlock_irqrestore(&pipe->lock, flags);
460         }
461
462         buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
463                                list);
464         spin_unlock_irqrestore(&video->qlock, flags);
465         buf->vb.state = VB2_BUF_STATE_ACTIVE;
466         return buf;
467 }
468
469 /* -----------------------------------------------------------------------------
470  * V4L2 ioctls
471  */
472
473 static int
474 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
475 {
476         struct iss_video *video = video_drvdata(file);
477
478         strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
479         strlcpy(cap->card, video->video.name, sizeof(cap->card));
480         strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
481
482         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
483                 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
484         else
485                 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
486
487         cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
488                           | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
489
490         return 0;
491 }
492
493 static int
494 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
495 {
496         struct iss_video_fh *vfh = to_iss_video_fh(fh);
497         struct iss_video *video = video_drvdata(file);
498
499         if (format->type != video->type)
500                 return -EINVAL;
501
502         mutex_lock(&video->mutex);
503         *format = vfh->format;
504         mutex_unlock(&video->mutex);
505
506         return 0;
507 }
508
509 static int
510 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
511 {
512         struct iss_video_fh *vfh = to_iss_video_fh(fh);
513         struct iss_video *video = video_drvdata(file);
514         struct v4l2_mbus_framefmt fmt;
515
516         if (format->type != video->type)
517                 return -EINVAL;
518
519         mutex_lock(&video->mutex);
520
521         /* Fill the bytesperline and sizeimage fields by converting to media bus
522          * format and back to pixel format.
523          */
524         iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
525         iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
526
527         vfh->format = *format;
528
529         mutex_unlock(&video->mutex);
530         return 0;
531 }
532
533 static int
534 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
535 {
536         struct iss_video *video = video_drvdata(file);
537         struct v4l2_subdev_format fmt;
538         struct v4l2_subdev *subdev;
539         u32 pad;
540         int ret;
541
542         if (format->type != video->type)
543                 return -EINVAL;
544
545         subdev = iss_video_remote_subdev(video, &pad);
546         if (subdev == NULL)
547                 return -EINVAL;
548
549         iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
550
551         fmt.pad = pad;
552         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
553         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
554         if (ret)
555                 return ret;
556
557         iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
558         return 0;
559 }
560
561 static int
562 iss_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
563 {
564         struct iss_video *video = video_drvdata(file);
565         struct v4l2_subdev *subdev;
566         int ret;
567
568         subdev = iss_video_remote_subdev(video, NULL);
569         if (subdev == NULL)
570                 return -EINVAL;
571
572         mutex_lock(&video->mutex);
573         ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
574         mutex_unlock(&video->mutex);
575
576         return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
577 }
578
579 static int
580 iss_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
581 {
582         struct iss_video *video = video_drvdata(file);
583         struct v4l2_subdev_format format;
584         struct v4l2_subdev *subdev;
585         u32 pad;
586         int ret;
587
588         subdev = iss_video_remote_subdev(video, &pad);
589         if (subdev == NULL)
590                 return -EINVAL;
591
592         /* Try the get crop operation first and fallback to get format if not
593          * implemented.
594          */
595         ret = v4l2_subdev_call(subdev, video, g_crop, crop);
596         if (ret != -ENOIOCTLCMD)
597                 return ret;
598
599         format.pad = pad;
600         format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
601         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
602         if (ret < 0)
603                 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
604
605         crop->c.left = 0;
606         crop->c.top = 0;
607         crop->c.width = format.format.width;
608         crop->c.height = format.format.height;
609
610         return 0;
611 }
612
613 static int
614 iss_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
615 {
616         struct iss_video *video = video_drvdata(file);
617         struct v4l2_subdev *subdev;
618         int ret;
619
620         subdev = iss_video_remote_subdev(video, NULL);
621         if (subdev == NULL)
622                 return -EINVAL;
623
624         mutex_lock(&video->mutex);
625         ret = v4l2_subdev_call(subdev, video, s_crop, crop);
626         mutex_unlock(&video->mutex);
627
628         return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
629 }
630
631 static int
632 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
633 {
634         struct iss_video_fh *vfh = to_iss_video_fh(fh);
635         struct iss_video *video = video_drvdata(file);
636
637         if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
638             video->type != a->type)
639                 return -EINVAL;
640
641         memset(a, 0, sizeof(*a));
642         a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
643         a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
644         a->parm.output.timeperframe = vfh->timeperframe;
645
646         return 0;
647 }
648
649 static int
650 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
651 {
652         struct iss_video_fh *vfh = to_iss_video_fh(fh);
653         struct iss_video *video = video_drvdata(file);
654
655         if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
656             video->type != a->type)
657                 return -EINVAL;
658
659         if (a->parm.output.timeperframe.denominator == 0)
660                 a->parm.output.timeperframe.denominator = 1;
661
662         vfh->timeperframe = a->parm.output.timeperframe;
663
664         return 0;
665 }
666
667 static int
668 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
669 {
670         struct iss_video_fh *vfh = to_iss_video_fh(fh);
671
672         return vb2_reqbufs(&vfh->queue, rb);
673 }
674
675 static int
676 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
677 {
678         struct iss_video_fh *vfh = to_iss_video_fh(fh);
679
680         return vb2_querybuf(&vfh->queue, b);
681 }
682
683 static int
684 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
685 {
686         struct iss_video_fh *vfh = to_iss_video_fh(fh);
687
688         return vb2_qbuf(&vfh->queue, b);
689 }
690
691 static int
692 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
693 {
694         struct iss_video_fh *vfh = to_iss_video_fh(fh);
695
696         return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
697 }
698
699 /*
700  * Stream management
701  *
702  * Every ISS pipeline has a single input and a single output. The input can be
703  * either a sensor or a video node. The output is always a video node.
704  *
705  * As every pipeline has an output video node, the ISS video objects at the
706  * pipeline output stores the pipeline state. It tracks the streaming state of
707  * both the input and output, as well as the availability of buffers.
708  *
709  * In sensor-to-memory mode, frames are always available at the pipeline input.
710  * Starting the sensor usually requires I2C transfers and must be done in
711  * interruptible context. The pipeline is started and stopped synchronously
712  * to the stream on/off commands. All modules in the pipeline will get their
713  * subdev set stream handler called. The module at the end of the pipeline must
714  * delay starting the hardware until buffers are available at its output.
715  *
716  * In memory-to-memory mode, starting/stopping the stream requires
717  * synchronization between the input and output. ISS modules can't be stopped
718  * in the middle of a frame, and at least some of the modules seem to become
719  * busy as soon as they're started, even if they don't receive a frame start
720  * event. For that reason frames need to be processed in single-shot mode. The
721  * driver needs to wait until a frame is completely processed and written to
722  * memory before restarting the pipeline for the next frame. Pipelined
723  * processing might be possible but requires more testing.
724  *
725  * Stream start must be delayed until buffers are available at both the input
726  * and output. The pipeline must be started in the videobuf queue callback with
727  * the buffers queue spinlock held. The modules subdev set stream operation must
728  * not sleep.
729  */
730 static int
731 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
732 {
733         struct iss_video_fh *vfh = to_iss_video_fh(fh);
734         struct iss_video *video = video_drvdata(file);
735         enum iss_pipeline_state state;
736         struct iss_pipeline *pipe;
737         struct iss_video *far_end;
738         unsigned long flags;
739         int ret;
740
741         if (type != video->type)
742                 return -EINVAL;
743
744         mutex_lock(&video->stream_lock);
745
746         /* Start streaming on the pipeline. No link touching an entity in the
747          * pipeline can be activated or deactivated once streaming is started.
748          */
749         pipe = video->video.entity.pipe
750              ? to_iss_pipeline(&video->video.entity) : &video->pipe;
751         pipe->external = NULL;
752         pipe->external_rate = 0;
753         pipe->external_bpp = 0;
754
755         if (video->iss->pdata->set_constraints)
756                 video->iss->pdata->set_constraints(video->iss, true);
757
758         ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
759         if (ret < 0)
760                 goto err_media_entity_pipeline_start;
761
762         /* Verify that the currently configured format matches the output of
763          * the connected subdev.
764          */
765         ret = iss_video_check_format(video, vfh);
766         if (ret < 0)
767                 goto err_iss_video_check_format;
768
769         video->bpl_padding = ret;
770         video->bpl_value = vfh->format.fmt.pix.bytesperline;
771
772         /* Find the ISS video node connected at the far end of the pipeline and
773          * update the pipeline.
774          */
775         far_end = iss_video_far_end(video);
776
777         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
778                 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
779                 pipe->input = far_end;
780                 pipe->output = video;
781         } else {
782                 if (far_end == NULL) {
783                         ret = -EPIPE;
784                         goto err_iss_video_check_format;
785                 }
786
787                 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
788                 pipe->input = video;
789                 pipe->output = far_end;
790         }
791
792         spin_lock_irqsave(&pipe->lock, flags);
793         pipe->state &= ~ISS_PIPELINE_STREAM;
794         pipe->state |= state;
795         spin_unlock_irqrestore(&pipe->lock, flags);
796
797         /* Set the maximum time per frame as the value requested by userspace.
798          * This is a soft limit that can be overridden if the hardware doesn't
799          * support the request limit.
800          */
801         if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
802                 pipe->max_timeperframe = vfh->timeperframe;
803
804         video->queue = &vfh->queue;
805         INIT_LIST_HEAD(&video->dmaqueue);
806         spin_lock_init(&video->qlock);
807         atomic_set(&pipe->frame_number, -1);
808
809         ret = vb2_streamon(&vfh->queue, type);
810         if (ret < 0)
811                 goto err_iss_video_check_format;
812
813         /* In sensor-to-memory mode, the stream can be started synchronously
814          * to the stream on command. In memory-to-memory mode, it will be
815          * started when buffers are queued on both the input and output.
816          */
817         if (pipe->input == NULL) {
818                 unsigned long flags;
819                 ret = omap4iss_pipeline_set_stream(pipe,
820                                               ISS_PIPELINE_STREAM_CONTINUOUS);
821                 if (ret < 0)
822                         goto err_omap4iss_set_stream;
823                 spin_lock_irqsave(&video->qlock, flags);
824                 if (list_empty(&video->dmaqueue))
825                         video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
826                 spin_unlock_irqrestore(&video->qlock, flags);
827         }
828
829         mutex_unlock(&video->stream_lock);
830         return 0;
831
832 err_omap4iss_set_stream:
833         vb2_streamoff(&vfh->queue, type);
834 err_iss_video_check_format:
835         media_entity_pipeline_stop(&video->video.entity);
836 err_media_entity_pipeline_start:
837         if (video->iss->pdata->set_constraints)
838                 video->iss->pdata->set_constraints(video->iss, false);
839         video->queue = NULL;
840
841         mutex_unlock(&video->stream_lock);
842         return ret;
843 }
844
845 static int
846 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
847 {
848         struct iss_video_fh *vfh = to_iss_video_fh(fh);
849         struct iss_video *video = video_drvdata(file);
850         struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
851         enum iss_pipeline_state state;
852         unsigned long flags;
853
854         if (type != video->type)
855                 return -EINVAL;
856
857         mutex_lock(&video->stream_lock);
858
859         if (!vb2_is_streaming(&vfh->queue))
860                 goto done;
861
862         /* Update the pipeline state. */
863         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
864                 state = ISS_PIPELINE_STREAM_OUTPUT
865                       | ISS_PIPELINE_QUEUE_OUTPUT;
866         else
867                 state = ISS_PIPELINE_STREAM_INPUT
868                       | ISS_PIPELINE_QUEUE_INPUT;
869
870         spin_lock_irqsave(&pipe->lock, flags);
871         pipe->state &= ~state;
872         spin_unlock_irqrestore(&pipe->lock, flags);
873
874         /* Stop the stream. */
875         omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
876         vb2_streamoff(&vfh->queue, type);
877         video->queue = NULL;
878
879         if (video->iss->pdata->set_constraints)
880                 video->iss->pdata->set_constraints(video->iss, false);
881         media_entity_pipeline_stop(&video->video.entity);
882
883 done:
884         mutex_unlock(&video->stream_lock);
885         return 0;
886 }
887
888 static int
889 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
890 {
891         if (input->index > 0)
892                 return -EINVAL;
893
894         strlcpy(input->name, "camera", sizeof(input->name));
895         input->type = V4L2_INPUT_TYPE_CAMERA;
896
897         return 0;
898 }
899
900 static int
901 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
902 {
903         *input = 0;
904
905         return 0;
906 }
907
908 static int
909 iss_video_s_input(struct file *file, void *fh, unsigned int input)
910 {
911         return input == 0 ? 0 : -EINVAL;
912 }
913
914 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
915         .vidioc_querycap                = iss_video_querycap,
916         .vidioc_g_fmt_vid_cap           = iss_video_get_format,
917         .vidioc_s_fmt_vid_cap           = iss_video_set_format,
918         .vidioc_try_fmt_vid_cap         = iss_video_try_format,
919         .vidioc_g_fmt_vid_out           = iss_video_get_format,
920         .vidioc_s_fmt_vid_out           = iss_video_set_format,
921         .vidioc_try_fmt_vid_out         = iss_video_try_format,
922         .vidioc_cropcap                 = iss_video_cropcap,
923         .vidioc_g_crop                  = iss_video_get_crop,
924         .vidioc_s_crop                  = iss_video_set_crop,
925         .vidioc_g_parm                  = iss_video_get_param,
926         .vidioc_s_parm                  = iss_video_set_param,
927         .vidioc_reqbufs                 = iss_video_reqbufs,
928         .vidioc_querybuf                = iss_video_querybuf,
929         .vidioc_qbuf                    = iss_video_qbuf,
930         .vidioc_dqbuf                   = iss_video_dqbuf,
931         .vidioc_streamon                = iss_video_streamon,
932         .vidioc_streamoff               = iss_video_streamoff,
933         .vidioc_enum_input              = iss_video_enum_input,
934         .vidioc_g_input                 = iss_video_g_input,
935         .vidioc_s_input                 = iss_video_s_input,
936 };
937
938 /* -----------------------------------------------------------------------------
939  * V4L2 file operations
940  */
941
942 static int iss_video_open(struct file *file)
943 {
944         struct iss_video *video = video_drvdata(file);
945         struct iss_video_fh *handle;
946         struct vb2_queue *q;
947         int ret = 0;
948
949         handle = kzalloc(sizeof(*handle), GFP_KERNEL);
950         if (handle == NULL)
951                 return -ENOMEM;
952
953         v4l2_fh_init(&handle->vfh, &video->video);
954         v4l2_fh_add(&handle->vfh);
955
956         /* If this is the first user, initialise the pipeline. */
957         if (omap4iss_get(video->iss) == NULL) {
958                 ret = -EBUSY;
959                 goto done;
960         }
961
962         ret = omap4iss_pipeline_pm_use(&video->video.entity, 1);
963         if (ret < 0) {
964                 omap4iss_put(video->iss);
965                 goto done;
966         }
967
968         video->alloc_ctx = vb2_dma_contig_init_ctx(video->iss->dev);
969         if (IS_ERR(video->alloc_ctx)) {
970                 ret = PTR_ERR(video->alloc_ctx);
971                 omap4iss_put(video->iss);
972                 goto done;
973         }
974
975         q = &handle->queue;
976
977         q->type = video->type;
978         q->io_modes = VB2_MMAP;
979         q->drv_priv = handle;
980         q->ops = &iss_video_vb2ops;
981         q->mem_ops = &vb2_dma_contig_memops;
982         q->buf_struct_size = sizeof(struct iss_buffer);
983         q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
984
985         ret = vb2_queue_init(q);
986         if (ret) {
987                 omap4iss_put(video->iss);
988                 goto done;
989         }
990
991         memset(&handle->format, 0, sizeof(handle->format));
992         handle->format.type = video->type;
993         handle->timeperframe.denominator = 1;
994
995         handle->video = video;
996         file->private_data = &handle->vfh;
997
998 done:
999         if (ret < 0) {
1000                 v4l2_fh_del(&handle->vfh);
1001                 kfree(handle);
1002         }
1003
1004         return ret;
1005 }
1006
1007 static int iss_video_release(struct file *file)
1008 {
1009         struct iss_video *video = video_drvdata(file);
1010         struct v4l2_fh *vfh = file->private_data;
1011         struct iss_video_fh *handle = to_iss_video_fh(vfh);
1012
1013         /* Disable streaming and free the buffers queue resources. */
1014         iss_video_streamoff(file, vfh, video->type);
1015
1016         omap4iss_pipeline_pm_use(&video->video.entity, 0);
1017
1018         /* Release the videobuf2 queue */
1019         vb2_queue_release(&handle->queue);
1020
1021         /* Release the file handle. */
1022         v4l2_fh_del(vfh);
1023         kfree(handle);
1024         file->private_data = NULL;
1025
1026         omap4iss_put(video->iss);
1027
1028         return 0;
1029 }
1030
1031 static unsigned int iss_video_poll(struct file *file, poll_table *wait)
1032 {
1033         struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1034
1035         return vb2_poll(&vfh->queue, file, wait);
1036 }
1037
1038 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1039 {
1040         struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1041
1042         return vb2_mmap(&vfh->queue, vma);
1043 }
1044
1045 static struct v4l2_file_operations iss_video_fops = {
1046         .owner = THIS_MODULE,
1047         .unlocked_ioctl = video_ioctl2,
1048         .open = iss_video_open,
1049         .release = iss_video_release,
1050         .poll = iss_video_poll,
1051         .mmap = iss_video_mmap,
1052 };
1053
1054 /* -----------------------------------------------------------------------------
1055  * ISS video core
1056  */
1057
1058 static const struct iss_video_operations iss_video_dummy_ops = {
1059 };
1060
1061 int omap4iss_video_init(struct iss_video *video, const char *name)
1062 {
1063         const char *direction;
1064         int ret;
1065
1066         switch (video->type) {
1067         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1068                 direction = "output";
1069                 video->pad.flags = MEDIA_PAD_FL_SINK;
1070                 break;
1071         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1072                 direction = "input";
1073                 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1074                 break;
1075
1076         default:
1077                 return -EINVAL;
1078         }
1079
1080         ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1081         if (ret < 0)
1082                 return ret;
1083
1084         mutex_init(&video->mutex);
1085         atomic_set(&video->active, 0);
1086
1087         spin_lock_init(&video->pipe.lock);
1088         mutex_init(&video->stream_lock);
1089
1090         /* Initialize the video device. */
1091         if (video->ops == NULL)
1092                 video->ops = &iss_video_dummy_ops;
1093
1094         video->video.fops = &iss_video_fops;
1095         snprintf(video->video.name, sizeof(video->video.name),
1096                  "OMAP4 ISS %s %s", name, direction);
1097         video->video.vfl_type = VFL_TYPE_GRABBER;
1098         video->video.release = video_device_release_empty;
1099         video->video.ioctl_ops = &iss_video_ioctl_ops;
1100         video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1101
1102         video_set_drvdata(&video->video, video);
1103
1104         return 0;
1105 }
1106
1107 void omap4iss_video_cleanup(struct iss_video *video)
1108 {
1109         media_entity_cleanup(&video->video.entity);
1110         mutex_destroy(&video->stream_lock);
1111         mutex_destroy(&video->mutex);
1112 }
1113
1114 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1115 {
1116         int ret;
1117
1118         video->video.v4l2_dev = vdev;
1119
1120         ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1121         if (ret < 0)
1122                 dev_err(video->iss->dev,
1123                         "%s: could not register video device (%d)\n",
1124                         __func__, ret);
1125
1126         return ret;
1127 }
1128
1129 void omap4iss_video_unregister(struct iss_video *video)
1130 {
1131         video_unregister_device(&video->video);
1132 }