4 * TI OMAP3 ISP - Generic video node
6 * Copyright (C) 2009-2010 Nokia Corporation
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * Sakari Ailus <sakari.ailus@iki.fi>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <asm/cacheflush.h>
17 #include <linux/clk.h>
19 #include <linux/module.h>
20 #include <linux/pagemap.h>
21 #include <linux/scatterlist.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <media/v4l2-dev.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/videobuf2-dma-contig.h>
33 /* -----------------------------------------------------------------------------
38 * NOTE: When adding new media bus codes, always remember to add
39 * corresponding in-memory formats to the table below!!!
41 static struct isp_format_info formats[] = {
42 { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
43 MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8,
44 V4L2_PIX_FMT_GREY, 8, 1, },
45 { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10,
46 MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8,
47 V4L2_PIX_FMT_Y10, 10, 2, },
48 { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10,
49 MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8,
50 V4L2_PIX_FMT_Y12, 12, 2, },
51 { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
52 MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8,
53 V4L2_PIX_FMT_SBGGR8, 8, 1, },
54 { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
55 MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8,
56 V4L2_PIX_FMT_SGBRG8, 8, 1, },
57 { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
58 MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8,
59 V4L2_PIX_FMT_SGRBG8, 8, 1, },
60 { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
61 MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8,
62 V4L2_PIX_FMT_SRGGB8, 8, 1, },
63 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
64 MEDIA_BUS_FMT_SBGGR10_1X10, 0,
65 V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, },
66 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
67 MEDIA_BUS_FMT_SGBRG10_1X10, 0,
68 V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, },
69 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
70 MEDIA_BUS_FMT_SGRBG10_1X10, 0,
71 V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, },
72 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
73 MEDIA_BUS_FMT_SRGGB10_1X10, 0,
74 V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, },
75 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10,
76 MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8,
77 V4L2_PIX_FMT_SBGGR10, 10, 2, },
78 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10,
79 MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8,
80 V4L2_PIX_FMT_SGBRG10, 10, 2, },
81 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10,
82 MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8,
83 V4L2_PIX_FMT_SGRBG10, 10, 2, },
84 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10,
85 MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8,
86 V4L2_PIX_FMT_SRGGB10, 10, 2, },
87 { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10,
88 MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8,
89 V4L2_PIX_FMT_SBGGR12, 12, 2, },
90 { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10,
91 MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8,
92 V4L2_PIX_FMT_SGBRG12, 12, 2, },
93 { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10,
94 MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8,
95 V4L2_PIX_FMT_SGRBG12, 12, 2, },
96 { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10,
97 MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8,
98 V4L2_PIX_FMT_SRGGB12, 12, 2, },
99 { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16,
100 MEDIA_BUS_FMT_UYVY8_1X16, 0,
101 V4L2_PIX_FMT_UYVY, 16, 2, },
102 { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16,
103 MEDIA_BUS_FMT_YUYV8_1X16, 0,
104 V4L2_PIX_FMT_YUYV, 16, 2, },
105 { MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_UYVY8_2X8,
106 MEDIA_BUS_FMT_UYVY8_2X8, 0,
107 V4L2_PIX_FMT_UYVY, 8, 2, },
108 { MEDIA_BUS_FMT_YUYV8_2X8, MEDIA_BUS_FMT_YUYV8_2X8,
109 MEDIA_BUS_FMT_YUYV8_2X8, 0,
110 V4L2_PIX_FMT_YUYV, 8, 2, },
111 /* Empty entry to catch the unsupported pixel code (0) used by the CCDC
112 * module and avoid NULL pointer dereferences.
117 const struct isp_format_info *omap3isp_video_format_info(u32 code)
121 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
122 if (formats[i].code == code)
130 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
131 * @video: ISP video instance
132 * @mbus: v4l2_mbus_framefmt format (input)
133 * @pix: v4l2_pix_format format (output)
135 * Fill the output pix structure with information from the input mbus format.
136 * The bytesperline and sizeimage fields are computed from the requested bytes
137 * per line value in the pix format and information from the video instance.
139 * Return the number of padding bytes at end of line.
141 static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
142 const struct v4l2_mbus_framefmt *mbus,
143 struct v4l2_pix_format *pix)
145 unsigned int bpl = pix->bytesperline;
146 unsigned int min_bpl;
149 memset(pix, 0, sizeof(*pix));
150 pix->width = mbus->width;
151 pix->height = mbus->height;
153 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
154 if (formats[i].code == mbus->code)
158 if (WARN_ON(i == ARRAY_SIZE(formats)))
161 min_bpl = pix->width * formats[i].bpp;
163 /* Clamp the requested bytes per line value. If the maximum bytes per
164 * line value is zero, the module doesn't support user configurable line
165 * sizes. Override the requested value with the minimum in that case.
168 bpl = clamp(bpl, min_bpl, video->bpl_max);
172 if (!video->bpl_zero_padding || bpl != min_bpl)
173 bpl = ALIGN(bpl, video->bpl_alignment);
175 pix->pixelformat = formats[i].pixelformat;
176 pix->bytesperline = bpl;
177 pix->sizeimage = pix->bytesperline * pix->height;
178 pix->colorspace = mbus->colorspace;
179 pix->field = mbus->field;
181 return bpl - min_bpl;
184 static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
185 struct v4l2_mbus_framefmt *mbus)
189 memset(mbus, 0, sizeof(*mbus));
190 mbus->width = pix->width;
191 mbus->height = pix->height;
193 /* Skip the last format in the loop so that it will be selected if no
196 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
197 if (formats[i].pixelformat == pix->pixelformat)
201 mbus->code = formats[i].code;
202 mbus->colorspace = pix->colorspace;
203 mbus->field = pix->field;
206 static struct v4l2_subdev *
207 isp_video_remote_subdev(struct isp_video *video, u32 *pad)
209 struct media_pad *remote;
211 remote = media_entity_remote_pad(&video->pad);
213 if (remote == NULL ||
214 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
218 *pad = remote->index;
220 return media_entity_to_v4l2_subdev(remote->entity);
223 /* Return a pointer to the ISP video instance at the far end of the pipeline. */
224 static int isp_video_get_graph_data(struct isp_video *video,
225 struct isp_pipeline *pipe)
227 struct media_entity_graph graph;
228 struct media_entity *entity = &video->video.entity;
229 struct media_device *mdev = entity->parent;
230 struct isp_video *far_end = NULL;
232 mutex_lock(&mdev->graph_mutex);
233 media_entity_graph_walk_start(&graph, entity);
235 while ((entity = media_entity_graph_walk_next(&graph))) {
236 struct isp_video *__video;
238 pipe->entities |= 1 << entity->id;
243 if (entity == &video->video.entity)
246 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
249 __video = to_isp_video(media_entity_to_video_device(entity));
250 if (__video->type != video->type)
254 mutex_unlock(&mdev->graph_mutex);
256 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
257 pipe->input = far_end;
258 pipe->output = video;
264 pipe->output = far_end;
271 __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
273 struct v4l2_subdev_format fmt;
274 struct v4l2_subdev *subdev;
278 subdev = isp_video_remote_subdev(video, &pad);
283 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
285 mutex_lock(&video->mutex);
286 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
287 mutex_unlock(&video->mutex);
292 format->type = video->type;
293 return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
297 isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
299 struct v4l2_format format;
302 memcpy(&format, &vfh->format, sizeof(format));
303 ret = __isp_video_get_format(video, &format);
307 if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
308 vfh->format.fmt.pix.height != format.fmt.pix.height ||
309 vfh->format.fmt.pix.width != format.fmt.pix.width ||
310 vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
311 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage ||
312 vfh->format.fmt.pix.field != format.fmt.pix.field)
318 /* -----------------------------------------------------------------------------
319 * Video queue operations
322 static int isp_video_queue_setup(struct vb2_queue *queue,
323 unsigned int *count, unsigned int *num_planes,
324 unsigned int sizes[], void *alloc_ctxs[])
326 struct isp_video_fh *vfh = vb2_get_drv_priv(queue);
327 struct isp_video *video = vfh->video;
331 sizes[0] = vfh->format.fmt.pix.sizeimage;
335 alloc_ctxs[0] = video->alloc_ctx;
337 *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0]));
342 static int isp_video_buffer_prepare(struct vb2_buffer *buf)
344 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf);
345 struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
346 struct isp_buffer *buffer = to_isp_buffer(vbuf);
347 struct isp_video *video = vfh->video;
350 /* Refuse to prepare the buffer is the video node has registered an
351 * error. We don't need to take any lock here as the operation is
352 * inherently racy. The authoritative check will be performed in the
353 * queue handler, which can't return an error, this check is just a best
354 * effort to notify userspace as early as possible.
356 if (unlikely(video->error))
359 addr = vb2_dma_contig_plane_dma_addr(buf, 0);
360 if (!IS_ALIGNED(addr, 32)) {
361 dev_dbg(video->isp->dev,
362 "Buffer address must be aligned to 32 bytes boundary.\n");
366 vb2_set_plane_payload(&buffer->vb.vb2_buf, 0,
367 vfh->format.fmt.pix.sizeimage);
374 * isp_video_buffer_queue - Add buffer to streaming queue
377 * In memory-to-memory mode, start streaming on the pipeline if buffers are
378 * queued on both the input and the output, if the pipeline isn't already busy.
379 * If the pipeline is busy, it will be restarted in the output module interrupt
382 static void isp_video_buffer_queue(struct vb2_buffer *buf)
384 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf);
385 struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue);
386 struct isp_buffer *buffer = to_isp_buffer(vbuf);
387 struct isp_video *video = vfh->video;
388 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
389 enum isp_pipeline_state state;
394 spin_lock_irqsave(&video->irqlock, flags);
396 if (unlikely(video->error)) {
397 vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_ERROR);
398 spin_unlock_irqrestore(&video->irqlock, flags);
402 empty = list_empty(&video->dmaqueue);
403 list_add_tail(&buffer->irqlist, &video->dmaqueue);
405 spin_unlock_irqrestore(&video->irqlock, flags);
408 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
409 state = ISP_PIPELINE_QUEUE_OUTPUT;
411 state = ISP_PIPELINE_QUEUE_INPUT;
413 spin_lock_irqsave(&pipe->lock, flags);
414 pipe->state |= state;
415 video->ops->queue(video, buffer);
416 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
418 start = isp_pipeline_ready(pipe);
420 pipe->state |= ISP_PIPELINE_STREAM;
421 spin_unlock_irqrestore(&pipe->lock, flags);
424 omap3isp_pipeline_set_stream(pipe,
425 ISP_PIPELINE_STREAM_SINGLESHOT);
429 static const struct vb2_ops isp_video_queue_ops = {
430 .queue_setup = isp_video_queue_setup,
431 .buf_prepare = isp_video_buffer_prepare,
432 .buf_queue = isp_video_buffer_queue,
436 * omap3isp_video_buffer_next - Complete the current buffer and return the next
437 * @video: ISP video object
439 * Remove the current video buffer from the DMA queue and fill its timestamp and
440 * field count before handing it back to videobuf2.
442 * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no
443 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
444 * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE.
446 * The DMA queue is expected to contain at least one buffer.
448 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
451 struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
453 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
454 enum isp_pipeline_state state;
455 struct isp_buffer *buf;
458 spin_lock_irqsave(&video->irqlock, flags);
459 if (WARN_ON(list_empty(&video->dmaqueue))) {
460 spin_unlock_irqrestore(&video->irqlock, flags);
464 buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
466 list_del(&buf->irqlist);
467 spin_unlock_irqrestore(&video->irqlock, flags);
469 buf->vb.vb2_buf.timestamp = ktime_get_ns();
471 /* Do frame number propagation only if this is the output video node.
472 * Frame number either comes from the CSI receivers or it gets
473 * incremented here if H3A is not active.
474 * Note: There is no guarantee that the output buffer will finish
475 * first, so the input number might lag behind by 1 in some cases.
477 if (video == pipe->output && !pipe->do_propagation)
479 atomic_inc_return(&pipe->frame_number);
481 buf->vb.sequence = atomic_read(&pipe->frame_number);
483 if (pipe->field != V4L2_FIELD_NONE)
484 buf->vb.sequence /= 2;
486 buf->vb.field = pipe->field;
488 /* Report pipeline errors to userspace on the capture device side. */
489 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
490 state = VB2_BUF_STATE_ERROR;
493 state = VB2_BUF_STATE_DONE;
496 vb2_buffer_done(&buf->vb.vb2_buf, state);
498 spin_lock_irqsave(&video->irqlock, flags);
500 if (list_empty(&video->dmaqueue)) {
501 spin_unlock_irqrestore(&video->irqlock, flags);
503 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
504 state = ISP_PIPELINE_QUEUE_OUTPUT
505 | ISP_PIPELINE_STREAM;
507 state = ISP_PIPELINE_QUEUE_INPUT
508 | ISP_PIPELINE_STREAM;
510 spin_lock_irqsave(&pipe->lock, flags);
511 pipe->state &= ~state;
512 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
513 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
514 spin_unlock_irqrestore(&pipe->lock, flags);
518 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
519 spin_lock(&pipe->lock);
520 pipe->state &= ~ISP_PIPELINE_STREAM;
521 spin_unlock(&pipe->lock);
524 buf = list_first_entry(&video->dmaqueue, struct isp_buffer,
527 spin_unlock_irqrestore(&video->irqlock, flags);
533 * omap3isp_video_cancel_stream - Cancel stream on a video node
534 * @video: ISP video object
536 * Cancelling a stream mark all buffers on the video node as erroneous and makes
537 * sure no new buffer can be queued.
539 void omap3isp_video_cancel_stream(struct isp_video *video)
543 spin_lock_irqsave(&video->irqlock, flags);
545 while (!list_empty(&video->dmaqueue)) {
546 struct isp_buffer *buf;
548 buf = list_first_entry(&video->dmaqueue,
549 struct isp_buffer, irqlist);
550 list_del(&buf->irqlist);
551 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
556 spin_unlock_irqrestore(&video->irqlock, flags);
560 * omap3isp_video_resume - Perform resume operation on the buffers
561 * @video: ISP video object
562 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
564 * This function is intended to be used on suspend/resume scenario. It
565 * requests video queue layer to discard buffers marked as DONE if it's in
566 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
569 void omap3isp_video_resume(struct isp_video *video, int continuous)
571 struct isp_buffer *buf = NULL;
573 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
574 mutex_lock(&video->queue_lock);
575 vb2_discard_done(video->queue);
576 mutex_unlock(&video->queue_lock);
579 if (!list_empty(&video->dmaqueue)) {
580 buf = list_first_entry(&video->dmaqueue,
581 struct isp_buffer, irqlist);
582 video->ops->queue(video, buf);
583 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
586 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
590 /* -----------------------------------------------------------------------------
595 isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
597 struct isp_video *video = video_drvdata(file);
599 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
600 strlcpy(cap->card, video->video.name, sizeof(cap->card));
601 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
603 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
604 | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
606 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
607 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
609 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
615 isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
617 struct isp_video_fh *vfh = to_isp_video_fh(fh);
618 struct isp_video *video = video_drvdata(file);
620 if (format->type != video->type)
623 mutex_lock(&video->mutex);
624 *format = vfh->format;
625 mutex_unlock(&video->mutex);
631 isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
633 struct isp_video_fh *vfh = to_isp_video_fh(fh);
634 struct isp_video *video = video_drvdata(file);
635 struct v4l2_mbus_framefmt fmt;
637 if (format->type != video->type)
640 /* Replace unsupported field orders with sane defaults. */
641 switch (format->fmt.pix.field) {
642 case V4L2_FIELD_NONE:
643 /* Progressive is supported everywhere. */
645 case V4L2_FIELD_ALTERNATE:
646 /* ALTERNATE is not supported on output nodes. */
647 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
648 format->fmt.pix.field = V4L2_FIELD_NONE;
650 case V4L2_FIELD_INTERLACED:
651 /* The ISP has no concept of video standard, select the
652 * top-bottom order when the unqualified interlaced order is
655 format->fmt.pix.field = V4L2_FIELD_INTERLACED_TB;
657 case V4L2_FIELD_INTERLACED_TB:
658 case V4L2_FIELD_INTERLACED_BT:
659 /* Interlaced orders are only supported at the CCDC output. */
660 if (video != &video->isp->isp_ccdc.video_out)
661 format->fmt.pix.field = V4L2_FIELD_NONE;
664 case V4L2_FIELD_BOTTOM:
665 case V4L2_FIELD_SEQ_TB:
666 case V4L2_FIELD_SEQ_BT:
668 /* All other field orders are currently unsupported, default to
671 format->fmt.pix.field = V4L2_FIELD_NONE;
675 /* Fill the bytesperline and sizeimage fields by converting to media bus
676 * format and back to pixel format.
678 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
679 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
681 mutex_lock(&video->mutex);
682 vfh->format = *format;
683 mutex_unlock(&video->mutex);
689 isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
691 struct isp_video *video = video_drvdata(file);
692 struct v4l2_subdev_format fmt;
693 struct v4l2_subdev *subdev;
697 if (format->type != video->type)
700 subdev = isp_video_remote_subdev(video, &pad);
704 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
707 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
708 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
710 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
712 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
717 isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
719 struct isp_video *video = video_drvdata(file);
720 struct v4l2_subdev *subdev;
723 subdev = isp_video_remote_subdev(video, NULL);
727 mutex_lock(&video->mutex);
728 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
729 mutex_unlock(&video->mutex);
731 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
735 isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
737 struct isp_video *video = video_drvdata(file);
738 struct v4l2_subdev_format format;
739 struct v4l2_subdev *subdev;
743 subdev = isp_video_remote_subdev(video, &pad);
747 /* Try the get crop operation first and fallback to get format if not
750 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
751 if (ret != -ENOIOCTLCMD)
755 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
756 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
758 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
762 crop->c.width = format.format.width;
763 crop->c.height = format.format.height;
769 isp_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
771 struct isp_video *video = video_drvdata(file);
772 struct v4l2_subdev *subdev;
775 subdev = isp_video_remote_subdev(video, NULL);
779 mutex_lock(&video->mutex);
780 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
781 mutex_unlock(&video->mutex);
783 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
787 isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
789 struct isp_video_fh *vfh = to_isp_video_fh(fh);
790 struct isp_video *video = video_drvdata(file);
792 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
793 video->type != a->type)
796 memset(a, 0, sizeof(*a));
797 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
798 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
799 a->parm.output.timeperframe = vfh->timeperframe;
805 isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
807 struct isp_video_fh *vfh = to_isp_video_fh(fh);
808 struct isp_video *video = video_drvdata(file);
810 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
811 video->type != a->type)
814 if (a->parm.output.timeperframe.denominator == 0)
815 a->parm.output.timeperframe.denominator = 1;
817 vfh->timeperframe = a->parm.output.timeperframe;
823 isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
825 struct isp_video_fh *vfh = to_isp_video_fh(fh);
826 struct isp_video *video = video_drvdata(file);
829 mutex_lock(&video->queue_lock);
830 ret = vb2_reqbufs(&vfh->queue, rb);
831 mutex_unlock(&video->queue_lock);
837 isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
839 struct isp_video_fh *vfh = to_isp_video_fh(fh);
840 struct isp_video *video = video_drvdata(file);
843 mutex_lock(&video->queue_lock);
844 ret = vb2_querybuf(&vfh->queue, b);
845 mutex_unlock(&video->queue_lock);
851 isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
853 struct isp_video_fh *vfh = to_isp_video_fh(fh);
854 struct isp_video *video = video_drvdata(file);
857 mutex_lock(&video->queue_lock);
858 ret = vb2_qbuf(&vfh->queue, b);
859 mutex_unlock(&video->queue_lock);
865 isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
867 struct isp_video_fh *vfh = to_isp_video_fh(fh);
868 struct isp_video *video = video_drvdata(file);
871 mutex_lock(&video->queue_lock);
872 ret = vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK);
873 mutex_unlock(&video->queue_lock);
878 static int isp_video_check_external_subdevs(struct isp_video *video,
879 struct isp_pipeline *pipe)
881 struct isp_device *isp = video->isp;
882 struct media_entity *ents[] = {
883 &isp->isp_csi2a.subdev.entity,
884 &isp->isp_csi2c.subdev.entity,
885 &isp->isp_ccp2.subdev.entity,
886 &isp->isp_ccdc.subdev.entity
888 struct media_pad *source_pad;
889 struct media_entity *source = NULL;
890 struct media_entity *sink;
891 struct v4l2_subdev_format fmt;
892 struct v4l2_ext_controls ctrls;
893 struct v4l2_ext_control ctrl;
897 /* Memory-to-memory pipelines have no external subdev. */
898 if (pipe->input != NULL)
901 for (i = 0; i < ARRAY_SIZE(ents); i++) {
902 /* Is the entity part of the pipeline? */
903 if (!(pipe->entities & (1 << ents[i]->id)))
906 /* ISP entities have always sink pad == 0. Find source. */
907 source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
908 if (source_pad == NULL)
911 source = source_pad->entity;
917 dev_warn(isp->dev, "can't find source, failing now\n");
921 if (media_entity_type(source) != MEDIA_ENT_T_V4L2_SUBDEV)
924 pipe->external = media_entity_to_v4l2_subdev(source);
926 fmt.pad = source_pad->index;
927 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
928 ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
929 pad, get_fmt, NULL, &fmt);
930 if (unlikely(ret < 0)) {
931 dev_warn(isp->dev, "get_fmt returned null!\n");
935 pipe->external_width =
936 omap3isp_video_format_info(fmt.format.code)->width;
938 memset(&ctrls, 0, sizeof(ctrls));
939 memset(&ctrl, 0, sizeof(ctrl));
941 ctrl.id = V4L2_CID_PIXEL_RATE;
944 ctrls.controls = &ctrl;
946 ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
948 dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
949 pipe->external->name);
953 pipe->external_rate = ctrl.value64;
955 if (pipe->entities & (1 << isp->isp_ccdc.subdev.entity.id)) {
956 unsigned int rate = UINT_MAX;
958 * Check that maximum allowed CCDC pixel rate isn't
959 * exceeded by the pixel rate.
961 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
962 if (pipe->external_rate > rate)
972 * Every ISP pipeline has a single input and a single output. The input can be
973 * either a sensor or a video node. The output is always a video node.
975 * As every pipeline has an output video node, the ISP video objects at the
976 * pipeline output stores the pipeline state. It tracks the streaming state of
977 * both the input and output, as well as the availability of buffers.
979 * In sensor-to-memory mode, frames are always available at the pipeline input.
980 * Starting the sensor usually requires I2C transfers and must be done in
981 * interruptible context. The pipeline is started and stopped synchronously
982 * to the stream on/off commands. All modules in the pipeline will get their
983 * subdev set stream handler called. The module at the end of the pipeline must
984 * delay starting the hardware until buffers are available at its output.
986 * In memory-to-memory mode, starting/stopping the stream requires
987 * synchronization between the input and output. ISP modules can't be stopped
988 * in the middle of a frame, and at least some of the modules seem to become
989 * busy as soon as they're started, even if they don't receive a frame start
990 * event. For that reason frames need to be processed in single-shot mode. The
991 * driver needs to wait until a frame is completely processed and written to
992 * memory before restarting the pipeline for the next frame. Pipelined
993 * processing might be possible but requires more testing.
995 * Stream start must be delayed until buffers are available at both the input
996 * and output. The pipeline must be started in the videobuf queue callback with
997 * the buffers queue spinlock held. The modules subdev set stream operation must
1001 isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1003 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1004 struct isp_video *video = video_drvdata(file);
1005 enum isp_pipeline_state state;
1006 struct isp_pipeline *pipe;
1007 unsigned long flags;
1010 if (type != video->type)
1013 mutex_lock(&video->stream_lock);
1015 /* Start streaming on the pipeline. No link touching an entity in the
1016 * pipeline can be activated or deactivated once streaming is started.
1018 pipe = video->video.entity.pipe
1019 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
1023 /* TODO: Implement PM QoS */
1024 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1025 pipe->max_rate = pipe->l3_ick;
1027 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
1029 goto err_pipeline_start;
1031 /* Verify that the currently configured format matches the output of
1032 * the connected subdev.
1034 ret = isp_video_check_format(video, vfh);
1036 goto err_check_format;
1038 video->bpl_padding = ret;
1039 video->bpl_value = vfh->format.fmt.pix.bytesperline;
1041 ret = isp_video_get_graph_data(video, pipe);
1043 goto err_check_format;
1045 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1046 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
1048 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1050 ret = isp_video_check_external_subdevs(video, pipe);
1052 goto err_check_format;
1054 pipe->error = false;
1056 spin_lock_irqsave(&pipe->lock, flags);
1057 pipe->state &= ~ISP_PIPELINE_STREAM;
1058 pipe->state |= state;
1059 spin_unlock_irqrestore(&pipe->lock, flags);
1061 /* Set the maximum time per frame as the value requested by userspace.
1062 * This is a soft limit that can be overridden if the hardware doesn't
1063 * support the request limit.
1065 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1066 pipe->max_timeperframe = vfh->timeperframe;
1068 video->queue = &vfh->queue;
1069 INIT_LIST_HEAD(&video->dmaqueue);
1070 atomic_set(&pipe->frame_number, -1);
1071 pipe->field = vfh->format.fmt.pix.field;
1073 mutex_lock(&video->queue_lock);
1074 ret = vb2_streamon(&vfh->queue, type);
1075 mutex_unlock(&video->queue_lock);
1077 goto err_check_format;
1079 /* In sensor-to-memory mode, the stream can be started synchronously
1080 * to the stream on command. In memory-to-memory mode, it will be
1081 * started when buffers are queued on both the input and output.
1083 if (pipe->input == NULL) {
1084 ret = omap3isp_pipeline_set_stream(pipe,
1085 ISP_PIPELINE_STREAM_CONTINUOUS);
1087 goto err_set_stream;
1088 spin_lock_irqsave(&video->irqlock, flags);
1089 if (list_empty(&video->dmaqueue))
1090 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1091 spin_unlock_irqrestore(&video->irqlock, flags);
1094 mutex_unlock(&video->stream_lock);
1098 mutex_lock(&video->queue_lock);
1099 vb2_streamoff(&vfh->queue, type);
1100 mutex_unlock(&video->queue_lock);
1102 media_entity_pipeline_stop(&video->video.entity);
1104 /* TODO: Implement PM QoS */
1105 /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1106 * will get triggered the next time the CCDC is powered up will try to
1107 * access buffers that might have been freed but still present in the
1108 * DMA queue. This can easily get triggered if the above
1109 * omap3isp_pipeline_set_stream() call fails on a system with a
1110 * free-running sensor.
1112 INIT_LIST_HEAD(&video->dmaqueue);
1113 video->queue = NULL;
1115 mutex_unlock(&video->stream_lock);
1120 isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1122 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1123 struct isp_video *video = video_drvdata(file);
1124 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1125 enum isp_pipeline_state state;
1126 unsigned int streaming;
1127 unsigned long flags;
1129 if (type != video->type)
1132 mutex_lock(&video->stream_lock);
1134 /* Make sure we're not streaming yet. */
1135 mutex_lock(&video->queue_lock);
1136 streaming = vb2_is_streaming(&vfh->queue);
1137 mutex_unlock(&video->queue_lock);
1142 /* Update the pipeline state. */
1143 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1144 state = ISP_PIPELINE_STREAM_OUTPUT
1145 | ISP_PIPELINE_QUEUE_OUTPUT;
1147 state = ISP_PIPELINE_STREAM_INPUT
1148 | ISP_PIPELINE_QUEUE_INPUT;
1150 spin_lock_irqsave(&pipe->lock, flags);
1151 pipe->state &= ~state;
1152 spin_unlock_irqrestore(&pipe->lock, flags);
1154 /* Stop the stream. */
1155 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1156 omap3isp_video_cancel_stream(video);
1158 mutex_lock(&video->queue_lock);
1159 vb2_streamoff(&vfh->queue, type);
1160 mutex_unlock(&video->queue_lock);
1161 video->queue = NULL;
1162 video->error = false;
1164 /* TODO: Implement PM QoS */
1165 media_entity_pipeline_stop(&video->video.entity);
1168 mutex_unlock(&video->stream_lock);
1173 isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1175 if (input->index > 0)
1178 strlcpy(input->name, "camera", sizeof(input->name));
1179 input->type = V4L2_INPUT_TYPE_CAMERA;
1185 isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1193 isp_video_s_input(struct file *file, void *fh, unsigned int input)
1195 return input == 0 ? 0 : -EINVAL;
1198 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1199 .vidioc_querycap = isp_video_querycap,
1200 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1201 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1202 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1203 .vidioc_g_fmt_vid_out = isp_video_get_format,
1204 .vidioc_s_fmt_vid_out = isp_video_set_format,
1205 .vidioc_try_fmt_vid_out = isp_video_try_format,
1206 .vidioc_cropcap = isp_video_cropcap,
1207 .vidioc_g_crop = isp_video_get_crop,
1208 .vidioc_s_crop = isp_video_set_crop,
1209 .vidioc_g_parm = isp_video_get_param,
1210 .vidioc_s_parm = isp_video_set_param,
1211 .vidioc_reqbufs = isp_video_reqbufs,
1212 .vidioc_querybuf = isp_video_querybuf,
1213 .vidioc_qbuf = isp_video_qbuf,
1214 .vidioc_dqbuf = isp_video_dqbuf,
1215 .vidioc_streamon = isp_video_streamon,
1216 .vidioc_streamoff = isp_video_streamoff,
1217 .vidioc_enum_input = isp_video_enum_input,
1218 .vidioc_g_input = isp_video_g_input,
1219 .vidioc_s_input = isp_video_s_input,
1222 /* -----------------------------------------------------------------------------
1223 * V4L2 file operations
1226 static int isp_video_open(struct file *file)
1228 struct isp_video *video = video_drvdata(file);
1229 struct isp_video_fh *handle;
1230 struct vb2_queue *queue;
1233 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1237 v4l2_fh_init(&handle->vfh, &video->video);
1238 v4l2_fh_add(&handle->vfh);
1240 /* If this is the first user, initialise the pipeline. */
1241 if (omap3isp_get(video->isp) == NULL) {
1246 ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1248 omap3isp_put(video->isp);
1252 queue = &handle->queue;
1253 queue->type = video->type;
1254 queue->io_modes = VB2_MMAP | VB2_USERPTR;
1255 queue->drv_priv = handle;
1256 queue->ops = &isp_video_queue_ops;
1257 queue->mem_ops = &vb2_dma_contig_memops;
1258 queue->buf_struct_size = sizeof(struct isp_buffer);
1259 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1261 ret = vb2_queue_init(&handle->queue);
1263 omap3isp_put(video->isp);
1267 memset(&handle->format, 0, sizeof(handle->format));
1268 handle->format.type = video->type;
1269 handle->timeperframe.denominator = 1;
1271 handle->video = video;
1272 file->private_data = &handle->vfh;
1276 v4l2_fh_del(&handle->vfh);
1283 static int isp_video_release(struct file *file)
1285 struct isp_video *video = video_drvdata(file);
1286 struct v4l2_fh *vfh = file->private_data;
1287 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1289 /* Disable streaming and free the buffers queue resources. */
1290 isp_video_streamoff(file, vfh, video->type);
1292 mutex_lock(&video->queue_lock);
1293 vb2_queue_release(&handle->queue);
1294 mutex_unlock(&video->queue_lock);
1296 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1298 /* Release the file handle. */
1301 file->private_data = NULL;
1303 omap3isp_put(video->isp);
1308 static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1310 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1311 struct isp_video *video = video_drvdata(file);
1314 mutex_lock(&video->queue_lock);
1315 ret = vb2_poll(&vfh->queue, file, wait);
1316 mutex_unlock(&video->queue_lock);
1321 static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1323 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1325 return vb2_mmap(&vfh->queue, vma);
1328 static struct v4l2_file_operations isp_video_fops = {
1329 .owner = THIS_MODULE,
1330 .unlocked_ioctl = video_ioctl2,
1331 .open = isp_video_open,
1332 .release = isp_video_release,
1333 .poll = isp_video_poll,
1334 .mmap = isp_video_mmap,
1337 /* -----------------------------------------------------------------------------
1341 static const struct isp_video_operations isp_video_dummy_ops = {
1344 int omap3isp_video_init(struct isp_video *video, const char *name)
1346 const char *direction;
1349 switch (video->type) {
1350 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1351 direction = "output";
1352 video->pad.flags = MEDIA_PAD_FL_SINK
1353 | MEDIA_PAD_FL_MUST_CONNECT;
1355 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1356 direction = "input";
1357 video->pad.flags = MEDIA_PAD_FL_SOURCE
1358 | MEDIA_PAD_FL_MUST_CONNECT;
1359 video->video.vfl_dir = VFL_DIR_TX;
1366 video->alloc_ctx = vb2_dma_contig_init_ctx(video->isp->dev);
1367 if (IS_ERR(video->alloc_ctx))
1368 return PTR_ERR(video->alloc_ctx);
1370 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1372 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1376 mutex_init(&video->mutex);
1377 atomic_set(&video->active, 0);
1379 spin_lock_init(&video->pipe.lock);
1380 mutex_init(&video->stream_lock);
1381 mutex_init(&video->queue_lock);
1382 spin_lock_init(&video->irqlock);
1384 /* Initialize the video device. */
1385 if (video->ops == NULL)
1386 video->ops = &isp_video_dummy_ops;
1388 video->video.fops = &isp_video_fops;
1389 snprintf(video->video.name, sizeof(video->video.name),
1390 "OMAP3 ISP %s %s", name, direction);
1391 video->video.vfl_type = VFL_TYPE_GRABBER;
1392 video->video.release = video_device_release_empty;
1393 video->video.ioctl_ops = &isp_video_ioctl_ops;
1394 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1396 video_set_drvdata(&video->video, video);
1401 void omap3isp_video_cleanup(struct isp_video *video)
1403 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1404 media_entity_cleanup(&video->video.entity);
1405 mutex_destroy(&video->queue_lock);
1406 mutex_destroy(&video->stream_lock);
1407 mutex_destroy(&video->mutex);
1410 int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1414 video->video.v4l2_dev = vdev;
1416 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1418 dev_err(video->isp->dev,
1419 "%s: could not register video device (%d)\n",
1425 void omap3isp_video_unregister(struct isp_video *video)
1427 if (video_is_registered(&video->video))
1428 video_unregister_device(&video->video);