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/pagemap.h>
30 #include <linux/scatterlist.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/vmalloc.h>
34 #include <media/v4l2-dev.h>
35 #include <media/v4l2-ioctl.h>
36 #include <plat/iommu.h>
37 #include <plat/iovmm.h>
38 #include <plat/omap-pm.h>
44 /* -----------------------------------------------------------------------------
48 static struct isp_format_info formats[] = {
49 { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
50 V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
51 V4L2_PIX_FMT_GREY, 8, },
52 { V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10,
53 V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8,
54 V4L2_PIX_FMT_Y10, 10, },
55 { V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10,
56 V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8,
57 V4L2_PIX_FMT_Y12, 12, },
58 { V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
59 V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
60 V4L2_PIX_FMT_SBGGR8, 8, },
61 { V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
62 V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
63 V4L2_PIX_FMT_SGBRG8, 8, },
64 { V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
65 V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
66 V4L2_PIX_FMT_SGRBG8, 8, },
67 { V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
68 V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
69 V4L2_PIX_FMT_SRGGB8, 8, },
70 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
71 V4L2_MBUS_FMT_SGRBG10_1X10, 0,
72 V4L2_PIX_FMT_SGRBG10DPCM8, 8, },
73 { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
74 V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8,
75 V4L2_PIX_FMT_SBGGR10, 10, },
76 { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
77 V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8,
78 V4L2_PIX_FMT_SGBRG10, 10, },
79 { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
80 V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8,
81 V4L2_PIX_FMT_SGRBG10, 10, },
82 { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
83 V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8,
84 V4L2_PIX_FMT_SRGGB10, 10, },
85 { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
86 V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8,
87 V4L2_PIX_FMT_SBGGR12, 12, },
88 { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
89 V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8,
90 V4L2_PIX_FMT_SGBRG12, 12, },
91 { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
92 V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8,
93 V4L2_PIX_FMT_SGRBG12, 12, },
94 { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
95 V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8,
96 V4L2_PIX_FMT_SRGGB12, 12, },
97 { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
98 V4L2_MBUS_FMT_UYVY8_1X16, 0,
99 V4L2_PIX_FMT_UYVY, 16, },
100 { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
101 V4L2_MBUS_FMT_YUYV8_1X16, 0,
102 V4L2_PIX_FMT_YUYV, 16, },
105 const struct isp_format_info *
106 omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
110 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
111 if (formats[i].code == code)
119 * Decide whether desired output pixel code can be obtained with
120 * the lane shifter by shifting the input pixel code.
121 * @in: input pixelcode to shifter
122 * @out: output pixelcode from shifter
123 * @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
125 * return true if the combination is possible
126 * return false otherwise
128 static bool isp_video_is_shiftable(enum v4l2_mbus_pixelcode in,
129 enum v4l2_mbus_pixelcode out,
130 unsigned int additional_shift)
132 const struct isp_format_info *in_info, *out_info;
137 in_info = omap3isp_video_format_info(in);
138 out_info = omap3isp_video_format_info(out);
140 if ((in_info->flavor == 0) || (out_info->flavor == 0))
143 if (in_info->flavor != out_info->flavor)
146 return in_info->bpp - out_info->bpp + additional_shift <= 6;
150 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
151 * @video: ISP video instance
152 * @mbus: v4l2_mbus_framefmt format (input)
153 * @pix: v4l2_pix_format format (output)
155 * Fill the output pix structure with information from the input mbus format.
156 * The bytesperline and sizeimage fields are computed from the requested bytes
157 * per line value in the pix format and information from the video instance.
159 * Return the number of padding bytes at end of line.
161 static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
162 const struct v4l2_mbus_framefmt *mbus,
163 struct v4l2_pix_format *pix)
165 unsigned int bpl = pix->bytesperline;
166 unsigned int min_bpl;
169 memset(pix, 0, sizeof(*pix));
170 pix->width = mbus->width;
171 pix->height = mbus->height;
173 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
174 if (formats[i].code == mbus->code)
178 if (WARN_ON(i == ARRAY_SIZE(formats)))
181 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
183 /* Clamp the requested bytes per line value. If the maximum bytes per
184 * line value is zero, the module doesn't support user configurable line
185 * sizes. Override the requested value with the minimum in that case.
188 bpl = clamp(bpl, min_bpl, video->bpl_max);
192 if (!video->bpl_zero_padding || bpl != min_bpl)
193 bpl = ALIGN(bpl, video->bpl_alignment);
195 pix->pixelformat = formats[i].pixelformat;
196 pix->bytesperline = bpl;
197 pix->sizeimage = pix->bytesperline * pix->height;
198 pix->colorspace = mbus->colorspace;
199 pix->field = mbus->field;
201 return bpl - min_bpl;
204 static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
205 struct v4l2_mbus_framefmt *mbus)
209 memset(mbus, 0, sizeof(*mbus));
210 mbus->width = pix->width;
211 mbus->height = pix->height;
213 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
214 if (formats[i].pixelformat == pix->pixelformat)
218 if (WARN_ON(i == ARRAY_SIZE(formats)))
221 mbus->code = formats[i].code;
222 mbus->colorspace = pix->colorspace;
223 mbus->field = pix->field;
226 static struct v4l2_subdev *
227 isp_video_remote_subdev(struct isp_video *video, u32 *pad)
229 struct media_pad *remote;
231 remote = media_entity_remote_source(&video->pad);
233 if (remote == NULL ||
234 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
238 *pad = remote->index;
240 return media_entity_to_v4l2_subdev(remote->entity);
243 /* Return a pointer to the ISP video instance at the far end of the pipeline. */
244 static struct isp_video *
245 isp_video_far_end(struct isp_video *video)
247 struct media_entity_graph graph;
248 struct media_entity *entity = &video->video.entity;
249 struct media_device *mdev = entity->parent;
250 struct isp_video *far_end = NULL;
252 mutex_lock(&mdev->graph_mutex);
253 media_entity_graph_walk_start(&graph, entity);
255 while ((entity = media_entity_graph_walk_next(&graph))) {
256 if (entity == &video->video.entity)
259 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
262 far_end = to_isp_video(media_entity_to_video_device(entity));
263 if (far_end->type != video->type)
269 mutex_unlock(&mdev->graph_mutex);
274 * Validate a pipeline by checking both ends of all links for format
277 * Compute the minimum time per frame value as the maximum of time per frame
278 * limits reported by every block in the pipeline.
280 * Return 0 if all formats match, or -EPIPE if at least one link is found with
281 * different formats on its two ends or if the pipeline doesn't start with a
282 * video source (either a subdev with no input pad, or a non-subdev entity).
284 static int isp_video_validate_pipeline(struct isp_pipeline *pipe)
286 struct isp_device *isp = pipe->output->isp;
287 struct v4l2_subdev_format fmt_source;
288 struct v4l2_subdev_format fmt_sink;
289 struct media_pad *pad;
290 struct v4l2_subdev *subdev;
293 pipe->max_rate = pipe->l3_ick;
295 subdev = isp_video_remote_subdev(pipe->output, NULL);
300 unsigned int shifter_link;
301 /* Retrieve the sink format */
302 pad = &subdev->entity.pads[0];
303 if (!(pad->flags & MEDIA_PAD_FL_SINK))
306 fmt_sink.pad = pad->index;
307 fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
308 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_sink);
309 if (ret < 0 && ret != -ENOIOCTLCMD)
312 /* Update the maximum frame rate */
313 if (subdev == &isp->isp_res.subdev)
314 omap3isp_resizer_max_rate(&isp->isp_res,
317 /* Check ccdc maximum data rate when data comes from sensor
318 * TODO: Include ccdc rate in pipe->max_rate and compare the
319 * total pipe rate with the input data rate from sensor.
321 if (subdev == &isp->isp_ccdc.subdev && pipe->input == NULL) {
322 unsigned int rate = UINT_MAX;
324 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
325 if (isp->isp_ccdc.vpcfg.pixelclk > rate)
329 /* If sink pad is on CCDC, the link has the lane shifter
330 * in the middle of it. */
331 shifter_link = subdev == &isp->isp_ccdc.subdev;
333 /* Retrieve the source format. Return an error if no source
334 * entity can be found, and stop checking the pipeline if the
335 * source entity isn't a subdev.
337 pad = media_entity_remote_source(pad);
341 if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
344 subdev = media_entity_to_v4l2_subdev(pad->entity);
346 fmt_source.pad = pad->index;
347 fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
348 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
349 if (ret < 0 && ret != -ENOIOCTLCMD)
352 /* Check if the two ends match */
353 if (fmt_source.format.width != fmt_sink.format.width ||
354 fmt_source.format.height != fmt_sink.format.height)
358 unsigned int parallel_shift = 0;
359 if (isp->isp_ccdc.input == CCDC_INPUT_PARALLEL) {
360 struct isp_parallel_platform_data *pdata =
361 &((struct isp_v4l2_subdevs_group *)
362 subdev->host_priv)->bus.parallel;
363 parallel_shift = pdata->data_lane_shift * 2;
365 if (!isp_video_is_shiftable(fmt_source.format.code,
366 fmt_sink.format.code,
369 } else if (fmt_source.format.code != fmt_sink.format.code)
377 __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
379 struct v4l2_subdev_format fmt;
380 struct v4l2_subdev *subdev;
384 subdev = isp_video_remote_subdev(video, &pad);
388 mutex_lock(&video->mutex);
391 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
392 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
393 if (ret == -ENOIOCTLCMD)
396 mutex_unlock(&video->mutex);
401 format->type = video->type;
402 return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
406 isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
408 struct v4l2_format format;
411 memcpy(&format, &vfh->format, sizeof(format));
412 ret = __isp_video_get_format(video, &format);
416 if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
417 vfh->format.fmt.pix.height != format.fmt.pix.height ||
418 vfh->format.fmt.pix.width != format.fmt.pix.width ||
419 vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
420 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
426 /* -----------------------------------------------------------------------------
430 #define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
433 * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
434 * @dev: Device pointer specific to the OMAP3 ISP.
435 * @sglist: Pointer to source Scatter gather list to allocate.
436 * @sglen: Number of elements of the scatter-gatter list.
438 * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
439 * we ran out of memory.
442 ispmmu_vmap(struct isp_device *isp, const struct scatterlist *sglist, int sglen)
444 struct sg_table *sgt;
447 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
451 sgt->sgl = (struct scatterlist *)sglist;
453 sgt->orig_nents = sglen;
455 da = omap_iommu_vmap(isp->domain, isp->iommu, 0, sgt, IOMMU_FLAG);
456 if (IS_ERR_VALUE(da))
463 * ispmmu_vunmap - Unmap a device address from the ISP MMU
464 * @dev: Device pointer specific to the OMAP3 ISP.
465 * @da: Device address generated from a ispmmu_vmap call.
467 static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
469 struct sg_table *sgt;
471 sgt = omap_iommu_vunmap(isp->domain, isp->iommu, (u32)da);
475 /* -----------------------------------------------------------------------------
476 * Video queue operations
479 static void isp_video_queue_prepare(struct isp_video_queue *queue,
480 unsigned int *nbuffers, unsigned int *size)
482 struct isp_video_fh *vfh =
483 container_of(queue, struct isp_video_fh, queue);
484 struct isp_video *video = vfh->video;
486 *size = vfh->format.fmt.pix.sizeimage;
490 *nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size));
493 static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
495 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
496 struct isp_buffer *buffer = to_isp_buffer(buf);
497 struct isp_video *video = vfh->video;
499 if (buffer->isp_addr) {
500 ispmmu_vunmap(video->isp, buffer->isp_addr);
501 buffer->isp_addr = 0;
505 static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
507 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
508 struct isp_buffer *buffer = to_isp_buffer(buf);
509 struct isp_video *video = vfh->video;
512 addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen);
513 if (IS_ERR_VALUE(addr))
516 if (!IS_ALIGNED(addr, 32)) {
517 dev_dbg(video->isp->dev, "Buffer address must be "
518 "aligned to 32 bytes boundary.\n");
519 ispmmu_vunmap(video->isp, buffer->isp_addr);
523 buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
524 buffer->isp_addr = addr;
529 * isp_video_buffer_queue - Add buffer to streaming queue
532 * In memory-to-memory mode, start streaming on the pipeline if buffers are
533 * queued on both the input and the output, if the pipeline isn't already busy.
534 * If the pipeline is busy, it will be restarted in the output module interrupt
537 static void isp_video_buffer_queue(struct isp_video_buffer *buf)
539 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
540 struct isp_buffer *buffer = to_isp_buffer(buf);
541 struct isp_video *video = vfh->video;
542 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
543 enum isp_pipeline_state state;
548 empty = list_empty(&video->dmaqueue);
549 list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
552 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
553 state = ISP_PIPELINE_QUEUE_OUTPUT;
555 state = ISP_PIPELINE_QUEUE_INPUT;
557 spin_lock_irqsave(&pipe->lock, flags);
558 pipe->state |= state;
559 video->ops->queue(video, buffer);
560 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
562 start = isp_pipeline_ready(pipe);
564 pipe->state |= ISP_PIPELINE_STREAM;
565 spin_unlock_irqrestore(&pipe->lock, flags);
568 omap3isp_pipeline_set_stream(pipe,
569 ISP_PIPELINE_STREAM_SINGLESHOT);
573 static const struct isp_video_queue_operations isp_video_queue_ops = {
574 .queue_prepare = &isp_video_queue_prepare,
575 .buffer_prepare = &isp_video_buffer_prepare,
576 .buffer_queue = &isp_video_buffer_queue,
577 .buffer_cleanup = &isp_video_buffer_cleanup,
581 * omap3isp_video_buffer_next - Complete the current buffer and return the next
582 * @video: ISP video object
583 * @error: Whether an error occurred during capture
585 * Remove the current video buffer from the DMA queue and fill its timestamp,
586 * field count and state fields before waking up its completion handler.
588 * The buffer state is set to VIDEOBUF_DONE if no error occurred (@error is 0)
589 * or VIDEOBUF_ERROR otherwise (@error is non-zero).
591 * The DMA queue is expected to contain at least one buffer.
593 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
596 struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video,
599 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
600 struct isp_video_queue *queue = video->queue;
601 enum isp_pipeline_state state;
602 struct isp_video_buffer *buf;
606 spin_lock_irqsave(&queue->irqlock, flags);
607 if (WARN_ON(list_empty(&video->dmaqueue))) {
608 spin_unlock_irqrestore(&queue->irqlock, flags);
612 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
614 list_del(&buf->irqlist);
615 spin_unlock_irqrestore(&queue->irqlock, flags);
618 buf->vbuf.timestamp.tv_sec = ts.tv_sec;
619 buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
621 /* Do frame number propagation only if this is the output video node.
622 * Frame number either comes from the CSI receivers or it gets
623 * incremented here if H3A is not active.
624 * Note: There is no guarantee that the output buffer will finish
625 * first, so the input number might lag behind by 1 in some cases.
627 if (video == pipe->output && !pipe->do_propagation)
628 buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
630 buf->vbuf.sequence = atomic_read(&pipe->frame_number);
632 buf->state = error ? ISP_BUF_STATE_ERROR : ISP_BUF_STATE_DONE;
636 if (list_empty(&video->dmaqueue)) {
637 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
638 state = ISP_PIPELINE_QUEUE_OUTPUT
639 | ISP_PIPELINE_STREAM;
641 state = ISP_PIPELINE_QUEUE_INPUT
642 | ISP_PIPELINE_STREAM;
644 spin_lock_irqsave(&pipe->lock, flags);
645 pipe->state &= ~state;
646 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
647 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
648 spin_unlock_irqrestore(&pipe->lock, flags);
652 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
653 spin_lock_irqsave(&pipe->lock, flags);
654 pipe->state &= ~ISP_PIPELINE_STREAM;
655 spin_unlock_irqrestore(&pipe->lock, flags);
658 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
660 buf->state = ISP_BUF_STATE_ACTIVE;
661 return to_isp_buffer(buf);
665 * omap3isp_video_resume - Perform resume operation on the buffers
666 * @video: ISP video object
667 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
669 * This function is intended to be used on suspend/resume scenario. It
670 * requests video queue layer to discard buffers marked as DONE if it's in
671 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
674 void omap3isp_video_resume(struct isp_video *video, int continuous)
676 struct isp_buffer *buf = NULL;
678 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
679 omap3isp_video_queue_discard_done(video->queue);
681 if (!list_empty(&video->dmaqueue)) {
682 buf = list_first_entry(&video->dmaqueue,
683 struct isp_buffer, buffer.irqlist);
684 video->ops->queue(video, buf);
685 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
688 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
692 /* -----------------------------------------------------------------------------
697 isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
699 struct isp_video *video = video_drvdata(file);
701 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
702 strlcpy(cap->card, video->video.name, sizeof(cap->card));
703 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
705 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
706 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
708 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
714 isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
716 struct isp_video_fh *vfh = to_isp_video_fh(fh);
717 struct isp_video *video = video_drvdata(file);
719 if (format->type != video->type)
722 mutex_lock(&video->mutex);
723 *format = vfh->format;
724 mutex_unlock(&video->mutex);
730 isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
732 struct isp_video_fh *vfh = to_isp_video_fh(fh);
733 struct isp_video *video = video_drvdata(file);
734 struct v4l2_mbus_framefmt fmt;
736 if (format->type != video->type)
739 mutex_lock(&video->mutex);
741 /* Fill the bytesperline and sizeimage fields by converting to media bus
742 * format and back to pixel format.
744 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
745 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
747 vfh->format = *format;
749 mutex_unlock(&video->mutex);
754 isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
756 struct isp_video *video = video_drvdata(file);
757 struct v4l2_subdev_format fmt;
758 struct v4l2_subdev *subdev;
762 if (format->type != video->type)
765 subdev = isp_video_remote_subdev(video, &pad);
769 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
772 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
773 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
775 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
777 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
782 isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
784 struct isp_video *video = video_drvdata(file);
785 struct v4l2_subdev *subdev;
788 subdev = isp_video_remote_subdev(video, NULL);
792 mutex_lock(&video->mutex);
793 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
794 mutex_unlock(&video->mutex);
796 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
800 isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
802 struct isp_video *video = video_drvdata(file);
803 struct v4l2_subdev_format format;
804 struct v4l2_subdev *subdev;
808 subdev = isp_video_remote_subdev(video, &pad);
812 /* Try the get crop operation first and fallback to get format if not
815 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
816 if (ret != -ENOIOCTLCMD)
820 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
821 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
823 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
827 crop->c.width = format.format.width;
828 crop->c.height = format.format.height;
834 isp_video_set_crop(struct file *file, void *fh, struct v4l2_crop *crop)
836 struct isp_video *video = video_drvdata(file);
837 struct v4l2_subdev *subdev;
840 subdev = isp_video_remote_subdev(video, NULL);
844 mutex_lock(&video->mutex);
845 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
846 mutex_unlock(&video->mutex);
848 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
852 isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
854 struct isp_video_fh *vfh = to_isp_video_fh(fh);
855 struct isp_video *video = video_drvdata(file);
857 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
858 video->type != a->type)
861 memset(a, 0, sizeof(*a));
862 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
863 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
864 a->parm.output.timeperframe = vfh->timeperframe;
870 isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
872 struct isp_video_fh *vfh = to_isp_video_fh(fh);
873 struct isp_video *video = video_drvdata(file);
875 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
876 video->type != a->type)
879 if (a->parm.output.timeperframe.denominator == 0)
880 a->parm.output.timeperframe.denominator = 1;
882 vfh->timeperframe = a->parm.output.timeperframe;
888 isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
890 struct isp_video_fh *vfh = to_isp_video_fh(fh);
892 return omap3isp_video_queue_reqbufs(&vfh->queue, rb);
896 isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
898 struct isp_video_fh *vfh = to_isp_video_fh(fh);
900 return omap3isp_video_queue_querybuf(&vfh->queue, b);
904 isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
906 struct isp_video_fh *vfh = to_isp_video_fh(fh);
908 return omap3isp_video_queue_qbuf(&vfh->queue, b);
912 isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
914 struct isp_video_fh *vfh = to_isp_video_fh(fh);
916 return omap3isp_video_queue_dqbuf(&vfh->queue, b,
917 file->f_flags & O_NONBLOCK);
923 * Every ISP pipeline has a single input and a single output. The input can be
924 * either a sensor or a video node. The output is always a video node.
926 * As every pipeline has an output video node, the ISP video objects at the
927 * pipeline output stores the pipeline state. It tracks the streaming state of
928 * both the input and output, as well as the availability of buffers.
930 * In sensor-to-memory mode, frames are always available at the pipeline input.
931 * Starting the sensor usually requires I2C transfers and must be done in
932 * interruptible context. The pipeline is started and stopped synchronously
933 * to the stream on/off commands. All modules in the pipeline will get their
934 * subdev set stream handler called. The module at the end of the pipeline must
935 * delay starting the hardware until buffers are available at its output.
937 * In memory-to-memory mode, starting/stopping the stream requires
938 * synchronization between the input and output. ISP modules can't be stopped
939 * in the middle of a frame, and at least some of the modules seem to become
940 * busy as soon as they're started, even if they don't receive a frame start
941 * event. For that reason frames need to be processed in single-shot mode. The
942 * driver needs to wait until a frame is completely processed and written to
943 * memory before restarting the pipeline for the next frame. Pipelined
944 * processing might be possible but requires more testing.
946 * Stream start must be delayed until buffers are available at both the input
947 * and output. The pipeline must be started in the videobuf queue callback with
948 * the buffers queue spinlock held. The modules subdev set stream operation must
952 isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
954 struct isp_video_fh *vfh = to_isp_video_fh(fh);
955 struct isp_video *video = video_drvdata(file);
956 enum isp_pipeline_state state;
957 struct isp_pipeline *pipe;
958 struct isp_video *far_end;
962 if (type != video->type)
965 mutex_lock(&video->stream_lock);
967 if (video->streaming) {
968 mutex_unlock(&video->stream_lock);
972 /* Start streaming on the pipeline. No link touching an entity in the
973 * pipeline can be activated or deactivated once streaming is started.
975 pipe = video->video.entity.pipe
976 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
977 media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
979 /* Verify that the currently configured format matches the output of
980 * the connected subdev.
982 ret = isp_video_check_format(video, vfh);
986 video->bpl_padding = ret;
987 video->bpl_value = vfh->format.fmt.pix.bytesperline;
989 /* Find the ISP video node connected at the far end of the pipeline and
990 * update the pipeline.
992 far_end = isp_video_far_end(video);
994 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
995 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
996 pipe->input = far_end;
997 pipe->output = video;
999 if (far_end == NULL) {
1004 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1005 pipe->input = video;
1006 pipe->output = far_end;
1009 if (video->isp->pdata->set_constraints)
1010 video->isp->pdata->set_constraints(video->isp, true);
1011 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1013 /* Validate the pipeline and update its state. */
1014 ret = isp_video_validate_pipeline(pipe);
1018 spin_lock_irqsave(&pipe->lock, flags);
1019 pipe->state &= ~ISP_PIPELINE_STREAM;
1020 pipe->state |= state;
1021 spin_unlock_irqrestore(&pipe->lock, flags);
1023 /* Set the maximum time per frame as the value requested by userspace.
1024 * This is a soft limit that can be overridden if the hardware doesn't
1025 * support the request limit.
1027 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1028 pipe->max_timeperframe = vfh->timeperframe;
1030 video->queue = &vfh->queue;
1031 INIT_LIST_HEAD(&video->dmaqueue);
1032 atomic_set(&pipe->frame_number, -1);
1034 ret = omap3isp_video_queue_streamon(&vfh->queue);
1038 /* In sensor-to-memory mode, the stream can be started synchronously
1039 * to the stream on command. In memory-to-memory mode, it will be
1040 * started when buffers are queued on both the input and output.
1042 if (pipe->input == NULL) {
1043 ret = omap3isp_pipeline_set_stream(pipe,
1044 ISP_PIPELINE_STREAM_CONTINUOUS);
1047 spin_lock_irqsave(&video->queue->irqlock, flags);
1048 if (list_empty(&video->dmaqueue))
1049 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1050 spin_unlock_irqrestore(&video->queue->irqlock, flags);
1055 omap3isp_video_queue_streamoff(&vfh->queue);
1056 if (video->isp->pdata->set_constraints)
1057 video->isp->pdata->set_constraints(video->isp, false);
1058 media_entity_pipeline_stop(&video->video.entity);
1059 /* The DMA queue must be emptied here, otherwise CCDC interrupts
1060 * that will get triggered the next time the CCDC is powered up
1061 * will try to access buffers that might have been freed but
1062 * still present in the DMA queue. This can easily get triggered
1063 * if the above omap3isp_pipeline_set_stream() call fails on a
1064 * system with a free-running sensor.
1066 INIT_LIST_HEAD(&video->dmaqueue);
1067 video->queue = NULL;
1071 video->streaming = 1;
1073 mutex_unlock(&video->stream_lock);
1078 isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1080 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1081 struct isp_video *video = video_drvdata(file);
1082 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1083 enum isp_pipeline_state state;
1084 unsigned int streaming;
1085 unsigned long flags;
1087 if (type != video->type)
1090 mutex_lock(&video->stream_lock);
1092 /* Make sure we're not streaming yet. */
1093 mutex_lock(&vfh->queue.lock);
1094 streaming = vfh->queue.streaming;
1095 mutex_unlock(&vfh->queue.lock);
1100 /* Update the pipeline state. */
1101 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1102 state = ISP_PIPELINE_STREAM_OUTPUT
1103 | ISP_PIPELINE_QUEUE_OUTPUT;
1105 state = ISP_PIPELINE_STREAM_INPUT
1106 | ISP_PIPELINE_QUEUE_INPUT;
1108 spin_lock_irqsave(&pipe->lock, flags);
1109 pipe->state &= ~state;
1110 spin_unlock_irqrestore(&pipe->lock, flags);
1112 /* Stop the stream. */
1113 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1114 omap3isp_video_queue_streamoff(&vfh->queue);
1115 video->queue = NULL;
1116 video->streaming = 0;
1118 if (video->isp->pdata->set_constraints)
1119 video->isp->pdata->set_constraints(video->isp, false);
1120 media_entity_pipeline_stop(&video->video.entity);
1123 mutex_unlock(&video->stream_lock);
1128 isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1130 if (input->index > 0)
1133 strlcpy(input->name, "camera", sizeof(input->name));
1134 input->type = V4L2_INPUT_TYPE_CAMERA;
1140 isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1148 isp_video_s_input(struct file *file, void *fh, unsigned int input)
1150 return input == 0 ? 0 : -EINVAL;
1153 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1154 .vidioc_querycap = isp_video_querycap,
1155 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1156 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1157 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1158 .vidioc_g_fmt_vid_out = isp_video_get_format,
1159 .vidioc_s_fmt_vid_out = isp_video_set_format,
1160 .vidioc_try_fmt_vid_out = isp_video_try_format,
1161 .vidioc_cropcap = isp_video_cropcap,
1162 .vidioc_g_crop = isp_video_get_crop,
1163 .vidioc_s_crop = isp_video_set_crop,
1164 .vidioc_g_parm = isp_video_get_param,
1165 .vidioc_s_parm = isp_video_set_param,
1166 .vidioc_reqbufs = isp_video_reqbufs,
1167 .vidioc_querybuf = isp_video_querybuf,
1168 .vidioc_qbuf = isp_video_qbuf,
1169 .vidioc_dqbuf = isp_video_dqbuf,
1170 .vidioc_streamon = isp_video_streamon,
1171 .vidioc_streamoff = isp_video_streamoff,
1172 .vidioc_enum_input = isp_video_enum_input,
1173 .vidioc_g_input = isp_video_g_input,
1174 .vidioc_s_input = isp_video_s_input,
1177 /* -----------------------------------------------------------------------------
1178 * V4L2 file operations
1181 static int isp_video_open(struct file *file)
1183 struct isp_video *video = video_drvdata(file);
1184 struct isp_video_fh *handle;
1187 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1191 v4l2_fh_init(&handle->vfh, &video->video);
1192 v4l2_fh_add(&handle->vfh);
1194 /* If this is the first user, initialise the pipeline. */
1195 if (omap3isp_get(video->isp) == NULL) {
1200 ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1202 omap3isp_put(video->isp);
1206 omap3isp_video_queue_init(&handle->queue, video->type,
1207 &isp_video_queue_ops, video->isp->dev,
1208 sizeof(struct isp_buffer));
1210 memset(&handle->format, 0, sizeof(handle->format));
1211 handle->format.type = video->type;
1212 handle->timeperframe.denominator = 1;
1214 handle->video = video;
1215 file->private_data = &handle->vfh;
1219 v4l2_fh_del(&handle->vfh);
1226 static int isp_video_release(struct file *file)
1228 struct isp_video *video = video_drvdata(file);
1229 struct v4l2_fh *vfh = file->private_data;
1230 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1232 /* Disable streaming and free the buffers queue resources. */
1233 isp_video_streamoff(file, vfh, video->type);
1235 mutex_lock(&handle->queue.lock);
1236 omap3isp_video_queue_cleanup(&handle->queue);
1237 mutex_unlock(&handle->queue.lock);
1239 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1241 /* Release the file handle. */
1244 file->private_data = NULL;
1246 omap3isp_put(video->isp);
1251 static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1253 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1254 struct isp_video_queue *queue = &vfh->queue;
1256 return omap3isp_video_queue_poll(queue, file, wait);
1259 static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1261 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1263 return omap3isp_video_queue_mmap(&vfh->queue, vma);
1266 static struct v4l2_file_operations isp_video_fops = {
1267 .owner = THIS_MODULE,
1268 .unlocked_ioctl = video_ioctl2,
1269 .open = isp_video_open,
1270 .release = isp_video_release,
1271 .poll = isp_video_poll,
1272 .mmap = isp_video_mmap,
1275 /* -----------------------------------------------------------------------------
1279 static const struct isp_video_operations isp_video_dummy_ops = {
1282 int omap3isp_video_init(struct isp_video *video, const char *name)
1284 const char *direction;
1287 switch (video->type) {
1288 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1289 direction = "output";
1290 video->pad.flags = MEDIA_PAD_FL_SINK;
1292 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1293 direction = "input";
1294 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1301 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1305 mutex_init(&video->mutex);
1306 atomic_set(&video->active, 0);
1308 spin_lock_init(&video->pipe.lock);
1309 mutex_init(&video->stream_lock);
1311 /* Initialize the video device. */
1312 if (video->ops == NULL)
1313 video->ops = &isp_video_dummy_ops;
1315 video->video.fops = &isp_video_fops;
1316 snprintf(video->video.name, sizeof(video->video.name),
1317 "OMAP3 ISP %s %s", name, direction);
1318 video->video.vfl_type = VFL_TYPE_GRABBER;
1319 video->video.release = video_device_release_empty;
1320 video->video.ioctl_ops = &isp_video_ioctl_ops;
1321 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1323 video_set_drvdata(&video->video, video);
1328 int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1332 video->video.v4l2_dev = vdev;
1334 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1336 printk(KERN_ERR "%s: could not register video device (%d)\n",
1342 void omap3isp_video_unregister(struct isp_video *video)
1344 if (video_is_registered(&video->video)) {
1345 media_entity_cleanup(&video->video.entity);
1346 video_unregister_device(&video->video);