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>
37 #include <plat/iommu.h>
38 #include <plat/iovmm.h>
39 #include <plat/omap-pm.h>
45 /* -----------------------------------------------------------------------------
50 * NOTE: When adding new media bus codes, always remember to add
51 * corresponding in-memory formats to the table below!!!
53 static struct isp_format_info formats[] = {
54 { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
55 V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
56 V4L2_PIX_FMT_GREY, 8, },
57 { V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10,
58 V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8,
59 V4L2_PIX_FMT_Y10, 10, },
60 { V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10,
61 V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8,
62 V4L2_PIX_FMT_Y12, 12, },
63 { V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
64 V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
65 V4L2_PIX_FMT_SBGGR8, 8, },
66 { V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
67 V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
68 V4L2_PIX_FMT_SGBRG8, 8, },
69 { V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
70 V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
71 V4L2_PIX_FMT_SGRBG8, 8, },
72 { V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
73 V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
74 V4L2_PIX_FMT_SRGGB8, 8, },
75 { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8,
76 V4L2_MBUS_FMT_SBGGR10_1X10, 0,
77 V4L2_PIX_FMT_SBGGR10DPCM8, 8, },
78 { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8,
79 V4L2_MBUS_FMT_SGBRG10_1X10, 0,
80 V4L2_PIX_FMT_SGBRG10DPCM8, 8, },
81 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
82 V4L2_MBUS_FMT_SGRBG10_1X10, 0,
83 V4L2_PIX_FMT_SGRBG10DPCM8, 8, },
84 { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8,
85 V4L2_MBUS_FMT_SRGGB10_1X10, 0,
86 V4L2_PIX_FMT_SRGGB10DPCM8, 8, },
87 { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
88 V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8,
89 V4L2_PIX_FMT_SBGGR10, 10, },
90 { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
91 V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8,
92 V4L2_PIX_FMT_SGBRG10, 10, },
93 { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
94 V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8,
95 V4L2_PIX_FMT_SGRBG10, 10, },
96 { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
97 V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8,
98 V4L2_PIX_FMT_SRGGB10, 10, },
99 { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
100 V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8,
101 V4L2_PIX_FMT_SBGGR12, 12, },
102 { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
103 V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8,
104 V4L2_PIX_FMT_SGBRG12, 12, },
105 { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
106 V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8,
107 V4L2_PIX_FMT_SGRBG12, 12, },
108 { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
109 V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8,
110 V4L2_PIX_FMT_SRGGB12, 12, },
111 { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
112 V4L2_MBUS_FMT_UYVY8_1X16, 0,
113 V4L2_PIX_FMT_UYVY, 16, },
114 { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
115 V4L2_MBUS_FMT_YUYV8_1X16, 0,
116 V4L2_PIX_FMT_YUYV, 16, },
119 const struct isp_format_info *
120 omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
124 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
125 if (formats[i].code == code)
133 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
134 * @video: ISP video instance
135 * @mbus: v4l2_mbus_framefmt format (input)
136 * @pix: v4l2_pix_format format (output)
138 * Fill the output pix structure with information from the input mbus format.
139 * The bytesperline and sizeimage fields are computed from the requested bytes
140 * per line value in the pix format and information from the video instance.
142 * Return the number of padding bytes at end of line.
144 static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
145 const struct v4l2_mbus_framefmt *mbus,
146 struct v4l2_pix_format *pix)
148 unsigned int bpl = pix->bytesperline;
149 unsigned int min_bpl;
152 memset(pix, 0, sizeof(*pix));
153 pix->width = mbus->width;
154 pix->height = mbus->height;
156 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
157 if (formats[i].code == mbus->code)
161 if (WARN_ON(i == ARRAY_SIZE(formats)))
164 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
166 /* Clamp the requested bytes per line value. If the maximum bytes per
167 * line value is zero, the module doesn't support user configurable line
168 * sizes. Override the requested value with the minimum in that case.
171 bpl = clamp(bpl, min_bpl, video->bpl_max);
175 if (!video->bpl_zero_padding || bpl != min_bpl)
176 bpl = ALIGN(bpl, video->bpl_alignment);
178 pix->pixelformat = formats[i].pixelformat;
179 pix->bytesperline = bpl;
180 pix->sizeimage = pix->bytesperline * pix->height;
181 pix->colorspace = mbus->colorspace;
182 pix->field = mbus->field;
184 return bpl - min_bpl;
187 static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
188 struct v4l2_mbus_framefmt *mbus)
192 memset(mbus, 0, sizeof(*mbus));
193 mbus->width = pix->width;
194 mbus->height = pix->height;
196 /* Skip the last format in the loop so that it will be selected if no
199 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
200 if (formats[i].pixelformat == pix->pixelformat)
204 mbus->code = formats[i].code;
205 mbus->colorspace = pix->colorspace;
206 mbus->field = pix->field;
209 static struct v4l2_subdev *
210 isp_video_remote_subdev(struct isp_video *video, u32 *pad)
212 struct media_pad *remote;
214 remote = media_entity_remote_source(&video->pad);
216 if (remote == NULL ||
217 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
221 *pad = remote->index;
223 return media_entity_to_v4l2_subdev(remote->entity);
226 /* Return a pointer to the ISP video instance at the far end of the pipeline. */
227 static int isp_video_get_graph_data(struct isp_video *video,
228 struct isp_pipeline *pipe)
230 struct media_entity_graph graph;
231 struct media_entity *entity = &video->video.entity;
232 struct media_device *mdev = entity->parent;
233 struct isp_video *far_end = NULL;
235 mutex_lock(&mdev->graph_mutex);
236 media_entity_graph_walk_start(&graph, entity);
238 while ((entity = media_entity_graph_walk_next(&graph))) {
239 struct isp_video *__video;
241 pipe->entities |= 1 << entity->id;
246 if (entity == &video->video.entity)
249 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
252 __video = to_isp_video(media_entity_to_video_device(entity));
253 if (__video->type != video->type)
257 mutex_unlock(&mdev->graph_mutex);
259 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
260 pipe->input = far_end;
261 pipe->output = video;
267 pipe->output = far_end;
274 * Validate a pipeline by checking both ends of all links for format
277 * Compute the minimum time per frame value as the maximum of time per frame
278 * limits reported by every block in the pipeline.
280 * Return 0 if all formats match, or -EPIPE if at least one link is found with
281 * different formats on its two ends or if the pipeline doesn't start with a
282 * video source (either a subdev with no input pad, or a non-subdev entity).
284 static int isp_video_validate_pipeline(struct isp_pipeline *pipe)
286 struct isp_device *isp = pipe->output->isp;
287 struct media_pad *pad;
288 struct v4l2_subdev *subdev;
290 subdev = isp_video_remote_subdev(pipe->output, NULL);
295 /* Retrieve the sink format */
296 pad = &subdev->entity.pads[0];
297 if (!(pad->flags & MEDIA_PAD_FL_SINK))
300 /* Update the maximum frame rate */
301 if (subdev == &isp->isp_res.subdev)
302 omap3isp_resizer_max_rate(&isp->isp_res,
305 /* Retrieve the source format. Return an error if no source
306 * entity can be found, and stop checking the pipeline if the
307 * source entity isn't a subdev.
309 pad = media_entity_remote_source(pad);
313 if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
316 subdev = media_entity_to_v4l2_subdev(pad->entity);
323 __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
325 struct v4l2_subdev_format fmt;
326 struct v4l2_subdev *subdev;
330 subdev = isp_video_remote_subdev(video, &pad);
334 mutex_lock(&video->mutex);
337 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
338 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
339 if (ret == -ENOIOCTLCMD)
342 mutex_unlock(&video->mutex);
347 format->type = video->type;
348 return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
352 isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
354 struct v4l2_format format;
357 memcpy(&format, &vfh->format, sizeof(format));
358 ret = __isp_video_get_format(video, &format);
362 if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
363 vfh->format.fmt.pix.height != format.fmt.pix.height ||
364 vfh->format.fmt.pix.width != format.fmt.pix.width ||
365 vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
366 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
372 /* -----------------------------------------------------------------------------
376 #define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
379 * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
380 * @dev: Device pointer specific to the OMAP3 ISP.
381 * @sglist: Pointer to source Scatter gather list to allocate.
382 * @sglen: Number of elements of the scatter-gatter list.
384 * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
385 * we ran out of memory.
388 ispmmu_vmap(struct isp_device *isp, const struct scatterlist *sglist, int sglen)
390 struct sg_table *sgt;
393 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
397 sgt->sgl = (struct scatterlist *)sglist;
399 sgt->orig_nents = sglen;
401 da = omap_iommu_vmap(isp->domain, isp->dev, 0, sgt, IOMMU_FLAG);
402 if (IS_ERR_VALUE(da))
409 * ispmmu_vunmap - Unmap a device address from the ISP MMU
410 * @dev: Device pointer specific to the OMAP3 ISP.
411 * @da: Device address generated from a ispmmu_vmap call.
413 static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
415 struct sg_table *sgt;
417 sgt = omap_iommu_vunmap(isp->domain, isp->dev, (u32)da);
421 /* -----------------------------------------------------------------------------
422 * Video queue operations
425 static void isp_video_queue_prepare(struct isp_video_queue *queue,
426 unsigned int *nbuffers, unsigned int *size)
428 struct isp_video_fh *vfh =
429 container_of(queue, struct isp_video_fh, queue);
430 struct isp_video *video = vfh->video;
432 *size = vfh->format.fmt.pix.sizeimage;
436 *nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size));
439 static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
441 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
442 struct isp_buffer *buffer = to_isp_buffer(buf);
443 struct isp_video *video = vfh->video;
445 if (buffer->isp_addr) {
446 ispmmu_vunmap(video->isp, buffer->isp_addr);
447 buffer->isp_addr = 0;
451 static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
453 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
454 struct isp_buffer *buffer = to_isp_buffer(buf);
455 struct isp_video *video = vfh->video;
458 addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen);
459 if (IS_ERR_VALUE(addr))
462 if (!IS_ALIGNED(addr, 32)) {
463 dev_dbg(video->isp->dev, "Buffer address must be "
464 "aligned to 32 bytes boundary.\n");
465 ispmmu_vunmap(video->isp, buffer->isp_addr);
469 buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
470 buffer->isp_addr = addr;
475 * isp_video_buffer_queue - Add buffer to streaming queue
478 * In memory-to-memory mode, start streaming on the pipeline if buffers are
479 * queued on both the input and the output, if the pipeline isn't already busy.
480 * If the pipeline is busy, it will be restarted in the output module interrupt
483 static void isp_video_buffer_queue(struct isp_video_buffer *buf)
485 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
486 struct isp_buffer *buffer = to_isp_buffer(buf);
487 struct isp_video *video = vfh->video;
488 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
489 enum isp_pipeline_state state;
494 empty = list_empty(&video->dmaqueue);
495 list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
498 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
499 state = ISP_PIPELINE_QUEUE_OUTPUT;
501 state = ISP_PIPELINE_QUEUE_INPUT;
503 spin_lock_irqsave(&pipe->lock, flags);
504 pipe->state |= state;
505 video->ops->queue(video, buffer);
506 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
508 start = isp_pipeline_ready(pipe);
510 pipe->state |= ISP_PIPELINE_STREAM;
511 spin_unlock_irqrestore(&pipe->lock, flags);
514 omap3isp_pipeline_set_stream(pipe,
515 ISP_PIPELINE_STREAM_SINGLESHOT);
519 static const struct isp_video_queue_operations isp_video_queue_ops = {
520 .queue_prepare = &isp_video_queue_prepare,
521 .buffer_prepare = &isp_video_buffer_prepare,
522 .buffer_queue = &isp_video_buffer_queue,
523 .buffer_cleanup = &isp_video_buffer_cleanup,
527 * omap3isp_video_buffer_next - Complete the current buffer and return the next
528 * @video: ISP video object
530 * Remove the current video buffer from the DMA queue and fill its timestamp,
531 * field count and state fields before waking up its completion handler.
533 * For capture video nodes the buffer state is set to ISP_BUF_STATE_DONE if no
534 * error has been flagged in the pipeline, or to ISP_BUF_STATE_ERROR otherwise.
535 * For video output nodes the buffer state is always set to ISP_BUF_STATE_DONE.
537 * The DMA queue is expected to contain at least one buffer.
539 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
542 struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
544 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
545 struct isp_video_queue *queue = video->queue;
546 enum isp_pipeline_state state;
547 struct isp_video_buffer *buf;
551 spin_lock_irqsave(&queue->irqlock, flags);
552 if (WARN_ON(list_empty(&video->dmaqueue))) {
553 spin_unlock_irqrestore(&queue->irqlock, flags);
557 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
559 list_del(&buf->irqlist);
560 spin_unlock_irqrestore(&queue->irqlock, flags);
563 buf->vbuf.timestamp.tv_sec = ts.tv_sec;
564 buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
566 /* Do frame number propagation only if this is the output video node.
567 * Frame number either comes from the CSI receivers or it gets
568 * incremented here if H3A is not active.
569 * Note: There is no guarantee that the output buffer will finish
570 * first, so the input number might lag behind by 1 in some cases.
572 if (video == pipe->output && !pipe->do_propagation)
573 buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
575 buf->vbuf.sequence = atomic_read(&pipe->frame_number);
577 /* Report pipeline errors to userspace on the capture device side. */
578 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
579 buf->state = ISP_BUF_STATE_ERROR;
582 buf->state = ISP_BUF_STATE_DONE;
587 if (list_empty(&video->dmaqueue)) {
588 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
589 state = ISP_PIPELINE_QUEUE_OUTPUT
590 | ISP_PIPELINE_STREAM;
592 state = ISP_PIPELINE_QUEUE_INPUT
593 | ISP_PIPELINE_STREAM;
595 spin_lock_irqsave(&pipe->lock, flags);
596 pipe->state &= ~state;
597 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
598 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
599 spin_unlock_irqrestore(&pipe->lock, flags);
603 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
604 spin_lock_irqsave(&pipe->lock, flags);
605 pipe->state &= ~ISP_PIPELINE_STREAM;
606 spin_unlock_irqrestore(&pipe->lock, flags);
609 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
611 buf->state = ISP_BUF_STATE_ACTIVE;
612 return to_isp_buffer(buf);
616 * omap3isp_video_resume - Perform resume operation on the buffers
617 * @video: ISP video object
618 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
620 * This function is intended to be used on suspend/resume scenario. It
621 * requests video queue layer to discard buffers marked as DONE if it's in
622 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
625 void omap3isp_video_resume(struct isp_video *video, int continuous)
627 struct isp_buffer *buf = NULL;
629 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
630 omap3isp_video_queue_discard_done(video->queue);
632 if (!list_empty(&video->dmaqueue)) {
633 buf = list_first_entry(&video->dmaqueue,
634 struct isp_buffer, buffer.irqlist);
635 video->ops->queue(video, buf);
636 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
639 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
643 /* -----------------------------------------------------------------------------
648 isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
650 struct isp_video *video = video_drvdata(file);
652 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
653 strlcpy(cap->card, video->video.name, sizeof(cap->card));
654 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
656 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
657 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
659 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
665 isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
667 struct isp_video_fh *vfh = to_isp_video_fh(fh);
668 struct isp_video *video = video_drvdata(file);
670 if (format->type != video->type)
673 mutex_lock(&video->mutex);
674 *format = vfh->format;
675 mutex_unlock(&video->mutex);
681 isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
683 struct isp_video_fh *vfh = to_isp_video_fh(fh);
684 struct isp_video *video = video_drvdata(file);
685 struct v4l2_mbus_framefmt fmt;
687 if (format->type != video->type)
690 mutex_lock(&video->mutex);
692 /* Fill the bytesperline and sizeimage fields by converting to media bus
693 * format and back to pixel format.
695 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
696 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
698 vfh->format = *format;
700 mutex_unlock(&video->mutex);
705 isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
707 struct isp_video *video = video_drvdata(file);
708 struct v4l2_subdev_format fmt;
709 struct v4l2_subdev *subdev;
713 if (format->type != video->type)
716 subdev = isp_video_remote_subdev(video, &pad);
720 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
723 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
724 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
726 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
728 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
733 isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
735 struct isp_video *video = video_drvdata(file);
736 struct v4l2_subdev *subdev;
739 subdev = isp_video_remote_subdev(video, NULL);
743 mutex_lock(&video->mutex);
744 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
745 mutex_unlock(&video->mutex);
747 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
751 isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
753 struct isp_video *video = video_drvdata(file);
754 struct v4l2_subdev_format format;
755 struct v4l2_subdev *subdev;
759 subdev = isp_video_remote_subdev(video, &pad);
763 /* Try the get crop operation first and fallback to get format if not
766 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
767 if (ret != -ENOIOCTLCMD)
771 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
772 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
774 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
778 crop->c.width = format.format.width;
779 crop->c.height = format.format.height;
785 isp_video_set_crop(struct file *file, void *fh, struct v4l2_crop *crop)
787 struct isp_video *video = video_drvdata(file);
788 struct v4l2_subdev *subdev;
791 subdev = isp_video_remote_subdev(video, NULL);
795 mutex_lock(&video->mutex);
796 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
797 mutex_unlock(&video->mutex);
799 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
803 isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
805 struct isp_video_fh *vfh = to_isp_video_fh(fh);
806 struct isp_video *video = video_drvdata(file);
808 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
809 video->type != a->type)
812 memset(a, 0, sizeof(*a));
813 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
814 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
815 a->parm.output.timeperframe = vfh->timeperframe;
821 isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
823 struct isp_video_fh *vfh = to_isp_video_fh(fh);
824 struct isp_video *video = video_drvdata(file);
826 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
827 video->type != a->type)
830 if (a->parm.output.timeperframe.denominator == 0)
831 a->parm.output.timeperframe.denominator = 1;
833 vfh->timeperframe = a->parm.output.timeperframe;
839 isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
841 struct isp_video_fh *vfh = to_isp_video_fh(fh);
843 return omap3isp_video_queue_reqbufs(&vfh->queue, rb);
847 isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
849 struct isp_video_fh *vfh = to_isp_video_fh(fh);
851 return omap3isp_video_queue_querybuf(&vfh->queue, b);
855 isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
857 struct isp_video_fh *vfh = to_isp_video_fh(fh);
859 return omap3isp_video_queue_qbuf(&vfh->queue, b);
863 isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
865 struct isp_video_fh *vfh = to_isp_video_fh(fh);
867 return omap3isp_video_queue_dqbuf(&vfh->queue, b,
868 file->f_flags & O_NONBLOCK);
871 static int isp_video_check_external_subdevs(struct isp_video *video,
872 struct isp_pipeline *pipe)
874 struct isp_device *isp = video->isp;
875 struct media_entity *ents[] = {
876 &isp->isp_csi2a.subdev.entity,
877 &isp->isp_csi2c.subdev.entity,
878 &isp->isp_ccp2.subdev.entity,
879 &isp->isp_ccdc.subdev.entity
881 struct media_pad *source_pad;
882 struct media_entity *source = NULL;
883 struct media_entity *sink;
884 struct v4l2_subdev_format fmt;
885 struct v4l2_ext_controls ctrls;
886 struct v4l2_ext_control ctrl;
890 for (i = 0; i < ARRAY_SIZE(ents); i++) {
891 /* Is the entity part of the pipeline? */
892 if (!(pipe->entities & (1 << ents[i]->id)))
895 /* ISP entities have always sink pad == 0. Find source. */
896 source_pad = media_entity_remote_source(&ents[i]->pads[0]);
897 if (source_pad == NULL)
900 source = source_pad->entity;
906 dev_warn(isp->dev, "can't find source, failing now\n");
910 if (media_entity_type(source) != MEDIA_ENT_T_V4L2_SUBDEV)
913 pipe->external = media_entity_to_v4l2_subdev(source);
915 fmt.pad = source_pad->index;
916 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
917 ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
918 pad, get_fmt, NULL, &fmt);
919 if (unlikely(ret < 0)) {
920 dev_warn(isp->dev, "get_fmt returned null!\n");
924 pipe->external_bpp = omap3isp_video_format_info(fmt.format.code)->bpp;
926 memset(&ctrls, 0, sizeof(ctrls));
927 memset(&ctrl, 0, sizeof(ctrl));
929 ctrl.id = V4L2_CID_PIXEL_RATE;
932 ctrls.controls = &ctrl;
934 ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
936 dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
937 pipe->external->name);
941 pipe->external_rate = ctrl.value64;
943 if (pipe->entities & (1 << isp->isp_ccdc.subdev.entity.id)) {
944 unsigned int rate = UINT_MAX;
946 * Check that maximum allowed CCDC pixel rate isn't
947 * exceeded by the pixel rate.
949 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
950 if (pipe->external_rate > rate)
960 * Every ISP pipeline has a single input and a single output. The input can be
961 * either a sensor or a video node. The output is always a video node.
963 * As every pipeline has an output video node, the ISP video objects at the
964 * pipeline output stores the pipeline state. It tracks the streaming state of
965 * both the input and output, as well as the availability of buffers.
967 * In sensor-to-memory mode, frames are always available at the pipeline input.
968 * Starting the sensor usually requires I2C transfers and must be done in
969 * interruptible context. The pipeline is started and stopped synchronously
970 * to the stream on/off commands. All modules in the pipeline will get their
971 * subdev set stream handler called. The module at the end of the pipeline must
972 * delay starting the hardware until buffers are available at its output.
974 * In memory-to-memory mode, starting/stopping the stream requires
975 * synchronization between the input and output. ISP modules can't be stopped
976 * in the middle of a frame, and at least some of the modules seem to become
977 * busy as soon as they're started, even if they don't receive a frame start
978 * event. For that reason frames need to be processed in single-shot mode. The
979 * driver needs to wait until a frame is completely processed and written to
980 * memory before restarting the pipeline for the next frame. Pipelined
981 * processing might be possible but requires more testing.
983 * Stream start must be delayed until buffers are available at both the input
984 * and output. The pipeline must be started in the videobuf queue callback with
985 * the buffers queue spinlock held. The modules subdev set stream operation must
989 isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
991 struct isp_video_fh *vfh = to_isp_video_fh(fh);
992 struct isp_video *video = video_drvdata(file);
993 enum isp_pipeline_state state;
994 struct isp_pipeline *pipe;
998 if (type != video->type)
1001 mutex_lock(&video->stream_lock);
1003 if (video->streaming) {
1004 mutex_unlock(&video->stream_lock);
1008 /* Start streaming on the pipeline. No link touching an entity in the
1009 * pipeline can be activated or deactivated once streaming is started.
1011 pipe = video->video.entity.pipe
1012 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
1016 if (video->isp->pdata->set_constraints)
1017 video->isp->pdata->set_constraints(video->isp, true);
1018 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1019 pipe->max_rate = pipe->l3_ick;
1021 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
1023 goto err_pipeline_start;
1025 /* Verify that the currently configured format matches the output of
1026 * the connected subdev.
1028 ret = isp_video_check_format(video, vfh);
1030 goto err_check_format;
1032 video->bpl_padding = ret;
1033 video->bpl_value = vfh->format.fmt.pix.bytesperline;
1035 ret = isp_video_get_graph_data(video, pipe);
1037 goto err_check_format;
1039 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1040 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
1042 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1044 ret = isp_video_check_external_subdevs(video, pipe);
1046 goto err_check_format;
1048 /* Validate the pipeline and update its state. */
1049 ret = isp_video_validate_pipeline(pipe);
1051 goto err_check_format;
1053 pipe->error = false;
1055 spin_lock_irqsave(&pipe->lock, flags);
1056 pipe->state &= ~ISP_PIPELINE_STREAM;
1057 pipe->state |= state;
1058 spin_unlock_irqrestore(&pipe->lock, flags);
1060 /* Set the maximum time per frame as the value requested by userspace.
1061 * This is a soft limit that can be overridden if the hardware doesn't
1062 * support the request limit.
1064 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1065 pipe->max_timeperframe = vfh->timeperframe;
1067 video->queue = &vfh->queue;
1068 INIT_LIST_HEAD(&video->dmaqueue);
1069 atomic_set(&pipe->frame_number, -1);
1071 ret = omap3isp_video_queue_streamon(&vfh->queue);
1073 goto err_check_format;
1075 /* In sensor-to-memory mode, the stream can be started synchronously
1076 * to the stream on command. In memory-to-memory mode, it will be
1077 * started when buffers are queued on both the input and output.
1079 if (pipe->input == NULL) {
1080 ret = omap3isp_pipeline_set_stream(pipe,
1081 ISP_PIPELINE_STREAM_CONTINUOUS);
1083 goto err_set_stream;
1084 spin_lock_irqsave(&video->queue->irqlock, flags);
1085 if (list_empty(&video->dmaqueue))
1086 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1087 spin_unlock_irqrestore(&video->queue->irqlock, flags);
1090 video->streaming = 1;
1092 mutex_unlock(&video->stream_lock);
1096 omap3isp_video_queue_streamoff(&vfh->queue);
1098 media_entity_pipeline_stop(&video->video.entity);
1100 if (video->isp->pdata->set_constraints)
1101 video->isp->pdata->set_constraints(video->isp, false);
1102 /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1103 * will get triggered the next time the CCDC is powered up will try to
1104 * access buffers that might have been freed but still present in the
1105 * DMA queue. This can easily get triggered if the above
1106 * omap3isp_pipeline_set_stream() call fails on a system with a
1107 * free-running sensor.
1109 INIT_LIST_HEAD(&video->dmaqueue);
1110 video->queue = NULL;
1112 mutex_unlock(&video->stream_lock);
1117 isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1119 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1120 struct isp_video *video = video_drvdata(file);
1121 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1122 enum isp_pipeline_state state;
1123 unsigned int streaming;
1124 unsigned long flags;
1126 if (type != video->type)
1129 mutex_lock(&video->stream_lock);
1131 /* Make sure we're not streaming yet. */
1132 mutex_lock(&vfh->queue.lock);
1133 streaming = vfh->queue.streaming;
1134 mutex_unlock(&vfh->queue.lock);
1139 /* Update the pipeline state. */
1140 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1141 state = ISP_PIPELINE_STREAM_OUTPUT
1142 | ISP_PIPELINE_QUEUE_OUTPUT;
1144 state = ISP_PIPELINE_STREAM_INPUT
1145 | ISP_PIPELINE_QUEUE_INPUT;
1147 spin_lock_irqsave(&pipe->lock, flags);
1148 pipe->state &= ~state;
1149 spin_unlock_irqrestore(&pipe->lock, flags);
1151 /* Stop the stream. */
1152 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1153 omap3isp_video_queue_streamoff(&vfh->queue);
1154 video->queue = NULL;
1155 video->streaming = 0;
1157 if (video->isp->pdata->set_constraints)
1158 video->isp->pdata->set_constraints(video->isp, false);
1159 media_entity_pipeline_stop(&video->video.entity);
1162 mutex_unlock(&video->stream_lock);
1167 isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1169 if (input->index > 0)
1172 strlcpy(input->name, "camera", sizeof(input->name));
1173 input->type = V4L2_INPUT_TYPE_CAMERA;
1179 isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1187 isp_video_s_input(struct file *file, void *fh, unsigned int input)
1189 return input == 0 ? 0 : -EINVAL;
1192 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1193 .vidioc_querycap = isp_video_querycap,
1194 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1195 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1196 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1197 .vidioc_g_fmt_vid_out = isp_video_get_format,
1198 .vidioc_s_fmt_vid_out = isp_video_set_format,
1199 .vidioc_try_fmt_vid_out = isp_video_try_format,
1200 .vidioc_cropcap = isp_video_cropcap,
1201 .vidioc_g_crop = isp_video_get_crop,
1202 .vidioc_s_crop = isp_video_set_crop,
1203 .vidioc_g_parm = isp_video_get_param,
1204 .vidioc_s_parm = isp_video_set_param,
1205 .vidioc_reqbufs = isp_video_reqbufs,
1206 .vidioc_querybuf = isp_video_querybuf,
1207 .vidioc_qbuf = isp_video_qbuf,
1208 .vidioc_dqbuf = isp_video_dqbuf,
1209 .vidioc_streamon = isp_video_streamon,
1210 .vidioc_streamoff = isp_video_streamoff,
1211 .vidioc_enum_input = isp_video_enum_input,
1212 .vidioc_g_input = isp_video_g_input,
1213 .vidioc_s_input = isp_video_s_input,
1216 /* -----------------------------------------------------------------------------
1217 * V4L2 file operations
1220 static int isp_video_open(struct file *file)
1222 struct isp_video *video = video_drvdata(file);
1223 struct isp_video_fh *handle;
1226 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1230 v4l2_fh_init(&handle->vfh, &video->video);
1231 v4l2_fh_add(&handle->vfh);
1233 /* If this is the first user, initialise the pipeline. */
1234 if (omap3isp_get(video->isp) == NULL) {
1239 ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1241 omap3isp_put(video->isp);
1245 omap3isp_video_queue_init(&handle->queue, video->type,
1246 &isp_video_queue_ops, video->isp->dev,
1247 sizeof(struct isp_buffer));
1249 memset(&handle->format, 0, sizeof(handle->format));
1250 handle->format.type = video->type;
1251 handle->timeperframe.denominator = 1;
1253 handle->video = video;
1254 file->private_data = &handle->vfh;
1258 v4l2_fh_del(&handle->vfh);
1265 static int isp_video_release(struct file *file)
1267 struct isp_video *video = video_drvdata(file);
1268 struct v4l2_fh *vfh = file->private_data;
1269 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1271 /* Disable streaming and free the buffers queue resources. */
1272 isp_video_streamoff(file, vfh, video->type);
1274 mutex_lock(&handle->queue.lock);
1275 omap3isp_video_queue_cleanup(&handle->queue);
1276 mutex_unlock(&handle->queue.lock);
1278 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1280 /* Release the file handle. */
1283 file->private_data = NULL;
1285 omap3isp_put(video->isp);
1290 static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1292 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1293 struct isp_video_queue *queue = &vfh->queue;
1295 return omap3isp_video_queue_poll(queue, file, wait);
1298 static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1300 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1302 return omap3isp_video_queue_mmap(&vfh->queue, vma);
1305 static struct v4l2_file_operations isp_video_fops = {
1306 .owner = THIS_MODULE,
1307 .unlocked_ioctl = video_ioctl2,
1308 .open = isp_video_open,
1309 .release = isp_video_release,
1310 .poll = isp_video_poll,
1311 .mmap = isp_video_mmap,
1314 /* -----------------------------------------------------------------------------
1318 static const struct isp_video_operations isp_video_dummy_ops = {
1321 int omap3isp_video_init(struct isp_video *video, const char *name)
1323 const char *direction;
1326 switch (video->type) {
1327 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1328 direction = "output";
1329 video->pad.flags = MEDIA_PAD_FL_SINK;
1331 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1332 direction = "input";
1333 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1340 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1344 mutex_init(&video->mutex);
1345 atomic_set(&video->active, 0);
1347 spin_lock_init(&video->pipe.lock);
1348 mutex_init(&video->stream_lock);
1350 /* Initialize the video device. */
1351 if (video->ops == NULL)
1352 video->ops = &isp_video_dummy_ops;
1354 video->video.fops = &isp_video_fops;
1355 snprintf(video->video.name, sizeof(video->video.name),
1356 "OMAP3 ISP %s %s", name, direction);
1357 video->video.vfl_type = VFL_TYPE_GRABBER;
1358 video->video.release = video_device_release_empty;
1359 video->video.ioctl_ops = &isp_video_ioctl_ops;
1360 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1362 video_set_drvdata(&video->video, video);
1367 void omap3isp_video_cleanup(struct isp_video *video)
1369 media_entity_cleanup(&video->video.entity);
1370 mutex_destroy(&video->stream_lock);
1371 mutex_destroy(&video->mutex);
1374 int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1378 video->video.v4l2_dev = vdev;
1380 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1382 printk(KERN_ERR "%s: could not register video device (%d)\n",
1388 void omap3isp_video_unregister(struct isp_video *video)
1390 if (video_is_registered(&video->video))
1391 video_unregister_device(&video->video);