[media] v4l: vsp1: Make the userspace API optional
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_pipe.c
CommitLineData
dba4a180
LP
1/*
2 * vsp1_pipe.c -- R-Car VSP1 Pipeline
3 *
4 * Copyright (C) 2013-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/list.h>
15#include <linux/sched.h>
16#include <linux/wait.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_entity.h"
24#include "vsp1_pipe.h"
25#include "vsp1_rwpf.h"
26#include "vsp1_uds.h"
27
c618b185
LP
28/* -----------------------------------------------------------------------------
29 * Helper Functions
30 */
31
32static const struct vsp1_format_info vsp1_video_formats[] = {
33 { V4L2_PIX_FMT_RGB332, MEDIA_BUS_FMT_ARGB8888_1X32,
34 VI6_FMT_RGB_332, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
35 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
36 1, { 8, 0, 0 }, false, false, 1, 1, false },
37 { V4L2_PIX_FMT_ARGB444, MEDIA_BUS_FMT_ARGB8888_1X32,
38 VI6_FMT_ARGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
39 VI6_RPF_DSWAP_P_WDS,
40 1, { 16, 0, 0 }, false, false, 1, 1, true },
41 { V4L2_PIX_FMT_XRGB444, MEDIA_BUS_FMT_ARGB8888_1X32,
42 VI6_FMT_XRGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
43 VI6_RPF_DSWAP_P_WDS,
44 1, { 16, 0, 0 }, false, false, 1, 1, true },
45 { V4L2_PIX_FMT_ARGB555, MEDIA_BUS_FMT_ARGB8888_1X32,
46 VI6_FMT_ARGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
47 VI6_RPF_DSWAP_P_WDS,
48 1, { 16, 0, 0 }, false, false, 1, 1, true },
49 { V4L2_PIX_FMT_XRGB555, MEDIA_BUS_FMT_ARGB8888_1X32,
50 VI6_FMT_XRGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
51 VI6_RPF_DSWAP_P_WDS,
52 1, { 16, 0, 0 }, false, false, 1, 1, false },
53 { V4L2_PIX_FMT_RGB565, MEDIA_BUS_FMT_ARGB8888_1X32,
54 VI6_FMT_RGB_565, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
55 VI6_RPF_DSWAP_P_WDS,
56 1, { 16, 0, 0 }, false, false, 1, 1, false },
57 { V4L2_PIX_FMT_BGR24, MEDIA_BUS_FMT_ARGB8888_1X32,
58 VI6_FMT_BGR_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
59 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
60 1, { 24, 0, 0 }, false, false, 1, 1, false },
61 { V4L2_PIX_FMT_RGB24, MEDIA_BUS_FMT_ARGB8888_1X32,
62 VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
63 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
64 1, { 24, 0, 0 }, false, false, 1, 1, false },
65 { V4L2_PIX_FMT_ABGR32, MEDIA_BUS_FMT_ARGB8888_1X32,
66 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
67 1, { 32, 0, 0 }, false, false, 1, 1, true },
68 { V4L2_PIX_FMT_XBGR32, MEDIA_BUS_FMT_ARGB8888_1X32,
69 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
70 1, { 32, 0, 0 }, false, false, 1, 1, false },
71 { V4L2_PIX_FMT_ARGB32, MEDIA_BUS_FMT_ARGB8888_1X32,
72 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
73 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
74 1, { 32, 0, 0 }, false, false, 1, 1, true },
75 { V4L2_PIX_FMT_XRGB32, MEDIA_BUS_FMT_ARGB8888_1X32,
76 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
77 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
78 1, { 32, 0, 0 }, false, false, 1, 1, false },
79 { V4L2_PIX_FMT_UYVY, MEDIA_BUS_FMT_AYUV8_1X32,
80 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
81 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
82 1, { 16, 0, 0 }, false, false, 2, 1, false },
83 { V4L2_PIX_FMT_VYUY, MEDIA_BUS_FMT_AYUV8_1X32,
84 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
85 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
86 1, { 16, 0, 0 }, false, true, 2, 1, false },
87 { V4L2_PIX_FMT_YUYV, MEDIA_BUS_FMT_AYUV8_1X32,
88 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
89 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
90 1, { 16, 0, 0 }, true, false, 2, 1, false },
91 { V4L2_PIX_FMT_YVYU, MEDIA_BUS_FMT_AYUV8_1X32,
92 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
93 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
94 1, { 16, 0, 0 }, true, true, 2, 1, false },
95 { V4L2_PIX_FMT_NV12M, MEDIA_BUS_FMT_AYUV8_1X32,
96 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
97 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
98 2, { 8, 16, 0 }, false, false, 2, 2, false },
99 { V4L2_PIX_FMT_NV21M, MEDIA_BUS_FMT_AYUV8_1X32,
100 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
101 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
102 2, { 8, 16, 0 }, false, true, 2, 2, false },
103 { V4L2_PIX_FMT_NV16M, MEDIA_BUS_FMT_AYUV8_1X32,
104 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
105 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
106 2, { 8, 16, 0 }, false, false, 2, 1, false },
107 { V4L2_PIX_FMT_NV61M, MEDIA_BUS_FMT_AYUV8_1X32,
108 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
109 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
110 2, { 8, 16, 0 }, false, true, 2, 1, false },
111 { V4L2_PIX_FMT_YUV420M, MEDIA_BUS_FMT_AYUV8_1X32,
112 VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
113 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
114 3, { 8, 8, 8 }, false, false, 2, 2, false },
115 { V4L2_PIX_FMT_YVU420M, MEDIA_BUS_FMT_AYUV8_1X32,
116 VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
117 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
118 3, { 8, 8, 8 }, false, true, 2, 2, false },
119 { V4L2_PIX_FMT_YUV422M, MEDIA_BUS_FMT_AYUV8_1X32,
120 VI6_FMT_Y_U_V_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
121 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
122 3, { 8, 8, 8 }, false, false, 2, 1, false },
123 { V4L2_PIX_FMT_YVU422M, MEDIA_BUS_FMT_AYUV8_1X32,
124 VI6_FMT_Y_U_V_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
125 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
126 3, { 8, 8, 8 }, false, true, 2, 1, false },
127 { V4L2_PIX_FMT_YUV444M, MEDIA_BUS_FMT_AYUV8_1X32,
128 VI6_FMT_Y_U_V_444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
129 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
130 3, { 8, 8, 8 }, false, false, 1, 1, false },
131 { V4L2_PIX_FMT_YVU444M, MEDIA_BUS_FMT_AYUV8_1X32,
132 VI6_FMT_Y_U_V_444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
133 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
134 3, { 8, 8, 8 }, false, true, 1, 1, false },
135};
136
137/*
138 * vsp1_get_format_info - Retrieve format information for a 4CC
139 * @fourcc: the format 4CC
140 *
141 * Return a pointer to the format information structure corresponding to the
142 * given V4L2 format 4CC, or NULL if no corresponding format can be found.
143 */
144const struct vsp1_format_info *vsp1_get_format_info(u32 fourcc)
145{
146 unsigned int i;
147
148 for (i = 0; i < ARRAY_SIZE(vsp1_video_formats); ++i) {
149 const struct vsp1_format_info *info = &vsp1_video_formats[i];
150
151 if (info->fourcc == fourcc)
152 return info;
153 }
154
155 return NULL;
156}
157
dba4a180
LP
158/* -----------------------------------------------------------------------------
159 * Pipeline Management
160 */
161
162void vsp1_pipeline_reset(struct vsp1_pipeline *pipe)
163{
164 if (pipe->bru) {
165 struct vsp1_bru *bru = to_bru(&pipe->bru->subdev);
166 unsigned int i;
167
168 for (i = 0; i < ARRAY_SIZE(bru->inputs); ++i)
169 bru->inputs[i].rpf = NULL;
170 }
171
172 INIT_LIST_HEAD(&pipe->entities);
173 pipe->state = VSP1_PIPELINE_STOPPED;
174 pipe->buffers_ready = 0;
175 pipe->num_inputs = 0;
176 pipe->output = NULL;
177 pipe->bru = NULL;
178 pipe->lif = NULL;
179 pipe->uds = NULL;
180}
181
f294c2f7
LP
182void vsp1_pipeline_init(struct vsp1_pipeline *pipe)
183{
184 mutex_init(&pipe->lock);
185 spin_lock_init(&pipe->irqlock);
186 init_waitqueue_head(&pipe->wq);
187
188 INIT_LIST_HEAD(&pipe->entities);
189 pipe->state = VSP1_PIPELINE_STOPPED;
190}
191
dba4a180
LP
192void vsp1_pipeline_run(struct vsp1_pipeline *pipe)
193{
194 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
195
196 vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index), VI6_CMD_STRCMD);
197 pipe->state = VSP1_PIPELINE_RUNNING;
198 pipe->buffers_ready = 0;
199}
200
201bool vsp1_pipeline_stopped(struct vsp1_pipeline *pipe)
202{
203 unsigned long flags;
204 bool stopped;
205
206 spin_lock_irqsave(&pipe->irqlock, flags);
207 stopped = pipe->state == VSP1_PIPELINE_STOPPED;
208 spin_unlock_irqrestore(&pipe->irqlock, flags);
209
210 return stopped;
211}
212
213int vsp1_pipeline_stop(struct vsp1_pipeline *pipe)
214{
215 struct vsp1_entity *entity;
216 unsigned long flags;
217 int ret;
218
219 spin_lock_irqsave(&pipe->irqlock, flags);
220 if (pipe->state == VSP1_PIPELINE_RUNNING)
221 pipe->state = VSP1_PIPELINE_STOPPING;
222 spin_unlock_irqrestore(&pipe->irqlock, flags);
223
224 ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe),
225 msecs_to_jiffies(500));
226 ret = ret == 0 ? -ETIMEDOUT : 0;
227
228 list_for_each_entry(entity, &pipe->entities, list_pipe) {
229 if (entity->route && entity->route->reg)
230 vsp1_write(entity->vsp1, entity->route->reg,
231 VI6_DPR_NODE_UNUSED);
232
233 v4l2_subdev_call(&entity->subdev, video, s_stream, 0);
234 }
235
236 return ret;
237}
238
239bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe)
240{
241 unsigned int mask;
242
243 mask = ((1 << pipe->num_inputs) - 1) << 1;
244 if (!pipe->lif)
245 mask |= 1 << 0;
246
247 return pipe->buffers_ready == mask;
248}
249
250void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
251{
252 enum vsp1_pipeline_state state;
253 unsigned long flags;
254
255 if (pipe == NULL)
256 return;
257
258 /* Signal frame end to the pipeline handler. */
259 pipe->frame_end(pipe);
260
261 spin_lock_irqsave(&pipe->irqlock, flags);
262
263 state = pipe->state;
264 pipe->state = VSP1_PIPELINE_STOPPED;
265
266 /* If a stop has been requested, mark the pipeline as stopped and
267 * return.
268 */
269 if (state == VSP1_PIPELINE_STOPPING) {
270 wake_up(&pipe->wq);
271 goto done;
272 }
273
274 /* Restart the pipeline if ready. */
275 if (vsp1_pipeline_ready(pipe))
276 vsp1_pipeline_run(pipe);
277
278done:
279 spin_unlock_irqrestore(&pipe->irqlock, flags);
280}
281
282/*
283 * Propagate the alpha value through the pipeline.
284 *
285 * As the UDS has restricted scaling capabilities when the alpha component needs
286 * to be scaled, we disable alpha scaling when the UDS input has a fixed alpha
287 * value. The UDS then outputs a fixed alpha value which needs to be programmed
288 * from the input RPF alpha.
289 */
290void vsp1_pipeline_propagate_alpha(struct vsp1_pipeline *pipe,
291 struct vsp1_entity *input,
292 unsigned int alpha)
293{
294 struct vsp1_entity *entity;
295 struct media_pad *pad;
296
297 pad = media_entity_remote_pad(&input->pads[RWPF_PAD_SOURCE]);
298
299 while (pad) {
300 if (!is_media_entity_v4l2_subdev(pad->entity))
301 break;
302
303 entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity));
304
305 /* The BRU background color has a fixed alpha value set to 255,
306 * the output alpha value is thus always equal to 255.
307 */
308 if (entity->type == VSP1_ENTITY_BRU)
309 alpha = 255;
310
311 if (entity->type == VSP1_ENTITY_UDS) {
312 struct vsp1_uds *uds = to_uds(&entity->subdev);
313
314 vsp1_uds_set_alpha(uds, alpha);
315 break;
316 }
317
318 pad = &entity->pads[entity->source_pad];
319 pad = media_entity_remote_pad(pad);
320 }
321}
322
323void vsp1_pipelines_suspend(struct vsp1_device *vsp1)
324{
325 unsigned long flags;
326 unsigned int i;
327 int ret;
328
329 /* To avoid increasing the system suspend time needlessly, loop over the
330 * pipelines twice, first to set them all to the stopping state, and
331 * then to wait for the stop to complete.
332 */
333 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
334 struct vsp1_rwpf *wpf = vsp1->wpf[i];
335 struct vsp1_pipeline *pipe;
336
337 if (wpf == NULL)
338 continue;
339
340 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
341 if (pipe == NULL)
342 continue;
343
344 spin_lock_irqsave(&pipe->irqlock, flags);
345 if (pipe->state == VSP1_PIPELINE_RUNNING)
346 pipe->state = VSP1_PIPELINE_STOPPING;
347 spin_unlock_irqrestore(&pipe->irqlock, flags);
348 }
349
350 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
351 struct vsp1_rwpf *wpf = vsp1->wpf[i];
352 struct vsp1_pipeline *pipe;
353
354 if (wpf == NULL)
355 continue;
356
357 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
358 if (pipe == NULL)
359 continue;
360
361 ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe),
362 msecs_to_jiffies(500));
363 if (ret == 0)
364 dev_warn(vsp1->dev, "pipeline %u stop timeout\n",
365 wpf->entity.index);
366 }
367}
368
369void vsp1_pipelines_resume(struct vsp1_device *vsp1)
370{
371 unsigned int i;
372
373 /* Resume pipeline all running pipelines. */
374 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
375 struct vsp1_rwpf *wpf = vsp1->wpf[i];
376 struct vsp1_pipeline *pipe;
377
378 if (wpf == NULL)
379 continue;
380
381 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
382 if (pipe == NULL)
383 continue;
384
385 if (vsp1_pipeline_ready(pipe))
386 vsp1_pipeline_run(pipe);
387 }
388}
This page took 0.040155 seconds and 5 git commands to generate.