2 * vsp1_video.c -- R-Car VSP1 Video Node
4 * Copyright (C) 2013 Renesas 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/sched.h>
18 #include <linux/slab.h>
19 #include <linux/v4l2-mediabus.h>
20 #include <linux/videodev2.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-core.h>
28 #include <media/videobuf2-dma-contig.h>
31 #include "vsp1_entity.h"
32 #include "vsp1_rwpf.h"
33 #include "vsp1_video.h"
35 #define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
36 #define VSP1_VIDEO_DEF_WIDTH 1024
37 #define VSP1_VIDEO_DEF_HEIGHT 768
39 #define VSP1_VIDEO_MIN_WIDTH 2U
40 #define VSP1_VIDEO_MAX_WIDTH 8190U
41 #define VSP1_VIDEO_MIN_HEIGHT 2U
42 #define VSP1_VIDEO_MAX_HEIGHT 8190U
44 /* -----------------------------------------------------------------------------
48 static const struct vsp1_format_info vsp1_video_formats[] = {
49 { V4L2_PIX_FMT_RGB332, V4L2_MBUS_FMT_ARGB8888_1X32,
50 VI6_FMT_RGB_332, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
51 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
52 1, { 8, 0, 0 }, false, false, 1, 1 },
53 { V4L2_PIX_FMT_RGB444, V4L2_MBUS_FMT_ARGB8888_1X32,
54 VI6_FMT_XRGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
56 1, { 16, 0, 0 }, false, false, 1, 1 },
57 { V4L2_PIX_FMT_RGB555, V4L2_MBUS_FMT_ARGB8888_1X32,
58 VI6_FMT_XRGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
60 1, { 16, 0, 0 }, false, false, 1, 1 },
61 { V4L2_PIX_FMT_RGB565, V4L2_MBUS_FMT_ARGB8888_1X32,
62 VI6_FMT_RGB_565, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
64 1, { 16, 0, 0 }, false, false, 1, 1 },
65 { V4L2_PIX_FMT_BGR24, V4L2_MBUS_FMT_ARGB8888_1X32,
66 VI6_FMT_BGR_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
67 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
68 1, { 24, 0, 0 }, false, false, 1, 1 },
69 { V4L2_PIX_FMT_RGB24, V4L2_MBUS_FMT_ARGB8888_1X32,
70 VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
71 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
72 1, { 24, 0, 0 }, false, false, 1, 1 },
73 { V4L2_PIX_FMT_BGR32, V4L2_MBUS_FMT_ARGB8888_1X32,
74 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
75 1, { 32, 0, 0 }, false, false, 1, 1 },
76 { V4L2_PIX_FMT_RGB32, V4L2_MBUS_FMT_ARGB8888_1X32,
77 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
78 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
79 1, { 32, 0, 0 }, false, false, 1, 1 },
80 { V4L2_PIX_FMT_UYVY, V4L2_MBUS_FMT_AYUV8_1X32,
81 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
82 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
83 1, { 16, 0, 0 }, false, false, 2, 1 },
84 { V4L2_PIX_FMT_VYUY, V4L2_MBUS_FMT_AYUV8_1X32,
85 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
86 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
87 1, { 16, 0, 0 }, false, true, 2, 1 },
88 { V4L2_PIX_FMT_YUYV, V4L2_MBUS_FMT_AYUV8_1X32,
89 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
90 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
91 1, { 16, 0, 0 }, true, false, 2, 1 },
92 { V4L2_PIX_FMT_YVYU, V4L2_MBUS_FMT_AYUV8_1X32,
93 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
94 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
95 1, { 16, 0, 0 }, true, true, 2, 1 },
96 { V4L2_PIX_FMT_NV12M, V4L2_MBUS_FMT_AYUV8_1X32,
97 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
98 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
99 2, { 8, 16, 0 }, false, false, 2, 2 },
100 { V4L2_PIX_FMT_NV21M, V4L2_MBUS_FMT_AYUV8_1X32,
101 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
102 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
103 2, { 8, 16, 0 }, false, true, 2, 2 },
104 { V4L2_PIX_FMT_NV16M, V4L2_MBUS_FMT_AYUV8_1X32,
105 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
106 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
107 2, { 8, 16, 0 }, false, false, 2, 1 },
108 { V4L2_PIX_FMT_NV61M, V4L2_MBUS_FMT_AYUV8_1X32,
109 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
110 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
111 2, { 8, 16, 0 }, false, true, 2, 1 },
112 { V4L2_PIX_FMT_YUV420M, V4L2_MBUS_FMT_AYUV8_1X32,
113 VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
114 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
115 3, { 8, 8, 8 }, false, false, 2, 2 },
119 * vsp1_get_format_info - Retrieve format information for a 4CC
120 * @fourcc: the format 4CC
122 * Return a pointer to the format information structure corresponding to the
123 * given V4L2 format 4CC, or NULL if no corresponding format can be found.
125 static const struct vsp1_format_info *vsp1_get_format_info(u32 fourcc)
129 for (i = 0; i < ARRAY_SIZE(vsp1_video_formats); ++i) {
130 const struct vsp1_format_info *info = &vsp1_video_formats[i];
132 if (info->fourcc == fourcc)
140 static struct v4l2_subdev *
141 vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
143 struct media_pad *remote;
145 remote = media_entity_remote_pad(local);
146 if (remote == NULL ||
147 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
151 *pad = remote->index;
153 return media_entity_to_v4l2_subdev(remote->entity);
156 static int vsp1_video_verify_format(struct vsp1_video *video)
158 struct v4l2_subdev_format fmt;
159 struct v4l2_subdev *subdev;
162 subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
166 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
167 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
169 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
171 if (video->fmtinfo->mbus != fmt.format.code ||
172 video->format.height != fmt.format.height ||
173 video->format.width != fmt.format.width)
179 static int __vsp1_video_try_format(struct vsp1_video *video,
180 struct v4l2_pix_format_mplane *pix,
181 const struct vsp1_format_info **fmtinfo)
183 const struct vsp1_format_info *info;
184 unsigned int width = pix->width;
185 unsigned int height = pix->height;
188 /* Retrieve format information and select the default format if the
189 * requested format isn't supported.
191 info = vsp1_get_format_info(pix->pixelformat);
193 info = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
195 pix->pixelformat = info->fourcc;
196 pix->colorspace = V4L2_COLORSPACE_SRGB;
197 pix->field = V4L2_FIELD_NONE;
198 memset(pix->reserved, 0, sizeof(pix->reserved));
200 /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
201 width = round_down(width, info->hsub);
202 height = round_down(height, info->vsub);
204 /* Clamp the width and height. */
205 pix->width = clamp(width, VSP1_VIDEO_MIN_WIDTH, VSP1_VIDEO_MAX_WIDTH);
206 pix->height = clamp(height, VSP1_VIDEO_MIN_HEIGHT,
207 VSP1_VIDEO_MAX_HEIGHT);
209 /* Compute and clamp the stride and image size. While not documented in
210 * the datasheet, strides not aligned to a multiple of 128 bytes result
211 * in image corruption.
213 for (i = 0; i < max(info->planes, 2U); ++i) {
214 unsigned int hsub = i > 0 ? info->hsub : 1;
215 unsigned int vsub = i > 0 ? info->vsub : 1;
216 unsigned int align = 128;
219 bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
220 pix->width / hsub * info->bpp[i] / 8,
221 round_down(65535U, align));
223 pix->plane_fmt[i].bytesperline = round_up(bpl, align);
224 pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
225 * pix->height / vsub;
228 if (info->planes == 3) {
229 /* The second and third planes must have the same stride. */
230 pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
231 pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
234 pix->num_planes = info->planes;
243 vsp1_video_format_adjust(struct vsp1_video *video,
244 const struct v4l2_pix_format_mplane *format,
245 struct v4l2_pix_format_mplane *adjust)
250 __vsp1_video_try_format(video, adjust, NULL);
252 if (format->width != adjust->width ||
253 format->height != adjust->height ||
254 format->pixelformat != adjust->pixelformat ||
255 format->num_planes != adjust->num_planes)
258 for (i = 0; i < format->num_planes; ++i) {
259 if (format->plane_fmt[i].bytesperline !=
260 adjust->plane_fmt[i].bytesperline)
263 adjust->plane_fmt[i].sizeimage =
264 max(adjust->plane_fmt[i].sizeimage,
265 format->plane_fmt[i].sizeimage);
271 /* -----------------------------------------------------------------------------
272 * Pipeline Management
275 static int vsp1_pipeline_validate_branch(struct vsp1_rwpf *input,
276 struct vsp1_rwpf *output)
278 struct vsp1_entity *entity;
279 unsigned int entities = 0;
280 struct media_pad *pad;
281 bool uds_found = false;
283 pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
289 /* We've reached a video node, that shouldn't have happened. */
290 if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
293 entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity));
295 /* We've reached the WPF, we're done. */
296 if (entity->type == VSP1_ENTITY_WPF)
299 /* Ensure the branch has no loop. */
300 if (entities & (1 << entity->subdev.entity.id))
303 entities |= 1 << entity->subdev.entity.id;
305 /* UDS can't be chained. */
306 if (entity->type == VSP1_ENTITY_UDS) {
312 /* Follow the source link. The link setup operations ensure
313 * that the output fan-out can't be more than one, there is thus
314 * no need to verify here that only a single source link is
317 pad = &entity->pads[entity->source_pad];
318 pad = media_entity_remote_pad(pad);
321 /* The last entity must be the output WPF. */
322 if (entity != &output->entity)
328 static int vsp1_pipeline_validate(struct vsp1_pipeline *pipe,
329 struct vsp1_video *video)
331 struct media_entity_graph graph;
332 struct media_entity *entity = &video->video.entity;
333 struct media_device *mdev = entity->parent;
337 mutex_lock(&mdev->graph_mutex);
339 /* Walk the graph to locate the entities and video nodes. */
340 media_entity_graph_walk_start(&graph, entity);
342 while ((entity = media_entity_graph_walk_next(&graph))) {
343 struct v4l2_subdev *subdev;
344 struct vsp1_rwpf *rwpf;
345 struct vsp1_entity *e;
347 if (media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV) {
352 subdev = media_entity_to_v4l2_subdev(entity);
353 e = to_vsp1_entity(subdev);
354 list_add_tail(&e->list_pipe, &pipe->entities);
356 if (e->type == VSP1_ENTITY_RPF) {
357 rwpf = to_rwpf(subdev);
358 pipe->inputs[pipe->num_inputs++] = rwpf;
359 rwpf->video.pipe_index = pipe->num_inputs;
360 } else if (e->type == VSP1_ENTITY_WPF) {
361 rwpf = to_rwpf(subdev);
362 pipe->output = to_rwpf(subdev);
363 rwpf->video.pipe_index = 0;
364 } else if (e->type == VSP1_ENTITY_LIF) {
369 mutex_unlock(&mdev->graph_mutex);
371 /* We need one output and at least one input. */
372 if (pipe->num_inputs == 0 || !pipe->output) {
377 /* Follow links downstream for each input and make sure the graph
378 * contains no loop and that all branches end at the output WPF.
380 for (i = 0; i < pipe->num_inputs; ++i) {
381 ret = vsp1_pipeline_validate_branch(pipe->inputs[i],
390 INIT_LIST_HEAD(&pipe->entities);
391 pipe->buffers_ready = 0;
393 pipe->num_inputs = 0;
399 static int vsp1_pipeline_init(struct vsp1_pipeline *pipe,
400 struct vsp1_video *video)
404 mutex_lock(&pipe->lock);
406 /* If we're the first user validate and initialize the pipeline. */
407 if (pipe->use_count == 0) {
408 ret = vsp1_pipeline_validate(pipe, video);
417 mutex_unlock(&pipe->lock);
421 static void vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
423 mutex_lock(&pipe->lock);
425 /* If we're the last user clean up the pipeline. */
426 if (--pipe->use_count == 0) {
427 INIT_LIST_HEAD(&pipe->entities);
428 pipe->state = VSP1_PIPELINE_STOPPED;
429 pipe->buffers_ready = 0;
431 pipe->num_inputs = 0;
436 mutex_unlock(&pipe->lock);
439 static void vsp1_pipeline_run(struct vsp1_pipeline *pipe)
441 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
443 vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index), VI6_CMD_STRCMD);
444 pipe->state = VSP1_PIPELINE_RUNNING;
445 pipe->buffers_ready = 0;
448 static int vsp1_pipeline_stop(struct vsp1_pipeline *pipe)
450 struct vsp1_entity *entity;
454 spin_lock_irqsave(&pipe->irqlock, flags);
455 pipe->state = VSP1_PIPELINE_STOPPING;
456 spin_unlock_irqrestore(&pipe->irqlock, flags);
458 ret = wait_event_timeout(pipe->wq, pipe->state == VSP1_PIPELINE_STOPPED,
459 msecs_to_jiffies(500));
460 ret = ret == 0 ? -ETIMEDOUT : 0;
462 list_for_each_entry(entity, &pipe->entities, list_pipe) {
464 vsp1_write(entity->vsp1, entity->route,
465 VI6_DPR_NODE_UNUSED);
467 v4l2_subdev_call(&entity->subdev, video, s_stream, 0);
473 static bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe)
477 mask = ((1 << pipe->num_inputs) - 1) << 1;
481 return pipe->buffers_ready == mask;
485 * vsp1_video_complete_buffer - Complete the current buffer
486 * @video: the video node
488 * This function completes the current buffer by filling its sequence number,
489 * time stamp and payload size, and hands it back to the videobuf core.
491 * When operating in DU output mode (deep pipeline to the DU through the LIF),
492 * the VSP1 needs to constantly supply frames to the display. In that case, if
493 * no other buffer is queued, reuse the one that has just been processed instead
494 * of handing it back to the videobuf core.
496 * Return the next queued buffer or NULL if the queue is empty.
498 static struct vsp1_video_buffer *
499 vsp1_video_complete_buffer(struct vsp1_video *video)
501 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
502 struct vsp1_video_buffer *next = NULL;
503 struct vsp1_video_buffer *done;
507 spin_lock_irqsave(&video->irqlock, flags);
509 if (list_empty(&video->irqqueue)) {
510 spin_unlock_irqrestore(&video->irqlock, flags);
514 done = list_first_entry(&video->irqqueue,
515 struct vsp1_video_buffer, queue);
517 /* In DU output mode reuse the buffer if the list is singular. */
518 if (pipe->lif && list_is_singular(&video->irqqueue)) {
519 spin_unlock_irqrestore(&video->irqlock, flags);
523 list_del(&done->queue);
525 if (!list_empty(&video->irqqueue))
526 next = list_first_entry(&video->irqqueue,
527 struct vsp1_video_buffer, queue);
529 spin_unlock_irqrestore(&video->irqlock, flags);
531 done->buf.v4l2_buf.sequence = video->sequence++;
532 v4l2_get_timestamp(&done->buf.v4l2_buf.timestamp);
533 for (i = 0; i < done->buf.num_planes; ++i)
534 vb2_set_plane_payload(&done->buf, i, done->length[i]);
535 vb2_buffer_done(&done->buf, VB2_BUF_STATE_DONE);
540 static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
541 struct vsp1_video *video)
543 struct vsp1_video_buffer *buf;
546 buf = vsp1_video_complete_buffer(video);
550 spin_lock_irqsave(&pipe->irqlock, flags);
552 video->ops->queue(video, buf);
553 pipe->buffers_ready |= 1 << video->pipe_index;
555 spin_unlock_irqrestore(&pipe->irqlock, flags);
558 void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
566 /* Complete buffers on all video nodes. */
567 for (i = 0; i < pipe->num_inputs; ++i)
568 vsp1_video_frame_end(pipe, &pipe->inputs[i]->video);
571 vsp1_video_frame_end(pipe, &pipe->output->video);
573 spin_lock_irqsave(&pipe->irqlock, flags);
575 /* If a stop has been requested, mark the pipeline as stopped and
578 if (pipe->state == VSP1_PIPELINE_STOPPING) {
579 pipe->state = VSP1_PIPELINE_STOPPED;
584 /* Restart the pipeline if ready. */
585 if (vsp1_pipeline_ready(pipe))
586 vsp1_pipeline_run(pipe);
589 spin_unlock_irqrestore(&pipe->irqlock, flags);
592 /* -----------------------------------------------------------------------------
593 * videobuf2 Queue Operations
597 vsp1_video_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
598 unsigned int *nbuffers, unsigned int *nplanes,
599 unsigned int sizes[], void *alloc_ctxs[])
601 struct vsp1_video *video = vb2_get_drv_priv(vq);
602 const struct v4l2_pix_format_mplane *format;
603 struct v4l2_pix_format_mplane pix_mp;
607 /* Make sure the format is valid and adjust the sizeimage field
610 if (!vsp1_video_format_adjust(video, &fmt->fmt.pix_mp, &pix_mp))
615 format = &video->format;
618 *nplanes = format->num_planes;
620 for (i = 0; i < format->num_planes; ++i) {
621 sizes[i] = format->plane_fmt[i].sizeimage;
622 alloc_ctxs[i] = video->alloc_ctx;
628 static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
630 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
631 struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
632 const struct v4l2_pix_format_mplane *format = &video->format;
635 if (vb->num_planes < format->num_planes)
640 for (i = 0; i < vb->num_planes; ++i) {
641 buf->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
642 buf->length[i] = vb2_plane_size(vb, i);
644 if (buf->length[i] < format->plane_fmt[i].sizeimage)
651 static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
653 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
654 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
655 struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
659 spin_lock_irqsave(&video->irqlock, flags);
660 empty = list_empty(&video->irqqueue);
661 list_add_tail(&buf->queue, &video->irqqueue);
662 spin_unlock_irqrestore(&video->irqlock, flags);
667 spin_lock_irqsave(&pipe->irqlock, flags);
669 video->ops->queue(video, buf);
670 pipe->buffers_ready |= 1 << video->pipe_index;
672 if (vb2_is_streaming(&video->queue) &&
673 vsp1_pipeline_ready(pipe))
674 vsp1_pipeline_run(pipe);
676 spin_unlock_irqrestore(&pipe->irqlock, flags);
679 static void vsp1_entity_route_setup(struct vsp1_entity *source)
681 struct vsp1_entity *sink;
683 if (source->route == 0)
686 sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
687 vsp1_write(source->vsp1, source->route, sink->id);
690 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
692 struct vsp1_video *video = vb2_get_drv_priv(vq);
693 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
694 struct vsp1_entity *entity;
698 mutex_lock(&pipe->lock);
699 if (pipe->stream_count == pipe->num_video - 1) {
700 list_for_each_entry(entity, &pipe->entities, list_pipe) {
701 vsp1_entity_route_setup(entity);
703 ret = v4l2_subdev_call(&entity->subdev, video,
706 mutex_unlock(&pipe->lock);
712 pipe->stream_count++;
713 mutex_unlock(&pipe->lock);
715 spin_lock_irqsave(&pipe->irqlock, flags);
716 if (vsp1_pipeline_ready(pipe))
717 vsp1_pipeline_run(pipe);
718 spin_unlock_irqrestore(&pipe->irqlock, flags);
723 static int vsp1_video_stop_streaming(struct vb2_queue *vq)
725 struct vsp1_video *video = vb2_get_drv_priv(vq);
726 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
730 mutex_lock(&pipe->lock);
731 if (--pipe->stream_count == 0) {
732 /* Stop the pipeline. */
733 ret = vsp1_pipeline_stop(pipe);
734 if (ret == -ETIMEDOUT)
735 dev_err(video->vsp1->dev, "pipeline stop timeout\n");
737 mutex_unlock(&pipe->lock);
739 vsp1_pipeline_cleanup(pipe);
740 media_entity_pipeline_stop(&video->video.entity);
742 /* Remove all buffers from the IRQ queue. */
743 spin_lock_irqsave(&video->irqlock, flags);
744 INIT_LIST_HEAD(&video->irqqueue);
745 spin_unlock_irqrestore(&video->irqlock, flags);
750 static struct vb2_ops vsp1_video_queue_qops = {
751 .queue_setup = vsp1_video_queue_setup,
752 .buf_prepare = vsp1_video_buffer_prepare,
753 .buf_queue = vsp1_video_buffer_queue,
754 .wait_prepare = vb2_ops_wait_prepare,
755 .wait_finish = vb2_ops_wait_finish,
756 .start_streaming = vsp1_video_start_streaming,
757 .stop_streaming = vsp1_video_stop_streaming,
760 /* -----------------------------------------------------------------------------
765 vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
767 struct v4l2_fh *vfh = file->private_data;
768 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
770 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
771 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
772 | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
774 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
775 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
776 | V4L2_CAP_STREAMING;
778 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
779 | V4L2_CAP_STREAMING;
781 strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
782 strlcpy(cap->card, video->video.name, sizeof(cap->card));
783 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
784 dev_name(video->vsp1->dev));
790 vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
792 struct v4l2_fh *vfh = file->private_data;
793 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
795 if (format->type != video->queue.type)
798 mutex_lock(&video->lock);
799 format->fmt.pix_mp = video->format;
800 mutex_unlock(&video->lock);
806 vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
808 struct v4l2_fh *vfh = file->private_data;
809 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
811 if (format->type != video->queue.type)
814 return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
818 vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
820 struct v4l2_fh *vfh = file->private_data;
821 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
822 const struct vsp1_format_info *info;
825 if (format->type != video->queue.type)
828 ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
832 mutex_lock(&video->lock);
834 if (vb2_is_busy(&video->queue)) {
839 video->format = format->fmt.pix_mp;
840 video->fmtinfo = info;
843 mutex_unlock(&video->lock);
848 vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
850 struct v4l2_fh *vfh = file->private_data;
851 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
852 struct vsp1_pipeline *pipe;
855 if (video->queue.owner && video->queue.owner != file->private_data)
860 /* Start streaming on the pipeline. No link touching an entity in the
861 * pipeline can be activated or deactivated once streaming is started.
863 * Use the VSP1 pipeline object embedded in the first video object that
866 pipe = video->video.entity.pipe
867 ? to_vsp1_pipeline(&video->video.entity) : &video->pipe;
869 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
873 /* Verify that the configured format matches the output of the connected
876 ret = vsp1_video_verify_format(video);
880 ret = vsp1_pipeline_init(pipe, video);
884 /* Start the queue. */
885 ret = vb2_streamon(&video->queue, type);
892 vsp1_pipeline_cleanup(pipe);
894 media_entity_pipeline_stop(&video->video.entity);
898 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
899 .vidioc_querycap = vsp1_video_querycap,
900 .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
901 .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
902 .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
903 .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
904 .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
905 .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
906 .vidioc_reqbufs = vb2_ioctl_reqbufs,
907 .vidioc_querybuf = vb2_ioctl_querybuf,
908 .vidioc_qbuf = vb2_ioctl_qbuf,
909 .vidioc_dqbuf = vb2_ioctl_dqbuf,
910 .vidioc_create_bufs = vb2_ioctl_create_bufs,
911 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
912 .vidioc_streamon = vsp1_video_streamon,
913 .vidioc_streamoff = vb2_ioctl_streamoff,
916 /* -----------------------------------------------------------------------------
917 * V4L2 File Operations
920 static int vsp1_video_open(struct file *file)
922 struct vsp1_video *video = video_drvdata(file);
926 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
930 v4l2_fh_init(vfh, &video->video);
933 file->private_data = vfh;
935 if (!vsp1_device_get(video->vsp1)) {
944 static int vsp1_video_release(struct file *file)
946 struct vsp1_video *video = video_drvdata(file);
947 struct v4l2_fh *vfh = file->private_data;
949 mutex_lock(&video->lock);
950 if (video->queue.owner == vfh) {
951 vb2_queue_release(&video->queue);
952 video->queue.owner = NULL;
954 mutex_unlock(&video->lock);
956 vsp1_device_put(video->vsp1);
958 v4l2_fh_release(file);
960 file->private_data = NULL;
965 static struct v4l2_file_operations vsp1_video_fops = {
966 .owner = THIS_MODULE,
967 .unlocked_ioctl = video_ioctl2,
968 .open = vsp1_video_open,
969 .release = vsp1_video_release,
970 .poll = vb2_fop_poll,
971 .mmap = vb2_fop_mmap,
974 /* -----------------------------------------------------------------------------
975 * Initialization and Cleanup
978 int vsp1_video_init(struct vsp1_video *video, struct vsp1_entity *rwpf)
980 const char *direction;
983 switch (video->type) {
984 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
985 direction = "output";
986 video->pad.flags = MEDIA_PAD_FL_SINK;
989 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
991 video->pad.flags = MEDIA_PAD_FL_SOURCE;
992 video->video.vfl_dir = VFL_DIR_TX;
1001 mutex_init(&video->lock);
1002 spin_lock_init(&video->irqlock);
1003 INIT_LIST_HEAD(&video->irqqueue);
1005 mutex_init(&video->pipe.lock);
1006 spin_lock_init(&video->pipe.irqlock);
1007 INIT_LIST_HEAD(&video->pipe.entities);
1008 init_waitqueue_head(&video->pipe.wq);
1009 video->pipe.state = VSP1_PIPELINE_STOPPED;
1011 /* Initialize the media entity... */
1012 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1016 /* ... and the format ... */
1017 video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
1018 video->format.pixelformat = video->fmtinfo->fourcc;
1019 video->format.colorspace = V4L2_COLORSPACE_SRGB;
1020 video->format.field = V4L2_FIELD_NONE;
1021 video->format.width = VSP1_VIDEO_DEF_WIDTH;
1022 video->format.height = VSP1_VIDEO_DEF_HEIGHT;
1023 video->format.num_planes = 1;
1024 video->format.plane_fmt[0].bytesperline =
1025 video->format.width * video->fmtinfo->bpp[0] / 8;
1026 video->format.plane_fmt[0].sizeimage =
1027 video->format.plane_fmt[0].bytesperline * video->format.height;
1029 /* ... and the video node... */
1030 video->video.v4l2_dev = &video->vsp1->v4l2_dev;
1031 video->video.fops = &vsp1_video_fops;
1032 snprintf(video->video.name, sizeof(video->video.name), "%s %s",
1033 rwpf->subdev.name, direction);
1034 video->video.vfl_type = VFL_TYPE_GRABBER;
1035 video->video.release = video_device_release_empty;
1036 video->video.ioctl_ops = &vsp1_video_ioctl_ops;
1038 video_set_drvdata(&video->video, video);
1040 /* ... and the buffers queue... */
1041 video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
1042 if (IS_ERR(video->alloc_ctx)) {
1043 ret = PTR_ERR(video->alloc_ctx);
1047 video->queue.type = video->type;
1048 video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1049 video->queue.lock = &video->lock;
1050 video->queue.drv_priv = video;
1051 video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer);
1052 video->queue.ops = &vsp1_video_queue_qops;
1053 video->queue.mem_ops = &vb2_dma_contig_memops;
1054 video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1055 ret = vb2_queue_init(&video->queue);
1057 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1061 /* ... and register the video device. */
1062 video->video.queue = &video->queue;
1063 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1065 dev_err(video->vsp1->dev, "failed to register video device\n");
1072 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1073 vsp1_video_cleanup(video);
1077 void vsp1_video_cleanup(struct vsp1_video *video)
1079 if (video_is_registered(&video->video))
1080 video_unregister_device(&video->video);
1082 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1083 media_entity_cleanup(&video->video.entity);