[media] v4l: vsp1: Support runtime modification of controls
[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
76c29755
LP
175/*
176 * vsp1_video_complete_buffer - Complete the current buffer
177 * @video: the video node
178 *
179 * This function completes the current buffer by filling its sequence number,
180 * time stamp and payload size, and hands it back to the videobuf core.
181 *
182 * When operating in DU output mode (deep pipeline to the DU through the LIF),
183 * the VSP1 needs to constantly supply frames to the display. In that case, if
184 * no other buffer is queued, reuse the one that has just been processed instead
185 * of handing it back to the videobuf core.
186 *
187 * Return the next queued buffer or NULL if the queue is empty.
188 */
189static struct vsp1_vb2_buffer *
190vsp1_video_complete_buffer(struct vsp1_video *video)
191{
192 struct vsp1_pipeline *pipe = video->rwpf->pipe;
193 struct vsp1_vb2_buffer *next = NULL;
194 struct vsp1_vb2_buffer *done;
195 unsigned long flags;
196 unsigned int i;
197
198 spin_lock_irqsave(&video->irqlock, flags);
199
200 if (list_empty(&video->irqqueue)) {
201 spin_unlock_irqrestore(&video->irqlock, flags);
202 return NULL;
203 }
204
205 done = list_first_entry(&video->irqqueue,
206 struct vsp1_vb2_buffer, queue);
207
208 /* In DU output mode reuse the buffer if the list is singular. */
209 if (pipe->lif && list_is_singular(&video->irqqueue)) {
210 spin_unlock_irqrestore(&video->irqlock, flags);
211 return done;
212 }
213
214 list_del(&done->queue);
215
216 if (!list_empty(&video->irqqueue))
217 next = list_first_entry(&video->irqqueue,
218 struct vsp1_vb2_buffer, queue);
219
220 spin_unlock_irqrestore(&video->irqlock, flags);
221
0c1a41b5 222 done->buf.sequence = pipe->sequence;
76c29755
LP
223 done->buf.vb2_buf.timestamp = ktime_get_ns();
224 for (i = 0; i < done->buf.vb2_buf.num_planes; ++i)
225 vb2_set_plane_payload(&done->buf.vb2_buf, i,
226 vb2_plane_size(&done->buf.vb2_buf, i));
227 vb2_buffer_done(&done->buf.vb2_buf, VB2_BUF_STATE_DONE);
228
229 return next;
230}
231
232static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
233 struct vsp1_rwpf *rwpf)
234{
235 struct vsp1_video *video = rwpf->video;
236 struct vsp1_vb2_buffer *buf;
237 unsigned long flags;
238
239 buf = vsp1_video_complete_buffer(video);
240 if (buf == NULL)
241 return;
242
243 spin_lock_irqsave(&pipe->irqlock, flags);
244
245 video->rwpf->mem = buf->mem;
246 pipe->buffers_ready |= 1 << video->pipe_index;
247
248 spin_unlock_irqrestore(&pipe->irqlock, flags);
249}
250
251static void vsp1_video_pipeline_run(struct vsp1_pipeline *pipe)
252{
253 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
fc845e52 254 struct vsp1_entity *entity;
76c29755
LP
255 unsigned int i;
256
257 if (!pipe->dl)
258 pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
259
fc845e52
LP
260 list_for_each_entry(entity, &pipe->entities, list_pipe) {
261 if (entity->ops->configure)
262 entity->ops->configure(entity, pipe, pipe->dl, false);
263 }
264
76c29755
LP
265 for (i = 0; i < vsp1->info->rpf_count; ++i) {
266 struct vsp1_rwpf *rwpf = pipe->inputs[i];
267
268 if (rwpf)
269 vsp1_rwpf_set_memory(rwpf, pipe->dl);
270 }
271
272 if (!pipe->lif)
273 vsp1_rwpf_set_memory(pipe->output, pipe->dl);
274
275 vsp1_dl_list_commit(pipe->dl);
276 pipe->dl = NULL;
277
278 vsp1_pipeline_run(pipe);
279}
280
281static void vsp1_video_pipeline_frame_end(struct vsp1_pipeline *pipe)
282{
283 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
284 enum vsp1_pipeline_state state;
285 unsigned long flags;
286 unsigned int i;
287
288 /* Complete buffers on all video nodes. */
289 for (i = 0; i < vsp1->info->rpf_count; ++i) {
290 if (!pipe->inputs[i])
291 continue;
292
293 vsp1_video_frame_end(pipe, pipe->inputs[i]);
294 }
295
296 vsp1_video_frame_end(pipe, pipe->output);
297
298 spin_lock_irqsave(&pipe->irqlock, flags);
299
300 state = pipe->state;
301 pipe->state = VSP1_PIPELINE_STOPPED;
302
303 /* If a stop has been requested, mark the pipeline as stopped and
304 * return. Otherwise restart the pipeline if ready.
305 */
306 if (state == VSP1_PIPELINE_STOPPING)
307 wake_up(&pipe->wq);
308 else if (vsp1_pipeline_ready(pipe))
309 vsp1_video_pipeline_run(pipe);
310
311 spin_unlock_irqrestore(&pipe->irqlock, flags);
312}
313
d2219824
LP
314static int vsp1_video_pipeline_build_branch(struct vsp1_pipeline *pipe,
315 struct vsp1_rwpf *input,
316 struct vsp1_rwpf *output)
26e0ca22 317{
54b5a749 318 struct media_entity_enum ent_enum;
6b1446bc 319 struct vsp1_entity *entity;
26e0ca22 320 struct media_pad *pad;
bdc2df62 321 bool bru_found = false;
6b1446bc 322 int ret;
26e0ca22 323
6b1446bc
LP
324 ret = media_entity_enum_init(&ent_enum, &input->entity.vsp1->media_dev);
325 if (ret < 0)
326 return ret;
54b5a749 327
26e0ca22
LP
328 pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
329
330 while (1) {
54b5a749 331 if (pad == NULL) {
6b1446bc 332 ret = -EPIPE;
54b5a749
SA
333 goto out;
334 }
26e0ca22
LP
335
336 /* We've reached a video node, that shouldn't have happened. */
54b5a749 337 if (!is_media_entity_v4l2_subdev(pad->entity)) {
6b1446bc 338 ret = -EPIPE;
54b5a749
SA
339 goto out;
340 }
26e0ca22 341
54b5a749
SA
342 entity = to_vsp1_entity(
343 media_entity_to_v4l2_subdev(pad->entity));
26e0ca22 344
b7e5107e
LP
345 /* A BRU is present in the pipeline, store the BRU input pad
346 * number in the input RPF for use when configuring the RPF.
629bb6d4
LP
347 */
348 if (entity->type == VSP1_ENTITY_BRU) {
349 struct vsp1_bru *bru = to_bru(&entity->subdev);
6418b4d6
LP
350
351 bru->inputs[pad->index].rpf = input;
b7e5107e 352 input->bru_input = pad->index;
bdc2df62
LP
353
354 bru_found = true;
629bb6d4
LP
355 }
356
26e0ca22
LP
357 /* We've reached the WPF, we're done. */
358 if (entity->type == VSP1_ENTITY_WPF)
359 break;
360
361 /* Ensure the branch has no loop. */
54b5a749
SA
362 if (media_entity_enum_test_and_set(&ent_enum,
363 &entity->subdev.entity)) {
6b1446bc 364 ret = -EPIPE;
54b5a749
SA
365 goto out;
366 }
26e0ca22
LP
367
368 /* UDS can't be chained. */
369 if (entity->type == VSP1_ENTITY_UDS) {
54b5a749 370 if (pipe->uds) {
6b1446bc 371 ret = -EPIPE;
54b5a749
SA
372 goto out;
373 }
bdc2df62
LP
374
375 pipe->uds = entity;
376 pipe->uds_input = bru_found ? pipe->bru
377 : &input->entity;
26e0ca22
LP
378 }
379
380 /* Follow the source link. The link setup operations ensure
381 * that the output fan-out can't be more than one, there is thus
382 * no need to verify here that only a single source link is
383 * activated.
384 */
385 pad = &entity->pads[entity->source_pad];
386 pad = media_entity_remote_pad(pad);
387 }
388
389 /* The last entity must be the output WPF. */
390 if (entity != &output->entity)
6b1446bc 391 ret = -EPIPE;
26e0ca22 392
54b5a749
SA
393out:
394 media_entity_enum_cleanup(&ent_enum);
395
6b1446bc 396 return ret;
26e0ca22
LP
397}
398
d2219824
LP
399static int vsp1_video_pipeline_build(struct vsp1_pipeline *pipe,
400 struct vsp1_video *video)
26e0ca22
LP
401{
402 struct media_entity_graph graph;
403 struct media_entity *entity = &video->video.entity;
d10c9894 404 struct media_device *mdev = entity->graph_obj.mdev;
26e0ca22
LP
405 unsigned int i;
406 int ret;
407
26e0ca22 408 /* Walk the graph to locate the entities and video nodes. */
c1a5f1bc 409 ret = media_entity_graph_walk_init(&graph, mdev);
a0cdac56 410 if (ret)
c1a5f1bc 411 return ret;
c1a5f1bc 412
26e0ca22
LP
413 media_entity_graph_walk_start(&graph, entity);
414
415 while ((entity = media_entity_graph_walk_next(&graph))) {
416 struct v4l2_subdev *subdev;
417 struct vsp1_rwpf *rwpf;
418 struct vsp1_entity *e;
419
02650ebd 420 if (!is_media_entity_v4l2_subdev(entity))
26e0ca22 421 continue;
26e0ca22
LP
422
423 subdev = media_entity_to_v4l2_subdev(entity);
424 e = to_vsp1_entity(subdev);
425 list_add_tail(&e->list_pipe, &pipe->entities);
426
427 if (e->type == VSP1_ENTITY_RPF) {
428 rwpf = to_rwpf(subdev);
96bfa6a5
LP
429 pipe->inputs[rwpf->entity.index] = rwpf;
430 rwpf->video->pipe_index = ++pipe->num_inputs;
ff7e97c9 431 rwpf->pipe = pipe;
26e0ca22
LP
432 } else if (e->type == VSP1_ENTITY_WPF) {
433 rwpf = to_rwpf(subdev);
f8562f21 434 pipe->output = rwpf;
faf2644d 435 rwpf->video->pipe_index = 0;
ff7e97c9 436 rwpf->pipe = pipe;
26e0ca22
LP
437 } else if (e->type == VSP1_ENTITY_LIF) {
438 pipe->lif = e;
629bb6d4
LP
439 } else if (e->type == VSP1_ENTITY_BRU) {
440 pipe->bru = e;
26e0ca22
LP
441 }
442 }
443
c1a5f1bc
SA
444 media_entity_graph_walk_cleanup(&graph);
445
26e0ca22 446 /* We need one output and at least one input. */
a0cdac56
LP
447 if (pipe->num_inputs == 0 || !pipe->output)
448 return -EPIPE;
26e0ca22
LP
449
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.
452 */
5aa2eb3c 453 for (i = 0; i < video->vsp1->info->rpf_count; ++i) {
96bfa6a5
LP
454 if (!pipe->inputs[i])
455 continue;
456
d2219824
LP
457 ret = vsp1_video_pipeline_build_branch(pipe, pipe->inputs[i],
458 pipe->output);
26e0ca22 459 if (ret < 0)
a0cdac56 460 return ret;
26e0ca22
LP
461 }
462
463 return 0;
26e0ca22
LP
464}
465
945f1276
LP
466static int vsp1_video_pipeline_init(struct vsp1_pipeline *pipe,
467 struct vsp1_video *video)
26e0ca22 468{
a0cdac56
LP
469 vsp1_pipeline_init(pipe);
470
471 pipe->frame_end = vsp1_video_pipeline_frame_end;
472
473 return vsp1_video_pipeline_build(pipe, video);
474}
475
476static struct vsp1_pipeline *vsp1_video_pipeline_get(struct vsp1_video *video)
477{
478 struct vsp1_pipeline *pipe;
26e0ca22
LP
479 int ret;
480
a0cdac56
LP
481 /* Get a pipeline object for the video node. If a pipeline has already
482 * been allocated just increment its reference count and return it.
483 * Otherwise allocate a new pipeline and initialize it, it will be freed
484 * when the last reference is released.
485 */
486 if (!video->rwpf->pipe) {
487 pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
488 if (!pipe)
489 return ERR_PTR(-ENOMEM);
26e0ca22 490
a0cdac56
LP
491 ret = vsp1_video_pipeline_init(pipe, video);
492 if (ret < 0) {
493 vsp1_pipeline_reset(pipe);
494 kfree(pipe);
495 return ERR_PTR(ret);
496 }
497 } else {
498 pipe = video->rwpf->pipe;
499 kref_get(&pipe->kref);
26e0ca22
LP
500 }
501
a0cdac56 502 return pipe;
26e0ca22
LP
503}
504
a0cdac56 505static void vsp1_video_pipeline_release(struct kref *kref)
26e0ca22 506{
a0cdac56 507 struct vsp1_pipeline *pipe = container_of(kref, typeof(*pipe), kref);
26e0ca22 508
a0cdac56
LP
509 vsp1_pipeline_reset(pipe);
510 kfree(pipe);
511}
26e0ca22 512
a0cdac56
LP
513static void vsp1_video_pipeline_put(struct vsp1_pipeline *pipe)
514{
515 struct media_device *mdev = &pipe->output->entity.vsp1->media_dev;
516
517 mutex_lock(&mdev->graph_mutex);
518 kref_put(&pipe->kref, vsp1_video_pipeline_release);
519 mutex_unlock(&mdev->graph_mutex);
26e0ca22
LP
520}
521
26e0ca22
LP
522/* -----------------------------------------------------------------------------
523 * videobuf2 Queue Operations
524 */
525
526static int
df9ecb0c 527vsp1_video_queue_setup(struct vb2_queue *vq,
26e0ca22
LP
528 unsigned int *nbuffers, unsigned int *nplanes,
529 unsigned int sizes[], void *alloc_ctxs[])
530{
531 struct vsp1_video *video = vb2_get_drv_priv(vq);
86960eec 532 const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
26e0ca22
LP
533 unsigned int i;
534
df9ecb0c
HV
535 if (*nplanes) {
536 if (*nplanes != format->num_planes)
26e0ca22
LP
537 return -EINVAL;
538
df9ecb0c
HV
539 for (i = 0; i < *nplanes; i++) {
540 if (sizes[i] < format->plane_fmt[i].sizeimage)
541 return -EINVAL;
542 alloc_ctxs[i] = video->alloc_ctx;
543 }
544 return 0;
26e0ca22
LP
545 }
546
547 *nplanes = format->num_planes;
548
549 for (i = 0; i < format->num_planes; ++i) {
550 sizes[i] = format->plane_fmt[i].sizeimage;
551 alloc_ctxs[i] = video->alloc_ctx;
552 }
553
554 return 0;
555}
556
557static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
558{
2d700715 559 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
26e0ca22 560 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
f7ebf3ca 561 struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
86960eec 562 const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
26e0ca22
LP
563 unsigned int i;
564
565 if (vb->num_planes < format->num_planes)
566 return -EINVAL;
567
26e0ca22 568 for (i = 0; i < vb->num_planes; ++i) {
b58faa95 569 buf->mem.addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
26e0ca22 570
351bbf99 571 if (vb2_plane_size(vb, i) < format->plane_fmt[i].sizeimage)
26e0ca22
LP
572 return -EINVAL;
573 }
574
351bbf99 575 for ( ; i < 3; ++i)
4d346be5 576 buf->mem.addr[i] = 0;
4d346be5 577
26e0ca22
LP
578 return 0;
579}
580
581static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
582{
2d700715 583 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
26e0ca22 584 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
ff7e97c9 585 struct vsp1_pipeline *pipe = video->rwpf->pipe;
f7ebf3ca 586 struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
26e0ca22
LP
587 unsigned long flags;
588 bool empty;
589
590 spin_lock_irqsave(&video->irqlock, flags);
591 empty = list_empty(&video->irqqueue);
592 list_add_tail(&buf->queue, &video->irqqueue);
593 spin_unlock_irqrestore(&video->irqlock, flags);
594
595 if (!empty)
596 return;
597
598 spin_lock_irqsave(&pipe->irqlock, flags);
599
351bbf99 600 video->rwpf->mem = buf->mem;
26e0ca22
LP
601 pipe->buffers_ready |= 1 << video->pipe_index;
602
603 if (vb2_is_streaming(&video->queue) &&
604 vsp1_pipeline_ready(pipe))
351bbf99 605 vsp1_video_pipeline_run(pipe);
26e0ca22
LP
606
607 spin_unlock_irqrestore(&pipe->irqlock, flags);
608}
609
351bbf99
LP
610static int vsp1_video_setup_pipeline(struct vsp1_pipeline *pipe)
611{
612 struct vsp1_entity *entity;
351bbf99
LP
613
614 /* Prepare the display list. */
615 pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
616 if (!pipe->dl)
617 return -ENOMEM;
618
619 if (pipe->uds) {
620 struct vsp1_uds *uds = to_uds(&pipe->uds->subdev);
621
622 /* If a BRU is present in the pipeline before the UDS, the alpha
623 * component doesn't need to be scaled as the BRU output alpha
624 * value is fixed to 255. Otherwise we need to scale the alpha
625 * component only when available at the input RPF.
626 */
627 if (pipe->uds_input->type == VSP1_ENTITY_BRU) {
628 uds->scale_alpha = false;
629 } else {
630 struct vsp1_rwpf *rpf =
631 to_rwpf(&pipe->uds_input->subdev);
632
633 uds->scale_alpha = rpf->fmtinfo->alpha;
634 }
635 }
636
637 list_for_each_entry(entity, &pipe->entities, list_pipe) {
5e8dbbf3 638 vsp1_entity_route_setup(entity, pipe->dl);
351bbf99 639
7b905f05 640 if (entity->ops->configure)
fc845e52 641 entity->ops->configure(entity, pipe, pipe->dl, true);
351bbf99
LP
642 }
643
7b905f05 644 return 0;
351bbf99
LP
645}
646
26e0ca22
LP
647static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
648{
649 struct vsp1_video *video = vb2_get_drv_priv(vq);
ff7e97c9 650 struct vsp1_pipeline *pipe = video->rwpf->pipe;
26e0ca22
LP
651 unsigned long flags;
652 int ret;
653
654 mutex_lock(&pipe->lock);
2f2db2f2 655 if (pipe->stream_count == pipe->num_inputs) {
351bbf99
LP
656 ret = vsp1_video_setup_pipeline(pipe);
657 if (ret < 0) {
658 mutex_unlock(&pipe->lock);
659 return ret;
26e0ca22
LP
660 }
661 }
662
663 pipe->stream_count++;
664 mutex_unlock(&pipe->lock);
665
666 spin_lock_irqsave(&pipe->irqlock, flags);
667 if (vsp1_pipeline_ready(pipe))
351bbf99 668 vsp1_video_pipeline_run(pipe);
26e0ca22
LP
669 spin_unlock_irqrestore(&pipe->irqlock, flags);
670
671 return 0;
672}
673
e37559b2 674static void vsp1_video_stop_streaming(struct vb2_queue *vq)
26e0ca22
LP
675{
676 struct vsp1_video *video = vb2_get_drv_priv(vq);
ff7e97c9 677 struct vsp1_pipeline *pipe = video->rwpf->pipe;
f7ebf3ca 678 struct vsp1_vb2_buffer *buffer;
26e0ca22
LP
679 unsigned long flags;
680 int ret;
681
682 mutex_lock(&pipe->lock);
b4dfb9b3 683 if (--pipe->stream_count == pipe->num_inputs) {
26e0ca22
LP
684 /* Stop the pipeline. */
685 ret = vsp1_pipeline_stop(pipe);
686 if (ret == -ETIMEDOUT)
687 dev_err(video->vsp1->dev, "pipeline stop timeout\n");
351bbf99
LP
688
689 vsp1_dl_list_put(pipe->dl);
690 pipe->dl = NULL;
26e0ca22
LP
691 }
692 mutex_unlock(&pipe->lock);
693
26e0ca22 694 media_entity_pipeline_stop(&video->video.entity);
a0cdac56 695 vsp1_video_pipeline_put(pipe);
26e0ca22
LP
696
697 /* Remove all buffers from the IRQ queue. */
698 spin_lock_irqsave(&video->irqlock, flags);
9df04e9d 699 list_for_each_entry(buffer, &video->irqqueue, queue)
2d700715 700 vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR);
26e0ca22
LP
701 INIT_LIST_HEAD(&video->irqqueue);
702 spin_unlock_irqrestore(&video->irqlock, flags);
26e0ca22
LP
703}
704
eb9163d3 705static const struct vb2_ops vsp1_video_queue_qops = {
26e0ca22
LP
706 .queue_setup = vsp1_video_queue_setup,
707 .buf_prepare = vsp1_video_buffer_prepare,
708 .buf_queue = vsp1_video_buffer_queue,
709 .wait_prepare = vb2_ops_wait_prepare,
710 .wait_finish = vb2_ops_wait_finish,
711 .start_streaming = vsp1_video_start_streaming,
712 .stop_streaming = vsp1_video_stop_streaming,
713};
714
715/* -----------------------------------------------------------------------------
716 * V4L2 ioctls
717 */
718
719static int
720vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
721{
722 struct v4l2_fh *vfh = file->private_data;
723 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
724
725 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
726 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
727 | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
728
729 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
730 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
731 | V4L2_CAP_STREAMING;
732 else
733 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
734 | V4L2_CAP_STREAMING;
735
736 strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
737 strlcpy(cap->card, video->video.name, sizeof(cap->card));
738 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
739 dev_name(video->vsp1->dev));
740
741 return 0;
742}
743
744static int
745vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
746{
747 struct v4l2_fh *vfh = file->private_data;
748 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
749
750 if (format->type != video->queue.type)
751 return -EINVAL;
752
753 mutex_lock(&video->lock);
86960eec 754 format->fmt.pix_mp = video->rwpf->format;
26e0ca22
LP
755 mutex_unlock(&video->lock);
756
757 return 0;
758}
759
760static int
761vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
762{
763 struct v4l2_fh *vfh = file->private_data;
764 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
765
766 if (format->type != video->queue.type)
767 return -EINVAL;
768
769 return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
770}
771
772static int
773vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
774{
775 struct v4l2_fh *vfh = file->private_data;
776 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
777 const struct vsp1_format_info *info;
778 int ret;
779
780 if (format->type != video->queue.type)
781 return -EINVAL;
782
783 ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
784 if (ret < 0)
785 return ret;
786
787 mutex_lock(&video->lock);
788
789 if (vb2_is_busy(&video->queue)) {
790 ret = -EBUSY;
791 goto done;
792 }
793
86960eec
LP
794 video->rwpf->format = format->fmt.pix_mp;
795 video->rwpf->fmtinfo = info;
26e0ca22
LP
796
797done:
798 mutex_unlock(&video->lock);
799 return ret;
800}
801
802static int
803vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
804{
805 struct v4l2_fh *vfh = file->private_data;
806 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
a0cdac56 807 struct media_device *mdev = &video->vsp1->media_dev;
26e0ca22
LP
808 struct vsp1_pipeline *pipe;
809 int ret;
810
26e0ca22
LP
811 if (video->queue.owner && video->queue.owner != file->private_data)
812 return -EBUSY;
813
a0cdac56
LP
814 /* Get a pipeline for the video node and start streaming on it. No link
815 * touching an entity in the pipeline can be activated or deactivated
816 * once streaming is started.
26e0ca22 817 */
a0cdac56 818 mutex_lock(&mdev->graph_mutex);
26e0ca22 819
a0cdac56
LP
820 pipe = vsp1_video_pipeline_get(video);
821 if (IS_ERR(pipe)) {
822 mutex_unlock(&mdev->graph_mutex);
823 return PTR_ERR(pipe);
824 }
825
826 ret = __media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
827 if (ret < 0) {
828 mutex_unlock(&mdev->graph_mutex);
829 goto err_pipe;
830 }
831
832 mutex_unlock(&mdev->graph_mutex);
26e0ca22
LP
833
834 /* Verify that the configured format matches the output of the connected
835 * subdev.
836 */
837 ret = vsp1_video_verify_format(video);
838 if (ret < 0)
839 goto err_stop;
840
26e0ca22
LP
841 /* Start the queue. */
842 ret = vb2_streamon(&video->queue, type);
843 if (ret < 0)
a0cdac56 844 goto err_stop;
26e0ca22
LP
845
846 return 0;
847
26e0ca22
LP
848err_stop:
849 media_entity_pipeline_stop(&video->video.entity);
a0cdac56
LP
850err_pipe:
851 vsp1_video_pipeline_put(pipe);
26e0ca22
LP
852 return ret;
853}
854
855static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
856 .vidioc_querycap = vsp1_video_querycap,
857 .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
858 .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
859 .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
860 .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
861 .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
862 .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
863 .vidioc_reqbufs = vb2_ioctl_reqbufs,
864 .vidioc_querybuf = vb2_ioctl_querybuf,
865 .vidioc_qbuf = vb2_ioctl_qbuf,
866 .vidioc_dqbuf = vb2_ioctl_dqbuf,
867 .vidioc_create_bufs = vb2_ioctl_create_bufs,
868 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
869 .vidioc_streamon = vsp1_video_streamon,
870 .vidioc_streamoff = vb2_ioctl_streamoff,
871};
872
873/* -----------------------------------------------------------------------------
874 * V4L2 File Operations
875 */
876
877static int vsp1_video_open(struct file *file)
878{
879 struct vsp1_video *video = video_drvdata(file);
880 struct v4l2_fh *vfh;
881 int ret = 0;
882
883 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
884 if (vfh == NULL)
885 return -ENOMEM;
886
887 v4l2_fh_init(vfh, &video->video);
888 v4l2_fh_add(vfh);
889
890 file->private_data = vfh;
891
4c16d6a0
LP
892 ret = vsp1_device_get(video->vsp1);
893 if (ret < 0) {
26e0ca22
LP
894 v4l2_fh_del(vfh);
895 kfree(vfh);
896 }
897
898 return ret;
899}
900
901static int vsp1_video_release(struct file *file)
902{
903 struct vsp1_video *video = video_drvdata(file);
904 struct v4l2_fh *vfh = file->private_data;
905
906 mutex_lock(&video->lock);
907 if (video->queue.owner == vfh) {
908 vb2_queue_release(&video->queue);
909 video->queue.owner = NULL;
910 }
911 mutex_unlock(&video->lock);
912
913 vsp1_device_put(video->vsp1);
914
915 v4l2_fh_release(file);
916
917 file->private_data = NULL;
918
919 return 0;
920}
921
eb9163d3 922static const struct v4l2_file_operations vsp1_video_fops = {
26e0ca22
LP
923 .owner = THIS_MODULE,
924 .unlocked_ioctl = video_ioctl2,
925 .open = vsp1_video_open,
926 .release = vsp1_video_release,
927 .poll = vb2_fop_poll,
928 .mmap = vb2_fop_mmap,
929};
930
931/* -----------------------------------------------------------------------------
932 * Initialization and Cleanup
933 */
934
9d40637a
LP
935struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1,
936 struct vsp1_rwpf *rwpf)
26e0ca22 937{
9d40637a 938 struct vsp1_video *video;
26e0ca22
LP
939 const char *direction;
940 int ret;
941
9d40637a
LP
942 video = devm_kzalloc(vsp1->dev, sizeof(*video), GFP_KERNEL);
943 if (!video)
944 return ERR_PTR(-ENOMEM);
26e0ca22 945
faf2644d 946 rwpf->video = video;
9d40637a
LP
947
948 video->vsp1 = vsp1;
949 video->rwpf = rwpf;
950
951 if (rwpf->entity.type == VSP1_ENTITY_RPF) {
26e0ca22 952 direction = "input";
9d40637a 953 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
26e0ca22
LP
954 video->pad.flags = MEDIA_PAD_FL_SOURCE;
955 video->video.vfl_dir = VFL_DIR_TX;
9d40637a
LP
956 } else {
957 direction = "output";
958 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
959 video->pad.flags = MEDIA_PAD_FL_SINK;
960 video->video.vfl_dir = VFL_DIR_RX;
26e0ca22
LP
961 }
962
26e0ca22
LP
963 mutex_init(&video->lock);
964 spin_lock_init(&video->irqlock);
965 INIT_LIST_HEAD(&video->irqqueue);
966
26e0ca22 967 /* Initialize the media entity... */
ab22e77c 968 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
26e0ca22 969 if (ret < 0)
9d40637a 970 return ERR_PTR(ret);
26e0ca22
LP
971
972 /* ... and the format ... */
b911605d 973 rwpf->format.pixelformat = VSP1_VIDEO_DEF_FORMAT;
86960eec
LP
974 rwpf->format.width = VSP1_VIDEO_DEF_WIDTH;
975 rwpf->format.height = VSP1_VIDEO_DEF_HEIGHT;
b911605d 976 __vsp1_video_try_format(video, &rwpf->format, &rwpf->fmtinfo);
26e0ca22
LP
977
978 /* ... and the video node... */
979 video->video.v4l2_dev = &video->vsp1->v4l2_dev;
980 video->video.fops = &vsp1_video_fops;
981 snprintf(video->video.name, sizeof(video->video.name), "%s %s",
8b4a0563 982 rwpf->entity.subdev.name, direction);
26e0ca22
LP
983 video->video.vfl_type = VFL_TYPE_GRABBER;
984 video->video.release = video_device_release_empty;
985 video->video.ioctl_ops = &vsp1_video_ioctl_ops;
986
987 video_set_drvdata(&video->video, video);
988
989 /* ... and the buffers queue... */
990 video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
b317828b
WY
991 if (IS_ERR(video->alloc_ctx)) {
992 ret = PTR_ERR(video->alloc_ctx);
26e0ca22 993 goto error;
b317828b 994 }
26e0ca22
LP
995
996 video->queue.type = video->type;
997 video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
998 video->queue.lock = &video->lock;
999 video->queue.drv_priv = video;
f7ebf3ca 1000 video->queue.buf_struct_size = sizeof(struct vsp1_vb2_buffer);
26e0ca22
LP
1001 video->queue.ops = &vsp1_video_queue_qops;
1002 video->queue.mem_ops = &vb2_dma_contig_memops;
ade48681 1003 video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
26e0ca22
LP
1004 ret = vb2_queue_init(&video->queue);
1005 if (ret < 0) {
1006 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1007 goto error;
1008 }
1009
1010 /* ... and register the video device. */
1011 video->video.queue = &video->queue;
1012 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1013 if (ret < 0) {
1014 dev_err(video->vsp1->dev, "failed to register video device\n");
1015 goto error;
1016 }
1017
9d40637a 1018 return video;
26e0ca22
LP
1019
1020error:
1021 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1022 vsp1_video_cleanup(video);
9d40637a 1023 return ERR_PTR(ret);
26e0ca22
LP
1024}
1025
1026void vsp1_video_cleanup(struct vsp1_video *video)
1027{
1028 if (video_is_registered(&video->video))
1029 video_unregister_device(&video->video);
1030
1031 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1032 media_entity_cleanup(&video->video.entity);
1033}
This page took 0.278089 seconds and 5 git commands to generate.