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