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>
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-ioctl.h>
25 #include "iss_video.h"
28 /* -----------------------------------------------------------------------------
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)", },
92 const struct iss_format_info *
93 omap4iss_video_format_info(u32 code)
97 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
98 if (formats[i].code == code)
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)
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.
115 * Return the number of padding bytes at end of line.
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)
121 unsigned int bpl = pix->bytesperline;
122 unsigned int min_bpl;
125 memset(pix, 0, sizeof(*pix));
126 pix->width = mbus->width;
127 pix->height = mbus->height;
129 /* Skip the last format in the loop so that it will be selected if no
132 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
133 if (formats[i].code == mbus->code)
137 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
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.
144 bpl = clamp(bpl, min_bpl, video->bpl_max);
148 if (!video->bpl_zero_padding || bpl != min_bpl)
149 bpl = ALIGN(bpl, video->bpl_alignment);
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;
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;
161 return bpl - min_bpl;
164 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
165 struct v4l2_mbus_framefmt *mbus)
169 memset(mbus, 0, sizeof(*mbus));
170 mbus->width = pix->width;
171 mbus->height = pix->height;
173 /* Skip the last format in the loop so that it will be selected if no
176 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
177 if (formats[i].pixelformat == pix->pixelformat)
181 mbus->code = formats[i].code;
182 mbus->colorspace = pix->colorspace;
183 mbus->field = pix->field;
186 static struct v4l2_subdev *
187 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
189 struct media_pad *remote;
191 remote = media_entity_remote_pad(&video->pad);
194 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
198 *pad = remote->index;
200 return media_entity_to_v4l2_subdev(remote->entity);
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)
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;
212 mutex_lock(&mdev->graph_mutex);
213 media_entity_graph_walk_start(&graph, entity);
215 while ((entity = media_entity_graph_walk_next(&graph))) {
216 if (entity == &video->video.entity)
219 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
222 far_end = to_iss_video(media_entity_to_video_device(entity));
223 if (far_end->type != video->type)
229 mutex_unlock(&mdev->graph_mutex);
234 __iss_video_get_format(struct iss_video *video,
235 struct v4l2_mbus_framefmt *format)
237 struct v4l2_subdev_format fmt;
238 struct v4l2_subdev *subdev;
242 subdev = iss_video_remote_subdev(video, &pad);
246 memset(&fmt, 0, sizeof(fmt));
248 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
250 mutex_lock(&video->mutex);
251 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
252 mutex_unlock(&video->mutex);
257 *format = fmt.format;
262 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
264 struct v4l2_mbus_framefmt format;
265 struct v4l2_pix_format pixfmt;
268 ret = __iss_video_get_format(video, &format);
272 pixfmt.bytesperline = 0;
273 ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
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)
285 /* -----------------------------------------------------------------------------
286 * Video queue operations
289 static int iss_video_queue_setup(struct vb2_queue *vq,
291 unsigned int *count, unsigned int *num_planes,
292 unsigned int sizes[], void *alloc_ctxs[])
294 struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
295 struct iss_video *video = vfh->video;
297 /* Revisit multi-planar support for NV12 */
300 sizes[0] = vfh->format.fmt.pix.sizeimage;
304 alloc_ctxs[0] = video->alloc_ctx;
306 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
311 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
313 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
314 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
316 if (buffer->iss_addr)
317 buffer->iss_addr = 0;
320 static int iss_video_buf_prepare(struct vb2_buffer *vb)
322 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
323 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
324 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
325 struct iss_video *video = vfh->video;
326 unsigned long size = vfh->format.fmt.pix.sizeimage;
329 if (vb2_plane_size(vb, 0) < size)
332 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
333 if (!IS_ALIGNED(addr, 32)) {
334 dev_dbg(video->iss->dev,
335 "Buffer address must be aligned to 32 bytes boundary.\n");
339 vb2_set_plane_payload(vb, 0, size);
340 buffer->iss_addr = addr;
344 static void iss_video_buf_queue(struct vb2_buffer *vb)
346 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
347 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
348 struct iss_video *video = vfh->video;
349 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
350 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
354 spin_lock_irqsave(&video->qlock, flags);
356 /* Mark the buffer is faulty and give it back to the queue immediately
357 * if the video node has registered an error. vb2 will perform the same
358 * check when preparing the buffer, but that is inherently racy, so we
359 * need to handle the race condition with an authoritative check here.
361 if (unlikely(video->error)) {
362 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
363 spin_unlock_irqrestore(&video->qlock, flags);
367 empty = list_empty(&video->dmaqueue);
368 list_add_tail(&buffer->list, &video->dmaqueue);
370 spin_unlock_irqrestore(&video->qlock, flags);
373 enum iss_pipeline_state state;
376 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
377 state = ISS_PIPELINE_QUEUE_OUTPUT;
379 state = ISS_PIPELINE_QUEUE_INPUT;
381 spin_lock_irqsave(&pipe->lock, flags);
382 pipe->state |= state;
383 video->ops->queue(video, buffer);
384 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
386 start = iss_pipeline_ready(pipe);
388 pipe->state |= ISS_PIPELINE_STREAM;
389 spin_unlock_irqrestore(&pipe->lock, flags);
392 omap4iss_pipeline_set_stream(pipe,
393 ISS_PIPELINE_STREAM_SINGLESHOT);
397 static const struct vb2_ops iss_video_vb2ops = {
398 .queue_setup = iss_video_queue_setup,
399 .buf_prepare = iss_video_buf_prepare,
400 .buf_queue = iss_video_buf_queue,
401 .buf_cleanup = iss_video_buf_cleanup,
405 * omap4iss_video_buffer_next - Complete the current buffer and return the next
406 * @video: ISS video object
408 * Remove the current video buffer from the DMA queue and fill its timestamp,
409 * field count and state fields before waking up its completion handler.
411 * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
412 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
414 * The DMA queue is expected to contain at least one buffer.
416 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
419 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
421 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
422 enum iss_pipeline_state state;
423 struct iss_buffer *buf;
426 spin_lock_irqsave(&video->qlock, flags);
427 if (WARN_ON(list_empty(&video->dmaqueue))) {
428 spin_unlock_irqrestore(&video->qlock, flags);
432 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
434 list_del(&buf->list);
435 spin_unlock_irqrestore(&video->qlock, flags);
437 v4l2_get_timestamp(&buf->vb.timestamp);
439 /* Do frame number propagation only if this is the output video node.
440 * Frame number either comes from the CSI receivers or it gets
441 * incremented here if H3A is not active.
442 * Note: There is no guarantee that the output buffer will finish
443 * first, so the input number might lag behind by 1 in some cases.
445 if (video == pipe->output && !pipe->do_propagation)
447 atomic_inc_return(&pipe->frame_number);
449 buf->vb.sequence = atomic_read(&pipe->frame_number);
451 vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
452 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
455 spin_lock_irqsave(&video->qlock, flags);
456 if (list_empty(&video->dmaqueue)) {
457 spin_unlock_irqrestore(&video->qlock, flags);
458 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
459 state = ISS_PIPELINE_QUEUE_OUTPUT
460 | ISS_PIPELINE_STREAM;
462 state = ISS_PIPELINE_QUEUE_INPUT
463 | ISS_PIPELINE_STREAM;
465 spin_lock_irqsave(&pipe->lock, flags);
466 pipe->state &= ~state;
467 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
468 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
469 spin_unlock_irqrestore(&pipe->lock, flags);
473 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) {
474 spin_lock(&pipe->lock);
475 pipe->state &= ~ISS_PIPELINE_STREAM;
476 spin_unlock(&pipe->lock);
479 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
481 spin_unlock_irqrestore(&video->qlock, flags);
482 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
487 * omap4iss_video_cancel_stream - Cancel stream on a video node
488 * @video: ISS video object
490 * Cancelling a stream mark all buffers on the video node as erroneous and makes
491 * sure no new buffer can be queued.
493 void omap4iss_video_cancel_stream(struct iss_video *video)
497 spin_lock_irqsave(&video->qlock, flags);
499 while (!list_empty(&video->dmaqueue)) {
500 struct iss_buffer *buf;
502 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
504 list_del(&buf->list);
505 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
508 vb2_queue_error(video->queue);
511 spin_unlock_irqrestore(&video->qlock, flags);
514 /* -----------------------------------------------------------------------------
519 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
521 struct iss_video *video = video_drvdata(file);
523 strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
524 strlcpy(cap->card, video->video.name, sizeof(cap->card));
525 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
527 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
528 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
530 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
532 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
533 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
539 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
541 struct iss_video *video = video_drvdata(file);
542 struct v4l2_mbus_framefmt format;
543 unsigned int index = f->index;
547 if (f->type != video->type)
550 ret = __iss_video_get_format(video, &format);
554 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
555 const struct iss_format_info *info = &formats[i];
557 if (format.code != info->code)
561 f->pixelformat = info->pixelformat;
562 strlcpy(f->description, info->description,
563 sizeof(f->description));
574 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
576 struct iss_video_fh *vfh = to_iss_video_fh(fh);
577 struct iss_video *video = video_drvdata(file);
579 if (format->type != video->type)
582 mutex_lock(&video->mutex);
583 *format = vfh->format;
584 mutex_unlock(&video->mutex);
590 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
592 struct iss_video_fh *vfh = to_iss_video_fh(fh);
593 struct iss_video *video = video_drvdata(file);
594 struct v4l2_mbus_framefmt fmt;
596 if (format->type != video->type)
599 mutex_lock(&video->mutex);
601 /* Fill the bytesperline and sizeimage fields by converting to media bus
602 * format and back to pixel format.
604 iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
605 iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
607 vfh->format = *format;
609 mutex_unlock(&video->mutex);
614 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
616 struct iss_video *video = video_drvdata(file);
617 struct v4l2_subdev_format fmt;
618 struct v4l2_subdev *subdev;
622 if (format->type != video->type)
625 subdev = iss_video_remote_subdev(video, &pad);
629 iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
632 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
633 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
637 iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
642 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
644 struct iss_video_fh *vfh = to_iss_video_fh(fh);
645 struct iss_video *video = video_drvdata(file);
647 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
648 video->type != a->type)
651 memset(a, 0, sizeof(*a));
652 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
653 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
654 a->parm.output.timeperframe = vfh->timeperframe;
660 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
662 struct iss_video_fh *vfh = to_iss_video_fh(fh);
663 struct iss_video *video = video_drvdata(file);
665 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
666 video->type != a->type)
669 if (a->parm.output.timeperframe.denominator == 0)
670 a->parm.output.timeperframe.denominator = 1;
672 vfh->timeperframe = a->parm.output.timeperframe;
678 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
680 struct iss_video_fh *vfh = to_iss_video_fh(fh);
682 return vb2_reqbufs(&vfh->queue, rb);
686 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
688 struct iss_video_fh *vfh = to_iss_video_fh(fh);
690 return vb2_querybuf(&vfh->queue, b);
694 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
696 struct iss_video_fh *vfh = to_iss_video_fh(fh);
698 return vb2_qbuf(&vfh->queue, b);
702 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
704 struct iss_video_fh *vfh = to_iss_video_fh(fh);
706 return vb2_expbuf(&vfh->queue, e);
710 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
712 struct iss_video_fh *vfh = to_iss_video_fh(fh);
714 return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
720 * Every ISS pipeline has a single input and a single output. The input can be
721 * either a sensor or a video node. The output is always a video node.
723 * As every pipeline has an output video node, the ISS video objects at the
724 * pipeline output stores the pipeline state. It tracks the streaming state of
725 * both the input and output, as well as the availability of buffers.
727 * In sensor-to-memory mode, frames are always available at the pipeline input.
728 * Starting the sensor usually requires I2C transfers and must be done in
729 * interruptible context. The pipeline is started and stopped synchronously
730 * to the stream on/off commands. All modules in the pipeline will get their
731 * subdev set stream handler called. The module at the end of the pipeline must
732 * delay starting the hardware until buffers are available at its output.
734 * In memory-to-memory mode, starting/stopping the stream requires
735 * synchronization between the input and output. ISS modules can't be stopped
736 * in the middle of a frame, and at least some of the modules seem to become
737 * busy as soon as they're started, even if they don't receive a frame start
738 * event. For that reason frames need to be processed in single-shot mode. The
739 * driver needs to wait until a frame is completely processed and written to
740 * memory before restarting the pipeline for the next frame. Pipelined
741 * processing might be possible but requires more testing.
743 * Stream start must be delayed until buffers are available at both the input
744 * and output. The pipeline must be started in the videobuf queue callback with
745 * the buffers queue spinlock held. The modules subdev set stream operation must
749 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
751 struct iss_video_fh *vfh = to_iss_video_fh(fh);
752 struct iss_video *video = video_drvdata(file);
753 struct media_entity_graph graph;
754 struct media_entity *entity;
755 enum iss_pipeline_state state;
756 struct iss_pipeline *pipe;
757 struct iss_video *far_end;
761 if (type != video->type)
764 mutex_lock(&video->stream_lock);
766 /* Start streaming on the pipeline. No link touching an entity in the
767 * pipeline can be activated or deactivated once streaming is started.
769 pipe = video->video.entity.pipe
770 ? to_iss_pipeline(&video->video.entity) : &video->pipe;
771 pipe->external = NULL;
772 pipe->external_rate = 0;
773 pipe->external_bpp = 0;
776 if (video->iss->pdata->set_constraints)
777 video->iss->pdata->set_constraints(video->iss, true);
779 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
781 goto err_media_entity_pipeline_start;
783 entity = &video->video.entity;
784 media_entity_graph_walk_start(&graph, entity);
785 while ((entity = media_entity_graph_walk_next(&graph)))
786 pipe->entities |= 1 << entity->id;
788 /* Verify that the currently configured format matches the output of
789 * the connected subdev.
791 ret = iss_video_check_format(video, vfh);
793 goto err_iss_video_check_format;
795 video->bpl_padding = ret;
796 video->bpl_value = vfh->format.fmt.pix.bytesperline;
798 /* Find the ISS video node connected at the far end of the pipeline and
799 * update the pipeline.
801 far_end = iss_video_far_end(video);
803 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
804 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
805 pipe->input = far_end;
806 pipe->output = video;
810 goto err_iss_video_check_format;
813 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
815 pipe->output = far_end;
818 spin_lock_irqsave(&pipe->lock, flags);
819 pipe->state &= ~ISS_PIPELINE_STREAM;
820 pipe->state |= state;
821 spin_unlock_irqrestore(&pipe->lock, flags);
823 /* Set the maximum time per frame as the value requested by userspace.
824 * This is a soft limit that can be overridden if the hardware doesn't
825 * support the request limit.
827 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
828 pipe->max_timeperframe = vfh->timeperframe;
830 video->queue = &vfh->queue;
831 INIT_LIST_HEAD(&video->dmaqueue);
832 video->error = false;
833 atomic_set(&pipe->frame_number, -1);
835 ret = vb2_streamon(&vfh->queue, type);
837 goto err_iss_video_check_format;
839 /* In sensor-to-memory mode, the stream can be started synchronously
840 * to the stream on command. In memory-to-memory mode, it will be
841 * started when buffers are queued on both the input and output.
846 ret = omap4iss_pipeline_set_stream(pipe,
847 ISS_PIPELINE_STREAM_CONTINUOUS);
849 goto err_omap4iss_set_stream;
850 spin_lock_irqsave(&video->qlock, flags);
851 if (list_empty(&video->dmaqueue))
852 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
853 spin_unlock_irqrestore(&video->qlock, flags);
856 mutex_unlock(&video->stream_lock);
859 err_omap4iss_set_stream:
860 vb2_streamoff(&vfh->queue, type);
861 err_iss_video_check_format:
862 media_entity_pipeline_stop(&video->video.entity);
863 err_media_entity_pipeline_start:
864 if (video->iss->pdata->set_constraints)
865 video->iss->pdata->set_constraints(video->iss, false);
868 mutex_unlock(&video->stream_lock);
873 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
875 struct iss_video_fh *vfh = to_iss_video_fh(fh);
876 struct iss_video *video = video_drvdata(file);
877 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
878 enum iss_pipeline_state state;
881 if (type != video->type)
884 mutex_lock(&video->stream_lock);
886 if (!vb2_is_streaming(&vfh->queue))
889 /* Update the pipeline state. */
890 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
891 state = ISS_PIPELINE_STREAM_OUTPUT
892 | ISS_PIPELINE_QUEUE_OUTPUT;
894 state = ISS_PIPELINE_STREAM_INPUT
895 | ISS_PIPELINE_QUEUE_INPUT;
897 spin_lock_irqsave(&pipe->lock, flags);
898 pipe->state &= ~state;
899 spin_unlock_irqrestore(&pipe->lock, flags);
901 /* Stop the stream. */
902 omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
903 vb2_streamoff(&vfh->queue, type);
906 if (video->iss->pdata->set_constraints)
907 video->iss->pdata->set_constraints(video->iss, false);
908 media_entity_pipeline_stop(&video->video.entity);
911 mutex_unlock(&video->stream_lock);
916 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
918 if (input->index > 0)
921 strlcpy(input->name, "camera", sizeof(input->name));
922 input->type = V4L2_INPUT_TYPE_CAMERA;
928 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
936 iss_video_s_input(struct file *file, void *fh, unsigned int input)
938 return input == 0 ? 0 : -EINVAL;
941 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
942 .vidioc_querycap = iss_video_querycap,
943 .vidioc_enum_fmt_vid_cap = iss_video_enum_format,
944 .vidioc_g_fmt_vid_cap = iss_video_get_format,
945 .vidioc_s_fmt_vid_cap = iss_video_set_format,
946 .vidioc_try_fmt_vid_cap = iss_video_try_format,
947 .vidioc_g_fmt_vid_out = iss_video_get_format,
948 .vidioc_s_fmt_vid_out = iss_video_set_format,
949 .vidioc_try_fmt_vid_out = iss_video_try_format,
950 .vidioc_g_parm = iss_video_get_param,
951 .vidioc_s_parm = iss_video_set_param,
952 .vidioc_reqbufs = iss_video_reqbufs,
953 .vidioc_querybuf = iss_video_querybuf,
954 .vidioc_qbuf = iss_video_qbuf,
955 .vidioc_expbuf = iss_video_expbuf,
956 .vidioc_dqbuf = iss_video_dqbuf,
957 .vidioc_streamon = iss_video_streamon,
958 .vidioc_streamoff = iss_video_streamoff,
959 .vidioc_enum_input = iss_video_enum_input,
960 .vidioc_g_input = iss_video_g_input,
961 .vidioc_s_input = iss_video_s_input,
964 /* -----------------------------------------------------------------------------
965 * V4L2 file operations
968 static int iss_video_open(struct file *file)
970 struct iss_video *video = video_drvdata(file);
971 struct iss_video_fh *handle;
975 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
979 v4l2_fh_init(&handle->vfh, &video->video);
980 v4l2_fh_add(&handle->vfh);
982 /* If this is the first user, initialise the pipeline. */
983 if (!omap4iss_get(video->iss)) {
988 ret = omap4iss_pipeline_pm_use(&video->video.entity, 1);
990 omap4iss_put(video->iss);
994 video->alloc_ctx = vb2_dma_contig_init_ctx(video->iss->dev);
995 if (IS_ERR(video->alloc_ctx)) {
996 ret = PTR_ERR(video->alloc_ctx);
997 omap4iss_put(video->iss);
1003 q->type = video->type;
1004 q->io_modes = VB2_MMAP | VB2_DMABUF;
1005 q->drv_priv = handle;
1006 q->ops = &iss_video_vb2ops;
1007 q->mem_ops = &vb2_dma_contig_memops;
1008 q->buf_struct_size = sizeof(struct iss_buffer);
1009 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1011 ret = vb2_queue_init(q);
1013 omap4iss_put(video->iss);
1017 memset(&handle->format, 0, sizeof(handle->format));
1018 handle->format.type = video->type;
1019 handle->timeperframe.denominator = 1;
1021 handle->video = video;
1022 file->private_data = &handle->vfh;
1026 v4l2_fh_del(&handle->vfh);
1033 static int iss_video_release(struct file *file)
1035 struct iss_video *video = video_drvdata(file);
1036 struct v4l2_fh *vfh = file->private_data;
1037 struct iss_video_fh *handle = to_iss_video_fh(vfh);
1039 /* Disable streaming and free the buffers queue resources. */
1040 iss_video_streamoff(file, vfh, video->type);
1042 omap4iss_pipeline_pm_use(&video->video.entity, 0);
1044 /* Release the videobuf2 queue */
1045 vb2_queue_release(&handle->queue);
1047 /* Release the file handle. */
1050 file->private_data = NULL;
1052 omap4iss_put(video->iss);
1057 static unsigned int iss_video_poll(struct file *file, poll_table *wait)
1059 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1061 return vb2_poll(&vfh->queue, file, wait);
1064 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1066 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1068 return vb2_mmap(&vfh->queue, vma);
1071 static struct v4l2_file_operations iss_video_fops = {
1072 .owner = THIS_MODULE,
1073 .unlocked_ioctl = video_ioctl2,
1074 .open = iss_video_open,
1075 .release = iss_video_release,
1076 .poll = iss_video_poll,
1077 .mmap = iss_video_mmap,
1080 /* -----------------------------------------------------------------------------
1084 static const struct iss_video_operations iss_video_dummy_ops = {
1087 int omap4iss_video_init(struct iss_video *video, const char *name)
1089 const char *direction;
1092 switch (video->type) {
1093 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1094 direction = "output";
1095 video->pad.flags = MEDIA_PAD_FL_SINK;
1097 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1098 direction = "input";
1099 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1106 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1110 spin_lock_init(&video->qlock);
1111 mutex_init(&video->mutex);
1112 atomic_set(&video->active, 0);
1114 spin_lock_init(&video->pipe.lock);
1115 mutex_init(&video->stream_lock);
1117 /* Initialize the video device. */
1119 video->ops = &iss_video_dummy_ops;
1121 video->video.fops = &iss_video_fops;
1122 snprintf(video->video.name, sizeof(video->video.name),
1123 "OMAP4 ISS %s %s", name, direction);
1124 video->video.vfl_type = VFL_TYPE_GRABBER;
1125 video->video.release = video_device_release_empty;
1126 video->video.ioctl_ops = &iss_video_ioctl_ops;
1127 video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1129 video_set_drvdata(&video->video, video);
1134 void omap4iss_video_cleanup(struct iss_video *video)
1136 media_entity_cleanup(&video->video.entity);
1137 mutex_destroy(&video->stream_lock);
1138 mutex_destroy(&video->mutex);
1141 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1145 video->video.v4l2_dev = vdev;
1147 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1149 dev_err(video->iss->dev,
1150 "could not register video device (%d)\n", ret);
1155 void omap4iss_video_unregister(struct iss_video *video)
1157 video_unregister_device(&video->video);