v4l: vsp1: bru: Support premultiplied alpha at the BRU inputs
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / media / platform / vsp1 / vsp1_video.c
1 /*
2  * vsp1_video.c  --  R-Car VSP1 Video Node
3  *
4  * Copyright (C) 2013-2014 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/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>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
29
30 #include "vsp1.h"
31 #include "vsp1_bru.h"
32 #include "vsp1_entity.h"
33 #include "vsp1_rwpf.h"
34 #include "vsp1_video.h"
35
36 #define VSP1_VIDEO_DEF_FORMAT           V4L2_PIX_FMT_YUYV
37 #define VSP1_VIDEO_DEF_WIDTH            1024
38 #define VSP1_VIDEO_DEF_HEIGHT           768
39
40 #define VSP1_VIDEO_MIN_WIDTH            2U
41 #define VSP1_VIDEO_MAX_WIDTH            8190U
42 #define VSP1_VIDEO_MIN_HEIGHT           2U
43 #define VSP1_VIDEO_MAX_HEIGHT           8190U
44
45 /* -----------------------------------------------------------------------------
46  * Helper functions
47  */
48
49 static const struct vsp1_format_info vsp1_video_formats[] = {
50         { V4L2_PIX_FMT_RGB332, V4L2_MBUS_FMT_ARGB8888_1X32,
51           VI6_FMT_RGB_332, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
52           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
53           1, { 8, 0, 0 }, false, false, 1, 1, false },
54         { V4L2_PIX_FMT_ARGB444, V4L2_MBUS_FMT_ARGB8888_1X32,
55           VI6_FMT_ARGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
56           VI6_RPF_DSWAP_P_WDS,
57           1, { 16, 0, 0 }, false, false, 1, 1, true },
58         { V4L2_PIX_FMT_XRGB444, V4L2_MBUS_FMT_ARGB8888_1X32,
59           VI6_FMT_XRGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
60           VI6_RPF_DSWAP_P_WDS,
61           1, { 16, 0, 0 }, false, false, 1, 1, true },
62         { V4L2_PIX_FMT_ARGB555, V4L2_MBUS_FMT_ARGB8888_1X32,
63           VI6_FMT_ARGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
64           VI6_RPF_DSWAP_P_WDS,
65           1, { 16, 0, 0 }, false, false, 1, 1, true },
66         { V4L2_PIX_FMT_XRGB555, V4L2_MBUS_FMT_ARGB8888_1X32,
67           VI6_FMT_XRGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
68           VI6_RPF_DSWAP_P_WDS,
69           1, { 16, 0, 0 }, false, false, 1, 1, false },
70         { V4L2_PIX_FMT_RGB565, V4L2_MBUS_FMT_ARGB8888_1X32,
71           VI6_FMT_RGB_565, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
72           VI6_RPF_DSWAP_P_WDS,
73           1, { 16, 0, 0 }, false, false, 1, 1, false },
74         { V4L2_PIX_FMT_BGR24, V4L2_MBUS_FMT_ARGB8888_1X32,
75           VI6_FMT_BGR_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
76           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
77           1, { 24, 0, 0 }, false, false, 1, 1, false },
78         { V4L2_PIX_FMT_RGB24, V4L2_MBUS_FMT_ARGB8888_1X32,
79           VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
80           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
81           1, { 24, 0, 0 }, false, false, 1, 1, false },
82         { V4L2_PIX_FMT_ABGR32, V4L2_MBUS_FMT_ARGB8888_1X32,
83           VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
84           1, { 32, 0, 0 }, false, false, 1, 1, true },
85         { V4L2_PIX_FMT_XBGR32, V4L2_MBUS_FMT_ARGB8888_1X32,
86           VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
87           1, { 32, 0, 0 }, false, false, 1, 1, false },
88         { V4L2_PIX_FMT_ARGB32, V4L2_MBUS_FMT_ARGB8888_1X32,
89           VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
90           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
91           1, { 32, 0, 0 }, false, false, 1, 1, true },
92         { V4L2_PIX_FMT_XRGB32, V4L2_MBUS_FMT_ARGB8888_1X32,
93           VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
94           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
95           1, { 32, 0, 0 }, false, false, 1, 1, false },
96         { V4L2_PIX_FMT_UYVY, V4L2_MBUS_FMT_AYUV8_1X32,
97           VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
98           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
99           1, { 16, 0, 0 }, false, false, 2, 1, false },
100         { V4L2_PIX_FMT_VYUY, V4L2_MBUS_FMT_AYUV8_1X32,
101           VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
102           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
103           1, { 16, 0, 0 }, false, true, 2, 1, false },
104         { V4L2_PIX_FMT_YUYV, V4L2_MBUS_FMT_AYUV8_1X32,
105           VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
106           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
107           1, { 16, 0, 0 }, true, false, 2, 1, false },
108         { V4L2_PIX_FMT_YVYU, V4L2_MBUS_FMT_AYUV8_1X32,
109           VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
110           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
111           1, { 16, 0, 0 }, true, true, 2, 1, false },
112         { V4L2_PIX_FMT_NV12M, V4L2_MBUS_FMT_AYUV8_1X32,
113           VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
114           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
115           2, { 8, 16, 0 }, false, false, 2, 2, false },
116         { V4L2_PIX_FMT_NV21M, V4L2_MBUS_FMT_AYUV8_1X32,
117           VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
118           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
119           2, { 8, 16, 0 }, false, true, 2, 2, false },
120         { V4L2_PIX_FMT_NV16M, V4L2_MBUS_FMT_AYUV8_1X32,
121           VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
122           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
123           2, { 8, 16, 0 }, false, false, 2, 1, false },
124         { V4L2_PIX_FMT_NV61M, V4L2_MBUS_FMT_AYUV8_1X32,
125           VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
126           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
127           2, { 8, 16, 0 }, false, true, 2, 1, false },
128         { V4L2_PIX_FMT_YUV420M, V4L2_MBUS_FMT_AYUV8_1X32,
129           VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
130           VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
131           3, { 8, 8, 8 }, false, false, 2, 2, false },
132 };
133
134 /*
135  * vsp1_get_format_info - Retrieve format information for a 4CC
136  * @fourcc: the format 4CC
137  *
138  * Return a pointer to the format information structure corresponding to the
139  * given V4L2 format 4CC, or NULL if no corresponding format can be found.
140  */
141 static const struct vsp1_format_info *vsp1_get_format_info(u32 fourcc)
142 {
143         unsigned int i;
144
145         for (i = 0; i < ARRAY_SIZE(vsp1_video_formats); ++i) {
146                 const struct vsp1_format_info *info = &vsp1_video_formats[i];
147
148                 if (info->fourcc == fourcc)
149                         return info;
150         }
151
152         return NULL;
153 }
154
155
156 static struct v4l2_subdev *
157 vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
158 {
159         struct media_pad *remote;
160
161         remote = media_entity_remote_pad(local);
162         if (remote == NULL ||
163             media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
164                 return NULL;
165
166         if (pad)
167                 *pad = remote->index;
168
169         return media_entity_to_v4l2_subdev(remote->entity);
170 }
171
172 static 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
195 static int __vsp1_video_try_format(struct vsp1_video *video,
196                                    struct v4l2_pix_format_mplane *pix,
197                                    const struct vsp1_format_info **fmtinfo)
198 {
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
206         const struct vsp1_format_info *info;
207         unsigned int width = pix->width;
208         unsigned int height = pix->height;
209         unsigned int i;
210
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
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          */
247         for (i = 0; i < max(info->planes, 2U); ++i) {
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
276 static bool
277 vsp1_video_format_adjust(struct vsp1_video *video,
278                          const struct v4l2_pix_format_mplane *format,
279                          struct v4l2_pix_format_mplane *adjust)
280 {
281         unsigned int i;
282
283         *adjust = *format;
284         __vsp1_video_try_format(video, adjust, NULL);
285
286         if (format->width != adjust->width ||
287             format->height != adjust->height ||
288             format->pixelformat != adjust->pixelformat ||
289             format->num_planes != adjust->num_planes)
290                 return false;
291
292         for (i = 0; i < format->num_planes; ++i) {
293                 if (format->plane_fmt[i].bytesperline !=
294                     adjust->plane_fmt[i].bytesperline)
295                         return false;
296
297                 adjust->plane_fmt[i].sizeimage =
298                         max(adjust->plane_fmt[i].sizeimage,
299                             format->plane_fmt[i].sizeimage);
300         }
301
302         return true;
303 }
304
305 /* -----------------------------------------------------------------------------
306  * Pipeline Management
307  */
308
309 static int vsp1_pipeline_validate_branch(struct vsp1_rwpf *input,
310                                          struct vsp1_rwpf *output)
311 {
312         struct vsp1_entity *entity;
313         unsigned int entities = 0;
314         struct media_pad *pad;
315         bool uds_found = false;
316
317         input->location.left = 0;
318         input->location.top = 0;
319
320         pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
321
322         while (1) {
323                 if (pad == NULL)
324                         return -EPIPE;
325
326                 /* We've reached a video node, that shouldn't have happened. */
327                 if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
328                         return -EPIPE;
329
330                 entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity));
331
332                 /* A BRU is present in the pipeline, store the compose rectangle
333                  * location in the input RPF for use when configuring the RPF.
334                  */
335                 if (entity->type == VSP1_ENTITY_BRU) {
336                         struct vsp1_bru *bru = to_bru(&entity->subdev);
337                         struct v4l2_rect *rect =
338                                 &bru->inputs[pad->index].compose;
339
340                         bru->inputs[pad->index].rpf = input;
341
342                         input->location.left = rect->left;
343                         input->location.top = rect->top;
344                 }
345
346                 /* We've reached the WPF, we're done. */
347                 if (entity->type == VSP1_ENTITY_WPF)
348                         break;
349
350                 /* Ensure the branch has no loop. */
351                 if (entities & (1 << entity->subdev.entity.id))
352                         return -EPIPE;
353
354                 entities |= 1 << entity->subdev.entity.id;
355
356                 /* UDS can't be chained. */
357                 if (entity->type == VSP1_ENTITY_UDS) {
358                         if (uds_found)
359                                 return -EPIPE;
360                         uds_found = true;
361                 }
362
363                 /* Follow the source link. The link setup operations ensure
364                  * that the output fan-out can't be more than one, there is thus
365                  * no need to verify here that only a single source link is
366                  * activated.
367                  */
368                 pad = &entity->pads[entity->source_pad];
369                 pad = media_entity_remote_pad(pad);
370         }
371
372         /* The last entity must be the output WPF. */
373         if (entity != &output->entity)
374                 return -EPIPE;
375
376         return 0;
377 }
378
379 static void __vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
380 {
381         if (pipe->bru) {
382                 struct vsp1_bru *bru = to_bru(&pipe->bru->subdev);
383                 unsigned int i;
384
385                 for (i = 0; i < ARRAY_SIZE(bru->inputs); ++i)
386                         bru->inputs[i].rpf = NULL;
387         }
388
389         INIT_LIST_HEAD(&pipe->entities);
390         pipe->state = VSP1_PIPELINE_STOPPED;
391         pipe->buffers_ready = 0;
392         pipe->num_video = 0;
393         pipe->num_inputs = 0;
394         pipe->output = NULL;
395         pipe->bru = NULL;
396         pipe->lif = NULL;
397 }
398
399 static int vsp1_pipeline_validate(struct vsp1_pipeline *pipe,
400                                   struct vsp1_video *video)
401 {
402         struct media_entity_graph graph;
403         struct media_entity *entity = &video->video.entity;
404         struct media_device *mdev = entity->parent;
405         unsigned int i;
406         int ret;
407
408         mutex_lock(&mdev->graph_mutex);
409
410         /* Walk the graph to locate the entities and video nodes. */
411         media_entity_graph_walk_start(&graph, entity);
412
413         while ((entity = media_entity_graph_walk_next(&graph))) {
414                 struct v4l2_subdev *subdev;
415                 struct vsp1_rwpf *rwpf;
416                 struct vsp1_entity *e;
417
418                 if (media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV) {
419                         pipe->num_video++;
420                         continue;
421                 }
422
423                 subdev = media_entity_to_v4l2_subdev(entity);
424                 e = to_vsp1_entity(subdev);
425                 list_add_tail(&e->list_pipe, &pipe->entities);
426
427                 if (e->type == VSP1_ENTITY_RPF) {
428                         rwpf = to_rwpf(subdev);
429                         pipe->inputs[pipe->num_inputs++] = rwpf;
430                         rwpf->video.pipe_index = pipe->num_inputs;
431                 } else if (e->type == VSP1_ENTITY_WPF) {
432                         rwpf = to_rwpf(subdev);
433                         pipe->output = to_rwpf(subdev);
434                         rwpf->video.pipe_index = 0;
435                 } else if (e->type == VSP1_ENTITY_LIF) {
436                         pipe->lif = e;
437                 } else if (e->type == VSP1_ENTITY_BRU) {
438                         pipe->bru = e;
439                 }
440         }
441
442         mutex_unlock(&mdev->graph_mutex);
443
444         /* We need one output and at least one input. */
445         if (pipe->num_inputs == 0 || !pipe->output) {
446                 ret = -EPIPE;
447                 goto error;
448         }
449
450         /* Follow links downstream for each input and make sure the graph
451          * contains no loop and that all branches end at the output WPF.
452          */
453         for (i = 0; i < pipe->num_inputs; ++i) {
454                 ret = vsp1_pipeline_validate_branch(pipe->inputs[i],
455                                                     pipe->output);
456                 if (ret < 0)
457                         goto error;
458         }
459
460         return 0;
461
462 error:
463         __vsp1_pipeline_cleanup(pipe);
464         return ret;
465 }
466
467 static int vsp1_pipeline_init(struct vsp1_pipeline *pipe,
468                               struct vsp1_video *video)
469 {
470         int ret;
471
472         mutex_lock(&pipe->lock);
473
474         /* If we're the first user validate and initialize the pipeline. */
475         if (pipe->use_count == 0) {
476                 ret = vsp1_pipeline_validate(pipe, video);
477                 if (ret < 0)
478                         goto done;
479         }
480
481         pipe->use_count++;
482         ret = 0;
483
484 done:
485         mutex_unlock(&pipe->lock);
486         return ret;
487 }
488
489 static void vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
490 {
491         mutex_lock(&pipe->lock);
492
493         /* If we're the last user clean up the pipeline. */
494         if (--pipe->use_count == 0)
495                 __vsp1_pipeline_cleanup(pipe);
496
497         mutex_unlock(&pipe->lock);
498 }
499
500 static void vsp1_pipeline_run(struct vsp1_pipeline *pipe)
501 {
502         struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
503
504         vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index), VI6_CMD_STRCMD);
505         pipe->state = VSP1_PIPELINE_RUNNING;
506         pipe->buffers_ready = 0;
507 }
508
509 static int vsp1_pipeline_stop(struct vsp1_pipeline *pipe)
510 {
511         struct vsp1_entity *entity;
512         unsigned long flags;
513         int ret;
514
515         spin_lock_irqsave(&pipe->irqlock, flags);
516         if (pipe->state == VSP1_PIPELINE_RUNNING)
517                 pipe->state = VSP1_PIPELINE_STOPPING;
518         spin_unlock_irqrestore(&pipe->irqlock, flags);
519
520         ret = wait_event_timeout(pipe->wq, pipe->state == VSP1_PIPELINE_STOPPED,
521                                  msecs_to_jiffies(500));
522         ret = ret == 0 ? -ETIMEDOUT : 0;
523
524         list_for_each_entry(entity, &pipe->entities, list_pipe) {
525                 if (entity->route && entity->route->reg)
526                         vsp1_write(entity->vsp1, entity->route->reg,
527                                    VI6_DPR_NODE_UNUSED);
528
529                 v4l2_subdev_call(&entity->subdev, video, s_stream, 0);
530         }
531
532         return ret;
533 }
534
535 static bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe)
536 {
537         unsigned int mask;
538
539         mask = ((1 << pipe->num_inputs) - 1) << 1;
540         if (!pipe->lif)
541                 mask |= 1 << 0;
542
543         return pipe->buffers_ready == mask;
544 }
545
546 /*
547  * vsp1_video_complete_buffer - Complete the current buffer
548  * @video: the video node
549  *
550  * This function completes the current buffer by filling its sequence number,
551  * time stamp and payload size, and hands it back to the videobuf core.
552  *
553  * When operating in DU output mode (deep pipeline to the DU through the LIF),
554  * the VSP1 needs to constantly supply frames to the display. In that case, if
555  * no other buffer is queued, reuse the one that has just been processed instead
556  * of handing it back to the videobuf core.
557  *
558  * Return the next queued buffer or NULL if the queue is empty.
559  */
560 static struct vsp1_video_buffer *
561 vsp1_video_complete_buffer(struct vsp1_video *video)
562 {
563         struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
564         struct vsp1_video_buffer *next = NULL;
565         struct vsp1_video_buffer *done;
566         unsigned long flags;
567         unsigned int i;
568
569         spin_lock_irqsave(&video->irqlock, flags);
570
571         if (list_empty(&video->irqqueue)) {
572                 spin_unlock_irqrestore(&video->irqlock, flags);
573                 return NULL;
574         }
575
576         done = list_first_entry(&video->irqqueue,
577                                 struct vsp1_video_buffer, queue);
578
579         /* In DU output mode reuse the buffer if the list is singular. */
580         if (pipe->lif && list_is_singular(&video->irqqueue)) {
581                 spin_unlock_irqrestore(&video->irqlock, flags);
582                 return done;
583         }
584
585         list_del(&done->queue);
586
587         if (!list_empty(&video->irqqueue))
588                 next = list_first_entry(&video->irqqueue,
589                                         struct vsp1_video_buffer, queue);
590
591         spin_unlock_irqrestore(&video->irqlock, flags);
592
593         done->buf.v4l2_buf.sequence = video->sequence++;
594         v4l2_get_timestamp(&done->buf.v4l2_buf.timestamp);
595         for (i = 0; i < done->buf.num_planes; ++i)
596                 vb2_set_plane_payload(&done->buf, i, done->length[i]);
597         vb2_buffer_done(&done->buf, VB2_BUF_STATE_DONE);
598
599         return next;
600 }
601
602 static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
603                                  struct vsp1_video *video)
604 {
605         struct vsp1_video_buffer *buf;
606         unsigned long flags;
607
608         buf = vsp1_video_complete_buffer(video);
609         if (buf == NULL)
610                 return;
611
612         spin_lock_irqsave(&pipe->irqlock, flags);
613
614         video->ops->queue(video, buf);
615         pipe->buffers_ready |= 1 << video->pipe_index;
616
617         spin_unlock_irqrestore(&pipe->irqlock, flags);
618 }
619
620 void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
621 {
622         enum vsp1_pipeline_state state;
623         unsigned long flags;
624         unsigned int i;
625
626         if (pipe == NULL)
627                 return;
628
629         /* Complete buffers on all video nodes. */
630         for (i = 0; i < pipe->num_inputs; ++i)
631                 vsp1_video_frame_end(pipe, &pipe->inputs[i]->video);
632
633         if (!pipe->lif)
634                 vsp1_video_frame_end(pipe, &pipe->output->video);
635
636         spin_lock_irqsave(&pipe->irqlock, flags);
637
638         state = pipe->state;
639         pipe->state = VSP1_PIPELINE_STOPPED;
640
641         /* If a stop has been requested, mark the pipeline as stopped and
642          * return.
643          */
644         if (state == VSP1_PIPELINE_STOPPING) {
645                 wake_up(&pipe->wq);
646                 goto done;
647         }
648
649         /* Restart the pipeline if ready. */
650         if (vsp1_pipeline_ready(pipe))
651                 vsp1_pipeline_run(pipe);
652
653 done:
654         spin_unlock_irqrestore(&pipe->irqlock, flags);
655 }
656
657 /* -----------------------------------------------------------------------------
658  * videobuf2 Queue Operations
659  */
660
661 static int
662 vsp1_video_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
663                      unsigned int *nbuffers, unsigned int *nplanes,
664                      unsigned int sizes[], void *alloc_ctxs[])
665 {
666         struct vsp1_video *video = vb2_get_drv_priv(vq);
667         const struct v4l2_pix_format_mplane *format;
668         struct v4l2_pix_format_mplane pix_mp;
669         unsigned int i;
670
671         if (fmt) {
672                 /* Make sure the format is valid and adjust the sizeimage field
673                  * if needed.
674                  */
675                 if (!vsp1_video_format_adjust(video, &fmt->fmt.pix_mp, &pix_mp))
676                         return -EINVAL;
677
678                 format = &pix_mp;
679         } else {
680                 format = &video->format;
681         }
682
683         *nplanes = format->num_planes;
684
685         for (i = 0; i < format->num_planes; ++i) {
686                 sizes[i] = format->plane_fmt[i].sizeimage;
687                 alloc_ctxs[i] = video->alloc_ctx;
688         }
689
690         return 0;
691 }
692
693 static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
694 {
695         struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
696         struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
697         const struct v4l2_pix_format_mplane *format = &video->format;
698         unsigned int i;
699
700         if (vb->num_planes < format->num_planes)
701                 return -EINVAL;
702
703         for (i = 0; i < vb->num_planes; ++i) {
704                 buf->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
705                 buf->length[i] = vb2_plane_size(vb, i);
706
707                 if (buf->length[i] < format->plane_fmt[i].sizeimage)
708                         return -EINVAL;
709         }
710
711         return 0;
712 }
713
714 static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
715 {
716         struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
717         struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
718         struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
719         unsigned long flags;
720         bool empty;
721
722         spin_lock_irqsave(&video->irqlock, flags);
723         empty = list_empty(&video->irqqueue);
724         list_add_tail(&buf->queue, &video->irqqueue);
725         spin_unlock_irqrestore(&video->irqlock, flags);
726
727         if (!empty)
728                 return;
729
730         spin_lock_irqsave(&pipe->irqlock, flags);
731
732         video->ops->queue(video, buf);
733         pipe->buffers_ready |= 1 << video->pipe_index;
734
735         if (vb2_is_streaming(&video->queue) &&
736             vsp1_pipeline_ready(pipe))
737                 vsp1_pipeline_run(pipe);
738
739         spin_unlock_irqrestore(&pipe->irqlock, flags);
740 }
741
742 static void vsp1_entity_route_setup(struct vsp1_entity *source)
743 {
744         struct vsp1_entity *sink;
745
746         if (source->route->reg == 0)
747                 return;
748
749         sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
750         vsp1_write(source->vsp1, source->route->reg,
751                    sink->route->inputs[source->sink_pad]);
752 }
753
754 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
755 {
756         struct vsp1_video *video = vb2_get_drv_priv(vq);
757         struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
758         struct vsp1_entity *entity;
759         unsigned long flags;
760         int ret;
761
762         mutex_lock(&pipe->lock);
763         if (pipe->stream_count == pipe->num_video - 1) {
764                 list_for_each_entry(entity, &pipe->entities, list_pipe) {
765                         vsp1_entity_route_setup(entity);
766
767                         ret = v4l2_subdev_call(&entity->subdev, video,
768                                                s_stream, 1);
769                         if (ret < 0) {
770                                 mutex_unlock(&pipe->lock);
771                                 return ret;
772                         }
773                 }
774         }
775
776         pipe->stream_count++;
777         mutex_unlock(&pipe->lock);
778
779         spin_lock_irqsave(&pipe->irqlock, flags);
780         if (vsp1_pipeline_ready(pipe))
781                 vsp1_pipeline_run(pipe);
782         spin_unlock_irqrestore(&pipe->irqlock, flags);
783
784         return 0;
785 }
786
787 static int vsp1_video_stop_streaming(struct vb2_queue *vq)
788 {
789         struct vsp1_video *video = vb2_get_drv_priv(vq);
790         struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
791         struct vsp1_video_buffer *buffer;
792         unsigned long flags;
793         int ret;
794
795         mutex_lock(&pipe->lock);
796         if (--pipe->stream_count == 0) {
797                 /* Stop the pipeline. */
798                 ret = vsp1_pipeline_stop(pipe);
799                 if (ret == -ETIMEDOUT)
800                         dev_err(video->vsp1->dev, "pipeline stop timeout\n");
801         }
802         mutex_unlock(&pipe->lock);
803
804         vsp1_pipeline_cleanup(pipe);
805         media_entity_pipeline_stop(&video->video.entity);
806
807         /* Remove all buffers from the IRQ queue. */
808         spin_lock_irqsave(&video->irqlock, flags);
809         list_for_each_entry(buffer, &video->irqqueue, queue)
810                 vb2_buffer_done(&buffer->buf, VB2_BUF_STATE_ERROR);
811         INIT_LIST_HEAD(&video->irqqueue);
812         spin_unlock_irqrestore(&video->irqlock, flags);
813
814         return 0;
815 }
816
817 static struct vb2_ops vsp1_video_queue_qops = {
818         .queue_setup = vsp1_video_queue_setup,
819         .buf_prepare = vsp1_video_buffer_prepare,
820         .buf_queue = vsp1_video_buffer_queue,
821         .wait_prepare = vb2_ops_wait_prepare,
822         .wait_finish = vb2_ops_wait_finish,
823         .start_streaming = vsp1_video_start_streaming,
824         .stop_streaming = vsp1_video_stop_streaming,
825 };
826
827 /* -----------------------------------------------------------------------------
828  * V4L2 ioctls
829  */
830
831 static int
832 vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
833 {
834         struct v4l2_fh *vfh = file->private_data;
835         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
836
837         cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
838                           | V4L2_CAP_VIDEO_CAPTURE_MPLANE
839                           | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
840
841         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
842                 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
843                                  | V4L2_CAP_STREAMING;
844         else
845                 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
846                                  | V4L2_CAP_STREAMING;
847
848         strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
849         strlcpy(cap->card, video->video.name, sizeof(cap->card));
850         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
851                  dev_name(video->vsp1->dev));
852
853         return 0;
854 }
855
856 static int
857 vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
858 {
859         struct v4l2_fh *vfh = file->private_data;
860         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
861
862         if (format->type != video->queue.type)
863                 return -EINVAL;
864
865         mutex_lock(&video->lock);
866         format->fmt.pix_mp = video->format;
867         mutex_unlock(&video->lock);
868
869         return 0;
870 }
871
872 static int
873 vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
874 {
875         struct v4l2_fh *vfh = file->private_data;
876         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
877
878         if (format->type != video->queue.type)
879                 return -EINVAL;
880
881         return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
882 }
883
884 static int
885 vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
886 {
887         struct v4l2_fh *vfh = file->private_data;
888         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
889         const struct vsp1_format_info *info;
890         int ret;
891
892         if (format->type != video->queue.type)
893                 return -EINVAL;
894
895         ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
896         if (ret < 0)
897                 return ret;
898
899         mutex_lock(&video->lock);
900
901         if (vb2_is_busy(&video->queue)) {
902                 ret = -EBUSY;
903                 goto done;
904         }
905
906         video->format = format->fmt.pix_mp;
907         video->fmtinfo = info;
908
909 done:
910         mutex_unlock(&video->lock);
911         return ret;
912 }
913
914 static int
915 vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
916 {
917         struct v4l2_fh *vfh = file->private_data;
918         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
919         struct vsp1_pipeline *pipe;
920         int ret;
921
922         if (video->queue.owner && video->queue.owner != file->private_data)
923                 return -EBUSY;
924
925         video->sequence = 0;
926
927         /* Start streaming on the pipeline. No link touching an entity in the
928          * pipeline can be activated or deactivated once streaming is started.
929          *
930          * Use the VSP1 pipeline object embedded in the first video object that
931          * starts streaming.
932          */
933         pipe = video->video.entity.pipe
934              ? to_vsp1_pipeline(&video->video.entity) : &video->pipe;
935
936         ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
937         if (ret < 0)
938                 return ret;
939
940         /* Verify that the configured format matches the output of the connected
941          * subdev.
942          */
943         ret = vsp1_video_verify_format(video);
944         if (ret < 0)
945                 goto err_stop;
946
947         ret = vsp1_pipeline_init(pipe, video);
948         if (ret < 0)
949                 goto err_stop;
950
951         /* Start the queue. */
952         ret = vb2_streamon(&video->queue, type);
953         if (ret < 0)
954                 goto err_cleanup;
955
956         return 0;
957
958 err_cleanup:
959         vsp1_pipeline_cleanup(pipe);
960 err_stop:
961         media_entity_pipeline_stop(&video->video.entity);
962         return ret;
963 }
964
965 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
966         .vidioc_querycap                = vsp1_video_querycap,
967         .vidioc_g_fmt_vid_cap_mplane    = vsp1_video_get_format,
968         .vidioc_s_fmt_vid_cap_mplane    = vsp1_video_set_format,
969         .vidioc_try_fmt_vid_cap_mplane  = vsp1_video_try_format,
970         .vidioc_g_fmt_vid_out_mplane    = vsp1_video_get_format,
971         .vidioc_s_fmt_vid_out_mplane    = vsp1_video_set_format,
972         .vidioc_try_fmt_vid_out_mplane  = vsp1_video_try_format,
973         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
974         .vidioc_querybuf                = vb2_ioctl_querybuf,
975         .vidioc_qbuf                    = vb2_ioctl_qbuf,
976         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
977         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
978         .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
979         .vidioc_streamon                = vsp1_video_streamon,
980         .vidioc_streamoff               = vb2_ioctl_streamoff,
981 };
982
983 /* -----------------------------------------------------------------------------
984  * V4L2 File Operations
985  */
986
987 static int vsp1_video_open(struct file *file)
988 {
989         struct vsp1_video *video = video_drvdata(file);
990         struct v4l2_fh *vfh;
991         int ret = 0;
992
993         vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
994         if (vfh == NULL)
995                 return -ENOMEM;
996
997         v4l2_fh_init(vfh, &video->video);
998         v4l2_fh_add(vfh);
999
1000         file->private_data = vfh;
1001
1002         ret = vsp1_device_get(video->vsp1);
1003         if (ret < 0) {
1004                 v4l2_fh_del(vfh);
1005                 kfree(vfh);
1006         }
1007
1008         return ret;
1009 }
1010
1011 static int vsp1_video_release(struct file *file)
1012 {
1013         struct vsp1_video *video = video_drvdata(file);
1014         struct v4l2_fh *vfh = file->private_data;
1015
1016         mutex_lock(&video->lock);
1017         if (video->queue.owner == vfh) {
1018                 vb2_queue_release(&video->queue);
1019                 video->queue.owner = NULL;
1020         }
1021         mutex_unlock(&video->lock);
1022
1023         vsp1_device_put(video->vsp1);
1024
1025         v4l2_fh_release(file);
1026
1027         file->private_data = NULL;
1028
1029         return 0;
1030 }
1031
1032 static struct v4l2_file_operations vsp1_video_fops = {
1033         .owner = THIS_MODULE,
1034         .unlocked_ioctl = video_ioctl2,
1035         .open = vsp1_video_open,
1036         .release = vsp1_video_release,
1037         .poll = vb2_fop_poll,
1038         .mmap = vb2_fop_mmap,
1039 };
1040
1041 /* -----------------------------------------------------------------------------
1042  * Initialization and Cleanup
1043  */
1044
1045 int vsp1_video_init(struct vsp1_video *video, struct vsp1_entity *rwpf)
1046 {
1047         const char *direction;
1048         int ret;
1049
1050         switch (video->type) {
1051         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1052                 direction = "output";
1053                 video->pad.flags = MEDIA_PAD_FL_SINK;
1054                 break;
1055
1056         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1057                 direction = "input";
1058                 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1059                 video->video.vfl_dir = VFL_DIR_TX;
1060                 break;
1061
1062         default:
1063                 return -EINVAL;
1064         }
1065
1066         video->rwpf = rwpf;
1067
1068         mutex_init(&video->lock);
1069         spin_lock_init(&video->irqlock);
1070         INIT_LIST_HEAD(&video->irqqueue);
1071
1072         mutex_init(&video->pipe.lock);
1073         spin_lock_init(&video->pipe.irqlock);
1074         INIT_LIST_HEAD(&video->pipe.entities);
1075         init_waitqueue_head(&video->pipe.wq);
1076         video->pipe.state = VSP1_PIPELINE_STOPPED;
1077
1078         /* Initialize the media entity... */
1079         ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1080         if (ret < 0)
1081                 return ret;
1082
1083         /* ... and the format ... */
1084         video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
1085         video->format.pixelformat = video->fmtinfo->fourcc;
1086         video->format.colorspace = V4L2_COLORSPACE_SRGB;
1087         video->format.field = V4L2_FIELD_NONE;
1088         video->format.width = VSP1_VIDEO_DEF_WIDTH;
1089         video->format.height = VSP1_VIDEO_DEF_HEIGHT;
1090         video->format.num_planes = 1;
1091         video->format.plane_fmt[0].bytesperline =
1092                 video->format.width * video->fmtinfo->bpp[0] / 8;
1093         video->format.plane_fmt[0].sizeimage =
1094                 video->format.plane_fmt[0].bytesperline * video->format.height;
1095
1096         /* ... and the video node... */
1097         video->video.v4l2_dev = &video->vsp1->v4l2_dev;
1098         video->video.fops = &vsp1_video_fops;
1099         snprintf(video->video.name, sizeof(video->video.name), "%s %s",
1100                  rwpf->subdev.name, direction);
1101         video->video.vfl_type = VFL_TYPE_GRABBER;
1102         video->video.release = video_device_release_empty;
1103         video->video.ioctl_ops = &vsp1_video_ioctl_ops;
1104
1105         video_set_drvdata(&video->video, video);
1106
1107         /* ... and the buffers queue... */
1108         video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
1109         if (IS_ERR(video->alloc_ctx)) {
1110                 ret = PTR_ERR(video->alloc_ctx);
1111                 goto error;
1112         }
1113
1114         video->queue.type = video->type;
1115         video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1116         video->queue.lock = &video->lock;
1117         video->queue.drv_priv = video;
1118         video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer);
1119         video->queue.ops = &vsp1_video_queue_qops;
1120         video->queue.mem_ops = &vb2_dma_contig_memops;
1121         video->queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1122         ret = vb2_queue_init(&video->queue);
1123         if (ret < 0) {
1124                 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1125                 goto error;
1126         }
1127
1128         /* ... and register the video device. */
1129         video->video.queue = &video->queue;
1130         ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1131         if (ret < 0) {
1132                 dev_err(video->vsp1->dev, "failed to register video device\n");
1133                 goto error;
1134         }
1135
1136         return 0;
1137
1138 error:
1139         vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1140         vsp1_video_cleanup(video);
1141         return ret;
1142 }
1143
1144 void vsp1_video_cleanup(struct vsp1_video *video)
1145 {
1146         if (video_is_registered(&video->video))
1147                 video_unregister_device(&video->video);
1148
1149         vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1150         media_entity_cleanup(&video->video.entity);
1151 }