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;
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;
142 /* Clamp the requested bytes per line value. If the maximum bytes per
143 * line value is zero, the module doesn't support user configurable line
144 * sizes. Override the requested value with the minimum in that case.
147 bpl = clamp(bpl, min_bpl, video->bpl_max);
151 if (!video->bpl_zero_padding || bpl != min_bpl)
152 bpl = ALIGN(bpl, video->bpl_alignment);
154 pix->pixelformat = formats[i].pixelformat;
155 pix->bytesperline = bpl;
156 pix->sizeimage = pix->bytesperline * pix->height;
157 pix->colorspace = mbus->colorspace;
158 pix->field = mbus->field;
160 /* FIXME: Special case for NV12! We should make this nicer... */
161 if (pix->pixelformat == V4L2_PIX_FMT_NV12)
162 pix->sizeimage += (pix->bytesperline * pix->height) / 2;
164 return bpl - min_bpl;
167 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
168 struct v4l2_mbus_framefmt *mbus)
172 memset(mbus, 0, sizeof(*mbus));
173 mbus->width = pix->width;
174 mbus->height = pix->height;
176 /* Skip the last format in the loop so that it will be selected if no
179 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
180 if (formats[i].pixelformat == pix->pixelformat)
184 mbus->code = formats[i].code;
185 mbus->colorspace = pix->colorspace;
186 mbus->field = pix->field;
189 static struct v4l2_subdev *
190 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
192 struct media_pad *remote;
194 remote = media_entity_remote_pad(&video->pad);
196 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
200 *pad = remote->index;
202 return media_entity_to_v4l2_subdev(remote->entity);
205 /* Return a pointer to the ISS video instance at the far end of the pipeline. */
206 static struct iss_video *
207 iss_video_far_end(struct iss_video *video)
209 struct media_entity_graph graph;
210 struct media_entity *entity = &video->video.entity;
211 struct media_device *mdev = entity->graph_obj.mdev;
212 struct iss_video *far_end = NULL;
214 mutex_lock(&mdev->graph_mutex);
216 if (media_entity_graph_walk_init(&graph, mdev)) {
217 mutex_unlock(&mdev->graph_mutex);
221 media_entity_graph_walk_start(&graph, entity);
223 while ((entity = media_entity_graph_walk_next(&graph))) {
224 if (entity == &video->video.entity)
227 if (!is_media_entity_v4l2_video_device(entity))
230 far_end = to_iss_video(media_entity_to_video_device(entity));
231 if (far_end->type != video->type)
237 mutex_unlock(&mdev->graph_mutex);
239 media_entity_graph_walk_cleanup(&graph);
245 __iss_video_get_format(struct iss_video *video,
246 struct v4l2_mbus_framefmt *format)
248 struct v4l2_subdev_format fmt;
249 struct v4l2_subdev *subdev;
253 subdev = iss_video_remote_subdev(video, &pad);
257 memset(&fmt, 0, sizeof(fmt));
259 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
261 mutex_lock(&video->mutex);
262 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
263 mutex_unlock(&video->mutex);
268 *format = fmt.format;
273 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
275 struct v4l2_mbus_framefmt format;
276 struct v4l2_pix_format pixfmt;
279 ret = __iss_video_get_format(video, &format);
283 pixfmt.bytesperline = 0;
284 ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
286 if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat ||
287 vfh->format.fmt.pix.height != pixfmt.height ||
288 vfh->format.fmt.pix.width != pixfmt.width ||
289 vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline ||
290 vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage)
296 /* -----------------------------------------------------------------------------
297 * Video queue operations
300 static int iss_video_queue_setup(struct vb2_queue *vq,
301 unsigned int *count, unsigned int *num_planes,
302 unsigned int sizes[], struct device *alloc_devs[])
304 struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
305 struct iss_video *video = vfh->video;
307 /* Revisit multi-planar support for NV12 */
310 sizes[0] = vfh->format.fmt.pix.sizeimage;
314 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
319 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
321 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
322 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
324 if (buffer->iss_addr)
325 buffer->iss_addr = 0;
328 static int iss_video_buf_prepare(struct vb2_buffer *vb)
330 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
331 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
332 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
333 struct iss_video *video = vfh->video;
334 unsigned long size = vfh->format.fmt.pix.sizeimage;
337 if (vb2_plane_size(vb, 0) < size)
340 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
341 if (!IS_ALIGNED(addr, 32)) {
342 dev_dbg(video->iss->dev,
343 "Buffer address must be aligned to 32 bytes boundary.\n");
347 vb2_set_plane_payload(vb, 0, size);
348 buffer->iss_addr = addr;
352 static void iss_video_buf_queue(struct vb2_buffer *vb)
354 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
355 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
356 struct iss_video *video = vfh->video;
357 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
358 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
362 spin_lock_irqsave(&video->qlock, flags);
364 /* Mark the buffer is faulty and give it back to the queue immediately
365 * if the video node has registered an error. vb2 will perform the same
366 * check when preparing the buffer, but that is inherently racy, so we
367 * need to handle the race condition with an authoritative check here.
369 if (unlikely(video->error)) {
370 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
371 spin_unlock_irqrestore(&video->qlock, flags);
375 empty = list_empty(&video->dmaqueue);
376 list_add_tail(&buffer->list, &video->dmaqueue);
378 spin_unlock_irqrestore(&video->qlock, flags);
381 enum iss_pipeline_state state;
384 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
385 state = ISS_PIPELINE_QUEUE_OUTPUT;
387 state = ISS_PIPELINE_QUEUE_INPUT;
389 spin_lock_irqsave(&pipe->lock, flags);
390 pipe->state |= state;
391 video->ops->queue(video, buffer);
392 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
394 start = iss_pipeline_ready(pipe);
396 pipe->state |= ISS_PIPELINE_STREAM;
397 spin_unlock_irqrestore(&pipe->lock, flags);
400 omap4iss_pipeline_set_stream(pipe,
401 ISS_PIPELINE_STREAM_SINGLESHOT);
405 static const struct vb2_ops iss_video_vb2ops = {
406 .queue_setup = iss_video_queue_setup,
407 .buf_prepare = iss_video_buf_prepare,
408 .buf_queue = iss_video_buf_queue,
409 .buf_cleanup = iss_video_buf_cleanup,
413 * omap4iss_video_buffer_next - Complete the current buffer and return the next
414 * @video: ISS video object
416 * Remove the current video buffer from the DMA queue and fill its timestamp,
417 * field count and state fields before waking up its completion handler.
419 * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
420 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
422 * The DMA queue is expected to contain at least one buffer.
424 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
427 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
429 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
430 enum iss_pipeline_state state;
431 struct iss_buffer *buf;
434 spin_lock_irqsave(&video->qlock, flags);
435 if (WARN_ON(list_empty(&video->dmaqueue))) {
436 spin_unlock_irqrestore(&video->qlock, flags);
440 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
442 list_del(&buf->list);
443 spin_unlock_irqrestore(&video->qlock, flags);
445 buf->vb.vb2_buf.timestamp = ktime_get_ns();
447 /* Do frame number propagation only if this is the output video node.
448 * Frame number either comes from the CSI receivers or it gets
449 * incremented here if H3A is not active.
450 * Note: There is no guarantee that the output buffer will finish
451 * first, so the input number might lag behind by 1 in some cases.
453 if (video == pipe->output && !pipe->do_propagation)
455 atomic_inc_return(&pipe->frame_number);
457 buf->vb.sequence = atomic_read(&pipe->frame_number);
459 vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
460 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
463 spin_lock_irqsave(&video->qlock, flags);
464 if (list_empty(&video->dmaqueue)) {
465 spin_unlock_irqrestore(&video->qlock, flags);
466 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
467 state = ISS_PIPELINE_QUEUE_OUTPUT
468 | ISS_PIPELINE_STREAM;
470 state = ISS_PIPELINE_QUEUE_INPUT
471 | ISS_PIPELINE_STREAM;
473 spin_lock_irqsave(&pipe->lock, flags);
474 pipe->state &= ~state;
475 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
476 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
477 spin_unlock_irqrestore(&pipe->lock, flags);
481 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) {
482 spin_lock(&pipe->lock);
483 pipe->state &= ~ISS_PIPELINE_STREAM;
484 spin_unlock(&pipe->lock);
487 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
489 spin_unlock_irqrestore(&video->qlock, flags);
490 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
495 * omap4iss_video_cancel_stream - Cancel stream on a video node
496 * @video: ISS video object
498 * Cancelling a stream mark all buffers on the video node as erroneous and makes
499 * sure no new buffer can be queued.
501 void omap4iss_video_cancel_stream(struct iss_video *video)
505 spin_lock_irqsave(&video->qlock, flags);
507 while (!list_empty(&video->dmaqueue)) {
508 struct iss_buffer *buf;
510 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
512 list_del(&buf->list);
513 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
516 vb2_queue_error(video->queue);
519 spin_unlock_irqrestore(&video->qlock, flags);
522 /* -----------------------------------------------------------------------------
527 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
529 struct iss_video *video = video_drvdata(file);
531 strlcpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
532 strlcpy(cap->card, video->video.name, sizeof(cap->card));
533 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
535 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
536 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
538 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
540 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
541 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
547 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
549 struct iss_video *video = video_drvdata(file);
550 struct v4l2_mbus_framefmt format;
551 unsigned int index = f->index;
555 if (f->type != video->type)
558 ret = __iss_video_get_format(video, &format);
562 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
563 const struct iss_format_info *info = &formats[i];
565 if (format.code != info->code)
569 f->pixelformat = info->pixelformat;
570 strlcpy(f->description, info->description,
571 sizeof(f->description));
582 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
584 struct iss_video_fh *vfh = to_iss_video_fh(fh);
585 struct iss_video *video = video_drvdata(file);
587 if (format->type != video->type)
590 mutex_lock(&video->mutex);
591 *format = vfh->format;
592 mutex_unlock(&video->mutex);
598 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
600 struct iss_video_fh *vfh = to_iss_video_fh(fh);
601 struct iss_video *video = video_drvdata(file);
602 struct v4l2_mbus_framefmt fmt;
604 if (format->type != video->type)
607 mutex_lock(&video->mutex);
609 /* Fill the bytesperline and sizeimage fields by converting to media bus
610 * format and back to pixel format.
612 iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
613 iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
615 vfh->format = *format;
617 mutex_unlock(&video->mutex);
622 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
624 struct iss_video *video = video_drvdata(file);
625 struct v4l2_subdev_format fmt;
626 struct v4l2_subdev *subdev;
630 if (format->type != video->type)
633 subdev = iss_video_remote_subdev(video, &pad);
637 iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
640 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
641 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
645 iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
650 iss_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
652 struct iss_video *video = video_drvdata(file);
653 struct v4l2_subdev_format format;
654 struct v4l2_subdev *subdev;
655 struct v4l2_subdev_selection sdsel = {
656 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
657 .target = sel->target,
662 switch (sel->target) {
663 case V4L2_SEL_TGT_CROP:
664 case V4L2_SEL_TGT_CROP_BOUNDS:
665 case V4L2_SEL_TGT_CROP_DEFAULT:
666 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
669 case V4L2_SEL_TGT_COMPOSE:
670 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
671 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
672 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
678 subdev = iss_video_remote_subdev(video, &pad);
682 /* Try the get selection operation first and fallback to get format if not
686 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
689 if (ret != -ENOIOCTLCMD)
693 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
694 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
696 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
700 sel->r.width = format.format.width;
701 sel->r.height = format.format.height;
707 iss_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
709 struct iss_video *video = video_drvdata(file);
710 struct v4l2_subdev *subdev;
711 struct v4l2_subdev_selection sdsel = {
712 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
713 .target = sel->target,
720 switch (sel->target) {
721 case V4L2_SEL_TGT_CROP:
722 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
725 case V4L2_SEL_TGT_COMPOSE:
726 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
732 subdev = iss_video_remote_subdev(video, &pad);
737 mutex_lock(&video->mutex);
738 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
739 mutex_unlock(&video->mutex);
743 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
747 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
749 struct iss_video_fh *vfh = to_iss_video_fh(fh);
750 struct iss_video *video = video_drvdata(file);
752 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
753 video->type != a->type)
756 memset(a, 0, sizeof(*a));
757 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
758 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
759 a->parm.output.timeperframe = vfh->timeperframe;
765 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
767 struct iss_video_fh *vfh = to_iss_video_fh(fh);
768 struct iss_video *video = video_drvdata(file);
770 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
771 video->type != a->type)
774 if (a->parm.output.timeperframe.denominator == 0)
775 a->parm.output.timeperframe.denominator = 1;
777 vfh->timeperframe = a->parm.output.timeperframe;
783 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
785 struct iss_video_fh *vfh = to_iss_video_fh(fh);
787 return vb2_reqbufs(&vfh->queue, rb);
791 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
793 struct iss_video_fh *vfh = to_iss_video_fh(fh);
795 return vb2_querybuf(&vfh->queue, b);
799 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
801 struct iss_video_fh *vfh = to_iss_video_fh(fh);
803 return vb2_qbuf(&vfh->queue, b);
807 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
809 struct iss_video_fh *vfh = to_iss_video_fh(fh);
811 return vb2_expbuf(&vfh->queue, e);
815 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
817 struct iss_video_fh *vfh = to_iss_video_fh(fh);
819 return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
825 * Every ISS pipeline has a single input and a single output. The input can be
826 * either a sensor or a video node. The output is always a video node.
828 * As every pipeline has an output video node, the ISS video objects at the
829 * pipeline output stores the pipeline state. It tracks the streaming state of
830 * both the input and output, as well as the availability of buffers.
832 * In sensor-to-memory mode, frames are always available at the pipeline input.
833 * Starting the sensor usually requires I2C transfers and must be done in
834 * interruptible context. The pipeline is started and stopped synchronously
835 * to the stream on/off commands. All modules in the pipeline will get their
836 * subdev set stream handler called. The module at the end of the pipeline must
837 * delay starting the hardware until buffers are available at its output.
839 * In memory-to-memory mode, starting/stopping the stream requires
840 * synchronization between the input and output. ISS modules can't be stopped
841 * in the middle of a frame, and at least some of the modules seem to become
842 * busy as soon as they're started, even if they don't receive a frame start
843 * event. For that reason frames need to be processed in single-shot mode. The
844 * driver needs to wait until a frame is completely processed and written to
845 * memory before restarting the pipeline for the next frame. Pipelined
846 * processing might be possible but requires more testing.
848 * Stream start must be delayed until buffers are available at both the input
849 * and output. The pipeline must be started in the videobuf queue callback with
850 * the buffers queue spinlock held. The modules subdev set stream operation must
854 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
856 struct iss_video_fh *vfh = to_iss_video_fh(fh);
857 struct iss_video *video = video_drvdata(file);
858 struct media_entity_graph graph;
859 struct media_entity *entity = &video->video.entity;
860 enum iss_pipeline_state state;
861 struct iss_pipeline *pipe;
862 struct iss_video *far_end;
866 if (type != video->type)
869 mutex_lock(&video->stream_lock);
871 /* Start streaming on the pipeline. No link touching an entity in the
872 * pipeline can be activated or deactivated once streaming is started.
875 ? to_iss_pipeline(entity) : &video->pipe;
876 pipe->external = NULL;
877 pipe->external_rate = 0;
878 pipe->external_bpp = 0;
880 ret = media_entity_enum_init(&pipe->ent_enum, entity->graph_obj.mdev);
882 goto err_graph_walk_init;
884 ret = media_entity_graph_walk_init(&graph, entity->graph_obj.mdev);
886 goto err_graph_walk_init;
888 if (video->iss->pdata->set_constraints)
889 video->iss->pdata->set_constraints(video->iss, true);
891 ret = media_entity_pipeline_start(entity, &pipe->pipe);
893 goto err_media_entity_pipeline_start;
895 media_entity_graph_walk_start(&graph, entity);
896 while ((entity = media_entity_graph_walk_next(&graph)))
897 media_entity_enum_set(&pipe->ent_enum, entity);
899 /* Verify that the currently configured format matches the output of
900 * the connected subdev.
902 ret = iss_video_check_format(video, vfh);
904 goto err_iss_video_check_format;
906 video->bpl_padding = ret;
907 video->bpl_value = vfh->format.fmt.pix.bytesperline;
909 /* Find the ISS video node connected at the far end of the pipeline and
910 * update the pipeline.
912 far_end = iss_video_far_end(video);
914 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
915 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
916 pipe->input = far_end;
917 pipe->output = video;
921 goto err_iss_video_check_format;
924 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
926 pipe->output = far_end;
929 spin_lock_irqsave(&pipe->lock, flags);
930 pipe->state &= ~ISS_PIPELINE_STREAM;
931 pipe->state |= state;
932 spin_unlock_irqrestore(&pipe->lock, flags);
934 /* Set the maximum time per frame as the value requested by userspace.
935 * This is a soft limit that can be overridden if the hardware doesn't
936 * support the request limit.
938 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
939 pipe->max_timeperframe = vfh->timeperframe;
941 video->queue = &vfh->queue;
942 INIT_LIST_HEAD(&video->dmaqueue);
943 video->error = false;
944 atomic_set(&pipe->frame_number, -1);
946 ret = vb2_streamon(&vfh->queue, type);
948 goto err_iss_video_check_format;
950 /* In sensor-to-memory mode, the stream can be started synchronously
951 * to the stream on command. In memory-to-memory mode, it will be
952 * started when buffers are queued on both the input and output.
957 ret = omap4iss_pipeline_set_stream(pipe,
958 ISS_PIPELINE_STREAM_CONTINUOUS);
960 goto err_omap4iss_set_stream;
961 spin_lock_irqsave(&video->qlock, flags);
962 if (list_empty(&video->dmaqueue))
963 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
964 spin_unlock_irqrestore(&video->qlock, flags);
967 media_entity_graph_walk_cleanup(&graph);
969 mutex_unlock(&video->stream_lock);
973 err_omap4iss_set_stream:
974 vb2_streamoff(&vfh->queue, type);
975 err_iss_video_check_format:
976 media_entity_pipeline_stop(&video->video.entity);
977 err_media_entity_pipeline_start:
978 if (video->iss->pdata->set_constraints)
979 video->iss->pdata->set_constraints(video->iss, false);
982 media_entity_graph_walk_cleanup(&graph);
985 media_entity_enum_cleanup(&pipe->ent_enum);
987 mutex_unlock(&video->stream_lock);
993 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
995 struct iss_video_fh *vfh = to_iss_video_fh(fh);
996 struct iss_video *video = video_drvdata(file);
997 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
998 enum iss_pipeline_state state;
1001 if (type != video->type)
1004 mutex_lock(&video->stream_lock);
1006 if (!vb2_is_streaming(&vfh->queue))
1009 /* Update the pipeline state. */
1010 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1011 state = ISS_PIPELINE_STREAM_OUTPUT
1012 | ISS_PIPELINE_QUEUE_OUTPUT;
1014 state = ISS_PIPELINE_STREAM_INPUT
1015 | ISS_PIPELINE_QUEUE_INPUT;
1017 spin_lock_irqsave(&pipe->lock, flags);
1018 pipe->state &= ~state;
1019 spin_unlock_irqrestore(&pipe->lock, flags);
1021 /* Stop the stream. */
1022 omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
1023 vb2_streamoff(&vfh->queue, type);
1024 video->queue = NULL;
1026 media_entity_enum_cleanup(&pipe->ent_enum);
1028 if (video->iss->pdata->set_constraints)
1029 video->iss->pdata->set_constraints(video->iss, false);
1030 media_entity_pipeline_stop(&video->video.entity);
1033 mutex_unlock(&video->stream_lock);
1038 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1040 if (input->index > 0)
1043 strlcpy(input->name, "camera", sizeof(input->name));
1044 input->type = V4L2_INPUT_TYPE_CAMERA;
1050 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
1058 iss_video_s_input(struct file *file, void *fh, unsigned int input)
1060 return input == 0 ? 0 : -EINVAL;
1063 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
1064 .vidioc_querycap = iss_video_querycap,
1065 .vidioc_enum_fmt_vid_cap = iss_video_enum_format,
1066 .vidioc_g_fmt_vid_cap = iss_video_get_format,
1067 .vidioc_s_fmt_vid_cap = iss_video_set_format,
1068 .vidioc_try_fmt_vid_cap = iss_video_try_format,
1069 .vidioc_g_fmt_vid_out = iss_video_get_format,
1070 .vidioc_s_fmt_vid_out = iss_video_set_format,
1071 .vidioc_try_fmt_vid_out = iss_video_try_format,
1072 .vidioc_g_selection = iss_video_get_selection,
1073 .vidioc_s_selection = iss_video_set_selection,
1074 .vidioc_g_parm = iss_video_get_param,
1075 .vidioc_s_parm = iss_video_set_param,
1076 .vidioc_reqbufs = iss_video_reqbufs,
1077 .vidioc_querybuf = iss_video_querybuf,
1078 .vidioc_qbuf = iss_video_qbuf,
1079 .vidioc_expbuf = iss_video_expbuf,
1080 .vidioc_dqbuf = iss_video_dqbuf,
1081 .vidioc_streamon = iss_video_streamon,
1082 .vidioc_streamoff = iss_video_streamoff,
1083 .vidioc_enum_input = iss_video_enum_input,
1084 .vidioc_g_input = iss_video_g_input,
1085 .vidioc_s_input = iss_video_s_input,
1088 /* -----------------------------------------------------------------------------
1089 * V4L2 file operations
1092 static int iss_video_open(struct file *file)
1094 struct iss_video *video = video_drvdata(file);
1095 struct iss_video_fh *handle;
1096 struct vb2_queue *q;
1099 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1103 v4l2_fh_init(&handle->vfh, &video->video);
1104 v4l2_fh_add(&handle->vfh);
1106 /* If this is the first user, initialise the pipeline. */
1107 if (!omap4iss_get(video->iss)) {
1112 ret = v4l2_pipeline_pm_use(&video->video.entity, 1);
1114 omap4iss_put(video->iss);
1120 q->type = video->type;
1121 q->io_modes = VB2_MMAP | VB2_DMABUF;
1122 q->drv_priv = handle;
1123 q->ops = &iss_video_vb2ops;
1124 q->mem_ops = &vb2_dma_contig_memops;
1125 q->buf_struct_size = sizeof(struct iss_buffer);
1126 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1127 q->dev = video->iss->dev;
1129 ret = vb2_queue_init(q);
1131 omap4iss_put(video->iss);
1135 memset(&handle->format, 0, sizeof(handle->format));
1136 handle->format.type = video->type;
1137 handle->timeperframe.denominator = 1;
1139 handle->video = video;
1140 file->private_data = &handle->vfh;
1144 v4l2_fh_del(&handle->vfh);
1151 static int iss_video_release(struct file *file)
1153 struct iss_video *video = video_drvdata(file);
1154 struct v4l2_fh *vfh = file->private_data;
1155 struct iss_video_fh *handle = to_iss_video_fh(vfh);
1157 /* Disable streaming and free the buffers queue resources. */
1158 iss_video_streamoff(file, vfh, video->type);
1160 v4l2_pipeline_pm_use(&video->video.entity, 0);
1162 /* Release the videobuf2 queue */
1163 vb2_queue_release(&handle->queue);
1167 file->private_data = NULL;
1169 omap4iss_put(video->iss);
1174 static unsigned int iss_video_poll(struct file *file, poll_table *wait)
1176 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1178 return vb2_poll(&vfh->queue, file, wait);
1181 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1183 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1185 return vb2_mmap(&vfh->queue, vma);
1188 static struct v4l2_file_operations iss_video_fops = {
1189 .owner = THIS_MODULE,
1190 .unlocked_ioctl = video_ioctl2,
1191 .open = iss_video_open,
1192 .release = iss_video_release,
1193 .poll = iss_video_poll,
1194 .mmap = iss_video_mmap,
1197 /* -----------------------------------------------------------------------------
1201 static const struct iss_video_operations iss_video_dummy_ops = {
1204 int omap4iss_video_init(struct iss_video *video, const char *name)
1206 const char *direction;
1209 switch (video->type) {
1210 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1211 direction = "output";
1212 video->pad.flags = MEDIA_PAD_FL_SINK;
1214 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1215 direction = "input";
1216 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1223 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1227 spin_lock_init(&video->qlock);
1228 mutex_init(&video->mutex);
1229 atomic_set(&video->active, 0);
1231 spin_lock_init(&video->pipe.lock);
1232 mutex_init(&video->stream_lock);
1234 /* Initialize the video device. */
1236 video->ops = &iss_video_dummy_ops;
1238 video->video.fops = &iss_video_fops;
1239 snprintf(video->video.name, sizeof(video->video.name),
1240 "OMAP4 ISS %s %s", name, direction);
1241 video->video.vfl_type = VFL_TYPE_GRABBER;
1242 video->video.release = video_device_release_empty;
1243 video->video.ioctl_ops = &iss_video_ioctl_ops;
1244 video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1246 video_set_drvdata(&video->video, video);
1251 void omap4iss_video_cleanup(struct iss_video *video)
1253 media_entity_cleanup(&video->video.entity);
1254 mutex_destroy(&video->stream_lock);
1255 mutex_destroy(&video->mutex);
1258 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1262 video->video.v4l2_dev = vdev;
1264 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1266 dev_err(video->iss->dev,
1267 "could not register video device (%d)\n", ret);
1272 void omap4iss_video_unregister(struct iss_video *video)
1274 video_unregister_device(&video->video);