2 * vsp1_video.c -- R-Car VSP1 Video Node
4 * Copyright (C) 2013-2015 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/v4l2-mediabus.h>
19 #include <linux/videodev2.h>
20 #include <linux/wait.h>
22 #include <media/media-entity.h>
23 #include <media/v4l2-dev.h>
24 #include <media/v4l2-fh.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-subdev.h>
27 #include <media/videobuf2-v4l2.h>
28 #include <media/videobuf2-dma-contig.h>
33 #include "vsp1_entity.h"
34 #include "vsp1_pipe.h"
35 #include "vsp1_rwpf.h"
37 #include "vsp1_video.h"
39 #define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
40 #define VSP1_VIDEO_DEF_WIDTH 1024
41 #define VSP1_VIDEO_DEF_HEIGHT 768
43 #define VSP1_VIDEO_MIN_WIDTH 2U
44 #define VSP1_VIDEO_MAX_WIDTH 8190U
45 #define VSP1_VIDEO_MIN_HEIGHT 2U
46 #define VSP1_VIDEO_MAX_HEIGHT 8190U
48 /* -----------------------------------------------------------------------------
52 static struct v4l2_subdev
*
53 vsp1_video_remote_subdev(struct media_pad
*local
, u32
*pad
)
55 struct media_pad
*remote
;
57 remote
= media_entity_remote_pad(local
);
58 if (!remote
|| !is_media_entity_v4l2_subdev(remote
->entity
))
64 return media_entity_to_v4l2_subdev(remote
->entity
);
67 static int vsp1_video_verify_format(struct vsp1_video
*video
)
69 struct v4l2_subdev_format fmt
;
70 struct v4l2_subdev
*subdev
;
73 subdev
= vsp1_video_remote_subdev(&video
->pad
, &fmt
.pad
);
77 fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
78 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &fmt
);
80 return ret
== -ENOIOCTLCMD
? -EINVAL
: ret
;
82 if (video
->rwpf
->fmtinfo
->mbus
!= fmt
.format
.code
||
83 video
->rwpf
->format
.height
!= fmt
.format
.height
||
84 video
->rwpf
->format
.width
!= fmt
.format
.width
)
90 static int __vsp1_video_try_format(struct vsp1_video
*video
,
91 struct v4l2_pix_format_mplane
*pix
,
92 const struct vsp1_format_info
**fmtinfo
)
94 static const u32 xrgb_formats
[][2] = {
95 { V4L2_PIX_FMT_RGB444
, V4L2_PIX_FMT_XRGB444
},
96 { V4L2_PIX_FMT_RGB555
, V4L2_PIX_FMT_XRGB555
},
97 { V4L2_PIX_FMT_BGR32
, V4L2_PIX_FMT_XBGR32
},
98 { V4L2_PIX_FMT_RGB32
, V4L2_PIX_FMT_XRGB32
},
101 const struct vsp1_format_info
*info
;
102 unsigned int width
= pix
->width
;
103 unsigned int height
= pix
->height
;
106 /* Backward compatibility: replace deprecated RGB formats by their XRGB
107 * equivalent. This selects the format older userspace applications want
108 * while still exposing the new format.
110 for (i
= 0; i
< ARRAY_SIZE(xrgb_formats
); ++i
) {
111 if (xrgb_formats
[i
][0] == pix
->pixelformat
) {
112 pix
->pixelformat
= xrgb_formats
[i
][1];
117 /* Retrieve format information and select the default format if the
118 * requested format isn't supported.
120 info
= vsp1_get_format_info(pix
->pixelformat
);
122 info
= vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT
);
124 pix
->pixelformat
= info
->fourcc
;
125 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
126 pix
->field
= V4L2_FIELD_NONE
;
127 memset(pix
->reserved
, 0, sizeof(pix
->reserved
));
129 /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
130 width
= round_down(width
, info
->hsub
);
131 height
= round_down(height
, info
->vsub
);
133 /* Clamp the width and height. */
134 pix
->width
= clamp(width
, VSP1_VIDEO_MIN_WIDTH
, VSP1_VIDEO_MAX_WIDTH
);
135 pix
->height
= clamp(height
, VSP1_VIDEO_MIN_HEIGHT
,
136 VSP1_VIDEO_MAX_HEIGHT
);
138 /* Compute and clamp the stride and image size. While not documented in
139 * the datasheet, strides not aligned to a multiple of 128 bytes result
140 * in image corruption.
142 for (i
= 0; i
< min(info
->planes
, 2U); ++i
) {
143 unsigned int hsub
= i
> 0 ? info
->hsub
: 1;
144 unsigned int vsub
= i
> 0 ? info
->vsub
: 1;
145 unsigned int align
= 128;
148 bpl
= clamp_t(unsigned int, pix
->plane_fmt
[i
].bytesperline
,
149 pix
->width
/ hsub
* info
->bpp
[i
] / 8,
150 round_down(65535U, align
));
152 pix
->plane_fmt
[i
].bytesperline
= round_up(bpl
, align
);
153 pix
->plane_fmt
[i
].sizeimage
= pix
->plane_fmt
[i
].bytesperline
154 * pix
->height
/ vsub
;
157 if (info
->planes
== 3) {
158 /* The second and third planes must have the same stride. */
159 pix
->plane_fmt
[2].bytesperline
= pix
->plane_fmt
[1].bytesperline
;
160 pix
->plane_fmt
[2].sizeimage
= pix
->plane_fmt
[1].sizeimage
;
163 pix
->num_planes
= info
->planes
;
171 /* -----------------------------------------------------------------------------
172 * Pipeline Management
176 * vsp1_video_complete_buffer - Complete the current buffer
177 * @video: the video node
179 * This function completes the current buffer by filling its sequence number,
180 * time stamp and payload size, and hands it back to the videobuf core.
182 * When operating in DU output mode (deep pipeline to the DU through the LIF),
183 * the VSP1 needs to constantly supply frames to the display. In that case, if
184 * no other buffer is queued, reuse the one that has just been processed instead
185 * of handing it back to the videobuf core.
187 * Return the next queued buffer or NULL if the queue is empty.
189 static struct vsp1_vb2_buffer
*
190 vsp1_video_complete_buffer(struct vsp1_video
*video
)
192 struct vsp1_pipeline
*pipe
= video
->rwpf
->pipe
;
193 struct vsp1_vb2_buffer
*next
= NULL
;
194 struct vsp1_vb2_buffer
*done
;
198 spin_lock_irqsave(&video
->irqlock
, flags
);
200 if (list_empty(&video
->irqqueue
)) {
201 spin_unlock_irqrestore(&video
->irqlock
, flags
);
205 done
= list_first_entry(&video
->irqqueue
,
206 struct vsp1_vb2_buffer
, queue
);
208 /* In DU output mode reuse the buffer if the list is singular. */
209 if (pipe
->lif
&& list_is_singular(&video
->irqqueue
)) {
210 spin_unlock_irqrestore(&video
->irqlock
, flags
);
214 list_del(&done
->queue
);
216 if (!list_empty(&video
->irqqueue
))
217 next
= list_first_entry(&video
->irqqueue
,
218 struct vsp1_vb2_buffer
, queue
);
220 spin_unlock_irqrestore(&video
->irqlock
, flags
);
222 done
->buf
.sequence
= video
->sequence
++;
223 done
->buf
.vb2_buf
.timestamp
= ktime_get_ns();
224 for (i
= 0; i
< done
->buf
.vb2_buf
.num_planes
; ++i
)
225 vb2_set_plane_payload(&done
->buf
.vb2_buf
, i
,
226 vb2_plane_size(&done
->buf
.vb2_buf
, i
));
227 vb2_buffer_done(&done
->buf
.vb2_buf
, VB2_BUF_STATE_DONE
);
232 static void vsp1_video_frame_end(struct vsp1_pipeline
*pipe
,
233 struct vsp1_rwpf
*rwpf
)
235 struct vsp1_video
*video
= rwpf
->video
;
236 struct vsp1_vb2_buffer
*buf
;
239 buf
= vsp1_video_complete_buffer(video
);
243 spin_lock_irqsave(&pipe
->irqlock
, flags
);
245 video
->rwpf
->mem
= buf
->mem
;
246 pipe
->buffers_ready
|= 1 << video
->pipe_index
;
248 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
251 static void vsp1_video_pipeline_run(struct vsp1_pipeline
*pipe
)
253 struct vsp1_device
*vsp1
= pipe
->output
->entity
.vsp1
;
257 pipe
->dl
= vsp1_dl_list_get(pipe
->output
->dlm
);
259 for (i
= 0; i
< vsp1
->info
->rpf_count
; ++i
) {
260 struct vsp1_rwpf
*rwpf
= pipe
->inputs
[i
];
263 vsp1_rwpf_set_memory(rwpf
, pipe
->dl
);
267 vsp1_rwpf_set_memory(pipe
->output
, pipe
->dl
);
269 vsp1_dl_list_commit(pipe
->dl
);
272 vsp1_pipeline_run(pipe
);
275 static void vsp1_video_pipeline_frame_end(struct vsp1_pipeline
*pipe
)
277 struct vsp1_device
*vsp1
= pipe
->output
->entity
.vsp1
;
278 enum vsp1_pipeline_state state
;
282 /* Complete buffers on all video nodes. */
283 for (i
= 0; i
< vsp1
->info
->rpf_count
; ++i
) {
284 if (!pipe
->inputs
[i
])
287 vsp1_video_frame_end(pipe
, pipe
->inputs
[i
]);
290 vsp1_video_frame_end(pipe
, pipe
->output
);
292 spin_lock_irqsave(&pipe
->irqlock
, flags
);
295 pipe
->state
= VSP1_PIPELINE_STOPPED
;
297 /* If a stop has been requested, mark the pipeline as stopped and
298 * return. Otherwise restart the pipeline if ready.
300 if (state
== VSP1_PIPELINE_STOPPING
)
302 else if (vsp1_pipeline_ready(pipe
))
303 vsp1_video_pipeline_run(pipe
);
305 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
308 static int vsp1_video_pipeline_build_branch(struct vsp1_pipeline
*pipe
,
309 struct vsp1_rwpf
*input
,
310 struct vsp1_rwpf
*output
)
312 struct media_entity_enum ent_enum
;
313 struct vsp1_entity
*entity
;
314 struct media_pad
*pad
;
315 bool bru_found
= false;
318 ret
= media_entity_enum_init(&ent_enum
, &input
->entity
.vsp1
->media_dev
);
322 pad
= media_entity_remote_pad(&input
->entity
.pads
[RWPF_PAD_SOURCE
]);
330 /* We've reached a video node, that shouldn't have happened. */
331 if (!is_media_entity_v4l2_subdev(pad
->entity
)) {
336 entity
= to_vsp1_entity(
337 media_entity_to_v4l2_subdev(pad
->entity
));
339 /* A BRU is present in the pipeline, store the BRU input pad
340 * number in the input RPF for use when configuring the RPF.
342 if (entity
->type
== VSP1_ENTITY_BRU
) {
343 struct vsp1_bru
*bru
= to_bru(&entity
->subdev
);
345 bru
->inputs
[pad
->index
].rpf
= input
;
346 input
->bru_input
= pad
->index
;
351 /* We've reached the WPF, we're done. */
352 if (entity
->type
== VSP1_ENTITY_WPF
)
355 /* Ensure the branch has no loop. */
356 if (media_entity_enum_test_and_set(&ent_enum
,
357 &entity
->subdev
.entity
)) {
362 /* UDS can't be chained. */
363 if (entity
->type
== VSP1_ENTITY_UDS
) {
370 pipe
->uds_input
= bru_found
? pipe
->bru
374 /* Follow the source link. The link setup operations ensure
375 * that the output fan-out can't be more than one, there is thus
376 * no need to verify here that only a single source link is
379 pad
= &entity
->pads
[entity
->source_pad
];
380 pad
= media_entity_remote_pad(pad
);
383 /* The last entity must be the output WPF. */
384 if (entity
!= &output
->entity
)
388 media_entity_enum_cleanup(&ent_enum
);
393 static int vsp1_video_pipeline_build(struct vsp1_pipeline
*pipe
,
394 struct vsp1_video
*video
)
396 struct media_entity_graph graph
;
397 struct media_entity
*entity
= &video
->video
.entity
;
398 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
402 mutex_lock(&mdev
->graph_mutex
);
404 /* Walk the graph to locate the entities and video nodes. */
405 ret
= media_entity_graph_walk_init(&graph
, mdev
);
407 mutex_unlock(&mdev
->graph_mutex
);
411 media_entity_graph_walk_start(&graph
, entity
);
413 while ((entity
= media_entity_graph_walk_next(&graph
))) {
414 struct v4l2_subdev
*subdev
;
415 struct vsp1_rwpf
*rwpf
;
416 struct vsp1_entity
*e
;
418 if (!is_media_entity_v4l2_subdev(entity
))
421 subdev
= media_entity_to_v4l2_subdev(entity
);
422 e
= to_vsp1_entity(subdev
);
423 list_add_tail(&e
->list_pipe
, &pipe
->entities
);
425 if (e
->type
== VSP1_ENTITY_RPF
) {
426 rwpf
= to_rwpf(subdev
);
427 pipe
->inputs
[rwpf
->entity
.index
] = rwpf
;
428 rwpf
->video
->pipe_index
= ++pipe
->num_inputs
;
430 } else if (e
->type
== VSP1_ENTITY_WPF
) {
431 rwpf
= to_rwpf(subdev
);
433 rwpf
->video
->pipe_index
= 0;
435 } else if (e
->type
== VSP1_ENTITY_LIF
) {
437 } else if (e
->type
== VSP1_ENTITY_BRU
) {
442 mutex_unlock(&mdev
->graph_mutex
);
444 media_entity_graph_walk_cleanup(&graph
);
446 /* We need one output and at least one input. */
447 if (pipe
->num_inputs
== 0 || !pipe
->output
) {
452 /* Follow links downstream for each input and make sure the graph
453 * contains no loop and that all branches end at the output WPF.
455 for (i
= 0; i
< video
->vsp1
->info
->rpf_count
; ++i
) {
456 if (!pipe
->inputs
[i
])
459 ret
= vsp1_video_pipeline_build_branch(pipe
, pipe
->inputs
[i
],
468 vsp1_pipeline_reset(pipe
);
472 static int vsp1_video_pipeline_init(struct vsp1_pipeline
*pipe
,
473 struct vsp1_video
*video
)
477 mutex_lock(&pipe
->lock
);
479 /* If we're the first user build and validate the pipeline. */
480 if (pipe
->use_count
== 0) {
481 ret
= vsp1_video_pipeline_build(pipe
, video
);
490 mutex_unlock(&pipe
->lock
);
494 static void vsp1_video_pipeline_cleanup(struct vsp1_pipeline
*pipe
)
496 mutex_lock(&pipe
->lock
);
498 /* If we're the last user clean up the pipeline. */
499 if (--pipe
->use_count
== 0)
500 vsp1_pipeline_reset(pipe
);
502 mutex_unlock(&pipe
->lock
);
505 /* -----------------------------------------------------------------------------
506 * videobuf2 Queue Operations
510 vsp1_video_queue_setup(struct vb2_queue
*vq
,
511 unsigned int *nbuffers
, unsigned int *nplanes
,
512 unsigned int sizes
[], void *alloc_ctxs
[])
514 struct vsp1_video
*video
= vb2_get_drv_priv(vq
);
515 const struct v4l2_pix_format_mplane
*format
= &video
->rwpf
->format
;
519 if (*nplanes
!= format
->num_planes
)
522 for (i
= 0; i
< *nplanes
; i
++) {
523 if (sizes
[i
] < format
->plane_fmt
[i
].sizeimage
)
525 alloc_ctxs
[i
] = video
->alloc_ctx
;
530 *nplanes
= format
->num_planes
;
532 for (i
= 0; i
< format
->num_planes
; ++i
) {
533 sizes
[i
] = format
->plane_fmt
[i
].sizeimage
;
534 alloc_ctxs
[i
] = video
->alloc_ctx
;
540 static int vsp1_video_buffer_prepare(struct vb2_buffer
*vb
)
542 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
543 struct vsp1_video
*video
= vb2_get_drv_priv(vb
->vb2_queue
);
544 struct vsp1_vb2_buffer
*buf
= to_vsp1_vb2_buffer(vbuf
);
545 const struct v4l2_pix_format_mplane
*format
= &video
->rwpf
->format
;
548 if (vb
->num_planes
< format
->num_planes
)
551 for (i
= 0; i
< vb
->num_planes
; ++i
) {
552 buf
->mem
.addr
[i
] = vb2_dma_contig_plane_dma_addr(vb
, i
);
554 if (vb2_plane_size(vb
, i
) < format
->plane_fmt
[i
].sizeimage
)
559 buf
->mem
.addr
[i
] = 0;
564 static void vsp1_video_buffer_queue(struct vb2_buffer
*vb
)
566 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
567 struct vsp1_video
*video
= vb2_get_drv_priv(vb
->vb2_queue
);
568 struct vsp1_pipeline
*pipe
= video
->rwpf
->pipe
;
569 struct vsp1_vb2_buffer
*buf
= to_vsp1_vb2_buffer(vbuf
);
573 spin_lock_irqsave(&video
->irqlock
, flags
);
574 empty
= list_empty(&video
->irqqueue
);
575 list_add_tail(&buf
->queue
, &video
->irqqueue
);
576 spin_unlock_irqrestore(&video
->irqlock
, flags
);
581 spin_lock_irqsave(&pipe
->irqlock
, flags
);
583 video
->rwpf
->mem
= buf
->mem
;
584 pipe
->buffers_ready
|= 1 << video
->pipe_index
;
586 if (vb2_is_streaming(&video
->queue
) &&
587 vsp1_pipeline_ready(pipe
))
588 vsp1_video_pipeline_run(pipe
);
590 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
593 static int vsp1_video_setup_pipeline(struct vsp1_pipeline
*pipe
)
595 struct vsp1_entity
*entity
;
597 /* Prepare the display list. */
598 pipe
->dl
= vsp1_dl_list_get(pipe
->output
->dlm
);
603 struct vsp1_uds
*uds
= to_uds(&pipe
->uds
->subdev
);
605 /* If a BRU is present in the pipeline before the UDS, the alpha
606 * component doesn't need to be scaled as the BRU output alpha
607 * value is fixed to 255. Otherwise we need to scale the alpha
608 * component only when available at the input RPF.
610 if (pipe
->uds_input
->type
== VSP1_ENTITY_BRU
) {
611 uds
->scale_alpha
= false;
613 struct vsp1_rwpf
*rpf
=
614 to_rwpf(&pipe
->uds_input
->subdev
);
616 uds
->scale_alpha
= rpf
->fmtinfo
->alpha
;
620 list_for_each_entry(entity
, &pipe
->entities
, list_pipe
) {
621 vsp1_entity_route_setup(entity
, pipe
->dl
);
623 if (entity
->ops
->configure
)
624 entity
->ops
->configure(entity
, pipe
, pipe
->dl
);
630 static int vsp1_video_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
632 struct vsp1_video
*video
= vb2_get_drv_priv(vq
);
633 struct vsp1_pipeline
*pipe
= video
->rwpf
->pipe
;
637 mutex_lock(&pipe
->lock
);
638 if (pipe
->stream_count
== pipe
->num_inputs
) {
639 ret
= vsp1_video_setup_pipeline(pipe
);
641 mutex_unlock(&pipe
->lock
);
646 pipe
->stream_count
++;
647 mutex_unlock(&pipe
->lock
);
649 spin_lock_irqsave(&pipe
->irqlock
, flags
);
650 if (vsp1_pipeline_ready(pipe
))
651 vsp1_video_pipeline_run(pipe
);
652 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
657 static void vsp1_video_stop_streaming(struct vb2_queue
*vq
)
659 struct vsp1_video
*video
= vb2_get_drv_priv(vq
);
660 struct vsp1_pipeline
*pipe
= video
->rwpf
->pipe
;
661 struct vsp1_vb2_buffer
*buffer
;
665 mutex_lock(&pipe
->lock
);
666 if (--pipe
->stream_count
== 0) {
667 /* Stop the pipeline. */
668 ret
= vsp1_pipeline_stop(pipe
);
669 if (ret
== -ETIMEDOUT
)
670 dev_err(video
->vsp1
->dev
, "pipeline stop timeout\n");
672 vsp1_dl_list_put(pipe
->dl
);
675 mutex_unlock(&pipe
->lock
);
677 vsp1_video_pipeline_cleanup(pipe
);
678 media_entity_pipeline_stop(&video
->video
.entity
);
680 /* Remove all buffers from the IRQ queue. */
681 spin_lock_irqsave(&video
->irqlock
, flags
);
682 list_for_each_entry(buffer
, &video
->irqqueue
, queue
)
683 vb2_buffer_done(&buffer
->buf
.vb2_buf
, VB2_BUF_STATE_ERROR
);
684 INIT_LIST_HEAD(&video
->irqqueue
);
685 spin_unlock_irqrestore(&video
->irqlock
, flags
);
688 static struct vb2_ops vsp1_video_queue_qops
= {
689 .queue_setup
= vsp1_video_queue_setup
,
690 .buf_prepare
= vsp1_video_buffer_prepare
,
691 .buf_queue
= vsp1_video_buffer_queue
,
692 .wait_prepare
= vb2_ops_wait_prepare
,
693 .wait_finish
= vb2_ops_wait_finish
,
694 .start_streaming
= vsp1_video_start_streaming
,
695 .stop_streaming
= vsp1_video_stop_streaming
,
698 /* -----------------------------------------------------------------------------
703 vsp1_video_querycap(struct file
*file
, void *fh
, struct v4l2_capability
*cap
)
705 struct v4l2_fh
*vfh
= file
->private_data
;
706 struct vsp1_video
*video
= to_vsp1_video(vfh
->vdev
);
708 cap
->capabilities
= V4L2_CAP_DEVICE_CAPS
| V4L2_CAP_STREAMING
709 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
710 | V4L2_CAP_VIDEO_OUTPUT_MPLANE
;
712 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
713 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE_MPLANE
714 | V4L2_CAP_STREAMING
;
716 cap
->device_caps
= V4L2_CAP_VIDEO_OUTPUT_MPLANE
717 | V4L2_CAP_STREAMING
;
719 strlcpy(cap
->driver
, "vsp1", sizeof(cap
->driver
));
720 strlcpy(cap
->card
, video
->video
.name
, sizeof(cap
->card
));
721 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
722 dev_name(video
->vsp1
->dev
));
728 vsp1_video_get_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
730 struct v4l2_fh
*vfh
= file
->private_data
;
731 struct vsp1_video
*video
= to_vsp1_video(vfh
->vdev
);
733 if (format
->type
!= video
->queue
.type
)
736 mutex_lock(&video
->lock
);
737 format
->fmt
.pix_mp
= video
->rwpf
->format
;
738 mutex_unlock(&video
->lock
);
744 vsp1_video_try_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
746 struct v4l2_fh
*vfh
= file
->private_data
;
747 struct vsp1_video
*video
= to_vsp1_video(vfh
->vdev
);
749 if (format
->type
!= video
->queue
.type
)
752 return __vsp1_video_try_format(video
, &format
->fmt
.pix_mp
, NULL
);
756 vsp1_video_set_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
758 struct v4l2_fh
*vfh
= file
->private_data
;
759 struct vsp1_video
*video
= to_vsp1_video(vfh
->vdev
);
760 const struct vsp1_format_info
*info
;
763 if (format
->type
!= video
->queue
.type
)
766 ret
= __vsp1_video_try_format(video
, &format
->fmt
.pix_mp
, &info
);
770 mutex_lock(&video
->lock
);
772 if (vb2_is_busy(&video
->queue
)) {
777 video
->rwpf
->format
= format
->fmt
.pix_mp
;
778 video
->rwpf
->fmtinfo
= info
;
781 mutex_unlock(&video
->lock
);
786 vsp1_video_streamon(struct file
*file
, void *fh
, enum v4l2_buf_type type
)
788 struct v4l2_fh
*vfh
= file
->private_data
;
789 struct vsp1_video
*video
= to_vsp1_video(vfh
->vdev
);
790 struct vsp1_pipeline
*pipe
;
793 if (video
->queue
.owner
&& video
->queue
.owner
!= file
->private_data
)
798 /* Start streaming on the pipeline. No link touching an entity in the
799 * pipeline can be activated or deactivated once streaming is started.
801 * Use the VSP1 pipeline object embedded in the first video object that
804 * FIXME: This is racy, the ioctl is only protected by the video node
807 pipe
= video
->rwpf
->pipe
? video
->rwpf
->pipe
: &video
->pipe
;
809 ret
= media_entity_pipeline_start(&video
->video
.entity
, &pipe
->pipe
);
813 /* Verify that the configured format matches the output of the connected
816 ret
= vsp1_video_verify_format(video
);
820 ret
= vsp1_video_pipeline_init(pipe
, video
);
824 /* Start the queue. */
825 ret
= vb2_streamon(&video
->queue
, type
);
832 vsp1_video_pipeline_cleanup(pipe
);
834 media_entity_pipeline_stop(&video
->video
.entity
);
838 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops
= {
839 .vidioc_querycap
= vsp1_video_querycap
,
840 .vidioc_g_fmt_vid_cap_mplane
= vsp1_video_get_format
,
841 .vidioc_s_fmt_vid_cap_mplane
= vsp1_video_set_format
,
842 .vidioc_try_fmt_vid_cap_mplane
= vsp1_video_try_format
,
843 .vidioc_g_fmt_vid_out_mplane
= vsp1_video_get_format
,
844 .vidioc_s_fmt_vid_out_mplane
= vsp1_video_set_format
,
845 .vidioc_try_fmt_vid_out_mplane
= vsp1_video_try_format
,
846 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
847 .vidioc_querybuf
= vb2_ioctl_querybuf
,
848 .vidioc_qbuf
= vb2_ioctl_qbuf
,
849 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
850 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
851 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
852 .vidioc_streamon
= vsp1_video_streamon
,
853 .vidioc_streamoff
= vb2_ioctl_streamoff
,
856 /* -----------------------------------------------------------------------------
857 * V4L2 File Operations
860 static int vsp1_video_open(struct file
*file
)
862 struct vsp1_video
*video
= video_drvdata(file
);
866 vfh
= kzalloc(sizeof(*vfh
), GFP_KERNEL
);
870 v4l2_fh_init(vfh
, &video
->video
);
873 file
->private_data
= vfh
;
875 ret
= vsp1_device_get(video
->vsp1
);
884 static int vsp1_video_release(struct file
*file
)
886 struct vsp1_video
*video
= video_drvdata(file
);
887 struct v4l2_fh
*vfh
= file
->private_data
;
889 mutex_lock(&video
->lock
);
890 if (video
->queue
.owner
== vfh
) {
891 vb2_queue_release(&video
->queue
);
892 video
->queue
.owner
= NULL
;
894 mutex_unlock(&video
->lock
);
896 vsp1_device_put(video
->vsp1
);
898 v4l2_fh_release(file
);
900 file
->private_data
= NULL
;
905 static struct v4l2_file_operations vsp1_video_fops
= {
906 .owner
= THIS_MODULE
,
907 .unlocked_ioctl
= video_ioctl2
,
908 .open
= vsp1_video_open
,
909 .release
= vsp1_video_release
,
910 .poll
= vb2_fop_poll
,
911 .mmap
= vb2_fop_mmap
,
914 /* -----------------------------------------------------------------------------
915 * Initialization and Cleanup
918 struct vsp1_video
*vsp1_video_create(struct vsp1_device
*vsp1
,
919 struct vsp1_rwpf
*rwpf
)
921 struct vsp1_video
*video
;
922 const char *direction
;
925 video
= devm_kzalloc(vsp1
->dev
, sizeof(*video
), GFP_KERNEL
);
927 return ERR_PTR(-ENOMEM
);
934 if (rwpf
->entity
.type
== VSP1_ENTITY_RPF
) {
936 video
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
937 video
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
938 video
->video
.vfl_dir
= VFL_DIR_TX
;
940 direction
= "output";
941 video
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
942 video
->pad
.flags
= MEDIA_PAD_FL_SINK
;
943 video
->video
.vfl_dir
= VFL_DIR_RX
;
946 mutex_init(&video
->lock
);
947 spin_lock_init(&video
->irqlock
);
948 INIT_LIST_HEAD(&video
->irqqueue
);
950 vsp1_pipeline_init(&video
->pipe
);
951 video
->pipe
.frame_end
= vsp1_video_pipeline_frame_end
;
953 /* Initialize the media entity... */
954 ret
= media_entity_pads_init(&video
->video
.entity
, 1, &video
->pad
);
958 /* ... and the format ... */
959 rwpf
->format
.pixelformat
= VSP1_VIDEO_DEF_FORMAT
;
960 rwpf
->format
.width
= VSP1_VIDEO_DEF_WIDTH
;
961 rwpf
->format
.height
= VSP1_VIDEO_DEF_HEIGHT
;
962 __vsp1_video_try_format(video
, &rwpf
->format
, &rwpf
->fmtinfo
);
964 /* ... and the video node... */
965 video
->video
.v4l2_dev
= &video
->vsp1
->v4l2_dev
;
966 video
->video
.fops
= &vsp1_video_fops
;
967 snprintf(video
->video
.name
, sizeof(video
->video
.name
), "%s %s",
968 rwpf
->entity
.subdev
.name
, direction
);
969 video
->video
.vfl_type
= VFL_TYPE_GRABBER
;
970 video
->video
.release
= video_device_release_empty
;
971 video
->video
.ioctl_ops
= &vsp1_video_ioctl_ops
;
973 video_set_drvdata(&video
->video
, video
);
975 /* ... and the buffers queue... */
976 video
->alloc_ctx
= vb2_dma_contig_init_ctx(video
->vsp1
->dev
);
977 if (IS_ERR(video
->alloc_ctx
)) {
978 ret
= PTR_ERR(video
->alloc_ctx
);
982 video
->queue
.type
= video
->type
;
983 video
->queue
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
984 video
->queue
.lock
= &video
->lock
;
985 video
->queue
.drv_priv
= video
;
986 video
->queue
.buf_struct_size
= sizeof(struct vsp1_vb2_buffer
);
987 video
->queue
.ops
= &vsp1_video_queue_qops
;
988 video
->queue
.mem_ops
= &vb2_dma_contig_memops
;
989 video
->queue
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
990 ret
= vb2_queue_init(&video
->queue
);
992 dev_err(video
->vsp1
->dev
, "failed to initialize vb2 queue\n");
996 /* ... and register the video device. */
997 video
->video
.queue
= &video
->queue
;
998 ret
= video_register_device(&video
->video
, VFL_TYPE_GRABBER
, -1);
1000 dev_err(video
->vsp1
->dev
, "failed to register video device\n");
1007 vb2_dma_contig_cleanup_ctx(video
->alloc_ctx
);
1008 vsp1_video_cleanup(video
);
1009 return ERR_PTR(ret
);
1012 void vsp1_video_cleanup(struct vsp1_video
*video
)
1014 if (video_is_registered(&video
->video
))
1015 video_unregister_device(&video
->video
);
1017 vb2_dma_contig_cleanup_ctx(video
->alloc_ctx
);
1018 media_entity_cleanup(&video
->video
.entity
);