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 "iss_video.h"
25 /* -----------------------------------------------------------------------------
29 static struct iss_format_info formats[] = {
30 { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
31 MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
32 V4L2_PIX_FMT_GREY, 8, },
33 { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
34 MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
35 V4L2_PIX_FMT_Y10, 10, },
36 { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
37 MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
38 V4L2_PIX_FMT_Y12, 12, },
39 { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
40 MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
41 V4L2_PIX_FMT_SBGGR8, 8, },
42 { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
43 MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
44 V4L2_PIX_FMT_SGBRG8, 8, },
45 { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
46 MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
47 V4L2_PIX_FMT_SGRBG8, 8, },
48 { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
49 MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
50 V4L2_PIX_FMT_SRGGB8, 8, },
51 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
52 MEDIA_BUS_FMT_SGRBG10_1X10, 0,
53 V4L2_PIX_FMT_SGRBG10DPCM8, 8, },
54 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
55 MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
56 V4L2_PIX_FMT_SBGGR10, 10, },
57 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
58 MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
59 V4L2_PIX_FMT_SGBRG10, 10, },
60 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
61 MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
62 V4L2_PIX_FMT_SGRBG10, 10, },
63 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
64 MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
65 V4L2_PIX_FMT_SRGGB10, 10, },
66 { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
67 MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
68 V4L2_PIX_FMT_SBGGR12, 12, },
69 { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
70 MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
71 V4L2_PIX_FMT_SGBRG12, 12, },
72 { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
73 MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
74 V4L2_PIX_FMT_SGRBG12, 12, },
75 { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
76 MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
77 V4L2_PIX_FMT_SRGGB12, 12, },
78 { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
79 MEDIA_BUS_FMT_UYVY8_1X16, 0,
80 V4L2_PIX_FMT_UYVY, 16, },
81 { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
82 MEDIA_BUS_FMT_YUYV8_1X16, 0,
83 V4L2_PIX_FMT_YUYV, 16, },
84 { MEDIA_BUS_FMT_YUYV8_1_5X8, MEDIA_BUS_FMT_YUYV8_1_5X8,
85 MEDIA_BUS_FMT_YUYV8_1_5X8, 0,
86 V4L2_PIX_FMT_NV12, 8, },
89 const struct iss_format_info *
90 omap4iss_video_format_info(u32 code)
94 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
95 if (formats[i].code == code)
103 * iss_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
104 * @video: ISS video instance
105 * @mbus: v4l2_mbus_framefmt format (input)
106 * @pix: v4l2_pix_format format (output)
108 * Fill the output pix structure with information from the input mbus format.
109 * The bytesperline and sizeimage fields are computed from the requested bytes
110 * per line value in the pix format and information from the video instance.
112 * Return the number of padding bytes at end of line.
114 static unsigned int iss_video_mbus_to_pix(const struct iss_video *video,
115 const struct v4l2_mbus_framefmt *mbus,
116 struct v4l2_pix_format *pix)
118 unsigned int bpl = pix->bytesperline;
119 unsigned int min_bpl;
122 memset(pix, 0, sizeof(*pix));
123 pix->width = mbus->width;
124 pix->height = mbus->height;
127 * Skip the last format in the loop so that it will be selected if no
130 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
131 if (formats[i].code == mbus->code)
135 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
138 * Clamp the requested bytes per line value. If the maximum bytes per
139 * line value is zero, the module doesn't support user configurable line
140 * sizes. Override the requested value with the minimum in that case.
143 bpl = clamp(bpl, min_bpl, video->bpl_max);
147 if (!video->bpl_zero_padding || bpl != min_bpl)
148 bpl = ALIGN(bpl, video->bpl_alignment);
150 pix->pixelformat = formats[i].pixelformat;
151 pix->bytesperline = bpl;
152 pix->sizeimage = pix->bytesperline * pix->height;
153 pix->colorspace = mbus->colorspace;
154 pix->field = mbus->field;
156 /* FIXME: Special case for NV12! We should make this nicer... */
157 if (pix->pixelformat == V4L2_PIX_FMT_NV12)
158 pix->sizeimage += (pix->bytesperline * pix->height) / 2;
160 return bpl - min_bpl;
163 static void iss_video_pix_to_mbus(const struct v4l2_pix_format *pix,
164 struct v4l2_mbus_framefmt *mbus)
168 memset(mbus, 0, sizeof(*mbus));
169 mbus->width = pix->width;
170 mbus->height = pix->height;
173 * Skip the last format in the loop so that it will be selected if no
176 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
177 if (formats[i].pixelformat == pix->pixelformat)
181 mbus->code = formats[i].code;
182 mbus->colorspace = pix->colorspace;
183 mbus->field = pix->field;
186 static struct v4l2_subdev *
187 iss_video_remote_subdev(struct iss_video *video, u32 *pad)
189 struct media_pad *remote;
191 remote = media_pad_remote_pad_first(&video->pad);
193 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
197 *pad = remote->index;
199 return media_entity_to_v4l2_subdev(remote->entity);
202 /* Return a pointer to the ISS video instance at the far end of the pipeline. */
203 static struct iss_video *
204 iss_video_far_end(struct iss_video *video, struct iss_pipeline *pipe)
206 struct media_pipeline_entity_iter iter;
207 struct media_entity *entity;
208 struct iss_video *far_end = NULL;
211 ret = media_pipeline_entity_iter_init(&pipe->pipe, &iter);
213 return ERR_PTR(-ENOMEM);
215 media_pipeline_for_each_entity(&pipe->pipe, &iter, entity) {
216 struct iss_video *other;
218 if (entity == &video->video.entity)
221 if (!is_media_entity_v4l2_video_device(entity))
224 other = to_iss_video(media_entity_to_video_device(entity));
225 if (other->type != video->type) {
231 media_pipeline_entity_iter_cleanup(&iter);
237 __iss_video_get_format(struct iss_video *video,
238 struct v4l2_mbus_framefmt *format)
240 struct v4l2_subdev_format fmt = {
241 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
243 struct v4l2_subdev *subdev;
247 subdev = iss_video_remote_subdev(video, &pad);
253 mutex_lock(&video->mutex);
254 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
255 mutex_unlock(&video->mutex);
260 *format = fmt.format;
265 iss_video_check_format(struct iss_video *video, struct iss_video_fh *vfh)
267 struct v4l2_mbus_framefmt format;
268 struct v4l2_pix_format pixfmt;
271 ret = __iss_video_get_format(video, &format);
275 pixfmt.bytesperline = 0;
276 ret = iss_video_mbus_to_pix(video, &format, &pixfmt);
278 if (vfh->format.fmt.pix.pixelformat != pixfmt.pixelformat ||
279 vfh->format.fmt.pix.height != pixfmt.height ||
280 vfh->format.fmt.pix.width != pixfmt.width ||
281 vfh->format.fmt.pix.bytesperline != pixfmt.bytesperline ||
282 vfh->format.fmt.pix.sizeimage != pixfmt.sizeimage)
288 /* -----------------------------------------------------------------------------
289 * Video queue operations
292 static int iss_video_queue_setup(struct vb2_queue *vq,
293 unsigned int *count, unsigned int *num_planes,
294 unsigned int sizes[],
295 struct device *alloc_devs[])
297 struct iss_video_fh *vfh = vb2_get_drv_priv(vq);
298 struct iss_video *video = vfh->video;
300 /* Revisit multi-planar support for NV12 */
303 sizes[0] = vfh->format.fmt.pix.sizeimage;
307 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
312 static void iss_video_buf_cleanup(struct vb2_buffer *vb)
314 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
315 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
317 if (buffer->iss_addr)
318 buffer->iss_addr = 0;
321 static int iss_video_buf_prepare(struct vb2_buffer *vb)
323 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
324 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
325 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
326 struct iss_video *video = vfh->video;
327 unsigned long size = vfh->format.fmt.pix.sizeimage;
330 if (vb2_plane_size(vb, 0) < size)
333 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
334 if (!IS_ALIGNED(addr, 32)) {
335 dev_dbg(video->iss->dev,
336 "Buffer address must be aligned to 32 bytes boundary.\n");
340 vb2_set_plane_payload(vb, 0, size);
341 buffer->iss_addr = addr;
345 static void iss_video_buf_queue(struct vb2_buffer *vb)
347 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
348 struct iss_video_fh *vfh = vb2_get_drv_priv(vb->vb2_queue);
349 struct iss_video *video = vfh->video;
350 struct iss_buffer *buffer = container_of(vbuf, struct iss_buffer, vb);
351 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
355 spin_lock_irqsave(&video->qlock, flags);
358 * Mark the buffer is faulty and give it back to the queue immediately
359 * if the video node has registered an error. vb2 will perform the same
360 * check when preparing the buffer, but that is inherently racy, so we
361 * need to handle the race condition with an authoritative check here.
363 if (unlikely(video->error)) {
364 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
365 spin_unlock_irqrestore(&video->qlock, flags);
369 empty = list_empty(&video->dmaqueue);
370 list_add_tail(&buffer->list, &video->dmaqueue);
372 spin_unlock_irqrestore(&video->qlock, flags);
375 enum iss_pipeline_state state;
378 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
379 state = ISS_PIPELINE_QUEUE_OUTPUT;
381 state = ISS_PIPELINE_QUEUE_INPUT;
383 spin_lock_irqsave(&pipe->lock, flags);
384 pipe->state |= state;
385 video->ops->queue(video, buffer);
386 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_QUEUED;
388 start = iss_pipeline_ready(pipe);
390 pipe->state |= ISS_PIPELINE_STREAM;
391 spin_unlock_irqrestore(&pipe->lock, flags);
394 omap4iss_pipeline_set_stream(pipe,
395 ISS_PIPELINE_STREAM_SINGLESHOT);
399 static const struct vb2_ops iss_video_vb2ops = {
400 .queue_setup = iss_video_queue_setup,
401 .buf_prepare = iss_video_buf_prepare,
402 .buf_queue = iss_video_buf_queue,
403 .buf_cleanup = iss_video_buf_cleanup,
407 * omap4iss_video_buffer_next - Complete the current buffer and return the next
408 * @video: ISS video object
410 * Remove the current video buffer from the DMA queue and fill its timestamp,
411 * field count and state fields before waking up its completion handler.
413 * For capture video nodes, the buffer state is set to VB2_BUF_STATE_DONE if no
414 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
416 * The DMA queue is expected to contain at least one buffer.
418 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
421 struct iss_buffer *omap4iss_video_buffer_next(struct iss_video *video)
423 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
424 enum iss_pipeline_state state;
425 struct iss_buffer *buf;
428 spin_lock_irqsave(&video->qlock, flags);
429 if (WARN_ON(list_empty(&video->dmaqueue))) {
430 spin_unlock_irqrestore(&video->qlock, flags);
434 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
436 list_del(&buf->list);
437 spin_unlock_irqrestore(&video->qlock, flags);
439 buf->vb.vb2_buf.timestamp = ktime_get_ns();
442 * Do frame number propagation only if this is the output video node.
443 * Frame number either comes from the CSI receivers or it gets
444 * incremented here if H3A is not active.
445 * Note: There is no guarantee that the output buffer will finish
446 * first, so the input number might lag behind by 1 in some cases.
448 if (video == pipe->output && !pipe->do_propagation)
450 atomic_inc_return(&pipe->frame_number);
452 buf->vb.sequence = atomic_read(&pipe->frame_number);
454 vb2_buffer_done(&buf->vb.vb2_buf, pipe->error ?
455 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
458 spin_lock_irqsave(&video->qlock, flags);
459 if (list_empty(&video->dmaqueue)) {
460 spin_unlock_irqrestore(&video->qlock, flags);
461 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
462 state = ISS_PIPELINE_QUEUE_OUTPUT
463 | ISS_PIPELINE_STREAM;
465 state = ISS_PIPELINE_QUEUE_INPUT
466 | ISS_PIPELINE_STREAM;
468 spin_lock_irqsave(&pipe->lock, flags);
469 pipe->state &= ~state;
470 if (video->pipe.stream_state == ISS_PIPELINE_STREAM_CONTINUOUS)
471 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
472 spin_unlock_irqrestore(&pipe->lock, flags);
476 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input) {
477 spin_lock(&pipe->lock);
478 pipe->state &= ~ISS_PIPELINE_STREAM;
479 spin_unlock(&pipe->lock);
482 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
484 spin_unlock_irqrestore(&video->qlock, flags);
485 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
490 * omap4iss_video_cancel_stream - Cancel stream on a video node
491 * @video: ISS video object
493 * Cancelling a stream mark all buffers on the video node as erroneous and makes
494 * sure no new buffer can be queued.
496 void omap4iss_video_cancel_stream(struct iss_video *video)
500 spin_lock_irqsave(&video->qlock, flags);
502 while (!list_empty(&video->dmaqueue)) {
503 struct iss_buffer *buf;
505 buf = list_first_entry(&video->dmaqueue, struct iss_buffer,
507 list_del(&buf->list);
508 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
511 vb2_queue_error(video->queue);
514 spin_unlock_irqrestore(&video->qlock, flags);
517 /* -----------------------------------------------------------------------------
522 iss_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
524 struct iss_video *video = video_drvdata(file);
526 strscpy(cap->driver, ISS_VIDEO_DRIVER_NAME, sizeof(cap->driver));
527 strscpy(cap->card, video->video.name, sizeof(cap->card));
528 strscpy(cap->bus_info, "media", sizeof(cap->bus_info));
529 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
530 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT;
536 iss_video_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
538 struct iss_video *video = video_drvdata(file);
539 struct v4l2_mbus_framefmt format;
540 unsigned int index = f->index;
544 if (f->type != video->type)
547 ret = __iss_video_get_format(video, &format);
551 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
552 const struct iss_format_info *info = &formats[i];
554 if (format.code != info->code)
558 f->pixelformat = info->pixelformat;
569 iss_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
571 struct iss_video_fh *vfh = to_iss_video_fh(fh);
572 struct iss_video *video = video_drvdata(file);
574 if (format->type != video->type)
577 mutex_lock(&video->mutex);
578 *format = vfh->format;
579 mutex_unlock(&video->mutex);
585 iss_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
587 struct iss_video_fh *vfh = to_iss_video_fh(fh);
588 struct iss_video *video = video_drvdata(file);
589 struct v4l2_mbus_framefmt fmt;
591 if (format->type != video->type)
594 mutex_lock(&video->mutex);
597 * Fill the bytesperline and sizeimage fields by converting to media bus
598 * format and back to pixel format.
600 iss_video_pix_to_mbus(&format->fmt.pix, &fmt);
601 iss_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
603 vfh->format = *format;
605 mutex_unlock(&video->mutex);
610 iss_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
612 struct iss_video *video = video_drvdata(file);
613 struct v4l2_subdev_format fmt = {
614 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
616 struct v4l2_subdev *subdev;
620 if (format->type != video->type)
623 subdev = iss_video_remote_subdev(video, &pad);
627 iss_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
630 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
634 iss_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
639 iss_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel)
641 struct iss_video *video = video_drvdata(file);
642 struct v4l2_subdev_format format = {
643 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
645 struct v4l2_subdev *subdev;
646 struct v4l2_subdev_selection sdsel = {
647 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
648 .target = sel->target,
653 switch (sel->target) {
654 case V4L2_SEL_TGT_CROP:
655 case V4L2_SEL_TGT_CROP_BOUNDS:
656 case V4L2_SEL_TGT_CROP_DEFAULT:
657 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
660 case V4L2_SEL_TGT_COMPOSE:
661 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
662 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
663 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
669 subdev = iss_video_remote_subdev(video, &pad);
674 * Try the get selection operation first and fallback to get format if
678 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel);
681 if (ret != -ENOIOCTLCMD)
685 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
687 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
691 sel->r.width = format.format.width;
692 sel->r.height = format.format.height;
698 iss_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel)
700 struct iss_video *video = video_drvdata(file);
701 struct v4l2_subdev *subdev;
702 struct v4l2_subdev_selection sdsel = {
703 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
704 .target = sel->target,
711 switch (sel->target) {
712 case V4L2_SEL_TGT_CROP:
713 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
716 case V4L2_SEL_TGT_COMPOSE:
717 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
723 subdev = iss_video_remote_subdev(video, &pad);
728 mutex_lock(&video->mutex);
729 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel);
730 mutex_unlock(&video->mutex);
734 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
738 iss_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
740 struct iss_video_fh *vfh = to_iss_video_fh(fh);
741 struct iss_video *video = video_drvdata(file);
743 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
744 video->type != a->type)
747 memset(a, 0, sizeof(*a));
748 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
749 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
750 a->parm.output.timeperframe = vfh->timeperframe;
756 iss_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
758 struct iss_video_fh *vfh = to_iss_video_fh(fh);
759 struct iss_video *video = video_drvdata(file);
761 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
762 video->type != a->type)
765 if (a->parm.output.timeperframe.denominator == 0)
766 a->parm.output.timeperframe.denominator = 1;
768 vfh->timeperframe = a->parm.output.timeperframe;
774 iss_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
776 struct iss_video_fh *vfh = to_iss_video_fh(fh);
778 return vb2_reqbufs(&vfh->queue, rb);
782 iss_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
784 struct iss_video_fh *vfh = to_iss_video_fh(fh);
786 return vb2_querybuf(&vfh->queue, b);
790 iss_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
792 struct iss_video *video = video_drvdata(file);
793 struct iss_video_fh *vfh = to_iss_video_fh(fh);
795 return vb2_qbuf(&vfh->queue, video->video.v4l2_dev->mdev, b);
799 iss_video_expbuf(struct file *file, void *fh, struct v4l2_exportbuffer *e)
801 struct iss_video_fh *vfh = to_iss_video_fh(fh);
803 return vb2_expbuf(&vfh->queue, e);
807 iss_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
809 struct iss_video_fh *vfh = to_iss_video_fh(fh);
811 return vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
817 * Every ISS pipeline has a single input and a single output. The input can be
818 * either a sensor or a video node. The output is always a video node.
820 * As every pipeline has an output video node, the ISS video objects at the
821 * pipeline output stores the pipeline state. It tracks the streaming state of
822 * both the input and output, as well as the availability of buffers.
824 * In sensor-to-memory mode, frames are always available at the pipeline input.
825 * Starting the sensor usually requires I2C transfers and must be done in
826 * interruptible context. The pipeline is started and stopped synchronously
827 * to the stream on/off commands. All modules in the pipeline will get their
828 * subdev set stream handler called. The module at the end of the pipeline must
829 * delay starting the hardware until buffers are available at its output.
831 * In memory-to-memory mode, starting/stopping the stream requires
832 * synchronization between the input and output. ISS modules can't be stopped
833 * in the middle of a frame, and at least some of the modules seem to become
834 * busy as soon as they're started, even if they don't receive a frame start
835 * event. For that reason frames need to be processed in single-shot mode. The
836 * driver needs to wait until a frame is completely processed and written to
837 * memory before restarting the pipeline for the next frame. Pipelined
838 * processing might be possible but requires more testing.
840 * Stream start must be delayed until buffers are available at both the input
841 * and output. The pipeline must be started in the vb2 queue callback with
842 * the buffers queue spinlock held. The modules subdev set stream operation must
846 iss_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
848 struct iss_video_fh *vfh = to_iss_video_fh(fh);
849 struct iss_video *video = video_drvdata(file);
850 struct media_device *mdev = video->video.entity.graph_obj.mdev;
851 struct media_pipeline_pad_iter iter;
852 enum iss_pipeline_state state;
853 struct iss_pipeline *pipe;
854 struct iss_video *far_end;
855 struct media_pad *pad;
859 if (type != video->type)
862 mutex_lock(&video->stream_lock);
865 * Start streaming on the pipeline. No link touching an entity in the
866 * pipeline can be activated or deactivated once streaming is started.
868 pipe = to_iss_pipeline(&video->video.entity) ? : &video->pipe;
869 pipe->external = NULL;
870 pipe->external_rate = 0;
871 pipe->external_bpp = 0;
873 ret = media_entity_enum_init(&pipe->ent_enum, mdev);
875 goto err_entity_enum_init;
877 if (video->iss->pdata->set_constraints)
878 video->iss->pdata->set_constraints(video->iss, true);
880 ret = video_device_pipeline_start(&video->video, &pipe->pipe);
882 goto err_media_pipeline_start;
884 media_pipeline_for_each_pad(&pipe->pipe, &iter, pad)
885 media_entity_enum_set(&pipe->ent_enum, pad->entity);
888 * Verify that the currently configured format matches the output of
889 * the connected subdev.
891 ret = iss_video_check_format(video, vfh);
893 goto err_iss_video_check_format;
895 video->bpl_padding = ret;
896 video->bpl_value = vfh->format.fmt.pix.bytesperline;
899 * Find the ISS video node connected at the far end of the pipeline and
900 * update the pipeline.
902 far_end = iss_video_far_end(video, pipe);
903 if (IS_ERR(far_end)) {
904 ret = PTR_ERR(far_end);
905 goto err_iss_video_check_format;
908 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
909 state = ISS_PIPELINE_STREAM_OUTPUT | ISS_PIPELINE_IDLE_OUTPUT;
910 pipe->input = far_end;
911 pipe->output = video;
915 goto err_iss_video_check_format;
918 state = ISS_PIPELINE_STREAM_INPUT | ISS_PIPELINE_IDLE_INPUT;
920 pipe->output = far_end;
923 spin_lock_irqsave(&pipe->lock, flags);
924 pipe->state &= ~ISS_PIPELINE_STREAM;
925 pipe->state |= state;
926 spin_unlock_irqrestore(&pipe->lock, flags);
929 * Set the maximum time per frame as the value requested by userspace.
930 * This is a soft limit that can be overridden if the hardware doesn't
931 * support the request limit.
933 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
934 pipe->max_timeperframe = vfh->timeperframe;
936 video->queue = &vfh->queue;
937 INIT_LIST_HEAD(&video->dmaqueue);
938 video->error = false;
939 atomic_set(&pipe->frame_number, -1);
941 ret = vb2_streamon(&vfh->queue, type);
943 goto err_iss_video_check_format;
946 * In sensor-to-memory mode, the stream can be started synchronously
947 * to the stream on command. In memory-to-memory mode, it will be
948 * started when buffers are queued on both the input and output.
953 ret = omap4iss_pipeline_set_stream(pipe,
954 ISS_PIPELINE_STREAM_CONTINUOUS);
956 goto err_omap4iss_set_stream;
957 spin_lock_irqsave(&video->qlock, flags);
958 if (list_empty(&video->dmaqueue))
959 video->dmaqueue_flags |= ISS_VIDEO_DMAQUEUE_UNDERRUN;
960 spin_unlock_irqrestore(&video->qlock, flags);
963 mutex_unlock(&video->stream_lock);
967 err_omap4iss_set_stream:
968 vb2_streamoff(&vfh->queue, type);
969 err_iss_video_check_format:
970 video_device_pipeline_stop(&video->video);
971 err_media_pipeline_start:
972 if (video->iss->pdata->set_constraints)
973 video->iss->pdata->set_constraints(video->iss, false);
976 err_entity_enum_init:
977 media_entity_enum_cleanup(&pipe->ent_enum);
979 mutex_unlock(&video->stream_lock);
985 iss_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
987 struct iss_video_fh *vfh = to_iss_video_fh(fh);
988 struct iss_video *video = video_drvdata(file);
989 struct iss_pipeline *pipe = to_iss_pipeline(&video->video.entity);
990 enum iss_pipeline_state state;
993 if (type != video->type)
996 mutex_lock(&video->stream_lock);
998 if (!vb2_is_streaming(&vfh->queue))
1001 /* Update the pipeline state. */
1002 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1003 state = ISS_PIPELINE_STREAM_OUTPUT
1004 | ISS_PIPELINE_QUEUE_OUTPUT;
1006 state = ISS_PIPELINE_STREAM_INPUT
1007 | ISS_PIPELINE_QUEUE_INPUT;
1009 spin_lock_irqsave(&pipe->lock, flags);
1010 pipe->state &= ~state;
1011 spin_unlock_irqrestore(&pipe->lock, flags);
1013 /* Stop the stream. */
1014 omap4iss_pipeline_set_stream(pipe, ISS_PIPELINE_STREAM_STOPPED);
1015 vb2_streamoff(&vfh->queue, type);
1016 video->queue = NULL;
1018 media_entity_enum_cleanup(&pipe->ent_enum);
1020 if (video->iss->pdata->set_constraints)
1021 video->iss->pdata->set_constraints(video->iss, false);
1022 video_device_pipeline_stop(&video->video);
1025 mutex_unlock(&video->stream_lock);
1030 iss_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1032 if (input->index > 0)
1035 strscpy(input->name, "camera", sizeof(input->name));
1036 input->type = V4L2_INPUT_TYPE_CAMERA;
1042 iss_video_g_input(struct file *file, void *fh, unsigned int *input)
1050 iss_video_s_input(struct file *file, void *fh, unsigned int input)
1052 return input == 0 ? 0 : -EINVAL;
1055 static const struct v4l2_ioctl_ops iss_video_ioctl_ops = {
1056 .vidioc_querycap = iss_video_querycap,
1057 .vidioc_enum_fmt_vid_cap = iss_video_enum_format,
1058 .vidioc_g_fmt_vid_cap = iss_video_get_format,
1059 .vidioc_s_fmt_vid_cap = iss_video_set_format,
1060 .vidioc_try_fmt_vid_cap = iss_video_try_format,
1061 .vidioc_g_fmt_vid_out = iss_video_get_format,
1062 .vidioc_s_fmt_vid_out = iss_video_set_format,
1063 .vidioc_try_fmt_vid_out = iss_video_try_format,
1064 .vidioc_g_selection = iss_video_get_selection,
1065 .vidioc_s_selection = iss_video_set_selection,
1066 .vidioc_g_parm = iss_video_get_param,
1067 .vidioc_s_parm = iss_video_set_param,
1068 .vidioc_reqbufs = iss_video_reqbufs,
1069 .vidioc_querybuf = iss_video_querybuf,
1070 .vidioc_qbuf = iss_video_qbuf,
1071 .vidioc_expbuf = iss_video_expbuf,
1072 .vidioc_dqbuf = iss_video_dqbuf,
1073 .vidioc_streamon = iss_video_streamon,
1074 .vidioc_streamoff = iss_video_streamoff,
1075 .vidioc_enum_input = iss_video_enum_input,
1076 .vidioc_g_input = iss_video_g_input,
1077 .vidioc_s_input = iss_video_s_input,
1080 /* -----------------------------------------------------------------------------
1081 * V4L2 file operations
1084 static int iss_video_open(struct file *file)
1086 struct iss_video *video = video_drvdata(file);
1087 struct iss_video_fh *handle;
1088 struct vb2_queue *q;
1091 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1095 v4l2_fh_init(&handle->vfh, &video->video);
1096 v4l2_fh_add(&handle->vfh);
1098 /* If this is the first user, initialise the pipeline. */
1099 if (!omap4iss_get(video->iss)) {
1104 ret = v4l2_pipeline_pm_get(&video->video.entity);
1106 omap4iss_put(video->iss);
1112 q->type = video->type;
1113 q->io_modes = VB2_MMAP | VB2_DMABUF;
1114 q->drv_priv = handle;
1115 q->ops = &iss_video_vb2ops;
1116 q->mem_ops = &vb2_dma_contig_memops;
1117 q->buf_struct_size = sizeof(struct iss_buffer);
1118 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1119 q->dev = video->iss->dev;
1121 ret = vb2_queue_init(q);
1123 omap4iss_put(video->iss);
1127 memset(&handle->format, 0, sizeof(handle->format));
1128 handle->format.type = video->type;
1129 handle->timeperframe.denominator = 1;
1131 handle->video = video;
1132 file->private_data = &handle->vfh;
1136 v4l2_fh_del(&handle->vfh);
1137 v4l2_fh_exit(&handle->vfh);
1144 static int iss_video_release(struct file *file)
1146 struct iss_video *video = video_drvdata(file);
1147 struct v4l2_fh *vfh = file->private_data;
1148 struct iss_video_fh *handle = to_iss_video_fh(vfh);
1150 /* Disable streaming and free the buffers queue resources. */
1151 iss_video_streamoff(file, vfh, video->type);
1153 v4l2_pipeline_pm_put(&video->video.entity);
1155 /* Release the videobuf2 queue */
1156 vb2_queue_release(&handle->queue);
1161 file->private_data = NULL;
1163 omap4iss_put(video->iss);
1168 static __poll_t iss_video_poll(struct file *file, poll_table *wait)
1170 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1172 return vb2_poll(&vfh->queue, file, wait);
1175 static int iss_video_mmap(struct file *file, struct vm_area_struct *vma)
1177 struct iss_video_fh *vfh = to_iss_video_fh(file->private_data);
1179 return vb2_mmap(&vfh->queue, vma);
1182 static const struct v4l2_file_operations iss_video_fops = {
1183 .owner = THIS_MODULE,
1184 .unlocked_ioctl = video_ioctl2,
1185 .open = iss_video_open,
1186 .release = iss_video_release,
1187 .poll = iss_video_poll,
1188 .mmap = iss_video_mmap,
1191 /* -----------------------------------------------------------------------------
1195 static const struct iss_video_operations iss_video_dummy_ops = {
1198 int omap4iss_video_init(struct iss_video *video, const char *name)
1200 const char *direction;
1203 switch (video->type) {
1204 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1205 direction = "output";
1206 video->pad.flags = MEDIA_PAD_FL_SINK;
1208 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1209 direction = "input";
1210 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1217 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1221 spin_lock_init(&video->qlock);
1222 mutex_init(&video->mutex);
1223 atomic_set(&video->active, 0);
1225 spin_lock_init(&video->pipe.lock);
1226 mutex_init(&video->stream_lock);
1228 /* Initialize the video device. */
1230 video->ops = &iss_video_dummy_ops;
1232 video->video.fops = &iss_video_fops;
1233 snprintf(video->video.name, sizeof(video->video.name),
1234 "OMAP4 ISS %s %s", name, direction);
1235 video->video.vfl_type = VFL_TYPE_VIDEO;
1236 video->video.release = video_device_release_empty;
1237 video->video.ioctl_ops = &iss_video_ioctl_ops;
1238 video->pipe.stream_state = ISS_PIPELINE_STREAM_STOPPED;
1240 video_set_drvdata(&video->video, video);
1245 void omap4iss_video_cleanup(struct iss_video *video)
1247 media_entity_cleanup(&video->video.entity);
1248 mutex_destroy(&video->stream_lock);
1249 mutex_destroy(&video->mutex);
1252 int omap4iss_video_register(struct iss_video *video, struct v4l2_device *vdev)
1256 video->video.v4l2_dev = vdev;
1257 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1258 video->video.device_caps = V4L2_CAP_VIDEO_CAPTURE;
1260 video->video.device_caps = V4L2_CAP_VIDEO_OUTPUT;
1261 video->video.device_caps |= V4L2_CAP_STREAMING;
1263 ret = video_register_device(&video->video, VFL_TYPE_VIDEO, -1);
1265 dev_err(video->iss->dev,
1266 "could not register video device (%d)\n", ret);
1271 void omap4iss_video_unregister(struct iss_video *video)
1273 video_unregister_device(&video->video);