2 * vsp1_video.c -- R-Car VSP1 Video Node
4 * Copyright (C) 2013-2014 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/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>
32 #include "vsp1_entity.h"
33 #include "vsp1_rwpf.h"
34 #include "vsp1_video.h"
36 #define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
37 #define VSP1_VIDEO_DEF_WIDTH 1024
38 #define VSP1_VIDEO_DEF_HEIGHT 768
40 #define VSP1_VIDEO_MIN_WIDTH 2U
41 #define VSP1_VIDEO_MAX_WIDTH 8190U
42 #define VSP1_VIDEO_MIN_HEIGHT 2U
43 #define VSP1_VIDEO_MAX_HEIGHT 8190U
45 /* -----------------------------------------------------------------------------
49 static const struct vsp1_format_info vsp1_video_formats[] = {
50 { V4L2_PIX_FMT_RGB332, V4L2_MBUS_FMT_ARGB8888_1X32,
51 VI6_FMT_RGB_332, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
52 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
53 1, { 8, 0, 0 }, false, false, 1, 1 },
54 { V4L2_PIX_FMT_RGB444, V4L2_MBUS_FMT_ARGB8888_1X32,
55 VI6_FMT_XRGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
57 1, { 16, 0, 0 }, false, false, 1, 1 },
58 { V4L2_PIX_FMT_RGB555, V4L2_MBUS_FMT_ARGB8888_1X32,
59 VI6_FMT_XRGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
61 1, { 16, 0, 0 }, false, false, 1, 1 },
62 { V4L2_PIX_FMT_RGB565, V4L2_MBUS_FMT_ARGB8888_1X32,
63 VI6_FMT_RGB_565, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
65 1, { 16, 0, 0 }, false, false, 1, 1 },
66 { V4L2_PIX_FMT_BGR24, V4L2_MBUS_FMT_ARGB8888_1X32,
67 VI6_FMT_BGR_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
68 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
69 1, { 24, 0, 0 }, false, false, 1, 1 },
70 { V4L2_PIX_FMT_RGB24, V4L2_MBUS_FMT_ARGB8888_1X32,
71 VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
72 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
73 1, { 24, 0, 0 }, false, false, 1, 1 },
74 { V4L2_PIX_FMT_BGR32, V4L2_MBUS_FMT_ARGB8888_1X32,
75 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
76 1, { 32, 0, 0 }, false, false, 1, 1 },
77 { V4L2_PIX_FMT_RGB32, V4L2_MBUS_FMT_ARGB8888_1X32,
78 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
79 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
80 1, { 32, 0, 0 }, false, false, 1, 1 },
81 { V4L2_PIX_FMT_UYVY, V4L2_MBUS_FMT_AYUV8_1X32,
82 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
83 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
84 1, { 16, 0, 0 }, false, false, 2, 1 },
85 { V4L2_PIX_FMT_VYUY, V4L2_MBUS_FMT_AYUV8_1X32,
86 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
87 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
88 1, { 16, 0, 0 }, false, true, 2, 1 },
89 { V4L2_PIX_FMT_YUYV, V4L2_MBUS_FMT_AYUV8_1X32,
90 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
91 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
92 1, { 16, 0, 0 }, true, false, 2, 1 },
93 { V4L2_PIX_FMT_YVYU, V4L2_MBUS_FMT_AYUV8_1X32,
94 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
95 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
96 1, { 16, 0, 0 }, true, true, 2, 1 },
97 { V4L2_PIX_FMT_NV12M, V4L2_MBUS_FMT_AYUV8_1X32,
98 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
99 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
100 2, { 8, 16, 0 }, false, false, 2, 2 },
101 { V4L2_PIX_FMT_NV21M, V4L2_MBUS_FMT_AYUV8_1X32,
102 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
103 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
104 2, { 8, 16, 0 }, false, true, 2, 2 },
105 { V4L2_PIX_FMT_NV16M, V4L2_MBUS_FMT_AYUV8_1X32,
106 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
107 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
108 2, { 8, 16, 0 }, false, false, 2, 1 },
109 { V4L2_PIX_FMT_NV61M, V4L2_MBUS_FMT_AYUV8_1X32,
110 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
111 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
112 2, { 8, 16, 0 }, false, true, 2, 1 },
113 { V4L2_PIX_FMT_YUV420M, V4L2_MBUS_FMT_AYUV8_1X32,
114 VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
115 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
116 3, { 8, 8, 8 }, false, false, 2, 2 },
120 * vsp1_get_format_info - Retrieve format information for a 4CC
121 * @fourcc: the format 4CC
123 * Return a pointer to the format information structure corresponding to the
124 * given V4L2 format 4CC, or NULL if no corresponding format can be found.
126 static const struct vsp1_format_info *vsp1_get_format_info(u32 fourcc)
130 for (i = 0; i < ARRAY_SIZE(vsp1_video_formats); ++i) {
131 const struct vsp1_format_info *info = &vsp1_video_formats[i];
133 if (info->fourcc == fourcc)
141 static struct v4l2_subdev *
142 vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
144 struct media_pad *remote;
146 remote = media_entity_remote_pad(local);
147 if (remote == NULL ||
148 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
152 *pad = remote->index;
154 return media_entity_to_v4l2_subdev(remote->entity);
157 static int vsp1_video_verify_format(struct vsp1_video *video)
159 struct v4l2_subdev_format fmt;
160 struct v4l2_subdev *subdev;
163 subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
167 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
168 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
170 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
172 if (video->fmtinfo->mbus != fmt.format.code ||
173 video->format.height != fmt.format.height ||
174 video->format.width != fmt.format.width)
180 static int __vsp1_video_try_format(struct vsp1_video *video,
181 struct v4l2_pix_format_mplane *pix,
182 const struct vsp1_format_info **fmtinfo)
184 const struct vsp1_format_info *info;
185 unsigned int width = pix->width;
186 unsigned int height = pix->height;
189 /* Retrieve format information and select the default format if the
190 * requested format isn't supported.
192 info = vsp1_get_format_info(pix->pixelformat);
194 info = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
196 pix->pixelformat = info->fourcc;
197 pix->colorspace = V4L2_COLORSPACE_SRGB;
198 pix->field = V4L2_FIELD_NONE;
199 memset(pix->reserved, 0, sizeof(pix->reserved));
201 /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
202 width = round_down(width, info->hsub);
203 height = round_down(height, info->vsub);
205 /* Clamp the width and height. */
206 pix->width = clamp(width, VSP1_VIDEO_MIN_WIDTH, VSP1_VIDEO_MAX_WIDTH);
207 pix->height = clamp(height, VSP1_VIDEO_MIN_HEIGHT,
208 VSP1_VIDEO_MAX_HEIGHT);
210 /* Compute and clamp the stride and image size. While not documented in
211 * the datasheet, strides not aligned to a multiple of 128 bytes result
212 * in image corruption.
214 for (i = 0; i < max(info->planes, 2U); ++i) {
215 unsigned int hsub = i > 0 ? info->hsub : 1;
216 unsigned int vsub = i > 0 ? info->vsub : 1;
217 unsigned int align = 128;
220 bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
221 pix->width / hsub * info->bpp[i] / 8,
222 round_down(65535U, align));
224 pix->plane_fmt[i].bytesperline = round_up(bpl, align);
225 pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
226 * pix->height / vsub;
229 if (info->planes == 3) {
230 /* The second and third planes must have the same stride. */
231 pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
232 pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
235 pix->num_planes = info->planes;
244 vsp1_video_format_adjust(struct vsp1_video *video,
245 const struct v4l2_pix_format_mplane *format,
246 struct v4l2_pix_format_mplane *adjust)
251 __vsp1_video_try_format(video, adjust, NULL);
253 if (format->width != adjust->width ||
254 format->height != adjust->height ||
255 format->pixelformat != adjust->pixelformat ||
256 format->num_planes != adjust->num_planes)
259 for (i = 0; i < format->num_planes; ++i) {
260 if (format->plane_fmt[i].bytesperline !=
261 adjust->plane_fmt[i].bytesperline)
264 adjust->plane_fmt[i].sizeimage =
265 max(adjust->plane_fmt[i].sizeimage,
266 format->plane_fmt[i].sizeimage);
272 /* -----------------------------------------------------------------------------
273 * Pipeline Management
276 static int vsp1_pipeline_validate_branch(struct vsp1_rwpf *input,
277 struct vsp1_rwpf *output)
279 struct vsp1_entity *entity;
280 unsigned int entities = 0;
281 struct media_pad *pad;
282 bool uds_found = false;
284 input->location.left = 0;
285 input->location.top = 0;
287 pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
293 /* We've reached a video node, that shouldn't have happened. */
294 if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
297 entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity));
299 /* A BRU is present in the pipeline, store the compose rectangle
300 * location in the input RPF for use when configuring the RPF.
302 if (entity->type == VSP1_ENTITY_BRU) {
303 struct vsp1_bru *bru = to_bru(&entity->subdev);
304 struct v4l2_rect *rect = &bru->compose[pad->index];
306 input->location.left = rect->left;
307 input->location.top = rect->top;
310 /* We've reached the WPF, we're done. */
311 if (entity->type == VSP1_ENTITY_WPF)
314 /* Ensure the branch has no loop. */
315 if (entities & (1 << entity->subdev.entity.id))
318 entities |= 1 << entity->subdev.entity.id;
320 /* UDS can't be chained. */
321 if (entity->type == VSP1_ENTITY_UDS) {
327 /* Follow the source link. The link setup operations ensure
328 * that the output fan-out can't be more than one, there is thus
329 * no need to verify here that only a single source link is
332 pad = &entity->pads[entity->source_pad];
333 pad = media_entity_remote_pad(pad);
336 /* The last entity must be the output WPF. */
337 if (entity != &output->entity)
343 static int vsp1_pipeline_validate(struct vsp1_pipeline *pipe,
344 struct vsp1_video *video)
346 struct media_entity_graph graph;
347 struct media_entity *entity = &video->video.entity;
348 struct media_device *mdev = entity->parent;
352 mutex_lock(&mdev->graph_mutex);
354 /* Walk the graph to locate the entities and video nodes. */
355 media_entity_graph_walk_start(&graph, entity);
357 while ((entity = media_entity_graph_walk_next(&graph))) {
358 struct v4l2_subdev *subdev;
359 struct vsp1_rwpf *rwpf;
360 struct vsp1_entity *e;
362 if (media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV) {
367 subdev = media_entity_to_v4l2_subdev(entity);
368 e = to_vsp1_entity(subdev);
369 list_add_tail(&e->list_pipe, &pipe->entities);
371 if (e->type == VSP1_ENTITY_RPF) {
372 rwpf = to_rwpf(subdev);
373 pipe->inputs[pipe->num_inputs++] = rwpf;
374 rwpf->video.pipe_index = pipe->num_inputs;
375 } else if (e->type == VSP1_ENTITY_WPF) {
376 rwpf = to_rwpf(subdev);
377 pipe->output = to_rwpf(subdev);
378 rwpf->video.pipe_index = 0;
379 } else if (e->type == VSP1_ENTITY_LIF) {
381 } else if (e->type == VSP1_ENTITY_BRU) {
386 mutex_unlock(&mdev->graph_mutex);
388 /* We need one output and at least one input. */
389 if (pipe->num_inputs == 0 || !pipe->output) {
394 /* Follow links downstream for each input and make sure the graph
395 * contains no loop and that all branches end at the output WPF.
397 for (i = 0; i < pipe->num_inputs; ++i) {
398 ret = vsp1_pipeline_validate_branch(pipe->inputs[i],
407 INIT_LIST_HEAD(&pipe->entities);
408 pipe->buffers_ready = 0;
410 pipe->num_inputs = 0;
417 static int vsp1_pipeline_init(struct vsp1_pipeline *pipe,
418 struct vsp1_video *video)
422 mutex_lock(&pipe->lock);
424 /* If we're the first user validate and initialize the pipeline. */
425 if (pipe->use_count == 0) {
426 ret = vsp1_pipeline_validate(pipe, video);
435 mutex_unlock(&pipe->lock);
439 static void vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
441 mutex_lock(&pipe->lock);
443 /* If we're the last user clean up the pipeline. */
444 if (--pipe->use_count == 0) {
445 INIT_LIST_HEAD(&pipe->entities);
446 pipe->state = VSP1_PIPELINE_STOPPED;
447 pipe->buffers_ready = 0;
449 pipe->num_inputs = 0;
455 mutex_unlock(&pipe->lock);
458 static void vsp1_pipeline_run(struct vsp1_pipeline *pipe)
460 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
462 vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index), VI6_CMD_STRCMD);
463 pipe->state = VSP1_PIPELINE_RUNNING;
464 pipe->buffers_ready = 0;
467 static int vsp1_pipeline_stop(struct vsp1_pipeline *pipe)
469 struct vsp1_entity *entity;
473 spin_lock_irqsave(&pipe->irqlock, flags);
474 pipe->state = VSP1_PIPELINE_STOPPING;
475 spin_unlock_irqrestore(&pipe->irqlock, flags);
477 ret = wait_event_timeout(pipe->wq, pipe->state == VSP1_PIPELINE_STOPPED,
478 msecs_to_jiffies(500));
479 ret = ret == 0 ? -ETIMEDOUT : 0;
481 list_for_each_entry(entity, &pipe->entities, list_pipe) {
483 vsp1_write(entity->vsp1, entity->route->reg,
484 VI6_DPR_NODE_UNUSED);
486 v4l2_subdev_call(&entity->subdev, video, s_stream, 0);
492 static bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe)
496 mask = ((1 << pipe->num_inputs) - 1) << 1;
500 return pipe->buffers_ready == mask;
504 * vsp1_video_complete_buffer - Complete the current buffer
505 * @video: the video node
507 * This function completes the current buffer by filling its sequence number,
508 * time stamp and payload size, and hands it back to the videobuf core.
510 * When operating in DU output mode (deep pipeline to the DU through the LIF),
511 * the VSP1 needs to constantly supply frames to the display. In that case, if
512 * no other buffer is queued, reuse the one that has just been processed instead
513 * of handing it back to the videobuf core.
515 * Return the next queued buffer or NULL if the queue is empty.
517 static struct vsp1_video_buffer *
518 vsp1_video_complete_buffer(struct vsp1_video *video)
520 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
521 struct vsp1_video_buffer *next = NULL;
522 struct vsp1_video_buffer *done;
526 spin_lock_irqsave(&video->irqlock, flags);
528 if (list_empty(&video->irqqueue)) {
529 spin_unlock_irqrestore(&video->irqlock, flags);
533 done = list_first_entry(&video->irqqueue,
534 struct vsp1_video_buffer, queue);
536 /* In DU output mode reuse the buffer if the list is singular. */
537 if (pipe->lif && list_is_singular(&video->irqqueue)) {
538 spin_unlock_irqrestore(&video->irqlock, flags);
542 list_del(&done->queue);
544 if (!list_empty(&video->irqqueue))
545 next = list_first_entry(&video->irqqueue,
546 struct vsp1_video_buffer, queue);
548 spin_unlock_irqrestore(&video->irqlock, flags);
550 done->buf.v4l2_buf.sequence = video->sequence++;
551 v4l2_get_timestamp(&done->buf.v4l2_buf.timestamp);
552 for (i = 0; i < done->buf.num_planes; ++i)
553 vb2_set_plane_payload(&done->buf, i, done->length[i]);
554 vb2_buffer_done(&done->buf, VB2_BUF_STATE_DONE);
559 static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
560 struct vsp1_video *video)
562 struct vsp1_video_buffer *buf;
565 buf = vsp1_video_complete_buffer(video);
569 spin_lock_irqsave(&pipe->irqlock, flags);
571 video->ops->queue(video, buf);
572 pipe->buffers_ready |= 1 << video->pipe_index;
574 spin_unlock_irqrestore(&pipe->irqlock, flags);
577 void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
585 /* Complete buffers on all video nodes. */
586 for (i = 0; i < pipe->num_inputs; ++i)
587 vsp1_video_frame_end(pipe, &pipe->inputs[i]->video);
590 vsp1_video_frame_end(pipe, &pipe->output->video);
592 spin_lock_irqsave(&pipe->irqlock, flags);
594 /* If a stop has been requested, mark the pipeline as stopped and
597 if (pipe->state == VSP1_PIPELINE_STOPPING) {
598 pipe->state = VSP1_PIPELINE_STOPPED;
603 /* Restart the pipeline if ready. */
604 if (vsp1_pipeline_ready(pipe))
605 vsp1_pipeline_run(pipe);
608 spin_unlock_irqrestore(&pipe->irqlock, flags);
611 /* -----------------------------------------------------------------------------
612 * videobuf2 Queue Operations
616 vsp1_video_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
617 unsigned int *nbuffers, unsigned int *nplanes,
618 unsigned int sizes[], void *alloc_ctxs[])
620 struct vsp1_video *video = vb2_get_drv_priv(vq);
621 const struct v4l2_pix_format_mplane *format;
622 struct v4l2_pix_format_mplane pix_mp;
626 /* Make sure the format is valid and adjust the sizeimage field
629 if (!vsp1_video_format_adjust(video, &fmt->fmt.pix_mp, &pix_mp))
634 format = &video->format;
637 *nplanes = format->num_planes;
639 for (i = 0; i < format->num_planes; ++i) {
640 sizes[i] = format->plane_fmt[i].sizeimage;
641 alloc_ctxs[i] = video->alloc_ctx;
647 static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
649 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
650 struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
651 const struct v4l2_pix_format_mplane *format = &video->format;
654 if (vb->num_planes < format->num_planes)
659 for (i = 0; i < vb->num_planes; ++i) {
660 buf->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
661 buf->length[i] = vb2_plane_size(vb, i);
663 if (buf->length[i] < format->plane_fmt[i].sizeimage)
670 static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
672 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
673 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
674 struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
678 spin_lock_irqsave(&video->irqlock, flags);
679 empty = list_empty(&video->irqqueue);
680 list_add_tail(&buf->queue, &video->irqqueue);
681 spin_unlock_irqrestore(&video->irqlock, flags);
686 spin_lock_irqsave(&pipe->irqlock, flags);
688 video->ops->queue(video, buf);
689 pipe->buffers_ready |= 1 << video->pipe_index;
691 if (vb2_is_streaming(&video->queue) &&
692 vsp1_pipeline_ready(pipe))
693 vsp1_pipeline_run(pipe);
695 spin_unlock_irqrestore(&pipe->irqlock, flags);
698 static void vsp1_entity_route_setup(struct vsp1_entity *source)
700 struct vsp1_entity *sink;
702 if (source->route->reg == 0)
705 sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
706 vsp1_write(source->vsp1, source->route->reg,
707 sink->route->inputs[source->sink_pad]);
710 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
712 struct vsp1_video *video = vb2_get_drv_priv(vq);
713 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
714 struct vsp1_entity *entity;
718 mutex_lock(&pipe->lock);
719 if (pipe->stream_count == pipe->num_video - 1) {
720 list_for_each_entry(entity, &pipe->entities, list_pipe) {
721 vsp1_entity_route_setup(entity);
723 ret = v4l2_subdev_call(&entity->subdev, video,
726 mutex_unlock(&pipe->lock);
732 pipe->stream_count++;
733 mutex_unlock(&pipe->lock);
735 spin_lock_irqsave(&pipe->irqlock, flags);
736 if (vsp1_pipeline_ready(pipe))
737 vsp1_pipeline_run(pipe);
738 spin_unlock_irqrestore(&pipe->irqlock, flags);
743 static void vsp1_video_stop_streaming(struct vb2_queue *vq)
745 struct vsp1_video *video = vb2_get_drv_priv(vq);
746 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
750 mutex_lock(&pipe->lock);
751 if (--pipe->stream_count == 0) {
752 /* Stop the pipeline. */
753 ret = vsp1_pipeline_stop(pipe);
754 if (ret == -ETIMEDOUT)
755 dev_err(video->vsp1->dev, "pipeline stop timeout\n");
757 mutex_unlock(&pipe->lock);
759 vsp1_pipeline_cleanup(pipe);
760 media_entity_pipeline_stop(&video->video.entity);
762 /* Remove all buffers from the IRQ queue. */
763 spin_lock_irqsave(&video->irqlock, flags);
764 INIT_LIST_HEAD(&video->irqqueue);
765 spin_unlock_irqrestore(&video->irqlock, flags);
768 static struct vb2_ops vsp1_video_queue_qops = {
769 .queue_setup = vsp1_video_queue_setup,
770 .buf_prepare = vsp1_video_buffer_prepare,
771 .buf_queue = vsp1_video_buffer_queue,
772 .wait_prepare = vb2_ops_wait_prepare,
773 .wait_finish = vb2_ops_wait_finish,
774 .start_streaming = vsp1_video_start_streaming,
775 .stop_streaming = vsp1_video_stop_streaming,
778 /* -----------------------------------------------------------------------------
783 vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
785 struct v4l2_fh *vfh = file->private_data;
786 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
788 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
789 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
790 | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
792 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
793 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
794 | V4L2_CAP_STREAMING;
796 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
797 | V4L2_CAP_STREAMING;
799 strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
800 strlcpy(cap->card, video->video.name, sizeof(cap->card));
801 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
802 dev_name(video->vsp1->dev));
808 vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
810 struct v4l2_fh *vfh = file->private_data;
811 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
813 if (format->type != video->queue.type)
816 mutex_lock(&video->lock);
817 format->fmt.pix_mp = video->format;
818 mutex_unlock(&video->lock);
824 vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
826 struct v4l2_fh *vfh = file->private_data;
827 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
829 if (format->type != video->queue.type)
832 return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
836 vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
838 struct v4l2_fh *vfh = file->private_data;
839 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
840 const struct vsp1_format_info *info;
843 if (format->type != video->queue.type)
846 ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
850 mutex_lock(&video->lock);
852 if (vb2_is_busy(&video->queue)) {
857 video->format = format->fmt.pix_mp;
858 video->fmtinfo = info;
861 mutex_unlock(&video->lock);
866 vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
868 struct v4l2_fh *vfh = file->private_data;
869 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
870 struct vsp1_pipeline *pipe;
873 if (video->queue.owner && video->queue.owner != file->private_data)
878 /* Start streaming on the pipeline. No link touching an entity in the
879 * pipeline can be activated or deactivated once streaming is started.
881 * Use the VSP1 pipeline object embedded in the first video object that
884 pipe = video->video.entity.pipe
885 ? to_vsp1_pipeline(&video->video.entity) : &video->pipe;
887 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
891 /* Verify that the configured format matches the output of the connected
894 ret = vsp1_video_verify_format(video);
898 ret = vsp1_pipeline_init(pipe, video);
902 /* Start the queue. */
903 ret = vb2_streamon(&video->queue, type);
910 vsp1_pipeline_cleanup(pipe);
912 media_entity_pipeline_stop(&video->video.entity);
916 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
917 .vidioc_querycap = vsp1_video_querycap,
918 .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
919 .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
920 .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
921 .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
922 .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
923 .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
924 .vidioc_reqbufs = vb2_ioctl_reqbufs,
925 .vidioc_querybuf = vb2_ioctl_querybuf,
926 .vidioc_qbuf = vb2_ioctl_qbuf,
927 .vidioc_dqbuf = vb2_ioctl_dqbuf,
928 .vidioc_create_bufs = vb2_ioctl_create_bufs,
929 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
930 .vidioc_streamon = vsp1_video_streamon,
931 .vidioc_streamoff = vb2_ioctl_streamoff,
934 /* -----------------------------------------------------------------------------
935 * V4L2 File Operations
938 static int vsp1_video_open(struct file *file)
940 struct vsp1_video *video = video_drvdata(file);
944 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
948 v4l2_fh_init(vfh, &video->video);
951 file->private_data = vfh;
953 if (!vsp1_device_get(video->vsp1)) {
962 static int vsp1_video_release(struct file *file)
964 struct vsp1_video *video = video_drvdata(file);
965 struct v4l2_fh *vfh = file->private_data;
967 mutex_lock(&video->lock);
968 if (video->queue.owner == vfh) {
969 vb2_queue_release(&video->queue);
970 video->queue.owner = NULL;
972 mutex_unlock(&video->lock);
974 vsp1_device_put(video->vsp1);
976 v4l2_fh_release(file);
978 file->private_data = NULL;
983 static struct v4l2_file_operations vsp1_video_fops = {
984 .owner = THIS_MODULE,
985 .unlocked_ioctl = video_ioctl2,
986 .open = vsp1_video_open,
987 .release = vsp1_video_release,
988 .poll = vb2_fop_poll,
989 .mmap = vb2_fop_mmap,
992 /* -----------------------------------------------------------------------------
993 * Initialization and Cleanup
996 int vsp1_video_init(struct vsp1_video *video, struct vsp1_entity *rwpf)
998 const char *direction;
1001 switch (video->type) {
1002 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1003 direction = "output";
1004 video->pad.flags = MEDIA_PAD_FL_SINK;
1007 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1008 direction = "input";
1009 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1010 video->video.vfl_dir = VFL_DIR_TX;
1019 mutex_init(&video->lock);
1020 spin_lock_init(&video->irqlock);
1021 INIT_LIST_HEAD(&video->irqqueue);
1023 mutex_init(&video->pipe.lock);
1024 spin_lock_init(&video->pipe.irqlock);
1025 INIT_LIST_HEAD(&video->pipe.entities);
1026 init_waitqueue_head(&video->pipe.wq);
1027 video->pipe.state = VSP1_PIPELINE_STOPPED;
1029 /* Initialize the media entity... */
1030 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1034 /* ... and the format ... */
1035 video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
1036 video->format.pixelformat = video->fmtinfo->fourcc;
1037 video->format.colorspace = V4L2_COLORSPACE_SRGB;
1038 video->format.field = V4L2_FIELD_NONE;
1039 video->format.width = VSP1_VIDEO_DEF_WIDTH;
1040 video->format.height = VSP1_VIDEO_DEF_HEIGHT;
1041 video->format.num_planes = 1;
1042 video->format.plane_fmt[0].bytesperline =
1043 video->format.width * video->fmtinfo->bpp[0] / 8;
1044 video->format.plane_fmt[0].sizeimage =
1045 video->format.plane_fmt[0].bytesperline * video->format.height;
1047 /* ... and the video node... */
1048 video->video.v4l2_dev = &video->vsp1->v4l2_dev;
1049 video->video.fops = &vsp1_video_fops;
1050 snprintf(video->video.name, sizeof(video->video.name), "%s %s",
1051 rwpf->subdev.name, direction);
1052 video->video.vfl_type = VFL_TYPE_GRABBER;
1053 video->video.release = video_device_release_empty;
1054 video->video.ioctl_ops = &vsp1_video_ioctl_ops;
1056 video_set_drvdata(&video->video, video);
1058 /* ... and the buffers queue... */
1059 video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
1060 if (IS_ERR(video->alloc_ctx)) {
1061 ret = PTR_ERR(video->alloc_ctx);
1065 video->queue.type = video->type;
1066 video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1067 video->queue.lock = &video->lock;
1068 video->queue.drv_priv = video;
1069 video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer);
1070 video->queue.ops = &vsp1_video_queue_qops;
1071 video->queue.mem_ops = &vb2_dma_contig_memops;
1072 video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1073 ret = vb2_queue_init(&video->queue);
1075 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1079 /* ... and register the video device. */
1080 video->video.queue = &video->queue;
1081 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1083 dev_err(video->vsp1->dev, "failed to register video device\n");
1090 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1091 vsp1_video_cleanup(video);
1095 void vsp1_video_cleanup(struct vsp1_video *video)
1097 if (video_is_registered(&video->video))
1098 video_unregister_device(&video->video);
1100 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1101 media_entity_cleanup(&video->video.entity);