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