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