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/omap-iommu.h>
31 #include <linux/pagemap.h>
32 #include <linux/scatterlist.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/vmalloc.h>
36 #include <media/v4l2-dev.h>
37 #include <media/v4l2-ioctl.h>
43 /* -----------------------------------------------------------------------------
48 * NOTE: When adding new media bus codes, always remember to add
49 * corresponding in-memory formats to the table below!!!
51 static struct isp_format_info formats[] = {
52 { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
53 V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
54 V4L2_PIX_FMT_GREY, 8, 1, },
55 { V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10,
56 V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8,
57 V4L2_PIX_FMT_Y10, 10, 2, },
58 { V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10,
59 V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8,
60 V4L2_PIX_FMT_Y12, 12, 2, },
61 { V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
62 V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
63 V4L2_PIX_FMT_SBGGR8, 8, 1, },
64 { V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
65 V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
66 V4L2_PIX_FMT_SGBRG8, 8, 1, },
67 { V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
68 V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
69 V4L2_PIX_FMT_SGRBG8, 8, 1, },
70 { V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
71 V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
72 V4L2_PIX_FMT_SRGGB8, 8, 1, },
73 { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8,
74 V4L2_MBUS_FMT_SBGGR10_1X10, 0,
75 V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, },
76 { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8,
77 V4L2_MBUS_FMT_SGBRG10_1X10, 0,
78 V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, },
79 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
80 V4L2_MBUS_FMT_SGRBG10_1X10, 0,
81 V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, },
82 { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8,
83 V4L2_MBUS_FMT_SRGGB10_1X10, 0,
84 V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, },
85 { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
86 V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8,
87 V4L2_PIX_FMT_SBGGR10, 10, 2, },
88 { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
89 V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8,
90 V4L2_PIX_FMT_SGBRG10, 10, 2, },
91 { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
92 V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8,
93 V4L2_PIX_FMT_SGRBG10, 10, 2, },
94 { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
95 V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8,
96 V4L2_PIX_FMT_SRGGB10, 10, 2, },
97 { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
98 V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8,
99 V4L2_PIX_FMT_SBGGR12, 12, 2, },
100 { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
101 V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8,
102 V4L2_PIX_FMT_SGBRG12, 12, 2, },
103 { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
104 V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8,
105 V4L2_PIX_FMT_SGRBG12, 12, 2, },
106 { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
107 V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8,
108 V4L2_PIX_FMT_SRGGB12, 12, 2, },
109 { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
110 V4L2_MBUS_FMT_UYVY8_1X16, 0,
111 V4L2_PIX_FMT_UYVY, 16, 2, },
112 { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
113 V4L2_MBUS_FMT_YUYV8_1X16, 0,
114 V4L2_PIX_FMT_YUYV, 16, 2, },
115 { V4L2_MBUS_FMT_UYVY8_2X8, V4L2_MBUS_FMT_UYVY8_2X8,
116 V4L2_MBUS_FMT_UYVY8_2X8, 0,
117 V4L2_PIX_FMT_UYVY, 8, 2, },
118 { V4L2_MBUS_FMT_YUYV8_2X8, V4L2_MBUS_FMT_YUYV8_2X8,
119 V4L2_MBUS_FMT_YUYV8_2X8, 0,
120 V4L2_PIX_FMT_YUYV, 8, 2, },
121 /* Empty entry to catch the unsupported pixel code (0) used by the CCDC
122 * module and avoid NULL pointer dereferences.
127 const struct isp_format_info *
128 omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
132 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
133 if (formats[i].code == code)
141 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
142 * @video: ISP video instance
143 * @mbus: v4l2_mbus_framefmt format (input)
144 * @pix: v4l2_pix_format format (output)
146 * Fill the output pix structure with information from the input mbus format.
147 * The bytesperline and sizeimage fields are computed from the requested bytes
148 * per line value in the pix format and information from the video instance.
150 * Return the number of padding bytes at end of line.
152 static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
153 const struct v4l2_mbus_framefmt *mbus,
154 struct v4l2_pix_format *pix)
156 unsigned int bpl = pix->bytesperline;
157 unsigned int min_bpl;
160 memset(pix, 0, sizeof(*pix));
161 pix->width = mbus->width;
162 pix->height = mbus->height;
164 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
165 if (formats[i].code == mbus->code)
169 if (WARN_ON(i == ARRAY_SIZE(formats)))
172 min_bpl = pix->width * formats[i].bpp;
174 /* Clamp the requested bytes per line value. If the maximum bytes per
175 * line value is zero, the module doesn't support user configurable line
176 * sizes. Override the requested value with the minimum in that case.
179 bpl = clamp(bpl, min_bpl, video->bpl_max);
183 if (!video->bpl_zero_padding || bpl != min_bpl)
184 bpl = ALIGN(bpl, video->bpl_alignment);
186 pix->pixelformat = formats[i].pixelformat;
187 pix->bytesperline = bpl;
188 pix->sizeimage = pix->bytesperline * pix->height;
189 pix->colorspace = mbus->colorspace;
190 pix->field = mbus->field;
192 return bpl - min_bpl;
195 static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
196 struct v4l2_mbus_framefmt *mbus)
200 memset(mbus, 0, sizeof(*mbus));
201 mbus->width = pix->width;
202 mbus->height = pix->height;
204 /* Skip the last format in the loop so that it will be selected if no
207 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
208 if (formats[i].pixelformat == pix->pixelformat)
212 mbus->code = formats[i].code;
213 mbus->colorspace = pix->colorspace;
214 mbus->field = pix->field;
217 static struct v4l2_subdev *
218 isp_video_remote_subdev(struct isp_video *video, u32 *pad)
220 struct media_pad *remote;
222 remote = media_entity_remote_pad(&video->pad);
224 if (remote == NULL ||
225 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
229 *pad = remote->index;
231 return media_entity_to_v4l2_subdev(remote->entity);
234 /* Return a pointer to the ISP video instance at the far end of the pipeline. */
235 static int isp_video_get_graph_data(struct isp_video *video,
236 struct isp_pipeline *pipe)
238 struct media_entity_graph graph;
239 struct media_entity *entity = &video->video.entity;
240 struct media_device *mdev = entity->parent;
241 struct isp_video *far_end = NULL;
243 mutex_lock(&mdev->graph_mutex);
244 media_entity_graph_walk_start(&graph, entity);
246 while ((entity = media_entity_graph_walk_next(&graph))) {
247 struct isp_video *__video;
249 pipe->entities |= 1 << entity->id;
254 if (entity == &video->video.entity)
257 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
260 __video = to_isp_video(media_entity_to_video_device(entity));
261 if (__video->type != video->type)
265 mutex_unlock(&mdev->graph_mutex);
267 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
268 pipe->input = far_end;
269 pipe->output = video;
275 pipe->output = far_end;
282 __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
284 struct v4l2_subdev_format fmt;
285 struct v4l2_subdev *subdev;
289 subdev = isp_video_remote_subdev(video, &pad);
294 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
296 mutex_lock(&video->mutex);
297 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
298 mutex_unlock(&video->mutex);
303 format->type = video->type;
304 return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
308 isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
310 struct v4l2_format format;
313 memcpy(&format, &vfh->format, sizeof(format));
314 ret = __isp_video_get_format(video, &format);
318 if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
319 vfh->format.fmt.pix.height != format.fmt.pix.height ||
320 vfh->format.fmt.pix.width != format.fmt.pix.width ||
321 vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
322 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
328 /* -----------------------------------------------------------------------------
332 #define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
335 * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
336 * @isp: Device pointer specific to the OMAP3 ISP.
337 * @sglist: Pointer to source Scatter gather list to allocate.
338 * @sglen: Number of elements of the scatter-gatter list.
340 * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
341 * we ran out of memory.
344 ispmmu_vmap(struct isp_device *isp, const struct scatterlist *sglist, int sglen)
346 struct sg_table *sgt;
349 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
353 sgt->sgl = (struct scatterlist *)sglist;
355 sgt->orig_nents = sglen;
357 da = omap_iommu_vmap(isp->domain, isp->dev, 0, sgt, IOMMU_FLAG);
358 if (IS_ERR_VALUE(da))
365 * ispmmu_vunmap - Unmap a device address from the ISP MMU
366 * @isp: Device pointer specific to the OMAP3 ISP.
367 * @da: Device address generated from a ispmmu_vmap call.
369 static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
371 struct sg_table *sgt;
373 sgt = omap_iommu_vunmap(isp->domain, isp->dev, (u32)da);
377 /* -----------------------------------------------------------------------------
378 * Video queue operations
381 static void isp_video_queue_prepare(struct isp_video_queue *queue,
382 unsigned int *nbuffers, unsigned int *size)
384 struct isp_video_fh *vfh =
385 container_of(queue, struct isp_video_fh, queue);
386 struct isp_video *video = vfh->video;
388 *size = vfh->format.fmt.pix.sizeimage;
392 *nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size));
395 static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
397 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
398 struct isp_buffer *buffer = to_isp_buffer(buf);
399 struct isp_video *video = vfh->video;
401 if (buffer->isp_addr) {
402 ispmmu_vunmap(video->isp, buffer->isp_addr);
403 buffer->isp_addr = 0;
407 static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
409 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
410 struct isp_buffer *buffer = to_isp_buffer(buf);
411 struct isp_video *video = vfh->video;
414 /* Refuse to prepare the buffer is the video node has registered an
415 * error. We don't need to take any lock here as the operation is
416 * inherently racy. The authoritative check will be performed in the
417 * queue handler, which can't return an error, this check is just a best
418 * effort to notify userspace as early as possible.
420 if (unlikely(video->error))
423 addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen);
424 if (IS_ERR_VALUE(addr))
427 if (!IS_ALIGNED(addr, 32)) {
428 dev_dbg(video->isp->dev, "Buffer address must be "
429 "aligned to 32 bytes boundary.\n");
430 ispmmu_vunmap(video->isp, buffer->isp_addr);
434 buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
435 buffer->isp_addr = addr;
440 * isp_video_buffer_queue - Add buffer to streaming queue
443 * In memory-to-memory mode, start streaming on the pipeline if buffers are
444 * queued on both the input and the output, if the pipeline isn't already busy.
445 * If the pipeline is busy, it will be restarted in the output module interrupt
448 static void isp_video_buffer_queue(struct isp_video_buffer *buf)
450 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
451 struct isp_buffer *buffer = to_isp_buffer(buf);
452 struct isp_video *video = vfh->video;
453 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
454 enum isp_pipeline_state state;
459 if (unlikely(video->error)) {
460 buf->state = ISP_BUF_STATE_ERROR;
465 empty = list_empty(&video->dmaqueue);
466 list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
469 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
470 state = ISP_PIPELINE_QUEUE_OUTPUT;
472 state = ISP_PIPELINE_QUEUE_INPUT;
474 spin_lock_irqsave(&pipe->lock, flags);
475 pipe->state |= state;
476 video->ops->queue(video, buffer);
477 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
479 start = isp_pipeline_ready(pipe);
481 pipe->state |= ISP_PIPELINE_STREAM;
482 spin_unlock_irqrestore(&pipe->lock, flags);
485 omap3isp_pipeline_set_stream(pipe,
486 ISP_PIPELINE_STREAM_SINGLESHOT);
490 static const struct isp_video_queue_operations isp_video_queue_ops = {
491 .queue_prepare = &isp_video_queue_prepare,
492 .buffer_prepare = &isp_video_buffer_prepare,
493 .buffer_queue = &isp_video_buffer_queue,
494 .buffer_cleanup = &isp_video_buffer_cleanup,
498 * omap3isp_video_buffer_next - Complete the current buffer and return the next
499 * @video: ISP video object
501 * Remove the current video buffer from the DMA queue and fill its timestamp,
502 * field count and state fields before waking up its completion handler.
504 * For capture video nodes the buffer state is set to ISP_BUF_STATE_DONE if no
505 * error has been flagged in the pipeline, or to ISP_BUF_STATE_ERROR otherwise.
506 * For video output nodes the buffer state is always set to ISP_BUF_STATE_DONE.
508 * The DMA queue is expected to contain at least one buffer.
510 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
513 struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
515 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
516 struct isp_video_queue *queue = video->queue;
517 enum isp_pipeline_state state;
518 struct isp_video_buffer *buf;
522 spin_lock_irqsave(&queue->irqlock, flags);
523 if (WARN_ON(list_empty(&video->dmaqueue))) {
524 spin_unlock_irqrestore(&queue->irqlock, flags);
528 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
530 list_del(&buf->irqlist);
531 spin_unlock_irqrestore(&queue->irqlock, flags);
534 buf->vbuf.timestamp.tv_sec = ts.tv_sec;
535 buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
537 /* Do frame number propagation only if this is the output video node.
538 * Frame number either comes from the CSI receivers or it gets
539 * incremented here if H3A is not active.
540 * Note: There is no guarantee that the output buffer will finish
541 * first, so the input number might lag behind by 1 in some cases.
543 if (video == pipe->output && !pipe->do_propagation)
544 buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
546 buf->vbuf.sequence = atomic_read(&pipe->frame_number);
548 /* Report pipeline errors to userspace on the capture device side. */
549 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
550 buf->state = ISP_BUF_STATE_ERROR;
553 buf->state = ISP_BUF_STATE_DONE;
558 if (list_empty(&video->dmaqueue)) {
559 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
560 state = ISP_PIPELINE_QUEUE_OUTPUT
561 | ISP_PIPELINE_STREAM;
563 state = ISP_PIPELINE_QUEUE_INPUT
564 | ISP_PIPELINE_STREAM;
566 spin_lock_irqsave(&pipe->lock, flags);
567 pipe->state &= ~state;
568 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
569 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
570 spin_unlock_irqrestore(&pipe->lock, flags);
574 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
575 spin_lock_irqsave(&pipe->lock, flags);
576 pipe->state &= ~ISP_PIPELINE_STREAM;
577 spin_unlock_irqrestore(&pipe->lock, flags);
580 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
582 buf->state = ISP_BUF_STATE_ACTIVE;
583 return to_isp_buffer(buf);
587 * omap3isp_video_cancel_stream - Cancel stream on a video node
588 * @video: ISP video object
590 * Cancelling a stream mark all buffers on the video node as erroneous and makes
591 * sure no new buffer can be queued.
593 void omap3isp_video_cancel_stream(struct isp_video *video)
595 struct isp_video_queue *queue = video->queue;
598 spin_lock_irqsave(&queue->irqlock, flags);
600 while (!list_empty(&video->dmaqueue)) {
601 struct isp_video_buffer *buf;
603 buf = list_first_entry(&video->dmaqueue,
604 struct isp_video_buffer, irqlist);
605 list_del(&buf->irqlist);
607 buf->state = ISP_BUF_STATE_ERROR;
613 spin_unlock_irqrestore(&queue->irqlock, flags);
617 * omap3isp_video_resume - Perform resume operation on the buffers
618 * @video: ISP video object
619 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
621 * This function is intended to be used on suspend/resume scenario. It
622 * requests video queue layer to discard buffers marked as DONE if it's in
623 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
626 void omap3isp_video_resume(struct isp_video *video, int continuous)
628 struct isp_buffer *buf = NULL;
630 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
631 omap3isp_video_queue_discard_done(video->queue);
633 if (!list_empty(&video->dmaqueue)) {
634 buf = list_first_entry(&video->dmaqueue,
635 struct isp_buffer, buffer.irqlist);
636 video->ops->queue(video, buf);
637 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
640 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
644 /* -----------------------------------------------------------------------------
649 isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
651 struct isp_video *video = video_drvdata(file);
653 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
654 strlcpy(cap->card, video->video.name, sizeof(cap->card));
655 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
657 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
658 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
660 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
666 isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
668 struct isp_video_fh *vfh = to_isp_video_fh(fh);
669 struct isp_video *video = video_drvdata(file);
671 if (format->type != video->type)
674 mutex_lock(&video->mutex);
675 *format = vfh->format;
676 mutex_unlock(&video->mutex);
682 isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
684 struct isp_video_fh *vfh = to_isp_video_fh(fh);
685 struct isp_video *video = video_drvdata(file);
686 struct v4l2_mbus_framefmt fmt;
688 if (format->type != video->type)
691 mutex_lock(&video->mutex);
693 /* Fill the bytesperline and sizeimage fields by converting to media bus
694 * format and back to pixel format.
696 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
697 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
699 vfh->format = *format;
701 mutex_unlock(&video->mutex);
706 isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
708 struct isp_video *video = video_drvdata(file);
709 struct v4l2_subdev_format fmt;
710 struct v4l2_subdev *subdev;
714 if (format->type != video->type)
717 subdev = isp_video_remote_subdev(video, &pad);
721 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
724 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
725 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
727 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
729 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
734 isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
736 struct isp_video *video = video_drvdata(file);
737 struct v4l2_subdev *subdev;
740 subdev = isp_video_remote_subdev(video, NULL);
744 mutex_lock(&video->mutex);
745 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
746 mutex_unlock(&video->mutex);
748 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
752 isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
754 struct isp_video *video = video_drvdata(file);
755 struct v4l2_subdev_format format;
756 struct v4l2_subdev *subdev;
760 subdev = isp_video_remote_subdev(video, &pad);
764 /* Try the get crop operation first and fallback to get format if not
767 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
768 if (ret != -ENOIOCTLCMD)
772 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
773 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
775 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
779 crop->c.width = format.format.width;
780 crop->c.height = format.format.height;
786 isp_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
788 struct isp_video *video = video_drvdata(file);
789 struct v4l2_subdev *subdev;
792 subdev = isp_video_remote_subdev(video, NULL);
796 mutex_lock(&video->mutex);
797 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
798 mutex_unlock(&video->mutex);
800 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
804 isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
806 struct isp_video_fh *vfh = to_isp_video_fh(fh);
807 struct isp_video *video = video_drvdata(file);
809 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
810 video->type != a->type)
813 memset(a, 0, sizeof(*a));
814 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
815 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
816 a->parm.output.timeperframe = vfh->timeperframe;
822 isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
824 struct isp_video_fh *vfh = to_isp_video_fh(fh);
825 struct isp_video *video = video_drvdata(file);
827 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
828 video->type != a->type)
831 if (a->parm.output.timeperframe.denominator == 0)
832 a->parm.output.timeperframe.denominator = 1;
834 vfh->timeperframe = a->parm.output.timeperframe;
840 isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
842 struct isp_video_fh *vfh = to_isp_video_fh(fh);
844 return omap3isp_video_queue_reqbufs(&vfh->queue, rb);
848 isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
850 struct isp_video_fh *vfh = to_isp_video_fh(fh);
852 return omap3isp_video_queue_querybuf(&vfh->queue, b);
856 isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
858 struct isp_video_fh *vfh = to_isp_video_fh(fh);
860 return omap3isp_video_queue_qbuf(&vfh->queue, b);
864 isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
866 struct isp_video_fh *vfh = to_isp_video_fh(fh);
868 return omap3isp_video_queue_dqbuf(&vfh->queue, b,
869 file->f_flags & O_NONBLOCK);
872 static int isp_video_check_external_subdevs(struct isp_video *video,
873 struct isp_pipeline *pipe)
875 struct isp_device *isp = video->isp;
876 struct media_entity *ents[] = {
877 &isp->isp_csi2a.subdev.entity,
878 &isp->isp_csi2c.subdev.entity,
879 &isp->isp_ccp2.subdev.entity,
880 &isp->isp_ccdc.subdev.entity
882 struct media_pad *source_pad;
883 struct media_entity *source = NULL;
884 struct media_entity *sink;
885 struct v4l2_subdev_format fmt;
886 struct v4l2_ext_controls ctrls;
887 struct v4l2_ext_control ctrl;
891 /* Memory-to-memory pipelines have no external subdev. */
892 if (pipe->input != NULL)
895 for (i = 0; i < ARRAY_SIZE(ents); i++) {
896 /* Is the entity part of the pipeline? */
897 if (!(pipe->entities & (1 << ents[i]->id)))
900 /* ISP entities have always sink pad == 0. Find source. */
901 source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
902 if (source_pad == NULL)
905 source = source_pad->entity;
911 dev_warn(isp->dev, "can't find source, failing now\n");
915 if (media_entity_type(source) != MEDIA_ENT_T_V4L2_SUBDEV)
918 pipe->external = media_entity_to_v4l2_subdev(source);
920 fmt.pad = source_pad->index;
921 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
922 ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
923 pad, get_fmt, NULL, &fmt);
924 if (unlikely(ret < 0)) {
925 dev_warn(isp->dev, "get_fmt returned null!\n");
929 pipe->external_width =
930 omap3isp_video_format_info(fmt.format.code)->width;
932 memset(&ctrls, 0, sizeof(ctrls));
933 memset(&ctrl, 0, sizeof(ctrl));
935 ctrl.id = V4L2_CID_PIXEL_RATE;
938 ctrls.controls = &ctrl;
940 ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
942 dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
943 pipe->external->name);
947 pipe->external_rate = ctrl.value64;
949 if (pipe->entities & (1 << isp->isp_ccdc.subdev.entity.id)) {
950 unsigned int rate = UINT_MAX;
952 * Check that maximum allowed CCDC pixel rate isn't
953 * exceeded by the pixel rate.
955 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
956 if (pipe->external_rate > rate)
966 * Every ISP pipeline has a single input and a single output. The input can be
967 * either a sensor or a video node. The output is always a video node.
969 * As every pipeline has an output video node, the ISP video objects at the
970 * pipeline output stores the pipeline state. It tracks the streaming state of
971 * both the input and output, as well as the availability of buffers.
973 * In sensor-to-memory mode, frames are always available at the pipeline input.
974 * Starting the sensor usually requires I2C transfers and must be done in
975 * interruptible context. The pipeline is started and stopped synchronously
976 * to the stream on/off commands. All modules in the pipeline will get their
977 * subdev set stream handler called. The module at the end of the pipeline must
978 * delay starting the hardware until buffers are available at its output.
980 * In memory-to-memory mode, starting/stopping the stream requires
981 * synchronization between the input and output. ISP modules can't be stopped
982 * in the middle of a frame, and at least some of the modules seem to become
983 * busy as soon as they're started, even if they don't receive a frame start
984 * event. For that reason frames need to be processed in single-shot mode. The
985 * driver needs to wait until a frame is completely processed and written to
986 * memory before restarting the pipeline for the next frame. Pipelined
987 * processing might be possible but requires more testing.
989 * Stream start must be delayed until buffers are available at both the input
990 * and output. The pipeline must be started in the videobuf queue callback with
991 * the buffers queue spinlock held. The modules subdev set stream operation must
995 isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
997 struct isp_video_fh *vfh = to_isp_video_fh(fh);
998 struct isp_video *video = video_drvdata(file);
999 enum isp_pipeline_state state;
1000 struct isp_pipeline *pipe;
1001 unsigned long flags;
1004 if (type != video->type)
1007 mutex_lock(&video->stream_lock);
1009 if (video->streaming) {
1010 mutex_unlock(&video->stream_lock);
1014 /* Start streaming on the pipeline. No link touching an entity in the
1015 * pipeline can be activated or deactivated once streaming is started.
1017 pipe = video->video.entity.pipe
1018 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
1022 if (video->isp->pdata->set_constraints)
1023 video->isp->pdata->set_constraints(video->isp, true);
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);
1072 ret = omap3isp_video_queue_streamon(&vfh->queue);
1074 goto err_check_format;
1076 /* In sensor-to-memory mode, the stream can be started synchronously
1077 * to the stream on command. In memory-to-memory mode, it will be
1078 * started when buffers are queued on both the input and output.
1080 if (pipe->input == NULL) {
1081 ret = omap3isp_pipeline_set_stream(pipe,
1082 ISP_PIPELINE_STREAM_CONTINUOUS);
1084 goto err_set_stream;
1085 spin_lock_irqsave(&video->queue->irqlock, flags);
1086 if (list_empty(&video->dmaqueue))
1087 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1088 spin_unlock_irqrestore(&video->queue->irqlock, flags);
1091 video->streaming = 1;
1093 mutex_unlock(&video->stream_lock);
1097 omap3isp_video_queue_streamoff(&vfh->queue);
1099 media_entity_pipeline_stop(&video->video.entity);
1101 if (video->isp->pdata->set_constraints)
1102 video->isp->pdata->set_constraints(video->isp, false);
1103 /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1104 * will get triggered the next time the CCDC is powered up will try to
1105 * access buffers that might have been freed but still present in the
1106 * DMA queue. This can easily get triggered if the above
1107 * omap3isp_pipeline_set_stream() call fails on a system with a
1108 * free-running sensor.
1110 INIT_LIST_HEAD(&video->dmaqueue);
1111 video->queue = NULL;
1113 mutex_unlock(&video->stream_lock);
1118 isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1120 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1121 struct isp_video *video = video_drvdata(file);
1122 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1123 enum isp_pipeline_state state;
1124 unsigned int streaming;
1125 unsigned long flags;
1127 if (type != video->type)
1130 mutex_lock(&video->stream_lock);
1132 /* Make sure we're not streaming yet. */
1133 mutex_lock(&vfh->queue.lock);
1134 streaming = vfh->queue.streaming;
1135 mutex_unlock(&vfh->queue.lock);
1140 /* Update the pipeline state. */
1141 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1142 state = ISP_PIPELINE_STREAM_OUTPUT
1143 | ISP_PIPELINE_QUEUE_OUTPUT;
1145 state = ISP_PIPELINE_STREAM_INPUT
1146 | ISP_PIPELINE_QUEUE_INPUT;
1148 spin_lock_irqsave(&pipe->lock, flags);
1149 pipe->state &= ~state;
1150 spin_unlock_irqrestore(&pipe->lock, flags);
1152 /* Stop the stream. */
1153 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1154 omap3isp_video_queue_streamoff(&vfh->queue);
1155 video->queue = NULL;
1156 video->streaming = 0;
1157 video->error = false;
1159 if (video->isp->pdata->set_constraints)
1160 video->isp->pdata->set_constraints(video->isp, false);
1161 media_entity_pipeline_stop(&video->video.entity);
1164 mutex_unlock(&video->stream_lock);
1169 isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1171 if (input->index > 0)
1174 strlcpy(input->name, "camera", sizeof(input->name));
1175 input->type = V4L2_INPUT_TYPE_CAMERA;
1181 isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1189 isp_video_s_input(struct file *file, void *fh, unsigned int input)
1191 return input == 0 ? 0 : -EINVAL;
1194 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1195 .vidioc_querycap = isp_video_querycap,
1196 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1197 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1198 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1199 .vidioc_g_fmt_vid_out = isp_video_get_format,
1200 .vidioc_s_fmt_vid_out = isp_video_set_format,
1201 .vidioc_try_fmt_vid_out = isp_video_try_format,
1202 .vidioc_cropcap = isp_video_cropcap,
1203 .vidioc_g_crop = isp_video_get_crop,
1204 .vidioc_s_crop = isp_video_set_crop,
1205 .vidioc_g_parm = isp_video_get_param,
1206 .vidioc_s_parm = isp_video_set_param,
1207 .vidioc_reqbufs = isp_video_reqbufs,
1208 .vidioc_querybuf = isp_video_querybuf,
1209 .vidioc_qbuf = isp_video_qbuf,
1210 .vidioc_dqbuf = isp_video_dqbuf,
1211 .vidioc_streamon = isp_video_streamon,
1212 .vidioc_streamoff = isp_video_streamoff,
1213 .vidioc_enum_input = isp_video_enum_input,
1214 .vidioc_g_input = isp_video_g_input,
1215 .vidioc_s_input = isp_video_s_input,
1218 /* -----------------------------------------------------------------------------
1219 * V4L2 file operations
1222 static int isp_video_open(struct file *file)
1224 struct isp_video *video = video_drvdata(file);
1225 struct isp_video_fh *handle;
1228 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1232 v4l2_fh_init(&handle->vfh, &video->video);
1233 v4l2_fh_add(&handle->vfh);
1235 /* If this is the first user, initialise the pipeline. */
1236 if (omap3isp_get(video->isp) == NULL) {
1241 ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1243 omap3isp_put(video->isp);
1247 omap3isp_video_queue_init(&handle->queue, video->type,
1248 &isp_video_queue_ops, video->isp->dev,
1249 sizeof(struct isp_buffer));
1251 memset(&handle->format, 0, sizeof(handle->format));
1252 handle->format.type = video->type;
1253 handle->timeperframe.denominator = 1;
1255 handle->video = video;
1256 file->private_data = &handle->vfh;
1260 v4l2_fh_del(&handle->vfh);
1267 static int isp_video_release(struct file *file)
1269 struct isp_video *video = video_drvdata(file);
1270 struct v4l2_fh *vfh = file->private_data;
1271 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1273 /* Disable streaming and free the buffers queue resources. */
1274 isp_video_streamoff(file, vfh, video->type);
1276 mutex_lock(&handle->queue.lock);
1277 omap3isp_video_queue_cleanup(&handle->queue);
1278 mutex_unlock(&handle->queue.lock);
1280 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1282 /* Release the file handle. */
1285 file->private_data = NULL;
1287 omap3isp_put(video->isp);
1292 static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1294 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1295 struct isp_video_queue *queue = &vfh->queue;
1297 return omap3isp_video_queue_poll(queue, file, wait);
1300 static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1302 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1304 return omap3isp_video_queue_mmap(&vfh->queue, vma);
1307 static struct v4l2_file_operations isp_video_fops = {
1308 .owner = THIS_MODULE,
1309 .unlocked_ioctl = video_ioctl2,
1310 .open = isp_video_open,
1311 .release = isp_video_release,
1312 .poll = isp_video_poll,
1313 .mmap = isp_video_mmap,
1316 /* -----------------------------------------------------------------------------
1320 static const struct isp_video_operations isp_video_dummy_ops = {
1323 int omap3isp_video_init(struct isp_video *video, const char *name)
1325 const char *direction;
1328 switch (video->type) {
1329 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1330 direction = "output";
1331 video->pad.flags = MEDIA_PAD_FL_SINK
1332 | MEDIA_PAD_FL_MUST_CONNECT;
1334 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1335 direction = "input";
1336 video->pad.flags = MEDIA_PAD_FL_SOURCE
1337 | MEDIA_PAD_FL_MUST_CONNECT;
1338 video->video.vfl_dir = VFL_DIR_TX;
1345 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1349 mutex_init(&video->mutex);
1350 atomic_set(&video->active, 0);
1352 spin_lock_init(&video->pipe.lock);
1353 mutex_init(&video->stream_lock);
1355 /* Initialize the video device. */
1356 if (video->ops == NULL)
1357 video->ops = &isp_video_dummy_ops;
1359 video->video.fops = &isp_video_fops;
1360 snprintf(video->video.name, sizeof(video->video.name),
1361 "OMAP3 ISP %s %s", name, direction);
1362 video->video.vfl_type = VFL_TYPE_GRABBER;
1363 video->video.release = video_device_release_empty;
1364 video->video.ioctl_ops = &isp_video_ioctl_ops;
1365 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1367 video_set_drvdata(&video->video, video);
1372 void omap3isp_video_cleanup(struct isp_video *video)
1374 media_entity_cleanup(&video->video.entity);
1375 mutex_destroy(&video->stream_lock);
1376 mutex_destroy(&video->mutex);
1379 int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1383 video->video.v4l2_dev = vdev;
1385 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1387 dev_err(video->isp->dev,
1388 "%s: could not register video device (%d)\n",
1394 void omap3isp_video_unregister(struct isp_video *video)
1396 if (video_is_registered(&video->video))
1397 video_unregister_device(&video->video);