Linux 3.14.25
[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 Renesas 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         for (i = 0; i < vb->num_planes; ++i) {
639                 buf->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
640                 buf->length[i] = vb2_plane_size(vb, i);
641
642                 if (buf->length[i] < format->plane_fmt[i].sizeimage)
643                         return -EINVAL;
644         }
645
646         return 0;
647 }
648
649 static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
650 {
651         struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
652         struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
653         struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
654         unsigned long flags;
655         bool empty;
656
657         spin_lock_irqsave(&video->irqlock, flags);
658         empty = list_empty(&video->irqqueue);
659         list_add_tail(&buf->queue, &video->irqqueue);
660         spin_unlock_irqrestore(&video->irqlock, flags);
661
662         if (!empty)
663                 return;
664
665         spin_lock_irqsave(&pipe->irqlock, flags);
666
667         video->ops->queue(video, buf);
668         pipe->buffers_ready |= 1 << video->pipe_index;
669
670         if (vb2_is_streaming(&video->queue) &&
671             vsp1_pipeline_ready(pipe))
672                 vsp1_pipeline_run(pipe);
673
674         spin_unlock_irqrestore(&pipe->irqlock, flags);
675 }
676
677 static void vsp1_entity_route_setup(struct vsp1_entity *source)
678 {
679         struct vsp1_entity *sink;
680
681         if (source->route == 0)
682                 return;
683
684         sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
685         vsp1_write(source->vsp1, source->route, sink->id);
686 }
687
688 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
689 {
690         struct vsp1_video *video = vb2_get_drv_priv(vq);
691         struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
692         struct vsp1_entity *entity;
693         unsigned long flags;
694         int ret;
695
696         mutex_lock(&pipe->lock);
697         if (pipe->stream_count == pipe->num_video - 1) {
698                 list_for_each_entry(entity, &pipe->entities, list_pipe) {
699                         vsp1_entity_route_setup(entity);
700
701                         ret = v4l2_subdev_call(&entity->subdev, video,
702                                                s_stream, 1);
703                         if (ret < 0) {
704                                 mutex_unlock(&pipe->lock);
705                                 return ret;
706                         }
707                 }
708         }
709
710         pipe->stream_count++;
711         mutex_unlock(&pipe->lock);
712
713         spin_lock_irqsave(&pipe->irqlock, flags);
714         if (vsp1_pipeline_ready(pipe))
715                 vsp1_pipeline_run(pipe);
716         spin_unlock_irqrestore(&pipe->irqlock, flags);
717
718         return 0;
719 }
720
721 static int vsp1_video_stop_streaming(struct vb2_queue *vq)
722 {
723         struct vsp1_video *video = vb2_get_drv_priv(vq);
724         struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
725         unsigned long flags;
726         int ret;
727
728         mutex_lock(&pipe->lock);
729         if (--pipe->stream_count == 0) {
730                 /* Stop the pipeline. */
731                 ret = vsp1_pipeline_stop(pipe);
732                 if (ret == -ETIMEDOUT)
733                         dev_err(video->vsp1->dev, "pipeline stop timeout\n");
734         }
735         mutex_unlock(&pipe->lock);
736
737         vsp1_pipeline_cleanup(pipe);
738         media_entity_pipeline_stop(&video->video.entity);
739
740         /* Remove all buffers from the IRQ queue. */
741         spin_lock_irqsave(&video->irqlock, flags);
742         INIT_LIST_HEAD(&video->irqqueue);
743         spin_unlock_irqrestore(&video->irqlock, flags);
744
745         return 0;
746 }
747
748 static struct vb2_ops vsp1_video_queue_qops = {
749         .queue_setup = vsp1_video_queue_setup,
750         .buf_prepare = vsp1_video_buffer_prepare,
751         .buf_queue = vsp1_video_buffer_queue,
752         .wait_prepare = vb2_ops_wait_prepare,
753         .wait_finish = vb2_ops_wait_finish,
754         .start_streaming = vsp1_video_start_streaming,
755         .stop_streaming = vsp1_video_stop_streaming,
756 };
757
758 /* -----------------------------------------------------------------------------
759  * V4L2 ioctls
760  */
761
762 static int
763 vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
764 {
765         struct v4l2_fh *vfh = file->private_data;
766         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
767
768         cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
769                           | V4L2_CAP_VIDEO_CAPTURE_MPLANE
770                           | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
771
772         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
773                 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
774                                  | V4L2_CAP_STREAMING;
775         else
776                 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
777                                  | V4L2_CAP_STREAMING;
778
779         strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
780         strlcpy(cap->card, video->video.name, sizeof(cap->card));
781         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
782                  dev_name(video->vsp1->dev));
783
784         return 0;
785 }
786
787 static int
788 vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
789 {
790         struct v4l2_fh *vfh = file->private_data;
791         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
792
793         if (format->type != video->queue.type)
794                 return -EINVAL;
795
796         mutex_lock(&video->lock);
797         format->fmt.pix_mp = video->format;
798         mutex_unlock(&video->lock);
799
800         return 0;
801 }
802
803 static int
804 vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
805 {
806         struct v4l2_fh *vfh = file->private_data;
807         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
808
809         if (format->type != video->queue.type)
810                 return -EINVAL;
811
812         return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
813 }
814
815 static int
816 vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
817 {
818         struct v4l2_fh *vfh = file->private_data;
819         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
820         const struct vsp1_format_info *info;
821         int ret;
822
823         if (format->type != video->queue.type)
824                 return -EINVAL;
825
826         ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
827         if (ret < 0)
828                 return ret;
829
830         mutex_lock(&video->lock);
831
832         if (vb2_is_busy(&video->queue)) {
833                 ret = -EBUSY;
834                 goto done;
835         }
836
837         video->format = format->fmt.pix_mp;
838         video->fmtinfo = info;
839
840 done:
841         mutex_unlock(&video->lock);
842         return ret;
843 }
844
845 static int
846 vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
847 {
848         struct v4l2_fh *vfh = file->private_data;
849         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
850         struct vsp1_pipeline *pipe;
851         int ret;
852
853         if (video->queue.owner && video->queue.owner != file->private_data)
854                 return -EBUSY;
855
856         video->sequence = 0;
857
858         /* Start streaming on the pipeline. No link touching an entity in the
859          * pipeline can be activated or deactivated once streaming is started.
860          *
861          * Use the VSP1 pipeline object embedded in the first video object that
862          * starts streaming.
863          */
864         pipe = video->video.entity.pipe
865              ? to_vsp1_pipeline(&video->video.entity) : &video->pipe;
866
867         ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
868         if (ret < 0)
869                 return ret;
870
871         /* Verify that the configured format matches the output of the connected
872          * subdev.
873          */
874         ret = vsp1_video_verify_format(video);
875         if (ret < 0)
876                 goto err_stop;
877
878         ret = vsp1_pipeline_init(pipe, video);
879         if (ret < 0)
880                 goto err_stop;
881
882         /* Start the queue. */
883         ret = vb2_streamon(&video->queue, type);
884         if (ret < 0)
885                 goto err_cleanup;
886
887         return 0;
888
889 err_cleanup:
890         vsp1_pipeline_cleanup(pipe);
891 err_stop:
892         media_entity_pipeline_stop(&video->video.entity);
893         return ret;
894 }
895
896 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
897         .vidioc_querycap                = vsp1_video_querycap,
898         .vidioc_g_fmt_vid_cap_mplane    = vsp1_video_get_format,
899         .vidioc_s_fmt_vid_cap_mplane    = vsp1_video_set_format,
900         .vidioc_try_fmt_vid_cap_mplane  = vsp1_video_try_format,
901         .vidioc_g_fmt_vid_out_mplane    = vsp1_video_get_format,
902         .vidioc_s_fmt_vid_out_mplane    = vsp1_video_set_format,
903         .vidioc_try_fmt_vid_out_mplane  = vsp1_video_try_format,
904         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
905         .vidioc_querybuf                = vb2_ioctl_querybuf,
906         .vidioc_qbuf                    = vb2_ioctl_qbuf,
907         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
908         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
909         .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
910         .vidioc_streamon                = vsp1_video_streamon,
911         .vidioc_streamoff               = vb2_ioctl_streamoff,
912 };
913
914 /* -----------------------------------------------------------------------------
915  * V4L2 File Operations
916  */
917
918 static int vsp1_video_open(struct file *file)
919 {
920         struct vsp1_video *video = video_drvdata(file);
921         struct v4l2_fh *vfh;
922         int ret = 0;
923
924         vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
925         if (vfh == NULL)
926                 return -ENOMEM;
927
928         v4l2_fh_init(vfh, &video->video);
929         v4l2_fh_add(vfh);
930
931         file->private_data = vfh;
932
933         if (!vsp1_device_get(video->vsp1)) {
934                 ret = -EBUSY;
935                 v4l2_fh_del(vfh);
936                 kfree(vfh);
937         }
938
939         return ret;
940 }
941
942 static int vsp1_video_release(struct file *file)
943 {
944         struct vsp1_video *video = video_drvdata(file);
945         struct v4l2_fh *vfh = file->private_data;
946
947         mutex_lock(&video->lock);
948         if (video->queue.owner == vfh) {
949                 vb2_queue_release(&video->queue);
950                 video->queue.owner = NULL;
951         }
952         mutex_unlock(&video->lock);
953
954         vsp1_device_put(video->vsp1);
955
956         v4l2_fh_release(file);
957
958         file->private_data = NULL;
959
960         return 0;
961 }
962
963 static struct v4l2_file_operations vsp1_video_fops = {
964         .owner = THIS_MODULE,
965         .unlocked_ioctl = video_ioctl2,
966         .open = vsp1_video_open,
967         .release = vsp1_video_release,
968         .poll = vb2_fop_poll,
969         .mmap = vb2_fop_mmap,
970 };
971
972 /* -----------------------------------------------------------------------------
973  * Initialization and Cleanup
974  */
975
976 int vsp1_video_init(struct vsp1_video *video, struct vsp1_entity *rwpf)
977 {
978         const char *direction;
979         int ret;
980
981         switch (video->type) {
982         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
983                 direction = "output";
984                 video->pad.flags = MEDIA_PAD_FL_SINK;
985                 break;
986
987         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
988                 direction = "input";
989                 video->pad.flags = MEDIA_PAD_FL_SOURCE;
990                 video->video.vfl_dir = VFL_DIR_TX;
991                 break;
992
993         default:
994                 return -EINVAL;
995         }
996
997         video->rwpf = rwpf;
998
999         mutex_init(&video->lock);
1000         spin_lock_init(&video->irqlock);
1001         INIT_LIST_HEAD(&video->irqqueue);
1002
1003         mutex_init(&video->pipe.lock);
1004         spin_lock_init(&video->pipe.irqlock);
1005         INIT_LIST_HEAD(&video->pipe.entities);
1006         init_waitqueue_head(&video->pipe.wq);
1007         video->pipe.state = VSP1_PIPELINE_STOPPED;
1008
1009         /* Initialize the media entity... */
1010         ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1011         if (ret < 0)
1012                 return ret;
1013
1014         /* ... and the format ... */
1015         video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
1016         video->format.pixelformat = video->fmtinfo->fourcc;
1017         video->format.colorspace = V4L2_COLORSPACE_SRGB;
1018         video->format.field = V4L2_FIELD_NONE;
1019         video->format.width = VSP1_VIDEO_DEF_WIDTH;
1020         video->format.height = VSP1_VIDEO_DEF_HEIGHT;
1021         video->format.num_planes = 1;
1022         video->format.plane_fmt[0].bytesperline =
1023                 video->format.width * video->fmtinfo->bpp[0] / 8;
1024         video->format.plane_fmt[0].sizeimage =
1025                 video->format.plane_fmt[0].bytesperline * video->format.height;
1026
1027         /* ... and the video node... */
1028         video->video.v4l2_dev = &video->vsp1->v4l2_dev;
1029         video->video.fops = &vsp1_video_fops;
1030         snprintf(video->video.name, sizeof(video->video.name), "%s %s",
1031                  rwpf->subdev.name, direction);
1032         video->video.vfl_type = VFL_TYPE_GRABBER;
1033         video->video.release = video_device_release_empty;
1034         video->video.ioctl_ops = &vsp1_video_ioctl_ops;
1035
1036         video_set_drvdata(&video->video, video);
1037
1038         /* ... and the buffers queue... */
1039         video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
1040         if (IS_ERR(video->alloc_ctx)) {
1041                 ret = PTR_ERR(video->alloc_ctx);
1042                 goto error;
1043         }
1044
1045         video->queue.type = video->type;
1046         video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1047         video->queue.lock = &video->lock;
1048         video->queue.drv_priv = video;
1049         video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer);
1050         video->queue.ops = &vsp1_video_queue_qops;
1051         video->queue.mem_ops = &vb2_dma_contig_memops;
1052         video->queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1053         ret = vb2_queue_init(&video->queue);
1054         if (ret < 0) {
1055                 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1056                 goto error;
1057         }
1058
1059         /* ... and register the video device. */
1060         video->video.queue = &video->queue;
1061         ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1062         if (ret < 0) {
1063                 dev_err(video->vsp1->dev, "failed to register video device\n");
1064                 goto error;
1065         }
1066
1067         return 0;
1068
1069 error:
1070         vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1071         vsp1_video_cleanup(video);
1072         return ret;
1073 }
1074
1075 void vsp1_video_cleanup(struct vsp1_video *video)
1076 {
1077         if (video_is_registered(&video->video))
1078                 video_unregister_device(&video->video);
1079
1080         vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1081         media_entity_cleanup(&video->video.entity);
1082 }