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, false },
54 { V4L2_PIX_FMT_ARGB444, V4L2_MBUS_FMT_ARGB8888_1X32,
55 VI6_FMT_ARGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
57 1, { 16, 0, 0 }, false, false, 1, 1, true },
58 { V4L2_PIX_FMT_XRGB444, V4L2_MBUS_FMT_ARGB8888_1X32,
59 VI6_FMT_XRGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
61 1, { 16, 0, 0 }, false, false, 1, 1, true },
62 { V4L2_PIX_FMT_ARGB555, V4L2_MBUS_FMT_ARGB8888_1X32,
63 VI6_FMT_ARGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
65 1, { 16, 0, 0 }, false, false, 1, 1, true },
66 { V4L2_PIX_FMT_XRGB555, V4L2_MBUS_FMT_ARGB8888_1X32,
67 VI6_FMT_XRGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
69 1, { 16, 0, 0 }, false, false, 1, 1, false },
70 { V4L2_PIX_FMT_RGB565, V4L2_MBUS_FMT_ARGB8888_1X32,
71 VI6_FMT_RGB_565, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
73 1, { 16, 0, 0 }, false, false, 1, 1, false },
74 { V4L2_PIX_FMT_BGR24, V4L2_MBUS_FMT_ARGB8888_1X32,
75 VI6_FMT_BGR_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
76 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
77 1, { 24, 0, 0 }, false, false, 1, 1, false },
78 { V4L2_PIX_FMT_RGB24, V4L2_MBUS_FMT_ARGB8888_1X32,
79 VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
80 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
81 1, { 24, 0, 0 }, false, false, 1, 1, false },
82 { V4L2_PIX_FMT_ABGR32, V4L2_MBUS_FMT_ARGB8888_1X32,
83 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
84 1, { 32, 0, 0 }, false, false, 1, 1, true },
85 { V4L2_PIX_FMT_XBGR32, V4L2_MBUS_FMT_ARGB8888_1X32,
86 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
87 1, { 32, 0, 0 }, false, false, 1, 1, false },
88 { V4L2_PIX_FMT_ARGB32, V4L2_MBUS_FMT_ARGB8888_1X32,
89 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
90 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
91 1, { 32, 0, 0 }, false, false, 1, 1, true },
92 { V4L2_PIX_FMT_XRGB32, V4L2_MBUS_FMT_ARGB8888_1X32,
93 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
94 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
95 1, { 32, 0, 0 }, false, false, 1, 1, false },
96 { V4L2_PIX_FMT_UYVY, V4L2_MBUS_FMT_AYUV8_1X32,
97 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
98 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
99 1, { 16, 0, 0 }, false, false, 2, 1, false },
100 { V4L2_PIX_FMT_VYUY, V4L2_MBUS_FMT_AYUV8_1X32,
101 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
102 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
103 1, { 16, 0, 0 }, false, true, 2, 1, false },
104 { V4L2_PIX_FMT_YUYV, V4L2_MBUS_FMT_AYUV8_1X32,
105 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
106 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
107 1, { 16, 0, 0 }, true, false, 2, 1, false },
108 { V4L2_PIX_FMT_YVYU, V4L2_MBUS_FMT_AYUV8_1X32,
109 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
110 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
111 1, { 16, 0, 0 }, true, true, 2, 1, false },
112 { V4L2_PIX_FMT_NV12M, V4L2_MBUS_FMT_AYUV8_1X32,
113 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
114 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
115 2, { 8, 16, 0 }, false, false, 2, 2, false },
116 { V4L2_PIX_FMT_NV21M, V4L2_MBUS_FMT_AYUV8_1X32,
117 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
118 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
119 2, { 8, 16, 0 }, false, true, 2, 2, false },
120 { V4L2_PIX_FMT_NV16M, V4L2_MBUS_FMT_AYUV8_1X32,
121 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
122 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
123 2, { 8, 16, 0 }, false, false, 2, 1, false },
124 { V4L2_PIX_FMT_NV61M, V4L2_MBUS_FMT_AYUV8_1X32,
125 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
126 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
127 2, { 8, 16, 0 }, false, true, 2, 1, false },
128 { V4L2_PIX_FMT_YUV420M, V4L2_MBUS_FMT_AYUV8_1X32,
129 VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
130 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
131 3, { 8, 8, 8 }, false, false, 2, 2, false },
135 * vsp1_get_format_info - Retrieve format information for a 4CC
136 * @fourcc: the format 4CC
138 * Return a pointer to the format information structure corresponding to the
139 * given V4L2 format 4CC, or NULL if no corresponding format can be found.
141 static const struct vsp1_format_info *vsp1_get_format_info(u32 fourcc)
145 for (i = 0; i < ARRAY_SIZE(vsp1_video_formats); ++i) {
146 const struct vsp1_format_info *info = &vsp1_video_formats[i];
148 if (info->fourcc == fourcc)
156 static struct v4l2_subdev *
157 vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
159 struct media_pad *remote;
161 remote = media_entity_remote_pad(local);
162 if (remote == NULL ||
163 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
167 *pad = remote->index;
169 return media_entity_to_v4l2_subdev(remote->entity);
172 static int vsp1_video_verify_format(struct vsp1_video *video)
174 struct v4l2_subdev_format fmt;
175 struct v4l2_subdev *subdev;
178 subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
182 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
183 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
185 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
187 if (video->fmtinfo->mbus != fmt.format.code ||
188 video->format.height != fmt.format.height ||
189 video->format.width != fmt.format.width)
195 static int __vsp1_video_try_format(struct vsp1_video *video,
196 struct v4l2_pix_format_mplane *pix,
197 const struct vsp1_format_info **fmtinfo)
199 static const u32 xrgb_formats[][2] = {
200 { V4L2_PIX_FMT_RGB444, V4L2_PIX_FMT_XRGB444 },
201 { V4L2_PIX_FMT_RGB555, V4L2_PIX_FMT_XRGB555 },
202 { V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_XBGR32 },
203 { V4L2_PIX_FMT_RGB32, V4L2_PIX_FMT_XRGB32 },
206 const struct vsp1_format_info *info;
207 unsigned int width = pix->width;
208 unsigned int height = pix->height;
211 /* Backward compatibility: replace deprecated RGB formats by their XRGB
212 * equivalent. This selects the format older userspace applications want
213 * while still exposing the new format.
215 for (i = 0; i < ARRAY_SIZE(xrgb_formats); ++i) {
216 if (xrgb_formats[i][0] == pix->pixelformat) {
217 pix->pixelformat = xrgb_formats[i][1];
222 /* Retrieve format information and select the default format if the
223 * requested format isn't supported.
225 info = vsp1_get_format_info(pix->pixelformat);
227 info = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
229 pix->pixelformat = info->fourcc;
230 pix->colorspace = V4L2_COLORSPACE_SRGB;
231 pix->field = V4L2_FIELD_NONE;
232 memset(pix->reserved, 0, sizeof(pix->reserved));
234 /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
235 width = round_down(width, info->hsub);
236 height = round_down(height, info->vsub);
238 /* Clamp the width and height. */
239 pix->width = clamp(width, VSP1_VIDEO_MIN_WIDTH, VSP1_VIDEO_MAX_WIDTH);
240 pix->height = clamp(height, VSP1_VIDEO_MIN_HEIGHT,
241 VSP1_VIDEO_MAX_HEIGHT);
243 /* Compute and clamp the stride and image size. While not documented in
244 * the datasheet, strides not aligned to a multiple of 128 bytes result
245 * in image corruption.
247 for (i = 0; i < max(info->planes, 2U); ++i) {
248 unsigned int hsub = i > 0 ? info->hsub : 1;
249 unsigned int vsub = i > 0 ? info->vsub : 1;
250 unsigned int align = 128;
253 bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
254 pix->width / hsub * info->bpp[i] / 8,
255 round_down(65535U, align));
257 pix->plane_fmt[i].bytesperline = round_up(bpl, align);
258 pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
259 * pix->height / vsub;
262 if (info->planes == 3) {
263 /* The second and third planes must have the same stride. */
264 pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
265 pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
268 pix->num_planes = info->planes;
277 vsp1_video_format_adjust(struct vsp1_video *video,
278 const struct v4l2_pix_format_mplane *format,
279 struct v4l2_pix_format_mplane *adjust)
284 __vsp1_video_try_format(video, adjust, NULL);
286 if (format->width != adjust->width ||
287 format->height != adjust->height ||
288 format->pixelformat != adjust->pixelformat ||
289 format->num_planes != adjust->num_planes)
292 for (i = 0; i < format->num_planes; ++i) {
293 if (format->plane_fmt[i].bytesperline !=
294 adjust->plane_fmt[i].bytesperline)
297 adjust->plane_fmt[i].sizeimage =
298 max(adjust->plane_fmt[i].sizeimage,
299 format->plane_fmt[i].sizeimage);
305 /* -----------------------------------------------------------------------------
306 * Pipeline Management
309 static int vsp1_pipeline_validate_branch(struct vsp1_rwpf *input,
310 struct vsp1_rwpf *output)
312 struct vsp1_entity *entity;
313 unsigned int entities = 0;
314 struct media_pad *pad;
315 bool uds_found = false;
317 input->location.left = 0;
318 input->location.top = 0;
320 pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
326 /* We've reached a video node, that shouldn't have happened. */
327 if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
330 entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity));
332 /* A BRU is present in the pipeline, store the compose rectangle
333 * location in the input RPF for use when configuring the RPF.
335 if (entity->type == VSP1_ENTITY_BRU) {
336 struct vsp1_bru *bru = to_bru(&entity->subdev);
337 struct v4l2_rect *rect =
338 &bru->inputs[pad->index].compose;
340 bru->inputs[pad->index].rpf = input;
342 input->location.left = rect->left;
343 input->location.top = rect->top;
346 /* We've reached the WPF, we're done. */
347 if (entity->type == VSP1_ENTITY_WPF)
350 /* Ensure the branch has no loop. */
351 if (entities & (1 << entity->subdev.entity.id))
354 entities |= 1 << entity->subdev.entity.id;
356 /* UDS can't be chained. */
357 if (entity->type == VSP1_ENTITY_UDS) {
363 /* Follow the source link. The link setup operations ensure
364 * that the output fan-out can't be more than one, there is thus
365 * no need to verify here that only a single source link is
368 pad = &entity->pads[entity->source_pad];
369 pad = media_entity_remote_pad(pad);
372 /* The last entity must be the output WPF. */
373 if (entity != &output->entity)
379 static void __vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
382 struct vsp1_bru *bru = to_bru(&pipe->bru->subdev);
385 for (i = 0; i < ARRAY_SIZE(bru->inputs); ++i)
386 bru->inputs[i].rpf = NULL;
389 INIT_LIST_HEAD(&pipe->entities);
390 pipe->state = VSP1_PIPELINE_STOPPED;
391 pipe->buffers_ready = 0;
393 pipe->num_inputs = 0;
399 static int vsp1_pipeline_validate(struct vsp1_pipeline *pipe,
400 struct vsp1_video *video)
402 struct media_entity_graph graph;
403 struct media_entity *entity = &video->video.entity;
404 struct media_device *mdev = entity->parent;
408 mutex_lock(&mdev->graph_mutex);
410 /* Walk the graph to locate the entities and video nodes. */
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 (media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV) {
423 subdev = media_entity_to_v4l2_subdev(entity);
424 e = to_vsp1_entity(subdev);
425 list_add_tail(&e->list_pipe, &pipe->entities);
427 if (e->type == VSP1_ENTITY_RPF) {
428 rwpf = to_rwpf(subdev);
429 pipe->inputs[pipe->num_inputs++] = rwpf;
430 rwpf->video.pipe_index = pipe->num_inputs;
431 } else if (e->type == VSP1_ENTITY_WPF) {
432 rwpf = to_rwpf(subdev);
433 pipe->output = to_rwpf(subdev);
434 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 /* We need one output and at least one input. */
445 if (pipe->num_inputs == 0 || !pipe->output) {
450 /* Follow links downstream for each input and make sure the graph
451 * contains no loop and that all branches end at the output WPF.
453 for (i = 0; i < pipe->num_inputs; ++i) {
454 ret = vsp1_pipeline_validate_branch(pipe->inputs[i],
463 __vsp1_pipeline_cleanup(pipe);
467 static int vsp1_pipeline_init(struct vsp1_pipeline *pipe,
468 struct vsp1_video *video)
472 mutex_lock(&pipe->lock);
474 /* If we're the first user validate and initialize the pipeline. */
475 if (pipe->use_count == 0) {
476 ret = vsp1_pipeline_validate(pipe, video);
485 mutex_unlock(&pipe->lock);
489 static void vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
491 mutex_lock(&pipe->lock);
493 /* If we're the last user clean up the pipeline. */
494 if (--pipe->use_count == 0)
495 __vsp1_pipeline_cleanup(pipe);
497 mutex_unlock(&pipe->lock);
500 static void vsp1_pipeline_run(struct vsp1_pipeline *pipe)
502 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
504 vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index), VI6_CMD_STRCMD);
505 pipe->state = VSP1_PIPELINE_RUNNING;
506 pipe->buffers_ready = 0;
509 static int vsp1_pipeline_stop(struct vsp1_pipeline *pipe)
511 struct vsp1_entity *entity;
515 spin_lock_irqsave(&pipe->irqlock, flags);
516 if (pipe->state == VSP1_PIPELINE_RUNNING)
517 pipe->state = VSP1_PIPELINE_STOPPING;
518 spin_unlock_irqrestore(&pipe->irqlock, flags);
520 ret = wait_event_timeout(pipe->wq, pipe->state == VSP1_PIPELINE_STOPPED,
521 msecs_to_jiffies(500));
522 ret = ret == 0 ? -ETIMEDOUT : 0;
524 list_for_each_entry(entity, &pipe->entities, list_pipe) {
525 if (entity->route && entity->route->reg)
526 vsp1_write(entity->vsp1, entity->route->reg,
527 VI6_DPR_NODE_UNUSED);
529 v4l2_subdev_call(&entity->subdev, video, s_stream, 0);
535 static bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe)
539 mask = ((1 << pipe->num_inputs) - 1) << 1;
543 return pipe->buffers_ready == mask;
547 * vsp1_video_complete_buffer - Complete the current buffer
548 * @video: the video node
550 * This function completes the current buffer by filling its sequence number,
551 * time stamp and payload size, and hands it back to the videobuf core.
553 * When operating in DU output mode (deep pipeline to the DU through the LIF),
554 * the VSP1 needs to constantly supply frames to the display. In that case, if
555 * no other buffer is queued, reuse the one that has just been processed instead
556 * of handing it back to the videobuf core.
558 * Return the next queued buffer or NULL if the queue is empty.
560 static struct vsp1_video_buffer *
561 vsp1_video_complete_buffer(struct vsp1_video *video)
563 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
564 struct vsp1_video_buffer *next = NULL;
565 struct vsp1_video_buffer *done;
569 spin_lock_irqsave(&video->irqlock, flags);
571 if (list_empty(&video->irqqueue)) {
572 spin_unlock_irqrestore(&video->irqlock, flags);
576 done = list_first_entry(&video->irqqueue,
577 struct vsp1_video_buffer, queue);
579 /* In DU output mode reuse the buffer if the list is singular. */
580 if (pipe->lif && list_is_singular(&video->irqqueue)) {
581 spin_unlock_irqrestore(&video->irqlock, flags);
585 list_del(&done->queue);
587 if (!list_empty(&video->irqqueue))
588 next = list_first_entry(&video->irqqueue,
589 struct vsp1_video_buffer, queue);
591 spin_unlock_irqrestore(&video->irqlock, flags);
593 done->buf.v4l2_buf.sequence = video->sequence++;
594 v4l2_get_timestamp(&done->buf.v4l2_buf.timestamp);
595 for (i = 0; i < done->buf.num_planes; ++i)
596 vb2_set_plane_payload(&done->buf, i, done->length[i]);
597 vb2_buffer_done(&done->buf, VB2_BUF_STATE_DONE);
602 static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
603 struct vsp1_video *video)
605 struct vsp1_video_buffer *buf;
608 buf = vsp1_video_complete_buffer(video);
612 spin_lock_irqsave(&pipe->irqlock, flags);
614 video->ops->queue(video, buf);
615 pipe->buffers_ready |= 1 << video->pipe_index;
617 spin_unlock_irqrestore(&pipe->irqlock, flags);
620 void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
622 enum vsp1_pipeline_state state;
629 /* Complete buffers on all video nodes. */
630 for (i = 0; i < pipe->num_inputs; ++i)
631 vsp1_video_frame_end(pipe, &pipe->inputs[i]->video);
634 vsp1_video_frame_end(pipe, &pipe->output->video);
636 spin_lock_irqsave(&pipe->irqlock, flags);
639 pipe->state = VSP1_PIPELINE_STOPPED;
641 /* If a stop has been requested, mark the pipeline as stopped and
644 if (state == VSP1_PIPELINE_STOPPING) {
649 /* Restart the pipeline if ready. */
650 if (vsp1_pipeline_ready(pipe))
651 vsp1_pipeline_run(pipe);
654 spin_unlock_irqrestore(&pipe->irqlock, flags);
657 /* -----------------------------------------------------------------------------
658 * videobuf2 Queue Operations
662 vsp1_video_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
663 unsigned int *nbuffers, unsigned int *nplanes,
664 unsigned int sizes[], void *alloc_ctxs[])
666 struct vsp1_video *video = vb2_get_drv_priv(vq);
667 const struct v4l2_pix_format_mplane *format;
668 struct v4l2_pix_format_mplane pix_mp;
672 /* Make sure the format is valid and adjust the sizeimage field
675 if (!vsp1_video_format_adjust(video, &fmt->fmt.pix_mp, &pix_mp))
680 format = &video->format;
683 *nplanes = format->num_planes;
685 for (i = 0; i < format->num_planes; ++i) {
686 sizes[i] = format->plane_fmt[i].sizeimage;
687 alloc_ctxs[i] = video->alloc_ctx;
693 static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
695 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
696 struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
697 const struct v4l2_pix_format_mplane *format = &video->format;
700 if (vb->num_planes < format->num_planes)
703 for (i = 0; i < vb->num_planes; ++i) {
704 buf->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
705 buf->length[i] = vb2_plane_size(vb, i);
707 if (buf->length[i] < format->plane_fmt[i].sizeimage)
714 static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
716 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
717 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
718 struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
722 spin_lock_irqsave(&video->irqlock, flags);
723 empty = list_empty(&video->irqqueue);
724 list_add_tail(&buf->queue, &video->irqqueue);
725 spin_unlock_irqrestore(&video->irqlock, flags);
730 spin_lock_irqsave(&pipe->irqlock, flags);
732 video->ops->queue(video, buf);
733 pipe->buffers_ready |= 1 << video->pipe_index;
735 if (vb2_is_streaming(&video->queue) &&
736 vsp1_pipeline_ready(pipe))
737 vsp1_pipeline_run(pipe);
739 spin_unlock_irqrestore(&pipe->irqlock, flags);
742 static void vsp1_entity_route_setup(struct vsp1_entity *source)
744 struct vsp1_entity *sink;
746 if (source->route->reg == 0)
749 sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
750 vsp1_write(source->vsp1, source->route->reg,
751 sink->route->inputs[source->sink_pad]);
754 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
756 struct vsp1_video *video = vb2_get_drv_priv(vq);
757 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
758 struct vsp1_entity *entity;
762 mutex_lock(&pipe->lock);
763 if (pipe->stream_count == pipe->num_video - 1) {
764 list_for_each_entry(entity, &pipe->entities, list_pipe) {
765 vsp1_entity_route_setup(entity);
767 ret = v4l2_subdev_call(&entity->subdev, video,
770 mutex_unlock(&pipe->lock);
776 pipe->stream_count++;
777 mutex_unlock(&pipe->lock);
779 spin_lock_irqsave(&pipe->irqlock, flags);
780 if (vsp1_pipeline_ready(pipe))
781 vsp1_pipeline_run(pipe);
782 spin_unlock_irqrestore(&pipe->irqlock, flags);
787 static int vsp1_video_stop_streaming(struct vb2_queue *vq)
789 struct vsp1_video *video = vb2_get_drv_priv(vq);
790 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
791 struct vsp1_video_buffer *buffer;
795 mutex_lock(&pipe->lock);
796 if (--pipe->stream_count == 0) {
797 /* Stop the pipeline. */
798 ret = vsp1_pipeline_stop(pipe);
799 if (ret == -ETIMEDOUT)
800 dev_err(video->vsp1->dev, "pipeline stop timeout\n");
802 mutex_unlock(&pipe->lock);
804 vsp1_pipeline_cleanup(pipe);
805 media_entity_pipeline_stop(&video->video.entity);
807 /* Remove all buffers from the IRQ queue. */
808 spin_lock_irqsave(&video->irqlock, flags);
809 list_for_each_entry(buffer, &video->irqqueue, queue)
810 vb2_buffer_done(&buffer->buf, VB2_BUF_STATE_ERROR);
811 INIT_LIST_HEAD(&video->irqqueue);
812 spin_unlock_irqrestore(&video->irqlock, flags);
817 static struct vb2_ops vsp1_video_queue_qops = {
818 .queue_setup = vsp1_video_queue_setup,
819 .buf_prepare = vsp1_video_buffer_prepare,
820 .buf_queue = vsp1_video_buffer_queue,
821 .wait_prepare = vb2_ops_wait_prepare,
822 .wait_finish = vb2_ops_wait_finish,
823 .start_streaming = vsp1_video_start_streaming,
824 .stop_streaming = vsp1_video_stop_streaming,
827 /* -----------------------------------------------------------------------------
832 vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
834 struct v4l2_fh *vfh = file->private_data;
835 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
837 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
838 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
839 | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
841 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
842 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
843 | V4L2_CAP_STREAMING;
845 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
846 | V4L2_CAP_STREAMING;
848 strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
849 strlcpy(cap->card, video->video.name, sizeof(cap->card));
850 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
851 dev_name(video->vsp1->dev));
857 vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
859 struct v4l2_fh *vfh = file->private_data;
860 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
862 if (format->type != video->queue.type)
865 mutex_lock(&video->lock);
866 format->fmt.pix_mp = video->format;
867 mutex_unlock(&video->lock);
873 vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
875 struct v4l2_fh *vfh = file->private_data;
876 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
878 if (format->type != video->queue.type)
881 return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
885 vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
887 struct v4l2_fh *vfh = file->private_data;
888 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
889 const struct vsp1_format_info *info;
892 if (format->type != video->queue.type)
895 ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
899 mutex_lock(&video->lock);
901 if (vb2_is_busy(&video->queue)) {
906 video->format = format->fmt.pix_mp;
907 video->fmtinfo = info;
910 mutex_unlock(&video->lock);
915 vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
917 struct v4l2_fh *vfh = file->private_data;
918 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
919 struct vsp1_pipeline *pipe;
922 if (video->queue.owner && video->queue.owner != file->private_data)
927 /* Start streaming on the pipeline. No link touching an entity in the
928 * pipeline can be activated or deactivated once streaming is started.
930 * Use the VSP1 pipeline object embedded in the first video object that
933 pipe = video->video.entity.pipe
934 ? to_vsp1_pipeline(&video->video.entity) : &video->pipe;
936 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
940 /* Verify that the configured format matches the output of the connected
943 ret = vsp1_video_verify_format(video);
947 ret = vsp1_pipeline_init(pipe, video);
951 /* Start the queue. */
952 ret = vb2_streamon(&video->queue, type);
959 vsp1_pipeline_cleanup(pipe);
961 media_entity_pipeline_stop(&video->video.entity);
965 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
966 .vidioc_querycap = vsp1_video_querycap,
967 .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
968 .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
969 .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
970 .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
971 .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
972 .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
973 .vidioc_reqbufs = vb2_ioctl_reqbufs,
974 .vidioc_querybuf = vb2_ioctl_querybuf,
975 .vidioc_qbuf = vb2_ioctl_qbuf,
976 .vidioc_dqbuf = vb2_ioctl_dqbuf,
977 .vidioc_create_bufs = vb2_ioctl_create_bufs,
978 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
979 .vidioc_streamon = vsp1_video_streamon,
980 .vidioc_streamoff = vb2_ioctl_streamoff,
983 /* -----------------------------------------------------------------------------
984 * V4L2 File Operations
987 static int vsp1_video_open(struct file *file)
989 struct vsp1_video *video = video_drvdata(file);
993 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
997 v4l2_fh_init(vfh, &video->video);
1000 file->private_data = vfh;
1002 ret = vsp1_device_get(video->vsp1);
1011 static int vsp1_video_release(struct file *file)
1013 struct vsp1_video *video = video_drvdata(file);
1014 struct v4l2_fh *vfh = file->private_data;
1016 mutex_lock(&video->lock);
1017 if (video->queue.owner == vfh) {
1018 vb2_queue_release(&video->queue);
1019 video->queue.owner = NULL;
1021 mutex_unlock(&video->lock);
1023 vsp1_device_put(video->vsp1);
1025 v4l2_fh_release(file);
1027 file->private_data = NULL;
1032 static struct v4l2_file_operations vsp1_video_fops = {
1033 .owner = THIS_MODULE,
1034 .unlocked_ioctl = video_ioctl2,
1035 .open = vsp1_video_open,
1036 .release = vsp1_video_release,
1037 .poll = vb2_fop_poll,
1038 .mmap = vb2_fop_mmap,
1041 /* -----------------------------------------------------------------------------
1042 * Initialization and Cleanup
1045 int vsp1_video_init(struct vsp1_video *video, struct vsp1_entity *rwpf)
1047 const char *direction;
1050 switch (video->type) {
1051 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1052 direction = "output";
1053 video->pad.flags = MEDIA_PAD_FL_SINK;
1056 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1057 direction = "input";
1058 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1059 video->video.vfl_dir = VFL_DIR_TX;
1068 mutex_init(&video->lock);
1069 spin_lock_init(&video->irqlock);
1070 INIT_LIST_HEAD(&video->irqqueue);
1072 mutex_init(&video->pipe.lock);
1073 spin_lock_init(&video->pipe.irqlock);
1074 INIT_LIST_HEAD(&video->pipe.entities);
1075 init_waitqueue_head(&video->pipe.wq);
1076 video->pipe.state = VSP1_PIPELINE_STOPPED;
1078 /* Initialize the media entity... */
1079 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1083 /* ... and the format ... */
1084 video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
1085 video->format.pixelformat = video->fmtinfo->fourcc;
1086 video->format.colorspace = V4L2_COLORSPACE_SRGB;
1087 video->format.field = V4L2_FIELD_NONE;
1088 video->format.width = VSP1_VIDEO_DEF_WIDTH;
1089 video->format.height = VSP1_VIDEO_DEF_HEIGHT;
1090 video->format.num_planes = 1;
1091 video->format.plane_fmt[0].bytesperline =
1092 video->format.width * video->fmtinfo->bpp[0] / 8;
1093 video->format.plane_fmt[0].sizeimage =
1094 video->format.plane_fmt[0].bytesperline * video->format.height;
1096 /* ... and the video node... */
1097 video->video.v4l2_dev = &video->vsp1->v4l2_dev;
1098 video->video.fops = &vsp1_video_fops;
1099 snprintf(video->video.name, sizeof(video->video.name), "%s %s",
1100 rwpf->subdev.name, direction);
1101 video->video.vfl_type = VFL_TYPE_GRABBER;
1102 video->video.release = video_device_release_empty;
1103 video->video.ioctl_ops = &vsp1_video_ioctl_ops;
1105 video_set_drvdata(&video->video, video);
1107 /* ... and the buffers queue... */
1108 video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
1109 if (IS_ERR(video->alloc_ctx)) {
1110 ret = PTR_ERR(video->alloc_ctx);
1114 video->queue.type = video->type;
1115 video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1116 video->queue.lock = &video->lock;
1117 video->queue.drv_priv = video;
1118 video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer);
1119 video->queue.ops = &vsp1_video_queue_qops;
1120 video->queue.mem_ops = &vb2_dma_contig_memops;
1121 video->queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1122 ret = vb2_queue_init(&video->queue);
1124 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1128 /* ... and register the video device. */
1129 video->video.queue = &video->queue;
1130 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1132 dev_err(video->vsp1->dev, "failed to register video device\n");
1139 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1140 vsp1_video_cleanup(video);
1144 void vsp1_video_cleanup(struct vsp1_video *video)
1146 if (video_is_registered(&video->video))
1147 video_unregister_device(&video->video);
1149 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1150 media_entity_cleanup(&video->video.entity);