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