[media] v4l: vsp1: Disconnect unused RPFs from the DRM pipeline
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_drm.c
CommitLineData
f3af9572
LP
1/*
2 * vsp1_drm.c -- R-Car VSP1 DRM API
3 *
4 * Copyright (C) 2015 Renesas Electronics Corporation
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/device.h>
15#include <linux/slab.h>
16#include <linux/vsp1.h>
17
18#include <media/media-entity.h>
19#include <media/v4l2-subdev.h>
20
21#include "vsp1.h"
22#include "vsp1_bru.h"
23#include "vsp1_drm.h"
24#include "vsp1_lif.h"
25#include "vsp1_pipe.h"
26#include "vsp1_rwpf.h"
27
28/* -----------------------------------------------------------------------------
29 * Runtime Handling
30 */
31
32static int vsp1_drm_pipeline_run(struct vsp1_pipeline *pipe)
33{
34 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
35 int ret;
36
37 if (vsp1->drm->update) {
38 struct vsp1_entity *entity;
39
40 list_for_each_entry(entity, &pipe->entities, list_pipe) {
3afb1939 41 /* Disconnect unused RPFs from the pipeline. */
f3af9572
LP
42 if (entity->type == VSP1_ENTITY_RPF) {
43 struct vsp1_rwpf *rpf =
44 to_rwpf(&entity->subdev);
45
3afb1939
LP
46 if (!pipe->inputs[rpf->entity.index]) {
47 vsp1_write(entity->vsp1,
48 entity->route->reg,
49 VI6_DPR_NODE_UNUSED);
f3af9572 50 continue;
3afb1939 51 }
f3af9572
LP
52 }
53
54 vsp1_entity_route_setup(entity);
55
56 ret = v4l2_subdev_call(&entity->subdev, video,
57 s_stream, 1);
58 if (ret < 0) {
59 dev_err(vsp1->dev,
60 "DRM pipeline start failure on entity %s\n",
61 entity->subdev.name);
62 return ret;
63 }
64 }
65
66 vsp1->drm->update = false;
67 }
68
69 vsp1_pipeline_run(pipe);
70
71 return 0;
72}
73
74static void vsp1_drm_pipeline_frame_end(struct vsp1_pipeline *pipe)
75{
76 unsigned long flags;
77
78 spin_lock_irqsave(&pipe->irqlock, flags);
79 if (pipe->num_inputs)
80 vsp1_drm_pipeline_run(pipe);
81 spin_unlock_irqrestore(&pipe->irqlock, flags);
82}
83
84/* -----------------------------------------------------------------------------
85 * DU Driver API
86 */
87
88int vsp1_du_init(struct device *dev)
89{
90 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
91
92 if (!vsp1)
93 return -EPROBE_DEFER;
94
95 return 0;
96}
97EXPORT_SYMBOL_GPL(vsp1_du_init);
98
99/**
100 * vsp1_du_setup_lif - Setup the output part of the VSP pipeline
101 * @dev: the VSP device
102 * @width: output frame width in pixels
103 * @height: output frame height in pixels
104 *
105 * Configure the output part of VSP DRM pipeline for the given frame @width and
106 * @height. This sets up formats on the BRU source pad, the WPF0 sink and source
107 * pads, and the LIF sink pad.
108 *
109 * As the media bus code on the BRU source pad is conditioned by the
110 * configuration of the BRU sink 0 pad, we also set up the formats on all BRU
111 * sinks, even if the configuration will be overwritten later by
112 * vsp1_du_setup_rpf(). This ensures that the BRU configuration is set to a well
113 * defined state.
114 *
115 * Return 0 on success or a negative error code on failure.
116 */
117int vsp1_du_setup_lif(struct device *dev, unsigned int width,
118 unsigned int height)
119{
120 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
121 struct vsp1_pipeline *pipe = &vsp1->drm->pipe;
122 struct vsp1_bru *bru = vsp1->bru;
123 struct v4l2_subdev_format format;
124 unsigned int i;
125 int ret;
126
127 dev_dbg(vsp1->dev, "%s: configuring LIF with format %ux%u\n",
128 __func__, width, height);
129
130 if (width == 0 || height == 0) {
131 /* Zero width or height means the CRTC is being disabled, stop
132 * the pipeline and turn the light off.
133 */
134 ret = vsp1_pipeline_stop(pipe);
135 if (ret == -ETIMEDOUT)
136 dev_err(vsp1->dev, "DRM pipeline stop timeout\n");
137
138 media_entity_pipeline_stop(&pipe->output->entity.subdev.entity);
139
140 for (i = 0; i < bru->entity.source_pad; ++i) {
141 bru->inputs[i].rpf = NULL;
142 pipe->inputs[i] = NULL;
143 }
144
145 pipe->num_inputs = 0;
146
147 vsp1_device_put(vsp1);
148
149 dev_dbg(vsp1->dev, "%s: pipeline disabled\n", __func__);
150
151 return 0;
152 }
153
154 /* Configure the format at the BRU sinks and propagate it through the
155 * pipeline.
156 */
157 memset(&format, 0, sizeof(format));
158 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
159
160 for (i = 0; i < bru->entity.source_pad; ++i) {
161 format.pad = i;
162
163 format.format.width = width;
164 format.format.height = height;
165 format.format.code = MEDIA_BUS_FMT_ARGB8888_1X32;
166 format.format.field = V4L2_FIELD_NONE;
167
168 ret = v4l2_subdev_call(&bru->entity.subdev, pad,
169 set_fmt, NULL, &format);
170 if (ret < 0)
171 return ret;
172
173 dev_dbg(vsp1->dev, "%s: set format %ux%u (%x) on BRU pad %u\n",
174 __func__, format.format.width, format.format.height,
175 format.format.code, i);
176 }
177
178 format.pad = bru->entity.source_pad;
179 format.format.width = width;
180 format.format.height = height;
181 format.format.code = MEDIA_BUS_FMT_ARGB8888_1X32;
182 format.format.field = V4L2_FIELD_NONE;
183
184 ret = v4l2_subdev_call(&bru->entity.subdev, pad, set_fmt, NULL,
185 &format);
186 if (ret < 0)
187 return ret;
188
189 dev_dbg(vsp1->dev, "%s: set format %ux%u (%x) on BRU pad %u\n",
190 __func__, format.format.width, format.format.height,
191 format.format.code, i);
192
193 format.pad = RWPF_PAD_SINK;
194 ret = v4l2_subdev_call(&vsp1->wpf[0]->entity.subdev, pad, set_fmt, NULL,
195 &format);
196 if (ret < 0)
197 return ret;
198
199 dev_dbg(vsp1->dev, "%s: set format %ux%u (%x) on WPF0 sink\n",
200 __func__, format.format.width, format.format.height,
201 format.format.code);
202
203 format.pad = RWPF_PAD_SOURCE;
204 ret = v4l2_subdev_call(&vsp1->wpf[0]->entity.subdev, pad, get_fmt, NULL,
205 &format);
206 if (ret < 0)
207 return ret;
208
209 dev_dbg(vsp1->dev, "%s: got format %ux%u (%x) on WPF0 source\n",
210 __func__, format.format.width, format.format.height,
211 format.format.code);
212
213 format.pad = LIF_PAD_SINK;
214 ret = v4l2_subdev_call(&vsp1->lif->entity.subdev, pad, set_fmt, NULL,
215 &format);
216 if (ret < 0)
217 return ret;
218
219 dev_dbg(vsp1->dev, "%s: set format %ux%u (%x) on LIF sink\n",
220 __func__, format.format.width, format.format.height,
221 format.format.code);
222
223 /* Verify that the format at the output of the pipeline matches the
224 * requested frame size and media bus code.
225 */
226 if (format.format.width != width || format.format.height != height ||
227 format.format.code != MEDIA_BUS_FMT_ARGB8888_1X32) {
228 dev_dbg(vsp1->dev, "%s: format mismatch\n", __func__);
229 return -EPIPE;
230 }
231
232 /* Mark the pipeline as streaming and enable the VSP1. This will store
233 * the pipeline pointer in all entities, which the s_stream handlers
234 * will need. We don't start the entities themselves right at this point
235 * as there's no plane configured yet, so we can't start processing
236 * buffers.
237 */
238 ret = vsp1_device_get(vsp1);
239 if (ret < 0)
240 return ret;
241
242 ret = media_entity_pipeline_start(&pipe->output->entity.subdev.entity,
243 &pipe->pipe);
244 if (ret < 0) {
245 dev_dbg(vsp1->dev, "%s: pipeline start failed\n", __func__);
246 vsp1_device_put(vsp1);
247 return ret;
248 }
249
250 dev_dbg(vsp1->dev, "%s: pipeline enabled\n", __func__);
251
252 return 0;
253}
254EXPORT_SYMBOL_GPL(vsp1_du_setup_lif);
255
256/**
257 * vsp1_du_setup_rpf - Setup one RPF input of the VSP pipeline
258 * @dev: the VSP device
259 * @rpf_index: index of the RPF to setup (0-based)
260 * @pixelformat: V4L2 pixel format for the RPF memory input
261 * @pitch: number of bytes per line in the image stored in memory
262 * @mem: DMA addresses of the memory buffers (one per plane)
263 * @src: the source crop rectangle for the RPF
264 * @dst: the destination compose rectangle for the BRU input
265 *
266 * Configure the VSP to perform composition of the image referenced by @mem
267 * through RPF @rpf_index, using the @src crop rectangle and the @dst
268 * composition rectangle. The Z-order is fixed with RPF 0 at the bottom.
269 *
270 * Image format as stored in memory is expressed as a V4L2 @pixelformat value.
271 * As a special case, setting the pixel format to 0 will disable the RPF. The
272 * @pitch, @mem, @src and @dst parameters are ignored in that case. Calling the
273 * function on a disabled RPF is allowed.
274 *
275 * The memory pitch is configurable to allow for padding at end of lines, or
276 * simple for images that extend beyond the crop rectangle boundaries. The
277 * @pitch value is expressed in bytes and applies to all planes for multiplanar
278 * formats.
279 *
280 * The source memory buffer is referenced by the DMA address of its planes in
281 * the @mem array. Up to two planes are supported. The second plane DMA address
282 * is ignored for formats using a single plane.
283 *
284 * This function isn't reentrant, the caller needs to serialize calls.
285 *
286 * TODO: Implement Z-order control by decoupling the RPF index from the BRU
287 * input index.
288 *
289 * Return 0 on success or a negative error code on failure.
290 */
291int vsp1_du_setup_rpf(struct device *dev, unsigned int rpf_index,
292 u32 pixelformat, unsigned int pitch,
293 dma_addr_t mem[2], const struct v4l2_rect *src,
294 const struct v4l2_rect *dst)
295{
296 struct vsp1_device *vsp1 = dev_get_drvdata(dev);
297 struct vsp1_pipeline *pipe = &vsp1->drm->pipe;
298 const struct vsp1_format_info *fmtinfo;
299 struct v4l2_subdev_selection sel;
300 struct v4l2_subdev_format format;
301 struct vsp1_rwpf_memory memory;
302 struct vsp1_rwpf *rpf;
303 unsigned long flags;
304 bool start_stop = false;
305 int ret;
306
307 if (rpf_index >= vsp1->pdata.rpf_count)
308 return -EINVAL;
309
310 rpf = vsp1->rpf[rpf_index];
311
312 if (pixelformat == 0) {
313 dev_dbg(vsp1->dev, "%s: RPF%u: disable requested\n", __func__,
314 rpf_index);
315
316 spin_lock_irqsave(&pipe->irqlock, flags);
317
318 if (pipe->inputs[rpf_index]) {
319 /* Remove the RPF from the pipeline if it was previously
320 * enabled.
321 */
322 vsp1->bru->inputs[rpf_index].rpf = NULL;
323 pipe->inputs[rpf_index] = NULL;
324
325 vsp1->drm->update = true;
326 start_stop = --pipe->num_inputs == 0;
327 }
328
329 spin_unlock_irqrestore(&pipe->irqlock, flags);
330
331 /* Stop the pipeline if we're the last user. */
332 if (start_stop)
333 vsp1_pipeline_stop(pipe);
334
335 return 0;
336 }
337
338 dev_dbg(vsp1->dev,
339 "%s: RPF%u: (%u,%u)/%ux%u -> (%u,%u)/%ux%u (%08x), pitch %u dma { %pad, %pad }\n",
340 __func__, rpf_index,
341 src->left, src->top, src->width, src->height,
342 dst->left, dst->top, dst->width, dst->height,
343 pixelformat, pitch, &mem[0], &mem[1]);
344
345 /* Set the stride at the RPF input. */
346 fmtinfo = vsp1_get_format_info(pixelformat);
347 if (!fmtinfo) {
348 dev_dbg(vsp1->dev, "Unsupport pixel format %08x for RPF\n",
349 pixelformat);
350 return -EINVAL;
351 }
352
353 rpf->fmtinfo = fmtinfo;
354 rpf->format.num_planes = fmtinfo->planes;
355 rpf->format.plane_fmt[0].bytesperline = pitch;
356 rpf->format.plane_fmt[1].bytesperline = pitch;
357
358 /* Configure the format on the RPF sink pad and propagate it up to the
359 * BRU sink pad.
360 */
361 memset(&format, 0, sizeof(format));
362 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
363 format.pad = RWPF_PAD_SINK;
364 format.format.width = src->width + src->left;
365 format.format.height = src->height + src->top;
366 format.format.code = fmtinfo->mbus;
367 format.format.field = V4L2_FIELD_NONE;
368
369 ret = v4l2_subdev_call(&rpf->entity.subdev, pad, set_fmt, NULL,
370 &format);
371 if (ret < 0)
372 return ret;
373
374 dev_dbg(vsp1->dev,
375 "%s: set format %ux%u (%x) on RPF%u sink\n",
376 __func__, format.format.width, format.format.height,
377 format.format.code, rpf->entity.index);
378
379 memset(&sel, 0, sizeof(sel));
380 sel.which = V4L2_SUBDEV_FORMAT_ACTIVE;
381 sel.pad = RWPF_PAD_SINK;
382 sel.target = V4L2_SEL_TGT_CROP;
383 sel.r = *src;
384
385 ret = v4l2_subdev_call(&rpf->entity.subdev, pad, set_selection, NULL,
386 &sel);
387 if (ret < 0)
388 return ret;
389
390 dev_dbg(vsp1->dev,
391 "%s: set selection (%u,%u)/%ux%u on RPF%u sink\n",
392 __func__, sel.r.left, sel.r.top, sel.r.width, sel.r.height,
393 rpf->entity.index);
394
395 /* RPF source, hardcode the format to ARGB8888 to turn on format
396 * conversion if needed.
397 */
398 format.pad = RWPF_PAD_SOURCE;
399
400 ret = v4l2_subdev_call(&rpf->entity.subdev, pad, get_fmt, NULL,
401 &format);
402 if (ret < 0)
403 return ret;
404
405 dev_dbg(vsp1->dev,
406 "%s: got format %ux%u (%x) on RPF%u source\n",
407 __func__, format.format.width, format.format.height,
408 format.format.code, rpf->entity.index);
409
410 format.format.code = MEDIA_BUS_FMT_ARGB8888_1X32;
411
412 ret = v4l2_subdev_call(&rpf->entity.subdev, pad, set_fmt, NULL,
413 &format);
414 if (ret < 0)
415 return ret;
416
417 /* BRU sink, propagate the format from the RPF source. */
418 format.pad = rpf->entity.index;
419
420 ret = v4l2_subdev_call(&vsp1->bru->entity.subdev, pad, set_fmt, NULL,
421 &format);
422 if (ret < 0)
423 return ret;
424
425 dev_dbg(vsp1->dev, "%s: set format %ux%u (%x) on BRU pad %u\n",
426 __func__, format.format.width, format.format.height,
427 format.format.code, format.pad);
428
429 sel.pad = rpf->entity.index;
430 sel.target = V4L2_SEL_TGT_COMPOSE;
431 sel.r = *dst;
432
433 ret = v4l2_subdev_call(&vsp1->bru->entity.subdev, pad, set_selection,
434 NULL, &sel);
435 if (ret < 0)
436 return ret;
437
438 dev_dbg(vsp1->dev,
439 "%s: set selection (%u,%u)/%ux%u on BRU pad %u\n",
440 __func__, sel.r.left, sel.r.top, sel.r.width, sel.r.height,
441 sel.pad);
442
443 /* Store the compose rectangle coordinates in the RPF. */
444 rpf->location.left = dst->left;
445 rpf->location.top = dst->top;
446
447 /* Set the memory buffer address. */
448 memory.num_planes = fmtinfo->planes;
449 memory.addr[0] = mem[0];
450 memory.addr[1] = mem[1];
451
452 rpf->ops->set_memory(rpf, &memory);
453
454 spin_lock_irqsave(&pipe->irqlock, flags);
455
456 /* If the RPF was previously stopped set the BRU input to the RPF and
457 * store the RPF in the pipeline inputs array.
458 */
459 if (!pipe->inputs[rpf->entity.index]) {
460 vsp1->bru->inputs[rpf_index].rpf = rpf;
461 pipe->inputs[rpf->entity.index] = rpf;
462 start_stop = pipe->num_inputs++ == 0;
463 }
464
465 /* Start the pipeline if it's currently stopped. */
466 vsp1->drm->update = true;
467 if (start_stop)
468 vsp1_drm_pipeline_run(pipe);
469
470 spin_unlock_irqrestore(&pipe->irqlock, flags);
471
472 return 0;
473}
474EXPORT_SYMBOL_GPL(vsp1_du_setup_rpf);
475
476/* -----------------------------------------------------------------------------
477 * Initialization
478 */
479
480int vsp1_drm_create_links(struct vsp1_device *vsp1)
481{
482 const u32 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
483 unsigned int i;
484 int ret;
485
486 /* VSPD instances require a BRU to perform composition and a LIF to
487 * output to the DU.
488 */
489 if (!vsp1->bru || !vsp1->lif)
490 return -ENXIO;
491
492 for (i = 0; i < vsp1->pdata.rpf_count; ++i) {
493 struct vsp1_rwpf *rpf = vsp1->rpf[i];
494
495 ret = media_create_pad_link(&rpf->entity.subdev.entity,
496 RWPF_PAD_SOURCE,
497 &vsp1->bru->entity.subdev.entity,
498 i, flags);
499 if (ret < 0)
500 return ret;
501
502 rpf->entity.sink = &vsp1->bru->entity.subdev.entity;
503 rpf->entity.sink_pad = i;
504 }
505
506 ret = media_create_pad_link(&vsp1->bru->entity.subdev.entity,
507 vsp1->bru->entity.source_pad,
508 &vsp1->wpf[0]->entity.subdev.entity,
509 RWPF_PAD_SINK, flags);
510 if (ret < 0)
511 return ret;
512
513 vsp1->bru->entity.sink = &vsp1->wpf[0]->entity.subdev.entity;
514 vsp1->bru->entity.sink_pad = RWPF_PAD_SINK;
515
516 ret = media_create_pad_link(&vsp1->wpf[0]->entity.subdev.entity,
517 RWPF_PAD_SOURCE,
518 &vsp1->lif->entity.subdev.entity,
519 LIF_PAD_SINK, flags);
520 if (ret < 0)
521 return ret;
522
523 return 0;
524}
525
526int vsp1_drm_init(struct vsp1_device *vsp1)
527{
528 struct vsp1_pipeline *pipe;
529 unsigned int i;
530
531 vsp1->drm = devm_kzalloc(vsp1->dev, sizeof(*vsp1->drm), GFP_KERNEL);
532 if (!vsp1->drm)
533 return -ENOMEM;
534
535 pipe = &vsp1->drm->pipe;
536
537 vsp1_pipeline_init(pipe);
538 pipe->frame_end = vsp1_drm_pipeline_frame_end;
539
540 /* The DRM pipeline is static, add entities manually. */
541 for (i = 0; i < vsp1->pdata.rpf_count; ++i) {
542 struct vsp1_rwpf *input = vsp1->rpf[i];
543
544 list_add_tail(&input->entity.list_pipe, &pipe->entities);
545 }
546
547 list_add_tail(&vsp1->bru->entity.list_pipe, &pipe->entities);
548 list_add_tail(&vsp1->wpf[0]->entity.list_pipe, &pipe->entities);
549 list_add_tail(&vsp1->lif->entity.list_pipe, &pipe->entities);
550
551 pipe->bru = &vsp1->bru->entity;
552 pipe->lif = &vsp1->lif->entity;
553 pipe->output = vsp1->wpf[0];
554
555 return 0;
556}
This page took 0.044981 seconds and 5 git commands to generate.