2 * TI OMAP4 ISS V4L2 Driver - Generic video node
4 * Copyright (C) 2012 Texas Instruments, Inc.
6 * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
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.
14 #include <asm/cacheflush.h>
15 #include <linux/clk.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>
23 #include <media/v4l2-dev.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-mc.h>
27 #include "iss_video.h"
30 /* -----------------------------------------------------------------------------
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)", },
94 const struct iss_format_info *
95 omap4iss_video_format_info(u32 code)
99 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
100 if (formats[i].code == code)
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)
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.
117 * Return the number of padding bytes at end of line.
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)
123 unsigned int bpl = pix->bytesperline;
124 unsigned int min_bpl;
127 memset(pix, 0, sizeof(*pix));
128 pix->width = mbus->width;
129 pix->height = mbus->height;
131 /* Skip the last format in the loop so that it will be selected if no
134 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
135 if (formats[i].code == mbus->code)
139 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
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.
146 bpl = clamp(bpl, min_bpl, video->bpl_max);
150 if (!video->bpl_zero_padding || bpl != min_bpl)
151 bpl = ALIGN(bpl, video->bpl_alignment);
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;
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;
163 return bpl - min_bpl;
166 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
167 struct v4l2_mbus_framefmt *mbus)
171 memset(mbus, 0, sizeof(*mbus));
172 mbus->width = pix->width;
173 mbus->height = pix->height;
175 /* Skip the last format in the loop so that it will be selected if no
178 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
179 if (formats[i].pixelformat == pix->pixelformat)
183 mbus->code = formats[i].code;
184 mbus->colorspace = pix->colorspace;
185 mbus->field = pix->field;
188 static struct v4l2_subdev *
189 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
191 struct media_pad *remote;
193 remote = media_entity_remote_pad(&video->pad);
195 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
199 *pad = remote->index;
201 return media_entity_to_v4l2_subdev(remote->entity);
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)
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;
213 mutex_lock(&mdev->graph_mutex);
215 if (media_entity_graph_walk_init(&graph, mdev)) {
216 mutex_unlock(&mdev->graph_mutex);
220 media_entity_graph_walk_start(&graph, entity);
222 while ((entity = media_entity_graph_walk_next(&graph))) {
223 if (entity == &video->video.entity)
226 if (!is_media_entity_v4l2_video_device(entity))
229 far_end = to_iss_video(media_entity_to_video_device(entity));
230 if (far_end->type != video->type)
236 mutex_unlock(&mdev->graph_mutex);
238 media_entity_graph_walk_cleanup(&graph);
244 __iss_video_get_format(struct iss_video *video,
245 struct v4l2_mbus_framefmt *format)
247 struct v4l2_subdev_format fmt;
248 struct v4l2_subdev *subdev;
252 subdev = iss_video_remote_subdev(video, &pad);
256 memset(&fmt, 0, sizeof(fmt));
258 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
260 mutex_lock(&video->mutex);
261 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
262 mutex_unlock(&video->mutex);
267 *format = fmt.format;
272 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
274 struct v4l2_mbus_framefmt format;
275 struct v4l2_pix_format pixfmt;
278 ret = __iss_video_get_format(video, &format);
282 pixfmt.bytesperline = 0;
283 ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
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)
295 /* -----------------------------------------------------------------------------
296 * Video queue operations
299 static int iss_video_queue_setup(struct vb2_queue *vq,
300 unsigned int *count, unsigned int *num_planes,
301 unsigned int sizes[], struct device *alloc_devs[])
303 struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
304 struct iss_video *video = vfh->video;
306 /* Revisit multi-planar support for NV12 */
309 sizes[0] = vfh->format.fmt.pix.sizeimage;
313 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
318 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
320 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
321 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
323 if (buffer->iss_addr)
324 buffer->iss_addr = 0;
327 static int iss_video_buf_prepare(struct vb2_buffer *vb)
329 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
330 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
331 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
332 struct iss_video *video = vfh->video;
333 unsigned long size = vfh->format.fmt.pix.sizeimage;
336 if (vb2_plane_size(vb, 0) < size)
339 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
340 if (!IS_ALIGNED(addr, 32)) {
341 dev_dbg(video->iss->dev,
342 "Buffer address must be aligned to 32 bytes boundary.\n");
346 vb2_set_plane_payload(vb, 0, size);
347 buffer->iss_addr = addr;
351 static void iss_video_buf_queue(struct vb2_buffer *vb)
353 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
354 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
355 struct iss_video *video = vfh->video;
356 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
357 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
361 spin_lock_irqsave(&video->qlock, flags);
363 /* Mark the buffer is faulty and give it back to the queue immediately
364 * if the video node has registered an error. vb2 will perform the same
365 * check when preparing the buffer, but that is inherently racy, so we
366 * need to handle the race condition with an authoritative check here.
368 if (unlikely(video->error)) {
369 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
370 spin_unlock_irqrestore(&video->qlock, flags);
374 empty = list_empty(&video->dmaqueue);
375 list_add_tail(&buffer->list, &video->dmaqueue);
377 spin_unlock_irqrestore(&video->qlock, flags);
380 enum iss_pipeline_state state;
383 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
384 state = ISS_PIPELINE_QUEUE_OUTPUT;
386 state = ISS_PIPELINE_QUEUE_INPUT;
388 spin_lock_irqsave(&pipe->lock, flags);
389 pipe->state |= state;
390 video->ops->queue(video, buffer);
391 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
393 start = iss_pipeline_ready(pipe);
395 pipe->state |= ISS_PIPELINE_STREAM;
396 spin_unlock_irqrestore(&pipe->lock, flags);
399 omap4iss_pipeline_set_stream(pipe,
400 ISS_PIPELINE_STREAM_SINGLESHOT);
404 static const struct vb2_ops iss_video_vb2ops = {
405 .queue_setup = iss_video_queue_setup,
406 .buf_prepare = iss_video_buf_prepare,
407 .buf_queue = iss_video_buf_queue,
408 .buf_cleanup = iss_video_buf_cleanup,
412 * omap4iss_video_buffer_next - Complete the current buffer and return the next
413 * @video: ISS video object
415 * Remove the current video buffer from the DMA queue and fill its timestamp,
416 * field count and state fields before waking up its completion handler.
418 * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
419 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
421 * The DMA queue is expected to contain at least one buffer.
423 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
426 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
428 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
429 enum iss_pipeline_state state;
430 struct iss_buffer *buf;
433 spin_lock_irqsave(&video->qlock, flags);
434 if (WARN_ON(list_empty(&video->dmaqueue))) {
435 spin_unlock_irqrestore(&video->qlock, flags);
439 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
441 list_del(&buf->list);
442 spin_unlock_irqrestore(&video->qlock, flags);
444 buf->vb.vb2_buf.timestamp = ktime_get_ns();
446 /* Do frame number propagation only if this is the output video node.
447 * Frame number either comes from the CSI receivers or it gets
448 * incremented here if H3A is not active.
449 * Note: There is no guarantee that the output buffer will finish
450 * first, so the input number might lag behind by 1 in some cases.
452 if (video == pipe->output && !pipe->do_propagation)
454 atomic_inc_return(&pipe->frame_number);
456 buf->vb.sequence = atomic_read(&pipe->frame_number);
458 vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
459 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
462 spin_lock_irqsave(&video->qlock, flags);
463 if (list_empty(&video->dmaqueue)) {
464 spin_unlock_irqrestore(&video->qlock, flags);
465 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
466 state = ISS_PIPELINE_QUEUE_OUTPUT
467 | ISS_PIPELINE_STREAM;
469 state = ISS_PIPELINE_QUEUE_INPUT
470 | ISS_PIPELINE_STREAM;
472 spin_lock_irqsave(&pipe->lock, flags);
473 pipe->state &= ~state;
474 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
475 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
476 spin_unlock_irqrestore(&pipe->lock, flags);
480 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) {
481 spin_lock(&pipe->lock);
482 pipe->state &= ~ISS_PIPELINE_STREAM;
483 spin_unlock(&pipe->lock);
486 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
488 spin_unlock_irqrestore(&video->qlock, flags);
489 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
494 * omap4iss_video_cancel_stream - Cancel stream on a video node
495 * @video: ISS video object
497 * Cancelling a stream mark all buffers on the video node as erroneous and makes
498 * sure no new buffer can be queued.
500 void omap4iss_video_cancel_stream(struct iss_video *video)
504 spin_lock_irqsave(&video->qlock, flags);
506 while (!list_empty(&video->dmaqueue)) {
507 struct iss_buffer *buf;
509 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
511 list_del(&buf->list);
512 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
515 vb2_queue_error(video->queue);
518 spin_unlock_irqrestore(&video->qlock, flags);
521 /* -----------------------------------------------------------------------------
526 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
528 struct iss_video *video = video_drvdata(file);
530 strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
531 strlcpy(cap->card, video->video.name, sizeof(cap->card));
532 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
534 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
535 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
537 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
539 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
540 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
546 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
548 struct iss_video *video = video_drvdata(file);
549 struct v4l2_mbus_framefmt format;
550 unsigned int index = f->index;
554 if (f->type != video->type)
557 ret = __iss_video_get_format(video, &format);
561 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
562 const struct iss_format_info *info = &formats[i];
564 if (format.code != info->code)
568 f->pixelformat = info->pixelformat;
569 strlcpy(f->description, info->description,
570 sizeof(f->description));
581 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
583 struct iss_video_fh *vfh = to_iss_video_fh(fh);
584 struct iss_video *video = video_drvdata(file);
586 if (format->type != video->type)
589 mutex_lock(&video->mutex);
590 *format = vfh->format;
591 mutex_unlock(&video->mutex);
597 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
599 struct iss_video_fh *vfh = to_iss_video_fh(fh);
600 struct iss_video *video = video_drvdata(file);
601 struct v4l2_mbus_framefmt fmt;
603 if (format->type != video->type)
606 mutex_lock(&video->mutex);
608 /* Fill the bytesperline and sizeimage fields by converting to media bus
609 * format and back to pixel format.
611 iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
612 iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
614 vfh->format = *format;
616 mutex_unlock(&video->mutex);
621 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
623 struct iss_video *video = video_drvdata(file);
624 struct v4l2_subdev_format fmt;
625 struct v4l2_subdev *subdev;
629 if (format->type != video->type)
632 subdev = iss_video_remote_subdev(video, &pad);
636 iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
639 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
640 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
644 iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
649 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
651 struct iss_video_fh *vfh = to_iss_video_fh(fh);
652 struct iss_video *video = video_drvdata(file);
654 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
655 video->type != a->type)
658 memset(a, 0, sizeof(*a));
659 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
660 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
661 a->parm.output.timeperframe = vfh->timeperframe;
667 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
669 struct iss_video_fh *vfh = to_iss_video_fh(fh);
670 struct iss_video *video = video_drvdata(file);
672 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
673 video->type != a->type)
676 if (a->parm.output.timeperframe.denominator == 0)
677 a->parm.output.timeperframe.denominator = 1;
679 vfh->timeperframe = a->parm.output.timeperframe;
685 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
687 struct iss_video_fh *vfh = to_iss_video_fh(fh);
689 return vb2_reqbufs(&vfh->queue, rb);
693 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
695 struct iss_video_fh *vfh = to_iss_video_fh(fh);
697 return vb2_querybuf(&vfh->queue, b);
701 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
703 struct iss_video_fh *vfh = to_iss_video_fh(fh);
705 return vb2_qbuf(&vfh->queue, b);
709 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
711 struct iss_video_fh *vfh = to_iss_video_fh(fh);
713 return vb2_expbuf(&vfh->queue, e);
717 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
719 struct iss_video_fh *vfh = to_iss_video_fh(fh);
721 return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
727 * Every ISS pipeline has a single input and a single output. The input can be
728 * either a sensor or a video node. The output is always a video node.
730 * As every pipeline has an output video node, the ISS video objects at the
731 * pipeline output stores the pipeline state. It tracks the streaming state of
732 * both the input and output, as well as the availability of buffers.
734 * In sensor-to-memory mode, frames are always available at the pipeline input.
735 * Starting the sensor usually requires I2C transfers and must be done in
736 * interruptible context. The pipeline is started and stopped synchronously
737 * to the stream on/off commands. All modules in the pipeline will get their
738 * subdev set stream handler called. The module at the end of the pipeline must
739 * delay starting the hardware until buffers are available at its output.
741 * In memory-to-memory mode, starting/stopping the stream requires
742 * synchronization between the input and output. ISS modules can't be stopped
743 * in the middle of a frame, and at least some of the modules seem to become
744 * busy as soon as they're started, even if they don't receive a frame start
745 * event. For that reason frames need to be processed in single-shot mode. The
746 * driver needs to wait until a frame is completely processed and written to
747 * memory before restarting the pipeline for the next frame. Pipelined
748 * processing might be possible but requires more testing.
750 * Stream start must be delayed until buffers are available at both the input
751 * and output. The pipeline must be started in the videobuf queue callback with
752 * the buffers queue spinlock held. The modules subdev set stream operation must
756 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
758 struct iss_video_fh *vfh = to_iss_video_fh(fh);
759 struct iss_video *video = video_drvdata(file);
760 struct media_entity_graph graph;
761 struct media_entity *entity = &video->video.entity;
762 enum iss_pipeline_state state;
763 struct iss_pipeline *pipe;
764 struct iss_video *far_end;
768 if (type != video->type)
771 mutex_lock(&video->stream_lock);
773 /* Start streaming on the pipeline. No link touching an entity in the
774 * pipeline can be activated or deactivated once streaming is started.
777 ? to_iss_pipeline(entity) : &video->pipe;
778 pipe->external = NULL;
779 pipe->external_rate = 0;
780 pipe->external_bpp = 0;
782 ret = media_entity_enum_init(&pipe->ent_enum, entity->graph_obj.mdev);
784 goto err_graph_walk_init;
786 ret = media_entity_graph_walk_init(&graph, entity->graph_obj.mdev);
788 goto err_graph_walk_init;
790 if (video->iss->pdata->set_constraints)
791 video->iss->pdata->set_constraints(video->iss, true);
793 ret = media_entity_pipeline_start(entity, &pipe->pipe);
795 goto err_media_entity_pipeline_start;
797 media_entity_graph_walk_start(&graph, entity);
798 while ((entity = media_entity_graph_walk_next(&graph)))
799 media_entity_enum_set(&pipe->ent_enum, entity);
801 /* Verify that the currently configured format matches the output of
802 * the connected subdev.
804 ret = iss_video_check_format(video, vfh);
806 goto err_iss_video_check_format;
808 video->bpl_padding = ret;
809 video->bpl_value = vfh->format.fmt.pix.bytesperline;
811 /* Find the ISS video node connected at the far end of the pipeline and
812 * update the pipeline.
814 far_end = iss_video_far_end(video);
816 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
817 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
818 pipe->input = far_end;
819 pipe->output = video;
823 goto err_iss_video_check_format;
826 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
828 pipe->output = far_end;
831 spin_lock_irqsave(&pipe->lock, flags);
832 pipe->state &= ~ISS_PIPELINE_STREAM;
833 pipe->state |= state;
834 spin_unlock_irqrestore(&pipe->lock, flags);
836 /* Set the maximum time per frame as the value requested by userspace.
837 * This is a soft limit that can be overridden if the hardware doesn't
838 * support the request limit.
840 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
841 pipe->max_timeperframe = vfh->timeperframe;
843 video->queue = &vfh->queue;
844 INIT_LIST_HEAD(&video->dmaqueue);
845 video->error = false;
846 atomic_set(&pipe->frame_number, -1);
848 ret = vb2_streamon(&vfh->queue, type);
850 goto err_iss_video_check_format;
852 /* In sensor-to-memory mode, the stream can be started synchronously
853 * to the stream on command. In memory-to-memory mode, it will be
854 * started when buffers are queued on both the input and output.
859 ret = omap4iss_pipeline_set_stream(pipe,
860 ISS_PIPELINE_STREAM_CONTINUOUS);
862 goto err_omap4iss_set_stream;
863 spin_lock_irqsave(&video->qlock, flags);
864 if (list_empty(&video->dmaqueue))
865 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
866 spin_unlock_irqrestore(&video->qlock, flags);
869 media_entity_graph_walk_cleanup(&graph);
871 mutex_unlock(&video->stream_lock);
875 err_omap4iss_set_stream:
876 vb2_streamoff(&vfh->queue, type);
877 err_iss_video_check_format:
878 media_entity_pipeline_stop(&video->video.entity);
879 err_media_entity_pipeline_start:
880 if (video->iss->pdata->set_constraints)
881 video->iss->pdata->set_constraints(video->iss, false);
884 media_entity_graph_walk_cleanup(&graph);
887 media_entity_enum_cleanup(&pipe->ent_enum);
889 mutex_unlock(&video->stream_lock);
895 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
897 struct iss_video_fh *vfh = to_iss_video_fh(fh);
898 struct iss_video *video = video_drvdata(file);
899 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
900 enum iss_pipeline_state state;
903 if (type != video->type)
906 mutex_lock(&video->stream_lock);
908 if (!vb2_is_streaming(&vfh->queue))
911 /* Update the pipeline state. */
912 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
913 state = ISS_PIPELINE_STREAM_OUTPUT
914 | ISS_PIPELINE_QUEUE_OUTPUT;
916 state = ISS_PIPELINE_STREAM_INPUT
917 | ISS_PIPELINE_QUEUE_INPUT;
919 spin_lock_irqsave(&pipe->lock, flags);
920 pipe->state &= ~state;
921 spin_unlock_irqrestore(&pipe->lock, flags);
923 /* Stop the stream. */
924 omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
925 vb2_streamoff(&vfh->queue, type);
928 media_entity_enum_cleanup(&pipe->ent_enum);
930 if (video->iss->pdata->set_constraints)
931 video->iss->pdata->set_constraints(video->iss, false);
932 media_entity_pipeline_stop(&video->video.entity);
935 mutex_unlock(&video->stream_lock);
940 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
942 if (input->index > 0)
945 strlcpy(input->name, "camera", sizeof(input->name));
946 input->type = V4L2_INPUT_TYPE_CAMERA;
952 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
960 iss_video_s_input(struct file *file, void *fh, unsigned int input)
962 return input == 0 ? 0 : -EINVAL;
965 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
966 .vidioc_querycap = iss_video_querycap,
967 .vidioc_enum_fmt_vid_cap = iss_video_enum_format,
968 .vidioc_g_fmt_vid_cap = iss_video_get_format,
969 .vidioc_s_fmt_vid_cap = iss_video_set_format,
970 .vidioc_try_fmt_vid_cap = iss_video_try_format,
971 .vidioc_g_fmt_vid_out = iss_video_get_format,
972 .vidioc_s_fmt_vid_out = iss_video_set_format,
973 .vidioc_try_fmt_vid_out = iss_video_try_format,
974 .vidioc_g_parm = iss_video_get_param,
975 .vidioc_s_parm = iss_video_set_param,
976 .vidioc_reqbufs = iss_video_reqbufs,
977 .vidioc_querybuf = iss_video_querybuf,
978 .vidioc_qbuf = iss_video_qbuf,
979 .vidioc_expbuf = iss_video_expbuf,
980 .vidioc_dqbuf = iss_video_dqbuf,
981 .vidioc_streamon = iss_video_streamon,
982 .vidioc_streamoff = iss_video_streamoff,
983 .vidioc_enum_input = iss_video_enum_input,
984 .vidioc_g_input = iss_video_g_input,
985 .vidioc_s_input = iss_video_s_input,
988 /* -----------------------------------------------------------------------------
989 * V4L2 file operations
992 static int iss_video_open(struct file *file)
994 struct iss_video *video = video_drvdata(file);
995 struct iss_video_fh *handle;
999 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1003 v4l2_fh_init(&handle->vfh, &video->video);
1004 v4l2_fh_add(&handle->vfh);
1006 /* If this is the first user, initialise the pipeline. */
1007 if (!omap4iss_get(video->iss)) {
1012 ret = v4l2_pipeline_pm_use(&video->video.entity, 1);
1014 omap4iss_put(video->iss);
1020 q->type = video->type;
1021 q->io_modes = VB2_MMAP | VB2_DMABUF;
1022 q->drv_priv = handle;
1023 q->ops = &iss_video_vb2ops;
1024 q->mem_ops = &vb2_dma_contig_memops;
1025 q->buf_struct_size = sizeof(struct iss_buffer);
1026 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1027 q->dev = video->iss->dev;
1029 ret = vb2_queue_init(q);
1031 omap4iss_put(video->iss);
1035 memset(&handle->format, 0, sizeof(handle->format));
1036 handle->format.type = video->type;
1037 handle->timeperframe.denominator = 1;
1039 handle->video = video;
1040 file->private_data = &handle->vfh;
1044 v4l2_fh_del(&handle->vfh);
1051 static int iss_video_release(struct file *file)
1053 struct iss_video *video = video_drvdata(file);
1054 struct v4l2_fh *vfh = file->private_data;
1055 struct iss_video_fh *handle = to_iss_video_fh(vfh);
1057 /* Disable streaming and free the buffers queue resources. */
1058 iss_video_streamoff(file, vfh, video->type);
1060 v4l2_pipeline_pm_use(&video->video.entity, 0);
1062 /* Release the videobuf2 queue */
1063 vb2_queue_release(&handle->queue);
1067 file->private_data = NULL;
1069 omap4iss_put(video->iss);
1074 static unsigned int iss_video_poll(struct file *file, poll_table *wait)
1076 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1078 return vb2_poll(&vfh->queue, file, wait);
1081 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1083 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1085 return vb2_mmap(&vfh->queue, vma);
1088 static struct v4l2_file_operations iss_video_fops = {
1089 .owner = THIS_MODULE,
1090 .unlocked_ioctl = video_ioctl2,
1091 .open = iss_video_open,
1092 .release = iss_video_release,
1093 .poll = iss_video_poll,
1094 .mmap = iss_video_mmap,
1097 /* -----------------------------------------------------------------------------
1101 static const struct iss_video_operations iss_video_dummy_ops = {
1104 int omap4iss_video_init(struct iss_video *video, const char *name)
1106 const char *direction;
1109 switch (video->type) {
1110 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1111 direction = "output";
1112 video->pad.flags = MEDIA_PAD_FL_SINK;
1114 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1115 direction = "input";
1116 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1123 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1127 spin_lock_init(&video->qlock);
1128 mutex_init(&video->mutex);
1129 atomic_set(&video->active, 0);
1131 spin_lock_init(&video->pipe.lock);
1132 mutex_init(&video->stream_lock);
1134 /* Initialize the video device. */
1136 video->ops = &iss_video_dummy_ops;
1138 video->video.fops = &iss_video_fops;
1139 snprintf(video->video.name, sizeof(video->video.name),
1140 "OMAP4 ISS %s %s", name, direction);
1141 video->video.vfl_type = VFL_TYPE_GRABBER;
1142 video->video.release = video_device_release_empty;
1143 video->video.ioctl_ops = &iss_video_ioctl_ops;
1144 video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1146 video_set_drvdata(&video->video, video);
1151 void omap4iss_video_cleanup(struct iss_video *video)
1153 media_entity_cleanup(&video->video.entity);
1154 mutex_destroy(&video->stream_lock);
1155 mutex_destroy(&video->mutex);
1158 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1162 video->video.v4l2_dev = vdev;
1164 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1166 dev_err(video->iss->dev,
1167 "could not register video device (%d)\n", ret);
1172 void omap4iss_video_unregister(struct iss_video *video)
1174 video_unregister_device(&video->video);