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 <linux/clk.h>
16 #include <linux/pagemap.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/module.h>
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-mc.h>
26 #include <asm/cacheflush.h>
28 #include "iss_video.h"
31 /* -----------------------------------------------------------------------------
35 static struct iss_format_info formats[] = {
36 { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
37 MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
38 V4L2_PIX_FMT_GREY, 8, "Greyscale 8 bpp", },
39 { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
40 MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
41 V4L2_PIX_FMT_Y10, 10, "Greyscale 10 bpp", },
42 { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
43 MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
44 V4L2_PIX_FMT_Y12, 12, "Greyscale 12 bpp", },
45 { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
46 MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
47 V4L2_PIX_FMT_SBGGR8, 8, "BGGR Bayer 8 bpp", },
48 { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
49 MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
50 V4L2_PIX_FMT_SGBRG8, 8, "GBRG Bayer 8 bpp", },
51 { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
52 MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
53 V4L2_PIX_FMT_SGRBG8, 8, "GRBG Bayer 8 bpp", },
54 { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
55 MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
56 V4L2_PIX_FMT_SRGGB8, 8, "RGGB Bayer 8 bpp", },
57 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
58 MEDIA_BUS_FMT_SGRBG10_1X10, 0,
59 V4L2_PIX_FMT_SGRBG10DPCM8, 8, "GRBG Bayer 10 bpp DPCM8", },
60 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
61 MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
62 V4L2_PIX_FMT_SBGGR10, 10, "BGGR Bayer 10 bpp", },
63 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
64 MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
65 V4L2_PIX_FMT_SGBRG10, 10, "GBRG Bayer 10 bpp", },
66 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
67 MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
68 V4L2_PIX_FMT_SGRBG10, 10, "GRBG Bayer 10 bpp", },
69 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
70 MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
71 V4L2_PIX_FMT_SRGGB10, 10, "RGGB Bayer 10 bpp", },
72 { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
73 MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
74 V4L2_PIX_FMT_SBGGR12, 12, "BGGR Bayer 12 bpp", },
75 { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
76 MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
77 V4L2_PIX_FMT_SGBRG12, 12, "GBRG Bayer 12 bpp", },
78 { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
79 MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
80 V4L2_PIX_FMT_SGRBG12, 12, "GRBG Bayer 12 bpp", },
81 { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
82 MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
83 V4L2_PIX_FMT_SRGGB12, 12, "RGGB Bayer 12 bpp", },
84 { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
85 MEDIA_BUS_FMT_UYVY8_1X16, 0,
86 V4L2_PIX_FMT_UYVY, 16, "YUV 4:2:2 (UYVY)", },
87 { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
88 MEDIA_BUS_FMT_YUYV8_1X16, 0,
89 V4L2_PIX_FMT_YUYV, 16, "YUV 4:2:2 (YUYV)", },
90 { MEDIA_BUS_FMT_YUYV8_1_5X8, MEDIA_BUS_FMT_YUYV8_1_5X8,
91 MEDIA_BUS_FMT_YUYV8_1_5X8, 0,
92 V4L2_PIX_FMT_NV12, 8, "YUV 4:2:0 (NV12)", },
95 const struct iss_format_info *
96 omap4iss_video_format_info(u32 code)
100 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
101 if (formats[i].code == code)
109 * iss_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
110 * @video: ISS video instance
111 * @mbus: v4l2_mbus_framefmt format (input)
112 * @pix: v4l2_pix_format format (output)
114 * Fill the output pix structure with information from the input mbus format.
115 * The bytesperline and sizeimage fields are computed from the requested bytes
116 * per line value in the pix format and information from the video instance.
118 * Return the number of padding bytes at end of line.
120 static unsigned int iss_video_mbus_to_pix(const struct iss_video *video,
121 const struct v4l2_mbus_framefmt *mbus,
122 struct v4l2_pix_format *pix)
124 unsigned int bpl = pix->bytesperline;
125 unsigned int min_bpl;
128 memset(pix, 0, sizeof(*pix));
129 pix->width = mbus->width;
130 pix->height = mbus->height;
133 * Skip the last format in the loop so that it will be selected if no
136 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
137 if (formats[i].code == mbus->code)
141 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
144 * Clamp the requested bytes per line value. If the maximum bytes per
145 * line value is zero, the module doesn't support user configurable line
146 * sizes. Override the requested value with the minimum in that case.
149 bpl = clamp(bpl, min_bpl, video->bpl_max);
153 if (!video->bpl_zero_padding || bpl != min_bpl)
154 bpl = ALIGN(bpl, video->bpl_alignment);
156 pix->pixelformat = formats[i].pixelformat;
157 pix->bytesperline = bpl;
158 pix->sizeimage = pix->bytesperline * pix->height;
159 pix->colorspace = mbus->colorspace;
160 pix->field = mbus->field;
162 /* FIXME: Special case for NV12! We should make this nicer... */
163 if (pix->pixelformat == V4L2_PIX_FMT_NV12)
164 pix->sizeimage += (pix->bytesperline * pix->height) / 2;
166 return bpl - min_bpl;
169 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
170 struct v4l2_mbus_framefmt *mbus)
174 memset(mbus, 0, sizeof(*mbus));
175 mbus->width = pix->width;
176 mbus->height = pix->height;
179 * Skip the last format in the loop so that it will be selected if no
182 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
183 if (formats[i].pixelformat == pix->pixelformat)
187 mbus->code = formats[i].code;
188 mbus->colorspace = pix->colorspace;
189 mbus->field = pix->field;
192 static struct v4l2_subdev *
193 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
195 struct media_pad *remote;
197 remote = media_entity_remote_pad(&video->pad);
199 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
203 *pad = remote->index;
205 return media_entity_to_v4l2_subdev(remote->entity);
208 /* Return a pointer to the ISS video instance at the far end of the pipeline. */
209 static struct iss_video *
210 iss_video_far_end(struct iss_video *video)
212 struct media_graph graph;
213 struct media_entity *entity = &video->video.entity;
214 struct media_device *mdev = entity->graph_obj.mdev;
215 struct iss_video *far_end = NULL;
217 mutex_lock(&mdev->graph_mutex);
219 if (media_graph_walk_init(&graph, mdev)) {
220 mutex_unlock(&mdev->graph_mutex);
224 media_graph_walk_start(&graph, entity);
226 while ((entity = media_graph_walk_next(&graph))) {
227 if (entity == &video->video.entity)
230 if (!is_media_entity_v4l2_video_device(entity))
233 far_end = to_iss_video(media_entity_to_video_device(entity));
234 if (far_end->type != video->type)
240 mutex_unlock(&mdev->graph_mutex);
242 media_graph_walk_cleanup(&graph);
248 __iss_video_get_format(struct iss_video *video,
249 struct v4l2_mbus_framefmt *format)
251 struct v4l2_subdev_format fmt;
252 struct v4l2_subdev *subdev;
256 subdev = iss_video_remote_subdev(video, &pad);
260 memset(&fmt, 0, sizeof(fmt));
262 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
264 mutex_lock(&video->mutex);
265 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
266 mutex_unlock(&video->mutex);
271 *format = fmt.format;
276 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
278 struct v4l2_mbus_framefmt format;
279 struct v4l2_pix_format pixfmt;
282 ret = __iss_video_get_format(video, &format);
286 pixfmt.bytesperline = 0;
287 ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
289 if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat ||
290 vfh->format.fmt.pix.height != pixfmt.height ||
291 vfh->format.fmt.pix.width != pixfmt.width ||
292 vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline ||
293 vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage)
299 /* -----------------------------------------------------------------------------
300 * Video queue operations
303 static int iss_video_queue_setup(struct vb2_queue *vq,
304 unsigned int *count, unsigned int *num_planes,
305 unsigned int sizes[],
306 struct device *alloc_devs[])
308 struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
309 struct iss_video *video = vfh->video;
311 /* Revisit multi-planar support for NV12 */
314 sizes[0] = vfh->format.fmt.pix.sizeimage;
318 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
323 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
325 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
326 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
328 if (buffer->iss_addr)
329 buffer->iss_addr = 0;
332 static int iss_video_buf_prepare(struct vb2_buffer *vb)
334 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
335 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
336 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
337 struct iss_video *video = vfh->video;
338 unsigned long size = vfh->format.fmt.pix.sizeimage;
341 if (vb2_plane_size(vb, 0) < size)
344 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
345 if (!IS_ALIGNED(addr, 32)) {
346 dev_dbg(video->iss->dev,
347 "Buffer address must be aligned to 32 bytes boundary.\n");
351 vb2_set_plane_payload(vb, 0, size);
352 buffer->iss_addr = addr;
356 static void iss_video_buf_queue(struct vb2_buffer *vb)
358 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
359 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
360 struct iss_video *video = vfh->video;
361 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
362 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
366 spin_lock_irqsave(&video->qlock, flags);
369 * Mark the buffer is faulty and give it back to the queue immediately
370 * if the video node has registered an error. vb2 will perform the same
371 * check when preparing the buffer, but that is inherently racy, so we
372 * need to handle the race condition with an authoritative check here.
374 if (unlikely(video->error)) {
375 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
376 spin_unlock_irqrestore(&video->qlock, flags);
380 empty = list_empty(&video->dmaqueue);
381 list_add_tail(&buffer->list, &video->dmaqueue);
383 spin_unlock_irqrestore(&video->qlock, flags);
386 enum iss_pipeline_state state;
389 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
390 state = ISS_PIPELINE_QUEUE_OUTPUT;
392 state = ISS_PIPELINE_QUEUE_INPUT;
394 spin_lock_irqsave(&pipe->lock, flags);
395 pipe->state |= state;
396 video->ops->queue(video, buffer);
397 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
399 start = iss_pipeline_ready(pipe);
401 pipe->state |= ISS_PIPELINE_STREAM;
402 spin_unlock_irqrestore(&pipe->lock, flags);
405 omap4iss_pipeline_set_stream(pipe,
406 ISS_PIPELINE_STREAM_SINGLESHOT);
410 static const struct vb2_ops iss_video_vb2ops = {
411 .queue_setup = iss_video_queue_setup,
412 .buf_prepare = iss_video_buf_prepare,
413 .buf_queue = iss_video_buf_queue,
414 .buf_cleanup = iss_video_buf_cleanup,
418 * omap4iss_video_buffer_next - Complete the current buffer and return the next
419 * @video: ISS video object
421 * Remove the current video buffer from the DMA queue and fill its timestamp,
422 * field count and state fields before waking up its completion handler.
424 * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
425 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
427 * The DMA queue is expected to contain at least one buffer.
429 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
432 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
434 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
435 enum iss_pipeline_state state;
436 struct iss_buffer *buf;
439 spin_lock_irqsave(&video->qlock, flags);
440 if (WARN_ON(list_empty(&video->dmaqueue))) {
441 spin_unlock_irqrestore(&video->qlock, flags);
445 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
447 list_del(&buf->list);
448 spin_unlock_irqrestore(&video->qlock, flags);
450 buf->vb.vb2_buf.timestamp = ktime_get_ns();
453 * Do frame number propagation only if this is the output video node.
454 * Frame number either comes from the CSI receivers or it gets
455 * incremented here if H3A is not active.
456 * Note: There is no guarantee that the output buffer will finish
457 * first, so the input number might lag behind by 1 in some cases.
459 if (video == pipe->output && !pipe->do_propagation)
461 atomic_inc_return(&pipe->frame_number);
463 buf->vb.sequence = atomic_read(&pipe->frame_number);
465 vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
466 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
469 spin_lock_irqsave(&video->qlock, flags);
470 if (list_empty(&video->dmaqueue)) {
471 spin_unlock_irqrestore(&video->qlock, flags);
472 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
473 state = ISS_PIPELINE_QUEUE_OUTPUT
474 | ISS_PIPELINE_STREAM;
476 state = ISS_PIPELINE_QUEUE_INPUT
477 | ISS_PIPELINE_STREAM;
479 spin_lock_irqsave(&pipe->lock, flags);
480 pipe->state &= ~state;
481 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
482 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
483 spin_unlock_irqrestore(&pipe->lock, flags);
487 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) {
488 spin_lock(&pipe->lock);
489 pipe->state &= ~ISS_PIPELINE_STREAM;
490 spin_unlock(&pipe->lock);
493 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
495 spin_unlock_irqrestore(&video->qlock, flags);
496 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
501 * omap4iss_video_cancel_stream - Cancel stream on a video node
502 * @video: ISS video object
504 * Cancelling a stream mark all buffers on the video node as erroneous and makes
505 * sure no new buffer can be queued.
507 void omap4iss_video_cancel_stream(struct iss_video *video)
511 spin_lock_irqsave(&video->qlock, flags);
513 while (!list_empty(&video->dmaqueue)) {
514 struct iss_buffer *buf;
516 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
518 list_del(&buf->list);
519 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
522 vb2_queue_error(video->queue);
525 spin_unlock_irqrestore(&video->qlock, flags);
528 /* -----------------------------------------------------------------------------
533 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
535 struct iss_video *video = video_drvdata(file);
537 strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
538 strlcpy(cap->card, video->video.name, sizeof(cap->card));
539 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
541 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
542 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
544 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
546 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
547 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
553 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
555 struct iss_video *video = video_drvdata(file);
556 struct v4l2_mbus_framefmt format;
557 unsigned int index = f->index;
561 if (f->type != video->type)
564 ret = __iss_video_get_format(video, &format);
568 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
569 const struct iss_format_info *info = &formats[i];
571 if (format.code != info->code)
575 f->pixelformat = info->pixelformat;
576 strlcpy(f->description, info->description,
577 sizeof(f->description));
588 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
590 struct iss_video_fh *vfh = to_iss_video_fh(fh);
591 struct iss_video *video = video_drvdata(file);
593 if (format->type != video->type)
596 mutex_lock(&video->mutex);
597 *format = vfh->format;
598 mutex_unlock(&video->mutex);
604 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
606 struct iss_video_fh *vfh = to_iss_video_fh(fh);
607 struct iss_video *video = video_drvdata(file);
608 struct v4l2_mbus_framefmt fmt;
610 if (format->type != video->type)
613 mutex_lock(&video->mutex);
616 * Fill the bytesperline and sizeimage fields by converting to media bus
617 * format and back to pixel format.
619 iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
620 iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
622 vfh->format = *format;
624 mutex_unlock(&video->mutex);
629 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
631 struct iss_video *video = video_drvdata(file);
632 struct v4l2_subdev_format fmt;
633 struct v4l2_subdev *subdev;
637 if (format->type != video->type)
640 subdev = iss_video_remote_subdev(video, &pad);
644 iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
647 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
648 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
652 iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
657 iss_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
659 struct iss_video *video = video_drvdata(file);
660 struct v4l2_subdev_format format;
661 struct v4l2_subdev *subdev;
662 struct v4l2_subdev_selection sdsel = {
663 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
664 .target = sel->target,
669 switch (sel->target) {
670 case V4L2_SEL_TGT_CROP:
671 case V4L2_SEL_TGT_CROP_BOUNDS:
672 case V4L2_SEL_TGT_CROP_DEFAULT:
673 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
676 case V4L2_SEL_TGT_COMPOSE:
677 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
678 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
679 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
685 subdev = iss_video_remote_subdev(video, &pad);
690 * Try the get selection operation first and fallback to get format if
694 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
697 if (ret != -ENOIOCTLCMD)
701 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
702 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
704 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
708 sel->r.width = format.format.width;
709 sel->r.height = format.format.height;
715 iss_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
717 struct iss_video *video = video_drvdata(file);
718 struct v4l2_subdev *subdev;
719 struct v4l2_subdev_selection sdsel = {
720 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
721 .target = sel->target,
728 switch (sel->target) {
729 case V4L2_SEL_TGT_CROP:
730 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
733 case V4L2_SEL_TGT_COMPOSE:
734 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
740 subdev = iss_video_remote_subdev(video, &pad);
745 mutex_lock(&video->mutex);
746 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
747 mutex_unlock(&video->mutex);
751 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
755 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
757 struct iss_video_fh *vfh = to_iss_video_fh(fh);
758 struct iss_video *video = video_drvdata(file);
760 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
761 video->type != a->type)
764 memset(a, 0, sizeof(*a));
765 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
766 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
767 a->parm.output.timeperframe = vfh->timeperframe;
773 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
775 struct iss_video_fh *vfh = to_iss_video_fh(fh);
776 struct iss_video *video = video_drvdata(file);
778 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
779 video->type != a->type)
782 if (a->parm.output.timeperframe.denominator == 0)
783 a->parm.output.timeperframe.denominator = 1;
785 vfh->timeperframe = a->parm.output.timeperframe;
791 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
793 struct iss_video_fh *vfh = to_iss_video_fh(fh);
795 return vb2_reqbufs(&vfh->queue, rb);
799 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
801 struct iss_video_fh *vfh = to_iss_video_fh(fh);
803 return vb2_querybuf(&vfh->queue, b);
807 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
809 struct iss_video_fh *vfh = to_iss_video_fh(fh);
811 return vb2_qbuf(&vfh->queue, b);
815 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
817 struct iss_video_fh *vfh = to_iss_video_fh(fh);
819 return vb2_expbuf(&vfh->queue, e);
823 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
825 struct iss_video_fh *vfh = to_iss_video_fh(fh);
827 return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
833 * Every ISS pipeline has a single input and a single output. The input can be
834 * either a sensor or a video node. The output is always a video node.
836 * As every pipeline has an output video node, the ISS video objects at the
837 * pipeline output stores the pipeline state. It tracks the streaming state of
838 * both the input and output, as well as the availability of buffers.
840 * In sensor-to-memory mode, frames are always available at the pipeline input.
841 * Starting the sensor usually requires I2C transfers and must be done in
842 * interruptible context. The pipeline is started and stopped synchronously
843 * to the stream on/off commands. All modules in the pipeline will get their
844 * subdev set stream handler called. The module at the end of the pipeline must
845 * delay starting the hardware until buffers are available at its output.
847 * In memory-to-memory mode, starting/stopping the stream requires
848 * synchronization between the input and output. ISS modules can't be stopped
849 * in the middle of a frame, and at least some of the modules seem to become
850 * busy as soon as they're started, even if they don't receive a frame start
851 * event. For that reason frames need to be processed in single-shot mode. The
852 * driver needs to wait until a frame is completely processed and written to
853 * memory before restarting the pipeline for the next frame. Pipelined
854 * processing might be possible but requires more testing.
856 * Stream start must be delayed until buffers are available at both the input
857 * and output. The pipeline must be started in the videobuf queue callback with
858 * the buffers queue spinlock held. The modules subdev set stream operation must
862 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
864 struct iss_video_fh *vfh = to_iss_video_fh(fh);
865 struct iss_video *video = video_drvdata(file);
866 struct media_graph graph;
867 struct media_entity *entity = &video->video.entity;
868 enum iss_pipeline_state state;
869 struct iss_pipeline *pipe;
870 struct iss_video *far_end;
874 if (type != video->type)
877 mutex_lock(&video->stream_lock);
880 * Start streaming on the pipeline. No link touching an entity in the
881 * pipeline can be activated or deactivated once streaming is started.
884 ? to_iss_pipeline(entity) : &video->pipe;
885 pipe->external = NULL;
886 pipe->external_rate = 0;
887 pipe->external_bpp = 0;
889 ret = media_entity_enum_init(&pipe->ent_enum, entity->graph_obj.mdev);
891 goto err_graph_walk_init;
893 ret = media_graph_walk_init(&graph, entity->graph_obj.mdev);
895 goto err_graph_walk_init;
897 if (video->iss->pdata->set_constraints)
898 video->iss->pdata->set_constraints(video->iss, true);
900 ret = media_pipeline_start(entity, &pipe->pipe);
902 goto err_media_pipeline_start;
904 media_graph_walk_start(&graph, entity);
905 while ((entity = media_graph_walk_next(&graph)))
906 media_entity_enum_set(&pipe->ent_enum, entity);
909 * Verify that the currently configured format matches the output of
910 * the connected subdev.
912 ret = iss_video_check_format(video, vfh);
914 goto err_iss_video_check_format;
916 video->bpl_padding = ret;
917 video->bpl_value = vfh->format.fmt.pix.bytesperline;
920 * Find the ISS video node connected at the far end of the pipeline and
921 * update the pipeline.
923 far_end = iss_video_far_end(video);
925 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
926 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
927 pipe->input = far_end;
928 pipe->output = video;
932 goto err_iss_video_check_format;
935 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
937 pipe->output = far_end;
940 spin_lock_irqsave(&pipe->lock, flags);
941 pipe->state &= ~ISS_PIPELINE_STREAM;
942 pipe->state |= state;
943 spin_unlock_irqrestore(&pipe->lock, flags);
946 * Set the maximum time per frame as the value requested by userspace.
947 * This is a soft limit that can be overridden if the hardware doesn't
948 * support the request limit.
950 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
951 pipe->max_timeperframe = vfh->timeperframe;
953 video->queue = &vfh->queue;
954 INIT_LIST_HEAD(&video->dmaqueue);
955 video->error = false;
956 atomic_set(&pipe->frame_number, -1);
958 ret = vb2_streamon(&vfh->queue, type);
960 goto err_iss_video_check_format;
963 * In sensor-to-memory mode, the stream can be started synchronously
964 * to the stream on command. In memory-to-memory mode, it will be
965 * started when buffers are queued on both the input and output.
970 ret = omap4iss_pipeline_set_stream(pipe,
971 ISS_PIPELINE_STREAM_CONTINUOUS);
973 goto err_omap4iss_set_stream;
974 spin_lock_irqsave(&video->qlock, flags);
975 if (list_empty(&video->dmaqueue))
976 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
977 spin_unlock_irqrestore(&video->qlock, flags);
980 media_graph_walk_cleanup(&graph);
982 mutex_unlock(&video->stream_lock);
986 err_omap4iss_set_stream:
987 vb2_streamoff(&vfh->queue, type);
988 err_iss_video_check_format:
989 media_pipeline_stop(&video->video.entity);
990 err_media_pipeline_start:
991 if (video->iss->pdata->set_constraints)
992 video->iss->pdata->set_constraints(video->iss, false);
995 media_graph_walk_cleanup(&graph);
998 media_entity_enum_cleanup(&pipe->ent_enum);
1000 mutex_unlock(&video->stream_lock);
1006 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1008 struct iss_video_fh *vfh = to_iss_video_fh(fh);
1009 struct iss_video *video = video_drvdata(file);
1010 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
1011 enum iss_pipeline_state state;
1012 unsigned long flags;
1014 if (type != video->type)
1017 mutex_lock(&video->stream_lock);
1019 if (!vb2_is_streaming(&vfh->queue))
1022 /* Update the pipeline state. */
1023 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1024 state = ISS_PIPELINE_STREAM_OUTPUT
1025 | ISS_PIPELINE_QUEUE_OUTPUT;
1027 state = ISS_PIPELINE_STREAM_INPUT
1028 | ISS_PIPELINE_QUEUE_INPUT;
1030 spin_lock_irqsave(&pipe->lock, flags);
1031 pipe->state &= ~state;
1032 spin_unlock_irqrestore(&pipe->lock, flags);
1034 /* Stop the stream. */
1035 omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
1036 vb2_streamoff(&vfh->queue, type);
1037 video->queue = NULL;
1039 media_entity_enum_cleanup(&pipe->ent_enum);
1041 if (video->iss->pdata->set_constraints)
1042 video->iss->pdata->set_constraints(video->iss, false);
1043 media_pipeline_stop(&video->video.entity);
1046 mutex_unlock(&video->stream_lock);
1051 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1053 if (input->index > 0)
1056 strlcpy(input->name, "camera", sizeof(input->name));
1057 input->type = V4L2_INPUT_TYPE_CAMERA;
1063 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
1071 iss_video_s_input(struct file *file, void *fh, unsigned int input)
1073 return input == 0 ? 0 : -EINVAL;
1076 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
1077 .vidioc_querycap = iss_video_querycap,
1078 .vidioc_enum_fmt_vid_cap = iss_video_enum_format,
1079 .vidioc_g_fmt_vid_cap = iss_video_get_format,
1080 .vidioc_s_fmt_vid_cap = iss_video_set_format,
1081 .vidioc_try_fmt_vid_cap = iss_video_try_format,
1082 .vidioc_g_fmt_vid_out = iss_video_get_format,
1083 .vidioc_s_fmt_vid_out = iss_video_set_format,
1084 .vidioc_try_fmt_vid_out = iss_video_try_format,
1085 .vidioc_g_selection = iss_video_get_selection,
1086 .vidioc_s_selection = iss_video_set_selection,
1087 .vidioc_g_parm = iss_video_get_param,
1088 .vidioc_s_parm = iss_video_set_param,
1089 .vidioc_reqbufs = iss_video_reqbufs,
1090 .vidioc_querybuf = iss_video_querybuf,
1091 .vidioc_qbuf = iss_video_qbuf,
1092 .vidioc_expbuf = iss_video_expbuf,
1093 .vidioc_dqbuf = iss_video_dqbuf,
1094 .vidioc_streamon = iss_video_streamon,
1095 .vidioc_streamoff = iss_video_streamoff,
1096 .vidioc_enum_input = iss_video_enum_input,
1097 .vidioc_g_input = iss_video_g_input,
1098 .vidioc_s_input = iss_video_s_input,
1101 /* -----------------------------------------------------------------------------
1102 * V4L2 file operations
1105 static int iss_video_open(struct file *file)
1107 struct iss_video *video = video_drvdata(file);
1108 struct iss_video_fh *handle;
1109 struct vb2_queue *q;
1112 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1116 v4l2_fh_init(&handle->vfh, &video->video);
1117 v4l2_fh_add(&handle->vfh);
1119 /* If this is the first user, initialise the pipeline. */
1120 if (!omap4iss_get(video->iss)) {
1125 ret = v4l2_pipeline_pm_use(&video->video.entity, 1);
1127 omap4iss_put(video->iss);
1133 q->type = video->type;
1134 q->io_modes = VB2_MMAP | VB2_DMABUF;
1135 q->drv_priv = handle;
1136 q->ops = &iss_video_vb2ops;
1137 q->mem_ops = &vb2_dma_contig_memops;
1138 q->buf_struct_size = sizeof(struct iss_buffer);
1139 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1140 q->dev = video->iss->dev;
1142 ret = vb2_queue_init(q);
1144 omap4iss_put(video->iss);
1148 memset(&handle->format, 0, sizeof(handle->format));
1149 handle->format.type = video->type;
1150 handle->timeperframe.denominator = 1;
1152 handle->video = video;
1153 file->private_data = &handle->vfh;
1157 v4l2_fh_del(&handle->vfh);
1158 v4l2_fh_exit(&handle->vfh);
1165 static int iss_video_release(struct file *file)
1167 struct iss_video *video = video_drvdata(file);
1168 struct v4l2_fh *vfh = file->private_data;
1169 struct iss_video_fh *handle = to_iss_video_fh(vfh);
1171 /* Disable streaming and free the buffers queue resources. */
1172 iss_video_streamoff(file, vfh, video->type);
1174 v4l2_pipeline_pm_use(&video->video.entity, 0);
1176 /* Release the videobuf2 queue */
1177 vb2_queue_release(&handle->queue);
1182 file->private_data = NULL;
1184 omap4iss_put(video->iss);
1189 static __poll_t iss_video_poll(struct file *file, poll_table *wait)
1191 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1193 return vb2_poll(&vfh->queue, file, wait);
1196 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1198 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1200 return vb2_mmap(&vfh->queue, vma);
1203 static const struct v4l2_file_operations iss_video_fops = {
1204 .owner = THIS_MODULE,
1205 .unlocked_ioctl = video_ioctl2,
1206 .open = iss_video_open,
1207 .release = iss_video_release,
1208 .poll = iss_video_poll,
1209 .mmap = iss_video_mmap,
1212 /* -----------------------------------------------------------------------------
1216 static const struct iss_video_operations iss_video_dummy_ops = {
1219 int omap4iss_video_init(struct iss_video *video, const char *name)
1221 const char *direction;
1224 switch (video->type) {
1225 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1226 direction = "output";
1227 video->pad.flags = MEDIA_PAD_FL_SINK;
1229 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1230 direction = "input";
1231 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1238 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1242 spin_lock_init(&video->qlock);
1243 mutex_init(&video->mutex);
1244 atomic_set(&video->active, 0);
1246 spin_lock_init(&video->pipe.lock);
1247 mutex_init(&video->stream_lock);
1249 /* Initialize the video device. */
1251 video->ops = &iss_video_dummy_ops;
1253 video->video.fops = &iss_video_fops;
1254 snprintf(video->video.name, sizeof(video->video.name),
1255 "OMAP4 ISS %s %s", name, direction);
1256 video->video.vfl_type = VFL_TYPE_GRABBER;
1257 video->video.release = video_device_release_empty;
1258 video->video.ioctl_ops = &iss_video_ioctl_ops;
1259 video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1261 video_set_drvdata(&video->video, video);
1266 void omap4iss_video_cleanup(struct iss_video *video)
1268 media_entity_cleanup(&video->video.entity);
1269 mutex_destroy(&video->stream_lock);
1270 mutex_destroy(&video->mutex);
1273 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1277 video->video.v4l2_dev = vdev;
1279 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1281 dev_err(video->iss->dev,
1282 "could not register video device (%d)\n", ret);
1287 void omap4iss_video_unregister(struct iss_video *video)
1289 video_unregister_device(&video->video);