]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/platform/vsp1/vsp1_video.c
[media] v4l: vsp1: Add HGO support
[karo-tx-linux.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_hgo.h"
35 #include "vsp1_pipe.h"
36 #include "vsp1_rwpf.h"
37 #include "vsp1_uds.h"
38 #include "vsp1_video.h"
39
40 #define VSP1_VIDEO_DEF_FORMAT           V4L2_PIX_FMT_YUYV
41 #define VSP1_VIDEO_DEF_WIDTH            1024
42 #define VSP1_VIDEO_DEF_HEIGHT           768
43
44 #define VSP1_VIDEO_MIN_WIDTH            2U
45 #define VSP1_VIDEO_MAX_WIDTH            8190U
46 #define VSP1_VIDEO_MIN_HEIGHT           2U
47 #define VSP1_VIDEO_MAX_HEIGHT           8190U
48
49 /* -----------------------------------------------------------------------------
50  * Helper functions
51  */
52
53 static struct v4l2_subdev *
54 vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
55 {
56         struct media_pad *remote;
57
58         remote = media_entity_remote_pad(local);
59         if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
60                 return NULL;
61
62         if (pad)
63                 *pad = remote->index;
64
65         return media_entity_to_v4l2_subdev(remote->entity);
66 }
67
68 static int vsp1_video_verify_format(struct vsp1_video *video)
69 {
70         struct v4l2_subdev_format fmt;
71         struct v4l2_subdev *subdev;
72         int ret;
73
74         subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
75         if (subdev == NULL)
76                 return -EINVAL;
77
78         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
79         ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
80         if (ret < 0)
81                 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
82
83         if (video->rwpf->fmtinfo->mbus != fmt.format.code ||
84             video->rwpf->format.height != fmt.format.height ||
85             video->rwpf->format.width != fmt.format.width)
86                 return -EINVAL;
87
88         return 0;
89 }
90
91 static int __vsp1_video_try_format(struct vsp1_video *video,
92                                    struct v4l2_pix_format_mplane *pix,
93                                    const struct vsp1_format_info **fmtinfo)
94 {
95         static const u32 xrgb_formats[][2] = {
96                 { V4L2_PIX_FMT_RGB444, V4L2_PIX_FMT_XRGB444 },
97                 { V4L2_PIX_FMT_RGB555, V4L2_PIX_FMT_XRGB555 },
98                 { V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_XBGR32 },
99                 { V4L2_PIX_FMT_RGB32, V4L2_PIX_FMT_XRGB32 },
100         };
101
102         const struct vsp1_format_info *info;
103         unsigned int width = pix->width;
104         unsigned int height = pix->height;
105         unsigned int i;
106
107         /*
108          * Backward compatibility: replace deprecated RGB formats by their XRGB
109          * equivalent. This selects the format older userspace applications want
110          * while still exposing the new format.
111          */
112         for (i = 0; i < ARRAY_SIZE(xrgb_formats); ++i) {
113                 if (xrgb_formats[i][0] == pix->pixelformat) {
114                         pix->pixelformat = xrgb_formats[i][1];
115                         break;
116                 }
117         }
118
119         /*
120          * Retrieve format information and select the default format if the
121          * requested format isn't supported.
122          */
123         info = vsp1_get_format_info(video->vsp1, pix->pixelformat);
124         if (info == NULL)
125                 info = vsp1_get_format_info(video->vsp1, VSP1_VIDEO_DEF_FORMAT);
126
127         pix->pixelformat = info->fourcc;
128         pix->colorspace = V4L2_COLORSPACE_SRGB;
129         pix->field = V4L2_FIELD_NONE;
130
131         if (info->fourcc == V4L2_PIX_FMT_HSV24 ||
132             info->fourcc == V4L2_PIX_FMT_HSV32)
133                 pix->hsv_enc = V4L2_HSV_ENC_256;
134
135         memset(pix->reserved, 0, sizeof(pix->reserved));
136
137         /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
138         width = round_down(width, info->hsub);
139         height = round_down(height, info->vsub);
140
141         /* Clamp the width and height. */
142         pix->width = clamp(width, VSP1_VIDEO_MIN_WIDTH, VSP1_VIDEO_MAX_WIDTH);
143         pix->height = clamp(height, VSP1_VIDEO_MIN_HEIGHT,
144                             VSP1_VIDEO_MAX_HEIGHT);
145
146         /*
147          * Compute and clamp the stride and image size. While not documented in
148          * the datasheet, strides not aligned to a multiple of 128 bytes result
149          * in image corruption.
150          */
151         for (i = 0; i < min(info->planes, 2U); ++i) {
152                 unsigned int hsub = i > 0 ? info->hsub : 1;
153                 unsigned int vsub = i > 0 ? info->vsub : 1;
154                 unsigned int align = 128;
155                 unsigned int bpl;
156
157                 bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
158                               pix->width / hsub * info->bpp[i] / 8,
159                               round_down(65535U, align));
160
161                 pix->plane_fmt[i].bytesperline = round_up(bpl, align);
162                 pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
163                                             * pix->height / vsub;
164         }
165
166         if (info->planes == 3) {
167                 /* The second and third planes must have the same stride. */
168                 pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
169                 pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
170         }
171
172         pix->num_planes = info->planes;
173
174         if (fmtinfo)
175                 *fmtinfo = info;
176
177         return 0;
178 }
179
180 /* -----------------------------------------------------------------------------
181  * VSP1 Partition Algorithm support
182  */
183
184 static void vsp1_video_pipeline_setup_partitions(struct vsp1_pipeline *pipe)
185 {
186         struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
187         const struct v4l2_mbus_framefmt *format;
188         struct vsp1_entity *entity;
189         unsigned int div_size;
190
191         /*
192          * Partitions are computed on the size before rotation, use the format
193          * at the WPF sink.
194          */
195         format = vsp1_entity_get_pad_format(&pipe->output->entity,
196                                             pipe->output->entity.config,
197                                             RWPF_PAD_SINK);
198         div_size = format->width;
199
200         /* Gen2 hardware doesn't require image partitioning. */
201         if (vsp1->info->gen == 2) {
202                 pipe->div_size = div_size;
203                 pipe->partitions = 1;
204                 return;
205         }
206
207         list_for_each_entry(entity, &pipe->entities, list_pipe) {
208                 unsigned int entity_max = VSP1_VIDEO_MAX_WIDTH;
209
210                 if (entity->ops->max_width) {
211                         entity_max = entity->ops->max_width(entity, pipe);
212                         if (entity_max)
213                                 div_size = min(div_size, entity_max);
214                 }
215         }
216
217         pipe->div_size = div_size;
218         pipe->partitions = DIV_ROUND_UP(format->width, div_size);
219 }
220
221 /**
222  * vsp1_video_partition - Calculate the active partition output window
223  *
224  * @div_size: pre-determined maximum partition division size
225  * @index: partition index
226  *
227  * Returns a v4l2_rect describing the partition window.
228  */
229 static struct v4l2_rect vsp1_video_partition(struct vsp1_pipeline *pipe,
230                                              unsigned int div_size,
231                                              unsigned int index)
232 {
233         const struct v4l2_mbus_framefmt *format;
234         struct v4l2_rect partition;
235         unsigned int modulus;
236
237         /*
238          * Partitions are computed on the size before rotation, use the format
239          * at the WPF sink.
240          */
241         format = vsp1_entity_get_pad_format(&pipe->output->entity,
242                                             pipe->output->entity.config,
243                                             RWPF_PAD_SINK);
244
245         /* A single partition simply processes the output size in full. */
246         if (pipe->partitions <= 1) {
247                 partition.left = 0;
248                 partition.top = 0;
249                 partition.width = format->width;
250                 partition.height = format->height;
251                 return partition;
252         }
253
254         /* Initialise the partition with sane starting conditions. */
255         partition.left = index * div_size;
256         partition.top = 0;
257         partition.width = div_size;
258         partition.height = format->height;
259
260         modulus = format->width % div_size;
261
262         /*
263          * We need to prevent the last partition from being smaller than the
264          * *minimum* width of the hardware capabilities.
265          *
266          * If the modulus is less than half of the partition size,
267          * the penultimate partition is reduced to half, which is added
268          * to the final partition: |1234|1234|1234|12|341|
269          * to prevents this:       |1234|1234|1234|1234|1|.
270          */
271         if (modulus) {
272                 /*
273                  * pipe->partitions is 1 based, whilst index is a 0 based index.
274                  * Normalise this locally.
275                  */
276                 unsigned int partitions = pipe->partitions - 1;
277
278                 if (modulus < div_size / 2) {
279                         if (index == partitions - 1) {
280                                 /* Halve the penultimate partition. */
281                                 partition.width = div_size / 2;
282                         } else if (index == partitions) {
283                                 /* Increase the final partition. */
284                                 partition.width = (div_size / 2) + modulus;
285                                 partition.left -= div_size / 2;
286                         }
287                 } else if (index == partitions) {
288                         partition.width = modulus;
289                 }
290         }
291
292         return partition;
293 }
294
295 /* -----------------------------------------------------------------------------
296  * Pipeline Management
297  */
298
299 /*
300  * vsp1_video_complete_buffer - Complete the current buffer
301  * @video: the video node
302  *
303  * This function completes the current buffer by filling its sequence number,
304  * time stamp and payload size, and hands it back to the videobuf core.
305  *
306  * When operating in DU output mode (deep pipeline to the DU through the LIF),
307  * the VSP1 needs to constantly supply frames to the display. In that case, if
308  * no other buffer is queued, reuse the one that has just been processed instead
309  * of handing it back to the videobuf core.
310  *
311  * Return the next queued buffer or NULL if the queue is empty.
312  */
313 static struct vsp1_vb2_buffer *
314 vsp1_video_complete_buffer(struct vsp1_video *video)
315 {
316         struct vsp1_pipeline *pipe = video->rwpf->pipe;
317         struct vsp1_vb2_buffer *next = NULL;
318         struct vsp1_vb2_buffer *done;
319         unsigned long flags;
320         unsigned int i;
321
322         spin_lock_irqsave(&video->irqlock, flags);
323
324         if (list_empty(&video->irqqueue)) {
325                 spin_unlock_irqrestore(&video->irqlock, flags);
326                 return NULL;
327         }
328
329         done = list_first_entry(&video->irqqueue,
330                                 struct vsp1_vb2_buffer, queue);
331
332         /* In DU output mode reuse the buffer if the list is singular. */
333         if (pipe->lif && list_is_singular(&video->irqqueue)) {
334                 spin_unlock_irqrestore(&video->irqlock, flags);
335                 return done;
336         }
337
338         list_del(&done->queue);
339
340         if (!list_empty(&video->irqqueue))
341                 next = list_first_entry(&video->irqqueue,
342                                         struct vsp1_vb2_buffer, queue);
343
344         spin_unlock_irqrestore(&video->irqlock, flags);
345
346         done->buf.sequence = pipe->sequence;
347         done->buf.vb2_buf.timestamp = ktime_get_ns();
348         for (i = 0; i < done->buf.vb2_buf.num_planes; ++i)
349                 vb2_set_plane_payload(&done->buf.vb2_buf, i,
350                                       vb2_plane_size(&done->buf.vb2_buf, i));
351         vb2_buffer_done(&done->buf.vb2_buf, VB2_BUF_STATE_DONE);
352
353         return next;
354 }
355
356 static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
357                                  struct vsp1_rwpf *rwpf)
358 {
359         struct vsp1_video *video = rwpf->video;
360         struct vsp1_vb2_buffer *buf;
361
362         buf = vsp1_video_complete_buffer(video);
363         if (buf == NULL)
364                 return;
365
366         video->rwpf->mem = buf->mem;
367         pipe->buffers_ready |= 1 << video->pipe_index;
368 }
369
370 static void vsp1_video_pipeline_run_partition(struct vsp1_pipeline *pipe,
371                                               struct vsp1_dl_list *dl)
372 {
373         struct vsp1_entity *entity;
374
375         pipe->partition = vsp1_video_partition(pipe, pipe->div_size,
376                                                pipe->current_partition);
377
378         list_for_each_entry(entity, &pipe->entities, list_pipe) {
379                 if (entity->ops->configure)
380                         entity->ops->configure(entity, pipe, dl,
381                                                VSP1_ENTITY_PARAMS_PARTITION);
382         }
383 }
384
385 static void vsp1_video_pipeline_run(struct vsp1_pipeline *pipe)
386 {
387         struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
388         struct vsp1_entity *entity;
389
390         if (!pipe->dl)
391                 pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
392
393         /*
394          * Start with the runtime parameters as the configure operation can
395          * compute/cache information needed when configuring partitions. This
396          * is the case with flipping in the WPF.
397          */
398         list_for_each_entry(entity, &pipe->entities, list_pipe) {
399                 if (entity->ops->configure)
400                         entity->ops->configure(entity, pipe, pipe->dl,
401                                                VSP1_ENTITY_PARAMS_RUNTIME);
402         }
403
404         /* Run the first partition */
405         pipe->current_partition = 0;
406         vsp1_video_pipeline_run_partition(pipe, pipe->dl);
407
408         /* Process consecutive partitions as necessary */
409         for (pipe->current_partition = 1;
410              pipe->current_partition < pipe->partitions;
411              pipe->current_partition++) {
412                 struct vsp1_dl_list *dl;
413
414                 /*
415                  * Partition configuration operations will utilise
416                  * the pipe->current_partition variable to determine
417                  * the work they should complete.
418                  */
419                 dl = vsp1_dl_list_get(pipe->output->dlm);
420
421                 /*
422                  * An incomplete chain will still function, but output only
423                  * the partitions that had a dl available. The frame end
424                  * interrupt will be marked on the last dl in the chain.
425                  */
426                 if (!dl) {
427                         dev_err(vsp1->dev, "Failed to obtain a dl list. Frame will be incomplete\n");
428                         break;
429                 }
430
431                 vsp1_video_pipeline_run_partition(pipe, dl);
432                 vsp1_dl_list_add_chain(pipe->dl, dl);
433         }
434
435         /* Complete, and commit the head display list. */
436         vsp1_dl_list_commit(pipe->dl);
437         pipe->dl = NULL;
438
439         vsp1_pipeline_run(pipe);
440 }
441
442 static void vsp1_video_pipeline_frame_end(struct vsp1_pipeline *pipe)
443 {
444         struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
445         enum vsp1_pipeline_state state;
446         unsigned long flags;
447         unsigned int i;
448
449         spin_lock_irqsave(&pipe->irqlock, flags);
450
451         /* Complete buffers on all video nodes. */
452         for (i = 0; i < vsp1->info->rpf_count; ++i) {
453                 if (!pipe->inputs[i])
454                         continue;
455
456                 vsp1_video_frame_end(pipe, pipe->inputs[i]);
457         }
458
459         vsp1_video_frame_end(pipe, pipe->output);
460
461         state = pipe->state;
462         pipe->state = VSP1_PIPELINE_STOPPED;
463
464         /*
465          * If a stop has been requested, mark the pipeline as stopped and
466          * return. Otherwise restart the pipeline if ready.
467          */
468         if (state == VSP1_PIPELINE_STOPPING)
469                 wake_up(&pipe->wq);
470         else if (vsp1_pipeline_ready(pipe))
471                 vsp1_video_pipeline_run(pipe);
472
473         spin_unlock_irqrestore(&pipe->irqlock, flags);
474 }
475
476 static int vsp1_video_pipeline_build_branch(struct vsp1_pipeline *pipe,
477                                             struct vsp1_rwpf *input,
478                                             struct vsp1_rwpf *output)
479 {
480         struct media_entity_enum ent_enum;
481         struct vsp1_entity *entity;
482         struct media_pad *pad;
483         bool bru_found = false;
484         int ret;
485
486         ret = media_entity_enum_init(&ent_enum, &input->entity.vsp1->media_dev);
487         if (ret < 0)
488                 return ret;
489
490         /*
491          * The main data path doesn't include the HGO or HGT, use
492          * vsp1_entity_remote_pad() to traverse the graph.
493          */
494
495         pad = vsp1_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
496
497         while (1) {
498                 if (pad == NULL) {
499                         ret = -EPIPE;
500                         goto out;
501                 }
502
503                 /* We've reached a video node, that shouldn't have happened. */
504                 if (!is_media_entity_v4l2_subdev(pad->entity)) {
505                         ret = -EPIPE;
506                         goto out;
507                 }
508
509                 entity = to_vsp1_entity(
510                         media_entity_to_v4l2_subdev(pad->entity));
511
512                 /*
513                  * A BRU is present in the pipeline, store the BRU input pad
514                  * number in the input RPF for use when configuring the RPF.
515                  */
516                 if (entity->type == VSP1_ENTITY_BRU) {
517                         struct vsp1_bru *bru = to_bru(&entity->subdev);
518
519                         bru->inputs[pad->index].rpf = input;
520                         input->bru_input = pad->index;
521
522                         bru_found = true;
523                 }
524
525                 /* We've reached the WPF, we're done. */
526                 if (entity->type == VSP1_ENTITY_WPF)
527                         break;
528
529                 /* Ensure the branch has no loop. */
530                 if (media_entity_enum_test_and_set(&ent_enum,
531                                                    &entity->subdev.entity)) {
532                         ret = -EPIPE;
533                         goto out;
534                 }
535
536                 /* UDS can't be chained. */
537                 if (entity->type == VSP1_ENTITY_UDS) {
538                         if (pipe->uds) {
539                                 ret = -EPIPE;
540                                 goto out;
541                         }
542
543                         pipe->uds = entity;
544                         pipe->uds_input = bru_found ? pipe->bru
545                                         : &input->entity;
546                 }
547
548                 /* Follow the source link, ignoring any HGO or HGT. */
549                 pad = &entity->pads[entity->source_pad];
550                 pad = vsp1_entity_remote_pad(pad);
551         }
552
553         /* The last entity must be the output WPF. */
554         if (entity != &output->entity)
555                 ret = -EPIPE;
556
557 out:
558         media_entity_enum_cleanup(&ent_enum);
559
560         return ret;
561 }
562
563 static int vsp1_video_pipeline_build(struct vsp1_pipeline *pipe,
564                                      struct vsp1_video *video)
565 {
566         struct media_graph graph;
567         struct media_entity *entity = &video->video.entity;
568         struct media_device *mdev = entity->graph_obj.mdev;
569         unsigned int i;
570         int ret;
571
572         /* Walk the graph to locate the entities and video nodes. */
573         ret = media_graph_walk_init(&graph, mdev);
574         if (ret)
575                 return ret;
576
577         media_graph_walk_start(&graph, entity);
578
579         while ((entity = media_graph_walk_next(&graph))) {
580                 struct v4l2_subdev *subdev;
581                 struct vsp1_rwpf *rwpf;
582                 struct vsp1_entity *e;
583
584                 if (!is_media_entity_v4l2_subdev(entity))
585                         continue;
586
587                 subdev = media_entity_to_v4l2_subdev(entity);
588                 e = to_vsp1_entity(subdev);
589                 list_add_tail(&e->list_pipe, &pipe->entities);
590
591                 if (e->type == VSP1_ENTITY_RPF) {
592                         rwpf = to_rwpf(subdev);
593                         pipe->inputs[rwpf->entity.index] = rwpf;
594                         rwpf->video->pipe_index = ++pipe->num_inputs;
595                         rwpf->pipe = pipe;
596                 } else if (e->type == VSP1_ENTITY_WPF) {
597                         rwpf = to_rwpf(subdev);
598                         pipe->output = rwpf;
599                         rwpf->video->pipe_index = 0;
600                         rwpf->pipe = pipe;
601                 } else if (e->type == VSP1_ENTITY_LIF) {
602                         pipe->lif = e;
603                 } else if (e->type == VSP1_ENTITY_BRU) {
604                         pipe->bru = e;
605                 } else if (e->type == VSP1_ENTITY_HGO) {
606                         struct vsp1_hgo *hgo = to_hgo(subdev);
607
608                         pipe->hgo = e;
609                         hgo->histo.pipe = pipe;
610                 }
611         }
612
613         media_graph_walk_cleanup(&graph);
614
615         /* We need one output and at least one input. */
616         if (pipe->num_inputs == 0 || !pipe->output)
617                 return -EPIPE;
618
619         /*
620          * Follow links downstream for each input and make sure the graph
621          * contains no loop and that all branches end at the output WPF.
622          */
623         for (i = 0; i < video->vsp1->info->rpf_count; ++i) {
624                 if (!pipe->inputs[i])
625                         continue;
626
627                 ret = vsp1_video_pipeline_build_branch(pipe, pipe->inputs[i],
628                                                        pipe->output);
629                 if (ret < 0)
630                         return ret;
631         }
632
633         return 0;
634 }
635
636 static int vsp1_video_pipeline_init(struct vsp1_pipeline *pipe,
637                                     struct vsp1_video *video)
638 {
639         vsp1_pipeline_init(pipe);
640
641         pipe->frame_end = vsp1_video_pipeline_frame_end;
642
643         return vsp1_video_pipeline_build(pipe, video);
644 }
645
646 static struct vsp1_pipeline *vsp1_video_pipeline_get(struct vsp1_video *video)
647 {
648         struct vsp1_pipeline *pipe;
649         int ret;
650
651         /*
652          * Get a pipeline object for the video node. If a pipeline has already
653          * been allocated just increment its reference count and return it.
654          * Otherwise allocate a new pipeline and initialize it, it will be freed
655          * when the last reference is released.
656          */
657         if (!video->rwpf->pipe) {
658                 pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
659                 if (!pipe)
660                         return ERR_PTR(-ENOMEM);
661
662                 ret = vsp1_video_pipeline_init(pipe, video);
663                 if (ret < 0) {
664                         vsp1_pipeline_reset(pipe);
665                         kfree(pipe);
666                         return ERR_PTR(ret);
667                 }
668         } else {
669                 pipe = video->rwpf->pipe;
670                 kref_get(&pipe->kref);
671         }
672
673         return pipe;
674 }
675
676 static void vsp1_video_pipeline_release(struct kref *kref)
677 {
678         struct vsp1_pipeline *pipe = container_of(kref, typeof(*pipe), kref);
679
680         vsp1_pipeline_reset(pipe);
681         kfree(pipe);
682 }
683
684 static void vsp1_video_pipeline_put(struct vsp1_pipeline *pipe)
685 {
686         struct media_device *mdev = &pipe->output->entity.vsp1->media_dev;
687
688         mutex_lock(&mdev->graph_mutex);
689         kref_put(&pipe->kref, vsp1_video_pipeline_release);
690         mutex_unlock(&mdev->graph_mutex);
691 }
692
693 /* -----------------------------------------------------------------------------
694  * videobuf2 Queue Operations
695  */
696
697 static int
698 vsp1_video_queue_setup(struct vb2_queue *vq,
699                        unsigned int *nbuffers, unsigned int *nplanes,
700                        unsigned int sizes[], struct device *alloc_devs[])
701 {
702         struct vsp1_video *video = vb2_get_drv_priv(vq);
703         const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
704         unsigned int i;
705
706         if (*nplanes) {
707                 if (*nplanes != format->num_planes)
708                         return -EINVAL;
709
710                 for (i = 0; i < *nplanes; i++)
711                         if (sizes[i] < format->plane_fmt[i].sizeimage)
712                                 return -EINVAL;
713                 return 0;
714         }
715
716         *nplanes = format->num_planes;
717
718         for (i = 0; i < format->num_planes; ++i)
719                 sizes[i] = format->plane_fmt[i].sizeimage;
720
721         return 0;
722 }
723
724 static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
725 {
726         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
727         struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
728         struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
729         const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
730         unsigned int i;
731
732         if (vb->num_planes < format->num_planes)
733                 return -EINVAL;
734
735         for (i = 0; i < vb->num_planes; ++i) {
736                 buf->mem.addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
737
738                 if (vb2_plane_size(vb, i) < format->plane_fmt[i].sizeimage)
739                         return -EINVAL;
740         }
741
742         for ( ; i < 3; ++i)
743                 buf->mem.addr[i] = 0;
744
745         return 0;
746 }
747
748 static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
749 {
750         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
751         struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
752         struct vsp1_pipeline *pipe = video->rwpf->pipe;
753         struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
754         unsigned long flags;
755         bool empty;
756
757         spin_lock_irqsave(&video->irqlock, flags);
758         empty = list_empty(&video->irqqueue);
759         list_add_tail(&buf->queue, &video->irqqueue);
760         spin_unlock_irqrestore(&video->irqlock, flags);
761
762         if (!empty)
763                 return;
764
765         spin_lock_irqsave(&pipe->irqlock, flags);
766
767         video->rwpf->mem = buf->mem;
768         pipe->buffers_ready |= 1 << video->pipe_index;
769
770         if (vb2_is_streaming(&video->queue) &&
771             vsp1_pipeline_ready(pipe))
772                 vsp1_video_pipeline_run(pipe);
773
774         spin_unlock_irqrestore(&pipe->irqlock, flags);
775 }
776
777 static int vsp1_video_setup_pipeline(struct vsp1_pipeline *pipe)
778 {
779         struct vsp1_entity *entity;
780
781         /* Determine this pipelines sizes for image partitioning support. */
782         vsp1_video_pipeline_setup_partitions(pipe);
783
784         /* Prepare the display list. */
785         pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
786         if (!pipe->dl)
787                 return -ENOMEM;
788
789         if (pipe->uds) {
790                 struct vsp1_uds *uds = to_uds(&pipe->uds->subdev);
791
792                 /*
793                  * If a BRU is present in the pipeline before the UDS, the alpha
794                  * component doesn't need to be scaled as the BRU output alpha
795                  * value is fixed to 255. Otherwise we need to scale the alpha
796                  * component only when available at the input RPF.
797                  */
798                 if (pipe->uds_input->type == VSP1_ENTITY_BRU) {
799                         uds->scale_alpha = false;
800                 } else {
801                         struct vsp1_rwpf *rpf =
802                                 to_rwpf(&pipe->uds_input->subdev);
803
804                         uds->scale_alpha = rpf->fmtinfo->alpha;
805                 }
806         }
807
808         list_for_each_entry(entity, &pipe->entities, list_pipe) {
809                 vsp1_entity_route_setup(entity, pipe, pipe->dl);
810
811                 if (entity->ops->configure)
812                         entity->ops->configure(entity, pipe, pipe->dl,
813                                                VSP1_ENTITY_PARAMS_INIT);
814         }
815
816         return 0;
817 }
818
819 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
820 {
821         struct vsp1_video *video = vb2_get_drv_priv(vq);
822         struct vsp1_pipeline *pipe = video->rwpf->pipe;
823         bool start_pipeline = false;
824         unsigned long flags;
825         int ret;
826
827         mutex_lock(&pipe->lock);
828         if (pipe->stream_count == pipe->num_inputs) {
829                 ret = vsp1_video_setup_pipeline(pipe);
830                 if (ret < 0) {
831                         mutex_unlock(&pipe->lock);
832                         return ret;
833                 }
834
835                 start_pipeline = true;
836         }
837
838         pipe->stream_count++;
839         mutex_unlock(&pipe->lock);
840
841         /*
842          * vsp1_pipeline_ready() is not sufficient to establish that all streams
843          * are prepared and the pipeline is configured, as multiple streams
844          * can race through streamon with buffers already queued; Therefore we
845          * don't even attempt to start the pipeline until the last stream has
846          * called through here.
847          */
848         if (!start_pipeline)
849                 return 0;
850
851         spin_lock_irqsave(&pipe->irqlock, flags);
852         if (vsp1_pipeline_ready(pipe))
853                 vsp1_video_pipeline_run(pipe);
854         spin_unlock_irqrestore(&pipe->irqlock, flags);
855
856         return 0;
857 }
858
859 static void vsp1_video_stop_streaming(struct vb2_queue *vq)
860 {
861         struct vsp1_video *video = vb2_get_drv_priv(vq);
862         struct vsp1_pipeline *pipe = video->rwpf->pipe;
863         struct vsp1_vb2_buffer *buffer;
864         unsigned long flags;
865         int ret;
866
867         /*
868          * Clear the buffers ready flag to make sure the device won't be started
869          * by a QBUF on the video node on the other side of the pipeline.
870          */
871         spin_lock_irqsave(&video->irqlock, flags);
872         pipe->buffers_ready &= ~(1 << video->pipe_index);
873         spin_unlock_irqrestore(&video->irqlock, flags);
874
875         mutex_lock(&pipe->lock);
876         if (--pipe->stream_count == pipe->num_inputs) {
877                 /* Stop the pipeline. */
878                 ret = vsp1_pipeline_stop(pipe);
879                 if (ret == -ETIMEDOUT)
880                         dev_err(video->vsp1->dev, "pipeline stop timeout\n");
881
882                 vsp1_dl_list_put(pipe->dl);
883                 pipe->dl = NULL;
884         }
885         mutex_unlock(&pipe->lock);
886
887         media_pipeline_stop(&video->video.entity);
888         vsp1_video_pipeline_put(pipe);
889
890         /* Remove all buffers from the IRQ queue. */
891         spin_lock_irqsave(&video->irqlock, flags);
892         list_for_each_entry(buffer, &video->irqqueue, queue)
893                 vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR);
894         INIT_LIST_HEAD(&video->irqqueue);
895         spin_unlock_irqrestore(&video->irqlock, flags);
896 }
897
898 static const struct vb2_ops vsp1_video_queue_qops = {
899         .queue_setup = vsp1_video_queue_setup,
900         .buf_prepare = vsp1_video_buffer_prepare,
901         .buf_queue = vsp1_video_buffer_queue,
902         .wait_prepare = vb2_ops_wait_prepare,
903         .wait_finish = vb2_ops_wait_finish,
904         .start_streaming = vsp1_video_start_streaming,
905         .stop_streaming = vsp1_video_stop_streaming,
906 };
907
908 /* -----------------------------------------------------------------------------
909  * V4L2 ioctls
910  */
911
912 static int
913 vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
914 {
915         struct v4l2_fh *vfh = file->private_data;
916         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
917
918         cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
919                           | V4L2_CAP_VIDEO_CAPTURE_MPLANE
920                           | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
921
922         if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
923                 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
924                                  | V4L2_CAP_STREAMING;
925         else
926                 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
927                                  | V4L2_CAP_STREAMING;
928
929         strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
930         strlcpy(cap->card, video->video.name, sizeof(cap->card));
931         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
932                  dev_name(video->vsp1->dev));
933
934         return 0;
935 }
936
937 static int
938 vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
939 {
940         struct v4l2_fh *vfh = file->private_data;
941         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
942
943         if (format->type != video->queue.type)
944                 return -EINVAL;
945
946         mutex_lock(&video->lock);
947         format->fmt.pix_mp = video->rwpf->format;
948         mutex_unlock(&video->lock);
949
950         return 0;
951 }
952
953 static int
954 vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
955 {
956         struct v4l2_fh *vfh = file->private_data;
957         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
958
959         if (format->type != video->queue.type)
960                 return -EINVAL;
961
962         return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
963 }
964
965 static int
966 vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
967 {
968         struct v4l2_fh *vfh = file->private_data;
969         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
970         const struct vsp1_format_info *info;
971         int ret;
972
973         if (format->type != video->queue.type)
974                 return -EINVAL;
975
976         ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
977         if (ret < 0)
978                 return ret;
979
980         mutex_lock(&video->lock);
981
982         if (vb2_is_busy(&video->queue)) {
983                 ret = -EBUSY;
984                 goto done;
985         }
986
987         video->rwpf->format = format->fmt.pix_mp;
988         video->rwpf->fmtinfo = info;
989
990 done:
991         mutex_unlock(&video->lock);
992         return ret;
993 }
994
995 static int
996 vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
997 {
998         struct v4l2_fh *vfh = file->private_data;
999         struct vsp1_video *video = to_vsp1_video(vfh->vdev);
1000         struct media_device *mdev = &video->vsp1->media_dev;
1001         struct vsp1_pipeline *pipe;
1002         int ret;
1003
1004         if (video->queue.owner && video->queue.owner != file->private_data)
1005                 return -EBUSY;
1006
1007         /*
1008          * Get a pipeline for the video node and start streaming on it. No link
1009          * touching an entity in the pipeline can be activated or deactivated
1010          * once streaming is started.
1011          */
1012         mutex_lock(&mdev->graph_mutex);
1013
1014         pipe = vsp1_video_pipeline_get(video);
1015         if (IS_ERR(pipe)) {
1016                 mutex_unlock(&mdev->graph_mutex);
1017                 return PTR_ERR(pipe);
1018         }
1019
1020         ret = __media_pipeline_start(&video->video.entity, &pipe->pipe);
1021         if (ret < 0) {
1022                 mutex_unlock(&mdev->graph_mutex);
1023                 goto err_pipe;
1024         }
1025
1026         mutex_unlock(&mdev->graph_mutex);
1027
1028         /*
1029          * Verify that the configured format matches the output of the connected
1030          * subdev.
1031          */
1032         ret = vsp1_video_verify_format(video);
1033         if (ret < 0)
1034                 goto err_stop;
1035
1036         /* Start the queue. */
1037         ret = vb2_streamon(&video->queue, type);
1038         if (ret < 0)
1039                 goto err_stop;
1040
1041         return 0;
1042
1043 err_stop:
1044         media_pipeline_stop(&video->video.entity);
1045 err_pipe:
1046         vsp1_video_pipeline_put(pipe);
1047         return ret;
1048 }
1049
1050 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
1051         .vidioc_querycap                = vsp1_video_querycap,
1052         .vidioc_g_fmt_vid_cap_mplane    = vsp1_video_get_format,
1053         .vidioc_s_fmt_vid_cap_mplane    = vsp1_video_set_format,
1054         .vidioc_try_fmt_vid_cap_mplane  = vsp1_video_try_format,
1055         .vidioc_g_fmt_vid_out_mplane    = vsp1_video_get_format,
1056         .vidioc_s_fmt_vid_out_mplane    = vsp1_video_set_format,
1057         .vidioc_try_fmt_vid_out_mplane  = vsp1_video_try_format,
1058         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
1059         .vidioc_querybuf                = vb2_ioctl_querybuf,
1060         .vidioc_qbuf                    = vb2_ioctl_qbuf,
1061         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
1062         .vidioc_expbuf                  = vb2_ioctl_expbuf,
1063         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
1064         .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
1065         .vidioc_streamon                = vsp1_video_streamon,
1066         .vidioc_streamoff               = vb2_ioctl_streamoff,
1067 };
1068
1069 /* -----------------------------------------------------------------------------
1070  * V4L2 File Operations
1071  */
1072
1073 static int vsp1_video_open(struct file *file)
1074 {
1075         struct vsp1_video *video = video_drvdata(file);
1076         struct v4l2_fh *vfh;
1077         int ret = 0;
1078
1079         vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
1080         if (vfh == NULL)
1081                 return -ENOMEM;
1082
1083         v4l2_fh_init(vfh, &video->video);
1084         v4l2_fh_add(vfh);
1085
1086         file->private_data = vfh;
1087
1088         ret = vsp1_device_get(video->vsp1);
1089         if (ret < 0) {
1090                 v4l2_fh_del(vfh);
1091                 v4l2_fh_exit(vfh);
1092                 kfree(vfh);
1093         }
1094
1095         return ret;
1096 }
1097
1098 static int vsp1_video_release(struct file *file)
1099 {
1100         struct vsp1_video *video = video_drvdata(file);
1101         struct v4l2_fh *vfh = file->private_data;
1102
1103         mutex_lock(&video->lock);
1104         if (video->queue.owner == vfh) {
1105                 vb2_queue_release(&video->queue);
1106                 video->queue.owner = NULL;
1107         }
1108         mutex_unlock(&video->lock);
1109
1110         vsp1_device_put(video->vsp1);
1111
1112         v4l2_fh_release(file);
1113
1114         file->private_data = NULL;
1115
1116         return 0;
1117 }
1118
1119 static const struct v4l2_file_operations vsp1_video_fops = {
1120         .owner = THIS_MODULE,
1121         .unlocked_ioctl = video_ioctl2,
1122         .open = vsp1_video_open,
1123         .release = vsp1_video_release,
1124         .poll = vb2_fop_poll,
1125         .mmap = vb2_fop_mmap,
1126 };
1127
1128 /* -----------------------------------------------------------------------------
1129  * Initialization and Cleanup
1130  */
1131
1132 struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1,
1133                                      struct vsp1_rwpf *rwpf)
1134 {
1135         struct vsp1_video *video;
1136         const char *direction;
1137         int ret;
1138
1139         video = devm_kzalloc(vsp1->dev, sizeof(*video), GFP_KERNEL);
1140         if (!video)
1141                 return ERR_PTR(-ENOMEM);
1142
1143         rwpf->video = video;
1144
1145         video->vsp1 = vsp1;
1146         video->rwpf = rwpf;
1147
1148         if (rwpf->entity.type == VSP1_ENTITY_RPF) {
1149                 direction = "input";
1150                 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1151                 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1152                 video->video.vfl_dir = VFL_DIR_TX;
1153         } else {
1154                 direction = "output";
1155                 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1156                 video->pad.flags = MEDIA_PAD_FL_SINK;
1157                 video->video.vfl_dir = VFL_DIR_RX;
1158         }
1159
1160         mutex_init(&video->lock);
1161         spin_lock_init(&video->irqlock);
1162         INIT_LIST_HEAD(&video->irqqueue);
1163
1164         /* Initialize the media entity... */
1165         ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1166         if (ret < 0)
1167                 return ERR_PTR(ret);
1168
1169         /* ... and the format ... */
1170         rwpf->format.pixelformat = VSP1_VIDEO_DEF_FORMAT;
1171         rwpf->format.width = VSP1_VIDEO_DEF_WIDTH;
1172         rwpf->format.height = VSP1_VIDEO_DEF_HEIGHT;
1173         __vsp1_video_try_format(video, &rwpf->format, &rwpf->fmtinfo);
1174
1175         /* ... and the video node... */
1176         video->video.v4l2_dev = &video->vsp1->v4l2_dev;
1177         video->video.fops = &vsp1_video_fops;
1178         snprintf(video->video.name, sizeof(video->video.name), "%s %s",
1179                  rwpf->entity.subdev.name, direction);
1180         video->video.vfl_type = VFL_TYPE_GRABBER;
1181         video->video.release = video_device_release_empty;
1182         video->video.ioctl_ops = &vsp1_video_ioctl_ops;
1183
1184         video_set_drvdata(&video->video, video);
1185
1186         video->queue.type = video->type;
1187         video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1188         video->queue.lock = &video->lock;
1189         video->queue.drv_priv = video;
1190         video->queue.buf_struct_size = sizeof(struct vsp1_vb2_buffer);
1191         video->queue.ops = &vsp1_video_queue_qops;
1192         video->queue.mem_ops = &vb2_dma_contig_memops;
1193         video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1194         video->queue.dev = video->vsp1->dev;
1195         ret = vb2_queue_init(&video->queue);
1196         if (ret < 0) {
1197                 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1198                 goto error;
1199         }
1200
1201         /* ... and register the video device. */
1202         video->video.queue = &video->queue;
1203         ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1204         if (ret < 0) {
1205                 dev_err(video->vsp1->dev, "failed to register video device\n");
1206                 goto error;
1207         }
1208
1209         return video;
1210
1211 error:
1212         vsp1_video_cleanup(video);
1213         return ERR_PTR(ret);
1214 }
1215
1216 void vsp1_video_cleanup(struct vsp1_video *video)
1217 {
1218         if (video_is_registered(&video->video))
1219                 video_unregister_device(&video->video);
1220
1221         media_entity_cleanup(&video->video.entity);
1222 }