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;
132 * Skip the last format in the loop so that it will be selected if no
135 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
136 if (formats[i].code == mbus->code)
140 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
143 * Clamp the requested bytes per line value. If the maximum bytes per
144 * line value is zero, the module doesn't support user configurable line
145 * sizes. Override the requested value with the minimum in that case.
148 bpl = clamp(bpl, min_bpl, video->bpl_max);
152 if (!video->bpl_zero_padding || bpl != min_bpl)
153 bpl = ALIGN(bpl, video->bpl_alignment);
155 pix->pixelformat = formats[i].pixelformat;
156 pix->bytesperline = bpl;
157 pix->sizeimage = pix->bytesperline * pix->height;
158 pix->colorspace = mbus->colorspace;
159 pix->field = mbus->field;
161 /* FIXME: Special case for NV12! We should make this nicer... */
162 if (pix->pixelformat == V4L2_PIX_FMT_NV12)
163 pix->sizeimage += (pix->bytesperline * pix->height) / 2;
165 return bpl - min_bpl;
168 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
169 struct v4l2_mbus_framefmt *mbus)
173 memset(mbus, 0, sizeof(*mbus));
174 mbus->width = pix->width;
175 mbus->height = pix->height;
178 * Skip the last format in the loop so that it will be selected if no
181 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
182 if (formats[i].pixelformat == pix->pixelformat)
186 mbus->code = formats[i].code;
187 mbus->colorspace = pix->colorspace;
188 mbus->field = pix->field;
191 static struct v4l2_subdev *
192 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
194 struct media_pad *remote;
196 remote = media_entity_remote_pad(&video->pad);
198 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
202 *pad = remote->index;
204 return media_entity_to_v4l2_subdev(remote->entity);
207 /* Return a pointer to the ISS video instance at the far end of the pipeline. */
208 static struct iss_video *
209 iss_video_far_end(struct iss_video *video)
211 struct media_graph graph;
212 struct media_entity *entity = &video->video.entity;
213 struct media_device *mdev = entity->graph_obj.mdev;
214 struct iss_video *far_end = NULL;
216 mutex_lock(&mdev->graph_mutex);
218 if (media_graph_walk_init(&graph, mdev)) {
219 mutex_unlock(&mdev->graph_mutex);
223 media_graph_walk_start(&graph, entity);
225 while ((entity = media_graph_walk_next(&graph))) {
226 if (entity == &video->video.entity)
229 if (!is_media_entity_v4l2_video_device(entity))
232 far_end = to_iss_video(media_entity_to_video_device(entity));
233 if (far_end->type != video->type)
239 mutex_unlock(&mdev->graph_mutex);
241 media_graph_walk_cleanup(&graph);
247 __iss_video_get_format(struct iss_video *video,
248 struct v4l2_mbus_framefmt *format)
250 struct v4l2_subdev_format fmt;
251 struct v4l2_subdev *subdev;
255 subdev = iss_video_remote_subdev(video, &pad);
259 memset(&fmt, 0, sizeof(fmt));
261 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
263 mutex_lock(&video->mutex);
264 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
265 mutex_unlock(&video->mutex);
270 *format = fmt.format;
275 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
277 struct v4l2_mbus_framefmt format;
278 struct v4l2_pix_format pixfmt;
281 ret = __iss_video_get_format(video, &format);
285 pixfmt.bytesperline = 0;
286 ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
288 if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat ||
289 vfh->format.fmt.pix.height != pixfmt.height ||
290 vfh->format.fmt.pix.width != pixfmt.width ||
291 vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline ||
292 vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage)
298 /* -----------------------------------------------------------------------------
299 * Video queue operations
302 static int iss_video_queue_setup(struct vb2_queue *vq,
303 unsigned int *count, unsigned int *num_planes,
304 unsigned int sizes[],
305 struct device *alloc_devs[])
307 struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
308 struct iss_video *video = vfh->video;
310 /* Revisit multi-planar support for NV12 */
313 sizes[0] = vfh->format.fmt.pix.sizeimage;
317 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
322 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
324 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
325 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
327 if (buffer->iss_addr)
328 buffer->iss_addr = 0;
331 static int iss_video_buf_prepare(struct vb2_buffer *vb)
333 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
334 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
335 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
336 struct iss_video *video = vfh->video;
337 unsigned long size = vfh->format.fmt.pix.sizeimage;
340 if (vb2_plane_size(vb, 0) < size)
343 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
344 if (!IS_ALIGNED(addr, 32)) {
345 dev_dbg(video->iss->dev,
346 "Buffer address must be aligned to 32 bytes boundary.\n");
350 vb2_set_plane_payload(vb, 0, size);
351 buffer->iss_addr = addr;
355 static void iss_video_buf_queue(struct vb2_buffer *vb)
357 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
358 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
359 struct iss_video *video = vfh->video;
360 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
361 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
365 spin_lock_irqsave(&video->qlock, flags);
368 * Mark the buffer is faulty and give it back to the queue immediately
369 * if the video node has registered an error. vb2 will perform the same
370 * check when preparing the buffer, but that is inherently racy, so we
371 * need to handle the race condition with an authoritative check here.
373 if (unlikely(video->error)) {
374 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
375 spin_unlock_irqrestore(&video->qlock, flags);
379 empty = list_empty(&video->dmaqueue);
380 list_add_tail(&buffer->list, &video->dmaqueue);
382 spin_unlock_irqrestore(&video->qlock, flags);
385 enum iss_pipeline_state state;
388 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
389 state = ISS_PIPELINE_QUEUE_OUTPUT;
391 state = ISS_PIPELINE_QUEUE_INPUT;
393 spin_lock_irqsave(&pipe->lock, flags);
394 pipe->state |= state;
395 video->ops->queue(video, buffer);
396 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
398 start = iss_pipeline_ready(pipe);
400 pipe->state |= ISS_PIPELINE_STREAM;
401 spin_unlock_irqrestore(&pipe->lock, flags);
404 omap4iss_pipeline_set_stream(pipe,
405 ISS_PIPELINE_STREAM_SINGLESHOT);
409 static const struct vb2_ops iss_video_vb2ops = {
410 .queue_setup = iss_video_queue_setup,
411 .buf_prepare = iss_video_buf_prepare,
412 .buf_queue = iss_video_buf_queue,
413 .buf_cleanup = iss_video_buf_cleanup,
417 * omap4iss_video_buffer_next - Complete the current buffer and return the next
418 * @video: ISS video object
420 * Remove the current video buffer from the DMA queue and fill its timestamp,
421 * field count and state fields before waking up its completion handler.
423 * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
424 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
426 * The DMA queue is expected to contain at least one buffer.
428 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
431 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
433 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
434 enum iss_pipeline_state state;
435 struct iss_buffer *buf;
438 spin_lock_irqsave(&video->qlock, flags);
439 if (WARN_ON(list_empty(&video->dmaqueue))) {
440 spin_unlock_irqrestore(&video->qlock, flags);
444 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
446 list_del(&buf->list);
447 spin_unlock_irqrestore(&video->qlock, flags);
449 buf->vb.vb2_buf.timestamp = ktime_get_ns();
452 * Do frame number propagation only if this is the output video node.
453 * Frame number either comes from the CSI receivers or it gets
454 * incremented here if H3A is not active.
455 * Note: There is no guarantee that the output buffer will finish
456 * first, so the input number might lag behind by 1 in some cases.
458 if (video == pipe->output && !pipe->do_propagation)
460 atomic_inc_return(&pipe->frame_number);
462 buf->vb.sequence = atomic_read(&pipe->frame_number);
464 vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
465 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
468 spin_lock_irqsave(&video->qlock, flags);
469 if (list_empty(&video->dmaqueue)) {
470 spin_unlock_irqrestore(&video->qlock, flags);
471 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
472 state = ISS_PIPELINE_QUEUE_OUTPUT
473 | ISS_PIPELINE_STREAM;
475 state = ISS_PIPELINE_QUEUE_INPUT
476 | ISS_PIPELINE_STREAM;
478 spin_lock_irqsave(&pipe->lock, flags);
479 pipe->state &= ~state;
480 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
481 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
482 spin_unlock_irqrestore(&pipe->lock, flags);
486 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) {
487 spin_lock(&pipe->lock);
488 pipe->state &= ~ISS_PIPELINE_STREAM;
489 spin_unlock(&pipe->lock);
492 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
494 spin_unlock_irqrestore(&video->qlock, flags);
495 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
500 * omap4iss_video_cancel_stream - Cancel stream on a video node
501 * @video: ISS video object
503 * Cancelling a stream mark all buffers on the video node as erroneous and makes
504 * sure no new buffer can be queued.
506 void omap4iss_video_cancel_stream(struct iss_video *video)
510 spin_lock_irqsave(&video->qlock, flags);
512 while (!list_empty(&video->dmaqueue)) {
513 struct iss_buffer *buf;
515 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
517 list_del(&buf->list);
518 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
521 vb2_queue_error(video->queue);
524 spin_unlock_irqrestore(&video->qlock, flags);
527 /* -----------------------------------------------------------------------------
532 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
534 struct iss_video *video = video_drvdata(file);
536 strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
537 strlcpy(cap->card, video->video.name, sizeof(cap->card));
538 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
540 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
541 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
543 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
545 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
546 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
552 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
554 struct iss_video *video = video_drvdata(file);
555 struct v4l2_mbus_framefmt format;
556 unsigned int index = f->index;
560 if (f->type != video->type)
563 ret = __iss_video_get_format(video, &format);
567 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
568 const struct iss_format_info *info = &formats[i];
570 if (format.code != info->code)
574 f->pixelformat = info->pixelformat;
575 strlcpy(f->description, info->description,
576 sizeof(f->description));
587 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
589 struct iss_video_fh *vfh = to_iss_video_fh(fh);
590 struct iss_video *video = video_drvdata(file);
592 if (format->type != video->type)
595 mutex_lock(&video->mutex);
596 *format = vfh->format;
597 mutex_unlock(&video->mutex);
603 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
605 struct iss_video_fh *vfh = to_iss_video_fh(fh);
606 struct iss_video *video = video_drvdata(file);
607 struct v4l2_mbus_framefmt fmt;
609 if (format->type != video->type)
612 mutex_lock(&video->mutex);
615 * Fill the bytesperline and sizeimage fields by converting to media bus
616 * format and back to pixel format.
618 iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
619 iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
621 vfh->format = *format;
623 mutex_unlock(&video->mutex);
628 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
630 struct iss_video *video = video_drvdata(file);
631 struct v4l2_subdev_format fmt;
632 struct v4l2_subdev *subdev;
636 if (format->type != video->type)
639 subdev = iss_video_remote_subdev(video, &pad);
643 iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
646 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
647 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
651 iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
656 iss_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
658 struct iss_video *video = video_drvdata(file);
659 struct v4l2_subdev_format format;
660 struct v4l2_subdev *subdev;
661 struct v4l2_subdev_selection sdsel = {
662 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
663 .target = sel->target,
668 switch (sel->target) {
669 case V4L2_SEL_TGT_CROP:
670 case V4L2_SEL_TGT_CROP_BOUNDS:
671 case V4L2_SEL_TGT_CROP_DEFAULT:
672 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
675 case V4L2_SEL_TGT_COMPOSE:
676 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
677 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
678 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
684 subdev = iss_video_remote_subdev(video, &pad);
689 * Try the get selection operation first and fallback to get format if
693 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
696 if (ret != -ENOIOCTLCMD)
700 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
701 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
703 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
707 sel->r.width = format.format.width;
708 sel->r.height = format.format.height;
714 iss_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
716 struct iss_video *video = video_drvdata(file);
717 struct v4l2_subdev *subdev;
718 struct v4l2_subdev_selection sdsel = {
719 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
720 .target = sel->target,
727 switch (sel->target) {
728 case V4L2_SEL_TGT_CROP:
729 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
732 case V4L2_SEL_TGT_COMPOSE:
733 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
739 subdev = iss_video_remote_subdev(video, &pad);
744 mutex_lock(&video->mutex);
745 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
746 mutex_unlock(&video->mutex);
750 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
754 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
756 struct iss_video_fh *vfh = to_iss_video_fh(fh);
757 struct iss_video *video = video_drvdata(file);
759 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
760 video->type != a->type)
763 memset(a, 0, sizeof(*a));
764 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
765 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
766 a->parm.output.timeperframe = vfh->timeperframe;
772 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
774 struct iss_video_fh *vfh = to_iss_video_fh(fh);
775 struct iss_video *video = video_drvdata(file);
777 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
778 video->type != a->type)
781 if (a->parm.output.timeperframe.denominator == 0)
782 a->parm.output.timeperframe.denominator = 1;
784 vfh->timeperframe = a->parm.output.timeperframe;
790 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
792 struct iss_video_fh *vfh = to_iss_video_fh(fh);
794 return vb2_reqbufs(&vfh->queue, rb);
798 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
800 struct iss_video_fh *vfh = to_iss_video_fh(fh);
802 return vb2_querybuf(&vfh->queue, b);
806 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
808 struct iss_video_fh *vfh = to_iss_video_fh(fh);
810 return vb2_qbuf(&vfh->queue, b);
814 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
816 struct iss_video_fh *vfh = to_iss_video_fh(fh);
818 return vb2_expbuf(&vfh->queue, e);
822 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
824 struct iss_video_fh *vfh = to_iss_video_fh(fh);
826 return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
832 * Every ISS pipeline has a single input and a single output. The input can be
833 * either a sensor or a video node. The output is always a video node.
835 * As every pipeline has an output video node, the ISS video objects at the
836 * pipeline output stores the pipeline state. It tracks the streaming state of
837 * both the input and output, as well as the availability of buffers.
839 * In sensor-to-memory mode, frames are always available at the pipeline input.
840 * Starting the sensor usually requires I2C transfers and must be done in
841 * interruptible context. The pipeline is started and stopped synchronously
842 * to the stream on/off commands. All modules in the pipeline will get their
843 * subdev set stream handler called. The module at the end of the pipeline must
844 * delay starting the hardware until buffers are available at its output.
846 * In memory-to-memory mode, starting/stopping the stream requires
847 * synchronization between the input and output. ISS modules can't be stopped
848 * in the middle of a frame, and at least some of the modules seem to become
849 * busy as soon as they're started, even if they don't receive a frame start
850 * event. For that reason frames need to be processed in single-shot mode. The
851 * driver needs to wait until a frame is completely processed and written to
852 * memory before restarting the pipeline for the next frame. Pipelined
853 * processing might be possible but requires more testing.
855 * Stream start must be delayed until buffers are available at both the input
856 * and output. The pipeline must be started in the videobuf queue callback with
857 * the buffers queue spinlock held. The modules subdev set stream operation must
861 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
863 struct iss_video_fh *vfh = to_iss_video_fh(fh);
864 struct iss_video *video = video_drvdata(file);
865 struct media_graph graph;
866 struct media_entity *entity = &video->video.entity;
867 enum iss_pipeline_state state;
868 struct iss_pipeline *pipe;
869 struct iss_video *far_end;
873 if (type != video->type)
876 mutex_lock(&video->stream_lock);
879 * Start streaming on the pipeline. No link touching an entity in the
880 * pipeline can be activated or deactivated once streaming is started.
883 ? to_iss_pipeline(entity) : &video->pipe;
884 pipe->external = NULL;
885 pipe->external_rate = 0;
886 pipe->external_bpp = 0;
888 ret = media_entity_enum_init(&pipe->ent_enum, entity->graph_obj.mdev);
890 goto err_graph_walk_init;
892 ret = media_graph_walk_init(&graph, entity->graph_obj.mdev);
894 goto err_graph_walk_init;
896 if (video->iss->pdata->set_constraints)
897 video->iss->pdata->set_constraints(video->iss, true);
899 ret = media_pipeline_start(entity, &pipe->pipe);
901 goto err_media_pipeline_start;
903 media_graph_walk_start(&graph, entity);
904 while ((entity = media_graph_walk_next(&graph)))
905 media_entity_enum_set(&pipe->ent_enum, entity);
908 * Verify that the currently configured format matches the output of
909 * the connected subdev.
911 ret = iss_video_check_format(video, vfh);
913 goto err_iss_video_check_format;
915 video->bpl_padding = ret;
916 video->bpl_value = vfh->format.fmt.pix.bytesperline;
919 * Find the ISS video node connected at the far end of the pipeline and
920 * update the pipeline.
922 far_end = iss_video_far_end(video);
924 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
925 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
926 pipe->input = far_end;
927 pipe->output = video;
931 goto err_iss_video_check_format;
934 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
936 pipe->output = far_end;
939 spin_lock_irqsave(&pipe->lock, flags);
940 pipe->state &= ~ISS_PIPELINE_STREAM;
941 pipe->state |= state;
942 spin_unlock_irqrestore(&pipe->lock, flags);
945 * Set the maximum time per frame as the value requested by userspace.
946 * This is a soft limit that can be overridden if the hardware doesn't
947 * support the request limit.
949 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
950 pipe->max_timeperframe = vfh->timeperframe;
952 video->queue = &vfh->queue;
953 INIT_LIST_HEAD(&video->dmaqueue);
954 video->error = false;
955 atomic_set(&pipe->frame_number, -1);
957 ret = vb2_streamon(&vfh->queue, type);
959 goto err_iss_video_check_format;
962 * In sensor-to-memory mode, the stream can be started synchronously
963 * to the stream on command. In memory-to-memory mode, it will be
964 * started when buffers are queued on both the input and output.
969 ret = omap4iss_pipeline_set_stream(pipe,
970 ISS_PIPELINE_STREAM_CONTINUOUS);
972 goto err_omap4iss_set_stream;
973 spin_lock_irqsave(&video->qlock, flags);
974 if (list_empty(&video->dmaqueue))
975 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
976 spin_unlock_irqrestore(&video->qlock, flags);
979 media_graph_walk_cleanup(&graph);
981 mutex_unlock(&video->stream_lock);
985 err_omap4iss_set_stream:
986 vb2_streamoff(&vfh->queue, type);
987 err_iss_video_check_format:
988 media_pipeline_stop(&video->video.entity);
989 err_media_pipeline_start:
990 if (video->iss->pdata->set_constraints)
991 video->iss->pdata->set_constraints(video->iss, false);
994 media_graph_walk_cleanup(&graph);
997 media_entity_enum_cleanup(&pipe->ent_enum);
999 mutex_unlock(&video->stream_lock);
1005 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1007 struct iss_video_fh *vfh = to_iss_video_fh(fh);
1008 struct iss_video *video = video_drvdata(file);
1009 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
1010 enum iss_pipeline_state state;
1011 unsigned long flags;
1013 if (type != video->type)
1016 mutex_lock(&video->stream_lock);
1018 if (!vb2_is_streaming(&vfh->queue))
1021 /* Update the pipeline state. */
1022 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1023 state = ISS_PIPELINE_STREAM_OUTPUT
1024 | ISS_PIPELINE_QUEUE_OUTPUT;
1026 state = ISS_PIPELINE_STREAM_INPUT
1027 | ISS_PIPELINE_QUEUE_INPUT;
1029 spin_lock_irqsave(&pipe->lock, flags);
1030 pipe->state &= ~state;
1031 spin_unlock_irqrestore(&pipe->lock, flags);
1033 /* Stop the stream. */
1034 omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
1035 vb2_streamoff(&vfh->queue, type);
1036 video->queue = NULL;
1038 media_entity_enum_cleanup(&pipe->ent_enum);
1040 if (video->iss->pdata->set_constraints)
1041 video->iss->pdata->set_constraints(video->iss, false);
1042 media_pipeline_stop(&video->video.entity);
1045 mutex_unlock(&video->stream_lock);
1050 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1052 if (input->index > 0)
1055 strlcpy(input->name, "camera", sizeof(input->name));
1056 input->type = V4L2_INPUT_TYPE_CAMERA;
1062 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
1070 iss_video_s_input(struct file *file, void *fh, unsigned int input)
1072 return input == 0 ? 0 : -EINVAL;
1075 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
1076 .vidioc_querycap = iss_video_querycap,
1077 .vidioc_enum_fmt_vid_cap = iss_video_enum_format,
1078 .vidioc_g_fmt_vid_cap = iss_video_get_format,
1079 .vidioc_s_fmt_vid_cap = iss_video_set_format,
1080 .vidioc_try_fmt_vid_cap = iss_video_try_format,
1081 .vidioc_g_fmt_vid_out = iss_video_get_format,
1082 .vidioc_s_fmt_vid_out = iss_video_set_format,
1083 .vidioc_try_fmt_vid_out = iss_video_try_format,
1084 .vidioc_g_selection = iss_video_get_selection,
1085 .vidioc_s_selection = iss_video_set_selection,
1086 .vidioc_g_parm = iss_video_get_param,
1087 .vidioc_s_parm = iss_video_set_param,
1088 .vidioc_reqbufs = iss_video_reqbufs,
1089 .vidioc_querybuf = iss_video_querybuf,
1090 .vidioc_qbuf = iss_video_qbuf,
1091 .vidioc_expbuf = iss_video_expbuf,
1092 .vidioc_dqbuf = iss_video_dqbuf,
1093 .vidioc_streamon = iss_video_streamon,
1094 .vidioc_streamoff = iss_video_streamoff,
1095 .vidioc_enum_input = iss_video_enum_input,
1096 .vidioc_g_input = iss_video_g_input,
1097 .vidioc_s_input = iss_video_s_input,
1100 /* -----------------------------------------------------------------------------
1101 * V4L2 file operations
1104 static int iss_video_open(struct file *file)
1106 struct iss_video *video = video_drvdata(file);
1107 struct iss_video_fh *handle;
1108 struct vb2_queue *q;
1111 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1115 v4l2_fh_init(&handle->vfh, &video->video);
1116 v4l2_fh_add(&handle->vfh);
1118 /* If this is the first user, initialise the pipeline. */
1119 if (!omap4iss_get(video->iss)) {
1124 ret = v4l2_pipeline_pm_use(&video->video.entity, 1);
1126 omap4iss_put(video->iss);
1132 q->type = video->type;
1133 q->io_modes = VB2_MMAP | VB2_DMABUF;
1134 q->drv_priv = handle;
1135 q->ops = &iss_video_vb2ops;
1136 q->mem_ops = &vb2_dma_contig_memops;
1137 q->buf_struct_size = sizeof(struct iss_buffer);
1138 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1139 q->dev = video->iss->dev;
1141 ret = vb2_queue_init(q);
1143 omap4iss_put(video->iss);
1147 memset(&handle->format, 0, sizeof(handle->format));
1148 handle->format.type = video->type;
1149 handle->timeperframe.denominator = 1;
1151 handle->video = video;
1152 file->private_data = &handle->vfh;
1156 v4l2_fh_del(&handle->vfh);
1157 v4l2_fh_exit(&handle->vfh);
1164 static int iss_video_release(struct file *file)
1166 struct iss_video *video = video_drvdata(file);
1167 struct v4l2_fh *vfh = file->private_data;
1168 struct iss_video_fh *handle = to_iss_video_fh(vfh);
1170 /* Disable streaming and free the buffers queue resources. */
1171 iss_video_streamoff(file, vfh, video->type);
1173 v4l2_pipeline_pm_use(&video->video.entity, 0);
1175 /* Release the videobuf2 queue */
1176 vb2_queue_release(&handle->queue);
1181 file->private_data = NULL;
1183 omap4iss_put(video->iss);
1188 static __poll_t iss_video_poll(struct file *file, poll_table *wait)
1190 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1192 return vb2_poll(&vfh->queue, file, wait);
1195 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1197 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1199 return vb2_mmap(&vfh->queue, vma);
1202 static const struct v4l2_file_operations iss_video_fops = {
1203 .owner = THIS_MODULE,
1204 .unlocked_ioctl = video_ioctl2,
1205 .open = iss_video_open,
1206 .release = iss_video_release,
1207 .poll = iss_video_poll,
1208 .mmap = iss_video_mmap,
1211 /* -----------------------------------------------------------------------------
1215 static const struct iss_video_operations iss_video_dummy_ops = {
1218 int omap4iss_video_init(struct iss_video *video, const char *name)
1220 const char *direction;
1223 switch (video->type) {
1224 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1225 direction = "output";
1226 video->pad.flags = MEDIA_PAD_FL_SINK;
1228 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1229 direction = "input";
1230 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1237 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1241 spin_lock_init(&video->qlock);
1242 mutex_init(&video->mutex);
1243 atomic_set(&video->active, 0);
1245 spin_lock_init(&video->pipe.lock);
1246 mutex_init(&video->stream_lock);
1248 /* Initialize the video device. */
1250 video->ops = &iss_video_dummy_ops;
1252 video->video.fops = &iss_video_fops;
1253 snprintf(video->video.name, sizeof(video->video.name),
1254 "OMAP4 ISS %s %s", name, direction);
1255 video->video.vfl_type = VFL_TYPE_GRABBER;
1256 video->video.release = video_device_release_empty;
1257 video->video.ioctl_ops = &iss_video_ioctl_ops;
1258 video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1260 video_set_drvdata(&video->video, video);
1265 void omap4iss_video_cleanup(struct iss_video *video)
1267 media_entity_cleanup(&video->video.entity);
1268 mutex_destroy(&video->stream_lock);
1269 mutex_destroy(&video->mutex);
1272 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1276 video->video.v4l2_dev = vdev;
1278 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1280 dev_err(video->iss->dev,
1281 "could not register video device (%d)\n", ret);
1286 void omap4iss_video_unregister(struct iss_video *video)
1288 video_unregister_device(&video->video);