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.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 #include <asm/cacheflush.h>
27 #include <linux/clk.h>
29 #include <linux/module.h>
30 #include <linux/pagemap.h>
31 #include <linux/scatterlist.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/vmalloc.h>
35 #include <media/v4l2-dev.h>
36 #include <media/v4l2-ioctl.h>
42 /* -----------------------------------------------------------------------------
47 * NOTE: When adding new media bus codes, always remember to add
48 * corresponding in-memory formats to the table below!!!
50 static struct isp_format_info formats[] = {
51 { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
52 V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
53 V4L2_PIX_FMT_GREY, 8, 1, },
54 { V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10,
55 V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8,
56 V4L2_PIX_FMT_Y10, 10, 2, },
57 { V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10,
58 V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8,
59 V4L2_PIX_FMT_Y12, 12, 2, },
60 { V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
61 V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
62 V4L2_PIX_FMT_SBGGR8, 8, 1, },
63 { V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
64 V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
65 V4L2_PIX_FMT_SGBRG8, 8, 1, },
66 { V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
67 V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
68 V4L2_PIX_FMT_SGRBG8, 8, 1, },
69 { V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
70 V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
71 V4L2_PIX_FMT_SRGGB8, 8, 1, },
72 { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8,
73 V4L2_MBUS_FMT_SBGGR10_1X10, 0,
74 V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, },
75 { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8,
76 V4L2_MBUS_FMT_SGBRG10_1X10, 0,
77 V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, },
78 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
79 V4L2_MBUS_FMT_SGRBG10_1X10, 0,
80 V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, },
81 { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8,
82 V4L2_MBUS_FMT_SRGGB10_1X10, 0,
83 V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, },
84 { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
85 V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8,
86 V4L2_PIX_FMT_SBGGR10, 10, 2, },
87 { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
88 V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8,
89 V4L2_PIX_FMT_SGBRG10, 10, 2, },
90 { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
91 V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8,
92 V4L2_PIX_FMT_SGRBG10, 10, 2, },
93 { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
94 V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8,
95 V4L2_PIX_FMT_SRGGB10, 10, 2, },
96 { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
97 V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8,
98 V4L2_PIX_FMT_SBGGR12, 12, 2, },
99 { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
100 V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8,
101 V4L2_PIX_FMT_SGBRG12, 12, 2, },
102 { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
103 V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8,
104 V4L2_PIX_FMT_SGRBG12, 12, 2, },
105 { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
106 V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8,
107 V4L2_PIX_FMT_SRGGB12, 12, 2, },
108 { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
109 V4L2_MBUS_FMT_UYVY8_1X16, 0,
110 V4L2_PIX_FMT_UYVY, 16, 2, },
111 { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
112 V4L2_MBUS_FMT_YUYV8_1X16, 0,
113 V4L2_PIX_FMT_YUYV, 16, 2, },
114 { V4L2_MBUS_FMT_UYVY8_2X8, V4L2_MBUS_FMT_UYVY8_2X8,
115 V4L2_MBUS_FMT_UYVY8_2X8, 0,
116 V4L2_PIX_FMT_UYVY, 8, 2, },
117 { V4L2_MBUS_FMT_YUYV8_2X8, V4L2_MBUS_FMT_YUYV8_2X8,
118 V4L2_MBUS_FMT_YUYV8_2X8, 0,
119 V4L2_PIX_FMT_YUYV, 8, 2, },
120 /* Empty entry to catch the unsupported pixel code (0) used by the CCDC
121 * module and avoid NULL pointer dereferences.
126 const struct isp_format_info *
127 omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
131 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
132 if (formats[i].code == code)
140 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
141 * @video: ISP video instance
142 * @mbus: v4l2_mbus_framefmt format (input)
143 * @pix: v4l2_pix_format format (output)
145 * Fill the output pix structure with information from the input mbus format.
146 * The bytesperline and sizeimage fields are computed from the requested bytes
147 * per line value in the pix format and information from the video instance.
149 * Return the number of padding bytes at end of line.
151 static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
152 const struct v4l2_mbus_framefmt *mbus,
153 struct v4l2_pix_format *pix)
155 unsigned int bpl = pix->bytesperline;
156 unsigned int min_bpl;
159 memset(pix, 0, sizeof(*pix));
160 pix->width = mbus->width;
161 pix->height = mbus->height;
163 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
164 if (formats[i].code == mbus->code)
168 if (WARN_ON(i == ARRAY_SIZE(formats)))
171 min_bpl = pix->width * formats[i].bpp;
173 /* Clamp the requested bytes per line value. If the maximum bytes per
174 * line value is zero, the module doesn't support user configurable line
175 * sizes. Override the requested value with the minimum in that case.
178 bpl = clamp(bpl, min_bpl, video->bpl_max);
182 if (!video->bpl_zero_padding || bpl != min_bpl)
183 bpl = ALIGN(bpl, video->bpl_alignment);
185 pix->pixelformat = formats[i].pixelformat;
186 pix->bytesperline = bpl;
187 pix->sizeimage = pix->bytesperline * pix->height;
188 pix->colorspace = mbus->colorspace;
189 pix->field = mbus->field;
191 return bpl - min_bpl;
194 static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
195 struct v4l2_mbus_framefmt *mbus)
199 memset(mbus, 0, sizeof(*mbus));
200 mbus->width = pix->width;
201 mbus->height = pix->height;
203 /* Skip the last format in the loop so that it will be selected if no
206 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
207 if (formats[i].pixelformat == pix->pixelformat)
211 mbus->code = formats[i].code;
212 mbus->colorspace = pix->colorspace;
213 mbus->field = pix->field;
216 static struct v4l2_subdev *
217 isp_video_remote_subdev(struct isp_video *video, u32 *pad)
219 struct media_pad *remote;
221 remote = media_entity_remote_pad(&video->pad);
223 if (remote == NULL ||
224 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
228 *pad = remote->index;
230 return media_entity_to_v4l2_subdev(remote->entity);
233 /* Return a pointer to the ISP video instance at the far end of the pipeline. */
234 static int isp_video_get_graph_data(struct isp_video *video,
235 struct isp_pipeline *pipe)
237 struct media_entity_graph graph;
238 struct media_entity *entity = &video->video.entity;
239 struct media_device *mdev = entity->parent;
240 struct isp_video *far_end = NULL;
242 mutex_lock(&mdev->graph_mutex);
243 media_entity_graph_walk_start(&graph, entity);
245 while ((entity = media_entity_graph_walk_next(&graph))) {
246 struct isp_video *__video;
248 pipe->entities |= 1 << entity->id;
253 if (entity == &video->video.entity)
256 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
259 __video = to_isp_video(media_entity_to_video_device(entity));
260 if (__video->type != video->type)
264 mutex_unlock(&mdev->graph_mutex);
266 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
267 pipe->input = far_end;
268 pipe->output = video;
274 pipe->output = far_end;
281 __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
283 struct v4l2_subdev_format fmt;
284 struct v4l2_subdev *subdev;
288 subdev = isp_video_remote_subdev(video, &pad);
293 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
295 mutex_lock(&video->mutex);
296 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
297 mutex_unlock(&video->mutex);
302 format->type = video->type;
303 return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
307 isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
309 struct v4l2_format format;
312 memcpy(&format, &vfh->format, sizeof(format));
313 ret = __isp_video_get_format(video, &format);
317 if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
318 vfh->format.fmt.pix.height != format.fmt.pix.height ||
319 vfh->format.fmt.pix.width != format.fmt.pix.width ||
320 vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
321 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
327 /* -----------------------------------------------------------------------------
328 * Video queue operations
331 static void isp_video_queue_prepare(struct isp_video_queue *queue,
332 unsigned int *nbuffers, unsigned int *size)
334 struct isp_video_fh *vfh =
335 container_of(queue, struct isp_video_fh, queue);
336 struct isp_video *video = vfh->video;
338 *size = vfh->format.fmt.pix.sizeimage;
342 *nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size));
345 static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
347 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
348 struct isp_buffer *buffer = to_isp_buffer(buf);
349 struct isp_video *video = vfh->video;
351 /* Refuse to prepare the buffer is the video node has registered an
352 * error. We don't need to take any lock here as the operation is
353 * inherently racy. The authoritative check will be performed in the
354 * queue handler, which can't return an error, this check is just a best
355 * effort to notify userspace as early as possible.
357 if (unlikely(video->error))
360 buffer->isp_addr = buf->dma;
365 * isp_video_buffer_queue - Add buffer to streaming queue
368 * In memory-to-memory mode, start streaming on the pipeline if buffers are
369 * queued on both the input and the output, if the pipeline isn't already busy.
370 * If the pipeline is busy, it will be restarted in the output module interrupt
373 static void isp_video_buffer_queue(struct isp_video_buffer *buf)
375 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
376 struct isp_buffer *buffer = to_isp_buffer(buf);
377 struct isp_video *video = vfh->video;
378 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
379 enum isp_pipeline_state state;
384 spin_lock_irqsave(&video->irqlock, flags);
386 if (unlikely(video->error)) {
387 buf->state = ISP_BUF_STATE_ERROR;
389 spin_unlock_irqrestore(&video->irqlock, flags);
393 empty = list_empty(&video->dmaqueue);
394 list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
396 spin_unlock_irqrestore(&video->irqlock, flags);
399 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
400 state = ISP_PIPELINE_QUEUE_OUTPUT;
402 state = ISP_PIPELINE_QUEUE_INPUT;
404 spin_lock_irqsave(&pipe->lock, flags);
405 pipe->state |= state;
406 video->ops->queue(video, buffer);
407 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
409 start = isp_pipeline_ready(pipe);
411 pipe->state |= ISP_PIPELINE_STREAM;
412 spin_unlock_irqrestore(&pipe->lock, flags);
415 omap3isp_pipeline_set_stream(pipe,
416 ISP_PIPELINE_STREAM_SINGLESHOT);
420 static const struct isp_video_queue_operations isp_video_queue_ops = {
421 .queue_prepare = &isp_video_queue_prepare,
422 .buffer_prepare = &isp_video_buffer_prepare,
423 .buffer_queue = &isp_video_buffer_queue,
427 * omap3isp_video_buffer_next - Complete the current buffer and return the next
428 * @video: ISP video object
430 * Remove the current video buffer from the DMA queue and fill its timestamp,
431 * field count and state fields before waking up its completion handler.
433 * For capture video nodes the buffer state is set to ISP_BUF_STATE_DONE if no
434 * error has been flagged in the pipeline, or to ISP_BUF_STATE_ERROR otherwise.
435 * For video output nodes the buffer state is always set to ISP_BUF_STATE_DONE.
437 * The DMA queue is expected to contain at least one buffer.
439 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
442 struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
444 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
445 struct isp_video_queue *queue = video->queue;
446 struct isp_video_fh *vfh =
447 container_of(queue, struct isp_video_fh, queue);
448 enum isp_pipeline_state state;
449 struct isp_video_buffer *buf;
453 spin_lock_irqsave(&video->irqlock, flags);
454 if (WARN_ON(list_empty(&video->dmaqueue))) {
455 spin_unlock_irqrestore(&video->irqlock, flags);
459 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
461 list_del(&buf->irqlist);
462 spin_unlock_irqrestore(&video->irqlock, flags);
464 buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
467 buf->vbuf.timestamp.tv_sec = ts.tv_sec;
468 buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
470 /* Do frame number propagation only if this is the output video node.
471 * Frame number either comes from the CSI receivers or it gets
472 * incremented here if H3A is not active.
473 * Note: There is no guarantee that the output buffer will finish
474 * first, so the input number might lag behind by 1 in some cases.
476 if (video == pipe->output && !pipe->do_propagation)
477 buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
479 buf->vbuf.sequence = atomic_read(&pipe->frame_number);
481 /* Report pipeline errors to userspace on the capture device side. */
482 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
483 buf->state = ISP_BUF_STATE_ERROR;
486 buf->state = ISP_BUF_STATE_DONE;
491 if (list_empty(&video->dmaqueue)) {
492 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
493 state = ISP_PIPELINE_QUEUE_OUTPUT
494 | ISP_PIPELINE_STREAM;
496 state = ISP_PIPELINE_QUEUE_INPUT
497 | ISP_PIPELINE_STREAM;
499 spin_lock_irqsave(&pipe->lock, flags);
500 pipe->state &= ~state;
501 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
502 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
503 spin_unlock_irqrestore(&pipe->lock, flags);
507 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
508 spin_lock_irqsave(&pipe->lock, flags);
509 pipe->state &= ~ISP_PIPELINE_STREAM;
510 spin_unlock_irqrestore(&pipe->lock, flags);
513 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
515 buf->state = ISP_BUF_STATE_ACTIVE;
516 return to_isp_buffer(buf);
520 * omap3isp_video_cancel_stream - Cancel stream on a video node
521 * @video: ISP video object
523 * Cancelling a stream mark all buffers on the video node as erroneous and makes
524 * sure no new buffer can be queued.
526 void omap3isp_video_cancel_stream(struct isp_video *video)
530 spin_lock_irqsave(&video->irqlock, flags);
532 while (!list_empty(&video->dmaqueue)) {
533 struct isp_video_buffer *buf;
535 buf = list_first_entry(&video->dmaqueue,
536 struct isp_video_buffer, irqlist);
537 list_del(&buf->irqlist);
539 buf->state = ISP_BUF_STATE_ERROR;
545 spin_unlock_irqrestore(&video->irqlock, flags);
549 * omap3isp_video_resume - Perform resume operation on the buffers
550 * @video: ISP video object
551 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
553 * This function is intended to be used on suspend/resume scenario. It
554 * requests video queue layer to discard buffers marked as DONE if it's in
555 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
558 void omap3isp_video_resume(struct isp_video *video, int continuous)
560 struct isp_buffer *buf = NULL;
562 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
563 mutex_lock(&video->queue_lock);
564 omap3isp_video_queue_discard_done(video->queue);
565 mutex_unlock(&video->queue_lock);
568 if (!list_empty(&video->dmaqueue)) {
569 buf = list_first_entry(&video->dmaqueue,
570 struct isp_buffer, buffer.irqlist);
571 video->ops->queue(video, buf);
572 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
575 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
579 /* -----------------------------------------------------------------------------
584 isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
586 struct isp_video *video = video_drvdata(file);
588 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
589 strlcpy(cap->card, video->video.name, sizeof(cap->card));
590 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
592 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
593 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
595 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
601 isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
603 struct isp_video_fh *vfh = to_isp_video_fh(fh);
604 struct isp_video *video = video_drvdata(file);
606 if (format->type != video->type)
609 mutex_lock(&video->mutex);
610 *format = vfh->format;
611 mutex_unlock(&video->mutex);
617 isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
619 struct isp_video_fh *vfh = to_isp_video_fh(fh);
620 struct isp_video *video = video_drvdata(file);
621 struct v4l2_mbus_framefmt fmt;
623 if (format->type != video->type)
626 mutex_lock(&video->mutex);
628 /* Fill the bytesperline and sizeimage fields by converting to media bus
629 * format and back to pixel format.
631 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
632 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
634 vfh->format = *format;
636 mutex_unlock(&video->mutex);
641 isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
643 struct isp_video *video = video_drvdata(file);
644 struct v4l2_subdev_format fmt;
645 struct v4l2_subdev *subdev;
649 if (format->type != video->type)
652 subdev = isp_video_remote_subdev(video, &pad);
656 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
659 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
660 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
662 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
664 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
669 isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
671 struct isp_video *video = video_drvdata(file);
672 struct v4l2_subdev *subdev;
675 subdev = isp_video_remote_subdev(video, NULL);
679 mutex_lock(&video->mutex);
680 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
681 mutex_unlock(&video->mutex);
683 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
687 isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
689 struct isp_video *video = video_drvdata(file);
690 struct v4l2_subdev_format format;
691 struct v4l2_subdev *subdev;
695 subdev = isp_video_remote_subdev(video, &pad);
699 /* Try the get crop operation first and fallback to get format if not
702 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
703 if (ret != -ENOIOCTLCMD)
707 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
708 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
710 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
714 crop->c.width = format.format.width;
715 crop->c.height = format.format.height;
721 isp_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
723 struct isp_video *video = video_drvdata(file);
724 struct v4l2_subdev *subdev;
727 subdev = isp_video_remote_subdev(video, NULL);
731 mutex_lock(&video->mutex);
732 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
733 mutex_unlock(&video->mutex);
735 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
739 isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
741 struct isp_video_fh *vfh = to_isp_video_fh(fh);
742 struct isp_video *video = video_drvdata(file);
744 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
745 video->type != a->type)
748 memset(a, 0, sizeof(*a));
749 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
750 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
751 a->parm.output.timeperframe = vfh->timeperframe;
757 isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
759 struct isp_video_fh *vfh = to_isp_video_fh(fh);
760 struct isp_video *video = video_drvdata(file);
762 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
763 video->type != a->type)
766 if (a->parm.output.timeperframe.denominator == 0)
767 a->parm.output.timeperframe.denominator = 1;
769 vfh->timeperframe = a->parm.output.timeperframe;
775 isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
777 struct isp_video_fh *vfh = to_isp_video_fh(fh);
778 struct isp_video *video = video_drvdata(file);
781 mutex_lock(&video->queue_lock);
782 ret = omap3isp_video_queue_reqbufs(&vfh->queue, rb);
783 mutex_unlock(&video->queue_lock);
789 isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
791 struct isp_video_fh *vfh = to_isp_video_fh(fh);
792 struct isp_video *video = video_drvdata(file);
795 mutex_lock(&video->queue_lock);
796 ret = omap3isp_video_queue_querybuf(&vfh->queue, b);
797 mutex_unlock(&video->queue_lock);
803 isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
805 struct isp_video_fh *vfh = to_isp_video_fh(fh);
806 struct isp_video *video = video_drvdata(file);
809 mutex_lock(&video->queue_lock);
810 ret = omap3isp_video_queue_qbuf(&vfh->queue, b);
811 mutex_unlock(&video->queue_lock);
817 isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
819 struct isp_video_fh *vfh = to_isp_video_fh(fh);
820 struct isp_video *video = video_drvdata(file);
823 mutex_lock(&video->queue_lock);
824 ret = omap3isp_video_queue_dqbuf(&vfh->queue, b,
825 file->f_flags & O_NONBLOCK);
826 mutex_unlock(&video->queue_lock);
831 static int isp_video_check_external_subdevs(struct isp_video *video,
832 struct isp_pipeline *pipe)
834 struct isp_device *isp = video->isp;
835 struct media_entity *ents[] = {
836 &isp->isp_csi2a.subdev.entity,
837 &isp->isp_csi2c.subdev.entity,
838 &isp->isp_ccp2.subdev.entity,
839 &isp->isp_ccdc.subdev.entity
841 struct media_pad *source_pad;
842 struct media_entity *source = NULL;
843 struct media_entity *sink;
844 struct v4l2_subdev_format fmt;
845 struct v4l2_ext_controls ctrls;
846 struct v4l2_ext_control ctrl;
850 /* Memory-to-memory pipelines have no external subdev. */
851 if (pipe->input != NULL)
854 for (i = 0; i < ARRAY_SIZE(ents); i++) {
855 /* Is the entity part of the pipeline? */
856 if (!(pipe->entities & (1 << ents[i]->id)))
859 /* ISP entities have always sink pad == 0. Find source. */
860 source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
861 if (source_pad == NULL)
864 source = source_pad->entity;
870 dev_warn(isp->dev, "can't find source, failing now\n");
874 if (media_entity_type(source) != MEDIA_ENT_T_V4L2_SUBDEV)
877 pipe->external = media_entity_to_v4l2_subdev(source);
879 fmt.pad = source_pad->index;
880 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
881 ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
882 pad, get_fmt, NULL, &fmt);
883 if (unlikely(ret < 0)) {
884 dev_warn(isp->dev, "get_fmt returned null!\n");
888 pipe->external_width =
889 omap3isp_video_format_info(fmt.format.code)->width;
891 memset(&ctrls, 0, sizeof(ctrls));
892 memset(&ctrl, 0, sizeof(ctrl));
894 ctrl.id = V4L2_CID_PIXEL_RATE;
897 ctrls.controls = &ctrl;
899 ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
901 dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
902 pipe->external->name);
906 pipe->external_rate = ctrl.value64;
908 if (pipe->entities & (1 << isp->isp_ccdc.subdev.entity.id)) {
909 unsigned int rate = UINT_MAX;
911 * Check that maximum allowed CCDC pixel rate isn't
912 * exceeded by the pixel rate.
914 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
915 if (pipe->external_rate > rate)
925 * Every ISP pipeline has a single input and a single output. The input can be
926 * either a sensor or a video node. The output is always a video node.
928 * As every pipeline has an output video node, the ISP video objects at the
929 * pipeline output stores the pipeline state. It tracks the streaming state of
930 * both the input and output, as well as the availability of buffers.
932 * In sensor-to-memory mode, frames are always available at the pipeline input.
933 * Starting the sensor usually requires I2C transfers and must be done in
934 * interruptible context. The pipeline is started and stopped synchronously
935 * to the stream on/off commands. All modules in the pipeline will get their
936 * subdev set stream handler called. The module at the end of the pipeline must
937 * delay starting the hardware until buffers are available at its output.
939 * In memory-to-memory mode, starting/stopping the stream requires
940 * synchronization between the input and output. ISP modules can't be stopped
941 * in the middle of a frame, and at least some of the modules seem to become
942 * busy as soon as they're started, even if they don't receive a frame start
943 * event. For that reason frames need to be processed in single-shot mode. The
944 * driver needs to wait until a frame is completely processed and written to
945 * memory before restarting the pipeline for the next frame. Pipelined
946 * processing might be possible but requires more testing.
948 * Stream start must be delayed until buffers are available at both the input
949 * and output. The pipeline must be started in the videobuf queue callback with
950 * the buffers queue spinlock held. The modules subdev set stream operation must
954 isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
956 struct isp_video_fh *vfh = to_isp_video_fh(fh);
957 struct isp_video *video = video_drvdata(file);
958 enum isp_pipeline_state state;
959 struct isp_pipeline *pipe;
963 if (type != video->type)
966 mutex_lock(&video->stream_lock);
968 if (video->streaming) {
969 mutex_unlock(&video->stream_lock);
973 /* Start streaming on the pipeline. No link touching an entity in the
974 * pipeline can be activated or deactivated once streaming is started.
976 pipe = video->video.entity.pipe
977 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
981 if (video->isp->pdata->set_constraints)
982 video->isp->pdata->set_constraints(video->isp, true);
983 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
984 pipe->max_rate = pipe->l3_ick;
986 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
988 goto err_pipeline_start;
990 /* Verify that the currently configured format matches the output of
991 * the connected subdev.
993 ret = isp_video_check_format(video, vfh);
995 goto err_check_format;
997 video->bpl_padding = ret;
998 video->bpl_value = vfh->format.fmt.pix.bytesperline;
1000 ret = isp_video_get_graph_data(video, pipe);
1002 goto err_check_format;
1004 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1005 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
1007 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1009 ret = isp_video_check_external_subdevs(video, pipe);
1011 goto err_check_format;
1013 pipe->error = false;
1015 spin_lock_irqsave(&pipe->lock, flags);
1016 pipe->state &= ~ISP_PIPELINE_STREAM;
1017 pipe->state |= state;
1018 spin_unlock_irqrestore(&pipe->lock, flags);
1020 /* Set the maximum time per frame as the value requested by userspace.
1021 * This is a soft limit that can be overridden if the hardware doesn't
1022 * support the request limit.
1024 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1025 pipe->max_timeperframe = vfh->timeperframe;
1027 video->queue = &vfh->queue;
1028 INIT_LIST_HEAD(&video->dmaqueue);
1029 atomic_set(&pipe->frame_number, -1);
1031 mutex_lock(&video->queue_lock);
1032 ret = omap3isp_video_queue_streamon(&vfh->queue);
1033 mutex_unlock(&video->queue_lock);
1035 goto err_check_format;
1037 /* In sensor-to-memory mode, the stream can be started synchronously
1038 * to the stream on command. In memory-to-memory mode, it will be
1039 * started when buffers are queued on both the input and output.
1041 if (pipe->input == NULL) {
1042 ret = omap3isp_pipeline_set_stream(pipe,
1043 ISP_PIPELINE_STREAM_CONTINUOUS);
1045 goto err_set_stream;
1046 spin_lock_irqsave(&video->irqlock, flags);
1047 if (list_empty(&video->dmaqueue))
1048 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1049 spin_unlock_irqrestore(&video->irqlock, flags);
1052 video->streaming = 1;
1054 mutex_unlock(&video->stream_lock);
1058 mutex_lock(&video->queue_lock);
1059 omap3isp_video_queue_streamoff(&vfh->queue);
1060 mutex_unlock(&video->queue_lock);
1062 media_entity_pipeline_stop(&video->video.entity);
1064 if (video->isp->pdata->set_constraints)
1065 video->isp->pdata->set_constraints(video->isp, false);
1066 /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1067 * will get triggered the next time the CCDC is powered up will try to
1068 * access buffers that might have been freed but still present in the
1069 * DMA queue. This can easily get triggered if the above
1070 * omap3isp_pipeline_set_stream() call fails on a system with a
1071 * free-running sensor.
1073 INIT_LIST_HEAD(&video->dmaqueue);
1074 video->queue = NULL;
1076 mutex_unlock(&video->stream_lock);
1081 isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1083 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1084 struct isp_video *video = video_drvdata(file);
1085 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1086 enum isp_pipeline_state state;
1087 unsigned int streaming;
1088 unsigned long flags;
1090 if (type != video->type)
1093 mutex_lock(&video->stream_lock);
1095 /* Make sure we're not streaming yet. */
1096 mutex_lock(&video->queue_lock);
1097 streaming = vfh->queue.streaming;
1098 mutex_unlock(&video->queue_lock);
1103 /* Update the pipeline state. */
1104 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1105 state = ISP_PIPELINE_STREAM_OUTPUT
1106 | ISP_PIPELINE_QUEUE_OUTPUT;
1108 state = ISP_PIPELINE_STREAM_INPUT
1109 | ISP_PIPELINE_QUEUE_INPUT;
1111 spin_lock_irqsave(&pipe->lock, flags);
1112 pipe->state &= ~state;
1113 spin_unlock_irqrestore(&pipe->lock, flags);
1115 /* Stop the stream. */
1116 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1117 mutex_lock(&video->queue_lock);
1118 omap3isp_video_queue_streamoff(&vfh->queue);
1119 mutex_unlock(&video->queue_lock);
1120 video->queue = NULL;
1121 video->streaming = 0;
1122 video->error = false;
1124 if (video->isp->pdata->set_constraints)
1125 video->isp->pdata->set_constraints(video->isp, false);
1126 media_entity_pipeline_stop(&video->video.entity);
1129 mutex_unlock(&video->stream_lock);
1134 isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1136 if (input->index > 0)
1139 strlcpy(input->name, "camera", sizeof(input->name));
1140 input->type = V4L2_INPUT_TYPE_CAMERA;
1146 isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1154 isp_video_s_input(struct file *file, void *fh, unsigned int input)
1156 return input == 0 ? 0 : -EINVAL;
1159 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1160 .vidioc_querycap = isp_video_querycap,
1161 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1162 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1163 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1164 .vidioc_g_fmt_vid_out = isp_video_get_format,
1165 .vidioc_s_fmt_vid_out = isp_video_set_format,
1166 .vidioc_try_fmt_vid_out = isp_video_try_format,
1167 .vidioc_cropcap = isp_video_cropcap,
1168 .vidioc_g_crop = isp_video_get_crop,
1169 .vidioc_s_crop = isp_video_set_crop,
1170 .vidioc_g_parm = isp_video_get_param,
1171 .vidioc_s_parm = isp_video_set_param,
1172 .vidioc_reqbufs = isp_video_reqbufs,
1173 .vidioc_querybuf = isp_video_querybuf,
1174 .vidioc_qbuf = isp_video_qbuf,
1175 .vidioc_dqbuf = isp_video_dqbuf,
1176 .vidioc_streamon = isp_video_streamon,
1177 .vidioc_streamoff = isp_video_streamoff,
1178 .vidioc_enum_input = isp_video_enum_input,
1179 .vidioc_g_input = isp_video_g_input,
1180 .vidioc_s_input = isp_video_s_input,
1183 /* -----------------------------------------------------------------------------
1184 * V4L2 file operations
1187 static int isp_video_open(struct file *file)
1189 struct isp_video *video = video_drvdata(file);
1190 struct isp_video_fh *handle;
1193 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1197 v4l2_fh_init(&handle->vfh, &video->video);
1198 v4l2_fh_add(&handle->vfh);
1200 /* If this is the first user, initialise the pipeline. */
1201 if (omap3isp_get(video->isp) == NULL) {
1206 ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1208 omap3isp_put(video->isp);
1212 omap3isp_video_queue_init(&handle->queue, video->type,
1213 &isp_video_queue_ops, video->isp->dev,
1214 sizeof(struct isp_buffer));
1216 memset(&handle->format, 0, sizeof(handle->format));
1217 handle->format.type = video->type;
1218 handle->timeperframe.denominator = 1;
1220 handle->video = video;
1221 file->private_data = &handle->vfh;
1225 v4l2_fh_del(&handle->vfh);
1232 static int isp_video_release(struct file *file)
1234 struct isp_video *video = video_drvdata(file);
1235 struct v4l2_fh *vfh = file->private_data;
1236 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1238 /* Disable streaming and free the buffers queue resources. */
1239 isp_video_streamoff(file, vfh, video->type);
1241 mutex_lock(&video->queue_lock);
1242 omap3isp_video_queue_cleanup(&handle->queue);
1243 mutex_unlock(&video->queue_lock);
1245 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1247 /* Release the file handle. */
1250 file->private_data = NULL;
1252 omap3isp_put(video->isp);
1257 static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1259 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1260 struct isp_video *video = video_drvdata(file);
1263 mutex_lock(&video->queue_lock);
1264 ret = omap3isp_video_queue_poll(&vfh->queue, file, wait);
1265 mutex_unlock(&video->queue_lock);
1270 static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1272 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1273 struct isp_video *video = video_drvdata(file);
1276 mutex_lock(&video->queue_lock);
1277 ret = omap3isp_video_queue_mmap(&vfh->queue, vma);
1278 mutex_unlock(&video->queue_lock);
1283 static struct v4l2_file_operations isp_video_fops = {
1284 .owner = THIS_MODULE,
1285 .unlocked_ioctl = video_ioctl2,
1286 .open = isp_video_open,
1287 .release = isp_video_release,
1288 .poll = isp_video_poll,
1289 .mmap = isp_video_mmap,
1292 /* -----------------------------------------------------------------------------
1296 static const struct isp_video_operations isp_video_dummy_ops = {
1299 int omap3isp_video_init(struct isp_video *video, const char *name)
1301 const char *direction;
1304 switch (video->type) {
1305 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1306 direction = "output";
1307 video->pad.flags = MEDIA_PAD_FL_SINK
1308 | MEDIA_PAD_FL_MUST_CONNECT;
1310 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1311 direction = "input";
1312 video->pad.flags = MEDIA_PAD_FL_SOURCE
1313 | MEDIA_PAD_FL_MUST_CONNECT;
1314 video->video.vfl_dir = VFL_DIR_TX;
1321 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1325 mutex_init(&video->mutex);
1326 atomic_set(&video->active, 0);
1328 spin_lock_init(&video->pipe.lock);
1329 mutex_init(&video->stream_lock);
1330 mutex_init(&video->queue_lock);
1331 spin_lock_init(&video->irqlock);
1333 /* Initialize the video device. */
1334 if (video->ops == NULL)
1335 video->ops = &isp_video_dummy_ops;
1337 video->video.fops = &isp_video_fops;
1338 snprintf(video->video.name, sizeof(video->video.name),
1339 "OMAP3 ISP %s %s", name, direction);
1340 video->video.vfl_type = VFL_TYPE_GRABBER;
1341 video->video.release = video_device_release_empty;
1342 video->video.ioctl_ops = &isp_video_ioctl_ops;
1343 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1345 video_set_drvdata(&video->video, video);
1350 void omap3isp_video_cleanup(struct isp_video *video)
1352 media_entity_cleanup(&video->video.entity);
1353 mutex_destroy(&video->queue_lock);
1354 mutex_destroy(&video->stream_lock);
1355 mutex_destroy(&video->mutex);
1358 int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1362 video->video.v4l2_dev = vdev;
1364 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1366 dev_err(video->isp->dev,
1367 "%s: could not register video device (%d)\n",
1373 void omap3isp_video_unregister(struct isp_video *video)
1375 if (video_is_registered(&video->video))
1376 video_unregister_device(&video->video);