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 * @dev: 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 * @dev: 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 addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen);
415 if (IS_ERR_VALUE(addr))
418 if (!IS_ALIGNED(addr, 32)) {
419 dev_dbg(video->isp->dev, "Buffer address must be "
420 "aligned to 32 bytes boundary.\n");
421 ispmmu_vunmap(video->isp, buffer->isp_addr);
425 buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
426 buffer->isp_addr = addr;
431 * isp_video_buffer_queue - Add buffer to streaming queue
434 * In memory-to-memory mode, start streaming on the pipeline if buffers are
435 * queued on both the input and the output, if the pipeline isn't already busy.
436 * If the pipeline is busy, it will be restarted in the output module interrupt
439 static void isp_video_buffer_queue(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;
444 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
445 enum isp_pipeline_state state;
450 empty = list_empty(&video->dmaqueue);
451 list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
454 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
455 state = ISP_PIPELINE_QUEUE_OUTPUT;
457 state = ISP_PIPELINE_QUEUE_INPUT;
459 spin_lock_irqsave(&pipe->lock, flags);
460 pipe->state |= state;
461 video->ops->queue(video, buffer);
462 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
464 start = isp_pipeline_ready(pipe);
466 pipe->state |= ISP_PIPELINE_STREAM;
467 spin_unlock_irqrestore(&pipe->lock, flags);
470 omap3isp_pipeline_set_stream(pipe,
471 ISP_PIPELINE_STREAM_SINGLESHOT);
475 static const struct isp_video_queue_operations isp_video_queue_ops = {
476 .queue_prepare = &isp_video_queue_prepare,
477 .buffer_prepare = &isp_video_buffer_prepare,
478 .buffer_queue = &isp_video_buffer_queue,
479 .buffer_cleanup = &isp_video_buffer_cleanup,
483 * omap3isp_video_buffer_next - Complete the current buffer and return the next
484 * @video: ISP video object
486 * Remove the current video buffer from the DMA queue and fill its timestamp,
487 * field count and state fields before waking up its completion handler.
489 * For capture video nodes the buffer state is set to ISP_BUF_STATE_DONE if no
490 * error has been flagged in the pipeline, or to ISP_BUF_STATE_ERROR otherwise.
491 * For video output nodes the buffer state is always set to ISP_BUF_STATE_DONE.
493 * The DMA queue is expected to contain at least one buffer.
495 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
498 struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
500 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
501 struct isp_video_queue *queue = video->queue;
502 enum isp_pipeline_state state;
503 struct isp_video_buffer *buf;
507 spin_lock_irqsave(&queue->irqlock, flags);
508 if (WARN_ON(list_empty(&video->dmaqueue))) {
509 spin_unlock_irqrestore(&queue->irqlock, flags);
513 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
515 list_del(&buf->irqlist);
516 spin_unlock_irqrestore(&queue->irqlock, flags);
519 buf->vbuf.timestamp.tv_sec = ts.tv_sec;
520 buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
522 /* Do frame number propagation only if this is the output video node.
523 * Frame number either comes from the CSI receivers or it gets
524 * incremented here if H3A is not active.
525 * Note: There is no guarantee that the output buffer will finish
526 * first, so the input number might lag behind by 1 in some cases.
528 if (video == pipe->output && !pipe->do_propagation)
529 buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
531 buf->vbuf.sequence = atomic_read(&pipe->frame_number);
533 /* Report pipeline errors to userspace on the capture device side. */
534 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
535 buf->state = ISP_BUF_STATE_ERROR;
538 buf->state = ISP_BUF_STATE_DONE;
543 if (list_empty(&video->dmaqueue)) {
544 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
545 state = ISP_PIPELINE_QUEUE_OUTPUT
546 | ISP_PIPELINE_STREAM;
548 state = ISP_PIPELINE_QUEUE_INPUT
549 | ISP_PIPELINE_STREAM;
551 spin_lock_irqsave(&pipe->lock, flags);
552 pipe->state &= ~state;
553 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
554 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
555 spin_unlock_irqrestore(&pipe->lock, flags);
559 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
560 spin_lock_irqsave(&pipe->lock, flags);
561 pipe->state &= ~ISP_PIPELINE_STREAM;
562 spin_unlock_irqrestore(&pipe->lock, flags);
565 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
567 buf->state = ISP_BUF_STATE_ACTIVE;
568 return to_isp_buffer(buf);
572 * omap3isp_video_resume - Perform resume operation on the buffers
573 * @video: ISP video object
574 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
576 * This function is intended to be used on suspend/resume scenario. It
577 * requests video queue layer to discard buffers marked as DONE if it's in
578 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
581 void omap3isp_video_resume(struct isp_video *video, int continuous)
583 struct isp_buffer *buf = NULL;
585 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
586 omap3isp_video_queue_discard_done(video->queue);
588 if (!list_empty(&video->dmaqueue)) {
589 buf = list_first_entry(&video->dmaqueue,
590 struct isp_buffer, buffer.irqlist);
591 video->ops->queue(video, buf);
592 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
595 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
599 /* -----------------------------------------------------------------------------
604 isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
606 struct isp_video *video = video_drvdata(file);
608 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
609 strlcpy(cap->card, video->video.name, sizeof(cap->card));
610 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
612 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
613 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
615 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
621 isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
623 struct isp_video_fh *vfh = to_isp_video_fh(fh);
624 struct isp_video *video = video_drvdata(file);
626 if (format->type != video->type)
629 mutex_lock(&video->mutex);
630 *format = vfh->format;
631 mutex_unlock(&video->mutex);
637 isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
639 struct isp_video_fh *vfh = to_isp_video_fh(fh);
640 struct isp_video *video = video_drvdata(file);
641 struct v4l2_mbus_framefmt fmt;
643 if (format->type != video->type)
646 mutex_lock(&video->mutex);
648 /* Fill the bytesperline and sizeimage fields by converting to media bus
649 * format and back to pixel format.
651 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
652 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
654 vfh->format = *format;
656 mutex_unlock(&video->mutex);
661 isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
663 struct isp_video *video = video_drvdata(file);
664 struct v4l2_subdev_format fmt;
665 struct v4l2_subdev *subdev;
669 if (format->type != video->type)
672 subdev = isp_video_remote_subdev(video, &pad);
676 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
679 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
680 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
682 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
684 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
689 isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
691 struct isp_video *video = video_drvdata(file);
692 struct v4l2_subdev *subdev;
695 subdev = isp_video_remote_subdev(video, NULL);
699 mutex_lock(&video->mutex);
700 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
701 mutex_unlock(&video->mutex);
703 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
707 isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
709 struct isp_video *video = video_drvdata(file);
710 struct v4l2_subdev_format format;
711 struct v4l2_subdev *subdev;
715 subdev = isp_video_remote_subdev(video, &pad);
719 /* Try the get crop operation first and fallback to get format if not
722 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
723 if (ret != -ENOIOCTLCMD)
727 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
728 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
730 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
734 crop->c.width = format.format.width;
735 crop->c.height = format.format.height;
741 isp_video_set_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
743 struct isp_video *video = video_drvdata(file);
744 struct v4l2_subdev *subdev;
747 subdev = isp_video_remote_subdev(video, NULL);
751 mutex_lock(&video->mutex);
752 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
753 mutex_unlock(&video->mutex);
755 return ret == -ENOIOCTLCMD ? -ENOTTY : ret;
759 isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
761 struct isp_video_fh *vfh = to_isp_video_fh(fh);
762 struct isp_video *video = video_drvdata(file);
764 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
765 video->type != a->type)
768 memset(a, 0, sizeof(*a));
769 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
770 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
771 a->parm.output.timeperframe = vfh->timeperframe;
777 isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
779 struct isp_video_fh *vfh = to_isp_video_fh(fh);
780 struct isp_video *video = video_drvdata(file);
782 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
783 video->type != a->type)
786 if (a->parm.output.timeperframe.denominator == 0)
787 a->parm.output.timeperframe.denominator = 1;
789 vfh->timeperframe = a->parm.output.timeperframe;
795 isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
797 struct isp_video_fh *vfh = to_isp_video_fh(fh);
799 return omap3isp_video_queue_reqbufs(&vfh->queue, rb);
803 isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
805 struct isp_video_fh *vfh = to_isp_video_fh(fh);
807 return omap3isp_video_queue_querybuf(&vfh->queue, b);
811 isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
813 struct isp_video_fh *vfh = to_isp_video_fh(fh);
815 return omap3isp_video_queue_qbuf(&vfh->queue, b);
819 isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
821 struct isp_video_fh *vfh = to_isp_video_fh(fh);
823 return omap3isp_video_queue_dqbuf(&vfh->queue, b,
824 file->f_flags & O_NONBLOCK);
827 static int isp_video_check_external_subdevs(struct isp_video *video,
828 struct isp_pipeline *pipe)
830 struct isp_device *isp = video->isp;
831 struct media_entity *ents[] = {
832 &isp->isp_csi2a.subdev.entity,
833 &isp->isp_csi2c.subdev.entity,
834 &isp->isp_ccp2.subdev.entity,
835 &isp->isp_ccdc.subdev.entity
837 struct media_pad *source_pad;
838 struct media_entity *source = NULL;
839 struct media_entity *sink;
840 struct v4l2_subdev_format fmt;
841 struct v4l2_ext_controls ctrls;
842 struct v4l2_ext_control ctrl;
846 for (i = 0; i < ARRAY_SIZE(ents); i++) {
847 /* Is the entity part of the pipeline? */
848 if (!(pipe->entities & (1 << ents[i]->id)))
851 /* ISP entities have always sink pad == 0. Find source. */
852 source_pad = media_entity_remote_pad(&ents[i]->pads[0]);
853 if (source_pad == NULL)
856 source = source_pad->entity;
862 dev_warn(isp->dev, "can't find source, failing now\n");
866 if (media_entity_type(source) != MEDIA_ENT_T_V4L2_SUBDEV)
869 pipe->external = media_entity_to_v4l2_subdev(source);
871 fmt.pad = source_pad->index;
872 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
873 ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink),
874 pad, get_fmt, NULL, &fmt);
875 if (unlikely(ret < 0)) {
876 dev_warn(isp->dev, "get_fmt returned null!\n");
880 pipe->external_width =
881 omap3isp_video_format_info(fmt.format.code)->width;
883 memset(&ctrls, 0, sizeof(ctrls));
884 memset(&ctrl, 0, sizeof(ctrl));
886 ctrl.id = V4L2_CID_PIXEL_RATE;
889 ctrls.controls = &ctrl;
891 ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, &ctrls);
893 dev_warn(isp->dev, "no pixel rate control in subdev %s\n",
894 pipe->external->name);
898 pipe->external_rate = ctrl.value64;
900 if (pipe->entities & (1 << isp->isp_ccdc.subdev.entity.id)) {
901 unsigned int rate = UINT_MAX;
903 * Check that maximum allowed CCDC pixel rate isn't
904 * exceeded by the pixel rate.
906 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
907 if (pipe->external_rate > rate)
917 * Every ISP pipeline has a single input and a single output. The input can be
918 * either a sensor or a video node. The output is always a video node.
920 * As every pipeline has an output video node, the ISP video objects at the
921 * pipeline output stores the pipeline state. It tracks the streaming state of
922 * both the input and output, as well as the availability of buffers.
924 * In sensor-to-memory mode, frames are always available at the pipeline input.
925 * Starting the sensor usually requires I2C transfers and must be done in
926 * interruptible context. The pipeline is started and stopped synchronously
927 * to the stream on/off commands. All modules in the pipeline will get their
928 * subdev set stream handler called. The module at the end of the pipeline must
929 * delay starting the hardware until buffers are available at its output.
931 * In memory-to-memory mode, starting/stopping the stream requires
932 * synchronization between the input and output. ISP modules can't be stopped
933 * in the middle of a frame, and at least some of the modules seem to become
934 * busy as soon as they're started, even if they don't receive a frame start
935 * event. For that reason frames need to be processed in single-shot mode. The
936 * driver needs to wait until a frame is completely processed and written to
937 * memory before restarting the pipeline for the next frame. Pipelined
938 * processing might be possible but requires more testing.
940 * Stream start must be delayed until buffers are available at both the input
941 * and output. The pipeline must be started in the videobuf queue callback with
942 * the buffers queue spinlock held. The modules subdev set stream operation must
946 isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
948 struct isp_video_fh *vfh = to_isp_video_fh(fh);
949 struct isp_video *video = video_drvdata(file);
950 enum isp_pipeline_state state;
951 struct isp_pipeline *pipe;
955 if (type != video->type)
958 mutex_lock(&video->stream_lock);
960 if (video->streaming) {
961 mutex_unlock(&video->stream_lock);
965 /* Start streaming on the pipeline. No link touching an entity in the
966 * pipeline can be activated or deactivated once streaming is started.
968 pipe = video->video.entity.pipe
969 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
973 if (video->isp->pdata->set_constraints)
974 video->isp->pdata->set_constraints(video->isp, true);
975 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
976 pipe->max_rate = pipe->l3_ick;
978 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
980 goto err_pipeline_start;
982 /* Verify that the currently configured format matches the output of
983 * the connected subdev.
985 ret = isp_video_check_format(video, vfh);
987 goto err_check_format;
989 video->bpl_padding = ret;
990 video->bpl_value = vfh->format.fmt.pix.bytesperline;
992 ret = isp_video_get_graph_data(video, pipe);
994 goto err_check_format;
996 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
997 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
999 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1001 ret = isp_video_check_external_subdevs(video, pipe);
1003 goto err_check_format;
1005 pipe->error = false;
1007 spin_lock_irqsave(&pipe->lock, flags);
1008 pipe->state &= ~ISP_PIPELINE_STREAM;
1009 pipe->state |= state;
1010 spin_unlock_irqrestore(&pipe->lock, flags);
1012 /* Set the maximum time per frame as the value requested by userspace.
1013 * This is a soft limit that can be overridden if the hardware doesn't
1014 * support the request limit.
1016 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1017 pipe->max_timeperframe = vfh->timeperframe;
1019 video->queue = &vfh->queue;
1020 INIT_LIST_HEAD(&video->dmaqueue);
1021 atomic_set(&pipe->frame_number, -1);
1023 ret = omap3isp_video_queue_streamon(&vfh->queue);
1025 goto err_check_format;
1027 /* In sensor-to-memory mode, the stream can be started synchronously
1028 * to the stream on command. In memory-to-memory mode, it will be
1029 * started when buffers are queued on both the input and output.
1031 if (pipe->input == NULL) {
1032 ret = omap3isp_pipeline_set_stream(pipe,
1033 ISP_PIPELINE_STREAM_CONTINUOUS);
1035 goto err_set_stream;
1036 spin_lock_irqsave(&video->queue->irqlock, flags);
1037 if (list_empty(&video->dmaqueue))
1038 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1039 spin_unlock_irqrestore(&video->queue->irqlock, flags);
1042 video->streaming = 1;
1044 mutex_unlock(&video->stream_lock);
1048 omap3isp_video_queue_streamoff(&vfh->queue);
1050 media_entity_pipeline_stop(&video->video.entity);
1052 if (video->isp->pdata->set_constraints)
1053 video->isp->pdata->set_constraints(video->isp, false);
1054 /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1055 * will get triggered the next time the CCDC is powered up will try to
1056 * access buffers that might have been freed but still present in the
1057 * DMA queue. This can easily get triggered if the above
1058 * omap3isp_pipeline_set_stream() call fails on a system with a
1059 * free-running sensor.
1061 INIT_LIST_HEAD(&video->dmaqueue);
1062 video->queue = NULL;
1064 mutex_unlock(&video->stream_lock);
1069 isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1071 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1072 struct isp_video *video = video_drvdata(file);
1073 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1074 enum isp_pipeline_state state;
1075 unsigned int streaming;
1076 unsigned long flags;
1078 if (type != video->type)
1081 mutex_lock(&video->stream_lock);
1083 /* Make sure we're not streaming yet. */
1084 mutex_lock(&vfh->queue.lock);
1085 streaming = vfh->queue.streaming;
1086 mutex_unlock(&vfh->queue.lock);
1091 /* Update the pipeline state. */
1092 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1093 state = ISP_PIPELINE_STREAM_OUTPUT
1094 | ISP_PIPELINE_QUEUE_OUTPUT;
1096 state = ISP_PIPELINE_STREAM_INPUT
1097 | ISP_PIPELINE_QUEUE_INPUT;
1099 spin_lock_irqsave(&pipe->lock, flags);
1100 pipe->state &= ~state;
1101 spin_unlock_irqrestore(&pipe->lock, flags);
1103 /* Stop the stream. */
1104 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1105 omap3isp_video_queue_streamoff(&vfh->queue);
1106 video->queue = NULL;
1107 video->streaming = 0;
1109 if (video->isp->pdata->set_constraints)
1110 video->isp->pdata->set_constraints(video->isp, false);
1111 media_entity_pipeline_stop(&video->video.entity);
1114 mutex_unlock(&video->stream_lock);
1119 isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1121 if (input->index > 0)
1124 strlcpy(input->name, "camera", sizeof(input->name));
1125 input->type = V4L2_INPUT_TYPE_CAMERA;
1131 isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1139 isp_video_s_input(struct file *file, void *fh, unsigned int input)
1141 return input == 0 ? 0 : -EINVAL;
1144 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1145 .vidioc_querycap = isp_video_querycap,
1146 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1147 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1148 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1149 .vidioc_g_fmt_vid_out = isp_video_get_format,
1150 .vidioc_s_fmt_vid_out = isp_video_set_format,
1151 .vidioc_try_fmt_vid_out = isp_video_try_format,
1152 .vidioc_cropcap = isp_video_cropcap,
1153 .vidioc_g_crop = isp_video_get_crop,
1154 .vidioc_s_crop = isp_video_set_crop,
1155 .vidioc_g_parm = isp_video_get_param,
1156 .vidioc_s_parm = isp_video_set_param,
1157 .vidioc_reqbufs = isp_video_reqbufs,
1158 .vidioc_querybuf = isp_video_querybuf,
1159 .vidioc_qbuf = isp_video_qbuf,
1160 .vidioc_dqbuf = isp_video_dqbuf,
1161 .vidioc_streamon = isp_video_streamon,
1162 .vidioc_streamoff = isp_video_streamoff,
1163 .vidioc_enum_input = isp_video_enum_input,
1164 .vidioc_g_input = isp_video_g_input,
1165 .vidioc_s_input = isp_video_s_input,
1168 /* -----------------------------------------------------------------------------
1169 * V4L2 file operations
1172 static int isp_video_open(struct file *file)
1174 struct isp_video *video = video_drvdata(file);
1175 struct isp_video_fh *handle;
1178 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1182 v4l2_fh_init(&handle->vfh, &video->video);
1183 v4l2_fh_add(&handle->vfh);
1185 /* If this is the first user, initialise the pipeline. */
1186 if (omap3isp_get(video->isp) == NULL) {
1191 ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1193 omap3isp_put(video->isp);
1197 omap3isp_video_queue_init(&handle->queue, video->type,
1198 &isp_video_queue_ops, video->isp->dev,
1199 sizeof(struct isp_buffer));
1201 memset(&handle->format, 0, sizeof(handle->format));
1202 handle->format.type = video->type;
1203 handle->timeperframe.denominator = 1;
1205 handle->video = video;
1206 file->private_data = &handle->vfh;
1210 v4l2_fh_del(&handle->vfh);
1217 static int isp_video_release(struct file *file)
1219 struct isp_video *video = video_drvdata(file);
1220 struct v4l2_fh *vfh = file->private_data;
1221 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1223 /* Disable streaming and free the buffers queue resources. */
1224 isp_video_streamoff(file, vfh, video->type);
1226 mutex_lock(&handle->queue.lock);
1227 omap3isp_video_queue_cleanup(&handle->queue);
1228 mutex_unlock(&handle->queue.lock);
1230 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1232 /* Release the file handle. */
1235 file->private_data = NULL;
1237 omap3isp_put(video->isp);
1242 static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1244 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1245 struct isp_video_queue *queue = &vfh->queue;
1247 return omap3isp_video_queue_poll(queue, file, wait);
1250 static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1252 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1254 return omap3isp_video_queue_mmap(&vfh->queue, vma);
1257 static struct v4l2_file_operations isp_video_fops = {
1258 .owner = THIS_MODULE,
1259 .unlocked_ioctl = video_ioctl2,
1260 .open = isp_video_open,
1261 .release = isp_video_release,
1262 .poll = isp_video_poll,
1263 .mmap = isp_video_mmap,
1266 /* -----------------------------------------------------------------------------
1270 static const struct isp_video_operations isp_video_dummy_ops = {
1273 int omap3isp_video_init(struct isp_video *video, const char *name)
1275 const char *direction;
1278 switch (video->type) {
1279 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1280 direction = "output";
1281 video->pad.flags = MEDIA_PAD_FL_SINK
1282 | MEDIA_PAD_FL_MUST_CONNECT;
1284 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1285 direction = "input";
1286 video->pad.flags = MEDIA_PAD_FL_SOURCE
1287 | MEDIA_PAD_FL_MUST_CONNECT;
1288 video->video.vfl_dir = VFL_DIR_TX;
1295 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1299 mutex_init(&video->mutex);
1300 atomic_set(&video->active, 0);
1302 spin_lock_init(&video->pipe.lock);
1303 mutex_init(&video->stream_lock);
1305 /* Initialize the video device. */
1306 if (video->ops == NULL)
1307 video->ops = &isp_video_dummy_ops;
1309 video->video.fops = &isp_video_fops;
1310 snprintf(video->video.name, sizeof(video->video.name),
1311 "OMAP3 ISP %s %s", name, direction);
1312 video->video.vfl_type = VFL_TYPE_GRABBER;
1313 video->video.release = video_device_release_empty;
1314 video->video.ioctl_ops = &isp_video_ioctl_ops;
1315 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1317 video_set_drvdata(&video->video, video);
1322 void omap3isp_video_cleanup(struct isp_video *video)
1324 media_entity_cleanup(&video->video.entity);
1325 mutex_destroy(&video->stream_lock);
1326 mutex_destroy(&video->mutex);
1329 int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1333 video->video.v4l2_dev = vdev;
1335 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1337 dev_err(video->isp->dev,
1338 "%s: could not register video device (%d)\n",
1344 void omap3isp_video_unregister(struct isp_video *video)
1346 if (video_is_registered(&video->video))
1347 video_unregister_device(&video->video);