1 // SPDX-License-Identifier: GPL-2.0+
3 * TI OMAP4 ISS V4L2 Driver - Generic video node
5 * Copyright (C) 2012 Texas Instruments, Inc.
7 * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
10 #include <linux/clk.h>
12 #include <linux/pagemap.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/module.h>
18 #include <media/v4l2-dev.h>
19 #include <media/v4l2-ioctl.h>
20 #include <media/v4l2-mc.h>
22 #include <asm/cacheflush.h>
24 #include "iss_video.h"
27 /* -----------------------------------------------------------------------------
31 static struct iss_format_info formats[] = {
32 { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
33 MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
34 V4L2_PIX_FMT_GREY, 8, },
35 { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
36 MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
37 V4L2_PIX_FMT_Y10, 10, },
38 { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
39 MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
40 V4L2_PIX_FMT_Y12, 12, },
41 { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
42 MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
43 V4L2_PIX_FMT_SBGGR8, 8, },
44 { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
45 MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
46 V4L2_PIX_FMT_SGBRG8, 8, },
47 { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
48 MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
49 V4L2_PIX_FMT_SGRBG8, 8, },
50 { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
51 MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
52 V4L2_PIX_FMT_SRGGB8, 8, },
53 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
54 MEDIA_BUS_FMT_SGRBG10_1X10, 0,
55 V4L2_PIX_FMT_SGRBG10DPCM8, 8, },
56 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
57 MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
58 V4L2_PIX_FMT_SBGGR10, 10, },
59 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
60 MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
61 V4L2_PIX_FMT_SGBRG10, 10, },
62 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
63 MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
64 V4L2_PIX_FMT_SGRBG10, 10, },
65 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
66 MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
67 V4L2_PIX_FMT_SRGGB10, 10, },
68 { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
69 MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
70 V4L2_PIX_FMT_SBGGR12, 12, },
71 { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
72 MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
73 V4L2_PIX_FMT_SGBRG12, 12, },
74 { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
75 MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
76 V4L2_PIX_FMT_SGRBG12, 12, },
77 { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
78 MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
79 V4L2_PIX_FMT_SRGGB12, 12, },
80 { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
81 MEDIA_BUS_FMT_UYVY8_1X16, 0,
82 V4L2_PIX_FMT_UYVY, 16, },
83 { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
84 MEDIA_BUS_FMT_YUYV8_1X16, 0,
85 V4L2_PIX_FMT_YUYV, 16, },
86 { MEDIA_BUS_FMT_YUYV8_1_5X8, MEDIA_BUS_FMT_YUYV8_1_5X8,
87 MEDIA_BUS_FMT_YUYV8_1_5X8, 0,
88 V4L2_PIX_FMT_NV12, 8, },
91 const struct iss_format_info *
92 omap4iss_video_format_info(u32 code)
96 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
97 if (formats[i].code == code)
105 * iss_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
106 * @video: ISS video instance
107 * @mbus: v4l2_mbus_framefmt format (input)
108 * @pix: v4l2_pix_format format (output)
110 * Fill the output pix structure with information from the input mbus format.
111 * The bytesperline and sizeimage fields are computed from the requested bytes
112 * per line value in the pix format and information from the video instance.
114 * Return the number of padding bytes at end of line.
116 static unsigned int iss_video_mbus_to_pix(const struct iss_video *video,
117 const struct v4l2_mbus_framefmt *mbus,
118 struct v4l2_pix_format *pix)
120 unsigned int bpl = pix->bytesperline;
121 unsigned int min_bpl;
124 memset(pix, 0, sizeof(*pix));
125 pix->width = mbus->width;
126 pix->height = mbus->height;
129 * Skip the last format in the loop so that it will be selected if no
132 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
133 if (formats[i].code == mbus->code)
137 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
140 * Clamp the requested bytes per line value. If the maximum bytes per
141 * line value is zero, the module doesn't support user configurable line
142 * sizes. Override the requested value with the minimum in that case.
145 bpl = clamp(bpl, min_bpl, video->bpl_max);
149 if (!video->bpl_zero_padding || bpl != min_bpl)
150 bpl = ALIGN(bpl, video->bpl_alignment);
152 pix->pixelformat = formats[i].pixelformat;
153 pix->bytesperline = bpl;
154 pix->sizeimage = pix->bytesperline * pix->height;
155 pix->colorspace = mbus->colorspace;
156 pix->field = mbus->field;
158 /* FIXME: Special case for NV12! We should make this nicer... */
159 if (pix->pixelformat == V4L2_PIX_FMT_NV12)
160 pix->sizeimage += (pix->bytesperline * pix->height) / 2;
162 return bpl - min_bpl;
165 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
166 struct v4l2_mbus_framefmt *mbus)
170 memset(mbus, 0, sizeof(*mbus));
171 mbus->width = pix->width;
172 mbus->height = pix->height;
175 * Skip the last format in the loop so that it will be selected if no
178 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
179 if (formats[i].pixelformat == pix->pixelformat)
183 mbus->code = formats[i].code;
184 mbus->colorspace = pix->colorspace;
185 mbus->field = pix->field;
188 static struct v4l2_subdev *
189 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
191 struct media_pad *remote;
193 remote = media_entity_remote_pad(&video->pad);
195 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
199 *pad = remote->index;
201 return media_entity_to_v4l2_subdev(remote->entity);
204 /* Return a pointer to the ISS video instance at the far end of the pipeline. */
205 static struct iss_video *
206 iss_video_far_end(struct iss_video *video)
208 struct media_graph graph;
209 struct media_entity *entity = &video->video.entity;
210 struct media_device *mdev = entity->graph_obj.mdev;
211 struct iss_video *far_end = NULL;
213 mutex_lock(&mdev->graph_mutex);
215 if (media_graph_walk_init(&graph, mdev)) {
216 mutex_unlock(&mdev->graph_mutex);
220 media_graph_walk_start(&graph, entity);
222 while ((entity = media_graph_walk_next(&graph))) {
223 if (entity == &video->video.entity)
226 if (!is_media_entity_v4l2_video_device(entity))
229 far_end = to_iss_video(media_entity_to_video_device(entity));
230 if (far_end->type != video->type)
236 mutex_unlock(&mdev->graph_mutex);
238 media_graph_walk_cleanup(&graph);
244 __iss_video_get_format(struct iss_video *video,
245 struct v4l2_mbus_framefmt *format)
247 struct v4l2_subdev_format fmt;
248 struct v4l2_subdev *subdev;
252 subdev = iss_video_remote_subdev(video, &pad);
256 memset(&fmt, 0, sizeof(fmt));
258 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
260 mutex_lock(&video->mutex);
261 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
262 mutex_unlock(&video->mutex);
267 *format = fmt.format;
272 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
274 struct v4l2_mbus_framefmt format;
275 struct v4l2_pix_format pixfmt;
278 ret = __iss_video_get_format(video, &format);
282 pixfmt.bytesperline = 0;
283 ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
285 if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat ||
286 vfh->format.fmt.pix.height != pixfmt.height ||
287 vfh->format.fmt.pix.width != pixfmt.width ||
288 vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline ||
289 vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage)
295 /* -----------------------------------------------------------------------------
296 * Video queue operations
299 static int iss_video_queue_setup(struct vb2_queue *vq,
300 unsigned int *count, unsigned int *num_planes,
301 unsigned int sizes[],
302 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);
365 * Mark the buffer is faulty and give it back to the queue immediately
366 * if the video node has registered an error. vb2 will perform the same
367 * check when preparing the buffer, but that is inherently racy, so we
368 * need to handle the race condition with an authoritative check here.
370 if (unlikely(video->error)) {
371 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
372 spin_unlock_irqrestore(&video->qlock, flags);
376 empty = list_empty(&video->dmaqueue);
377 list_add_tail(&buffer->list, &video->dmaqueue);
379 spin_unlock_irqrestore(&video->qlock, flags);
382 enum iss_pipeline_state state;
385 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
386 state = ISS_PIPELINE_QUEUE_OUTPUT;
388 state = ISS_PIPELINE_QUEUE_INPUT;
390 spin_lock_irqsave(&pipe->lock, flags);
391 pipe->state |= state;
392 video->ops->queue(video, buffer);
393 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
395 start = iss_pipeline_ready(pipe);
397 pipe->state |= ISS_PIPELINE_STREAM;
398 spin_unlock_irqrestore(&pipe->lock, flags);
401 omap4iss_pipeline_set_stream(pipe,
402 ISS_PIPELINE_STREAM_SINGLESHOT);
406 static const struct vb2_ops iss_video_vb2ops = {
407 .queue_setup = iss_video_queue_setup,
408 .buf_prepare = iss_video_buf_prepare,
409 .buf_queue = iss_video_buf_queue,
410 .buf_cleanup = iss_video_buf_cleanup,
414 * omap4iss_video_buffer_next - Complete the current buffer and return the next
415 * @video: ISS video object
417 * Remove the current video buffer from the DMA queue and fill its timestamp,
418 * field count and state fields before waking up its completion handler.
420 * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
421 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
423 * The DMA queue is expected to contain at least one buffer.
425 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
428 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
430 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
431 enum iss_pipeline_state state;
432 struct iss_buffer *buf;
435 spin_lock_irqsave(&video->qlock, flags);
436 if (WARN_ON(list_empty(&video->dmaqueue))) {
437 spin_unlock_irqrestore(&video->qlock, flags);
441 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
443 list_del(&buf->list);
444 spin_unlock_irqrestore(&video->qlock, flags);
446 buf->vb.vb2_buf.timestamp = ktime_get_ns();
449 * Do frame number propagation only if this is the output video node.
450 * Frame number either comes from the CSI receivers or it gets
451 * incremented here if H3A is not active.
452 * Note: There is no guarantee that the output buffer will finish
453 * first, so the input number might lag behind by 1 in some cases.
455 if (video == pipe->output && !pipe->do_propagation)
457 atomic_inc_return(&pipe->frame_number);
459 buf->vb.sequence = atomic_read(&pipe->frame_number);
461 vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
462 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
465 spin_lock_irqsave(&video->qlock, flags);
466 if (list_empty(&video->dmaqueue)) {
467 spin_unlock_irqrestore(&video->qlock, flags);
468 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
469 state = ISS_PIPELINE_QUEUE_OUTPUT
470 | ISS_PIPELINE_STREAM;
472 state = ISS_PIPELINE_QUEUE_INPUT
473 | ISS_PIPELINE_STREAM;
475 spin_lock_irqsave(&pipe->lock, flags);
476 pipe->state &= ~state;
477 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
478 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
479 spin_unlock_irqrestore(&pipe->lock, flags);
483 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) {
484 spin_lock(&pipe->lock);
485 pipe->state &= ~ISS_PIPELINE_STREAM;
486 spin_unlock(&pipe->lock);
489 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
491 spin_unlock_irqrestore(&video->qlock, flags);
492 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
497 * omap4iss_video_cancel_stream - Cancel stream on a video node
498 * @video: ISS video object
500 * Cancelling a stream mark all buffers on the video node as erroneous and makes
501 * sure no new buffer can be queued.
503 void omap4iss_video_cancel_stream(struct iss_video *video)
507 spin_lock_irqsave(&video->qlock, flags);
509 while (!list_empty(&video->dmaqueue)) {
510 struct iss_buffer *buf;
512 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
514 list_del(&buf->list);
515 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
518 vb2_queue_error(video->queue);
521 spin_unlock_irqrestore(&video->qlock, flags);
524 /* -----------------------------------------------------------------------------
529 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
531 struct iss_video *video = video_drvdata(file);
533 strscpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
534 strscpy(cap->card, video->video.name, sizeof(cap->card));
535 strscpy(cap->bus_info, "media", sizeof(cap->bus_info));
536 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
537 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
543 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
545 struct iss_video *video = video_drvdata(file);
546 struct v4l2_mbus_framefmt format;
547 unsigned int index = f->index;
551 if (f->type != video->type)
554 ret = __iss_video_get_format(video, &format);
558 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
559 const struct iss_format_info *info = &formats[i];
561 if (format.code != info->code)
565 f->pixelformat = info->pixelformat;
576 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
578 struct iss_video_fh *vfh = to_iss_video_fh(fh);
579 struct iss_video *video = video_drvdata(file);
581 if (format->type != video->type)
584 mutex_lock(&video->mutex);
585 *format = vfh->format;
586 mutex_unlock(&video->mutex);
592 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
594 struct iss_video_fh *vfh = to_iss_video_fh(fh);
595 struct iss_video *video = video_drvdata(file);
596 struct v4l2_mbus_framefmt fmt;
598 if (format->type != video->type)
601 mutex_lock(&video->mutex);
604 * Fill the bytesperline and sizeimage fields by converting to media bus
605 * format and back to pixel format.
607 iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
608 iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
610 vfh->format = *format;
612 mutex_unlock(&video->mutex);
617 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
619 struct iss_video *video = video_drvdata(file);
620 struct v4l2_subdev_format fmt;
621 struct v4l2_subdev *subdev;
625 if (format->type != video->type)
628 subdev = iss_video_remote_subdev(video, &pad);
632 iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
635 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
636 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
640 iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
645 iss_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
647 struct iss_video *video = video_drvdata(file);
648 struct v4l2_subdev_format format;
649 struct v4l2_subdev *subdev;
650 struct v4l2_subdev_selection sdsel = {
651 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
652 .target = sel->target,
657 switch (sel->target) {
658 case V4L2_SEL_TGT_CROP:
659 case V4L2_SEL_TGT_CROP_BOUNDS:
660 case V4L2_SEL_TGT_CROP_DEFAULT:
661 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
664 case V4L2_SEL_TGT_COMPOSE:
665 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
666 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
667 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
673 subdev = iss_video_remote_subdev(video, &pad);
678 * Try the get selection operation first and fallback to get format if
682 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
685 if (ret != -ENOIOCTLCMD)
689 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
690 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
692 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
696 sel->r.width = format.format.width;
697 sel->r.height = format.format.height;
703 iss_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
705 struct iss_video *video = video_drvdata(file);
706 struct v4l2_subdev *subdev;
707 struct v4l2_subdev_selection sdsel = {
708 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
709 .target = sel->target,
716 switch (sel->target) {
717 case V4L2_SEL_TGT_CROP:
718 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
721 case V4L2_SEL_TGT_COMPOSE:
722 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
728 subdev = iss_video_remote_subdev(video, &pad);
733 mutex_lock(&video->mutex);
734 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
735 mutex_unlock(&video->mutex);
739 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
743 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
745 struct iss_video_fh *vfh = to_iss_video_fh(fh);
746 struct iss_video *video = video_drvdata(file);
748 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
749 video->type != a->type)
752 memset(a, 0, sizeof(*a));
753 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
754 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
755 a->parm.output.timeperframe = vfh->timeperframe;
761 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
763 struct iss_video_fh *vfh = to_iss_video_fh(fh);
764 struct iss_video *video = video_drvdata(file);
766 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
767 video->type != a->type)
770 if (a->parm.output.timeperframe.denominator == 0)
771 a->parm.output.timeperframe.denominator = 1;
773 vfh->timeperframe = a->parm.output.timeperframe;
779 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
781 struct iss_video_fh *vfh = to_iss_video_fh(fh);
783 return vb2_reqbufs(&vfh->queue, rb);
787 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
789 struct iss_video_fh *vfh = to_iss_video_fh(fh);
791 return vb2_querybuf(&vfh->queue, b);
795 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
797 struct iss_video *video = video_drvdata(file);
798 struct iss_video_fh *vfh = to_iss_video_fh(fh);
800 return vb2_qbuf(&vfh->queue, video->video.v4l2_dev->mdev, b);
804 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
806 struct iss_video_fh *vfh = to_iss_video_fh(fh);
808 return vb2_expbuf(&vfh->queue, e);
812 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
814 struct iss_video_fh *vfh = to_iss_video_fh(fh);
816 return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
822 * Every ISS pipeline has a single input and a single output. The input can be
823 * either a sensor or a video node. The output is always a video node.
825 * As every pipeline has an output video node, the ISS video objects at the
826 * pipeline output stores the pipeline state. It tracks the streaming state of
827 * both the input and output, as well as the availability of buffers.
829 * In sensor-to-memory mode, frames are always available at the pipeline input.
830 * Starting the sensor usually requires I2C transfers and must be done in
831 * interruptible context. The pipeline is started and stopped synchronously
832 * to the stream on/off commands. All modules in the pipeline will get their
833 * subdev set stream handler called. The module at the end of the pipeline must
834 * delay starting the hardware until buffers are available at its output.
836 * In memory-to-memory mode, starting/stopping the stream requires
837 * synchronization between the input and output. ISS modules can't be stopped
838 * in the middle of a frame, and at least some of the modules seem to become
839 * busy as soon as they're started, even if they don't receive a frame start
840 * event. For that reason frames need to be processed in single-shot mode. The
841 * driver needs to wait until a frame is completely processed and written to
842 * memory before restarting the pipeline for the next frame. Pipelined
843 * processing might be possible but requires more testing.
845 * Stream start must be delayed until buffers are available at both the input
846 * and output. The pipeline must be started in the videobuf queue callback with
847 * the buffers queue spinlock held. The modules subdev set stream operation must
851 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
853 struct iss_video_fh *vfh = to_iss_video_fh(fh);
854 struct iss_video *video = video_drvdata(file);
855 struct media_graph graph;
856 struct media_entity *entity = &video->video.entity;
857 struct media_device *mdev = entity->graph_obj.mdev;
858 enum iss_pipeline_state state;
859 struct iss_pipeline *pipe;
860 struct iss_video *far_end;
864 if (type != video->type)
867 mutex_lock(&video->stream_lock);
870 * Start streaming on the pipeline. No link touching an entity in the
871 * pipeline can be activated or deactivated once streaming is started.
874 ? to_iss_pipeline(entity) : &video->pipe;
875 pipe->external = NULL;
876 pipe->external_rate = 0;
877 pipe->external_bpp = 0;
879 ret = media_entity_enum_init(&pipe->ent_enum, entity->graph_obj.mdev);
881 goto err_graph_walk_init;
883 ret = media_graph_walk_init(&graph, entity->graph_obj.mdev);
885 goto err_graph_walk_init;
887 if (video->iss->pdata->set_constraints)
888 video->iss->pdata->set_constraints(video->iss, true);
890 ret = media_pipeline_start(entity, &pipe->pipe);
892 goto err_media_pipeline_start;
894 mutex_lock(&mdev->graph_mutex);
895 media_graph_walk_start(&graph, entity);
896 while ((entity = media_graph_walk_next(&graph)))
897 media_entity_enum_set(&pipe->ent_enum, entity);
898 mutex_unlock(&mdev->graph_mutex);
901 * Verify that the currently configured format matches the output of
902 * the connected subdev.
904 ret = iss_video_check_format(video, vfh);
906 goto err_iss_video_check_format;
908 video->bpl_padding = ret;
909 video->bpl_value = vfh->format.fmt.pix.bytesperline;
912 * Find the ISS video node connected at the far end of the pipeline and
913 * update the pipeline.
915 far_end = iss_video_far_end(video);
917 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
918 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
919 pipe->input = far_end;
920 pipe->output = video;
924 goto err_iss_video_check_format;
927 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
929 pipe->output = far_end;
932 spin_lock_irqsave(&pipe->lock, flags);
933 pipe->state &= ~ISS_PIPELINE_STREAM;
934 pipe->state |= state;
935 spin_unlock_irqrestore(&pipe->lock, flags);
938 * Set the maximum time per frame as the value requested by userspace.
939 * This is a soft limit that can be overridden if the hardware doesn't
940 * support the request limit.
942 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
943 pipe->max_timeperframe = vfh->timeperframe;
945 video->queue = &vfh->queue;
946 INIT_LIST_HEAD(&video->dmaqueue);
947 video->error = false;
948 atomic_set(&pipe->frame_number, -1);
950 ret = vb2_streamon(&vfh->queue, type);
952 goto err_iss_video_check_format;
955 * In sensor-to-memory mode, the stream can be started synchronously
956 * to the stream on command. In memory-to-memory mode, it will be
957 * started when buffers are queued on both the input and output.
962 ret = omap4iss_pipeline_set_stream(pipe,
963 ISS_PIPELINE_STREAM_CONTINUOUS);
965 goto err_omap4iss_set_stream;
966 spin_lock_irqsave(&video->qlock, flags);
967 if (list_empty(&video->dmaqueue))
968 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
969 spin_unlock_irqrestore(&video->qlock, flags);
972 media_graph_walk_cleanup(&graph);
974 mutex_unlock(&video->stream_lock);
978 err_omap4iss_set_stream:
979 vb2_streamoff(&vfh->queue, type);
980 err_iss_video_check_format:
981 media_pipeline_stop(&video->video.entity);
982 err_media_pipeline_start:
983 if (video->iss->pdata->set_constraints)
984 video->iss->pdata->set_constraints(video->iss, false);
987 media_graph_walk_cleanup(&graph);
990 media_entity_enum_cleanup(&pipe->ent_enum);
992 mutex_unlock(&video->stream_lock);
998 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1000 struct iss_video_fh *vfh = to_iss_video_fh(fh);
1001 struct iss_video *video = video_drvdata(file);
1002 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
1003 enum iss_pipeline_state state;
1004 unsigned long flags;
1006 if (type != video->type)
1009 mutex_lock(&video->stream_lock);
1011 if (!vb2_is_streaming(&vfh->queue))
1014 /* Update the pipeline state. */
1015 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1016 state = ISS_PIPELINE_STREAM_OUTPUT
1017 | ISS_PIPELINE_QUEUE_OUTPUT;
1019 state = ISS_PIPELINE_STREAM_INPUT
1020 | ISS_PIPELINE_QUEUE_INPUT;
1022 spin_lock_irqsave(&pipe->lock, flags);
1023 pipe->state &= ~state;
1024 spin_unlock_irqrestore(&pipe->lock, flags);
1026 /* Stop the stream. */
1027 omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
1028 vb2_streamoff(&vfh->queue, type);
1029 video->queue = NULL;
1031 media_entity_enum_cleanup(&pipe->ent_enum);
1033 if (video->iss->pdata->set_constraints)
1034 video->iss->pdata->set_constraints(video->iss, false);
1035 media_pipeline_stop(&video->video.entity);
1038 mutex_unlock(&video->stream_lock);
1043 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1045 if (input->index > 0)
1048 strscpy(input->name, "camera", sizeof(input->name));
1049 input->type = V4L2_INPUT_TYPE_CAMERA;
1055 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
1063 iss_video_s_input(struct file *file, void *fh, unsigned int input)
1065 return input == 0 ? 0 : -EINVAL;
1068 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
1069 .vidioc_querycap = iss_video_querycap,
1070 .vidioc_enum_fmt_vid_cap = iss_video_enum_format,
1071 .vidioc_g_fmt_vid_cap = iss_video_get_format,
1072 .vidioc_s_fmt_vid_cap = iss_video_set_format,
1073 .vidioc_try_fmt_vid_cap = iss_video_try_format,
1074 .vidioc_g_fmt_vid_out = iss_video_get_format,
1075 .vidioc_s_fmt_vid_out = iss_video_set_format,
1076 .vidioc_try_fmt_vid_out = iss_video_try_format,
1077 .vidioc_g_selection = iss_video_get_selection,
1078 .vidioc_s_selection = iss_video_set_selection,
1079 .vidioc_g_parm = iss_video_get_param,
1080 .vidioc_s_parm = iss_video_set_param,
1081 .vidioc_reqbufs = iss_video_reqbufs,
1082 .vidioc_querybuf = iss_video_querybuf,
1083 .vidioc_qbuf = iss_video_qbuf,
1084 .vidioc_expbuf = iss_video_expbuf,
1085 .vidioc_dqbuf = iss_video_dqbuf,
1086 .vidioc_streamon = iss_video_streamon,
1087 .vidioc_streamoff = iss_video_streamoff,
1088 .vidioc_enum_input = iss_video_enum_input,
1089 .vidioc_g_input = iss_video_g_input,
1090 .vidioc_s_input = iss_video_s_input,
1093 /* -----------------------------------------------------------------------------
1094 * V4L2 file operations
1097 static int iss_video_open(struct file *file)
1099 struct iss_video *video = video_drvdata(file);
1100 struct iss_video_fh *handle;
1101 struct vb2_queue *q;
1104 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1108 v4l2_fh_init(&handle->vfh, &video->video);
1109 v4l2_fh_add(&handle->vfh);
1111 /* If this is the first user, initialise the pipeline. */
1112 if (!omap4iss_get(video->iss)) {
1117 ret = v4l2_pipeline_pm_get(&video->video.entity);
1119 omap4iss_put(video->iss);
1125 q->type = video->type;
1126 q->io_modes = VB2_MMAP | VB2_DMABUF;
1127 q->drv_priv = handle;
1128 q->ops = &iss_video_vb2ops;
1129 q->mem_ops = &vb2_dma_contig_memops;
1130 q->buf_struct_size = sizeof(struct iss_buffer);
1131 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1132 q->dev = video->iss->dev;
1134 ret = vb2_queue_init(q);
1136 omap4iss_put(video->iss);
1140 memset(&handle->format, 0, sizeof(handle->format));
1141 handle->format.type = video->type;
1142 handle->timeperframe.denominator = 1;
1144 handle->video = video;
1145 file->private_data = &handle->vfh;
1149 v4l2_fh_del(&handle->vfh);
1150 v4l2_fh_exit(&handle->vfh);
1157 static int iss_video_release(struct file *file)
1159 struct iss_video *video = video_drvdata(file);
1160 struct v4l2_fh *vfh = file->private_data;
1161 struct iss_video_fh *handle = to_iss_video_fh(vfh);
1163 /* Disable streaming and free the buffers queue resources. */
1164 iss_video_streamoff(file, vfh, video->type);
1166 v4l2_pipeline_pm_put(&video->video.entity);
1168 /* Release the videobuf2 queue */
1169 vb2_queue_release(&handle->queue);
1174 file->private_data = NULL;
1176 omap4iss_put(video->iss);
1181 static __poll_t iss_video_poll(struct file *file, poll_table *wait)
1183 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1185 return vb2_poll(&vfh->queue, file, wait);
1188 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1190 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1192 return vb2_mmap(&vfh->queue, vma);
1195 static const struct v4l2_file_operations iss_video_fops = {
1196 .owner = THIS_MODULE,
1197 .unlocked_ioctl = video_ioctl2,
1198 .open = iss_video_open,
1199 .release = iss_video_release,
1200 .poll = iss_video_poll,
1201 .mmap = iss_video_mmap,
1204 /* -----------------------------------------------------------------------------
1208 static const struct iss_video_operations iss_video_dummy_ops = {
1211 int omap4iss_video_init(struct iss_video *video, const char *name)
1213 const char *direction;
1216 switch (video->type) {
1217 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1218 direction = "output";
1219 video->pad.flags = MEDIA_PAD_FL_SINK;
1221 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1222 direction = "input";
1223 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1230 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1234 spin_lock_init(&video->qlock);
1235 mutex_init(&video->mutex);
1236 atomic_set(&video->active, 0);
1238 spin_lock_init(&video->pipe.lock);
1239 mutex_init(&video->stream_lock);
1241 /* Initialize the video device. */
1243 video->ops = &iss_video_dummy_ops;
1245 video->video.fops = &iss_video_fops;
1246 snprintf(video->video.name, sizeof(video->video.name),
1247 "OMAP4 ISS %s %s", name, direction);
1248 video->video.vfl_type = VFL_TYPE_VIDEO;
1249 video->video.release = video_device_release_empty;
1250 video->video.ioctl_ops = &iss_video_ioctl_ops;
1251 video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1253 video_set_drvdata(&video->video, video);
1258 void omap4iss_video_cleanup(struct iss_video *video)
1260 media_entity_cleanup(&video->video.entity);
1261 mutex_destroy(&video->stream_lock);
1262 mutex_destroy(&video->mutex);
1265 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1269 video->video.v4l2_dev = vdev;
1270 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1271 video->video.device_caps = V4L2_CAP_VIDEO_CAPTURE;
1273 video->video.device_caps = V4L2_CAP_VIDEO_OUTPUT;
1274 video->video.device_caps |= V4L2_CAP_STREAMING;
1276 ret = video_register_device(&video->video, VFL_TYPE_VIDEO, -1);
1278 dev_err(video->iss->dev,
1279 "could not register video device (%d)\n", ret);
1284 void omap4iss_video_unregister(struct iss_video *video)
1286 video_unregister_device(&video->video);