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/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-v4l2.h>
28 #include <media/videobuf2-dma-contig.h>
32 #include "vsp1_entity.h"
33 #include "vsp1_rwpf.h"
35 #include "vsp1_video.h"
37 #define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
38 #define VSP1_VIDEO_DEF_WIDTH 1024
39 #define VSP1_VIDEO_DEF_HEIGHT 768
41 #define VSP1_VIDEO_MIN_WIDTH 2U
42 #define VSP1_VIDEO_MAX_WIDTH 8190U
43 #define VSP1_VIDEO_MIN_HEIGHT 2U
44 #define VSP1_VIDEO_MAX_HEIGHT 8190U
46 /* -----------------------------------------------------------------------------
50 static const struct vsp1_format_info vsp1_video_formats
[] = {
51 { V4L2_PIX_FMT_RGB332
, MEDIA_BUS_FMT_ARGB8888_1X32
,
52 VI6_FMT_RGB_332
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
53 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
54 1, { 8, 0, 0 }, false, false, 1, 1, false },
55 { V4L2_PIX_FMT_ARGB444
, MEDIA_BUS_FMT_ARGB8888_1X32
,
56 VI6_FMT_ARGB_4444
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
58 1, { 16, 0, 0 }, false, false, 1, 1, true },
59 { V4L2_PIX_FMT_XRGB444
, MEDIA_BUS_FMT_ARGB8888_1X32
,
60 VI6_FMT_XRGB_4444
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
62 1, { 16, 0, 0 }, false, false, 1, 1, true },
63 { V4L2_PIX_FMT_ARGB555
, MEDIA_BUS_FMT_ARGB8888_1X32
,
64 VI6_FMT_ARGB_1555
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
66 1, { 16, 0, 0 }, false, false, 1, 1, true },
67 { V4L2_PIX_FMT_XRGB555
, MEDIA_BUS_FMT_ARGB8888_1X32
,
68 VI6_FMT_XRGB_1555
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
70 1, { 16, 0, 0 }, false, false, 1, 1, false },
71 { V4L2_PIX_FMT_RGB565
, MEDIA_BUS_FMT_ARGB8888_1X32
,
72 VI6_FMT_RGB_565
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
74 1, { 16, 0, 0 }, false, false, 1, 1, false },
75 { V4L2_PIX_FMT_BGR24
, MEDIA_BUS_FMT_ARGB8888_1X32
,
76 VI6_FMT_BGR_888
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
77 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
78 1, { 24, 0, 0 }, false, false, 1, 1, false },
79 { V4L2_PIX_FMT_RGB24
, MEDIA_BUS_FMT_ARGB8888_1X32
,
80 VI6_FMT_RGB_888
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
81 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
82 1, { 24, 0, 0 }, false, false, 1, 1, false },
83 { V4L2_PIX_FMT_ABGR32
, MEDIA_BUS_FMT_ARGB8888_1X32
,
84 VI6_FMT_ARGB_8888
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
,
85 1, { 32, 0, 0 }, false, false, 1, 1, true },
86 { V4L2_PIX_FMT_XBGR32
, MEDIA_BUS_FMT_ARGB8888_1X32
,
87 VI6_FMT_ARGB_8888
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
,
88 1, { 32, 0, 0 }, false, false, 1, 1, false },
89 { V4L2_PIX_FMT_ARGB32
, MEDIA_BUS_FMT_ARGB8888_1X32
,
90 VI6_FMT_ARGB_8888
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
91 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
92 1, { 32, 0, 0 }, false, false, 1, 1, true },
93 { V4L2_PIX_FMT_XRGB32
, MEDIA_BUS_FMT_ARGB8888_1X32
,
94 VI6_FMT_ARGB_8888
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
95 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
96 1, { 32, 0, 0 }, false, false, 1, 1, false },
97 { V4L2_PIX_FMT_UYVY
, MEDIA_BUS_FMT_AYUV8_1X32
,
98 VI6_FMT_YUYV_422
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
99 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
100 1, { 16, 0, 0 }, false, false, 2, 1, false },
101 { V4L2_PIX_FMT_VYUY
, MEDIA_BUS_FMT_AYUV8_1X32
,
102 VI6_FMT_YUYV_422
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
103 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
104 1, { 16, 0, 0 }, false, true, 2, 1, false },
105 { V4L2_PIX_FMT_YUYV
, MEDIA_BUS_FMT_AYUV8_1X32
,
106 VI6_FMT_YUYV_422
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
107 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
108 1, { 16, 0, 0 }, true, false, 2, 1, false },
109 { V4L2_PIX_FMT_YVYU
, MEDIA_BUS_FMT_AYUV8_1X32
,
110 VI6_FMT_YUYV_422
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
111 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
112 1, { 16, 0, 0 }, true, true, 2, 1, false },
113 { V4L2_PIX_FMT_NV12M
, MEDIA_BUS_FMT_AYUV8_1X32
,
114 VI6_FMT_Y_UV_420
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
115 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
116 2, { 8, 16, 0 }, false, false, 2, 2, false },
117 { V4L2_PIX_FMT_NV21M
, MEDIA_BUS_FMT_AYUV8_1X32
,
118 VI6_FMT_Y_UV_420
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
119 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
120 2, { 8, 16, 0 }, false, true, 2, 2, false },
121 { V4L2_PIX_FMT_NV16M
, MEDIA_BUS_FMT_AYUV8_1X32
,
122 VI6_FMT_Y_UV_422
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
123 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
124 2, { 8, 16, 0 }, false, false, 2, 1, false },
125 { V4L2_PIX_FMT_NV61M
, MEDIA_BUS_FMT_AYUV8_1X32
,
126 VI6_FMT_Y_UV_422
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
127 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
128 2, { 8, 16, 0 }, false, true, 2, 1, false },
129 { V4L2_PIX_FMT_YUV420M
, MEDIA_BUS_FMT_AYUV8_1X32
,
130 VI6_FMT_Y_U_V_420
, VI6_RPF_DSWAP_P_LLS
| VI6_RPF_DSWAP_P_LWS
|
131 VI6_RPF_DSWAP_P_WDS
| VI6_RPF_DSWAP_P_BTS
,
132 3, { 8, 8, 8 }, false, false, 2, 2, false },
136 * vsp1_get_format_info - Retrieve format information for a 4CC
137 * @fourcc: the format 4CC
139 * Return a pointer to the format information structure corresponding to the
140 * given V4L2 format 4CC, or NULL if no corresponding format can be found.
142 static const struct vsp1_format_info
*vsp1_get_format_info(u32 fourcc
)
146 for (i
= 0; i
< ARRAY_SIZE(vsp1_video_formats
); ++i
) {
147 const struct vsp1_format_info
*info
= &vsp1_video_formats
[i
];
149 if (info
->fourcc
== fourcc
)
157 static struct v4l2_subdev
*
158 vsp1_video_remote_subdev(struct media_pad
*local
, u32
*pad
)
160 struct media_pad
*remote
;
162 remote
= media_entity_remote_pad(local
);
163 if (remote
== NULL
||
164 media_entity_type(remote
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
168 *pad
= remote
->index
;
170 return media_entity_to_v4l2_subdev(remote
->entity
);
173 static int vsp1_video_verify_format(struct vsp1_video
*video
)
175 struct v4l2_subdev_format fmt
;
176 struct v4l2_subdev
*subdev
;
179 subdev
= vsp1_video_remote_subdev(&video
->pad
, &fmt
.pad
);
183 fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
184 ret
= v4l2_subdev_call(subdev
, pad
, get_fmt
, NULL
, &fmt
);
186 return ret
== -ENOIOCTLCMD
? -EINVAL
: ret
;
188 if (video
->fmtinfo
->mbus
!= fmt
.format
.code
||
189 video
->format
.height
!= fmt
.format
.height
||
190 video
->format
.width
!= fmt
.format
.width
)
196 static int __vsp1_video_try_format(struct vsp1_video
*video
,
197 struct v4l2_pix_format_mplane
*pix
,
198 const struct vsp1_format_info
**fmtinfo
)
200 static const u32 xrgb_formats
[][2] = {
201 { V4L2_PIX_FMT_RGB444
, V4L2_PIX_FMT_XRGB444
},
202 { V4L2_PIX_FMT_RGB555
, V4L2_PIX_FMT_XRGB555
},
203 { V4L2_PIX_FMT_BGR32
, V4L2_PIX_FMT_XBGR32
},
204 { V4L2_PIX_FMT_RGB32
, V4L2_PIX_FMT_XRGB32
},
207 const struct vsp1_format_info
*info
;
208 unsigned int width
= pix
->width
;
209 unsigned int height
= pix
->height
;
212 /* Backward compatibility: replace deprecated RGB formats by their XRGB
213 * equivalent. This selects the format older userspace applications want
214 * while still exposing the new format.
216 for (i
= 0; i
< ARRAY_SIZE(xrgb_formats
); ++i
) {
217 if (xrgb_formats
[i
][0] == pix
->pixelformat
) {
218 pix
->pixelformat
= xrgb_formats
[i
][1];
223 /* Retrieve format information and select the default format if the
224 * requested format isn't supported.
226 info
= vsp1_get_format_info(pix
->pixelformat
);
228 info
= vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT
);
230 pix
->pixelformat
= info
->fourcc
;
231 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
232 pix
->field
= V4L2_FIELD_NONE
;
233 memset(pix
->reserved
, 0, sizeof(pix
->reserved
));
235 /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
236 width
= round_down(width
, info
->hsub
);
237 height
= round_down(height
, info
->vsub
);
239 /* Clamp the width and height. */
240 pix
->width
= clamp(width
, VSP1_VIDEO_MIN_WIDTH
, VSP1_VIDEO_MAX_WIDTH
);
241 pix
->height
= clamp(height
, VSP1_VIDEO_MIN_HEIGHT
,
242 VSP1_VIDEO_MAX_HEIGHT
);
244 /* Compute and clamp the stride and image size. While not documented in
245 * the datasheet, strides not aligned to a multiple of 128 bytes result
246 * in image corruption.
248 for (i
= 0; i
< min(info
->planes
, 2U); ++i
) {
249 unsigned int hsub
= i
> 0 ? info
->hsub
: 1;
250 unsigned int vsub
= i
> 0 ? info
->vsub
: 1;
251 unsigned int align
= 128;
254 bpl
= clamp_t(unsigned int, pix
->plane_fmt
[i
].bytesperline
,
255 pix
->width
/ hsub
* info
->bpp
[i
] / 8,
256 round_down(65535U, align
));
258 pix
->plane_fmt
[i
].bytesperline
= round_up(bpl
, align
);
259 pix
->plane_fmt
[i
].sizeimage
= pix
->plane_fmt
[i
].bytesperline
260 * pix
->height
/ vsub
;
263 if (info
->planes
== 3) {
264 /* The second and third planes must have the same stride. */
265 pix
->plane_fmt
[2].bytesperline
= pix
->plane_fmt
[1].bytesperline
;
266 pix
->plane_fmt
[2].sizeimage
= pix
->plane_fmt
[1].sizeimage
;
269 pix
->num_planes
= info
->planes
;
277 /* -----------------------------------------------------------------------------
278 * Pipeline Management
281 static int vsp1_pipeline_validate_branch(struct vsp1_pipeline
*pipe
,
282 struct vsp1_rwpf
*input
,
283 struct vsp1_rwpf
*output
)
285 struct vsp1_entity
*entity
;
286 unsigned int entities
= 0;
287 struct media_pad
*pad
;
288 bool bru_found
= false;
290 input
->location
.left
= 0;
291 input
->location
.top
= 0;
293 pad
= media_entity_remote_pad(&input
->entity
.pads
[RWPF_PAD_SOURCE
]);
299 /* We've reached a video node, that shouldn't have happened. */
300 if (media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
303 entity
= to_vsp1_entity(media_entity_to_v4l2_subdev(pad
->entity
));
305 /* A BRU is present in the pipeline, store the compose rectangle
306 * location in the input RPF for use when configuring the RPF.
308 if (entity
->type
== VSP1_ENTITY_BRU
) {
309 struct vsp1_bru
*bru
= to_bru(&entity
->subdev
);
310 struct v4l2_rect
*rect
=
311 &bru
->inputs
[pad
->index
].compose
;
313 bru
->inputs
[pad
->index
].rpf
= input
;
315 input
->location
.left
= rect
->left
;
316 input
->location
.top
= rect
->top
;
321 /* We've reached the WPF, we're done. */
322 if (entity
->type
== VSP1_ENTITY_WPF
)
325 /* Ensure the branch has no loop. */
326 if (entities
& (1 << media_entity_id(&entity
->subdev
.entity
)))
329 entities
|= 1 << media_entity_id(&entity
->subdev
.entity
);
331 /* UDS can't be chained. */
332 if (entity
->type
== VSP1_ENTITY_UDS
) {
337 pipe
->uds_input
= bru_found
? pipe
->bru
341 /* Follow the source link. The link setup operations ensure
342 * that the output fan-out can't be more than one, there is thus
343 * no need to verify here that only a single source link is
346 pad
= &entity
->pads
[entity
->source_pad
];
347 pad
= media_entity_remote_pad(pad
);
350 /* The last entity must be the output WPF. */
351 if (entity
!= &output
->entity
)
357 static void __vsp1_pipeline_cleanup(struct vsp1_pipeline
*pipe
)
360 struct vsp1_bru
*bru
= to_bru(&pipe
->bru
->subdev
);
363 for (i
= 0; i
< ARRAY_SIZE(bru
->inputs
); ++i
)
364 bru
->inputs
[i
].rpf
= NULL
;
367 INIT_LIST_HEAD(&pipe
->entities
);
368 pipe
->state
= VSP1_PIPELINE_STOPPED
;
369 pipe
->buffers_ready
= 0;
371 pipe
->num_inputs
= 0;
378 static int vsp1_pipeline_validate(struct vsp1_pipeline
*pipe
,
379 struct vsp1_video
*video
)
381 struct media_entity_graph graph
;
382 struct media_entity
*entity
= &video
->video
.entity
;
383 struct media_device
*mdev
= entity
->parent
;
387 mutex_lock(&mdev
->graph_mutex
);
389 /* Walk the graph to locate the entities and video nodes. */
390 media_entity_graph_walk_start(&graph
, entity
);
392 while ((entity
= media_entity_graph_walk_next(&graph
))) {
393 struct v4l2_subdev
*subdev
;
394 struct vsp1_rwpf
*rwpf
;
395 struct vsp1_entity
*e
;
397 if (media_entity_type(entity
) != MEDIA_ENT_T_V4L2_SUBDEV
) {
402 subdev
= media_entity_to_v4l2_subdev(entity
);
403 e
= to_vsp1_entity(subdev
);
404 list_add_tail(&e
->list_pipe
, &pipe
->entities
);
406 if (e
->type
== VSP1_ENTITY_RPF
) {
407 rwpf
= to_rwpf(subdev
);
408 pipe
->inputs
[pipe
->num_inputs
++] = rwpf
;
409 rwpf
->video
.pipe_index
= pipe
->num_inputs
;
410 } else if (e
->type
== VSP1_ENTITY_WPF
) {
411 rwpf
= to_rwpf(subdev
);
412 pipe
->output
= to_rwpf(subdev
);
413 rwpf
->video
.pipe_index
= 0;
414 } else if (e
->type
== VSP1_ENTITY_LIF
) {
416 } else if (e
->type
== VSP1_ENTITY_BRU
) {
421 mutex_unlock(&mdev
->graph_mutex
);
423 /* We need one output and at least one input. */
424 if (pipe
->num_inputs
== 0 || !pipe
->output
) {
429 /* Follow links downstream for each input and make sure the graph
430 * contains no loop and that all branches end at the output WPF.
432 for (i
= 0; i
< pipe
->num_inputs
; ++i
) {
433 ret
= vsp1_pipeline_validate_branch(pipe
, pipe
->inputs
[i
],
442 __vsp1_pipeline_cleanup(pipe
);
446 static int vsp1_pipeline_init(struct vsp1_pipeline
*pipe
,
447 struct vsp1_video
*video
)
451 mutex_lock(&pipe
->lock
);
453 /* If we're the first user validate and initialize the pipeline. */
454 if (pipe
->use_count
== 0) {
455 ret
= vsp1_pipeline_validate(pipe
, video
);
464 mutex_unlock(&pipe
->lock
);
468 static void vsp1_pipeline_cleanup(struct vsp1_pipeline
*pipe
)
470 mutex_lock(&pipe
->lock
);
472 /* If we're the last user clean up the pipeline. */
473 if (--pipe
->use_count
== 0)
474 __vsp1_pipeline_cleanup(pipe
);
476 mutex_unlock(&pipe
->lock
);
479 static void vsp1_pipeline_run(struct vsp1_pipeline
*pipe
)
481 struct vsp1_device
*vsp1
= pipe
->output
->entity
.vsp1
;
483 vsp1_write(vsp1
, VI6_CMD(pipe
->output
->entity
.index
), VI6_CMD_STRCMD
);
484 pipe
->state
= VSP1_PIPELINE_RUNNING
;
485 pipe
->buffers_ready
= 0;
488 static bool vsp1_pipeline_stopped(struct vsp1_pipeline
*pipe
)
493 spin_lock_irqsave(&pipe
->irqlock
, flags
);
494 stopped
= pipe
->state
== VSP1_PIPELINE_STOPPED
,
495 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
500 static int vsp1_pipeline_stop(struct vsp1_pipeline
*pipe
)
502 struct vsp1_entity
*entity
;
506 spin_lock_irqsave(&pipe
->irqlock
, flags
);
507 if (pipe
->state
== VSP1_PIPELINE_RUNNING
)
508 pipe
->state
= VSP1_PIPELINE_STOPPING
;
509 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
511 ret
= wait_event_timeout(pipe
->wq
, vsp1_pipeline_stopped(pipe
),
512 msecs_to_jiffies(500));
513 ret
= ret
== 0 ? -ETIMEDOUT
: 0;
515 list_for_each_entry(entity
, &pipe
->entities
, list_pipe
) {
516 if (entity
->route
&& entity
->route
->reg
)
517 vsp1_write(entity
->vsp1
, entity
->route
->reg
,
518 VI6_DPR_NODE_UNUSED
);
520 v4l2_subdev_call(&entity
->subdev
, video
, s_stream
, 0);
526 static bool vsp1_pipeline_ready(struct vsp1_pipeline
*pipe
)
530 mask
= ((1 << pipe
->num_inputs
) - 1) << 1;
534 return pipe
->buffers_ready
== mask
;
538 * vsp1_video_complete_buffer - Complete the current buffer
539 * @video: the video node
541 * This function completes the current buffer by filling its sequence number,
542 * time stamp and payload size, and hands it back to the videobuf core.
544 * When operating in DU output mode (deep pipeline to the DU through the LIF),
545 * the VSP1 needs to constantly supply frames to the display. In that case, if
546 * no other buffer is queued, reuse the one that has just been processed instead
547 * of handing it back to the videobuf core.
549 * Return the next queued buffer or NULL if the queue is empty.
551 static struct vsp1_video_buffer
*
552 vsp1_video_complete_buffer(struct vsp1_video
*video
)
554 struct vsp1_pipeline
*pipe
= to_vsp1_pipeline(&video
->video
.entity
);
555 struct vsp1_video_buffer
*next
= NULL
;
556 struct vsp1_video_buffer
*done
;
560 spin_lock_irqsave(&video
->irqlock
, flags
);
562 if (list_empty(&video
->irqqueue
)) {
563 spin_unlock_irqrestore(&video
->irqlock
, flags
);
567 done
= list_first_entry(&video
->irqqueue
,
568 struct vsp1_video_buffer
, queue
);
570 /* In DU output mode reuse the buffer if the list is singular. */
571 if (pipe
->lif
&& list_is_singular(&video
->irqqueue
)) {
572 spin_unlock_irqrestore(&video
->irqlock
, flags
);
576 list_del(&done
->queue
);
578 if (!list_empty(&video
->irqqueue
))
579 next
= list_first_entry(&video
->irqqueue
,
580 struct vsp1_video_buffer
, queue
);
582 spin_unlock_irqrestore(&video
->irqlock
, flags
);
584 done
->buf
.sequence
= video
->sequence
++;
585 done
->buf
.vb2_buf
.timestamp
= ktime_get_ns();
586 for (i
= 0; i
< done
->buf
.vb2_buf
.num_planes
; ++i
)
587 vb2_set_plane_payload(&done
->buf
.vb2_buf
, i
, done
->length
[i
]);
588 vb2_buffer_done(&done
->buf
.vb2_buf
, VB2_BUF_STATE_DONE
);
593 static void vsp1_video_frame_end(struct vsp1_pipeline
*pipe
,
594 struct vsp1_video
*video
)
596 struct vsp1_video_buffer
*buf
;
599 buf
= vsp1_video_complete_buffer(video
);
603 spin_lock_irqsave(&pipe
->irqlock
, flags
);
605 video
->ops
->queue(video
, buf
);
606 pipe
->buffers_ready
|= 1 << video
->pipe_index
;
608 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
611 void vsp1_pipeline_frame_end(struct vsp1_pipeline
*pipe
)
613 enum vsp1_pipeline_state state
;
620 /* Complete buffers on all video nodes. */
621 for (i
= 0; i
< pipe
->num_inputs
; ++i
)
622 vsp1_video_frame_end(pipe
, &pipe
->inputs
[i
]->video
);
625 vsp1_video_frame_end(pipe
, &pipe
->output
->video
);
627 spin_lock_irqsave(&pipe
->irqlock
, flags
);
630 pipe
->state
= VSP1_PIPELINE_STOPPED
;
632 /* If a stop has been requested, mark the pipeline as stopped and
635 if (state
== VSP1_PIPELINE_STOPPING
) {
640 /* Restart the pipeline if ready. */
641 if (vsp1_pipeline_ready(pipe
))
642 vsp1_pipeline_run(pipe
);
645 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
649 * Propagate the alpha value through the pipeline.
651 * As the UDS has restricted scaling capabilities when the alpha component needs
652 * to be scaled, we disable alpha scaling when the UDS input has a fixed alpha
653 * value. The UDS then outputs a fixed alpha value which needs to be programmed
654 * from the input RPF alpha.
656 void vsp1_pipeline_propagate_alpha(struct vsp1_pipeline
*pipe
,
657 struct vsp1_entity
*input
,
660 struct vsp1_entity
*entity
;
661 struct media_pad
*pad
;
663 pad
= media_entity_remote_pad(&input
->pads
[RWPF_PAD_SOURCE
]);
666 if (media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
669 entity
= to_vsp1_entity(media_entity_to_v4l2_subdev(pad
->entity
));
671 /* The BRU background color has a fixed alpha value set to 255,
672 * the output alpha value is thus always equal to 255.
674 if (entity
->type
== VSP1_ENTITY_BRU
)
677 if (entity
->type
== VSP1_ENTITY_UDS
) {
678 struct vsp1_uds
*uds
= to_uds(&entity
->subdev
);
680 vsp1_uds_set_alpha(uds
, alpha
);
684 pad
= &entity
->pads
[entity
->source_pad
];
685 pad
= media_entity_remote_pad(pad
);
689 void vsp1_pipelines_suspend(struct vsp1_device
*vsp1
)
695 /* To avoid increasing the system suspend time needlessly, loop over the
696 * pipelines twice, first to set them all to the stopping state, and then
697 * to wait for the stop to complete.
699 for (i
= 0; i
< vsp1
->pdata
.wpf_count
; ++i
) {
700 struct vsp1_rwpf
*wpf
= vsp1
->wpf
[i
];
701 struct vsp1_pipeline
*pipe
;
706 pipe
= to_vsp1_pipeline(&wpf
->entity
.subdev
.entity
);
710 spin_lock_irqsave(&pipe
->irqlock
, flags
);
711 if (pipe
->state
== VSP1_PIPELINE_RUNNING
)
712 pipe
->state
= VSP1_PIPELINE_STOPPING
;
713 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
716 for (i
= 0; i
< vsp1
->pdata
.wpf_count
; ++i
) {
717 struct vsp1_rwpf
*wpf
= vsp1
->wpf
[i
];
718 struct vsp1_pipeline
*pipe
;
723 pipe
= to_vsp1_pipeline(&wpf
->entity
.subdev
.entity
);
727 ret
= wait_event_timeout(pipe
->wq
, vsp1_pipeline_stopped(pipe
),
728 msecs_to_jiffies(500));
730 dev_warn(vsp1
->dev
, "pipeline %u stop timeout\n",
735 void vsp1_pipelines_resume(struct vsp1_device
*vsp1
)
739 /* Resume pipeline all running pipelines. */
740 for (i
= 0; i
< vsp1
->pdata
.wpf_count
; ++i
) {
741 struct vsp1_rwpf
*wpf
= vsp1
->wpf
[i
];
742 struct vsp1_pipeline
*pipe
;
747 pipe
= to_vsp1_pipeline(&wpf
->entity
.subdev
.entity
);
751 if (vsp1_pipeline_ready(pipe
))
752 vsp1_pipeline_run(pipe
);
756 /* -----------------------------------------------------------------------------
757 * videobuf2 Queue Operations
761 vsp1_video_queue_setup(struct vb2_queue
*vq
,
762 unsigned int *nbuffers
, unsigned int *nplanes
,
763 unsigned int sizes
[], void *alloc_ctxs
[])
765 struct vsp1_video
*video
= vb2_get_drv_priv(vq
);
766 const struct v4l2_pix_format_mplane
*format
= &video
->format
;
770 if (*nplanes
!= format
->num_planes
)
773 for (i
= 0; i
< *nplanes
; i
++) {
774 if (sizes
[i
] < format
->plane_fmt
[i
].sizeimage
)
776 alloc_ctxs
[i
] = video
->alloc_ctx
;
781 *nplanes
= format
->num_planes
;
783 for (i
= 0; i
< format
->num_planes
; ++i
) {
784 sizes
[i
] = format
->plane_fmt
[i
].sizeimage
;
785 alloc_ctxs
[i
] = video
->alloc_ctx
;
791 static int vsp1_video_buffer_prepare(struct vb2_buffer
*vb
)
793 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
794 struct vsp1_video
*video
= vb2_get_drv_priv(vb
->vb2_queue
);
795 struct vsp1_video_buffer
*buf
= to_vsp1_video_buffer(vbuf
);
796 const struct v4l2_pix_format_mplane
*format
= &video
->format
;
799 if (vb
->num_planes
< format
->num_planes
)
802 for (i
= 0; i
< vb
->num_planes
; ++i
) {
803 buf
->addr
[i
] = vb2_dma_contig_plane_dma_addr(vb
, i
);
804 buf
->length
[i
] = vb2_plane_size(vb
, i
);
806 if (buf
->length
[i
] < format
->plane_fmt
[i
].sizeimage
)
813 static void vsp1_video_buffer_queue(struct vb2_buffer
*vb
)
815 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
816 struct vsp1_video
*video
= vb2_get_drv_priv(vb
->vb2_queue
);
817 struct vsp1_pipeline
*pipe
= to_vsp1_pipeline(&video
->video
.entity
);
818 struct vsp1_video_buffer
*buf
= to_vsp1_video_buffer(vbuf
);
822 spin_lock_irqsave(&video
->irqlock
, flags
);
823 empty
= list_empty(&video
->irqqueue
);
824 list_add_tail(&buf
->queue
, &video
->irqqueue
);
825 spin_unlock_irqrestore(&video
->irqlock
, flags
);
830 spin_lock_irqsave(&pipe
->irqlock
, flags
);
832 video
->ops
->queue(video
, buf
);
833 pipe
->buffers_ready
|= 1 << video
->pipe_index
;
835 if (vb2_is_streaming(&video
->queue
) &&
836 vsp1_pipeline_ready(pipe
))
837 vsp1_pipeline_run(pipe
);
839 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
842 static void vsp1_entity_route_setup(struct vsp1_entity
*source
)
844 struct vsp1_entity
*sink
;
846 if (source
->route
->reg
== 0)
849 sink
= container_of(source
->sink
, struct vsp1_entity
, subdev
.entity
);
850 vsp1_write(source
->vsp1
, source
->route
->reg
,
851 sink
->route
->inputs
[source
->sink_pad
]);
854 static int vsp1_video_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
856 struct vsp1_video
*video
= vb2_get_drv_priv(vq
);
857 struct vsp1_pipeline
*pipe
= to_vsp1_pipeline(&video
->video
.entity
);
858 struct vsp1_entity
*entity
;
862 mutex_lock(&pipe
->lock
);
863 if (pipe
->stream_count
== pipe
->num_video
- 1) {
865 struct vsp1_uds
*uds
= to_uds(&pipe
->uds
->subdev
);
867 /* If a BRU is present in the pipeline before the UDS,
868 * the alpha component doesn't need to be scaled as the
869 * BRU output alpha value is fixed to 255. Otherwise we
870 * need to scale the alpha component only when available
873 if (pipe
->uds_input
->type
== VSP1_ENTITY_BRU
) {
874 uds
->scale_alpha
= false;
876 struct vsp1_rwpf
*rpf
=
877 to_rwpf(&pipe
->uds_input
->subdev
);
879 uds
->scale_alpha
= rpf
->video
.fmtinfo
->alpha
;
883 list_for_each_entry(entity
, &pipe
->entities
, list_pipe
) {
884 vsp1_entity_route_setup(entity
);
886 ret
= v4l2_subdev_call(&entity
->subdev
, video
,
889 mutex_unlock(&pipe
->lock
);
895 pipe
->stream_count
++;
896 mutex_unlock(&pipe
->lock
);
898 spin_lock_irqsave(&pipe
->irqlock
, flags
);
899 if (vsp1_pipeline_ready(pipe
))
900 vsp1_pipeline_run(pipe
);
901 spin_unlock_irqrestore(&pipe
->irqlock
, flags
);
906 static void vsp1_video_stop_streaming(struct vb2_queue
*vq
)
908 struct vsp1_video
*video
= vb2_get_drv_priv(vq
);
909 struct vsp1_pipeline
*pipe
= to_vsp1_pipeline(&video
->video
.entity
);
910 struct vsp1_video_buffer
*buffer
;
914 mutex_lock(&pipe
->lock
);
915 if (--pipe
->stream_count
== 0) {
916 /* Stop the pipeline. */
917 ret
= vsp1_pipeline_stop(pipe
);
918 if (ret
== -ETIMEDOUT
)
919 dev_err(video
->vsp1
->dev
, "pipeline stop timeout\n");
921 mutex_unlock(&pipe
->lock
);
923 vsp1_pipeline_cleanup(pipe
);
924 media_entity_pipeline_stop(&video
->video
.entity
);
926 /* Remove all buffers from the IRQ queue. */
927 spin_lock_irqsave(&video
->irqlock
, flags
);
928 list_for_each_entry(buffer
, &video
->irqqueue
, queue
)
929 vb2_buffer_done(&buffer
->buf
.vb2_buf
, VB2_BUF_STATE_ERROR
);
930 INIT_LIST_HEAD(&video
->irqqueue
);
931 spin_unlock_irqrestore(&video
->irqlock
, flags
);
934 static struct vb2_ops vsp1_video_queue_qops
= {
935 .queue_setup
= vsp1_video_queue_setup
,
936 .buf_prepare
= vsp1_video_buffer_prepare
,
937 .buf_queue
= vsp1_video_buffer_queue
,
938 .wait_prepare
= vb2_ops_wait_prepare
,
939 .wait_finish
= vb2_ops_wait_finish
,
940 .start_streaming
= vsp1_video_start_streaming
,
941 .stop_streaming
= vsp1_video_stop_streaming
,
944 /* -----------------------------------------------------------------------------
949 vsp1_video_querycap(struct file
*file
, void *fh
, struct v4l2_capability
*cap
)
951 struct v4l2_fh
*vfh
= file
->private_data
;
952 struct vsp1_video
*video
= to_vsp1_video(vfh
->vdev
);
954 cap
->capabilities
= V4L2_CAP_DEVICE_CAPS
| V4L2_CAP_STREAMING
955 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
956 | V4L2_CAP_VIDEO_OUTPUT_MPLANE
;
958 if (video
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
959 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE_MPLANE
960 | V4L2_CAP_STREAMING
;
962 cap
->device_caps
= V4L2_CAP_VIDEO_OUTPUT_MPLANE
963 | V4L2_CAP_STREAMING
;
965 strlcpy(cap
->driver
, "vsp1", sizeof(cap
->driver
));
966 strlcpy(cap
->card
, video
->video
.name
, sizeof(cap
->card
));
967 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
968 dev_name(video
->vsp1
->dev
));
974 vsp1_video_get_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
976 struct v4l2_fh
*vfh
= file
->private_data
;
977 struct vsp1_video
*video
= to_vsp1_video(vfh
->vdev
);
979 if (format
->type
!= video
->queue
.type
)
982 mutex_lock(&video
->lock
);
983 format
->fmt
.pix_mp
= video
->format
;
984 mutex_unlock(&video
->lock
);
990 vsp1_video_try_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
992 struct v4l2_fh
*vfh
= file
->private_data
;
993 struct vsp1_video
*video
= to_vsp1_video(vfh
->vdev
);
995 if (format
->type
!= video
->queue
.type
)
998 return __vsp1_video_try_format(video
, &format
->fmt
.pix_mp
, NULL
);
1002 vsp1_video_set_format(struct file
*file
, void *fh
, struct v4l2_format
*format
)
1004 struct v4l2_fh
*vfh
= file
->private_data
;
1005 struct vsp1_video
*video
= to_vsp1_video(vfh
->vdev
);
1006 const struct vsp1_format_info
*info
;
1009 if (format
->type
!= video
->queue
.type
)
1012 ret
= __vsp1_video_try_format(video
, &format
->fmt
.pix_mp
, &info
);
1016 mutex_lock(&video
->lock
);
1018 if (vb2_is_busy(&video
->queue
)) {
1023 video
->format
= format
->fmt
.pix_mp
;
1024 video
->fmtinfo
= info
;
1027 mutex_unlock(&video
->lock
);
1032 vsp1_video_streamon(struct file
*file
, void *fh
, enum v4l2_buf_type type
)
1034 struct v4l2_fh
*vfh
= file
->private_data
;
1035 struct vsp1_video
*video
= to_vsp1_video(vfh
->vdev
);
1036 struct vsp1_pipeline
*pipe
;
1039 if (video
->queue
.owner
&& video
->queue
.owner
!= file
->private_data
)
1042 video
->sequence
= 0;
1044 /* Start streaming on the pipeline. No link touching an entity in the
1045 * pipeline can be activated or deactivated once streaming is started.
1047 * Use the VSP1 pipeline object embedded in the first video object that
1050 pipe
= video
->video
.entity
.pipe
1051 ? to_vsp1_pipeline(&video
->video
.entity
) : &video
->pipe
;
1053 ret
= media_entity_pipeline_start(&video
->video
.entity
, &pipe
->pipe
);
1057 /* Verify that the configured format matches the output of the connected
1060 ret
= vsp1_video_verify_format(video
);
1064 ret
= vsp1_pipeline_init(pipe
, video
);
1068 /* Start the queue. */
1069 ret
= vb2_streamon(&video
->queue
, type
);
1076 vsp1_pipeline_cleanup(pipe
);
1078 media_entity_pipeline_stop(&video
->video
.entity
);
1082 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops
= {
1083 .vidioc_querycap
= vsp1_video_querycap
,
1084 .vidioc_g_fmt_vid_cap_mplane
= vsp1_video_get_format
,
1085 .vidioc_s_fmt_vid_cap_mplane
= vsp1_video_set_format
,
1086 .vidioc_try_fmt_vid_cap_mplane
= vsp1_video_try_format
,
1087 .vidioc_g_fmt_vid_out_mplane
= vsp1_video_get_format
,
1088 .vidioc_s_fmt_vid_out_mplane
= vsp1_video_set_format
,
1089 .vidioc_try_fmt_vid_out_mplane
= vsp1_video_try_format
,
1090 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1091 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1092 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1093 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1094 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1095 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1096 .vidioc_streamon
= vsp1_video_streamon
,
1097 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1100 /* -----------------------------------------------------------------------------
1101 * V4L2 File Operations
1104 static int vsp1_video_open(struct file
*file
)
1106 struct vsp1_video
*video
= video_drvdata(file
);
1107 struct v4l2_fh
*vfh
;
1110 vfh
= kzalloc(sizeof(*vfh
), GFP_KERNEL
);
1114 v4l2_fh_init(vfh
, &video
->video
);
1117 file
->private_data
= vfh
;
1119 ret
= vsp1_device_get(video
->vsp1
);
1128 static int vsp1_video_release(struct file
*file
)
1130 struct vsp1_video
*video
= video_drvdata(file
);
1131 struct v4l2_fh
*vfh
= file
->private_data
;
1133 mutex_lock(&video
->lock
);
1134 if (video
->queue
.owner
== vfh
) {
1135 vb2_queue_release(&video
->queue
);
1136 video
->queue
.owner
= NULL
;
1138 mutex_unlock(&video
->lock
);
1140 vsp1_device_put(video
->vsp1
);
1142 v4l2_fh_release(file
);
1144 file
->private_data
= NULL
;
1149 static struct v4l2_file_operations vsp1_video_fops
= {
1150 .owner
= THIS_MODULE
,
1151 .unlocked_ioctl
= video_ioctl2
,
1152 .open
= vsp1_video_open
,
1153 .release
= vsp1_video_release
,
1154 .poll
= vb2_fop_poll
,
1155 .mmap
= vb2_fop_mmap
,
1158 /* -----------------------------------------------------------------------------
1159 * Initialization and Cleanup
1162 int vsp1_video_init(struct vsp1_video
*video
, struct vsp1_entity
*rwpf
)
1164 const char *direction
;
1167 switch (video
->type
) {
1168 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
:
1169 direction
= "output";
1170 video
->pad
.flags
= MEDIA_PAD_FL_SINK
;
1173 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
:
1174 direction
= "input";
1175 video
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1176 video
->video
.vfl_dir
= VFL_DIR_TX
;
1185 mutex_init(&video
->lock
);
1186 spin_lock_init(&video
->irqlock
);
1187 INIT_LIST_HEAD(&video
->irqqueue
);
1189 mutex_init(&video
->pipe
.lock
);
1190 spin_lock_init(&video
->pipe
.irqlock
);
1191 INIT_LIST_HEAD(&video
->pipe
.entities
);
1192 init_waitqueue_head(&video
->pipe
.wq
);
1193 video
->pipe
.state
= VSP1_PIPELINE_STOPPED
;
1195 /* Initialize the media entity... */
1196 ret
= media_entity_init(&video
->video
.entity
, 1, &video
->pad
);
1200 /* ... and the format ... */
1201 video
->fmtinfo
= vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT
);
1202 video
->format
.pixelformat
= video
->fmtinfo
->fourcc
;
1203 video
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
1204 video
->format
.field
= V4L2_FIELD_NONE
;
1205 video
->format
.width
= VSP1_VIDEO_DEF_WIDTH
;
1206 video
->format
.height
= VSP1_VIDEO_DEF_HEIGHT
;
1207 video
->format
.num_planes
= 1;
1208 video
->format
.plane_fmt
[0].bytesperline
=
1209 video
->format
.width
* video
->fmtinfo
->bpp
[0] / 8;
1210 video
->format
.plane_fmt
[0].sizeimage
=
1211 video
->format
.plane_fmt
[0].bytesperline
* video
->format
.height
;
1213 /* ... and the video node... */
1214 video
->video
.v4l2_dev
= &video
->vsp1
->v4l2_dev
;
1215 video
->video
.fops
= &vsp1_video_fops
;
1216 snprintf(video
->video
.name
, sizeof(video
->video
.name
), "%s %s",
1217 rwpf
->subdev
.name
, direction
);
1218 video
->video
.vfl_type
= VFL_TYPE_GRABBER
;
1219 video
->video
.release
= video_device_release_empty
;
1220 video
->video
.ioctl_ops
= &vsp1_video_ioctl_ops
;
1222 video_set_drvdata(&video
->video
, video
);
1224 /* ... and the buffers queue... */
1225 video
->alloc_ctx
= vb2_dma_contig_init_ctx(video
->vsp1
->dev
);
1226 if (IS_ERR(video
->alloc_ctx
)) {
1227 ret
= PTR_ERR(video
->alloc_ctx
);
1231 video
->queue
.type
= video
->type
;
1232 video
->queue
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
1233 video
->queue
.lock
= &video
->lock
;
1234 video
->queue
.drv_priv
= video
;
1235 video
->queue
.buf_struct_size
= sizeof(struct vsp1_video_buffer
);
1236 video
->queue
.ops
= &vsp1_video_queue_qops
;
1237 video
->queue
.mem_ops
= &vb2_dma_contig_memops
;
1238 video
->queue
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
1239 ret
= vb2_queue_init(&video
->queue
);
1241 dev_err(video
->vsp1
->dev
, "failed to initialize vb2 queue\n");
1245 /* ... and register the video device. */
1246 video
->video
.queue
= &video
->queue
;
1247 ret
= video_register_device(&video
->video
, VFL_TYPE_GRABBER
, -1);
1249 dev_err(video
->vsp1
->dev
, "failed to register video device\n");
1256 vb2_dma_contig_cleanup_ctx(video
->alloc_ctx
);
1257 vsp1_video_cleanup(video
);
1261 void vsp1_video_cleanup(struct vsp1_video
*video
)
1263 if (video_is_registered(&video
->video
))
1264 video_unregister_device(&video
->video
);
1266 vb2_dma_contig_cleanup_ctx(video
->alloc_ctx
);
1267 media_entity_cleanup(&video
->video
.entity
);