1 // SPDX-License-Identifier: GPL-2.0-only
5 * TI OMAP3 ISP - Generic video node
7 * Copyright (C) 2009-2010 Nokia Corporation
9 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10 * Sakari Ailus <sakari.ailus@iki.fi>
13 #include <linux/clk.h>
15 #include <linux/module.h>
16 #include <linux/pagemap.h>
17 #include <linux/scatterlist.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-mc.h>
25 #include <media/videobuf2-dma-contig.h>
31 /* -----------------------------------------------------------------------------
36 * NOTE: When adding new media bus codes, always remember to add
37 * corresponding in-memory formats to the table below!!!
39 static struct isp_format_info formats
[] = {
40 { MEDIA_BUS_FMT_Y8_1X8
, MEDIA_BUS_FMT_Y8_1X8
,
41 MEDIA_BUS_FMT_Y8_1X8
, MEDIA_BUS_FMT_Y8_1X8
,
42 V4L2_PIX_FMT_GREY
, 8, 1, },
43 { MEDIA_BUS_FMT_Y10_1X10
, MEDIA_BUS_FMT_Y10_1X10
,
44 MEDIA_BUS_FMT_Y10_1X10
, MEDIA_BUS_FMT_Y8_1X8
,
45 V4L2_PIX_FMT_Y10
, 10, 2, },
46 { MEDIA_BUS_FMT_Y12_1X12
, MEDIA_BUS_FMT_Y10_1X10
,
47 MEDIA_BUS_FMT_Y12_1X12
, MEDIA_BUS_FMT_Y8_1X8
,
48 V4L2_PIX_FMT_Y12
, 12, 2, },
49 { MEDIA_BUS_FMT_SBGGR8_1X8
, MEDIA_BUS_FMT_SBGGR8_1X8
,
50 MEDIA_BUS_FMT_SBGGR8_1X8
, MEDIA_BUS_FMT_SBGGR8_1X8
,
51 V4L2_PIX_FMT_SBGGR8
, 8, 1, },
52 { MEDIA_BUS_FMT_SGBRG8_1X8
, MEDIA_BUS_FMT_SGBRG8_1X8
,
53 MEDIA_BUS_FMT_SGBRG8_1X8
, MEDIA_BUS_FMT_SGBRG8_1X8
,
54 V4L2_PIX_FMT_SGBRG8
, 8, 1, },
55 { MEDIA_BUS_FMT_SGRBG8_1X8
, MEDIA_BUS_FMT_SGRBG8_1X8
,
56 MEDIA_BUS_FMT_SGRBG8_1X8
, MEDIA_BUS_FMT_SGRBG8_1X8
,
57 V4L2_PIX_FMT_SGRBG8
, 8, 1, },
58 { MEDIA_BUS_FMT_SRGGB8_1X8
, MEDIA_BUS_FMT_SRGGB8_1X8
,
59 MEDIA_BUS_FMT_SRGGB8_1X8
, MEDIA_BUS_FMT_SRGGB8_1X8
,
60 V4L2_PIX_FMT_SRGGB8
, 8, 1, },
61 { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8
, MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8
,
62 MEDIA_BUS_FMT_SBGGR10_1X10
, 0,
63 V4L2_PIX_FMT_SBGGR10DPCM8
, 8, 1, },
64 { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8
, MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8
,
65 MEDIA_BUS_FMT_SGBRG10_1X10
, 0,
66 V4L2_PIX_FMT_SGBRG10DPCM8
, 8, 1, },
67 { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8
, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8
,
68 MEDIA_BUS_FMT_SGRBG10_1X10
, 0,
69 V4L2_PIX_FMT_SGRBG10DPCM8
, 8, 1, },
70 { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8
, MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8
,
71 MEDIA_BUS_FMT_SRGGB10_1X10
, 0,
72 V4L2_PIX_FMT_SRGGB10DPCM8
, 8, 1, },
73 { MEDIA_BUS_FMT_SBGGR10_1X10
, MEDIA_BUS_FMT_SBGGR10_1X10
,
74 MEDIA_BUS_FMT_SBGGR10_1X10
, MEDIA_BUS_FMT_SBGGR8_1X8
,
75 V4L2_PIX_FMT_SBGGR10
, 10, 2, },
76 { MEDIA_BUS_FMT_SGBRG10_1X10
, MEDIA_BUS_FMT_SGBRG10_1X10
,
77 MEDIA_BUS_FMT_SGBRG10_1X10
, MEDIA_BUS_FMT_SGBRG8_1X8
,
78 V4L2_PIX_FMT_SGBRG10
, 10, 2, },
79 { MEDIA_BUS_FMT_SGRBG10_1X10
, MEDIA_BUS_FMT_SGRBG10_1X10
,
80 MEDIA_BUS_FMT_SGRBG10_1X10
, MEDIA_BUS_FMT_SGRBG8_1X8
,
81 V4L2_PIX_FMT_SGRBG10
, 10, 2, },
82 { MEDIA_BUS_FMT_SRGGB10_1X10
, MEDIA_BUS_FMT_SRGGB10_1X10
,
83 MEDIA_BUS_FMT_SRGGB10_1X10
, MEDIA_BUS_FMT_SRGGB8_1X8
,
84 V4L2_PIX_FMT_SRGGB10
, 10, 2, },
85 { MEDIA_BUS_FMT_SBGGR12_1X12
, MEDIA_BUS_FMT_SBGGR10_1X10
,
86 MEDIA_BUS_FMT_SBGGR12_1X12
, MEDIA_BUS_FMT_SBGGR8_1X8
,
87 V4L2_PIX_FMT_SBGGR12
, 12, 2, },
88 { MEDIA_BUS_FMT_SGBRG12_1X12
, MEDIA_BUS_FMT_SGBRG10_1X10
,
89 MEDIA_BUS_FMT_SGBRG12_1X12
, MEDIA_BUS_FMT_SGBRG8_1X8
,
90 V4L2_PIX_FMT_SGBRG12
, 12, 2, },
91 { MEDIA_BUS_FMT_SGRBG12_1X12
, MEDIA_BUS_FMT_SGRBG10_1X10
,
92 MEDIA_BUS_FMT_SGRBG12_1X12
, MEDIA_BUS_FMT_SGRBG8_1X8
,
93 V4L2_PIX_FMT_SGRBG12
, 12, 2, },
94 { MEDIA_BUS_FMT_SRGGB12_1X12
, MEDIA_BUS_FMT_SRGGB10_1X10
,
95 MEDIA_BUS_FMT_SRGGB12_1X12
, MEDIA_BUS_FMT_SRGGB8_1X8
,
96 V4L2_PIX_FMT_SRGGB12
, 12, 2, },
97 { MEDIA_BUS_FMT_UYVY8_1X16
, MEDIA_BUS_FMT_UYVY8_1X16
,
98 MEDIA_BUS_FMT_UYVY8_1X16
, 0,
99 V4L2_PIX_FMT_UYVY
, 16, 2, },
100 { MEDIA_BUS_FMT_YUYV8_1X16
, MEDIA_BUS_FMT_YUYV8_1X16
,
101 MEDIA_BUS_FMT_YUYV8_1X16
, 0,
102 V4L2_PIX_FMT_YUYV
, 16, 2, },
103 { MEDIA_BUS_FMT_UYVY8_2X8
, MEDIA_BUS_FMT_UYVY8_2X8
,
104 MEDIA_BUS_FMT_UYVY8_2X8
, 0,
105 V4L2_PIX_FMT_UYVY
, 8, 2, },
106 { MEDIA_BUS_FMT_YUYV8_2X8
, MEDIA_BUS_FMT_YUYV8_2X8
,
107 MEDIA_BUS_FMT_YUYV8_2X8
, 0,
108 V4L2_PIX_FMT_YUYV
, 8, 2, },
109 /* Empty entry to catch the unsupported pixel code (0) used by the CCDC
110 * module and avoid NULL pointer dereferences.
115 const struct isp_format_info
*omap3isp_video_format_info(u32 code
)
119 for (i
= 0; i
< ARRAY_SIZE(formats
); ++i
) {
120 if (formats
[i
].code
== code
)
128 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
129 * @video: ISP video instance
130 * @mbus: v4l2_mbus_framefmt format (input)
131 * @pix: v4l2_pix_format format (output)
133 * Fill the output pix structure with information from the input mbus format.
134 * The bytesperline and sizeimage fields are computed from the requested bytes
135 * per line value in the pix format and information from the video instance.
137 * Return the number of padding bytes at end of line.
139 static unsigned int isp_video_mbus_to_pix(const struct isp_video
*video
,
140 const struct v4l2_mbus_framefmt
*mbus
,
141 struct v4l2_pix_format
*pix
)
143 unsigned int bpl
= pix
->bytesperline
;
144 unsigned int min_bpl
;
147 memset(pix
, 0, sizeof(*pix
));
148 pix
->width
= mbus
->width
;
149 pix
->height
= mbus
->height
;
151 for (i
= 0; i
< ARRAY_SIZE(formats
); ++i
) {
152 if (formats
[i
].code
== mbus
->code
)
156 if (WARN_ON(i
== ARRAY_SIZE(formats
)))
159 min_bpl
= pix
->width
* formats
[i
].bpp
;
161 /* Clamp the requested bytes per line value. If the maximum bytes per
162 * line value is zero, the module doesn't support user configurable line
163 * sizes. Override the requested value with the minimum in that case.
166 bpl
= clamp(bpl
, min_bpl
, video
->bpl_max
);
170 if (!video
->bpl_zero_padding
|| bpl
!= min_bpl
)
171 bpl
= ALIGN(bpl
, video
->bpl_alignment
);
173 pix
->pixelformat
= formats
[i
].pixelformat
;
174 pix
->bytesperline
= bpl
;
175 pix
->sizeimage
= pix
->bytesperline
* pix
->height
;
176 pix
->colorspace
= mbus
->colorspace
;
177 pix
->field
= mbus
->field
;
179 return bpl
- min_bpl
;
182 static void isp_video_pix_to_mbus(const struct v4l2_pix_format
*pix
,
183 struct v4l2_mbus_framefmt
*mbus
)
187 memset(mbus
, 0, sizeof(*mbus
));
188 mbus
->width
= pix
->width
;
189 mbus
->height
= pix
->height
;
191 /* Skip the last format in the loop so that it will be selected if no
194 for (i
= 0; i
< ARRAY_SIZE(formats
) - 1; ++i
) {
195 if (formats
[i
].pixelformat
== pix
->pixelformat
)
199 mbus
->code
= formats
[i
].code
;
200 mbus
->colorspace
= pix
->colorspace
;
201 mbus
->field
= pix
->field
;
204 static struct v4l2_subdev
*
205 isp_video_remote_subdev(struct isp_video
*video
, u32
*pad
)
207 struct media_pad
*remote
;
209 remote
= media_entity_remote_pad(&video
->pad
);
211 if (!remote
|| !is_media_entity_v4l2_subdev(remote
->entity
))
215 *pad
= remote
->index
;
217 return media_entity_to_v4l2_subdev(remote
->entity
);
220 /* Return a pointer to the ISP video instance at the far end of the pipeline. */
221 static int isp_video_get_graph_data(struct isp_video
*video
,
222 struct isp_pipeline
*pipe
)
224 struct media_graph graph
;
225 struct media_entity
*entity
= &video
->video
.entity
;
226 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
227 struct isp_video
*far_end
= NULL
;
230 mutex_lock(&mdev
->graph_mutex
);
231 ret
= media_graph_walk_init(&graph
, mdev
);
233 mutex_unlock(&mdev
->graph_mutex
);
237 media_graph_walk_start(&graph
, entity
);
239 while ((entity
= media_graph_walk_next(&graph
))) {
240 struct isp_video
*__video
;
242 media_entity_enum_set(&pipe
->ent_enum
, entity
);
247 if (entity
== &video
->video
.entity
)
250 if (!is_media_entity_v4l2_video_device(entity
))
253 __video
= to_isp_video(media_entity_to_video_device(entity
));
254 if (__video
->type
!= video
->type
)
258 mutex_unlock(&mdev
->graph_mutex
);
260 media_graph_walk_cleanup(&graph
);
262 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
263 pipe
->input
= far_end
;
264 pipe
->output
= video
;
270 pipe
->output
= far_end
;
277 __isp_video_get_format(struct isp_video
*video
, struct v4l2_format
*format
)
279 struct v4l2_subdev_format fmt
;
280 struct v4l2_subdev
*subdev
;
284 subdev
= isp_video_remote_subdev(video
, &pad
);
289 fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
291 mutex_lock(&video
->mutex
);
292 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &fmt
);
293 mutex_unlock(&video
->mutex
);
298 format
->type
= video
->type
;
299 return isp_video_mbus_to_pix(video
, &fmt
.format
, &format
->fmt
.pix
);
303 isp_video_check_format(struct isp_video
*video
, struct isp_video_fh
*vfh
)
305 struct v4l2_format format
;
308 memcpy(&format
, &vfh
->format
, sizeof(format
));
309 ret
= __isp_video_get_format(video
, &format
);
313 if (vfh
->format
.fmt
.pix
.pixelformat
!= format
.fmt
.pix
.pixelformat
||
314 vfh
->format
.fmt
.pix
.height
!= format
.fmt
.pix
.height
||
315 vfh
->format
.fmt
.pix
.width
!= format
.fmt
.pix
.width
||
316 vfh
->format
.fmt
.pix
.bytesperline
!= format
.fmt
.pix
.bytesperline
||
317 vfh
->format
.fmt
.pix
.sizeimage
!= format
.fmt
.pix
.sizeimage
||
318 vfh
->format
.fmt
.pix
.field
!= format
.fmt
.pix
.field
)
324 /* -----------------------------------------------------------------------------
325 * Video queue operations
328 static int isp_video_queue_setup(struct vb2_queue
*queue
,
329 unsigned int *count
, unsigned int *num_planes
,
330 unsigned int sizes
[], struct device
*alloc_devs
[])
332 struct isp_video_fh
*vfh
= vb2_get_drv_priv(queue
);
333 struct isp_video
*video
= vfh
->video
;
337 sizes
[0] = vfh
->format
.fmt
.pix
.sizeimage
;
341 *count
= min(*count
, video
->capture_mem
/ PAGE_ALIGN(sizes
[0]));
346 static int isp_video_buffer_prepare(struct vb2_buffer
*buf
)
348 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(buf
);
349 struct isp_video_fh
*vfh
= vb2_get_drv_priv(buf
->vb2_queue
);
350 struct isp_buffer
*buffer
= to_isp_buffer(vbuf
);
351 struct isp_video
*video
= vfh
->video
;
354 /* Refuse to prepare the buffer is the video node has registered an
355 * error. We don't need to take any lock here as the operation is
356 * inherently racy. The authoritative check will be performed in the
357 * queue handler, which can't return an error, this check is just a best
358 * effort to notify userspace as early as possible.
360 if (unlikely(video
->error
))
363 addr
= vb2_dma_contig_plane_dma_addr(buf
, 0);
364 if (!IS_ALIGNED(addr
, 32)) {
365 dev_dbg(video
->isp
->dev
,
366 "Buffer address must be aligned to 32 bytes boundary.\n");
370 vb2_set_plane_payload(&buffer
->vb
.vb2_buf
, 0,
371 vfh
->format
.fmt
.pix
.sizeimage
);
378 * isp_video_buffer_queue - Add buffer to streaming queue
381 * In memory-to-memory mode, start streaming on the pipeline if buffers are
382 * queued on both the input and the output, if the pipeline isn't already busy.
383 * If the pipeline is busy, it will be restarted in the output module interrupt
386 static void isp_video_buffer_queue(struct vb2_buffer
*buf
)
388 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(buf
);
389 struct isp_video_fh
*vfh
= vb2_get_drv_priv(buf
->vb2_queue
);
390 struct isp_buffer
*buffer
= to_isp_buffer(vbuf
);
391 struct isp_video
*video
= vfh
->video
;
392 struct isp_pipeline
*pipe
= to_isp_pipeline(&video
->video
.entity
);
393 enum isp_pipeline_state state
;
398 spin_lock_irqsave(&video
->irqlock
, flags
);
400 if (unlikely(video
->error
)) {
401 vb2_buffer_done(&buffer
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
402 spin_unlock_irqrestore(&video
->irqlock
, flags
);
406 empty
= list_empty(&video
->dmaqueue
);
407 list_add_tail(&buffer
->irqlist
, &video
->dmaqueue
);
409 spin_unlock_irqrestore(&video
->irqlock
, flags
);
412 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
413 state
= ISP_PIPELINE_QUEUE_OUTPUT
;
415 state
= ISP_PIPELINE_QUEUE_INPUT
;
417 spin_lock_irqsave(&pipe
->lock
, flags
);
418 pipe
->state
|= state
;
419 video
->ops
->queue(video
, buffer
);
420 video
->dmaqueue_flags
|= ISP_VIDEO_DMAQUEUE_QUEUED
;
422 start
= isp_pipeline_ready(pipe
);
424 pipe
->state
|= ISP_PIPELINE_STREAM
;
425 spin_unlock_irqrestore(&pipe
->lock
, flags
);
428 omap3isp_pipeline_set_stream(pipe
,
429 ISP_PIPELINE_STREAM_SINGLESHOT
);
434 * omap3isp_video_return_buffers - Return all queued buffers to videobuf2
435 * @video: ISP video object
436 * @state: new state for the returned buffers
438 * Return all buffers queued on the video node to videobuf2 in the given state.
439 * The buffer state should be VB2_BUF_STATE_QUEUED if called due to an error
440 * when starting the stream, or VB2_BUF_STATE_ERROR otherwise.
442 * The function must be called with the video irqlock held.
444 static void omap3isp_video_return_buffers(struct isp_video
*video
,
445 enum vb2_buffer_state state
)
447 while (!list_empty(&video
->dmaqueue
)) {
448 struct isp_buffer
*buf
;
450 buf
= list_first_entry(&video
->dmaqueue
,
451 struct isp_buffer
, irqlist
);
452 list_del(&buf
->irqlist
);
453 vb2_buffer_done(&buf
->vb
.vb2_buf
, state
);
457 static int isp_video_start_streaming(struct vb2_queue
*queue
,
460 struct isp_video_fh
*vfh
= vb2_get_drv_priv(queue
);
461 struct isp_video
*video
= vfh
->video
;
462 struct isp_pipeline
*pipe
= to_isp_pipeline(&video
->video
.entity
);
466 /* In sensor-to-memory mode, the stream can be started synchronously
467 * to the stream on command. In memory-to-memory mode, it will be
468 * started when buffers are queued on both the input and output.
473 ret
= omap3isp_pipeline_set_stream(pipe
,
474 ISP_PIPELINE_STREAM_CONTINUOUS
);
476 spin_lock_irqsave(&video
->irqlock
, flags
);
477 omap3isp_video_return_buffers(video
, VB2_BUF_STATE_QUEUED
);
478 spin_unlock_irqrestore(&video
->irqlock
, flags
);
482 spin_lock_irqsave(&video
->irqlock
, flags
);
483 if (list_empty(&video
->dmaqueue
))
484 video
->dmaqueue_flags
|= ISP_VIDEO_DMAQUEUE_UNDERRUN
;
485 spin_unlock_irqrestore(&video
->irqlock
, flags
);
490 static const struct vb2_ops isp_video_queue_ops
= {
491 .queue_setup
= isp_video_queue_setup
,
492 .buf_prepare
= isp_video_buffer_prepare
,
493 .buf_queue
= isp_video_buffer_queue
,
494 .start_streaming
= isp_video_start_streaming
,
498 * omap3isp_video_buffer_next - Complete the current buffer and return the next
499 * @video: ISP video object
501 * Remove the current video buffer from the DMA queue and fill its timestamp and
502 * field count before handing it back to videobuf2.
504 * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no
505 * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise.
506 * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE.
508 * The DMA queue is expected to contain at least one buffer.
510 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
513 struct isp_buffer
*omap3isp_video_buffer_next(struct isp_video
*video
)
515 struct isp_pipeline
*pipe
= to_isp_pipeline(&video
->video
.entity
);
516 enum vb2_buffer_state vb_state
;
517 struct isp_buffer
*buf
;
520 spin_lock_irqsave(&video
->irqlock
, flags
);
521 if (WARN_ON(list_empty(&video
->dmaqueue
))) {
522 spin_unlock_irqrestore(&video
->irqlock
, flags
);
526 buf
= list_first_entry(&video
->dmaqueue
, struct isp_buffer
,
528 list_del(&buf
->irqlist
);
529 spin_unlock_irqrestore(&video
->irqlock
, flags
);
531 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
533 /* Do frame number propagation only if this is the output video node.
534 * Frame number either comes from the CSI receivers or it gets
535 * incremented here if H3A is not active.
536 * Note: There is no guarantee that the output buffer will finish
537 * first, so the input number might lag behind by 1 in some cases.
539 if (video
== pipe
->output
&& !pipe
->do_propagation
)
541 atomic_inc_return(&pipe
->frame_number
);
543 buf
->vb
.sequence
= atomic_read(&pipe
->frame_number
);
545 if (pipe
->field
!= V4L2_FIELD_NONE
)
546 buf
->vb
.sequence
/= 2;
548 buf
->vb
.field
= pipe
->field
;
550 /* Report pipeline errors to userspace on the capture device side. */
551 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& pipe
->error
) {
552 vb_state
= VB2_BUF_STATE_ERROR
;
555 vb_state
= VB2_BUF_STATE_DONE
;
558 vb2_buffer_done(&buf
->vb
.vb2_buf
, vb_state
);
560 spin_lock_irqsave(&video
->irqlock
, flags
);
562 if (list_empty(&video
->dmaqueue
)) {
563 enum isp_pipeline_state state
;
565 spin_unlock_irqrestore(&video
->irqlock
, flags
);
567 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
568 state
= ISP_PIPELINE_QUEUE_OUTPUT
569 | ISP_PIPELINE_STREAM
;
571 state
= ISP_PIPELINE_QUEUE_INPUT
572 | ISP_PIPELINE_STREAM
;
574 spin_lock_irqsave(&pipe
->lock
, flags
);
575 pipe
->state
&= ~state
;
576 if (video
->pipe
.stream_state
== ISP_PIPELINE_STREAM_CONTINUOUS
)
577 video
->dmaqueue_flags
|= ISP_VIDEO_DMAQUEUE_UNDERRUN
;
578 spin_unlock_irqrestore(&pipe
->lock
, flags
);
582 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& pipe
->input
!= NULL
) {
583 spin_lock(&pipe
->lock
);
584 pipe
->state
&= ~ISP_PIPELINE_STREAM
;
585 spin_unlock(&pipe
->lock
);
588 buf
= list_first_entry(&video
->dmaqueue
, struct isp_buffer
,
591 spin_unlock_irqrestore(&video
->irqlock
, flags
);
597 * omap3isp_video_cancel_stream - Cancel stream on a video node
598 * @video: ISP video object
600 * Cancelling a stream returns all buffers queued on the video node to videobuf2
601 * in the erroneous state and makes sure no new buffer can be queued.
603 void omap3isp_video_cancel_stream(struct isp_video
*video
)
607 spin_lock_irqsave(&video
->irqlock
, flags
);
608 omap3isp_video_return_buffers(video
, VB2_BUF_STATE_ERROR
);
610 spin_unlock_irqrestore(&video
->irqlock
, flags
);
614 * omap3isp_video_resume - Perform resume operation on the buffers
615 * @video: ISP video object
616 * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise
618 * This function is intended to be used on suspend/resume scenario. It
619 * requests video queue layer to discard buffers marked as DONE if it's in
620 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
623 void omap3isp_video_resume(struct isp_video
*video
, int continuous
)
625 struct isp_buffer
*buf
= NULL
;
627 if (continuous
&& video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
628 mutex_lock(&video
->queue_lock
);
629 vb2_discard_done(video
->queue
);
630 mutex_unlock(&video
->queue_lock
);
633 if (!list_empty(&video
->dmaqueue
)) {
634 buf
= list_first_entry(&video
->dmaqueue
,
635 struct isp_buffer
, irqlist
);
636 video
->ops
->queue(video
, buf
);
637 video
->dmaqueue_flags
|= ISP_VIDEO_DMAQUEUE_QUEUED
;
640 video
->dmaqueue_flags
|= ISP_VIDEO_DMAQUEUE_UNDERRUN
;
644 /* -----------------------------------------------------------------------------
649 isp_video_querycap(struct file
*file
, void *fh
, struct v4l2_capability
*cap
)
651 struct isp_video
*video
= video_drvdata(file
);
653 strscpy(cap
->driver
, ISP_VIDEO_DRIVER_NAME
, sizeof(cap
->driver
));
654 strscpy(cap
->card
, video
->video
.name
, sizeof(cap
->card
));
655 strscpy(cap
->bus_info
, "media", sizeof(cap
->bus_info
));
657 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_VIDEO_OUTPUT
658 | V4L2_CAP_STREAMING
| V4L2_CAP_DEVICE_CAPS
;
665 isp_video_get_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
667 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
668 struct isp_video
*video
= video_drvdata(file
);
670 if (format
->type
!= video
->type
)
673 mutex_lock(&video
->mutex
);
674 *format
= vfh
->format
;
675 mutex_unlock(&video
->mutex
);
681 isp_video_set_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
683 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
684 struct isp_video
*video
= video_drvdata(file
);
685 struct v4l2_mbus_framefmt fmt
;
687 if (format
->type
!= video
->type
)
690 /* Replace unsupported field orders with sane defaults. */
691 switch (format
->fmt
.pix
.field
) {
692 case V4L2_FIELD_NONE
:
693 /* Progressive is supported everywhere. */
695 case V4L2_FIELD_ALTERNATE
:
696 /* ALTERNATE is not supported on output nodes. */
697 if (video
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
698 format
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
700 case V4L2_FIELD_INTERLACED
:
701 /* The ISP has no concept of video standard, select the
702 * top-bottom order when the unqualified interlaced order is
705 format
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED_TB
;
707 case V4L2_FIELD_INTERLACED_TB
:
708 case V4L2_FIELD_INTERLACED_BT
:
709 /* Interlaced orders are only supported at the CCDC output. */
710 if (video
!= &video
->isp
->isp_ccdc
.video_out
)
711 format
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
714 case V4L2_FIELD_BOTTOM
:
715 case V4L2_FIELD_SEQ_TB
:
716 case V4L2_FIELD_SEQ_BT
:
718 /* All other field orders are currently unsupported, default to
721 format
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
725 /* Fill the bytesperline and sizeimage fields by converting to media bus
726 * format and back to pixel format.
728 isp_video_pix_to_mbus(&format
->fmt
.pix
, &fmt
);
729 isp_video_mbus_to_pix(video
, &fmt
, &format
->fmt
.pix
);
731 mutex_lock(&video
->mutex
);
732 vfh
->format
= *format
;
733 mutex_unlock(&video
->mutex
);
739 isp_video_try_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
741 struct isp_video
*video
= video_drvdata(file
);
742 struct v4l2_subdev_format fmt
;
743 struct v4l2_subdev
*subdev
;
747 if (format
->type
!= video
->type
)
750 subdev
= isp_video_remote_subdev(video
, &pad
);
754 isp_video_pix_to_mbus(&format
->fmt
.pix
, &fmt
.format
);
757 fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
758 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &fmt
);
760 return ret
== -ENOIOCTLCMD
? -ENOTTY
: ret
;
762 isp_video_mbus_to_pix(video
, &fmt
.format
, &format
->fmt
.pix
);
767 isp_video_get_selection(struct file
*file
, void *fh
, struct v4l2_selection
*sel
)
769 struct isp_video
*video
= video_drvdata(file
);
770 struct v4l2_subdev_format format
;
771 struct v4l2_subdev
*subdev
;
772 struct v4l2_subdev_selection sdsel
= {
773 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
774 .target
= sel
->target
,
779 switch (sel
->target
) {
780 case V4L2_SEL_TGT_CROP
:
781 case V4L2_SEL_TGT_CROP_BOUNDS
:
782 case V4L2_SEL_TGT_CROP_DEFAULT
:
783 if (video
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
786 case V4L2_SEL_TGT_COMPOSE
:
787 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
788 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
789 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
795 subdev
= isp_video_remote_subdev(video
, &pad
);
799 /* Try the get selection operation first and fallback to get format if not
803 ret
= v4l2_subdev_call(subdev
, pad
, get_selection
, NULL
, &sdsel
);
806 if (ret
!= -ENOIOCTLCMD
)
810 format
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
811 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &format
);
813 return ret
== -ENOIOCTLCMD
? -ENOTTY
: ret
;
817 sel
->r
.width
= format
.format
.width
;
818 sel
->r
.height
= format
.format
.height
;
824 isp_video_set_selection(struct file
*file
, void *fh
, struct v4l2_selection
*sel
)
826 struct isp_video
*video
= video_drvdata(file
);
827 struct v4l2_subdev
*subdev
;
828 struct v4l2_subdev_selection sdsel
= {
829 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
830 .target
= sel
->target
,
837 switch (sel
->target
) {
838 case V4L2_SEL_TGT_CROP
:
839 if (video
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
842 case V4L2_SEL_TGT_COMPOSE
:
843 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
849 subdev
= isp_video_remote_subdev(video
, &pad
);
854 mutex_lock(&video
->mutex
);
855 ret
= v4l2_subdev_call(subdev
, pad
, set_selection
, NULL
, &sdsel
);
856 mutex_unlock(&video
->mutex
);
860 return ret
== -ENOIOCTLCMD
? -ENOTTY
: ret
;
864 isp_video_get_param(struct file
*file
, void *fh
, struct v4l2_streamparm
*a
)
866 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
867 struct isp_video
*video
= video_drvdata(file
);
869 if (video
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
||
870 video
->type
!= a
->type
)
873 memset(a
, 0, sizeof(*a
));
874 a
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
875 a
->parm
.output
.capability
= V4L2_CAP_TIMEPERFRAME
;
876 a
->parm
.output
.timeperframe
= vfh
->timeperframe
;
882 isp_video_set_param(struct file
*file
, void *fh
, struct v4l2_streamparm
*a
)
884 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
885 struct isp_video
*video
= video_drvdata(file
);
887 if (video
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
||
888 video
->type
!= a
->type
)
891 if (a
->parm
.output
.timeperframe
.denominator
== 0)
892 a
->parm
.output
.timeperframe
.denominator
= 1;
894 vfh
->timeperframe
= a
->parm
.output
.timeperframe
;
900 isp_video_reqbufs(struct file
*file
, void *fh
, struct v4l2_requestbuffers
*rb
)
902 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
903 struct isp_video
*video
= video_drvdata(file
);
906 mutex_lock(&video
->queue_lock
);
907 ret
= vb2_reqbufs(&vfh
->queue
, rb
);
908 mutex_unlock(&video
->queue_lock
);
914 isp_video_querybuf(struct file
*file
, void *fh
, struct v4l2_buffer
*b
)
916 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
917 struct isp_video
*video
= video_drvdata(file
);
920 mutex_lock(&video
->queue_lock
);
921 ret
= vb2_querybuf(&vfh
->queue
, b
);
922 mutex_unlock(&video
->queue_lock
);
928 isp_video_qbuf(struct file
*file
, void *fh
, struct v4l2_buffer
*b
)
930 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
931 struct isp_video
*video
= video_drvdata(file
);
934 mutex_lock(&video
->queue_lock
);
935 ret
= vb2_qbuf(&vfh
->queue
, video
->video
.v4l2_dev
->mdev
, b
);
936 mutex_unlock(&video
->queue_lock
);
942 isp_video_dqbuf(struct file
*file
, void *fh
, struct v4l2_buffer
*b
)
944 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
945 struct isp_video
*video
= video_drvdata(file
);
948 mutex_lock(&video
->queue_lock
);
949 ret
= vb2_dqbuf(&vfh
->queue
, b
, file
->f_flags
& O_NONBLOCK
);
950 mutex_unlock(&video
->queue_lock
);
955 static int isp_video_check_external_subdevs(struct isp_video
*video
,
956 struct isp_pipeline
*pipe
)
958 struct isp_device
*isp
= video
->isp
;
959 struct media_entity
*ents
[] = {
960 &isp
->isp_csi2a
.subdev
.entity
,
961 &isp
->isp_csi2c
.subdev
.entity
,
962 &isp
->isp_ccp2
.subdev
.entity
,
963 &isp
->isp_ccdc
.subdev
.entity
965 struct media_pad
*source_pad
;
966 struct media_entity
*source
= NULL
;
967 struct media_entity
*sink
;
968 struct v4l2_subdev_format fmt
;
969 struct v4l2_ext_controls ctrls
;
970 struct v4l2_ext_control ctrl
;
974 /* Memory-to-memory pipelines have no external subdev. */
975 if (pipe
->input
!= NULL
)
978 for (i
= 0; i
< ARRAY_SIZE(ents
); i
++) {
979 /* Is the entity part of the pipeline? */
980 if (!media_entity_enum_test(&pipe
->ent_enum
, ents
[i
]))
983 /* ISP entities have always sink pad == 0. Find source. */
984 source_pad
= media_entity_remote_pad(&ents
[i
]->pads
[0]);
985 if (source_pad
== NULL
)
988 source
= source_pad
->entity
;
994 dev_warn(isp
->dev
, "can't find source, failing now\n");
998 if (!is_media_entity_v4l2_subdev(source
))
1001 pipe
->external
= media_entity_to_v4l2_subdev(source
);
1003 fmt
.pad
= source_pad
->index
;
1004 fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
1005 ret
= v4l2_subdev_call(media_entity_to_v4l2_subdev(sink
),
1006 pad
, get_fmt
, NULL
, &fmt
);
1007 if (unlikely(ret
< 0)) {
1008 dev_warn(isp
->dev
, "get_fmt returned null!\n");
1012 pipe
->external_width
=
1013 omap3isp_video_format_info(fmt
.format
.code
)->width
;
1015 memset(&ctrls
, 0, sizeof(ctrls
));
1016 memset(&ctrl
, 0, sizeof(ctrl
));
1018 ctrl
.id
= V4L2_CID_PIXEL_RATE
;
1021 ctrls
.controls
= &ctrl
;
1022 ret
= v4l2_g_ext_ctrls(pipe
->external
->ctrl_handler
, &video
->video
,
1025 dev_warn(isp
->dev
, "no pixel rate control in subdev %s\n",
1026 pipe
->external
->name
);
1030 pipe
->external_rate
= ctrl
.value64
;
1032 if (media_entity_enum_test(&pipe
->ent_enum
,
1033 &isp
->isp_ccdc
.subdev
.entity
)) {
1034 unsigned int rate
= UINT_MAX
;
1036 * Check that maximum allowed CCDC pixel rate isn't
1037 * exceeded by the pixel rate.
1039 omap3isp_ccdc_max_rate(&isp
->isp_ccdc
, &rate
);
1040 if (pipe
->external_rate
> rate
)
1050 * Every ISP pipeline has a single input and a single output. The input can be
1051 * either a sensor or a video node. The output is always a video node.
1053 * As every pipeline has an output video node, the ISP video objects at the
1054 * pipeline output stores the pipeline state. It tracks the streaming state of
1055 * both the input and output, as well as the availability of buffers.
1057 * In sensor-to-memory mode, frames are always available at the pipeline input.
1058 * Starting the sensor usually requires I2C transfers and must be done in
1059 * interruptible context. The pipeline is started and stopped synchronously
1060 * to the stream on/off commands. All modules in the pipeline will get their
1061 * subdev set stream handler called. The module at the end of the pipeline must
1062 * delay starting the hardware until buffers are available at its output.
1064 * In memory-to-memory mode, starting/stopping the stream requires
1065 * synchronization between the input and output. ISP modules can't be stopped
1066 * in the middle of a frame, and at least some of the modules seem to become
1067 * busy as soon as they're started, even if they don't receive a frame start
1068 * event. For that reason frames need to be processed in single-shot mode. The
1069 * driver needs to wait until a frame is completely processed and written to
1070 * memory before restarting the pipeline for the next frame. Pipelined
1071 * processing might be possible but requires more testing.
1073 * Stream start must be delayed until buffers are available at both the input
1074 * and output. The pipeline must be started in the videobuf queue callback with
1075 * the buffers queue spinlock held. The modules subdev set stream operation must
1079 isp_video_streamon(struct file
*file
, void *fh
, enum v4l2_buf_type type
)
1081 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
1082 struct isp_video
*video
= video_drvdata(file
);
1083 enum isp_pipeline_state state
;
1084 struct isp_pipeline
*pipe
;
1085 unsigned long flags
;
1088 if (type
!= video
->type
)
1091 mutex_lock(&video
->stream_lock
);
1093 /* Start streaming on the pipeline. No link touching an entity in the
1094 * pipeline can be activated or deactivated once streaming is started.
1096 pipe
= video
->video
.entity
.pipe
1097 ? to_isp_pipeline(&video
->video
.entity
) : &video
->pipe
;
1099 ret
= media_entity_enum_init(&pipe
->ent_enum
, &video
->isp
->media_dev
);
1103 /* TODO: Implement PM QoS */
1104 pipe
->l3_ick
= clk_get_rate(video
->isp
->clock
[ISP_CLK_L3_ICK
]);
1105 pipe
->max_rate
= pipe
->l3_ick
;
1107 ret
= media_pipeline_start(&video
->video
.entity
, &pipe
->pipe
);
1109 goto err_pipeline_start
;
1111 /* Verify that the currently configured format matches the output of
1112 * the connected subdev.
1114 ret
= isp_video_check_format(video
, vfh
);
1116 goto err_check_format
;
1118 video
->bpl_padding
= ret
;
1119 video
->bpl_value
= vfh
->format
.fmt
.pix
.bytesperline
;
1121 ret
= isp_video_get_graph_data(video
, pipe
);
1123 goto err_check_format
;
1125 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1126 state
= ISP_PIPELINE_STREAM_OUTPUT
| ISP_PIPELINE_IDLE_OUTPUT
;
1128 state
= ISP_PIPELINE_STREAM_INPUT
| ISP_PIPELINE_IDLE_INPUT
;
1130 ret
= isp_video_check_external_subdevs(video
, pipe
);
1132 goto err_check_format
;
1134 pipe
->error
= false;
1136 spin_lock_irqsave(&pipe
->lock
, flags
);
1137 pipe
->state
&= ~ISP_PIPELINE_STREAM
;
1138 pipe
->state
|= state
;
1139 spin_unlock_irqrestore(&pipe
->lock
, flags
);
1141 /* Set the maximum time per frame as the value requested by userspace.
1142 * This is a soft limit that can be overridden if the hardware doesn't
1143 * support the request limit.
1145 if (video
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
)
1146 pipe
->max_timeperframe
= vfh
->timeperframe
;
1148 video
->queue
= &vfh
->queue
;
1149 INIT_LIST_HEAD(&video
->dmaqueue
);
1150 atomic_set(&pipe
->frame_number
, -1);
1151 pipe
->field
= vfh
->format
.fmt
.pix
.field
;
1153 mutex_lock(&video
->queue_lock
);
1154 ret
= vb2_streamon(&vfh
->queue
, type
);
1155 mutex_unlock(&video
->queue_lock
);
1157 goto err_check_format
;
1159 mutex_unlock(&video
->stream_lock
);
1164 media_pipeline_stop(&video
->video
.entity
);
1166 /* TODO: Implement PM QoS */
1167 /* The DMA queue must be emptied here, otherwise CCDC interrupts that
1168 * will get triggered the next time the CCDC is powered up will try to
1169 * access buffers that might have been freed but still present in the
1170 * DMA queue. This can easily get triggered if the above
1171 * omap3isp_pipeline_set_stream() call fails on a system with a
1172 * free-running sensor.
1174 INIT_LIST_HEAD(&video
->dmaqueue
);
1175 video
->queue
= NULL
;
1177 media_entity_enum_cleanup(&pipe
->ent_enum
);
1180 mutex_unlock(&video
->stream_lock
);
1186 isp_video_streamoff(struct file
*file
, void *fh
, enum v4l2_buf_type type
)
1188 struct isp_video_fh
*vfh
= to_isp_video_fh(fh
);
1189 struct isp_video
*video
= video_drvdata(file
);
1190 struct isp_pipeline
*pipe
= to_isp_pipeline(&video
->video
.entity
);
1191 enum isp_pipeline_state state
;
1192 unsigned int streaming
;
1193 unsigned long flags
;
1195 if (type
!= video
->type
)
1198 mutex_lock(&video
->stream_lock
);
1200 /* Make sure we're not streaming yet. */
1201 mutex_lock(&video
->queue_lock
);
1202 streaming
= vb2_is_streaming(&vfh
->queue
);
1203 mutex_unlock(&video
->queue_lock
);
1208 /* Update the pipeline state. */
1209 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1210 state
= ISP_PIPELINE_STREAM_OUTPUT
1211 | ISP_PIPELINE_QUEUE_OUTPUT
;
1213 state
= ISP_PIPELINE_STREAM_INPUT
1214 | ISP_PIPELINE_QUEUE_INPUT
;
1216 spin_lock_irqsave(&pipe
->lock
, flags
);
1217 pipe
->state
&= ~state
;
1218 spin_unlock_irqrestore(&pipe
->lock
, flags
);
1220 /* Stop the stream. */
1221 omap3isp_pipeline_set_stream(pipe
, ISP_PIPELINE_STREAM_STOPPED
);
1222 omap3isp_video_cancel_stream(video
);
1224 mutex_lock(&video
->queue_lock
);
1225 vb2_streamoff(&vfh
->queue
, type
);
1226 mutex_unlock(&video
->queue_lock
);
1227 video
->queue
= NULL
;
1228 video
->error
= false;
1230 /* TODO: Implement PM QoS */
1231 media_pipeline_stop(&video
->video
.entity
);
1233 media_entity_enum_cleanup(&pipe
->ent_enum
);
1236 mutex_unlock(&video
->stream_lock
);
1241 isp_video_enum_input(struct file
*file
, void *fh
, struct v4l2_input
*input
)
1243 if (input
->index
> 0)
1246 strscpy(input
->name
, "camera", sizeof(input
->name
));
1247 input
->type
= V4L2_INPUT_TYPE_CAMERA
;
1253 isp_video_g_input(struct file
*file
, void *fh
, unsigned int *input
)
1261 isp_video_s_input(struct file
*file
, void *fh
, unsigned int input
)
1263 return input
== 0 ? 0 : -EINVAL
;
1266 static const struct v4l2_ioctl_ops isp_video_ioctl_ops
= {
1267 .vidioc_querycap
= isp_video_querycap
,
1268 .vidioc_g_fmt_vid_cap
= isp_video_get_format
,
1269 .vidioc_s_fmt_vid_cap
= isp_video_set_format
,
1270 .vidioc_try_fmt_vid_cap
= isp_video_try_format
,
1271 .vidioc_g_fmt_vid_out
= isp_video_get_format
,
1272 .vidioc_s_fmt_vid_out
= isp_video_set_format
,
1273 .vidioc_try_fmt_vid_out
= isp_video_try_format
,
1274 .vidioc_g_selection
= isp_video_get_selection
,
1275 .vidioc_s_selection
= isp_video_set_selection
,
1276 .vidioc_g_parm
= isp_video_get_param
,
1277 .vidioc_s_parm
= isp_video_set_param
,
1278 .vidioc_reqbufs
= isp_video_reqbufs
,
1279 .vidioc_querybuf
= isp_video_querybuf
,
1280 .vidioc_qbuf
= isp_video_qbuf
,
1281 .vidioc_dqbuf
= isp_video_dqbuf
,
1282 .vidioc_streamon
= isp_video_streamon
,
1283 .vidioc_streamoff
= isp_video_streamoff
,
1284 .vidioc_enum_input
= isp_video_enum_input
,
1285 .vidioc_g_input
= isp_video_g_input
,
1286 .vidioc_s_input
= isp_video_s_input
,
1289 /* -----------------------------------------------------------------------------
1290 * V4L2 file operations
1293 static int isp_video_open(struct file
*file
)
1295 struct isp_video
*video
= video_drvdata(file
);
1296 struct isp_video_fh
*handle
;
1297 struct vb2_queue
*queue
;
1300 handle
= kzalloc(sizeof(*handle
), GFP_KERNEL
);
1304 v4l2_fh_init(&handle
->vfh
, &video
->video
);
1305 v4l2_fh_add(&handle
->vfh
);
1307 /* If this is the first user, initialise the pipeline. */
1308 if (omap3isp_get(video
->isp
) == NULL
) {
1313 ret
= v4l2_pipeline_pm_get(&video
->video
.entity
);
1315 omap3isp_put(video
->isp
);
1319 queue
= &handle
->queue
;
1320 queue
->type
= video
->type
;
1321 queue
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1322 queue
->drv_priv
= handle
;
1323 queue
->ops
= &isp_video_queue_ops
;
1324 queue
->mem_ops
= &vb2_dma_contig_memops
;
1325 queue
->buf_struct_size
= sizeof(struct isp_buffer
);
1326 queue
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1327 queue
->dev
= video
->isp
->dev
;
1329 ret
= vb2_queue_init(&handle
->queue
);
1331 omap3isp_put(video
->isp
);
1335 memset(&handle
->format
, 0, sizeof(handle
->format
));
1336 handle
->format
.type
= video
->type
;
1337 handle
->timeperframe
.denominator
= 1;
1339 handle
->video
= video
;
1340 file
->private_data
= &handle
->vfh
;
1344 v4l2_fh_del(&handle
->vfh
);
1345 v4l2_fh_exit(&handle
->vfh
);
1352 static int isp_video_release(struct file
*file
)
1354 struct isp_video
*video
= video_drvdata(file
);
1355 struct v4l2_fh
*vfh
= file
->private_data
;
1356 struct isp_video_fh
*handle
= to_isp_video_fh(vfh
);
1358 /* Disable streaming and free the buffers queue resources. */
1359 isp_video_streamoff(file
, vfh
, video
->type
);
1361 mutex_lock(&video
->queue_lock
);
1362 vb2_queue_release(&handle
->queue
);
1363 mutex_unlock(&video
->queue_lock
);
1365 v4l2_pipeline_pm_put(&video
->video
.entity
);
1367 /* Release the file handle. */
1371 file
->private_data
= NULL
;
1373 omap3isp_put(video
->isp
);
1378 static __poll_t
isp_video_poll(struct file
*file
, poll_table
*wait
)
1380 struct isp_video_fh
*vfh
= to_isp_video_fh(file
->private_data
);
1381 struct isp_video
*video
= video_drvdata(file
);
1384 mutex_lock(&video
->queue_lock
);
1385 ret
= vb2_poll(&vfh
->queue
, file
, wait
);
1386 mutex_unlock(&video
->queue_lock
);
1391 static int isp_video_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1393 struct isp_video_fh
*vfh
= to_isp_video_fh(file
->private_data
);
1395 return vb2_mmap(&vfh
->queue
, vma
);
1398 static const struct v4l2_file_operations isp_video_fops
= {
1399 .owner
= THIS_MODULE
,
1400 .unlocked_ioctl
= video_ioctl2
,
1401 .open
= isp_video_open
,
1402 .release
= isp_video_release
,
1403 .poll
= isp_video_poll
,
1404 .mmap
= isp_video_mmap
,
1407 /* -----------------------------------------------------------------------------
1411 static const struct isp_video_operations isp_video_dummy_ops
= {
1414 int omap3isp_video_init(struct isp_video
*video
, const char *name
)
1416 const char *direction
;
1419 switch (video
->type
) {
1420 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1421 direction
= "output";
1422 video
->pad
.flags
= MEDIA_PAD_FL_SINK
1423 | MEDIA_PAD_FL_MUST_CONNECT
;
1425 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
1426 direction
= "input";
1427 video
->pad
.flags
= MEDIA_PAD_FL_SOURCE
1428 | MEDIA_PAD_FL_MUST_CONNECT
;
1429 video
->video
.vfl_dir
= VFL_DIR_TX
;
1436 ret
= media_entity_pads_init(&video
->video
.entity
, 1, &video
->pad
);
1440 mutex_init(&video
->mutex
);
1441 atomic_set(&video
->active
, 0);
1443 spin_lock_init(&video
->pipe
.lock
);
1444 mutex_init(&video
->stream_lock
);
1445 mutex_init(&video
->queue_lock
);
1446 spin_lock_init(&video
->irqlock
);
1448 /* Initialize the video device. */
1449 if (video
->ops
== NULL
)
1450 video
->ops
= &isp_video_dummy_ops
;
1452 video
->video
.fops
= &isp_video_fops
;
1453 snprintf(video
->video
.name
, sizeof(video
->video
.name
),
1454 "OMAP3 ISP %s %s", name
, direction
);
1455 video
->video
.vfl_type
= VFL_TYPE_VIDEO
;
1456 video
->video
.release
= video_device_release_empty
;
1457 video
->video
.ioctl_ops
= &isp_video_ioctl_ops
;
1458 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1459 video
->video
.device_caps
= V4L2_CAP_VIDEO_CAPTURE
1460 | V4L2_CAP_STREAMING
;
1462 video
->video
.device_caps
= V4L2_CAP_VIDEO_OUTPUT
1463 | V4L2_CAP_STREAMING
;
1465 video
->pipe
.stream_state
= ISP_PIPELINE_STREAM_STOPPED
;
1467 video_set_drvdata(&video
->video
, video
);
1472 void omap3isp_video_cleanup(struct isp_video
*video
)
1474 media_entity_cleanup(&video
->video
.entity
);
1475 mutex_destroy(&video
->queue_lock
);
1476 mutex_destroy(&video
->stream_lock
);
1477 mutex_destroy(&video
->mutex
);
1480 int omap3isp_video_register(struct isp_video
*video
, struct v4l2_device
*vdev
)
1484 video
->video
.v4l2_dev
= vdev
;
1486 ret
= video_register_device(&video
->video
, VFL_TYPE_VIDEO
, -1);
1488 dev_err(video
->isp
->dev
,
1489 "%s: could not register video device (%d)\n",
1495 void omap3isp_video_unregister(struct isp_video
*video
)
1497 video_unregister_device(&video
->video
);