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"
29 /* -----------------------------------------------------------------------------
33 static struct iss_format_info formats[] = {
34 { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
35 MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
36 V4L2_PIX_FMT_GREY, 8, "Greyscale 8 bpp", },
37 { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
38 MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
39 V4L2_PIX_FMT_Y10, 10, "Greyscale 10 bpp", },
40 { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
41 MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
42 V4L2_PIX_FMT_Y12, 12, "Greyscale 12 bpp", },
43 { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
44 MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
45 V4L2_PIX_FMT_SBGGR8, 8, "BGGR Bayer 8 bpp", },
46 { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
47 MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
48 V4L2_PIX_FMT_SGBRG8, 8, "GBRG Bayer 8 bpp", },
49 { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
50 MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
51 V4L2_PIX_FMT_SGRBG8, 8, "GRBG Bayer 8 bpp", },
52 { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
53 MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
54 V4L2_PIX_FMT_SRGGB8, 8, "RGGB Bayer 8 bpp", },
55 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
56 MEDIA_BUS_FMT_SGRBG10_1X10, 0,
57 V4L2_PIX_FMT_SGRBG10DPCM8, 8, "GRBG Bayer 10 bpp DPCM8", },
58 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
59 MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
60 V4L2_PIX_FMT_SBGGR10, 10, "BGGR Bayer 10 bpp", },
61 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
62 MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
63 V4L2_PIX_FMT_SGBRG10, 10, "GBRG Bayer 10 bpp", },
64 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
65 MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
66 V4L2_PIX_FMT_SGRBG10, 10, "GRBG Bayer 10 bpp", },
67 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
68 MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
69 V4L2_PIX_FMT_SRGGB10, 10, "RGGB Bayer 10 bpp", },
70 { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
71 MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
72 V4L2_PIX_FMT_SBGGR12, 12, "BGGR Bayer 12 bpp", },
73 { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
74 MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
75 V4L2_PIX_FMT_SGBRG12, 12, "GBRG Bayer 12 bpp", },
76 { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
77 MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
78 V4L2_PIX_FMT_SGRBG12, 12, "GRBG Bayer 12 bpp", },
79 { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
80 MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
81 V4L2_PIX_FMT_SRGGB12, 12, "RGGB Bayer 12 bpp", },
82 { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
83 MEDIA_BUS_FMT_UYVY8_1X16, 0,
84 V4L2_PIX_FMT_UYVY, 16, "YUV 4:2:2 (UYVY)", },
85 { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
86 MEDIA_BUS_FMT_YUYV8_1X16, 0,
87 V4L2_PIX_FMT_YUYV, 16, "YUV 4:2:2 (YUYV)", },
88 { MEDIA_BUS_FMT_YUYV8_1_5X8, MEDIA_BUS_FMT_YUYV8_1_5X8,
89 MEDIA_BUS_FMT_YUYV8_1_5X8, 0,
90 V4L2_PIX_FMT_NV12, 8, "YUV 4:2:0 (NV12)", },
93 const struct iss_format_info *
94 omap4iss_video_format_info(u32 code)
98 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
99 if (formats[i].code == code)
107 * iss_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
108 * @video: ISS video instance
109 * @mbus: v4l2_mbus_framefmt format (input)
110 * @pix: v4l2_pix_format format (output)
112 * Fill the output pix structure with information from the input mbus format.
113 * The bytesperline and sizeimage fields are computed from the requested bytes
114 * per line value in the pix format and information from the video instance.
116 * Return the number of padding bytes at end of line.
118 static unsigned int iss_video_mbus_to_pix(const struct iss_video *video,
119 const struct v4l2_mbus_framefmt *mbus,
120 struct v4l2_pix_format *pix)
122 unsigned int bpl = pix->bytesperline;
123 unsigned int min_bpl;
126 memset(pix, 0, sizeof(*pix));
127 pix->width = mbus->width;
128 pix->height = mbus->height;
130 /* Skip the last format in the loop so that it will be selected if no
133 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
134 if (formats[i].code == mbus->code)
138 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
140 /* Clamp the requested bytes per line value. If the maximum bytes per
141 * line value is zero, the module doesn't support user configurable line
142 * sizes. Override the requested value with the minimum in that case.
145 bpl = clamp(bpl, min_bpl, video->bpl_max);
149 if (!video->bpl_zero_padding || bpl != min_bpl)
150 bpl = ALIGN(bpl, video->bpl_alignment);
152 pix->pixelformat = formats[i].pixelformat;
153 pix->bytesperline = bpl;
154 pix->sizeimage = pix->bytesperline * pix->height;
155 pix->colorspace = mbus->colorspace;
156 pix->field = mbus->field;
158 /* FIXME: Special case for NV12! We should make this nicer... */
159 if (pix->pixelformat == V4L2_PIX_FMT_NV12)
160 pix->sizeimage += (pix->bytesperline * pix->height) / 2;
162 return bpl - min_bpl;
165 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
166 struct v4l2_mbus_framefmt *mbus)
170 memset(mbus, 0, sizeof(*mbus));
171 mbus->width = pix->width;
172 mbus->height = pix->height;
174 /* Skip the last format in the loop so that it will be selected if no
177 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
178 if (formats[i].pixelformat == pix->pixelformat)
182 mbus->code = formats[i].code;
183 mbus->colorspace = pix->colorspace;
184 mbus->field = pix->field;
187 static struct v4l2_subdev *
188 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
190 struct media_pad *remote;
192 remote = media_entity_remote_pad(&video->pad);
194 if (remote == NULL ||
195 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
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->parent;
211 struct iss_video *far_end = NULL;
213 mutex_lock(&mdev->graph_mutex);
214 media_entity_graph_walk_start(&graph, entity);
216 while ((entity = media_entity_graph_walk_next(&graph))) {
217 if (entity == &video->video.entity)
220 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
223 far_end = to_iss_video(media_entity_to_video_device(entity));
224 if (far_end->type != video->type)
230 mutex_unlock(&mdev->graph_mutex);
235 __iss_video_get_format(struct iss_video *video,
236 struct v4l2_mbus_framefmt *format)
238 struct v4l2_subdev_format fmt;
239 struct v4l2_subdev *subdev;
243 subdev = iss_video_remote_subdev(video, &pad);
247 memset(&fmt, 0, sizeof(fmt));
249 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
251 mutex_lock(&video->mutex);
252 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
253 mutex_unlock(&video->mutex);
258 *format = fmt.format;
263 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
265 struct v4l2_mbus_framefmt format;
266 struct v4l2_pix_format pixfmt;
269 ret = __iss_video_get_format(video, &format);
273 pixfmt.bytesperline = 0;
274 ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
276 if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat ||
277 vfh->format.fmt.pix.height != pixfmt.height ||
278 vfh->format.fmt.pix.width != pixfmt.width ||
279 vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline ||
280 vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage)
286 /* -----------------------------------------------------------------------------
287 * Video queue operations
290 static int iss_video_queue_setup(struct vb2_queue *vq,
292 unsigned int *count, unsigned int *num_planes,
293 unsigned int sizes[], void *alloc_ctxs[])
295 struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
296 struct iss_video *video = vfh->video;
298 /* Revisit multi-planar support for NV12 */
301 sizes[0] = vfh->format.fmt.pix.sizeimage;
305 alloc_ctxs[0] = video->alloc_ctx;
307 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
312 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
314 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
315 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
317 if (buffer->iss_addr)
318 buffer->iss_addr = 0;
321 static int iss_video_buf_prepare(struct vb2_buffer *vb)
323 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
324 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
325 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
326 struct iss_video *video = vfh->video;
327 unsigned long size = vfh->format.fmt.pix.sizeimage;
330 if (vb2_plane_size(vb, 0) < size)
333 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
334 if (!IS_ALIGNED(addr, 32)) {
335 dev_dbg(video->iss->dev,
336 "Buffer address must be aligned to 32 bytes boundary.\n");
340 vb2_set_plane_payload(vb, 0, size);
341 buffer->iss_addr = addr;
345 static void iss_video_buf_queue(struct vb2_buffer *vb)
347 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
348 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
349 struct iss_video *video = vfh->video;
350 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
351 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
355 spin_lock_irqsave(&video->qlock, flags);
357 /* Mark the buffer is faulty and give it back to the queue immediately
358 * if the video node has registered an error. vb2 will perform the same
359 * check when preparing the buffer, but that is inherently racy, so we
360 * need to handle the race condition with an authoritative check here.
362 if (unlikely(video->error)) {
363 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
364 spin_unlock_irqrestore(&video->qlock, flags);
368 empty = list_empty(&video->dmaqueue);
369 list_add_tail(&buffer->list, &video->dmaqueue);
371 spin_unlock_irqrestore(&video->qlock, flags);
374 enum iss_pipeline_state state;
377 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
378 state = ISS_PIPELINE_QUEUE_OUTPUT;
380 state = ISS_PIPELINE_QUEUE_INPUT;
382 spin_lock_irqsave(&pipe->lock, flags);
383 pipe->state |= state;
384 video->ops->queue(video, buffer);
385 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
387 start = iss_pipeline_ready(pipe);
389 pipe->state |= ISS_PIPELINE_STREAM;
390 spin_unlock_irqrestore(&pipe->lock, flags);
393 omap4iss_pipeline_set_stream(pipe,
394 ISS_PIPELINE_STREAM_SINGLESHOT);
398 static const struct vb2_ops iss_video_vb2ops = {
399 .queue_setup = iss_video_queue_setup,
400 .buf_prepare = iss_video_buf_prepare,
401 .buf_queue = iss_video_buf_queue,
402 .buf_cleanup = iss_video_buf_cleanup,
406 * omap4iss_video_buffer_next - Complete the current buffer and return the next
407 * @video: ISS video object
409 * Remove the current video buffer from the DMA queue and fill its timestamp,
410 * field count and state fields before waking up its completion handler.
412 * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
413 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
415 * The DMA queue is expected to contain at least one buffer.
417 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
420 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
422 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
423 enum iss_pipeline_state state;
424 struct iss_buffer *buf;
427 spin_lock_irqsave(&video->qlock, flags);
428 if (WARN_ON(list_empty(&video->dmaqueue))) {
429 spin_unlock_irqrestore(&video->qlock, flags);
433 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
435 list_del(&buf->list);
436 spin_unlock_irqrestore(&video->qlock, flags);
438 v4l2_get_timestamp(&buf->vb.timestamp);
440 /* Do frame number propagation only if this is the output video node.
441 * Frame number either comes from the CSI receivers or it gets
442 * incremented here if H3A is not active.
443 * Note: There is no guarantee that the output buffer will finish
444 * first, so the input number might lag behind by 1 in some cases.
446 if (video == pipe->output && !pipe->do_propagation)
448 atomic_inc_return(&pipe->frame_number);
450 buf->vb.sequence = atomic_read(&pipe->frame_number);
452 vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
453 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
456 spin_lock_irqsave(&video->qlock, flags);
457 if (list_empty(&video->dmaqueue)) {
458 spin_unlock_irqrestore(&video->qlock, flags);
459 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
460 state = ISS_PIPELINE_QUEUE_OUTPUT
461 | ISS_PIPELINE_STREAM;
463 state = ISS_PIPELINE_QUEUE_INPUT
464 | ISS_PIPELINE_STREAM;
466 spin_lock_irqsave(&pipe->lock, flags);
467 pipe->state &= ~state;
468 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
469 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
470 spin_unlock_irqrestore(&pipe->lock, flags);
474 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
475 spin_lock(&pipe->lock);
476 pipe->state &= ~ISS_PIPELINE_STREAM;
477 spin_unlock(&pipe->lock);
480 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
482 spin_unlock_irqrestore(&video->qlock, flags);
483 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
488 * omap4iss_video_cancel_stream - Cancel stream on a video node
489 * @video: ISS video object
491 * Cancelling a stream mark all buffers on the video node as erroneous and makes
492 * sure no new buffer can be queued.
494 void omap4iss_video_cancel_stream(struct iss_video *video)
498 spin_lock_irqsave(&video->qlock, flags);
500 while (!list_empty(&video->dmaqueue)) {
501 struct iss_buffer *buf;
503 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
505 list_del(&buf->list);
506 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
509 vb2_queue_error(video->queue);
512 spin_unlock_irqrestore(&video->qlock, flags);
515 /* -----------------------------------------------------------------------------
520 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
522 struct iss_video *video = video_drvdata(file);
524 strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
525 strlcpy(cap->card, video->video.name, sizeof(cap->card));
526 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
528 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
529 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
531 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
533 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
534 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
540 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
542 struct iss_video *video = video_drvdata(file);
543 struct v4l2_mbus_framefmt format;
544 unsigned int index = f->index;
548 if (f->type != video->type)
551 ret = __iss_video_get_format(video, &format);
555 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
556 const struct iss_format_info *info = &formats[i];
558 if (format.code != info->code)
562 f->pixelformat = info->pixelformat;
563 strlcpy(f->description, info->description,
564 sizeof(f->description));
575 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
577 struct iss_video_fh *vfh = to_iss_video_fh(fh);
578 struct iss_video *video = video_drvdata(file);
580 if (format->type != video->type)
583 mutex_lock(&video->mutex);
584 *format = vfh->format;
585 mutex_unlock(&video->mutex);
591 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
593 struct iss_video_fh *vfh = to_iss_video_fh(fh);
594 struct iss_video *video = video_drvdata(file);
595 struct v4l2_mbus_framefmt fmt;
597 if (format->type != video->type)
600 mutex_lock(&video->mutex);
602 /* Fill the bytesperline and sizeimage fields by converting to media bus
603 * format and back to pixel format.
605 iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
606 iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
608 vfh->format = *format;
610 mutex_unlock(&video->mutex);
615 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
617 struct iss_video *video = video_drvdata(file);
618 struct v4l2_subdev_format fmt;
619 struct v4l2_subdev *subdev;
623 if (format->type != video->type)
626 subdev = iss_video_remote_subdev(video, &pad);
630 iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
633 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
634 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
638 iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
643 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
645 struct iss_video_fh *vfh = to_iss_video_fh(fh);
646 struct iss_video *video = video_drvdata(file);
648 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
649 video->type != a->type)
652 memset(a, 0, sizeof(*a));
653 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
654 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
655 a->parm.output.timeperframe = vfh->timeperframe;
661 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
663 struct iss_video_fh *vfh = to_iss_video_fh(fh);
664 struct iss_video *video = video_drvdata(file);
666 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
667 video->type != a->type)
670 if (a->parm.output.timeperframe.denominator == 0)
671 a->parm.output.timeperframe.denominator = 1;
673 vfh->timeperframe = a->parm.output.timeperframe;
679 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
681 struct iss_video_fh *vfh = to_iss_video_fh(fh);
683 return vb2_reqbufs(&vfh->queue, rb);
687 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
689 struct iss_video_fh *vfh = to_iss_video_fh(fh);
691 return vb2_querybuf(&vfh->queue, b);
695 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
697 struct iss_video_fh *vfh = to_iss_video_fh(fh);
699 return vb2_qbuf(&vfh->queue, b);
703 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
705 struct iss_video_fh *vfh = to_iss_video_fh(fh);
707 return vb2_expbuf(&vfh->queue, e);
711 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
713 struct iss_video_fh *vfh = to_iss_video_fh(fh);
715 return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
721 * Every ISS pipeline has a single input and a single output. The input can be
722 * either a sensor or a video node. The output is always a video node.
724 * As every pipeline has an output video node, the ISS video objects at the
725 * pipeline output stores the pipeline state. It tracks the streaming state of
726 * both the input and output, as well as the availability of buffers.
728 * In sensor-to-memory mode, frames are always available at the pipeline input.
729 * Starting the sensor usually requires I2C transfers and must be done in
730 * interruptible context. The pipeline is started and stopped synchronously
731 * to the stream on/off commands. All modules in the pipeline will get their
732 * subdev set stream handler called. The module at the end of the pipeline must
733 * delay starting the hardware until buffers are available at its output.
735 * In memory-to-memory mode, starting/stopping the stream requires
736 * synchronization between the input and output. ISS modules can't be stopped
737 * in the middle of a frame, and at least some of the modules seem to become
738 * busy as soon as they're started, even if they don't receive a frame start
739 * event. For that reason frames need to be processed in single-shot mode. The
740 * driver needs to wait until a frame is completely processed and written to
741 * memory before restarting the pipeline for the next frame. Pipelined
742 * processing might be possible but requires more testing.
744 * Stream start must be delayed until buffers are available at both the input
745 * and output. The pipeline must be started in the videobuf queue callback with
746 * the buffers queue spinlock held. The modules subdev set stream operation must
750 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
752 struct iss_video_fh *vfh = to_iss_video_fh(fh);
753 struct iss_video *video = video_drvdata(file);
754 struct media_entity_graph graph;
755 struct media_entity *entity;
756 enum iss_pipeline_state state;
757 struct iss_pipeline *pipe;
758 struct iss_video *far_end;
762 if (type != video->type)
765 mutex_lock(&video->stream_lock);
767 /* Start streaming on the pipeline. No link touching an entity in the
768 * pipeline can be activated or deactivated once streaming is started.
770 pipe = video->video.entity.pipe
771 ? to_iss_pipeline(&video->video.entity) : &video->pipe;
772 pipe->external = NULL;
773 pipe->external_rate = 0;
774 pipe->external_bpp = 0;
777 if (video->iss->pdata->set_constraints)
778 video->iss->pdata->set_constraints(video->iss, true);
780 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
782 goto err_media_entity_pipeline_start;
784 entity = &video->video.entity;
785 media_entity_graph_walk_start(&graph, entity);
786 while ((entity = media_entity_graph_walk_next(&graph)))
787 pipe->entities |= 1 << entity->id;
789 /* Verify that the currently configured format matches the output of
790 * the connected subdev.
792 ret = iss_video_check_format(video, vfh);
794 goto err_iss_video_check_format;
796 video->bpl_padding = ret;
797 video->bpl_value = vfh->format.fmt.pix.bytesperline;
799 /* Find the ISS video node connected at the far end of the pipeline and
800 * update the pipeline.
802 far_end = iss_video_far_end(video);
804 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
805 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
806 pipe->input = far_end;
807 pipe->output = video;
809 if (far_end == NULL) {
811 goto err_iss_video_check_format;
814 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
816 pipe->output = far_end;
819 spin_lock_irqsave(&pipe->lock, flags);
820 pipe->state &= ~ISS_PIPELINE_STREAM;
821 pipe->state |= state;
822 spin_unlock_irqrestore(&pipe->lock, flags);
824 /* Set the maximum time per frame as the value requested by userspace.
825 * This is a soft limit that can be overridden if the hardware doesn't
826 * support the request limit.
828 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
829 pipe->max_timeperframe = vfh->timeperframe;
831 video->queue = &vfh->queue;
832 INIT_LIST_HEAD(&video->dmaqueue);
833 video->error = false;
834 atomic_set(&pipe->frame_number, -1);
836 ret = vb2_streamon(&vfh->queue, type);
838 goto err_iss_video_check_format;
840 /* In sensor-to-memory mode, the stream can be started synchronously
841 * to the stream on command. In memory-to-memory mode, it will be
842 * started when buffers are queued on both the input and output.
844 if (pipe->input == NULL) {
847 ret = omap4iss_pipeline_set_stream(pipe,
848 ISS_PIPELINE_STREAM_CONTINUOUS);
850 goto err_omap4iss_set_stream;
851 spin_lock_irqsave(&video->qlock, flags);
852 if (list_empty(&video->dmaqueue))
853 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
854 spin_unlock_irqrestore(&video->qlock, flags);
857 mutex_unlock(&video->stream_lock);
860 err_omap4iss_set_stream:
861 vb2_streamoff(&vfh->queue, type);
862 err_iss_video_check_format:
863 media_entity_pipeline_stop(&video->video.entity);
864 err_media_entity_pipeline_start:
865 if (video->iss->pdata->set_constraints)
866 video->iss->pdata->set_constraints(video->iss, false);
869 mutex_unlock(&video->stream_lock);
874 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
876 struct iss_video_fh *vfh = to_iss_video_fh(fh);
877 struct iss_video *video = video_drvdata(file);
878 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
879 enum iss_pipeline_state state;
882 if (type != video->type)
885 mutex_lock(&video->stream_lock);
887 if (!vb2_is_streaming(&vfh->queue))
890 /* Update the pipeline state. */
891 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
892 state = ISS_PIPELINE_STREAM_OUTPUT
893 | ISS_PIPELINE_QUEUE_OUTPUT;
895 state = ISS_PIPELINE_STREAM_INPUT
896 | ISS_PIPELINE_QUEUE_INPUT;
898 spin_lock_irqsave(&pipe->lock, flags);
899 pipe->state &= ~state;
900 spin_unlock_irqrestore(&pipe->lock, flags);
902 /* Stop the stream. */
903 omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
904 vb2_streamoff(&vfh->queue, type);
907 if (video->iss->pdata->set_constraints)
908 video->iss->pdata->set_constraints(video->iss, false);
909 media_entity_pipeline_stop(&video->video.entity);
912 mutex_unlock(&video->stream_lock);
917 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
919 if (input->index > 0)
922 strlcpy(input->name, "camera", sizeof(input->name));
923 input->type = V4L2_INPUT_TYPE_CAMERA;
929 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
937 iss_video_s_input(struct file *file, void *fh, unsigned int input)
939 return input == 0 ? 0 : -EINVAL;
942 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
943 .vidioc_querycap = iss_video_querycap,
944 .vidioc_enum_fmt_vid_cap = iss_video_enum_format,
945 .vidioc_g_fmt_vid_cap = iss_video_get_format,
946 .vidioc_s_fmt_vid_cap = iss_video_set_format,
947 .vidioc_try_fmt_vid_cap = iss_video_try_format,
948 .vidioc_g_fmt_vid_out = iss_video_get_format,
949 .vidioc_s_fmt_vid_out = iss_video_set_format,
950 .vidioc_try_fmt_vid_out = iss_video_try_format,
951 .vidioc_g_parm = iss_video_get_param,
952 .vidioc_s_parm = iss_video_set_param,
953 .vidioc_reqbufs = iss_video_reqbufs,
954 .vidioc_querybuf = iss_video_querybuf,
955 .vidioc_qbuf = iss_video_qbuf,
956 .vidioc_expbuf = iss_video_expbuf,
957 .vidioc_dqbuf = iss_video_dqbuf,
958 .vidioc_streamon = iss_video_streamon,
959 .vidioc_streamoff = iss_video_streamoff,
960 .vidioc_enum_input = iss_video_enum_input,
961 .vidioc_g_input = iss_video_g_input,
962 .vidioc_s_input = iss_video_s_input,
965 /* -----------------------------------------------------------------------------
966 * V4L2 file operations
969 static int iss_video_open(struct file *file)
971 struct iss_video *video = video_drvdata(file);
972 struct iss_video_fh *handle;
976 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
980 v4l2_fh_init(&handle->vfh, &video->video);
981 v4l2_fh_add(&handle->vfh);
983 /* If this is the first user, initialise the pipeline. */
984 if (omap4iss_get(video->iss) == NULL) {
989 ret = omap4iss_pipeline_pm_use(&video->video.entity, 1);
991 omap4iss_put(video->iss);
995 video->alloc_ctx = vb2_dma_contig_init_ctx(video->iss->dev);
996 if (IS_ERR(video->alloc_ctx)) {
997 ret = PTR_ERR(video->alloc_ctx);
998 omap4iss_put(video->iss);
1004 q->type = video->type;
1005 q->io_modes = VB2_MMAP | VB2_DMABUF;
1006 q->drv_priv = handle;
1007 q->ops = &iss_video_vb2ops;
1008 q->mem_ops = &vb2_dma_contig_memops;
1009 q->buf_struct_size = sizeof(struct iss_buffer);
1010 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1012 ret = vb2_queue_init(q);
1014 omap4iss_put(video->iss);
1018 memset(&handle->format, 0, sizeof(handle->format));
1019 handle->format.type = video->type;
1020 handle->timeperframe.denominator = 1;
1022 handle->video = video;
1023 file->private_data = &handle->vfh;
1027 v4l2_fh_del(&handle->vfh);
1034 static int iss_video_release(struct file *file)
1036 struct iss_video *video = video_drvdata(file);
1037 struct v4l2_fh *vfh = file->private_data;
1038 struct iss_video_fh *handle = to_iss_video_fh(vfh);
1040 /* Disable streaming and free the buffers queue resources. */
1041 iss_video_streamoff(file, vfh, video->type);
1043 omap4iss_pipeline_pm_use(&video->video.entity, 0);
1045 /* Release the videobuf2 queue */
1046 vb2_queue_release(&handle->queue);
1048 /* Release the file handle. */
1051 file->private_data = NULL;
1053 omap4iss_put(video->iss);
1058 static unsigned int iss_video_poll(struct file *file, poll_table *wait)
1060 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1062 return vb2_poll(&vfh->queue, file, wait);
1065 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1067 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1069 return vb2_mmap(&vfh->queue, vma);
1072 static struct v4l2_file_operations iss_video_fops = {
1073 .owner = THIS_MODULE,
1074 .unlocked_ioctl = video_ioctl2,
1075 .open = iss_video_open,
1076 .release = iss_video_release,
1077 .poll = iss_video_poll,
1078 .mmap = iss_video_mmap,
1081 /* -----------------------------------------------------------------------------
1085 static const struct iss_video_operations iss_video_dummy_ops = {
1088 int omap4iss_video_init(struct iss_video *video, const char *name)
1090 const char *direction;
1093 switch (video->type) {
1094 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1095 direction = "output";
1096 video->pad.flags = MEDIA_PAD_FL_SINK;
1098 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1099 direction = "input";
1100 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1107 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1111 spin_lock_init(&video->qlock);
1112 mutex_init(&video->mutex);
1113 atomic_set(&video->active, 0);
1115 spin_lock_init(&video->pipe.lock);
1116 mutex_init(&video->stream_lock);
1118 /* Initialize the video device. */
1119 if (video->ops == NULL)
1120 video->ops = &iss_video_dummy_ops;
1122 video->video.fops = &iss_video_fops;
1123 snprintf(video->video.name, sizeof(video->video.name),
1124 "OMAP4 ISS %s %s", name, direction);
1125 video->video.vfl_type = VFL_TYPE_GRABBER;
1126 video->video.release = video_device_release_empty;
1127 video->video.ioctl_ops = &iss_video_ioctl_ops;
1128 video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1130 video_set_drvdata(&video->video, video);
1135 void omap4iss_video_cleanup(struct iss_video *video)
1137 media_entity_cleanup(&video->video.entity);
1138 mutex_destroy(&video->stream_lock);
1139 mutex_destroy(&video->mutex);
1142 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1146 video->video.v4l2_dev = vdev;
1148 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1150 dev_err(video->iss->dev,
1151 "could not register video device (%d)\n", ret);
1156 void omap4iss_video_unregister(struct iss_video *video)
1158 video_unregister_device(&video->video);