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