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_PIX_FMT_GREY
, 8, },
51 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8
, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8
,
52 V4L2_MBUS_FMT_SGRBG10_1X10
, V4L2_PIX_FMT_SGRBG10DPCM8
, 8, },
53 { V4L2_MBUS_FMT_SBGGR10_1X10
, V4L2_MBUS_FMT_SBGGR10_1X10
,
54 V4L2_MBUS_FMT_SBGGR10_1X10
, V4L2_PIX_FMT_SBGGR10
, 10, },
55 { V4L2_MBUS_FMT_SGBRG10_1X10
, V4L2_MBUS_FMT_SGBRG10_1X10
,
56 V4L2_MBUS_FMT_SGBRG10_1X10
, V4L2_PIX_FMT_SGBRG10
, 10, },
57 { V4L2_MBUS_FMT_SGRBG10_1X10
, V4L2_MBUS_FMT_SGRBG10_1X10
,
58 V4L2_MBUS_FMT_SGRBG10_1X10
, V4L2_PIX_FMT_SGRBG10
, 10, },
59 { V4L2_MBUS_FMT_SRGGB10_1X10
, V4L2_MBUS_FMT_SRGGB10_1X10
,
60 V4L2_MBUS_FMT_SRGGB10_1X10
, V4L2_PIX_FMT_SRGGB10
, 10, },
61 { V4L2_MBUS_FMT_SBGGR12_1X12
, V4L2_MBUS_FMT_SBGGR10_1X10
,
62 V4L2_MBUS_FMT_SBGGR12_1X12
, V4L2_PIX_FMT_SBGGR12
, 12, },
63 { V4L2_MBUS_FMT_SGBRG12_1X12
, V4L2_MBUS_FMT_SGBRG10_1X10
,
64 V4L2_MBUS_FMT_SGBRG12_1X12
, V4L2_PIX_FMT_SGBRG12
, 12, },
65 { V4L2_MBUS_FMT_SGRBG12_1X12
, V4L2_MBUS_FMT_SGRBG10_1X10
,
66 V4L2_MBUS_FMT_SGRBG12_1X12
, V4L2_PIX_FMT_SGRBG12
, 12, },
67 { V4L2_MBUS_FMT_SRGGB12_1X12
, V4L2_MBUS_FMT_SRGGB10_1X10
,
68 V4L2_MBUS_FMT_SRGGB12_1X12
, V4L2_PIX_FMT_SRGGB12
, 12, },
69 { V4L2_MBUS_FMT_UYVY8_1X16
, V4L2_MBUS_FMT_UYVY8_1X16
,
70 V4L2_MBUS_FMT_UYVY8_1X16
, V4L2_PIX_FMT_UYVY
, 16, },
71 { V4L2_MBUS_FMT_YUYV8_1X16
, V4L2_MBUS_FMT_YUYV8_1X16
,
72 V4L2_MBUS_FMT_YUYV8_1X16
, V4L2_PIX_FMT_YUYV
, 16, },
75 const struct isp_format_info
*
76 omap3isp_video_format_info(enum v4l2_mbus_pixelcode code
)
80 for (i
= 0; i
< ARRAY_SIZE(formats
); ++i
) {
81 if (formats
[i
].code
== code
)
89 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
90 * @video: ISP video instance
91 * @mbus: v4l2_mbus_framefmt format (input)
92 * @pix: v4l2_pix_format format (output)
94 * Fill the output pix structure with information from the input mbus format.
95 * The bytesperline and sizeimage fields are computed from the requested bytes
96 * per line value in the pix format and information from the video instance.
98 * Return the number of padding bytes at end of line.
100 static unsigned int isp_video_mbus_to_pix(const struct isp_video
*video
,
101 const struct v4l2_mbus_framefmt
*mbus
,
102 struct v4l2_pix_format
*pix
)
104 unsigned int bpl
= pix
->bytesperline
;
105 unsigned int min_bpl
;
108 memset(pix
, 0, sizeof(*pix
));
109 pix
->width
= mbus
->width
;
110 pix
->height
= mbus
->height
;
112 for (i
= 0; i
< ARRAY_SIZE(formats
); ++i
) {
113 if (formats
[i
].code
== mbus
->code
)
117 if (WARN_ON(i
== ARRAY_SIZE(formats
)))
120 min_bpl
= pix
->width
* ALIGN(formats
[i
].bpp
, 8) / 8;
122 /* Clamp the requested bytes per line value. If the maximum bytes per
123 * line value is zero, the module doesn't support user configurable line
124 * sizes. Override the requested value with the minimum in that case.
127 bpl
= clamp(bpl
, min_bpl
, video
->bpl_max
);
131 if (!video
->bpl_zero_padding
|| bpl
!= min_bpl
)
132 bpl
= ALIGN(bpl
, video
->bpl_alignment
);
134 pix
->pixelformat
= formats
[i
].pixelformat
;
135 pix
->bytesperline
= bpl
;
136 pix
->sizeimage
= pix
->bytesperline
* pix
->height
;
137 pix
->colorspace
= mbus
->colorspace
;
138 pix
->field
= mbus
->field
;
140 return bpl
- min_bpl
;
143 static void isp_video_pix_to_mbus(const struct v4l2_pix_format
*pix
,
144 struct v4l2_mbus_framefmt
*mbus
)
148 memset(mbus
, 0, sizeof(*mbus
));
149 mbus
->width
= pix
->width
;
150 mbus
->height
= pix
->height
;
152 for (i
= 0; i
< ARRAY_SIZE(formats
); ++i
) {
153 if (formats
[i
].pixelformat
== pix
->pixelformat
)
157 if (WARN_ON(i
== ARRAY_SIZE(formats
)))
160 mbus
->code
= formats
[i
].code
;
161 mbus
->colorspace
= pix
->colorspace
;
162 mbus
->field
= pix
->field
;
165 static struct v4l2_subdev
*
166 isp_video_remote_subdev(struct isp_video
*video
, u32
*pad
)
168 struct media_pad
*remote
;
170 remote
= media_entity_remote_source(&video
->pad
);
172 if (remote
== NULL
||
173 media_entity_type(remote
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
177 *pad
= remote
->index
;
179 return media_entity_to_v4l2_subdev(remote
->entity
);
182 /* Return a pointer to the ISP video instance at the far end of the pipeline. */
183 static struct isp_video
*
184 isp_video_far_end(struct isp_video
*video
)
186 struct media_entity_graph graph
;
187 struct media_entity
*entity
= &video
->video
.entity
;
188 struct media_device
*mdev
= entity
->parent
;
189 struct isp_video
*far_end
= NULL
;
191 mutex_lock(&mdev
->graph_mutex
);
192 media_entity_graph_walk_start(&graph
, entity
);
194 while ((entity
= media_entity_graph_walk_next(&graph
))) {
195 if (entity
== &video
->video
.entity
)
198 if (media_entity_type(entity
) != MEDIA_ENT_T_DEVNODE
)
201 far_end
= to_isp_video(media_entity_to_video_device(entity
));
202 if (far_end
->type
!= video
->type
)
208 mutex_unlock(&mdev
->graph_mutex
);
213 * Validate a pipeline by checking both ends of all links for format
216 * Compute the minimum time per frame value as the maximum of time per frame
217 * limits reported by every block in the pipeline.
219 * Return 0 if all formats match, or -EPIPE if at least one link is found with
220 * different formats on its two ends.
222 static int isp_video_validate_pipeline(struct isp_pipeline
*pipe
)
224 struct isp_device
*isp
= pipe
->output
->isp
;
225 struct v4l2_subdev_format fmt_source
;
226 struct v4l2_subdev_format fmt_sink
;
227 struct media_pad
*pad
;
228 struct v4l2_subdev
*subdev
;
231 pipe
->max_rate
= pipe
->l3_ick
;
233 subdev
= isp_video_remote_subdev(pipe
->output
, NULL
);
238 /* Retrieve the sink format */
239 pad
= &subdev
->entity
.pads
[0];
240 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
243 fmt_sink
.pad
= pad
->index
;
244 fmt_sink
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
245 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &fmt_sink
);
246 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
249 /* Update the maximum frame rate */
250 if (subdev
== &isp
->isp_res
.subdev
)
251 omap3isp_resizer_max_rate(&isp
->isp_res
,
254 /* Check ccdc maximum data rate when data comes from sensor
255 * TODO: Include ccdc rate in pipe->max_rate and compare the
256 * total pipe rate with the input data rate from sensor.
258 if (subdev
== &isp
->isp_ccdc
.subdev
&& pipe
->input
== NULL
) {
259 unsigned int rate
= UINT_MAX
;
261 omap3isp_ccdc_max_rate(&isp
->isp_ccdc
, &rate
);
262 if (isp
->isp_ccdc
.vpcfg
.pixelclk
> rate
)
266 /* Retrieve the source format */
267 pad
= media_entity_remote_source(pad
);
269 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
272 subdev
= media_entity_to_v4l2_subdev(pad
->entity
);
274 fmt_source
.pad
= pad
->index
;
275 fmt_source
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
276 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &fmt_source
);
277 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
280 /* Check if the two ends match */
281 if (fmt_source
.format
.code
!= fmt_sink
.format
.code
||
282 fmt_source
.format
.width
!= fmt_sink
.format
.width
||
283 fmt_source
.format
.height
!= fmt_sink
.format
.height
)
291 __isp_video_get_format(struct isp_video
*video
, struct v4l2_format
*format
)
293 struct v4l2_subdev_format fmt
;
294 struct v4l2_subdev
*subdev
;
298 subdev
= isp_video_remote_subdev(video
, &pad
);
302 mutex_lock(&video
->mutex
);
305 fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
306 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &fmt
);
307 if (ret
== -ENOIOCTLCMD
)
310 mutex_unlock(&video
->mutex
);
315 format
->type
= video
->type
;
316 return isp_video_mbus_to_pix(video
, &fmt
.format
, &format
->fmt
.pix
);
320 isp_video_check_format(struct isp_video
*video
, struct isp_video_fh
*vfh
)
322 struct v4l2_format format
;
325 memcpy(&format
, &vfh
->format
, sizeof(format
));
326 ret
= __isp_video_get_format(video
, &format
);
330 if (vfh
->format
.fmt
.pix
.pixelformat
!= format
.fmt
.pix
.pixelformat
||
331 vfh
->format
.fmt
.pix
.height
!= format
.fmt
.pix
.height
||
332 vfh
->format
.fmt
.pix
.width
!= format
.fmt
.pix
.width
||
333 vfh
->format
.fmt
.pix
.bytesperline
!= format
.fmt
.pix
.bytesperline
||
334 vfh
->format
.fmt
.pix
.sizeimage
!= format
.fmt
.pix
.sizeimage
)
340 /* -----------------------------------------------------------------------------
344 #define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
347 * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
348 * @dev: Device pointer specific to the OMAP3 ISP.
349 * @sglist: Pointer to source Scatter gather list to allocate.
350 * @sglen: Number of elements of the scatter-gatter list.
352 * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
353 * we ran out of memory.
356 ispmmu_vmap(struct isp_device
*isp
, const struct scatterlist
*sglist
, int sglen
)
358 struct sg_table
*sgt
;
361 sgt
= kmalloc(sizeof(*sgt
), GFP_KERNEL
);
365 sgt
->sgl
= (struct scatterlist
*)sglist
;
367 sgt
->orig_nents
= sglen
;
369 da
= iommu_vmap(isp
->iommu
, 0, sgt
, IOMMU_FLAG
);
370 if (IS_ERR_VALUE(da
))
377 * ispmmu_vunmap - Unmap a device address from the ISP MMU
378 * @dev: Device pointer specific to the OMAP3 ISP.
379 * @da: Device address generated from a ispmmu_vmap call.
381 static void ispmmu_vunmap(struct isp_device
*isp
, dma_addr_t da
)
383 struct sg_table
*sgt
;
385 sgt
= iommu_vunmap(isp
->iommu
, (u32
)da
);
389 /* -----------------------------------------------------------------------------
390 * Video queue operations
393 static void isp_video_queue_prepare(struct isp_video_queue
*queue
,
394 unsigned int *nbuffers
, unsigned int *size
)
396 struct isp_video_fh
*vfh
=
397 container_of(queue
, struct isp_video_fh
, queue
);
398 struct isp_video
*video
= vfh
->video
;
400 *size
= vfh
->format
.fmt
.pix
.sizeimage
;
404 *nbuffers
= min(*nbuffers
, video
->capture_mem
/ PAGE_ALIGN(*size
));
407 static void isp_video_buffer_cleanup(struct isp_video_buffer
*buf
)
409 struct isp_video_fh
*vfh
= isp_video_queue_to_isp_video_fh(buf
->queue
);
410 struct isp_buffer
*buffer
= to_isp_buffer(buf
);
411 struct isp_video
*video
= vfh
->video
;
413 if (buffer
->isp_addr
) {
414 ispmmu_vunmap(video
->isp
, buffer
->isp_addr
);
415 buffer
->isp_addr
= 0;
419 static int isp_video_buffer_prepare(struct isp_video_buffer
*buf
)
421 struct isp_video_fh
*vfh
= isp_video_queue_to_isp_video_fh(buf
->queue
);
422 struct isp_buffer
*buffer
= to_isp_buffer(buf
);
423 struct isp_video
*video
= vfh
->video
;
426 addr
= ispmmu_vmap(video
->isp
, buf
->sglist
, buf
->sglen
);
427 if (IS_ERR_VALUE(addr
))
430 if (!IS_ALIGNED(addr
, 32)) {
431 dev_dbg(video
->isp
->dev
, "Buffer address must be "
432 "aligned to 32 bytes boundary.\n");
433 ispmmu_vunmap(video
->isp
, buffer
->isp_addr
);
437 buf
->vbuf
.bytesused
= vfh
->format
.fmt
.pix
.sizeimage
;
438 buffer
->isp_addr
= addr
;
443 * isp_video_buffer_queue - Add buffer to streaming queue
446 * In memory-to-memory mode, start streaming on the pipeline if buffers are
447 * queued on both the input and the output, if the pipeline isn't already busy.
448 * If the pipeline is busy, it will be restarted in the output module interrupt
451 static void isp_video_buffer_queue(struct isp_video_buffer
*buf
)
453 struct isp_video_fh
*vfh
= isp_video_queue_to_isp_video_fh(buf
->queue
);
454 struct isp_buffer
*buffer
= to_isp_buffer(buf
);
455 struct isp_video
*video
= vfh
->video
;
456 struct isp_pipeline
*pipe
= to_isp_pipeline(&video
->video
.entity
);
457 enum isp_pipeline_state state
;
462 empty
= list_empty(&video
->dmaqueue
);
463 list_add_tail(&buffer
->buffer
.irqlist
, &video
->dmaqueue
);
466 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
467 state
= ISP_PIPELINE_QUEUE_OUTPUT
;
469 state
= ISP_PIPELINE_QUEUE_INPUT
;
471 spin_lock_irqsave(&pipe
->lock
, flags
);
472 pipe
->state
|= state
;
473 video
->ops
->queue(video
, buffer
);
474 video
->dmaqueue_flags
|= ISP_VIDEO_DMAQUEUE_QUEUED
;
476 start
= isp_pipeline_ready(pipe
);
478 pipe
->state
|= ISP_PIPELINE_STREAM
;
479 spin_unlock_irqrestore(&pipe
->lock
, flags
);
482 omap3isp_pipeline_set_stream(pipe
,
483 ISP_PIPELINE_STREAM_SINGLESHOT
);
487 static const struct isp_video_queue_operations isp_video_queue_ops
= {
488 .queue_prepare
= &isp_video_queue_prepare
,
489 .buffer_prepare
= &isp_video_buffer_prepare
,
490 .buffer_queue
= &isp_video_buffer_queue
,
491 .buffer_cleanup
= &isp_video_buffer_cleanup
,
495 * omap3isp_video_buffer_next - Complete the current buffer and return the next
496 * @video: ISP video object
497 * @error: Whether an error occurred during capture
499 * Remove the current video buffer from the DMA queue and fill its timestamp,
500 * field count and state fields before waking up its completion handler.
502 * The buffer state is set to VIDEOBUF_DONE if no error occurred (@error is 0)
503 * or VIDEOBUF_ERROR otherwise (@error is non-zero).
505 * The DMA queue is expected to contain at least one buffer.
507 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
510 struct isp_buffer
*omap3isp_video_buffer_next(struct isp_video
*video
,
513 struct isp_pipeline
*pipe
= to_isp_pipeline(&video
->video
.entity
);
514 struct isp_video_queue
*queue
= video
->queue
;
515 enum isp_pipeline_state state
;
516 struct isp_video_buffer
*buf
;
520 spin_lock_irqsave(&queue
->irqlock
, flags
);
521 if (WARN_ON(list_empty(&video
->dmaqueue
))) {
522 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
526 buf
= list_first_entry(&video
->dmaqueue
, struct isp_video_buffer
,
528 list_del(&buf
->irqlist
);
529 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
532 buf
->vbuf
.timestamp
.tv_sec
= ts
.tv_sec
;
533 buf
->vbuf
.timestamp
.tv_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
535 /* Do frame number propagation only if this is the output video node.
536 * Frame number either comes from the CSI receivers or it gets
537 * incremented here if H3A is not active.
538 * Note: There is no guarantee that the output buffer will finish
539 * first, so the input number might lag behind by 1 in some cases.
541 if (video
== pipe
->output
&& !pipe
->do_propagation
)
542 buf
->vbuf
.sequence
= atomic_inc_return(&pipe
->frame_number
);
544 buf
->vbuf
.sequence
= atomic_read(&pipe
->frame_number
);
546 buf
->state
= error
? ISP_BUF_STATE_ERROR
: ISP_BUF_STATE_DONE
;
550 if (list_empty(&video
->dmaqueue
)) {
551 if (queue
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
552 state
= ISP_PIPELINE_QUEUE_OUTPUT
553 | ISP_PIPELINE_STREAM
;
555 state
= ISP_PIPELINE_QUEUE_INPUT
556 | ISP_PIPELINE_STREAM
;
558 spin_lock_irqsave(&pipe
->lock
, flags
);
559 pipe
->state
&= ~state
;
560 if (video
->pipe
.stream_state
== ISP_PIPELINE_STREAM_CONTINUOUS
)
561 video
->dmaqueue_flags
|= ISP_VIDEO_DMAQUEUE_UNDERRUN
;
562 spin_unlock_irqrestore(&pipe
->lock
, flags
);
566 if (queue
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& pipe
->input
!= NULL
) {
567 spin_lock_irqsave(&pipe
->lock
, flags
);
568 pipe
->state
&= ~ISP_PIPELINE_STREAM
;
569 spin_unlock_irqrestore(&pipe
->lock
, flags
);
572 buf
= list_first_entry(&video
->dmaqueue
, struct isp_video_buffer
,
574 buf
->state
= ISP_BUF_STATE_ACTIVE
;
575 return to_isp_buffer(buf
);
579 * omap3isp_video_resume - Perform resume operation on the buffers
580 * @video: ISP video object
581 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
583 * This function is intended to be used on suspend/resume scenario. It
584 * requests video queue layer to discard buffers marked as DONE if it's in
585 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
588 void omap3isp_video_resume(struct isp_video
*video
, int continuous
)
590 struct isp_buffer
*buf
= NULL
;
592 if (continuous
&& video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
593 omap3isp_video_queue_discard_done(video
->queue
);
595 if (!list_empty(&video
->dmaqueue
)) {
596 buf
= list_first_entry(&video
->dmaqueue
,
597 struct isp_buffer
, buffer
.irqlist
);
598 video
->ops
->queue(video
, buf
);
599 video
->dmaqueue_flags
|= ISP_VIDEO_DMAQUEUE_QUEUED
;
602 video
->dmaqueue_flags
|= ISP_VIDEO_DMAQUEUE_UNDERRUN
;
606 /* -----------------------------------------------------------------------------
611 isp_video_querycap(struct file
*file
, void *fh
, struct v4l2_capability
*cap
)
613 struct isp_video
*video
= video_drvdata(file
);
615 strlcpy(cap
->driver
, ISP_VIDEO_DRIVER_NAME
, sizeof(cap
->driver
));
616 strlcpy(cap
->card
, video
->video
.name
, sizeof(cap
->card
));
617 strlcpy(cap
->bus_info
, "media", sizeof(cap
->bus_info
));
618 cap
->version
= ISP_VIDEO_DRIVER_VERSION
;
620 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
621 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
623 cap
->capabilities
= V4L2_CAP_VIDEO_OUTPUT
| V4L2_CAP_STREAMING
;
629 isp_video_get_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
631 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
632 struct isp_video
*video
= video_drvdata(file
);
634 if (format
->type
!= video
->type
)
637 mutex_lock(&video
->mutex
);
638 *format
= vfh
->format
;
639 mutex_unlock(&video
->mutex
);
645 isp_video_set_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
647 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
648 struct isp_video
*video
= video_drvdata(file
);
649 struct v4l2_mbus_framefmt fmt
;
651 if (format
->type
!= video
->type
)
654 mutex_lock(&video
->mutex
);
656 /* Fill the bytesperline and sizeimage fields by converting to media bus
657 * format and back to pixel format.
659 isp_video_pix_to_mbus(&format
->fmt
.pix
, &fmt
);
660 isp_video_mbus_to_pix(video
, &fmt
, &format
->fmt
.pix
);
662 vfh
->format
= *format
;
664 mutex_unlock(&video
->mutex
);
669 isp_video_try_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
671 struct isp_video
*video
= video_drvdata(file
);
672 struct v4l2_subdev_format fmt
;
673 struct v4l2_subdev
*subdev
;
677 if (format
->type
!= video
->type
)
680 subdev
= isp_video_remote_subdev(video
, &pad
);
684 isp_video_pix_to_mbus(&format
->fmt
.pix
, &fmt
.format
);
687 fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
688 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &fmt
);
690 return ret
== -ENOIOCTLCMD
? -EINVAL
: ret
;
692 isp_video_mbus_to_pix(video
, &fmt
.format
, &format
->fmt
.pix
);
697 isp_video_cropcap(struct file
*file
, void *fh
, struct v4l2_cropcap
*cropcap
)
699 struct isp_video
*video
= video_drvdata(file
);
700 struct v4l2_subdev
*subdev
;
703 subdev
= isp_video_remote_subdev(video
, NULL
);
707 mutex_lock(&video
->mutex
);
708 ret
= v4l2_subdev_call(subdev
, video
, cropcap
, cropcap
);
709 mutex_unlock(&video
->mutex
);
711 return ret
== -ENOIOCTLCMD
? -EINVAL
: ret
;
715 isp_video_get_crop(struct file
*file
, void *fh
, struct v4l2_crop
*crop
)
717 struct isp_video
*video
= video_drvdata(file
);
718 struct v4l2_subdev_format format
;
719 struct v4l2_subdev
*subdev
;
723 subdev
= isp_video_remote_subdev(video
, &pad
);
727 /* Try the get crop operation first and fallback to get format if not
730 ret
= v4l2_subdev_call(subdev
, video
, g_crop
, crop
);
731 if (ret
!= -ENOIOCTLCMD
)
735 format
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
736 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &format
);
738 return ret
== -ENOIOCTLCMD
? -EINVAL
: ret
;
742 crop
->c
.width
= format
.format
.width
;
743 crop
->c
.height
= format
.format
.height
;
749 isp_video_set_crop(struct file
*file
, void *fh
, struct v4l2_crop
*crop
)
751 struct isp_video
*video
= video_drvdata(file
);
752 struct v4l2_subdev
*subdev
;
755 subdev
= isp_video_remote_subdev(video
, NULL
);
759 mutex_lock(&video
->mutex
);
760 ret
= v4l2_subdev_call(subdev
, video
, s_crop
, crop
);
761 mutex_unlock(&video
->mutex
);
763 return ret
== -ENOIOCTLCMD
? -EINVAL
: ret
;
767 isp_video_get_param(struct file
*file
, void *fh
, struct v4l2_streamparm
*a
)
769 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
770 struct isp_video
*video
= video_drvdata(file
);
772 if (video
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
||
773 video
->type
!= a
->type
)
776 memset(a
, 0, sizeof(*a
));
777 a
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
778 a
->parm
.output
.capability
= V4L2_CAP_TIMEPERFRAME
;
779 a
->parm
.output
.timeperframe
= vfh
->timeperframe
;
785 isp_video_set_param(struct file
*file
, void *fh
, struct v4l2_streamparm
*a
)
787 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
788 struct isp_video
*video
= video_drvdata(file
);
790 if (video
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
||
791 video
->type
!= a
->type
)
794 if (a
->parm
.output
.timeperframe
.denominator
== 0)
795 a
->parm
.output
.timeperframe
.denominator
= 1;
797 vfh
->timeperframe
= a
->parm
.output
.timeperframe
;
803 isp_video_reqbufs(struct file
*file
, void *fh
, struct v4l2_requestbuffers
*rb
)
805 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
807 return omap3isp_video_queue_reqbufs(&vfh
->queue
, rb
);
811 isp_video_querybuf(struct file
*file
, void *fh
, struct v4l2_buffer
*b
)
813 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
815 return omap3isp_video_queue_querybuf(&vfh
->queue
, b
);
819 isp_video_qbuf(struct file
*file
, void *fh
, struct v4l2_buffer
*b
)
821 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
823 return omap3isp_video_queue_qbuf(&vfh
->queue
, b
);
827 isp_video_dqbuf(struct file
*file
, void *fh
, struct v4l2_buffer
*b
)
829 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
831 return omap3isp_video_queue_dqbuf(&vfh
->queue
, b
,
832 file
->f_flags
& O_NONBLOCK
);
838 * Every ISP pipeline has a single input and a single output. The input can be
839 * either a sensor or a video node. The output is always a video node.
841 * As every pipeline has an output video node, the ISP video objects at the
842 * pipeline output stores the pipeline state. It tracks the streaming state of
843 * both the input and output, as well as the availability of buffers.
845 * In sensor-to-memory mode, frames are always available at the pipeline input.
846 * Starting the sensor usually requires I2C transfers and must be done in
847 * interruptible context. The pipeline is started and stopped synchronously
848 * to the stream on/off commands. All modules in the pipeline will get their
849 * subdev set stream handler called. The module at the end of the pipeline must
850 * delay starting the hardware until buffers are available at its output.
852 * In memory-to-memory mode, starting/stopping the stream requires
853 * synchronization between the input and output. ISP modules can't be stopped
854 * in the middle of a frame, and at least some of the modules seem to become
855 * busy as soon as they're started, even if they don't receive a frame start
856 * event. For that reason frames need to be processed in single-shot mode. The
857 * driver needs to wait until a frame is completely processed and written to
858 * memory before restarting the pipeline for the next frame. Pipelined
859 * processing might be possible but requires more testing.
861 * Stream start must be delayed until buffers are available at both the input
862 * and output. The pipeline must be started in the videobuf queue callback with
863 * the buffers queue spinlock held. The modules subdev set stream operation must
867 isp_video_streamon(struct file
*file
, void *fh
, enum v4l2_buf_type type
)
869 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
870 struct isp_video
*video
= video_drvdata(file
);
871 enum isp_pipeline_state state
;
872 struct isp_pipeline
*pipe
;
873 struct isp_video
*far_end
;
877 if (type
!= video
->type
)
880 mutex_lock(&video
->stream_lock
);
882 if (video
->streaming
) {
883 mutex_unlock(&video
->stream_lock
);
887 /* Start streaming on the pipeline. No link touching an entity in the
888 * pipeline can be activated or deactivated once streaming is started.
890 pipe
= video
->video
.entity
.pipe
891 ? to_isp_pipeline(&video
->video
.entity
) : &video
->pipe
;
892 media_entity_pipeline_start(&video
->video
.entity
, &pipe
->pipe
);
894 /* Verify that the currently configured format matches the output of
895 * the connected subdev.
897 ret
= isp_video_check_format(video
, vfh
);
901 video
->bpl_padding
= ret
;
902 video
->bpl_value
= vfh
->format
.fmt
.pix
.bytesperline
;
904 /* Find the ISP video node connected at the far end of the pipeline and
905 * update the pipeline.
907 far_end
= isp_video_far_end(video
);
909 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
910 state
= ISP_PIPELINE_STREAM_OUTPUT
| ISP_PIPELINE_IDLE_OUTPUT
;
911 pipe
->input
= far_end
;
912 pipe
->output
= video
;
914 if (far_end
== NULL
) {
919 state
= ISP_PIPELINE_STREAM_INPUT
| ISP_PIPELINE_IDLE_INPUT
;
921 pipe
->output
= far_end
;
924 if (video
->isp
->pdata
->set_constraints
)
925 video
->isp
->pdata
->set_constraints(video
->isp
, true);
926 pipe
->l3_ick
= clk_get_rate(video
->isp
->clock
[ISP_CLK_L3_ICK
]);
928 /* Validate the pipeline and update its state. */
929 ret
= isp_video_validate_pipeline(pipe
);
933 spin_lock_irqsave(&pipe
->lock
, flags
);
934 pipe
->state
&= ~ISP_PIPELINE_STREAM
;
935 pipe
->state
|= state
;
936 spin_unlock_irqrestore(&pipe
->lock
, flags
);
938 /* Set the maximum time per frame as the value requested by userspace.
939 * This is a soft limit that can be overridden if the hardware doesn't
940 * support the request limit.
942 if (video
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
943 pipe
->max_timeperframe
= vfh
->timeperframe
;
945 video
->queue
= &vfh
->queue
;
946 INIT_LIST_HEAD(&video
->dmaqueue
);
947 atomic_set(&pipe
->frame_number
, -1);
949 ret
= omap3isp_video_queue_streamon(&vfh
->queue
);
953 /* In sensor-to-memory mode, the stream can be started synchronously
954 * to the stream on command. In memory-to-memory mode, it will be
955 * started when buffers are queued on both the input and output.
957 if (pipe
->input
== NULL
) {
958 ret
= omap3isp_pipeline_set_stream(pipe
,
959 ISP_PIPELINE_STREAM_CONTINUOUS
);
962 spin_lock_irqsave(&video
->queue
->irqlock
, flags
);
963 if (list_empty(&video
->dmaqueue
))
964 video
->dmaqueue_flags
|= ISP_VIDEO_DMAQUEUE_UNDERRUN
;
965 spin_unlock_irqrestore(&video
->queue
->irqlock
, flags
);
970 omap3isp_video_queue_streamoff(&vfh
->queue
);
971 if (video
->isp
->pdata
->set_constraints
)
972 video
->isp
->pdata
->set_constraints(video
->isp
, false);
973 media_entity_pipeline_stop(&video
->video
.entity
);
978 video
->streaming
= 1;
980 mutex_unlock(&video
->stream_lock
);
985 isp_video_streamoff(struct file
*file
, void *fh
, enum v4l2_buf_type type
)
987 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
988 struct isp_video
*video
= video_drvdata(file
);
989 struct isp_pipeline
*pipe
= to_isp_pipeline(&video
->video
.entity
);
990 enum isp_pipeline_state state
;
991 unsigned int streaming
;
994 if (type
!= video
->type
)
997 mutex_lock(&video
->stream_lock
);
999 /* Make sure we're not streaming yet. */
1000 mutex_lock(&vfh
->queue
.lock
);
1001 streaming
= vfh
->queue
.streaming
;
1002 mutex_unlock(&vfh
->queue
.lock
);
1007 /* Update the pipeline state. */
1008 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1009 state
= ISP_PIPELINE_STREAM_OUTPUT
1010 | ISP_PIPELINE_QUEUE_OUTPUT
;
1012 state
= ISP_PIPELINE_STREAM_INPUT
1013 | ISP_PIPELINE_QUEUE_INPUT
;
1015 spin_lock_irqsave(&pipe
->lock
, flags
);
1016 pipe
->state
&= ~state
;
1017 spin_unlock_irqrestore(&pipe
->lock
, flags
);
1019 /* Stop the stream. */
1020 omap3isp_pipeline_set_stream(pipe
, ISP_PIPELINE_STREAM_STOPPED
);
1021 omap3isp_video_queue_streamoff(&vfh
->queue
);
1022 video
->queue
= NULL
;
1023 video
->streaming
= 0;
1025 if (video
->isp
->pdata
->set_constraints
)
1026 video
->isp
->pdata
->set_constraints(video
->isp
, false);
1027 media_entity_pipeline_stop(&video
->video
.entity
);
1030 mutex_unlock(&video
->stream_lock
);
1035 isp_video_enum_input(struct file
*file
, void *fh
, struct v4l2_input
*input
)
1037 if (input
->index
> 0)
1040 strlcpy(input
->name
, "camera", sizeof(input
->name
));
1041 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1047 isp_video_g_input(struct file
*file
, void *fh
, unsigned int *input
)
1055 isp_video_s_input(struct file
*file
, void *fh
, unsigned int input
)
1057 return input
== 0 ? 0 : -EINVAL
;
1060 static const struct v4l2_ioctl_ops isp_video_ioctl_ops
= {
1061 .vidioc_querycap
= isp_video_querycap
,
1062 .vidioc_g_fmt_vid_cap
= isp_video_get_format
,
1063 .vidioc_s_fmt_vid_cap
= isp_video_set_format
,
1064 .vidioc_try_fmt_vid_cap
= isp_video_try_format
,
1065 .vidioc_g_fmt_vid_out
= isp_video_get_format
,
1066 .vidioc_s_fmt_vid_out
= isp_video_set_format
,
1067 .vidioc_try_fmt_vid_out
= isp_video_try_format
,
1068 .vidioc_cropcap
= isp_video_cropcap
,
1069 .vidioc_g_crop
= isp_video_get_crop
,
1070 .vidioc_s_crop
= isp_video_set_crop
,
1071 .vidioc_g_parm
= isp_video_get_param
,
1072 .vidioc_s_parm
= isp_video_set_param
,
1073 .vidioc_reqbufs
= isp_video_reqbufs
,
1074 .vidioc_querybuf
= isp_video_querybuf
,
1075 .vidioc_qbuf
= isp_video_qbuf
,
1076 .vidioc_dqbuf
= isp_video_dqbuf
,
1077 .vidioc_streamon
= isp_video_streamon
,
1078 .vidioc_streamoff
= isp_video_streamoff
,
1079 .vidioc_enum_input
= isp_video_enum_input
,
1080 .vidioc_g_input
= isp_video_g_input
,
1081 .vidioc_s_input
= isp_video_s_input
,
1084 /* -----------------------------------------------------------------------------
1085 * V4L2 file operations
1088 static int isp_video_open(struct file
*file
)
1090 struct isp_video
*video
= video_drvdata(file
);
1091 struct isp_video_fh
*handle
;
1094 handle
= kzalloc(sizeof(*handle
), GFP_KERNEL
);
1098 v4l2_fh_init(&handle
->vfh
, &video
->video
);
1099 v4l2_fh_add(&handle
->vfh
);
1101 /* If this is the first user, initialise the pipeline. */
1102 if (omap3isp_get(video
->isp
) == NULL
) {
1107 ret
= omap3isp_pipeline_pm_use(&video
->video
.entity
, 1);
1109 omap3isp_put(video
->isp
);
1113 omap3isp_video_queue_init(&handle
->queue
, video
->type
,
1114 &isp_video_queue_ops
, video
->isp
->dev
,
1115 sizeof(struct isp_buffer
));
1117 memset(&handle
->format
, 0, sizeof(handle
->format
));
1118 handle
->format
.type
= video
->type
;
1119 handle
->timeperframe
.denominator
= 1;
1121 handle
->video
= video
;
1122 file
->private_data
= &handle
->vfh
;
1126 v4l2_fh_del(&handle
->vfh
);
1133 static int isp_video_release(struct file
*file
)
1135 struct isp_video
*video
= video_drvdata(file
);
1136 struct v4l2_fh
*vfh
= file
->private_data
;
1137 struct isp_video_fh
*handle
= to_isp_video_fh(vfh
);
1139 /* Disable streaming and free the buffers queue resources. */
1140 isp_video_streamoff(file
, vfh
, video
->type
);
1142 mutex_lock(&handle
->queue
.lock
);
1143 omap3isp_video_queue_cleanup(&handle
->queue
);
1144 mutex_unlock(&handle
->queue
.lock
);
1146 omap3isp_pipeline_pm_use(&video
->video
.entity
, 0);
1148 /* Release the file handle. */
1151 file
->private_data
= NULL
;
1153 omap3isp_put(video
->isp
);
1158 static unsigned int isp_video_poll(struct file
*file
, poll_table
*wait
)
1160 struct isp_video_fh
*vfh
= to_isp_video_fh(file
->private_data
);
1161 struct isp_video_queue
*queue
= &vfh
->queue
;
1163 return omap3isp_video_queue_poll(queue
, file
, wait
);
1166 static int isp_video_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1168 struct isp_video_fh
*vfh
= to_isp_video_fh(file
->private_data
);
1170 return omap3isp_video_queue_mmap(&vfh
->queue
, vma
);
1173 static struct v4l2_file_operations isp_video_fops
= {
1174 .owner
= THIS_MODULE
,
1175 .unlocked_ioctl
= video_ioctl2
,
1176 .open
= isp_video_open
,
1177 .release
= isp_video_release
,
1178 .poll
= isp_video_poll
,
1179 .mmap
= isp_video_mmap
,
1182 /* -----------------------------------------------------------------------------
1186 static const struct isp_video_operations isp_video_dummy_ops
= {
1189 int omap3isp_video_init(struct isp_video
*video
, const char *name
)
1191 const char *direction
;
1194 switch (video
->type
) {
1195 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1196 direction
= "output";
1197 video
->pad
.flags
= MEDIA_PAD_FL_SINK
;
1199 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
1200 direction
= "input";
1201 video
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1208 ret
= media_entity_init(&video
->video
.entity
, 1, &video
->pad
, 0);
1212 mutex_init(&video
->mutex
);
1213 atomic_set(&video
->active
, 0);
1215 spin_lock_init(&video
->pipe
.lock
);
1216 mutex_init(&video
->stream_lock
);
1218 /* Initialize the video device. */
1219 if (video
->ops
== NULL
)
1220 video
->ops
= &isp_video_dummy_ops
;
1222 video
->video
.fops
= &isp_video_fops
;
1223 snprintf(video
->video
.name
, sizeof(video
->video
.name
),
1224 "OMAP3 ISP %s %s", name
, direction
);
1225 video
->video
.vfl_type
= VFL_TYPE_GRABBER
;
1226 video
->video
.release
= video_device_release_empty
;
1227 video
->video
.ioctl_ops
= &isp_video_ioctl_ops
;
1228 video
->pipe
.stream_state
= ISP_PIPELINE_STREAM_STOPPED
;
1230 video_set_drvdata(&video
->video
, video
);
1235 int omap3isp_video_register(struct isp_video
*video
, struct v4l2_device
*vdev
)
1239 video
->video
.v4l2_dev
= vdev
;
1241 ret
= video_register_device(&video
->video
, VFL_TYPE_GRABBER
, -1);
1243 printk(KERN_ERR
"%s: could not register video device (%d)\n",
1249 void omap3isp_video_unregister(struct isp_video
*video
)
1251 if (video_is_registered(&video
->video
)) {
1252 media_entity_cleanup(&video
->video
.entity
);
1253 video_unregister_device(&video
->video
);