[media] v4l: vsp1: Use display lists with the userspace API
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_video.c
CommitLineData
26e0ca22
LP
1/*
2 * vsp1_video.c -- R-Car VSP1 Video Node
3 *
139c9286 4 * Copyright (C) 2013-2015 Renesas Electronics Corporation
26e0ca22
LP
5 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
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.
12 */
13
14#include <linux/list.h>
15#include <linux/module.h>
16#include <linux/mutex.h>
26e0ca22
LP
17#include <linux/slab.h>
18#include <linux/v4l2-mediabus.h>
19#include <linux/videodev2.h>
dba4a180 20#include <linux/wait.h>
26e0ca22
LP
21
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>
c139990e 27#include <media/videobuf2-v4l2.h>
26e0ca22
LP
28#include <media/videobuf2-dma-contig.h>
29
30#include "vsp1.h"
629bb6d4 31#include "vsp1_bru.h"
351bbf99 32#include "vsp1_dl.h"
26e0ca22 33#include "vsp1_entity.h"
dba4a180 34#include "vsp1_pipe.h"
26e0ca22 35#include "vsp1_rwpf.h"
bdc2df62 36#include "vsp1_uds.h"
26e0ca22
LP
37#include "vsp1_video.h"
38
39#define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
40#define VSP1_VIDEO_DEF_WIDTH 1024
41#define VSP1_VIDEO_DEF_HEIGHT 768
42
43#define VSP1_VIDEO_MIN_WIDTH 2U
44#define VSP1_VIDEO_MAX_WIDTH 8190U
45#define VSP1_VIDEO_MIN_HEIGHT 2U
46#define VSP1_VIDEO_MAX_HEIGHT 8190U
47
48/* -----------------------------------------------------------------------------
49 * Helper functions
50 */
51
26e0ca22
LP
52static struct v4l2_subdev *
53vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
54{
55 struct media_pad *remote;
56
57 remote = media_entity_remote_pad(local);
3efdf62c 58 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
26e0ca22
LP
59 return NULL;
60
61 if (pad)
62 *pad = remote->index;
63
64 return media_entity_to_v4l2_subdev(remote->entity);
65}
66
67static int vsp1_video_verify_format(struct vsp1_video *video)
68{
69 struct v4l2_subdev_format fmt;
70 struct v4l2_subdev *subdev;
71 int ret;
72
73 subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
74 if (subdev == NULL)
75 return -EINVAL;
76
77 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
78 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
79 if (ret < 0)
80 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
81
86960eec
LP
82 if (video->rwpf->fmtinfo->mbus != fmt.format.code ||
83 video->rwpf->format.height != fmt.format.height ||
84 video->rwpf->format.width != fmt.format.width)
26e0ca22
LP
85 return -EINVAL;
86
87 return 0;
88}
89
90static int __vsp1_video_try_format(struct vsp1_video *video,
91 struct v4l2_pix_format_mplane *pix,
92 const struct vsp1_format_info **fmtinfo)
93{
56bfef3e
LP
94 static const u32 xrgb_formats[][2] = {
95 { V4L2_PIX_FMT_RGB444, V4L2_PIX_FMT_XRGB444 },
96 { V4L2_PIX_FMT_RGB555, V4L2_PIX_FMT_XRGB555 },
97 { V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_XBGR32 },
98 { V4L2_PIX_FMT_RGB32, V4L2_PIX_FMT_XRGB32 },
99 };
100
26e0ca22
LP
101 const struct vsp1_format_info *info;
102 unsigned int width = pix->width;
103 unsigned int height = pix->height;
104 unsigned int i;
105
56bfef3e
LP
106 /* Backward compatibility: replace deprecated RGB formats by their XRGB
107 * equivalent. This selects the format older userspace applications want
108 * while still exposing the new format.
109 */
110 for (i = 0; i < ARRAY_SIZE(xrgb_formats); ++i) {
111 if (xrgb_formats[i][0] == pix->pixelformat) {
112 pix->pixelformat = xrgb_formats[i][1];
113 break;
114 }
115 }
116
26e0ca22
LP
117 /* Retrieve format information and select the default format if the
118 * requested format isn't supported.
119 */
120 info = vsp1_get_format_info(pix->pixelformat);
121 if (info == NULL)
122 info = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
123
124 pix->pixelformat = info->fourcc;
125 pix->colorspace = V4L2_COLORSPACE_SRGB;
126 pix->field = V4L2_FIELD_NONE;
127 memset(pix->reserved, 0, sizeof(pix->reserved));
128
129 /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
130 width = round_down(width, info->hsub);
131 height = round_down(height, info->vsub);
132
133 /* Clamp the width and height. */
134 pix->width = clamp(width, VSP1_VIDEO_MIN_WIDTH, VSP1_VIDEO_MAX_WIDTH);
135 pix->height = clamp(height, VSP1_VIDEO_MIN_HEIGHT,
136 VSP1_VIDEO_MAX_HEIGHT);
137
138 /* Compute and clamp the stride and image size. While not documented in
139 * the datasheet, strides not aligned to a multiple of 128 bytes result
140 * in image corruption.
141 */
df5c3e7c 142 for (i = 0; i < min(info->planes, 2U); ++i) {
26e0ca22
LP
143 unsigned int hsub = i > 0 ? info->hsub : 1;
144 unsigned int vsub = i > 0 ? info->vsub : 1;
145 unsigned int align = 128;
146 unsigned int bpl;
147
148 bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
149 pix->width / hsub * info->bpp[i] / 8,
150 round_down(65535U, align));
151
152 pix->plane_fmt[i].bytesperline = round_up(bpl, align);
153 pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
154 * pix->height / vsub;
155 }
156
157 if (info->planes == 3) {
158 /* The second and third planes must have the same stride. */
159 pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
160 pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
161 }
162
163 pix->num_planes = info->planes;
164
165 if (fmtinfo)
166 *fmtinfo = info;
167
168 return 0;
169}
170
26e0ca22
LP
171/* -----------------------------------------------------------------------------
172 * Pipeline Management
173 */
174
945f1276
LP
175static int vsp1_video_pipeline_validate_branch(struct vsp1_pipeline *pipe,
176 struct vsp1_rwpf *input,
177 struct vsp1_rwpf *output)
26e0ca22 178{
54b5a749 179 struct media_entity_enum ent_enum;
6b1446bc 180 struct vsp1_entity *entity;
26e0ca22 181 struct media_pad *pad;
bdc2df62 182 bool bru_found = false;
6b1446bc 183 int ret;
26e0ca22 184
629bb6d4
LP
185 input->location.left = 0;
186 input->location.top = 0;
187
6b1446bc
LP
188 ret = media_entity_enum_init(&ent_enum, &input->entity.vsp1->media_dev);
189 if (ret < 0)
190 return ret;
54b5a749 191
26e0ca22
LP
192 pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
193
194 while (1) {
54b5a749 195 if (pad == NULL) {
6b1446bc 196 ret = -EPIPE;
54b5a749
SA
197 goto out;
198 }
26e0ca22
LP
199
200 /* We've reached a video node, that shouldn't have happened. */
54b5a749 201 if (!is_media_entity_v4l2_subdev(pad->entity)) {
6b1446bc 202 ret = -EPIPE;
54b5a749
SA
203 goto out;
204 }
26e0ca22 205
54b5a749
SA
206 entity = to_vsp1_entity(
207 media_entity_to_v4l2_subdev(pad->entity));
26e0ca22 208
629bb6d4
LP
209 /* A BRU is present in the pipeline, store the compose rectangle
210 * location in the input RPF for use when configuring the RPF.
211 */
212 if (entity->type == VSP1_ENTITY_BRU) {
213 struct vsp1_bru *bru = to_bru(&entity->subdev);
6418b4d6
LP
214 struct v4l2_rect *rect =
215 &bru->inputs[pad->index].compose;
216
217 bru->inputs[pad->index].rpf = input;
629bb6d4
LP
218
219 input->location.left = rect->left;
220 input->location.top = rect->top;
bdc2df62
LP
221
222 bru_found = true;
629bb6d4
LP
223 }
224
26e0ca22
LP
225 /* We've reached the WPF, we're done. */
226 if (entity->type == VSP1_ENTITY_WPF)
227 break;
228
229 /* Ensure the branch has no loop. */
54b5a749
SA
230 if (media_entity_enum_test_and_set(&ent_enum,
231 &entity->subdev.entity)) {
6b1446bc 232 ret = -EPIPE;
54b5a749
SA
233 goto out;
234 }
26e0ca22
LP
235
236 /* UDS can't be chained. */
237 if (entity->type == VSP1_ENTITY_UDS) {
54b5a749 238 if (pipe->uds) {
6b1446bc 239 ret = -EPIPE;
54b5a749
SA
240 goto out;
241 }
bdc2df62
LP
242
243 pipe->uds = entity;
244 pipe->uds_input = bru_found ? pipe->bru
245 : &input->entity;
26e0ca22
LP
246 }
247
248 /* Follow the source link. The link setup operations ensure
249 * that the output fan-out can't be more than one, there is thus
250 * no need to verify here that only a single source link is
251 * activated.
252 */
253 pad = &entity->pads[entity->source_pad];
254 pad = media_entity_remote_pad(pad);
255 }
256
257 /* The last entity must be the output WPF. */
258 if (entity != &output->entity)
6b1446bc 259 ret = -EPIPE;
26e0ca22 260
54b5a749
SA
261out:
262 media_entity_enum_cleanup(&ent_enum);
263
6b1446bc 264 return ret;
26e0ca22
LP
265}
266
945f1276
LP
267static int vsp1_video_pipeline_validate(struct vsp1_pipeline *pipe,
268 struct vsp1_video *video)
26e0ca22
LP
269{
270 struct media_entity_graph graph;
271 struct media_entity *entity = &video->video.entity;
d10c9894 272 struct media_device *mdev = entity->graph_obj.mdev;
26e0ca22
LP
273 unsigned int i;
274 int ret;
275
276 mutex_lock(&mdev->graph_mutex);
277
278 /* Walk the graph to locate the entities and video nodes. */
c1a5f1bc
SA
279 ret = media_entity_graph_walk_init(&graph, mdev);
280 if (ret) {
281 mutex_unlock(&mdev->graph_mutex);
282 return ret;
283 }
284
26e0ca22
LP
285 media_entity_graph_walk_start(&graph, entity);
286
287 while ((entity = media_entity_graph_walk_next(&graph))) {
288 struct v4l2_subdev *subdev;
289 struct vsp1_rwpf *rwpf;
290 struct vsp1_entity *e;
291
02650ebd 292 if (!is_media_entity_v4l2_subdev(entity))
26e0ca22 293 continue;
26e0ca22
LP
294
295 subdev = media_entity_to_v4l2_subdev(entity);
296 e = to_vsp1_entity(subdev);
297 list_add_tail(&e->list_pipe, &pipe->entities);
298
299 if (e->type == VSP1_ENTITY_RPF) {
300 rwpf = to_rwpf(subdev);
96bfa6a5
LP
301 pipe->inputs[rwpf->entity.index] = rwpf;
302 rwpf->video->pipe_index = ++pipe->num_inputs;
26e0ca22
LP
303 } else if (e->type == VSP1_ENTITY_WPF) {
304 rwpf = to_rwpf(subdev);
f8562f21 305 pipe->output = rwpf;
faf2644d 306 rwpf->video->pipe_index = 0;
26e0ca22
LP
307 } else if (e->type == VSP1_ENTITY_LIF) {
308 pipe->lif = e;
629bb6d4
LP
309 } else if (e->type == VSP1_ENTITY_BRU) {
310 pipe->bru = e;
26e0ca22
LP
311 }
312 }
313
314 mutex_unlock(&mdev->graph_mutex);
315
c1a5f1bc
SA
316 media_entity_graph_walk_cleanup(&graph);
317
26e0ca22
LP
318 /* We need one output and at least one input. */
319 if (pipe->num_inputs == 0 || !pipe->output) {
320 ret = -EPIPE;
321 goto error;
322 }
323
324 /* Follow links downstream for each input and make sure the graph
325 * contains no loop and that all branches end at the output WPF.
326 */
5aa2eb3c 327 for (i = 0; i < video->vsp1->info->rpf_count; ++i) {
96bfa6a5
LP
328 if (!pipe->inputs[i])
329 continue;
330
945f1276
LP
331 ret = vsp1_video_pipeline_validate_branch(pipe, pipe->inputs[i],
332 pipe->output);
26e0ca22
LP
333 if (ret < 0)
334 goto error;
335 }
336
337 return 0;
338
339error:
dba4a180 340 vsp1_pipeline_reset(pipe);
26e0ca22
LP
341 return ret;
342}
343
945f1276
LP
344static int vsp1_video_pipeline_init(struct vsp1_pipeline *pipe,
345 struct vsp1_video *video)
26e0ca22
LP
346{
347 int ret;
348
349 mutex_lock(&pipe->lock);
350
351 /* If we're the first user validate and initialize the pipeline. */
352 if (pipe->use_count == 0) {
945f1276 353 ret = vsp1_video_pipeline_validate(pipe, video);
26e0ca22
LP
354 if (ret < 0)
355 goto done;
356 }
357
358 pipe->use_count++;
359 ret = 0;
360
361done:
362 mutex_unlock(&pipe->lock);
363 return ret;
364}
365
945f1276 366static void vsp1_video_pipeline_cleanup(struct vsp1_pipeline *pipe)
26e0ca22
LP
367{
368 mutex_lock(&pipe->lock);
369
370 /* If we're the last user clean up the pipeline. */
6418b4d6 371 if (--pipe->use_count == 0)
dba4a180 372 vsp1_pipeline_reset(pipe);
26e0ca22
LP
373
374 mutex_unlock(&pipe->lock);
375}
376
26e0ca22
LP
377/*
378 * vsp1_video_complete_buffer - Complete the current buffer
379 * @video: the video node
380 *
381 * This function completes the current buffer by filling its sequence number,
382 * time stamp and payload size, and hands it back to the videobuf core.
383 *
3299ba5c
LP
384 * When operating in DU output mode (deep pipeline to the DU through the LIF),
385 * the VSP1 needs to constantly supply frames to the display. In that case, if
386 * no other buffer is queued, reuse the one that has just been processed instead
387 * of handing it back to the videobuf core.
388 *
26e0ca22
LP
389 * Return the next queued buffer or NULL if the queue is empty.
390 */
f7ebf3ca 391static struct vsp1_vb2_buffer *
26e0ca22
LP
392vsp1_video_complete_buffer(struct vsp1_video *video)
393{
3299ba5c 394 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
f7ebf3ca
LP
395 struct vsp1_vb2_buffer *next = NULL;
396 struct vsp1_vb2_buffer *done;
26e0ca22
LP
397 unsigned long flags;
398 unsigned int i;
399
400 spin_lock_irqsave(&video->irqlock, flags);
401
402 if (list_empty(&video->irqqueue)) {
403 spin_unlock_irqrestore(&video->irqlock, flags);
404 return NULL;
405 }
406
407 done = list_first_entry(&video->irqqueue,
f7ebf3ca 408 struct vsp1_vb2_buffer, queue);
3299ba5c
LP
409
410 /* In DU output mode reuse the buffer if the list is singular. */
411 if (pipe->lif && list_is_singular(&video->irqqueue)) {
412 spin_unlock_irqrestore(&video->irqlock, flags);
413 return done;
414 }
415
26e0ca22
LP
416 list_del(&done->queue);
417
418 if (!list_empty(&video->irqqueue))
419 next = list_first_entry(&video->irqqueue,
f7ebf3ca 420 struct vsp1_vb2_buffer, queue);
26e0ca22
LP
421
422 spin_unlock_irqrestore(&video->irqlock, flags);
423
2d700715 424 done->buf.sequence = video->sequence++;
d6dd645e 425 done->buf.vb2_buf.timestamp = ktime_get_ns();
2d700715 426 for (i = 0; i < done->buf.vb2_buf.num_planes; ++i)
b58faa95 427 vb2_set_plane_payload(&done->buf.vb2_buf, i,
351bbf99 428 vb2_plane_size(&done->buf.vb2_buf, i));
2d700715 429 vb2_buffer_done(&done->buf.vb2_buf, VB2_BUF_STATE_DONE);
26e0ca22
LP
430
431 return next;
432}
433
434static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
3f725b7e 435 struct vsp1_rwpf *rwpf)
26e0ca22 436{
3f725b7e 437 struct vsp1_video *video = rwpf->video;
f7ebf3ca 438 struct vsp1_vb2_buffer *buf;
26e0ca22
LP
439 unsigned long flags;
440
441 buf = vsp1_video_complete_buffer(video);
442 if (buf == NULL)
443 return;
444
445 spin_lock_irqsave(&pipe->irqlock, flags);
446
351bbf99 447 video->rwpf->mem = buf->mem;
26e0ca22
LP
448 pipe->buffers_ready |= 1 << video->pipe_index;
449
450 spin_unlock_irqrestore(&pipe->irqlock, flags);
451}
452
351bbf99
LP
453static void vsp1_video_pipeline_run(struct vsp1_pipeline *pipe)
454{
455 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
456 unsigned int i;
457
458 if (!pipe->dl)
459 pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
460
461 for (i = 0; i < vsp1->info->rpf_count; ++i) {
462 struct vsp1_rwpf *rwpf = pipe->inputs[i];
463
464 if (rwpf)
465 vsp1_rwpf_set_memory(rwpf);
466 }
467
468 if (!pipe->lif)
469 vsp1_rwpf_set_memory(pipe->output);
470
471 vsp1_dl_list_commit(pipe->dl);
472 pipe->dl = NULL;
473
474 vsp1_pipeline_run(pipe);
475}
476
3f725b7e
LP
477static void vsp1_video_pipeline_frame_end(struct vsp1_pipeline *pipe)
478{
96bfa6a5 479 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
351bbf99
LP
480 enum vsp1_pipeline_state state;
481 unsigned long flags;
3f725b7e
LP
482 unsigned int i;
483
484 /* Complete buffers on all video nodes. */
5aa2eb3c 485 for (i = 0; i < vsp1->info->rpf_count; ++i) {
96bfa6a5
LP
486 if (!pipe->inputs[i])
487 continue;
488
3f725b7e 489 vsp1_video_frame_end(pipe, pipe->inputs[i]);
96bfa6a5 490 }
3f725b7e 491
351bbf99
LP
492 vsp1_video_frame_end(pipe, pipe->output);
493
494 spin_lock_irqsave(&pipe->irqlock, flags);
495
496 state = pipe->state;
497 pipe->state = VSP1_PIPELINE_STOPPED;
498
499 /* If a stop has been requested, mark the pipeline as stopped and
500 * return. Otherwise restart the pipeline if ready.
501 */
502 if (state == VSP1_PIPELINE_STOPPING)
503 wake_up(&pipe->wq);
504 else if (vsp1_pipeline_ready(pipe))
505 vsp1_video_pipeline_run(pipe);
506
507 spin_unlock_irqrestore(&pipe->irqlock, flags);
3f725b7e
LP
508}
509
26e0ca22
LP
510/* -----------------------------------------------------------------------------
511 * videobuf2 Queue Operations
512 */
513
514static int
df9ecb0c 515vsp1_video_queue_setup(struct vb2_queue *vq,
26e0ca22
LP
516 unsigned int *nbuffers, unsigned int *nplanes,
517 unsigned int sizes[], void *alloc_ctxs[])
518{
519 struct vsp1_video *video = vb2_get_drv_priv(vq);
86960eec 520 const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
26e0ca22
LP
521 unsigned int i;
522
df9ecb0c
HV
523 if (*nplanes) {
524 if (*nplanes != format->num_planes)
26e0ca22
LP
525 return -EINVAL;
526
df9ecb0c
HV
527 for (i = 0; i < *nplanes; i++) {
528 if (sizes[i] < format->plane_fmt[i].sizeimage)
529 return -EINVAL;
530 alloc_ctxs[i] = video->alloc_ctx;
531 }
532 return 0;
26e0ca22
LP
533 }
534
535 *nplanes = format->num_planes;
536
537 for (i = 0; i < format->num_planes; ++i) {
538 sizes[i] = format->plane_fmt[i].sizeimage;
539 alloc_ctxs[i] = video->alloc_ctx;
540 }
541
542 return 0;
543}
544
545static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
546{
2d700715 547 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
26e0ca22 548 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
f7ebf3ca 549 struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
86960eec 550 const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
26e0ca22
LP
551 unsigned int i;
552
553 if (vb->num_planes < format->num_planes)
554 return -EINVAL;
555
26e0ca22 556 for (i = 0; i < vb->num_planes; ++i) {
b58faa95 557 buf->mem.addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
26e0ca22 558
351bbf99 559 if (vb2_plane_size(vb, i) < format->plane_fmt[i].sizeimage)
26e0ca22
LP
560 return -EINVAL;
561 }
562
351bbf99 563 for ( ; i < 3; ++i)
4d346be5 564 buf->mem.addr[i] = 0;
4d346be5 565
26e0ca22
LP
566 return 0;
567}
568
569static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
570{
2d700715 571 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
26e0ca22
LP
572 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
573 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
f7ebf3ca 574 struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
26e0ca22
LP
575 unsigned long flags;
576 bool empty;
577
578 spin_lock_irqsave(&video->irqlock, flags);
579 empty = list_empty(&video->irqqueue);
580 list_add_tail(&buf->queue, &video->irqqueue);
581 spin_unlock_irqrestore(&video->irqlock, flags);
582
583 if (!empty)
584 return;
585
586 spin_lock_irqsave(&pipe->irqlock, flags);
587
351bbf99 588 video->rwpf->mem = buf->mem;
26e0ca22
LP
589 pipe->buffers_ready |= 1 << video->pipe_index;
590
591 if (vb2_is_streaming(&video->queue) &&
592 vsp1_pipeline_ready(pipe))
351bbf99 593 vsp1_video_pipeline_run(pipe);
26e0ca22
LP
594
595 spin_unlock_irqrestore(&pipe->irqlock, flags);
596}
597
351bbf99
LP
598static int vsp1_video_setup_pipeline(struct vsp1_pipeline *pipe)
599{
600 struct vsp1_entity *entity;
601 int ret;
602
603 /* Prepare the display list. */
604 pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
605 if (!pipe->dl)
606 return -ENOMEM;
607
608 if (pipe->uds) {
609 struct vsp1_uds *uds = to_uds(&pipe->uds->subdev);
610
611 /* If a BRU is present in the pipeline before the UDS, the alpha
612 * component doesn't need to be scaled as the BRU output alpha
613 * value is fixed to 255. Otherwise we need to scale the alpha
614 * component only when available at the input RPF.
615 */
616 if (pipe->uds_input->type == VSP1_ENTITY_BRU) {
617 uds->scale_alpha = false;
618 } else {
619 struct vsp1_rwpf *rpf =
620 to_rwpf(&pipe->uds_input->subdev);
621
622 uds->scale_alpha = rpf->fmtinfo->alpha;
623 }
624 }
625
626 list_for_each_entry(entity, &pipe->entities, list_pipe) {
627 vsp1_entity_route_setup(entity);
628
629 ret = v4l2_subdev_call(&entity->subdev, video, s_stream, 1);
630 if (ret < 0)
631 goto error;
632 }
633
634 return 0;
635
636error:
637 vsp1_dl_list_put(pipe->dl);
638 pipe->dl = NULL;
639
640 return ret;
641}
642
26e0ca22
LP
643static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
644{
645 struct vsp1_video *video = vb2_get_drv_priv(vq);
646 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
26e0ca22
LP
647 unsigned long flags;
648 int ret;
649
650 mutex_lock(&pipe->lock);
2f2db2f2 651 if (pipe->stream_count == pipe->num_inputs) {
351bbf99
LP
652 ret = vsp1_video_setup_pipeline(pipe);
653 if (ret < 0) {
654 mutex_unlock(&pipe->lock);
655 return ret;
26e0ca22
LP
656 }
657 }
658
659 pipe->stream_count++;
660 mutex_unlock(&pipe->lock);
661
662 spin_lock_irqsave(&pipe->irqlock, flags);
663 if (vsp1_pipeline_ready(pipe))
351bbf99 664 vsp1_video_pipeline_run(pipe);
26e0ca22
LP
665 spin_unlock_irqrestore(&pipe->irqlock, flags);
666
667 return 0;
668}
669
e37559b2 670static void vsp1_video_stop_streaming(struct vb2_queue *vq)
26e0ca22
LP
671{
672 struct vsp1_video *video = vb2_get_drv_priv(vq);
673 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
f7ebf3ca 674 struct vsp1_vb2_buffer *buffer;
26e0ca22
LP
675 unsigned long flags;
676 int ret;
677
678 mutex_lock(&pipe->lock);
679 if (--pipe->stream_count == 0) {
680 /* Stop the pipeline. */
681 ret = vsp1_pipeline_stop(pipe);
682 if (ret == -ETIMEDOUT)
683 dev_err(video->vsp1->dev, "pipeline stop timeout\n");
351bbf99
LP
684
685 vsp1_dl_list_put(pipe->dl);
686 pipe->dl = NULL;
26e0ca22
LP
687 }
688 mutex_unlock(&pipe->lock);
689
945f1276 690 vsp1_video_pipeline_cleanup(pipe);
26e0ca22
LP
691 media_entity_pipeline_stop(&video->video.entity);
692
693 /* Remove all buffers from the IRQ queue. */
694 spin_lock_irqsave(&video->irqlock, flags);
9df04e9d 695 list_for_each_entry(buffer, &video->irqqueue, queue)
2d700715 696 vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR);
26e0ca22
LP
697 INIT_LIST_HEAD(&video->irqqueue);
698 spin_unlock_irqrestore(&video->irqlock, flags);
26e0ca22
LP
699}
700
701static struct vb2_ops vsp1_video_queue_qops = {
702 .queue_setup = vsp1_video_queue_setup,
703 .buf_prepare = vsp1_video_buffer_prepare,
704 .buf_queue = vsp1_video_buffer_queue,
705 .wait_prepare = vb2_ops_wait_prepare,
706 .wait_finish = vb2_ops_wait_finish,
707 .start_streaming = vsp1_video_start_streaming,
708 .stop_streaming = vsp1_video_stop_streaming,
709};
710
711/* -----------------------------------------------------------------------------
712 * V4L2 ioctls
713 */
714
715static int
716vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
717{
718 struct v4l2_fh *vfh = file->private_data;
719 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
720
721 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
722 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
723 | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
724
725 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
726 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
727 | V4L2_CAP_STREAMING;
728 else
729 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
730 | V4L2_CAP_STREAMING;
731
732 strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
733 strlcpy(cap->card, video->video.name, sizeof(cap->card));
734 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
735 dev_name(video->vsp1->dev));
736
737 return 0;
738}
739
740static int
741vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
742{
743 struct v4l2_fh *vfh = file->private_data;
744 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
745
746 if (format->type != video->queue.type)
747 return -EINVAL;
748
749 mutex_lock(&video->lock);
86960eec 750 format->fmt.pix_mp = video->rwpf->format;
26e0ca22
LP
751 mutex_unlock(&video->lock);
752
753 return 0;
754}
755
756static int
757vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
758{
759 struct v4l2_fh *vfh = file->private_data;
760 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
761
762 if (format->type != video->queue.type)
763 return -EINVAL;
764
765 return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
766}
767
768static int
769vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
770{
771 struct v4l2_fh *vfh = file->private_data;
772 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
773 const struct vsp1_format_info *info;
774 int ret;
775
776 if (format->type != video->queue.type)
777 return -EINVAL;
778
779 ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
780 if (ret < 0)
781 return ret;
782
783 mutex_lock(&video->lock);
784
785 if (vb2_is_busy(&video->queue)) {
786 ret = -EBUSY;
787 goto done;
788 }
789
86960eec
LP
790 video->rwpf->format = format->fmt.pix_mp;
791 video->rwpf->fmtinfo = info;
26e0ca22
LP
792
793done:
794 mutex_unlock(&video->lock);
795 return ret;
796}
797
798static int
799vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
800{
801 struct v4l2_fh *vfh = file->private_data;
802 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
803 struct vsp1_pipeline *pipe;
804 int ret;
805
26e0ca22
LP
806 if (video->queue.owner && video->queue.owner != file->private_data)
807 return -EBUSY;
808
809 video->sequence = 0;
810
811 /* Start streaming on the pipeline. No link touching an entity in the
812 * pipeline can be activated or deactivated once streaming is started.
813 *
814 * Use the VSP1 pipeline object embedded in the first video object that
815 * starts streaming.
816 */
817 pipe = video->video.entity.pipe
818 ? to_vsp1_pipeline(&video->video.entity) : &video->pipe;
819
820 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
821 if (ret < 0)
822 return ret;
823
824 /* Verify that the configured format matches the output of the connected
825 * subdev.
826 */
827 ret = vsp1_video_verify_format(video);
828 if (ret < 0)
829 goto err_stop;
830
945f1276 831 ret = vsp1_video_pipeline_init(pipe, video);
26e0ca22
LP
832 if (ret < 0)
833 goto err_stop;
834
835 /* Start the queue. */
836 ret = vb2_streamon(&video->queue, type);
837 if (ret < 0)
838 goto err_cleanup;
839
840 return 0;
841
842err_cleanup:
945f1276 843 vsp1_video_pipeline_cleanup(pipe);
26e0ca22
LP
844err_stop:
845 media_entity_pipeline_stop(&video->video.entity);
846 return ret;
847}
848
849static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
850 .vidioc_querycap = vsp1_video_querycap,
851 .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
852 .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
853 .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
854 .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
855 .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
856 .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
857 .vidioc_reqbufs = vb2_ioctl_reqbufs,
858 .vidioc_querybuf = vb2_ioctl_querybuf,
859 .vidioc_qbuf = vb2_ioctl_qbuf,
860 .vidioc_dqbuf = vb2_ioctl_dqbuf,
861 .vidioc_create_bufs = vb2_ioctl_create_bufs,
862 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
863 .vidioc_streamon = vsp1_video_streamon,
864 .vidioc_streamoff = vb2_ioctl_streamoff,
865};
866
867/* -----------------------------------------------------------------------------
868 * V4L2 File Operations
869 */
870
871static int vsp1_video_open(struct file *file)
872{
873 struct vsp1_video *video = video_drvdata(file);
874 struct v4l2_fh *vfh;
875 int ret = 0;
876
877 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
878 if (vfh == NULL)
879 return -ENOMEM;
880
881 v4l2_fh_init(vfh, &video->video);
882 v4l2_fh_add(vfh);
883
884 file->private_data = vfh;
885
4c16d6a0
LP
886 ret = vsp1_device_get(video->vsp1);
887 if (ret < 0) {
26e0ca22
LP
888 v4l2_fh_del(vfh);
889 kfree(vfh);
890 }
891
892 return ret;
893}
894
895static int vsp1_video_release(struct file *file)
896{
897 struct vsp1_video *video = video_drvdata(file);
898 struct v4l2_fh *vfh = file->private_data;
899
900 mutex_lock(&video->lock);
901 if (video->queue.owner == vfh) {
902 vb2_queue_release(&video->queue);
903 video->queue.owner = NULL;
904 }
905 mutex_unlock(&video->lock);
906
907 vsp1_device_put(video->vsp1);
908
909 v4l2_fh_release(file);
910
911 file->private_data = NULL;
912
913 return 0;
914}
915
916static struct v4l2_file_operations vsp1_video_fops = {
917 .owner = THIS_MODULE,
918 .unlocked_ioctl = video_ioctl2,
919 .open = vsp1_video_open,
920 .release = vsp1_video_release,
921 .poll = vb2_fop_poll,
922 .mmap = vb2_fop_mmap,
923};
924
925/* -----------------------------------------------------------------------------
926 * Initialization and Cleanup
927 */
928
9d40637a
LP
929struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1,
930 struct vsp1_rwpf *rwpf)
26e0ca22 931{
9d40637a 932 struct vsp1_video *video;
26e0ca22
LP
933 const char *direction;
934 int ret;
935
9d40637a
LP
936 video = devm_kzalloc(vsp1->dev, sizeof(*video), GFP_KERNEL);
937 if (!video)
938 return ERR_PTR(-ENOMEM);
26e0ca22 939
faf2644d 940 rwpf->video = video;
9d40637a
LP
941
942 video->vsp1 = vsp1;
943 video->rwpf = rwpf;
944
945 if (rwpf->entity.type == VSP1_ENTITY_RPF) {
26e0ca22 946 direction = "input";
9d40637a 947 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
26e0ca22
LP
948 video->pad.flags = MEDIA_PAD_FL_SOURCE;
949 video->video.vfl_dir = VFL_DIR_TX;
9d40637a
LP
950 } else {
951 direction = "output";
952 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
953 video->pad.flags = MEDIA_PAD_FL_SINK;
954 video->video.vfl_dir = VFL_DIR_RX;
26e0ca22
LP
955 }
956
26e0ca22
LP
957 mutex_init(&video->lock);
958 spin_lock_init(&video->irqlock);
959 INIT_LIST_HEAD(&video->irqqueue);
960
f294c2f7 961 vsp1_pipeline_init(&video->pipe);
3f725b7e 962 video->pipe.frame_end = vsp1_video_pipeline_frame_end;
26e0ca22
LP
963
964 /* Initialize the media entity... */
ab22e77c 965 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
26e0ca22 966 if (ret < 0)
9d40637a 967 return ERR_PTR(ret);
26e0ca22
LP
968
969 /* ... and the format ... */
86960eec
LP
970 rwpf->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
971 rwpf->format.pixelformat = rwpf->fmtinfo->fourcc;
972 rwpf->format.colorspace = V4L2_COLORSPACE_SRGB;
973 rwpf->format.field = V4L2_FIELD_NONE;
974 rwpf->format.width = VSP1_VIDEO_DEF_WIDTH;
975 rwpf->format.height = VSP1_VIDEO_DEF_HEIGHT;
976 rwpf->format.num_planes = 1;
977 rwpf->format.plane_fmt[0].bytesperline =
978 rwpf->format.width * rwpf->fmtinfo->bpp[0] / 8;
979 rwpf->format.plane_fmt[0].sizeimage =
980 rwpf->format.plane_fmt[0].bytesperline * rwpf->format.height;
26e0ca22
LP
981
982 /* ... and the video node... */
983 video->video.v4l2_dev = &video->vsp1->v4l2_dev;
984 video->video.fops = &vsp1_video_fops;
985 snprintf(video->video.name, sizeof(video->video.name), "%s %s",
8b4a0563 986 rwpf->entity.subdev.name, direction);
26e0ca22
LP
987 video->video.vfl_type = VFL_TYPE_GRABBER;
988 video->video.release = video_device_release_empty;
989 video->video.ioctl_ops = &vsp1_video_ioctl_ops;
990
991 video_set_drvdata(&video->video, video);
992
993 /* ... and the buffers queue... */
994 video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
b317828b
WY
995 if (IS_ERR(video->alloc_ctx)) {
996 ret = PTR_ERR(video->alloc_ctx);
26e0ca22 997 goto error;
b317828b 998 }
26e0ca22
LP
999
1000 video->queue.type = video->type;
1001 video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1002 video->queue.lock = &video->lock;
1003 video->queue.drv_priv = video;
f7ebf3ca 1004 video->queue.buf_struct_size = sizeof(struct vsp1_vb2_buffer);
26e0ca22
LP
1005 video->queue.ops = &vsp1_video_queue_qops;
1006 video->queue.mem_ops = &vb2_dma_contig_memops;
ade48681 1007 video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
26e0ca22
LP
1008 ret = vb2_queue_init(&video->queue);
1009 if (ret < 0) {
1010 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1011 goto error;
1012 }
1013
1014 /* ... and register the video device. */
1015 video->video.queue = &video->queue;
1016 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1017 if (ret < 0) {
1018 dev_err(video->vsp1->dev, "failed to register video device\n");
1019 goto error;
1020 }
1021
9d40637a 1022 return video;
26e0ca22
LP
1023
1024error:
1025 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1026 vsp1_video_cleanup(video);
9d40637a 1027 return ERR_PTR(ret);
26e0ca22
LP
1028}
1029
1030void vsp1_video_cleanup(struct vsp1_video *video)
1031{
1032 if (video_is_registered(&video->video))
1033 video_unregister_device(&video->video);
1034
1035 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1036 media_entity_cleanup(&video->video.entity);
1037}
This page took 0.278032 seconds and 5 git commands to generate.