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