4 * TI OMAP3 ISP - Generic video node
6 * Copyright (C) 2009-2010 Nokia Corporation
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * Sakari Ailus <sakari.ailus@iki.fi>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 #include <asm/cacheflush.h>
27 #include <linux/clk.h>
29 #include <linux/module.h>
30 #include <linux/pagemap.h>
31 #include <linux/scatterlist.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/vmalloc.h>
35 #include <media/v4l2-dev.h>
36 #include <media/v4l2-ioctl.h>
37 #include <plat/iommu.h>
38 #include <plat/iovmm.h>
39 #include <plat/omap-pm.h>
45 /* -----------------------------------------------------------------------------
50 * NOTE: When adding new media bus codes, always remember to add
51 * corresponding in-memory formats to the table below!!!
53 static struct isp_format_info formats[] = {
54 { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
55 V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
56 V4L2_PIX_FMT_GREY, 8, },
57 { V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y10_1X10,
58 V4L2_MBUS_FMT_Y10_1X10, V4L2_MBUS_FMT_Y8_1X8,
59 V4L2_PIX_FMT_Y10, 10, },
60 { V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y10_1X10,
61 V4L2_MBUS_FMT_Y12_1X12, V4L2_MBUS_FMT_Y8_1X8,
62 V4L2_PIX_FMT_Y12, 12, },
63 { V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
64 V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_MBUS_FMT_SBGGR8_1X8,
65 V4L2_PIX_FMT_SBGGR8, 8, },
66 { V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
67 V4L2_MBUS_FMT_SGBRG8_1X8, V4L2_MBUS_FMT_SGBRG8_1X8,
68 V4L2_PIX_FMT_SGBRG8, 8, },
69 { V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
70 V4L2_MBUS_FMT_SGRBG8_1X8, V4L2_MBUS_FMT_SGRBG8_1X8,
71 V4L2_PIX_FMT_SGRBG8, 8, },
72 { V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
73 V4L2_MBUS_FMT_SRGGB8_1X8, V4L2_MBUS_FMT_SRGGB8_1X8,
74 V4L2_PIX_FMT_SRGGB8, 8, },
75 { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8,
76 V4L2_MBUS_FMT_SBGGR10_1X10, 0,
77 V4L2_PIX_FMT_SBGGR10DPCM8, 8, },
78 { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8,
79 V4L2_MBUS_FMT_SGBRG10_1X10, 0,
80 V4L2_PIX_FMT_SGBRG10DPCM8, 8, },
81 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
82 V4L2_MBUS_FMT_SGRBG10_1X10, 0,
83 V4L2_PIX_FMT_SGRBG10DPCM8, 8, },
84 { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8,
85 V4L2_MBUS_FMT_SRGGB10_1X10, 0,
86 V4L2_PIX_FMT_SRGGB10DPCM8, 8, },
87 { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
88 V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR8_1X8,
89 V4L2_PIX_FMT_SBGGR10, 10, },
90 { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
91 V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG8_1X8,
92 V4L2_PIX_FMT_SGBRG10, 10, },
93 { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
94 V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG8_1X8,
95 V4L2_PIX_FMT_SGRBG10, 10, },
96 { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
97 V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB8_1X8,
98 V4L2_PIX_FMT_SRGGB10, 10, },
99 { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
100 V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR8_1X8,
101 V4L2_PIX_FMT_SBGGR12, 12, },
102 { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
103 V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG8_1X8,
104 V4L2_PIX_FMT_SGBRG12, 12, },
105 { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
106 V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG8_1X8,
107 V4L2_PIX_FMT_SGRBG12, 12, },
108 { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
109 V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB8_1X8,
110 V4L2_PIX_FMT_SRGGB12, 12, },
111 { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
112 V4L2_MBUS_FMT_UYVY8_1X16, 0,
113 V4L2_PIX_FMT_UYVY, 16, },
114 { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
115 V4L2_MBUS_FMT_YUYV8_1X16, 0,
116 V4L2_PIX_FMT_YUYV, 16, },
119 const struct isp_format_info *
120 omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
124 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
125 if (formats[i].code == code)
133 * Decide whether desired output pixel code can be obtained with
134 * the lane shifter by shifting the input pixel code.
135 * @in: input pixelcode to shifter
136 * @out: output pixelcode from shifter
137 * @additional_shift: # of bits the sensor's LSB is offset from CAMEXT[0]
139 * return true if the combination is possible
140 * return false otherwise
142 static bool isp_video_is_shiftable(enum v4l2_mbus_pixelcode in,
143 enum v4l2_mbus_pixelcode out,
144 unsigned int additional_shift)
146 const struct isp_format_info *in_info, *out_info;
151 in_info = omap3isp_video_format_info(in);
152 out_info = omap3isp_video_format_info(out);
154 if ((in_info->flavor == 0) || (out_info->flavor == 0))
157 if (in_info->flavor != out_info->flavor)
160 return in_info->bpp - out_info->bpp + additional_shift <= 6;
164 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
165 * @video: ISP video instance
166 * @mbus: v4l2_mbus_framefmt format (input)
167 * @pix: v4l2_pix_format format (output)
169 * Fill the output pix structure with information from the input mbus format.
170 * The bytesperline and sizeimage fields are computed from the requested bytes
171 * per line value in the pix format and information from the video instance.
173 * Return the number of padding bytes at end of line.
175 static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
176 const struct v4l2_mbus_framefmt *mbus,
177 struct v4l2_pix_format *pix)
179 unsigned int bpl = pix->bytesperline;
180 unsigned int min_bpl;
183 memset(pix, 0, sizeof(*pix));
184 pix->width = mbus->width;
185 pix->height = mbus->height;
187 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
188 if (formats[i].code == mbus->code)
192 if (WARN_ON(i == ARRAY_SIZE(formats)))
195 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
197 /* Clamp the requested bytes per line value. If the maximum bytes per
198 * line value is zero, the module doesn't support user configurable line
199 * sizes. Override the requested value with the minimum in that case.
202 bpl = clamp(bpl, min_bpl, video->bpl_max);
206 if (!video->bpl_zero_padding || bpl != min_bpl)
207 bpl = ALIGN(bpl, video->bpl_alignment);
209 pix->pixelformat = formats[i].pixelformat;
210 pix->bytesperline = bpl;
211 pix->sizeimage = pix->bytesperline * pix->height;
212 pix->colorspace = mbus->colorspace;
213 pix->field = mbus->field;
215 return bpl - min_bpl;
218 static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
219 struct v4l2_mbus_framefmt *mbus)
223 memset(mbus, 0, sizeof(*mbus));
224 mbus->width = pix->width;
225 mbus->height = pix->height;
227 /* Skip the last format in the loop so that it will be selected if no
230 for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) {
231 if (formats[i].pixelformat == pix->pixelformat)
235 mbus->code = formats[i].code;
236 mbus->colorspace = pix->colorspace;
237 mbus->field = pix->field;
240 static struct v4l2_subdev *
241 isp_video_remote_subdev(struct isp_video *video, u32 *pad)
243 struct media_pad *remote;
245 remote = media_entity_remote_source(&video->pad);
247 if (remote == NULL ||
248 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
252 *pad = remote->index;
254 return media_entity_to_v4l2_subdev(remote->entity);
257 /* Return a pointer to the ISP video instance at the far end of the pipeline. */
258 static struct isp_video *
259 isp_video_far_end(struct isp_video *video)
261 struct media_entity_graph graph;
262 struct media_entity *entity = &video->video.entity;
263 struct media_device *mdev = entity->parent;
264 struct isp_video *far_end = NULL;
266 mutex_lock(&mdev->graph_mutex);
267 media_entity_graph_walk_start(&graph, entity);
269 while ((entity = media_entity_graph_walk_next(&graph))) {
270 if (entity == &video->video.entity)
273 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
276 far_end = to_isp_video(media_entity_to_video_device(entity));
277 if (far_end->type != video->type)
283 mutex_unlock(&mdev->graph_mutex);
288 * Validate a pipeline by checking both ends of all links for format
291 * Compute the minimum time per frame value as the maximum of time per frame
292 * limits reported by every block in the pipeline.
294 * Return 0 if all formats match, or -EPIPE if at least one link is found with
295 * different formats on its two ends or if the pipeline doesn't start with a
296 * video source (either a subdev with no input pad, or a non-subdev entity).
298 static int isp_video_validate_pipeline(struct isp_pipeline *pipe)
300 struct isp_device *isp = pipe->output->isp;
301 struct v4l2_subdev_format fmt_source;
302 struct v4l2_subdev_format fmt_sink;
303 struct media_pad *pad;
304 struct v4l2_subdev *subdev;
309 subdev = isp_video_remote_subdev(pipe->output, NULL);
314 unsigned int shifter_link;
316 pipe->entities |= 1U << subdev->entity.id;
318 /* Retrieve the sink format */
319 pad = &subdev->entity.pads[0];
320 if (!(pad->flags & MEDIA_PAD_FL_SINK))
323 fmt_sink.pad = pad->index;
324 fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
325 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_sink);
326 if (ret < 0 && ret != -ENOIOCTLCMD)
329 /* Update the maximum frame rate */
330 if (subdev == &isp->isp_res.subdev)
331 omap3isp_resizer_max_rate(&isp->isp_res,
334 /* Check ccdc maximum data rate when data comes from sensor
335 * TODO: Include ccdc rate in pipe->max_rate and compare the
336 * total pipe rate with the input data rate from sensor.
338 if (subdev == &isp->isp_ccdc.subdev && pipe->input == NULL) {
339 unsigned int rate = UINT_MAX;
341 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
342 if (isp->isp_ccdc.vpcfg.pixelclk > rate)
346 /* If sink pad is on CCDC, the link has the lane shifter
347 * in the middle of it. */
348 shifter_link = subdev == &isp->isp_ccdc.subdev;
350 /* Retrieve the source format. Return an error if no source
351 * entity can be found, and stop checking the pipeline if the
352 * source entity isn't a subdev.
354 pad = media_entity_remote_source(pad);
358 if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
361 subdev = media_entity_to_v4l2_subdev(pad->entity);
363 fmt_source.pad = pad->index;
364 fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
365 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
366 if (ret < 0 && ret != -ENOIOCTLCMD)
369 /* Check if the two ends match */
370 if (fmt_source.format.width != fmt_sink.format.width ||
371 fmt_source.format.height != fmt_sink.format.height)
375 unsigned int parallel_shift = 0;
376 if (isp->isp_ccdc.input == CCDC_INPUT_PARALLEL) {
377 struct isp_parallel_platform_data *pdata =
378 &((struct isp_v4l2_subdevs_group *)
379 subdev->host_priv)->bus.parallel;
380 parallel_shift = pdata->data_lane_shift * 2;
382 if (!isp_video_is_shiftable(fmt_source.format.code,
383 fmt_sink.format.code,
386 } else if (fmt_source.format.code != fmt_sink.format.code)
394 __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
396 struct v4l2_subdev_format fmt;
397 struct v4l2_subdev *subdev;
401 subdev = isp_video_remote_subdev(video, &pad);
405 mutex_lock(&video->mutex);
408 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
409 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
410 if (ret == -ENOIOCTLCMD)
413 mutex_unlock(&video->mutex);
418 format->type = video->type;
419 return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
423 isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
425 struct v4l2_format format;
428 memcpy(&format, &vfh->format, sizeof(format));
429 ret = __isp_video_get_format(video, &format);
433 if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
434 vfh->format.fmt.pix.height != format.fmt.pix.height ||
435 vfh->format.fmt.pix.width != format.fmt.pix.width ||
436 vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
437 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
443 /* -----------------------------------------------------------------------------
447 #define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
450 * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
451 * @dev: Device pointer specific to the OMAP3 ISP.
452 * @sglist: Pointer to source Scatter gather list to allocate.
453 * @sglen: Number of elements of the scatter-gatter list.
455 * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
456 * we ran out of memory.
459 ispmmu_vmap(struct isp_device *isp, const struct scatterlist *sglist, int sglen)
461 struct sg_table *sgt;
464 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
468 sgt->sgl = (struct scatterlist *)sglist;
470 sgt->orig_nents = sglen;
472 da = omap_iommu_vmap(isp->domain, isp->dev, 0, sgt, IOMMU_FLAG);
473 if (IS_ERR_VALUE(da))
480 * ispmmu_vunmap - Unmap a device address from the ISP MMU
481 * @dev: Device pointer specific to the OMAP3 ISP.
482 * @da: Device address generated from a ispmmu_vmap call.
484 static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
486 struct sg_table *sgt;
488 sgt = omap_iommu_vunmap(isp->domain, isp->dev, (u32)da);
492 /* -----------------------------------------------------------------------------
493 * Video queue operations
496 static void isp_video_queue_prepare(struct isp_video_queue *queue,
497 unsigned int *nbuffers, unsigned int *size)
499 struct isp_video_fh *vfh =
500 container_of(queue, struct isp_video_fh, queue);
501 struct isp_video *video = vfh->video;
503 *size = vfh->format.fmt.pix.sizeimage;
507 *nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size));
510 static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
512 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
513 struct isp_buffer *buffer = to_isp_buffer(buf);
514 struct isp_video *video = vfh->video;
516 if (buffer->isp_addr) {
517 ispmmu_vunmap(video->isp, buffer->isp_addr);
518 buffer->isp_addr = 0;
522 static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
524 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
525 struct isp_buffer *buffer = to_isp_buffer(buf);
526 struct isp_video *video = vfh->video;
529 addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen);
530 if (IS_ERR_VALUE(addr))
533 if (!IS_ALIGNED(addr, 32)) {
534 dev_dbg(video->isp->dev, "Buffer address must be "
535 "aligned to 32 bytes boundary.\n");
536 ispmmu_vunmap(video->isp, buffer->isp_addr);
540 buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
541 buffer->isp_addr = addr;
546 * isp_video_buffer_queue - Add buffer to streaming queue
549 * In memory-to-memory mode, start streaming on the pipeline if buffers are
550 * queued on both the input and the output, if the pipeline isn't already busy.
551 * If the pipeline is busy, it will be restarted in the output module interrupt
554 static void isp_video_buffer_queue(struct isp_video_buffer *buf)
556 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
557 struct isp_buffer *buffer = to_isp_buffer(buf);
558 struct isp_video *video = vfh->video;
559 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
560 enum isp_pipeline_state state;
565 empty = list_empty(&video->dmaqueue);
566 list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
569 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
570 state = ISP_PIPELINE_QUEUE_OUTPUT;
572 state = ISP_PIPELINE_QUEUE_INPUT;
574 spin_lock_irqsave(&pipe->lock, flags);
575 pipe->state |= state;
576 video->ops->queue(video, buffer);
577 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
579 start = isp_pipeline_ready(pipe);
581 pipe->state |= ISP_PIPELINE_STREAM;
582 spin_unlock_irqrestore(&pipe->lock, flags);
585 omap3isp_pipeline_set_stream(pipe,
586 ISP_PIPELINE_STREAM_SINGLESHOT);
590 static const struct isp_video_queue_operations isp_video_queue_ops = {
591 .queue_prepare = &isp_video_queue_prepare,
592 .buffer_prepare = &isp_video_buffer_prepare,
593 .buffer_queue = &isp_video_buffer_queue,
594 .buffer_cleanup = &isp_video_buffer_cleanup,
598 * omap3isp_video_buffer_next - Complete the current buffer and return the next
599 * @video: ISP video object
601 * Remove the current video buffer from the DMA queue and fill its timestamp,
602 * field count and state fields before waking up its completion handler.
604 * For capture video nodes the buffer state is set to ISP_BUF_STATE_DONE if no
605 * error has been flagged in the pipeline, or to ISP_BUF_STATE_ERROR otherwise.
606 * For video output nodes the buffer state is always set to ISP_BUF_STATE_DONE.
608 * The DMA queue is expected to contain at least one buffer.
610 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
613 struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video)
615 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
616 struct isp_video_queue *queue = video->queue;
617 enum isp_pipeline_state state;
618 struct isp_video_buffer *buf;
622 spin_lock_irqsave(&queue->irqlock, flags);
623 if (WARN_ON(list_empty(&video->dmaqueue))) {
624 spin_unlock_irqrestore(&queue->irqlock, flags);
628 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
630 list_del(&buf->irqlist);
631 spin_unlock_irqrestore(&queue->irqlock, flags);
634 buf->vbuf.timestamp.tv_sec = ts.tv_sec;
635 buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
637 /* Do frame number propagation only if this is the output video node.
638 * Frame number either comes from the CSI receivers or it gets
639 * incremented here if H3A is not active.
640 * Note: There is no guarantee that the output buffer will finish
641 * first, so the input number might lag behind by 1 in some cases.
643 if (video == pipe->output && !pipe->do_propagation)
644 buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
646 buf->vbuf.sequence = atomic_read(&pipe->frame_number);
648 /* Report pipeline errors to userspace on the capture device side. */
649 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) {
650 buf->state = ISP_BUF_STATE_ERROR;
653 buf->state = ISP_BUF_STATE_DONE;
658 if (list_empty(&video->dmaqueue)) {
659 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
660 state = ISP_PIPELINE_QUEUE_OUTPUT
661 | ISP_PIPELINE_STREAM;
663 state = ISP_PIPELINE_QUEUE_INPUT
664 | ISP_PIPELINE_STREAM;
666 spin_lock_irqsave(&pipe->lock, flags);
667 pipe->state &= ~state;
668 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
669 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
670 spin_unlock_irqrestore(&pipe->lock, flags);
674 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
675 spin_lock_irqsave(&pipe->lock, flags);
676 pipe->state &= ~ISP_PIPELINE_STREAM;
677 spin_unlock_irqrestore(&pipe->lock, flags);
680 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
682 buf->state = ISP_BUF_STATE_ACTIVE;
683 return to_isp_buffer(buf);
687 * omap3isp_video_resume - Perform resume operation on the buffers
688 * @video: ISP video object
689 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
691 * This function is intended to be used on suspend/resume scenario. It
692 * requests video queue layer to discard buffers marked as DONE if it's in
693 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
696 void omap3isp_video_resume(struct isp_video *video, int continuous)
698 struct isp_buffer *buf = NULL;
700 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
701 omap3isp_video_queue_discard_done(video->queue);
703 if (!list_empty(&video->dmaqueue)) {
704 buf = list_first_entry(&video->dmaqueue,
705 struct isp_buffer, buffer.irqlist);
706 video->ops->queue(video, buf);
707 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
710 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
714 /* -----------------------------------------------------------------------------
719 isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
721 struct isp_video *video = video_drvdata(file);
723 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
724 strlcpy(cap->card, video->video.name, sizeof(cap->card));
725 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
727 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
728 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
730 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
736 isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
738 struct isp_video_fh *vfh = to_isp_video_fh(fh);
739 struct isp_video *video = video_drvdata(file);
741 if (format->type != video->type)
744 mutex_lock(&video->mutex);
745 *format = vfh->format;
746 mutex_unlock(&video->mutex);
752 isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
754 struct isp_video_fh *vfh = to_isp_video_fh(fh);
755 struct isp_video *video = video_drvdata(file);
756 struct v4l2_mbus_framefmt fmt;
758 if (format->type != video->type)
761 mutex_lock(&video->mutex);
763 /* Fill the bytesperline and sizeimage fields by converting to media bus
764 * format and back to pixel format.
766 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
767 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
769 vfh->format = *format;
771 mutex_unlock(&video->mutex);
776 isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
778 struct isp_video *video = video_drvdata(file);
779 struct v4l2_subdev_format fmt;
780 struct v4l2_subdev *subdev;
784 if (format->type != video->type)
787 subdev = isp_video_remote_subdev(video, &pad);
791 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
794 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
795 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
797 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
799 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
804 isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
806 struct isp_video *video = video_drvdata(file);
807 struct v4l2_subdev *subdev;
810 subdev = isp_video_remote_subdev(video, NULL);
814 mutex_lock(&video->mutex);
815 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
816 mutex_unlock(&video->mutex);
818 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
822 isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
824 struct isp_video *video = video_drvdata(file);
825 struct v4l2_subdev_format format;
826 struct v4l2_subdev *subdev;
830 subdev = isp_video_remote_subdev(video, &pad);
834 /* Try the get crop operation first and fallback to get format if not
837 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
838 if (ret != -ENOIOCTLCMD)
842 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
843 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
845 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
849 crop->c.width = format.format.width;
850 crop->c.height = format.format.height;
856 isp_video_set_crop(struct file *file, void *fh, struct v4l2_crop *crop)
858 struct isp_video *video = video_drvdata(file);
859 struct v4l2_subdev *subdev;
862 subdev = isp_video_remote_subdev(video, NULL);
866 mutex_lock(&video->mutex);
867 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
868 mutex_unlock(&video->mutex);
870 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
874 isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
876 struct isp_video_fh *vfh = to_isp_video_fh(fh);
877 struct isp_video *video = video_drvdata(file);
879 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
880 video->type != a->type)
883 memset(a, 0, sizeof(*a));
884 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
885 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
886 a->parm.output.timeperframe = vfh->timeperframe;
892 isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
894 struct isp_video_fh *vfh = to_isp_video_fh(fh);
895 struct isp_video *video = video_drvdata(file);
897 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
898 video->type != a->type)
901 if (a->parm.output.timeperframe.denominator == 0)
902 a->parm.output.timeperframe.denominator = 1;
904 vfh->timeperframe = a->parm.output.timeperframe;
910 isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
912 struct isp_video_fh *vfh = to_isp_video_fh(fh);
914 return omap3isp_video_queue_reqbufs(&vfh->queue, rb);
918 isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
920 struct isp_video_fh *vfh = to_isp_video_fh(fh);
922 return omap3isp_video_queue_querybuf(&vfh->queue, b);
926 isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
928 struct isp_video_fh *vfh = to_isp_video_fh(fh);
930 return omap3isp_video_queue_qbuf(&vfh->queue, b);
934 isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
936 struct isp_video_fh *vfh = to_isp_video_fh(fh);
938 return omap3isp_video_queue_dqbuf(&vfh->queue, b,
939 file->f_flags & O_NONBLOCK);
945 * Every ISP pipeline has a single input and a single output. The input can be
946 * either a sensor or a video node. The output is always a video node.
948 * As every pipeline has an output video node, the ISP video objects at the
949 * pipeline output stores the pipeline state. It tracks the streaming state of
950 * both the input and output, as well as the availability of buffers.
952 * In sensor-to-memory mode, frames are always available at the pipeline input.
953 * Starting the sensor usually requires I2C transfers and must be done in
954 * interruptible context. The pipeline is started and stopped synchronously
955 * to the stream on/off commands. All modules in the pipeline will get their
956 * subdev set stream handler called. The module at the end of the pipeline must
957 * delay starting the hardware until buffers are available at its output.
959 * In memory-to-memory mode, starting/stopping the stream requires
960 * synchronization between the input and output. ISP modules can't be stopped
961 * in the middle of a frame, and at least some of the modules seem to become
962 * busy as soon as they're started, even if they don't receive a frame start
963 * event. For that reason frames need to be processed in single-shot mode. The
964 * driver needs to wait until a frame is completely processed and written to
965 * memory before restarting the pipeline for the next frame. Pipelined
966 * processing might be possible but requires more testing.
968 * Stream start must be delayed until buffers are available at both the input
969 * and output. The pipeline must be started in the videobuf queue callback with
970 * the buffers queue spinlock held. The modules subdev set stream operation must
974 isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
976 struct isp_video_fh *vfh = to_isp_video_fh(fh);
977 struct isp_video *video = video_drvdata(file);
978 enum isp_pipeline_state state;
979 struct isp_pipeline *pipe;
980 struct isp_video *far_end;
984 if (type != video->type)
987 mutex_lock(&video->stream_lock);
989 if (video->streaming) {
990 mutex_unlock(&video->stream_lock);
994 /* Start streaming on the pipeline. No link touching an entity in the
995 * pipeline can be activated or deactivated once streaming is started.
997 pipe = video->video.entity.pipe
998 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
1000 if (video->isp->pdata->set_constraints)
1001 video->isp->pdata->set_constraints(video->isp, true);
1002 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
1003 pipe->max_rate = pipe->l3_ick;
1005 media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
1007 /* Verify that the currently configured format matches the output of
1008 * the connected subdev.
1010 ret = isp_video_check_format(video, vfh);
1014 video->bpl_padding = ret;
1015 video->bpl_value = vfh->format.fmt.pix.bytesperline;
1017 /* Find the ISP video node connected at the far end of the pipeline and
1018 * update the pipeline.
1020 far_end = isp_video_far_end(video);
1022 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1023 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
1024 pipe->input = far_end;
1025 pipe->output = video;
1027 if (far_end == NULL) {
1032 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
1033 pipe->input = video;
1034 pipe->output = far_end;
1037 /* Validate the pipeline and update its state. */
1038 ret = isp_video_validate_pipeline(pipe);
1042 pipe->error = false;
1044 spin_lock_irqsave(&pipe->lock, flags);
1045 pipe->state &= ~ISP_PIPELINE_STREAM;
1046 pipe->state |= state;
1047 spin_unlock_irqrestore(&pipe->lock, flags);
1049 /* Set the maximum time per frame as the value requested by userspace.
1050 * This is a soft limit that can be overridden if the hardware doesn't
1051 * support the request limit.
1053 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1054 pipe->max_timeperframe = vfh->timeperframe;
1056 video->queue = &vfh->queue;
1057 INIT_LIST_HEAD(&video->dmaqueue);
1058 atomic_set(&pipe->frame_number, -1);
1060 ret = omap3isp_video_queue_streamon(&vfh->queue);
1064 /* In sensor-to-memory mode, the stream can be started synchronously
1065 * to the stream on command. In memory-to-memory mode, it will be
1066 * started when buffers are queued on both the input and output.
1068 if (pipe->input == NULL) {
1069 ret = omap3isp_pipeline_set_stream(pipe,
1070 ISP_PIPELINE_STREAM_CONTINUOUS);
1073 spin_lock_irqsave(&video->queue->irqlock, flags);
1074 if (list_empty(&video->dmaqueue))
1075 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
1076 spin_unlock_irqrestore(&video->queue->irqlock, flags);
1081 omap3isp_video_queue_streamoff(&vfh->queue);
1082 media_entity_pipeline_stop(&video->video.entity);
1083 if (video->isp->pdata->set_constraints)
1084 video->isp->pdata->set_constraints(video->isp, false);
1085 /* The DMA queue must be emptied here, otherwise CCDC interrupts
1086 * that will get triggered the next time the CCDC is powered up
1087 * will try to access buffers that might have been freed but
1088 * still present in the DMA queue. This can easily get triggered
1089 * if the above omap3isp_pipeline_set_stream() call fails on a
1090 * system with a free-running sensor.
1092 INIT_LIST_HEAD(&video->dmaqueue);
1093 video->queue = NULL;
1097 video->streaming = 1;
1099 mutex_unlock(&video->stream_lock);
1104 isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1106 struct isp_video_fh *vfh = to_isp_video_fh(fh);
1107 struct isp_video *video = video_drvdata(file);
1108 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1109 enum isp_pipeline_state state;
1110 unsigned int streaming;
1111 unsigned long flags;
1113 if (type != video->type)
1116 mutex_lock(&video->stream_lock);
1118 /* Make sure we're not streaming yet. */
1119 mutex_lock(&vfh->queue.lock);
1120 streaming = vfh->queue.streaming;
1121 mutex_unlock(&vfh->queue.lock);
1126 /* Update the pipeline state. */
1127 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1128 state = ISP_PIPELINE_STREAM_OUTPUT
1129 | ISP_PIPELINE_QUEUE_OUTPUT;
1131 state = ISP_PIPELINE_STREAM_INPUT
1132 | ISP_PIPELINE_QUEUE_INPUT;
1134 spin_lock_irqsave(&pipe->lock, flags);
1135 pipe->state &= ~state;
1136 spin_unlock_irqrestore(&pipe->lock, flags);
1138 /* Stop the stream. */
1139 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1140 omap3isp_video_queue_streamoff(&vfh->queue);
1141 video->queue = NULL;
1142 video->streaming = 0;
1144 if (video->isp->pdata->set_constraints)
1145 video->isp->pdata->set_constraints(video->isp, false);
1146 media_entity_pipeline_stop(&video->video.entity);
1149 mutex_unlock(&video->stream_lock);
1154 isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1156 if (input->index > 0)
1159 strlcpy(input->name, "camera", sizeof(input->name));
1160 input->type = V4L2_INPUT_TYPE_CAMERA;
1166 isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1174 isp_video_s_input(struct file *file, void *fh, unsigned int input)
1176 return input == 0 ? 0 : -EINVAL;
1179 static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1180 .vidioc_querycap = isp_video_querycap,
1181 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1182 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1183 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1184 .vidioc_g_fmt_vid_out = isp_video_get_format,
1185 .vidioc_s_fmt_vid_out = isp_video_set_format,
1186 .vidioc_try_fmt_vid_out = isp_video_try_format,
1187 .vidioc_cropcap = isp_video_cropcap,
1188 .vidioc_g_crop = isp_video_get_crop,
1189 .vidioc_s_crop = isp_video_set_crop,
1190 .vidioc_g_parm = isp_video_get_param,
1191 .vidioc_s_parm = isp_video_set_param,
1192 .vidioc_reqbufs = isp_video_reqbufs,
1193 .vidioc_querybuf = isp_video_querybuf,
1194 .vidioc_qbuf = isp_video_qbuf,
1195 .vidioc_dqbuf = isp_video_dqbuf,
1196 .vidioc_streamon = isp_video_streamon,
1197 .vidioc_streamoff = isp_video_streamoff,
1198 .vidioc_enum_input = isp_video_enum_input,
1199 .vidioc_g_input = isp_video_g_input,
1200 .vidioc_s_input = isp_video_s_input,
1203 /* -----------------------------------------------------------------------------
1204 * V4L2 file operations
1207 static int isp_video_open(struct file *file)
1209 struct isp_video *video = video_drvdata(file);
1210 struct isp_video_fh *handle;
1213 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1217 v4l2_fh_init(&handle->vfh, &video->video);
1218 v4l2_fh_add(&handle->vfh);
1220 /* If this is the first user, initialise the pipeline. */
1221 if (omap3isp_get(video->isp) == NULL) {
1226 ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1228 omap3isp_put(video->isp);
1232 omap3isp_video_queue_init(&handle->queue, video->type,
1233 &isp_video_queue_ops, video->isp->dev,
1234 sizeof(struct isp_buffer));
1236 memset(&handle->format, 0, sizeof(handle->format));
1237 handle->format.type = video->type;
1238 handle->timeperframe.denominator = 1;
1240 handle->video = video;
1241 file->private_data = &handle->vfh;
1245 v4l2_fh_del(&handle->vfh);
1252 static int isp_video_release(struct file *file)
1254 struct isp_video *video = video_drvdata(file);
1255 struct v4l2_fh *vfh = file->private_data;
1256 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1258 /* Disable streaming and free the buffers queue resources. */
1259 isp_video_streamoff(file, vfh, video->type);
1261 mutex_lock(&handle->queue.lock);
1262 omap3isp_video_queue_cleanup(&handle->queue);
1263 mutex_unlock(&handle->queue.lock);
1265 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1267 /* Release the file handle. */
1270 file->private_data = NULL;
1272 omap3isp_put(video->isp);
1277 static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1279 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1280 struct isp_video_queue *queue = &vfh->queue;
1282 return omap3isp_video_queue_poll(queue, file, wait);
1285 static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1287 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1289 return omap3isp_video_queue_mmap(&vfh->queue, vma);
1292 static struct v4l2_file_operations isp_video_fops = {
1293 .owner = THIS_MODULE,
1294 .unlocked_ioctl = video_ioctl2,
1295 .open = isp_video_open,
1296 .release = isp_video_release,
1297 .poll = isp_video_poll,
1298 .mmap = isp_video_mmap,
1301 /* -----------------------------------------------------------------------------
1305 static const struct isp_video_operations isp_video_dummy_ops = {
1308 int omap3isp_video_init(struct isp_video *video, const char *name)
1310 const char *direction;
1313 switch (video->type) {
1314 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1315 direction = "output";
1316 video->pad.flags = MEDIA_PAD_FL_SINK;
1318 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1319 direction = "input";
1320 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1327 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1331 mutex_init(&video->mutex);
1332 atomic_set(&video->active, 0);
1334 spin_lock_init(&video->pipe.lock);
1335 mutex_init(&video->stream_lock);
1337 /* Initialize the video device. */
1338 if (video->ops == NULL)
1339 video->ops = &isp_video_dummy_ops;
1341 video->video.fops = &isp_video_fops;
1342 snprintf(video->video.name, sizeof(video->video.name),
1343 "OMAP3 ISP %s %s", name, direction);
1344 video->video.vfl_type = VFL_TYPE_GRABBER;
1345 video->video.release = video_device_release_empty;
1346 video->video.ioctl_ops = &isp_video_ioctl_ops;
1347 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1349 video_set_drvdata(&video->video, video);
1354 void omap3isp_video_cleanup(struct isp_video *video)
1356 media_entity_cleanup(&video->video.entity);
1357 mutex_destroy(&video->stream_lock);
1358 mutex_destroy(&video->mutex);
1361 int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1365 video->video.v4l2_dev = vdev;
1367 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1369 printk(KERN_ERR "%s: could not register video device (%d)\n",
1375 void omap3isp_video_unregister(struct isp_video *video)
1377 if (video_is_registered(&video->video))
1378 video_unregister_device(&video->video);