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 /* -----------------------------------------------------------------------------
49 static struct isp_format_info formats[] = {
50 { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
51 V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
52 V4L2_PIX_FMT_GREY, 8, },
53 { V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10,
54 V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8,
55 V4L2_PIX_FMT_Y10, 10, },
56 { V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10,
57 V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8,
58 V4L2_PIX_FMT_Y12, 12, },
59 { V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
60 V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
61 V4L2_PIX_FMT_SBGGR8, 8, },
62 { V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
63 V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
64 V4L2_PIX_FMT_SGBRG8, 8, },
65 { V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
66 V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
67 V4L2_PIX_FMT_SGRBG8, 8, },
68 { V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
69 V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
70 V4L2_PIX_FMT_SRGGB8, 8, },
71 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
72 V4L2_MBUS_FMT_SGRBG10_1X10, 0,
73 V4L2_PIX_FMT_SGRBG10DPCM8, 8, },
74 { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
75 V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8,
76 V4L2_PIX_FMT_SBGGR10, 10, },
77 { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
78 V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8,
79 V4L2_PIX_FMT_SGBRG10, 10, },
80 { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
81 V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8,
82 V4L2_PIX_FMT_SGRBG10, 10, },
83 { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
84 V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8,
85 V4L2_PIX_FMT_SRGGB10, 10, },
86 { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
87 V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8,
88 V4L2_PIX_FMT_SBGGR12, 12, },
89 { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
90 V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8,
91 V4L2_PIX_FMT_SGBRG12, 12, },
92 { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
93 V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8,
94 V4L2_PIX_FMT_SGRBG12, 12, },
95 { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
96 V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8,
97 V4L2_PIX_FMT_SRGGB12, 12, },
98 { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
99 V4L2_MBUS_FMT_UYVY8_1X16, 0,
100 V4L2_PIX_FMT_UYVY, 16, },
101 { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
102 V4L2_MBUS_FMT_YUYV8_1X16, 0,
103 V4L2_PIX_FMT_YUYV, 16, },
106 const struct isp_format_info *
107 omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
111 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
112 if (formats[i].code == code)
120 * Decide whether desired output pixel code can be obtained with
121 * the lane shifter by shifting the input pixel code.
122 * @in: input pixelcode to shifter
123 * @out: output pixelcode from shifter
124 * @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
126 * return true if the combination is possible
127 * return false otherwise
129 static bool isp_video_is_shiftable(enum v4l2_mbus_pixelcode in,
130 enum v4l2_mbus_pixelcode out,
131 unsigned int additional_shift)
133 const struct isp_format_info *in_info, *out_info;
138 in_info = omap3isp_video_format_info(in);
139 out_info = omap3isp_video_format_info(out);
141 if ((in_info->flavor == 0) || (out_info->flavor == 0))
144 if (in_info->flavor != out_info->flavor)
147 return in_info->bpp - out_info->bpp + additional_shift <= 6;
151 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
152 * @video: ISP video instance
153 * @mbus: v4l2_mbus_framefmt format (input)
154 * @pix: v4l2_pix_format format (output)
156 * Fill the output pix structure with information from the input mbus format.
157 * The bytesperline and sizeimage fields are computed from the requested bytes
158 * per line value in the pix format and information from the video instance.
160 * Return the number of padding bytes at end of line.
162 static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
163 const struct v4l2_mbus_framefmt *mbus,
164 struct v4l2_pix_format *pix)
166 unsigned int bpl = pix->bytesperline;
167 unsigned int min_bpl;
170 memset(pix, 0, sizeof(*pix));
171 pix->width = mbus->width;
172 pix->height = mbus->height;
174 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
175 if (formats[i].code == mbus->code)
179 if (WARN_ON(i == ARRAY_SIZE(formats)))
182 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
184 /* Clamp the requested bytes per line value. If the maximum bytes per
185 * line value is zero, the module doesn't support user configurable line
186 * sizes. Override the requested value with the minimum in that case.
189 bpl = clamp(bpl, min_bpl, video->bpl_max);
193 if (!video->bpl_zero_padding || bpl != min_bpl)
194 bpl = ALIGN(bpl, video->bpl_alignment);
196 pix->pixelformat = formats[i].pixelformat;
197 pix->bytesperline = bpl;
198 pix->sizeimage = pix->bytesperline * pix->height;
199 pix->colorspace = mbus->colorspace;
200 pix->field = mbus->field;
202 return bpl - min_bpl;
205 static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
206 struct v4l2_mbus_framefmt *mbus)
210 memset(mbus, 0, sizeof(*mbus));
211 mbus->width = pix->width;
212 mbus->height = pix->height;
214 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
215 if (formats[i].pixelformat == pix->pixelformat)
219 if (WARN_ON(i == ARRAY_SIZE(formats)))
222 mbus->code = formats[i].code;
223 mbus->colorspace = pix->colorspace;
224 mbus->field = pix->field;
227 static struct v4l2_subdev *
228 isp_video_remote_subdev(struct isp_video *video, u32 *pad)
230 struct media_pad *remote;
232 remote = media_entity_remote_source(&video->pad);
234 if (remote == NULL ||
235 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
239 *pad = remote->index;
241 return media_entity_to_v4l2_subdev(remote->entity);
244 /* Return a pointer to the ISP video instance at the far end of the pipeline. */
245 static struct isp_video *
246 isp_video_far_end(struct isp_video *video)
248 struct media_entity_graph graph;
249 struct media_entity *entity = &video->video.entity;
250 struct media_device *mdev = entity->parent;
251 struct isp_video *far_end = NULL;
253 mutex_lock(&mdev->graph_mutex);
254 media_entity_graph_walk_start(&graph, entity);
256 while ((entity = media_entity_graph_walk_next(&graph))) {
257 if (entity == &video->video.entity)
260 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
263 far_end = to_isp_video(media_entity_to_video_device(entity));
264 if (far_end->type != video->type)
270 mutex_unlock(&mdev->graph_mutex);
275 * Validate a pipeline by checking both ends of all links for format
278 * Compute the minimum time per frame value as the maximum of time per frame
279 * limits reported by every block in the pipeline.
281 * Return 0 if all formats match, or -EPIPE if at least one link is found with
282 * different formats on its two ends or if the pipeline doesn't start with a
283 * video source (either a subdev with no input pad, or a non-subdev entity).
285 static int isp_video_validate_pipeline(struct isp_pipeline *pipe)
287 struct isp_device *isp = pipe->output->isp;
288 struct v4l2_subdev_format fmt_source;
289 struct v4l2_subdev_format fmt_sink;
290 struct media_pad *pad;
291 struct v4l2_subdev *subdev;
294 pipe->max_rate = pipe->l3_ick;
296 subdev = isp_video_remote_subdev(pipe->output, NULL);
301 unsigned int shifter_link;
302 /* Retrieve the sink format */
303 pad = &subdev->entity.pads[0];
304 if (!(pad->flags & MEDIA_PAD_FL_SINK))
307 fmt_sink.pad = pad->index;
308 fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
309 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_sink);
310 if (ret < 0 && ret != -ENOIOCTLCMD)
313 /* Update the maximum frame rate */
314 if (subdev == &isp->isp_res.subdev)
315 omap3isp_resizer_max_rate(&isp->isp_res,
318 /* Check ccdc maximum data rate when data comes from sensor
319 * TODO: Include ccdc rate in pipe->max_rate and compare the
320 * total pipe rate with the input data rate from sensor.
322 if (subdev == &isp->isp_ccdc.subdev && pipe->input == NULL) {
323 unsigned int rate = UINT_MAX;
325 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
326 if (isp->isp_ccdc.vpcfg.pixelclk > rate)
330 /* If sink pad is on CCDC, the link has the lane shifter
331 * in the middle of it. */
332 shifter_link = subdev == &isp->isp_ccdc.subdev;
334 /* Retrieve the source format. Return an error if no source
335 * entity can be found, and stop checking the pipeline if the
336 * source entity isn't a subdev.
338 pad = media_entity_remote_source(pad);
342 if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
345 subdev = media_entity_to_v4l2_subdev(pad->entity);
347 fmt_source.pad = pad->index;
348 fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
349 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
350 if (ret < 0 && ret != -ENOIOCTLCMD)
353 /* Check if the two ends match */
354 if (fmt_source.format.width != fmt_sink.format.width ||
355 fmt_source.format.height != fmt_sink.format.height)
359 unsigned int parallel_shift = 0;
360 if (isp->isp_ccdc.input == CCDC_INPUT_PARALLEL) {
361 struct isp_parallel_platform_data *pdata =
362 &((struct isp_v4l2_subdevs_group *)
363 subdev->host_priv)->bus.parallel;
364 parallel_shift = pdata->data_lane_shift * 2;
366 if (!isp_video_is_shiftable(fmt_source.format.code,
367 fmt_sink.format.code,
370 } else if (fmt_source.format.code != fmt_sink.format.code)
378 __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
380 struct v4l2_subdev_format fmt;
381 struct v4l2_subdev *subdev;
385 subdev = isp_video_remote_subdev(video, &pad);
389 mutex_lock(&video->mutex);
392 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
393 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
394 if (ret == -ENOIOCTLCMD)
397 mutex_unlock(&video->mutex);
402 format->type = video->type;
403 return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
407 isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
409 struct v4l2_format format;
412 memcpy(&format, &vfh->format, sizeof(format));
413 ret = __isp_video_get_format(video, &format);
417 if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
418 vfh->format.fmt.pix.height != format.fmt.pix.height ||
419 vfh->format.fmt.pix.width != format.fmt.pix.width ||
420 vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
421 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
427 /* -----------------------------------------------------------------------------
431 #define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
434 * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
435 * @dev: Device pointer specific to the OMAP3 ISP.
436 * @sglist: Pointer to source Scatter gather list to allocate.
437 * @sglen: Number of elements of the scatter-gatter list.
439 * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
440 * we ran out of memory.
443 ispmmu_vmap(struct isp_device *isp, const struct scatterlist *sglist, int sglen)
445 struct sg_table *sgt;
448 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
452 sgt->sgl = (struct scatterlist *)sglist;
454 sgt->orig_nents = sglen;
456 da = omap_iommu_vmap(isp->domain, isp->iommu, 0, sgt, IOMMU_FLAG);
457 if (IS_ERR_VALUE(da))
464 * ispmmu_vunmap - Unmap a device address from the ISP MMU
465 * @dev: Device pointer specific to the OMAP3 ISP.
466 * @da: Device address generated from a ispmmu_vmap call.
468 static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
470 struct sg_table *sgt;
472 sgt = omap_iommu_vunmap(isp->domain, isp->iommu, (u32)da);
476 /* -----------------------------------------------------------------------------
477 * Video queue operations
480 static void isp_video_queue_prepare(struct isp_video_queue *queue,
481 unsigned int *nbuffers, unsigned int *size)
483 struct isp_video_fh *vfh =
484 container_of(queue, struct isp_video_fh, queue);
485 struct isp_video *video = vfh->video;
487 *size = vfh->format.fmt.pix.sizeimage;
491 *nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size));
494 static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
496 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
497 struct isp_buffer *buffer = to_isp_buffer(buf);
498 struct isp_video *video = vfh->video;
500 if (buffer->isp_addr) {
501 ispmmu_vunmap(video->isp, buffer->isp_addr);
502 buffer->isp_addr = 0;
506 static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
508 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
509 struct isp_buffer *buffer = to_isp_buffer(buf);
510 struct isp_video *video = vfh->video;
513 addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen);
514 if (IS_ERR_VALUE(addr))
517 if (!IS_ALIGNED(addr, 32)) {
518 dev_dbg(video->isp->dev, "Buffer address must be "
519 "aligned to 32 bytes boundary.\n");
520 ispmmu_vunmap(video->isp, buffer->isp_addr);
524 buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
525 buffer->isp_addr = addr;
530 * isp_video_buffer_queue - Add buffer to streaming queue
533 * In memory-to-memory mode, start streaming on the pipeline if buffers are
534 * queued on both the input and the output, if the pipeline isn't already busy.
535 * If the pipeline is busy, it will be restarted in the output module interrupt
538 static void isp_video_buffer_queue(struct isp_video_buffer *buf)
540 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
541 struct isp_buffer *buffer = to_isp_buffer(buf);
542 struct isp_video *video = vfh->video;
543 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
544 enum isp_pipeline_state state;
549 empty = list_empty(&video->dmaqueue);
550 list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
553 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
554 state = ISP_PIPELINE_QUEUE_OUTPUT;
556 state = ISP_PIPELINE_QUEUE_INPUT;
558 spin_lock_irqsave(&pipe->lock, flags);
559 pipe->state |= state;
560 video->ops->queue(video, buffer);
561 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
563 start = isp_pipeline_ready(pipe);
565 pipe->state |= ISP_PIPELINE_STREAM;
566 spin_unlock_irqrestore(&pipe->lock, flags);
569 omap3isp_pipeline_set_stream(pipe,
570 ISP_PIPELINE_STREAM_SINGLESHOT);
574 static const struct isp_video_queue_operations isp_video_queue_ops = {
575 .queue_prepare = &isp_video_queue_prepare,
576 .buffer_prepare = &isp_video_buffer_prepare,
577 .buffer_queue = &isp_video_buffer_queue,
578 .buffer_cleanup = &isp_video_buffer_cleanup,
582 * omap3isp_video_buffer_next - Complete the current buffer and return the next
583 * @video: ISP video object
584 * @error: Whether an error occurred during capture
586 * Remove the current video buffer from the DMA queue and fill its timestamp,
587 * field count and state fields before waking up its completion handler.
589 * The buffer state is set to VIDEOBUF_DONE if no error occurred (@error is 0)
590 * or VIDEOBUF_ERROR otherwise (@error is non-zero).
592 * The DMA queue is expected to contain at least one buffer.
594 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
597 struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video,
600 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
601 struct isp_video_queue *queue = video->queue;
602 enum isp_pipeline_state state;
603 struct isp_video_buffer *buf;
607 spin_lock_irqsave(&queue->irqlock, flags);
608 if (WARN_ON(list_empty(&video->dmaqueue))) {
609 spin_unlock_irqrestore(&queue->irqlock, flags);
613 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
615 list_del(&buf->irqlist);
616 spin_unlock_irqrestore(&queue->irqlock, flags);
619 buf->vbuf.timestamp.tv_sec = ts.tv_sec;
620 buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
622 /* Do frame number propagation only if this is the output video node.
623 * Frame number either comes from the CSI receivers or it gets
624 * incremented here if H3A is not active.
625 * Note: There is no guarantee that the output buffer will finish
626 * first, so the input number might lag behind by 1 in some cases.
628 if (video == pipe->output && !pipe->do_propagation)
629 buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
631 buf->vbuf.sequence = atomic_read(&pipe->frame_number);
633 buf->state = error ? ISP_BUF_STATE_ERROR : ISP_BUF_STATE_DONE;
637 if (list_empty(&video->dmaqueue)) {
638 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
639 state = ISP_PIPELINE_QUEUE_OUTPUT
640 | ISP_PIPELINE_STREAM;
642 state = ISP_PIPELINE_QUEUE_INPUT
643 | ISP_PIPELINE_STREAM;
645 spin_lock_irqsave(&pipe->lock, flags);
646 pipe->state &= ~state;
647 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
648 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
649 spin_unlock_irqrestore(&pipe->lock, flags);
653 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
654 spin_lock_irqsave(&pipe->lock, flags);
655 pipe->state &= ~ISP_PIPELINE_STREAM;
656 spin_unlock_irqrestore(&pipe->lock, flags);
659 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
661 buf->state = ISP_BUF_STATE_ACTIVE;
662 return to_isp_buffer(buf);
666 * omap3isp_video_resume - Perform resume operation on the buffers
667 * @video: ISP video object
668 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
670 * This function is intended to be used on suspend/resume scenario. It
671 * requests video queue layer to discard buffers marked as DONE if it's in
672 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
675 void omap3isp_video_resume(struct isp_video *video, int continuous)
677 struct isp_buffer *buf = NULL;
679 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
680 omap3isp_video_queue_discard_done(video->queue);
682 if (!list_empty(&video->dmaqueue)) {
683 buf = list_first_entry(&video->dmaqueue,
684 struct isp_buffer, buffer.irqlist);
685 video->ops->queue(video, buf);
686 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
689 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
693 /* -----------------------------------------------------------------------------
698 isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
700 struct isp_video *video = video_drvdata(file);
702 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
703 strlcpy(cap->card, video->video.name, sizeof(cap->card));
704 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
706 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
707 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
709 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
715 isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
717 struct isp_video_fh *vfh = to_isp_video_fh(fh);
718 struct isp_video *video = video_drvdata(file);
720 if (format->type != video->type)
723 mutex_lock(&video->mutex);
724 *format = vfh->format;
725 mutex_unlock(&video->mutex);
731 isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
733 struct isp_video_fh *vfh = to_isp_video_fh(fh);
734 struct isp_video *video = video_drvdata(file);
735 struct v4l2_mbus_framefmt fmt;
737 if (format->type != video->type)
740 mutex_lock(&video->mutex);
742 /* Fill the bytesperline and sizeimage fields by converting to media bus
743 * format and back to pixel format.
745 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
746 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
748 vfh->format = *format;
750 mutex_unlock(&video->mutex);
755 isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
757 struct isp_video *video = video_drvdata(file);
758 struct v4l2_subdev_format fmt;
759 struct v4l2_subdev *subdev;
763 if (format->type != video->type)
766 subdev = isp_video_remote_subdev(video, &pad);
770 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
773 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
774 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
776 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
778 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
783 isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
785 struct isp_video *video = video_drvdata(file);
786 struct v4l2_subdev *subdev;
789 subdev = isp_video_remote_subdev(video, NULL);
793 mutex_lock(&video->mutex);
794 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
795 mutex_unlock(&video->mutex);
797 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
801 isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
803 struct isp_video *video = video_drvdata(file);
804 struct v4l2_subdev_format format;
805 struct v4l2_subdev *subdev;
809 subdev = isp_video_remote_subdev(video, &pad);
813 /* Try the get crop operation first and fallback to get format if not
816 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
817 if (ret != -ENOIOCTLCMD)
821 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
822 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
824 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
828 crop->c.width = format.format.width;
829 crop->c.height = format.format.height;
835 isp_video_set_crop(struct file *file, void *fh, struct v4l2_crop *crop)
837 struct isp_video *video = video_drvdata(file);
838 struct v4l2_subdev *subdev;
841 subdev = isp_video_remote_subdev(video, NULL);
845 mutex_lock(&video->mutex);
846 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
847 mutex_unlock(&video->mutex);
849 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
853 isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
855 struct isp_video_fh *vfh = to_isp_video_fh(fh);
856 struct isp_video *video = video_drvdata(file);
858 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
859 video->type != a->type)
862 memset(a, 0, sizeof(*a));
863 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
864 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
865 a->parm.output.timeperframe = vfh->timeperframe;
871 isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
873 struct isp_video_fh *vfh = to_isp_video_fh(fh);
874 struct isp_video *video = video_drvdata(file);
876 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
877 video->type != a->type)
880 if (a->parm.output.timeperframe.denominator == 0)
881 a->parm.output.timeperframe.denominator = 1;
883 vfh->timeperframe = a->parm.output.timeperframe;
889 isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
891 struct isp_video_fh *vfh = to_isp_video_fh(fh);
893 return omap3isp_video_queue_reqbufs(&vfh->queue, rb);
897 isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
899 struct isp_video_fh *vfh = to_isp_video_fh(fh);
901 return omap3isp_video_queue_querybuf(&vfh->queue, b);
905 isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
907 struct isp_video_fh *vfh = to_isp_video_fh(fh);
909 return omap3isp_video_queue_qbuf(&vfh->queue, b);
913 isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
915 struct isp_video_fh *vfh = to_isp_video_fh(fh);
917 return omap3isp_video_queue_dqbuf(&vfh->queue, b,
918 file->f_flags & O_NONBLOCK);
924 * Every ISP pipeline has a single input and a single output. The input can be
925 * either a sensor or a video node. The output is always a video node.
927 * As every pipeline has an output video node, the ISP video objects at the
928 * pipeline output stores the pipeline state. It tracks the streaming state of
929 * both the input and output, as well as the availability of buffers.
931 * In sensor-to-memory mode, frames are always available at the pipeline input.
932 * Starting the sensor usually requires I2C transfers and must be done in
933 * interruptible context. The pipeline is started and stopped synchronously
934 * to the stream on/off commands. All modules in the pipeline will get their
935 * subdev set stream handler called. The module at the end of the pipeline must
936 * delay starting the hardware until buffers are available at its output.
938 * In memory-to-memory mode, starting/stopping the stream requires
939 * synchronization between the input and output. ISP modules can't be stopped
940 * in the middle of a frame, and at least some of the modules seem to become
941 * busy as soon as they're started, even if they don't receive a frame start
942 * event. For that reason frames need to be processed in single-shot mode. The
943 * driver needs to wait until a frame is completely processed and written to
944 * memory before restarting the pipeline for the next frame. Pipelined
945 * processing might be possible but requires more testing.
947 * Stream start must be delayed until buffers are available at both the input
948 * and output. The pipeline must be started in the videobuf queue callback with
949 * the buffers queue spinlock held. The modules subdev set stream operation must
953 isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
955 struct isp_video_fh *vfh = to_isp_video_fh(fh);
956 struct isp_video *video = video_drvdata(file);
957 enum isp_pipeline_state state;
958 struct isp_pipeline *pipe;
959 struct isp_video *far_end;
963 if (type != video->type)
966 mutex_lock(&video->stream_lock);
968 if (video->streaming) {
969 mutex_unlock(&video->stream_lock);
973 /* Start streaming on the pipeline. No link touching an entity in the
974 * pipeline can be activated or deactivated once streaming is started.
976 pipe = video->video.entity.pipe
977 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
978 media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
980 /* Verify that the currently configured format matches the output of
981 * the connected subdev.
983 ret = isp_video_check_format(video, vfh);
987 video->bpl_padding = ret;
988 video->bpl_value = vfh->format.fmt.pix.bytesperline;
990 /* Find the ISP video node connected at the far end of the pipeline and
991 * update the pipeline.
993 far_end = isp_video_far_end(video);
995 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
996 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
997 pipe->input = far_end;
998 pipe->output = video;
1000 if (far_end == NULL) {
1005 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1006 pipe->input = video;
1007 pipe->output = far_end;
1010 if (video->isp->pdata->set_constraints)
1011 video->isp->pdata->set_constraints(video->isp, true);
1012 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1014 /* Validate the pipeline and update its state. */
1015 ret = isp_video_validate_pipeline(pipe);
1019 spin_lock_irqsave(&pipe->lock, flags);
1020 pipe->state &= ~ISP_PIPELINE_STREAM;
1021 pipe->state |= state;
1022 spin_unlock_irqrestore(&pipe->lock, flags);
1024 /* Set the maximum time per frame as the value requested by userspace.
1025 * This is a soft limit that can be overridden if the hardware doesn't
1026 * support the request limit.
1028 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1029 pipe->max_timeperframe = vfh->timeperframe;
1031 video->queue = &vfh->queue;
1032 INIT_LIST_HEAD(&video->dmaqueue);
1033 atomic_set(&pipe->frame_number, -1);
1035 ret = omap3isp_video_queue_streamon(&vfh->queue);
1039 /* In sensor-to-memory mode, the stream can be started synchronously
1040 * to the stream on command. In memory-to-memory mode, it will be
1041 * started when buffers are queued on both the input and output.
1043 if (pipe->input == NULL) {
1044 ret = omap3isp_pipeline_set_stream(pipe,
1045 ISP_PIPELINE_STREAM_CONTINUOUS);
1048 spin_lock_irqsave(&video->queue->irqlock, flags);
1049 if (list_empty(&video->dmaqueue))
1050 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1051 spin_unlock_irqrestore(&video->queue->irqlock, flags);
1056 omap3isp_video_queue_streamoff(&vfh->queue);
1057 if (video->isp->pdata->set_constraints)
1058 video->isp->pdata->set_constraints(video->isp, false);
1059 media_entity_pipeline_stop(&video->video.entity);
1060 /* The DMA queue must be emptied here, otherwise CCDC interrupts
1061 * that will get triggered the next time the CCDC is powered up
1062 * will try to access buffers that might have been freed but
1063 * still present in the DMA queue. This can easily get triggered
1064 * if the above omap3isp_pipeline_set_stream() call fails on a
1065 * system with a free-running sensor.
1067 INIT_LIST_HEAD(&video->dmaqueue);
1068 video->queue = NULL;
1072 video->streaming = 1;
1074 mutex_unlock(&video->stream_lock);
1079 isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1081 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1082 struct isp_video *video = video_drvdata(file);
1083 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1084 enum isp_pipeline_state state;
1085 unsigned int streaming;
1086 unsigned long flags;
1088 if (type != video->type)
1091 mutex_lock(&video->stream_lock);
1093 /* Make sure we're not streaming yet. */
1094 mutex_lock(&vfh->queue.lock);
1095 streaming = vfh->queue.streaming;
1096 mutex_unlock(&vfh->queue.lock);
1101 /* Update the pipeline state. */
1102 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1103 state = ISP_PIPELINE_STREAM_OUTPUT
1104 | ISP_PIPELINE_QUEUE_OUTPUT;
1106 state = ISP_PIPELINE_STREAM_INPUT
1107 | ISP_PIPELINE_QUEUE_INPUT;
1109 spin_lock_irqsave(&pipe->lock, flags);
1110 pipe->state &= ~state;
1111 spin_unlock_irqrestore(&pipe->lock, flags);
1113 /* Stop the stream. */
1114 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1115 omap3isp_video_queue_streamoff(&vfh->queue);
1116 video->queue = NULL;
1117 video->streaming = 0;
1119 if (video->isp->pdata->set_constraints)
1120 video->isp->pdata->set_constraints(video->isp, false);
1121 media_entity_pipeline_stop(&video->video.entity);
1124 mutex_unlock(&video->stream_lock);
1129 isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1131 if (input->index > 0)
1134 strlcpy(input->name, "camera", sizeof(input->name));
1135 input->type = V4L2_INPUT_TYPE_CAMERA;
1141 isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1149 isp_video_s_input(struct file *file, void *fh, unsigned int input)
1151 return input == 0 ? 0 : -EINVAL;
1154 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1155 .vidioc_querycap = isp_video_querycap,
1156 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1157 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1158 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1159 .vidioc_g_fmt_vid_out = isp_video_get_format,
1160 .vidioc_s_fmt_vid_out = isp_video_set_format,
1161 .vidioc_try_fmt_vid_out = isp_video_try_format,
1162 .vidioc_cropcap = isp_video_cropcap,
1163 .vidioc_g_crop = isp_video_get_crop,
1164 .vidioc_s_crop = isp_video_set_crop,
1165 .vidioc_g_parm = isp_video_get_param,
1166 .vidioc_s_parm = isp_video_set_param,
1167 .vidioc_reqbufs = isp_video_reqbufs,
1168 .vidioc_querybuf = isp_video_querybuf,
1169 .vidioc_qbuf = isp_video_qbuf,
1170 .vidioc_dqbuf = isp_video_dqbuf,
1171 .vidioc_streamon = isp_video_streamon,
1172 .vidioc_streamoff = isp_video_streamoff,
1173 .vidioc_enum_input = isp_video_enum_input,
1174 .vidioc_g_input = isp_video_g_input,
1175 .vidioc_s_input = isp_video_s_input,
1178 /* -----------------------------------------------------------------------------
1179 * V4L2 file operations
1182 static int isp_video_open(struct file *file)
1184 struct isp_video *video = video_drvdata(file);
1185 struct isp_video_fh *handle;
1188 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1192 v4l2_fh_init(&handle->vfh, &video->video);
1193 v4l2_fh_add(&handle->vfh);
1195 /* If this is the first user, initialise the pipeline. */
1196 if (omap3isp_get(video->isp) == NULL) {
1201 ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1203 omap3isp_put(video->isp);
1207 omap3isp_video_queue_init(&handle->queue, video->type,
1208 &isp_video_queue_ops, video->isp->dev,
1209 sizeof(struct isp_buffer));
1211 memset(&handle->format, 0, sizeof(handle->format));
1212 handle->format.type = video->type;
1213 handle->timeperframe.denominator = 1;
1215 handle->video = video;
1216 file->private_data = &handle->vfh;
1220 v4l2_fh_del(&handle->vfh);
1227 static int isp_video_release(struct file *file)
1229 struct isp_video *video = video_drvdata(file);
1230 struct v4l2_fh *vfh = file->private_data;
1231 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1233 /* Disable streaming and free the buffers queue resources. */
1234 isp_video_streamoff(file, vfh, video->type);
1236 mutex_lock(&handle->queue.lock);
1237 omap3isp_video_queue_cleanup(&handle->queue);
1238 mutex_unlock(&handle->queue.lock);
1240 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1242 /* Release the file handle. */
1245 file->private_data = NULL;
1247 omap3isp_put(video->isp);
1252 static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1254 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1255 struct isp_video_queue *queue = &vfh->queue;
1257 return omap3isp_video_queue_poll(queue, file, wait);
1260 static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1262 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1264 return omap3isp_video_queue_mmap(&vfh->queue, vma);
1267 static struct v4l2_file_operations isp_video_fops = {
1268 .owner = THIS_MODULE,
1269 .unlocked_ioctl = video_ioctl2,
1270 .open = isp_video_open,
1271 .release = isp_video_release,
1272 .poll = isp_video_poll,
1273 .mmap = isp_video_mmap,
1276 /* -----------------------------------------------------------------------------
1280 static const struct isp_video_operations isp_video_dummy_ops = {
1283 int omap3isp_video_init(struct isp_video *video, const char *name)
1285 const char *direction;
1288 switch (video->type) {
1289 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1290 direction = "output";
1291 video->pad.flags = MEDIA_PAD_FL_SINK;
1293 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1294 direction = "input";
1295 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1302 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1306 mutex_init(&video->mutex);
1307 atomic_set(&video->active, 0);
1309 spin_lock_init(&video->pipe.lock);
1310 mutex_init(&video->stream_lock);
1312 /* Initialize the video device. */
1313 if (video->ops == NULL)
1314 video->ops = &isp_video_dummy_ops;
1316 video->video.fops = &isp_video_fops;
1317 snprintf(video->video.name, sizeof(video->video.name),
1318 "OMAP3 ISP %s %s", name, direction);
1319 video->video.vfl_type = VFL_TYPE_GRABBER;
1320 video->video.release = video_device_release_empty;
1321 video->video.ioctl_ops = &isp_video_ioctl_ops;
1322 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1324 video_set_drvdata(&video->video, video);
1329 void omap3isp_video_cleanup(struct isp_video *video)
1331 media_entity_cleanup(&video->video.entity);
1332 mutex_destroy(&video->stream_lock);
1333 mutex_destroy(&video->mutex);
1336 int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1340 video->video.v4l2_dev = vdev;
1342 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1344 printk(KERN_ERR "%s: could not register video device (%d)\n",
1350 void omap3isp_video_unregister(struct isp_video *video)
1352 if (video_is_registered(&video->video))
1353 video_unregister_device(&video->video);