[media] v4l: vsp1: Change the type of the rwpf field in struct vsp1_video
[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>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/v4l2-mediabus.h>
20#include <linux/videodev2.h>
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"
26e0ca22
LP
32#include "vsp1_entity.h"
33#include "vsp1_rwpf.h"
bdc2df62 34#include "vsp1_uds.h"
26e0ca22
LP
35#include "vsp1_video.h"
36
37#define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
38#define VSP1_VIDEO_DEF_WIDTH 1024
39#define VSP1_VIDEO_DEF_HEIGHT 768
40
41#define VSP1_VIDEO_MIN_WIDTH 2U
42#define VSP1_VIDEO_MAX_WIDTH 8190U
43#define VSP1_VIDEO_MIN_HEIGHT 2U
44#define VSP1_VIDEO_MAX_HEIGHT 8190U
45
46/* -----------------------------------------------------------------------------
47 * Helper functions
48 */
49
50static const struct vsp1_format_info vsp1_video_formats[] = {
27ffaeb0 51 { V4L2_PIX_FMT_RGB332, MEDIA_BUS_FMT_ARGB8888_1X32,
26e0ca22
LP
52 VI6_FMT_RGB_332, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
53 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 54 1, { 8, 0, 0 }, false, false, 1, 1, false },
27ffaeb0 55 { V4L2_PIX_FMT_ARGB444, MEDIA_BUS_FMT_ARGB8888_1X32,
7a52b6de
LP
56 VI6_FMT_ARGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
57 VI6_RPF_DSWAP_P_WDS,
58 1, { 16, 0, 0 }, false, false, 1, 1, true },
27ffaeb0 59 { V4L2_PIX_FMT_XRGB444, MEDIA_BUS_FMT_ARGB8888_1X32,
26e0ca22
LP
60 VI6_FMT_XRGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
61 VI6_RPF_DSWAP_P_WDS,
7a52b6de 62 1, { 16, 0, 0 }, false, false, 1, 1, true },
27ffaeb0 63 { V4L2_PIX_FMT_ARGB555, MEDIA_BUS_FMT_ARGB8888_1X32,
7a52b6de
LP
64 VI6_FMT_ARGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
65 VI6_RPF_DSWAP_P_WDS,
66 1, { 16, 0, 0 }, false, false, 1, 1, true },
27ffaeb0 67 { V4L2_PIX_FMT_XRGB555, MEDIA_BUS_FMT_ARGB8888_1X32,
26e0ca22
LP
68 VI6_FMT_XRGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
69 VI6_RPF_DSWAP_P_WDS,
7a52b6de 70 1, { 16, 0, 0 }, false, false, 1, 1, false },
27ffaeb0 71 { V4L2_PIX_FMT_RGB565, MEDIA_BUS_FMT_ARGB8888_1X32,
26e0ca22
LP
72 VI6_FMT_RGB_565, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
73 VI6_RPF_DSWAP_P_WDS,
7a52b6de 74 1, { 16, 0, 0 }, false, false, 1, 1, false },
27ffaeb0 75 { V4L2_PIX_FMT_BGR24, MEDIA_BUS_FMT_ARGB8888_1X32,
26e0ca22
LP
76 VI6_FMT_BGR_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
77 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 78 1, { 24, 0, 0 }, false, false, 1, 1, false },
27ffaeb0 79 { V4L2_PIX_FMT_RGB24, MEDIA_BUS_FMT_ARGB8888_1X32,
26e0ca22
LP
80 VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
81 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 82 1, { 24, 0, 0 }, false, false, 1, 1, false },
27ffaeb0 83 { V4L2_PIX_FMT_ABGR32, MEDIA_BUS_FMT_ARGB8888_1X32,
7a52b6de
LP
84 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
85 1, { 32, 0, 0 }, false, false, 1, 1, true },
27ffaeb0 86 { V4L2_PIX_FMT_XBGR32, MEDIA_BUS_FMT_ARGB8888_1X32,
26e0ca22 87 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
7a52b6de 88 1, { 32, 0, 0 }, false, false, 1, 1, false },
27ffaeb0 89 { V4L2_PIX_FMT_ARGB32, MEDIA_BUS_FMT_ARGB8888_1X32,
7a52b6de
LP
90 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
91 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
92 1, { 32, 0, 0 }, false, false, 1, 1, true },
27ffaeb0 93 { V4L2_PIX_FMT_XRGB32, MEDIA_BUS_FMT_ARGB8888_1X32,
26e0ca22
LP
94 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
95 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 96 1, { 32, 0, 0 }, false, false, 1, 1, false },
27ffaeb0 97 { V4L2_PIX_FMT_UYVY, MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22
LP
98 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
99 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 100 1, { 16, 0, 0 }, false, false, 2, 1, false },
27ffaeb0 101 { V4L2_PIX_FMT_VYUY, MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22
LP
102 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
103 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 104 1, { 16, 0, 0 }, false, true, 2, 1, false },
27ffaeb0 105 { V4L2_PIX_FMT_YUYV, MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22
LP
106 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
107 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 108 1, { 16, 0, 0 }, true, false, 2, 1, false },
27ffaeb0 109 { V4L2_PIX_FMT_YVYU, MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22
LP
110 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
111 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 112 1, { 16, 0, 0 }, true, true, 2, 1, false },
27ffaeb0 113 { V4L2_PIX_FMT_NV12M, MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22
LP
114 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
115 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 116 2, { 8, 16, 0 }, false, false, 2, 2, false },
27ffaeb0 117 { V4L2_PIX_FMT_NV21M, MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22
LP
118 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
119 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 120 2, { 8, 16, 0 }, false, true, 2, 2, false },
27ffaeb0 121 { V4L2_PIX_FMT_NV16M, MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22
LP
122 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
123 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 124 2, { 8, 16, 0 }, false, false, 2, 1, false },
27ffaeb0 125 { V4L2_PIX_FMT_NV61M, MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22
LP
126 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
127 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 128 2, { 8, 16, 0 }, false, true, 2, 1, false },
27ffaeb0 129 { V4L2_PIX_FMT_YUV420M, MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22
LP
130 VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
131 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
7a52b6de 132 3, { 8, 8, 8 }, false, false, 2, 2, false },
b915bd24
LP
133 { V4L2_PIX_FMT_YVU420M, MEDIA_BUS_FMT_AYUV8_1X32,
134 VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
135 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
136 3, { 8, 8, 8 }, false, true, 2, 2, false },
137 { V4L2_PIX_FMT_YUV422M, MEDIA_BUS_FMT_AYUV8_1X32,
138 VI6_FMT_Y_U_V_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
139 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
140 3, { 8, 8, 8 }, false, false, 2, 1, false },
141 { V4L2_PIX_FMT_YVU422M, MEDIA_BUS_FMT_AYUV8_1X32,
142 VI6_FMT_Y_U_V_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
143 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
144 3, { 8, 8, 8 }, false, true, 2, 1, false },
145 { V4L2_PIX_FMT_YUV444M, MEDIA_BUS_FMT_AYUV8_1X32,
146 VI6_FMT_Y_U_V_444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
147 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
148 3, { 8, 8, 8 }, false, false, 1, 1, false },
149 { V4L2_PIX_FMT_YVU444M, MEDIA_BUS_FMT_AYUV8_1X32,
150 VI6_FMT_Y_U_V_444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
151 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
152 3, { 8, 8, 8 }, false, true, 1, 1, false },
26e0ca22
LP
153};
154
155/*
156 * vsp1_get_format_info - Retrieve format information for a 4CC
157 * @fourcc: the format 4CC
158 *
159 * Return a pointer to the format information structure corresponding to the
160 * given V4L2 format 4CC, or NULL if no corresponding format can be found.
161 */
162static const struct vsp1_format_info *vsp1_get_format_info(u32 fourcc)
163{
164 unsigned int i;
165
166 for (i = 0; i < ARRAY_SIZE(vsp1_video_formats); ++i) {
167 const struct vsp1_format_info *info = &vsp1_video_formats[i];
168
169 if (info->fourcc == fourcc)
170 return info;
171 }
172
173 return NULL;
174}
175
176
177static struct v4l2_subdev *
178vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
179{
180 struct media_pad *remote;
181
182 remote = media_entity_remote_pad(local);
3efdf62c 183 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
26e0ca22
LP
184 return NULL;
185
186 if (pad)
187 *pad = remote->index;
188
189 return media_entity_to_v4l2_subdev(remote->entity);
190}
191
192static int vsp1_video_verify_format(struct vsp1_video *video)
193{
194 struct v4l2_subdev_format fmt;
195 struct v4l2_subdev *subdev;
196 int ret;
197
198 subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
199 if (subdev == NULL)
200 return -EINVAL;
201
202 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
203 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
204 if (ret < 0)
205 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
206
207 if (video->fmtinfo->mbus != fmt.format.code ||
208 video->format.height != fmt.format.height ||
209 video->format.width != fmt.format.width)
210 return -EINVAL;
211
212 return 0;
213}
214
215static int __vsp1_video_try_format(struct vsp1_video *video,
216 struct v4l2_pix_format_mplane *pix,
217 const struct vsp1_format_info **fmtinfo)
218{
56bfef3e
LP
219 static const u32 xrgb_formats[][2] = {
220 { V4L2_PIX_FMT_RGB444, V4L2_PIX_FMT_XRGB444 },
221 { V4L2_PIX_FMT_RGB555, V4L2_PIX_FMT_XRGB555 },
222 { V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_XBGR32 },
223 { V4L2_PIX_FMT_RGB32, V4L2_PIX_FMT_XRGB32 },
224 };
225
26e0ca22
LP
226 const struct vsp1_format_info *info;
227 unsigned int width = pix->width;
228 unsigned int height = pix->height;
229 unsigned int i;
230
56bfef3e
LP
231 /* Backward compatibility: replace deprecated RGB formats by their XRGB
232 * equivalent. This selects the format older userspace applications want
233 * while still exposing the new format.
234 */
235 for (i = 0; i < ARRAY_SIZE(xrgb_formats); ++i) {
236 if (xrgb_formats[i][0] == pix->pixelformat) {
237 pix->pixelformat = xrgb_formats[i][1];
238 break;
239 }
240 }
241
26e0ca22
LP
242 /* Retrieve format information and select the default format if the
243 * requested format isn't supported.
244 */
245 info = vsp1_get_format_info(pix->pixelformat);
246 if (info == NULL)
247 info = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
248
249 pix->pixelformat = info->fourcc;
250 pix->colorspace = V4L2_COLORSPACE_SRGB;
251 pix->field = V4L2_FIELD_NONE;
252 memset(pix->reserved, 0, sizeof(pix->reserved));
253
254 /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
255 width = round_down(width, info->hsub);
256 height = round_down(height, info->vsub);
257
258 /* Clamp the width and height. */
259 pix->width = clamp(width, VSP1_VIDEO_MIN_WIDTH, VSP1_VIDEO_MAX_WIDTH);
260 pix->height = clamp(height, VSP1_VIDEO_MIN_HEIGHT,
261 VSP1_VIDEO_MAX_HEIGHT);
262
263 /* Compute and clamp the stride and image size. While not documented in
264 * the datasheet, strides not aligned to a multiple of 128 bytes result
265 * in image corruption.
266 */
df5c3e7c 267 for (i = 0; i < min(info->planes, 2U); ++i) {
26e0ca22
LP
268 unsigned int hsub = i > 0 ? info->hsub : 1;
269 unsigned int vsub = i > 0 ? info->vsub : 1;
270 unsigned int align = 128;
271 unsigned int bpl;
272
273 bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
274 pix->width / hsub * info->bpp[i] / 8,
275 round_down(65535U, align));
276
277 pix->plane_fmt[i].bytesperline = round_up(bpl, align);
278 pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
279 * pix->height / vsub;
280 }
281
282 if (info->planes == 3) {
283 /* The second and third planes must have the same stride. */
284 pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
285 pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
286 }
287
288 pix->num_planes = info->planes;
289
290 if (fmtinfo)
291 *fmtinfo = info;
292
293 return 0;
294}
295
26e0ca22
LP
296/* -----------------------------------------------------------------------------
297 * Pipeline Management
298 */
299
bdc2df62
LP
300static int vsp1_pipeline_validate_branch(struct vsp1_pipeline *pipe,
301 struct vsp1_rwpf *input,
26e0ca22
LP
302 struct vsp1_rwpf *output)
303{
304 struct vsp1_entity *entity;
54b5a749 305 struct media_entity_enum ent_enum;
26e0ca22 306 struct media_pad *pad;
54b5a749 307 int rval;
bdc2df62 308 bool bru_found = false;
26e0ca22 309
629bb6d4
LP
310 input->location.left = 0;
311 input->location.top = 0;
312
54b5a749
SA
313 rval = media_entity_enum_init(
314 &ent_enum, input->entity.pads[RWPF_PAD_SOURCE].graph_obj.mdev);
315 if (rval)
316 return rval;
317
26e0ca22
LP
318 pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
319
320 while (1) {
54b5a749
SA
321 if (pad == NULL) {
322 rval = -EPIPE;
323 goto out;
324 }
26e0ca22
LP
325
326 /* We've reached a video node, that shouldn't have happened. */
54b5a749
SA
327 if (!is_media_entity_v4l2_subdev(pad->entity)) {
328 rval = -EPIPE;
329 goto out;
330 }
26e0ca22 331
54b5a749
SA
332 entity = to_vsp1_entity(
333 media_entity_to_v4l2_subdev(pad->entity));
26e0ca22 334
629bb6d4
LP
335 /* A BRU is present in the pipeline, store the compose rectangle
336 * location in the input RPF for use when configuring the RPF.
337 */
338 if (entity->type == VSP1_ENTITY_BRU) {
339 struct vsp1_bru *bru = to_bru(&entity->subdev);
6418b4d6
LP
340 struct v4l2_rect *rect =
341 &bru->inputs[pad->index].compose;
342
343 bru->inputs[pad->index].rpf = input;
629bb6d4
LP
344
345 input->location.left = rect->left;
346 input->location.top = rect->top;
bdc2df62
LP
347
348 bru_found = true;
629bb6d4
LP
349 }
350
26e0ca22
LP
351 /* We've reached the WPF, we're done. */
352 if (entity->type == VSP1_ENTITY_WPF)
353 break;
354
355 /* Ensure the branch has no loop. */
54b5a749
SA
356 if (media_entity_enum_test_and_set(&ent_enum,
357 &entity->subdev.entity)) {
358 rval = -EPIPE;
359 goto out;
360 }
26e0ca22
LP
361
362 /* UDS can't be chained. */
363 if (entity->type == VSP1_ENTITY_UDS) {
54b5a749
SA
364 if (pipe->uds) {
365 rval = -EPIPE;
366 goto out;
367 }
bdc2df62
LP
368
369 pipe->uds = entity;
370 pipe->uds_input = bru_found ? pipe->bru
371 : &input->entity;
26e0ca22
LP
372 }
373
374 /* Follow the source link. The link setup operations ensure
375 * that the output fan-out can't be more than one, there is thus
376 * no need to verify here that only a single source link is
377 * activated.
378 */
379 pad = &entity->pads[entity->source_pad];
380 pad = media_entity_remote_pad(pad);
381 }
382
383 /* The last entity must be the output WPF. */
384 if (entity != &output->entity)
54b5a749 385 rval = -EPIPE;
26e0ca22 386
54b5a749
SA
387out:
388 media_entity_enum_cleanup(&ent_enum);
389
390 return rval;
26e0ca22
LP
391}
392
6418b4d6
LP
393static void __vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
394{
395 if (pipe->bru) {
396 struct vsp1_bru *bru = to_bru(&pipe->bru->subdev);
397 unsigned int i;
398
399 for (i = 0; i < ARRAY_SIZE(bru->inputs); ++i)
400 bru->inputs[i].rpf = NULL;
401 }
402
403 INIT_LIST_HEAD(&pipe->entities);
404 pipe->state = VSP1_PIPELINE_STOPPED;
405 pipe->buffers_ready = 0;
406 pipe->num_video = 0;
407 pipe->num_inputs = 0;
408 pipe->output = NULL;
409 pipe->bru = NULL;
410 pipe->lif = NULL;
bdc2df62 411 pipe->uds = NULL;
6418b4d6
LP
412}
413
26e0ca22
LP
414static int vsp1_pipeline_validate(struct vsp1_pipeline *pipe,
415 struct vsp1_video *video)
416{
417 struct media_entity_graph graph;
418 struct media_entity *entity = &video->video.entity;
d10c9894 419 struct media_device *mdev = entity->graph_obj.mdev;
26e0ca22
LP
420 unsigned int i;
421 int ret;
422
423 mutex_lock(&mdev->graph_mutex);
424
425 /* Walk the graph to locate the entities and video nodes. */
c1a5f1bc
SA
426 ret = media_entity_graph_walk_init(&graph, mdev);
427 if (ret) {
428 mutex_unlock(&mdev->graph_mutex);
429 return ret;
430 }
431
26e0ca22
LP
432 media_entity_graph_walk_start(&graph, entity);
433
434 while ((entity = media_entity_graph_walk_next(&graph))) {
435 struct v4l2_subdev *subdev;
436 struct vsp1_rwpf *rwpf;
437 struct vsp1_entity *e;
438
3efdf62c 439 if (is_media_entity_v4l2_io(entity)) {
26e0ca22
LP
440 pipe->num_video++;
441 continue;
442 }
443
444 subdev = media_entity_to_v4l2_subdev(entity);
445 e = to_vsp1_entity(subdev);
446 list_add_tail(&e->list_pipe, &pipe->entities);
447
448 if (e->type == VSP1_ENTITY_RPF) {
449 rwpf = to_rwpf(subdev);
450 pipe->inputs[pipe->num_inputs++] = rwpf;
451 rwpf->video.pipe_index = pipe->num_inputs;
452 } else if (e->type == VSP1_ENTITY_WPF) {
453 rwpf = to_rwpf(subdev);
454 pipe->output = to_rwpf(subdev);
455 rwpf->video.pipe_index = 0;
456 } else if (e->type == VSP1_ENTITY_LIF) {
457 pipe->lif = e;
629bb6d4
LP
458 } else if (e->type == VSP1_ENTITY_BRU) {
459 pipe->bru = e;
26e0ca22
LP
460 }
461 }
462
463 mutex_unlock(&mdev->graph_mutex);
464
c1a5f1bc
SA
465 media_entity_graph_walk_cleanup(&graph);
466
26e0ca22
LP
467 /* We need one output and at least one input. */
468 if (pipe->num_inputs == 0 || !pipe->output) {
469 ret = -EPIPE;
470 goto error;
471 }
472
473 /* Follow links downstream for each input and make sure the graph
474 * contains no loop and that all branches end at the output WPF.
475 */
476 for (i = 0; i < pipe->num_inputs; ++i) {
bdc2df62 477 ret = vsp1_pipeline_validate_branch(pipe, pipe->inputs[i],
26e0ca22
LP
478 pipe->output);
479 if (ret < 0)
480 goto error;
481 }
482
483 return 0;
484
485error:
6418b4d6 486 __vsp1_pipeline_cleanup(pipe);
26e0ca22
LP
487 return ret;
488}
489
490static int vsp1_pipeline_init(struct vsp1_pipeline *pipe,
491 struct vsp1_video *video)
492{
493 int ret;
494
495 mutex_lock(&pipe->lock);
496
497 /* If we're the first user validate and initialize the pipeline. */
498 if (pipe->use_count == 0) {
499 ret = vsp1_pipeline_validate(pipe, video);
500 if (ret < 0)
501 goto done;
502 }
503
504 pipe->use_count++;
505 ret = 0;
506
507done:
508 mutex_unlock(&pipe->lock);
509 return ret;
510}
511
512static void vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
513{
514 mutex_lock(&pipe->lock);
515
516 /* If we're the last user clean up the pipeline. */
6418b4d6
LP
517 if (--pipe->use_count == 0)
518 __vsp1_pipeline_cleanup(pipe);
26e0ca22
LP
519
520 mutex_unlock(&pipe->lock);
521}
522
523static void vsp1_pipeline_run(struct vsp1_pipeline *pipe)
524{
525 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
526
527 vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index), VI6_CMD_STRCMD);
528 pipe->state = VSP1_PIPELINE_RUNNING;
529 pipe->buffers_ready = 0;
530}
531
41bdc3cf 532static bool vsp1_pipeline_stopped(struct vsp1_pipeline *pipe)
1c991fee
LP
533{
534 unsigned long flags;
535 bool stopped;
536
537 spin_lock_irqsave(&pipe->irqlock, flags);
1b4ca5aa 538 stopped = pipe->state == VSP1_PIPELINE_STOPPED;
1c991fee
LP
539 spin_unlock_irqrestore(&pipe->irqlock, flags);
540
541 return stopped;
542}
543
26e0ca22
LP
544static int vsp1_pipeline_stop(struct vsp1_pipeline *pipe)
545{
546 struct vsp1_entity *entity;
547 unsigned long flags;
548 int ret;
549
550 spin_lock_irqsave(&pipe->irqlock, flags);
9b3e6e2a
LP
551 if (pipe->state == VSP1_PIPELINE_RUNNING)
552 pipe->state = VSP1_PIPELINE_STOPPING;
26e0ca22
LP
553 spin_unlock_irqrestore(&pipe->irqlock, flags);
554
1c991fee 555 ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe),
26e0ca22
LP
556 msecs_to_jiffies(500));
557 ret = ret == 0 ? -ETIMEDOUT : 0;
558
559 list_for_each_entry(entity, &pipe->entities, list_pipe) {
aaa48cbb 560 if (entity->route && entity->route->reg)
d9b45ed3 561 vsp1_write(entity->vsp1, entity->route->reg,
26e0ca22
LP
562 VI6_DPR_NODE_UNUSED);
563
564 v4l2_subdev_call(&entity->subdev, video, s_stream, 0);
565 }
566
567 return ret;
568}
569
570static bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe)
571{
572 unsigned int mask;
573
574 mask = ((1 << pipe->num_inputs) - 1) << 1;
575 if (!pipe->lif)
576 mask |= 1 << 0;
577
578 return pipe->buffers_ready == mask;
579}
580
581/*
582 * vsp1_video_complete_buffer - Complete the current buffer
583 * @video: the video node
584 *
585 * This function completes the current buffer by filling its sequence number,
586 * time stamp and payload size, and hands it back to the videobuf core.
587 *
3299ba5c
LP
588 * When operating in DU output mode (deep pipeline to the DU through the LIF),
589 * the VSP1 needs to constantly supply frames to the display. In that case, if
590 * no other buffer is queued, reuse the one that has just been processed instead
591 * of handing it back to the videobuf core.
592 *
26e0ca22
LP
593 * Return the next queued buffer or NULL if the queue is empty.
594 */
595static struct vsp1_video_buffer *
596vsp1_video_complete_buffer(struct vsp1_video *video)
597{
3299ba5c 598 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
26e0ca22
LP
599 struct vsp1_video_buffer *next = NULL;
600 struct vsp1_video_buffer *done;
601 unsigned long flags;
602 unsigned int i;
603
604 spin_lock_irqsave(&video->irqlock, flags);
605
606 if (list_empty(&video->irqqueue)) {
607 spin_unlock_irqrestore(&video->irqlock, flags);
608 return NULL;
609 }
610
611 done = list_first_entry(&video->irqqueue,
612 struct vsp1_video_buffer, queue);
3299ba5c
LP
613
614 /* In DU output mode reuse the buffer if the list is singular. */
615 if (pipe->lif && list_is_singular(&video->irqqueue)) {
616 spin_unlock_irqrestore(&video->irqlock, flags);
617 return done;
618 }
619
26e0ca22
LP
620 list_del(&done->queue);
621
622 if (!list_empty(&video->irqqueue))
623 next = list_first_entry(&video->irqqueue,
624 struct vsp1_video_buffer, queue);
625
626 spin_unlock_irqrestore(&video->irqlock, flags);
627
2d700715 628 done->buf.sequence = video->sequence++;
d6dd645e 629 done->buf.vb2_buf.timestamp = ktime_get_ns();
2d700715
JS
630 for (i = 0; i < done->buf.vb2_buf.num_planes; ++i)
631 vb2_set_plane_payload(&done->buf.vb2_buf, i, done->length[i]);
632 vb2_buffer_done(&done->buf.vb2_buf, VB2_BUF_STATE_DONE);
26e0ca22
LP
633
634 return next;
635}
636
637static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
638 struct vsp1_video *video)
639{
640 struct vsp1_video_buffer *buf;
641 unsigned long flags;
642
643 buf = vsp1_video_complete_buffer(video);
644 if (buf == NULL)
645 return;
646
647 spin_lock_irqsave(&pipe->irqlock, flags);
648
649 video->ops->queue(video, buf);
650 pipe->buffers_ready |= 1 << video->pipe_index;
651
652 spin_unlock_irqrestore(&pipe->irqlock, flags);
653}
654
655void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
656{
9b3e6e2a 657 enum vsp1_pipeline_state state;
26e0ca22
LP
658 unsigned long flags;
659 unsigned int i;
660
661 if (pipe == NULL)
662 return;
663
664 /* Complete buffers on all video nodes. */
665 for (i = 0; i < pipe->num_inputs; ++i)
666 vsp1_video_frame_end(pipe, &pipe->inputs[i]->video);
667
668 if (!pipe->lif)
669 vsp1_video_frame_end(pipe, &pipe->output->video);
670
671 spin_lock_irqsave(&pipe->irqlock, flags);
672
9b3e6e2a
LP
673 state = pipe->state;
674 pipe->state = VSP1_PIPELINE_STOPPED;
675
26e0ca22
LP
676 /* If a stop has been requested, mark the pipeline as stopped and
677 * return.
678 */
9b3e6e2a 679 if (state == VSP1_PIPELINE_STOPPING) {
26e0ca22
LP
680 wake_up(&pipe->wq);
681 goto done;
682 }
683
684 /* Restart the pipeline if ready. */
685 if (vsp1_pipeline_ready(pipe))
686 vsp1_pipeline_run(pipe);
687
688done:
689 spin_unlock_irqrestore(&pipe->irqlock, flags);
690}
691
bdc2df62
LP
692/*
693 * Propagate the alpha value through the pipeline.
694 *
695 * As the UDS has restricted scaling capabilities when the alpha component needs
696 * to be scaled, we disable alpha scaling when the UDS input has a fixed alpha
697 * value. The UDS then outputs a fixed alpha value which needs to be programmed
698 * from the input RPF alpha.
699 */
700void vsp1_pipeline_propagate_alpha(struct vsp1_pipeline *pipe,
701 struct vsp1_entity *input,
702 unsigned int alpha)
703{
704 struct vsp1_entity *entity;
705 struct media_pad *pad;
706
707 pad = media_entity_remote_pad(&input->pads[RWPF_PAD_SOURCE]);
708
709 while (pad) {
3efdf62c 710 if (!is_media_entity_v4l2_subdev(pad->entity))
bdc2df62
LP
711 break;
712
713 entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity));
714
715 /* The BRU background color has a fixed alpha value set to 255,
716 * the output alpha value is thus always equal to 255.
717 */
718 if (entity->type == VSP1_ENTITY_BRU)
719 alpha = 255;
720
721 if (entity->type == VSP1_ENTITY_UDS) {
722 struct vsp1_uds *uds = to_uds(&entity->subdev);
723
724 vsp1_uds_set_alpha(uds, alpha);
725 break;
726 }
727
728 pad = &entity->pads[entity->source_pad];
729 pad = media_entity_remote_pad(pad);
730 }
731}
732
139c9286
SF
733void vsp1_pipelines_suspend(struct vsp1_device *vsp1)
734{
735 unsigned long flags;
736 unsigned int i;
737 int ret;
738
739 /* To avoid increasing the system suspend time needlessly, loop over the
740 * pipelines twice, first to set them all to the stopping state, and then
741 * to wait for the stop to complete.
742 */
743 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
744 struct vsp1_rwpf *wpf = vsp1->wpf[i];
745 struct vsp1_pipeline *pipe;
746
747 if (wpf == NULL)
748 continue;
749
750 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
751 if (pipe == NULL)
752 continue;
753
754 spin_lock_irqsave(&pipe->irqlock, flags);
755 if (pipe->state == VSP1_PIPELINE_RUNNING)
756 pipe->state = VSP1_PIPELINE_STOPPING;
757 spin_unlock_irqrestore(&pipe->irqlock, flags);
758 }
759
760 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
761 struct vsp1_rwpf *wpf = vsp1->wpf[i];
762 struct vsp1_pipeline *pipe;
763
764 if (wpf == NULL)
765 continue;
766
767 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
768 if (pipe == NULL)
769 continue;
770
1c991fee 771 ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe),
139c9286
SF
772 msecs_to_jiffies(500));
773 if (ret == 0)
774 dev_warn(vsp1->dev, "pipeline %u stop timeout\n",
775 wpf->entity.index);
776 }
777}
778
779void vsp1_pipelines_resume(struct vsp1_device *vsp1)
780{
781 unsigned int i;
782
783 /* Resume pipeline all running pipelines. */
784 for (i = 0; i < vsp1->pdata.wpf_count; ++i) {
785 struct vsp1_rwpf *wpf = vsp1->wpf[i];
786 struct vsp1_pipeline *pipe;
787
788 if (wpf == NULL)
789 continue;
790
791 pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity);
792 if (pipe == NULL)
793 continue;
794
795 if (vsp1_pipeline_ready(pipe))
796 vsp1_pipeline_run(pipe);
797 }
798}
799
26e0ca22
LP
800/* -----------------------------------------------------------------------------
801 * videobuf2 Queue Operations
802 */
803
804static int
df9ecb0c 805vsp1_video_queue_setup(struct vb2_queue *vq,
26e0ca22
LP
806 unsigned int *nbuffers, unsigned int *nplanes,
807 unsigned int sizes[], void *alloc_ctxs[])
808{
809 struct vsp1_video *video = vb2_get_drv_priv(vq);
df9ecb0c 810 const struct v4l2_pix_format_mplane *format = &video->format;
26e0ca22
LP
811 unsigned int i;
812
df9ecb0c
HV
813 if (*nplanes) {
814 if (*nplanes != format->num_planes)
26e0ca22
LP
815 return -EINVAL;
816
df9ecb0c
HV
817 for (i = 0; i < *nplanes; i++) {
818 if (sizes[i] < format->plane_fmt[i].sizeimage)
819 return -EINVAL;
820 alloc_ctxs[i] = video->alloc_ctx;
821 }
822 return 0;
26e0ca22
LP
823 }
824
825 *nplanes = format->num_planes;
826
827 for (i = 0; i < format->num_planes; ++i) {
828 sizes[i] = format->plane_fmt[i].sizeimage;
829 alloc_ctxs[i] = video->alloc_ctx;
830 }
831
832 return 0;
833}
834
835static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
836{
2d700715 837 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
26e0ca22 838 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
2d700715 839 struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vbuf);
26e0ca22
LP
840 const struct v4l2_pix_format_mplane *format = &video->format;
841 unsigned int i;
842
843 if (vb->num_planes < format->num_planes)
844 return -EINVAL;
845
26e0ca22
LP
846 for (i = 0; i < vb->num_planes; ++i) {
847 buf->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
848 buf->length[i] = vb2_plane_size(vb, i);
849
850 if (buf->length[i] < format->plane_fmt[i].sizeimage)
851 return -EINVAL;
852 }
853
854 return 0;
855}
856
857static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
858{
2d700715 859 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
26e0ca22
LP
860 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
861 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
2d700715 862 struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vbuf);
26e0ca22
LP
863 unsigned long flags;
864 bool empty;
865
866 spin_lock_irqsave(&video->irqlock, flags);
867 empty = list_empty(&video->irqqueue);
868 list_add_tail(&buf->queue, &video->irqqueue);
869 spin_unlock_irqrestore(&video->irqlock, flags);
870
871 if (!empty)
872 return;
873
874 spin_lock_irqsave(&pipe->irqlock, flags);
875
876 video->ops->queue(video, buf);
877 pipe->buffers_ready |= 1 << video->pipe_index;
878
879 if (vb2_is_streaming(&video->queue) &&
880 vsp1_pipeline_ready(pipe))
881 vsp1_pipeline_run(pipe);
882
883 spin_unlock_irqrestore(&pipe->irqlock, flags);
884}
885
886static void vsp1_entity_route_setup(struct vsp1_entity *source)
887{
888 struct vsp1_entity *sink;
889
d9b45ed3 890 if (source->route->reg == 0)
26e0ca22
LP
891 return;
892
893 sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
d9b45ed3
LP
894 vsp1_write(source->vsp1, source->route->reg,
895 sink->route->inputs[source->sink_pad]);
26e0ca22
LP
896}
897
898static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
899{
900 struct vsp1_video *video = vb2_get_drv_priv(vq);
901 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
902 struct vsp1_entity *entity;
903 unsigned long flags;
904 int ret;
905
906 mutex_lock(&pipe->lock);
907 if (pipe->stream_count == pipe->num_video - 1) {
bdc2df62
LP
908 if (pipe->uds) {
909 struct vsp1_uds *uds = to_uds(&pipe->uds->subdev);
910
911 /* If a BRU is present in the pipeline before the UDS,
912 * the alpha component doesn't need to be scaled as the
913 * BRU output alpha value is fixed to 255. Otherwise we
914 * need to scale the alpha component only when available
915 * at the input RPF.
916 */
917 if (pipe->uds_input->type == VSP1_ENTITY_BRU) {
918 uds->scale_alpha = false;
919 } else {
920 struct vsp1_rwpf *rpf =
921 to_rwpf(&pipe->uds_input->subdev);
922
923 uds->scale_alpha = rpf->video.fmtinfo->alpha;
924 }
925 }
926
26e0ca22
LP
927 list_for_each_entry(entity, &pipe->entities, list_pipe) {
928 vsp1_entity_route_setup(entity);
929
930 ret = v4l2_subdev_call(&entity->subdev, video,
931 s_stream, 1);
932 if (ret < 0) {
933 mutex_unlock(&pipe->lock);
934 return ret;
935 }
936 }
937 }
938
939 pipe->stream_count++;
940 mutex_unlock(&pipe->lock);
941
942 spin_lock_irqsave(&pipe->irqlock, flags);
943 if (vsp1_pipeline_ready(pipe))
944 vsp1_pipeline_run(pipe);
945 spin_unlock_irqrestore(&pipe->irqlock, flags);
946
947 return 0;
948}
949
e37559b2 950static void vsp1_video_stop_streaming(struct vb2_queue *vq)
26e0ca22
LP
951{
952 struct vsp1_video *video = vb2_get_drv_priv(vq);
953 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
9df04e9d 954 struct vsp1_video_buffer *buffer;
26e0ca22
LP
955 unsigned long flags;
956 int ret;
957
958 mutex_lock(&pipe->lock);
959 if (--pipe->stream_count == 0) {
960 /* Stop the pipeline. */
961 ret = vsp1_pipeline_stop(pipe);
962 if (ret == -ETIMEDOUT)
963 dev_err(video->vsp1->dev, "pipeline stop timeout\n");
964 }
965 mutex_unlock(&pipe->lock);
966
967 vsp1_pipeline_cleanup(pipe);
968 media_entity_pipeline_stop(&video->video.entity);
969
970 /* Remove all buffers from the IRQ queue. */
971 spin_lock_irqsave(&video->irqlock, flags);
9df04e9d 972 list_for_each_entry(buffer, &video->irqqueue, queue)
2d700715 973 vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR);
26e0ca22
LP
974 INIT_LIST_HEAD(&video->irqqueue);
975 spin_unlock_irqrestore(&video->irqlock, flags);
26e0ca22
LP
976}
977
978static struct vb2_ops vsp1_video_queue_qops = {
979 .queue_setup = vsp1_video_queue_setup,
980 .buf_prepare = vsp1_video_buffer_prepare,
981 .buf_queue = vsp1_video_buffer_queue,
982 .wait_prepare = vb2_ops_wait_prepare,
983 .wait_finish = vb2_ops_wait_finish,
984 .start_streaming = vsp1_video_start_streaming,
985 .stop_streaming = vsp1_video_stop_streaming,
986};
987
988/* -----------------------------------------------------------------------------
989 * V4L2 ioctls
990 */
991
992static int
993vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
994{
995 struct v4l2_fh *vfh = file->private_data;
996 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
997
998 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
999 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
1000 | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
1001
1002 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1003 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
1004 | V4L2_CAP_STREAMING;
1005 else
1006 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
1007 | V4L2_CAP_STREAMING;
1008
1009 strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
1010 strlcpy(cap->card, video->video.name, sizeof(cap->card));
1011 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1012 dev_name(video->vsp1->dev));
1013
1014 return 0;
1015}
1016
1017static int
1018vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
1019{
1020 struct v4l2_fh *vfh = file->private_data;
1021 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
1022
1023 if (format->type != video->queue.type)
1024 return -EINVAL;
1025
1026 mutex_lock(&video->lock);
1027 format->fmt.pix_mp = video->format;
1028 mutex_unlock(&video->lock);
1029
1030 return 0;
1031}
1032
1033static int
1034vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
1035{
1036 struct v4l2_fh *vfh = file->private_data;
1037 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
1038
1039 if (format->type != video->queue.type)
1040 return -EINVAL;
1041
1042 return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
1043}
1044
1045static int
1046vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
1047{
1048 struct v4l2_fh *vfh = file->private_data;
1049 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
1050 const struct vsp1_format_info *info;
1051 int ret;
1052
1053 if (format->type != video->queue.type)
1054 return -EINVAL;
1055
1056 ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
1057 if (ret < 0)
1058 return ret;
1059
1060 mutex_lock(&video->lock);
1061
1062 if (vb2_is_busy(&video->queue)) {
1063 ret = -EBUSY;
1064 goto done;
1065 }
1066
1067 video->format = format->fmt.pix_mp;
1068 video->fmtinfo = info;
1069
1070done:
1071 mutex_unlock(&video->lock);
1072 return ret;
1073}
1074
1075static int
1076vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1077{
1078 struct v4l2_fh *vfh = file->private_data;
1079 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
1080 struct vsp1_pipeline *pipe;
1081 int ret;
1082
26e0ca22
LP
1083 if (video->queue.owner && video->queue.owner != file->private_data)
1084 return -EBUSY;
1085
1086 video->sequence = 0;
1087
1088 /* Start streaming on the pipeline. No link touching an entity in the
1089 * pipeline can be activated or deactivated once streaming is started.
1090 *
1091 * Use the VSP1 pipeline object embedded in the first video object that
1092 * starts streaming.
1093 */
1094 pipe = video->video.entity.pipe
1095 ? to_vsp1_pipeline(&video->video.entity) : &video->pipe;
1096
1097 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
1098 if (ret < 0)
1099 return ret;
1100
1101 /* Verify that the configured format matches the output of the connected
1102 * subdev.
1103 */
1104 ret = vsp1_video_verify_format(video);
1105 if (ret < 0)
1106 goto err_stop;
1107
1108 ret = vsp1_pipeline_init(pipe, video);
1109 if (ret < 0)
1110 goto err_stop;
1111
1112 /* Start the queue. */
1113 ret = vb2_streamon(&video->queue, type);
1114 if (ret < 0)
1115 goto err_cleanup;
1116
1117 return 0;
1118
1119err_cleanup:
1120 vsp1_pipeline_cleanup(pipe);
1121err_stop:
1122 media_entity_pipeline_stop(&video->video.entity);
1123 return ret;
1124}
1125
1126static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
1127 .vidioc_querycap = vsp1_video_querycap,
1128 .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
1129 .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
1130 .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
1131 .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
1132 .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
1133 .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
1134 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1135 .vidioc_querybuf = vb2_ioctl_querybuf,
1136 .vidioc_qbuf = vb2_ioctl_qbuf,
1137 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1138 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1139 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1140 .vidioc_streamon = vsp1_video_streamon,
1141 .vidioc_streamoff = vb2_ioctl_streamoff,
1142};
1143
1144/* -----------------------------------------------------------------------------
1145 * V4L2 File Operations
1146 */
1147
1148static int vsp1_video_open(struct file *file)
1149{
1150 struct vsp1_video *video = video_drvdata(file);
1151 struct v4l2_fh *vfh;
1152 int ret = 0;
1153
1154 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
1155 if (vfh == NULL)
1156 return -ENOMEM;
1157
1158 v4l2_fh_init(vfh, &video->video);
1159 v4l2_fh_add(vfh);
1160
1161 file->private_data = vfh;
1162
4c16d6a0
LP
1163 ret = vsp1_device_get(video->vsp1);
1164 if (ret < 0) {
26e0ca22
LP
1165 v4l2_fh_del(vfh);
1166 kfree(vfh);
1167 }
1168
1169 return ret;
1170}
1171
1172static int vsp1_video_release(struct file *file)
1173{
1174 struct vsp1_video *video = video_drvdata(file);
1175 struct v4l2_fh *vfh = file->private_data;
1176
1177 mutex_lock(&video->lock);
1178 if (video->queue.owner == vfh) {
1179 vb2_queue_release(&video->queue);
1180 video->queue.owner = NULL;
1181 }
1182 mutex_unlock(&video->lock);
1183
1184 vsp1_device_put(video->vsp1);
1185
1186 v4l2_fh_release(file);
1187
1188 file->private_data = NULL;
1189
1190 return 0;
1191}
1192
1193static struct v4l2_file_operations vsp1_video_fops = {
1194 .owner = THIS_MODULE,
1195 .unlocked_ioctl = video_ioctl2,
1196 .open = vsp1_video_open,
1197 .release = vsp1_video_release,
1198 .poll = vb2_fop_poll,
1199 .mmap = vb2_fop_mmap,
1200};
1201
1202/* -----------------------------------------------------------------------------
1203 * Initialization and Cleanup
1204 */
1205
8b4a0563 1206int vsp1_video_init(struct vsp1_video *video, struct vsp1_rwpf *rwpf)
26e0ca22
LP
1207{
1208 const char *direction;
1209 int ret;
1210
1211 switch (video->type) {
1212 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1213 direction = "output";
1214 video->pad.flags = MEDIA_PAD_FL_SINK;
1215 break;
1216
1217 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1218 direction = "input";
1219 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1220 video->video.vfl_dir = VFL_DIR_TX;
1221 break;
1222
1223 default:
1224 return -EINVAL;
1225 }
1226
1227 video->rwpf = rwpf;
1228
1229 mutex_init(&video->lock);
1230 spin_lock_init(&video->irqlock);
1231 INIT_LIST_HEAD(&video->irqqueue);
1232
1233 mutex_init(&video->pipe.lock);
1234 spin_lock_init(&video->pipe.irqlock);
1235 INIT_LIST_HEAD(&video->pipe.entities);
1236 init_waitqueue_head(&video->pipe.wq);
1237 video->pipe.state = VSP1_PIPELINE_STOPPED;
1238
1239 /* Initialize the media entity... */
ab22e77c 1240 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
26e0ca22
LP
1241 if (ret < 0)
1242 return ret;
1243
1244 /* ... and the format ... */
1245 video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
1246 video->format.pixelformat = video->fmtinfo->fourcc;
1247 video->format.colorspace = V4L2_COLORSPACE_SRGB;
1248 video->format.field = V4L2_FIELD_NONE;
1249 video->format.width = VSP1_VIDEO_DEF_WIDTH;
1250 video->format.height = VSP1_VIDEO_DEF_HEIGHT;
1251 video->format.num_planes = 1;
1252 video->format.plane_fmt[0].bytesperline =
1253 video->format.width * video->fmtinfo->bpp[0] / 8;
1254 video->format.plane_fmt[0].sizeimage =
1255 video->format.plane_fmt[0].bytesperline * video->format.height;
1256
1257 /* ... and the video node... */
1258 video->video.v4l2_dev = &video->vsp1->v4l2_dev;
1259 video->video.fops = &vsp1_video_fops;
1260 snprintf(video->video.name, sizeof(video->video.name), "%s %s",
8b4a0563 1261 rwpf->entity.subdev.name, direction);
26e0ca22
LP
1262 video->video.vfl_type = VFL_TYPE_GRABBER;
1263 video->video.release = video_device_release_empty;
1264 video->video.ioctl_ops = &vsp1_video_ioctl_ops;
1265
1266 video_set_drvdata(&video->video, video);
1267
1268 /* ... and the buffers queue... */
1269 video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
b317828b
WY
1270 if (IS_ERR(video->alloc_ctx)) {
1271 ret = PTR_ERR(video->alloc_ctx);
26e0ca22 1272 goto error;
b317828b 1273 }
26e0ca22
LP
1274
1275 video->queue.type = video->type;
1276 video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1277 video->queue.lock = &video->lock;
1278 video->queue.drv_priv = video;
1279 video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer);
1280 video->queue.ops = &vsp1_video_queue_qops;
1281 video->queue.mem_ops = &vb2_dma_contig_memops;
ade48681 1282 video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
26e0ca22
LP
1283 ret = vb2_queue_init(&video->queue);
1284 if (ret < 0) {
1285 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1286 goto error;
1287 }
1288
1289 /* ... and register the video device. */
1290 video->video.queue = &video->queue;
1291 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1292 if (ret < 0) {
1293 dev_err(video->vsp1->dev, "failed to register video device\n");
1294 goto error;
1295 }
1296
1297 return 0;
1298
1299error:
1300 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1301 vsp1_video_cleanup(video);
1302 return ret;
1303}
1304
1305void vsp1_video_cleanup(struct vsp1_video *video)
1306{
1307 if (video_is_registered(&video->video))
1308 video_unregister_device(&video->video);
1309
1310 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1311 media_entity_cleanup(&video->video.entity);
1312}
This page took 0.186426 seconds and 5 git commands to generate.